blob: 15db057ec52aa2df734f59c19cbe1cdda4b32479 [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();
Kenny Guyed131872014-04-30 03:02:21 +0100856 long serialNumber = UserManagerCompat.getInstance(context)
857 .getSerialNumberForUser(user);
858 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1"
859 + " and profileId = " + serialNumber;
Chris Wrenb6d4c282014-01-27 14:17:08 -0500860 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Kenny Guyed131872014-04-30 03:02:21 +0100861 new String[]{"intent", "restored", "profileId"}, where, null, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500862 boolean result = false;
863 try {
864 result = c.moveToFirst();
865 } finally {
866 c.close();
867 }
868 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
869 return result;
870 }
871
872 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700873 * Returns an ItemInfo array containing all the items in the LauncherModel.
874 * The ItemInfo.id is not set through this function.
875 */
876 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
877 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
878 final ContentResolver cr = context.getContentResolver();
879 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
880 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
Kenny Guyed131872014-04-30 03:02:21 +0100881 LauncherSettings.Favorites.SCREEN,
882 LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
883 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY,
884 LauncherSettings.Favorites.PROFILE_ID }, null, null, null);
Winson Chungaafa03c2010-06-11 17:34:16 -0700885
886 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
887 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
888 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
889 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
890 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
891 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
892 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
Kenny Guyed131872014-04-30 03:02:21 +0100893 final int profileIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
894 UserManagerCompat userManager = UserManagerCompat.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700895 try {
896 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700897 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700898 item.cellX = c.getInt(cellXIndex);
899 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700900 item.spanX = Math.max(1, c.getInt(spanXIndex));
901 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700902 item.container = c.getInt(containerIndex);
903 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 item.screenId = c.getInt(screenIndex);
Kenny Guyed131872014-04-30 03:02:21 +0100905 int serialNumber = c.getInt(profileIdIndex);
906 item.user = userManager.getUserForSerialNumber(serialNumber);
907 // Skip if user has been deleted.
908 if (item.user != null) {
909 items.add(item);
910 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700911 }
912 } catch (Exception e) {
913 items.clear();
914 } finally {
915 c.close();
916 }
917
918 return items;
919 }
920
921 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
923 */
924 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
925 final ContentResolver cr = context.getContentResolver();
926 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
927 "_id=? and (itemType=? or itemType=?)",
928 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700929 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 try {
932 if (c.moveToFirst()) {
933 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
934 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
935 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
936 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
937 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
938 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 FolderInfo folderInfo = null;
941 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700942 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
943 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700945 }
946
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 folderInfo.title = c.getString(titleIndex);
948 folderInfo.id = id;
949 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700951 folderInfo.cellX = c.getInt(cellXIndex);
952 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953
954 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 } finally {
957 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700958 }
959
960 return null;
961 }
962
Joe Onorato9c1289c2009-08-17 11:03:03 -0400963 /**
964 * Add an item to the database in a specified container. Sets the container, screen, cellX and
965 * cellY fields of the item. Also assigns an ID to the item.
966 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700968 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 item.cellX = cellX;
971 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 // We store hotseat items in canonical form which is this orientation invariant position
973 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980
981 final ContentValues values = new ContentValues();
982 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +0100983 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984
Michael Jurka414300a2013-08-27 15:42:35 +0200985 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700986 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700988
Jason Monk8e19cf22014-03-20 15:06:57 -0400989 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700990 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700991 public void run() {
992 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
993 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700995 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700996 synchronized (sBgLock) {
Jason Monk8e19cf22014-03-20 15:06:57 -0400997 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 sBgItemsIdMap.put(item.id, item);
999 switch (item.itemType) {
1000 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1001 sBgFolders.put(item.id, (FolderInfo) item);
1002 // Fall through
1003 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1004 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1005 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1006 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1007 sBgWorkspaceItems.add(item);
1008 } else {
1009 if (!sBgFolders.containsKey(item.container)) {
1010 // Adding an item to a folder that doesn't exist.
1011 String msg = "adding item: " + item + " to a folder that " +
1012 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001013 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001015 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 break;
1017 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1018 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1019 break;
1020 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021 }
1022 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001023 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001025 }
1026
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001028 * Creates a new unique child id, for a given cell span across all layouts.
1029 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001030 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001031 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001033 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001034 }
1035
Winson Chungaafa03c2010-06-11 17:34:16 -07001036 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001037 * Removes the specified item from the database
1038 * @param context
1039 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001041 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001043 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001044
Michael Jurka83df1882011-08-31 20:59:26 -07001045 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001046 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001048
1049 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001050 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 switch (item.itemType) {
1052 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1053 sBgFolders.remove(item.id);
1054 for (ItemInfo info: sBgItemsIdMap.values()) {
1055 if (info.container == item.id) {
1056 // We are deleting a folder which still contains items that
1057 // think they are contained by that folder.
1058 String msg = "deleting a folder (" + item + ") which still " +
1059 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001060 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001062 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 sBgWorkspaceItems.remove(item);
1064 break;
1065 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1066 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1067 sBgWorkspaceItems.remove(item);
1068 break;
1069 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1070 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1071 break;
1072 }
1073 sBgItemsIdMap.remove(item.id);
1074 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001075 }
1076 }
Michael Jurka83df1882011-08-31 20:59:26 -07001077 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001078 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 }
1080
1081 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001082 * Update the order of the workspace screens in the database. The array list contains
1083 * a list of screen ids in the order that they should appear.
1084 */
Winson Chungc9168342013-06-26 14:54:55 -07001085 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001086 // Log to disk
1087 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1088 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1089
Winson Chung64359a52013-07-08 17:17:08 -07001090 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 final ContentResolver cr = context.getContentResolver();
1092 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1093
1094 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001095 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 while (iter.hasNext()) {
1097 long id = iter.next();
1098 if (id < 0) {
1099 iter.remove();
1100 }
1101 }
1102
1103 Runnable r = new Runnable() {
1104 @Override
1105 public void run() {
Yura085c8532014-02-11 15:15:29 +00001106 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001107 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001108 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001109 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 for (int i = 0; i < count; i++) {
1111 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001112 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001113 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1114 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001115 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001116 }
Yura085c8532014-02-11 15:15:29 +00001117
1118 try {
1119 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1120 } catch (Exception ex) {
1121 throw new RuntimeException(ex);
1122 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001123
Winson Chungba9c37f2013-08-30 14:11:37 -07001124 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001125 sBgWorkspaceScreens.clear();
1126 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001127 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 }
1129 };
1130 runOnWorkerThread(r);
1131 }
1132
1133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 * Remove the contents of the specified folder from the database
1135 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001136 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 final ContentResolver cr = context.getContentResolver();
1138
Michael Jurkac9d95c52011-08-29 14:03:34 -07001139 Runnable r = new Runnable() {
1140 public void run() {
1141 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001142 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001143 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001144 sBgItemsIdMap.remove(info.id);
1145 sBgFolders.remove(info.id);
1146 sBgDbIconCache.remove(info);
1147 sBgWorkspaceItems.remove(info);
1148 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001149
Michael Jurkac9d95c52011-08-29 14:03:34 -07001150 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1151 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001152 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001153 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001154 for (ItemInfo childInfo : info.contents) {
1155 sBgItemsIdMap.remove(childInfo.id);
1156 sBgDbIconCache.remove(childInfo);
1157 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001158 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001159 }
1160 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001161 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 }
1163
1164 /**
1165 * Set this as the current Launcher activity object for the loader.
1166 */
1167 public void initialize(Callbacks callbacks) {
1168 synchronized (mLock) {
1169 mCallbacks = new WeakReference<Callbacks>(callbacks);
1170 }
1171 }
1172
Kenny Guyed131872014-04-30 03:02:21 +01001173 @Override
1174 public void onPackageChanged(UserHandleCompat user, String packageName) {
1175 int op = PackageUpdatedTask.OP_UPDATE;
1176 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1177 user));
1178 }
1179
1180 @Override
1181 public void onPackageRemoved(UserHandleCompat user, String packageName) {
1182 int op = PackageUpdatedTask.OP_REMOVE;
1183 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1184 user));
1185 }
1186
1187 @Override
1188 public void onPackageAdded(UserHandleCompat user, String packageName) {
1189 int op = PackageUpdatedTask.OP_ADD;
1190 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1191 user));
1192 }
1193
1194 @Override
1195 public void onPackagesAvailable(UserHandleCompat user, String[] packageNames,
1196 boolean replacing) {
1197 if (!replacing) {
1198 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1199 user));
1200 if (mAppsCanBeOnRemoveableStorage) {
1201 // Only rebind if we support removable storage. It catches the
1202 // case where
1203 // apps on the external sd card need to be reloaded
1204 startLoaderFromBackground();
1205 }
1206 } else {
1207 // If we are replacing then just update the packages in the list
1208 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1209 packageNames, user));
1210 }
1211 }
1212
1213 @Override
1214 public void onPackagesUnavailable(UserHandleCompat user, String[] packageNames,
1215 boolean replacing) {
1216 if (!replacing) {
1217 enqueuePackageUpdated(new PackageUpdatedTask(
1218 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1219 user));
1220 }
1221
1222 }
1223
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001224 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1226 * ACTION_PACKAGE_CHANGED.
1227 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001228 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001229 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001230 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001231
Joe Onorato36115782010-06-17 13:28:48 -04001232 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001233 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001234 // If we have changed locale we need to clear out the labels in all apps/workspace.
1235 forceReload();
1236 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1237 // Check if configuration change was an mcc/mnc change which would affect app resources
1238 // and we would need to clear out the labels in all apps/workspace. Same handling as
1239 // above for ACTION_LOCALE_CHANGED
1240 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001241 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001242 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001243 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001244 forceReload();
1245 }
1246 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001247 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001248 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1249 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001250 if (mCallbacks != null) {
1251 Callbacks callbacks = mCallbacks.get();
1252 if (callbacks != null) {
1253 callbacks.bindSearchablesChanged();
1254 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001255 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001256 }
1257 }
1258
Reena Lee93f824a2011-09-23 17:20:28 -07001259 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001260 resetLoadedState(true, true);
1261
Reena Lee93f824a2011-09-23 17:20:28 -07001262 // Do this here because if the launcher activity is running it will be restarted.
1263 // If it's not running startLoaderFromBackground will merely tell it that it needs
1264 // to reload.
1265 startLoaderFromBackground();
1266 }
1267
Winson Chungf0c6ae02012-03-21 16:10:31 -07001268 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1269 synchronized (mLock) {
1270 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1271 // mWorkspaceLoaded to true later
1272 stopLoaderLocked();
1273 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1274 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1275 }
1276 }
1277
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001278 /**
1279 * When the launcher is in the background, it's possible for it to miss paired
1280 * configuration changes. So whenever we trigger the loader from the background
1281 * tell the launcher that it needs to re-run the loader when it comes back instead
1282 * of doing it now.
1283 */
1284 public void startLoaderFromBackground() {
1285 boolean runLoader = false;
1286 if (mCallbacks != null) {
1287 Callbacks callbacks = mCallbacks.get();
1288 if (callbacks != null) {
1289 // Only actually run the loader if they're not paused.
1290 if (!callbacks.setLoadOnResume()) {
1291 runLoader = true;
1292 }
1293 }
1294 }
1295 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001296 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001297 }
Joe Onorato36115782010-06-17 13:28:48 -04001298 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001299
Reena Lee93f824a2011-09-23 17:20:28 -07001300 // If there is already a loader task running, tell it to stop.
1301 // returns true if isLaunching() was true on the old task
1302 private boolean stopLoaderLocked() {
1303 boolean isLaunching = false;
1304 LoaderTask oldTask = mLoaderTask;
1305 if (oldTask != null) {
1306 if (oldTask.isLaunching()) {
1307 isLaunching = true;
1308 }
1309 oldTask.stopLocked();
1310 }
1311 return isLaunching;
1312 }
1313
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001315 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1316 }
1317
1318 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001319 synchronized (mLock) {
1320 if (DEBUG_LOADERS) {
1321 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001324 // Clear any deferred bind-runnables from the synchronized load process
1325 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001326 synchronized (mDeferredBindRunnables) {
1327 mDeferredBindRunnables.clear();
1328 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001329
Joe Onorato36115782010-06-17 13:28:48 -04001330 // Don't bother to start the thread if we know it's not going to do anything
1331 if (mCallbacks != null && mCallbacks.get() != null) {
1332 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001333 // also, don't downgrade isLaunching if we're already running
1334 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001335 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001336 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1337 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001338 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1339 } else {
1340 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1341 sWorker.post(mLoaderTask);
1342 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001344 }
1345 }
1346
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347 void bindRemainingSynchronousPages() {
1348 // Post the remaining side pages to be loaded
1349 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001350 Runnable[] deferredBindRunnables = null;
1351 synchronized (mDeferredBindRunnables) {
1352 deferredBindRunnables = mDeferredBindRunnables.toArray(
1353 new Runnable[mDeferredBindRunnables.size()]);
1354 mDeferredBindRunnables.clear();
1355 }
1356 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001357 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001359 }
1360 }
1361
Joe Onorato36115782010-06-17 13:28:48 -04001362 public void stopLoader() {
1363 synchronized (mLock) {
1364 if (mLoaderTask != null) {
1365 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 }
1367 }
Joe Onorato36115782010-06-17 13:28:48 -04001368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369
Winson Chung76828c82013-08-19 15:43:29 -07001370 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1371 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1372 final ContentResolver contentResolver = context.getContentResolver();
1373 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1374 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1375 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1376
1377 try {
1378 final int idIndex = sc.getColumnIndexOrThrow(
1379 LauncherSettings.WorkspaceScreens._ID);
1380 final int rankIndex = sc.getColumnIndexOrThrow(
1381 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1382 while (sc.moveToNext()) {
1383 try {
1384 long screenId = sc.getLong(idIndex);
1385 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001386 orderedScreens.put(rank, screenId);
1387 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001388 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001389 }
1390 }
1391 } finally {
1392 sc.close();
1393 }
Winson Chunga90303b2013-11-15 13:05:06 -08001394
1395 // Log to disk
1396 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1397 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1398 for (Integer i : orderedScreens.keySet()) {
1399 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1400 }
1401 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1402 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001403 return orderedScreens;
1404 }
1405
Michael Jurkac57b7a82011-08-09 22:02:20 -07001406 public boolean isAllAppsLoaded() {
1407 return mAllAppsLoaded;
1408 }
1409
Winson Chung36a62fe2012-05-06 18:04:42 -07001410 boolean isLoadingWorkspace() {
1411 synchronized (mLock) {
1412 if (mLoaderTask != null) {
1413 return mLoaderTask.isLoadingWorkspace();
1414 }
1415 }
1416 return false;
1417 }
1418
Joe Onorato36115782010-06-17 13:28:48 -04001419 /**
1420 * Runnable for the thread that loads the contents of the launcher:
1421 * - workspace icons
1422 * - widgets
1423 * - all apps icons
1424 */
1425 private class LoaderTask implements Runnable {
1426 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001427 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001428 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001429 private boolean mStopped;
1430 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001431 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432
Winson Chungc3eecff2011-07-11 17:44:15 -07001433 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001434
Dan Sandlerd5024042014-01-09 15:01:33 -05001435 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001436 mContext = context;
1437 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001438 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001439 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 }
1441
Joe Onorato36115782010-06-17 13:28:48 -04001442 boolean isLaunching() {
1443 return mIsLaunching;
1444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445
Winson Chung36a62fe2012-05-06 18:04:42 -07001446 boolean isLoadingWorkspace() {
1447 return mIsLoadingAndBindingWorkspace;
1448 }
1449
Winson Chungc763c4e2013-07-19 13:49:06 -07001450 /** Returns whether this is an upgrade path */
1451 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001452 mIsLoadingAndBindingWorkspace = true;
1453
Joe Onorato36115782010-06-17 13:28:48 -04001454 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001455 if (DEBUG_LOADERS) {
1456 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001458
Winson Chungc763c4e2013-07-19 13:49:06 -07001459 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001460 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001461 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001462 synchronized (LoaderTask.this) {
1463 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001464 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001465 }
1466 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
1468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001471 bindWorkspace(-1, isUpgradePath);
1472 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001473 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001474
Joe Onorato36115782010-06-17 13:28:48 -04001475 private void waitForIdle() {
1476 // Wait until the either we're stopped or the other threads are done.
1477 // This way we don't start loading all apps until the workspace has settled
1478 // down.
1479 synchronized (LoaderTask.this) {
1480 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001481
Joe Onorato36115782010-06-17 13:28:48 -04001482 mHandler.postIdle(new Runnable() {
1483 public void run() {
1484 synchronized (LoaderTask.this) {
1485 mLoadAndBindStepFinished = true;
1486 if (DEBUG_LOADERS) {
1487 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001488 }
Joe Onorato36115782010-06-17 13:28:48 -04001489 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001490 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 });
1493
Michael Jurkac7700af2013-05-14 20:17:58 +02001494 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001495 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001496 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1497 // wait no longer than 1sec at a time
1498 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001499 } catch (InterruptedException ex) {
1500 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001501 }
1502 }
Joe Onorato36115782010-06-17 13:28:48 -04001503 if (DEBUG_LOADERS) {
1504 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001505 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001506 + "ms for previous step to finish binding");
1507 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001510
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001511 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001512 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001513 // Ensure that we have a valid page index to load synchronously
1514 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1515 "valid page index");
1516 }
1517 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1518 // Ensure that we don't try and bind a specified page when the pages have not been
1519 // loaded already (we should load everything asynchronously in that case)
1520 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1521 }
1522 synchronized (mLock) {
1523 if (mIsLoaderTaskRunning) {
1524 // Ensure that we are never running the background loading at this point since
1525 // we also touch the background collections
1526 throw new RuntimeException("Error! Background loading is already running");
1527 }
1528 }
1529
1530 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1531 // data structures, we can't allow any other thread to touch that data, but because
1532 // this call is synchronous, we can get away with not locking).
1533
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001534 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001535 // operations from the previous activity. We need to ensure that all queued operations
1536 // are executed before any synchronous binding work is done.
1537 mHandler.flush();
1538
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001539 // Divide the set of loaded items into those that we are binding synchronously, and
1540 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001541 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001542 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1543 // arise from that.
1544 onlyBindAllApps();
1545 }
1546
Joe Onorato36115782010-06-17 13:28:48 -04001547 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001548 boolean isUpgrade = false;
1549
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001550 synchronized (mLock) {
1551 mIsLoaderTaskRunning = true;
1552 }
Joe Onorato36115782010-06-17 13:28:48 -04001553 // Optimize for end-user experience: if the Launcher is up and // running with the
1554 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1555 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001556 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001557 // Elevate priority when Home launches for the first time to avoid
1558 // starving at boot time. Staring at a blank home is not cool.
1559 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001560 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1561 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001562 android.os.Process.setThreadPriority(mIsLaunching
1563 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1564 }
Winson Chung64359a52013-07-08 17:17:08 -07001565 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001566 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001567
Joe Onorato36115782010-06-17 13:28:48 -04001568 if (mStopped) {
1569 break keep_running;
1570 }
1571
1572 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1573 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001574 synchronized (mLock) {
1575 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001576 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001577 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1578 }
1579 }
Joe Onorato36115782010-06-17 13:28:48 -04001580 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001581
1582 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001583 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1584 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001585
1586 // Restore the default thread priority after we are done loading items
1587 synchronized (mLock) {
1588 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1589 }
Joe Onorato36115782010-06-17 13:28:48 -04001590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001591
Winson Chungaac01e12011-08-17 10:37:13 -07001592 // Update the saved icons if necessary
1593 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001594 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001595 for (Object key : sBgDbIconCache.keySet()) {
1596 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1597 }
1598 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001599 }
Winson Chungaac01e12011-08-17 10:37:13 -07001600
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001601 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001602 // Ensure that all the applications that are in the system are
1603 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001604 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001605 verifyApplications();
1606 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001607 }
1608
Joe Onorato36115782010-06-17 13:28:48 -04001609 // Clear out this reference, otherwise we end up holding it until all of the
1610 // callback runnables are done.
1611 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612
Joe Onorato36115782010-06-17 13:28:48 -04001613 synchronized (mLock) {
1614 // If we are still the last one to be scheduled, remove ourselves.
1615 if (mLoaderTask == this) {
1616 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001618 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001619 }
Joe Onorato36115782010-06-17 13:28:48 -04001620 }
1621
1622 public void stopLocked() {
1623 synchronized (LoaderTask.this) {
1624 mStopped = true;
1625 this.notify();
1626 }
1627 }
1628
1629 /**
1630 * Gets the callbacks object. If we've been stopped, or if the launcher object
1631 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1632 * object that was around when the deferred message was scheduled, and if there's
1633 * a new Callbacks object around then also return null. This will save us from
1634 * calling onto it with data that will be ignored.
1635 */
1636 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1637 synchronized (mLock) {
1638 if (mStopped) {
1639 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001640 }
Joe Onorato36115782010-06-17 13:28:48 -04001641
1642 if (mCallbacks == null) {
1643 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001644 }
Joe Onorato36115782010-06-17 13:28:48 -04001645
1646 final Callbacks callbacks = mCallbacks.get();
1647 if (callbacks != oldCallbacks) {
1648 return null;
1649 }
1650 if (callbacks == null) {
1651 Log.w(TAG, "no mCallbacks");
1652 return null;
1653 }
1654
1655 return callbacks;
1656 }
1657 }
1658
Winson Chungc763c4e2013-07-19 13:49:06 -07001659 private void verifyApplications() {
1660 final Context context = mApp.getContext();
1661
1662 // Cross reference all the applications in our apps list with items in the workspace
1663 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001664 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001665 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001666 for (AppInfo app : mBgAllAppsList.data) {
Kenny Guyed131872014-04-30 03:02:21 +01001667 tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
Winson Chungc763c4e2013-07-19 13:49:06 -07001668 if (tmpInfos.isEmpty()) {
1669 // We are missing an application icon, so add this to the workspace
1670 added.add(app);
1671 // This is a rare event, so lets log it
1672 Log.e(TAG, "Missing Application on load: " + app);
1673 }
1674 }
1675 }
1676 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001677 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001678 }
1679 }
1680
Joe Onorato36115782010-06-17 13:28:48 -04001681 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001682 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001683 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001684 LauncherAppState app = LauncherAppState.getInstance();
1685 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001686 final int countX = (int) grid.numColumns;
1687 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001688
Adam Cohendcd297f2013-06-18 13:13:40 -07001689 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001690 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001691 // Return early if we detect that an item is under the hotseat button
1692 if (mCallbacks == null ||
1693 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001694 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001695 Log.e(TAG, "Error loading shortcut into hotseat " + item
1696 + " into position (" + item.screenId + ":" + item.cellX + ","
1697 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001698 return false;
1699 }
1700
Dan Sandler295ae182013-12-10 16:05:47 -05001701 final ItemInfo[][] hotseatItems =
1702 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1703
Adam Cohenae4409d2013-11-26 10:34:59 -08001704 if (item.screenId >= grid.numHotseatIcons) {
1705 Log.e(TAG, "Error loading shortcut " + item
1706 + " into hotseat position " + item.screenId
1707 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1708 + ")");
1709 return false;
1710 }
1711
Dan Sandler295ae182013-12-10 16:05:47 -05001712 if (hotseatItems != null) {
1713 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001714 Log.e(TAG, "Error loading shortcut into hotseat " + item
1715 + " into position (" + item.screenId + ":" + item.cellX + ","
1716 + item.cellY + ") occupied by "
1717 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1718 [(int) item.screenId][0]);
1719 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001720 } else {
1721 hotseatItems[(int) item.screenId][0] = item;
1722 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001723 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001724 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001725 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001726 items[(int) item.screenId][0] = item;
1727 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001728 return true;
1729 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001730 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1731 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001732 return true;
1733 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001734
Adam Cohendcd297f2013-06-18 13:13:40 -07001735 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001736 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001737 occupied.put(item.screenId, items);
1738 }
1739
Dan Sandler295ae182013-12-10 16:05:47 -05001740 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001741 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1742 item.cellX < 0 || item.cellY < 0 ||
1743 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1744 Log.e(TAG, "Error loading shortcut " + item
1745 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1746 + item.cellX + "," + item.cellY
1747 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1748 return false;
1749 }
1750
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001751 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001752 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1753 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001754 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001755 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001756 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001757 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001758 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001759 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001760 return false;
1761 }
1762 }
1763 }
1764 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1765 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001766 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001767 }
1768 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001769
Joe Onorato36115782010-06-17 13:28:48 -04001770 return true;
1771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772
Winson Chungba9c37f2013-08-30 14:11:37 -07001773 /** Clears all the sBg data structures */
1774 private void clearSBgDataStructures() {
1775 synchronized (sBgLock) {
1776 sBgWorkspaceItems.clear();
1777 sBgAppWidgets.clear();
1778 sBgFolders.clear();
1779 sBgItemsIdMap.clear();
1780 sBgDbIconCache.clear();
1781 sBgWorkspaceScreens.clear();
1782 }
1783 }
1784
Dan Sandlerd5024042014-01-09 15:01:33 -05001785 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001786 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001787 // Log to disk
1788 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1789
Joe Onorato36115782010-06-17 13:28:48 -04001790 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791
Joe Onorato36115782010-06-17 13:28:48 -04001792 final Context context = mContext;
1793 final ContentResolver contentResolver = context.getContentResolver();
1794 final PackageManager manager = context.getPackageManager();
1795 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1796 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001797
Winson Chung892c74d2013-08-22 16:15:50 -07001798 LauncherAppState app = LauncherAppState.getInstance();
1799 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1800 int countX = (int) grid.numColumns;
1801 int countY = (int) grid.numRows;
1802
Dan Sandlerd5024042014-01-09 15:01:33 -05001803 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1804 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1805 LauncherAppState.getLauncherProvider().deleteDatabase();
1806 }
1807
1808 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1809 // append the user's Launcher2 shortcuts
1810 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1811 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1812 } else {
1813 // Make sure the default workspace is loaded
1814 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1815 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1816 }
Adam Cohene25af792013-06-06 23:08:25 -07001817
Winson Chungc763c4e2013-07-19 13:49:06 -07001818 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001819 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001820 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001821
Winson Chung9f9f00b2013-11-15 13:27:00 -08001822 // Log to disk
1823 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001824
Winson Chung2abf94d2012-07-18 18:16:38 -07001825 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001826 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001827
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001829 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001830 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001831 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001832 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 // +1 for the hotseat (it can be larger than the workspace)
1835 // Load workspace in reverse order to ensure that latest items are loaded first (and
1836 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001837 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 try {
1840 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1841 final int intentIndex = c.getColumnIndexOrThrow
1842 (LauncherSettings.Favorites.INTENT);
1843 final int titleIndex = c.getColumnIndexOrThrow
1844 (LauncherSettings.Favorites.TITLE);
1845 final int iconTypeIndex = c.getColumnIndexOrThrow(
1846 LauncherSettings.Favorites.ICON_TYPE);
1847 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1848 final int iconPackageIndex = c.getColumnIndexOrThrow(
1849 LauncherSettings.Favorites.ICON_PACKAGE);
1850 final int iconResourceIndex = c.getColumnIndexOrThrow(
1851 LauncherSettings.Favorites.ICON_RESOURCE);
1852 final int containerIndex = c.getColumnIndexOrThrow(
1853 LauncherSettings.Favorites.CONTAINER);
1854 final int itemTypeIndex = c.getColumnIndexOrThrow(
1855 LauncherSettings.Favorites.ITEM_TYPE);
1856 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1857 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001858 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1859 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 final int screenIndex = c.getColumnIndexOrThrow(
1861 LauncherSettings.Favorites.SCREEN);
1862 final int cellXIndex = c.getColumnIndexOrThrow
1863 (LauncherSettings.Favorites.CELLX);
1864 final int cellYIndex = c.getColumnIndexOrThrow
1865 (LauncherSettings.Favorites.CELLY);
1866 final int spanXIndex = c.getColumnIndexOrThrow
1867 (LauncherSettings.Favorites.SPANX);
1868 final int spanYIndex = c.getColumnIndexOrThrow(
1869 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001870 final int restoredIndex = c.getColumnIndexOrThrow(
1871 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001872 final int profileIdIndex = c.getColumnIndexOrThrow(
1873 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1875 //final int displayModeIndex = c.getColumnIndexOrThrow(
1876 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 ShortcutInfo info;
1879 String intentDescription;
1880 LauncherAppWidgetInfo appWidgetInfo;
1881 int container;
1882 long id;
1883 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001884 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001885
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001887 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 try {
1889 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001890 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 switch (itemType) {
1893 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1894 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001895 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896 intentDescription = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001897 int serialNumber = c.getInt(profileIdIndex);
1898 user = mUserManager.getUserForSerialNumber(serialNumber);
1899 if (user == null) {
1900 // User has been deleted remove the item.
1901 itemsToRemove.add(id);
1902 continue;
1903 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 try {
1905 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001906 ComponentName cn = intent.getComponent();
Kenny Guyed131872014-04-30 03:02:21 +01001907 if (cn != null && !isValidPackageActivity(context, cn, user)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001908 if (restored) {
1909 // might be installed later
1910 Launcher.addDumpLog(TAG,
1911 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001912 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001913 if (!mAppsCanBeOnRemoveableStorage) {
1914 // Log the invalid package, and remove it
1915 Launcher.addDumpLog(TAG,
1916 "Invalid package removed: " + cn, true);
1917 itemsToRemove.add(id);
1918 } else {
1919 // If apps can be on external storage, then we just
1920 // leave them for the user to remove (maybe add
1921 // visual treatment to it)
1922 Launcher.addDumpLog(TAG,
1923 "Invalid package found: " + cn, true);
1924 }
1925 continue;
Winson Chungee055712013-07-30 14:46:24 -07001926 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001927 } else if (restored) {
1928 // no special handling necessary for this restored item
1929 restoredRows.add(id);
1930 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001931 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001933 Launcher.addDumpLog(TAG,
1934 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001935 continue;
1936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001937
Chris Wrenf4d08112014-01-16 18:13:56 -05001938 if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01001939 if (user.equals(UserHandleCompat.myUserHandle())) {
1940 Launcher.addDumpLog(TAG,
1941 "constructing info for partially restored package",
1942 true);
1943 info = getRestoredItemInfo(c, titleIndex, intent);
1944 intent = getRestoredItemIntent(c, context, intent);
1945 } else {
1946 // Don't restore items for other profiles.
1947 itemsToRemove.add(id);
1948 continue;
1949 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001950 } else if (itemType ==
1951 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Kenny Guyed131872014-04-30 03:02:21 +01001952 info = getShortcutInfo(manager, intent, user, context, c, iconIndex,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001953 titleIndex, mLabelCache);
1954 } else {
1955 info = getShortcutInfo(c, context, iconTypeIndex,
1956 iconPackageIndex, iconResourceIndex, iconIndex,
1957 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001958
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 // App shortcuts that used to be automatically added to Launcher
1960 // didn't always have the correct intent flags set, so do that
1961 // here
1962 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001963 intent.getCategories() != null &&
1964 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001965 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 intent.addFlags(
1967 Intent.FLAG_ACTIVITY_NEW_TASK |
1968 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1969 }
Michael Jurka96879562012-03-22 05:54:33 -07001970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001973 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 container = c.getInt(containerIndex);
1976 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 info.cellX = c.getInt(cellXIndex);
1979 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001980 info.spanX = 1;
1981 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01001982 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Adam Cohenae4409d2013-11-26 10:34:59 -08001983
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001985 deleteOnInvalidPlacement.set(false);
1986 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1987 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001988 itemsToRemove.add(id);
1989 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 break;
1991 }
1992
1993 switch (container) {
1994 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1995 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1996 sBgWorkspaceItems.add(info);
1997 break;
1998 default:
1999 // Item is in a user folder
2000 FolderInfo folderInfo =
2001 findOrMakeFolder(sBgFolders, container);
2002 folderInfo.add(info);
2003 break;
2004 }
2005 sBgItemsIdMap.put(info.id, info);
2006
2007 // now that we've loaded everthing re-save it with the
2008 // icon in case it disappears somehow.
2009 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002010 } else {
2011 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 }
2013 break;
2014
2015 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2016 id = c.getLong(idIndex);
2017 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2018
2019 folderInfo.title = c.getString(titleIndex);
2020 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002023 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 folderInfo.cellX = c.getInt(cellXIndex);
2025 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002026 folderInfo.spanX = 1;
2027 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028
Daniel Sandler8802e962010-05-26 16:28:16 -04002029 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002030 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002031 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002032 deleteOnInvalidPlacement)) {
2033 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002034 itemsToRemove.add(id);
2035 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002036 break;
2037 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002038
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2041 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2042 sBgWorkspaceItems.add(folderInfo);
2043 break;
Joe Onorato36115782010-06-17 13:28:48 -04002044 }
Joe Onorato17a89222011-02-08 17:26:11 -08002045
Chris Wrenf4d08112014-01-16 18:13:56 -05002046 if (restored) {
2047 // no special handling required for restored folders
2048 restoredRows.add(id);
2049 }
2050
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2052 sBgFolders.put(folderInfo.id, folderInfo);
2053 break;
2054
2055 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2056 // Read all Launcher-specific widget details
2057 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002058 String savedProvider = c.getString(appWidgetProviderIndex);
2059
Joe Onorato36115782010-06-17 13:28:48 -04002060 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002061
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 final AppWidgetProviderInfo provider =
2063 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002064
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 if (!isSafeMode && (provider == null || provider.provider == null ||
2066 provider.provider.getPackageName() == null)) {
2067 String log = "Deleting widget that isn't installed anymore: id="
2068 + id + " appWidgetId=" + appWidgetId;
2069 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002070 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 itemsToRemove.add(id);
2072 } else {
2073 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2074 provider.provider);
2075 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002076 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 appWidgetInfo.cellX = c.getInt(cellXIndex);
2078 appWidgetInfo.cellY = c.getInt(cellYIndex);
2079 appWidgetInfo.spanX = c.getInt(spanXIndex);
2080 appWidgetInfo.spanY = c.getInt(spanYIndex);
2081 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2082 appWidgetInfo.minSpanX = minSpan[0];
2083 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002084
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 container = c.getInt(containerIndex);
2086 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2087 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2088 Log.e(TAG, "Widget found where container != " +
2089 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2090 continue;
2091 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092
Adam Cohene25af792013-06-06 23:08:25 -07002093 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002095 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002096 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002097 deleteOnInvalidPlacement)) {
2098 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002099 itemsToRemove.add(id);
2100 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 break;
2102 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002103 String providerName = provider.provider.flattenToString();
2104 if (!providerName.equals(savedProvider)) {
2105 ContentValues values = new ContentValues();
2106 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2107 providerName);
2108 String where = BaseColumns._ID + "= ?";
2109 String[] args = {Integer.toString(c.getInt(idIndex))};
2110 contentResolver.update(contentUri, values, where, args);
2111 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2113 sBgAppWidgets.add(appWidgetInfo);
2114 }
Joe Onorato36115782010-06-17 13:28:48 -04002115 break;
2116 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002118 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002119 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 }
2121 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002122 if (c != null) {
2123 c.close();
2124 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 }
2126
Winson Chungba9c37f2013-08-30 14:11:37 -07002127 // Break early if we've stopped loading
2128 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002129 clearSBgDataStructures();
2130 return false;
2131 }
2132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 if (itemsToRemove.size() > 0) {
2134 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002135 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 // Remove dead items
2137 for (long id : itemsToRemove) {
2138 if (DEBUG_LOADERS) {
2139 Log.d(TAG, "Removed id = " + id);
2140 }
2141 // Don't notify content observers
2142 try {
2143 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2144 null, null);
2145 } catch (RemoteException e) {
2146 Log.w(TAG, "Could not remove id = " + id);
2147 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002148 }
2149 }
2150
Chris Wrenf4d08112014-01-16 18:13:56 -05002151 if (restoredRows.size() > 0) {
2152 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2153 LauncherSettings.Favorites.CONTENT_URI);
2154 // Update restored items that no longer require special handling
2155 try {
2156 StringBuilder selectionBuilder = new StringBuilder();
2157 selectionBuilder.append(LauncherSettings.Favorites._ID);
2158 selectionBuilder.append(" IN (");
2159 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2160 selectionBuilder.append(")");
2161 ContentValues values = new ContentValues();
2162 values.put(LauncherSettings.Favorites.RESTORED, 0);
2163 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2164 values, selectionBuilder.toString(), null);
2165 } catch (RemoteException e) {
2166 Log.w(TAG, "Could not update restored rows");
2167 }
2168 }
2169
Winson Chungc763c4e2013-07-19 13:49:06 -07002170 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002171 long maxScreenId = 0;
2172 // If we're importing we use the old screen order.
2173 for (ItemInfo item: sBgItemsIdMap.values()) {
2174 long screenId = item.screenId;
2175 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2176 !sBgWorkspaceScreens.contains(screenId)) {
2177 sBgWorkspaceScreens.add(screenId);
2178 if (screenId > maxScreenId) {
2179 maxScreenId = screenId;
2180 }
2181 }
2182 }
2183 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002184 // Log to disk
2185 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2186 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2187 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002188
Michael Jurka414300a2013-08-27 15:42:35 +02002189 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002190 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002191
2192 // Update the max item id after we load an old db
2193 long maxItemId = 0;
2194 // If we're importing we use the old screen order.
2195 for (ItemInfo item: sBgItemsIdMap.values()) {
2196 maxItemId = Math.max(maxItemId, item.id);
2197 }
Michael Jurka414300a2013-08-27 15:42:35 +02002198 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002199 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002200 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2201 for (Integer i : orderedScreens.keySet()) {
2202 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002204 // Log to disk
2205 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2206 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002207
2208 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002209 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002210 for (ItemInfo item: sBgItemsIdMap.values()) {
2211 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2213 unusedScreens.contains(screenId)) {
2214 unusedScreens.remove(screenId);
2215 }
2216 }
2217
2218 // If there are any empty screens remove them, and update.
2219 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002220 // Log to disk
2221 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2222 TextUtils.join(", ", unusedScreens), true);
2223
Winson Chung933bae62013-08-29 11:42:30 -07002224 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2226 }
2227 }
2228
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 if (DEBUG_LOADERS) {
2230 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2231 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002233 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002235
Daniel Sandler566da102013-06-25 23:43:45 -04002236 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002237 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002238 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002239 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 line += " | ";
2241 }
Winson Chung892c74d2013-08-22 16:15:50 -07002242 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002243 ItemInfo[][] screen = occupied.get(screenId);
2244 if (x < screen.length && y < screen[x].length) {
2245 line += (screen[x][y] != null) ? "#" : ".";
2246 } else {
2247 line += "!";
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 }
Joe Onorato36115782010-06-17 13:28:48 -04002250 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002252 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002253 }
Joe Onorato36115782010-06-17 13:28:48 -04002254 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002255 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002256 }
2257
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 /** Filters the set of items who are directly or indirectly (via another container) on the
2259 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002260 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 ArrayList<ItemInfo> allWorkspaceItems,
2262 ArrayList<ItemInfo> currentScreenItems,
2263 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002264 // Purge any null ItemInfos
2265 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2266 while (iter.hasNext()) {
2267 ItemInfo i = iter.next();
2268 if (i == null) {
2269 iter.remove();
2270 }
2271 }
2272
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 // Order the set of items by their containers first, this allows use to walk through the
2274 // list sequentially, build up a list of containers that are in the specified screen,
2275 // as well as all items in those containers.
2276 Set<Long> itemsOnScreen = new HashSet<Long>();
2277 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2278 @Override
2279 public int compare(ItemInfo lhs, ItemInfo rhs) {
2280 return (int) (lhs.container - rhs.container);
2281 }
2282 });
2283 for (ItemInfo info : allWorkspaceItems) {
2284 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002285 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 currentScreenItems.add(info);
2287 itemsOnScreen.add(info.id);
2288 } else {
2289 otherScreenItems.add(info);
2290 }
2291 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2292 currentScreenItems.add(info);
2293 itemsOnScreen.add(info.id);
2294 } else {
2295 if (itemsOnScreen.contains(info.container)) {
2296 currentScreenItems.add(info);
2297 itemsOnScreen.add(info.id);
2298 } else {
2299 otherScreenItems.add(info);
2300 }
2301 }
2302 }
2303 }
2304
2305 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002306 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002307 ArrayList<LauncherAppWidgetInfo> appWidgets,
2308 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2309 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310
2311 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002312 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002314 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 currentScreenWidgets.add(widget);
2316 } else {
2317 otherScreenWidgets.add(widget);
2318 }
2319 }
2320 }
2321
2322 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002323 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 HashMap<Long, ItemInfo> itemsIdMap,
2325 HashMap<Long, FolderInfo> folders,
2326 HashMap<Long, FolderInfo> currentScreenFolders,
2327 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328
2329 for (long id : folders.keySet()) {
2330 ItemInfo info = itemsIdMap.get(id);
2331 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002332 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002334 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002335 currentScreenFolders.put(id, folder);
2336 } else {
2337 otherScreenFolders.put(id, folder);
2338 }
2339 }
2340 }
2341
2342 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2343 * right) */
2344 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002345 final LauncherAppState app = LauncherAppState.getInstance();
2346 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 // XXX: review this
2348 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002349 @Override
2350 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002351 int cellCountX = (int) grid.numColumns;
2352 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002353 int screenOffset = cellCountX * cellCountY;
2354 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002356 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002358 rhs.cellY * cellCountX + rhs.cellX);
2359 return (int) (lr - rr);
2360 }
2361 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002362 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002363
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2365 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002366 final Runnable r = new Runnable() {
2367 @Override
2368 public void run() {
2369 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2370 if (callbacks != null) {
2371 callbacks.bindScreens(orderedScreens);
2372 }
2373 }
2374 };
2375 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2376 }
2377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002378 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2379 final ArrayList<ItemInfo> workspaceItems,
2380 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2381 final HashMap<Long, FolderInfo> folders,
2382 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002383
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 final boolean postOnMainThread = (deferredBindRunnables != null);
2385
2386 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002387 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002388 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002389 final int start = i;
2390 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002391 final Runnable r = new Runnable() {
2392 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002394 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002396 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2397 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 }
2399 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002400 };
2401 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002402 synchronized (deferredBindRunnables) {
2403 deferredBindRunnables.add(r);
2404 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002405 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002406 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002407 }
Joe Onorato36115782010-06-17 13:28:48 -04002408 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409
2410 // Bind the folders
2411 if (!folders.isEmpty()) {
2412 final Runnable r = new Runnable() {
2413 public void run() {
2414 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2415 if (callbacks != null) {
2416 callbacks.bindFolders(folders);
2417 }
2418 }
2419 };
2420 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002421 synchronized (deferredBindRunnables) {
2422 deferredBindRunnables.add(r);
2423 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002425 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 }
2427 }
2428
2429 // Bind the widgets, one at a time
2430 N = appWidgets.size();
2431 for (int i = 0; i < N; i++) {
2432 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2433 final Runnable r = new Runnable() {
2434 public void run() {
2435 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2436 if (callbacks != null) {
2437 callbacks.bindAppWidget(widget);
2438 }
2439 }
2440 };
2441 if (postOnMainThread) {
2442 deferredBindRunnables.add(r);
2443 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002444 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002445 }
2446 }
2447 }
2448
2449 /**
2450 * Binds all loaded data to actual views on the main thread.
2451 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002452 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002453 final long t = SystemClock.uptimeMillis();
2454 Runnable r;
2455
2456 // Don't use these two variables in any of the callback runnables.
2457 // Otherwise we hold a reference to them.
2458 final Callbacks oldCallbacks = mCallbacks.get();
2459 if (oldCallbacks == null) {
2460 // This launcher has exited and nobody bothered to tell us. Just bail.
2461 Log.w(TAG, "LoaderTask running with no launcher");
2462 return;
2463 }
2464
Winson Chung9b9fb962013-11-15 15:39:34 -08002465 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002466 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2467 ArrayList<LauncherAppWidgetInfo> appWidgets =
2468 new ArrayList<LauncherAppWidgetInfo>();
2469 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2470 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002471 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002472 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 workspaceItems.addAll(sBgWorkspaceItems);
2474 appWidgets.addAll(sBgAppWidgets);
2475 folders.putAll(sBgFolders);
2476 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002477 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002478 }
2479
Derek Prothro7aff3992013-12-10 14:00:37 -05002480 final boolean isLoadingSynchronously =
2481 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002482 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002483 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002484 if (currScreen >= orderedScreenIds.size()) {
2485 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002486 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002487 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002488 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002489 final long currentScreenId = currentScreen < 0
2490 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002491
2492 // Load all the items that are on the current page first (and in the process, unbind
2493 // all the existing workspace items before we call startBinding() below.
2494 unbindWorkspaceItemsOnMainThread();
2495
2496 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002497 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2498 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2499 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2500 new ArrayList<LauncherAppWidgetInfo>();
2501 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2502 new ArrayList<LauncherAppWidgetInfo>();
2503 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2504 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2505
Winson Chung9b9fb962013-11-15 15:39:34 -08002506 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002507 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002508 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002510 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511 otherFolders);
2512 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2513 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2514
2515 // Tell the workspace that we're about to start binding items
2516 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002517 public void run() {
2518 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2519 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002521 }
2522 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002523 };
Winson Chung81b52252012-08-27 15:34:29 -07002524 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002525
Adam Cohendcd297f2013-06-18 13:13:40 -07002526 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2527
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 // Load items on the current page
2529 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2530 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002531 if (isLoadingSynchronously) {
2532 r = new Runnable() {
2533 public void run() {
2534 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002535 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002536 callbacks.onPageBoundSynchronously(currentScreen);
2537 }
2538 }
2539 };
Winson Chung81b52252012-08-27 15:34:29 -07002540 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002541 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002542
Winson Chung4a2afa32012-07-19 14:53:05 -07002543 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2544 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002545 synchronized (mDeferredBindRunnables) {
2546 mDeferredBindRunnables.clear();
2547 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002549 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002550
2551 // Tell the workspace that we're done binding items
2552 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002553 public void run() {
2554 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2555 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002556 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002557 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002558
Winson Chung98e030b2012-05-07 16:01:11 -07002559 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002560 if (DEBUG_LOADERS) {
2561 Log.d(TAG, "bound workspace in "
2562 + (SystemClock.uptimeMillis()-t) + "ms");
2563 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002564
2565 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002566 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002567 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002568 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002569 synchronized (mDeferredBindRunnables) {
2570 mDeferredBindRunnables.add(r);
2571 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002572 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002573 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002574 }
Joe Onorato36115782010-06-17 13:28:48 -04002575 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002576
Joe Onorato36115782010-06-17 13:28:48 -04002577 private void loadAndBindAllApps() {
2578 if (DEBUG_LOADERS) {
2579 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2580 }
2581 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002582 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002583 synchronized (LoaderTask.this) {
2584 if (mStopped) {
2585 return;
2586 }
2587 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002588 }
Joe Onorato36115782010-06-17 13:28:48 -04002589 } else {
2590 onlyBindAllApps();
2591 }
2592 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002593
Joe Onorato36115782010-06-17 13:28:48 -04002594 private void onlyBindAllApps() {
2595 final Callbacks oldCallbacks = mCallbacks.get();
2596 if (oldCallbacks == null) {
2597 // This launcher has exited and nobody bothered to tell us. Just bail.
2598 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2599 return;
2600 }
2601
2602 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002603 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002604 final ArrayList<AppInfo> list
2605 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002606 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002607 public void run() {
2608 final long t = SystemClock.uptimeMillis();
2609 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2610 if (callbacks != null) {
2611 callbacks.bindAllApplications(list);
2612 }
2613 if (DEBUG_LOADERS) {
2614 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2615 + (SystemClock.uptimeMillis()-t) + "ms");
2616 }
2617 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002618 };
2619 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002620 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002621 r.run();
2622 } else {
2623 mHandler.post(r);
2624 }
Joe Onorato36115782010-06-17 13:28:48 -04002625 }
2626
Winson Chung64359a52013-07-08 17:17:08 -07002627 private void loadAllApps() {
2628 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002629
Joe Onorato36115782010-06-17 13:28:48 -04002630 final Callbacks oldCallbacks = mCallbacks.get();
2631 if (oldCallbacks == null) {
2632 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002633 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002634 return;
2635 }
2636
2637 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2638 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2639
Kenny Guyed131872014-04-30 03:02:21 +01002640 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2641
Winson Chung64359a52013-07-08 17:17:08 -07002642 // Clear the list of apps
2643 mBgAllAppsList.clear();
Kenny Guyed131872014-04-30 03:02:21 +01002644 for (UserHandleCompat user : profiles) {
2645 // Query for the set of apps
2646 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2647 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2648 if (DEBUG_LOADERS) {
2649 Log.d(TAG, "getActivityList took "
2650 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2651 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2652 }
2653 // Fail if we don't have any apps
2654 if (apps == null || apps.isEmpty()) {
2655 return;
2656 }
2657 // Sort the applications by name
2658 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2659 Collections.sort(apps,
2660 new LauncherModel.ShortcutNameComparator(mLabelCache));
2661 if (DEBUG_LOADERS) {
2662 Log.d(TAG, "sort took "
2663 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2664 }
Joe Onorato36115782010-06-17 13:28:48 -04002665
Kenny Guyed131872014-04-30 03:02:21 +01002666 // Create the ApplicationInfos
2667 for (int i = 0; i < apps.size(); i++) {
2668 LauncherActivityInfoCompat app = apps.get(i);
2669 // This builds the icon bitmaps.
2670 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2671 }
Winson Chung64359a52013-07-08 17:17:08 -07002672 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002673 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002674 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2675 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002676
2677 // Post callback on main thread
2678 mHandler.post(new Runnable() {
2679 public void run() {
2680 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002681 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002682 if (callbacks != null) {
2683 callbacks.bindAllApplications(added);
2684 if (DEBUG_LOADERS) {
2685 Log.d(TAG, "bound " + added.size() + " apps in "
2686 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2687 }
2688 } else {
2689 Log.i(TAG, "not binding apps: no Launcher activity");
2690 }
2691 }
2692 });
2693
Joe Onorato36115782010-06-17 13:28:48 -04002694 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002695 Log.d(TAG, "Icons processed in "
2696 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002697 }
2698 }
2699
2700 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002701 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002702 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2703 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2704 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2705 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2706 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2707 }
Joe Onorato36115782010-06-17 13:28:48 -04002708 }
2709 }
2710
2711 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002712 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002713 }
2714
2715 private class PackageUpdatedTask implements Runnable {
2716 int mOp;
2717 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002718 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002719
2720 public static final int OP_NONE = 0;
2721 public static final int OP_ADD = 1;
2722 public static final int OP_UPDATE = 2;
2723 public static final int OP_REMOVE = 3; // uninstlled
2724 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2725
2726
Kenny Guyed131872014-04-30 03:02:21 +01002727 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002728 mOp = op;
2729 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002730 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002731 }
2732
2733 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002734 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002735
2736 final String[] packages = mPackages;
2737 final int N = packages.length;
2738 switch (mOp) {
2739 case OP_ADD:
2740 for (int i=0; i<N; i++) {
2741 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002742 mIconCache.remove(packages[i], mUser);
2743 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002744 }
2745 break;
2746 case OP_UPDATE:
2747 for (int i=0; i<N; i++) {
2748 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002749 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002750 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002751 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002752 }
2753 break;
2754 case OP_REMOVE:
2755 case OP_UNAVAILABLE:
2756 for (int i=0; i<N; i++) {
2757 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002758 mBgAllAppsList.removePackage(packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002759 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002760 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002761 }
2762 break;
2763 }
2764
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002765 ArrayList<AppInfo> added = null;
2766 ArrayList<AppInfo> modified = null;
2767 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002768
Adam Cohen487f7dd2012-06-28 18:12:10 -07002769 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002770 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002771 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002772 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002773 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002774 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002775 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002776 }
Winson Chung5d55f332012-07-16 20:45:03 -07002777 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002778 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002779 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002780 }
2781
Joe Onorato36115782010-06-17 13:28:48 -04002782 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2783 if (callbacks == null) {
2784 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2785 return;
2786 }
2787
2788 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002789 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002790 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002791 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002792 addAndBindAddedWorkspaceApps(context, addedInfos);
2793 } else {
2794 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002795 }
Joe Onorato36115782010-06-17 13:28:48 -04002796 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002797
Joe Onorato36115782010-06-17 13:28:48 -04002798 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002799 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002800
2801 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002802 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002803 ArrayList<ItemInfo> infos =
Kenny Guyed131872014-04-30 03:02:21 +01002804 getItemInfoForComponentName(a.componentName, mUser);
Winson Chung64359a52013-07-08 17:17:08 -07002805 for (ItemInfo i : infos) {
2806 if (isShortcutInfoUpdateable(i)) {
2807 ShortcutInfo info = (ShortcutInfo) i;
2808 info.title = a.title.toString();
2809 updateItemInDatabase(context, info);
2810 }
2811 }
2812 }
2813
Joe Onorato36115782010-06-17 13:28:48 -04002814 mHandler.post(new Runnable() {
2815 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002816 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2817 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002818 callbacks.bindAppsUpdated(modifiedFinal);
2819 }
2820 }
2821 });
2822 }
Winson Chung83892cc2013-05-01 16:53:33 -07002823
Winson Chungdf95eb12013-10-16 14:57:07 -07002824 final ArrayList<String> removedPackageNames =
2825 new ArrayList<String>();
2826 if (mOp == OP_REMOVE) {
2827 // Mark all packages in the broadcast to be removed
2828 removedPackageNames.addAll(Arrays.asList(packages));
2829 } else if (mOp == OP_UPDATE) {
2830 // Mark disabled packages in the broadcast to be removed
2831 final PackageManager pm = context.getPackageManager();
2832 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01002833 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002834 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002835 }
2836 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002837 }
2838 // Remove all the components associated with this package
2839 for (String pn : removedPackageNames) {
Kenny Guyed131872014-04-30 03:02:21 +01002840 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002841 for (ItemInfo i : infos) {
2842 deleteItemFromDatabase(context, i);
2843 }
2844 }
2845 // Remove all the specific components
2846 for (AppInfo a : removedApps) {
Kenny Guyed131872014-04-30 03:02:21 +01002847 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07002848 for (ItemInfo i : infos) {
2849 deleteItemFromDatabase(context, i);
2850 }
2851 }
2852 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2853 // Remove any queued items from the install queue
2854 String spKey = LauncherAppState.getSharedPreferencesKey();
2855 SharedPreferences sp =
2856 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2857 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2858 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002859 mHandler.post(new Runnable() {
2860 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002861 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2862 if (callbacks == cb && cb != null) {
Kenny Guyed131872014-04-30 03:02:21 +01002863 callbacks.bindComponentsRemoved(removedPackageNames, removedApps, mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002864 }
2865 }
2866 });
Joe Onoratobe386092009-11-17 17:32:16 -08002867 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002868
Michael Jurkac402cd92013-05-20 15:49:32 +02002869 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01002870 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002871 mHandler.post(new Runnable() {
2872 @Override
2873 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002874 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2875 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002876 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002877 }
2878 }
2879 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002880
2881 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002882 mHandler.post(new Runnable() {
2883 public void run() {
2884 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2885 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002886 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002887 }
2888 }
2889 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 }
2891 }
2892
Michael Jurkac402cd92013-05-20 15:49:32 +02002893 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2894 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2895 PackageManager packageManager = context.getPackageManager();
2896 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2897 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2898 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2899 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2900 Collections.sort(widgetsAndShortcuts,
2901 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2902 return widgetsAndShortcuts;
2903 }
2904
Kenny Guyed131872014-04-30 03:02:21 +01002905 private static boolean isPackageDisabled(Context context, String packageName,
2906 UserHandleCompat user) {
2907 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2908 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07002909 }
Adam Cohen556f6132014-01-15 15:18:08 -08002910
Kenny Guyed131872014-04-30 03:02:21 +01002911 public static boolean isValidPackageActivity(Context context, ComponentName cn,
2912 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07002913 if (cn == null) {
2914 return false;
2915 }
Kenny Guyed131872014-04-30 03:02:21 +01002916 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
2917 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002918 return false;
2919 }
Kenny Guyed131872014-04-30 03:02:21 +01002920 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07002921 }
2922
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002924 * Make an ShortcutInfo object for a restored application or shortcut item that points
2925 * to a package that is not yet installed on the system.
2926 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002927 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002928 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002929 if (cursor != null) {
2930 info.title = cursor.getString(titleIndex);
2931 } else {
2932 info.title = "";
2933 }
Kenny Guyed131872014-04-30 03:02:21 +01002934 info.user = UserHandleCompat.myUserHandle();
2935 info.setIcon(mIconCache.getIcon(intent, info.title.toString(), info.user));
Chris Wrenf4d08112014-01-16 18:13:56 -05002936 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002937 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002938 return info;
2939 }
2940
2941 /**
2942 * Make an Intent object for a restored application or shortcut item that points
2943 * to the market page for the item.
2944 */
2945 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002946 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002947 ComponentName componentName = intent.getComponent();
2948 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2949 Uri marketUri = new Uri.Builder()
2950 .scheme("market")
2951 .authority("details")
2952 .appendQueryParameter("id", componentName.getPackageName())
2953 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002954 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002955 marketIntent.setData(marketUri);
2956 return marketIntent;
2957 }
2958
2959 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002960 * This is called from the code that adds shortcuts from the intent receiver. This
2961 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 */
Kenny Guyed131872014-04-30 03:02:21 +01002963 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
2964 UserHandleCompat user, Context context) {
2965 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967
Joe Onorato56d82912010-03-07 14:32:10 -05002968 /**
2969 * Make an ShortcutInfo object for a shortcut that is an application.
2970 *
2971 * If c is not null, then it will be used to fill in missing data like the title and icon.
2972 */
Kenny Guyed131872014-04-30 03:02:21 +01002973 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
2974 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
2975 HashMap<Object, CharSequence> labelCache) {
2976 if (user == null) {
2977 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002978 return null;
2979 }
2980
Kenny Guyed131872014-04-30 03:02:21 +01002981 ComponentName componentName = intent.getComponent();
2982 if (componentName == null) {
2983 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
2984 return null;
2985 }
2986
2987 Intent newIntent = new Intent(intent.getAction(), null);
2988 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2989 newIntent.setComponent(componentName);
2990 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
2991 if (lai == null) {
2992 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
2993 return null;
2994 }
2995
2996 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002997
Joe Onorato56d82912010-03-07 14:32:10 -05002998 // the resource -- This may implicitly give us back the fallback icon,
2999 // but don't worry about that. All we're doing with usingFallbackIcon is
3000 // to avoid saving lots of copies of that in the database, and most apps
3001 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003002 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003003
Joe Onorato56d82912010-03-07 14:32:10 -05003004 // the db
3005 if (icon == null) {
3006 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003007 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003008 }
3009 }
3010 // the fallback icon
3011 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003012 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003013 info.usingFallbackIcon = true;
3014 }
3015 info.setIcon(icon);
3016
Kenny Guyed131872014-04-30 03:02:21 +01003017 // From the cache.
3018 if (labelCache != null) {
3019 info.title = labelCache.get(componentName);
3020 }
3021
Joe Onorato56d82912010-03-07 14:32:10 -05003022 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003023 if (info.title == null && lai != null) {
3024 info.title = lai.getLabel();
3025 if (labelCache != null) {
3026 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003027 }
Joe Onorato56d82912010-03-07 14:32:10 -05003028 }
3029 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003031 if (c != null) {
3032 info.title = c.getString(titleIndex);
3033 }
3034 }
3035 // fall back to the class name of the activity
3036 if (info.title == null) {
3037 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003040 info.user = user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003043
Winson Chung64359a52013-07-08 17:17:08 -07003044 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3045 ItemInfoFilter f) {
3046 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3047 for (ItemInfo i : infos) {
3048 if (i instanceof ShortcutInfo) {
3049 ShortcutInfo info = (ShortcutInfo) i;
3050 ComponentName cn = info.intent.getComponent();
3051 if (cn != null && f.filterItem(null, info, cn)) {
3052 filtered.add(info);
3053 }
3054 } else if (i instanceof FolderInfo) {
3055 FolderInfo info = (FolderInfo) i;
3056 for (ShortcutInfo s : info.contents) {
3057 ComponentName cn = s.intent.getComponent();
3058 if (cn != null && f.filterItem(info, s, cn)) {
3059 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003060 }
3061 }
Winson Chung64359a52013-07-08 17:17:08 -07003062 } else if (i instanceof LauncherAppWidgetInfo) {
3063 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3064 ComponentName cn = info.providerName;
3065 if (cn != null && f.filterItem(null, info, cn)) {
3066 filtered.add(info);
3067 }
Winson Chung8a435102012-08-30 17:16:53 -07003068 }
3069 }
Winson Chung64359a52013-07-08 17:17:08 -07003070 return new ArrayList<ItemInfo>(filtered);
3071 }
3072
Kenny Guyed131872014-04-30 03:02:21 +01003073 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn,
3074 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003075 ItemInfoFilter filter = new ItemInfoFilter() {
3076 @Override
3077 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003078 return cn.getPackageName().equals(pn) && info.user.equals(user);
Winson Chung64359a52013-07-08 17:17:08 -07003079 }
3080 };
3081 return filterItemInfos(sBgItemsIdMap.values(), filter);
3082 }
3083
Kenny Guyed131872014-04-30 03:02:21 +01003084 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3085 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003086 ItemInfoFilter filter = new ItemInfoFilter() {
3087 @Override
3088 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003089 if (info.user == null) {
3090 return cn.equals(cname);
3091 } else {
3092 return cn.equals(cname) && info.user.equals(user);
3093 }
Winson Chung64359a52013-07-08 17:17:08 -07003094 }
3095 };
3096 return filterItemInfos(sBgItemsIdMap.values(), filter);
3097 }
3098
3099 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3100 if (i instanceof ShortcutInfo) {
3101 ShortcutInfo info = (ShortcutInfo) i;
3102 // We need to check for ACTION_MAIN otherwise getComponent() might
3103 // return null for some shortcuts (for instance, for shortcuts to
3104 // web pages.)
3105 Intent intent = info.intent;
3106 ComponentName name = intent.getComponent();
3107 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3108 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3109 return true;
3110 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003111 // placeholder shortcuts get special treatment, let them through too.
3112 if (info.getRestoredIntent() != null) {
3113 return true;
3114 }
Winson Chung64359a52013-07-08 17:17:08 -07003115 }
3116 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003117 }
3118
3119 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003120 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003122 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003123 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3124 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125
Joe Onorato56d82912010-03-07 14:32:10 -05003126 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003127 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003128 // Non-app shortcuts are only supported for current user.
3129 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003132 // TODO: If there's an explicit component and we can't install that, delete it.
3133
Joe Onorato56d82912010-03-07 14:32:10 -05003134 info.title = c.getString(titleIndex);
3135
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 int iconType = c.getInt(iconTypeIndex);
3137 switch (iconType) {
3138 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3139 String packageName = c.getString(iconPackageIndex);
3140 String resourceName = c.getString(iconResourceIndex);
3141 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003142 info.customIcon = false;
3143 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003146 if (resources != null) {
3147 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003148 icon = Utilities.createIconBitmap(
3149 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003152 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
Joe Onorato56d82912010-03-07 14:32:10 -05003154 // the db
3155 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003156 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003157 }
3158 // the fallback icon
3159 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003160 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003161 info.usingFallbackIcon = true;
3162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 break;
3164 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003165 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003166 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003167 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003168 info.customIcon = false;
3169 info.usingFallbackIcon = true;
3170 } else {
3171 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003173 break;
3174 default:
Kenny Guyed131872014-04-30 03:02:21 +01003175 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003176 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 info.customIcon = false;
3178 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003180 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003181 return info;
3182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183
Michael Jurka931dc972011-08-05 15:08:15 -07003184 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003185 @SuppressWarnings("all") // suppress dead code warning
3186 final boolean debug = false;
3187 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003188 Log.d(TAG, "getIconFromCursor app="
3189 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3190 }
3191 byte[] data = c.getBlob(iconIndex);
3192 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003193 return Utilities.createIconBitmap(
3194 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003195 } catch (Exception e) {
3196 return null;
3197 }
3198 }
3199
Winson Chung3d503fb2011-07-13 17:25:49 -07003200 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3201 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003202 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003203 if (info == null) {
3204 return null;
3205 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003207
3208 return info;
3209 }
3210
Winson Chunga9abd0e2010-10-27 17:18:37 -07003211 /**
Winson Chung55cef262010-10-28 14:14:18 -07003212 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3213 */
3214 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3215 ComponentName component) {
3216 List<AppWidgetProviderInfo> widgets =
3217 AppWidgetManager.getInstance(context).getInstalledProviders();
3218 for (AppWidgetProviderInfo info : widgets) {
3219 if (info.provider.equals(component)) {
3220 return info;
3221 }
3222 }
3223 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003224 }
3225
Winson Chung68846fd2010-10-29 11:00:27 -07003226 /**
3227 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3228 */
3229 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3230 final PackageManager packageManager = context.getPackageManager();
3231 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3232 new ArrayList<WidgetMimeTypeHandlerData>();
3233
3234 final Intent supportsIntent =
3235 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3236 supportsIntent.setType(mimeType);
3237
3238 // Create a set of widget configuration components that we can test against
3239 final List<AppWidgetProviderInfo> widgets =
3240 AppWidgetManager.getInstance(context).getInstalledProviders();
3241 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3242 new HashMap<ComponentName, AppWidgetProviderInfo>();
3243 for (AppWidgetProviderInfo info : widgets) {
3244 configurationComponentToWidget.put(info.configure, info);
3245 }
3246
3247 // Run through each of the intents that can handle this type of clip data, and cross
3248 // reference them with the components that are actual configuration components
3249 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3250 PackageManager.MATCH_DEFAULT_ONLY);
3251 for (ResolveInfo info : activities) {
3252 final ActivityInfo activityInfo = info.activityInfo;
3253 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3254 activityInfo.name);
3255 if (configurationComponentToWidget.containsKey(infoComponent)) {
3256 supportedConfigurationActivities.add(
3257 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3258 configurationComponentToWidget.get(infoComponent)));
3259 }
3260 }
3261 return supportedConfigurationActivities;
3262 }
3263
Winson Chunga9abd0e2010-10-27 17:18:37 -07003264 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003265 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3266 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3267 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3268
Adam Cohend9198822011-11-22 16:42:47 -08003269 if (intent == null) {
3270 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3271 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3272 return null;
3273 }
3274
Joe Onorato0589f0f2010-02-08 13:44:00 -08003275 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003276 boolean customIcon = false;
3277 ShortcutIconResource iconResource = null;
3278
3279 if (bitmap != null && bitmap instanceof Bitmap) {
3280 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003281 customIcon = true;
3282 } else {
3283 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3284 if (extra != null && extra instanceof ShortcutIconResource) {
3285 try {
3286 iconResource = (ShortcutIconResource) extra;
3287 final PackageManager packageManager = context.getPackageManager();
3288 Resources resources = packageManager.getResourcesForApplication(
3289 iconResource.packageName);
3290 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003291 icon = Utilities.createIconBitmap(
Kenny Guyed131872014-04-30 03:02:21 +01003292 mIconCache.getFullResIcon(resources, id),
3293 context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003294 } catch (Exception e) {
3295 Log.w(TAG, "Could not load shortcut icon: " + extra);
3296 }
3297 }
3298 }
3299
Michael Jurkac9d95c52011-08-29 14:03:34 -07003300 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003301
Kenny Guyed131872014-04-30 03:02:21 +01003302 // Only support intents for current user for now. Intents sent from other
3303 // users wouldn't get here without intent forwarding anyway.
3304 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003305 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003306 if (fallbackIcon != null) {
3307 icon = fallbackIcon;
3308 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003309 icon = mIconCache.getDefaultIcon(info.user);
Winson Chunga9abd0e2010-10-27 17:18:37 -07003310 info.usingFallbackIcon = true;
3311 }
Joe Onorato56d82912010-03-07 14:32:10 -05003312 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003313 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003314
Joe Onorato0589f0f2010-02-08 13:44:00 -08003315 info.title = name;
3316 info.intent = intent;
3317 info.customIcon = customIcon;
3318 info.iconResource = iconResource;
3319
3320 return info;
3321 }
3322
Winson Chungaac01e12011-08-17 10:37:13 -07003323 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3324 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003325 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003326 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003327 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003328 }
Joe Onorato56d82912010-03-07 14:32:10 -05003329 // If this icon doesn't have a custom icon, check to see
3330 // what's stored in the DB, and if it doesn't match what
3331 // we're going to show, store what we are going to show back
3332 // into the DB. We do this so when we're loading, if the
3333 // package manager can't find an icon (for example because
3334 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003335 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003336 cache.put(info, c.getBlob(iconIndex));
3337 return true;
3338 }
3339 return false;
3340 }
3341 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3342 boolean needSave = false;
3343 try {
3344 if (data != null) {
3345 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3346 Bitmap loaded = info.getIcon(mIconCache);
3347 needSave = !saved.sameAs(loaded);
3348 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003349 needSave = true;
3350 }
Winson Chungaac01e12011-08-17 10:37:13 -07003351 } catch (Exception e) {
3352 needSave = true;
3353 }
3354 if (needSave) {
3355 Log.d(TAG, "going to save icon bitmap for info=" + info);
3356 // This is slower than is ideal, but this only happens once
3357 // or when the app is updated with a new icon.
3358 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003359 }
3360 }
3361
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003363 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 * or make a new one.
3365 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003366 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 // See if a placeholder was created for us already
3368 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003369 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003370 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003371 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 folders.put(id, folderInfo);
3373 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003374 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003375 }
3376
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003377 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003378 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003379 return new Comparator<AppInfo>() {
3380 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003381 if (a.user.equals(b.user)) {
3382 int result = collator.compare(a.title.toString().trim(),
3383 b.title.toString().trim());
3384 if (result == 0) {
3385 result = a.componentName.compareTo(b.componentName);
3386 }
3387 return result;
3388 } else {
3389 // TODO Need to figure out rules for sorting
3390 // profiles, this puts work second.
3391 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003392 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003393 }
Winson Chung11904872012-09-17 16:58:46 -07003394 };
3395 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003396 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3397 = new Comparator<AppInfo>() {
3398 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003399 if (a.firstInstallTime < b.firstInstallTime) return 1;
3400 if (a.firstInstallTime > b.firstInstallTime) return -1;
3401 return 0;
3402 }
3403 };
Winson Chung11904872012-09-17 16:58:46 -07003404 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3405 final Collator collator = Collator.getInstance();
3406 return new Comparator<AppWidgetProviderInfo>() {
3407 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003408 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003409 }
3410 };
3411 }
Winson Chung5308f242011-08-18 12:12:41 -07003412 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3413 if (info.activityInfo != null) {
3414 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3415 } else {
3416 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3417 }
3418 }
Kenny Guyed131872014-04-30 03:02:21 +01003419 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003420 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003421 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003422 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003423 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003424 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003425 }
Kenny Guyed131872014-04-30 03:02:21 +01003426 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003427 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003428 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003429 }
Kenny Guyed131872014-04-30 03:02:21 +01003430 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003431 CharSequence labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003432 ComponentName keyA = a.getComponentName();
3433 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003434 if (mLabelCache.containsKey(keyA)) {
3435 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003436 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003437 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003438
Winson Chung5308f242011-08-18 12:12:41 -07003439 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003440 }
Winson Chung5308f242011-08-18 12:12:41 -07003441 if (mLabelCache.containsKey(keyB)) {
3442 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003443 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003444 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003445
Winson Chung5308f242011-08-18 12:12:41 -07003446 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003447 }
Winson Chung11904872012-09-17 16:58:46 -07003448 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003449 }
3450 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003451 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003452 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003453 private PackageManager mPackageManager;
3454 private HashMap<Object, String> mLabelCache;
3455 WidgetAndShortcutNameComparator(PackageManager pm) {
3456 mPackageManager = pm;
3457 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003458 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003459 }
3460 public final int compare(Object a, Object b) {
3461 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003462 if (mLabelCache.containsKey(a)) {
3463 labelA = mLabelCache.get(a);
3464 } else {
3465 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003466 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003467 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003468 mLabelCache.put(a, labelA);
3469 }
3470 if (mLabelCache.containsKey(b)) {
3471 labelB = mLabelCache.get(b);
3472 } else {
3473 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003474 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003475 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003476 mLabelCache.put(b, labelB);
3477 }
Winson Chung11904872012-09-17 16:58:46 -07003478 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003479 }
3480 };
Joe Onoratobe386092009-11-17 17:32:16 -08003481
3482 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003483 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003484 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3485 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3486 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3487 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003488 if (mLoaderTask != null) {
3489 mLoaderTask.dumpState();
3490 } else {
3491 Log.d(TAG, "mLoaderTask=null");
3492 }
Joe Onoratobe386092009-11-17 17:32:16 -08003493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003494}