blob: e5eca625b8e58e27c6d455ded40ef577be5b67b5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Dan Sandlerd5024042014-01-09 15:01:33 -050078 public static final int LOADER_FLAG_NONE = 0;
79 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
80 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
81
Joe Onorato36115782010-06-17 13:28:48 -040082 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050083 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080084
Winson Chungee055712013-07-30 14:46:24 -070085 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080086 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040087
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private final Object mLock = new Object();
90 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040091 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020093 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Winson Chung81b52252012-08-27 15:34:29 -070095 // Specific runnable types that are run on the main thread deferred handler, this allows us to
96 // clear all queued binding runnables when the Launcher activity is destroyed.
97 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
98 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
99
100
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700101 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
102 static {
103 sWorkerThread.start();
104 }
105 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
106
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 // We start off with everything not loaded. After that, we assume that
108 // our monitoring of the package manager provides all updates and we never
109 // need to do a requery. These are only ever touched from the loader thread.
110 private boolean mWorkspaceLoaded;
111 private boolean mAllAppsLoaded;
112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // When we are loading pages synchronously, we can't just post the binding of items on the side
114 // pages as this delays the rotation process. Instead, we wait for a callback from the first
115 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
116 // a normal load, we also clear this set of Runnables.
117 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700122 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // The lock that must be acquired before referencing any static bg data structures. Unlike
125 // other locks, this one can generally be held long-term because we never expect any of these
126 // static data structures to be referenced outside of the worker thread except on the first
127 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700128 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
135 // created by LauncherModel that are directly on the home screen (however, no widgets or
136 // shortcuts within folders).
137 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
140 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 new ArrayList<LauncherAppWidgetInfo>();
142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
144 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
147 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700148
149 // sBgWorkspaceScreens is the ordered set of workspace screens.
150 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
151
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // </ only access in worker thread >
153
154 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Reena Lee99a73f32011-10-24 17:27:37 -0700157 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700160 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public int getCurrentWorkspaceScreen();
162 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700163 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
164 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700166 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500167 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700168 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700171 public void bindAppsAdded(ArrayList<Long> newScreens,
172 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700173 ArrayList<ItemInfo> addAnimated,
174 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200175 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500176 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700177 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700178 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200179 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100180 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700181 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700182 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700183 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Winson Chung64359a52013-07-08 17:17:08 -0700186 public interface ItemInfoFilter {
187 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
188 }
189
Bjorn Bringert1307f632013-10-03 22:31:03 +0100190 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800191 Context context = app.getContext();
192 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400193
Winson Chungee055712013-07-30 14:46:24 -0700194 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chungeb23cb22014-03-06 10:39:43 -0800195 ContentProviderClient client = contentResolver.acquireContentProviderClient(
196 LauncherSettings.Favorites.OLD_CONTENT_URI);
197 mOldContentProviderExists = (client != null);
198 if (client != null) {
199 client.release();
200 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400201 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100202 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 mIconCache = iconCache;
204
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400205 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700206 Configuration config = res.getConfiguration();
207 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 }
209
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700210 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
211 * posted on the main thread handler. */
212 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700213 runOnMainThread(r, 0);
214 }
215 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700216 if (sWorkerThread.getThreadId() == Process.myTid()) {
217 // If we are on the worker thread, post onto the main handler
218 mHandler.post(r);
219 } else {
220 r.run();
221 }
222 }
223
224 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
225 * posted on the worker thread handler. */
226 private static void runOnWorkerThread(Runnable r) {
227 if (sWorkerThread.getThreadId() == Process.myTid()) {
228 r.run();
229 } else {
230 // If we are not on the worker thread, then post to the worker handler
231 sWorker.post(r);
232 }
233 }
234
Winson Chunge43a1e72014-01-15 10:33:02 -0800235 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
236 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800237 }
238
Winson Chungc9168342013-06-26 14:54:55 -0700239 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
240 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700241 LauncherAppState app = LauncherAppState.getInstance();
242 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
243 final int xCount = (int) grid.numColumns;
244 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700245 boolean[][] occupied = new boolean[xCount][yCount];
246
247 int cellX, cellY, spanX, spanY;
248 for (int i = 0; i < items.size(); ++i) {
249 final ItemInfo item = items.get(i);
250 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
251 if (item.screenId == screen) {
252 cellX = item.cellX;
253 cellY = item.cellY;
254 spanX = item.spanX;
255 spanY = item.spanY;
256 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
257 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
258 occupied[x][y] = true;
259 }
260 }
261 }
262 }
263 }
264
265 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
266 }
267 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700269 int firstScreenIndex,
270 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 // Lock on the app so that we don't try and get the items while apps are being added
272 LauncherAppState app = LauncherAppState.getInstance();
273 LauncherModel model = app.getModel();
274 boolean found = false;
275 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700276 if (sWorkerThread.getThreadId() != Process.myTid()) {
277 // Flush the LauncherModel worker thread, so that if we just did another
278 // processInstallShortcut, we give it time for its shortcut to get added to the
279 // database (getItemsInLocalCoordinates reads the database)
280 model.flushWorkerThread();
281 }
Winson Chungc9168342013-06-26 14:54:55 -0700282 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700283
284 // Try adding to the workspace screens incrementally, starting at the default or center
285 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700286 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
287 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700288 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700289 int[] tmpCoordinates = new int[2];
290 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700291 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700292 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700293 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700294 }
295 }
296 }
Winson Chungc9168342013-06-26 14:54:55 -0700297 return null;
298 }
299
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500300 public void setPackageState(final String pkgName, final int state) {
301 // Process the updated package state
302 Runnable r = new Runnable() {
303 public void run() {
304 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
305 if (callbacks != null) {
306 callbacks.updatePackageState(pkgName, state);
307 }
308 }
309 };
310 mHandler.post(r);
311 }
312
Adam Cohen76a47a12014-02-05 11:47:43 -0800313 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
314 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
315
316 if (allAppsApps == null) {
317 throw new RuntimeException("allAppsApps must not be null");
318 }
319 if (allAppsApps.isEmpty()) {
320 return;
321 }
322
Chris Wrenb6d4c282014-01-27 14:17:08 -0500323 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
324 Iterator<AppInfo> iter = allAppsApps.iterator();
325 while (iter.hasNext()) {
326 ItemInfo a = iter.next();
327 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
328 restoredAppsFinal.add((AppInfo) a);
329 }
330 }
331
Adam Cohen76a47a12014-02-05 11:47:43 -0800332 // Process the newly added applications and add them to the database first
333 Runnable r = new Runnable() {
334 public void run() {
335 runOnMainThread(new Runnable() {
336 public void run() {
337 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
338 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500339 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500340 for (AppInfo info : restoredAppsFinal) {
341 final Intent intent = info.getIntent();
342 if (intent != null) {
343 mIconCache.deletePreloadedIcon(intent.getComponent());
344 }
345 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500346 callbacks.bindAppsUpdated(restoredAppsFinal);
347 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500348 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800349 }
350 }
351 });
352 }
353 };
354 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700355 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800356
357 public void addAndBindAddedWorkspaceApps(final Context context,
358 final ArrayList<ItemInfo> workspaceApps) {
359 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
360
361 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800362 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
363 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700365 return;
Winson Chung997a9232013-07-24 15:33:46 -0700366 }
Winson Chung64359a52013-07-08 17:17:08 -0700367 // Process the newly added applications and add them to the database first
368 Runnable r = new Runnable() {
369 public void run() {
370 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
371 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500372 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700373
Winson Chung76828c82013-08-19 15:43:29 -0700374 // Get the list of workspace screens. We need to append to this list and
375 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
376 // called.
377 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
378 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
379 for (Integer i : orderedScreens.keySet()) {
380 long screenId = orderedScreens.get(i);
381 workspaceScreens.add(screenId);
382 }
383
Winson Chung64359a52013-07-08 17:17:08 -0700384 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700385 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700386 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700387 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700388 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700389 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700390
391 // Short-circuit this logic if the icon exists somewhere on the workspace
392 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500393 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
394 if (a instanceof AppInfo &&
395 LauncherModel.appWasRestored(context, launchIntent)) {
396 restoredAppsFinal.add((AppInfo) a);
397 }
Winson Chung64359a52013-07-08 17:17:08 -0700398 continue;
399 }
400
Winson Chung87412982013-10-03 18:34:14 -0700401 // Add this icon to the db, creating a new page if necessary. If there
402 // is only the empty page then we just add items to the first page.
403 // Otherwise, we add them to the next pages.
404 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700405 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700406 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700407 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200408 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700409
410 // If we can't find a valid position, then just add a new screen.
411 // This takes time so we need to re-queue the add until the new
412 // page is added. Create as many screens as necessary to satisfy
413 // the startSearchPageIndex.
414 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700415 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700416 while (numPagesToAdd > 0) {
417 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700418 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700419 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700420 addedWorkspaceScreensFinal.add(screenId);
421 numPagesToAdd--;
422 }
Winson Chung76828c82013-08-19 15:43:29 -0700423
Winson Chung64359a52013-07-08 17:17:08 -0700424 // Find the coordinate again
425 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700426 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700427 }
428 if (coords == null) {
429 throw new RuntimeException("Coordinates should not be null");
430 }
431
Winson Chung997a9232013-07-24 15:33:46 -0700432 ShortcutInfo shortcutInfo;
433 if (a instanceof ShortcutInfo) {
434 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200435 } else if (a instanceof AppInfo) {
436 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700437 } else {
438 throw new RuntimeException("Unexpected info type");
439 }
Winson Chung94d67682013-09-25 16:29:40 -0700440
Winson Chung64359a52013-07-08 17:17:08 -0700441 // Add the shortcut to the db
442 addItemToDatabase(context, shortcutInfo,
443 LauncherSettings.Favorites.CONTAINER_DESKTOP,
444 coords.first, coords.second[0], coords.second[1], false);
445 // Save the ShortcutInfo for binding in the workspace
446 addedShortcutsFinal.add(shortcutInfo);
447 }
448 }
449
Winson Chung76828c82013-08-19 15:43:29 -0700450 // Update the workspace screens
451 updateWorkspaceScreenOrder(context, workspaceScreens);
452
Adam Cohen76a47a12014-02-05 11:47:43 -0800453 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700454 runOnMainThread(new Runnable() {
455 public void run() {
456 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
457 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700458 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
459 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700460 if (!addedShortcutsFinal.isEmpty()) {
461 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
462 long lastScreenId = info.screenId;
463 for (ItemInfo i : addedShortcutsFinal) {
464 if (i.screenId == lastScreenId) {
465 addAnimated.add(i);
466 } else {
467 addNotAnimated.add(i);
468 }
Winson Chung997a9232013-07-24 15:33:46 -0700469 }
470 }
Winson Chungd64d1762013-08-20 14:37:16 -0700471 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800472 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500473 if (!restoredAppsFinal.isEmpty()) {
474 callbacks.bindAppsUpdated(restoredAppsFinal);
475 }
Winson Chung997a9232013-07-24 15:33:46 -0700476 }
Winson Chung64359a52013-07-08 17:17:08 -0700477 }
Winson Chung997a9232013-07-24 15:33:46 -0700478 });
479 }
Winson Chung64359a52013-07-08 17:17:08 -0700480 }
481 };
482 runOnWorkerThread(r);
483 }
484
Joe Onorato56d82912010-03-07 14:32:10 -0500485 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700486 if (mDefaultIcon == null) {
487 final Context context = LauncherAppState.getInstance().getContext();
488 mDefaultIcon = Utilities.createIconBitmap(
489 mIconCache.getFullResDefaultActivityIcon(), context);
490 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800491 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Winson Chung81b52252012-08-27 15:34:29 -0700494 public void unbindItemInfosAndClearQueuedBindRunnables() {
495 if (sWorkerThread.getThreadId() == Process.myTid()) {
496 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
497 "main thread");
498 }
499
500 // Clear any deferred bind runnables
501 mDeferredBindRunnables.clear();
502 // Remove any queued bind runnables
503 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
504 // Unbind all the workspace items
505 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700506 }
507
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700508 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700509 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700510 // Ensure that we don't use the same workspace items data structure on the main thread
511 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700512 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
513 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700514 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700515 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
516 tmpAppWidgets.addAll(sBgAppWidgets);
517 }
518 Runnable r = new Runnable() {
519 @Override
520 public void run() {
521 for (ItemInfo item : tmpWorkspaceItems) {
522 item.unbind();
523 }
524 for (ItemInfo item : tmpAppWidgets) {
525 item.unbind();
526 }
527 }
528 };
529 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700530 }
531
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 /**
533 * Adds an item to the DB if it was not created previously, or move it to a new
534 * <container, screen, cellX, cellY>
535 */
536 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700537 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 if (item.container == ItemInfo.NO_ID) {
539 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700540 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 } else {
542 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700543 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700547 static void checkItemInfoLocked(
548 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
549 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
550 if (modelItem != null && item != modelItem) {
551 // check all the data is consistent
552 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
553 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
554 ShortcutInfo shortcut = (ShortcutInfo) item;
555 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
556 modelShortcut.intent.filterEquals(shortcut.intent) &&
557 modelShortcut.id == shortcut.id &&
558 modelShortcut.itemType == shortcut.itemType &&
559 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700560 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700561 modelShortcut.cellX == shortcut.cellX &&
562 modelShortcut.cellY == shortcut.cellY &&
563 modelShortcut.spanX == shortcut.spanX &&
564 modelShortcut.spanY == shortcut.spanY &&
565 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
566 (modelShortcut.dropPos != null &&
567 shortcut.dropPos != null &&
568 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
569 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
570 // For all intents and purposes, this is the same object
571 return;
572 }
573 }
574
575 // the modelItem needs to match up perfectly with item if our model is
576 // to be consistent with the database-- for now, just require
577 // modelItem == item or the equality check above
578 String msg = "item: " + ((item != null) ? item.toString() : "null") +
579 "modelItem: " +
580 ((modelItem != null) ? modelItem.toString() : "null") +
581 "Error: ItemInfo passed to checkItemInfo doesn't match original";
582 RuntimeException e = new RuntimeException(msg);
583 if (stackTrace != null) {
584 e.setStackTrace(stackTrace);
585 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800586 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700587 }
588 }
589
Michael Jurka816474f2012-06-25 14:49:02 -0700590 static void checkItemInfo(final ItemInfo item) {
591 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
592 final long itemId = item.id;
593 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 public void run() {
595 synchronized (sBgLock) {
596 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700597 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 }
599 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700601 }
602
Michael Jurkac9d95c52011-08-29 14:03:34 -0700603 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
604 final ItemInfo item, final String callingFunction) {
605 final long itemId = item.id;
606 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
607 final ContentResolver cr = context.getContentResolver();
608
Adam Cohen487f7dd2012-06-28 18:12:10 -0700609 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700610 Runnable r = new Runnable() {
611 public void run() {
612 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700613 updateItemArrays(item, itemId, stackTrace);
614 }
615 };
616 runOnWorkerThread(r);
617 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700618
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700619 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
620 final ArrayList<ItemInfo> items, final String callingFunction) {
621 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700622
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700623 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
624 Runnable r = new Runnable() {
625 public void run() {
626 ArrayList<ContentProviderOperation> ops =
627 new ArrayList<ContentProviderOperation>();
628 int count = items.size();
629 for (int i = 0; i < count; i++) {
630 ItemInfo item = items.get(i);
631 final long itemId = item.id;
632 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
633 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700634
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700635 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
636 updateItemArrays(item, itemId, stackTrace);
637
638 }
639 try {
640 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
641 } catch (Exception e) {
642 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643 }
644 }
645 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700646 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700647 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700648
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
650 // Lock on mBgLock *after* the db operation
651 synchronized (sBgLock) {
652 checkItemInfoLocked(itemId, item, stackTrace);
653
654 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
655 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
656 // Item is in a folder, make sure this folder exists
657 if (!sBgFolders.containsKey(item.container)) {
658 // An items container is being set to a that of an item which is not in
659 // the list of Folders.
660 String msg = "item: " + item + " container being set to: " +
661 item.container + ", not in the list of folders";
662 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 }
664 }
665
666 // Items are added/removed from the corresponding FolderInfo elsewhere, such
667 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
668 // that are on the desktop, as appropriate
669 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800670 if (modelItem != null &&
671 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
672 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 switch (modelItem.itemType) {
674 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
675 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
676 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
677 if (!sBgWorkspaceItems.contains(modelItem)) {
678 sBgWorkspaceItems.add(modelItem);
679 }
680 break;
681 default:
682 break;
683 }
684 } else {
685 sBgWorkspaceItems.remove(modelItem);
686 }
687 }
688 }
689
Michael Jurkac7700af2013-05-14 20:17:58 +0200690 public void flushWorkerThread() {
691 mFlushingWorkerThread = true;
692 Runnable waiter = new Runnable() {
693 public void run() {
694 synchronized (this) {
695 notifyAll();
696 mFlushingWorkerThread = false;
697 }
698 }
699 };
700
701 synchronized(waiter) {
702 runOnWorkerThread(waiter);
703 if (mLoaderTask != null) {
704 synchronized(mLoaderTask) {
705 mLoaderTask.notify();
706 }
707 }
708 boolean success = false;
709 while (!success) {
710 try {
711 waiter.wait();
712 success = true;
713 } catch (InterruptedException e) {
714 }
715 }
716 }
717 }
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700721 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700722 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 item.cellX = cellX;
726 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 // We store hotseat items in canonical form which is this orientation invariant position
729 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
737 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
740 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700744 }
745
746 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
748 * cellX, cellY have already been updated on the ItemInfos.
749 */
750 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
751 final long container, final int screen) {
752
753 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
754 int count = items.size();
755
756 for (int i = 0; i < count; i++) {
757 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 item.container = container;
759
760 // We store hotseat items in canonical form which is this orientation invariant position
761 // in the hotseat
762 if (context instanceof Launcher && screen < 0 &&
763 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 item.cellY);
766 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 }
769
770 final ContentValues values = new ContentValues();
771 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
772 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
773 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775
776 contentValues.add(values);
777 }
778 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
779 }
780
781 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800783 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700786 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800787 item.cellX = cellX;
788 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 item.spanX = spanX;
790 item.spanY = spanY;
791
792 // We store hotseat items in canonical form which is this orientation invariant position
793 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700795 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 }
Adam Cohend4844c32011-02-18 19:25:06 -0800800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800802 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
804 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
805 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800808
Michael Jurka816474f2012-06-25 14:49:02 -0700809 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811
812 /**
813 * Update an item to the database in a specified container.
814 */
815 static void updateItemInDatabase(Context context, final ItemInfo item) {
816 final ContentValues values = new ContentValues();
817 item.onAddToDatabase(values);
818 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
819 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800820 }
821
822 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 * Returns true if the shortcuts already exists in the database.
824 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 static boolean shortcutExists(Context context, String title, Intent intent) {
827 final ContentResolver cr = context.getContentResolver();
828 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
829 new String[] { "title", "intent" }, "title=? and intent=?",
830 new String[] { title, intent.toUri(0) }, null);
831 boolean result = false;
832 try {
833 result = c.moveToFirst();
834 } finally {
835 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700838 }
839
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500841 * Returns true if the shortcuts already exists in the database.
842 * we identify a shortcut by the component name of the intent.
843 */
844 static boolean appWasRestored(Context context, Intent intent) {
845 final ContentResolver cr = context.getContentResolver();
846 final ComponentName component = intent.getComponent();
847 if (component == null) {
848 return false;
849 }
850 String componentName = component.flattenToString();
851 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
852 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
853 new String[]{"intent", "restored"}, where, null, null);
854 boolean result = false;
855 try {
856 result = c.moveToFirst();
857 } finally {
858 c.close();
859 }
860 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
861 return result;
862 }
863
864 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 * Returns an ItemInfo array containing all the items in the LauncherModel.
866 * The ItemInfo.id is not set through this function.
867 */
868 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
869 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
870 final ContentResolver cr = context.getContentResolver();
871 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
872 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
873 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
874 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
875
876 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
877 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
878 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
879 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
880 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
881 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
882 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
883
884 try {
885 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700887 item.cellX = c.getInt(cellXIndex);
888 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700889 item.spanX = Math.max(1, c.getInt(spanXIndex));
890 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700891 item.container = c.getInt(containerIndex);
892 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700894
895 items.add(item);
896 }
897 } catch (Exception e) {
898 items.clear();
899 } finally {
900 c.close();
901 }
902
903 return items;
904 }
905
906 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
908 */
909 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
910 final ContentResolver cr = context.getContentResolver();
911 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
912 "_id=? and (itemType=? or itemType=?)",
913 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700914 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700915
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 try {
917 if (c.moveToFirst()) {
918 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
919 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
920 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
921 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
922 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
923 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 FolderInfo folderInfo = null;
926 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700927 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
928 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700930 }
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 folderInfo.title = c.getString(titleIndex);
933 folderInfo.id = id;
934 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700936 folderInfo.cellX = c.getInt(cellXIndex);
937 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938
939 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 } finally {
942 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
944
945 return null;
946 }
947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 /**
949 * Add an item to the database in a specified container. Sets the container, screen, cellX and
950 * cellY fields of the item. Also assigns an ID to the item.
951 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700952 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 item.cellX = cellX;
956 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 // We store hotseat items in canonical form which is this orientation invariant position
958 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
966 final ContentValues values = new ContentValues();
967 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 item.onAddToDatabase(values);
969
Michael Jurka414300a2013-08-27 15:42:35 +0200970 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700971 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700973
Jason Monk8e19cf22014-03-20 15:06:57 -0400974 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700975 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700976 public void run() {
977 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
978 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700980 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700981 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400982 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700983 sBgItemsIdMap.put(item.id, item);
984 switch (item.itemType) {
985 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
986 sBgFolders.put(item.id, (FolderInfo) item);
987 // Fall through
988 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
989 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
990 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
991 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
992 sBgWorkspaceItems.add(item);
993 } else {
994 if (!sBgFolders.containsKey(item.container)) {
995 // Adding an item to a folder that doesn't exist.
996 String msg = "adding item: " + item + " to a folder that " +
997 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700998 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700999 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001000 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001001 break;
1002 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1003 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1004 break;
1005 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 }
1007 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001008 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001010 }
1011
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001013 * Creates a new unique child id, for a given cell span across all layouts.
1014 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001015 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001017 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001018 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001019 }
1020
Winson Chungaafa03c2010-06-11 17:34:16 -07001021 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 * Removes the specified item from the database
1023 * @param context
1024 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001026 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001028 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001029
Michael Jurka83df1882011-08-31 20:59:26 -07001030 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001031 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001032 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001033
1034 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001035 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001036 switch (item.itemType) {
1037 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1038 sBgFolders.remove(item.id);
1039 for (ItemInfo info: sBgItemsIdMap.values()) {
1040 if (info.container == item.id) {
1041 // We are deleting a folder which still contains items that
1042 // think they are contained by that folder.
1043 String msg = "deleting a folder (" + item + ") which still " +
1044 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001045 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001047 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048 sBgWorkspaceItems.remove(item);
1049 break;
1050 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1051 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1052 sBgWorkspaceItems.remove(item);
1053 break;
1054 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1055 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1056 break;
1057 }
1058 sBgItemsIdMap.remove(item.id);
1059 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001060 }
1061 }
Michael Jurka83df1882011-08-31 20:59:26 -07001062 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 }
1065
1066 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001067 * Update the order of the workspace screens in the database. The array list contains
1068 * a list of screen ids in the order that they should appear.
1069 */
Winson Chungc9168342013-06-26 14:54:55 -07001070 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001071 // Log to disk
1072 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1073 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1074
Winson Chung64359a52013-07-08 17:17:08 -07001075 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 final ContentResolver cr = context.getContentResolver();
1077 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1078
1079 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001080 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001081 while (iter.hasNext()) {
1082 long id = iter.next();
1083 if (id < 0) {
1084 iter.remove();
1085 }
1086 }
1087
1088 Runnable r = new Runnable() {
1089 @Override
1090 public void run() {
Yura085c8532014-02-11 15:15:29 +00001091 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001093 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001094 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 for (int i = 0; i < count; i++) {
1096 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001097 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1099 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001100 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001101 }
Yura085c8532014-02-11 15:15:29 +00001102
1103 try {
1104 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1105 } catch (Exception ex) {
1106 throw new RuntimeException(ex);
1107 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001108
Winson Chungba9c37f2013-08-30 14:11:37 -07001109 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001110 sBgWorkspaceScreens.clear();
1111 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001112 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001113 }
1114 };
1115 runOnWorkerThread(r);
1116 }
1117
1118 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001119 * Remove the contents of the specified folder from the database
1120 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001121 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 final ContentResolver cr = context.getContentResolver();
1123
Michael Jurkac9d95c52011-08-29 14:03:34 -07001124 Runnable r = new Runnable() {
1125 public void run() {
1126 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001127 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001128 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001129 sBgItemsIdMap.remove(info.id);
1130 sBgFolders.remove(info.id);
1131 sBgDbIconCache.remove(info);
1132 sBgWorkspaceItems.remove(info);
1133 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001134
Michael Jurkac9d95c52011-08-29 14:03:34 -07001135 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1136 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001137 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001138 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001139 for (ItemInfo childInfo : info.contents) {
1140 sBgItemsIdMap.remove(childInfo.id);
1141 sBgDbIconCache.remove(childInfo);
1142 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001143 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001144 }
1145 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001146 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 }
1148
1149 /**
1150 * Set this as the current Launcher activity object for the loader.
1151 */
1152 public void initialize(Callbacks callbacks) {
1153 synchronized (mLock) {
1154 mCallbacks = new WeakReference<Callbacks>(callbacks);
1155 }
1156 }
1157
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001158 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1160 * ACTION_PACKAGE_CHANGED.
1161 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001162 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001163 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001164 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001165
Joe Onorato36115782010-06-17 13:28:48 -04001166 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001167
Joe Onorato36115782010-06-17 13:28:48 -04001168 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1169 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1170 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1171 final String packageName = intent.getData().getSchemeSpecificPart();
1172 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173
Joe Onorato36115782010-06-17 13:28:48 -04001174 int op = PackageUpdatedTask.OP_NONE;
1175
1176 if (packageName == null || packageName.length() == 0) {
1177 // they sent us a bad intent
1178 return;
1179 }
1180
1181 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1182 op = PackageUpdatedTask.OP_UPDATE;
1183 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1184 if (!replacing) {
1185 op = PackageUpdatedTask.OP_REMOVE;
1186 }
1187 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1188 // later, we will update the package at this time
1189 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1190 if (!replacing) {
1191 op = PackageUpdatedTask.OP_ADD;
1192 } else {
1193 op = PackageUpdatedTask.OP_UPDATE;
1194 }
1195 }
1196
1197 if (op != PackageUpdatedTask.OP_NONE) {
1198 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1199 }
1200
1201 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001202 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001203 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001204 if (!replacing) {
1205 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1206 if (mAppsCanBeOnRemoveableStorage) {
1207 // Only rebind if we support removable storage. It catches the case where
1208 // apps on the external sd card need to be reloaded
1209 startLoaderFromBackground();
1210 }
1211 } else {
1212 // If we are replacing then just update the packages in the list
1213 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1214 packages));
1215 }
Joe Onorato36115782010-06-17 13:28:48 -04001216 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001217 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1218 if (!replacing) {
1219 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1220 enqueuePackageUpdated(new PackageUpdatedTask(
1221 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1222 }
1223 // else, we are replacing the packages, so ignore this event and wait for
1224 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001225 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001226 // If we have changed locale we need to clear out the labels in all apps/workspace.
1227 forceReload();
1228 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1229 // Check if configuration change was an mcc/mnc change which would affect app resources
1230 // and we would need to clear out the labels in all apps/workspace. Same handling as
1231 // above for ACTION_LOCALE_CHANGED
1232 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001233 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001234 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001235 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001236 forceReload();
1237 }
1238 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001239 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001240 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1241 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001242 if (mCallbacks != null) {
1243 Callbacks callbacks = mCallbacks.get();
1244 if (callbacks != null) {
1245 callbacks.bindSearchablesChanged();
1246 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001247 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001248 }
1249 }
1250
Reena Lee93f824a2011-09-23 17:20:28 -07001251 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001252 resetLoadedState(true, true);
1253
Reena Lee93f824a2011-09-23 17:20:28 -07001254 // Do this here because if the launcher activity is running it will be restarted.
1255 // If it's not running startLoaderFromBackground will merely tell it that it needs
1256 // to reload.
1257 startLoaderFromBackground();
1258 }
1259
Winson Chungf0c6ae02012-03-21 16:10:31 -07001260 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1261 synchronized (mLock) {
1262 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1263 // mWorkspaceLoaded to true later
1264 stopLoaderLocked();
1265 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1266 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1267 }
1268 }
1269
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001270 /**
1271 * When the launcher is in the background, it's possible for it to miss paired
1272 * configuration changes. So whenever we trigger the loader from the background
1273 * tell the launcher that it needs to re-run the loader when it comes back instead
1274 * of doing it now.
1275 */
1276 public void startLoaderFromBackground() {
1277 boolean runLoader = false;
1278 if (mCallbacks != null) {
1279 Callbacks callbacks = mCallbacks.get();
1280 if (callbacks != null) {
1281 // Only actually run the loader if they're not paused.
1282 if (!callbacks.setLoadOnResume()) {
1283 runLoader = true;
1284 }
1285 }
1286 }
1287 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001288 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001289 }
Joe Onorato36115782010-06-17 13:28:48 -04001290 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001291
Reena Lee93f824a2011-09-23 17:20:28 -07001292 // If there is already a loader task running, tell it to stop.
1293 // returns true if isLaunching() was true on the old task
1294 private boolean stopLoaderLocked() {
1295 boolean isLaunching = false;
1296 LoaderTask oldTask = mLoaderTask;
1297 if (oldTask != null) {
1298 if (oldTask.isLaunching()) {
1299 isLaunching = true;
1300 }
1301 oldTask.stopLocked();
1302 }
1303 return isLaunching;
1304 }
1305
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001306 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001307 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1308 }
1309
1310 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001311 synchronized (mLock) {
1312 if (DEBUG_LOADERS) {
1313 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 // Clear any deferred bind-runnables from the synchronized load process
1317 // We must do this before any loading/binding is scheduled below.
1318 mDeferredBindRunnables.clear();
1319
Joe Onorato36115782010-06-17 13:28:48 -04001320 // Don't bother to start the thread if we know it's not going to do anything
1321 if (mCallbacks != null && mCallbacks.get() != null) {
1322 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001323 // also, don't downgrade isLaunching if we're already running
1324 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001325 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001326 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1327 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001328 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1329 } else {
1330 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1331 sWorker.post(mLoaderTask);
1332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001334 }
1335 }
1336
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001337 void bindRemainingSynchronousPages() {
1338 // Post the remaining side pages to be loaded
1339 if (!mDeferredBindRunnables.isEmpty()) {
1340 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001341 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 }
1343 mDeferredBindRunnables.clear();
1344 }
1345 }
1346
Joe Onorato36115782010-06-17 13:28:48 -04001347 public void stopLoader() {
1348 synchronized (mLock) {
1349 if (mLoaderTask != null) {
1350 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 }
1352 }
Joe Onorato36115782010-06-17 13:28:48 -04001353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354
Winson Chung76828c82013-08-19 15:43:29 -07001355 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1356 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1357 final ContentResolver contentResolver = context.getContentResolver();
1358 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1359 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1360 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1361
1362 try {
1363 final int idIndex = sc.getColumnIndexOrThrow(
1364 LauncherSettings.WorkspaceScreens._ID);
1365 final int rankIndex = sc.getColumnIndexOrThrow(
1366 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1367 while (sc.moveToNext()) {
1368 try {
1369 long screenId = sc.getLong(idIndex);
1370 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001371 orderedScreens.put(rank, screenId);
1372 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001373 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001374 }
1375 }
1376 } finally {
1377 sc.close();
1378 }
Winson Chunga90303b2013-11-15 13:05:06 -08001379
1380 // Log to disk
1381 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1382 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1383 for (Integer i : orderedScreens.keySet()) {
1384 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1385 }
1386 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1387 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001388 return orderedScreens;
1389 }
1390
Michael Jurkac57b7a82011-08-09 22:02:20 -07001391 public boolean isAllAppsLoaded() {
1392 return mAllAppsLoaded;
1393 }
1394
Winson Chung36a62fe2012-05-06 18:04:42 -07001395 boolean isLoadingWorkspace() {
1396 synchronized (mLock) {
1397 if (mLoaderTask != null) {
1398 return mLoaderTask.isLoadingWorkspace();
1399 }
1400 }
1401 return false;
1402 }
1403
Joe Onorato36115782010-06-17 13:28:48 -04001404 /**
1405 * Runnable for the thread that loads the contents of the launcher:
1406 * - workspace icons
1407 * - widgets
1408 * - all apps icons
1409 */
1410 private class LoaderTask implements Runnable {
1411 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001412 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001413 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001414 private boolean mStopped;
1415 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001416 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001417
Winson Chungc3eecff2011-07-11 17:44:15 -07001418 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001419
Dan Sandlerd5024042014-01-09 15:01:33 -05001420 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001421 mContext = context;
1422 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001423 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001424 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 }
1426
Joe Onorato36115782010-06-17 13:28:48 -04001427 boolean isLaunching() {
1428 return mIsLaunching;
1429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430
Winson Chung36a62fe2012-05-06 18:04:42 -07001431 boolean isLoadingWorkspace() {
1432 return mIsLoadingAndBindingWorkspace;
1433 }
1434
Winson Chungc763c4e2013-07-19 13:49:06 -07001435 /** Returns whether this is an upgrade path */
1436 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001437 mIsLoadingAndBindingWorkspace = true;
1438
Joe Onorato36115782010-06-17 13:28:48 -04001439 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001440 if (DEBUG_LOADERS) {
1441 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001443
Winson Chungc763c4e2013-07-19 13:49:06 -07001444 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001445 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001446 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001447 synchronized (LoaderTask.this) {
1448 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001449 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001450 }
1451 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 }
1453 }
1454
Joe Onorato36115782010-06-17 13:28:48 -04001455 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001456 bindWorkspace(-1, isUpgradePath);
1457 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001458 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001459
Joe Onorato36115782010-06-17 13:28:48 -04001460 private void waitForIdle() {
1461 // Wait until the either we're stopped or the other threads are done.
1462 // This way we don't start loading all apps until the workspace has settled
1463 // down.
1464 synchronized (LoaderTask.this) {
1465 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001466
Joe Onorato36115782010-06-17 13:28:48 -04001467 mHandler.postIdle(new Runnable() {
1468 public void run() {
1469 synchronized (LoaderTask.this) {
1470 mLoadAndBindStepFinished = true;
1471 if (DEBUG_LOADERS) {
1472 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001473 }
Joe Onorato36115782010-06-17 13:28:48 -04001474 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001475 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477 });
1478
Michael Jurkac7700af2013-05-14 20:17:58 +02001479 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001480 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001481 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1482 // wait no longer than 1sec at a time
1483 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001484 } catch (InterruptedException ex) {
1485 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001486 }
1487 }
Joe Onorato36115782010-06-17 13:28:48 -04001488 if (DEBUG_LOADERS) {
1489 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001490 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001491 + "ms for previous step to finish binding");
1492 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001495
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001496 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001497 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001498 // Ensure that we have a valid page index to load synchronously
1499 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1500 "valid page index");
1501 }
1502 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1503 // Ensure that we don't try and bind a specified page when the pages have not been
1504 // loaded already (we should load everything asynchronously in that case)
1505 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1506 }
1507 synchronized (mLock) {
1508 if (mIsLoaderTaskRunning) {
1509 // Ensure that we are never running the background loading at this point since
1510 // we also touch the background collections
1511 throw new RuntimeException("Error! Background loading is already running");
1512 }
1513 }
1514
1515 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1516 // data structures, we can't allow any other thread to touch that data, but because
1517 // this call is synchronous, we can get away with not locking).
1518
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001519 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001520 // operations from the previous activity. We need to ensure that all queued operations
1521 // are executed before any synchronous binding work is done.
1522 mHandler.flush();
1523
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001524 // Divide the set of loaded items into those that we are binding synchronously, and
1525 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001526 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001527 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1528 // arise from that.
1529 onlyBindAllApps();
1530 }
1531
Joe Onorato36115782010-06-17 13:28:48 -04001532 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001533 boolean isUpgrade = false;
1534
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001535 synchronized (mLock) {
1536 mIsLoaderTaskRunning = true;
1537 }
Joe Onorato36115782010-06-17 13:28:48 -04001538 // Optimize for end-user experience: if the Launcher is up and // running with the
1539 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1540 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001541 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001542 // Elevate priority when Home launches for the first time to avoid
1543 // starving at boot time. Staring at a blank home is not cool.
1544 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001545 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1546 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001547 android.os.Process.setThreadPriority(mIsLaunching
1548 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1549 }
Winson Chung64359a52013-07-08 17:17:08 -07001550 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001551 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001552
Joe Onorato36115782010-06-17 13:28:48 -04001553 if (mStopped) {
1554 break keep_running;
1555 }
1556
1557 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1558 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001559 synchronized (mLock) {
1560 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001561 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001562 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1563 }
1564 }
Joe Onorato36115782010-06-17 13:28:48 -04001565 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001566
1567 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001568 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1569 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001570
1571 // Restore the default thread priority after we are done loading items
1572 synchronized (mLock) {
1573 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1574 }
Joe Onorato36115782010-06-17 13:28:48 -04001575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576
Winson Chungaac01e12011-08-17 10:37:13 -07001577 // Update the saved icons if necessary
1578 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001579 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001580 for (Object key : sBgDbIconCache.keySet()) {
1581 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1582 }
1583 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001584 }
Winson Chungaac01e12011-08-17 10:37:13 -07001585
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001586 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001587 // Ensure that all the applications that are in the system are
1588 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001589 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001590 verifyApplications();
1591 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001592 }
1593
Joe Onorato36115782010-06-17 13:28:48 -04001594 // Clear out this reference, otherwise we end up holding it until all of the
1595 // callback runnables are done.
1596 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001597
Joe Onorato36115782010-06-17 13:28:48 -04001598 synchronized (mLock) {
1599 // If we are still the last one to be scheduled, remove ourselves.
1600 if (mLoaderTask == this) {
1601 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001603 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
Joe Onorato36115782010-06-17 13:28:48 -04001605 }
1606
1607 public void stopLocked() {
1608 synchronized (LoaderTask.this) {
1609 mStopped = true;
1610 this.notify();
1611 }
1612 }
1613
1614 /**
1615 * Gets the callbacks object. If we've been stopped, or if the launcher object
1616 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1617 * object that was around when the deferred message was scheduled, and if there's
1618 * a new Callbacks object around then also return null. This will save us from
1619 * calling onto it with data that will be ignored.
1620 */
1621 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1622 synchronized (mLock) {
1623 if (mStopped) {
1624 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626
1627 if (mCallbacks == null) {
1628 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001629 }
Joe Onorato36115782010-06-17 13:28:48 -04001630
1631 final Callbacks callbacks = mCallbacks.get();
1632 if (callbacks != oldCallbacks) {
1633 return null;
1634 }
1635 if (callbacks == null) {
1636 Log.w(TAG, "no mCallbacks");
1637 return null;
1638 }
1639
1640 return callbacks;
1641 }
1642 }
1643
Winson Chungc763c4e2013-07-19 13:49:06 -07001644 private void verifyApplications() {
1645 final Context context = mApp.getContext();
1646
1647 // Cross reference all the applications in our apps list with items in the workspace
1648 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001649 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001650 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001651 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001652 tmpInfos = getItemInfoForComponentName(app.componentName);
1653 if (tmpInfos.isEmpty()) {
1654 // We are missing an application icon, so add this to the workspace
1655 added.add(app);
1656 // This is a rare event, so lets log it
1657 Log.e(TAG, "Missing Application on load: " + app);
1658 }
1659 }
1660 }
1661 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001662 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001663 }
1664 }
1665
Joe Onorato36115782010-06-17 13:28:48 -04001666 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001667 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001668 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001669 LauncherAppState app = LauncherAppState.getInstance();
1670 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001671 final int countX = (int) grid.numColumns;
1672 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001673
Adam Cohendcd297f2013-06-18 13:13:40 -07001674 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001675 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001676 // Return early if we detect that an item is under the hotseat button
1677 if (mCallbacks == null ||
1678 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001679 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001680 Log.e(TAG, "Error loading shortcut into hotseat " + item
1681 + " into position (" + item.screenId + ":" + item.cellX + ","
1682 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001683 return false;
1684 }
1685
Dan Sandler295ae182013-12-10 16:05:47 -05001686 final ItemInfo[][] hotseatItems =
1687 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1688
Adam Cohenae4409d2013-11-26 10:34:59 -08001689 if (item.screenId >= grid.numHotseatIcons) {
1690 Log.e(TAG, "Error loading shortcut " + item
1691 + " into hotseat position " + item.screenId
1692 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1693 + ")");
1694 return false;
1695 }
1696
Dan Sandler295ae182013-12-10 16:05:47 -05001697 if (hotseatItems != null) {
1698 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001699 Log.e(TAG, "Error loading shortcut into hotseat " + item
1700 + " into position (" + item.screenId + ":" + item.cellX + ","
1701 + item.cellY + ") occupied by "
1702 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1703 [(int) item.screenId][0]);
1704 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001705 } else {
1706 hotseatItems[(int) item.screenId][0] = item;
1707 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001708 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001709 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001710 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001711 items[(int) item.screenId][0] = item;
1712 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001713 return true;
1714 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001715 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1716 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001717 return true;
1718 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001719
Adam Cohendcd297f2013-06-18 13:13:40 -07001720 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001721 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001722 occupied.put(item.screenId, items);
1723 }
1724
Dan Sandler295ae182013-12-10 16:05:47 -05001725 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001726 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1727 item.cellX < 0 || item.cellY < 0 ||
1728 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1729 Log.e(TAG, "Error loading shortcut " + item
1730 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1731 + item.cellX + "," + item.cellY
1732 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1733 return false;
1734 }
1735
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001736 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001737 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1738 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001739 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001740 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001741 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001742 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001743 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001744 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001745 return false;
1746 }
1747 }
1748 }
1749 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1750 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001751 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001752 }
1753 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001754
Joe Onorato36115782010-06-17 13:28:48 -04001755 return true;
1756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757
Winson Chungba9c37f2013-08-30 14:11:37 -07001758 /** Clears all the sBg data structures */
1759 private void clearSBgDataStructures() {
1760 synchronized (sBgLock) {
1761 sBgWorkspaceItems.clear();
1762 sBgAppWidgets.clear();
1763 sBgFolders.clear();
1764 sBgItemsIdMap.clear();
1765 sBgDbIconCache.clear();
1766 sBgWorkspaceScreens.clear();
1767 }
1768 }
1769
Dan Sandlerd5024042014-01-09 15:01:33 -05001770 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001771 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001772 // Log to disk
1773 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1774
Joe Onorato36115782010-06-17 13:28:48 -04001775 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776
Joe Onorato36115782010-06-17 13:28:48 -04001777 final Context context = mContext;
1778 final ContentResolver contentResolver = context.getContentResolver();
1779 final PackageManager manager = context.getPackageManager();
1780 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1781 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001782
Winson Chung892c74d2013-08-22 16:15:50 -07001783 LauncherAppState app = LauncherAppState.getInstance();
1784 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1785 int countX = (int) grid.numColumns;
1786 int countY = (int) grid.numRows;
1787
Dan Sandlerd5024042014-01-09 15:01:33 -05001788 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1789 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1790 LauncherAppState.getLauncherProvider().deleteDatabase();
1791 }
1792
1793 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1794 // append the user's Launcher2 shortcuts
1795 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1796 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1797 } else {
1798 // Make sure the default workspace is loaded
1799 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1800 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1801 }
Adam Cohene25af792013-06-06 23:08:25 -07001802
Winson Chungc763c4e2013-07-19 13:49:06 -07001803 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001804 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001805 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001806
Winson Chung9f9f00b2013-11-15 13:27:00 -08001807 // Log to disk
1808 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001809
Winson Chung2abf94d2012-07-18 18:16:38 -07001810 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001811 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001812
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001814 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001815 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001816 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001817 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001818
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 // +1 for the hotseat (it can be larger than the workspace)
1820 // Load workspace in reverse order to ensure that latest items are loaded first (and
1821 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001822 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 try {
1825 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1826 final int intentIndex = c.getColumnIndexOrThrow
1827 (LauncherSettings.Favorites.INTENT);
1828 final int titleIndex = c.getColumnIndexOrThrow
1829 (LauncherSettings.Favorites.TITLE);
1830 final int iconTypeIndex = c.getColumnIndexOrThrow(
1831 LauncherSettings.Favorites.ICON_TYPE);
1832 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1833 final int iconPackageIndex = c.getColumnIndexOrThrow(
1834 LauncherSettings.Favorites.ICON_PACKAGE);
1835 final int iconResourceIndex = c.getColumnIndexOrThrow(
1836 LauncherSettings.Favorites.ICON_RESOURCE);
1837 final int containerIndex = c.getColumnIndexOrThrow(
1838 LauncherSettings.Favorites.CONTAINER);
1839 final int itemTypeIndex = c.getColumnIndexOrThrow(
1840 LauncherSettings.Favorites.ITEM_TYPE);
1841 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1842 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001843 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1844 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 final int screenIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.SCREEN);
1847 final int cellXIndex = c.getColumnIndexOrThrow
1848 (LauncherSettings.Favorites.CELLX);
1849 final int cellYIndex = c.getColumnIndexOrThrow
1850 (LauncherSettings.Favorites.CELLY);
1851 final int spanXIndex = c.getColumnIndexOrThrow
1852 (LauncherSettings.Favorites.SPANX);
1853 final int spanYIndex = c.getColumnIndexOrThrow(
1854 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001855 final int restoredIndex = c.getColumnIndexOrThrow(
1856 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1858 //final int displayModeIndex = c.getColumnIndexOrThrow(
1859 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 ShortcutInfo info;
1862 String intentDescription;
1863 LauncherAppWidgetInfo appWidgetInfo;
1864 int container;
1865 long id;
1866 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001867
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001869 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 try {
1871 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001872 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001873
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 switch (itemType) {
1875 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1876 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001877 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 intentDescription = c.getString(intentIndex);
1879 try {
1880 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001881 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001882 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001883 if (restored) {
1884 // might be installed later
1885 Launcher.addDumpLog(TAG,
1886 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001887 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001888 if (!mAppsCanBeOnRemoveableStorage) {
1889 // Log the invalid package, and remove it
1890 Launcher.addDumpLog(TAG,
1891 "Invalid package removed: " + cn, true);
1892 itemsToRemove.add(id);
1893 } else {
1894 // If apps can be on external storage, then we just
1895 // leave them for the user to remove (maybe add
1896 // visual treatment to it)
1897 Launcher.addDumpLog(TAG,
1898 "Invalid package found: " + cn, true);
1899 }
1900 continue;
Winson Chungee055712013-07-30 14:46:24 -07001901 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001902 } else if (restored) {
1903 // no special handling necessary for this restored item
1904 restoredRows.add(id);
1905 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001908 Launcher.addDumpLog(TAG,
1909 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 continue;
1911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912
Chris Wrenf4d08112014-01-16 18:13:56 -05001913 if (restored) {
1914 Launcher.addDumpLog(TAG,
1915 "constructing info for partially restored package",
1916 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001917 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001918 intent = getRestoredItemIntent(c, context, intent);
1919 } else if (itemType ==
1920 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1922 titleIndex, mLabelCache);
1923 } else {
1924 info = getShortcutInfo(c, context, iconTypeIndex,
1925 iconPackageIndex, iconResourceIndex, iconIndex,
1926 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001927
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 // App shortcuts that used to be automatically added to Launcher
1929 // didn't always have the correct intent flags set, so do that
1930 // here
1931 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001932 intent.getCategories() != null &&
1933 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001934 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001935 intent.addFlags(
1936 Intent.FLAG_ACTIVITY_NEW_TASK |
1937 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1938 }
Michael Jurka96879562012-03-22 05:54:33 -07001939 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001941 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001942 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 container = c.getInt(containerIndex);
1945 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 info.cellX = c.getInt(cellXIndex);
1948 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001949 info.spanX = 1;
1950 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001951
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001952 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001953 deleteOnInvalidPlacement.set(false);
1954 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1955 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001956 itemsToRemove.add(id);
1957 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001958 break;
1959 }
1960
1961 switch (container) {
1962 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1963 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1964 sBgWorkspaceItems.add(info);
1965 break;
1966 default:
1967 // Item is in a user folder
1968 FolderInfo folderInfo =
1969 findOrMakeFolder(sBgFolders, container);
1970 folderInfo.add(info);
1971 break;
1972 }
1973 sBgItemsIdMap.put(info.id, info);
1974
1975 // now that we've loaded everthing re-save it with the
1976 // icon in case it disappears somehow.
1977 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001978 } else {
1979 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 }
1981 break;
1982
1983 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1984 id = c.getLong(idIndex);
1985 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1986
1987 folderInfo.title = c.getString(titleIndex);
1988 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 folderInfo.cellX = c.getInt(cellXIndex);
1993 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001994 folderInfo.spanX = 1;
1995 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001996
Daniel Sandler8802e962010-05-26 16:28:16 -04001997 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001998 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001999 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002000 deleteOnInvalidPlacement)) {
2001 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002002 itemsToRemove.add(id);
2003 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002004 break;
2005 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002006
Joe Onorato9c1289c2009-08-17 11:03:03 -04002007 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2009 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2010 sBgWorkspaceItems.add(folderInfo);
2011 break;
Joe Onorato36115782010-06-17 13:28:48 -04002012 }
Joe Onorato17a89222011-02-08 17:26:11 -08002013
Chris Wrenf4d08112014-01-16 18:13:56 -05002014 if (restored) {
2015 // no special handling required for restored folders
2016 restoredRows.add(id);
2017 }
2018
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2020 sBgFolders.put(folderInfo.id, folderInfo);
2021 break;
2022
2023 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2024 // Read all Launcher-specific widget details
2025 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002026 String savedProvider = c.getString(appWidgetProviderIndex);
2027
Joe Onorato36115782010-06-17 13:28:48 -04002028 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002029
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 final AppWidgetProviderInfo provider =
2031 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002032
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 if (!isSafeMode && (provider == null || provider.provider == null ||
2034 provider.provider.getPackageName() == null)) {
2035 String log = "Deleting widget that isn't installed anymore: id="
2036 + id + " appWidgetId=" + appWidgetId;
2037 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002038 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 itemsToRemove.add(id);
2040 } else {
2041 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2042 provider.provider);
2043 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002044 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 appWidgetInfo.cellX = c.getInt(cellXIndex);
2046 appWidgetInfo.cellY = c.getInt(cellYIndex);
2047 appWidgetInfo.spanX = c.getInt(spanXIndex);
2048 appWidgetInfo.spanY = c.getInt(spanYIndex);
2049 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2050 appWidgetInfo.minSpanX = minSpan[0];
2051 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002052
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 container = c.getInt(containerIndex);
2054 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2055 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2056 Log.e(TAG, "Widget found where container != " +
2057 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2058 continue;
2059 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060
Adam Cohene25af792013-06-06 23:08:25 -07002061 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002063 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002064 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002065 deleteOnInvalidPlacement)) {
2066 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002067 itemsToRemove.add(id);
2068 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 break;
2070 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002071 String providerName = provider.provider.flattenToString();
2072 if (!providerName.equals(savedProvider)) {
2073 ContentValues values = new ContentValues();
2074 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2075 providerName);
2076 String where = BaseColumns._ID + "= ?";
2077 String[] args = {Integer.toString(c.getInt(idIndex))};
2078 contentResolver.update(contentUri, values, where, args);
2079 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2081 sBgAppWidgets.add(appWidgetInfo);
2082 }
Joe Onorato36115782010-06-17 13:28:48 -04002083 break;
2084 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002086 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002087 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 }
2089 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002090 if (c != null) {
2091 c.close();
2092 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 }
2094
Winson Chungba9c37f2013-08-30 14:11:37 -07002095 // Break early if we've stopped loading
2096 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002097 clearSBgDataStructures();
2098 return false;
2099 }
2100
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 if (itemsToRemove.size() > 0) {
2102 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002103 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 // Remove dead items
2105 for (long id : itemsToRemove) {
2106 if (DEBUG_LOADERS) {
2107 Log.d(TAG, "Removed id = " + id);
2108 }
2109 // Don't notify content observers
2110 try {
2111 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2112 null, null);
2113 } catch (RemoteException e) {
2114 Log.w(TAG, "Could not remove id = " + id);
2115 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002116 }
2117 }
2118
Chris Wrenf4d08112014-01-16 18:13:56 -05002119 if (restoredRows.size() > 0) {
2120 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2121 LauncherSettings.Favorites.CONTENT_URI);
2122 // Update restored items that no longer require special handling
2123 try {
2124 StringBuilder selectionBuilder = new StringBuilder();
2125 selectionBuilder.append(LauncherSettings.Favorites._ID);
2126 selectionBuilder.append(" IN (");
2127 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2128 selectionBuilder.append(")");
2129 ContentValues values = new ContentValues();
2130 values.put(LauncherSettings.Favorites.RESTORED, 0);
2131 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2132 values, selectionBuilder.toString(), null);
2133 } catch (RemoteException e) {
2134 Log.w(TAG, "Could not update restored rows");
2135 }
2136 }
2137
Winson Chungc763c4e2013-07-19 13:49:06 -07002138 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 long maxScreenId = 0;
2140 // If we're importing we use the old screen order.
2141 for (ItemInfo item: sBgItemsIdMap.values()) {
2142 long screenId = item.screenId;
2143 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2144 !sBgWorkspaceScreens.contains(screenId)) {
2145 sBgWorkspaceScreens.add(screenId);
2146 if (screenId > maxScreenId) {
2147 maxScreenId = screenId;
2148 }
2149 }
2150 }
2151 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002152 // Log to disk
2153 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2154 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2155 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002156
Michael Jurka414300a2013-08-27 15:42:35 +02002157 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002158 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002159
2160 // Update the max item id after we load an old db
2161 long maxItemId = 0;
2162 // If we're importing we use the old screen order.
2163 for (ItemInfo item: sBgItemsIdMap.values()) {
2164 maxItemId = Math.max(maxItemId, item.id);
2165 }
Michael Jurka414300a2013-08-27 15:42:35 +02002166 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002167 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002168 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2169 for (Integer i : orderedScreens.keySet()) {
2170 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002171 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002172 // Log to disk
2173 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2174 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002175
2176 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002177 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002178 for (ItemInfo item: sBgItemsIdMap.values()) {
2179 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2181 unusedScreens.contains(screenId)) {
2182 unusedScreens.remove(screenId);
2183 }
2184 }
2185
2186 // If there are any empty screens remove them, and update.
2187 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002188 // Log to disk
2189 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2190 TextUtils.join(", ", unusedScreens), true);
2191
Winson Chung933bae62013-08-29 11:42:30 -07002192 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2194 }
2195 }
2196
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 if (DEBUG_LOADERS) {
2198 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2199 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002201 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002203
Daniel Sandler566da102013-06-25 23:43:45 -04002204 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002205 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002206 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002207 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 line += " | ";
2209 }
Winson Chung892c74d2013-08-22 16:15:50 -07002210 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002211 ItemInfo[][] screen = occupied.get(screenId);
2212 if (x < screen.length && y < screen[x].length) {
2213 line += (screen[x][y] != null) ? "#" : ".";
2214 } else {
2215 line += "!";
2216 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 }
Joe Onorato36115782010-06-17 13:28:48 -04002218 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002219 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 }
Joe Onorato36115782010-06-17 13:28:48 -04002222 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002223 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002224 }
2225
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 /** Filters the set of items who are directly or indirectly (via another container) on the
2227 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002228 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 ArrayList<ItemInfo> allWorkspaceItems,
2230 ArrayList<ItemInfo> currentScreenItems,
2231 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002232 // Purge any null ItemInfos
2233 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2234 while (iter.hasNext()) {
2235 ItemInfo i = iter.next();
2236 if (i == null) {
2237 iter.remove();
2238 }
2239 }
2240
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 // Order the set of items by their containers first, this allows use to walk through the
2242 // list sequentially, build up a list of containers that are in the specified screen,
2243 // as well as all items in those containers.
2244 Set<Long> itemsOnScreen = new HashSet<Long>();
2245 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2246 @Override
2247 public int compare(ItemInfo lhs, ItemInfo rhs) {
2248 return (int) (lhs.container - rhs.container);
2249 }
2250 });
2251 for (ItemInfo info : allWorkspaceItems) {
2252 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002253 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 currentScreenItems.add(info);
2255 itemsOnScreen.add(info.id);
2256 } else {
2257 otherScreenItems.add(info);
2258 }
2259 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2260 currentScreenItems.add(info);
2261 itemsOnScreen.add(info.id);
2262 } else {
2263 if (itemsOnScreen.contains(info.container)) {
2264 currentScreenItems.add(info);
2265 itemsOnScreen.add(info.id);
2266 } else {
2267 otherScreenItems.add(info);
2268 }
2269 }
2270 }
2271 }
2272
2273 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002274 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 ArrayList<LauncherAppWidgetInfo> appWidgets,
2276 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2277 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278
2279 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002280 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002282 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 currentScreenWidgets.add(widget);
2284 } else {
2285 otherScreenWidgets.add(widget);
2286 }
2287 }
2288 }
2289
2290 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002291 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 HashMap<Long, ItemInfo> itemsIdMap,
2293 HashMap<Long, FolderInfo> folders,
2294 HashMap<Long, FolderInfo> currentScreenFolders,
2295 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296
2297 for (long id : folders.keySet()) {
2298 ItemInfo info = itemsIdMap.get(id);
2299 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002300 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002302 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303 currentScreenFolders.put(id, folder);
2304 } else {
2305 otherScreenFolders.put(id, folder);
2306 }
2307 }
2308 }
2309
2310 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2311 * right) */
2312 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002313 final LauncherAppState app = LauncherAppState.getInstance();
2314 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 // XXX: review this
2316 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002317 @Override
2318 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002319 int cellCountX = (int) grid.numColumns;
2320 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002321 int screenOffset = cellCountX * cellCountY;
2322 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002324 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002326 rhs.cellY * cellCountX + rhs.cellX);
2327 return (int) (lr - rr);
2328 }
2329 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002331
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2333 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 final Runnable r = new Runnable() {
2335 @Override
2336 public void run() {
2337 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2338 if (callbacks != null) {
2339 callbacks.bindScreens(orderedScreens);
2340 }
2341 }
2342 };
2343 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2344 }
2345
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2347 final ArrayList<ItemInfo> workspaceItems,
2348 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2349 final HashMap<Long, FolderInfo> folders,
2350 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002351
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352 final boolean postOnMainThread = (deferredBindRunnables != null);
2353
2354 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002355 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002356 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002357 final int start = i;
2358 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359 final Runnable r = new Runnable() {
2360 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002361 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002362 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002363 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002364 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2365 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002366 }
2367 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002368 };
2369 if (postOnMainThread) {
2370 deferredBindRunnables.add(r);
2371 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002372 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002373 }
Joe Onorato36115782010-06-17 13:28:48 -04002374 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375
2376 // Bind the folders
2377 if (!folders.isEmpty()) {
2378 final Runnable r = new Runnable() {
2379 public void run() {
2380 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2381 if (callbacks != null) {
2382 callbacks.bindFolders(folders);
2383 }
2384 }
2385 };
2386 if (postOnMainThread) {
2387 deferredBindRunnables.add(r);
2388 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002389 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 }
2391 }
2392
2393 // Bind the widgets, one at a time
2394 N = appWidgets.size();
2395 for (int i = 0; i < N; i++) {
2396 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2397 final Runnable r = new Runnable() {
2398 public void run() {
2399 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2400 if (callbacks != null) {
2401 callbacks.bindAppWidget(widget);
2402 }
2403 }
2404 };
2405 if (postOnMainThread) {
2406 deferredBindRunnables.add(r);
2407 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002408 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 }
2410 }
2411 }
2412
2413 /**
2414 * Binds all loaded data to actual views on the main thread.
2415 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002416 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002417 final long t = SystemClock.uptimeMillis();
2418 Runnable r;
2419
2420 // Don't use these two variables in any of the callback runnables.
2421 // Otherwise we hold a reference to them.
2422 final Callbacks oldCallbacks = mCallbacks.get();
2423 if (oldCallbacks == null) {
2424 // This launcher has exited and nobody bothered to tell us. Just bail.
2425 Log.w(TAG, "LoaderTask running with no launcher");
2426 return;
2427 }
2428
Winson Chung9b9fb962013-11-15 15:39:34 -08002429 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2431 ArrayList<LauncherAppWidgetInfo> appWidgets =
2432 new ArrayList<LauncherAppWidgetInfo>();
2433 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2434 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002435 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002436 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 workspaceItems.addAll(sBgWorkspaceItems);
2438 appWidgets.addAll(sBgAppWidgets);
2439 folders.putAll(sBgFolders);
2440 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002441 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002442 }
2443
Derek Prothro7aff3992013-12-10 14:00:37 -05002444 final boolean isLoadingSynchronously =
2445 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002446 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002447 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002448 if (currScreen >= orderedScreenIds.size()) {
2449 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002450 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002451 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002452 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002453 final long currentScreenId = currentScreen < 0
2454 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002455
2456 // Load all the items that are on the current page first (and in the process, unbind
2457 // all the existing workspace items before we call startBinding() below.
2458 unbindWorkspaceItemsOnMainThread();
2459
2460 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002461 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2462 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2463 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2464 new ArrayList<LauncherAppWidgetInfo>();
2465 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2466 new ArrayList<LauncherAppWidgetInfo>();
2467 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2468 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2469
Winson Chung9b9fb962013-11-15 15:39:34 -08002470 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002472 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002474 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 otherFolders);
2476 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2477 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2478
2479 // Tell the workspace that we're about to start binding items
2480 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002481 public void run() {
2482 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2483 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002485 }
2486 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002487 };
Winson Chung81b52252012-08-27 15:34:29 -07002488 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002489
Adam Cohendcd297f2013-06-18 13:13:40 -07002490 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2491
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 // Load items on the current page
2493 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2494 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002495 if (isLoadingSynchronously) {
2496 r = new Runnable() {
2497 public void run() {
2498 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002499 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002500 callbacks.onPageBoundSynchronously(currentScreen);
2501 }
2502 }
2503 };
Winson Chung81b52252012-08-27 15:34:29 -07002504 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002505 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002506
Winson Chung4a2afa32012-07-19 14:53:05 -07002507 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2508 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 mDeferredBindRunnables.clear();
2510 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002511 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002512
2513 // Tell the workspace that we're done binding items
2514 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002515 public void run() {
2516 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2517 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002518 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002519 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520
Winson Chung98e030b2012-05-07 16:01:11 -07002521 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002522 if (DEBUG_LOADERS) {
2523 Log.d(TAG, "bound workspace in "
2524 + (SystemClock.uptimeMillis()-t) + "ms");
2525 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002526
2527 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002528 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002529 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002530 if (isLoadingSynchronously) {
2531 mDeferredBindRunnables.add(r);
2532 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002533 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002534 }
Joe Onorato36115782010-06-17 13:28:48 -04002535 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002536
Joe Onorato36115782010-06-17 13:28:48 -04002537 private void loadAndBindAllApps() {
2538 if (DEBUG_LOADERS) {
2539 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2540 }
2541 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002542 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002543 synchronized (LoaderTask.this) {
2544 if (mStopped) {
2545 return;
2546 }
2547 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002548 }
Joe Onorato36115782010-06-17 13:28:48 -04002549 } else {
2550 onlyBindAllApps();
2551 }
2552 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002553
Joe Onorato36115782010-06-17 13:28:48 -04002554 private void onlyBindAllApps() {
2555 final Callbacks oldCallbacks = mCallbacks.get();
2556 if (oldCallbacks == null) {
2557 // This launcher has exited and nobody bothered to tell us. Just bail.
2558 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2559 return;
2560 }
2561
2562 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002563 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002564 final ArrayList<AppInfo> list
2565 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002566 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002567 public void run() {
2568 final long t = SystemClock.uptimeMillis();
2569 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2570 if (callbacks != null) {
2571 callbacks.bindAllApplications(list);
2572 }
2573 if (DEBUG_LOADERS) {
2574 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2575 + (SystemClock.uptimeMillis()-t) + "ms");
2576 }
2577 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002578 };
2579 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002580 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002581 r.run();
2582 } else {
2583 mHandler.post(r);
2584 }
Joe Onorato36115782010-06-17 13:28:48 -04002585 }
2586
Winson Chung64359a52013-07-08 17:17:08 -07002587 private void loadAllApps() {
2588 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002589
Joe Onorato36115782010-06-17 13:28:48 -04002590 final Callbacks oldCallbacks = mCallbacks.get();
2591 if (oldCallbacks == null) {
2592 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002593 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002594 return;
2595 }
2596
Winson Chung64359a52013-07-08 17:17:08 -07002597 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002598 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2599 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2600
Winson Chung64359a52013-07-08 17:17:08 -07002601 // Clear the list of apps
2602 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002603
Winson Chung64359a52013-07-08 17:17:08 -07002604 // Query for the set of apps
2605 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2606 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2607 if (DEBUG_LOADERS) {
2608 Log.d(TAG, "queryIntentActivities took "
2609 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2610 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2611 }
2612 // Fail if we don't have any apps
2613 if (apps == null || apps.isEmpty()) {
2614 return;
2615 }
2616 // Sort the applications by name
2617 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2618 Collections.sort(apps,
2619 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2620 if (DEBUG_LOADERS) {
2621 Log.d(TAG, "sort took "
2622 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002623 }
2624
Winson Chung64359a52013-07-08 17:17:08 -07002625 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002626 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002627 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002628 // This builds the icon bitmaps.
2629 mBgAllAppsList.add(new AppInfo(packageManager, app,
2630 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002631 }
2632
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002633 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002634 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2635 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002636
2637 // Post callback on main thread
2638 mHandler.post(new Runnable() {
2639 public void run() {
2640 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002641 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002642 if (callbacks != null) {
2643 callbacks.bindAllApplications(added);
2644 if (DEBUG_LOADERS) {
2645 Log.d(TAG, "bound " + added.size() + " apps in "
2646 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2647 }
2648 } else {
2649 Log.i(TAG, "not binding apps: no Launcher activity");
2650 }
2651 }
2652 });
2653
Joe Onorato36115782010-06-17 13:28:48 -04002654 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002655 Log.d(TAG, "Icons processed in "
2656 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002657 }
2658 }
2659
2660 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002661 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002662 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2663 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2664 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2665 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2666 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2667 }
Joe Onorato36115782010-06-17 13:28:48 -04002668 }
2669 }
2670
2671 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002672 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002673 }
2674
2675 private class PackageUpdatedTask implements Runnable {
2676 int mOp;
2677 String[] mPackages;
2678
2679 public static final int OP_NONE = 0;
2680 public static final int OP_ADD = 1;
2681 public static final int OP_UPDATE = 2;
2682 public static final int OP_REMOVE = 3; // uninstlled
2683 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2684
2685
2686 public PackageUpdatedTask(int op, String[] packages) {
2687 mOp = op;
2688 mPackages = packages;
2689 }
2690
2691 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002692 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002693
2694 final String[] packages = mPackages;
2695 final int N = packages.length;
2696 switch (mOp) {
2697 case OP_ADD:
2698 for (int i=0; i<N; i++) {
2699 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002700 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002701 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002702 }
2703 break;
2704 case OP_UPDATE:
2705 for (int i=0; i<N; i++) {
2706 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002707 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002708 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002709 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002710 }
2711 break;
2712 case OP_REMOVE:
2713 case OP_UNAVAILABLE:
2714 for (int i=0; i<N; i++) {
2715 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002716 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002717 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002718 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002719 }
2720 break;
2721 }
2722
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002723 ArrayList<AppInfo> added = null;
2724 ArrayList<AppInfo> modified = null;
2725 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002726
Adam Cohen487f7dd2012-06-28 18:12:10 -07002727 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002728 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002729 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002730 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002731 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002732 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002733 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002734 }
Winson Chung5d55f332012-07-16 20:45:03 -07002735 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002736 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002737 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002738 }
2739
Joe Onorato36115782010-06-17 13:28:48 -04002740 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2741 if (callbacks == null) {
2742 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2743 return;
2744 }
2745
2746 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002747 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002748 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002749 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002750 addAndBindAddedWorkspaceApps(context, addedInfos);
2751 } else {
2752 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002753 }
Joe Onorato36115782010-06-17 13:28:48 -04002754 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002755
Joe Onorato36115782010-06-17 13:28:48 -04002756 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002757 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002758
2759 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002760 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002761 ArrayList<ItemInfo> infos =
2762 getItemInfoForComponentName(a.componentName);
2763 for (ItemInfo i : infos) {
2764 if (isShortcutInfoUpdateable(i)) {
2765 ShortcutInfo info = (ShortcutInfo) i;
2766 info.title = a.title.toString();
2767 updateItemInDatabase(context, info);
2768 }
2769 }
2770 }
2771
Joe Onorato36115782010-06-17 13:28:48 -04002772 mHandler.post(new Runnable() {
2773 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002774 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2775 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002776 callbacks.bindAppsUpdated(modifiedFinal);
2777 }
2778 }
2779 });
2780 }
Winson Chung83892cc2013-05-01 16:53:33 -07002781
Winson Chungdf95eb12013-10-16 14:57:07 -07002782 final ArrayList<String> removedPackageNames =
2783 new ArrayList<String>();
2784 if (mOp == OP_REMOVE) {
2785 // Mark all packages in the broadcast to be removed
2786 removedPackageNames.addAll(Arrays.asList(packages));
2787 } else if (mOp == OP_UPDATE) {
2788 // Mark disabled packages in the broadcast to be removed
2789 final PackageManager pm = context.getPackageManager();
2790 for (int i=0; i<N; i++) {
2791 if (isPackageDisabled(pm, packages[i])) {
2792 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002793 }
2794 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002795 }
2796 // Remove all the components associated with this package
2797 for (String pn : removedPackageNames) {
2798 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2799 for (ItemInfo i : infos) {
2800 deleteItemFromDatabase(context, i);
2801 }
2802 }
2803 // Remove all the specific components
2804 for (AppInfo a : removedApps) {
2805 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2806 for (ItemInfo i : infos) {
2807 deleteItemFromDatabase(context, i);
2808 }
2809 }
2810 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2811 // Remove any queued items from the install queue
2812 String spKey = LauncherAppState.getSharedPreferencesKey();
2813 SharedPreferences sp =
2814 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2815 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2816 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002817 mHandler.post(new Runnable() {
2818 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002819 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2820 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002821 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002822 }
2823 }
2824 });
Joe Onoratobe386092009-11-17 17:32:16 -08002825 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002826
Michael Jurkac402cd92013-05-20 15:49:32 +02002827 final ArrayList<Object> widgetsAndShortcuts =
2828 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002829 mHandler.post(new Runnable() {
2830 @Override
2831 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002832 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2833 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002834 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002835 }
2836 }
2837 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002838
2839 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002840 mHandler.post(new Runnable() {
2841 public void run() {
2842 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2843 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002844 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002845 }
2846 }
2847 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 }
2849 }
2850
Michael Jurkac402cd92013-05-20 15:49:32 +02002851 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2852 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2853 PackageManager packageManager = context.getPackageManager();
2854 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2855 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2856 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2857 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2858 Collections.sort(widgetsAndShortcuts,
2859 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2860 return widgetsAndShortcuts;
2861 }
2862
Adam Cohen556f6132014-01-15 15:18:08 -08002863 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002864 try {
2865 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2866 return !pi.applicationInfo.enabled;
2867 } catch (NameNotFoundException e) {
2868 // Fall through
2869 }
2870 return false;
2871 }
Adam Cohen556f6132014-01-15 15:18:08 -08002872
2873 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002874 if (cn == null) {
2875 return false;
2876 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002877 if (isPackageDisabled(pm, cn.getPackageName())) {
2878 return false;
2879 }
Winson Chungee055712013-07-30 14:46:24 -07002880
2881 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002882 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002883 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002884 return (pm.getActivityInfo(cn, 0) != null);
2885 } catch (NameNotFoundException e) {
2886 return false;
2887 }
2888 }
2889
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002891 * Make an ShortcutInfo object for a restored application or shortcut item that points
2892 * to a package that is not yet installed on the system.
2893 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002894 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002895 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002896 if (cursor != null) {
2897 info.title = cursor.getString(titleIndex);
2898 } else {
2899 info.title = "";
2900 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002901 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002902 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002903 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002904 return info;
2905 }
2906
2907 /**
2908 * Make an Intent object for a restored application or shortcut item that points
2909 * to the market page for the item.
2910 */
2911 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002912 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002913 ComponentName componentName = intent.getComponent();
2914 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2915 Uri marketUri = new Uri.Builder()
2916 .scheme("market")
2917 .authority("details")
2918 .appendQueryParameter("id", componentName.getPackageName())
2919 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002920 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002921 marketIntent.setData(marketUri);
2922 return marketIntent;
2923 }
2924
2925 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002926 * This is called from the code that adds shortcuts from the intent receiver. This
2927 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 */
Joe Onorato56d82912010-03-07 14:32:10 -05002929 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002930 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932
Joe Onorato56d82912010-03-07 14:32:10 -05002933 /**
2934 * Make an ShortcutInfo object for a shortcut that is an application.
2935 *
2936 * If c is not null, then it will be used to fill in missing data like the title and icon.
2937 */
2938 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002939 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002940 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002941 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002942 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002943 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002944 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002945 } else {
2946 try {
2947 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2948 info.initFlagsAndFirstInstallTime(pi);
2949 } catch (NameNotFoundException e) {
2950 Log.d(TAG, "getPackInfo failed for package " +
2951 componentName.getPackageName());
2952 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002953 }
2954
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002955 // TODO: See if the PackageManager knows about this case. If it doesn't
2956 // then return null & delete this.
2957
Joe Onorato56d82912010-03-07 14:32:10 -05002958 // the resource -- This may implicitly give us back the fallback icon,
2959 // but don't worry about that. All we're doing with usingFallbackIcon is
2960 // to avoid saving lots of copies of that in the database, and most apps
2961 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002962
2963 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2964 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2965 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002966 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002967 ResolveInfo resolveInfo = null;
2968 ComponentName oldComponent = intent.getComponent();
2969 Intent newIntent = new Intent(intent.getAction(), null);
2970 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2971 newIntent.setPackage(oldComponent.getPackageName());
2972 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2973 for (ResolveInfo i : infos) {
2974 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2975 i.activityInfo.name);
2976 if (cn.equals(oldComponent)) {
2977 resolveInfo = i;
2978 }
2979 }
2980 if (resolveInfo == null) {
2981 resolveInfo = manager.resolveActivity(intent, 0);
2982 }
Joe Onorato56d82912010-03-07 14:32:10 -05002983 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002984 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002985 }
Joe Onorato56d82912010-03-07 14:32:10 -05002986 // the db
2987 if (icon == null) {
2988 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002989 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002990 }
2991 }
2992 // the fallback icon
2993 if (icon == null) {
2994 icon = getFallbackIcon();
2995 info.usingFallbackIcon = true;
2996 }
2997 info.setIcon(icon);
2998
2999 // from the resource
3000 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003001 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
3002 if (labelCache != null && labelCache.containsKey(key)) {
3003 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07003004 } else {
3005 info.title = resolveInfo.activityInfo.loadLabel(manager);
3006 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003007 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003008 }
3009 }
Joe Onorato56d82912010-03-07 14:32:10 -05003010 }
3011 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003013 if (c != null) {
3014 info.title = c.getString(titleIndex);
3015 }
3016 }
3017 // fall back to the class name of the activity
3018 if (info.title == null) {
3019 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003020 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3022 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003024
Winson Chung64359a52013-07-08 17:17:08 -07003025 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3026 ItemInfoFilter f) {
3027 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3028 for (ItemInfo i : infos) {
3029 if (i instanceof ShortcutInfo) {
3030 ShortcutInfo info = (ShortcutInfo) i;
3031 ComponentName cn = info.intent.getComponent();
3032 if (cn != null && f.filterItem(null, info, cn)) {
3033 filtered.add(info);
3034 }
3035 } else if (i instanceof FolderInfo) {
3036 FolderInfo info = (FolderInfo) i;
3037 for (ShortcutInfo s : info.contents) {
3038 ComponentName cn = s.intent.getComponent();
3039 if (cn != null && f.filterItem(info, s, cn)) {
3040 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003041 }
3042 }
Winson Chung64359a52013-07-08 17:17:08 -07003043 } else if (i instanceof LauncherAppWidgetInfo) {
3044 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3045 ComponentName cn = info.providerName;
3046 if (cn != null && f.filterItem(null, info, cn)) {
3047 filtered.add(info);
3048 }
Winson Chung8a435102012-08-30 17:16:53 -07003049 }
3050 }
Winson Chung64359a52013-07-08 17:17:08 -07003051 return new ArrayList<ItemInfo>(filtered);
3052 }
3053
3054 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003055 ItemInfoFilter filter = new ItemInfoFilter() {
3056 @Override
3057 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3058 return cn.getPackageName().equals(pn);
3059 }
3060 };
3061 return filterItemInfos(sBgItemsIdMap.values(), filter);
3062 }
3063
3064 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003065 ItemInfoFilter filter = new ItemInfoFilter() {
3066 @Override
3067 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3068 return cn.equals(cname);
3069 }
3070 };
3071 return filterItemInfos(sBgItemsIdMap.values(), filter);
3072 }
3073
3074 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3075 if (i instanceof ShortcutInfo) {
3076 ShortcutInfo info = (ShortcutInfo) i;
3077 // We need to check for ACTION_MAIN otherwise getComponent() might
3078 // return null for some shortcuts (for instance, for shortcuts to
3079 // web pages.)
3080 Intent intent = info.intent;
3081 ComponentName name = intent.getComponent();
3082 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3083 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3084 return true;
3085 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003086 // placeholder shortcuts get special treatment, let them through too.
3087 if (info.getRestoredIntent() != null) {
3088 return true;
3089 }
Winson Chung64359a52013-07-08 17:17:08 -07003090 }
3091 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003092 }
3093
3094 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003095 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003097 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003098 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3099 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100
Joe Onorato56d82912010-03-07 14:32:10 -05003101 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003102 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003105 // TODO: If there's an explicit component and we can't install that, delete it.
3106
Joe Onorato56d82912010-03-07 14:32:10 -05003107 info.title = c.getString(titleIndex);
3108
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 int iconType = c.getInt(iconTypeIndex);
3110 switch (iconType) {
3111 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3112 String packageName = c.getString(iconPackageIndex);
3113 String resourceName = c.getString(iconResourceIndex);
3114 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003115 info.customIcon = false;
3116 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003119 if (resources != null) {
3120 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003121 icon = Utilities.createIconBitmap(
3122 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003123 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003125 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 }
Joe Onorato56d82912010-03-07 14:32:10 -05003127 // the db
3128 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003129 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003130 }
3131 // the fallback icon
3132 if (icon == null) {
3133 icon = getFallbackIcon();
3134 info.usingFallbackIcon = true;
3135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 break;
3137 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003138 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003139 if (icon == null) {
3140 icon = getFallbackIcon();
3141 info.customIcon = false;
3142 info.usingFallbackIcon = true;
3143 } else {
3144 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 break;
3147 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003148 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003149 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 info.customIcon = false;
3151 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003153 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 return info;
3155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
Michael Jurka931dc972011-08-05 15:08:15 -07003157 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003158 @SuppressWarnings("all") // suppress dead code warning
3159 final boolean debug = false;
3160 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003161 Log.d(TAG, "getIconFromCursor app="
3162 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3163 }
3164 byte[] data = c.getBlob(iconIndex);
3165 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003166 return Utilities.createIconBitmap(
3167 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003168 } catch (Exception e) {
3169 return null;
3170 }
3171 }
3172
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3174 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003175 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003176 if (info == null) {
3177 return null;
3178 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003180
3181 return info;
3182 }
3183
Winson Chunga9abd0e2010-10-27 17:18:37 -07003184 /**
Winson Chung55cef262010-10-28 14:14:18 -07003185 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3186 */
3187 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3188 ComponentName component) {
3189 List<AppWidgetProviderInfo> widgets =
3190 AppWidgetManager.getInstance(context).getInstalledProviders();
3191 for (AppWidgetProviderInfo info : widgets) {
3192 if (info.provider.equals(component)) {
3193 return info;
3194 }
3195 }
3196 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003197 }
3198
Winson Chung68846fd2010-10-29 11:00:27 -07003199 /**
3200 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3201 */
3202 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3203 final PackageManager packageManager = context.getPackageManager();
3204 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3205 new ArrayList<WidgetMimeTypeHandlerData>();
3206
3207 final Intent supportsIntent =
3208 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3209 supportsIntent.setType(mimeType);
3210
3211 // Create a set of widget configuration components that we can test against
3212 final List<AppWidgetProviderInfo> widgets =
3213 AppWidgetManager.getInstance(context).getInstalledProviders();
3214 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3215 new HashMap<ComponentName, AppWidgetProviderInfo>();
3216 for (AppWidgetProviderInfo info : widgets) {
3217 configurationComponentToWidget.put(info.configure, info);
3218 }
3219
3220 // Run through each of the intents that can handle this type of clip data, and cross
3221 // reference them with the components that are actual configuration components
3222 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3223 PackageManager.MATCH_DEFAULT_ONLY);
3224 for (ResolveInfo info : activities) {
3225 final ActivityInfo activityInfo = info.activityInfo;
3226 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3227 activityInfo.name);
3228 if (configurationComponentToWidget.containsKey(infoComponent)) {
3229 supportedConfigurationActivities.add(
3230 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3231 configurationComponentToWidget.get(infoComponent)));
3232 }
3233 }
3234 return supportedConfigurationActivities;
3235 }
3236
Winson Chunga9abd0e2010-10-27 17:18:37 -07003237 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003238 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3239 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3240 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3241
Adam Cohend9198822011-11-22 16:42:47 -08003242 if (intent == null) {
3243 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3244 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3245 return null;
3246 }
3247
Joe Onorato0589f0f2010-02-08 13:44:00 -08003248 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003249 boolean customIcon = false;
3250 ShortcutIconResource iconResource = null;
3251
3252 if (bitmap != null && bitmap instanceof Bitmap) {
3253 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003254 customIcon = true;
3255 } else {
3256 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3257 if (extra != null && extra instanceof ShortcutIconResource) {
3258 try {
3259 iconResource = (ShortcutIconResource) extra;
3260 final PackageManager packageManager = context.getPackageManager();
3261 Resources resources = packageManager.getResourcesForApplication(
3262 iconResource.packageName);
3263 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003264 icon = Utilities.createIconBitmap(
3265 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003266 } catch (Exception e) {
3267 Log.w(TAG, "Could not load shortcut icon: " + extra);
3268 }
3269 }
3270 }
3271
Michael Jurkac9d95c52011-08-29 14:03:34 -07003272 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003273
3274 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003275 if (fallbackIcon != null) {
3276 icon = fallbackIcon;
3277 } else {
3278 icon = getFallbackIcon();
3279 info.usingFallbackIcon = true;
3280 }
Joe Onorato56d82912010-03-07 14:32:10 -05003281 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003282 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003283
Joe Onorato0589f0f2010-02-08 13:44:00 -08003284 info.title = name;
3285 info.intent = intent;
3286 info.customIcon = customIcon;
3287 info.iconResource = iconResource;
3288
3289 return info;
3290 }
3291
Winson Chungaac01e12011-08-17 10:37:13 -07003292 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3293 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003294 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003295 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003296 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003297 }
Joe Onorato56d82912010-03-07 14:32:10 -05003298 // If this icon doesn't have a custom icon, check to see
3299 // what's stored in the DB, and if it doesn't match what
3300 // we're going to show, store what we are going to show back
3301 // into the DB. We do this so when we're loading, if the
3302 // package manager can't find an icon (for example because
3303 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003304 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003305 cache.put(info, c.getBlob(iconIndex));
3306 return true;
3307 }
3308 return false;
3309 }
3310 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3311 boolean needSave = false;
3312 try {
3313 if (data != null) {
3314 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3315 Bitmap loaded = info.getIcon(mIconCache);
3316 needSave = !saved.sameAs(loaded);
3317 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003318 needSave = true;
3319 }
Winson Chungaac01e12011-08-17 10:37:13 -07003320 } catch (Exception e) {
3321 needSave = true;
3322 }
3323 if (needSave) {
3324 Log.d(TAG, "going to save icon bitmap for info=" + info);
3325 // This is slower than is ideal, but this only happens once
3326 // or when the app is updated with a new icon.
3327 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003328 }
3329 }
3330
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003332 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 * or make a new one.
3334 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003335 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 // See if a placeholder was created for us already
3337 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003338 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003340 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 folders.put(id, folderInfo);
3342 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003343 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003344 }
3345
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003346 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003347 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003348 return new Comparator<AppInfo>() {
3349 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003350 int result = collator.compare(a.title.toString().trim(),
3351 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003352 if (result == 0) {
3353 result = a.componentName.compareTo(b.componentName);
3354 }
3355 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003356 }
Winson Chung11904872012-09-17 16:58:46 -07003357 };
3358 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003359 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3360 = new Comparator<AppInfo>() {
3361 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003362 if (a.firstInstallTime < b.firstInstallTime) return 1;
3363 if (a.firstInstallTime > b.firstInstallTime) return -1;
3364 return 0;
3365 }
3366 };
Winson Chung11904872012-09-17 16:58:46 -07003367 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3368 final Collator collator = Collator.getInstance();
3369 return new Comparator<AppWidgetProviderInfo>() {
3370 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003371 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003372 }
3373 };
3374 }
Winson Chung5308f242011-08-18 12:12:41 -07003375 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3376 if (info.activityInfo != null) {
3377 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3378 } else {
3379 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3380 }
3381 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003382 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003383 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003384 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003385 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003386 ShortcutNameComparator(PackageManager pm) {
3387 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003388 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003389 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003390 }
3391 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3392 mPackageManager = pm;
3393 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003394 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003395 }
3396 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003397 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003398 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3399 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3400 if (mLabelCache.containsKey(keyA)) {
3401 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003402 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003403 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003404
Winson Chung5308f242011-08-18 12:12:41 -07003405 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003406 }
Winson Chung5308f242011-08-18 12:12:41 -07003407 if (mLabelCache.containsKey(keyB)) {
3408 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003409 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003410 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003411
Winson Chung5308f242011-08-18 12:12:41 -07003412 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003413 }
Winson Chung11904872012-09-17 16:58:46 -07003414 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003415 }
3416 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003417 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003418 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003419 private PackageManager mPackageManager;
3420 private HashMap<Object, String> mLabelCache;
3421 WidgetAndShortcutNameComparator(PackageManager pm) {
3422 mPackageManager = pm;
3423 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003424 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003425 }
3426 public final int compare(Object a, Object b) {
3427 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003428 if (mLabelCache.containsKey(a)) {
3429 labelA = mLabelCache.get(a);
3430 } else {
3431 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003432 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003433 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003434 mLabelCache.put(a, labelA);
3435 }
3436 if (mLabelCache.containsKey(b)) {
3437 labelB = mLabelCache.get(b);
3438 } else {
3439 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003440 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003441 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003442 mLabelCache.put(b, labelB);
3443 }
Winson Chung11904872012-09-17 16:58:46 -07003444 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003445 }
3446 };
Joe Onoratobe386092009-11-17 17:32:16 -08003447
3448 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003449 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003450 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3451 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3452 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3453 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003454 if (mLoaderTask != null) {
3455 mLoaderTask.dumpState();
3456 } else {
3457 Log.d(TAG, "mLoaderTask=null");
3458 }
Joe Onoratobe386092009-11-17 17:32:16 -08003459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003460}