blob: f4fa10ffac8cf71b799ed623f7e4d7a6ef5dd26c [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) {
321 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500322 if (!restoredAppsFinal.isEmpty()) {
323 callbacks.bindAppsUpdated(restoredAppsFinal);
324 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800325 }
326 }
327 });
328 }
329 };
330 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700331 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800332
333 public void addAndBindAddedWorkspaceApps(final Context context,
334 final ArrayList<ItemInfo> workspaceApps) {
335 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
336
337 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800338 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
339 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800340 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700341 return;
Winson Chung997a9232013-07-24 15:33:46 -0700342 }
Winson Chung64359a52013-07-08 17:17:08 -0700343 // Process the newly added applications and add them to the database first
344 Runnable r = new Runnable() {
345 public void run() {
346 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
347 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500348 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700349
Winson Chung76828c82013-08-19 15:43:29 -0700350 // Get the list of workspace screens. We need to append to this list and
351 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
352 // called.
353 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
354 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
355 for (Integer i : orderedScreens.keySet()) {
356 long screenId = orderedScreens.get(i);
357 workspaceScreens.add(screenId);
358 }
359
Winson Chung64359a52013-07-08 17:17:08 -0700360 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700361 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700362 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700363 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700364 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700365 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700366
367 // Short-circuit this logic if the icon exists somewhere on the workspace
368 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500369 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
370 if (a instanceof AppInfo &&
371 LauncherModel.appWasRestored(context, launchIntent)) {
372 restoredAppsFinal.add((AppInfo) a);
373 }
Winson Chung64359a52013-07-08 17:17:08 -0700374 continue;
375 }
376
Winson Chung87412982013-10-03 18:34:14 -0700377 // Add this icon to the db, creating a new page if necessary. If there
378 // is only the empty page then we just add items to the first page.
379 // Otherwise, we add them to the next pages.
380 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700381 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700382 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700383 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200384 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700385
386 // If we can't find a valid position, then just add a new screen.
387 // This takes time so we need to re-queue the add until the new
388 // page is added. Create as many screens as necessary to satisfy
389 // the startSearchPageIndex.
390 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700391 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700392 while (numPagesToAdd > 0) {
393 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700394 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700395 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700396 addedWorkspaceScreensFinal.add(screenId);
397 numPagesToAdd--;
398 }
Winson Chung76828c82013-08-19 15:43:29 -0700399
Winson Chung64359a52013-07-08 17:17:08 -0700400 // Find the coordinate again
401 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700402 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700403 }
404 if (coords == null) {
405 throw new RuntimeException("Coordinates should not be null");
406 }
407
Winson Chung997a9232013-07-24 15:33:46 -0700408 ShortcutInfo shortcutInfo;
409 if (a instanceof ShortcutInfo) {
410 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200411 } else if (a instanceof AppInfo) {
412 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700413 } else {
414 throw new RuntimeException("Unexpected info type");
415 }
Winson Chung94d67682013-09-25 16:29:40 -0700416
Winson Chung64359a52013-07-08 17:17:08 -0700417 // Add the shortcut to the db
418 addItemToDatabase(context, shortcutInfo,
419 LauncherSettings.Favorites.CONTAINER_DESKTOP,
420 coords.first, coords.second[0], coords.second[1], false);
421 // Save the ShortcutInfo for binding in the workspace
422 addedShortcutsFinal.add(shortcutInfo);
423 }
424 }
425
Winson Chung76828c82013-08-19 15:43:29 -0700426 // Update the workspace screens
427 updateWorkspaceScreenOrder(context, workspaceScreens);
428
Adam Cohen76a47a12014-02-05 11:47:43 -0800429 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700430 runOnMainThread(new Runnable() {
431 public void run() {
432 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
433 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700434 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
435 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700436 if (!addedShortcutsFinal.isEmpty()) {
437 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
438 long lastScreenId = info.screenId;
439 for (ItemInfo i : addedShortcutsFinal) {
440 if (i.screenId == lastScreenId) {
441 addAnimated.add(i);
442 } else {
443 addNotAnimated.add(i);
444 }
Winson Chung997a9232013-07-24 15:33:46 -0700445 }
446 }
Winson Chungd64d1762013-08-20 14:37:16 -0700447 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800448 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500449 if (!restoredAppsFinal.isEmpty()) {
450 callbacks.bindAppsUpdated(restoredAppsFinal);
451 }
Winson Chung997a9232013-07-24 15:33:46 -0700452 }
Winson Chung64359a52013-07-08 17:17:08 -0700453 }
Winson Chung997a9232013-07-24 15:33:46 -0700454 });
455 }
Winson Chung64359a52013-07-08 17:17:08 -0700456 }
457 };
458 runOnWorkerThread(r);
459 }
460
Joe Onorato56d82912010-03-07 14:32:10 -0500461 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700462 if (mDefaultIcon == null) {
463 final Context context = LauncherAppState.getInstance().getContext();
464 mDefaultIcon = Utilities.createIconBitmap(
465 mIconCache.getFullResDefaultActivityIcon(), context);
466 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800467 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469
Winson Chung81b52252012-08-27 15:34:29 -0700470 public void unbindItemInfosAndClearQueuedBindRunnables() {
471 if (sWorkerThread.getThreadId() == Process.myTid()) {
472 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
473 "main thread");
474 }
475
476 // Clear any deferred bind runnables
477 mDeferredBindRunnables.clear();
478 // Remove any queued bind runnables
479 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
480 // Unbind all the workspace items
481 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700482 }
483
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700484 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700485 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700486 // Ensure that we don't use the same workspace items data structure on the main thread
487 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700488 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
489 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700490 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700491 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
492 tmpAppWidgets.addAll(sBgAppWidgets);
493 }
494 Runnable r = new Runnable() {
495 @Override
496 public void run() {
497 for (ItemInfo item : tmpWorkspaceItems) {
498 item.unbind();
499 }
500 for (ItemInfo item : tmpAppWidgets) {
501 item.unbind();
502 }
503 }
504 };
505 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700506 }
507
Joe Onorato9c1289c2009-08-17 11:03:03 -0400508 /**
509 * Adds an item to the DB if it was not created previously, or move it to a new
510 * <container, screen, cellX, cellY>
511 */
512 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700513 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 if (item.container == ItemInfo.NO_ID) {
515 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700516 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 } else {
518 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700519 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
521 }
522
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700523 static void checkItemInfoLocked(
524 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
525 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
526 if (modelItem != null && item != modelItem) {
527 // check all the data is consistent
528 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
529 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
530 ShortcutInfo shortcut = (ShortcutInfo) item;
531 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
532 modelShortcut.intent.filterEquals(shortcut.intent) &&
533 modelShortcut.id == shortcut.id &&
534 modelShortcut.itemType == shortcut.itemType &&
535 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700536 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700537 modelShortcut.cellX == shortcut.cellX &&
538 modelShortcut.cellY == shortcut.cellY &&
539 modelShortcut.spanX == shortcut.spanX &&
540 modelShortcut.spanY == shortcut.spanY &&
541 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
542 (modelShortcut.dropPos != null &&
543 shortcut.dropPos != null &&
544 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
545 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
546 // For all intents and purposes, this is the same object
547 return;
548 }
549 }
550
551 // the modelItem needs to match up perfectly with item if our model is
552 // to be consistent with the database-- for now, just require
553 // modelItem == item or the equality check above
554 String msg = "item: " + ((item != null) ? item.toString() : "null") +
555 "modelItem: " +
556 ((modelItem != null) ? modelItem.toString() : "null") +
557 "Error: ItemInfo passed to checkItemInfo doesn't match original";
558 RuntimeException e = new RuntimeException(msg);
559 if (stackTrace != null) {
560 e.setStackTrace(stackTrace);
561 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800562 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700563 }
564 }
565
Michael Jurka816474f2012-06-25 14:49:02 -0700566 static void checkItemInfo(final ItemInfo item) {
567 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
568 final long itemId = item.id;
569 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700570 public void run() {
571 synchronized (sBgLock) {
572 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700573 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700574 }
575 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700576 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700577 }
578
Michael Jurkac9d95c52011-08-29 14:03:34 -0700579 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
580 final ItemInfo item, final String callingFunction) {
581 final long itemId = item.id;
582 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
583 final ContentResolver cr = context.getContentResolver();
584
Adam Cohen487f7dd2012-06-28 18:12:10 -0700585 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700586 Runnable r = new Runnable() {
587 public void run() {
588 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700589 updateItemArrays(item, itemId, stackTrace);
590 }
591 };
592 runOnWorkerThread(r);
593 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700594
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700595 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
596 final ArrayList<ItemInfo> items, final String callingFunction) {
597 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700598
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700599 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
600 Runnable r = new Runnable() {
601 public void run() {
602 ArrayList<ContentProviderOperation> ops =
603 new ArrayList<ContentProviderOperation>();
604 int count = items.size();
605 for (int i = 0; i < count; i++) {
606 ItemInfo item = items.get(i);
607 final long itemId = item.id;
608 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
609 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700610
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700611 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
612 updateItemArrays(item, itemId, stackTrace);
613
614 }
615 try {
616 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
617 } catch (Exception e) {
618 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700619 }
620 }
621 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700622 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700623 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700624
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700625 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
626 // Lock on mBgLock *after* the db operation
627 synchronized (sBgLock) {
628 checkItemInfoLocked(itemId, item, stackTrace);
629
630 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
631 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
632 // Item is in a folder, make sure this folder exists
633 if (!sBgFolders.containsKey(item.container)) {
634 // An items container is being set to a that of an item which is not in
635 // the list of Folders.
636 String msg = "item: " + item + " container being set to: " +
637 item.container + ", not in the list of folders";
638 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 }
640 }
641
642 // Items are added/removed from the corresponding FolderInfo elsewhere, such
643 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
644 // that are on the desktop, as appropriate
645 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800646 if (modelItem != null &&
647 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
648 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 switch (modelItem.itemType) {
650 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
651 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
652 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
653 if (!sBgWorkspaceItems.contains(modelItem)) {
654 sBgWorkspaceItems.add(modelItem);
655 }
656 break;
657 default:
658 break;
659 }
660 } else {
661 sBgWorkspaceItems.remove(modelItem);
662 }
663 }
664 }
665
Michael Jurkac7700af2013-05-14 20:17:58 +0200666 public void flushWorkerThread() {
667 mFlushingWorkerThread = true;
668 Runnable waiter = new Runnable() {
669 public void run() {
670 synchronized (this) {
671 notifyAll();
672 mFlushingWorkerThread = false;
673 }
674 }
675 };
676
677 synchronized(waiter) {
678 runOnWorkerThread(waiter);
679 if (mLoaderTask != null) {
680 synchronized(mLoaderTask) {
681 mLoaderTask.notify();
682 }
683 }
684 boolean success = false;
685 while (!success) {
686 try {
687 waiter.wait();
688 success = true;
689 } catch (InterruptedException e) {
690 }
691 }
692 }
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700697 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700698 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 item.cellX = cellX;
702 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700703
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 // We store hotseat items in canonical form which is this orientation invariant position
705 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700707 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700709 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712
713 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
716 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718
Michael Jurkac9d95c52011-08-29 14:03:34 -0700719 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700720 }
721
722 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700723 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
724 * cellX, cellY have already been updated on the ItemInfos.
725 */
726 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
727 final long container, final int screen) {
728
729 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
730 int count = items.size();
731
732 for (int i = 0; i < count; i++) {
733 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700734 item.container = container;
735
736 // We store hotseat items in canonical form which is this orientation invariant position
737 // in the hotseat
738 if (context instanceof Launcher && screen < 0 &&
739 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700741 item.cellY);
742 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 }
745
746 final ContentValues values = new ContentValues();
747 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
748 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
749 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751
752 contentValues.add(values);
753 }
754 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
755 }
756
757 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700758 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800759 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700760 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700761 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700762 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800763 item.cellX = cellX;
764 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700765 item.spanX = spanX;
766 item.spanY = spanY;
767
768 // We store hotseat items in canonical form which is this orientation invariant position
769 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700770 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700771 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700772 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700773 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 }
Adam Cohend4844c32011-02-18 19:25:06 -0800776
Adam Cohend4844c32011-02-18 19:25:06 -0800777 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800778 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700779 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
780 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
781 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
782 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800784
Michael Jurka816474f2012-06-25 14:49:02 -0700785 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700787
788 /**
789 * Update an item to the database in a specified container.
790 */
791 static void updateItemInDatabase(Context context, final ItemInfo item) {
792 final ContentValues values = new ContentValues();
793 item.onAddToDatabase(values);
794 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
795 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800796 }
797
798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 * Returns true if the shortcuts already exists in the database.
800 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 static boolean shortcutExists(Context context, String title, Intent intent) {
803 final ContentResolver cr = context.getContentResolver();
804 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
805 new String[] { "title", "intent" }, "title=? and intent=?",
806 new String[] { title, intent.toUri(0) }, null);
807 boolean result = false;
808 try {
809 result = c.moveToFirst();
810 } finally {
811 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700814 }
815
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500817 * Returns true if the shortcuts already exists in the database.
818 * we identify a shortcut by the component name of the intent.
819 */
820 static boolean appWasRestored(Context context, Intent intent) {
821 final ContentResolver cr = context.getContentResolver();
822 final ComponentName component = intent.getComponent();
823 if (component == null) {
824 return false;
825 }
826 String componentName = component.flattenToString();
827 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
828 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
829 new String[]{"intent", "restored"}, where, null, null);
830 boolean result = false;
831 try {
832 result = c.moveToFirst();
833 } finally {
834 c.close();
835 }
836 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
837 return result;
838 }
839
840 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700841 * Returns an ItemInfo array containing all the items in the LauncherModel.
842 * The ItemInfo.id is not set through this function.
843 */
844 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
845 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
846 final ContentResolver cr = context.getContentResolver();
847 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
848 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
849 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
850 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
851
852 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
853 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
854 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
855 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
856 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
857 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
858 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
859
860 try {
861 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700863 item.cellX = c.getInt(cellXIndex);
864 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700865 item.spanX = Math.max(1, c.getInt(spanXIndex));
866 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700867 item.container = c.getInt(containerIndex);
868 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
871 items.add(item);
872 }
873 } catch (Exception e) {
874 items.clear();
875 } finally {
876 c.close();
877 }
878
879 return items;
880 }
881
882 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
884 */
885 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
886 final ContentResolver cr = context.getContentResolver();
887 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
888 "_id=? and (itemType=? or itemType=?)",
889 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700890 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700891
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 try {
893 if (c.moveToFirst()) {
894 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
895 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
896 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
897 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
898 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
899 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 FolderInfo folderInfo = null;
902 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700903 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
904 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700906 }
907
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 folderInfo.title = c.getString(titleIndex);
909 folderInfo.id = id;
910 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700911 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700912 folderInfo.cellX = c.getInt(cellXIndex);
913 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
915 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700916 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 } finally {
918 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700919 }
920
921 return null;
922 }
923
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 /**
925 * Add an item to the database in a specified container. Sets the container, screen, cellX and
926 * cellY fields of the item. Also assigns an ID to the item.
927 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700928 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700929 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 item.cellX = cellX;
932 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 // We store hotseat items in canonical form which is this orientation invariant position
934 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 final ContentValues values = new ContentValues();
943 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 item.onAddToDatabase(values);
945
Michael Jurka414300a2013-08-27 15:42:35 +0200946 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700949
Michael Jurkac9d95c52011-08-29 14:03:34 -0700950 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 public void run() {
952 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
953 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700956 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700957 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 sBgItemsIdMap.put(item.id, item);
959 switch (item.itemType) {
960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
961 sBgFolders.put(item.id, (FolderInfo) item);
962 // Fall through
963 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
964 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
965 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
966 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
967 sBgWorkspaceItems.add(item);
968 } else {
969 if (!sBgFolders.containsKey(item.container)) {
970 // Adding an item to a folder that doesn't exist.
971 String msg = "adding item: " + item + " to a folder that " +
972 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700973 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700975 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700976 break;
977 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
978 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
979 break;
980 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700981 }
982 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700983 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
986
Joe Onorato9c1289c2009-08-17 11:03:03 -0400987 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700988 * Creates a new unique child id, for a given cell span across all layouts.
989 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700990 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700994 }
995
Winson Chungaafa03c2010-06-11 17:34:16 -0700996 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700997 * Removes the specified item from the database
998 * @param context
999 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001001 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001003 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001004
Michael Jurka83df1882011-08-31 20:59:26 -07001005 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008
1009 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001010 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001011 switch (item.itemType) {
1012 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1013 sBgFolders.remove(item.id);
1014 for (ItemInfo info: sBgItemsIdMap.values()) {
1015 if (info.container == item.id) {
1016 // We are deleting a folder which still contains items that
1017 // think they are contained by that folder.
1018 String msg = "deleting a folder (" + item + ") which still " +
1019 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001020 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001022 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 sBgWorkspaceItems.remove(item);
1024 break;
1025 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1026 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1027 sBgWorkspaceItems.remove(item);
1028 break;
1029 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1030 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1031 break;
1032 }
1033 sBgItemsIdMap.remove(item.id);
1034 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 }
1036 }
Michael Jurka83df1882011-08-31 20:59:26 -07001037 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001038 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 }
1040
1041 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001042 * Update the order of the workspace screens in the database. The array list contains
1043 * a list of screen ids in the order that they should appear.
1044 */
Winson Chungc9168342013-06-26 14:54:55 -07001045 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001046 // Log to disk
1047 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1048 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1049
Winson Chung64359a52013-07-08 17:17:08 -07001050 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001051 final ContentResolver cr = context.getContentResolver();
1052 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1053
1054 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001055 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001056 while (iter.hasNext()) {
1057 long id = iter.next();
1058 if (id < 0) {
1059 iter.remove();
1060 }
1061 }
1062
1063 Runnable r = new Runnable() {
1064 @Override
1065 public void run() {
Yura085c8532014-02-11 15:15:29 +00001066 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001068 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001069 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 for (int i = 0; i < count; i++) {
1071 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001072 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1074 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001075 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 }
Yura085c8532014-02-11 15:15:29 +00001077
1078 try {
1079 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1080 } catch (Exception ex) {
1081 throw new RuntimeException(ex);
1082 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001083
Winson Chungba9c37f2013-08-30 14:11:37 -07001084 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001085 sBgWorkspaceScreens.clear();
1086 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001087 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 }
1089 };
1090 runOnWorkerThread(r);
1091 }
1092
1093 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 * Remove the contents of the specified folder from the database
1095 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001096 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 final ContentResolver cr = context.getContentResolver();
1098
Michael Jurkac9d95c52011-08-29 14:03:34 -07001099 Runnable r = new Runnable() {
1100 public void run() {
1101 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001102 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001103 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001104 sBgItemsIdMap.remove(info.id);
1105 sBgFolders.remove(info.id);
1106 sBgDbIconCache.remove(info);
1107 sBgWorkspaceItems.remove(info);
1108 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001109
Michael Jurkac9d95c52011-08-29 14:03:34 -07001110 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1111 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001112 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001113 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001114 for (ItemInfo childInfo : info.contents) {
1115 sBgItemsIdMap.remove(childInfo.id);
1116 sBgDbIconCache.remove(childInfo);
1117 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001118 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001119 }
1120 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001121 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 }
1123
1124 /**
1125 * Set this as the current Launcher activity object for the loader.
1126 */
1127 public void initialize(Callbacks callbacks) {
1128 synchronized (mLock) {
1129 mCallbacks = new WeakReference<Callbacks>(callbacks);
1130 }
1131 }
1132
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1135 * ACTION_PACKAGE_CHANGED.
1136 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001137 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001138 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001139 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001140
Joe Onorato36115782010-06-17 13:28:48 -04001141 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001142
Joe Onorato36115782010-06-17 13:28:48 -04001143 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1144 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1145 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1146 final String packageName = intent.getData().getSchemeSpecificPart();
1147 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148
Joe Onorato36115782010-06-17 13:28:48 -04001149 int op = PackageUpdatedTask.OP_NONE;
1150
1151 if (packageName == null || packageName.length() == 0) {
1152 // they sent us a bad intent
1153 return;
1154 }
1155
1156 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1157 op = PackageUpdatedTask.OP_UPDATE;
1158 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1159 if (!replacing) {
1160 op = PackageUpdatedTask.OP_REMOVE;
1161 }
1162 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1163 // later, we will update the package at this time
1164 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1165 if (!replacing) {
1166 op = PackageUpdatedTask.OP_ADD;
1167 } else {
1168 op = PackageUpdatedTask.OP_UPDATE;
1169 }
1170 }
1171
1172 if (op != PackageUpdatedTask.OP_NONE) {
1173 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1174 }
1175
1176 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001177 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001178 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001179 if (!replacing) {
1180 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1181 if (mAppsCanBeOnRemoveableStorage) {
1182 // Only rebind if we support removable storage. It catches the case where
1183 // apps on the external sd card need to be reloaded
1184 startLoaderFromBackground();
1185 }
1186 } else {
1187 // If we are replacing then just update the packages in the list
1188 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1189 packages));
1190 }
Joe Onorato36115782010-06-17 13:28:48 -04001191 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001192 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1193 if (!replacing) {
1194 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1195 enqueuePackageUpdated(new PackageUpdatedTask(
1196 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1197 }
1198 // else, we are replacing the packages, so ignore this event and wait for
1199 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001200 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001201 // If we have changed locale we need to clear out the labels in all apps/workspace.
1202 forceReload();
1203 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1204 // Check if configuration change was an mcc/mnc change which would affect app resources
1205 // and we would need to clear out the labels in all apps/workspace. Same handling as
1206 // above for ACTION_LOCALE_CHANGED
1207 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001208 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001209 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001210 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001211 forceReload();
1212 }
1213 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001214 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001215 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1216 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001217 if (mCallbacks != null) {
1218 Callbacks callbacks = mCallbacks.get();
1219 if (callbacks != null) {
1220 callbacks.bindSearchablesChanged();
1221 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001222 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001223 }
1224 }
1225
Reena Lee93f824a2011-09-23 17:20:28 -07001226 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001227 resetLoadedState(true, true);
1228
Reena Lee93f824a2011-09-23 17:20:28 -07001229 // Do this here because if the launcher activity is running it will be restarted.
1230 // If it's not running startLoaderFromBackground will merely tell it that it needs
1231 // to reload.
1232 startLoaderFromBackground();
1233 }
1234
Winson Chungf0c6ae02012-03-21 16:10:31 -07001235 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1236 synchronized (mLock) {
1237 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1238 // mWorkspaceLoaded to true later
1239 stopLoaderLocked();
1240 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1241 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1242 }
1243 }
1244
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001245 /**
1246 * When the launcher is in the background, it's possible for it to miss paired
1247 * configuration changes. So whenever we trigger the loader from the background
1248 * tell the launcher that it needs to re-run the loader when it comes back instead
1249 * of doing it now.
1250 */
1251 public void startLoaderFromBackground() {
1252 boolean runLoader = false;
1253 if (mCallbacks != null) {
1254 Callbacks callbacks = mCallbacks.get();
1255 if (callbacks != null) {
1256 // Only actually run the loader if they're not paused.
1257 if (!callbacks.setLoadOnResume()) {
1258 runLoader = true;
1259 }
1260 }
1261 }
1262 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001263 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001264 }
Joe Onorato36115782010-06-17 13:28:48 -04001265 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001266
Reena Lee93f824a2011-09-23 17:20:28 -07001267 // If there is already a loader task running, tell it to stop.
1268 // returns true if isLaunching() was true on the old task
1269 private boolean stopLoaderLocked() {
1270 boolean isLaunching = false;
1271 LoaderTask oldTask = mLoaderTask;
1272 if (oldTask != null) {
1273 if (oldTask.isLaunching()) {
1274 isLaunching = true;
1275 }
1276 oldTask.stopLocked();
1277 }
1278 return isLaunching;
1279 }
1280
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001281 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001282 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1283 }
1284
1285 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001286 synchronized (mLock) {
1287 if (DEBUG_LOADERS) {
1288 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1289 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001291 // Clear any deferred bind-runnables from the synchronized load process
1292 // We must do this before any loading/binding is scheduled below.
1293 mDeferredBindRunnables.clear();
1294
Joe Onorato36115782010-06-17 13:28:48 -04001295 // Don't bother to start the thread if we know it's not going to do anything
1296 if (mCallbacks != null && mCallbacks.get() != null) {
1297 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001298 // also, don't downgrade isLaunching if we're already running
1299 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001300 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001301 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1302 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001303 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1304 } else {
1305 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1306 sWorker.post(mLoaderTask);
1307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 }
1310 }
1311
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001312 void bindRemainingSynchronousPages() {
1313 // Post the remaining side pages to be loaded
1314 if (!mDeferredBindRunnables.isEmpty()) {
1315 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001316 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001317 }
1318 mDeferredBindRunnables.clear();
1319 }
1320 }
1321
Joe Onorato36115782010-06-17 13:28:48 -04001322 public void stopLoader() {
1323 synchronized (mLock) {
1324 if (mLoaderTask != null) {
1325 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 }
1327 }
Joe Onorato36115782010-06-17 13:28:48 -04001328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329
Winson Chung76828c82013-08-19 15:43:29 -07001330 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1331 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1332 final ContentResolver contentResolver = context.getContentResolver();
1333 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1334 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1335 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1336
1337 try {
1338 final int idIndex = sc.getColumnIndexOrThrow(
1339 LauncherSettings.WorkspaceScreens._ID);
1340 final int rankIndex = sc.getColumnIndexOrThrow(
1341 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1342 while (sc.moveToNext()) {
1343 try {
1344 long screenId = sc.getLong(idIndex);
1345 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001346 orderedScreens.put(rank, screenId);
1347 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001348 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001349 }
1350 }
1351 } finally {
1352 sc.close();
1353 }
Winson Chunga90303b2013-11-15 13:05:06 -08001354
1355 // Log to disk
1356 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1357 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1358 for (Integer i : orderedScreens.keySet()) {
1359 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1360 }
1361 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1362 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001363 return orderedScreens;
1364 }
1365
Michael Jurkac57b7a82011-08-09 22:02:20 -07001366 public boolean isAllAppsLoaded() {
1367 return mAllAppsLoaded;
1368 }
1369
Winson Chung36a62fe2012-05-06 18:04:42 -07001370 boolean isLoadingWorkspace() {
1371 synchronized (mLock) {
1372 if (mLoaderTask != null) {
1373 return mLoaderTask.isLoadingWorkspace();
1374 }
1375 }
1376 return false;
1377 }
1378
Joe Onorato36115782010-06-17 13:28:48 -04001379 /**
1380 * Runnable for the thread that loads the contents of the launcher:
1381 * - workspace icons
1382 * - widgets
1383 * - all apps icons
1384 */
1385 private class LoaderTask implements Runnable {
1386 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001387 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001388 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001389 private boolean mStopped;
1390 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001391 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392
Winson Chungc3eecff2011-07-11 17:44:15 -07001393 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001394
Dan Sandlerd5024042014-01-09 15:01:33 -05001395 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001396 mContext = context;
1397 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001398 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001399 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 }
1401
Joe Onorato36115782010-06-17 13:28:48 -04001402 boolean isLaunching() {
1403 return mIsLaunching;
1404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001405
Winson Chung36a62fe2012-05-06 18:04:42 -07001406 boolean isLoadingWorkspace() {
1407 return mIsLoadingAndBindingWorkspace;
1408 }
1409
Winson Chungc763c4e2013-07-19 13:49:06 -07001410 /** Returns whether this is an upgrade path */
1411 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001412 mIsLoadingAndBindingWorkspace = true;
1413
Joe Onorato36115782010-06-17 13:28:48 -04001414 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001415 if (DEBUG_LOADERS) {
1416 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001418
Winson Chungc763c4e2013-07-19 13:49:06 -07001419 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001420 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001421 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001422 synchronized (LoaderTask.this) {
1423 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001424 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001425 }
1426 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
1428 }
1429
Joe Onorato36115782010-06-17 13:28:48 -04001430 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001431 bindWorkspace(-1, isUpgradePath);
1432 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001433 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001434
Joe Onorato36115782010-06-17 13:28:48 -04001435 private void waitForIdle() {
1436 // Wait until the either we're stopped or the other threads are done.
1437 // This way we don't start loading all apps until the workspace has settled
1438 // down.
1439 synchronized (LoaderTask.this) {
1440 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001441
Joe Onorato36115782010-06-17 13:28:48 -04001442 mHandler.postIdle(new Runnable() {
1443 public void run() {
1444 synchronized (LoaderTask.this) {
1445 mLoadAndBindStepFinished = true;
1446 if (DEBUG_LOADERS) {
1447 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001448 }
Joe Onorato36115782010-06-17 13:28:48 -04001449 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001450 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001451 }
Joe Onorato36115782010-06-17 13:28:48 -04001452 });
1453
Michael Jurkac7700af2013-05-14 20:17:58 +02001454 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001455 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001456 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1457 // wait no longer than 1sec at a time
1458 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001459 } catch (InterruptedException ex) {
1460 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001461 }
1462 }
Joe Onorato36115782010-06-17 13:28:48 -04001463 if (DEBUG_LOADERS) {
1464 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001465 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001466 + "ms for previous step to finish binding");
1467 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001468 }
Joe Onorato36115782010-06-17 13:28:48 -04001469 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001470
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001472 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001473 // Ensure that we have a valid page index to load synchronously
1474 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1475 "valid page index");
1476 }
1477 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1478 // Ensure that we don't try and bind a specified page when the pages have not been
1479 // loaded already (we should load everything asynchronously in that case)
1480 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1481 }
1482 synchronized (mLock) {
1483 if (mIsLoaderTaskRunning) {
1484 // Ensure that we are never running the background loading at this point since
1485 // we also touch the background collections
1486 throw new RuntimeException("Error! Background loading is already running");
1487 }
1488 }
1489
1490 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1491 // data structures, we can't allow any other thread to touch that data, but because
1492 // this call is synchronous, we can get away with not locking).
1493
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001494 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001495 // operations from the previous activity. We need to ensure that all queued operations
1496 // are executed before any synchronous binding work is done.
1497 mHandler.flush();
1498
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001499 // Divide the set of loaded items into those that we are binding synchronously, and
1500 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001501 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001502 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1503 // arise from that.
1504 onlyBindAllApps();
1505 }
1506
Joe Onorato36115782010-06-17 13:28:48 -04001507 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001508 boolean isUpgrade = false;
1509
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001510 synchronized (mLock) {
1511 mIsLoaderTaskRunning = true;
1512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513 // Optimize for end-user experience: if the Launcher is up and // running with the
1514 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1515 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001516 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001517 // Elevate priority when Home launches for the first time to avoid
1518 // starving at boot time. Staring at a blank home is not cool.
1519 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001520 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1521 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001522 android.os.Process.setThreadPriority(mIsLaunching
1523 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1524 }
Winson Chung64359a52013-07-08 17:17:08 -07001525 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001526 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001527
Joe Onorato36115782010-06-17 13:28:48 -04001528 if (mStopped) {
1529 break keep_running;
1530 }
1531
1532 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1533 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001534 synchronized (mLock) {
1535 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001536 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001537 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1538 }
1539 }
Joe Onorato36115782010-06-17 13:28:48 -04001540 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001541
1542 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001543 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1544 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001545
1546 // Restore the default thread priority after we are done loading items
1547 synchronized (mLock) {
1548 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1549 }
Joe Onorato36115782010-06-17 13:28:48 -04001550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551
Winson Chungaac01e12011-08-17 10:37:13 -07001552 // Update the saved icons if necessary
1553 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001554 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001555 for (Object key : sBgDbIconCache.keySet()) {
1556 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1557 }
1558 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001559 }
Winson Chungaac01e12011-08-17 10:37:13 -07001560
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001561 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001562 // Ensure that all the applications that are in the system are
1563 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001564 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001565 verifyApplications();
1566 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001567 }
1568
Joe Onorato36115782010-06-17 13:28:48 -04001569 // Clear out this reference, otherwise we end up holding it until all of the
1570 // callback runnables are done.
1571 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572
Joe Onorato36115782010-06-17 13:28:48 -04001573 synchronized (mLock) {
1574 // If we are still the last one to be scheduled, remove ourselves.
1575 if (mLoaderTask == this) {
1576 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001577 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001578 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001579 }
Joe Onorato36115782010-06-17 13:28:48 -04001580 }
1581
1582 public void stopLocked() {
1583 synchronized (LoaderTask.this) {
1584 mStopped = true;
1585 this.notify();
1586 }
1587 }
1588
1589 /**
1590 * Gets the callbacks object. If we've been stopped, or if the launcher object
1591 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1592 * object that was around when the deferred message was scheduled, and if there's
1593 * a new Callbacks object around then also return null. This will save us from
1594 * calling onto it with data that will be ignored.
1595 */
1596 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1597 synchronized (mLock) {
1598 if (mStopped) {
1599 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001600 }
Joe Onorato36115782010-06-17 13:28:48 -04001601
1602 if (mCallbacks == null) {
1603 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001604 }
Joe Onorato36115782010-06-17 13:28:48 -04001605
1606 final Callbacks callbacks = mCallbacks.get();
1607 if (callbacks != oldCallbacks) {
1608 return null;
1609 }
1610 if (callbacks == null) {
1611 Log.w(TAG, "no mCallbacks");
1612 return null;
1613 }
1614
1615 return callbacks;
1616 }
1617 }
1618
Winson Chungc763c4e2013-07-19 13:49:06 -07001619 private void verifyApplications() {
1620 final Context context = mApp.getContext();
1621
1622 // Cross reference all the applications in our apps list with items in the workspace
1623 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001624 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001626 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001627 tmpInfos = getItemInfoForComponentName(app.componentName);
1628 if (tmpInfos.isEmpty()) {
1629 // We are missing an application icon, so add this to the workspace
1630 added.add(app);
1631 // This is a rare event, so lets log it
1632 Log.e(TAG, "Missing Application on load: " + app);
1633 }
1634 }
1635 }
1636 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001637 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001638 }
1639 }
1640
Joe Onorato36115782010-06-17 13:28:48 -04001641 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001642 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001643 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001644 LauncherAppState app = LauncherAppState.getInstance();
1645 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001646 final int countX = (int) grid.numColumns;
1647 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001648
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001650 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001651 // Return early if we detect that an item is under the hotseat button
1652 if (mCallbacks == null ||
1653 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001654 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001655 Log.e(TAG, "Error loading shortcut into hotseat " + item
1656 + " into position (" + item.screenId + ":" + item.cellX + ","
1657 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001658 return false;
1659 }
1660
Dan Sandler295ae182013-12-10 16:05:47 -05001661 final ItemInfo[][] hotseatItems =
1662 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1663
Adam Cohenae4409d2013-11-26 10:34:59 -08001664 if (item.screenId >= grid.numHotseatIcons) {
1665 Log.e(TAG, "Error loading shortcut " + item
1666 + " into hotseat position " + item.screenId
1667 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1668 + ")");
1669 return false;
1670 }
1671
Dan Sandler295ae182013-12-10 16:05:47 -05001672 if (hotseatItems != null) {
1673 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001674 Log.e(TAG, "Error loading shortcut into hotseat " + item
1675 + " into position (" + item.screenId + ":" + item.cellX + ","
1676 + item.cellY + ") occupied by "
1677 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1678 [(int) item.screenId][0]);
1679 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001680 } else {
1681 hotseatItems[(int) item.screenId][0] = item;
1682 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001683 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001684 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001685 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001686 items[(int) item.screenId][0] = item;
1687 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001688 return true;
1689 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001690 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1691 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001692 return true;
1693 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001694
Adam Cohendcd297f2013-06-18 13:13:40 -07001695 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001696 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001697 occupied.put(item.screenId, items);
1698 }
1699
Dan Sandler295ae182013-12-10 16:05:47 -05001700 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001701 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1702 item.cellX < 0 || item.cellY < 0 ||
1703 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1704 Log.e(TAG, "Error loading shortcut " + item
1705 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1706 + item.cellX + "," + item.cellY
1707 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1708 return false;
1709 }
1710
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001711 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001712 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1713 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001715 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001717 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001718 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001720 return false;
1721 }
1722 }
1723 }
1724 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1725 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001726 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001727 }
1728 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001729
Joe Onorato36115782010-06-17 13:28:48 -04001730 return true;
1731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732
Winson Chungba9c37f2013-08-30 14:11:37 -07001733 /** Clears all the sBg data structures */
1734 private void clearSBgDataStructures() {
1735 synchronized (sBgLock) {
1736 sBgWorkspaceItems.clear();
1737 sBgAppWidgets.clear();
1738 sBgFolders.clear();
1739 sBgItemsIdMap.clear();
1740 sBgDbIconCache.clear();
1741 sBgWorkspaceScreens.clear();
1742 }
1743 }
1744
Dan Sandlerd5024042014-01-09 15:01:33 -05001745 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001746 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001747 // Log to disk
1748 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1749
Joe Onorato36115782010-06-17 13:28:48 -04001750 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751
Joe Onorato36115782010-06-17 13:28:48 -04001752 final Context context = mContext;
1753 final ContentResolver contentResolver = context.getContentResolver();
1754 final PackageManager manager = context.getPackageManager();
1755 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1756 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001757
Winson Chung892c74d2013-08-22 16:15:50 -07001758 LauncherAppState app = LauncherAppState.getInstance();
1759 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1760 int countX = (int) grid.numColumns;
1761 int countY = (int) grid.numRows;
1762
Dan Sandlerd5024042014-01-09 15:01:33 -05001763 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1764 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1765 LauncherAppState.getLauncherProvider().deleteDatabase();
1766 }
1767
1768 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1769 // append the user's Launcher2 shortcuts
1770 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1771 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1772 } else {
1773 // Make sure the default workspace is loaded
1774 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1775 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1776 }
Adam Cohene25af792013-06-06 23:08:25 -07001777
Winson Chungc763c4e2013-07-19 13:49:06 -07001778 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001779 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001780 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001781
Winson Chung9f9f00b2013-11-15 13:27:00 -08001782 // Log to disk
1783 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001784
Winson Chung2abf94d2012-07-18 18:16:38 -07001785 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001786 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001787
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001789 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001790 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001791 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001792 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001793
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001794 // +1 for the hotseat (it can be larger than the workspace)
1795 // Load workspace in reverse order to ensure that latest items are loaded first (and
1796 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001797 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 try {
1800 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1801 final int intentIndex = c.getColumnIndexOrThrow
1802 (LauncherSettings.Favorites.INTENT);
1803 final int titleIndex = c.getColumnIndexOrThrow
1804 (LauncherSettings.Favorites.TITLE);
1805 final int iconTypeIndex = c.getColumnIndexOrThrow(
1806 LauncherSettings.Favorites.ICON_TYPE);
1807 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1808 final int iconPackageIndex = c.getColumnIndexOrThrow(
1809 LauncherSettings.Favorites.ICON_PACKAGE);
1810 final int iconResourceIndex = c.getColumnIndexOrThrow(
1811 LauncherSettings.Favorites.ICON_RESOURCE);
1812 final int containerIndex = c.getColumnIndexOrThrow(
1813 LauncherSettings.Favorites.CONTAINER);
1814 final int itemTypeIndex = c.getColumnIndexOrThrow(
1815 LauncherSettings.Favorites.ITEM_TYPE);
1816 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1817 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001818 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 final int screenIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.SCREEN);
1822 final int cellXIndex = c.getColumnIndexOrThrow
1823 (LauncherSettings.Favorites.CELLX);
1824 final int cellYIndex = c.getColumnIndexOrThrow
1825 (LauncherSettings.Favorites.CELLY);
1826 final int spanXIndex = c.getColumnIndexOrThrow
1827 (LauncherSettings.Favorites.SPANX);
1828 final int spanYIndex = c.getColumnIndexOrThrow(
1829 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001830 final int restoredIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1833 //final int displayModeIndex = c.getColumnIndexOrThrow(
1834 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 ShortcutInfo info;
1837 String intentDescription;
1838 LauncherAppWidgetInfo appWidgetInfo;
1839 int container;
1840 long id;
1841 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001844 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 try {
1846 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001847 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 switch (itemType) {
1850 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1851 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001852 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 intentDescription = c.getString(intentIndex);
1854 try {
1855 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001856 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001857 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001858 if (restored) {
1859 // might be installed later
1860 Launcher.addDumpLog(TAG,
1861 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001862 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001863 if (!mAppsCanBeOnRemoveableStorage) {
1864 // Log the invalid package, and remove it
1865 Launcher.addDumpLog(TAG,
1866 "Invalid package removed: " + cn, true);
1867 itemsToRemove.add(id);
1868 } else {
1869 // If apps can be on external storage, then we just
1870 // leave them for the user to remove (maybe add
1871 // visual treatment to it)
1872 Launcher.addDumpLog(TAG,
1873 "Invalid package found: " + cn, true);
1874 }
1875 continue;
Winson Chungee055712013-07-30 14:46:24 -07001876 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001877 } else if (restored) {
1878 // no special handling necessary for this restored item
1879 restoredRows.add(id);
1880 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001883 Launcher.addDumpLog(TAG,
1884 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 continue;
1886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887
Chris Wrenf4d08112014-01-16 18:13:56 -05001888 if (restored) {
1889 Launcher.addDumpLog(TAG,
1890 "constructing info for partially restored package",
1891 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001892 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001893 intent = getRestoredItemIntent(c, context, intent);
1894 } else if (itemType ==
1895 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1897 titleIndex, mLabelCache);
1898 } else {
1899 info = getShortcutInfo(c, context, iconTypeIndex,
1900 iconPackageIndex, iconResourceIndex, iconIndex,
1901 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001902
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903 // App shortcuts that used to be automatically added to Launcher
1904 // didn't always have the correct intent flags set, so do that
1905 // here
1906 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001907 intent.getCategories() != null &&
1908 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001909 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 intent.addFlags(
1911 Intent.FLAG_ACTIVITY_NEW_TASK |
1912 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1913 }
Michael Jurka96879562012-03-22 05:54:33 -07001914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001917 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 container = c.getInt(containerIndex);
1920 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 info.cellX = c.getInt(cellXIndex);
1923 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001924 info.spanX = 1;
1925 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001926
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001928 deleteOnInvalidPlacement.set(false);
1929 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1930 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001931 itemsToRemove.add(id);
1932 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 break;
1934 }
1935
1936 switch (container) {
1937 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1938 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1939 sBgWorkspaceItems.add(info);
1940 break;
1941 default:
1942 // Item is in a user folder
1943 FolderInfo folderInfo =
1944 findOrMakeFolder(sBgFolders, container);
1945 folderInfo.add(info);
1946 break;
1947 }
1948 sBgItemsIdMap.put(info.id, info);
1949
1950 // now that we've loaded everthing re-save it with the
1951 // icon in case it disappears somehow.
1952 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001953 } else {
1954 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001955 }
1956 break;
1957
1958 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1959 id = c.getLong(idIndex);
1960 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1961
1962 folderInfo.title = c.getString(titleIndex);
1963 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001964 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 folderInfo.cellX = c.getInt(cellXIndex);
1968 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001969 folderInfo.spanX = 1;
1970 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971
Daniel Sandler8802e962010-05-26 16:28:16 -04001972 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001973 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001974 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001975 deleteOnInvalidPlacement)) {
1976 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001977 itemsToRemove.add(id);
1978 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001979 break;
1980 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001981
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1984 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1985 sBgWorkspaceItems.add(folderInfo);
1986 break;
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
Joe Onorato17a89222011-02-08 17:26:11 -08001988
Chris Wrenf4d08112014-01-16 18:13:56 -05001989 if (restored) {
1990 // no special handling required for restored folders
1991 restoredRows.add(id);
1992 }
1993
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1995 sBgFolders.put(folderInfo.id, folderInfo);
1996 break;
1997
1998 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1999 // Read all Launcher-specific widget details
2000 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002001 String savedProvider = c.getString(appWidgetProviderIndex);
2002
Joe Onorato36115782010-06-17 13:28:48 -04002003 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 final AppWidgetProviderInfo provider =
2006 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002007
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 if (!isSafeMode && (provider == null || provider.provider == null ||
2009 provider.provider.getPackageName() == null)) {
2010 String log = "Deleting widget that isn't installed anymore: id="
2011 + id + " appWidgetId=" + appWidgetId;
2012 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002013 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 itemsToRemove.add(id);
2015 } else {
2016 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2017 provider.provider);
2018 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 appWidgetInfo.cellX = c.getInt(cellXIndex);
2021 appWidgetInfo.cellY = c.getInt(cellYIndex);
2022 appWidgetInfo.spanX = c.getInt(spanXIndex);
2023 appWidgetInfo.spanY = c.getInt(spanYIndex);
2024 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2025 appWidgetInfo.minSpanX = minSpan[0];
2026 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002027
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 container = c.getInt(containerIndex);
2029 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2030 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2031 Log.e(TAG, "Widget found where container != " +
2032 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2033 continue;
2034 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035
Adam Cohene25af792013-06-06 23:08:25 -07002036 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002037 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002038 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002039 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002040 deleteOnInvalidPlacement)) {
2041 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002042 itemsToRemove.add(id);
2043 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 break;
2045 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002046 String providerName = provider.provider.flattenToString();
2047 if (!providerName.equals(savedProvider)) {
2048 ContentValues values = new ContentValues();
2049 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2050 providerName);
2051 String where = BaseColumns._ID + "= ?";
2052 String[] args = {Integer.toString(c.getInt(idIndex))};
2053 contentResolver.update(contentUri, values, where, args);
2054 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2056 sBgAppWidgets.add(appWidgetInfo);
2057 }
Joe Onorato36115782010-06-17 13:28:48 -04002058 break;
2059 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002061 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002062 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 }
2064 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002065 if (c != null) {
2066 c.close();
2067 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 }
2069
Winson Chungba9c37f2013-08-30 14:11:37 -07002070 // Break early if we've stopped loading
2071 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002072 clearSBgDataStructures();
2073 return false;
2074 }
2075
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 if (itemsToRemove.size() > 0) {
2077 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002078 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 // Remove dead items
2080 for (long id : itemsToRemove) {
2081 if (DEBUG_LOADERS) {
2082 Log.d(TAG, "Removed id = " + id);
2083 }
2084 // Don't notify content observers
2085 try {
2086 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2087 null, null);
2088 } catch (RemoteException e) {
2089 Log.w(TAG, "Could not remove id = " + id);
2090 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002091 }
2092 }
2093
Chris Wrenf4d08112014-01-16 18:13:56 -05002094 if (restoredRows.size() > 0) {
2095 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2096 LauncherSettings.Favorites.CONTENT_URI);
2097 // Update restored items that no longer require special handling
2098 try {
2099 StringBuilder selectionBuilder = new StringBuilder();
2100 selectionBuilder.append(LauncherSettings.Favorites._ID);
2101 selectionBuilder.append(" IN (");
2102 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2103 selectionBuilder.append(")");
2104 ContentValues values = new ContentValues();
2105 values.put(LauncherSettings.Favorites.RESTORED, 0);
2106 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2107 values, selectionBuilder.toString(), null);
2108 } catch (RemoteException e) {
2109 Log.w(TAG, "Could not update restored rows");
2110 }
2111 }
2112
Winson Chungc763c4e2013-07-19 13:49:06 -07002113 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 long maxScreenId = 0;
2115 // If we're importing we use the old screen order.
2116 for (ItemInfo item: sBgItemsIdMap.values()) {
2117 long screenId = item.screenId;
2118 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2119 !sBgWorkspaceScreens.contains(screenId)) {
2120 sBgWorkspaceScreens.add(screenId);
2121 if (screenId > maxScreenId) {
2122 maxScreenId = screenId;
2123 }
2124 }
2125 }
2126 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002127 // Log to disk
2128 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2129 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2130 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002131
Michael Jurka414300a2013-08-27 15:42:35 +02002132 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002133 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002134
2135 // Update the max item id after we load an old db
2136 long maxItemId = 0;
2137 // If we're importing we use the old screen order.
2138 for (ItemInfo item: sBgItemsIdMap.values()) {
2139 maxItemId = Math.max(maxItemId, item.id);
2140 }
Michael Jurka414300a2013-08-27 15:42:35 +02002141 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002143 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2144 for (Integer i : orderedScreens.keySet()) {
2145 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002146 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002147 // Log to disk
2148 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2149 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002150
2151 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002152 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002153 for (ItemInfo item: sBgItemsIdMap.values()) {
2154 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002155 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2156 unusedScreens.contains(screenId)) {
2157 unusedScreens.remove(screenId);
2158 }
2159 }
2160
2161 // If there are any empty screens remove them, and update.
2162 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002163 // Log to disk
2164 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2165 TextUtils.join(", ", unusedScreens), true);
2166
Winson Chung933bae62013-08-29 11:42:30 -07002167 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002168 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2169 }
2170 }
2171
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 if (DEBUG_LOADERS) {
2173 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2174 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002175 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002176 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002178
Daniel Sandler566da102013-06-25 23:43:45 -04002179 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002180 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002181 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002182 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002183 line += " | ";
2184 }
Winson Chung892c74d2013-08-22 16:15:50 -07002185 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002186 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 }
Joe Onorato36115782010-06-17 13:28:48 -04002188 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 }
Joe Onorato36115782010-06-17 13:28:48 -04002192 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002193 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002194 }
2195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 /** Filters the set of items who are directly or indirectly (via another container) on the
2197 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002198 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 ArrayList<ItemInfo> allWorkspaceItems,
2200 ArrayList<ItemInfo> currentScreenItems,
2201 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002202 // Purge any null ItemInfos
2203 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2204 while (iter.hasNext()) {
2205 ItemInfo i = iter.next();
2206 if (i == null) {
2207 iter.remove();
2208 }
2209 }
2210
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002211 // Order the set of items by their containers first, this allows use to walk through the
2212 // list sequentially, build up a list of containers that are in the specified screen,
2213 // as well as all items in those containers.
2214 Set<Long> itemsOnScreen = new HashSet<Long>();
2215 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2216 @Override
2217 public int compare(ItemInfo lhs, ItemInfo rhs) {
2218 return (int) (lhs.container - rhs.container);
2219 }
2220 });
2221 for (ItemInfo info : allWorkspaceItems) {
2222 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002223 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 currentScreenItems.add(info);
2225 itemsOnScreen.add(info.id);
2226 } else {
2227 otherScreenItems.add(info);
2228 }
2229 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2230 currentScreenItems.add(info);
2231 itemsOnScreen.add(info.id);
2232 } else {
2233 if (itemsOnScreen.contains(info.container)) {
2234 currentScreenItems.add(info);
2235 itemsOnScreen.add(info.id);
2236 } else {
2237 otherScreenItems.add(info);
2238 }
2239 }
2240 }
2241 }
2242
2243 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002244 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 ArrayList<LauncherAppWidgetInfo> appWidgets,
2246 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2247 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002248
2249 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002250 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002252 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 currentScreenWidgets.add(widget);
2254 } else {
2255 otherScreenWidgets.add(widget);
2256 }
2257 }
2258 }
2259
2260 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002261 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002262 HashMap<Long, ItemInfo> itemsIdMap,
2263 HashMap<Long, FolderInfo> folders,
2264 HashMap<Long, FolderInfo> currentScreenFolders,
2265 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266
2267 for (long id : folders.keySet()) {
2268 ItemInfo info = itemsIdMap.get(id);
2269 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002270 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002272 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 currentScreenFolders.put(id, folder);
2274 } else {
2275 otherScreenFolders.put(id, folder);
2276 }
2277 }
2278 }
2279
2280 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2281 * right) */
2282 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002283 final LauncherAppState app = LauncherAppState.getInstance();
2284 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 // XXX: review this
2286 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002287 @Override
2288 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002289 int cellCountX = (int) grid.numColumns;
2290 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002291 int screenOffset = cellCountX * cellCountY;
2292 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002294 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002296 rhs.cellY * cellCountX + rhs.cellX);
2297 return (int) (lr - rr);
2298 }
2299 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002301
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2303 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 final Runnable r = new Runnable() {
2305 @Override
2306 public void run() {
2307 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2308 if (callbacks != null) {
2309 callbacks.bindScreens(orderedScreens);
2310 }
2311 }
2312 };
2313 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2314 }
2315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2317 final ArrayList<ItemInfo> workspaceItems,
2318 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2319 final HashMap<Long, FolderInfo> folders,
2320 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 final boolean postOnMainThread = (deferredBindRunnables != null);
2323
2324 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002325 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002327 final int start = i;
2328 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 final Runnable r = new Runnable() {
2330 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002331 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002332 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002333 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002334 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2335 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002336 }
2337 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 };
2339 if (postOnMainThread) {
2340 deferredBindRunnables.add(r);
2341 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002342 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 }
Joe Onorato36115782010-06-17 13:28:48 -04002344 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345
2346 // Bind the folders
2347 if (!folders.isEmpty()) {
2348 final Runnable r = new Runnable() {
2349 public void run() {
2350 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2351 if (callbacks != null) {
2352 callbacks.bindFolders(folders);
2353 }
2354 }
2355 };
2356 if (postOnMainThread) {
2357 deferredBindRunnables.add(r);
2358 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002359 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360 }
2361 }
2362
2363 // Bind the widgets, one at a time
2364 N = appWidgets.size();
2365 for (int i = 0; i < N; i++) {
2366 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2367 final Runnable r = new Runnable() {
2368 public void run() {
2369 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2370 if (callbacks != null) {
2371 callbacks.bindAppWidget(widget);
2372 }
2373 }
2374 };
2375 if (postOnMainThread) {
2376 deferredBindRunnables.add(r);
2377 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002378 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002379 }
2380 }
2381 }
2382
2383 /**
2384 * Binds all loaded data to actual views on the main thread.
2385 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002386 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 final long t = SystemClock.uptimeMillis();
2388 Runnable r;
2389
2390 // Don't use these two variables in any of the callback runnables.
2391 // Otherwise we hold a reference to them.
2392 final Callbacks oldCallbacks = mCallbacks.get();
2393 if (oldCallbacks == null) {
2394 // This launcher has exited and nobody bothered to tell us. Just bail.
2395 Log.w(TAG, "LoaderTask running with no launcher");
2396 return;
2397 }
2398
Winson Chung9b9fb962013-11-15 15:39:34 -08002399 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2401 ArrayList<LauncherAppWidgetInfo> appWidgets =
2402 new ArrayList<LauncherAppWidgetInfo>();
2403 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2404 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002405 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002406 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 workspaceItems.addAll(sBgWorkspaceItems);
2408 appWidgets.addAll(sBgAppWidgets);
2409 folders.putAll(sBgFolders);
2410 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002411 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002412 }
2413
Derek Prothro7aff3992013-12-10 14:00:37 -05002414 final boolean isLoadingSynchronously =
2415 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002416 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002417 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002418 if (currScreen >= orderedScreenIds.size()) {
2419 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002420 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002421 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002422 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002423 final long currentScreenId = currentScreen < 0
2424 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002425
2426 // Load all the items that are on the current page first (and in the process, unbind
2427 // all the existing workspace items before we call startBinding() below.
2428 unbindWorkspaceItemsOnMainThread();
2429
2430 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2432 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2433 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2434 new ArrayList<LauncherAppWidgetInfo>();
2435 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2436 new ArrayList<LauncherAppWidgetInfo>();
2437 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2438 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2439
Winson Chung9b9fb962013-11-15 15:39:34 -08002440 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002442 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002444 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 otherFolders);
2446 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2447 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2448
2449 // Tell the workspace that we're about to start binding items
2450 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002451 public void run() {
2452 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2453 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002454 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002455 }
2456 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 };
Winson Chung81b52252012-08-27 15:34:29 -07002458 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2461
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002462 // Load items on the current page
2463 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2464 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002465 if (isLoadingSynchronously) {
2466 r = new Runnable() {
2467 public void run() {
2468 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002469 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002470 callbacks.onPageBoundSynchronously(currentScreen);
2471 }
2472 }
2473 };
Winson Chung81b52252012-08-27 15:34:29 -07002474 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002475 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476
Winson Chung4a2afa32012-07-19 14:53:05 -07002477 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2478 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002479 mDeferredBindRunnables.clear();
2480 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002481 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002482
2483 // Tell the workspace that we're done binding items
2484 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002485 public void run() {
2486 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2487 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002488 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002489 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490
Winson Chung98e030b2012-05-07 16:01:11 -07002491 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002492 if (DEBUG_LOADERS) {
2493 Log.d(TAG, "bound workspace in "
2494 + (SystemClock.uptimeMillis()-t) + "ms");
2495 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002496
2497 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002498 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002500 if (isLoadingSynchronously) {
2501 mDeferredBindRunnables.add(r);
2502 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002503 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002504 }
Joe Onorato36115782010-06-17 13:28:48 -04002505 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002506
Joe Onorato36115782010-06-17 13:28:48 -04002507 private void loadAndBindAllApps() {
2508 if (DEBUG_LOADERS) {
2509 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2510 }
2511 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002512 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002513 synchronized (LoaderTask.this) {
2514 if (mStopped) {
2515 return;
2516 }
2517 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002518 }
Joe Onorato36115782010-06-17 13:28:48 -04002519 } else {
2520 onlyBindAllApps();
2521 }
2522 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002523
Joe Onorato36115782010-06-17 13:28:48 -04002524 private void onlyBindAllApps() {
2525 final Callbacks oldCallbacks = mCallbacks.get();
2526 if (oldCallbacks == null) {
2527 // This launcher has exited and nobody bothered to tell us. Just bail.
2528 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2529 return;
2530 }
2531
2532 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002533 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002534 final ArrayList<AppInfo> list
2535 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002536 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002537 public void run() {
2538 final long t = SystemClock.uptimeMillis();
2539 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2540 if (callbacks != null) {
2541 callbacks.bindAllApplications(list);
2542 }
2543 if (DEBUG_LOADERS) {
2544 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2545 + (SystemClock.uptimeMillis()-t) + "ms");
2546 }
2547 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002548 };
2549 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002550 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002551 r.run();
2552 } else {
2553 mHandler.post(r);
2554 }
Joe Onorato36115782010-06-17 13:28:48 -04002555 }
2556
Winson Chung64359a52013-07-08 17:17:08 -07002557 private void loadAllApps() {
2558 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002559
Joe Onorato36115782010-06-17 13:28:48 -04002560 final Callbacks oldCallbacks = mCallbacks.get();
2561 if (oldCallbacks == null) {
2562 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002563 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002564 return;
2565 }
2566
Winson Chung64359a52013-07-08 17:17:08 -07002567 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002568 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2569 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2570
Winson Chung64359a52013-07-08 17:17:08 -07002571 // Clear the list of apps
2572 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002573
Winson Chung64359a52013-07-08 17:17:08 -07002574 // Query for the set of apps
2575 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2576 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2577 if (DEBUG_LOADERS) {
2578 Log.d(TAG, "queryIntentActivities took "
2579 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2580 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2581 }
2582 // Fail if we don't have any apps
2583 if (apps == null || apps.isEmpty()) {
2584 return;
2585 }
2586 // Sort the applications by name
2587 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2588 Collections.sort(apps,
2589 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2590 if (DEBUG_LOADERS) {
2591 Log.d(TAG, "sort took "
2592 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002593 }
2594
Winson Chung64359a52013-07-08 17:17:08 -07002595 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002596 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002597 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002598 // This builds the icon bitmaps.
2599 mBgAllAppsList.add(new AppInfo(packageManager, app,
2600 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002601 }
2602
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002603 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002604 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2605 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002606
2607 // Post callback on main thread
2608 mHandler.post(new Runnable() {
2609 public void run() {
2610 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002611 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002612 if (callbacks != null) {
2613 callbacks.bindAllApplications(added);
2614 if (DEBUG_LOADERS) {
2615 Log.d(TAG, "bound " + added.size() + " apps in "
2616 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2617 }
2618 } else {
2619 Log.i(TAG, "not binding apps: no Launcher activity");
2620 }
2621 }
2622 });
2623
Joe Onorato36115782010-06-17 13:28:48 -04002624 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002625 Log.d(TAG, "Icons processed in "
2626 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002627 }
2628 }
2629
2630 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002631 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002632 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2633 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2634 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2635 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2636 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2637 }
Joe Onorato36115782010-06-17 13:28:48 -04002638 }
2639 }
2640
2641 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002642 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002643 }
2644
2645 private class PackageUpdatedTask implements Runnable {
2646 int mOp;
2647 String[] mPackages;
2648
2649 public static final int OP_NONE = 0;
2650 public static final int OP_ADD = 1;
2651 public static final int OP_UPDATE = 2;
2652 public static final int OP_REMOVE = 3; // uninstlled
2653 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2654
2655
2656 public PackageUpdatedTask(int op, String[] packages) {
2657 mOp = op;
2658 mPackages = packages;
2659 }
2660
2661 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002662 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002663
2664 final String[] packages = mPackages;
2665 final int N = packages.length;
2666 switch (mOp) {
2667 case OP_ADD:
2668 for (int i=0; i<N; i++) {
2669 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002670 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002671 }
2672 break;
2673 case OP_UPDATE:
2674 for (int i=0; i<N; i++) {
2675 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002676 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002677 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002678 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002679 }
2680 break;
2681 case OP_REMOVE:
2682 case OP_UNAVAILABLE:
2683 for (int i=0; i<N; i++) {
2684 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002685 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002686 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002687 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002688 }
2689 break;
2690 }
2691
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002692 ArrayList<AppInfo> added = null;
2693 ArrayList<AppInfo> modified = null;
2694 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002695
Adam Cohen487f7dd2012-06-28 18:12:10 -07002696 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002697 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002698 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002699 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002700 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002701 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002702 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002703 }
Winson Chung5d55f332012-07-16 20:45:03 -07002704 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002705 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002706 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002707 }
2708
Joe Onorato36115782010-06-17 13:28:48 -04002709 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2710 if (callbacks == null) {
2711 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2712 return;
2713 }
2714
2715 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002716 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002717 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002718 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002719 addAndBindAddedWorkspaceApps(context, addedInfos);
2720 } else {
2721 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002722 }
Joe Onorato36115782010-06-17 13:28:48 -04002723 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002724
Joe Onorato36115782010-06-17 13:28:48 -04002725 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002726 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002727
2728 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002729 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002730 ArrayList<ItemInfo> infos =
2731 getItemInfoForComponentName(a.componentName);
2732 for (ItemInfo i : infos) {
2733 if (isShortcutInfoUpdateable(i)) {
2734 ShortcutInfo info = (ShortcutInfo) i;
2735 info.title = a.title.toString();
2736 updateItemInDatabase(context, info);
2737 }
2738 }
2739 }
2740
Joe Onorato36115782010-06-17 13:28:48 -04002741 mHandler.post(new Runnable() {
2742 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002743 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2744 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002745 callbacks.bindAppsUpdated(modifiedFinal);
2746 }
2747 }
2748 });
2749 }
Winson Chung83892cc2013-05-01 16:53:33 -07002750
Winson Chungdf95eb12013-10-16 14:57:07 -07002751 final ArrayList<String> removedPackageNames =
2752 new ArrayList<String>();
2753 if (mOp == OP_REMOVE) {
2754 // Mark all packages in the broadcast to be removed
2755 removedPackageNames.addAll(Arrays.asList(packages));
2756 } else if (mOp == OP_UPDATE) {
2757 // Mark disabled packages in the broadcast to be removed
2758 final PackageManager pm = context.getPackageManager();
2759 for (int i=0; i<N; i++) {
2760 if (isPackageDisabled(pm, packages[i])) {
2761 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002762 }
2763 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002764 }
2765 // Remove all the components associated with this package
2766 for (String pn : removedPackageNames) {
2767 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2768 for (ItemInfo i : infos) {
2769 deleteItemFromDatabase(context, i);
2770 }
2771 }
2772 // Remove all the specific components
2773 for (AppInfo a : removedApps) {
2774 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2775 for (ItemInfo i : infos) {
2776 deleteItemFromDatabase(context, i);
2777 }
2778 }
2779 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2780 // Remove any queued items from the install queue
2781 String spKey = LauncherAppState.getSharedPreferencesKey();
2782 SharedPreferences sp =
2783 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2784 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2785 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002786 mHandler.post(new Runnable() {
2787 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002788 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2789 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002790 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002791 }
2792 }
2793 });
Joe Onoratobe386092009-11-17 17:32:16 -08002794 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002795
Michael Jurkac402cd92013-05-20 15:49:32 +02002796 final ArrayList<Object> widgetsAndShortcuts =
2797 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002798 mHandler.post(new Runnable() {
2799 @Override
2800 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002801 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2802 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002803 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002804 }
2805 }
2806 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002807
2808 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002809 mHandler.post(new Runnable() {
2810 public void run() {
2811 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2812 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002813 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002814 }
2815 }
2816 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 }
2818 }
2819
Michael Jurkac402cd92013-05-20 15:49:32 +02002820 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2821 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2822 PackageManager packageManager = context.getPackageManager();
2823 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2824 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2825 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2826 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2827 Collections.sort(widgetsAndShortcuts,
2828 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2829 return widgetsAndShortcuts;
2830 }
2831
Adam Cohen556f6132014-01-15 15:18:08 -08002832 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002833 try {
2834 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2835 return !pi.applicationInfo.enabled;
2836 } catch (NameNotFoundException e) {
2837 // Fall through
2838 }
2839 return false;
2840 }
Adam Cohen556f6132014-01-15 15:18:08 -08002841
2842 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002843 if (cn == null) {
2844 return false;
2845 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002846 if (isPackageDisabled(pm, cn.getPackageName())) {
2847 return false;
2848 }
Winson Chungee055712013-07-30 14:46:24 -07002849
2850 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002851 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002852 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002853 return (pm.getActivityInfo(cn, 0) != null);
2854 } catch (NameNotFoundException e) {
2855 return false;
2856 }
2857 }
2858
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002860 * Make an ShortcutInfo object for a restored application or shortcut item that points
2861 * to a package that is not yet installed on the system.
2862 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002863 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002864 final ShortcutInfo info = new ShortcutInfo();
2865 info.usingFallbackIcon = true;
2866 info.setIcon(getFallbackIcon());
2867 if (cursor != null) {
2868 info.title = cursor.getString(titleIndex);
2869 } else {
2870 info.title = "";
2871 }
2872 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002873 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002874 return info;
2875 }
2876
2877 /**
2878 * Make an Intent object for a restored application or shortcut item that points
2879 * to the market page for the item.
2880 */
2881 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002882 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002883 ComponentName componentName = intent.getComponent();
2884 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2885 Uri marketUri = new Uri.Builder()
2886 .scheme("market")
2887 .authority("details")
2888 .appendQueryParameter("id", componentName.getPackageName())
2889 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002890 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002891 marketIntent.setData(marketUri);
2892 return marketIntent;
2893 }
2894
2895 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002896 * This is called from the code that adds shortcuts from the intent receiver. This
2897 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898 */
Joe Onorato56d82912010-03-07 14:32:10 -05002899 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002900 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002902
Joe Onorato56d82912010-03-07 14:32:10 -05002903 /**
2904 * Make an ShortcutInfo object for a shortcut that is an application.
2905 *
2906 * If c is not null, then it will be used to fill in missing data like the title and icon.
2907 */
2908 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002909 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002910 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002911 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002912 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002913 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002914 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002915 } else {
2916 try {
2917 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2918 info.initFlagsAndFirstInstallTime(pi);
2919 } catch (NameNotFoundException e) {
2920 Log.d(TAG, "getPackInfo failed for package " +
2921 componentName.getPackageName());
2922 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002923 }
2924
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002925 // TODO: See if the PackageManager knows about this case. If it doesn't
2926 // then return null & delete this.
2927
Joe Onorato56d82912010-03-07 14:32:10 -05002928 // the resource -- This may implicitly give us back the fallback icon,
2929 // but don't worry about that. All we're doing with usingFallbackIcon is
2930 // to avoid saving lots of copies of that in the database, and most apps
2931 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002932
2933 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2934 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2935 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002936 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002937 ResolveInfo resolveInfo = null;
2938 ComponentName oldComponent = intent.getComponent();
2939 Intent newIntent = new Intent(intent.getAction(), null);
2940 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2941 newIntent.setPackage(oldComponent.getPackageName());
2942 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2943 for (ResolveInfo i : infos) {
2944 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2945 i.activityInfo.name);
2946 if (cn.equals(oldComponent)) {
2947 resolveInfo = i;
2948 }
2949 }
2950 if (resolveInfo == null) {
2951 resolveInfo = manager.resolveActivity(intent, 0);
2952 }
Joe Onorato56d82912010-03-07 14:32:10 -05002953 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002954 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002955 }
Joe Onorato56d82912010-03-07 14:32:10 -05002956 // the db
2957 if (icon == null) {
2958 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002959 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002960 }
2961 }
2962 // the fallback icon
2963 if (icon == null) {
2964 icon = getFallbackIcon();
2965 info.usingFallbackIcon = true;
2966 }
2967 info.setIcon(icon);
2968
2969 // from the resource
2970 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002971 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2972 if (labelCache != null && labelCache.containsKey(key)) {
2973 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002974 } else {
2975 info.title = resolveInfo.activityInfo.loadLabel(manager);
2976 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002977 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002978 }
2979 }
Joe Onorato56d82912010-03-07 14:32:10 -05002980 }
2981 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002982 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002983 if (c != null) {
2984 info.title = c.getString(titleIndex);
2985 }
2986 }
2987 // fall back to the class name of the activity
2988 if (info.title == null) {
2989 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2992 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002994
Winson Chung64359a52013-07-08 17:17:08 -07002995 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2996 ItemInfoFilter f) {
2997 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2998 for (ItemInfo i : infos) {
2999 if (i instanceof ShortcutInfo) {
3000 ShortcutInfo info = (ShortcutInfo) i;
3001 ComponentName cn = info.intent.getComponent();
3002 if (cn != null && f.filterItem(null, info, cn)) {
3003 filtered.add(info);
3004 }
3005 } else if (i instanceof FolderInfo) {
3006 FolderInfo info = (FolderInfo) i;
3007 for (ShortcutInfo s : info.contents) {
3008 ComponentName cn = s.intent.getComponent();
3009 if (cn != null && f.filterItem(info, s, cn)) {
3010 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003011 }
3012 }
Winson Chung64359a52013-07-08 17:17:08 -07003013 } else if (i instanceof LauncherAppWidgetInfo) {
3014 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3015 ComponentName cn = info.providerName;
3016 if (cn != null && f.filterItem(null, info, cn)) {
3017 filtered.add(info);
3018 }
Winson Chung8a435102012-08-30 17:16:53 -07003019 }
3020 }
Winson Chung64359a52013-07-08 17:17:08 -07003021 return new ArrayList<ItemInfo>(filtered);
3022 }
3023
3024 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003025 ItemInfoFilter filter = new ItemInfoFilter() {
3026 @Override
3027 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3028 return cn.getPackageName().equals(pn);
3029 }
3030 };
3031 return filterItemInfos(sBgItemsIdMap.values(), filter);
3032 }
3033
3034 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003035 ItemInfoFilter filter = new ItemInfoFilter() {
3036 @Override
3037 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3038 return cn.equals(cname);
3039 }
3040 };
3041 return filterItemInfos(sBgItemsIdMap.values(), filter);
3042 }
3043
3044 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3045 if (i instanceof ShortcutInfo) {
3046 ShortcutInfo info = (ShortcutInfo) i;
3047 // We need to check for ACTION_MAIN otherwise getComponent() might
3048 // return null for some shortcuts (for instance, for shortcuts to
3049 // web pages.)
3050 Intent intent = info.intent;
3051 ComponentName name = intent.getComponent();
3052 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3053 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3054 return true;
3055 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003056 // placeholder shortcuts get special treatment, let them through too.
3057 if (info.getRestoredIntent() != null) {
3058 return true;
3059 }
Winson Chung64359a52013-07-08 17:17:08 -07003060 }
3061 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003062 }
3063
3064 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003065 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003067 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003068 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3069 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070
Joe Onorato56d82912010-03-07 14:32:10 -05003071 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003072 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003075 // TODO: If there's an explicit component and we can't install that, delete it.
3076
Joe Onorato56d82912010-03-07 14:32:10 -05003077 info.title = c.getString(titleIndex);
3078
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 int iconType = c.getInt(iconTypeIndex);
3080 switch (iconType) {
3081 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3082 String packageName = c.getString(iconPackageIndex);
3083 String resourceName = c.getString(iconResourceIndex);
3084 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003085 info.customIcon = false;
3086 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003089 if (resources != null) {
3090 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003091 icon = Utilities.createIconBitmap(
3092 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003093 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003095 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 }
Joe Onorato56d82912010-03-07 14:32:10 -05003097 // the db
3098 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003099 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003100 }
3101 // the fallback icon
3102 if (icon == null) {
3103 icon = getFallbackIcon();
3104 info.usingFallbackIcon = true;
3105 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 break;
3107 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003108 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003109 if (icon == null) {
3110 icon = getFallbackIcon();
3111 info.customIcon = false;
3112 info.usingFallbackIcon = true;
3113 } else {
3114 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 break;
3117 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003118 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003119 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 info.customIcon = false;
3121 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003123 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 return info;
3125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126
Michael Jurka931dc972011-08-05 15:08:15 -07003127 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003128 @SuppressWarnings("all") // suppress dead code warning
3129 final boolean debug = false;
3130 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003131 Log.d(TAG, "getIconFromCursor app="
3132 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3133 }
3134 byte[] data = c.getBlob(iconIndex);
3135 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003136 return Utilities.createIconBitmap(
3137 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003138 } catch (Exception e) {
3139 return null;
3140 }
3141 }
3142
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3144 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003145 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003146 if (info == null) {
3147 return null;
3148 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003150
3151 return info;
3152 }
3153
Winson Chunga9abd0e2010-10-27 17:18:37 -07003154 /**
Winson Chung55cef262010-10-28 14:14:18 -07003155 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3156 */
3157 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3158 ComponentName component) {
3159 List<AppWidgetProviderInfo> widgets =
3160 AppWidgetManager.getInstance(context).getInstalledProviders();
3161 for (AppWidgetProviderInfo info : widgets) {
3162 if (info.provider.equals(component)) {
3163 return info;
3164 }
3165 }
3166 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003167 }
3168
Winson Chung68846fd2010-10-29 11:00:27 -07003169 /**
3170 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3171 */
3172 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3173 final PackageManager packageManager = context.getPackageManager();
3174 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3175 new ArrayList<WidgetMimeTypeHandlerData>();
3176
3177 final Intent supportsIntent =
3178 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3179 supportsIntent.setType(mimeType);
3180
3181 // Create a set of widget configuration components that we can test against
3182 final List<AppWidgetProviderInfo> widgets =
3183 AppWidgetManager.getInstance(context).getInstalledProviders();
3184 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3185 new HashMap<ComponentName, AppWidgetProviderInfo>();
3186 for (AppWidgetProviderInfo info : widgets) {
3187 configurationComponentToWidget.put(info.configure, info);
3188 }
3189
3190 // Run through each of the intents that can handle this type of clip data, and cross
3191 // reference them with the components that are actual configuration components
3192 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3193 PackageManager.MATCH_DEFAULT_ONLY);
3194 for (ResolveInfo info : activities) {
3195 final ActivityInfo activityInfo = info.activityInfo;
3196 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3197 activityInfo.name);
3198 if (configurationComponentToWidget.containsKey(infoComponent)) {
3199 supportedConfigurationActivities.add(
3200 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3201 configurationComponentToWidget.get(infoComponent)));
3202 }
3203 }
3204 return supportedConfigurationActivities;
3205 }
3206
Winson Chunga9abd0e2010-10-27 17:18:37 -07003207 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003208 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3209 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3210 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3211
Adam Cohend9198822011-11-22 16:42:47 -08003212 if (intent == null) {
3213 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3214 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3215 return null;
3216 }
3217
Joe Onorato0589f0f2010-02-08 13:44:00 -08003218 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003219 boolean customIcon = false;
3220 ShortcutIconResource iconResource = null;
3221
3222 if (bitmap != null && bitmap instanceof Bitmap) {
3223 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003224 customIcon = true;
3225 } else {
3226 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3227 if (extra != null && extra instanceof ShortcutIconResource) {
3228 try {
3229 iconResource = (ShortcutIconResource) extra;
3230 final PackageManager packageManager = context.getPackageManager();
3231 Resources resources = packageManager.getResourcesForApplication(
3232 iconResource.packageName);
3233 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003234 icon = Utilities.createIconBitmap(
3235 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003236 } catch (Exception e) {
3237 Log.w(TAG, "Could not load shortcut icon: " + extra);
3238 }
3239 }
3240 }
3241
Michael Jurkac9d95c52011-08-29 14:03:34 -07003242 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003243
3244 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003245 if (fallbackIcon != null) {
3246 icon = fallbackIcon;
3247 } else {
3248 icon = getFallbackIcon();
3249 info.usingFallbackIcon = true;
3250 }
Joe Onorato56d82912010-03-07 14:32:10 -05003251 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003252 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003253
Joe Onorato0589f0f2010-02-08 13:44:00 -08003254 info.title = name;
3255 info.intent = intent;
3256 info.customIcon = customIcon;
3257 info.iconResource = iconResource;
3258
3259 return info;
3260 }
3261
Winson Chungaac01e12011-08-17 10:37:13 -07003262 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3263 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003264 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003265 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003266 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003267 }
Joe Onorato56d82912010-03-07 14:32:10 -05003268 // If this icon doesn't have a custom icon, check to see
3269 // what's stored in the DB, and if it doesn't match what
3270 // we're going to show, store what we are going to show back
3271 // into the DB. We do this so when we're loading, if the
3272 // package manager can't find an icon (for example because
3273 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003274 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003275 cache.put(info, c.getBlob(iconIndex));
3276 return true;
3277 }
3278 return false;
3279 }
3280 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3281 boolean needSave = false;
3282 try {
3283 if (data != null) {
3284 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3285 Bitmap loaded = info.getIcon(mIconCache);
3286 needSave = !saved.sameAs(loaded);
3287 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003288 needSave = true;
3289 }
Winson Chungaac01e12011-08-17 10:37:13 -07003290 } catch (Exception e) {
3291 needSave = true;
3292 }
3293 if (needSave) {
3294 Log.d(TAG, "going to save icon bitmap for info=" + info);
3295 // This is slower than is ideal, but this only happens once
3296 // or when the app is updated with a new icon.
3297 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003298 }
3299 }
3300
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003302 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 * or make a new one.
3304 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003305 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 // See if a placeholder was created for us already
3307 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003308 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003309 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003310 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 folders.put(id, folderInfo);
3312 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003313 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314 }
3315
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003316 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003317 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003318 return new Comparator<AppInfo>() {
3319 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003320 int result = collator.compare(a.title.toString().trim(),
3321 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003322 if (result == 0) {
3323 result = a.componentName.compareTo(b.componentName);
3324 }
3325 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003326 }
Winson Chung11904872012-09-17 16:58:46 -07003327 };
3328 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003329 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3330 = new Comparator<AppInfo>() {
3331 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003332 if (a.firstInstallTime < b.firstInstallTime) return 1;
3333 if (a.firstInstallTime > b.firstInstallTime) return -1;
3334 return 0;
3335 }
3336 };
Winson Chung11904872012-09-17 16:58:46 -07003337 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3338 final Collator collator = Collator.getInstance();
3339 return new Comparator<AppWidgetProviderInfo>() {
3340 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003341 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003342 }
3343 };
3344 }
Winson Chung5308f242011-08-18 12:12:41 -07003345 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3346 if (info.activityInfo != null) {
3347 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3348 } else {
3349 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3350 }
3351 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003352 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003353 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003354 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003355 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003356 ShortcutNameComparator(PackageManager pm) {
3357 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003358 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003359 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003360 }
3361 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3362 mPackageManager = pm;
3363 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003364 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003365 }
3366 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003367 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003368 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3369 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3370 if (mLabelCache.containsKey(keyA)) {
3371 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003372 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003373 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003374
Winson Chung5308f242011-08-18 12:12:41 -07003375 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003376 }
Winson Chung5308f242011-08-18 12:12:41 -07003377 if (mLabelCache.containsKey(keyB)) {
3378 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003379 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003380 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003381
Winson Chung5308f242011-08-18 12:12:41 -07003382 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003383 }
Winson Chung11904872012-09-17 16:58:46 -07003384 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003385 }
3386 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003387 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003388 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003389 private PackageManager mPackageManager;
3390 private HashMap<Object, String> mLabelCache;
3391 WidgetAndShortcutNameComparator(PackageManager pm) {
3392 mPackageManager = pm;
3393 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003394 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003395 }
3396 public final int compare(Object a, Object b) {
3397 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003398 if (mLabelCache.containsKey(a)) {
3399 labelA = mLabelCache.get(a);
3400 } else {
3401 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003402 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003403 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003404 mLabelCache.put(a, labelA);
3405 }
3406 if (mLabelCache.containsKey(b)) {
3407 labelB = mLabelCache.get(b);
3408 } else {
3409 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003410 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003411 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003412 mLabelCache.put(b, labelB);
3413 }
Winson Chung11904872012-09-17 16:58:46 -07003414 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003415 }
3416 };
Joe Onoratobe386092009-11-17 17:32:16 -08003417
3418 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003419 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003420 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3421 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3422 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3423 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003424 if (mLoaderTask != null) {
3425 mLoaderTask.dumpState();
3426 } else {
3427 Log.d(TAG, "mLoaderTask=null");
3428 }
Joe Onoratobe386092009-11-17 17:32:16 -08003429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003430}