blob: 5f8f80cea43c0d6877a6982c3a29cc929800e0d7 [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 Chungeb23cb22014-03-06 10:39:43 -0800194 ContentProviderClient client = contentResolver.acquireContentProviderClient(
195 LauncherSettings.Favorites.OLD_CONTENT_URI);
196 mOldContentProviderExists = (client != null);
197 if (client != null) {
198 client.release();
199 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400200 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100201 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800202 mIconCache = iconCache;
203
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400204 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700205 Configuration config = res.getConfiguration();
206 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 }
208
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700209 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
210 * posted on the main thread handler. */
211 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700212 runOnMainThread(r, 0);
213 }
214 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700215 if (sWorkerThread.getThreadId() == Process.myTid()) {
216 // If we are on the worker thread, post onto the main handler
217 mHandler.post(r);
218 } else {
219 r.run();
220 }
221 }
222
223 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
224 * posted on the worker thread handler. */
225 private static void runOnWorkerThread(Runnable r) {
226 if (sWorkerThread.getThreadId() == Process.myTid()) {
227 r.run();
228 } else {
229 // If we are not on the worker thread, then post to the worker handler
230 sWorker.post(r);
231 }
232 }
233
Winson Chunge43a1e72014-01-15 10:33:02 -0800234 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
235 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800236 }
237
Winson Chungc9168342013-06-26 14:54:55 -0700238 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
239 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700240 LauncherAppState app = LauncherAppState.getInstance();
241 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
242 final int xCount = (int) grid.numColumns;
243 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700244 boolean[][] occupied = new boolean[xCount][yCount];
245
246 int cellX, cellY, spanX, spanY;
247 for (int i = 0; i < items.size(); ++i) {
248 final ItemInfo item = items.get(i);
249 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
250 if (item.screenId == screen) {
251 cellX = item.cellX;
252 cellY = item.cellY;
253 spanX = item.spanX;
254 spanY = item.spanY;
255 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
256 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
257 occupied[x][y] = true;
258 }
259 }
260 }
261 }
262 }
263
264 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
265 }
266 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700267 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700268 int firstScreenIndex,
269 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700270 // Lock on the app so that we don't try and get the items while apps are being added
271 LauncherAppState app = LauncherAppState.getInstance();
272 LauncherModel model = app.getModel();
273 boolean found = false;
274 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700275 if (sWorkerThread.getThreadId() != Process.myTid()) {
276 // Flush the LauncherModel worker thread, so that if we just did another
277 // processInstallShortcut, we give it time for its shortcut to get added to the
278 // database (getItemsInLocalCoordinates reads the database)
279 model.flushWorkerThread();
280 }
Winson Chungc9168342013-06-26 14:54:55 -0700281 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700282
283 // Try adding to the workspace screens incrementally, starting at the default or center
284 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700285 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
286 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700287 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700288 int[] tmpCoordinates = new int[2];
289 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700290 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700291 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700292 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700293 }
294 }
295 }
Winson Chungc9168342013-06-26 14:54:55 -0700296 return null;
297 }
298
Adam Cohen76a47a12014-02-05 11:47:43 -0800299 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
300 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
301
302 if (allAppsApps == null) {
303 throw new RuntimeException("allAppsApps must not be null");
304 }
305 if (allAppsApps.isEmpty()) {
306 return;
307 }
308
Chris Wrenb6d4c282014-01-27 14:17:08 -0500309 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
310 Iterator<AppInfo> iter = allAppsApps.iterator();
311 while (iter.hasNext()) {
312 ItemInfo a = iter.next();
313 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
314 restoredAppsFinal.add((AppInfo) a);
315 }
316 }
317
Adam Cohen76a47a12014-02-05 11:47:43 -0800318 // Process the newly added applications and add them to the database first
319 Runnable r = new Runnable() {
320 public void run() {
321 runOnMainThread(new Runnable() {
322 public void run() {
323 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
324 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500325 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500326 for (AppInfo info : restoredAppsFinal) {
327 final Intent intent = info.getIntent();
328 if (intent != null) {
329 mIconCache.deletePreloadedIcon(intent.getComponent());
330 }
331 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500332 callbacks.bindAppsUpdated(restoredAppsFinal);
333 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500334 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800335 }
336 }
337 });
338 }
339 };
340 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700341 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800342
343 public void addAndBindAddedWorkspaceApps(final Context context,
344 final ArrayList<ItemInfo> workspaceApps) {
345 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
346
347 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800348 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
349 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800350 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700351 return;
Winson Chung997a9232013-07-24 15:33:46 -0700352 }
Winson Chung64359a52013-07-08 17:17:08 -0700353 // Process the newly added applications and add them to the database first
354 Runnable r = new Runnable() {
355 public void run() {
356 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
357 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500358 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700359
Winson Chung76828c82013-08-19 15:43:29 -0700360 // Get the list of workspace screens. We need to append to this list and
361 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
362 // called.
363 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
364 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
365 for (Integer i : orderedScreens.keySet()) {
366 long screenId = orderedScreens.get(i);
367 workspaceScreens.add(screenId);
368 }
369
Winson Chung64359a52013-07-08 17:17:08 -0700370 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700371 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700372 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700373 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700374 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700375 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700376
377 // Short-circuit this logic if the icon exists somewhere on the workspace
378 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500379 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
380 if (a instanceof AppInfo &&
381 LauncherModel.appWasRestored(context, launchIntent)) {
382 restoredAppsFinal.add((AppInfo) a);
383 }
Winson Chung64359a52013-07-08 17:17:08 -0700384 continue;
385 }
386
Winson Chung87412982013-10-03 18:34:14 -0700387 // Add this icon to the db, creating a new page if necessary. If there
388 // is only the empty page then we just add items to the first page.
389 // Otherwise, we add them to the next pages.
390 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700391 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700392 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700393 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200394 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700395
396 // If we can't find a valid position, then just add a new screen.
397 // This takes time so we need to re-queue the add until the new
398 // page is added. Create as many screens as necessary to satisfy
399 // the startSearchPageIndex.
400 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700401 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700402 while (numPagesToAdd > 0) {
403 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700404 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700405 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700406 addedWorkspaceScreensFinal.add(screenId);
407 numPagesToAdd--;
408 }
Winson Chung76828c82013-08-19 15:43:29 -0700409
Winson Chung64359a52013-07-08 17:17:08 -0700410 // Find the coordinate again
411 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700412 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700413 }
414 if (coords == null) {
415 throw new RuntimeException("Coordinates should not be null");
416 }
417
Winson Chung997a9232013-07-24 15:33:46 -0700418 ShortcutInfo shortcutInfo;
419 if (a instanceof ShortcutInfo) {
420 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200421 } else if (a instanceof AppInfo) {
422 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700423 } else {
424 throw new RuntimeException("Unexpected info type");
425 }
Winson Chung94d67682013-09-25 16:29:40 -0700426
Winson Chung64359a52013-07-08 17:17:08 -0700427 // Add the shortcut to the db
428 addItemToDatabase(context, shortcutInfo,
429 LauncherSettings.Favorites.CONTAINER_DESKTOP,
430 coords.first, coords.second[0], coords.second[1], false);
431 // Save the ShortcutInfo for binding in the workspace
432 addedShortcutsFinal.add(shortcutInfo);
433 }
434 }
435
Winson Chung76828c82013-08-19 15:43:29 -0700436 // Update the workspace screens
437 updateWorkspaceScreenOrder(context, workspaceScreens);
438
Adam Cohen76a47a12014-02-05 11:47:43 -0800439 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700440 runOnMainThread(new Runnable() {
441 public void run() {
442 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
443 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700444 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
445 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700446 if (!addedShortcutsFinal.isEmpty()) {
447 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
448 long lastScreenId = info.screenId;
449 for (ItemInfo i : addedShortcutsFinal) {
450 if (i.screenId == lastScreenId) {
451 addAnimated.add(i);
452 } else {
453 addNotAnimated.add(i);
454 }
Winson Chung997a9232013-07-24 15:33:46 -0700455 }
456 }
Winson Chungd64d1762013-08-20 14:37:16 -0700457 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800458 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500459 if (!restoredAppsFinal.isEmpty()) {
460 callbacks.bindAppsUpdated(restoredAppsFinal);
461 }
Winson Chung997a9232013-07-24 15:33:46 -0700462 }
Winson Chung64359a52013-07-08 17:17:08 -0700463 }
Winson Chung997a9232013-07-24 15:33:46 -0700464 });
465 }
Winson Chung64359a52013-07-08 17:17:08 -0700466 }
467 };
468 runOnWorkerThread(r);
469 }
470
Joe Onorato56d82912010-03-07 14:32:10 -0500471 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700472 if (mDefaultIcon == null) {
473 final Context context = LauncherAppState.getInstance().getContext();
474 mDefaultIcon = Utilities.createIconBitmap(
475 mIconCache.getFullResDefaultActivityIcon(), context);
476 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800477 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479
Winson Chung81b52252012-08-27 15:34:29 -0700480 public void unbindItemInfosAndClearQueuedBindRunnables() {
481 if (sWorkerThread.getThreadId() == Process.myTid()) {
482 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
483 "main thread");
484 }
485
486 // Clear any deferred bind runnables
487 mDeferredBindRunnables.clear();
488 // Remove any queued bind runnables
489 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
490 // Unbind all the workspace items
491 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700492 }
493
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700494 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700495 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700496 // Ensure that we don't use the same workspace items data structure on the main thread
497 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700498 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
499 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700500 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700501 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
502 tmpAppWidgets.addAll(sBgAppWidgets);
503 }
504 Runnable r = new Runnable() {
505 @Override
506 public void run() {
507 for (ItemInfo item : tmpWorkspaceItems) {
508 item.unbind();
509 }
510 for (ItemInfo item : tmpAppWidgets) {
511 item.unbind();
512 }
513 }
514 };
515 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700516 }
517
Joe Onorato9c1289c2009-08-17 11:03:03 -0400518 /**
519 * Adds an item to the DB if it was not created previously, or move it to a new
520 * <container, screen, cellX, cellY>
521 */
522 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700523 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 if (item.container == ItemInfo.NO_ID) {
525 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700526 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400527 } else {
528 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700529 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
531 }
532
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700533 static void checkItemInfoLocked(
534 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
535 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
536 if (modelItem != null && item != modelItem) {
537 // check all the data is consistent
538 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
539 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
540 ShortcutInfo shortcut = (ShortcutInfo) item;
541 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
542 modelShortcut.intent.filterEquals(shortcut.intent) &&
543 modelShortcut.id == shortcut.id &&
544 modelShortcut.itemType == shortcut.itemType &&
545 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700546 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700547 modelShortcut.cellX == shortcut.cellX &&
548 modelShortcut.cellY == shortcut.cellY &&
549 modelShortcut.spanX == shortcut.spanX &&
550 modelShortcut.spanY == shortcut.spanY &&
551 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
552 (modelShortcut.dropPos != null &&
553 shortcut.dropPos != null &&
554 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
555 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
556 // For all intents and purposes, this is the same object
557 return;
558 }
559 }
560
561 // the modelItem needs to match up perfectly with item if our model is
562 // to be consistent with the database-- for now, just require
563 // modelItem == item or the equality check above
564 String msg = "item: " + ((item != null) ? item.toString() : "null") +
565 "modelItem: " +
566 ((modelItem != null) ? modelItem.toString() : "null") +
567 "Error: ItemInfo passed to checkItemInfo doesn't match original";
568 RuntimeException e = new RuntimeException(msg);
569 if (stackTrace != null) {
570 e.setStackTrace(stackTrace);
571 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800572 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700573 }
574 }
575
Michael Jurka816474f2012-06-25 14:49:02 -0700576 static void checkItemInfo(final ItemInfo item) {
577 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
578 final long itemId = item.id;
579 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700580 public void run() {
581 synchronized (sBgLock) {
582 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700583 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700584 }
585 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700586 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700587 }
588
Michael Jurkac9d95c52011-08-29 14:03:34 -0700589 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
590 final ItemInfo item, final String callingFunction) {
591 final long itemId = item.id;
592 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
593 final ContentResolver cr = context.getContentResolver();
594
Adam Cohen487f7dd2012-06-28 18:12:10 -0700595 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700596 Runnable r = new Runnable() {
597 public void run() {
598 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700599 updateItemArrays(item, itemId, stackTrace);
600 }
601 };
602 runOnWorkerThread(r);
603 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700604
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700605 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
606 final ArrayList<ItemInfo> items, final String callingFunction) {
607 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700608
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700609 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
610 Runnable r = new Runnable() {
611 public void run() {
612 ArrayList<ContentProviderOperation> ops =
613 new ArrayList<ContentProviderOperation>();
614 int count = items.size();
615 for (int i = 0; i < count; i++) {
616 ItemInfo item = items.get(i);
617 final long itemId = item.id;
618 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
619 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700620
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700621 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
622 updateItemArrays(item, itemId, stackTrace);
623
624 }
625 try {
626 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
627 } catch (Exception e) {
628 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700629 }
630 }
631 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700632 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700633 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700634
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700635 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
636 // Lock on mBgLock *after* the db operation
637 synchronized (sBgLock) {
638 checkItemInfoLocked(itemId, item, stackTrace);
639
640 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
641 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
642 // Item is in a folder, make sure this folder exists
643 if (!sBgFolders.containsKey(item.container)) {
644 // An items container is being set to a that of an item which is not in
645 // the list of Folders.
646 String msg = "item: " + item + " container being set to: " +
647 item.container + ", not in the list of folders";
648 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 }
650 }
651
652 // Items are added/removed from the corresponding FolderInfo elsewhere, such
653 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
654 // that are on the desktop, as appropriate
655 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800656 if (modelItem != null &&
657 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
658 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 switch (modelItem.itemType) {
660 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
661 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
662 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
663 if (!sBgWorkspaceItems.contains(modelItem)) {
664 sBgWorkspaceItems.add(modelItem);
665 }
666 break;
667 default:
668 break;
669 }
670 } else {
671 sBgWorkspaceItems.remove(modelItem);
672 }
673 }
674 }
675
Michael Jurkac7700af2013-05-14 20:17:58 +0200676 public void flushWorkerThread() {
677 mFlushingWorkerThread = true;
678 Runnable waiter = new Runnable() {
679 public void run() {
680 synchronized (this) {
681 notifyAll();
682 mFlushingWorkerThread = false;
683 }
684 }
685 };
686
687 synchronized(waiter) {
688 runOnWorkerThread(waiter);
689 if (mLoaderTask != null) {
690 synchronized(mLoaderTask) {
691 mLoaderTask.notify();
692 }
693 }
694 boolean success = false;
695 while (!success) {
696 try {
697 waiter.wait();
698 success = true;
699 } catch (InterruptedException e) {
700 }
701 }
702 }
703 }
704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700707 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700708 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 item.cellX = cellX;
712 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700713
Winson Chung3d503fb2011-07-13 17:25:49 -0700714 // We store hotseat items in canonical form which is this orientation invariant position
715 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700719 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722
723 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
726 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700730 }
731
732 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700733 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
734 * cellX, cellY have already been updated on the ItemInfos.
735 */
736 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
737 final long container, final int screen) {
738
739 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
740 int count = items.size();
741
742 for (int i = 0; i < count; i++) {
743 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700744 item.container = container;
745
746 // We store hotseat items in canonical form which is this orientation invariant position
747 // in the hotseat
748 if (context instanceof Launcher && screen < 0 &&
749 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 item.cellY);
752 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700754 }
755
756 final ContentValues values = new ContentValues();
757 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
758 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
759 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700760 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700761
762 contentValues.add(values);
763 }
764 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
765 }
766
767 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700768 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800769 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700770 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700772 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800773 item.cellX = cellX;
774 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700775 item.spanX = spanX;
776 item.spanY = spanY;
777
778 // We store hotseat items in canonical form which is this orientation invariant position
779 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700781 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700783 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700785 }
Adam Cohend4844c32011-02-18 19:25:06 -0800786
Adam Cohend4844c32011-02-18 19:25:06 -0800787 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800788 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
790 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
791 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
792 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800794
Michael Jurka816474f2012-06-25 14:49:02 -0700795 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700796 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700797
798 /**
799 * Update an item to the database in a specified container.
800 */
801 static void updateItemInDatabase(Context context, final ItemInfo item) {
802 final ContentValues values = new ContentValues();
803 item.onAddToDatabase(values);
804 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
805 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800806 }
807
808 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 * Returns true if the shortcuts already exists in the database.
810 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 static boolean shortcutExists(Context context, String title, Intent intent) {
813 final ContentResolver cr = context.getContentResolver();
814 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
815 new String[] { "title", "intent" }, "title=? and intent=?",
816 new String[] { title, intent.toUri(0) }, null);
817 boolean result = false;
818 try {
819 result = c.moveToFirst();
820 } finally {
821 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700824 }
825
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500827 * Returns true if the shortcuts already exists in the database.
828 * we identify a shortcut by the component name of the intent.
829 */
830 static boolean appWasRestored(Context context, Intent intent) {
831 final ContentResolver cr = context.getContentResolver();
832 final ComponentName component = intent.getComponent();
833 if (component == null) {
834 return false;
835 }
836 String componentName = component.flattenToString();
837 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
838 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
839 new String[]{"intent", "restored"}, where, null, null);
840 boolean result = false;
841 try {
842 result = c.moveToFirst();
843 } finally {
844 c.close();
845 }
846 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
847 return result;
848 }
849
850 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700851 * Returns an ItemInfo array containing all the items in the LauncherModel.
852 * The ItemInfo.id is not set through this function.
853 */
854 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
855 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
856 final ContentResolver cr = context.getContentResolver();
857 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
858 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
859 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
860 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
861
862 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
863 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
864 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
865 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
866 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
867 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
868 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
869
870 try {
871 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700873 item.cellX = c.getInt(cellXIndex);
874 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700875 item.spanX = Math.max(1, c.getInt(spanXIndex));
876 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700877 item.container = c.getInt(containerIndex);
878 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700880
881 items.add(item);
882 }
883 } catch (Exception e) {
884 items.clear();
885 } finally {
886 c.close();
887 }
888
889 return items;
890 }
891
892 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
894 */
895 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
896 final ContentResolver cr = context.getContentResolver();
897 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
898 "_id=? and (itemType=? or itemType=?)",
899 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700900 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 try {
903 if (c.moveToFirst()) {
904 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
905 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
906 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
907 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
908 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
909 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 FolderInfo folderInfo = null;
912 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700913 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
914 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700916 }
917
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 folderInfo.title = c.getString(titleIndex);
919 folderInfo.id = id;
920 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700922 folderInfo.cellX = c.getInt(cellXIndex);
923 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924
925 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700926 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 } finally {
928 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700929 }
930
931 return null;
932 }
933
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 /**
935 * Add an item to the database in a specified container. Sets the container, screen, cellX and
936 * cellY fields of the item. Also assigns an ID to the item.
937 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 item.cellX = cellX;
942 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 // We store hotseat items in canonical form which is this orientation invariant position
944 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951
952 final ContentValues values = new ContentValues();
953 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 item.onAddToDatabase(values);
955
Michael Jurka414300a2013-08-27 15:42:35 +0200956 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700957 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700959
Michael Jurkac9d95c52011-08-29 14:03:34 -0700960 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700961 public void run() {
962 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
963 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700965 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700966 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700967 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700968 sBgItemsIdMap.put(item.id, item);
969 switch (item.itemType) {
970 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
971 sBgFolders.put(item.id, (FolderInfo) item);
972 // Fall through
973 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
974 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
975 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
976 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
977 sBgWorkspaceItems.add(item);
978 } else {
979 if (!sBgFolders.containsKey(item.container)) {
980 // Adding an item to a folder that doesn't exist.
981 String msg = "adding item: " + item + " to a folder that " +
982 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700983 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700984 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700985 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700986 break;
987 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
988 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
989 break;
990 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700991 }
992 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700993 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700994 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700995 }
996
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700998 * Creates a new unique child id, for a given cell span across all layouts.
999 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001000 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001004 }
1005
Winson Chungaafa03c2010-06-11 17:34:16 -07001006 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 * Removes the specified item from the database
1008 * @param context
1009 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001014
Michael Jurka83df1882011-08-31 20:59:26 -07001015 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001017 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018
1019 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001020 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001021 switch (item.itemType) {
1022 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1023 sBgFolders.remove(item.id);
1024 for (ItemInfo info: sBgItemsIdMap.values()) {
1025 if (info.container == item.id) {
1026 // We are deleting a folder which still contains items that
1027 // think they are contained by that folder.
1028 String msg = "deleting a folder (" + item + ") which still " +
1029 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001030 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001031 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001032 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001033 sBgWorkspaceItems.remove(item);
1034 break;
1035 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1036 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1037 sBgWorkspaceItems.remove(item);
1038 break;
1039 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1040 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1041 break;
1042 }
1043 sBgItemsIdMap.remove(item.id);
1044 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001045 }
1046 }
Michael Jurka83df1882011-08-31 20:59:26 -07001047 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 }
1050
1051 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001052 * Update the order of the workspace screens in the database. The array list contains
1053 * a list of screen ids in the order that they should appear.
1054 */
Winson Chungc9168342013-06-26 14:54:55 -07001055 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001056 // Log to disk
1057 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1058 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1059
Winson Chung64359a52013-07-08 17:17:08 -07001060 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001061 final ContentResolver cr = context.getContentResolver();
1062 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1063
1064 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001065 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 while (iter.hasNext()) {
1067 long id = iter.next();
1068 if (id < 0) {
1069 iter.remove();
1070 }
1071 }
1072
1073 Runnable r = new Runnable() {
1074 @Override
1075 public void run() {
Yura085c8532014-02-11 15:15:29 +00001076 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001078 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001079 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 for (int i = 0; i < count; i++) {
1081 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001082 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001083 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1084 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001085 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001086 }
Yura085c8532014-02-11 15:15:29 +00001087
1088 try {
1089 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1090 } catch (Exception ex) {
1091 throw new RuntimeException(ex);
1092 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001093
Winson Chungba9c37f2013-08-30 14:11:37 -07001094 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001095 sBgWorkspaceScreens.clear();
1096 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001097 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 }
1099 };
1100 runOnWorkerThread(r);
1101 }
1102
1103 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 * Remove the contents of the specified folder from the database
1105 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001106 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 final ContentResolver cr = context.getContentResolver();
1108
Michael Jurkac9d95c52011-08-29 14:03:34 -07001109 Runnable r = new Runnable() {
1110 public void run() {
1111 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, 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 sBgItemsIdMap.remove(info.id);
1115 sBgFolders.remove(info.id);
1116 sBgDbIconCache.remove(info);
1117 sBgWorkspaceItems.remove(info);
1118 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001119
Michael Jurkac9d95c52011-08-29 14:03:34 -07001120 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1121 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001123 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001124 for (ItemInfo childInfo : info.contents) {
1125 sBgItemsIdMap.remove(childInfo.id);
1126 sBgDbIconCache.remove(childInfo);
1127 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001128 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001129 }
1130 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001131 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
1133
1134 /**
1135 * Set this as the current Launcher activity object for the loader.
1136 */
1137 public void initialize(Callbacks callbacks) {
1138 synchronized (mLock) {
1139 mCallbacks = new WeakReference<Callbacks>(callbacks);
1140 }
1141 }
1142
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001143 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1145 * ACTION_PACKAGE_CHANGED.
1146 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001147 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001148 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001149 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001150
Joe Onorato36115782010-06-17 13:28:48 -04001151 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001152
Joe Onorato36115782010-06-17 13:28:48 -04001153 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1154 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1155 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1156 final String packageName = intent.getData().getSchemeSpecificPart();
1157 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001158
Joe Onorato36115782010-06-17 13:28:48 -04001159 int op = PackageUpdatedTask.OP_NONE;
1160
1161 if (packageName == null || packageName.length() == 0) {
1162 // they sent us a bad intent
1163 return;
1164 }
1165
1166 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1167 op = PackageUpdatedTask.OP_UPDATE;
1168 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1169 if (!replacing) {
1170 op = PackageUpdatedTask.OP_REMOVE;
1171 }
1172 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1173 // later, we will update the package at this time
1174 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1175 if (!replacing) {
1176 op = PackageUpdatedTask.OP_ADD;
1177 } else {
1178 op = PackageUpdatedTask.OP_UPDATE;
1179 }
1180 }
1181
1182 if (op != PackageUpdatedTask.OP_NONE) {
1183 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1184 }
1185
1186 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001187 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001188 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001189 if (!replacing) {
1190 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1191 if (mAppsCanBeOnRemoveableStorage) {
1192 // Only rebind if we support removable storage. It catches the case where
1193 // apps on the external sd card need to be reloaded
1194 startLoaderFromBackground();
1195 }
1196 } else {
1197 // If we are replacing then just update the packages in the list
1198 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1199 packages));
1200 }
Joe Onorato36115782010-06-17 13:28:48 -04001201 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001202 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1203 if (!replacing) {
1204 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1205 enqueuePackageUpdated(new PackageUpdatedTask(
1206 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1207 }
1208 // else, we are replacing the packages, so ignore this event and wait for
1209 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001210 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001211 // If we have changed locale we need to clear out the labels in all apps/workspace.
1212 forceReload();
1213 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1214 // Check if configuration change was an mcc/mnc change which would affect app resources
1215 // and we would need to clear out the labels in all apps/workspace. Same handling as
1216 // above for ACTION_LOCALE_CHANGED
1217 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001218 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001219 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001220 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001221 forceReload();
1222 }
1223 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001224 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001225 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1226 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001227 if (mCallbacks != null) {
1228 Callbacks callbacks = mCallbacks.get();
1229 if (callbacks != null) {
1230 callbacks.bindSearchablesChanged();
1231 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001232 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001233 }
1234 }
1235
Reena Lee93f824a2011-09-23 17:20:28 -07001236 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001237 resetLoadedState(true, true);
1238
Reena Lee93f824a2011-09-23 17:20:28 -07001239 // Do this here because if the launcher activity is running it will be restarted.
1240 // If it's not running startLoaderFromBackground will merely tell it that it needs
1241 // to reload.
1242 startLoaderFromBackground();
1243 }
1244
Winson Chungf0c6ae02012-03-21 16:10:31 -07001245 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1246 synchronized (mLock) {
1247 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1248 // mWorkspaceLoaded to true later
1249 stopLoaderLocked();
1250 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1251 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1252 }
1253 }
1254
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001255 /**
1256 * When the launcher is in the background, it's possible for it to miss paired
1257 * configuration changes. So whenever we trigger the loader from the background
1258 * tell the launcher that it needs to re-run the loader when it comes back instead
1259 * of doing it now.
1260 */
1261 public void startLoaderFromBackground() {
1262 boolean runLoader = false;
1263 if (mCallbacks != null) {
1264 Callbacks callbacks = mCallbacks.get();
1265 if (callbacks != null) {
1266 // Only actually run the loader if they're not paused.
1267 if (!callbacks.setLoadOnResume()) {
1268 runLoader = true;
1269 }
1270 }
1271 }
1272 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001273 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001274 }
Joe Onorato36115782010-06-17 13:28:48 -04001275 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001276
Reena Lee93f824a2011-09-23 17:20:28 -07001277 // If there is already a loader task running, tell it to stop.
1278 // returns true if isLaunching() was true on the old task
1279 private boolean stopLoaderLocked() {
1280 boolean isLaunching = false;
1281 LoaderTask oldTask = mLoaderTask;
1282 if (oldTask != null) {
1283 if (oldTask.isLaunching()) {
1284 isLaunching = true;
1285 }
1286 oldTask.stopLocked();
1287 }
1288 return isLaunching;
1289 }
1290
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001291 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001292 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1293 }
1294
1295 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001296 synchronized (mLock) {
1297 if (DEBUG_LOADERS) {
1298 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1299 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301 // Clear any deferred bind-runnables from the synchronized load process
1302 // We must do this before any loading/binding is scheduled below.
1303 mDeferredBindRunnables.clear();
1304
Joe Onorato36115782010-06-17 13:28:48 -04001305 // Don't bother to start the thread if we know it's not going to do anything
1306 if (mCallbacks != null && mCallbacks.get() != null) {
1307 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001308 // also, don't downgrade isLaunching if we're already running
1309 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001310 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001311 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1312 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1314 } else {
1315 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1316 sWorker.post(mLoaderTask);
1317 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 }
1320 }
1321
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001322 void bindRemainingSynchronousPages() {
1323 // Post the remaining side pages to be loaded
1324 if (!mDeferredBindRunnables.isEmpty()) {
1325 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001326 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 }
1328 mDeferredBindRunnables.clear();
1329 }
1330 }
1331
Joe Onorato36115782010-06-17 13:28:48 -04001332 public void stopLoader() {
1333 synchronized (mLock) {
1334 if (mLoaderTask != null) {
1335 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001336 }
1337 }
Joe Onorato36115782010-06-17 13:28:48 -04001338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339
Winson Chung76828c82013-08-19 15:43:29 -07001340 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1341 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1342 final ContentResolver contentResolver = context.getContentResolver();
1343 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1344 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1345 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1346
1347 try {
1348 final int idIndex = sc.getColumnIndexOrThrow(
1349 LauncherSettings.WorkspaceScreens._ID);
1350 final int rankIndex = sc.getColumnIndexOrThrow(
1351 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1352 while (sc.moveToNext()) {
1353 try {
1354 long screenId = sc.getLong(idIndex);
1355 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001356 orderedScreens.put(rank, screenId);
1357 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001358 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001359 }
1360 }
1361 } finally {
1362 sc.close();
1363 }
Winson Chunga90303b2013-11-15 13:05:06 -08001364
1365 // Log to disk
1366 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1367 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1368 for (Integer i : orderedScreens.keySet()) {
1369 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1370 }
1371 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1372 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001373 return orderedScreens;
1374 }
1375
Michael Jurkac57b7a82011-08-09 22:02:20 -07001376 public boolean isAllAppsLoaded() {
1377 return mAllAppsLoaded;
1378 }
1379
Winson Chung36a62fe2012-05-06 18:04:42 -07001380 boolean isLoadingWorkspace() {
1381 synchronized (mLock) {
1382 if (mLoaderTask != null) {
1383 return mLoaderTask.isLoadingWorkspace();
1384 }
1385 }
1386 return false;
1387 }
1388
Joe Onorato36115782010-06-17 13:28:48 -04001389 /**
1390 * Runnable for the thread that loads the contents of the launcher:
1391 * - workspace icons
1392 * - widgets
1393 * - all apps icons
1394 */
1395 private class LoaderTask implements Runnable {
1396 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001397 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001398 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001399 private boolean mStopped;
1400 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001401 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001402
Winson Chungc3eecff2011-07-11 17:44:15 -07001403 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001404
Dan Sandlerd5024042014-01-09 15:01:33 -05001405 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001406 mContext = context;
1407 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001408 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001409 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 }
1411
Joe Onorato36115782010-06-17 13:28:48 -04001412 boolean isLaunching() {
1413 return mIsLaunching;
1414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415
Winson Chung36a62fe2012-05-06 18:04:42 -07001416 boolean isLoadingWorkspace() {
1417 return mIsLoadingAndBindingWorkspace;
1418 }
1419
Winson Chungc763c4e2013-07-19 13:49:06 -07001420 /** Returns whether this is an upgrade path */
1421 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001422 mIsLoadingAndBindingWorkspace = true;
1423
Joe Onorato36115782010-06-17 13:28:48 -04001424 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001425 if (DEBUG_LOADERS) {
1426 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001428
Winson Chungc763c4e2013-07-19 13:49:06 -07001429 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001430 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001431 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001432 synchronized (LoaderTask.this) {
1433 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001434 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001435 }
1436 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001437 }
1438 }
1439
Joe Onorato36115782010-06-17 13:28:48 -04001440 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001441 bindWorkspace(-1, isUpgradePath);
1442 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001443 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001444
Joe Onorato36115782010-06-17 13:28:48 -04001445 private void waitForIdle() {
1446 // Wait until the either we're stopped or the other threads are done.
1447 // This way we don't start loading all apps until the workspace has settled
1448 // down.
1449 synchronized (LoaderTask.this) {
1450 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001451
Joe Onorato36115782010-06-17 13:28:48 -04001452 mHandler.postIdle(new Runnable() {
1453 public void run() {
1454 synchronized (LoaderTask.this) {
1455 mLoadAndBindStepFinished = true;
1456 if (DEBUG_LOADERS) {
1457 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001458 }
Joe Onorato36115782010-06-17 13:28:48 -04001459 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001460 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462 });
1463
Michael Jurkac7700af2013-05-14 20:17:58 +02001464 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001465 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001466 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1467 // wait no longer than 1sec at a time
1468 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001469 } catch (InterruptedException ex) {
1470 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001471 }
1472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473 if (DEBUG_LOADERS) {
1474 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001475 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001476 + "ms for previous step to finish binding");
1477 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001478 }
Joe Onorato36115782010-06-17 13:28:48 -04001479 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001480
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001481 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001482 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483 // Ensure that we have a valid page index to load synchronously
1484 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1485 "valid page index");
1486 }
1487 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1488 // Ensure that we don't try and bind a specified page when the pages have not been
1489 // loaded already (we should load everything asynchronously in that case)
1490 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1491 }
1492 synchronized (mLock) {
1493 if (mIsLoaderTaskRunning) {
1494 // Ensure that we are never running the background loading at this point since
1495 // we also touch the background collections
1496 throw new RuntimeException("Error! Background loading is already running");
1497 }
1498 }
1499
1500 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1501 // data structures, we can't allow any other thread to touch that data, but because
1502 // this call is synchronous, we can get away with not locking).
1503
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001504 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001505 // operations from the previous activity. We need to ensure that all queued operations
1506 // are executed before any synchronous binding work is done.
1507 mHandler.flush();
1508
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001509 // Divide the set of loaded items into those that we are binding synchronously, and
1510 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001511 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001512 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1513 // arise from that.
1514 onlyBindAllApps();
1515 }
1516
Joe Onorato36115782010-06-17 13:28:48 -04001517 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001518 boolean isUpgrade = false;
1519
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001520 synchronized (mLock) {
1521 mIsLoaderTaskRunning = true;
1522 }
Joe Onorato36115782010-06-17 13:28:48 -04001523 // Optimize for end-user experience: if the Launcher is up and // running with the
1524 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1525 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001526 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001527 // Elevate priority when Home launches for the first time to avoid
1528 // starving at boot time. Staring at a blank home is not cool.
1529 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001530 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1531 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001532 android.os.Process.setThreadPriority(mIsLaunching
1533 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1534 }
Winson Chung64359a52013-07-08 17:17:08 -07001535 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001536 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001537
Joe Onorato36115782010-06-17 13:28:48 -04001538 if (mStopped) {
1539 break keep_running;
1540 }
1541
1542 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1543 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001544 synchronized (mLock) {
1545 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001546 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001547 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1548 }
1549 }
Joe Onorato36115782010-06-17 13:28:48 -04001550 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001551
1552 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001553 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1554 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001555
1556 // Restore the default thread priority after we are done loading items
1557 synchronized (mLock) {
1558 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1559 }
Joe Onorato36115782010-06-17 13:28:48 -04001560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561
Winson Chungaac01e12011-08-17 10:37:13 -07001562 // Update the saved icons if necessary
1563 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001564 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001565 for (Object key : sBgDbIconCache.keySet()) {
1566 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1567 }
1568 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001569 }
Winson Chungaac01e12011-08-17 10:37:13 -07001570
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001571 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001572 // Ensure that all the applications that are in the system are
1573 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001574 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001575 verifyApplications();
1576 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001577 }
1578
Joe Onorato36115782010-06-17 13:28:48 -04001579 // Clear out this reference, otherwise we end up holding it until all of the
1580 // callback runnables are done.
1581 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 synchronized (mLock) {
1584 // If we are still the last one to be scheduled, remove ourselves.
1585 if (mLoaderTask == this) {
1586 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001588 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001589 }
Joe Onorato36115782010-06-17 13:28:48 -04001590 }
1591
1592 public void stopLocked() {
1593 synchronized (LoaderTask.this) {
1594 mStopped = true;
1595 this.notify();
1596 }
1597 }
1598
1599 /**
1600 * Gets the callbacks object. If we've been stopped, or if the launcher object
1601 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1602 * object that was around when the deferred message was scheduled, and if there's
1603 * a new Callbacks object around then also return null. This will save us from
1604 * calling onto it with data that will be ignored.
1605 */
1606 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1607 synchronized (mLock) {
1608 if (mStopped) {
1609 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001610 }
Joe Onorato36115782010-06-17 13:28:48 -04001611
1612 if (mCallbacks == null) {
1613 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001614 }
Joe Onorato36115782010-06-17 13:28:48 -04001615
1616 final Callbacks callbacks = mCallbacks.get();
1617 if (callbacks != oldCallbacks) {
1618 return null;
1619 }
1620 if (callbacks == null) {
1621 Log.w(TAG, "no mCallbacks");
1622 return null;
1623 }
1624
1625 return callbacks;
1626 }
1627 }
1628
Winson Chungc763c4e2013-07-19 13:49:06 -07001629 private void verifyApplications() {
1630 final Context context = mApp.getContext();
1631
1632 // Cross reference all the applications in our apps list with items in the workspace
1633 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001634 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001635 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001636 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001637 tmpInfos = getItemInfoForComponentName(app.componentName);
1638 if (tmpInfos.isEmpty()) {
1639 // We are missing an application icon, so add this to the workspace
1640 added.add(app);
1641 // This is a rare event, so lets log it
1642 Log.e(TAG, "Missing Application on load: " + app);
1643 }
1644 }
1645 }
1646 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001647 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001648 }
1649 }
1650
Joe Onorato36115782010-06-17 13:28:48 -04001651 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001652 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001653 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001654 LauncherAppState app = LauncherAppState.getInstance();
1655 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001656 final int countX = (int) grid.numColumns;
1657 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001658
Adam Cohendcd297f2013-06-18 13:13:40 -07001659 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001660 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001661 // Return early if we detect that an item is under the hotseat button
1662 if (mCallbacks == null ||
1663 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001664 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001665 Log.e(TAG, "Error loading shortcut into hotseat " + item
1666 + " into position (" + item.screenId + ":" + item.cellX + ","
1667 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001668 return false;
1669 }
1670
Dan Sandler295ae182013-12-10 16:05:47 -05001671 final ItemInfo[][] hotseatItems =
1672 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1673
Adam Cohenae4409d2013-11-26 10:34:59 -08001674 if (item.screenId >= grid.numHotseatIcons) {
1675 Log.e(TAG, "Error loading shortcut " + item
1676 + " into hotseat position " + item.screenId
1677 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1678 + ")");
1679 return false;
1680 }
1681
Dan Sandler295ae182013-12-10 16:05:47 -05001682 if (hotseatItems != null) {
1683 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001684 Log.e(TAG, "Error loading shortcut into hotseat " + item
1685 + " into position (" + item.screenId + ":" + item.cellX + ","
1686 + item.cellY + ") occupied by "
1687 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1688 [(int) item.screenId][0]);
1689 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001690 } else {
1691 hotseatItems[(int) item.screenId][0] = item;
1692 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001694 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001695 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 items[(int) item.screenId][0] = item;
1697 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001698 return true;
1699 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001700 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1701 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001702 return true;
1703 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001704
Adam Cohendcd297f2013-06-18 13:13:40 -07001705 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001706 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001707 occupied.put(item.screenId, items);
1708 }
1709
Dan Sandler295ae182013-12-10 16:05:47 -05001710 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001711 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1712 item.cellX < 0 || item.cellY < 0 ||
1713 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1714 Log.e(TAG, "Error loading shortcut " + item
1715 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1716 + item.cellX + "," + item.cellY
1717 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1718 return false;
1719 }
1720
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001721 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001722 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1723 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001724 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001725 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001726 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001727 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001728 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001729 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001730 return false;
1731 }
1732 }
1733 }
1734 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1735 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001737 }
1738 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001739
Joe Onorato36115782010-06-17 13:28:48 -04001740 return true;
1741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742
Winson Chungba9c37f2013-08-30 14:11:37 -07001743 /** Clears all the sBg data structures */
1744 private void clearSBgDataStructures() {
1745 synchronized (sBgLock) {
1746 sBgWorkspaceItems.clear();
1747 sBgAppWidgets.clear();
1748 sBgFolders.clear();
1749 sBgItemsIdMap.clear();
1750 sBgDbIconCache.clear();
1751 sBgWorkspaceScreens.clear();
1752 }
1753 }
1754
Dan Sandlerd5024042014-01-09 15:01:33 -05001755 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001756 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001757 // Log to disk
1758 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1759
Joe Onorato36115782010-06-17 13:28:48 -04001760 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761
Joe Onorato36115782010-06-17 13:28:48 -04001762 final Context context = mContext;
1763 final ContentResolver contentResolver = context.getContentResolver();
1764 final PackageManager manager = context.getPackageManager();
1765 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1766 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001767
Winson Chung892c74d2013-08-22 16:15:50 -07001768 LauncherAppState app = LauncherAppState.getInstance();
1769 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1770 int countX = (int) grid.numColumns;
1771 int countY = (int) grid.numRows;
1772
Dan Sandlerd5024042014-01-09 15:01:33 -05001773 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1774 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1775 LauncherAppState.getLauncherProvider().deleteDatabase();
1776 }
1777
1778 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1779 // append the user's Launcher2 shortcuts
1780 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1781 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1782 } else {
1783 // Make sure the default workspace is loaded
1784 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1785 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1786 }
Adam Cohene25af792013-06-06 23:08:25 -07001787
Winson Chungc763c4e2013-07-19 13:49:06 -07001788 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001789 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001790 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001791
Winson Chung9f9f00b2013-11-15 13:27:00 -08001792 // Log to disk
1793 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001794
Winson Chung2abf94d2012-07-18 18:16:38 -07001795 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001796 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001797
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001799 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001800 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001801 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001802 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001803
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001804 // +1 for the hotseat (it can be larger than the workspace)
1805 // Load workspace in reverse order to ensure that latest items are loaded first (and
1806 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001807 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 try {
1810 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1811 final int intentIndex = c.getColumnIndexOrThrow
1812 (LauncherSettings.Favorites.INTENT);
1813 final int titleIndex = c.getColumnIndexOrThrow
1814 (LauncherSettings.Favorites.TITLE);
1815 final int iconTypeIndex = c.getColumnIndexOrThrow(
1816 LauncherSettings.Favorites.ICON_TYPE);
1817 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1818 final int iconPackageIndex = c.getColumnIndexOrThrow(
1819 LauncherSettings.Favorites.ICON_PACKAGE);
1820 final int iconResourceIndex = c.getColumnIndexOrThrow(
1821 LauncherSettings.Favorites.ICON_RESOURCE);
1822 final int containerIndex = c.getColumnIndexOrThrow(
1823 LauncherSettings.Favorites.CONTAINER);
1824 final int itemTypeIndex = c.getColumnIndexOrThrow(
1825 LauncherSettings.Favorites.ITEM_TYPE);
1826 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1827 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001828 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1829 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 final int screenIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.SCREEN);
1832 final int cellXIndex = c.getColumnIndexOrThrow
1833 (LauncherSettings.Favorites.CELLX);
1834 final int cellYIndex = c.getColumnIndexOrThrow
1835 (LauncherSettings.Favorites.CELLY);
1836 final int spanXIndex = c.getColumnIndexOrThrow
1837 (LauncherSettings.Favorites.SPANX);
1838 final int spanYIndex = c.getColumnIndexOrThrow(
1839 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001840 final int restoredIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1843 //final int displayModeIndex = c.getColumnIndexOrThrow(
1844 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 ShortcutInfo info;
1847 String intentDescription;
1848 LauncherAppWidgetInfo appWidgetInfo;
1849 int container;
1850 long id;
1851 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001852
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001854 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 try {
1856 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001857 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 switch (itemType) {
1860 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1861 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001862 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 intentDescription = c.getString(intentIndex);
1864 try {
1865 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001866 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001867 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001868 if (restored) {
1869 // might be installed later
1870 Launcher.addDumpLog(TAG,
1871 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001872 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001873 if (!mAppsCanBeOnRemoveableStorage) {
1874 // Log the invalid package, and remove it
1875 Launcher.addDumpLog(TAG,
1876 "Invalid package removed: " + cn, true);
1877 itemsToRemove.add(id);
1878 } else {
1879 // If apps can be on external storage, then we just
1880 // leave them for the user to remove (maybe add
1881 // visual treatment to it)
1882 Launcher.addDumpLog(TAG,
1883 "Invalid package found: " + cn, true);
1884 }
1885 continue;
Winson Chungee055712013-07-30 14:46:24 -07001886 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001887 } else if (restored) {
1888 // no special handling necessary for this restored item
1889 restoredRows.add(id);
1890 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001893 Launcher.addDumpLog(TAG,
1894 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 continue;
1896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897
Chris Wrenf4d08112014-01-16 18:13:56 -05001898 if (restored) {
1899 Launcher.addDumpLog(TAG,
1900 "constructing info for partially restored package",
1901 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001902 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001903 intent = getRestoredItemIntent(c, context, intent);
1904 } else if (itemType ==
1905 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1907 titleIndex, mLabelCache);
1908 } else {
1909 info = getShortcutInfo(c, context, iconTypeIndex,
1910 iconPackageIndex, iconResourceIndex, iconIndex,
1911 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001912
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 // App shortcuts that used to be automatically added to Launcher
1914 // didn't always have the correct intent flags set, so do that
1915 // here
1916 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001917 intent.getCategories() != null &&
1918 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001919 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 intent.addFlags(
1921 Intent.FLAG_ACTIVITY_NEW_TASK |
1922 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1923 }
Michael Jurka96879562012-03-22 05:54:33 -07001924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001927 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 container = c.getInt(containerIndex);
1930 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 info.cellX = c.getInt(cellXIndex);
1933 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001934 info.spanX = 1;
1935 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001938 deleteOnInvalidPlacement.set(false);
1939 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1940 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001941 itemsToRemove.add(id);
1942 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 break;
1944 }
1945
1946 switch (container) {
1947 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1948 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1949 sBgWorkspaceItems.add(info);
1950 break;
1951 default:
1952 // Item is in a user folder
1953 FolderInfo folderInfo =
1954 findOrMakeFolder(sBgFolders, container);
1955 folderInfo.add(info);
1956 break;
1957 }
1958 sBgItemsIdMap.put(info.id, info);
1959
1960 // now that we've loaded everthing re-save it with the
1961 // icon in case it disappears somehow.
1962 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001963 } else {
1964 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001965 }
1966 break;
1967
1968 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1969 id = c.getLong(idIndex);
1970 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1971
1972 folderInfo.title = c.getString(titleIndex);
1973 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 folderInfo.cellX = c.getInt(cellXIndex);
1978 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001979 folderInfo.spanX = 1;
1980 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981
Daniel Sandler8802e962010-05-26 16:28:16 -04001982 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001983 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001984 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001985 deleteOnInvalidPlacement)) {
1986 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001987 itemsToRemove.add(id);
1988 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001989 break;
1990 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001991
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1994 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1995 sBgWorkspaceItems.add(folderInfo);
1996 break;
Joe Onorato36115782010-06-17 13:28:48 -04001997 }
Joe Onorato17a89222011-02-08 17:26:11 -08001998
Chris Wrenf4d08112014-01-16 18:13:56 -05001999 if (restored) {
2000 // no special handling required for restored folders
2001 restoredRows.add(id);
2002 }
2003
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2005 sBgFolders.put(folderInfo.id, folderInfo);
2006 break;
2007
2008 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2009 // Read all Launcher-specific widget details
2010 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002011 String savedProvider = c.getString(appWidgetProviderIndex);
2012
Joe Onorato36115782010-06-17 13:28:48 -04002013 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 final AppWidgetProviderInfo provider =
2016 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 if (!isSafeMode && (provider == null || provider.provider == null ||
2019 provider.provider.getPackageName() == null)) {
2020 String log = "Deleting widget that isn't installed anymore: id="
2021 + id + " appWidgetId=" + appWidgetId;
2022 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002023 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 itemsToRemove.add(id);
2025 } else {
2026 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2027 provider.provider);
2028 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 appWidgetInfo.cellX = c.getInt(cellXIndex);
2031 appWidgetInfo.cellY = c.getInt(cellYIndex);
2032 appWidgetInfo.spanX = c.getInt(spanXIndex);
2033 appWidgetInfo.spanY = c.getInt(spanYIndex);
2034 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2035 appWidgetInfo.minSpanX = minSpan[0];
2036 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 container = c.getInt(containerIndex);
2039 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2040 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2041 Log.e(TAG, "Widget found where container != " +
2042 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2043 continue;
2044 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045
Adam Cohene25af792013-06-06 23:08:25 -07002046 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002048 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002049 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002050 deleteOnInvalidPlacement)) {
2051 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002052 itemsToRemove.add(id);
2053 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 break;
2055 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002056 String providerName = provider.provider.flattenToString();
2057 if (!providerName.equals(savedProvider)) {
2058 ContentValues values = new ContentValues();
2059 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2060 providerName);
2061 String where = BaseColumns._ID + "= ?";
2062 String[] args = {Integer.toString(c.getInt(idIndex))};
2063 contentResolver.update(contentUri, values, where, args);
2064 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2066 sBgAppWidgets.add(appWidgetInfo);
2067 }
Joe Onorato36115782010-06-17 13:28:48 -04002068 break;
2069 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002071 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002072 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 }
2074 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002075 if (c != null) {
2076 c.close();
2077 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 }
2079
Winson Chungba9c37f2013-08-30 14:11:37 -07002080 // Break early if we've stopped loading
2081 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002082 clearSBgDataStructures();
2083 return false;
2084 }
2085
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 if (itemsToRemove.size() > 0) {
2087 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002088 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 // Remove dead items
2090 for (long id : itemsToRemove) {
2091 if (DEBUG_LOADERS) {
2092 Log.d(TAG, "Removed id = " + id);
2093 }
2094 // Don't notify content observers
2095 try {
2096 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2097 null, null);
2098 } catch (RemoteException e) {
2099 Log.w(TAG, "Could not remove id = " + id);
2100 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002101 }
2102 }
2103
Chris Wrenf4d08112014-01-16 18:13:56 -05002104 if (restoredRows.size() > 0) {
2105 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2106 LauncherSettings.Favorites.CONTENT_URI);
2107 // Update restored items that no longer require special handling
2108 try {
2109 StringBuilder selectionBuilder = new StringBuilder();
2110 selectionBuilder.append(LauncherSettings.Favorites._ID);
2111 selectionBuilder.append(" IN (");
2112 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2113 selectionBuilder.append(")");
2114 ContentValues values = new ContentValues();
2115 values.put(LauncherSettings.Favorites.RESTORED, 0);
2116 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2117 values, selectionBuilder.toString(), null);
2118 } catch (RemoteException e) {
2119 Log.w(TAG, "Could not update restored rows");
2120 }
2121 }
2122
Winson Chungc763c4e2013-07-19 13:49:06 -07002123 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002124 long maxScreenId = 0;
2125 // If we're importing we use the old screen order.
2126 for (ItemInfo item: sBgItemsIdMap.values()) {
2127 long screenId = item.screenId;
2128 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2129 !sBgWorkspaceScreens.contains(screenId)) {
2130 sBgWorkspaceScreens.add(screenId);
2131 if (screenId > maxScreenId) {
2132 maxScreenId = screenId;
2133 }
2134 }
2135 }
2136 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002137 // Log to disk
2138 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2139 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2140 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002141
Michael Jurka414300a2013-08-27 15:42:35 +02002142 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002144
2145 // Update the max item id after we load an old db
2146 long maxItemId = 0;
2147 // If we're importing we use the old screen order.
2148 for (ItemInfo item: sBgItemsIdMap.values()) {
2149 maxItemId = Math.max(maxItemId, item.id);
2150 }
Michael Jurka414300a2013-08-27 15:42:35 +02002151 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002152 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002153 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2154 for (Integer i : orderedScreens.keySet()) {
2155 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002156 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002157 // Log to disk
2158 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2159 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002160
2161 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002162 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002163 for (ItemInfo item: sBgItemsIdMap.values()) {
2164 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2166 unusedScreens.contains(screenId)) {
2167 unusedScreens.remove(screenId);
2168 }
2169 }
2170
2171 // If there are any empty screens remove them, and update.
2172 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002173 // Log to disk
2174 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2175 TextUtils.join(", ", unusedScreens), true);
2176
Winson Chung933bae62013-08-29 11:42:30 -07002177 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002178 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2179 }
2180 }
2181
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 if (DEBUG_LOADERS) {
2183 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2184 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002185 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002186 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002188
Daniel Sandler566da102013-06-25 23:43:45 -04002189 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002190 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002192 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 line += " | ";
2194 }
Winson Chung892c74d2013-08-22 16:15:50 -07002195 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002196 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 }
Joe Onorato36115782010-06-17 13:28:48 -04002198 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 }
Joe Onorato36115782010-06-17 13:28:48 -04002202 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002203 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002204 }
2205
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 /** Filters the set of items who are directly or indirectly (via another container) on the
2207 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002208 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 ArrayList<ItemInfo> allWorkspaceItems,
2210 ArrayList<ItemInfo> currentScreenItems,
2211 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002212 // Purge any null ItemInfos
2213 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2214 while (iter.hasNext()) {
2215 ItemInfo i = iter.next();
2216 if (i == null) {
2217 iter.remove();
2218 }
2219 }
2220
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 // Order the set of items by their containers first, this allows use to walk through the
2222 // list sequentially, build up a list of containers that are in the specified screen,
2223 // as well as all items in those containers.
2224 Set<Long> itemsOnScreen = new HashSet<Long>();
2225 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2226 @Override
2227 public int compare(ItemInfo lhs, ItemInfo rhs) {
2228 return (int) (lhs.container - rhs.container);
2229 }
2230 });
2231 for (ItemInfo info : allWorkspaceItems) {
2232 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002233 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 currentScreenItems.add(info);
2235 itemsOnScreen.add(info.id);
2236 } else {
2237 otherScreenItems.add(info);
2238 }
2239 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2240 currentScreenItems.add(info);
2241 itemsOnScreen.add(info.id);
2242 } else {
2243 if (itemsOnScreen.contains(info.container)) {
2244 currentScreenItems.add(info);
2245 itemsOnScreen.add(info.id);
2246 } else {
2247 otherScreenItems.add(info);
2248 }
2249 }
2250 }
2251 }
2252
2253 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002254 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 ArrayList<LauncherAppWidgetInfo> appWidgets,
2256 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2257 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258
2259 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002260 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002262 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 currentScreenWidgets.add(widget);
2264 } else {
2265 otherScreenWidgets.add(widget);
2266 }
2267 }
2268 }
2269
2270 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002271 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 HashMap<Long, ItemInfo> itemsIdMap,
2273 HashMap<Long, FolderInfo> folders,
2274 HashMap<Long, FolderInfo> currentScreenFolders,
2275 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276
2277 for (long id : folders.keySet()) {
2278 ItemInfo info = itemsIdMap.get(id);
2279 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002280 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002282 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 currentScreenFolders.put(id, folder);
2284 } else {
2285 otherScreenFolders.put(id, folder);
2286 }
2287 }
2288 }
2289
2290 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2291 * right) */
2292 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002293 final LauncherAppState app = LauncherAppState.getInstance();
2294 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 // XXX: review this
2296 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002297 @Override
2298 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002299 int cellCountX = (int) grid.numColumns;
2300 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002301 int screenOffset = cellCountX * cellCountY;
2302 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002304 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002306 rhs.cellY * cellCountX + rhs.cellX);
2307 return (int) (lr - rr);
2308 }
2309 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002311
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2313 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 final Runnable r = new Runnable() {
2315 @Override
2316 public void run() {
2317 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2318 if (callbacks != null) {
2319 callbacks.bindScreens(orderedScreens);
2320 }
2321 }
2322 };
2323 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2324 }
2325
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2327 final ArrayList<ItemInfo> workspaceItems,
2328 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2329 final HashMap<Long, FolderInfo> folders,
2330 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 final boolean postOnMainThread = (deferredBindRunnables != null);
2333
2334 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002335 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002337 final int start = i;
2338 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 final Runnable r = new Runnable() {
2340 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002341 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002342 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002343 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002344 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2345 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 }
2347 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 };
2349 if (postOnMainThread) {
2350 deferredBindRunnables.add(r);
2351 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002352 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355
2356 // Bind the folders
2357 if (!folders.isEmpty()) {
2358 final Runnable r = new Runnable() {
2359 public void run() {
2360 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2361 if (callbacks != null) {
2362 callbacks.bindFolders(folders);
2363 }
2364 }
2365 };
2366 if (postOnMainThread) {
2367 deferredBindRunnables.add(r);
2368 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002369 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 }
2371 }
2372
2373 // Bind the widgets, one at a time
2374 N = appWidgets.size();
2375 for (int i = 0; i < N; i++) {
2376 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2377 final Runnable r = new Runnable() {
2378 public void run() {
2379 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2380 if (callbacks != null) {
2381 callbacks.bindAppWidget(widget);
2382 }
2383 }
2384 };
2385 if (postOnMainThread) {
2386 deferredBindRunnables.add(r);
2387 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002388 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002389 }
2390 }
2391 }
2392
2393 /**
2394 * Binds all loaded data to actual views on the main thread.
2395 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002396 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002397 final long t = SystemClock.uptimeMillis();
2398 Runnable r;
2399
2400 // Don't use these two variables in any of the callback runnables.
2401 // Otherwise we hold a reference to them.
2402 final Callbacks oldCallbacks = mCallbacks.get();
2403 if (oldCallbacks == null) {
2404 // This launcher has exited and nobody bothered to tell us. Just bail.
2405 Log.w(TAG, "LoaderTask running with no launcher");
2406 return;
2407 }
2408
Winson Chung9b9fb962013-11-15 15:39:34 -08002409 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002410 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2411 ArrayList<LauncherAppWidgetInfo> appWidgets =
2412 new ArrayList<LauncherAppWidgetInfo>();
2413 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2414 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002416 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 workspaceItems.addAll(sBgWorkspaceItems);
2418 appWidgets.addAll(sBgAppWidgets);
2419 folders.putAll(sBgFolders);
2420 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422 }
2423
Derek Prothro7aff3992013-12-10 14:00:37 -05002424 final boolean isLoadingSynchronously =
2425 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002426 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002427 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002428 if (currScreen >= orderedScreenIds.size()) {
2429 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002430 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002431 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002432 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002433 final long currentScreenId = currentScreen < 0
2434 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002435
2436 // Load all the items that are on the current page first (and in the process, unbind
2437 // all the existing workspace items before we call startBinding() below.
2438 unbindWorkspaceItemsOnMainThread();
2439
2440 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2442 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2443 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2444 new ArrayList<LauncherAppWidgetInfo>();
2445 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2446 new ArrayList<LauncherAppWidgetInfo>();
2447 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2448 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2449
Winson Chung9b9fb962013-11-15 15:39:34 -08002450 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002451 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002452 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002454 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 otherFolders);
2456 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2457 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2458
2459 // Tell the workspace that we're about to start binding items
2460 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002461 public void run() {
2462 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2463 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002465 }
2466 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 };
Winson Chung81b52252012-08-27 15:34:29 -07002468 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002469
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2471
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 // Load items on the current page
2473 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2474 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002475 if (isLoadingSynchronously) {
2476 r = new Runnable() {
2477 public void run() {
2478 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002479 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002480 callbacks.onPageBoundSynchronously(currentScreen);
2481 }
2482 }
2483 };
Winson Chung81b52252012-08-27 15:34:29 -07002484 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486
Winson Chung4a2afa32012-07-19 14:53:05 -07002487 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2488 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489 mDeferredBindRunnables.clear();
2490 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002491 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492
2493 // Tell the workspace that we're done binding items
2494 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002495 public void run() {
2496 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2497 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002498 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002499 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500
Winson Chung98e030b2012-05-07 16:01:11 -07002501 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002502 if (DEBUG_LOADERS) {
2503 Log.d(TAG, "bound workspace in "
2504 + (SystemClock.uptimeMillis()-t) + "ms");
2505 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002506
2507 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002508 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002510 if (isLoadingSynchronously) {
2511 mDeferredBindRunnables.add(r);
2512 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002513 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002514 }
Joe Onorato36115782010-06-17 13:28:48 -04002515 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002516
Joe Onorato36115782010-06-17 13:28:48 -04002517 private void loadAndBindAllApps() {
2518 if (DEBUG_LOADERS) {
2519 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2520 }
2521 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002522 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002523 synchronized (LoaderTask.this) {
2524 if (mStopped) {
2525 return;
2526 }
2527 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002528 }
Joe Onorato36115782010-06-17 13:28:48 -04002529 } else {
2530 onlyBindAllApps();
2531 }
2532 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002533
Joe Onorato36115782010-06-17 13:28:48 -04002534 private void onlyBindAllApps() {
2535 final Callbacks oldCallbacks = mCallbacks.get();
2536 if (oldCallbacks == null) {
2537 // This launcher has exited and nobody bothered to tell us. Just bail.
2538 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2539 return;
2540 }
2541
2542 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002543 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002544 final ArrayList<AppInfo> list
2545 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002546 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002547 public void run() {
2548 final long t = SystemClock.uptimeMillis();
2549 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2550 if (callbacks != null) {
2551 callbacks.bindAllApplications(list);
2552 }
2553 if (DEBUG_LOADERS) {
2554 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2555 + (SystemClock.uptimeMillis()-t) + "ms");
2556 }
2557 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002558 };
2559 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002560 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002561 r.run();
2562 } else {
2563 mHandler.post(r);
2564 }
Joe Onorato36115782010-06-17 13:28:48 -04002565 }
2566
Winson Chung64359a52013-07-08 17:17:08 -07002567 private void loadAllApps() {
2568 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002569
Joe Onorato36115782010-06-17 13:28:48 -04002570 final Callbacks oldCallbacks = mCallbacks.get();
2571 if (oldCallbacks == null) {
2572 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002573 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002574 return;
2575 }
2576
Winson Chung64359a52013-07-08 17:17:08 -07002577 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002578 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2579 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2580
Winson Chung64359a52013-07-08 17:17:08 -07002581 // Clear the list of apps
2582 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002583
Winson Chung64359a52013-07-08 17:17:08 -07002584 // Query for the set of apps
2585 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2586 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2587 if (DEBUG_LOADERS) {
2588 Log.d(TAG, "queryIntentActivities took "
2589 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2590 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2591 }
2592 // Fail if we don't have any apps
2593 if (apps == null || apps.isEmpty()) {
2594 return;
2595 }
2596 // Sort the applications by name
2597 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2598 Collections.sort(apps,
2599 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2600 if (DEBUG_LOADERS) {
2601 Log.d(TAG, "sort took "
2602 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002603 }
2604
Winson Chung64359a52013-07-08 17:17:08 -07002605 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002606 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002607 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002608 // This builds the icon bitmaps.
2609 mBgAllAppsList.add(new AppInfo(packageManager, app,
2610 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002611 }
2612
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002613 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002614 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2615 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002616
2617 // Post callback on main thread
2618 mHandler.post(new Runnable() {
2619 public void run() {
2620 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002621 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002622 if (callbacks != null) {
2623 callbacks.bindAllApplications(added);
2624 if (DEBUG_LOADERS) {
2625 Log.d(TAG, "bound " + added.size() + " apps in "
2626 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2627 }
2628 } else {
2629 Log.i(TAG, "not binding apps: no Launcher activity");
2630 }
2631 }
2632 });
2633
Joe Onorato36115782010-06-17 13:28:48 -04002634 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002635 Log.d(TAG, "Icons processed in "
2636 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002637 }
2638 }
2639
2640 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002641 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002642 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2643 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2644 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2645 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2646 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2647 }
Joe Onorato36115782010-06-17 13:28:48 -04002648 }
2649 }
2650
2651 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002652 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002653 }
2654
2655 private class PackageUpdatedTask implements Runnable {
2656 int mOp;
2657 String[] mPackages;
2658
2659 public static final int OP_NONE = 0;
2660 public static final int OP_ADD = 1;
2661 public static final int OP_UPDATE = 2;
2662 public static final int OP_REMOVE = 3; // uninstlled
2663 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2664
2665
2666 public PackageUpdatedTask(int op, String[] packages) {
2667 mOp = op;
2668 mPackages = packages;
2669 }
2670
2671 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002672 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002673
2674 final String[] packages = mPackages;
2675 final int N = packages.length;
2676 switch (mOp) {
2677 case OP_ADD:
2678 for (int i=0; i<N; i++) {
2679 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002680 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002681 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002682 }
2683 break;
2684 case OP_UPDATE:
2685 for (int i=0; i<N; i++) {
2686 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002687 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002688 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002689 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002690 }
2691 break;
2692 case OP_REMOVE:
2693 case OP_UNAVAILABLE:
2694 for (int i=0; i<N; i++) {
2695 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002696 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002697 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002698 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002699 }
2700 break;
2701 }
2702
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002703 ArrayList<AppInfo> added = null;
2704 ArrayList<AppInfo> modified = null;
2705 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002706
Adam Cohen487f7dd2012-06-28 18:12:10 -07002707 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002708 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002709 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002710 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002711 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002712 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002713 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002714 }
Winson Chung5d55f332012-07-16 20:45:03 -07002715 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002716 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002717 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002718 }
2719
Joe Onorato36115782010-06-17 13:28:48 -04002720 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2721 if (callbacks == null) {
2722 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2723 return;
2724 }
2725
2726 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002727 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002728 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002729 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002730 addAndBindAddedWorkspaceApps(context, addedInfos);
2731 } else {
2732 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002733 }
Joe Onorato36115782010-06-17 13:28:48 -04002734 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002735
Joe Onorato36115782010-06-17 13:28:48 -04002736 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002737 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002738
2739 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002740 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002741 ArrayList<ItemInfo> infos =
2742 getItemInfoForComponentName(a.componentName);
2743 for (ItemInfo i : infos) {
2744 if (isShortcutInfoUpdateable(i)) {
2745 ShortcutInfo info = (ShortcutInfo) i;
2746 info.title = a.title.toString();
2747 updateItemInDatabase(context, info);
2748 }
2749 }
2750 }
2751
Joe Onorato36115782010-06-17 13:28:48 -04002752 mHandler.post(new Runnable() {
2753 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002754 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2755 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002756 callbacks.bindAppsUpdated(modifiedFinal);
2757 }
2758 }
2759 });
2760 }
Winson Chung83892cc2013-05-01 16:53:33 -07002761
Winson Chungdf95eb12013-10-16 14:57:07 -07002762 final ArrayList<String> removedPackageNames =
2763 new ArrayList<String>();
2764 if (mOp == OP_REMOVE) {
2765 // Mark all packages in the broadcast to be removed
2766 removedPackageNames.addAll(Arrays.asList(packages));
2767 } else if (mOp == OP_UPDATE) {
2768 // Mark disabled packages in the broadcast to be removed
2769 final PackageManager pm = context.getPackageManager();
2770 for (int i=0; i<N; i++) {
2771 if (isPackageDisabled(pm, packages[i])) {
2772 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002773 }
2774 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002775 }
2776 // Remove all the components associated with this package
2777 for (String pn : removedPackageNames) {
2778 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2779 for (ItemInfo i : infos) {
2780 deleteItemFromDatabase(context, i);
2781 }
2782 }
2783 // Remove all the specific components
2784 for (AppInfo a : removedApps) {
2785 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2786 for (ItemInfo i : infos) {
2787 deleteItemFromDatabase(context, i);
2788 }
2789 }
2790 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2791 // Remove any queued items from the install queue
2792 String spKey = LauncherAppState.getSharedPreferencesKey();
2793 SharedPreferences sp =
2794 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2795 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2796 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002797 mHandler.post(new Runnable() {
2798 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002799 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2800 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002801 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002802 }
2803 }
2804 });
Joe Onoratobe386092009-11-17 17:32:16 -08002805 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002806
Michael Jurkac402cd92013-05-20 15:49:32 +02002807 final ArrayList<Object> widgetsAndShortcuts =
2808 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002809 mHandler.post(new Runnable() {
2810 @Override
2811 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002812 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2813 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002814 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002815 }
2816 }
2817 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002818
2819 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002820 mHandler.post(new Runnable() {
2821 public void run() {
2822 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2823 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002824 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002825 }
2826 }
2827 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 }
2829 }
2830
Michael Jurkac402cd92013-05-20 15:49:32 +02002831 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2832 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2833 PackageManager packageManager = context.getPackageManager();
2834 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2835 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2836 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2837 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2838 Collections.sort(widgetsAndShortcuts,
2839 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2840 return widgetsAndShortcuts;
2841 }
2842
Adam Cohen556f6132014-01-15 15:18:08 -08002843 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002844 try {
2845 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2846 return !pi.applicationInfo.enabled;
2847 } catch (NameNotFoundException e) {
2848 // Fall through
2849 }
2850 return false;
2851 }
Adam Cohen556f6132014-01-15 15:18:08 -08002852
2853 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002854 if (cn == null) {
2855 return false;
2856 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002857 if (isPackageDisabled(pm, cn.getPackageName())) {
2858 return false;
2859 }
Winson Chungee055712013-07-30 14:46:24 -07002860
2861 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002862 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002863 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002864 return (pm.getActivityInfo(cn, 0) != null);
2865 } catch (NameNotFoundException e) {
2866 return false;
2867 }
2868 }
2869
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002871 * Make an ShortcutInfo object for a restored application or shortcut item that points
2872 * to a package that is not yet installed on the system.
2873 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002874 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002875 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002876 if (cursor != null) {
2877 info.title = cursor.getString(titleIndex);
2878 } else {
2879 info.title = "";
2880 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002881 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002882 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002883 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002884 return info;
2885 }
2886
2887 /**
2888 * Make an Intent object for a restored application or shortcut item that points
2889 * to the market page for the item.
2890 */
2891 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002892 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002893 ComponentName componentName = intent.getComponent();
2894 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2895 Uri marketUri = new Uri.Builder()
2896 .scheme("market")
2897 .authority("details")
2898 .appendQueryParameter("id", componentName.getPackageName())
2899 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002900 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002901 marketIntent.setData(marketUri);
2902 return marketIntent;
2903 }
2904
2905 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002906 * This is called from the code that adds shortcuts from the intent receiver. This
2907 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 */
Joe Onorato56d82912010-03-07 14:32:10 -05002909 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002910 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912
Joe Onorato56d82912010-03-07 14:32:10 -05002913 /**
2914 * Make an ShortcutInfo object for a shortcut that is an application.
2915 *
2916 * If c is not null, then it will be used to fill in missing data like the title and icon.
2917 */
2918 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002919 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002920 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002921 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002922 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002923 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002924 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002925 } else {
2926 try {
2927 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2928 info.initFlagsAndFirstInstallTime(pi);
2929 } catch (NameNotFoundException e) {
2930 Log.d(TAG, "getPackInfo failed for package " +
2931 componentName.getPackageName());
2932 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002933 }
2934
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002935 // TODO: See if the PackageManager knows about this case. If it doesn't
2936 // then return null & delete this.
2937
Joe Onorato56d82912010-03-07 14:32:10 -05002938 // the resource -- This may implicitly give us back the fallback icon,
2939 // but don't worry about that. All we're doing with usingFallbackIcon is
2940 // to avoid saving lots of copies of that in the database, and most apps
2941 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002942
2943 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2944 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2945 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002946 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002947 ResolveInfo resolveInfo = null;
2948 ComponentName oldComponent = intent.getComponent();
2949 Intent newIntent = new Intent(intent.getAction(), null);
2950 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2951 newIntent.setPackage(oldComponent.getPackageName());
2952 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2953 for (ResolveInfo i : infos) {
2954 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2955 i.activityInfo.name);
2956 if (cn.equals(oldComponent)) {
2957 resolveInfo = i;
2958 }
2959 }
2960 if (resolveInfo == null) {
2961 resolveInfo = manager.resolveActivity(intent, 0);
2962 }
Joe Onorato56d82912010-03-07 14:32:10 -05002963 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002964 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002965 }
Joe Onorato56d82912010-03-07 14:32:10 -05002966 // the db
2967 if (icon == null) {
2968 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002969 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002970 }
2971 }
2972 // the fallback icon
2973 if (icon == null) {
2974 icon = getFallbackIcon();
2975 info.usingFallbackIcon = true;
2976 }
2977 info.setIcon(icon);
2978
2979 // from the resource
2980 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002981 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2982 if (labelCache != null && labelCache.containsKey(key)) {
2983 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002984 } else {
2985 info.title = resolveInfo.activityInfo.loadLabel(manager);
2986 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002987 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002988 }
2989 }
Joe Onorato56d82912010-03-07 14:32:10 -05002990 }
2991 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002993 if (c != null) {
2994 info.title = c.getString(titleIndex);
2995 }
2996 }
2997 // fall back to the class name of the activity
2998 if (info.title == null) {
2999 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3002 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003004
Winson Chung64359a52013-07-08 17:17:08 -07003005 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3006 ItemInfoFilter f) {
3007 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3008 for (ItemInfo i : infos) {
3009 if (i instanceof ShortcutInfo) {
3010 ShortcutInfo info = (ShortcutInfo) i;
3011 ComponentName cn = info.intent.getComponent();
3012 if (cn != null && f.filterItem(null, info, cn)) {
3013 filtered.add(info);
3014 }
3015 } else if (i instanceof FolderInfo) {
3016 FolderInfo info = (FolderInfo) i;
3017 for (ShortcutInfo s : info.contents) {
3018 ComponentName cn = s.intent.getComponent();
3019 if (cn != null && f.filterItem(info, s, cn)) {
3020 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003021 }
3022 }
Winson Chung64359a52013-07-08 17:17:08 -07003023 } else if (i instanceof LauncherAppWidgetInfo) {
3024 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3025 ComponentName cn = info.providerName;
3026 if (cn != null && f.filterItem(null, info, cn)) {
3027 filtered.add(info);
3028 }
Winson Chung8a435102012-08-30 17:16:53 -07003029 }
3030 }
Winson Chung64359a52013-07-08 17:17:08 -07003031 return new ArrayList<ItemInfo>(filtered);
3032 }
3033
3034 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
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.getPackageName().equals(pn);
3039 }
3040 };
3041 return filterItemInfos(sBgItemsIdMap.values(), filter);
3042 }
3043
3044 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003045 ItemInfoFilter filter = new ItemInfoFilter() {
3046 @Override
3047 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3048 return cn.equals(cname);
3049 }
3050 };
3051 return filterItemInfos(sBgItemsIdMap.values(), filter);
3052 }
3053
3054 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3055 if (i instanceof ShortcutInfo) {
3056 ShortcutInfo info = (ShortcutInfo) i;
3057 // We need to check for ACTION_MAIN otherwise getComponent() might
3058 // return null for some shortcuts (for instance, for shortcuts to
3059 // web pages.)
3060 Intent intent = info.intent;
3061 ComponentName name = intent.getComponent();
3062 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3063 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3064 return true;
3065 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003066 // placeholder shortcuts get special treatment, let them through too.
3067 if (info.getRestoredIntent() != null) {
3068 return true;
3069 }
Winson Chung64359a52013-07-08 17:17:08 -07003070 }
3071 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003072 }
3073
3074 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003075 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003077 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003078 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3079 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080
Joe Onorato56d82912010-03-07 14:32:10 -05003081 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003082 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003085 // TODO: If there's an explicit component and we can't install that, delete it.
3086
Joe Onorato56d82912010-03-07 14:32:10 -05003087 info.title = c.getString(titleIndex);
3088
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 int iconType = c.getInt(iconTypeIndex);
3090 switch (iconType) {
3091 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3092 String packageName = c.getString(iconPackageIndex);
3093 String resourceName = c.getString(iconResourceIndex);
3094 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003095 info.customIcon = false;
3096 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003099 if (resources != null) {
3100 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003101 icon = Utilities.createIconBitmap(
3102 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003103 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003105 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
Joe Onorato56d82912010-03-07 14:32:10 -05003107 // the db
3108 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003109 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003110 }
3111 // the fallback icon
3112 if (icon == null) {
3113 icon = getFallbackIcon();
3114 info.usingFallbackIcon = true;
3115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 break;
3117 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003118 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003119 if (icon == null) {
3120 icon = getFallbackIcon();
3121 info.customIcon = false;
3122 info.usingFallbackIcon = true;
3123 } else {
3124 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 break;
3127 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003128 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003129 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 info.customIcon = false;
3131 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003133 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 return info;
3135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136
Michael Jurka931dc972011-08-05 15:08:15 -07003137 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003138 @SuppressWarnings("all") // suppress dead code warning
3139 final boolean debug = false;
3140 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003141 Log.d(TAG, "getIconFromCursor app="
3142 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3143 }
3144 byte[] data = c.getBlob(iconIndex);
3145 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003146 return Utilities.createIconBitmap(
3147 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003148 } catch (Exception e) {
3149 return null;
3150 }
3151 }
3152
Winson Chung3d503fb2011-07-13 17:25:49 -07003153 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3154 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003155 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003156 if (info == null) {
3157 return null;
3158 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003160
3161 return info;
3162 }
3163
Winson Chunga9abd0e2010-10-27 17:18:37 -07003164 /**
Winson Chung55cef262010-10-28 14:14:18 -07003165 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3166 */
3167 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3168 ComponentName component) {
3169 List<AppWidgetProviderInfo> widgets =
3170 AppWidgetManager.getInstance(context).getInstalledProviders();
3171 for (AppWidgetProviderInfo info : widgets) {
3172 if (info.provider.equals(component)) {
3173 return info;
3174 }
3175 }
3176 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003177 }
3178
Winson Chung68846fd2010-10-29 11:00:27 -07003179 /**
3180 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3181 */
3182 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3183 final PackageManager packageManager = context.getPackageManager();
3184 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3185 new ArrayList<WidgetMimeTypeHandlerData>();
3186
3187 final Intent supportsIntent =
3188 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3189 supportsIntent.setType(mimeType);
3190
3191 // Create a set of widget configuration components that we can test against
3192 final List<AppWidgetProviderInfo> widgets =
3193 AppWidgetManager.getInstance(context).getInstalledProviders();
3194 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3195 new HashMap<ComponentName, AppWidgetProviderInfo>();
3196 for (AppWidgetProviderInfo info : widgets) {
3197 configurationComponentToWidget.put(info.configure, info);
3198 }
3199
3200 // Run through each of the intents that can handle this type of clip data, and cross
3201 // reference them with the components that are actual configuration components
3202 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3203 PackageManager.MATCH_DEFAULT_ONLY);
3204 for (ResolveInfo info : activities) {
3205 final ActivityInfo activityInfo = info.activityInfo;
3206 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3207 activityInfo.name);
3208 if (configurationComponentToWidget.containsKey(infoComponent)) {
3209 supportedConfigurationActivities.add(
3210 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3211 configurationComponentToWidget.get(infoComponent)));
3212 }
3213 }
3214 return supportedConfigurationActivities;
3215 }
3216
Winson Chunga9abd0e2010-10-27 17:18:37 -07003217 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003218 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3219 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3220 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3221
Adam Cohend9198822011-11-22 16:42:47 -08003222 if (intent == null) {
3223 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3224 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3225 return null;
3226 }
3227
Joe Onorato0589f0f2010-02-08 13:44:00 -08003228 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003229 boolean customIcon = false;
3230 ShortcutIconResource iconResource = null;
3231
3232 if (bitmap != null && bitmap instanceof Bitmap) {
3233 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003234 customIcon = true;
3235 } else {
3236 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3237 if (extra != null && extra instanceof ShortcutIconResource) {
3238 try {
3239 iconResource = (ShortcutIconResource) extra;
3240 final PackageManager packageManager = context.getPackageManager();
3241 Resources resources = packageManager.getResourcesForApplication(
3242 iconResource.packageName);
3243 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003244 icon = Utilities.createIconBitmap(
3245 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003246 } catch (Exception e) {
3247 Log.w(TAG, "Could not load shortcut icon: " + extra);
3248 }
3249 }
3250 }
3251
Michael Jurkac9d95c52011-08-29 14:03:34 -07003252 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003253
3254 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003255 if (fallbackIcon != null) {
3256 icon = fallbackIcon;
3257 } else {
3258 icon = getFallbackIcon();
3259 info.usingFallbackIcon = true;
3260 }
Joe Onorato56d82912010-03-07 14:32:10 -05003261 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003262 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003263
Joe Onorato0589f0f2010-02-08 13:44:00 -08003264 info.title = name;
3265 info.intent = intent;
3266 info.customIcon = customIcon;
3267 info.iconResource = iconResource;
3268
3269 return info;
3270 }
3271
Winson Chungaac01e12011-08-17 10:37:13 -07003272 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3273 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003274 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003275 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003276 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003277 }
Joe Onorato56d82912010-03-07 14:32:10 -05003278 // If this icon doesn't have a custom icon, check to see
3279 // what's stored in the DB, and if it doesn't match what
3280 // we're going to show, store what we are going to show back
3281 // into the DB. We do this so when we're loading, if the
3282 // package manager can't find an icon (for example because
3283 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003284 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003285 cache.put(info, c.getBlob(iconIndex));
3286 return true;
3287 }
3288 return false;
3289 }
3290 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3291 boolean needSave = false;
3292 try {
3293 if (data != null) {
3294 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3295 Bitmap loaded = info.getIcon(mIconCache);
3296 needSave = !saved.sameAs(loaded);
3297 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003298 needSave = true;
3299 }
Winson Chungaac01e12011-08-17 10:37:13 -07003300 } catch (Exception e) {
3301 needSave = true;
3302 }
3303 if (needSave) {
3304 Log.d(TAG, "going to save icon bitmap for info=" + info);
3305 // This is slower than is ideal, but this only happens once
3306 // or when the app is updated with a new icon.
3307 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003308 }
3309 }
3310
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003312 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 * or make a new one.
3314 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003315 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 // See if a placeholder was created for us already
3317 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003318 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003320 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321 folders.put(id, folderInfo);
3322 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003323 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 }
3325
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003326 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003327 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003328 return new Comparator<AppInfo>() {
3329 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003330 int result = collator.compare(a.title.toString().trim(),
3331 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003332 if (result == 0) {
3333 result = a.componentName.compareTo(b.componentName);
3334 }
3335 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003336 }
Winson Chung11904872012-09-17 16:58:46 -07003337 };
3338 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003339 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3340 = new Comparator<AppInfo>() {
3341 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003342 if (a.firstInstallTime < b.firstInstallTime) return 1;
3343 if (a.firstInstallTime > b.firstInstallTime) return -1;
3344 return 0;
3345 }
3346 };
Winson Chung11904872012-09-17 16:58:46 -07003347 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3348 final Collator collator = Collator.getInstance();
3349 return new Comparator<AppWidgetProviderInfo>() {
3350 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003351 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003352 }
3353 };
3354 }
Winson Chung5308f242011-08-18 12:12:41 -07003355 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3356 if (info.activityInfo != null) {
3357 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3358 } else {
3359 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3360 }
3361 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003362 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003363 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003364 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003365 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003366 ShortcutNameComparator(PackageManager pm) {
3367 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003368 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003369 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003370 }
3371 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3372 mPackageManager = pm;
3373 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003374 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003375 }
3376 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003377 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003378 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3379 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3380 if (mLabelCache.containsKey(keyA)) {
3381 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003382 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003383 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003384
Winson Chung5308f242011-08-18 12:12:41 -07003385 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003386 }
Winson Chung5308f242011-08-18 12:12:41 -07003387 if (mLabelCache.containsKey(keyB)) {
3388 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003389 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003390 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003391
Winson Chung5308f242011-08-18 12:12:41 -07003392 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003393 }
Winson Chung11904872012-09-17 16:58:46 -07003394 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003395 }
3396 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003397 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003398 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003399 private PackageManager mPackageManager;
3400 private HashMap<Object, String> mLabelCache;
3401 WidgetAndShortcutNameComparator(PackageManager pm) {
3402 mPackageManager = pm;
3403 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003404 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003405 }
3406 public final int compare(Object a, Object b) {
3407 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003408 if (mLabelCache.containsKey(a)) {
3409 labelA = mLabelCache.get(a);
3410 } else {
3411 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003412 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003413 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003414 mLabelCache.put(a, labelA);
3415 }
3416 if (mLabelCache.containsKey(b)) {
3417 labelB = mLabelCache.get(b);
3418 } else {
3419 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003420 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003421 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003422 mLabelCache.put(b, labelB);
3423 }
Winson Chung11904872012-09-17 16:58:46 -07003424 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003425 }
3426 };
Joe Onoratobe386092009-11-17 17:32:16 -08003427
3428 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003429 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003430 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3431 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3432 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3433 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003434 if (mLoaderTask != null) {
3435 mLoaderTask.dumpState();
3436 } else {
3437 Log.d(TAG, "mLoaderTask=null");
3438 }
Joe Onoratobe386092009-11-17 17:32:16 -08003439 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003440}