blob: 585c238c33182c39f19556a6d255be4cce5843a8 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Kenny Guyed131872014-04-30 03:02:21 +010047import com.android.launcher3.compat.LauncherActivityInfoCompat;
48import com.android.launcher3.compat.LauncherAppsCompat;
49import com.android.launcher3.compat.UserHandleCompat;
50import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080052
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.lang.ref.WeakReference;
54import java.net.URISyntaxException;
55import java.text.Collator;
56import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070057import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070058import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.Collections;
60import java.util.Comparator;
61import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070062import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070063import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070065import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070066import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070067import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070068
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069/**
70 * Maintains in-memory state of the Launcher. It is expected that there should be only one
71 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070072 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 */
Kenny Guyed131872014-04-30 03:02:21 +010074public class LauncherModel extends BroadcastReceiver
75 implements LauncherAppsCompat.OnAppsChangedListenerCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080076 static final boolean DEBUG_LOADERS = false;
Chris Wrenb358f812014-04-16 13:37:00 -040077 private static final boolean DEBUG_RECEIVER = true; // STOPSHIP(cwren) temporary for debugging
78
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070080
Daniel Sandler8707e0f2013-08-15 15:54:18 -070081 // true = use a "More Apps" folder for non-workspace apps on upgrade
82 // false = strew non-workspace apps across the workspace on upgrade
83 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
Dan Sandlerd5024042014-01-09 15:01:33 -050084 public static final int LOADER_FLAG_NONE = 0;
85 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
86 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
87
Joe Onorato36115782010-06-17 13:28:48 -040088 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050089 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080090
Winson Chungee055712013-07-30 14:46:24 -070091 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080092 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040093
Daniel Sandlercc8befa2013-06-11 14:45:48 -040094 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040095 private final Object mLock = new Object();
96 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040097 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020099 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Winson Chung81b52252012-08-27 15:34:29 -0700101 // Specific runnable types that are run on the main thread deferred handler, this allows us to
102 // clear all queued binding runnables when the Launcher activity is destroyed.
103 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
104 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
105
106
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700107 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
108 static {
109 sWorkerThread.start();
110 }
111 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
112
Joe Onoratocc67f472010-06-08 10:54:30 -0700113 // We start off with everything not loaded. After that, we assume that
114 // our monitoring of the package manager provides all updates and we never
115 // need to do a requery. These are only ever touched from the loader thread.
116 private boolean mWorkspaceLoaded;
117 private boolean mAllAppsLoaded;
118
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700119 // When we are loading pages synchronously, we can't just post the binding of items on the side
120 // pages as this delays the rotation process. Instead, we wait for a callback from the first
121 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
122 // a normal load, we also clear this set of Runnables.
123 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700128 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800129
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700130 // The lock that must be acquired before referencing any static bg data structures. Unlike
131 // other locks, this one can generally be held long-term because we never expect any of these
132 // static data structures to be referenced outside of the worker thread except on the first
133 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700134 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700139
Adam Cohen487f7dd2012-06-28 18:12:10 -0700140 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
141 // created by LauncherModel that are directly on the home screen (however, no widgets or
142 // shortcuts within folders).
143 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144
Adam Cohen487f7dd2012-06-28 18:12:10 -0700145 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
146 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147 new ArrayList<LauncherAppWidgetInfo>();
148
Adam Cohen487f7dd2012-06-28 18:12:10 -0700149 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
150 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700151
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
153 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700154
155 // sBgWorkspaceScreens is the ordered set of workspace screens.
156 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
157
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158 // </ only access in worker thread >
159
160 private IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Reena Lee99a73f32011-10-24 17:27:37 -0700162 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700163
Kenny Guyed131872014-04-30 03:02:21 +0100164 private final LauncherAppsCompat mLauncherApps;
165 private final UserManagerCompat mUserManager;
166
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700168 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public int getCurrentWorkspaceScreen();
170 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700171 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
172 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700173 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700174 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500175 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700176 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200178 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700179 public void bindAppsAdded(ArrayList<Long> newScreens,
180 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700181 ArrayList<ItemInfo> addAnimated,
182 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200183 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500184 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700185 public void bindComponentsRemoved(ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +0100186 ArrayList<AppInfo> appInfos, UserHandleCompat user);
Michael Jurkac402cd92013-05-20 15:49:32 +0200187 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100188 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700189 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700190 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700191 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Winson Chung64359a52013-07-08 17:17:08 -0700194 public interface ItemInfoFilter {
195 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
196 }
197
Bjorn Bringert1307f632013-10-03 22:31:03 +0100198 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800199 Context context = app.getContext();
200 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400201
Winson Chungee055712013-07-30 14:46:24 -0700202 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chungeb23cb22014-03-06 10:39:43 -0800203 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Jason Monk0bfcceb2014-03-21 15:42:06 -0400204 Uri.parse(context.getString(R.string.old_launcher_provider_uri)));
Winson Chungeb23cb22014-03-06 10:39:43 -0800205 mOldContentProviderExists = (client != null);
206 if (client != null) {
207 client.release();
208 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400209 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100210 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 mIconCache = iconCache;
212
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700214 Configuration config = res.getConfiguration();
215 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100216 mLauncherApps = LauncherAppsCompat.getInstance(context);
217 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 }
219
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700220 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
221 * posted on the main thread handler. */
222 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700223 runOnMainThread(r, 0);
224 }
225 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700226 if (sWorkerThread.getThreadId() == Process.myTid()) {
227 // If we are on the worker thread, post onto the main handler
228 mHandler.post(r);
229 } else {
230 r.run();
231 }
232 }
233
234 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
235 * posted on the worker thread handler. */
236 private static void runOnWorkerThread(Runnable r) {
237 if (sWorkerThread.getThreadId() == Process.myTid()) {
238 r.run();
239 } else {
240 // If we are not on the worker thread, then post to the worker handler
241 sWorker.post(r);
242 }
243 }
244
Winson Chunge43a1e72014-01-15 10:33:02 -0800245 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
246 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800247 }
248
Winson Chungc9168342013-06-26 14:54:55 -0700249 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
250 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700251 LauncherAppState app = LauncherAppState.getInstance();
252 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
253 final int xCount = (int) grid.numColumns;
254 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700255 boolean[][] occupied = new boolean[xCount][yCount];
256
257 int cellX, cellY, spanX, spanY;
258 for (int i = 0; i < items.size(); ++i) {
259 final ItemInfo item = items.get(i);
260 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
261 if (item.screenId == screen) {
262 cellX = item.cellX;
263 cellY = item.cellY;
264 spanX = item.spanX;
265 spanY = item.spanY;
266 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
267 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
268 occupied[x][y] = true;
269 }
270 }
271 }
272 }
273 }
274
275 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
276 }
277 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700278 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700279 int firstScreenIndex,
280 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700281 // Lock on the app so that we don't try and get the items while apps are being added
282 LauncherAppState app = LauncherAppState.getInstance();
283 LauncherModel model = app.getModel();
284 boolean found = false;
285 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700286 if (sWorkerThread.getThreadId() != Process.myTid()) {
287 // Flush the LauncherModel worker thread, so that if we just did another
288 // processInstallShortcut, we give it time for its shortcut to get added to the
289 // database (getItemsInLocalCoordinates reads the database)
290 model.flushWorkerThread();
291 }
Winson Chungc9168342013-06-26 14:54:55 -0700292 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700293
294 // Try adding to the workspace screens incrementally, starting at the default or center
295 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700296 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
297 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700298 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700299 int[] tmpCoordinates = new int[2];
300 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700301 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700302 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700303 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700304 }
305 }
306 }
Winson Chungc9168342013-06-26 14:54:55 -0700307 return null;
308 }
309
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500310 public void setPackageState(final String pkgName, final int state) {
311 // Process the updated package state
312 Runnable r = new Runnable() {
313 public void run() {
314 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
315 if (callbacks != null) {
316 callbacks.updatePackageState(pkgName, state);
317 }
318 }
319 };
320 mHandler.post(r);
321 }
322
Adam Cohen76a47a12014-02-05 11:47:43 -0800323 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
324 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
325
326 if (allAppsApps == null) {
327 throw new RuntimeException("allAppsApps must not be null");
328 }
329 if (allAppsApps.isEmpty()) {
330 return;
331 }
332
Chris Wrenb6d4c282014-01-27 14:17:08 -0500333 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
334 Iterator<AppInfo> iter = allAppsApps.iterator();
335 while (iter.hasNext()) {
336 ItemInfo a = iter.next();
Kenny Guyed131872014-04-30 03:02:21 +0100337 if (LauncherModel.appWasRestored(ctx, a.getIntent(), a.user)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500338 restoredAppsFinal.add((AppInfo) a);
339 }
340 }
341
Adam Cohen76a47a12014-02-05 11:47:43 -0800342 // Process the newly added applications and add them to the database first
343 Runnable r = new Runnable() {
344 public void run() {
345 runOnMainThread(new Runnable() {
346 public void run() {
347 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
348 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500349 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500350 for (AppInfo info : restoredAppsFinal) {
351 final Intent intent = info.getIntent();
352 if (intent != null) {
Kenny Guyed131872014-04-30 03:02:21 +0100353 mIconCache.deletePreloadedIcon(intent.getComponent(),
354 info.user);
Chris Wren6d0dde02014-02-10 12:16:54 -0500355 }
356 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500357 callbacks.bindAppsUpdated(restoredAppsFinal);
358 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500359 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800360 }
361 }
362 });
363 }
364 };
365 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700366 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800367
368 public void addAndBindAddedWorkspaceApps(final Context context,
369 final ArrayList<ItemInfo> workspaceApps) {
370 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
371
372 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800373 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
374 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800375 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700376 return;
Winson Chung997a9232013-07-24 15:33:46 -0700377 }
Winson Chung64359a52013-07-08 17:17:08 -0700378 // Process the newly added applications and add them to the database first
379 Runnable r = new Runnable() {
380 public void run() {
381 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
382 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500383 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700384
Winson Chung76828c82013-08-19 15:43:29 -0700385 // Get the list of workspace screens. We need to append to this list and
386 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
387 // called.
388 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
389 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
390 for (Integer i : orderedScreens.keySet()) {
391 long screenId = orderedScreens.get(i);
392 workspaceScreens.add(screenId);
393 }
394
Winson Chung64359a52013-07-08 17:17:08 -0700395 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700396 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700397 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700398 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700399 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700400 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700401
402 // Short-circuit this logic if the icon exists somewhere on the workspace
403 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500404 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
405 if (a instanceof AppInfo &&
Kenny Guyed131872014-04-30 03:02:21 +0100406 LauncherModel.appWasRestored(context, launchIntent, a.user)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500407 restoredAppsFinal.add((AppInfo) a);
408 }
Winson Chung64359a52013-07-08 17:17:08 -0700409 continue;
410 }
411
Winson Chung87412982013-10-03 18:34:14 -0700412 // Add this icon to the db, creating a new page if necessary. If there
413 // is only the empty page then we just add items to the first page.
414 // Otherwise, we add them to the next pages.
415 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700416 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700417 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700418 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200419 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700420
421 // If we can't find a valid position, then just add a new screen.
422 // This takes time so we need to re-queue the add until the new
423 // page is added. Create as many screens as necessary to satisfy
424 // the startSearchPageIndex.
425 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700426 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700427 while (numPagesToAdd > 0) {
428 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700429 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700430 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700431 addedWorkspaceScreensFinal.add(screenId);
432 numPagesToAdd--;
433 }
Winson Chung76828c82013-08-19 15:43:29 -0700434
Winson Chung64359a52013-07-08 17:17:08 -0700435 // Find the coordinate again
436 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700437 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700438 }
439 if (coords == null) {
440 throw new RuntimeException("Coordinates should not be null");
441 }
442
Winson Chung997a9232013-07-24 15:33:46 -0700443 ShortcutInfo shortcutInfo;
444 if (a instanceof ShortcutInfo) {
445 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200446 } else if (a instanceof AppInfo) {
447 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700448 } else {
449 throw new RuntimeException("Unexpected info type");
450 }
Winson Chung94d67682013-09-25 16:29:40 -0700451
Winson Chung64359a52013-07-08 17:17:08 -0700452 // Add the shortcut to the db
453 addItemToDatabase(context, shortcutInfo,
454 LauncherSettings.Favorites.CONTAINER_DESKTOP,
455 coords.first, coords.second[0], coords.second[1], false);
456 // Save the ShortcutInfo for binding in the workspace
457 addedShortcutsFinal.add(shortcutInfo);
458 }
459 }
460
Winson Chung76828c82013-08-19 15:43:29 -0700461 // Update the workspace screens
462 updateWorkspaceScreenOrder(context, workspaceScreens);
463
Adam Cohen76a47a12014-02-05 11:47:43 -0800464 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700465 runOnMainThread(new Runnable() {
466 public void run() {
467 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
468 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700469 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
470 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700471 if (!addedShortcutsFinal.isEmpty()) {
472 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
473 long lastScreenId = info.screenId;
474 for (ItemInfo i : addedShortcutsFinal) {
475 if (i.screenId == lastScreenId) {
476 addAnimated.add(i);
477 } else {
478 addNotAnimated.add(i);
479 }
Winson Chung997a9232013-07-24 15:33:46 -0700480 }
481 }
Winson Chungd64d1762013-08-20 14:37:16 -0700482 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800483 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500484 if (!restoredAppsFinal.isEmpty()) {
485 callbacks.bindAppsUpdated(restoredAppsFinal);
486 }
Winson Chung997a9232013-07-24 15:33:46 -0700487 }
Winson Chung64359a52013-07-08 17:17:08 -0700488 }
Winson Chung997a9232013-07-24 15:33:46 -0700489 });
490 }
Winson Chung64359a52013-07-08 17:17:08 -0700491 }
492 };
493 runOnWorkerThread(r);
494 }
495
Winson Chung81b52252012-08-27 15:34:29 -0700496 public void unbindItemInfosAndClearQueuedBindRunnables() {
497 if (sWorkerThread.getThreadId() == Process.myTid()) {
498 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
499 "main thread");
500 }
501
502 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400503 synchronized (mDeferredBindRunnables) {
504 mDeferredBindRunnables.clear();
505 }
Winson Chung81b52252012-08-27 15:34:29 -0700506 // Remove any queued bind runnables
507 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
508 // Unbind all the workspace items
509 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700510 }
511
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700512 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700513 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700514 // Ensure that we don't use the same workspace items data structure on the main thread
515 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700516 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
517 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700518 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700519 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
520 tmpAppWidgets.addAll(sBgAppWidgets);
521 }
522 Runnable r = new Runnable() {
523 @Override
524 public void run() {
525 for (ItemInfo item : tmpWorkspaceItems) {
526 item.unbind();
527 }
528 for (ItemInfo item : tmpAppWidgets) {
529 item.unbind();
530 }
531 }
532 };
533 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700534 }
535
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 /**
537 * Adds an item to the DB if it was not created previously, or move it to a new
538 * <container, screen, cellX, cellY>
539 */
540 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700541 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 if (item.container == ItemInfo.NO_ID) {
543 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700544 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400545 } else {
546 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700547 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549 }
550
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700551 static void checkItemInfoLocked(
552 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
553 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
554 if (modelItem != null && item != modelItem) {
555 // check all the data is consistent
556 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
557 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
558 ShortcutInfo shortcut = (ShortcutInfo) item;
559 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
560 modelShortcut.intent.filterEquals(shortcut.intent) &&
561 modelShortcut.id == shortcut.id &&
562 modelShortcut.itemType == shortcut.itemType &&
563 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700564 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700565 modelShortcut.cellX == shortcut.cellX &&
566 modelShortcut.cellY == shortcut.cellY &&
567 modelShortcut.spanX == shortcut.spanX &&
568 modelShortcut.spanY == shortcut.spanY &&
569 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
570 (modelShortcut.dropPos != null &&
571 shortcut.dropPos != null &&
572 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
573 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
574 // For all intents and purposes, this is the same object
575 return;
576 }
577 }
578
579 // the modelItem needs to match up perfectly with item if our model is
580 // to be consistent with the database-- for now, just require
581 // modelItem == item or the equality check above
582 String msg = "item: " + ((item != null) ? item.toString() : "null") +
583 "modelItem: " +
584 ((modelItem != null) ? modelItem.toString() : "null") +
585 "Error: ItemInfo passed to checkItemInfo doesn't match original";
586 RuntimeException e = new RuntimeException(msg);
587 if (stackTrace != null) {
588 e.setStackTrace(stackTrace);
589 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800590 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700591 }
592 }
593
Michael Jurka816474f2012-06-25 14:49:02 -0700594 static void checkItemInfo(final ItemInfo item) {
595 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
596 final long itemId = item.id;
597 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 public void run() {
599 synchronized (sBgLock) {
600 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700601 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700602 }
603 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700604 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700605 }
606
Michael Jurkac9d95c52011-08-29 14:03:34 -0700607 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
608 final ItemInfo item, final String callingFunction) {
609 final long itemId = item.id;
610 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
611 final ContentResolver cr = context.getContentResolver();
612
Adam Cohen487f7dd2012-06-28 18:12:10 -0700613 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700614 Runnable r = new Runnable() {
615 public void run() {
616 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700617 updateItemArrays(item, itemId, stackTrace);
618 }
619 };
620 runOnWorkerThread(r);
621 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700622
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700623 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
624 final ArrayList<ItemInfo> items, final String callingFunction) {
625 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700626
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700627 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
628 Runnable r = new Runnable() {
629 public void run() {
630 ArrayList<ContentProviderOperation> ops =
631 new ArrayList<ContentProviderOperation>();
632 int count = items.size();
633 for (int i = 0; i < count; i++) {
634 ItemInfo item = items.get(i);
635 final long itemId = item.id;
636 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
637 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700638
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700639 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
640 updateItemArrays(item, itemId, stackTrace);
641
642 }
643 try {
644 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
645 } catch (Exception e) {
646 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700647 }
648 }
649 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700650 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700651 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700652
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700653 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
654 // Lock on mBgLock *after* the db operation
655 synchronized (sBgLock) {
656 checkItemInfoLocked(itemId, item, stackTrace);
657
658 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
659 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
660 // Item is in a folder, make sure this folder exists
661 if (!sBgFolders.containsKey(item.container)) {
662 // An items container is being set to a that of an item which is not in
663 // the list of Folders.
664 String msg = "item: " + item + " container being set to: " +
665 item.container + ", not in the list of folders";
666 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700667 }
668 }
669
670 // Items are added/removed from the corresponding FolderInfo elsewhere, such
671 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
672 // that are on the desktop, as appropriate
673 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800674 if (modelItem != null &&
675 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
676 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 switch (modelItem.itemType) {
678 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
679 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
680 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
681 if (!sBgWorkspaceItems.contains(modelItem)) {
682 sBgWorkspaceItems.add(modelItem);
683 }
684 break;
685 default:
686 break;
687 }
688 } else {
689 sBgWorkspaceItems.remove(modelItem);
690 }
691 }
692 }
693
Michael Jurkac7700af2013-05-14 20:17:58 +0200694 public void flushWorkerThread() {
695 mFlushingWorkerThread = true;
696 Runnable waiter = new Runnable() {
697 public void run() {
698 synchronized (this) {
699 notifyAll();
700 mFlushingWorkerThread = false;
701 }
702 }
703 };
704
705 synchronized(waiter) {
706 runOnWorkerThread(waiter);
707 if (mLoaderTask != null) {
708 synchronized(mLoaderTask) {
709 mLoaderTask.notify();
710 }
711 }
712 boolean success = false;
713 while (!success) {
714 try {
715 waiter.wait();
716 success = true;
717 } catch (InterruptedException e) {
718 }
719 }
720 }
721 }
722
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700725 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700726 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729 item.cellX = cellX;
730 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700731
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 // We store hotseat items in canonical form which is this orientation invariant position
733 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700737 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740
741 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
744 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700745 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746
Michael Jurkac9d95c52011-08-29 14:03:34 -0700747 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700748 }
749
750 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700751 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
752 * cellX, cellY have already been updated on the ItemInfos.
753 */
754 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
755 final long container, final int screen) {
756
757 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
758 int count = items.size();
759
760 for (int i = 0; i < count; i++) {
761 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700762 item.container = container;
763
764 // We store hotseat items in canonical form which is this orientation invariant position
765 // in the hotseat
766 if (context instanceof Launcher && screen < 0 &&
767 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700768 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700769 item.cellY);
770 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700771 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700772 }
773
774 final ContentValues values = new ContentValues();
775 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
776 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
777 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700778 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700779
780 contentValues.add(values);
781 }
782 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
783 }
784
785 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700786 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800787 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700788 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700790 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800791 item.cellX = cellX;
792 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700793 item.spanX = spanX;
794 item.spanY = spanY;
795
796 // We store hotseat items in canonical form which is this orientation invariant position
797 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700800 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700801 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700802 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 }
Adam Cohend4844c32011-02-18 19:25:06 -0800804
Adam Cohend4844c32011-02-18 19:25:06 -0800805 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800806 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700807 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
808 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
809 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
810 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700811 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800812
Michael Jurka816474f2012-06-25 14:49:02 -0700813 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700814 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700815
816 /**
817 * Update an item to the database in a specified container.
818 */
819 static void updateItemInDatabase(Context context, final ItemInfo item) {
820 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100821 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700822 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
823 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800824 }
825
826 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 * Returns true if the shortcuts already exists in the database.
828 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 static boolean shortcutExists(Context context, String title, Intent intent) {
831 final ContentResolver cr = context.getContentResolver();
832 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
833 new String[] { "title", "intent" }, "title=? and intent=?",
834 new String[] { title, intent.toUri(0) }, null);
835 boolean result = false;
836 try {
837 result = c.moveToFirst();
838 } finally {
839 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700842 }
843
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500845 * Returns true if the shortcuts already exists in the database.
Kenny Guyed131872014-04-30 03:02:21 +0100846 * we identify a shortcut by the component name of the intent
847 * and the user.
Chris Wrenb6d4c282014-01-27 14:17:08 -0500848 */
Kenny Guyed131872014-04-30 03:02:21 +0100849 static boolean appWasRestored(Context context, Intent intent, UserHandleCompat user) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500850 final ContentResolver cr = context.getContentResolver();
851 final ComponentName component = intent.getComponent();
852 if (component == null) {
853 return false;
854 }
855 String componentName = component.flattenToString();
Chris Wren0e584b52014-05-12 16:07:25 -0400856 String shortName = component.flattenToShortString();
Kenny Guyed131872014-04-30 03:02:21 +0100857 long serialNumber = UserManagerCompat.getInstance(context)
858 .getSerialNumberForUser(user);
Chris Wren0e584b52014-05-12 16:07:25 -0400859 final String where = "(intent glob \"*component=" + componentName + "*\" or " +
860 "intent glob \"*component=" + shortName + "*\")" +
861 "and restored = 1 and profileId = " + serialNumber;
Chris Wrenb6d4c282014-01-27 14:17:08 -0500862 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Kenny Guyed131872014-04-30 03:02:21 +0100863 new String[]{"intent", "restored", "profileId"}, where, null, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500864 boolean result = false;
865 try {
866 result = c.moveToFirst();
867 } finally {
868 c.close();
869 }
870 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
871 return result;
872 }
873
874 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700875 * Returns an ItemInfo array containing all the items in the LauncherModel.
876 * The ItemInfo.id is not set through this function.
877 */
878 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
879 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
880 final ContentResolver cr = context.getContentResolver();
881 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
882 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
Kenny Guyed131872014-04-30 03:02:21 +0100883 LauncherSettings.Favorites.SCREEN,
884 LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
885 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY,
886 LauncherSettings.Favorites.PROFILE_ID }, null, null, null);
Winson Chungaafa03c2010-06-11 17:34:16 -0700887
888 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
889 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
890 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
891 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
892 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
893 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
894 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
Kenny Guyed131872014-04-30 03:02:21 +0100895 final int profileIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
896 UserManagerCompat userManager = UserManagerCompat.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700897 try {
898 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700899 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700900 item.cellX = c.getInt(cellXIndex);
901 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700902 item.spanX = Math.max(1, c.getInt(spanXIndex));
903 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 item.container = c.getInt(containerIndex);
905 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 item.screenId = c.getInt(screenIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100907 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +0100908 item.user = userManager.getUserForSerialNumber(serialNumber);
909 // Skip if user has been deleted.
910 if (item.user != null) {
911 items.add(item);
912 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700913 }
914 } catch (Exception e) {
915 items.clear();
916 } finally {
917 c.close();
918 }
919
920 return items;
921 }
922
923 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
925 */
926 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
927 final ContentResolver cr = context.getContentResolver();
928 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
929 "_id=? and (itemType=? or itemType=?)",
930 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700931 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 try {
934 if (c.moveToFirst()) {
935 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
936 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
937 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
938 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
939 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
940 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 FolderInfo folderInfo = null;
943 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700944 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
945 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700947 }
948
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 folderInfo.title = c.getString(titleIndex);
950 folderInfo.id = id;
951 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700953 folderInfo.cellX = c.getInt(cellXIndex);
954 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955
956 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 } finally {
959 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700960 }
961
962 return null;
963 }
964
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965 /**
966 * Add an item to the database in a specified container. Sets the container, screen, cellX and
967 * cellY fields of the item. Also assigns an ID to the item.
968 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700970 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 item.cellX = cellX;
973 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700974 // We store hotseat items in canonical form which is this orientation invariant position
975 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700980 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700981 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982
983 final ContentValues values = new ContentValues();
984 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100985 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986
Michael Jurka414300a2013-08-27 15:42:35 +0200987 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700988 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700990
Jason Monk8e19cf22014-03-20 15:06:57 -0400991 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700992 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700993 public void run() {
994 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
995 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700997 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700998 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400999 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 sBgItemsIdMap.put(item.id, item);
1001 switch (item.itemType) {
1002 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1003 sBgFolders.put(item.id, (FolderInfo) item);
1004 // Fall through
1005 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1006 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1007 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1008 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1009 sBgWorkspaceItems.add(item);
1010 } else {
1011 if (!sBgFolders.containsKey(item.container)) {
1012 // Adding an item to a folder that doesn't exist.
1013 String msg = "adding item: " + item + " to a folder that " +
1014 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001015 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001017 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 break;
1019 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1020 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1021 break;
1022 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001023 }
1024 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001025 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001027 }
1028
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001030 * Creates a new unique child id, for a given cell span across all layouts.
1031 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001032 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001033 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001034 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001035 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001036 }
1037
Winson Chungaafa03c2010-06-11 17:34:16 -07001038 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001039 * Removes the specified item from the database
1040 * @param context
1041 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001043 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001045 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001046
Michael Jurka83df1882011-08-31 20:59:26 -07001047 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001049 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050
1051 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001052 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 switch (item.itemType) {
1054 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1055 sBgFolders.remove(item.id);
1056 for (ItemInfo info: sBgItemsIdMap.values()) {
1057 if (info.container == item.id) {
1058 // We are deleting a folder which still contains items that
1059 // think they are contained by that folder.
1060 String msg = "deleting a folder (" + item + ") which still " +
1061 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001062 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001064 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001065 sBgWorkspaceItems.remove(item);
1066 break;
1067 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1068 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1069 sBgWorkspaceItems.remove(item);
1070 break;
1071 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1072 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1073 break;
1074 }
1075 sBgItemsIdMap.remove(item.id);
1076 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001077 }
1078 }
Michael Jurka83df1882011-08-31 20:59:26 -07001079 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001080 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 }
1082
1083 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001084 * Update the order of the workspace screens in the database. The array list contains
1085 * a list of screen ids in the order that they should appear.
1086 */
Winson Chungc9168342013-06-26 14:54:55 -07001087 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001088 // Log to disk
1089 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1090 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1091
Winson Chung64359a52013-07-08 17:17:08 -07001092 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 final ContentResolver cr = context.getContentResolver();
1094 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1095
1096 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001097 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 while (iter.hasNext()) {
1099 long id = iter.next();
1100 if (id < 0) {
1101 iter.remove();
1102 }
1103 }
1104
1105 Runnable r = new Runnable() {
1106 @Override
1107 public void run() {
Yura085c8532014-02-11 15:15:29 +00001108 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001110 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001111 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 for (int i = 0; i < count; i++) {
1113 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001114 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1116 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001117 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001118 }
Yura085c8532014-02-11 15:15:29 +00001119
1120 try {
1121 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1122 } catch (Exception ex) {
1123 throw new RuntimeException(ex);
1124 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001125
Winson Chungba9c37f2013-08-30 14:11:37 -07001126 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001127 sBgWorkspaceScreens.clear();
1128 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001129 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 }
1131 };
1132 runOnWorkerThread(r);
1133 }
1134
1135 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 * Remove the contents of the specified folder from the database
1137 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001138 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 final ContentResolver cr = context.getContentResolver();
1140
Michael Jurkac9d95c52011-08-29 14:03:34 -07001141 Runnable r = new Runnable() {
1142 public void run() {
1143 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001144 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001145 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001146 sBgItemsIdMap.remove(info.id);
1147 sBgFolders.remove(info.id);
1148 sBgDbIconCache.remove(info);
1149 sBgWorkspaceItems.remove(info);
1150 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001151
Michael Jurkac9d95c52011-08-29 14:03:34 -07001152 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1153 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001154 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001155 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001156 for (ItemInfo childInfo : info.contents) {
1157 sBgItemsIdMap.remove(childInfo.id);
1158 sBgDbIconCache.remove(childInfo);
1159 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001160 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001161 }
1162 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001163 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001164 }
1165
1166 /**
1167 * Set this as the current Launcher activity object for the loader.
1168 */
1169 public void initialize(Callbacks callbacks) {
1170 synchronized (mLock) {
1171 mCallbacks = new WeakReference<Callbacks>(callbacks);
1172 }
1173 }
1174
Kenny Guyed131872014-04-30 03:02:21 +01001175 @Override
1176 public void onPackageChanged(UserHandleCompat user, String packageName) {
1177 int op = PackageUpdatedTask.OP_UPDATE;
1178 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1179 user));
1180 }
1181
1182 @Override
1183 public void onPackageRemoved(UserHandleCompat user, String packageName) {
1184 int op = PackageUpdatedTask.OP_REMOVE;
1185 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1186 user));
1187 }
1188
1189 @Override
1190 public void onPackageAdded(UserHandleCompat user, String packageName) {
1191 int op = PackageUpdatedTask.OP_ADD;
1192 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1193 user));
1194 }
1195
1196 @Override
1197 public void onPackagesAvailable(UserHandleCompat user, String[] packageNames,
1198 boolean replacing) {
1199 if (!replacing) {
1200 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1201 user));
1202 if (mAppsCanBeOnRemoveableStorage) {
1203 // Only rebind if we support removable storage. It catches the
1204 // case where
1205 // apps on the external sd card need to be reloaded
1206 startLoaderFromBackground();
1207 }
1208 } else {
1209 // If we are replacing then just update the packages in the list
1210 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1211 packageNames, user));
1212 }
1213 }
1214
1215 @Override
1216 public void onPackagesUnavailable(UserHandleCompat user, String[] packageNames,
1217 boolean replacing) {
1218 if (!replacing) {
1219 enqueuePackageUpdated(new PackageUpdatedTask(
1220 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1221 user));
1222 }
1223
1224 }
1225
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001226 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001227 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1228 * ACTION_PACKAGE_CHANGED.
1229 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001230 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001231 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001232 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001233
Joe Onorato36115782010-06-17 13:28:48 -04001234 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001235 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001236 // If we have changed locale we need to clear out the labels in all apps/workspace.
1237 forceReload();
1238 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1239 // Check if configuration change was an mcc/mnc change which would affect app resources
1240 // and we would need to clear out the labels in all apps/workspace. Same handling as
1241 // above for ACTION_LOCALE_CHANGED
1242 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001243 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001244 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001245 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001246 forceReload();
1247 }
1248 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001249 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001250 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1251 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001252 if (mCallbacks != null) {
1253 Callbacks callbacks = mCallbacks.get();
1254 if (callbacks != null) {
1255 callbacks.bindSearchablesChanged();
1256 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001257 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001258 }
1259 }
1260
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001261 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001262 resetLoadedState(true, true);
1263
Reena Lee93f824a2011-09-23 17:20:28 -07001264 // Do this here because if the launcher activity is running it will be restarted.
1265 // If it's not running startLoaderFromBackground will merely tell it that it needs
1266 // to reload.
1267 startLoaderFromBackground();
1268 }
1269
Winson Chungf0c6ae02012-03-21 16:10:31 -07001270 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1271 synchronized (mLock) {
1272 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1273 // mWorkspaceLoaded to true later
1274 stopLoaderLocked();
1275 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1276 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1277 }
1278 }
1279
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001280 /**
1281 * When the launcher is in the background, it's possible for it to miss paired
1282 * configuration changes. So whenever we trigger the loader from the background
1283 * tell the launcher that it needs to re-run the loader when it comes back instead
1284 * of doing it now.
1285 */
1286 public void startLoaderFromBackground() {
1287 boolean runLoader = false;
1288 if (mCallbacks != null) {
1289 Callbacks callbacks = mCallbacks.get();
1290 if (callbacks != null) {
1291 // Only actually run the loader if they're not paused.
1292 if (!callbacks.setLoadOnResume()) {
1293 runLoader = true;
1294 }
1295 }
1296 }
1297 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001298 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001299 }
Joe Onorato36115782010-06-17 13:28:48 -04001300 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001301
Reena Lee93f824a2011-09-23 17:20:28 -07001302 // If there is already a loader task running, tell it to stop.
1303 // returns true if isLaunching() was true on the old task
1304 private boolean stopLoaderLocked() {
1305 boolean isLaunching = false;
1306 LoaderTask oldTask = mLoaderTask;
1307 if (oldTask != null) {
1308 if (oldTask.isLaunching()) {
1309 isLaunching = true;
1310 }
1311 oldTask.stopLocked();
1312 }
1313 return isLaunching;
1314 }
1315
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001316 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001317 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1318 }
1319
1320 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001321 synchronized (mLock) {
1322 if (DEBUG_LOADERS) {
1323 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001326 // Clear any deferred bind-runnables from the synchronized load process
1327 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001328 synchronized (mDeferredBindRunnables) {
1329 mDeferredBindRunnables.clear();
1330 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001331
Joe Onorato36115782010-06-17 13:28:48 -04001332 // Don't bother to start the thread if we know it's not going to do anything
1333 if (mCallbacks != null && mCallbacks.get() != null) {
1334 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001335 // also, don't downgrade isLaunching if we're already running
1336 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001337 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001338 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1339 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001340 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1341 } else {
1342 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1343 sWorker.post(mLoaderTask);
1344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001346 }
1347 }
1348
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001349 void bindRemainingSynchronousPages() {
1350 // Post the remaining side pages to be loaded
1351 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001352 Runnable[] deferredBindRunnables = null;
1353 synchronized (mDeferredBindRunnables) {
1354 deferredBindRunnables = mDeferredBindRunnables.toArray(
1355 new Runnable[mDeferredBindRunnables.size()]);
1356 mDeferredBindRunnables.clear();
1357 }
1358 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001359 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361 }
1362 }
1363
Joe Onorato36115782010-06-17 13:28:48 -04001364 public void stopLoader() {
1365 synchronized (mLock) {
1366 if (mLoaderTask != null) {
1367 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 }
1369 }
Joe Onorato36115782010-06-17 13:28:48 -04001370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371
Winson Chung76828c82013-08-19 15:43:29 -07001372 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1373 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1374 final ContentResolver contentResolver = context.getContentResolver();
1375 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1376 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1377 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1378
1379 try {
1380 final int idIndex = sc.getColumnIndexOrThrow(
1381 LauncherSettings.WorkspaceScreens._ID);
1382 final int rankIndex = sc.getColumnIndexOrThrow(
1383 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1384 while (sc.moveToNext()) {
1385 try {
1386 long screenId = sc.getLong(idIndex);
1387 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001388 orderedScreens.put(rank, screenId);
1389 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001390 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001391 }
1392 }
1393 } finally {
1394 sc.close();
1395 }
Winson Chunga90303b2013-11-15 13:05:06 -08001396
1397 // Log to disk
1398 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1399 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1400 for (Integer i : orderedScreens.keySet()) {
1401 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1402 }
1403 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1404 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001405 return orderedScreens;
1406 }
1407
Michael Jurkac57b7a82011-08-09 22:02:20 -07001408 public boolean isAllAppsLoaded() {
1409 return mAllAppsLoaded;
1410 }
1411
Winson Chung36a62fe2012-05-06 18:04:42 -07001412 boolean isLoadingWorkspace() {
1413 synchronized (mLock) {
1414 if (mLoaderTask != null) {
1415 return mLoaderTask.isLoadingWorkspace();
1416 }
1417 }
1418 return false;
1419 }
1420
Joe Onorato36115782010-06-17 13:28:48 -04001421 /**
1422 * Runnable for the thread that loads the contents of the launcher:
1423 * - workspace icons
1424 * - widgets
1425 * - all apps icons
1426 */
1427 private class LoaderTask implements Runnable {
1428 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001429 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001430 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001431 private boolean mStopped;
1432 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001433 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001434
Winson Chungc3eecff2011-07-11 17:44:15 -07001435 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001436
Dan Sandlerd5024042014-01-09 15:01:33 -05001437 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001438 mContext = context;
1439 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001440 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001441 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442 }
1443
Joe Onorato36115782010-06-17 13:28:48 -04001444 boolean isLaunching() {
1445 return mIsLaunching;
1446 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447
Winson Chung36a62fe2012-05-06 18:04:42 -07001448 boolean isLoadingWorkspace() {
1449 return mIsLoadingAndBindingWorkspace;
1450 }
1451
Winson Chungc763c4e2013-07-19 13:49:06 -07001452 /** Returns whether this is an upgrade path */
1453 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001454 mIsLoadingAndBindingWorkspace = true;
1455
Joe Onorato36115782010-06-17 13:28:48 -04001456 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001457 if (DEBUG_LOADERS) {
1458 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001460
Winson Chungc763c4e2013-07-19 13:49:06 -07001461 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001462 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001463 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001464 synchronized (LoaderTask.this) {
1465 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001466 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001467 }
1468 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 }
1470 }
1471
Joe Onorato36115782010-06-17 13:28:48 -04001472 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001473 bindWorkspace(-1, isUpgradePath);
1474 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001475 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001476
Joe Onorato36115782010-06-17 13:28:48 -04001477 private void waitForIdle() {
1478 // Wait until the either we're stopped or the other threads are done.
1479 // This way we don't start loading all apps until the workspace has settled
1480 // down.
1481 synchronized (LoaderTask.this) {
1482 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001483
Joe Onorato36115782010-06-17 13:28:48 -04001484 mHandler.postIdle(new Runnable() {
1485 public void run() {
1486 synchronized (LoaderTask.this) {
1487 mLoadAndBindStepFinished = true;
1488 if (DEBUG_LOADERS) {
1489 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001490 }
Joe Onorato36115782010-06-17 13:28:48 -04001491 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001492 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494 });
1495
Michael Jurkac7700af2013-05-14 20:17:58 +02001496 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001497 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001498 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1499 // wait no longer than 1sec at a time
1500 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001501 } catch (InterruptedException ex) {
1502 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001503 }
1504 }
Joe Onorato36115782010-06-17 13:28:48 -04001505 if (DEBUG_LOADERS) {
1506 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001507 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001508 + "ms for previous step to finish binding");
1509 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001510 }
Joe Onorato36115782010-06-17 13:28:48 -04001511 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001512
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001514 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001515 // Ensure that we have a valid page index to load synchronously
1516 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1517 "valid page index");
1518 }
1519 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1520 // Ensure that we don't try and bind a specified page when the pages have not been
1521 // loaded already (we should load everything asynchronously in that case)
1522 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1523 }
1524 synchronized (mLock) {
1525 if (mIsLoaderTaskRunning) {
1526 // Ensure that we are never running the background loading at this point since
1527 // we also touch the background collections
1528 throw new RuntimeException("Error! Background loading is already running");
1529 }
1530 }
1531
1532 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1533 // data structures, we can't allow any other thread to touch that data, but because
1534 // this call is synchronous, we can get away with not locking).
1535
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001536 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001537 // operations from the previous activity. We need to ensure that all queued operations
1538 // are executed before any synchronous binding work is done.
1539 mHandler.flush();
1540
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001541 // Divide the set of loaded items into those that we are binding synchronously, and
1542 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001543 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001544 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1545 // arise from that.
1546 onlyBindAllApps();
1547 }
1548
Joe Onorato36115782010-06-17 13:28:48 -04001549 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001550 boolean isUpgrade = false;
1551
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001552 synchronized (mLock) {
1553 mIsLoaderTaskRunning = true;
1554 }
Joe Onorato36115782010-06-17 13:28:48 -04001555 // Optimize for end-user experience: if the Launcher is up and // running with the
1556 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1557 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001558 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001559 // Elevate priority when Home launches for the first time to avoid
1560 // starving at boot time. Staring at a blank home is not cool.
1561 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001562 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1563 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001564 android.os.Process.setThreadPriority(mIsLaunching
1565 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1566 }
Winson Chung64359a52013-07-08 17:17:08 -07001567 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001568 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001569
Joe Onorato36115782010-06-17 13:28:48 -04001570 if (mStopped) {
1571 break keep_running;
1572 }
1573
1574 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1575 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001576 synchronized (mLock) {
1577 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001578 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001579 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1580 }
1581 }
Joe Onorato36115782010-06-17 13:28:48 -04001582 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001583
1584 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001585 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1586 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001587
1588 // Restore the default thread priority after we are done loading items
1589 synchronized (mLock) {
1590 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1591 }
Joe Onorato36115782010-06-17 13:28:48 -04001592 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001593
Winson Chungaac01e12011-08-17 10:37:13 -07001594 // Update the saved icons if necessary
1595 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001596 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001597 for (Object key : sBgDbIconCache.keySet()) {
1598 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1599 }
1600 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001601 }
Winson Chungaac01e12011-08-17 10:37:13 -07001602
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001603 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001604 // Ensure that all the applications that are in the system are
1605 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001606 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001607 verifyApplications();
1608 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001609 }
1610
Joe Onorato36115782010-06-17 13:28:48 -04001611 // Clear out this reference, otherwise we end up holding it until all of the
1612 // callback runnables are done.
1613 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001614
Joe Onorato36115782010-06-17 13:28:48 -04001615 synchronized (mLock) {
1616 // If we are still the last one to be scheduled, remove ourselves.
1617 if (mLoaderTask == this) {
1618 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001620 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
1623
1624 public void stopLocked() {
1625 synchronized (LoaderTask.this) {
1626 mStopped = true;
1627 this.notify();
1628 }
1629 }
1630
1631 /**
1632 * Gets the callbacks object. If we've been stopped, or if the launcher object
1633 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1634 * object that was around when the deferred message was scheduled, and if there's
1635 * a new Callbacks object around then also return null. This will save us from
1636 * calling onto it with data that will be ignored.
1637 */
1638 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1639 synchronized (mLock) {
1640 if (mStopped) {
1641 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001642 }
Joe Onorato36115782010-06-17 13:28:48 -04001643
1644 if (mCallbacks == null) {
1645 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001646 }
Joe Onorato36115782010-06-17 13:28:48 -04001647
1648 final Callbacks callbacks = mCallbacks.get();
1649 if (callbacks != oldCallbacks) {
1650 return null;
1651 }
1652 if (callbacks == null) {
1653 Log.w(TAG, "no mCallbacks");
1654 return null;
1655 }
1656
1657 return callbacks;
1658 }
1659 }
1660
Winson Chungc763c4e2013-07-19 13:49:06 -07001661 private void verifyApplications() {
1662 final Context context = mApp.getContext();
1663
1664 // Cross reference all the applications in our apps list with items in the workspace
1665 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001666 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001667 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001668 for (AppInfo app : mBgAllAppsList.data) {
Kenny Guyed131872014-04-30 03:02:21 +01001669 tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
Winson Chungc763c4e2013-07-19 13:49:06 -07001670 if (tmpInfos.isEmpty()) {
1671 // We are missing an application icon, so add this to the workspace
1672 added.add(app);
1673 // This is a rare event, so lets log it
1674 Log.e(TAG, "Missing Application on load: " + app);
1675 }
1676 }
1677 }
1678 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001679 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001680 }
1681 }
1682
Joe Onorato36115782010-06-17 13:28:48 -04001683 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001684 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001685 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001686 LauncherAppState app = LauncherAppState.getInstance();
1687 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001688 final int countX = (int) grid.numColumns;
1689 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001690
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001692 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001693 // Return early if we detect that an item is under the hotseat button
1694 if (mCallbacks == null ||
1695 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001696 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001697 Log.e(TAG, "Error loading shortcut into hotseat " + item
1698 + " into position (" + item.screenId + ":" + item.cellX + ","
1699 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001700 return false;
1701 }
1702
Dan Sandler295ae182013-12-10 16:05:47 -05001703 final ItemInfo[][] hotseatItems =
1704 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1705
Adam Cohenae4409d2013-11-26 10:34:59 -08001706 if (item.screenId >= grid.numHotseatIcons) {
1707 Log.e(TAG, "Error loading shortcut " + item
1708 + " into hotseat position " + item.screenId
1709 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1710 + ")");
1711 return false;
1712 }
1713
Dan Sandler295ae182013-12-10 16:05:47 -05001714 if (hotseatItems != null) {
1715 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001716 Log.e(TAG, "Error loading shortcut into hotseat " + item
1717 + " into position (" + item.screenId + ":" + item.cellX + ","
1718 + item.cellY + ") occupied by "
1719 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1720 [(int) item.screenId][0]);
1721 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001722 } else {
1723 hotseatItems[(int) item.screenId][0] = item;
1724 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001725 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001726 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001727 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001728 items[(int) item.screenId][0] = item;
1729 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001730 return true;
1731 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001732 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1733 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001734 return true;
1735 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001736
Adam Cohendcd297f2013-06-18 13:13:40 -07001737 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001738 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001739 occupied.put(item.screenId, items);
1740 }
1741
Dan Sandler295ae182013-12-10 16:05:47 -05001742 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001743 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1744 item.cellX < 0 || item.cellY < 0 ||
1745 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1746 Log.e(TAG, "Error loading shortcut " + item
1747 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1748 + item.cellX + "," + item.cellY
1749 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1750 return false;
1751 }
1752
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001753 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001754 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1755 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001756 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001757 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001758 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001759 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001760 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001761 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001762 return false;
1763 }
1764 }
1765 }
1766 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1767 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001768 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001769 }
1770 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001771
Joe Onorato36115782010-06-17 13:28:48 -04001772 return true;
1773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774
Winson Chungba9c37f2013-08-30 14:11:37 -07001775 /** Clears all the sBg data structures */
1776 private void clearSBgDataStructures() {
1777 synchronized (sBgLock) {
1778 sBgWorkspaceItems.clear();
1779 sBgAppWidgets.clear();
1780 sBgFolders.clear();
1781 sBgItemsIdMap.clear();
1782 sBgDbIconCache.clear();
1783 sBgWorkspaceScreens.clear();
1784 }
1785 }
1786
Dan Sandlerd5024042014-01-09 15:01:33 -05001787 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001788 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001789 // Log to disk
1790 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1791
Joe Onorato36115782010-06-17 13:28:48 -04001792 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Joe Onorato36115782010-06-17 13:28:48 -04001794 final Context context = mContext;
1795 final ContentResolver contentResolver = context.getContentResolver();
1796 final PackageManager manager = context.getPackageManager();
1797 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1798 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001799
Winson Chung892c74d2013-08-22 16:15:50 -07001800 LauncherAppState app = LauncherAppState.getInstance();
1801 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1802 int countX = (int) grid.numColumns;
1803 int countY = (int) grid.numRows;
1804
Dan Sandlerd5024042014-01-09 15:01:33 -05001805 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1806 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1807 LauncherAppState.getLauncherProvider().deleteDatabase();
1808 }
1809
1810 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1811 // append the user's Launcher2 shortcuts
1812 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1813 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1814 } else {
1815 // Make sure the default workspace is loaded
1816 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1817 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1818 }
Adam Cohene25af792013-06-06 23:08:25 -07001819
Winson Chungc763c4e2013-07-19 13:49:06 -07001820 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001821 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001822 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001823
Winson Chung9f9f00b2013-11-15 13:27:00 -08001824 // Log to disk
1825 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001826
Winson Chung2abf94d2012-07-18 18:16:38 -07001827 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001828 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001831 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001832 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001833 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001834 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001835
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 // +1 for the hotseat (it can be larger than the workspace)
1837 // Load workspace in reverse order to ensure that latest items are loaded first (and
1838 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001839 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 try {
1842 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1843 final int intentIndex = c.getColumnIndexOrThrow
1844 (LauncherSettings.Favorites.INTENT);
1845 final int titleIndex = c.getColumnIndexOrThrow
1846 (LauncherSettings.Favorites.TITLE);
1847 final int iconTypeIndex = c.getColumnIndexOrThrow(
1848 LauncherSettings.Favorites.ICON_TYPE);
1849 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1850 final int iconPackageIndex = c.getColumnIndexOrThrow(
1851 LauncherSettings.Favorites.ICON_PACKAGE);
1852 final int iconResourceIndex = c.getColumnIndexOrThrow(
1853 LauncherSettings.Favorites.ICON_RESOURCE);
1854 final int containerIndex = c.getColumnIndexOrThrow(
1855 LauncherSettings.Favorites.CONTAINER);
1856 final int itemTypeIndex = c.getColumnIndexOrThrow(
1857 LauncherSettings.Favorites.ITEM_TYPE);
1858 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1859 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001860 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1861 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 final int screenIndex = c.getColumnIndexOrThrow(
1863 LauncherSettings.Favorites.SCREEN);
1864 final int cellXIndex = c.getColumnIndexOrThrow
1865 (LauncherSettings.Favorites.CELLX);
1866 final int cellYIndex = c.getColumnIndexOrThrow
1867 (LauncherSettings.Favorites.CELLY);
1868 final int spanXIndex = c.getColumnIndexOrThrow
1869 (LauncherSettings.Favorites.SPANX);
1870 final int spanYIndex = c.getColumnIndexOrThrow(
1871 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001872 final int restoredIndex = c.getColumnIndexOrThrow(
1873 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001874 final int profileIdIndex = c.getColumnIndexOrThrow(
1875 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1877 //final int displayModeIndex = c.getColumnIndexOrThrow(
1878 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 ShortcutInfo info;
1881 String intentDescription;
1882 LauncherAppWidgetInfo appWidgetInfo;
1883 int container;
1884 long id;
1885 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001886 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001889 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 try {
1891 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001892 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001893
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 switch (itemType) {
1895 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1896 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001897 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 intentDescription = c.getString(intentIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001899 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001900 user = mUserManager.getUserForSerialNumber(serialNumber);
1901 if (user == null) {
1902 // User has been deleted remove the item.
1903 itemsToRemove.add(id);
1904 continue;
1905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 try {
1907 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001908 ComponentName cn = intent.getComponent();
Kenny Guyed131872014-04-30 03:02:21 +01001909 if (cn != null && !isValidPackageActivity(context, cn, user)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001910 if (restored) {
1911 // might be installed later
1912 Launcher.addDumpLog(TAG,
1913 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001914 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001915 if (!mAppsCanBeOnRemoveableStorage) {
1916 // Log the invalid package, and remove it
1917 Launcher.addDumpLog(TAG,
1918 "Invalid package removed: " + cn, true);
1919 itemsToRemove.add(id);
1920 } else {
1921 // If apps can be on external storage, then we just
1922 // leave them for the user to remove (maybe add
1923 // visual treatment to it)
1924 Launcher.addDumpLog(TAG,
1925 "Invalid package found: " + cn, true);
1926 }
1927 continue;
Winson Chungee055712013-07-30 14:46:24 -07001928 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001929 } else if (restored) {
1930 // no special handling necessary for this restored item
1931 restoredRows.add(id);
1932 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001933 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001934 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001935 Launcher.addDumpLog(TAG,
1936 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 continue;
1938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939
Chris Wrenf4d08112014-01-16 18:13:56 -05001940 if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001941 if (user.equals(UserHandleCompat.myUserHandle())) {
1942 Launcher.addDumpLog(TAG,
1943 "constructing info for partially restored package",
1944 true);
1945 info = getRestoredItemInfo(c, titleIndex, intent);
1946 intent = getRestoredItemIntent(c, context, intent);
1947 } else {
1948 // Don't restore items for other profiles.
1949 itemsToRemove.add(id);
1950 continue;
1951 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001952 } else if (itemType ==
1953 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Kenny Guyed131872014-04-30 03:02:21 +01001954 info = getShortcutInfo(manager, intent, user, context, c, iconIndex,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001955 titleIndex, mLabelCache);
1956 } else {
1957 info = getShortcutInfo(c, context, iconTypeIndex,
1958 iconPackageIndex, iconResourceIndex, iconIndex,
1959 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001960
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 // App shortcuts that used to be automatically added to Launcher
1962 // didn't always have the correct intent flags set, so do that
1963 // here
1964 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001965 intent.getCategories() != null &&
1966 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001967 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 intent.addFlags(
1969 Intent.FLAG_ACTIVITY_NEW_TASK |
1970 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1971 }
Michael Jurka96879562012-03-22 05:54:33 -07001972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001975 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 container = c.getInt(containerIndex);
1978 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001979 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 info.cellX = c.getInt(cellXIndex);
1981 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001982 info.spanX = 1;
1983 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001984 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Adam Cohenae4409d2013-11-26 10:34:59 -08001985
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001987 deleteOnInvalidPlacement.set(false);
1988 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1989 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001990 itemsToRemove.add(id);
1991 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 break;
1993 }
1994
1995 switch (container) {
1996 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1997 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1998 sBgWorkspaceItems.add(info);
1999 break;
2000 default:
2001 // Item is in a user folder
2002 FolderInfo folderInfo =
2003 findOrMakeFolder(sBgFolders, container);
2004 folderInfo.add(info);
2005 break;
2006 }
2007 sBgItemsIdMap.put(info.id, info);
2008
2009 // now that we've loaded everthing re-save it with the
2010 // icon in case it disappears somehow.
2011 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002012 } else {
2013 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 }
2015 break;
2016
2017 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2018 id = c.getLong(idIndex);
2019 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2020
2021 folderInfo.title = c.getString(titleIndex);
2022 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002023 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002025 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 folderInfo.cellX = c.getInt(cellXIndex);
2027 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002028 folderInfo.spanX = 1;
2029 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030
Daniel Sandler8802e962010-05-26 16:28:16 -04002031 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002032 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002033 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002034 deleteOnInvalidPlacement)) {
2035 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002036 itemsToRemove.add(id);
2037 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002038 break;
2039 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002040
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2043 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2044 sBgWorkspaceItems.add(folderInfo);
2045 break;
Joe Onorato36115782010-06-17 13:28:48 -04002046 }
Joe Onorato17a89222011-02-08 17:26:11 -08002047
Chris Wrenf4d08112014-01-16 18:13:56 -05002048 if (restored) {
2049 // no special handling required for restored folders
2050 restoredRows.add(id);
2051 }
2052
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2054 sBgFolders.put(folderInfo.id, folderInfo);
2055 break;
2056
2057 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2058 // Read all Launcher-specific widget details
2059 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002060 String savedProvider = c.getString(appWidgetProviderIndex);
2061
Joe Onorato36115782010-06-17 13:28:48 -04002062 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 final AppWidgetProviderInfo provider =
2065 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002066
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002067 if (!isSafeMode && (provider == null || provider.provider == null ||
2068 provider.provider.getPackageName() == null)) {
2069 String log = "Deleting widget that isn't installed anymore: id="
2070 + id + " appWidgetId=" + appWidgetId;
2071 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002072 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 itemsToRemove.add(id);
2074 } else {
2075 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2076 provider.provider);
2077 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002078 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 appWidgetInfo.cellX = c.getInt(cellXIndex);
2080 appWidgetInfo.cellY = c.getInt(cellYIndex);
2081 appWidgetInfo.spanX = c.getInt(spanXIndex);
2082 appWidgetInfo.spanY = c.getInt(spanYIndex);
2083 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2084 appWidgetInfo.minSpanX = minSpan[0];
2085 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002086
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 container = c.getInt(containerIndex);
2088 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2089 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2090 Log.e(TAG, "Widget found where container != " +
2091 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2092 continue;
2093 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094
Adam Cohene25af792013-06-06 23:08:25 -07002095 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002097 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002098 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002099 deleteOnInvalidPlacement)) {
2100 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002101 itemsToRemove.add(id);
2102 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 break;
2104 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002105 String providerName = provider.provider.flattenToString();
2106 if (!providerName.equals(savedProvider)) {
2107 ContentValues values = new ContentValues();
2108 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2109 providerName);
2110 String where = BaseColumns._ID + "= ?";
2111 String[] args = {Integer.toString(c.getInt(idIndex))};
2112 contentResolver.update(contentUri, values, where, args);
2113 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2115 sBgAppWidgets.add(appWidgetInfo);
2116 }
Joe Onorato36115782010-06-17 13:28:48 -04002117 break;
2118 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002120 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002121 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 }
2123 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002124 if (c != null) {
2125 c.close();
2126 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 }
2128
Winson Chungba9c37f2013-08-30 14:11:37 -07002129 // Break early if we've stopped loading
2130 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002131 clearSBgDataStructures();
2132 return false;
2133 }
2134
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002135 if (itemsToRemove.size() > 0) {
2136 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002137 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 // Remove dead items
2139 for (long id : itemsToRemove) {
2140 if (DEBUG_LOADERS) {
2141 Log.d(TAG, "Removed id = " + id);
2142 }
2143 // Don't notify content observers
2144 try {
2145 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2146 null, null);
2147 } catch (RemoteException e) {
2148 Log.w(TAG, "Could not remove id = " + id);
2149 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002150 }
2151 }
2152
Chris Wrenf4d08112014-01-16 18:13:56 -05002153 if (restoredRows.size() > 0) {
2154 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2155 LauncherSettings.Favorites.CONTENT_URI);
2156 // Update restored items that no longer require special handling
2157 try {
2158 StringBuilder selectionBuilder = new StringBuilder();
2159 selectionBuilder.append(LauncherSettings.Favorites._ID);
2160 selectionBuilder.append(" IN (");
2161 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2162 selectionBuilder.append(")");
2163 ContentValues values = new ContentValues();
2164 values.put(LauncherSettings.Favorites.RESTORED, 0);
2165 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2166 values, selectionBuilder.toString(), null);
2167 } catch (RemoteException e) {
2168 Log.w(TAG, "Could not update restored rows");
2169 }
2170 }
2171
Winson Chungc763c4e2013-07-19 13:49:06 -07002172 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002173 long maxScreenId = 0;
2174 // If we're importing we use the old screen order.
2175 for (ItemInfo item: sBgItemsIdMap.values()) {
2176 long screenId = item.screenId;
2177 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2178 !sBgWorkspaceScreens.contains(screenId)) {
2179 sBgWorkspaceScreens.add(screenId);
2180 if (screenId > maxScreenId) {
2181 maxScreenId = screenId;
2182 }
2183 }
2184 }
2185 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002186 // Log to disk
2187 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2188 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2189 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002190
Michael Jurka414300a2013-08-27 15:42:35 +02002191 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002193
2194 // Update the max item id after we load an old db
2195 long maxItemId = 0;
2196 // If we're importing we use the old screen order.
2197 for (ItemInfo item: sBgItemsIdMap.values()) {
2198 maxItemId = Math.max(maxItemId, item.id);
2199 }
Michael Jurka414300a2013-08-27 15:42:35 +02002200 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002202 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2203 for (Integer i : orderedScreens.keySet()) {
2204 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002205 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002206 // Log to disk
2207 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2208 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002209
2210 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002211 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 for (ItemInfo item: sBgItemsIdMap.values()) {
2213 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2215 unusedScreens.contains(screenId)) {
2216 unusedScreens.remove(screenId);
2217 }
2218 }
2219
2220 // If there are any empty screens remove them, and update.
2221 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002222 // Log to disk
2223 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2224 TextUtils.join(", ", unusedScreens), true);
2225
Winson Chung933bae62013-08-29 11:42:30 -07002226 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2228 }
2229 }
2230
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 if (DEBUG_LOADERS) {
2232 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2233 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002235 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002237
Daniel Sandler566da102013-06-25 23:43:45 -04002238 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002239 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002241 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 line += " | ";
2243 }
Winson Chung892c74d2013-08-22 16:15:50 -07002244 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002245 ItemInfo[][] screen = occupied.get(screenId);
2246 if (x < screen.length && y < screen[x].length) {
2247 line += (screen[x][y] != null) ? "#" : ".";
2248 } else {
2249 line += "!";
2250 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 }
Joe Onorato36115782010-06-17 13:28:48 -04002252 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002254 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 }
Joe Onorato36115782010-06-17 13:28:48 -04002256 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002257 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002258 }
2259
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 /** Filters the set of items who are directly or indirectly (via another container) on the
2261 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002262 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 ArrayList<ItemInfo> allWorkspaceItems,
2264 ArrayList<ItemInfo> currentScreenItems,
2265 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002266 // Purge any null ItemInfos
2267 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2268 while (iter.hasNext()) {
2269 ItemInfo i = iter.next();
2270 if (i == null) {
2271 iter.remove();
2272 }
2273 }
2274
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 // Order the set of items by their containers first, this allows use to walk through the
2276 // list sequentially, build up a list of containers that are in the specified screen,
2277 // as well as all items in those containers.
2278 Set<Long> itemsOnScreen = new HashSet<Long>();
2279 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2280 @Override
2281 public int compare(ItemInfo lhs, ItemInfo rhs) {
2282 return (int) (lhs.container - rhs.container);
2283 }
2284 });
2285 for (ItemInfo info : allWorkspaceItems) {
2286 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002287 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 currentScreenItems.add(info);
2289 itemsOnScreen.add(info.id);
2290 } else {
2291 otherScreenItems.add(info);
2292 }
2293 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2294 currentScreenItems.add(info);
2295 itemsOnScreen.add(info.id);
2296 } else {
2297 if (itemsOnScreen.contains(info.container)) {
2298 currentScreenItems.add(info);
2299 itemsOnScreen.add(info.id);
2300 } else {
2301 otherScreenItems.add(info);
2302 }
2303 }
2304 }
2305 }
2306
2307 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002308 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309 ArrayList<LauncherAppWidgetInfo> appWidgets,
2310 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2311 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312
2313 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002314 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002316 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 currentScreenWidgets.add(widget);
2318 } else {
2319 otherScreenWidgets.add(widget);
2320 }
2321 }
2322 }
2323
2324 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002325 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 HashMap<Long, ItemInfo> itemsIdMap,
2327 HashMap<Long, FolderInfo> folders,
2328 HashMap<Long, FolderInfo> currentScreenFolders,
2329 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330
2331 for (long id : folders.keySet()) {
2332 ItemInfo info = itemsIdMap.get(id);
2333 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002334 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002336 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002337 currentScreenFolders.put(id, folder);
2338 } else {
2339 otherScreenFolders.put(id, folder);
2340 }
2341 }
2342 }
2343
2344 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2345 * right) */
2346 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002347 final LauncherAppState app = LauncherAppState.getInstance();
2348 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 // XXX: review this
2350 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002351 @Override
2352 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002353 int cellCountX = (int) grid.numColumns;
2354 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002355 int screenOffset = cellCountX * cellCountY;
2356 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002358 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002359 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002360 rhs.cellY * cellCountX + rhs.cellX);
2361 return (int) (lr - rr);
2362 }
2363 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002365
Adam Cohendcd297f2013-06-18 13:13:40 -07002366 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2367 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002368 final Runnable r = new Runnable() {
2369 @Override
2370 public void run() {
2371 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2372 if (callbacks != null) {
2373 callbacks.bindScreens(orderedScreens);
2374 }
2375 }
2376 };
2377 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2378 }
2379
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002380 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2381 final ArrayList<ItemInfo> workspaceItems,
2382 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2383 final HashMap<Long, FolderInfo> folders,
2384 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002385
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002386 final boolean postOnMainThread = (deferredBindRunnables != null);
2387
2388 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002389 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002390 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002391 final int start = i;
2392 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002393 final Runnable r = new Runnable() {
2394 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002396 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002398 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2399 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 }
2401 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002402 };
2403 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002404 synchronized (deferredBindRunnables) {
2405 deferredBindRunnables.add(r);
2406 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002408 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 }
Joe Onorato36115782010-06-17 13:28:48 -04002410 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002411
2412 // Bind the folders
2413 if (!folders.isEmpty()) {
2414 final Runnable r = new Runnable() {
2415 public void run() {
2416 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2417 if (callbacks != null) {
2418 callbacks.bindFolders(folders);
2419 }
2420 }
2421 };
2422 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002423 synchronized (deferredBindRunnables) {
2424 deferredBindRunnables.add(r);
2425 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002427 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002428 }
2429 }
2430
2431 // Bind the widgets, one at a time
2432 N = appWidgets.size();
2433 for (int i = 0; i < N; i++) {
2434 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2435 final Runnable r = new Runnable() {
2436 public void run() {
2437 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2438 if (callbacks != null) {
2439 callbacks.bindAppWidget(widget);
2440 }
2441 }
2442 };
2443 if (postOnMainThread) {
2444 deferredBindRunnables.add(r);
2445 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002446 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002447 }
2448 }
2449 }
2450
2451 /**
2452 * Binds all loaded data to actual views on the main thread.
2453 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002454 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 final long t = SystemClock.uptimeMillis();
2456 Runnable r;
2457
2458 // Don't use these two variables in any of the callback runnables.
2459 // Otherwise we hold a reference to them.
2460 final Callbacks oldCallbacks = mCallbacks.get();
2461 if (oldCallbacks == null) {
2462 // This launcher has exited and nobody bothered to tell us. Just bail.
2463 Log.w(TAG, "LoaderTask running with no launcher");
2464 return;
2465 }
2466
Winson Chung9b9fb962013-11-15 15:39:34 -08002467 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002468 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2469 ArrayList<LauncherAppWidgetInfo> appWidgets =
2470 new ArrayList<LauncherAppWidgetInfo>();
2471 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2472 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002473 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002474 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002475 workspaceItems.addAll(sBgWorkspaceItems);
2476 appWidgets.addAll(sBgAppWidgets);
2477 folders.putAll(sBgFolders);
2478 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002479 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002480 }
2481
Derek Prothro7aff3992013-12-10 14:00:37 -05002482 final boolean isLoadingSynchronously =
2483 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002484 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002485 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002486 if (currScreen >= orderedScreenIds.size()) {
2487 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002488 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002489 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002490 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002491 final long currentScreenId = currentScreen < 0
2492 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002493
2494 // Load all the items that are on the current page first (and in the process, unbind
2495 // all the existing workspace items before we call startBinding() below.
2496 unbindWorkspaceItemsOnMainThread();
2497
2498 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002499 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2500 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2501 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2502 new ArrayList<LauncherAppWidgetInfo>();
2503 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2504 new ArrayList<LauncherAppWidgetInfo>();
2505 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2506 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2507
Winson Chung9b9fb962013-11-15 15:39:34 -08002508 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002510 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002512 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513 otherFolders);
2514 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2515 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2516
2517 // Tell the workspace that we're about to start binding items
2518 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002519 public void run() {
2520 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2521 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002522 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002523 }
2524 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525 };
Winson Chung81b52252012-08-27 15:34:29 -07002526 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002527
Adam Cohendcd297f2013-06-18 13:13:40 -07002528 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2529
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002530 // Load items on the current page
2531 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2532 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002533 if (isLoadingSynchronously) {
2534 r = new Runnable() {
2535 public void run() {
2536 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002537 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002538 callbacks.onPageBoundSynchronously(currentScreen);
2539 }
2540 }
2541 };
Winson Chung81b52252012-08-27 15:34:29 -07002542 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002543 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002544
Winson Chung4a2afa32012-07-19 14:53:05 -07002545 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2546 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002547 synchronized (mDeferredBindRunnables) {
2548 mDeferredBindRunnables.clear();
2549 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002551 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002552
2553 // Tell the workspace that we're done binding items
2554 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002555 public void run() {
2556 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2557 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002558 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002559 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002560
Winson Chung98e030b2012-05-07 16:01:11 -07002561 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002562 if (DEBUG_LOADERS) {
2563 Log.d(TAG, "bound workspace in "
2564 + (SystemClock.uptimeMillis()-t) + "ms");
2565 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002566
2567 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002568 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002570 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002571 synchronized (mDeferredBindRunnables) {
2572 mDeferredBindRunnables.add(r);
2573 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002574 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002575 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002576 }
Joe Onorato36115782010-06-17 13:28:48 -04002577 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002578
Joe Onorato36115782010-06-17 13:28:48 -04002579 private void loadAndBindAllApps() {
2580 if (DEBUG_LOADERS) {
2581 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2582 }
2583 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002584 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002585 synchronized (LoaderTask.this) {
2586 if (mStopped) {
2587 return;
2588 }
2589 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002590 }
Joe Onorato36115782010-06-17 13:28:48 -04002591 } else {
2592 onlyBindAllApps();
2593 }
2594 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002595
Joe Onorato36115782010-06-17 13:28:48 -04002596 private void onlyBindAllApps() {
2597 final Callbacks oldCallbacks = mCallbacks.get();
2598 if (oldCallbacks == null) {
2599 // This launcher has exited and nobody bothered to tell us. Just bail.
2600 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2601 return;
2602 }
2603
2604 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002605 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002606 final ArrayList<AppInfo> list
2607 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002608 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002609 public void run() {
2610 final long t = SystemClock.uptimeMillis();
2611 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2612 if (callbacks != null) {
2613 callbacks.bindAllApplications(list);
2614 }
2615 if (DEBUG_LOADERS) {
2616 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2617 + (SystemClock.uptimeMillis()-t) + "ms");
2618 }
2619 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002620 };
2621 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002622 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002623 r.run();
2624 } else {
2625 mHandler.post(r);
2626 }
Joe Onorato36115782010-06-17 13:28:48 -04002627 }
2628
Winson Chung64359a52013-07-08 17:17:08 -07002629 private void loadAllApps() {
2630 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002631
Joe Onorato36115782010-06-17 13:28:48 -04002632 final Callbacks oldCallbacks = mCallbacks.get();
2633 if (oldCallbacks == null) {
2634 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002635 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002636 return;
2637 }
2638
2639 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2640 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2641
Kenny Guyed131872014-04-30 03:02:21 +01002642 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2643
Winson Chung64359a52013-07-08 17:17:08 -07002644 // Clear the list of apps
2645 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002646 for (UserHandleCompat user : profiles) {
2647 // Query for the set of apps
2648 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2649 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2650 if (DEBUG_LOADERS) {
2651 Log.d(TAG, "getActivityList took "
2652 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2653 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2654 }
2655 // Fail if we don't have any apps
2656 if (apps == null || apps.isEmpty()) {
2657 return;
2658 }
2659 // Sort the applications by name
2660 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2661 Collections.sort(apps,
2662 new LauncherModel.ShortcutNameComparator(mLabelCache));
2663 if (DEBUG_LOADERS) {
2664 Log.d(TAG, "sort took "
2665 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2666 }
Joe Onorato36115782010-06-17 13:28:48 -04002667
Kenny Guyed131872014-04-30 03:02:21 +01002668 // Create the ApplicationInfos
2669 for (int i = 0; i < apps.size(); i++) {
2670 LauncherActivityInfoCompat app = apps.get(i);
2671 // This builds the icon bitmaps.
2672 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2673 }
Winson Chung64359a52013-07-08 17:17:08 -07002674 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002675 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002676 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2677 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002678
2679 // Post callback on main thread
2680 mHandler.post(new Runnable() {
2681 public void run() {
2682 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002683 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002684 if (callbacks != null) {
2685 callbacks.bindAllApplications(added);
2686 if (DEBUG_LOADERS) {
2687 Log.d(TAG, "bound " + added.size() + " apps in "
2688 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2689 }
2690 } else {
2691 Log.i(TAG, "not binding apps: no Launcher activity");
2692 }
2693 }
2694 });
2695
Joe Onorato36115782010-06-17 13:28:48 -04002696 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002697 Log.d(TAG, "Icons processed in "
2698 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002699 }
2700 }
2701
2702 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002703 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002704 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2705 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2706 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2707 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2708 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2709 }
Joe Onorato36115782010-06-17 13:28:48 -04002710 }
2711 }
2712
2713 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002714 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002715 }
2716
2717 private class PackageUpdatedTask implements Runnable {
2718 int mOp;
2719 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002720 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002721
2722 public static final int OP_NONE = 0;
2723 public static final int OP_ADD = 1;
2724 public static final int OP_UPDATE = 2;
2725 public static final int OP_REMOVE = 3; // uninstlled
2726 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2727
2728
Kenny Guyed131872014-04-30 03:02:21 +01002729 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002730 mOp = op;
2731 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002732 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002733 }
2734
2735 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002736 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002737
2738 final String[] packages = mPackages;
2739 final int N = packages.length;
2740 switch (mOp) {
2741 case OP_ADD:
2742 for (int i=0; i<N; i++) {
2743 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002744 mIconCache.remove(packages[i], mUser);
2745 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002746 }
2747 break;
2748 case OP_UPDATE:
2749 for (int i=0; i<N; i++) {
2750 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002751 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002752 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002753 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002754 }
2755 break;
2756 case OP_REMOVE:
2757 case OP_UNAVAILABLE:
2758 for (int i=0; i<N; i++) {
2759 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002760 mBgAllAppsList.removePackage(packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002761 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002762 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002763 }
2764 break;
2765 }
2766
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002767 ArrayList<AppInfo> added = null;
2768 ArrayList<AppInfo> modified = null;
2769 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002770
Adam Cohen487f7dd2012-06-28 18:12:10 -07002771 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002772 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002773 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002774 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002775 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002776 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002777 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002778 }
Winson Chung5d55f332012-07-16 20:45:03 -07002779 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002780 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002781 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002782 }
2783
Joe Onorato36115782010-06-17 13:28:48 -04002784 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2785 if (callbacks == null) {
2786 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2787 return;
2788 }
2789
2790 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002791 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002792 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002793 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002794 addAndBindAddedWorkspaceApps(context, addedInfos);
2795 } else {
2796 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002797 }
Joe Onorato36115782010-06-17 13:28:48 -04002798 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002799
Joe Onorato36115782010-06-17 13:28:48 -04002800 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002801 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002802
2803 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002804 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002805 ArrayList<ItemInfo> infos =
Kenny Guyed131872014-04-30 03:02:21 +01002806 getItemInfoForComponentName(a.componentName, mUser);
Winson Chung64359a52013-07-08 17:17:08 -07002807 for (ItemInfo i : infos) {
2808 if (isShortcutInfoUpdateable(i)) {
2809 ShortcutInfo info = (ShortcutInfo) i;
2810 info.title = a.title.toString();
2811 updateItemInDatabase(context, info);
2812 }
2813 }
2814 }
2815
Joe Onorato36115782010-06-17 13:28:48 -04002816 mHandler.post(new Runnable() {
2817 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002818 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2819 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002820 callbacks.bindAppsUpdated(modifiedFinal);
2821 }
2822 }
2823 });
2824 }
Winson Chung83892cc2013-05-01 16:53:33 -07002825
Winson Chungdf95eb12013-10-16 14:57:07 -07002826 final ArrayList<String> removedPackageNames =
2827 new ArrayList<String>();
2828 if (mOp == OP_REMOVE) {
2829 // Mark all packages in the broadcast to be removed
2830 removedPackageNames.addAll(Arrays.asList(packages));
2831 } else if (mOp == OP_UPDATE) {
2832 // Mark disabled packages in the broadcast to be removed
2833 final PackageManager pm = context.getPackageManager();
2834 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01002835 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002836 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002837 }
2838 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002839 }
2840 // Remove all the components associated with this package
2841 for (String pn : removedPackageNames) {
Kenny Guyed131872014-04-30 03:02:21 +01002842 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002843 for (ItemInfo i : infos) {
2844 deleteItemFromDatabase(context, i);
2845 }
2846 }
2847 // Remove all the specific components
2848 for (AppInfo a : removedApps) {
Kenny Guyed131872014-04-30 03:02:21 +01002849 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002850 for (ItemInfo i : infos) {
2851 deleteItemFromDatabase(context, i);
2852 }
2853 }
2854 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2855 // Remove any queued items from the install queue
2856 String spKey = LauncherAppState.getSharedPreferencesKey();
2857 SharedPreferences sp =
2858 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2859 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2860 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002861 mHandler.post(new Runnable() {
2862 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002863 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2864 if (callbacks == cb && cb != null) {
Kenny Guyed131872014-04-30 03:02:21 +01002865 callbacks.bindComponentsRemoved(removedPackageNames, removedApps, mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002866 }
2867 }
2868 });
Joe Onoratobe386092009-11-17 17:32:16 -08002869 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002870
Michael Jurkac402cd92013-05-20 15:49:32 +02002871 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01002872 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002873 mHandler.post(new Runnable() {
2874 @Override
2875 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002876 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2877 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002878 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002879 }
2880 }
2881 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002882
2883 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002884 mHandler.post(new Runnable() {
2885 public void run() {
2886 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2887 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002888 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002889 }
2890 }
2891 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 }
2893 }
2894
Michael Jurkac402cd92013-05-20 15:49:32 +02002895 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2896 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2897 PackageManager packageManager = context.getPackageManager();
2898 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2899 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2900 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2901 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2902 Collections.sort(widgetsAndShortcuts,
2903 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2904 return widgetsAndShortcuts;
2905 }
2906
Kenny Guyed131872014-04-30 03:02:21 +01002907 private static boolean isPackageDisabled(Context context, String packageName,
2908 UserHandleCompat user) {
2909 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2910 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07002911 }
Adam Cohen556f6132014-01-15 15:18:08 -08002912
Kenny Guyed131872014-04-30 03:02:21 +01002913 public static boolean isValidPackageActivity(Context context, ComponentName cn,
2914 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07002915 if (cn == null) {
2916 return false;
2917 }
Kenny Guyed131872014-04-30 03:02:21 +01002918 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2919 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002920 return false;
2921 }
Kenny Guyed131872014-04-30 03:02:21 +01002922 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07002923 }
2924
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002926 * Make an ShortcutInfo object for a restored application or shortcut item that points
2927 * to a package that is not yet installed on the system.
2928 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002929 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002930 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002931 if (cursor != null) {
2932 info.title = cursor.getString(titleIndex);
2933 } else {
2934 info.title = "";
2935 }
Kenny Guyed131872014-04-30 03:02:21 +01002936 info.user = UserHandleCompat.myUserHandle();
2937 info.setIcon(mIconCache.getIcon(intent, info.title.toString(), info.user));
Chris Wrenf4d08112014-01-16 18:13:56 -05002938 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002939 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002940 return info;
2941 }
2942
2943 /**
2944 * Make an Intent object for a restored application or shortcut item that points
2945 * to the market page for the item.
2946 */
2947 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002948 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002949 ComponentName componentName = intent.getComponent();
2950 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2951 Uri marketUri = new Uri.Builder()
2952 .scheme("market")
2953 .authority("details")
2954 .appendQueryParameter("id", componentName.getPackageName())
2955 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002956 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002957 marketIntent.setData(marketUri);
2958 return marketIntent;
2959 }
2960
2961 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002962 * This is called from the code that adds shortcuts from the intent receiver. This
2963 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 */
Kenny Guyed131872014-04-30 03:02:21 +01002965 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
2966 UserHandleCompat user, Context context) {
2967 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969
Joe Onorato56d82912010-03-07 14:32:10 -05002970 /**
2971 * Make an ShortcutInfo object for a shortcut that is an application.
2972 *
2973 * If c is not null, then it will be used to fill in missing data like the title and icon.
2974 */
Kenny Guyed131872014-04-30 03:02:21 +01002975 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
2976 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
2977 HashMap<Object, CharSequence> labelCache) {
2978 if (user == null) {
2979 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002980 return null;
2981 }
2982
Kenny Guyed131872014-04-30 03:02:21 +01002983 ComponentName componentName = intent.getComponent();
2984 if (componentName == null) {
2985 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
2986 return null;
2987 }
2988
2989 Intent newIntent = new Intent(intent.getAction(), null);
2990 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2991 newIntent.setComponent(componentName);
2992 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
2993 if (lai == null) {
2994 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
2995 return null;
2996 }
2997
2998 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002999
Joe Onorato56d82912010-03-07 14:32:10 -05003000 // the resource -- This may implicitly give us back the fallback icon,
3001 // but don't worry about that. All we're doing with usingFallbackIcon is
3002 // to avoid saving lots of copies of that in the database, and most apps
3003 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003004 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003005
Joe Onorato56d82912010-03-07 14:32:10 -05003006 // the db
3007 if (icon == null) {
3008 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003009 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003010 }
3011 }
3012 // the fallback icon
3013 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003014 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003015 info.usingFallbackIcon = true;
3016 }
3017 info.setIcon(icon);
3018
Kenny Guyed131872014-04-30 03:02:21 +01003019 // From the cache.
3020 if (labelCache != null) {
3021 info.title = labelCache.get(componentName);
3022 }
3023
Joe Onorato56d82912010-03-07 14:32:10 -05003024 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003025 if (info.title == null && lai != null) {
3026 info.title = lai.getLabel();
3027 if (labelCache != null) {
3028 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003029 }
Joe Onorato56d82912010-03-07 14:32:10 -05003030 }
3031 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003033 if (c != null) {
3034 info.title = c.getString(titleIndex);
3035 }
3036 }
3037 // fall back to the class name of the activity
3038 if (info.title == null) {
3039 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003040 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003042 info.user = user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003045
Winson Chung64359a52013-07-08 17:17:08 -07003046 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3047 ItemInfoFilter f) {
3048 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3049 for (ItemInfo i : infos) {
3050 if (i instanceof ShortcutInfo) {
3051 ShortcutInfo info = (ShortcutInfo) i;
3052 ComponentName cn = info.intent.getComponent();
3053 if (cn != null && f.filterItem(null, info, cn)) {
3054 filtered.add(info);
3055 }
3056 } else if (i instanceof FolderInfo) {
3057 FolderInfo info = (FolderInfo) i;
3058 for (ShortcutInfo s : info.contents) {
3059 ComponentName cn = s.intent.getComponent();
3060 if (cn != null && f.filterItem(info, s, cn)) {
3061 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003062 }
3063 }
Winson Chung64359a52013-07-08 17:17:08 -07003064 } else if (i instanceof LauncherAppWidgetInfo) {
3065 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3066 ComponentName cn = info.providerName;
3067 if (cn != null && f.filterItem(null, info, cn)) {
3068 filtered.add(info);
3069 }
Winson Chung8a435102012-08-30 17:16:53 -07003070 }
3071 }
Winson Chung64359a52013-07-08 17:17:08 -07003072 return new ArrayList<ItemInfo>(filtered);
3073 }
3074
Kenny Guyed131872014-04-30 03:02:21 +01003075 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn,
3076 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003077 ItemInfoFilter filter = new ItemInfoFilter() {
3078 @Override
3079 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003080 return cn.getPackageName().equals(pn) && info.user.equals(user);
Winson Chung64359a52013-07-08 17:17:08 -07003081 }
3082 };
3083 return filterItemInfos(sBgItemsIdMap.values(), filter);
3084 }
3085
Kenny Guyed131872014-04-30 03:02:21 +01003086 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3087 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003088 ItemInfoFilter filter = new ItemInfoFilter() {
3089 @Override
3090 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003091 if (info.user == null) {
3092 return cn.equals(cname);
3093 } else {
3094 return cn.equals(cname) && info.user.equals(user);
3095 }
Winson Chung64359a52013-07-08 17:17:08 -07003096 }
3097 };
3098 return filterItemInfos(sBgItemsIdMap.values(), filter);
3099 }
3100
3101 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3102 if (i instanceof ShortcutInfo) {
3103 ShortcutInfo info = (ShortcutInfo) i;
3104 // We need to check for ACTION_MAIN otherwise getComponent() might
3105 // return null for some shortcuts (for instance, for shortcuts to
3106 // web pages.)
3107 Intent intent = info.intent;
3108 ComponentName name = intent.getComponent();
3109 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3110 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3111 return true;
3112 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003113 // placeholder shortcuts get special treatment, let them through too.
3114 if (info.getRestoredIntent() != null) {
3115 return true;
3116 }
Winson Chung64359a52013-07-08 17:17:08 -07003117 }
3118 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003119 }
3120
3121 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003122 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003124 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003125 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3126 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127
Joe Onorato56d82912010-03-07 14:32:10 -05003128 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003129 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003130 // Non-app shortcuts are only supported for current user.
3131 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003133
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003134 // TODO: If there's an explicit component and we can't install that, delete it.
3135
Joe Onorato56d82912010-03-07 14:32:10 -05003136 info.title = c.getString(titleIndex);
3137
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 int iconType = c.getInt(iconTypeIndex);
3139 switch (iconType) {
3140 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3141 String packageName = c.getString(iconPackageIndex);
3142 String resourceName = c.getString(iconResourceIndex);
3143 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003144 info.customIcon = false;
3145 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003148 if (resources != null) {
3149 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003150 icon = Utilities.createIconBitmap(
3151 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003152 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003154 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 }
Joe Onorato56d82912010-03-07 14:32:10 -05003156 // the db
3157 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003158 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003159 }
3160 // the fallback icon
3161 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003162 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003163 info.usingFallbackIcon = true;
3164 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 break;
3166 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003167 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003168 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003169 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003170 info.customIcon = false;
3171 info.usingFallbackIcon = true;
3172 } else {
3173 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 break;
3176 default:
Kenny Guyed131872014-04-30 03:02:21 +01003177 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003178 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 info.customIcon = false;
3180 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003182 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 return info;
3184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185
Michael Jurka931dc972011-08-05 15:08:15 -07003186 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003187 @SuppressWarnings("all") // suppress dead code warning
3188 final boolean debug = false;
3189 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003190 Log.d(TAG, "getIconFromCursor app="
3191 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3192 }
3193 byte[] data = c.getBlob(iconIndex);
3194 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003195 return Utilities.createIconBitmap(
3196 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003197 } catch (Exception e) {
3198 return null;
3199 }
3200 }
3201
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3203 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003204 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003205 if (info == null) {
3206 return null;
3207 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003209
3210 return info;
3211 }
3212
Winson Chunga9abd0e2010-10-27 17:18:37 -07003213 /**
Winson Chung55cef262010-10-28 14:14:18 -07003214 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3215 */
3216 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3217 ComponentName component) {
3218 List<AppWidgetProviderInfo> widgets =
3219 AppWidgetManager.getInstance(context).getInstalledProviders();
3220 for (AppWidgetProviderInfo info : widgets) {
3221 if (info.provider.equals(component)) {
3222 return info;
3223 }
3224 }
3225 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003226 }
3227
Winson Chung68846fd2010-10-29 11:00:27 -07003228 /**
3229 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3230 */
3231 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3232 final PackageManager packageManager = context.getPackageManager();
3233 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3234 new ArrayList<WidgetMimeTypeHandlerData>();
3235
3236 final Intent supportsIntent =
3237 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3238 supportsIntent.setType(mimeType);
3239
3240 // Create a set of widget configuration components that we can test against
3241 final List<AppWidgetProviderInfo> widgets =
3242 AppWidgetManager.getInstance(context).getInstalledProviders();
3243 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3244 new HashMap<ComponentName, AppWidgetProviderInfo>();
3245 for (AppWidgetProviderInfo info : widgets) {
3246 configurationComponentToWidget.put(info.configure, info);
3247 }
3248
3249 // Run through each of the intents that can handle this type of clip data, and cross
3250 // reference them with the components that are actual configuration components
3251 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3252 PackageManager.MATCH_DEFAULT_ONLY);
3253 for (ResolveInfo info : activities) {
3254 final ActivityInfo activityInfo = info.activityInfo;
3255 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3256 activityInfo.name);
3257 if (configurationComponentToWidget.containsKey(infoComponent)) {
3258 supportedConfigurationActivities.add(
3259 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3260 configurationComponentToWidget.get(infoComponent)));
3261 }
3262 }
3263 return supportedConfigurationActivities;
3264 }
3265
Winson Chunga9abd0e2010-10-27 17:18:37 -07003266 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003267 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3268 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3269 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3270
Adam Cohend9198822011-11-22 16:42:47 -08003271 if (intent == null) {
3272 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3273 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3274 return null;
3275 }
3276
Joe Onorato0589f0f2010-02-08 13:44:00 -08003277 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003278 boolean customIcon = false;
3279 ShortcutIconResource iconResource = null;
3280
3281 if (bitmap != null && bitmap instanceof Bitmap) {
3282 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003283 customIcon = true;
3284 } else {
3285 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3286 if (extra != null && extra instanceof ShortcutIconResource) {
3287 try {
3288 iconResource = (ShortcutIconResource) extra;
3289 final PackageManager packageManager = context.getPackageManager();
3290 Resources resources = packageManager.getResourcesForApplication(
3291 iconResource.packageName);
3292 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003293 icon = Utilities.createIconBitmap(
Kenny Guyed131872014-04-30 03:02:21 +01003294 mIconCache.getFullResIcon(resources, id),
3295 context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003296 } catch (Exception e) {
3297 Log.w(TAG, "Could not load shortcut icon: " + extra);
3298 }
3299 }
3300 }
3301
Michael Jurkac9d95c52011-08-29 14:03:34 -07003302 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003303
Kenny Guyed131872014-04-30 03:02:21 +01003304 // Only support intents for current user for now. Intents sent from other
3305 // users wouldn't get here without intent forwarding anyway.
3306 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003307 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003308 if (fallbackIcon != null) {
3309 icon = fallbackIcon;
3310 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003311 icon = mIconCache.getDefaultIcon(info.user);
Winson Chunga9abd0e2010-10-27 17:18:37 -07003312 info.usingFallbackIcon = true;
3313 }
Joe Onorato56d82912010-03-07 14:32:10 -05003314 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003315 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003316
Joe Onorato0589f0f2010-02-08 13:44:00 -08003317 info.title = name;
3318 info.intent = intent;
3319 info.customIcon = customIcon;
3320 info.iconResource = iconResource;
3321
3322 return info;
3323 }
3324
Winson Chungaac01e12011-08-17 10:37:13 -07003325 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3326 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003327 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003328 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003329 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003330 }
Joe Onorato56d82912010-03-07 14:32:10 -05003331 // If this icon doesn't have a custom icon, check to see
3332 // what's stored in the DB, and if it doesn't match what
3333 // we're going to show, store what we are going to show back
3334 // into the DB. We do this so when we're loading, if the
3335 // package manager can't find an icon (for example because
3336 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003337 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003338 cache.put(info, c.getBlob(iconIndex));
3339 return true;
3340 }
3341 return false;
3342 }
3343 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3344 boolean needSave = false;
3345 try {
3346 if (data != null) {
3347 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3348 Bitmap loaded = info.getIcon(mIconCache);
3349 needSave = !saved.sameAs(loaded);
3350 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003351 needSave = true;
3352 }
Winson Chungaac01e12011-08-17 10:37:13 -07003353 } catch (Exception e) {
3354 needSave = true;
3355 }
3356 if (needSave) {
3357 Log.d(TAG, "going to save icon bitmap for info=" + info);
3358 // This is slower than is ideal, but this only happens once
3359 // or when the app is updated with a new icon.
3360 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003361 }
3362 }
3363
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003365 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 * or make a new one.
3367 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003368 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 // See if a placeholder was created for us already
3370 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003371 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003373 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 folders.put(id, folderInfo);
3375 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003376 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003377 }
3378
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003379 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003380 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003381 return new Comparator<AppInfo>() {
3382 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003383 if (a.user.equals(b.user)) {
3384 int result = collator.compare(a.title.toString().trim(),
3385 b.title.toString().trim());
3386 if (result == 0) {
3387 result = a.componentName.compareTo(b.componentName);
3388 }
3389 return result;
3390 } else {
3391 // TODO Need to figure out rules for sorting
3392 // profiles, this puts work second.
3393 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003394 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003395 }
Winson Chung11904872012-09-17 16:58:46 -07003396 };
3397 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003398 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3399 = new Comparator<AppInfo>() {
3400 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003401 if (a.firstInstallTime < b.firstInstallTime) return 1;
3402 if (a.firstInstallTime > b.firstInstallTime) return -1;
3403 return 0;
3404 }
3405 };
Winson Chung11904872012-09-17 16:58:46 -07003406 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3407 final Collator collator = Collator.getInstance();
3408 return new Comparator<AppWidgetProviderInfo>() {
3409 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003410 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003411 }
3412 };
3413 }
Winson Chung5308f242011-08-18 12:12:41 -07003414 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3415 if (info.activityInfo != null) {
3416 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3417 } else {
3418 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3419 }
3420 }
Kenny Guyed131872014-04-30 03:02:21 +01003421 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003422 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003423 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003424 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003425 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003426 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003427 }
Kenny Guyed131872014-04-30 03:02:21 +01003428 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003429 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003430 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003431 }
Kenny Guyed131872014-04-30 03:02:21 +01003432 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003433 CharSequence labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003434 ComponentName keyA = a.getComponentName();
3435 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003436 if (mLabelCache.containsKey(keyA)) {
3437 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003438 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003439 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003440
Winson Chung5308f242011-08-18 12:12:41 -07003441 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003442 }
Winson Chung5308f242011-08-18 12:12:41 -07003443 if (mLabelCache.containsKey(keyB)) {
3444 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003445 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003446 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003447
Winson Chung5308f242011-08-18 12:12:41 -07003448 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003449 }
Winson Chung11904872012-09-17 16:58:46 -07003450 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003451 }
3452 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003453 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003454 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003455 private PackageManager mPackageManager;
3456 private HashMap<Object, String> mLabelCache;
3457 WidgetAndShortcutNameComparator(PackageManager pm) {
3458 mPackageManager = pm;
3459 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003460 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003461 }
3462 public final int compare(Object a, Object b) {
3463 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003464 if (mLabelCache.containsKey(a)) {
3465 labelA = mLabelCache.get(a);
3466 } else {
3467 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003468 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003469 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003470 mLabelCache.put(a, labelA);
3471 }
3472 if (mLabelCache.containsKey(b)) {
3473 labelB = mLabelCache.get(b);
3474 } else {
3475 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003476 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003477 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003478 mLabelCache.put(b, labelB);
3479 }
Winson Chung11904872012-09-17 16:58:46 -07003480 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003481 }
3482 };
Joe Onoratobe386092009-11-17 17:32:16 -08003483
3484 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003485 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003486 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3487 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3488 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3489 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003490 if (mLoaderTask != null) {
3491 mLoaderTask.dumpState();
3492 } else {
3493 Log.d(TAG, "mLoaderTask=null");
3494 }
Joe Onoratobe386092009-11-17 17:32:16 -08003495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003496}