blob: 145d225dc9e33baa41bc7ac923ff57126b35a9e1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Dan Sandlerd5024042014-01-09 15:01:33 -050078 public static final int LOADER_FLAG_NONE = 0;
79 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
80 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
81
Joe Onorato36115782010-06-17 13:28:48 -040082 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050083 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080084
Winson Chungee055712013-07-30 14:46:24 -070085 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -080086 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -040087
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private final Object mLock = new Object();
90 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040091 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070092 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020093 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Winson Chung81b52252012-08-27 15:34:29 -070095 // Specific runnable types that are run on the main thread deferred handler, this allows us to
96 // clear all queued binding runnables when the Launcher activity is destroyed.
97 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
98 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
99
100
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700101 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
102 static {
103 sWorkerThread.start();
104 }
105 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
106
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 // We start off with everything not loaded. After that, we assume that
108 // our monitoring of the package manager provides all updates and we never
109 // need to do a requery. These are only ever touched from the loader thread.
110 private boolean mWorkspaceLoaded;
111 private boolean mAllAppsLoaded;
112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // When we are loading pages synchronously, we can't just post the binding of items on the side
114 // pages as this delays the rotation process. Instead, we wait for a callback from the first
115 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
116 // a normal load, we also clear this set of Runnables.
117 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700122 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800123
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700124 // The lock that must be acquired before referencing any static bg data structures. Unlike
125 // other locks, this one can generally be held long-term because we never expect any of these
126 // static data structures to be referenced outside of the worker thread except on the first
127 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700128 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
135 // created by LauncherModel that are directly on the home screen (however, no widgets or
136 // shortcuts within folders).
137 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
140 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 new ArrayList<LauncherAppWidgetInfo>();
142
Adam Cohen487f7dd2012-06-28 18:12:10 -0700143 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
144 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700145
Adam Cohen487f7dd2012-06-28 18:12:10 -0700146 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
147 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700148
149 // sBgWorkspaceScreens is the ordered set of workspace screens.
150 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
151
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 // </ only access in worker thread >
153
154 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800155 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Reena Lee99a73f32011-10-24 17:27:37 -0700157 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700160 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public int getCurrentWorkspaceScreen();
162 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700163 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
164 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700165 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700166 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500167 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700168 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200170 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700171 public void bindAppsAdded(ArrayList<Long> newScreens,
172 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700173 ArrayList<ItemInfo> addAnimated,
174 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200175 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500176 public void updatePackageState(String pkgName, int state);
Winson Chung83892cc2013-05-01 16:53:33 -0700177 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700178 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200179 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100180 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700181 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700182 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700183 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
Winson Chung64359a52013-07-08 17:17:08 -0700186 public interface ItemInfoFilter {
187 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
188 }
189
Bjorn Bringert1307f632013-10-03 22:31:03 +0100190 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800191 Context context = app.getContext();
192 ContentResolver contentResolver = context.getContentResolver();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400193
Winson Chungee055712013-07-30 14:46:24 -0700194 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Winson Chungeb23cb22014-03-06 10:39:43 -0800195 ContentProviderClient client = contentResolver.acquireContentProviderClient(
196 LauncherSettings.Favorites.OLD_CONTENT_URI);
197 mOldContentProviderExists = (client != null);
198 if (client != null) {
199 client.release();
200 }
Daniel Sandlere4f98912013-06-25 15:13:26 -0400201 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100202 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 mIconCache = iconCache;
204
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400205 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700206 Configuration config = res.getConfiguration();
207 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 }
209
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700210 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
211 * posted on the main thread handler. */
212 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700213 runOnMainThread(r, 0);
214 }
215 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700216 if (sWorkerThread.getThreadId() == Process.myTid()) {
217 // If we are on the worker thread, post onto the main handler
218 mHandler.post(r);
219 } else {
220 r.run();
221 }
222 }
223
224 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
225 * posted on the worker thread handler. */
226 private static void runOnWorkerThread(Runnable r) {
227 if (sWorkerThread.getThreadId() == Process.myTid()) {
228 r.run();
229 } else {
230 // If we are not on the worker thread, then post to the worker handler
231 sWorker.post(r);
232 }
233 }
234
Winson Chunge43a1e72014-01-15 10:33:02 -0800235 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
236 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800237 }
238
Winson Chungc9168342013-06-26 14:54:55 -0700239 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
240 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700241 LauncherAppState app = LauncherAppState.getInstance();
242 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
243 final int xCount = (int) grid.numColumns;
244 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700245 boolean[][] occupied = new boolean[xCount][yCount];
246
247 int cellX, cellY, spanX, spanY;
248 for (int i = 0; i < items.size(); ++i) {
249 final ItemInfo item = items.get(i);
250 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
251 if (item.screenId == screen) {
252 cellX = item.cellX;
253 cellY = item.cellY;
254 spanX = item.spanX;
255 spanY = item.spanY;
256 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
257 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
258 occupied[x][y] = true;
259 }
260 }
261 }
262 }
263 }
264
265 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
266 }
267 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700269 int firstScreenIndex,
270 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 // Lock on the app so that we don't try and get the items while apps are being added
272 LauncherAppState app = LauncherAppState.getInstance();
273 LauncherModel model = app.getModel();
274 boolean found = false;
275 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700276 if (sWorkerThread.getThreadId() != Process.myTid()) {
277 // Flush the LauncherModel worker thread, so that if we just did another
278 // processInstallShortcut, we give it time for its shortcut to get added to the
279 // database (getItemsInLocalCoordinates reads the database)
280 model.flushWorkerThread();
281 }
Winson Chungc9168342013-06-26 14:54:55 -0700282 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700283
284 // Try adding to the workspace screens incrementally, starting at the default or center
285 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700286 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
287 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700288 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700289 int[] tmpCoordinates = new int[2];
290 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700291 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700292 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700293 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700294 }
295 }
296 }
Winson Chungc9168342013-06-26 14:54:55 -0700297 return null;
298 }
299
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500300 public void setPackageState(final String pkgName, final int state) {
301 // Process the updated package state
302 Runnable r = new Runnable() {
303 public void run() {
304 Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
305 if (callbacks != null) {
306 callbacks.updatePackageState(pkgName, state);
307 }
308 }
309 };
310 mHandler.post(r);
311 }
312
Adam Cohen76a47a12014-02-05 11:47:43 -0800313 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
314 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
315
316 if (allAppsApps == null) {
317 throw new RuntimeException("allAppsApps must not be null");
318 }
319 if (allAppsApps.isEmpty()) {
320 return;
321 }
322
Chris Wrenb6d4c282014-01-27 14:17:08 -0500323 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
324 Iterator<AppInfo> iter = allAppsApps.iterator();
325 while (iter.hasNext()) {
326 ItemInfo a = iter.next();
327 if (LauncherModel.appWasRestored(ctx, a.getIntent())) {
328 restoredAppsFinal.add((AppInfo) a);
329 }
330 }
331
Adam Cohen76a47a12014-02-05 11:47:43 -0800332 // Process the newly added applications and add them to the database first
333 Runnable r = new Runnable() {
334 public void run() {
335 runOnMainThread(new Runnable() {
336 public void run() {
337 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
338 if (callbacks == cb && cb != null) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500339 if (!restoredAppsFinal.isEmpty()) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500340 for (AppInfo info : restoredAppsFinal) {
341 final Intent intent = info.getIntent();
342 if (intent != null) {
343 mIconCache.deletePreloadedIcon(intent.getComponent());
344 }
345 }
Chris Wrenb6d4c282014-01-27 14:17:08 -0500346 callbacks.bindAppsUpdated(restoredAppsFinal);
347 }
Chris Wren6d0dde02014-02-10 12:16:54 -0500348 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800349 }
350 }
351 });
352 }
353 };
354 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700355 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800356
357 public void addAndBindAddedWorkspaceApps(final Context context,
358 final ArrayList<ItemInfo> workspaceApps) {
359 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
360
361 if (workspaceApps == null) {
Winson Chungfe9d96a2013-11-14 11:30:05 -0800362 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
363 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800364 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700365 return;
Winson Chung997a9232013-07-24 15:33:46 -0700366 }
Winson Chung64359a52013-07-08 17:17:08 -0700367 // Process the newly added applications and add them to the database first
368 Runnable r = new Runnable() {
369 public void run() {
370 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
371 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Chris Wrenb6d4c282014-01-27 14:17:08 -0500372 final ArrayList<AppInfo> restoredAppsFinal = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -0700373
Winson Chung76828c82013-08-19 15:43:29 -0700374 // Get the list of workspace screens. We need to append to this list and
375 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
376 // called.
377 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
378 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
379 for (Integer i : orderedScreens.keySet()) {
380 long screenId = orderedScreens.get(i);
381 workspaceScreens.add(screenId);
382 }
383
Winson Chung64359a52013-07-08 17:17:08 -0700384 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700385 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700386 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700387 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700388 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700389 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700390
391 // Short-circuit this logic if the icon exists somewhere on the workspace
392 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
Chris Wrenb6d4c282014-01-27 14:17:08 -0500393 // Only InstallShortcutReceiver sends us shortcutInfos, ignore them
394 if (a instanceof AppInfo &&
395 LauncherModel.appWasRestored(context, launchIntent)) {
396 restoredAppsFinal.add((AppInfo) a);
397 }
Winson Chung64359a52013-07-08 17:17:08 -0700398 continue;
399 }
400
Winson Chung87412982013-10-03 18:34:14 -0700401 // Add this icon to the db, creating a new page if necessary. If there
402 // is only the empty page then we just add items to the first page.
403 // Otherwise, we add them to the next pages.
404 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700405 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700406 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700407 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200408 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700409
410 // If we can't find a valid position, then just add a new screen.
411 // This takes time so we need to re-queue the add until the new
412 // page is added. Create as many screens as necessary to satisfy
413 // the startSearchPageIndex.
414 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700415 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700416 while (numPagesToAdd > 0) {
417 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700418 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700419 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700420 addedWorkspaceScreensFinal.add(screenId);
421 numPagesToAdd--;
422 }
Winson Chung76828c82013-08-19 15:43:29 -0700423
Winson Chung64359a52013-07-08 17:17:08 -0700424 // Find the coordinate again
425 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700426 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700427 }
428 if (coords == null) {
429 throw new RuntimeException("Coordinates should not be null");
430 }
431
Winson Chung997a9232013-07-24 15:33:46 -0700432 ShortcutInfo shortcutInfo;
433 if (a instanceof ShortcutInfo) {
434 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200435 } else if (a instanceof AppInfo) {
436 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700437 } else {
438 throw new RuntimeException("Unexpected info type");
439 }
Winson Chung94d67682013-09-25 16:29:40 -0700440
Winson Chung64359a52013-07-08 17:17:08 -0700441 // Add the shortcut to the db
442 addItemToDatabase(context, shortcutInfo,
443 LauncherSettings.Favorites.CONTAINER_DESKTOP,
444 coords.first, coords.second[0], coords.second[1], false);
445 // Save the ShortcutInfo for binding in the workspace
446 addedShortcutsFinal.add(shortcutInfo);
447 }
448 }
449
Winson Chung76828c82013-08-19 15:43:29 -0700450 // Update the workspace screens
451 updateWorkspaceScreenOrder(context, workspaceScreens);
452
Adam Cohen76a47a12014-02-05 11:47:43 -0800453 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700454 runOnMainThread(new Runnable() {
455 public void run() {
456 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
457 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700458 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
459 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700460 if (!addedShortcutsFinal.isEmpty()) {
461 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
462 long lastScreenId = info.screenId;
463 for (ItemInfo i : addedShortcutsFinal) {
464 if (i.screenId == lastScreenId) {
465 addAnimated.add(i);
466 } else {
467 addNotAnimated.add(i);
468 }
Winson Chung997a9232013-07-24 15:33:46 -0700469 }
470 }
Winson Chungd64d1762013-08-20 14:37:16 -0700471 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800472 addNotAnimated, addAnimated, null);
Chris Wrenb6d4c282014-01-27 14:17:08 -0500473 if (!restoredAppsFinal.isEmpty()) {
474 callbacks.bindAppsUpdated(restoredAppsFinal);
475 }
Winson Chung997a9232013-07-24 15:33:46 -0700476 }
Winson Chung64359a52013-07-08 17:17:08 -0700477 }
Winson Chung997a9232013-07-24 15:33:46 -0700478 });
479 }
Winson Chung64359a52013-07-08 17:17:08 -0700480 }
481 };
482 runOnWorkerThread(r);
483 }
484
Joe Onorato56d82912010-03-07 14:32:10 -0500485 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700486 if (mDefaultIcon == null) {
487 final Context context = LauncherAppState.getInstance().getContext();
488 mDefaultIcon = Utilities.createIconBitmap(
489 mIconCache.getFullResDefaultActivityIcon(), context);
490 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800491 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Winson Chung81b52252012-08-27 15:34:29 -0700494 public void unbindItemInfosAndClearQueuedBindRunnables() {
495 if (sWorkerThread.getThreadId() == Process.myTid()) {
496 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
497 "main thread");
498 }
499
500 // Clear any deferred bind runnables
501 mDeferredBindRunnables.clear();
502 // Remove any queued bind runnables
503 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
504 // Unbind all the workspace items
505 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700506 }
507
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700508 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700509 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700510 // Ensure that we don't use the same workspace items data structure on the main thread
511 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700512 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
513 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700514 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700515 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
516 tmpAppWidgets.addAll(sBgAppWidgets);
517 }
518 Runnable r = new Runnable() {
519 @Override
520 public void run() {
521 for (ItemInfo item : tmpWorkspaceItems) {
522 item.unbind();
523 }
524 for (ItemInfo item : tmpAppWidgets) {
525 item.unbind();
526 }
527 }
528 };
529 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700530 }
531
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 /**
533 * Adds an item to the DB if it was not created previously, or move it to a new
534 * <container, screen, cellX, cellY>
535 */
536 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700537 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 if (item.container == ItemInfo.NO_ID) {
539 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700540 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 } else {
542 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700543 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700547 static void checkItemInfoLocked(
548 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
549 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
550 if (modelItem != null && item != modelItem) {
551 // check all the data is consistent
552 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
553 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
554 ShortcutInfo shortcut = (ShortcutInfo) item;
555 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
556 modelShortcut.intent.filterEquals(shortcut.intent) &&
557 modelShortcut.id == shortcut.id &&
558 modelShortcut.itemType == shortcut.itemType &&
559 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700560 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700561 modelShortcut.cellX == shortcut.cellX &&
562 modelShortcut.cellY == shortcut.cellY &&
563 modelShortcut.spanX == shortcut.spanX &&
564 modelShortcut.spanY == shortcut.spanY &&
565 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
566 (modelShortcut.dropPos != null &&
567 shortcut.dropPos != null &&
568 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
569 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
570 // For all intents and purposes, this is the same object
571 return;
572 }
573 }
574
575 // the modelItem needs to match up perfectly with item if our model is
576 // to be consistent with the database-- for now, just require
577 // modelItem == item or the equality check above
578 String msg = "item: " + ((item != null) ? item.toString() : "null") +
579 "modelItem: " +
580 ((modelItem != null) ? modelItem.toString() : "null") +
581 "Error: ItemInfo passed to checkItemInfo doesn't match original";
582 RuntimeException e = new RuntimeException(msg);
583 if (stackTrace != null) {
584 e.setStackTrace(stackTrace);
585 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800586 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700587 }
588 }
589
Michael Jurka816474f2012-06-25 14:49:02 -0700590 static void checkItemInfo(final ItemInfo item) {
591 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
592 final long itemId = item.id;
593 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700594 public void run() {
595 synchronized (sBgLock) {
596 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700597 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700598 }
599 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700600 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700601 }
602
Michael Jurkac9d95c52011-08-29 14:03:34 -0700603 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
604 final ItemInfo item, final String callingFunction) {
605 final long itemId = item.id;
606 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
607 final ContentResolver cr = context.getContentResolver();
608
Adam Cohen487f7dd2012-06-28 18:12:10 -0700609 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700610 Runnable r = new Runnable() {
611 public void run() {
612 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700613 updateItemArrays(item, itemId, stackTrace);
614 }
615 };
616 runOnWorkerThread(r);
617 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700618
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700619 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
620 final ArrayList<ItemInfo> items, final String callingFunction) {
621 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700622
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700623 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
624 Runnable r = new Runnable() {
625 public void run() {
626 ArrayList<ContentProviderOperation> ops =
627 new ArrayList<ContentProviderOperation>();
628 int count = items.size();
629 for (int i = 0; i < count; i++) {
630 ItemInfo item = items.get(i);
631 final long itemId = item.id;
632 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
633 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700634
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700635 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
636 updateItemArrays(item, itemId, stackTrace);
637
638 }
639 try {
640 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
641 } catch (Exception e) {
642 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643 }
644 }
645 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700646 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700647 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700648
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700649 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
650 // Lock on mBgLock *after* the db operation
651 synchronized (sBgLock) {
652 checkItemInfoLocked(itemId, item, stackTrace);
653
654 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
655 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
656 // Item is in a folder, make sure this folder exists
657 if (!sBgFolders.containsKey(item.container)) {
658 // An items container is being set to a that of an item which is not in
659 // the list of Folders.
660 String msg = "item: " + item + " container being set to: " +
661 item.container + ", not in the list of folders";
662 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 }
664 }
665
666 // Items are added/removed from the corresponding FolderInfo elsewhere, such
667 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
668 // that are on the desktop, as appropriate
669 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800670 if (modelItem != null &&
671 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
672 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 switch (modelItem.itemType) {
674 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
675 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
676 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
677 if (!sBgWorkspaceItems.contains(modelItem)) {
678 sBgWorkspaceItems.add(modelItem);
679 }
680 break;
681 default:
682 break;
683 }
684 } else {
685 sBgWorkspaceItems.remove(modelItem);
686 }
687 }
688 }
689
Michael Jurkac7700af2013-05-14 20:17:58 +0200690 public void flushWorkerThread() {
691 mFlushingWorkerThread = true;
692 Runnable waiter = new Runnable() {
693 public void run() {
694 synchronized (this) {
695 notifyAll();
696 mFlushingWorkerThread = false;
697 }
698 }
699 };
700
701 synchronized(waiter) {
702 runOnWorkerThread(waiter);
703 if (mLoaderTask != null) {
704 synchronized(mLoaderTask) {
705 mLoaderTask.notify();
706 }
707 }
708 boolean success = false;
709 while (!success) {
710 try {
711 waiter.wait();
712 success = true;
713 } catch (InterruptedException e) {
714 }
715 }
716 }
717 }
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700721 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700722 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400724 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 item.cellX = cellX;
726 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 // We store hotseat items in canonical form which is this orientation invariant position
729 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700734 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736
737 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
740 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742
Michael Jurkac9d95c52011-08-29 14:03:34 -0700743 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700744 }
745
746 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700747 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
748 * cellX, cellY have already been updated on the ItemInfos.
749 */
750 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
751 final long container, final int screen) {
752
753 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
754 int count = items.size();
755
756 for (int i = 0; i < count; i++) {
757 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700758 item.container = container;
759
760 // We store hotseat items in canonical form which is this orientation invariant position
761 // in the hotseat
762 if (context instanceof Launcher && screen < 0 &&
763 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700764 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700765 item.cellY);
766 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700767 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700768 }
769
770 final ContentValues values = new ContentValues();
771 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
772 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
773 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700775
776 contentValues.add(values);
777 }
778 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
779 }
780
781 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700782 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800783 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700784 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700786 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800787 item.cellX = cellX;
788 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700789 item.spanX = spanX;
790 item.spanY = spanY;
791
792 // We store hotseat items in canonical form which is this orientation invariant position
793 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700795 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700796 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700797 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700798 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700799 }
Adam Cohend4844c32011-02-18 19:25:06 -0800800
Adam Cohend4844c32011-02-18 19:25:06 -0800801 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800802 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700803 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
804 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
805 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
806 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700807 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800808
Michael Jurka816474f2012-06-25 14:49:02 -0700809 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700810 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700811
812 /**
813 * Update an item to the database in a specified container.
814 */
815 static void updateItemInDatabase(Context context, final ItemInfo item) {
816 final ContentValues values = new ContentValues();
817 item.onAddToDatabase(values);
818 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
819 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800820 }
821
822 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 * Returns true if the shortcuts already exists in the database.
824 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 static boolean shortcutExists(Context context, String title, Intent intent) {
827 final ContentResolver cr = context.getContentResolver();
828 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
829 new String[] { "title", "intent" }, "title=? and intent=?",
830 new String[] { title, intent.toUri(0) }, null);
831 boolean result = false;
832 try {
833 result = c.moveToFirst();
834 } finally {
835 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700838 }
839
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 /**
Chris Wrenb6d4c282014-01-27 14:17:08 -0500841 * Returns true if the shortcuts already exists in the database.
842 * we identify a shortcut by the component name of the intent.
843 */
844 static boolean appWasRestored(Context context, Intent intent) {
845 final ContentResolver cr = context.getContentResolver();
846 final ComponentName component = intent.getComponent();
847 if (component == null) {
848 return false;
849 }
850 String componentName = component.flattenToString();
851 final String where = "intent glob \"*component=" + componentName + "*\" and restored = 1";
852 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
853 new String[]{"intent", "restored"}, where, null, null);
854 boolean result = false;
855 try {
856 result = c.moveToFirst();
857 } finally {
858 c.close();
859 }
860 Log.d(TAG, "shortcutWasRestored is " + result + " for " + componentName);
861 return result;
862 }
863
864 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 * Returns an ItemInfo array containing all the items in the LauncherModel.
866 * The ItemInfo.id is not set through this function.
867 */
868 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
869 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
870 final ContentResolver cr = context.getContentResolver();
871 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
872 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
873 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
874 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
875
876 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
877 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
878 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
879 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
880 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
881 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
882 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
883
884 try {
885 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700886 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700887 item.cellX = c.getInt(cellXIndex);
888 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700889 item.spanX = Math.max(1, c.getInt(spanXIndex));
890 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700891 item.container = c.getInt(containerIndex);
892 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700894
895 items.add(item);
896 }
897 } catch (Exception e) {
898 items.clear();
899 } finally {
900 c.close();
901 }
902
903 return items;
904 }
905
906 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
908 */
909 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
910 final ContentResolver cr = context.getContentResolver();
911 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
912 "_id=? and (itemType=? or itemType=?)",
913 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700914 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700915
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 try {
917 if (c.moveToFirst()) {
918 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
919 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
920 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
921 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
922 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
923 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 FolderInfo folderInfo = null;
926 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700927 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
928 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700930 }
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 folderInfo.title = c.getString(titleIndex);
933 folderInfo.id = id;
934 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700936 folderInfo.cellX = c.getInt(cellXIndex);
937 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938
939 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700940 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 } finally {
942 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700943 }
944
945 return null;
946 }
947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 /**
949 * Add an item to the database in a specified container. Sets the container, screen, cellX and
950 * cellY fields of the item. Also assigns an ID to the item.
951 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700952 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 item.cellX = cellX;
956 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 // We store hotseat items in canonical form which is this orientation invariant position
958 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400965
966 final ContentValues values = new ContentValues();
967 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400968 item.onAddToDatabase(values);
969
Michael Jurka414300a2013-08-27 15:42:35 +0200970 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700971 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700973
Michael Jurkac9d95c52011-08-29 14:03:34 -0700974 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700975 public void run() {
976 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
977 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700979 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700980 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700981 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700982 sBgItemsIdMap.put(item.id, item);
983 switch (item.itemType) {
984 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
985 sBgFolders.put(item.id, (FolderInfo) item);
986 // Fall through
987 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
988 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
989 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
990 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
991 sBgWorkspaceItems.add(item);
992 } else {
993 if (!sBgFolders.containsKey(item.container)) {
994 // Adding an item to a folder that doesn't exist.
995 String msg = "adding item: " + item + " to a folder that " +
996 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700997 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700999 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 break;
1001 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1002 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1003 break;
1004 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001005 }
1006 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001009 }
1010
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001012 * Creates a new unique child id, for a given cell span across all layouts.
1013 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001014 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001015 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001016 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001018 }
1019
Winson Chungaafa03c2010-06-11 17:34:16 -07001020 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001021 * Removes the specified item from the database
1022 * @param context
1023 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001025 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001027 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -07001028
Michael Jurka83df1882011-08-31 20:59:26 -07001029 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001030 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001031 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032
1033 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001034 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035 switch (item.itemType) {
1036 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1037 sBgFolders.remove(item.id);
1038 for (ItemInfo info: sBgItemsIdMap.values()) {
1039 if (info.container == item.id) {
1040 // We are deleting a folder which still contains items that
1041 // think they are contained by that folder.
1042 String msg = "deleting a folder (" + item + ") which still " +
1043 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -07001044 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001046 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 sBgWorkspaceItems.remove(item);
1048 break;
1049 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1050 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1051 sBgWorkspaceItems.remove(item);
1052 break;
1053 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1054 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1055 break;
1056 }
1057 sBgItemsIdMap.remove(item.id);
1058 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001059 }
1060 }
Michael Jurka83df1882011-08-31 20:59:26 -07001061 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001062 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 }
1064
1065 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 * Update the order of the workspace screens in the database. The array list contains
1067 * a list of screen ids in the order that they should appear.
1068 */
Winson Chungc9168342013-06-26 14:54:55 -07001069 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001070 // Log to disk
1071 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1072 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1073
Winson Chung64359a52013-07-08 17:17:08 -07001074 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 final ContentResolver cr = context.getContentResolver();
1076 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1077
1078 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001079 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 while (iter.hasNext()) {
1081 long id = iter.next();
1082 if (id < 0) {
1083 iter.remove();
1084 }
1085 }
1086
1087 Runnable r = new Runnable() {
1088 @Override
1089 public void run() {
Yura085c8532014-02-11 15:15:29 +00001090 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001092 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001093 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001094 for (int i = 0; i < count; i++) {
1095 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001096 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001097 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1098 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001099 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 }
Yura085c8532014-02-11 15:15:29 +00001101
1102 try {
1103 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1104 } catch (Exception ex) {
1105 throw new RuntimeException(ex);
1106 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001107
Winson Chungba9c37f2013-08-30 14:11:37 -07001108 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001109 sBgWorkspaceScreens.clear();
1110 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001111 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 }
1113 };
1114 runOnWorkerThread(r);
1115 }
1116
1117 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 * Remove the contents of the specified folder from the database
1119 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001120 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 final ContentResolver cr = context.getContentResolver();
1122
Michael Jurkac9d95c52011-08-29 14:03:34 -07001123 Runnable r = new Runnable() {
1124 public void run() {
1125 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001126 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001127 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001128 sBgItemsIdMap.remove(info.id);
1129 sBgFolders.remove(info.id);
1130 sBgDbIconCache.remove(info);
1131 sBgWorkspaceItems.remove(info);
1132 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001133
Michael Jurkac9d95c52011-08-29 14:03:34 -07001134 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1135 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001136 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001137 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001138 for (ItemInfo childInfo : info.contents) {
1139 sBgItemsIdMap.remove(childInfo.id);
1140 sBgDbIconCache.remove(childInfo);
1141 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001142 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001143 }
1144 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001145 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 }
1147
1148 /**
1149 * Set this as the current Launcher activity object for the loader.
1150 */
1151 public void initialize(Callbacks callbacks) {
1152 synchronized (mLock) {
1153 mCallbacks = new WeakReference<Callbacks>(callbacks);
1154 }
1155 }
1156
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001158 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1159 * ACTION_PACKAGE_CHANGED.
1160 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001161 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001162 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001163 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001164
Joe Onorato36115782010-06-17 13:28:48 -04001165 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001166
Joe Onorato36115782010-06-17 13:28:48 -04001167 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1168 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1169 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1170 final String packageName = intent.getData().getSchemeSpecificPart();
1171 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172
Joe Onorato36115782010-06-17 13:28:48 -04001173 int op = PackageUpdatedTask.OP_NONE;
1174
1175 if (packageName == null || packageName.length() == 0) {
1176 // they sent us a bad intent
1177 return;
1178 }
1179
1180 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1181 op = PackageUpdatedTask.OP_UPDATE;
1182 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1183 if (!replacing) {
1184 op = PackageUpdatedTask.OP_REMOVE;
1185 }
1186 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1187 // later, we will update the package at this time
1188 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1189 if (!replacing) {
1190 op = PackageUpdatedTask.OP_ADD;
1191 } else {
1192 op = PackageUpdatedTask.OP_UPDATE;
1193 }
1194 }
1195
1196 if (op != PackageUpdatedTask.OP_NONE) {
1197 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1198 }
1199
1200 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001201 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001202 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001203 if (!replacing) {
1204 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1205 if (mAppsCanBeOnRemoveableStorage) {
1206 // Only rebind if we support removable storage. It catches the case where
1207 // apps on the external sd card need to be reloaded
1208 startLoaderFromBackground();
1209 }
1210 } else {
1211 // If we are replacing then just update the packages in the list
1212 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1213 packages));
1214 }
Joe Onorato36115782010-06-17 13:28:48 -04001215 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001216 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1217 if (!replacing) {
1218 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1219 enqueuePackageUpdated(new PackageUpdatedTask(
1220 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1221 }
1222 // else, we are replacing the packages, so ignore this event and wait for
1223 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001224 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001225 // If we have changed locale we need to clear out the labels in all apps/workspace.
1226 forceReload();
1227 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1228 // Check if configuration change was an mcc/mnc change which would affect app resources
1229 // and we would need to clear out the labels in all apps/workspace. Same handling as
1230 // above for ACTION_LOCALE_CHANGED
1231 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001232 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001233 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001234 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001235 forceReload();
1236 }
1237 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001238 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001239 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1240 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001241 if (mCallbacks != null) {
1242 Callbacks callbacks = mCallbacks.get();
1243 if (callbacks != null) {
1244 callbacks.bindSearchablesChanged();
1245 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001246 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001247 }
1248 }
1249
Reena Lee93f824a2011-09-23 17:20:28 -07001250 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001251 resetLoadedState(true, true);
1252
Reena Lee93f824a2011-09-23 17:20:28 -07001253 // Do this here because if the launcher activity is running it will be restarted.
1254 // If it's not running startLoaderFromBackground will merely tell it that it needs
1255 // to reload.
1256 startLoaderFromBackground();
1257 }
1258
Winson Chungf0c6ae02012-03-21 16:10:31 -07001259 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1260 synchronized (mLock) {
1261 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1262 // mWorkspaceLoaded to true later
1263 stopLoaderLocked();
1264 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1265 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1266 }
1267 }
1268
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001269 /**
1270 * When the launcher is in the background, it's possible for it to miss paired
1271 * configuration changes. So whenever we trigger the loader from the background
1272 * tell the launcher that it needs to re-run the loader when it comes back instead
1273 * of doing it now.
1274 */
1275 public void startLoaderFromBackground() {
1276 boolean runLoader = false;
1277 if (mCallbacks != null) {
1278 Callbacks callbacks = mCallbacks.get();
1279 if (callbacks != null) {
1280 // Only actually run the loader if they're not paused.
1281 if (!callbacks.setLoadOnResume()) {
1282 runLoader = true;
1283 }
1284 }
1285 }
1286 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001287 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001288 }
Joe Onorato36115782010-06-17 13:28:48 -04001289 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001290
Reena Lee93f824a2011-09-23 17:20:28 -07001291 // If there is already a loader task running, tell it to stop.
1292 // returns true if isLaunching() was true on the old task
1293 private boolean stopLoaderLocked() {
1294 boolean isLaunching = false;
1295 LoaderTask oldTask = mLoaderTask;
1296 if (oldTask != null) {
1297 if (oldTask.isLaunching()) {
1298 isLaunching = true;
1299 }
1300 oldTask.stopLocked();
1301 }
1302 return isLaunching;
1303 }
1304
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001306 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1307 }
1308
1309 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001310 synchronized (mLock) {
1311 if (DEBUG_LOADERS) {
1312 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001315 // Clear any deferred bind-runnables from the synchronized load process
1316 // We must do this before any loading/binding is scheduled below.
1317 mDeferredBindRunnables.clear();
1318
Joe Onorato36115782010-06-17 13:28:48 -04001319 // Don't bother to start the thread if we know it's not going to do anything
1320 if (mCallbacks != null && mCallbacks.get() != null) {
1321 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001322 // also, don't downgrade isLaunching if we're already running
1323 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001324 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001325 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1326 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001327 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1328 } else {
1329 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1330 sWorker.post(mLoaderTask);
1331 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
1334 }
1335
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001336 void bindRemainingSynchronousPages() {
1337 // Post the remaining side pages to be loaded
1338 if (!mDeferredBindRunnables.isEmpty()) {
1339 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001340 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001341 }
1342 mDeferredBindRunnables.clear();
1343 }
1344 }
1345
Joe Onorato36115782010-06-17 13:28:48 -04001346 public void stopLoader() {
1347 synchronized (mLock) {
1348 if (mLoaderTask != null) {
1349 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350 }
1351 }
Joe Onorato36115782010-06-17 13:28:48 -04001352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353
Winson Chung76828c82013-08-19 15:43:29 -07001354 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1355 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1356 final ContentResolver contentResolver = context.getContentResolver();
1357 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1358 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1359 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1360
1361 try {
1362 final int idIndex = sc.getColumnIndexOrThrow(
1363 LauncherSettings.WorkspaceScreens._ID);
1364 final int rankIndex = sc.getColumnIndexOrThrow(
1365 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1366 while (sc.moveToNext()) {
1367 try {
1368 long screenId = sc.getLong(idIndex);
1369 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001370 orderedScreens.put(rank, screenId);
1371 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001372 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001373 }
1374 }
1375 } finally {
1376 sc.close();
1377 }
Winson Chunga90303b2013-11-15 13:05:06 -08001378
1379 // Log to disk
1380 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1381 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1382 for (Integer i : orderedScreens.keySet()) {
1383 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1384 }
1385 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1386 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001387 return orderedScreens;
1388 }
1389
Michael Jurkac57b7a82011-08-09 22:02:20 -07001390 public boolean isAllAppsLoaded() {
1391 return mAllAppsLoaded;
1392 }
1393
Winson Chung36a62fe2012-05-06 18:04:42 -07001394 boolean isLoadingWorkspace() {
1395 synchronized (mLock) {
1396 if (mLoaderTask != null) {
1397 return mLoaderTask.isLoadingWorkspace();
1398 }
1399 }
1400 return false;
1401 }
1402
Joe Onorato36115782010-06-17 13:28:48 -04001403 /**
1404 * Runnable for the thread that loads the contents of the launcher:
1405 * - workspace icons
1406 * - widgets
1407 * - all apps icons
1408 */
1409 private class LoaderTask implements Runnable {
1410 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001411 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001412 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001413 private boolean mStopped;
1414 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001415 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001416
Winson Chungc3eecff2011-07-11 17:44:15 -07001417 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001418
Dan Sandlerd5024042014-01-09 15:01:33 -05001419 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001420 mContext = context;
1421 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001422 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001423 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424 }
1425
Joe Onorato36115782010-06-17 13:28:48 -04001426 boolean isLaunching() {
1427 return mIsLaunching;
1428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429
Winson Chung36a62fe2012-05-06 18:04:42 -07001430 boolean isLoadingWorkspace() {
1431 return mIsLoadingAndBindingWorkspace;
1432 }
1433
Winson Chungc763c4e2013-07-19 13:49:06 -07001434 /** Returns whether this is an upgrade path */
1435 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001436 mIsLoadingAndBindingWorkspace = true;
1437
Joe Onorato36115782010-06-17 13:28:48 -04001438 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001439 if (DEBUG_LOADERS) {
1440 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001442
Winson Chungc763c4e2013-07-19 13:49:06 -07001443 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001444 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001445 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001446 synchronized (LoaderTask.this) {
1447 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001448 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001449 }
1450 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 }
1452 }
1453
Joe Onorato36115782010-06-17 13:28:48 -04001454 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001455 bindWorkspace(-1, isUpgradePath);
1456 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001457 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001458
Joe Onorato36115782010-06-17 13:28:48 -04001459 private void waitForIdle() {
1460 // Wait until the either we're stopped or the other threads are done.
1461 // This way we don't start loading all apps until the workspace has settled
1462 // down.
1463 synchronized (LoaderTask.this) {
1464 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001465
Joe Onorato36115782010-06-17 13:28:48 -04001466 mHandler.postIdle(new Runnable() {
1467 public void run() {
1468 synchronized (LoaderTask.this) {
1469 mLoadAndBindStepFinished = true;
1470 if (DEBUG_LOADERS) {
1471 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001474 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001475 }
Joe Onorato36115782010-06-17 13:28:48 -04001476 });
1477
Michael Jurkac7700af2013-05-14 20:17:58 +02001478 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001479 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001480 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1481 // wait no longer than 1sec at a time
1482 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001483 } catch (InterruptedException ex) {
1484 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001485 }
1486 }
Joe Onorato36115782010-06-17 13:28:48 -04001487 if (DEBUG_LOADERS) {
1488 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001489 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001490 + "ms for previous step to finish binding");
1491 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001492 }
Joe Onorato36115782010-06-17 13:28:48 -04001493 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001494
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001495 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001496 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001497 // Ensure that we have a valid page index to load synchronously
1498 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1499 "valid page index");
1500 }
1501 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1502 // Ensure that we don't try and bind a specified page when the pages have not been
1503 // loaded already (we should load everything asynchronously in that case)
1504 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1505 }
1506 synchronized (mLock) {
1507 if (mIsLoaderTaskRunning) {
1508 // Ensure that we are never running the background loading at this point since
1509 // we also touch the background collections
1510 throw new RuntimeException("Error! Background loading is already running");
1511 }
1512 }
1513
1514 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1515 // data structures, we can't allow any other thread to touch that data, but because
1516 // this call is synchronous, we can get away with not locking).
1517
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001518 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001519 // operations from the previous activity. We need to ensure that all queued operations
1520 // are executed before any synchronous binding work is done.
1521 mHandler.flush();
1522
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001523 // Divide the set of loaded items into those that we are binding synchronously, and
1524 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001525 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001526 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1527 // arise from that.
1528 onlyBindAllApps();
1529 }
1530
Joe Onorato36115782010-06-17 13:28:48 -04001531 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001532 boolean isUpgrade = false;
1533
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001534 synchronized (mLock) {
1535 mIsLoaderTaskRunning = true;
1536 }
Joe Onorato36115782010-06-17 13:28:48 -04001537 // Optimize for end-user experience: if the Launcher is up and // running with the
1538 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1539 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001540 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001541 // Elevate priority when Home launches for the first time to avoid
1542 // starving at boot time. Staring at a blank home is not cool.
1543 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001544 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1545 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001546 android.os.Process.setThreadPriority(mIsLaunching
1547 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1548 }
Winson Chung64359a52013-07-08 17:17:08 -07001549 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001550 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001551
Joe Onorato36115782010-06-17 13:28:48 -04001552 if (mStopped) {
1553 break keep_running;
1554 }
1555
1556 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1557 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001558 synchronized (mLock) {
1559 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001560 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001561 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1562 }
1563 }
Joe Onorato36115782010-06-17 13:28:48 -04001564 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001565
1566 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001567 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1568 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001569
1570 // Restore the default thread priority after we are done loading items
1571 synchronized (mLock) {
1572 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1573 }
Joe Onorato36115782010-06-17 13:28:48 -04001574 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575
Winson Chungaac01e12011-08-17 10:37:13 -07001576 // Update the saved icons if necessary
1577 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001578 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001579 for (Object key : sBgDbIconCache.keySet()) {
1580 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1581 }
1582 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001583 }
Winson Chungaac01e12011-08-17 10:37:13 -07001584
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001585 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001586 // Ensure that all the applications that are in the system are
1587 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001588 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001589 verifyApplications();
1590 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001591 }
1592
Joe Onorato36115782010-06-17 13:28:48 -04001593 // Clear out this reference, otherwise we end up holding it until all of the
1594 // callback runnables are done.
1595 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596
Joe Onorato36115782010-06-17 13:28:48 -04001597 synchronized (mLock) {
1598 // If we are still the last one to be scheduled, remove ourselves.
1599 if (mLoaderTask == this) {
1600 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001602 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001603 }
Joe Onorato36115782010-06-17 13:28:48 -04001604 }
1605
1606 public void stopLocked() {
1607 synchronized (LoaderTask.this) {
1608 mStopped = true;
1609 this.notify();
1610 }
1611 }
1612
1613 /**
1614 * Gets the callbacks object. If we've been stopped, or if the launcher object
1615 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1616 * object that was around when the deferred message was scheduled, and if there's
1617 * a new Callbacks object around then also return null. This will save us from
1618 * calling onto it with data that will be ignored.
1619 */
1620 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1621 synchronized (mLock) {
1622 if (mStopped) {
1623 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001624 }
Joe Onorato36115782010-06-17 13:28:48 -04001625
1626 if (mCallbacks == null) {
1627 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001628 }
Joe Onorato36115782010-06-17 13:28:48 -04001629
1630 final Callbacks callbacks = mCallbacks.get();
1631 if (callbacks != oldCallbacks) {
1632 return null;
1633 }
1634 if (callbacks == null) {
1635 Log.w(TAG, "no mCallbacks");
1636 return null;
1637 }
1638
1639 return callbacks;
1640 }
1641 }
1642
Winson Chungc763c4e2013-07-19 13:49:06 -07001643 private void verifyApplications() {
1644 final Context context = mApp.getContext();
1645
1646 // Cross reference all the applications in our apps list with items in the workspace
1647 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001648 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001649 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001650 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001651 tmpInfos = getItemInfoForComponentName(app.componentName);
1652 if (tmpInfos.isEmpty()) {
1653 // We are missing an application icon, so add this to the workspace
1654 added.add(app);
1655 // This is a rare event, so lets log it
1656 Log.e(TAG, "Missing Application on load: " + app);
1657 }
1658 }
1659 }
1660 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001661 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001662 }
1663 }
1664
Joe Onorato36115782010-06-17 13:28:48 -04001665 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001666 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001667 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001668 LauncherAppState app = LauncherAppState.getInstance();
1669 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001670 final int countX = (int) grid.numColumns;
1671 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001672
Adam Cohendcd297f2013-06-18 13:13:40 -07001673 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001674 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001675 // Return early if we detect that an item is under the hotseat button
1676 if (mCallbacks == null ||
1677 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001678 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001679 Log.e(TAG, "Error loading shortcut into hotseat " + item
1680 + " into position (" + item.screenId + ":" + item.cellX + ","
1681 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001682 return false;
1683 }
1684
Dan Sandler295ae182013-12-10 16:05:47 -05001685 final ItemInfo[][] hotseatItems =
1686 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1687
Adam Cohenae4409d2013-11-26 10:34:59 -08001688 if (item.screenId >= grid.numHotseatIcons) {
1689 Log.e(TAG, "Error loading shortcut " + item
1690 + " into hotseat position " + item.screenId
1691 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1692 + ")");
1693 return false;
1694 }
1695
Dan Sandler295ae182013-12-10 16:05:47 -05001696 if (hotseatItems != null) {
1697 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001698 Log.e(TAG, "Error loading shortcut into hotseat " + item
1699 + " into position (" + item.screenId + ":" + item.cellX + ","
1700 + item.cellY + ") occupied by "
1701 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1702 [(int) item.screenId][0]);
1703 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001704 } else {
1705 hotseatItems[(int) item.screenId][0] = item;
1706 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001707 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001708 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001709 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001710 items[(int) item.screenId][0] = item;
1711 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001712 return true;
1713 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001714 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1715 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001716 return true;
1717 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001718
Adam Cohendcd297f2013-06-18 13:13:40 -07001719 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001720 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001721 occupied.put(item.screenId, items);
1722 }
1723
Dan Sandler295ae182013-12-10 16:05:47 -05001724 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001725 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1726 item.cellX < 0 || item.cellY < 0 ||
1727 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1728 Log.e(TAG, "Error loading shortcut " + item
1729 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1730 + item.cellX + "," + item.cellY
1731 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1732 return false;
1733 }
1734
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001735 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001736 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1737 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001738 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001739 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001740 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001741 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001742 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001743 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001744 return false;
1745 }
1746 }
1747 }
1748 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1749 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001750 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001751 }
1752 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001753
Joe Onorato36115782010-06-17 13:28:48 -04001754 return true;
1755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756
Winson Chungba9c37f2013-08-30 14:11:37 -07001757 /** Clears all the sBg data structures */
1758 private void clearSBgDataStructures() {
1759 synchronized (sBgLock) {
1760 sBgWorkspaceItems.clear();
1761 sBgAppWidgets.clear();
1762 sBgFolders.clear();
1763 sBgItemsIdMap.clear();
1764 sBgDbIconCache.clear();
1765 sBgWorkspaceScreens.clear();
1766 }
1767 }
1768
Dan Sandlerd5024042014-01-09 15:01:33 -05001769 /** Returns whether this is an upgrade path */
Winson Chungc763c4e2013-07-19 13:49:06 -07001770 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001771 // Log to disk
1772 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1773
Joe Onorato36115782010-06-17 13:28:48 -04001774 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775
Joe Onorato36115782010-06-17 13:28:48 -04001776 final Context context = mContext;
1777 final ContentResolver contentResolver = context.getContentResolver();
1778 final PackageManager manager = context.getPackageManager();
1779 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1780 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001781
Winson Chung892c74d2013-08-22 16:15:50 -07001782 LauncherAppState app = LauncherAppState.getInstance();
1783 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1784 int countX = (int) grid.numColumns;
1785 int countY = (int) grid.numRows;
1786
Dan Sandlerd5024042014-01-09 15:01:33 -05001787 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1788 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1789 LauncherAppState.getLauncherProvider().deleteDatabase();
1790 }
1791
1792 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1793 // append the user's Launcher2 shortcuts
1794 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1795 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1796 } else {
1797 // Make sure the default workspace is loaded
1798 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
1799 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
1800 }
Adam Cohene25af792013-06-06 23:08:25 -07001801
Winson Chungc763c4e2013-07-19 13:49:06 -07001802 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001803 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001804 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001805
Winson Chung9f9f00b2013-11-15 13:27:00 -08001806 // Log to disk
1807 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001808
Winson Chung2abf94d2012-07-18 18:16:38 -07001809 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001810 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001811
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001813 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001814 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001815 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001816 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001817
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 // +1 for the hotseat (it can be larger than the workspace)
1819 // Load workspace in reverse order to ensure that latest items are loaded first (and
1820 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001821 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 try {
1824 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1825 final int intentIndex = c.getColumnIndexOrThrow
1826 (LauncherSettings.Favorites.INTENT);
1827 final int titleIndex = c.getColumnIndexOrThrow
1828 (LauncherSettings.Favorites.TITLE);
1829 final int iconTypeIndex = c.getColumnIndexOrThrow(
1830 LauncherSettings.Favorites.ICON_TYPE);
1831 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1832 final int iconPackageIndex = c.getColumnIndexOrThrow(
1833 LauncherSettings.Favorites.ICON_PACKAGE);
1834 final int iconResourceIndex = c.getColumnIndexOrThrow(
1835 LauncherSettings.Favorites.ICON_RESOURCE);
1836 final int containerIndex = c.getColumnIndexOrThrow(
1837 LauncherSettings.Favorites.CONTAINER);
1838 final int itemTypeIndex = c.getColumnIndexOrThrow(
1839 LauncherSettings.Favorites.ITEM_TYPE);
1840 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1841 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001842 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1843 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 final int screenIndex = c.getColumnIndexOrThrow(
1845 LauncherSettings.Favorites.SCREEN);
1846 final int cellXIndex = c.getColumnIndexOrThrow
1847 (LauncherSettings.Favorites.CELLX);
1848 final int cellYIndex = c.getColumnIndexOrThrow
1849 (LauncherSettings.Favorites.CELLY);
1850 final int spanXIndex = c.getColumnIndexOrThrow
1851 (LauncherSettings.Favorites.SPANX);
1852 final int spanYIndex = c.getColumnIndexOrThrow(
1853 LauncherSettings.Favorites.SPANY);
Chris Wrenf4d08112014-01-16 18:13:56 -05001854 final int restoredIndex = c.getColumnIndexOrThrow(
1855 LauncherSettings.Favorites.RESTORED);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1857 //final int displayModeIndex = c.getColumnIndexOrThrow(
1858 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 ShortcutInfo info;
1861 String intentDescription;
1862 LauncherAppWidgetInfo appWidgetInfo;
1863 int container;
1864 long id;
1865 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001868 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 try {
1870 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001871 boolean restored = 0 != c.getInt(restoredIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 switch (itemType) {
1874 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1875 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001876 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 intentDescription = c.getString(intentIndex);
1878 try {
1879 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001880 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001881 if (cn != null && !isValidPackageComponent(manager, cn)) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001882 if (restored) {
1883 // might be installed later
1884 Launcher.addDumpLog(TAG,
1885 "package not yet restored: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001886 } else {
Chris Wrenf4d08112014-01-16 18:13:56 -05001887 if (!mAppsCanBeOnRemoveableStorage) {
1888 // Log the invalid package, and remove it
1889 Launcher.addDumpLog(TAG,
1890 "Invalid package removed: " + cn, true);
1891 itemsToRemove.add(id);
1892 } else {
1893 // If apps can be on external storage, then we just
1894 // leave them for the user to remove (maybe add
1895 // visual treatment to it)
1896 Launcher.addDumpLog(TAG,
1897 "Invalid package found: " + cn, true);
1898 }
1899 continue;
Winson Chungee055712013-07-30 14:46:24 -07001900 }
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 } else if (restored) {
1902 // no special handling necessary for this restored item
1903 restoredRows.add(id);
1904 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07001905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05001907 Launcher.addDumpLog(TAG,
1908 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 continue;
1910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911
Chris Wrenf4d08112014-01-16 18:13:56 -05001912 if (restored) {
1913 Launcher.addDumpLog(TAG,
1914 "constructing info for partially restored package",
1915 true);
Chris Wrenb6d4c282014-01-27 14:17:08 -05001916 info = getRestoredItemInfo(c, titleIndex, intent);
Chris Wrenf4d08112014-01-16 18:13:56 -05001917 intent = getRestoredItemIntent(c, context, intent);
1918 } else if (itemType ==
1919 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1921 titleIndex, mLabelCache);
1922 } else {
1923 info = getShortcutInfo(c, context, iconTypeIndex,
1924 iconPackageIndex, iconResourceIndex, iconIndex,
1925 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001926
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001927 // App shortcuts that used to be automatically added to Launcher
1928 // didn't always have the correct intent flags set, so do that
1929 // here
1930 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001931 intent.getCategories() != null &&
1932 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001933 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001934 intent.addFlags(
1935 Intent.FLAG_ACTIVITY_NEW_TASK |
1936 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1937 }
Michael Jurka96879562012-03-22 05:54:33 -07001938 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001941 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001942 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 container = c.getInt(containerIndex);
1944 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001946 info.cellX = c.getInt(cellXIndex);
1947 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001948 info.spanX = 1;
1949 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001950
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001951 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001952 deleteOnInvalidPlacement.set(false);
1953 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1954 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001955 itemsToRemove.add(id);
1956 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 break;
1958 }
1959
1960 switch (container) {
1961 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1962 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1963 sBgWorkspaceItems.add(info);
1964 break;
1965 default:
1966 // Item is in a user folder
1967 FolderInfo folderInfo =
1968 findOrMakeFolder(sBgFolders, container);
1969 folderInfo.add(info);
1970 break;
1971 }
1972 sBgItemsIdMap.put(info.id, info);
1973
1974 // now that we've loaded everthing re-save it with the
1975 // icon in case it disappears somehow.
1976 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001977 } else {
1978 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 }
1980 break;
1981
1982 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1983 id = c.getLong(idIndex);
1984 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1985
1986 folderInfo.title = c.getString(titleIndex);
1987 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 folderInfo.cellX = c.getInt(cellXIndex);
1992 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001993 folderInfo.spanX = 1;
1994 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995
Daniel Sandler8802e962010-05-26 16:28:16 -04001996 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001997 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001998 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001999 deleteOnInvalidPlacement)) {
2000 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002001 itemsToRemove.add(id);
2002 }
Daniel Sandler8802e962010-05-26 16:28:16 -04002003 break;
2004 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002005
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2008 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2009 sBgWorkspaceItems.add(folderInfo);
2010 break;
Joe Onorato36115782010-06-17 13:28:48 -04002011 }
Joe Onorato17a89222011-02-08 17:26:11 -08002012
Chris Wrenf4d08112014-01-16 18:13:56 -05002013 if (restored) {
2014 // no special handling required for restored folders
2015 restoredRows.add(id);
2016 }
2017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2019 sBgFolders.put(folderInfo.id, folderInfo);
2020 break;
2021
2022 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2023 // Read all Launcher-specific widget details
2024 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002025 String savedProvider = c.getString(appWidgetProviderIndex);
2026
Joe Onorato36115782010-06-17 13:28:48 -04002027 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002028
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 final AppWidgetProviderInfo provider =
2030 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04002031
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 if (!isSafeMode && (provider == null || provider.provider == null ||
2033 provider.provider.getPackageName() == null)) {
2034 String log = "Deleting widget that isn't installed anymore: id="
2035 + id + " appWidgetId=" + appWidgetId;
2036 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002037 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 itemsToRemove.add(id);
2039 } else {
2040 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2041 provider.provider);
2042 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002043 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 appWidgetInfo.cellX = c.getInt(cellXIndex);
2045 appWidgetInfo.cellY = c.getInt(cellYIndex);
2046 appWidgetInfo.spanX = c.getInt(spanXIndex);
2047 appWidgetInfo.spanY = c.getInt(spanYIndex);
2048 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2049 appWidgetInfo.minSpanX = minSpan[0];
2050 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04002051
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002052 container = c.getInt(containerIndex);
2053 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2054 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2055 Log.e(TAG, "Widget found where container != " +
2056 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2057 continue;
2058 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059
Adam Cohene25af792013-06-06 23:08:25 -07002060 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08002062 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07002063 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08002064 deleteOnInvalidPlacement)) {
2065 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07002066 itemsToRemove.add(id);
2067 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 break;
2069 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002070 String providerName = provider.provider.flattenToString();
2071 if (!providerName.equals(savedProvider)) {
2072 ContentValues values = new ContentValues();
2073 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2074 providerName);
2075 String where = BaseColumns._ID + "= ?";
2076 String[] args = {Integer.toString(c.getInt(idIndex))};
2077 contentResolver.update(contentUri, values, where, args);
2078 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2080 sBgAppWidgets.add(appWidgetInfo);
2081 }
Joe Onorato36115782010-06-17 13:28:48 -04002082 break;
2083 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002085 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002086 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 }
2088 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002089 if (c != null) {
2090 c.close();
2091 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092 }
2093
Winson Chungba9c37f2013-08-30 14:11:37 -07002094 // Break early if we've stopped loading
2095 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002096 clearSBgDataStructures();
2097 return false;
2098 }
2099
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 if (itemsToRemove.size() > 0) {
2101 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07002102 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002103 // Remove dead items
2104 for (long id : itemsToRemove) {
2105 if (DEBUG_LOADERS) {
2106 Log.d(TAG, "Removed id = " + id);
2107 }
2108 // Don't notify content observers
2109 try {
2110 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2111 null, null);
2112 } catch (RemoteException e) {
2113 Log.w(TAG, "Could not remove id = " + id);
2114 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002115 }
2116 }
2117
Chris Wrenf4d08112014-01-16 18:13:56 -05002118 if (restoredRows.size() > 0) {
2119 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
2120 LauncherSettings.Favorites.CONTENT_URI);
2121 // Update restored items that no longer require special handling
2122 try {
2123 StringBuilder selectionBuilder = new StringBuilder();
2124 selectionBuilder.append(LauncherSettings.Favorites._ID);
2125 selectionBuilder.append(" IN (");
2126 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2127 selectionBuilder.append(")");
2128 ContentValues values = new ContentValues();
2129 values.put(LauncherSettings.Favorites.RESTORED, 0);
2130 updater.update(LauncherSettings.Favorites.CONTENT_URI,
2131 values, selectionBuilder.toString(), null);
2132 } catch (RemoteException e) {
2133 Log.w(TAG, "Could not update restored rows");
2134 }
2135 }
2136
Winson Chungc763c4e2013-07-19 13:49:06 -07002137 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002138 long maxScreenId = 0;
2139 // If we're importing we use the old screen order.
2140 for (ItemInfo item: sBgItemsIdMap.values()) {
2141 long screenId = item.screenId;
2142 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2143 !sBgWorkspaceScreens.contains(screenId)) {
2144 sBgWorkspaceScreens.add(screenId);
2145 if (screenId > maxScreenId) {
2146 maxScreenId = screenId;
2147 }
2148 }
2149 }
2150 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08002151 // Log to disk
2152 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
2153 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2154 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002155
Michael Jurka414300a2013-08-27 15:42:35 +02002156 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002157 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07002158
2159 // Update the max item id after we load an old db
2160 long maxItemId = 0;
2161 // If we're importing we use the old screen order.
2162 for (ItemInfo item: sBgItemsIdMap.values()) {
2163 maxItemId = Math.max(maxItemId, item.id);
2164 }
Michael Jurka414300a2013-08-27 15:42:35 +02002165 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07002166 } else {
Winson Chung76828c82013-08-19 15:43:29 -07002167 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
2168 for (Integer i : orderedScreens.keySet()) {
2169 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07002170 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08002171 // Log to disk
2172 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2173 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002174
2175 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002176 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 for (ItemInfo item: sBgItemsIdMap.values()) {
2178 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2180 unusedScreens.contains(screenId)) {
2181 unusedScreens.remove(screenId);
2182 }
2183 }
2184
2185 // If there are any empty screens remove them, and update.
2186 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002187 // Log to disk
2188 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2189 TextUtils.join(", ", unusedScreens), true);
2190
Winson Chung933bae62013-08-29 11:42:30 -07002191 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2193 }
2194 }
2195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 if (DEBUG_LOADERS) {
2197 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2198 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002199 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002200 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002202
Daniel Sandler566da102013-06-25 23:43:45 -04002203 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002204 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002205 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002206 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 line += " | ";
2208 }
Winson Chung892c74d2013-08-22 16:15:50 -07002209 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002210 ItemInfo[][] screen = occupied.get(screenId);
2211 if (x < screen.length && y < screen[x].length) {
2212 line += (screen[x][y] != null) ? "#" : ".";
2213 } else {
2214 line += "!";
2215 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 }
Joe Onorato36115782010-06-17 13:28:48 -04002217 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 }
Joe Onorato36115782010-06-17 13:28:48 -04002221 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002222 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002223 }
2224
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 /** Filters the set of items who are directly or indirectly (via another container) on the
2226 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002227 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 ArrayList<ItemInfo> allWorkspaceItems,
2229 ArrayList<ItemInfo> currentScreenItems,
2230 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002231 // Purge any null ItemInfos
2232 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2233 while (iter.hasNext()) {
2234 ItemInfo i = iter.next();
2235 if (i == null) {
2236 iter.remove();
2237 }
2238 }
2239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 // Order the set of items by their containers first, this allows use to walk through the
2241 // list sequentially, build up a list of containers that are in the specified screen,
2242 // as well as all items in those containers.
2243 Set<Long> itemsOnScreen = new HashSet<Long>();
2244 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2245 @Override
2246 public int compare(ItemInfo lhs, ItemInfo rhs) {
2247 return (int) (lhs.container - rhs.container);
2248 }
2249 });
2250 for (ItemInfo info : allWorkspaceItems) {
2251 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002252 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 currentScreenItems.add(info);
2254 itemsOnScreen.add(info.id);
2255 } else {
2256 otherScreenItems.add(info);
2257 }
2258 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2259 currentScreenItems.add(info);
2260 itemsOnScreen.add(info.id);
2261 } else {
2262 if (itemsOnScreen.contains(info.container)) {
2263 currentScreenItems.add(info);
2264 itemsOnScreen.add(info.id);
2265 } else {
2266 otherScreenItems.add(info);
2267 }
2268 }
2269 }
2270 }
2271
2272 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002273 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 ArrayList<LauncherAppWidgetInfo> appWidgets,
2275 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2276 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277
2278 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002279 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002281 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 currentScreenWidgets.add(widget);
2283 } else {
2284 otherScreenWidgets.add(widget);
2285 }
2286 }
2287 }
2288
2289 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002290 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 HashMap<Long, ItemInfo> itemsIdMap,
2292 HashMap<Long, FolderInfo> folders,
2293 HashMap<Long, FolderInfo> currentScreenFolders,
2294 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295
2296 for (long id : folders.keySet()) {
2297 ItemInfo info = itemsIdMap.get(id);
2298 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002299 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002301 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 currentScreenFolders.put(id, folder);
2303 } else {
2304 otherScreenFolders.put(id, folder);
2305 }
2306 }
2307 }
2308
2309 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2310 * right) */
2311 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002312 final LauncherAppState app = LauncherAppState.getInstance();
2313 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314 // XXX: review this
2315 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002316 @Override
2317 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002318 int cellCountX = (int) grid.numColumns;
2319 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002320 int screenOffset = cellCountX * cellCountY;
2321 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002323 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002325 rhs.cellY * cellCountX + rhs.cellX);
2326 return (int) (lr - rr);
2327 }
2328 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002330
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2332 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 final Runnable r = new Runnable() {
2334 @Override
2335 public void run() {
2336 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2337 if (callbacks != null) {
2338 callbacks.bindScreens(orderedScreens);
2339 }
2340 }
2341 };
2342 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2343 }
2344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002345 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2346 final ArrayList<ItemInfo> workspaceItems,
2347 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2348 final HashMap<Long, FolderInfo> folders,
2349 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002350
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002351 final boolean postOnMainThread = (deferredBindRunnables != null);
2352
2353 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002354 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002355 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002356 final int start = i;
2357 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002358 final Runnable r = new Runnable() {
2359 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002360 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002361 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002362 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002363 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2364 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002365 }
2366 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367 };
2368 if (postOnMainThread) {
2369 deferredBindRunnables.add(r);
2370 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002371 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002372 }
Joe Onorato36115782010-06-17 13:28:48 -04002373 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002374
2375 // Bind the folders
2376 if (!folders.isEmpty()) {
2377 final Runnable r = new Runnable() {
2378 public void run() {
2379 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2380 if (callbacks != null) {
2381 callbacks.bindFolders(folders);
2382 }
2383 }
2384 };
2385 if (postOnMainThread) {
2386 deferredBindRunnables.add(r);
2387 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002388 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002389 }
2390 }
2391
2392 // Bind the widgets, one at a time
2393 N = appWidgets.size();
2394 for (int i = 0; i < N; i++) {
2395 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2396 final Runnable r = new Runnable() {
2397 public void run() {
2398 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2399 if (callbacks != null) {
2400 callbacks.bindAppWidget(widget);
2401 }
2402 }
2403 };
2404 if (postOnMainThread) {
2405 deferredBindRunnables.add(r);
2406 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002407 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408 }
2409 }
2410 }
2411
2412 /**
2413 * Binds all loaded data to actual views on the main thread.
2414 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002415 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002416 final long t = SystemClock.uptimeMillis();
2417 Runnable r;
2418
2419 // Don't use these two variables in any of the callback runnables.
2420 // Otherwise we hold a reference to them.
2421 final Callbacks oldCallbacks = mCallbacks.get();
2422 if (oldCallbacks == null) {
2423 // This launcher has exited and nobody bothered to tell us. Just bail.
2424 Log.w(TAG, "LoaderTask running with no launcher");
2425 return;
2426 }
2427
Winson Chung9b9fb962013-11-15 15:39:34 -08002428 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002429 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2430 ArrayList<LauncherAppWidgetInfo> appWidgets =
2431 new ArrayList<LauncherAppWidgetInfo>();
2432 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2433 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002434 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002435 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 workspaceItems.addAll(sBgWorkspaceItems);
2437 appWidgets.addAll(sBgAppWidgets);
2438 folders.putAll(sBgFolders);
2439 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002440 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002441 }
2442
Derek Prothro7aff3992013-12-10 14:00:37 -05002443 final boolean isLoadingSynchronously =
2444 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002445 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002446 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002447 if (currScreen >= orderedScreenIds.size()) {
2448 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002449 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002450 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002451 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002452 final long currentScreenId = currentScreen < 0
2453 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002454
2455 // Load all the items that are on the current page first (and in the process, unbind
2456 // all the existing workspace items before we call startBinding() below.
2457 unbindWorkspaceItemsOnMainThread();
2458
2459 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002460 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2461 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2462 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2463 new ArrayList<LauncherAppWidgetInfo>();
2464 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2465 new ArrayList<LauncherAppWidgetInfo>();
2466 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2467 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2468
Winson Chung9b9fb962013-11-15 15:39:34 -08002469 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002471 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002473 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002474 otherFolders);
2475 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2476 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2477
2478 // Tell the workspace that we're about to start binding items
2479 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002480 public void run() {
2481 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2482 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002483 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002484 }
2485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002486 };
Winson Chung81b52252012-08-27 15:34:29 -07002487 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002488
Adam Cohendcd297f2013-06-18 13:13:40 -07002489 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2490
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002491 // Load items on the current page
2492 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2493 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002494 if (isLoadingSynchronously) {
2495 r = new Runnable() {
2496 public void run() {
2497 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002498 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002499 callbacks.onPageBoundSynchronously(currentScreen);
2500 }
2501 }
2502 };
Winson Chung81b52252012-08-27 15:34:29 -07002503 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002504 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505
Winson Chung4a2afa32012-07-19 14:53:05 -07002506 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2507 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002508 mDeferredBindRunnables.clear();
2509 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002510 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002511
2512 // Tell the workspace that we're done binding items
2513 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002514 public void run() {
2515 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2516 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002517 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002518 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519
Winson Chung98e030b2012-05-07 16:01:11 -07002520 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002521 if (DEBUG_LOADERS) {
2522 Log.d(TAG, "bound workspace in "
2523 + (SystemClock.uptimeMillis()-t) + "ms");
2524 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002525
2526 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002527 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002528 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002529 if (isLoadingSynchronously) {
2530 mDeferredBindRunnables.add(r);
2531 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002532 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002533 }
Joe Onorato36115782010-06-17 13:28:48 -04002534 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002535
Joe Onorato36115782010-06-17 13:28:48 -04002536 private void loadAndBindAllApps() {
2537 if (DEBUG_LOADERS) {
2538 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2539 }
2540 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002541 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002542 synchronized (LoaderTask.this) {
2543 if (mStopped) {
2544 return;
2545 }
2546 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002547 }
Joe Onorato36115782010-06-17 13:28:48 -04002548 } else {
2549 onlyBindAllApps();
2550 }
2551 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002552
Joe Onorato36115782010-06-17 13:28:48 -04002553 private void onlyBindAllApps() {
2554 final Callbacks oldCallbacks = mCallbacks.get();
2555 if (oldCallbacks == null) {
2556 // This launcher has exited and nobody bothered to tell us. Just bail.
2557 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2558 return;
2559 }
2560
2561 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002562 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002563 final ArrayList<AppInfo> list
2564 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002565 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002566 public void run() {
2567 final long t = SystemClock.uptimeMillis();
2568 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2569 if (callbacks != null) {
2570 callbacks.bindAllApplications(list);
2571 }
2572 if (DEBUG_LOADERS) {
2573 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2574 + (SystemClock.uptimeMillis()-t) + "ms");
2575 }
2576 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002577 };
2578 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002579 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002580 r.run();
2581 } else {
2582 mHandler.post(r);
2583 }
Joe Onorato36115782010-06-17 13:28:48 -04002584 }
2585
Winson Chung64359a52013-07-08 17:17:08 -07002586 private void loadAllApps() {
2587 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002588
Joe Onorato36115782010-06-17 13:28:48 -04002589 final Callbacks oldCallbacks = mCallbacks.get();
2590 if (oldCallbacks == null) {
2591 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002592 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002593 return;
2594 }
2595
Winson Chung64359a52013-07-08 17:17:08 -07002596 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002597 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2598 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2599
Winson Chung64359a52013-07-08 17:17:08 -07002600 // Clear the list of apps
2601 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002602
Winson Chung64359a52013-07-08 17:17:08 -07002603 // Query for the set of apps
2604 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2605 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2606 if (DEBUG_LOADERS) {
2607 Log.d(TAG, "queryIntentActivities took "
2608 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2609 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2610 }
2611 // Fail if we don't have any apps
2612 if (apps == null || apps.isEmpty()) {
2613 return;
2614 }
2615 // Sort the applications by name
2616 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2617 Collections.sort(apps,
2618 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2619 if (DEBUG_LOADERS) {
2620 Log.d(TAG, "sort took "
2621 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002622 }
2623
Winson Chung64359a52013-07-08 17:17:08 -07002624 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002625 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002626 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002627 // This builds the icon bitmaps.
2628 mBgAllAppsList.add(new AppInfo(packageManager, app,
2629 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002630 }
2631
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002632 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002633 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2634 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002635
2636 // Post callback on main thread
2637 mHandler.post(new Runnable() {
2638 public void run() {
2639 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002640 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002641 if (callbacks != null) {
2642 callbacks.bindAllApplications(added);
2643 if (DEBUG_LOADERS) {
2644 Log.d(TAG, "bound " + added.size() + " apps in "
2645 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2646 }
2647 } else {
2648 Log.i(TAG, "not binding apps: no Launcher activity");
2649 }
2650 }
2651 });
2652
Joe Onorato36115782010-06-17 13:28:48 -04002653 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002654 Log.d(TAG, "Icons processed in "
2655 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002656 }
2657 }
2658
2659 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002660 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002661 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2662 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2663 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2664 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2665 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2666 }
Joe Onorato36115782010-06-17 13:28:48 -04002667 }
2668 }
2669
2670 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002671 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002672 }
2673
2674 private class PackageUpdatedTask implements Runnable {
2675 int mOp;
2676 String[] mPackages;
2677
2678 public static final int OP_NONE = 0;
2679 public static final int OP_ADD = 1;
2680 public static final int OP_UPDATE = 2;
2681 public static final int OP_REMOVE = 3; // uninstlled
2682 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2683
2684
2685 public PackageUpdatedTask(int op, String[] packages) {
2686 mOp = op;
2687 mPackages = packages;
2688 }
2689
2690 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002691 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002692
2693 final String[] packages = mPackages;
2694 final int N = packages.length;
2695 switch (mOp) {
2696 case OP_ADD:
2697 for (int i=0; i<N; i++) {
2698 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Chris Wren6d0dde02014-02-10 12:16:54 -05002699 mIconCache.remove(packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002700 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002701 }
2702 break;
2703 case OP_UPDATE:
2704 for (int i=0; i<N; i++) {
2705 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002706 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002707 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002708 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002709 }
2710 break;
2711 case OP_REMOVE:
2712 case OP_UNAVAILABLE:
2713 for (int i=0; i<N; i++) {
2714 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002715 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002716 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002717 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002718 }
2719 break;
2720 }
2721
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002722 ArrayList<AppInfo> added = null;
2723 ArrayList<AppInfo> modified = null;
2724 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002725
Adam Cohen487f7dd2012-06-28 18:12:10 -07002726 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002727 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002728 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002729 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002730 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002731 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002732 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002733 }
Winson Chung5d55f332012-07-16 20:45:03 -07002734 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002735 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002736 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002737 }
2738
Joe Onorato36115782010-06-17 13:28:48 -04002739 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2740 if (callbacks == null) {
2741 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2742 return;
2743 }
2744
2745 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002746 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08002747 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07002748 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08002749 addAndBindAddedWorkspaceApps(context, addedInfos);
2750 } else {
2751 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07002752 }
Joe Onorato36115782010-06-17 13:28:48 -04002753 }
Adam Cohen76a47a12014-02-05 11:47:43 -08002754
Joe Onorato36115782010-06-17 13:28:48 -04002755 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002756 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002757
2758 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002759 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002760 ArrayList<ItemInfo> infos =
2761 getItemInfoForComponentName(a.componentName);
2762 for (ItemInfo i : infos) {
2763 if (isShortcutInfoUpdateable(i)) {
2764 ShortcutInfo info = (ShortcutInfo) i;
2765 info.title = a.title.toString();
2766 updateItemInDatabase(context, info);
2767 }
2768 }
2769 }
2770
Joe Onorato36115782010-06-17 13:28:48 -04002771 mHandler.post(new Runnable() {
2772 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002773 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2774 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002775 callbacks.bindAppsUpdated(modifiedFinal);
2776 }
2777 }
2778 });
2779 }
Winson Chung83892cc2013-05-01 16:53:33 -07002780
Winson Chungdf95eb12013-10-16 14:57:07 -07002781 final ArrayList<String> removedPackageNames =
2782 new ArrayList<String>();
2783 if (mOp == OP_REMOVE) {
2784 // Mark all packages in the broadcast to be removed
2785 removedPackageNames.addAll(Arrays.asList(packages));
2786 } else if (mOp == OP_UPDATE) {
2787 // Mark disabled packages in the broadcast to be removed
2788 final PackageManager pm = context.getPackageManager();
2789 for (int i=0; i<N; i++) {
2790 if (isPackageDisabled(pm, packages[i])) {
2791 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002792 }
2793 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002794 }
2795 // Remove all the components associated with this package
2796 for (String pn : removedPackageNames) {
2797 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2798 for (ItemInfo i : infos) {
2799 deleteItemFromDatabase(context, i);
2800 }
2801 }
2802 // Remove all the specific components
2803 for (AppInfo a : removedApps) {
2804 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2805 for (ItemInfo i : infos) {
2806 deleteItemFromDatabase(context, i);
2807 }
2808 }
2809 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2810 // Remove any queued items from the install queue
2811 String spKey = LauncherAppState.getSharedPreferencesKey();
2812 SharedPreferences sp =
2813 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2814 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2815 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002816 mHandler.post(new Runnable() {
2817 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002818 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2819 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002820 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002821 }
2822 }
2823 });
Joe Onoratobe386092009-11-17 17:32:16 -08002824 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002825
Michael Jurkac402cd92013-05-20 15:49:32 +02002826 final ArrayList<Object> widgetsAndShortcuts =
2827 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002828 mHandler.post(new Runnable() {
2829 @Override
2830 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002831 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2832 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002833 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002834 }
2835 }
2836 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002837
2838 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002839 mHandler.post(new Runnable() {
2840 public void run() {
2841 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2842 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002843 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002844 }
2845 }
2846 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002847 }
2848 }
2849
Michael Jurkac402cd92013-05-20 15:49:32 +02002850 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2851 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2852 PackageManager packageManager = context.getPackageManager();
2853 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2854 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2855 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2856 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2857 Collections.sort(widgetsAndShortcuts,
2858 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2859 return widgetsAndShortcuts;
2860 }
2861
Adam Cohen556f6132014-01-15 15:18:08 -08002862 private static boolean isPackageDisabled(PackageManager pm, String packageName) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002863 try {
2864 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2865 return !pi.applicationInfo.enabled;
2866 } catch (NameNotFoundException e) {
2867 // Fall through
2868 }
2869 return false;
2870 }
Adam Cohen556f6132014-01-15 15:18:08 -08002871
2872 public static boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002873 if (cn == null) {
2874 return false;
2875 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002876 if (isPackageDisabled(pm, cn.getPackageName())) {
2877 return false;
2878 }
Winson Chungee055712013-07-30 14:46:24 -07002879
2880 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002881 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002882 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002883 return (pm.getActivityInfo(cn, 0) != null);
2884 } catch (NameNotFoundException e) {
2885 return false;
2886 }
2887 }
2888
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05002890 * Make an ShortcutInfo object for a restored application or shortcut item that points
2891 * to a package that is not yet installed on the system.
2892 */
Chris Wrenb6d4c282014-01-27 14:17:08 -05002893 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002894 final ShortcutInfo info = new ShortcutInfo();
Chris Wrenf4d08112014-01-16 18:13:56 -05002895 if (cursor != null) {
2896 info.title = cursor.getString(titleIndex);
2897 } else {
2898 info.title = "";
2899 }
Chris Wren6d0dde02014-02-10 12:16:54 -05002900 info.setIcon(mIconCache.getIcon(intent, info.title.toString()));
Chris Wrenf4d08112014-01-16 18:13:56 -05002901 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Chris Wrenb6d4c282014-01-27 14:17:08 -05002902 info.restoredIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05002903 return info;
2904 }
2905
2906 /**
2907 * Make an Intent object for a restored application or shortcut item that points
2908 * to the market page for the item.
2909 */
2910 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
Chris Wrenb6d4c282014-01-27 14:17:08 -05002911 final boolean debug = false;
Chris Wrenf4d08112014-01-16 18:13:56 -05002912 ComponentName componentName = intent.getComponent();
2913 Intent marketIntent = new Intent(Intent.ACTION_VIEW);
2914 Uri marketUri = new Uri.Builder()
2915 .scheme("market")
2916 .authority("details")
2917 .appendQueryParameter("id", componentName.getPackageName())
2918 .build();
Chris Wrenb6d4c282014-01-27 14:17:08 -05002919 if (debug) Log.d(TAG, "manufactured intent uri: " + marketUri.toString());
Chris Wrenf4d08112014-01-16 18:13:56 -05002920 marketIntent.setData(marketUri);
2921 return marketIntent;
2922 }
2923
2924 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002925 * This is called from the code that adds shortcuts from the intent receiver. This
2926 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 */
Joe Onorato56d82912010-03-07 14:32:10 -05002928 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002929 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931
Joe Onorato56d82912010-03-07 14:32:10 -05002932 /**
2933 * Make an ShortcutInfo object for a shortcut that is an application.
2934 *
2935 * If c is not null, then it will be used to fill in missing data like the title and icon.
2936 */
2937 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002938 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002939 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002940 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002941 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002942 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002943 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002944 } else {
2945 try {
2946 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2947 info.initFlagsAndFirstInstallTime(pi);
2948 } catch (NameNotFoundException e) {
2949 Log.d(TAG, "getPackInfo failed for package " +
2950 componentName.getPackageName());
2951 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002952 }
2953
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002954 // TODO: See if the PackageManager knows about this case. If it doesn't
2955 // then return null & delete this.
2956
Joe Onorato56d82912010-03-07 14:32:10 -05002957 // the resource -- This may implicitly give us back the fallback icon,
2958 // but don't worry about that. All we're doing with usingFallbackIcon is
2959 // to avoid saving lots of copies of that in the database, and most apps
2960 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002961
2962 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2963 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2964 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002965 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002966 ResolveInfo resolveInfo = null;
2967 ComponentName oldComponent = intent.getComponent();
2968 Intent newIntent = new Intent(intent.getAction(), null);
2969 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2970 newIntent.setPackage(oldComponent.getPackageName());
2971 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2972 for (ResolveInfo i : infos) {
2973 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2974 i.activityInfo.name);
2975 if (cn.equals(oldComponent)) {
2976 resolveInfo = i;
2977 }
2978 }
2979 if (resolveInfo == null) {
2980 resolveInfo = manager.resolveActivity(intent, 0);
2981 }
Joe Onorato56d82912010-03-07 14:32:10 -05002982 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002983 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002984 }
Joe Onorato56d82912010-03-07 14:32:10 -05002985 // the db
2986 if (icon == null) {
2987 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002988 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002989 }
2990 }
2991 // the fallback icon
2992 if (icon == null) {
2993 icon = getFallbackIcon();
2994 info.usingFallbackIcon = true;
2995 }
2996 info.setIcon(icon);
2997
2998 // from the resource
2999 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003000 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
3001 if (labelCache != null && labelCache.containsKey(key)) {
3002 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07003003 } else {
3004 info.title = resolveInfo.activityInfo.loadLabel(manager);
3005 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07003006 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003007 }
3008 }
Joe Onorato56d82912010-03-07 14:32:10 -05003009 }
3010 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003011 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003012 if (c != null) {
3013 info.title = c.getString(titleIndex);
3014 }
3015 }
3016 // fall back to the class name of the activity
3017 if (info.title == null) {
3018 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003019 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
3021 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003023
Winson Chung64359a52013-07-08 17:17:08 -07003024 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3025 ItemInfoFilter f) {
3026 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3027 for (ItemInfo i : infos) {
3028 if (i instanceof ShortcutInfo) {
3029 ShortcutInfo info = (ShortcutInfo) i;
3030 ComponentName cn = info.intent.getComponent();
3031 if (cn != null && f.filterItem(null, info, cn)) {
3032 filtered.add(info);
3033 }
3034 } else if (i instanceof FolderInfo) {
3035 FolderInfo info = (FolderInfo) i;
3036 for (ShortcutInfo s : info.contents) {
3037 ComponentName cn = s.intent.getComponent();
3038 if (cn != null && f.filterItem(info, s, cn)) {
3039 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003040 }
3041 }
Winson Chung64359a52013-07-08 17:17:08 -07003042 } else if (i instanceof LauncherAppWidgetInfo) {
3043 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3044 ComponentName cn = info.providerName;
3045 if (cn != null && f.filterItem(null, info, cn)) {
3046 filtered.add(info);
3047 }
Winson Chung8a435102012-08-30 17:16:53 -07003048 }
3049 }
Winson Chung64359a52013-07-08 17:17:08 -07003050 return new ArrayList<ItemInfo>(filtered);
3051 }
3052
3053 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07003054 ItemInfoFilter filter = new ItemInfoFilter() {
3055 @Override
3056 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3057 return cn.getPackageName().equals(pn);
3058 }
3059 };
3060 return filterItemInfos(sBgItemsIdMap.values(), filter);
3061 }
3062
3063 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07003064 ItemInfoFilter filter = new ItemInfoFilter() {
3065 @Override
3066 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
3067 return cn.equals(cname);
3068 }
3069 };
3070 return filterItemInfos(sBgItemsIdMap.values(), filter);
3071 }
3072
3073 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
3074 if (i instanceof ShortcutInfo) {
3075 ShortcutInfo info = (ShortcutInfo) i;
3076 // We need to check for ACTION_MAIN otherwise getComponent() might
3077 // return null for some shortcuts (for instance, for shortcuts to
3078 // web pages.)
3079 Intent intent = info.intent;
3080 ComponentName name = intent.getComponent();
3081 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
3082 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3083 return true;
3084 }
Chris Wrenb6d4c282014-01-27 14:17:08 -05003085 // placeholder shortcuts get special treatment, let them through too.
3086 if (info.getRestoredIntent() != null) {
3087 return true;
3088 }
Winson Chung64359a52013-07-08 17:17:08 -07003089 }
3090 return false;
Winson Chung8a435102012-08-30 17:16:53 -07003091 }
3092
3093 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003094 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003096 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003097 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3098 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Joe Onorato56d82912010-03-07 14:32:10 -05003100 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003101 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003104 // TODO: If there's an explicit component and we can't install that, delete it.
3105
Joe Onorato56d82912010-03-07 14:32:10 -05003106 info.title = c.getString(titleIndex);
3107
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 int iconType = c.getInt(iconTypeIndex);
3109 switch (iconType) {
3110 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3111 String packageName = c.getString(iconPackageIndex);
3112 String resourceName = c.getString(iconResourceIndex);
3113 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05003114 info.customIcon = false;
3115 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05003118 if (resources != null) {
3119 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003120 icon = Utilities.createIconBitmap(
3121 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05003124 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 }
Joe Onorato56d82912010-03-07 14:32:10 -05003126 // the db
3127 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003128 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003129 }
3130 // the fallback icon
3131 if (icon == null) {
3132 icon = getFallbackIcon();
3133 info.usingFallbackIcon = true;
3134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 break;
3136 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003137 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003138 if (icon == null) {
3139 icon = getFallbackIcon();
3140 info.customIcon = false;
3141 info.usingFallbackIcon = true;
3142 } else {
3143 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 break;
3146 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08003147 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05003148 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 info.customIcon = false;
3150 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003152 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 return info;
3154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155
Michael Jurka931dc972011-08-05 15:08:15 -07003156 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003157 @SuppressWarnings("all") // suppress dead code warning
3158 final boolean debug = false;
3159 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003160 Log.d(TAG, "getIconFromCursor app="
3161 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3162 }
3163 byte[] data = c.getBlob(iconIndex);
3164 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003165 return Utilities.createIconBitmap(
3166 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003167 } catch (Exception e) {
3168 return null;
3169 }
3170 }
3171
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
3173 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003174 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08003175 if (info == null) {
3176 return null;
3177 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003179
3180 return info;
3181 }
3182
Winson Chunga9abd0e2010-10-27 17:18:37 -07003183 /**
Winson Chung55cef262010-10-28 14:14:18 -07003184 * Attempts to find an AppWidgetProviderInfo that matches the given component.
3185 */
3186 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
3187 ComponentName component) {
3188 List<AppWidgetProviderInfo> widgets =
3189 AppWidgetManager.getInstance(context).getInstalledProviders();
3190 for (AppWidgetProviderInfo info : widgets) {
3191 if (info.provider.equals(component)) {
3192 return info;
3193 }
3194 }
3195 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07003196 }
3197
Winson Chung68846fd2010-10-29 11:00:27 -07003198 /**
3199 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
3200 */
3201 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
3202 final PackageManager packageManager = context.getPackageManager();
3203 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3204 new ArrayList<WidgetMimeTypeHandlerData>();
3205
3206 final Intent supportsIntent =
3207 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3208 supportsIntent.setType(mimeType);
3209
3210 // Create a set of widget configuration components that we can test against
3211 final List<AppWidgetProviderInfo> widgets =
3212 AppWidgetManager.getInstance(context).getInstalledProviders();
3213 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3214 new HashMap<ComponentName, AppWidgetProviderInfo>();
3215 for (AppWidgetProviderInfo info : widgets) {
3216 configurationComponentToWidget.put(info.configure, info);
3217 }
3218
3219 // Run through each of the intents that can handle this type of clip data, and cross
3220 // reference them with the components that are actual configuration components
3221 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3222 PackageManager.MATCH_DEFAULT_ONLY);
3223 for (ResolveInfo info : activities) {
3224 final ActivityInfo activityInfo = info.activityInfo;
3225 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3226 activityInfo.name);
3227 if (configurationComponentToWidget.containsKey(infoComponent)) {
3228 supportedConfigurationActivities.add(
3229 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3230 configurationComponentToWidget.get(infoComponent)));
3231 }
3232 }
3233 return supportedConfigurationActivities;
3234 }
3235
Winson Chunga9abd0e2010-10-27 17:18:37 -07003236 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003237 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3238 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3239 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3240
Adam Cohend9198822011-11-22 16:42:47 -08003241 if (intent == null) {
3242 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3243 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3244 return null;
3245 }
3246
Joe Onorato0589f0f2010-02-08 13:44:00 -08003247 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003248 boolean customIcon = false;
3249 ShortcutIconResource iconResource = null;
3250
3251 if (bitmap != null && bitmap instanceof Bitmap) {
3252 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003253 customIcon = true;
3254 } else {
3255 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3256 if (extra != null && extra instanceof ShortcutIconResource) {
3257 try {
3258 iconResource = (ShortcutIconResource) extra;
3259 final PackageManager packageManager = context.getPackageManager();
3260 Resources resources = packageManager.getResourcesForApplication(
3261 iconResource.packageName);
3262 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003263 icon = Utilities.createIconBitmap(
3264 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003265 } catch (Exception e) {
3266 Log.w(TAG, "Could not load shortcut icon: " + extra);
3267 }
3268 }
3269 }
3270
Michael Jurkac9d95c52011-08-29 14:03:34 -07003271 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003272
3273 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003274 if (fallbackIcon != null) {
3275 icon = fallbackIcon;
3276 } else {
3277 icon = getFallbackIcon();
3278 info.usingFallbackIcon = true;
3279 }
Joe Onorato56d82912010-03-07 14:32:10 -05003280 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003281 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003282
Joe Onorato0589f0f2010-02-08 13:44:00 -08003283 info.title = name;
3284 info.intent = intent;
3285 info.customIcon = customIcon;
3286 info.iconResource = iconResource;
3287
3288 return info;
3289 }
3290
Winson Chungaac01e12011-08-17 10:37:13 -07003291 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3292 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003293 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003294 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003295 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003296 }
Joe Onorato56d82912010-03-07 14:32:10 -05003297 // If this icon doesn't have a custom icon, check to see
3298 // what's stored in the DB, and if it doesn't match what
3299 // we're going to show, store what we are going to show back
3300 // into the DB. We do this so when we're loading, if the
3301 // package manager can't find an icon (for example because
3302 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003303 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003304 cache.put(info, c.getBlob(iconIndex));
3305 return true;
3306 }
3307 return false;
3308 }
3309 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3310 boolean needSave = false;
3311 try {
3312 if (data != null) {
3313 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3314 Bitmap loaded = info.getIcon(mIconCache);
3315 needSave = !saved.sameAs(loaded);
3316 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003317 needSave = true;
3318 }
Winson Chungaac01e12011-08-17 10:37:13 -07003319 } catch (Exception e) {
3320 needSave = true;
3321 }
3322 if (needSave) {
3323 Log.d(TAG, "going to save icon bitmap for info=" + info);
3324 // This is slower than is ideal, but this only happens once
3325 // or when the app is updated with a new icon.
3326 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003327 }
3328 }
3329
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003331 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003332 * or make a new one.
3333 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003334 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 // See if a placeholder was created for us already
3336 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003337 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003339 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 folders.put(id, folderInfo);
3341 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003342 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003343 }
3344
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003345 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003346 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003347 return new Comparator<AppInfo>() {
3348 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003349 int result = collator.compare(a.title.toString().trim(),
3350 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003351 if (result == 0) {
3352 result = a.componentName.compareTo(b.componentName);
3353 }
3354 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003355 }
Winson Chung11904872012-09-17 16:58:46 -07003356 };
3357 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003358 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3359 = new Comparator<AppInfo>() {
3360 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003361 if (a.firstInstallTime < b.firstInstallTime) return 1;
3362 if (a.firstInstallTime > b.firstInstallTime) return -1;
3363 return 0;
3364 }
3365 };
Winson Chung11904872012-09-17 16:58:46 -07003366 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3367 final Collator collator = Collator.getInstance();
3368 return new Comparator<AppWidgetProviderInfo>() {
3369 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003370 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003371 }
3372 };
3373 }
Winson Chung5308f242011-08-18 12:12:41 -07003374 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3375 if (info.activityInfo != null) {
3376 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3377 } else {
3378 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3379 }
3380 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003381 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003382 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003384 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003385 ShortcutNameComparator(PackageManager pm) {
3386 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003387 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003388 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003389 }
3390 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3391 mPackageManager = pm;
3392 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003393 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003394 }
3395 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003396 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003397 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3398 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3399 if (mLabelCache.containsKey(keyA)) {
3400 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003401 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003402 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003403
Winson Chung5308f242011-08-18 12:12:41 -07003404 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003405 }
Winson Chung5308f242011-08-18 12:12:41 -07003406 if (mLabelCache.containsKey(keyB)) {
3407 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003408 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003409 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003410
Winson Chung5308f242011-08-18 12:12:41 -07003411 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003412 }
Winson Chung11904872012-09-17 16:58:46 -07003413 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003414 }
3415 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003416 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003417 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003418 private PackageManager mPackageManager;
3419 private HashMap<Object, String> mLabelCache;
3420 WidgetAndShortcutNameComparator(PackageManager pm) {
3421 mPackageManager = pm;
3422 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003423 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003424 }
3425 public final int compare(Object a, Object b) {
3426 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003427 if (mLabelCache.containsKey(a)) {
3428 labelA = mLabelCache.get(a);
3429 } else {
3430 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003431 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003432 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003433 mLabelCache.put(a, labelA);
3434 }
3435 if (mLabelCache.containsKey(b)) {
3436 labelB = mLabelCache.get(b);
3437 } else {
3438 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003439 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003440 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003441 mLabelCache.put(b, labelB);
3442 }
Winson Chung11904872012-09-17 16:58:46 -07003443 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003444 }
3445 };
Joe Onoratobe386092009-11-17 17:32:16 -08003446
3447 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003448 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003449 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3450 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3451 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3452 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003453 if (mLoaderTask != null) {
3454 mLoaderTask.dumpState();
3455 } else {
3456 Log.d(TAG, "mLoaderTask=null");
3457 }
Joe Onoratobe386092009-11-17 17:32:16 -08003458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003459}