blob: 07389c9a781c62d6437fde25328e2ee703827762 [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;
Jason Monkbbe1e242014-05-16 17:37:34 -040028import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070030import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.res.Resources;
32import android.database.Cursor;
33import android.graphics.Bitmap;
34import android.graphics.BitmapFactory;
35import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080036import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040037import android.os.Handler;
38import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070041import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040042import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040043import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080044import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070046import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010047
Kenny Guyed131872014-04-30 03:02:21 +010048import com.android.launcher3.compat.LauncherActivityInfoCompat;
49import com.android.launcher3.compat.LauncherAppsCompat;
50import com.android.launcher3.compat.UserHandleCompat;
51import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080053
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.lang.ref.WeakReference;
55import java.net.URISyntaxException;
56import java.text.Collator;
57import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070058import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070059import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.Collections;
61import java.util.Comparator;
62import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070063import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070064import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070065import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070066import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070067import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070068import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070069
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070/**
71 * Maintains in-memory state of the Launcher. It is expected that there should be only one
72 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070073 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 */
Kenny Guyed131872014-04-30 03:02:21 +010075public class LauncherModel extends BroadcastReceiver
Kenny Guy7bc272a2014-06-24 10:29:28 +010076 implements LauncherAppsCompat.OnAppsChangedListenerCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080077 static final boolean DEBUG_LOADERS = false;
Chris Wrenb358f812014-04-16 13:37:00 -040078 private static final boolean DEBUG_RECEIVER = true; // STOPSHIP(cwren) temporary for debugging
79
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070081
Daniel Sandler8707e0f2013-08-15 15:54:18 -070082 // true = use a "More Apps" folder for non-workspace apps on upgrade
83 // false = strew non-workspace apps across the workspace on upgrade
84 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
Dan Sandlerd5024042014-01-09 15:01:33 -050085 public static final int LOADER_FLAG_NONE = 0;
86 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
87 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
88
Joe Onorato36115782010-06-17 13:28:48 -040089 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050090 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080091
Winson Chungee055712013-07-30 14:46:24 -070092 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080093 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040094
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 private final Object mLock = new Object();
97 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040098 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070099 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +0200100 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Winson Chung81b52252012-08-27 15:34:29 -0700102 // Specific runnable types that are run on the main thread deferred handler, this allows us to
103 // clear all queued binding runnables when the Launcher activity is destroyed.
104 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
105 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
106
Jason Monkbbe1e242014-05-16 17:37:34 -0400107 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700108
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700109 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
110 static {
111 sWorkerThread.start();
112 }
113 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
114
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 // We start off with everything not loaded. After that, we assume that
116 // our monitoring of the package manager provides all updates and we never
117 // need to do a requery. These are only ever touched from the loader thread.
118 private boolean mWorkspaceLoaded;
119 private boolean mAllAppsLoaded;
120
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121 // When we are loading pages synchronously, we can't just post the binding of items on the side
122 // pages as this delays the rotation process. Instead, we wait for a callback from the first
123 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
124 // a normal load, we also clear this set of Runnables.
125 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
126
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700129 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700130 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800131
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700132 // The lock that must be acquired before referencing any static bg data structures. Unlike
133 // other locks, this one can generally be held long-term because we never expect any of these
134 // static data structures to be referenced outside of the worker thread except on the first
135 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700136 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700140 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141
Adam Cohen487f7dd2012-06-28 18:12:10 -0700142 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
143 // created by LauncherModel that are directly on the home screen (however, no widgets or
144 // shortcuts within folders).
145 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146
Adam Cohen487f7dd2012-06-28 18:12:10 -0700147 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
148 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 new ArrayList<LauncherAppWidgetInfo>();
150
Adam Cohen487f7dd2012-06-28 18:12:10 -0700151 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
152 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
155 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700156
157 // sBgWorkspaceScreens is the ordered set of workspace screens.
158 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
159
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 // </ only access in worker thread >
161
162 private IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Reena Lee99a73f32011-10-24 17:27:37 -0700164 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700165
Kenny Guyed131872014-04-30 03:02:21 +0100166 private final LauncherAppsCompat mLauncherApps;
167 private final UserManagerCompat mUserManager;
168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700170 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 public int getCurrentWorkspaceScreen();
172 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700173 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
174 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700175 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700176 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500177 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700178 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200180 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700181 public void bindAppsAdded(ArrayList<Long> newScreens,
182 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700183 ArrayList<ItemInfo> addAnimated,
184 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200185 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500186 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700187 public void bindComponentsRemoved(ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +0100188 ArrayList<AppInfo> appInfos, UserHandleCompat user);
Michael Jurkac402cd92013-05-20 15:49:32 +0200189 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100190 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700191 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700192 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700193 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Winson Chung64359a52013-07-08 17:17:08 -0700196 public interface ItemInfoFilter {
197 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
198 }
199
Bjorn Bringert1307f632013-10-03 22:31:03 +0100200 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800201 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400202
Winson Chungee055712013-07-30 14:46:24 -0700203 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700204 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400205 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
206 // resource string.
207 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
208 ProviderInfo providerInfo =
209 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
210 ProviderInfo redirectProvider =
211 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700212
213 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400214 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700215
216 if (mOldContentProviderExists) {
217 Log.d(TAG, "Old launcher provider exists.");
218 } else {
219 Log.d(TAG, "Old launcher provider does not exist.");
220 }
221
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100223 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800224 mIconCache = iconCache;
225
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400226 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700227 Configuration config = res.getConfiguration();
228 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100229 mLauncherApps = LauncherAppsCompat.getInstance(context);
230 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 }
232
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700233 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
234 * posted on the main thread handler. */
235 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700236 runOnMainThread(r, 0);
237 }
238 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700239 if (sWorkerThread.getThreadId() == Process.myTid()) {
240 // If we are on the worker thread, post onto the main handler
241 mHandler.post(r);
242 } else {
243 r.run();
244 }
245 }
246
247 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
248 * posted on the worker thread handler. */
249 private static void runOnWorkerThread(Runnable r) {
250 if (sWorkerThread.getThreadId() == Process.myTid()) {
251 r.run();
252 } else {
253 // If we are not on the worker thread, then post to the worker handler
254 sWorker.post(r);
255 }
256 }
257
Winson Chunge43a1e72014-01-15 10:33:02 -0800258 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
259 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800260 }
261
Winson Chungc9168342013-06-26 14:54:55 -0700262 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
263 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700264 LauncherAppState app = LauncherAppState.getInstance();
265 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
266 final int xCount = (int) grid.numColumns;
267 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700268 boolean[][] occupied = new boolean[xCount][yCount];
269
270 int cellX, cellY, spanX, spanY;
271 for (int i = 0; i < items.size(); ++i) {
272 final ItemInfo item = items.get(i);
273 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
274 if (item.screenId == screen) {
275 cellX = item.cellX;
276 cellY = item.cellY;
277 spanX = item.spanX;
278 spanY = item.spanY;
279 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
280 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
281 occupied[x][y] = true;
282 }
283 }
284 }
285 }
286 }
287
288 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
289 }
290 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700291 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700292 int firstScreenIndex,
293 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700294 // Lock on the app so that we don't try and get the items while apps are being added
295 LauncherAppState app = LauncherAppState.getInstance();
296 LauncherModel model = app.getModel();
297 boolean found = false;
298 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700299 if (sWorkerThread.getThreadId() != Process.myTid()) {
300 // Flush the LauncherModel worker thread, so that if we just did another
301 // processInstallShortcut, we give it time for its shortcut to get added to the
302 // database (getItemsInLocalCoordinates reads the database)
303 model.flushWorkerThread();
304 }
Winson Chungc9168342013-06-26 14:54:55 -0700305 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700306
307 // Try adding to the workspace screens incrementally, starting at the default or center
308 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700309 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
310 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700311 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700312 int[] tmpCoordinates = new int[2];
313 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700314 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700315 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700316 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700317 }
318 }
319 }
Winson Chungc9168342013-06-26 14:54:55 -0700320 return null;
321 }
322
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500323 public void setPackageState(final String pkgName, final int state) {
324 // Process the updated package state
325 Runnable r = new Runnable() {
326 public void run() {
327 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
328 if (callbacks != null) {
329 callbacks.updatePackageState(pkgName, state);
330 }
331 }
332 };
333 mHandler.post(r);
334 }
335
Adam Cohen76a47a12014-02-05 11:47:43 -0800336 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
337 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
338
339 if (allAppsApps == null) {
340 throw new RuntimeException("allAppsApps must not be null");
341 }
342 if (allAppsApps.isEmpty()) {
343 return;
344 }
345
Chris Wrenb6d4c282014-01-27 14:17:08 -0500346 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
347 Iterator<AppInfo> iter = allAppsApps.iterator();
348 while (iter.hasNext()) {
349 ItemInfo a = iter.next();
Kenny Guyed131872014-04-30 03:02:21 +0100350 if (LauncherModel.appWasRestored(ctx, a.getIntent(), a.user)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500351 restoredAppsFinal.add((AppInfo) a);
352 }
353 }
354
Adam Cohen76a47a12014-02-05 11:47:43 -0800355 // Process the newly added applications and add them to the database first
356 Runnable r = new Runnable() {
357 public void run() {
358 runOnMainThread(new Runnable() {
359 public void run() {
360 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
361 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500362 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500363 for (AppInfo info : restoredAppsFinal) {
364 final Intent intent = info.getIntent();
365 if (intent != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100366 mIconCache.deletePreloadedIcon(intent.getComponent(),
367 info.user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500368 }
369 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500370 callbacks.bindAppsUpdated(restoredAppsFinal);
371 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500372 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800373 }
374 }
375 });
376 }
377 };
378 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700379 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800380
381 public void addAndBindAddedWorkspaceApps(final Context context,
382 final ArrayList<ItemInfo> workspaceApps) {
383 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
384
385 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800386 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
387 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800388 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700389 return;
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 // Process the newly added applications and add them to the database first
392 Runnable r = new Runnable() {
393 public void run() {
394 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
395 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500396 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700397
Winson Chung76828c82013-08-19 15:43:29 -0700398 // Get the list of workspace screens. We need to append to this list and
399 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
400 // called.
401 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
402 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
403 for (Integer i : orderedScreens.keySet()) {
404 long screenId = orderedScreens.get(i);
405 workspaceScreens.add(screenId);
406 }
407
Winson Chung64359a52013-07-08 17:17:08 -0700408 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700409 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700410 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700411 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700412 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700413 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700414
415 // Short-circuit this logic if the icon exists somewhere on the workspace
416 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500417 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
418 if (a instanceof AppInfo &&
Kenny Guyed131872014-04-30 03:02:21 +0100419 LauncherModel.appWasRestored(context, launchIntent, a.user)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500420 restoredAppsFinal.add((AppInfo) a);
421 }
Winson Chung64359a52013-07-08 17:17:08 -0700422 continue;
423 }
424
Winson Chung87412982013-10-03 18:34:14 -0700425 // Add this icon to the db, creating a new page if necessary. If there
426 // is only the empty page then we just add items to the first page.
427 // Otherwise, we add them to the next pages.
428 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700429 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700430 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700431 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200432 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700433
434 // If we can't find a valid position, then just add a new screen.
435 // This takes time so we need to re-queue the add until the new
436 // page is added. Create as many screens as necessary to satisfy
437 // the startSearchPageIndex.
438 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700439 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700440 while (numPagesToAdd > 0) {
441 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700442 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700443 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700444 addedWorkspaceScreensFinal.add(screenId);
445 numPagesToAdd--;
446 }
Winson Chung76828c82013-08-19 15:43:29 -0700447
Winson Chung64359a52013-07-08 17:17:08 -0700448 // Find the coordinate again
449 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700450 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700451 }
452 if (coords == null) {
453 throw new RuntimeException("Coordinates should not be null");
454 }
455
Winson Chung997a9232013-07-24 15:33:46 -0700456 ShortcutInfo shortcutInfo;
457 if (a instanceof ShortcutInfo) {
458 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200459 } else if (a instanceof AppInfo) {
460 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700461 } else {
462 throw new RuntimeException("Unexpected info type");
463 }
Winson Chung94d67682013-09-25 16:29:40 -0700464
Winson Chung64359a52013-07-08 17:17:08 -0700465 // Add the shortcut to the db
466 addItemToDatabase(context, shortcutInfo,
467 LauncherSettings.Favorites.CONTAINER_DESKTOP,
468 coords.first, coords.second[0], coords.second[1], false);
469 // Save the ShortcutInfo for binding in the workspace
470 addedShortcutsFinal.add(shortcutInfo);
471 }
472 }
473
Winson Chung76828c82013-08-19 15:43:29 -0700474 // Update the workspace screens
475 updateWorkspaceScreenOrder(context, workspaceScreens);
476
Adam Cohen76a47a12014-02-05 11:47:43 -0800477 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700478 runOnMainThread(new Runnable() {
479 public void run() {
480 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
481 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700482 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
483 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700484 if (!addedShortcutsFinal.isEmpty()) {
485 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
486 long lastScreenId = info.screenId;
487 for (ItemInfo i : addedShortcutsFinal) {
488 if (i.screenId == lastScreenId) {
489 addAnimated.add(i);
490 } else {
491 addNotAnimated.add(i);
492 }
Winson Chung997a9232013-07-24 15:33:46 -0700493 }
494 }
Winson Chungd64d1762013-08-20 14:37:16 -0700495 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800496 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500497 if (!restoredAppsFinal.isEmpty()) {
498 callbacks.bindAppsUpdated(restoredAppsFinal);
499 }
Winson Chung997a9232013-07-24 15:33:46 -0700500 }
Winson Chung64359a52013-07-08 17:17:08 -0700501 }
Winson Chung997a9232013-07-24 15:33:46 -0700502 });
503 }
Winson Chung64359a52013-07-08 17:17:08 -0700504 }
505 };
506 runOnWorkerThread(r);
507 }
508
Winson Chung81b52252012-08-27 15:34:29 -0700509 public void unbindItemInfosAndClearQueuedBindRunnables() {
510 if (sWorkerThread.getThreadId() == Process.myTid()) {
511 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
512 "main thread");
513 }
514
515 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400516 synchronized (mDeferredBindRunnables) {
517 mDeferredBindRunnables.clear();
518 }
Winson Chung81b52252012-08-27 15:34:29 -0700519 // Remove any queued bind runnables
520 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
521 // Unbind all the workspace items
522 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700523 }
524
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700525 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700526 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700527 // Ensure that we don't use the same workspace items data structure on the main thread
528 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700529 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
530 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700531 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700532 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
533 tmpAppWidgets.addAll(sBgAppWidgets);
534 }
535 Runnable r = new Runnable() {
536 @Override
537 public void run() {
538 for (ItemInfo item : tmpWorkspaceItems) {
539 item.unbind();
540 }
541 for (ItemInfo item : tmpAppWidgets) {
542 item.unbind();
543 }
544 }
545 };
546 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700547 }
548
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 /**
550 * Adds an item to the DB if it was not created previously, or move it to a new
551 * <container, screen, cellX, cellY>
552 */
553 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700554 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 if (item.container == ItemInfo.NO_ID) {
556 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700557 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 } else {
559 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700560 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 }
562 }
563
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700564 static void checkItemInfoLocked(
565 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
566 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
567 if (modelItem != null && item != modelItem) {
568 // check all the data is consistent
569 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
570 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
571 ShortcutInfo shortcut = (ShortcutInfo) item;
572 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
573 modelShortcut.intent.filterEquals(shortcut.intent) &&
574 modelShortcut.id == shortcut.id &&
575 modelShortcut.itemType == shortcut.itemType &&
576 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700577 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700578 modelShortcut.cellX == shortcut.cellX &&
579 modelShortcut.cellY == shortcut.cellY &&
580 modelShortcut.spanX == shortcut.spanX &&
581 modelShortcut.spanY == shortcut.spanY &&
582 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
583 (modelShortcut.dropPos != null &&
584 shortcut.dropPos != null &&
585 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
586 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
587 // For all intents and purposes, this is the same object
588 return;
589 }
590 }
591
592 // the modelItem needs to match up perfectly with item if our model is
593 // to be consistent with the database-- for now, just require
594 // modelItem == item or the equality check above
595 String msg = "item: " + ((item != null) ? item.toString() : "null") +
596 "modelItem: " +
597 ((modelItem != null) ? modelItem.toString() : "null") +
598 "Error: ItemInfo passed to checkItemInfo doesn't match original";
599 RuntimeException e = new RuntimeException(msg);
600 if (stackTrace != null) {
601 e.setStackTrace(stackTrace);
602 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800603 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700604 }
605 }
606
Michael Jurka816474f2012-06-25 14:49:02 -0700607 static void checkItemInfo(final ItemInfo item) {
608 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
609 final long itemId = item.id;
610 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700611 public void run() {
612 synchronized (sBgLock) {
613 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700614 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700615 }
616 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700617 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700618 }
619
Michael Jurkac9d95c52011-08-29 14:03:34 -0700620 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
621 final ItemInfo item, final String callingFunction) {
622 final long itemId = item.id;
623 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
624 final ContentResolver cr = context.getContentResolver();
625
Adam Cohen487f7dd2012-06-28 18:12:10 -0700626 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700627 Runnable r = new Runnable() {
628 public void run() {
629 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700630 updateItemArrays(item, itemId, stackTrace);
631 }
632 };
633 runOnWorkerThread(r);
634 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700636 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
637 final ArrayList<ItemInfo> items, final String callingFunction) {
638 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700639
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700640 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
641 Runnable r = new Runnable() {
642 public void run() {
643 ArrayList<ContentProviderOperation> ops =
644 new ArrayList<ContentProviderOperation>();
645 int count = items.size();
646 for (int i = 0; i < count; i++) {
647 ItemInfo item = items.get(i);
648 final long itemId = item.id;
649 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
650 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700651
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700652 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
653 updateItemArrays(item, itemId, stackTrace);
654
655 }
656 try {
657 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
658 } catch (Exception e) {
659 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700660 }
661 }
662 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700663 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700664 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700665
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
667 // Lock on mBgLock *after* the db operation
668 synchronized (sBgLock) {
669 checkItemInfoLocked(itemId, item, stackTrace);
670
671 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
672 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
673 // Item is in a folder, make sure this folder exists
674 if (!sBgFolders.containsKey(item.container)) {
675 // An items container is being set to a that of an item which is not in
676 // the list of Folders.
677 String msg = "item: " + item + " container being set to: " +
678 item.container + ", not in the list of folders";
679 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 }
681 }
682
683 // Items are added/removed from the corresponding FolderInfo elsewhere, such
684 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
685 // that are on the desktop, as appropriate
686 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800687 if (modelItem != null &&
688 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
689 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 switch (modelItem.itemType) {
691 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
692 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
693 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
694 if (!sBgWorkspaceItems.contains(modelItem)) {
695 sBgWorkspaceItems.add(modelItem);
696 }
697 break;
698 default:
699 break;
700 }
701 } else {
702 sBgWorkspaceItems.remove(modelItem);
703 }
704 }
705 }
706
Michael Jurkac7700af2013-05-14 20:17:58 +0200707 public void flushWorkerThread() {
708 mFlushingWorkerThread = true;
709 Runnable waiter = new Runnable() {
710 public void run() {
711 synchronized (this) {
712 notifyAll();
713 mFlushingWorkerThread = false;
714 }
715 }
716 };
717
718 synchronized(waiter) {
719 runOnWorkerThread(waiter);
720 if (mLoaderTask != null) {
721 synchronized(mLoaderTask) {
722 mLoaderTask.notify();
723 }
724 }
725 boolean success = false;
726 while (!success) {
727 try {
728 waiter.wait();
729 success = true;
730 } catch (InterruptedException e) {
731 }
732 }
733 }
734 }
735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700738 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700739 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 item.cellX = cellX;
743 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700744
Winson Chung3d503fb2011-07-13 17:25:49 -0700745 // We store hotseat items in canonical form which is this orientation invariant position
746 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700747 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700748 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700750 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700751 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753
754 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
757 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700758 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759
Michael Jurkac9d95c52011-08-29 14:03:34 -0700760 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700761 }
762
763 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700764 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
765 * cellX, cellY have already been updated on the ItemInfos.
766 */
767 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
768 final long container, final int screen) {
769
770 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
771 int count = items.size();
772
773 for (int i = 0; i < count; i++) {
774 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775 item.container = container;
776
777 // We store hotseat items in canonical form which is this orientation invariant position
778 // in the hotseat
779 if (context instanceof Launcher && screen < 0 &&
780 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700782 item.cellY);
783 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700785 }
786
787 final ContentValues values = new ContentValues();
788 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
789 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
790 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700791 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700792
793 contentValues.add(values);
794 }
795 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
796 }
797
798 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800800 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700802 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700803 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800804 item.cellX = cellX;
805 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700806 item.spanX = spanX;
807 item.spanY = spanY;
808
809 // We store hotseat items in canonical form which is this orientation invariant position
810 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700812 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700813 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700814 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700815 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700816 }
Adam Cohend4844c32011-02-18 19:25:06 -0800817
Adam Cohend4844c32011-02-18 19:25:06 -0800818 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800819 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700820 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
821 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
822 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
823 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800825
Michael Jurka816474f2012-06-25 14:49:02 -0700826 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700827 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700828
829 /**
830 * Update an item to the database in a specified container.
831 */
832 static void updateItemInDatabase(Context context, final ItemInfo item) {
833 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100834 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700835 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
836 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800837 }
838
839 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 * Returns true if the shortcuts already exists in the database.
841 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 static boolean shortcutExists(Context context, String title, Intent intent) {
844 final ContentResolver cr = context.getContentResolver();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700845 final Intent intentWithPkg, intentWithoutPkg;
846
847 if (intent.getComponent() != null) {
848 // If component is not null, an intent with null package will produce
849 // the same result and should also be a match.
850 if (intent.getPackage() != null) {
851 intentWithPkg = intent;
852 intentWithoutPkg = new Intent(intent).setPackage(null);
853 } else {
854 intentWithPkg = new Intent(intent).setPackage(
855 intent.getComponent().getPackageName());
856 intentWithoutPkg = intent;
857 }
858 } else {
859 intentWithPkg = intent;
860 intentWithoutPkg = intent;
861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700863 new String[] { "title", "intent" }, "title=? and (intent=? or intent=?)",
864 new String[] { title, intentWithPkg.toUri(0), intentWithoutPkg.toUri(0) }, null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 boolean result = false;
866 try {
867 result = c.moveToFirst();
868 } finally {
869 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700872 }
873
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500875 * Returns true if the shortcuts already exists in the database.
Kenny Guyed131872014-04-30 03:02:21 +0100876 * we identify a shortcut by the component name of the intent
877 * and the user.
Chris Wrenb6d4c282014-01-27 14:17:08 -0500878 */
Kenny Guyed131872014-04-30 03:02:21 +0100879 static boolean appWasRestored(Context context, Intent intent, UserHandleCompat user) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500880 final ContentResolver cr = context.getContentResolver();
881 final ComponentName component = intent.getComponent();
882 if (component == null) {
883 return false;
884 }
885 String componentName = component.flattenToString();
Chris Wren0e584b52014-05-12 16:07:25 -0400886 String shortName = component.flattenToShortString();
Kenny Guyed131872014-04-30 03:02:21 +0100887 long serialNumber = UserManagerCompat.getInstance(context)
888 .getSerialNumberForUser(user);
Chris Wren0e584b52014-05-12 16:07:25 -0400889 final String where = "(intent glob \"*component=" + componentName + "*\" or " +
890 "intent glob \"*component=" + shortName + "*\")" +
891 "and restored = 1 and profileId = " + serialNumber;
Chris Wrenb6d4c282014-01-27 14:17:08 -0500892 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Kenny Guyed131872014-04-30 03:02:21 +0100893 new String[]{"intent", "restored", "profileId"}, where, null, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500894 boolean result = false;
895 try {
896 result = c.moveToFirst();
897 } finally {
898 c.close();
899 }
900 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
901 return result;
902 }
903
904 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700905 * Returns an ItemInfo array containing all the items in the LauncherModel.
906 * The ItemInfo.id is not set through this function.
907 */
908 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
909 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
910 final ContentResolver cr = context.getContentResolver();
911 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
912 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
Kenny Guyed131872014-04-30 03:02:21 +0100913 LauncherSettings.Favorites.SCREEN,
914 LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
915 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY,
916 LauncherSettings.Favorites.PROFILE_ID }, null, null, null);
Winson Chungaafa03c2010-06-11 17:34:16 -0700917
918 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
919 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
920 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
921 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
922 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
923 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
924 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
Kenny Guyed131872014-04-30 03:02:21 +0100925 final int profileIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
926 UserManagerCompat userManager = UserManagerCompat.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700927 try {
928 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700929 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700930 item.cellX = c.getInt(cellXIndex);
931 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700932 item.spanX = Math.max(1, c.getInt(spanXIndex));
933 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700934 item.container = c.getInt(containerIndex);
935 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 item.screenId = c.getInt(screenIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100937 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +0100938 item.user = userManager.getUserForSerialNumber(serialNumber);
939 // Skip if user has been deleted.
940 if (item.user != null) {
941 items.add(item);
942 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700943 }
944 } catch (Exception e) {
945 items.clear();
946 } finally {
947 c.close();
948 }
949
950 return items;
951 }
952
953 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
955 */
956 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
957 final ContentResolver cr = context.getContentResolver();
958 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
959 "_id=? and (itemType=? or itemType=?)",
960 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700961 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700962
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 try {
964 if (c.moveToFirst()) {
965 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
966 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
967 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
968 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
969 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
970 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 FolderInfo folderInfo = null;
973 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700974 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
975 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700977 }
978
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 folderInfo.title = c.getString(titleIndex);
980 folderInfo.id = id;
981 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700983 folderInfo.cellX = c.getInt(cellXIndex);
984 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985
986 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 } finally {
989 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700990 }
991
992 return null;
993 }
994
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 /**
996 * Add an item to the database in a specified container. Sets the container, screen, cellX and
997 * cellY fields of the item. Also assigns an ID to the item.
998 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 item.cellX = cellX;
1003 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 // We store hotseat items in canonical form which is this orientation invariant position
1005 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012
1013 final ContentValues values = new ContentValues();
1014 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001015 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016
Michael Jurka414300a2013-08-27 15:42:35 +02001017 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001018 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -07001020
Jason Monk8e19cf22014-03-20 15:06:57 -04001021 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001023 public void run() {
1024 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
1025 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001028 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001029 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001030 sBgItemsIdMap.put(item.id, item);
1031 switch (item.itemType) {
1032 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1033 sBgFolders.put(item.id, (FolderInfo) item);
1034 // Fall through
1035 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1036 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1037 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1038 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1039 sBgWorkspaceItems.add(item);
1040 } else {
1041 if (!sBgFolders.containsKey(item.container)) {
1042 // Adding an item to a folder that doesn't exist.
1043 String msg = "adding item: " + item + " to a folder that " +
1044 " doesn't exist";
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 break;
1049 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1050 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1051 break;
1052 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001053 }
1054 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001055 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001056 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001057 }
1058
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001060 * Creates a new unique child id, for a given cell span across all layouts.
1061 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001062 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001063 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001064 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001066 }
1067
Winson Chungaafa03c2010-06-11 17:34:16 -07001068 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001069 * Removes the specified item from the database
1070 * @param context
1071 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001073 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001075 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001076
Michael Jurka83df1882011-08-31 20:59:26 -07001077 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001078 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001079 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001080
1081 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001082 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001083 switch (item.itemType) {
1084 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1085 sBgFolders.remove(item.id);
1086 for (ItemInfo info: sBgItemsIdMap.values()) {
1087 if (info.container == item.id) {
1088 // We are deleting a folder which still contains items that
1089 // think they are contained by that folder.
1090 String msg = "deleting a folder (" + item + ") which still " +
1091 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001092 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001093 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001094 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001095 sBgWorkspaceItems.remove(item);
1096 break;
1097 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1098 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1099 sBgWorkspaceItems.remove(item);
1100 break;
1101 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1102 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1103 break;
1104 }
1105 sBgItemsIdMap.remove(item.id);
1106 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001107 }
1108 }
Michael Jurka83df1882011-08-31 20:59:26 -07001109 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001110 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 }
1112
1113 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001114 * Update the order of the workspace screens in the database. The array list contains
1115 * a list of screen ids in the order that they should appear.
1116 */
Winson Chungc9168342013-06-26 14:54:55 -07001117 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001118 // Log to disk
1119 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1120 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1121
Winson Chung64359a52013-07-08 17:17:08 -07001122 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001123 final ContentResolver cr = context.getContentResolver();
1124 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1125
1126 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001127 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 while (iter.hasNext()) {
1129 long id = iter.next();
1130 if (id < 0) {
1131 iter.remove();
1132 }
1133 }
1134
1135 Runnable r = new Runnable() {
1136 @Override
1137 public void run() {
Yura085c8532014-02-11 15:15:29 +00001138 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001139 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001140 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001141 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001142 for (int i = 0; i < count; i++) {
1143 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001144 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001145 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1146 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001147 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 }
Yura085c8532014-02-11 15:15:29 +00001149
1150 try {
1151 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1152 } catch (Exception ex) {
1153 throw new RuntimeException(ex);
1154 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001155
Winson Chungba9c37f2013-08-30 14:11:37 -07001156 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001157 sBgWorkspaceScreens.clear();
1158 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001159 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001160 }
1161 };
1162 runOnWorkerThread(r);
1163 }
1164
1165 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 * Remove the contents of the specified folder from the database
1167 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001168 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001169 final ContentResolver cr = context.getContentResolver();
1170
Michael Jurkac9d95c52011-08-29 14:03:34 -07001171 Runnable r = new Runnable() {
1172 public void run() {
1173 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001175 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 sBgItemsIdMap.remove(info.id);
1177 sBgFolders.remove(info.id);
1178 sBgDbIconCache.remove(info);
1179 sBgWorkspaceItems.remove(info);
1180 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001181
Michael Jurkac9d95c52011-08-29 14:03:34 -07001182 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1183 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001185 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 for (ItemInfo childInfo : info.contents) {
1187 sBgItemsIdMap.remove(childInfo.id);
1188 sBgDbIconCache.remove(childInfo);
1189 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001190 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001191 }
1192 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001193 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 }
1195
1196 /**
1197 * Set this as the current Launcher activity object for the loader.
1198 */
1199 public void initialize(Callbacks callbacks) {
1200 synchronized (mLock) {
1201 mCallbacks = new WeakReference<Callbacks>(callbacks);
1202 }
1203 }
1204
Kenny Guyed131872014-04-30 03:02:21 +01001205 @Override
Kenny Guy7bc272a2014-06-24 10:29:28 +01001206 public void onPackageChanged(UserHandleCompat user, String packageName) {
Kenny Guyed131872014-04-30 03:02:21 +01001207 int op = PackageUpdatedTask.OP_UPDATE;
1208 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1209 user));
1210 }
1211
1212 @Override
Kenny Guy7bc272a2014-06-24 10:29:28 +01001213 public void onPackageRemoved(UserHandleCompat user, String packageName) {
Kenny Guyed131872014-04-30 03:02:21 +01001214 int op = PackageUpdatedTask.OP_REMOVE;
1215 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1216 user));
1217 }
1218
1219 @Override
Kenny Guy7bc272a2014-06-24 10:29:28 +01001220 public void onPackageAdded(UserHandleCompat user, String packageName) {
Kenny Guyed131872014-04-30 03:02:21 +01001221 int op = PackageUpdatedTask.OP_ADD;
1222 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1223 user));
1224 }
1225
1226 @Override
Kenny Guy7bc272a2014-06-24 10:29:28 +01001227 public void onPackagesAvailable(UserHandleCompat user, String[] packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01001228 boolean replacing) {
1229 if (!replacing) {
1230 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1231 user));
1232 if (mAppsCanBeOnRemoveableStorage) {
1233 // Only rebind if we support removable storage. It catches the
1234 // case where
1235 // apps on the external sd card need to be reloaded
1236 startLoaderFromBackground();
1237 }
1238 } else {
1239 // If we are replacing then just update the packages in the list
1240 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1241 packageNames, user));
1242 }
1243 }
1244
1245 @Override
Kenny Guy7bc272a2014-06-24 10:29:28 +01001246 public void onPackagesUnavailable(UserHandleCompat user, String[] packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01001247 boolean replacing) {
1248 if (!replacing) {
1249 enqueuePackageUpdated(new PackageUpdatedTask(
1250 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1251 user));
1252 }
1253
1254 }
1255
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001256 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1258 * ACTION_PACKAGE_CHANGED.
1259 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001260 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001261 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001262 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001263
Joe Onorato36115782010-06-17 13:28:48 -04001264 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001265 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001266 // If we have changed locale we need to clear out the labels in all apps/workspace.
1267 forceReload();
1268 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1269 // Check if configuration change was an mcc/mnc change which would affect app resources
1270 // and we would need to clear out the labels in all apps/workspace. Same handling as
1271 // above for ACTION_LOCALE_CHANGED
1272 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001273 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001274 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001275 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001276 forceReload();
1277 }
1278 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001279 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001280 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1281 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001282 if (mCallbacks != null) {
1283 Callbacks callbacks = mCallbacks.get();
1284 if (callbacks != null) {
1285 callbacks.bindSearchablesChanged();
1286 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001287 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001288 }
1289 }
1290
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001291 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001292 resetLoadedState(true, true);
1293
Reena Lee93f824a2011-09-23 17:20:28 -07001294 // Do this here because if the launcher activity is running it will be restarted.
1295 // If it's not running startLoaderFromBackground will merely tell it that it needs
1296 // to reload.
1297 startLoaderFromBackground();
1298 }
1299
Winson Chungf0c6ae02012-03-21 16:10:31 -07001300 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1301 synchronized (mLock) {
1302 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1303 // mWorkspaceLoaded to true later
1304 stopLoaderLocked();
1305 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1306 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1307 }
1308 }
1309
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001310 /**
1311 * When the launcher is in the background, it's possible for it to miss paired
1312 * configuration changes. So whenever we trigger the loader from the background
1313 * tell the launcher that it needs to re-run the loader when it comes back instead
1314 * of doing it now.
1315 */
1316 public void startLoaderFromBackground() {
1317 boolean runLoader = false;
1318 if (mCallbacks != null) {
1319 Callbacks callbacks = mCallbacks.get();
1320 if (callbacks != null) {
1321 // Only actually run the loader if they're not paused.
1322 if (!callbacks.setLoadOnResume()) {
1323 runLoader = true;
1324 }
1325 }
1326 }
1327 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001328 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001329 }
Joe Onorato36115782010-06-17 13:28:48 -04001330 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001331
Reena Lee93f824a2011-09-23 17:20:28 -07001332 // If there is already a loader task running, tell it to stop.
1333 // returns true if isLaunching() was true on the old task
1334 private boolean stopLoaderLocked() {
1335 boolean isLaunching = false;
1336 LoaderTask oldTask = mLoaderTask;
1337 if (oldTask != null) {
1338 if (oldTask.isLaunching()) {
1339 isLaunching = true;
1340 }
1341 oldTask.stopLocked();
1342 }
1343 return isLaunching;
1344 }
1345
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001346 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001347 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1348 }
1349
1350 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001351 synchronized (mLock) {
1352 if (DEBUG_LOADERS) {
1353 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1354 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001356 // Clear any deferred bind-runnables from the synchronized load process
1357 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001358 synchronized (mDeferredBindRunnables) {
1359 mDeferredBindRunnables.clear();
1360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361
Joe Onorato36115782010-06-17 13:28:48 -04001362 // Don't bother to start the thread if we know it's not going to do anything
1363 if (mCallbacks != null && mCallbacks.get() != null) {
1364 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001365 // also, don't downgrade isLaunching if we're already running
1366 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001367 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001368 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1369 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1371 } else {
1372 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1373 sWorker.post(mLoaderTask);
1374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 }
1377 }
1378
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 void bindRemainingSynchronousPages() {
1380 // Post the remaining side pages to be loaded
1381 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001382 Runnable[] deferredBindRunnables = null;
1383 synchronized (mDeferredBindRunnables) {
1384 deferredBindRunnables = mDeferredBindRunnables.toArray(
1385 new Runnable[mDeferredBindRunnables.size()]);
1386 mDeferredBindRunnables.clear();
1387 }
1388 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001389 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001390 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001391 }
1392 }
1393
Joe Onorato36115782010-06-17 13:28:48 -04001394 public void stopLoader() {
1395 synchronized (mLock) {
1396 if (mLoaderTask != null) {
1397 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 }
1399 }
Joe Onorato36115782010-06-17 13:28:48 -04001400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401
Winson Chung76828c82013-08-19 15:43:29 -07001402 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1403 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1404 final ContentResolver contentResolver = context.getContentResolver();
1405 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1406 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1407 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1408
1409 try {
1410 final int idIndex = sc.getColumnIndexOrThrow(
1411 LauncherSettings.WorkspaceScreens._ID);
1412 final int rankIndex = sc.getColumnIndexOrThrow(
1413 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1414 while (sc.moveToNext()) {
1415 try {
1416 long screenId = sc.getLong(idIndex);
1417 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001418 orderedScreens.put(rank, screenId);
1419 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001420 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001421 }
1422 }
1423 } finally {
1424 sc.close();
1425 }
Winson Chunga90303b2013-11-15 13:05:06 -08001426
1427 // Log to disk
1428 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1429 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1430 for (Integer i : orderedScreens.keySet()) {
1431 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1432 }
1433 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1434 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001435 return orderedScreens;
1436 }
1437
Michael Jurkac57b7a82011-08-09 22:02:20 -07001438 public boolean isAllAppsLoaded() {
1439 return mAllAppsLoaded;
1440 }
1441
Winson Chung36a62fe2012-05-06 18:04:42 -07001442 boolean isLoadingWorkspace() {
1443 synchronized (mLock) {
1444 if (mLoaderTask != null) {
1445 return mLoaderTask.isLoadingWorkspace();
1446 }
1447 }
1448 return false;
1449 }
1450
Joe Onorato36115782010-06-17 13:28:48 -04001451 /**
1452 * Runnable for the thread that loads the contents of the launcher:
1453 * - workspace icons
1454 * - widgets
1455 * - all apps icons
1456 */
1457 private class LoaderTask implements Runnable {
1458 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001459 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001460 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001461 private boolean mStopped;
1462 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001463 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001464
Winson Chungc3eecff2011-07-11 17:44:15 -07001465 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001466
Dan Sandlerd5024042014-01-09 15:01:33 -05001467 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001468 mContext = context;
1469 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001470 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001471 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 }
1473
Joe Onorato36115782010-06-17 13:28:48 -04001474 boolean isLaunching() {
1475 return mIsLaunching;
1476 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001477
Winson Chung36a62fe2012-05-06 18:04:42 -07001478 boolean isLoadingWorkspace() {
1479 return mIsLoadingAndBindingWorkspace;
1480 }
1481
Winson Chungc763c4e2013-07-19 13:49:06 -07001482 /** Returns whether this is an upgrade path */
1483 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001484 mIsLoadingAndBindingWorkspace = true;
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001487 if (DEBUG_LOADERS) {
1488 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001490
Winson Chungc763c4e2013-07-19 13:49:06 -07001491 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001492 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001493 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001494 synchronized (LoaderTask.this) {
1495 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001496 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001497 }
1498 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 }
1500 }
1501
Joe Onorato36115782010-06-17 13:28:48 -04001502 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001503 bindWorkspace(-1, isUpgradePath);
1504 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001505 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001506
Joe Onorato36115782010-06-17 13:28:48 -04001507 private void waitForIdle() {
1508 // Wait until the either we're stopped or the other threads are done.
1509 // This way we don't start loading all apps until the workspace has settled
1510 // down.
1511 synchronized (LoaderTask.this) {
1512 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001513
Joe Onorato36115782010-06-17 13:28:48 -04001514 mHandler.postIdle(new Runnable() {
1515 public void run() {
1516 synchronized (LoaderTask.this) {
1517 mLoadAndBindStepFinished = true;
1518 if (DEBUG_LOADERS) {
1519 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001520 }
Joe Onorato36115782010-06-17 13:28:48 -04001521 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001522 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001523 }
Joe Onorato36115782010-06-17 13:28:48 -04001524 });
1525
Michael Jurkac7700af2013-05-14 20:17:58 +02001526 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001527 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001528 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1529 // wait no longer than 1sec at a time
1530 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001531 } catch (InterruptedException ex) {
1532 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001533 }
1534 }
Joe Onorato36115782010-06-17 13:28:48 -04001535 if (DEBUG_LOADERS) {
1536 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001537 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001538 + "ms for previous step to finish binding");
1539 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001540 }
Joe Onorato36115782010-06-17 13:28:48 -04001541 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001542
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001543 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001544 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001545 // Ensure that we have a valid page index to load synchronously
1546 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1547 "valid page index");
1548 }
1549 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1550 // Ensure that we don't try and bind a specified page when the pages have not been
1551 // loaded already (we should load everything asynchronously in that case)
1552 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1553 }
1554 synchronized (mLock) {
1555 if (mIsLoaderTaskRunning) {
1556 // Ensure that we are never running the background loading at this point since
1557 // we also touch the background collections
1558 throw new RuntimeException("Error! Background loading is already running");
1559 }
1560 }
1561
1562 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1563 // data structures, we can't allow any other thread to touch that data, but because
1564 // this call is synchronous, we can get away with not locking).
1565
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001566 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001567 // operations from the previous activity. We need to ensure that all queued operations
1568 // are executed before any synchronous binding work is done.
1569 mHandler.flush();
1570
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001571 // Divide the set of loaded items into those that we are binding synchronously, and
1572 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001573 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001574 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1575 // arise from that.
1576 onlyBindAllApps();
1577 }
1578
Joe Onorato36115782010-06-17 13:28:48 -04001579 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001580 boolean isUpgrade = false;
1581
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001582 synchronized (mLock) {
1583 mIsLoaderTaskRunning = true;
1584 }
Joe Onorato36115782010-06-17 13:28:48 -04001585 // Optimize for end-user experience: if the Launcher is up and // running with the
1586 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1587 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001588 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001589 // Elevate priority when Home launches for the first time to avoid
1590 // starving at boot time. Staring at a blank home is not cool.
1591 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001592 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1593 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001594 android.os.Process.setThreadPriority(mIsLaunching
1595 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1596 }
Winson Chung64359a52013-07-08 17:17:08 -07001597 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001598 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001599
Joe Onorato36115782010-06-17 13:28:48 -04001600 if (mStopped) {
1601 break keep_running;
1602 }
1603
1604 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1605 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001606 synchronized (mLock) {
1607 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001608 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001609 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1610 }
1611 }
Joe Onorato36115782010-06-17 13:28:48 -04001612 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001613
1614 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001615 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1616 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001617
1618 // Restore the default thread priority after we are done loading items
1619 synchronized (mLock) {
1620 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623
Winson Chungaac01e12011-08-17 10:37:13 -07001624 // Update the saved icons if necessary
1625 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001626 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001627 for (Object key : sBgDbIconCache.keySet()) {
1628 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1629 }
1630 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001631 }
Winson Chungaac01e12011-08-17 10:37:13 -07001632
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001633 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001634 // Ensure that all the applications that are in the system are
1635 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001636 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001637 verifyApplications();
1638 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001639 }
1640
Joe Onorato36115782010-06-17 13:28:48 -04001641 // Clear out this reference, otherwise we end up holding it until all of the
1642 // callback runnables are done.
1643 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644
Joe Onorato36115782010-06-17 13:28:48 -04001645 synchronized (mLock) {
1646 // If we are still the last one to be scheduled, remove ourselves.
1647 if (mLoaderTask == this) {
1648 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001651 }
Joe Onorato36115782010-06-17 13:28:48 -04001652 }
1653
1654 public void stopLocked() {
1655 synchronized (LoaderTask.this) {
1656 mStopped = true;
1657 this.notify();
1658 }
1659 }
1660
1661 /**
1662 * Gets the callbacks object. If we've been stopped, or if the launcher object
1663 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1664 * object that was around when the deferred message was scheduled, and if there's
1665 * a new Callbacks object around then also return null. This will save us from
1666 * calling onto it with data that will be ignored.
1667 */
1668 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1669 synchronized (mLock) {
1670 if (mStopped) {
1671 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001672 }
Joe Onorato36115782010-06-17 13:28:48 -04001673
1674 if (mCallbacks == null) {
1675 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001676 }
Joe Onorato36115782010-06-17 13:28:48 -04001677
1678 final Callbacks callbacks = mCallbacks.get();
1679 if (callbacks != oldCallbacks) {
1680 return null;
1681 }
1682 if (callbacks == null) {
1683 Log.w(TAG, "no mCallbacks");
1684 return null;
1685 }
1686
1687 return callbacks;
1688 }
1689 }
1690
Winson Chungc763c4e2013-07-19 13:49:06 -07001691 private void verifyApplications() {
1692 final Context context = mApp.getContext();
1693
1694 // Cross reference all the applications in our apps list with items in the workspace
1695 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001696 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001697 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001698 for (AppInfo app : mBgAllAppsList.data) {
Kenny Guyed131872014-04-30 03:02:21 +01001699 tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
Winson Chungc763c4e2013-07-19 13:49:06 -07001700 if (tmpInfos.isEmpty()) {
1701 // We are missing an application icon, so add this to the workspace
1702 added.add(app);
1703 // This is a rare event, so lets log it
1704 Log.e(TAG, "Missing Application on load: " + app);
1705 }
1706 }
1707 }
1708 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001709 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001710 }
1711 }
1712
Joe Onorato36115782010-06-17 13:28:48 -04001713 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001714 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001715 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001716 LauncherAppState app = LauncherAppState.getInstance();
1717 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001718 final int countX = (int) grid.numColumns;
1719 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001720
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001722 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001723 // Return early if we detect that an item is under the hotseat button
1724 if (mCallbacks == null ||
1725 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001726 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001727 Log.e(TAG, "Error loading shortcut into hotseat " + item
1728 + " into position (" + item.screenId + ":" + item.cellX + ","
1729 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001730 return false;
1731 }
1732
Dan Sandler295ae182013-12-10 16:05:47 -05001733 final ItemInfo[][] hotseatItems =
1734 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1735
Adam Cohenae4409d2013-11-26 10:34:59 -08001736 if (item.screenId >= grid.numHotseatIcons) {
1737 Log.e(TAG, "Error loading shortcut " + item
1738 + " into hotseat position " + item.screenId
1739 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1740 + ")");
1741 return false;
1742 }
1743
Dan Sandler295ae182013-12-10 16:05:47 -05001744 if (hotseatItems != null) {
1745 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001746 Log.e(TAG, "Error loading shortcut into hotseat " + item
1747 + " into position (" + item.screenId + ":" + item.cellX + ","
1748 + item.cellY + ") occupied by "
1749 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1750 [(int) item.screenId][0]);
1751 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001752 } else {
1753 hotseatItems[(int) item.screenId][0] = item;
1754 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001755 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001756 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001757 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001758 items[(int) item.screenId][0] = item;
1759 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001760 return true;
1761 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001762 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1763 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001764 return true;
1765 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001766
Adam Cohendcd297f2013-06-18 13:13:40 -07001767 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001768 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001769 occupied.put(item.screenId, items);
1770 }
1771
Dan Sandler295ae182013-12-10 16:05:47 -05001772 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001773 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1774 item.cellX < 0 || item.cellY < 0 ||
1775 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1776 Log.e(TAG, "Error loading shortcut " + item
1777 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1778 + item.cellX + "," + item.cellY
1779 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1780 return false;
1781 }
1782
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001783 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001784 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1785 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001786 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001787 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001788 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001789 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001790 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001792 return false;
1793 }
1794 }
1795 }
1796 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1797 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001798 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001799 }
1800 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001801
Joe Onorato36115782010-06-17 13:28:48 -04001802 return true;
1803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Winson Chungba9c37f2013-08-30 14:11:37 -07001805 /** Clears all the sBg data structures */
1806 private void clearSBgDataStructures() {
1807 synchronized (sBgLock) {
1808 sBgWorkspaceItems.clear();
1809 sBgAppWidgets.clear();
1810 sBgFolders.clear();
1811 sBgItemsIdMap.clear();
1812 sBgDbIconCache.clear();
1813 sBgWorkspaceScreens.clear();
1814 }
1815 }
1816
Dan Sandlerd5024042014-01-09 15:01:33 -05001817 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001818 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001819 // Log to disk
1820 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1821
Joe Onorato36115782010-06-17 13:28:48 -04001822 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823
Joe Onorato36115782010-06-17 13:28:48 -04001824 final Context context = mContext;
1825 final ContentResolver contentResolver = context.getContentResolver();
1826 final PackageManager manager = context.getPackageManager();
1827 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1828 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001829
Winson Chung892c74d2013-08-22 16:15:50 -07001830 LauncherAppState app = LauncherAppState.getInstance();
1831 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1832 int countX = (int) grid.numColumns;
1833 int countY = (int) grid.numRows;
1834
Dan Sandlerd5024042014-01-09 15:01:33 -05001835 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1836 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1837 LauncherAppState.getLauncherProvider().deleteDatabase();
1838 }
1839
1840 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1841 // append the user's Launcher2 shortcuts
1842 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1843 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1844 } else {
1845 // Make sure the default workspace is loaded
1846 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1847 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1848 }
Adam Cohene25af792013-06-06 23:08:25 -07001849
Adam Cohen71483f42014-05-15 14:04:01 -07001850 // This code path is for our old migration code and should no longer be exercised
1851 boolean loadedOldDb = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001852
Winson Chung9f9f00b2013-11-15 13:27:00 -08001853 // Log to disk
1854 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001855
Winson Chung2abf94d2012-07-18 18:16:38 -07001856 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001857 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001860 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001861 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001862 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001863 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001864
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 // +1 for the hotseat (it can be larger than the workspace)
1866 // Load workspace in reverse order to ensure that latest items are loaded first (and
1867 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001868 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001869
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 try {
1871 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1872 final int intentIndex = c.getColumnIndexOrThrow
1873 (LauncherSettings.Favorites.INTENT);
1874 final int titleIndex = c.getColumnIndexOrThrow
1875 (LauncherSettings.Favorites.TITLE);
1876 final int iconTypeIndex = c.getColumnIndexOrThrow(
1877 LauncherSettings.Favorites.ICON_TYPE);
1878 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1879 final int iconPackageIndex = c.getColumnIndexOrThrow(
1880 LauncherSettings.Favorites.ICON_PACKAGE);
1881 final int iconResourceIndex = c.getColumnIndexOrThrow(
1882 LauncherSettings.Favorites.ICON_RESOURCE);
1883 final int containerIndex = c.getColumnIndexOrThrow(
1884 LauncherSettings.Favorites.CONTAINER);
1885 final int itemTypeIndex = c.getColumnIndexOrThrow(
1886 LauncherSettings.Favorites.ITEM_TYPE);
1887 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1888 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001889 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1890 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 final int screenIndex = c.getColumnIndexOrThrow(
1892 LauncherSettings.Favorites.SCREEN);
1893 final int cellXIndex = c.getColumnIndexOrThrow
1894 (LauncherSettings.Favorites.CELLX);
1895 final int cellYIndex = c.getColumnIndexOrThrow
1896 (LauncherSettings.Favorites.CELLY);
1897 final int spanXIndex = c.getColumnIndexOrThrow
1898 (LauncherSettings.Favorites.SPANX);
1899 final int spanYIndex = c.getColumnIndexOrThrow(
1900 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 final int restoredIndex = c.getColumnIndexOrThrow(
1902 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001903 final int profileIdIndex = c.getColumnIndexOrThrow(
1904 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1906 //final int displayModeIndex = c.getColumnIndexOrThrow(
1907 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 ShortcutInfo info;
1910 String intentDescription;
1911 LauncherAppWidgetInfo appWidgetInfo;
1912 int container;
1913 long id;
1914 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001915 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001916
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001918 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 try {
1920 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001921 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 switch (itemType) {
1924 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1925 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001926 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 intentDescription = c.getString(intentIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001928 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001929 user = mUserManager.getUserForSerialNumber(serialNumber);
1930 if (user == null) {
1931 // User has been deleted remove the item.
1932 itemsToRemove.add(id);
1933 continue;
1934 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001935 try {
1936 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001937 ComponentName cn = intent.getComponent();
Kenny Guyed131872014-04-30 03:02:21 +01001938 if (cn != null && !isValidPackageActivity(context, cn, user)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001939 if (restored) {
1940 // might be installed later
1941 Launcher.addDumpLog(TAG,
1942 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001943 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001944 if (!mAppsCanBeOnRemoveableStorage) {
1945 // Log the invalid package, and remove it
1946 Launcher.addDumpLog(TAG,
1947 "Invalid package removed: " + cn, true);
1948 itemsToRemove.add(id);
1949 } else {
1950 // If apps can be on external storage, then we just
1951 // leave them for the user to remove (maybe add
1952 // visual treatment to it)
1953 Launcher.addDumpLog(TAG,
1954 "Invalid package found: " + cn, true);
1955 }
1956 continue;
Winson Chungee055712013-07-30 14:46:24 -07001957 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001958 } else if (restored) {
1959 // no special handling necessary for this restored item
1960 restoredRows.add(id);
1961 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001962 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001964 Launcher.addDumpLog(TAG,
1965 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 continue;
1967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001968
Chris Wrenf4d08112014-01-16 18:13:56 -05001969 if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001970 if (user.equals(UserHandleCompat.myUserHandle())) {
1971 Launcher.addDumpLog(TAG,
1972 "constructing info for partially restored package",
1973 true);
1974 info = getRestoredItemInfo(c, titleIndex, intent);
1975 intent = getRestoredItemIntent(c, context, intent);
1976 } else {
1977 // Don't restore items for other profiles.
1978 itemsToRemove.add(id);
1979 continue;
1980 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001981 } else if (itemType ==
1982 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Kenny Guyed131872014-04-30 03:02:21 +01001983 info = getShortcutInfo(manager, intent, user, context, c, iconIndex,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 titleIndex, mLabelCache);
1985 } else {
1986 info = getShortcutInfo(c, context, iconTypeIndex,
1987 iconPackageIndex, iconResourceIndex, iconIndex,
1988 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001989
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 // App shortcuts that used to be automatically added to Launcher
1991 // didn't always have the correct intent flags set, so do that
1992 // here
1993 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001994 intent.getCategories() != null &&
1995 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001996 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 intent.addFlags(
1998 Intent.FLAG_ACTIVITY_NEW_TASK |
1999 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2000 }
Michael Jurka96879562012-03-22 05:54:33 -07002001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002004 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 container = c.getInt(containerIndex);
2007 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 info.cellX = c.getInt(cellXIndex);
2010 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002011 info.spanX = 1;
2012 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002013 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Adam Cohenae4409d2013-11-26 10:34:59 -08002014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002016 deleteOnInvalidPlacement.set(false);
2017 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
2018 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002019 itemsToRemove.add(id);
2020 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 break;
2022 }
2023
2024 switch (container) {
2025 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2026 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2027 sBgWorkspaceItems.add(info);
2028 break;
2029 default:
2030 // Item is in a user folder
2031 FolderInfo folderInfo =
2032 findOrMakeFolder(sBgFolders, container);
2033 folderInfo.add(info);
2034 break;
2035 }
2036 sBgItemsIdMap.put(info.id, info);
2037
2038 // now that we've loaded everthing re-save it with the
2039 // icon in case it disappears somehow.
2040 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002041 } else {
2042 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 }
2044 break;
2045
2046 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2047 id = c.getLong(idIndex);
2048 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2049
2050 folderInfo.title = c.getString(titleIndex);
2051 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002054 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 folderInfo.cellX = c.getInt(cellXIndex);
2056 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002057 folderInfo.spanX = 1;
2058 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059
Daniel Sandler8802e962010-05-26 16:28:16 -04002060 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002061 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002062 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002063 deleteOnInvalidPlacement)) {
2064 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002065 itemsToRemove.add(id);
2066 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002067 break;
2068 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002069
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2072 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2073 sBgWorkspaceItems.add(folderInfo);
2074 break;
Joe Onorato36115782010-06-17 13:28:48 -04002075 }
Joe Onorato17a89222011-02-08 17:26:11 -08002076
Chris Wrenf4d08112014-01-16 18:13:56 -05002077 if (restored) {
2078 // no special handling required for restored folders
2079 restoredRows.add(id);
2080 }
2081
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2083 sBgFolders.put(folderInfo.id, folderInfo);
2084 break;
2085
2086 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2087 // Read all Launcher-specific widget details
2088 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002089 String savedProvider = c.getString(appWidgetProviderIndex);
2090
Joe Onorato36115782010-06-17 13:28:48 -04002091 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002092
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 final AppWidgetProviderInfo provider =
2094 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002095
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 if (!isSafeMode && (provider == null || provider.provider == null ||
2097 provider.provider.getPackageName() == null)) {
2098 String log = "Deleting widget that isn't installed anymore: id="
2099 + id + " appWidgetId=" + appWidgetId;
2100 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002101 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 itemsToRemove.add(id);
2103 } else {
2104 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2105 provider.provider);
2106 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002107 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 appWidgetInfo.cellX = c.getInt(cellXIndex);
2109 appWidgetInfo.cellY = c.getInt(cellYIndex);
2110 appWidgetInfo.spanX = c.getInt(spanXIndex);
2111 appWidgetInfo.spanY = c.getInt(spanYIndex);
2112 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2113 appWidgetInfo.minSpanX = minSpan[0];
2114 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002115
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 container = c.getInt(containerIndex);
2117 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2118 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2119 Log.e(TAG, "Widget found where container != " +
2120 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2121 continue;
2122 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002123
Adam Cohene25af792013-06-06 23:08:25 -07002124 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002126 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002127 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002128 deleteOnInvalidPlacement)) {
2129 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002130 itemsToRemove.add(id);
2131 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 break;
2133 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002134 String providerName = provider.provider.flattenToString();
2135 if (!providerName.equals(savedProvider)) {
2136 ContentValues values = new ContentValues();
2137 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2138 providerName);
2139 String where = BaseColumns._ID + "= ?";
2140 String[] args = {Integer.toString(c.getInt(idIndex))};
2141 contentResolver.update(contentUri, values, where, args);
2142 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2144 sBgAppWidgets.add(appWidgetInfo);
2145 }
Joe Onorato36115782010-06-17 13:28:48 -04002146 break;
2147 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002148 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002149 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002150 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 }
2152 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002153 if (c != null) {
2154 c.close();
2155 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 }
2157
Winson Chungba9c37f2013-08-30 14:11:37 -07002158 // Break early if we've stopped loading
2159 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002160 clearSBgDataStructures();
2161 return false;
2162 }
2163
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 if (itemsToRemove.size() > 0) {
2165 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002166 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 // Remove dead items
2168 for (long id : itemsToRemove) {
2169 if (DEBUG_LOADERS) {
2170 Log.d(TAG, "Removed id = " + id);
2171 }
2172 // Don't notify content observers
2173 try {
2174 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2175 null, null);
2176 } catch (RemoteException e) {
2177 Log.w(TAG, "Could not remove id = " + id);
2178 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002179 }
2180 }
2181
Chris Wrenf4d08112014-01-16 18:13:56 -05002182 if (restoredRows.size() > 0) {
2183 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2184 LauncherSettings.Favorites.CONTENT_URI);
2185 // Update restored items that no longer require special handling
2186 try {
2187 StringBuilder selectionBuilder = new StringBuilder();
2188 selectionBuilder.append(LauncherSettings.Favorites._ID);
2189 selectionBuilder.append(" IN (");
2190 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2191 selectionBuilder.append(")");
2192 ContentValues values = new ContentValues();
2193 values.put(LauncherSettings.Favorites.RESTORED, 0);
2194 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2195 values, selectionBuilder.toString(), null);
2196 } catch (RemoteException e) {
2197 Log.w(TAG, "Could not update restored rows");
2198 }
2199 }
2200
Winson Chungc763c4e2013-07-19 13:49:06 -07002201 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002202 long maxScreenId = 0;
2203 // If we're importing we use the old screen order.
2204 for (ItemInfo item: sBgItemsIdMap.values()) {
2205 long screenId = item.screenId;
2206 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2207 !sBgWorkspaceScreens.contains(screenId)) {
2208 sBgWorkspaceScreens.add(screenId);
2209 if (screenId > maxScreenId) {
2210 maxScreenId = screenId;
2211 }
2212 }
2213 }
2214 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002215 // Log to disk
2216 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2217 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2218 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002219
Michael Jurka414300a2013-08-27 15:42:35 +02002220 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002222
2223 // Update the max item id after we load an old db
2224 long maxItemId = 0;
2225 // If we're importing we use the old screen order.
2226 for (ItemInfo item: sBgItemsIdMap.values()) {
2227 maxItemId = Math.max(maxItemId, item.id);
2228 }
Michael Jurka414300a2013-08-27 15:42:35 +02002229 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002231 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2232 for (Integer i : orderedScreens.keySet()) {
2233 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002235 // Log to disk
2236 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2237 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002238
2239 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002240 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 for (ItemInfo item: sBgItemsIdMap.values()) {
2242 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2244 unusedScreens.contains(screenId)) {
2245 unusedScreens.remove(screenId);
2246 }
2247 }
2248
2249 // If there are any empty screens remove them, and update.
2250 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002251 // Log to disk
2252 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2253 TextUtils.join(", ", unusedScreens), true);
2254
Winson Chung933bae62013-08-29 11:42:30 -07002255 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002256 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2257 }
2258 }
2259
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 if (DEBUG_LOADERS) {
2261 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2262 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002264 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002266
Daniel Sandler566da102013-06-25 23:43:45 -04002267 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002268 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002270 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 line += " | ";
2272 }
Winson Chung892c74d2013-08-22 16:15:50 -07002273 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002274 ItemInfo[][] screen = occupied.get(screenId);
2275 if (x < screen.length && y < screen[x].length) {
2276 line += (screen[x][y] != null) ? "#" : ".";
2277 } else {
2278 line += "!";
2279 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 }
Joe Onorato36115782010-06-17 13:28:48 -04002281 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002284 }
Joe Onorato36115782010-06-17 13:28:48 -04002285 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002286 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002287 }
2288
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 /** Filters the set of items who are directly or indirectly (via another container) on the
2290 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002291 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 ArrayList<ItemInfo> allWorkspaceItems,
2293 ArrayList<ItemInfo> currentScreenItems,
2294 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002295 // Purge any null ItemInfos
2296 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2297 while (iter.hasNext()) {
2298 ItemInfo i = iter.next();
2299 if (i == null) {
2300 iter.remove();
2301 }
2302 }
2303
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 // Order the set of items by their containers first, this allows use to walk through the
2305 // list sequentially, build up a list of containers that are in the specified screen,
2306 // as well as all items in those containers.
2307 Set<Long> itemsOnScreen = new HashSet<Long>();
2308 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2309 @Override
2310 public int compare(ItemInfo lhs, ItemInfo rhs) {
2311 return (int) (lhs.container - rhs.container);
2312 }
2313 });
2314 for (ItemInfo info : allWorkspaceItems) {
2315 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002316 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 currentScreenItems.add(info);
2318 itemsOnScreen.add(info.id);
2319 } else {
2320 otherScreenItems.add(info);
2321 }
2322 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2323 currentScreenItems.add(info);
2324 itemsOnScreen.add(info.id);
2325 } else {
2326 if (itemsOnScreen.contains(info.container)) {
2327 currentScreenItems.add(info);
2328 itemsOnScreen.add(info.id);
2329 } else {
2330 otherScreenItems.add(info);
2331 }
2332 }
2333 }
2334 }
2335
2336 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002337 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002338 ArrayList<LauncherAppWidgetInfo> appWidgets,
2339 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2340 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002341
2342 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002343 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002345 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346 currentScreenWidgets.add(widget);
2347 } else {
2348 otherScreenWidgets.add(widget);
2349 }
2350 }
2351 }
2352
2353 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002354 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 HashMap<Long, ItemInfo> itemsIdMap,
2356 HashMap<Long, FolderInfo> folders,
2357 HashMap<Long, FolderInfo> currentScreenFolders,
2358 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002359
2360 for (long id : folders.keySet()) {
2361 ItemInfo info = itemsIdMap.get(id);
2362 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002363 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002365 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002366 currentScreenFolders.put(id, folder);
2367 } else {
2368 otherScreenFolders.put(id, folder);
2369 }
2370 }
2371 }
2372
2373 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2374 * right) */
2375 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002376 final LauncherAppState app = LauncherAppState.getInstance();
2377 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 // XXX: review this
2379 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002380 @Override
2381 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002382 int cellCountX = (int) grid.numColumns;
2383 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002384 int screenOffset = cellCountX * cellCountY;
2385 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002387 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002389 rhs.cellY * cellCountX + rhs.cellX);
2390 return (int) (lr - rr);
2391 }
2392 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002394
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2396 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002397 final Runnable r = new Runnable() {
2398 @Override
2399 public void run() {
2400 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2401 if (callbacks != null) {
2402 callbacks.bindScreens(orderedScreens);
2403 }
2404 }
2405 };
2406 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2407 }
2408
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2410 final ArrayList<ItemInfo> workspaceItems,
2411 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2412 final HashMap<Long, FolderInfo> folders,
2413 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002414
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 final boolean postOnMainThread = (deferredBindRunnables != null);
2416
2417 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002418 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002419 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002420 final int start = i;
2421 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002422 final Runnable r = new Runnable() {
2423 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002424 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002425 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002426 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002427 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2428 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 }
2430 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002431 };
2432 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002433 synchronized (deferredBindRunnables) {
2434 deferredBindRunnables.add(r);
2435 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002437 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 }
Joe Onorato36115782010-06-17 13:28:48 -04002439 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002440
2441 // Bind the folders
2442 if (!folders.isEmpty()) {
2443 final Runnable r = new Runnable() {
2444 public void run() {
2445 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2446 if (callbacks != null) {
2447 callbacks.bindFolders(folders);
2448 }
2449 }
2450 };
2451 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002452 synchronized (deferredBindRunnables) {
2453 deferredBindRunnables.add(r);
2454 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002456 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002457 }
2458 }
2459
2460 // Bind the widgets, one at a time
2461 N = appWidgets.size();
2462 for (int i = 0; i < N; i++) {
2463 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2464 final Runnable r = new Runnable() {
2465 public void run() {
2466 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2467 if (callbacks != null) {
2468 callbacks.bindAppWidget(widget);
2469 }
2470 }
2471 };
2472 if (postOnMainThread) {
2473 deferredBindRunnables.add(r);
2474 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002475 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 }
2477 }
2478 }
2479
2480 /**
2481 * Binds all loaded data to actual views on the main thread.
2482 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002483 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002484 final long t = SystemClock.uptimeMillis();
2485 Runnable r;
2486
2487 // Don't use these two variables in any of the callback runnables.
2488 // Otherwise we hold a reference to them.
2489 final Callbacks oldCallbacks = mCallbacks.get();
2490 if (oldCallbacks == null) {
2491 // This launcher has exited and nobody bothered to tell us. Just bail.
2492 Log.w(TAG, "LoaderTask running with no launcher");
2493 return;
2494 }
2495
Winson Chung9b9fb962013-11-15 15:39:34 -08002496 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2498 ArrayList<LauncherAppWidgetInfo> appWidgets =
2499 new ArrayList<LauncherAppWidgetInfo>();
2500 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2501 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002502 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002503 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 workspaceItems.addAll(sBgWorkspaceItems);
2505 appWidgets.addAll(sBgAppWidgets);
2506 folders.putAll(sBgFolders);
2507 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002508 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 }
2510
Derek Prothro7aff3992013-12-10 14:00:37 -05002511 final boolean isLoadingSynchronously =
2512 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002513 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002514 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002515 if (currScreen >= orderedScreenIds.size()) {
2516 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002517 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002518 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002519 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002520 final long currentScreenId = currentScreen < 0
2521 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002522
2523 // Load all the items that are on the current page first (and in the process, unbind
2524 // all the existing workspace items before we call startBinding() below.
2525 unbindWorkspaceItemsOnMainThread();
2526
2527 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2529 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2530 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2531 new ArrayList<LauncherAppWidgetInfo>();
2532 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2533 new ArrayList<LauncherAppWidgetInfo>();
2534 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2535 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2536
Winson Chung9b9fb962013-11-15 15:39:34 -08002537 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002538 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002539 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002540 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002541 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542 otherFolders);
2543 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2544 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2545
2546 // Tell the workspace that we're about to start binding items
2547 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002548 public void run() {
2549 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2550 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002551 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
2553 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002554 };
Winson Chung81b52252012-08-27 15:34:29 -07002555 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002556
Adam Cohendcd297f2013-06-18 13:13:40 -07002557 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2558
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002559 // Load items on the current page
2560 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2561 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002562 if (isLoadingSynchronously) {
2563 r = new Runnable() {
2564 public void run() {
2565 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002566 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002567 callbacks.onPageBoundSynchronously(currentScreen);
2568 }
2569 }
2570 };
Winson Chung81b52252012-08-27 15:34:29 -07002571 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002572 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573
Winson Chung4a2afa32012-07-19 14:53:05 -07002574 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2575 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002576 synchronized (mDeferredBindRunnables) {
2577 mDeferredBindRunnables.clear();
2578 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002579 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002580 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002581
2582 // Tell the workspace that we're done binding items
2583 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002584 public void run() {
2585 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2586 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002587 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002588 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002589
Winson Chung98e030b2012-05-07 16:01:11 -07002590 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002591 if (DEBUG_LOADERS) {
2592 Log.d(TAG, "bound workspace in "
2593 + (SystemClock.uptimeMillis()-t) + "ms");
2594 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002595
2596 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002597 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002598 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002599 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002600 synchronized (mDeferredBindRunnables) {
2601 mDeferredBindRunnables.add(r);
2602 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002603 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002604 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002605 }
Joe Onorato36115782010-06-17 13:28:48 -04002606 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002607
Joe Onorato36115782010-06-17 13:28:48 -04002608 private void loadAndBindAllApps() {
2609 if (DEBUG_LOADERS) {
2610 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2611 }
2612 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002613 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002614 synchronized (LoaderTask.this) {
2615 if (mStopped) {
2616 return;
2617 }
2618 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002619 }
Joe Onorato36115782010-06-17 13:28:48 -04002620 } else {
2621 onlyBindAllApps();
2622 }
2623 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002624
Joe Onorato36115782010-06-17 13:28:48 -04002625 private void onlyBindAllApps() {
2626 final Callbacks oldCallbacks = mCallbacks.get();
2627 if (oldCallbacks == null) {
2628 // This launcher has exited and nobody bothered to tell us. Just bail.
2629 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2630 return;
2631 }
2632
2633 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002634 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002635 final ArrayList<AppInfo> list
2636 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002637 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002638 public void run() {
2639 final long t = SystemClock.uptimeMillis();
2640 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2641 if (callbacks != null) {
2642 callbacks.bindAllApplications(list);
2643 }
2644 if (DEBUG_LOADERS) {
2645 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2646 + (SystemClock.uptimeMillis()-t) + "ms");
2647 }
2648 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002649 };
2650 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002651 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002652 r.run();
2653 } else {
2654 mHandler.post(r);
2655 }
Joe Onorato36115782010-06-17 13:28:48 -04002656 }
2657
Winson Chung64359a52013-07-08 17:17:08 -07002658 private void loadAllApps() {
2659 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002660
Joe Onorato36115782010-06-17 13:28:48 -04002661 final Callbacks oldCallbacks = mCallbacks.get();
2662 if (oldCallbacks == null) {
2663 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002664 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002665 return;
2666 }
2667
2668 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2669 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2670
Kenny Guyed131872014-04-30 03:02:21 +01002671 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2672
Winson Chung64359a52013-07-08 17:17:08 -07002673 // Clear the list of apps
2674 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002675 for (UserHandleCompat user : profiles) {
2676 // Query for the set of apps
2677 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2678 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2679 if (DEBUG_LOADERS) {
2680 Log.d(TAG, "getActivityList took "
2681 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2682 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2683 }
2684 // Fail if we don't have any apps
2685 if (apps == null || apps.isEmpty()) {
2686 return;
2687 }
2688 // Sort the applications by name
2689 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2690 Collections.sort(apps,
2691 new LauncherModel.ShortcutNameComparator(mLabelCache));
2692 if (DEBUG_LOADERS) {
2693 Log.d(TAG, "sort took "
2694 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2695 }
Joe Onorato36115782010-06-17 13:28:48 -04002696
Kenny Guyed131872014-04-30 03:02:21 +01002697 // Create the ApplicationInfos
2698 for (int i = 0; i < apps.size(); i++) {
2699 LauncherActivityInfoCompat app = apps.get(i);
2700 // This builds the icon bitmaps.
2701 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2702 }
Winson Chung64359a52013-07-08 17:17:08 -07002703 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002704 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002705 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2706 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002707
2708 // Post callback on main thread
2709 mHandler.post(new Runnable() {
2710 public void run() {
2711 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002712 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002713 if (callbacks != null) {
2714 callbacks.bindAllApplications(added);
2715 if (DEBUG_LOADERS) {
2716 Log.d(TAG, "bound " + added.size() + " apps in "
2717 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2718 }
2719 } else {
2720 Log.i(TAG, "not binding apps: no Launcher activity");
2721 }
2722 }
2723 });
2724
Joe Onorato36115782010-06-17 13:28:48 -04002725 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002726 Log.d(TAG, "Icons processed in "
2727 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002728 }
2729 }
2730
2731 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002732 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002733 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2734 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2735 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2736 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2737 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2738 }
Joe Onorato36115782010-06-17 13:28:48 -04002739 }
2740 }
2741
2742 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002743 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002744 }
2745
2746 private class PackageUpdatedTask implements Runnable {
2747 int mOp;
2748 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002749 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002750
2751 public static final int OP_NONE = 0;
2752 public static final int OP_ADD = 1;
2753 public static final int OP_UPDATE = 2;
2754 public static final int OP_REMOVE = 3; // uninstlled
2755 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2756
2757
Kenny Guyed131872014-04-30 03:02:21 +01002758 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002759 mOp = op;
2760 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002761 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002762 }
2763
2764 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002765 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002766
2767 final String[] packages = mPackages;
2768 final int N = packages.length;
2769 switch (mOp) {
2770 case OP_ADD:
2771 for (int i=0; i<N; i++) {
2772 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002773 mIconCache.remove(packages[i], mUser);
2774 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002775 }
2776 break;
2777 case OP_UPDATE:
2778 for (int i=0; i<N; i++) {
2779 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002780 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002781 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002782 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002783 }
2784 break;
2785 case OP_REMOVE:
2786 case OP_UNAVAILABLE:
2787 for (int i=0; i<N; i++) {
2788 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002789 mBgAllAppsList.removePackage(packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002790 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002791 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002792 }
2793 break;
2794 }
2795
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002796 ArrayList<AppInfo> added = null;
2797 ArrayList<AppInfo> modified = null;
2798 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002799
Adam Cohen487f7dd2012-06-28 18:12:10 -07002800 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002801 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002802 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002803 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002804 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002805 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002806 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002807 }
Winson Chung5d55f332012-07-16 20:45:03 -07002808 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002809 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002810 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002811 }
2812
Joe Onorato36115782010-06-17 13:28:48 -04002813 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2814 if (callbacks == null) {
2815 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2816 return;
2817 }
2818
2819 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002820 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002821 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002822 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002823 addAndBindAddedWorkspaceApps(context, addedInfos);
2824 } else {
2825 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002826 }
Joe Onorato36115782010-06-17 13:28:48 -04002827 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002828
Joe Onorato36115782010-06-17 13:28:48 -04002829 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002830 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002831
2832 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002833 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002834 ArrayList<ItemInfo> infos =
Kenny Guyed131872014-04-30 03:02:21 +01002835 getItemInfoForComponentName(a.componentName, mUser);
Winson Chung64359a52013-07-08 17:17:08 -07002836 for (ItemInfo i : infos) {
2837 if (isShortcutInfoUpdateable(i)) {
2838 ShortcutInfo info = (ShortcutInfo) i;
2839 info.title = a.title.toString();
2840 updateItemInDatabase(context, info);
2841 }
2842 }
2843 }
2844
Joe Onorato36115782010-06-17 13:28:48 -04002845 mHandler.post(new Runnable() {
2846 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002847 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2848 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002849 callbacks.bindAppsUpdated(modifiedFinal);
2850 }
2851 }
2852 });
2853 }
Winson Chung83892cc2013-05-01 16:53:33 -07002854
Winson Chungdf95eb12013-10-16 14:57:07 -07002855 final ArrayList<String> removedPackageNames =
2856 new ArrayList<String>();
2857 if (mOp == OP_REMOVE) {
2858 // Mark all packages in the broadcast to be removed
2859 removedPackageNames.addAll(Arrays.asList(packages));
2860 } else if (mOp == OP_UPDATE) {
2861 // Mark disabled packages in the broadcast to be removed
2862 final PackageManager pm = context.getPackageManager();
2863 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01002864 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002865 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002866 }
2867 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002868 }
2869 // Remove all the components associated with this package
2870 for (String pn : removedPackageNames) {
Kenny Guyed131872014-04-30 03:02:21 +01002871 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002872 for (ItemInfo i : infos) {
2873 deleteItemFromDatabase(context, i);
2874 }
2875 }
2876 // Remove all the specific components
2877 for (AppInfo a : removedApps) {
Kenny Guyed131872014-04-30 03:02:21 +01002878 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002879 for (ItemInfo i : infos) {
2880 deleteItemFromDatabase(context, i);
2881 }
2882 }
2883 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2884 // Remove any queued items from the install queue
2885 String spKey = LauncherAppState.getSharedPreferencesKey();
2886 SharedPreferences sp =
2887 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2888 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2889 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002890 mHandler.post(new Runnable() {
2891 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002892 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2893 if (callbacks == cb && cb != null) {
Kenny Guyed131872014-04-30 03:02:21 +01002894 callbacks.bindComponentsRemoved(removedPackageNames, removedApps, mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002895 }
2896 }
2897 });
Joe Onoratobe386092009-11-17 17:32:16 -08002898 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002899
Michael Jurkac402cd92013-05-20 15:49:32 +02002900 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01002901 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002902 mHandler.post(new Runnable() {
2903 @Override
2904 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002905 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2906 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002907 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002908 }
2909 }
2910 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002911
2912 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002913 mHandler.post(new Runnable() {
2914 public void run() {
2915 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2916 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002917 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002918 }
2919 }
2920 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 }
2922 }
2923
Michael Jurkac402cd92013-05-20 15:49:32 +02002924 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2925 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2926 PackageManager packageManager = context.getPackageManager();
2927 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2928 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2929 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2930 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2931 Collections.sort(widgetsAndShortcuts,
2932 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2933 return widgetsAndShortcuts;
2934 }
2935
Kenny Guyed131872014-04-30 03:02:21 +01002936 private static boolean isPackageDisabled(Context context, String packageName,
2937 UserHandleCompat user) {
2938 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2939 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07002940 }
Adam Cohen556f6132014-01-15 15:18:08 -08002941
Kenny Guyed131872014-04-30 03:02:21 +01002942 public static boolean isValidPackageActivity(Context context, ComponentName cn,
2943 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07002944 if (cn == null) {
2945 return false;
2946 }
Kenny Guyed131872014-04-30 03:02:21 +01002947 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2948 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002949 return false;
2950 }
Kenny Guyed131872014-04-30 03:02:21 +01002951 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07002952 }
2953
Adam Cohena28b78e2014-05-20 17:03:04 -07002954 public static boolean isValidPackage(Context context, String packageName,
2955 UserHandleCompat user) {
2956 if (packageName == null) {
2957 return false;
2958 }
2959 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2960 return launcherApps.isPackageEnabledForProfile(packageName, user);
2961 }
2962
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002964 * Make an ShortcutInfo object for a restored application or shortcut item that points
2965 * to a package that is not yet installed on the system.
2966 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002967 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002968 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002969 if (cursor != null) {
2970 info.title = cursor.getString(titleIndex);
2971 } else {
2972 info.title = "";
2973 }
Kenny Guyed131872014-04-30 03:02:21 +01002974 info.user = UserHandleCompat.myUserHandle();
2975 info.setIcon(mIconCache.getIcon(intent, info.title.toString(), info.user));
Chris Wrenf4d08112014-01-16 18:13:56 -05002976 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002977 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002978 return info;
2979 }
2980
2981 /**
2982 * Make an Intent object for a restored application or shortcut item that points
2983 * to the market page for the item.
2984 */
2985 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002986 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002987 ComponentName componentName = intent.getComponent();
2988 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2989 Uri marketUri = new Uri.Builder()
2990 .scheme("market")
2991 .authority("details")
2992 .appendQueryParameter("id", componentName.getPackageName())
2993 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002994 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002995 marketIntent.setData(marketUri);
2996 return marketIntent;
2997 }
2998
2999 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003000 * This is called from the code that adds shortcuts from the intent receiver. This
3001 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 */
Kenny Guyed131872014-04-30 03:02:21 +01003003 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3004 UserHandleCompat user, Context context) {
3005 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05003006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007
Joe Onorato56d82912010-03-07 14:32:10 -05003008 /**
3009 * Make an ShortcutInfo object for a shortcut that is an application.
3010 *
3011 * If c is not null, then it will be used to fill in missing data like the title and icon.
3012 */
Kenny Guyed131872014-04-30 03:02:21 +01003013 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3014 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
3015 HashMap<Object, CharSequence> labelCache) {
3016 if (user == null) {
3017 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003018 return null;
3019 }
3020
Kenny Guyed131872014-04-30 03:02:21 +01003021 ComponentName componentName = intent.getComponent();
3022 if (componentName == null) {
3023 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3024 return null;
3025 }
3026
3027 Intent newIntent = new Intent(intent.getAction(), null);
3028 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3029 newIntent.setComponent(componentName);
3030 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
3031 if (lai == null) {
3032 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3033 return null;
3034 }
3035
3036 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003037
Joe Onorato56d82912010-03-07 14:32:10 -05003038 // the resource -- This may implicitly give us back the fallback icon,
3039 // but don't worry about that. All we're doing with usingFallbackIcon is
3040 // to avoid saving lots of copies of that in the database, and most apps
3041 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003042 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003043
Joe Onorato56d82912010-03-07 14:32:10 -05003044 // the db
3045 if (icon == null) {
3046 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003047 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003048 }
3049 }
3050 // the fallback icon
3051 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003052 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003053 info.usingFallbackIcon = true;
3054 }
3055 info.setIcon(icon);
3056
Kenny Guyed131872014-04-30 03:02:21 +01003057 // From the cache.
3058 if (labelCache != null) {
3059 info.title = labelCache.get(componentName);
3060 }
3061
Joe Onorato56d82912010-03-07 14:32:10 -05003062 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003063 if (info.title == null && lai != null) {
3064 info.title = lai.getLabel();
3065 if (labelCache != null) {
3066 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003067 }
Joe Onorato56d82912010-03-07 14:32:10 -05003068 }
3069 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003071 if (c != null) {
3072 info.title = c.getString(titleIndex);
3073 }
3074 }
3075 // fall back to the class name of the activity
3076 if (info.title == null) {
3077 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003080 info.user = user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003083
Winson Chung64359a52013-07-08 17:17:08 -07003084 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3085 ItemInfoFilter f) {
3086 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3087 for (ItemInfo i : infos) {
3088 if (i instanceof ShortcutInfo) {
3089 ShortcutInfo info = (ShortcutInfo) i;
3090 ComponentName cn = info.intent.getComponent();
3091 if (cn != null && f.filterItem(null, info, cn)) {
3092 filtered.add(info);
3093 }
3094 } else if (i instanceof FolderInfo) {
3095 FolderInfo info = (FolderInfo) i;
3096 for (ShortcutInfo s : info.contents) {
3097 ComponentName cn = s.intent.getComponent();
3098 if (cn != null && f.filterItem(info, s, cn)) {
3099 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003100 }
3101 }
Winson Chung64359a52013-07-08 17:17:08 -07003102 } else if (i instanceof LauncherAppWidgetInfo) {
3103 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3104 ComponentName cn = info.providerName;
3105 if (cn != null && f.filterItem(null, info, cn)) {
3106 filtered.add(info);
3107 }
Winson Chung8a435102012-08-30 17:16:53 -07003108 }
3109 }
Winson Chung64359a52013-07-08 17:17:08 -07003110 return new ArrayList<ItemInfo>(filtered);
3111 }
3112
Kenny Guyed131872014-04-30 03:02:21 +01003113 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn,
3114 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003115 ItemInfoFilter filter = new ItemInfoFilter() {
3116 @Override
3117 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003118 return cn.getPackageName().equals(pn) && info.user.equals(user);
Winson Chung64359a52013-07-08 17:17:08 -07003119 }
3120 };
3121 return filterItemInfos(sBgItemsIdMap.values(), filter);
3122 }
3123
Kenny Guyed131872014-04-30 03:02:21 +01003124 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3125 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003126 ItemInfoFilter filter = new ItemInfoFilter() {
3127 @Override
3128 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003129 if (info.user == null) {
3130 return cn.equals(cname);
3131 } else {
3132 return cn.equals(cname) && info.user.equals(user);
3133 }
Winson Chung64359a52013-07-08 17:17:08 -07003134 }
3135 };
3136 return filterItemInfos(sBgItemsIdMap.values(), filter);
3137 }
3138
3139 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3140 if (i instanceof ShortcutInfo) {
3141 ShortcutInfo info = (ShortcutInfo) i;
3142 // We need to check for ACTION_MAIN otherwise getComponent() might
3143 // return null for some shortcuts (for instance, for shortcuts to
3144 // web pages.)
3145 Intent intent = info.intent;
3146 ComponentName name = intent.getComponent();
3147 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3148 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3149 return true;
3150 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003151 // placeholder shortcuts get special treatment, let them through too.
3152 if (info.getRestoredIntent() != null) {
3153 return true;
3154 }
Winson Chung64359a52013-07-08 17:17:08 -07003155 }
3156 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003157 }
3158
3159 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003160 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003162 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003163 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3164 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165
Joe Onorato56d82912010-03-07 14:32:10 -05003166 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003167 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003168 // Non-app shortcuts are only supported for current user.
3169 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003172 // TODO: If there's an explicit component and we can't install that, delete it.
3173
Joe Onorato56d82912010-03-07 14:32:10 -05003174 info.title = c.getString(titleIndex);
3175
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 int iconType = c.getInt(iconTypeIndex);
3177 switch (iconType) {
3178 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3179 String packageName = c.getString(iconPackageIndex);
3180 String resourceName = c.getString(iconResourceIndex);
3181 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003182 info.customIcon = false;
3183 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003186 if (resources != null) {
3187 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003188 icon = Utilities.createIconBitmap(
3189 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003192 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 }
Joe Onorato56d82912010-03-07 14:32:10 -05003194 // the db
3195 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003196 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003197 }
3198 // the fallback icon
3199 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003200 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003201 info.usingFallbackIcon = true;
3202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 break;
3204 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003205 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003206 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003207 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003208 info.customIcon = false;
3209 info.usingFallbackIcon = true;
3210 } else {
3211 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 break;
3214 default:
Kenny Guyed131872014-04-30 03:02:21 +01003215 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003216 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217 info.customIcon = false;
3218 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003220 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003221 return info;
3222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223
Michael Jurka931dc972011-08-05 15:08:15 -07003224 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003225 @SuppressWarnings("all") // suppress dead code warning
3226 final boolean debug = false;
3227 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003228 Log.d(TAG, "getIconFromCursor app="
3229 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3230 }
3231 byte[] data = c.getBlob(iconIndex);
3232 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003233 return Utilities.createIconBitmap(
3234 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003235 } catch (Exception e) {
3236 return null;
3237 }
3238 }
3239
Winson Chung3d503fb2011-07-13 17:25:49 -07003240 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3241 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003242 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003243 if (info == null) {
3244 return null;
3245 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003246 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003247
3248 return info;
3249 }
3250
Winson Chunga9abd0e2010-10-27 17:18:37 -07003251 /**
Winson Chung55cef262010-10-28 14:14:18 -07003252 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3253 */
3254 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3255 ComponentName component) {
3256 List<AppWidgetProviderInfo> widgets =
3257 AppWidgetManager.getInstance(context).getInstalledProviders();
3258 for (AppWidgetProviderInfo info : widgets) {
3259 if (info.provider.equals(component)) {
3260 return info;
3261 }
3262 }
3263 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003264 }
3265
Winson Chung68846fd2010-10-29 11:00:27 -07003266 /**
3267 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3268 */
3269 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3270 final PackageManager packageManager = context.getPackageManager();
3271 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3272 new ArrayList<WidgetMimeTypeHandlerData>();
3273
3274 final Intent supportsIntent =
3275 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3276 supportsIntent.setType(mimeType);
3277
3278 // Create a set of widget configuration components that we can test against
3279 final List<AppWidgetProviderInfo> widgets =
3280 AppWidgetManager.getInstance(context).getInstalledProviders();
3281 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3282 new HashMap<ComponentName, AppWidgetProviderInfo>();
3283 for (AppWidgetProviderInfo info : widgets) {
3284 configurationComponentToWidget.put(info.configure, info);
3285 }
3286
3287 // Run through each of the intents that can handle this type of clip data, and cross
3288 // reference them with the components that are actual configuration components
3289 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3290 PackageManager.MATCH_DEFAULT_ONLY);
3291 for (ResolveInfo info : activities) {
3292 final ActivityInfo activityInfo = info.activityInfo;
3293 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3294 activityInfo.name);
3295 if (configurationComponentToWidget.containsKey(infoComponent)) {
3296 supportedConfigurationActivities.add(
3297 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3298 configurationComponentToWidget.get(infoComponent)));
3299 }
3300 }
3301 return supportedConfigurationActivities;
3302 }
3303
Winson Chunga9abd0e2010-10-27 17:18:37 -07003304 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003305 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3306 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3307 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3308
Adam Cohend9198822011-11-22 16:42:47 -08003309 if (intent == null) {
3310 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3311 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3312 return null;
3313 }
3314
Joe Onorato0589f0f2010-02-08 13:44:00 -08003315 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003316 boolean customIcon = false;
3317 ShortcutIconResource iconResource = null;
3318
3319 if (bitmap != null && bitmap instanceof Bitmap) {
3320 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003321 customIcon = true;
3322 } else {
3323 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3324 if (extra != null && extra instanceof ShortcutIconResource) {
3325 try {
3326 iconResource = (ShortcutIconResource) extra;
3327 final PackageManager packageManager = context.getPackageManager();
3328 Resources resources = packageManager.getResourcesForApplication(
3329 iconResource.packageName);
3330 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003331 icon = Utilities.createIconBitmap(
Kenny Guyed131872014-04-30 03:02:21 +01003332 mIconCache.getFullResIcon(resources, id),
3333 context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003334 } catch (Exception e) {
3335 Log.w(TAG, "Could not load shortcut icon: " + extra);
3336 }
3337 }
3338 }
3339
Michael Jurkac9d95c52011-08-29 14:03:34 -07003340 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003341
Kenny Guyed131872014-04-30 03:02:21 +01003342 // Only support intents for current user for now. Intents sent from other
3343 // users wouldn't get here without intent forwarding anyway.
3344 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003345 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003346 if (fallbackIcon != null) {
3347 icon = fallbackIcon;
3348 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003349 icon = mIconCache.getDefaultIcon(info.user);
Winson Chunga9abd0e2010-10-27 17:18:37 -07003350 info.usingFallbackIcon = true;
3351 }
Joe Onorato56d82912010-03-07 14:32:10 -05003352 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003353 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003354
Joe Onorato0589f0f2010-02-08 13:44:00 -08003355 info.title = name;
3356 info.intent = intent;
3357 info.customIcon = customIcon;
3358 info.iconResource = iconResource;
3359
3360 return info;
3361 }
3362
Winson Chungaac01e12011-08-17 10:37:13 -07003363 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3364 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003365 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003366 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003367 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003368 }
Joe Onorato56d82912010-03-07 14:32:10 -05003369 // If this icon doesn't have a custom icon, check to see
3370 // what's stored in the DB, and if it doesn't match what
3371 // we're going to show, store what we are going to show back
3372 // into the DB. We do this so when we're loading, if the
3373 // package manager can't find an icon (for example because
3374 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003375 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003376 cache.put(info, c.getBlob(iconIndex));
3377 return true;
3378 }
3379 return false;
3380 }
3381 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3382 boolean needSave = false;
3383 try {
3384 if (data != null) {
3385 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3386 Bitmap loaded = info.getIcon(mIconCache);
3387 needSave = !saved.sameAs(loaded);
3388 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003389 needSave = true;
3390 }
Winson Chungaac01e12011-08-17 10:37:13 -07003391 } catch (Exception e) {
3392 needSave = true;
3393 }
3394 if (needSave) {
3395 Log.d(TAG, "going to save icon bitmap for info=" + info);
3396 // This is slower than is ideal, but this only happens once
3397 // or when the app is updated with a new icon.
3398 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003399 }
3400 }
3401
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003403 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 * or make a new one.
3405 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003406 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 // See if a placeholder was created for us already
3408 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003409 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003411 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 folders.put(id, folderInfo);
3413 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003414 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003415 }
3416
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003417 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003418 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003419 return new Comparator<AppInfo>() {
3420 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003421 if (a.user.equals(b.user)) {
3422 int result = collator.compare(a.title.toString().trim(),
3423 b.title.toString().trim());
3424 if (result == 0) {
3425 result = a.componentName.compareTo(b.componentName);
3426 }
3427 return result;
3428 } else {
3429 // TODO Need to figure out rules for sorting
3430 // profiles, this puts work second.
3431 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003432 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003433 }
Winson Chung11904872012-09-17 16:58:46 -07003434 };
3435 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003436 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3437 = new Comparator<AppInfo>() {
3438 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003439 if (a.firstInstallTime < b.firstInstallTime) return 1;
3440 if (a.firstInstallTime > b.firstInstallTime) return -1;
3441 return 0;
3442 }
3443 };
Winson Chung11904872012-09-17 16:58:46 -07003444 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3445 final Collator collator = Collator.getInstance();
3446 return new Comparator<AppWidgetProviderInfo>() {
3447 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003448 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003449 }
3450 };
3451 }
Winson Chung5308f242011-08-18 12:12:41 -07003452 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3453 if (info.activityInfo != null) {
3454 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3455 } else {
3456 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3457 }
3458 }
Kenny Guyed131872014-04-30 03:02:21 +01003459 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003460 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003461 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003462 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003463 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003464 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003465 }
Kenny Guyed131872014-04-30 03:02:21 +01003466 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003467 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003468 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003469 }
Kenny Guyed131872014-04-30 03:02:21 +01003470 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003471 CharSequence labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003472 ComponentName keyA = a.getComponentName();
3473 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003474 if (mLabelCache.containsKey(keyA)) {
3475 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003476 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003477 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003478
Winson Chung5308f242011-08-18 12:12:41 -07003479 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003480 }
Winson Chung5308f242011-08-18 12:12:41 -07003481 if (mLabelCache.containsKey(keyB)) {
3482 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003483 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003484 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003485
Winson Chung5308f242011-08-18 12:12:41 -07003486 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003487 }
Winson Chung11904872012-09-17 16:58:46 -07003488 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003489 }
3490 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003491 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003492 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003493 private PackageManager mPackageManager;
3494 private HashMap<Object, String> mLabelCache;
3495 WidgetAndShortcutNameComparator(PackageManager pm) {
3496 mPackageManager = pm;
3497 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003498 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003499 }
3500 public final int compare(Object a, Object b) {
3501 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003502 if (mLabelCache.containsKey(a)) {
3503 labelA = mLabelCache.get(a);
3504 } else {
3505 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003506 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003507 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003508 mLabelCache.put(a, labelA);
3509 }
3510 if (mLabelCache.containsKey(b)) {
3511 labelB = mLabelCache.get(b);
3512 } else {
3513 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003514 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003515 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003516 mLabelCache.put(b, labelB);
3517 }
Winson Chung11904872012-09-17 16:58:46 -07003518 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003519 }
3520 };
Joe Onoratobe386092009-11-17 17:32:16 -08003521
3522 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003523 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003524 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3525 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3526 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3527 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003528 if (mLoaderTask != null) {
3529 mLoaderTask.dumpState();
3530 } else {
3531 Log.d(TAG, "mLoaderTask=null");
3532 }
Joe Onoratobe386092009-11-17 17:32:16 -08003533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003534}