blob: 1d264aa5512a700e049b5799bdf563d218552750 [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 Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070044import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080046
Michael Jurkac2f801e2011-07-12 14:19:46 -070047import java.lang.ref.WeakReference;
48import java.net.URISyntaxException;
49import java.text.Collator;
50import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070051import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070052import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070056import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070057import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070058import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070059import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070060import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070061import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Maintains in-memory state of the Launcher. It is expected that there should be only one
65 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070066 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040068public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080069 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070071
Daniel Sandler8707e0f2013-08-15 15:54:18 -070072 // true = use a "More Apps" folder for non-workspace apps on upgrade
73 // false = strew non-workspace apps across the workspace on upgrade
74 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
75
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070077 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040078
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 private final Object mLock = new Object();
81 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040082 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020084 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Winson Chung81b52252012-08-27 15:34:29 -070086 // Specific runnable types that are run on the main thread deferred handler, this allows us to
87 // clear all queued binding runnables when the Launcher activity is destroyed.
88 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
89 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
90
91
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070092 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
93 static {
94 sWorkerThread.start();
95 }
96 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
97
Joe Onoratocc67f472010-06-08 10:54:30 -070098 // We start off with everything not loaded. After that, we assume that
99 // our monitoring of the package manager provides all updates and we never
100 // need to do a requery. These are only ever touched from the loader thread.
101 private boolean mWorkspaceLoaded;
102 private boolean mAllAppsLoaded;
103
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700104 // When we are loading pages synchronously, we can't just post the binding of items on the side
105 // pages as this delays the rotation process. Instead, we wait for a callback from the first
106 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
107 // a normal load, we also clear this set of Runnables.
108 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700112 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700113 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700115 // The lock that must be acquired before referencing any static bg data structures. Unlike
116 // other locks, this one can generally be held long-term because we never expect any of these
117 // static data structures to be referenced outside of the worker thread except on the first
118 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700119 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700120
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
126 // created by LauncherModel that are directly on the home screen (however, no widgets or
127 // shortcuts within folders).
128 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
131 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 new ArrayList<LauncherAppWidgetInfo>();
133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
135 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
138 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700139
140 // sBgWorkspaceScreens is the ordered set of workspace screens.
141 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
142
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // </ only access in worker thread >
144
145 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Reena Lee99a73f32011-10-24 17:27:37 -0700148 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700151 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 public int getCurrentWorkspaceScreen();
153 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700154 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
155 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700156 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700157 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500158 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700159 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 public void bindAppWidget(LauncherAppWidgetInfo info);
Bjorn Bringert85f418d2013-09-06 12:50:05 +0100161 public boolean shouldShowApp(ResolveInfo app);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200162 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700163 public void bindAppsAdded(ArrayList<Long> newScreens,
164 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700165 ArrayList<ItemInfo> addAnimated,
166 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700168 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200169 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700170 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200171 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100172 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700173 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700174 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700175 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung64359a52013-07-08 17:17:08 -0700178 public interface ItemInfoFilter {
179 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
180 }
181
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 LauncherModel(LauncherAppState app, IconCache iconCache) {
183 final Context context = app.getContext();
184
Winson Chungee055712013-07-30 14:46:24 -0700185 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400186 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700187 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 mIconCache = iconCache;
189
190 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400192
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400193 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700194 Configuration config = res.getConfiguration();
195 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800196 }
197
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700198 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
199 * posted on the main thread handler. */
200 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700201 runOnMainThread(r, 0);
202 }
203 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700204 if (sWorkerThread.getThreadId() == Process.myTid()) {
205 // If we are on the worker thread, post onto the main handler
206 mHandler.post(r);
207 } else {
208 r.run();
209 }
210 }
211
212 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
213 * posted on the worker thread handler. */
214 private static void runOnWorkerThread(Runnable r) {
215 if (sWorkerThread.getThreadId() == Process.myTid()) {
216 r.run();
217 } else {
218 // If we are not on the worker thread, then post to the worker handler
219 sWorker.post(r);
220 }
221 }
222
Winson Chungc9168342013-06-26 14:54:55 -0700223 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
224 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700225 LauncherAppState app = LauncherAppState.getInstance();
226 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
227 final int xCount = (int) grid.numColumns;
228 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700229 boolean[][] occupied = new boolean[xCount][yCount];
230
231 int cellX, cellY, spanX, spanY;
232 for (int i = 0; i < items.size(); ++i) {
233 final ItemInfo item = items.get(i);
234 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
235 if (item.screenId == screen) {
236 cellX = item.cellX;
237 cellY = item.cellY;
238 spanX = item.spanX;
239 spanY = item.spanY;
240 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
241 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
242 occupied[x][y] = true;
243 }
244 }
245 }
246 }
247 }
248
249 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
250 }
251 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700252 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700253 int firstScreenIndex,
254 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700255 // Lock on the app so that we don't try and get the items while apps are being added
256 LauncherAppState app = LauncherAppState.getInstance();
257 LauncherModel model = app.getModel();
258 boolean found = false;
259 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700260 if (sWorkerThread.getThreadId() != Process.myTid()) {
261 // Flush the LauncherModel worker thread, so that if we just did another
262 // processInstallShortcut, we give it time for its shortcut to get added to the
263 // database (getItemsInLocalCoordinates reads the database)
264 model.flushWorkerThread();
265 }
Winson Chungc9168342013-06-26 14:54:55 -0700266 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700267
268 // Try adding to the workspace screens incrementally, starting at the default or center
269 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700270 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
271 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700272 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700273 int[] tmpCoordinates = new int[2];
274 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700275 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700276 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700277 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700278 }
279 }
280 }
Winson Chungc9168342013-06-26 14:54:55 -0700281 return null;
282 }
283
Winson Chungc58497e2013-09-03 17:48:37 -0700284 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
285 final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700286 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -0700287 addAndBindAddedApps(context, added, cb, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700288 }
289 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chungc58497e2013-09-03 17:48:37 -0700290 final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700291 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700292 return;
Winson Chung997a9232013-07-24 15:33:46 -0700293 }
Winson Chung64359a52013-07-08 17:17:08 -0700294 // Process the newly added applications and add them to the database first
295 Runnable r = new Runnable() {
296 public void run() {
297 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
298 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
299
Winson Chung76828c82013-08-19 15:43:29 -0700300 // Get the list of workspace screens. We need to append to this list and
301 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
302 // called.
303 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
304 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
305 for (Integer i : orderedScreens.keySet()) {
306 long screenId = orderedScreens.get(i);
307 workspaceScreens.add(screenId);
308 }
309
Winson Chung64359a52013-07-08 17:17:08 -0700310 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700311 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700312 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700313 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700314 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700315 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700316
317 // Short-circuit this logic if the icon exists somewhere on the workspace
318 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
319 continue;
320 }
321
322 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700323 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700324 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700325 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700326 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200327 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700328
329 // If we can't find a valid position, then just add a new screen.
330 // This takes time so we need to re-queue the add until the new
331 // page is added. Create as many screens as necessary to satisfy
332 // the startSearchPageIndex.
333 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700334 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 while (numPagesToAdd > 0) {
336 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700338 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700339 addedWorkspaceScreensFinal.add(screenId);
340 numPagesToAdd--;
341 }
Winson Chung76828c82013-08-19 15:43:29 -0700342
Winson Chung64359a52013-07-08 17:17:08 -0700343 // Find the coordinate again
344 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700345 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700346 }
347 if (coords == null) {
348 throw new RuntimeException("Coordinates should not be null");
349 }
350
Winson Chung997a9232013-07-24 15:33:46 -0700351 ShortcutInfo shortcutInfo;
352 if (a instanceof ShortcutInfo) {
353 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200354 } else if (a instanceof AppInfo) {
355 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700356 } else {
357 throw new RuntimeException("Unexpected info type");
358 }
Winson Chung64359a52013-07-08 17:17:08 -0700359 // Add the shortcut to the db
360 addItemToDatabase(context, shortcutInfo,
361 LauncherSettings.Favorites.CONTAINER_DESKTOP,
362 coords.first, coords.second[0], coords.second[1], false);
363 // Save the ShortcutInfo for binding in the workspace
364 addedShortcutsFinal.add(shortcutInfo);
365 }
366 }
367
Winson Chung76828c82013-08-19 15:43:29 -0700368 // Update the workspace screens
369 updateWorkspaceScreenOrder(context, workspaceScreens);
370
Winson Chung997a9232013-07-24 15:33:46 -0700371 if (!addedShortcutsFinal.isEmpty()) {
372 runOnMainThread(new Runnable() {
373 public void run() {
374 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
375 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700376 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
377 long lastScreenId = info.screenId;
378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
380 for (ItemInfo i : addedShortcutsFinal) {
381 if (i.screenId == lastScreenId) {
382 addAnimated.add(i);
383 } else {
384 addNotAnimated.add(i);
385 }
386 }
Winson Chungd64d1762013-08-20 14:37:16 -0700387 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chungc58497e2013-09-03 17:48:37 -0700388 addNotAnimated, addAnimated, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
Winson Chung64359a52013-07-08 17:17:08 -0700390 }
Winson Chung997a9232013-07-24 15:33:46 -0700391 });
392 }
Winson Chung64359a52013-07-08 17:17:08 -0700393 }
394 };
395 runOnWorkerThread(r);
396 }
397
Joe Onorato56d82912010-03-07 14:32:10 -0500398 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Winson Chung81b52252012-08-27 15:34:29 -0700402 public void unbindItemInfosAndClearQueuedBindRunnables() {
403 if (sWorkerThread.getThreadId() == Process.myTid()) {
404 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
405 "main thread");
406 }
407
408 // Clear any deferred bind runnables
409 mDeferredBindRunnables.clear();
410 // Remove any queued bind runnables
411 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
412 // Unbind all the workspace items
413 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700414 }
415
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700416 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700417 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700418 // Ensure that we don't use the same workspace items data structure on the main thread
419 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
421 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700422 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700423 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
424 tmpAppWidgets.addAll(sBgAppWidgets);
425 }
426 Runnable r = new Runnable() {
427 @Override
428 public void run() {
429 for (ItemInfo item : tmpWorkspaceItems) {
430 item.unbind();
431 }
432 for (ItemInfo item : tmpAppWidgets) {
433 item.unbind();
434 }
435 }
436 };
437 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700438 }
439
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 /**
441 * Adds an item to the DB if it was not created previously, or move it to a new
442 * <container, screen, cellX, cellY>
443 */
444 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700445 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 if (item.container == ItemInfo.NO_ID) {
447 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700448 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 } else {
450 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700451 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453 }
454
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700455 static void checkItemInfoLocked(
456 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
457 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
458 if (modelItem != null && item != modelItem) {
459 // check all the data is consistent
460 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
461 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
462 ShortcutInfo shortcut = (ShortcutInfo) item;
463 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
464 modelShortcut.intent.filterEquals(shortcut.intent) &&
465 modelShortcut.id == shortcut.id &&
466 modelShortcut.itemType == shortcut.itemType &&
467 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700468 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700469 modelShortcut.cellX == shortcut.cellX &&
470 modelShortcut.cellY == shortcut.cellY &&
471 modelShortcut.spanX == shortcut.spanX &&
472 modelShortcut.spanY == shortcut.spanY &&
473 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
474 (modelShortcut.dropPos != null &&
475 shortcut.dropPos != null &&
476 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
477 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
478 // For all intents and purposes, this is the same object
479 return;
480 }
481 }
482
483 // the modelItem needs to match up perfectly with item if our model is
484 // to be consistent with the database-- for now, just require
485 // modelItem == item or the equality check above
486 String msg = "item: " + ((item != null) ? item.toString() : "null") +
487 "modelItem: " +
488 ((modelItem != null) ? modelItem.toString() : "null") +
489 "Error: ItemInfo passed to checkItemInfo doesn't match original";
490 RuntimeException e = new RuntimeException(msg);
491 if (stackTrace != null) {
492 e.setStackTrace(stackTrace);
493 }
Adam Cohene25af792013-06-06 23:08:25 -0700494 // TODO: something breaks this in the upgrade path
495 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700496 }
497 }
498
Michael Jurka816474f2012-06-25 14:49:02 -0700499 static void checkItemInfo(final ItemInfo item) {
500 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
501 final long itemId = item.id;
502 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700503 public void run() {
504 synchronized (sBgLock) {
505 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700506 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700507 }
508 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700509 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700510 }
511
Michael Jurkac9d95c52011-08-29 14:03:34 -0700512 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
513 final ItemInfo item, final String callingFunction) {
514 final long itemId = item.id;
515 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
516 final ContentResolver cr = context.getContentResolver();
517
Adam Cohen487f7dd2012-06-28 18:12:10 -0700518 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700519 Runnable r = new Runnable() {
520 public void run() {
521 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700522 updateItemArrays(item, itemId, stackTrace);
523 }
524 };
525 runOnWorkerThread(r);
526 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700528 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
529 final ArrayList<ItemInfo> items, final String callingFunction) {
530 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700531
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
533 Runnable r = new Runnable() {
534 public void run() {
535 ArrayList<ContentProviderOperation> ops =
536 new ArrayList<ContentProviderOperation>();
537 int count = items.size();
538 for (int i = 0; i < count; i++) {
539 ItemInfo item = items.get(i);
540 final long itemId = item.id;
541 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
542 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700543
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700544 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
545 updateItemArrays(item, itemId, stackTrace);
546
547 }
548 try {
549 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
550 } catch (Exception e) {
551 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700552 }
553 }
554 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700555 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700556 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700557
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700558 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
559 // Lock on mBgLock *after* the db operation
560 synchronized (sBgLock) {
561 checkItemInfoLocked(itemId, item, stackTrace);
562
563 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
564 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
565 // Item is in a folder, make sure this folder exists
566 if (!sBgFolders.containsKey(item.container)) {
567 // An items container is being set to a that of an item which is not in
568 // the list of Folders.
569 String msg = "item: " + item + " container being set to: " +
570 item.container + ", not in the list of folders";
571 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700572 }
573 }
574
575 // Items are added/removed from the corresponding FolderInfo elsewhere, such
576 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
577 // that are on the desktop, as appropriate
578 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
579 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
580 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
581 switch (modelItem.itemType) {
582 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
583 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
584 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
585 if (!sBgWorkspaceItems.contains(modelItem)) {
586 sBgWorkspaceItems.add(modelItem);
587 }
588 break;
589 default:
590 break;
591 }
592 } else {
593 sBgWorkspaceItems.remove(modelItem);
594 }
595 }
596 }
597
Michael Jurkac7700af2013-05-14 20:17:58 +0200598 public void flushWorkerThread() {
599 mFlushingWorkerThread = true;
600 Runnable waiter = new Runnable() {
601 public void run() {
602 synchronized (this) {
603 notifyAll();
604 mFlushingWorkerThread = false;
605 }
606 }
607 };
608
609 synchronized(waiter) {
610 runOnWorkerThread(waiter);
611 if (mLoaderTask != null) {
612 synchronized(mLoaderTask) {
613 mLoaderTask.notify();
614 }
615 }
616 boolean success = false;
617 while (!success) {
618 try {
619 waiter.wait();
620 success = true;
621 } catch (InterruptedException e) {
622 }
623 }
624 }
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700629 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700630 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 item.cellX = cellX;
634 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700635
Winson Chung3d503fb2011-07-13 17:25:49 -0700636 // We store hotseat items in canonical form which is this orientation invariant position
637 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700639 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644
645 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
648 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
Michael Jurkac9d95c52011-08-29 14:03:34 -0700651 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700652 }
653
654 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700655 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
656 * cellX, cellY have already been updated on the ItemInfos.
657 */
658 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
659 final long container, final int screen) {
660
661 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
662 int count = items.size();
663
664 for (int i = 0; i < count; i++) {
665 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 item.container = container;
667
668 // We store hotseat items in canonical form which is this orientation invariant position
669 // in the hotseat
670 if (context instanceof Launcher && screen < 0 &&
671 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700672 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 item.cellY);
674 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700675 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 }
677
678 final ContentValues values = new ContentValues();
679 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
680 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
681 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683
684 contentValues.add(values);
685 }
686 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
687 }
688
689 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700690 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800691 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700692 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800694 item.cellX = cellX;
695 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700696 item.spanX = spanX;
697 item.spanY = spanY;
698
699 // We store hotseat items in canonical form which is this orientation invariant position
700 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700704 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700706 }
Adam Cohend4844c32011-02-18 19:25:06 -0800707
Adam Cohend4844c32011-02-18 19:25:06 -0800708 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800709 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
711 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
712 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
713 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800715
Michael Jurka816474f2012-06-25 14:49:02 -0700716 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700718
719 /**
720 * Update an item to the database in a specified container.
721 */
722 static void updateItemInDatabase(Context context, final ItemInfo item) {
723 final ContentValues values = new ContentValues();
724 item.onAddToDatabase(values);
725 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
726 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800727 }
728
729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730 * Returns true if the shortcuts already exists in the database.
731 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 static boolean shortcutExists(Context context, String title, Intent intent) {
734 final ContentResolver cr = context.getContentResolver();
735 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
736 new String[] { "title", "intent" }, "title=? and intent=?",
737 new String[] { title, intent.toUri(0) }, null);
738 boolean result = false;
739 try {
740 result = c.moveToFirst();
741 } finally {
742 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700745 }
746
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700748 * Returns an ItemInfo array containing all the items in the LauncherModel.
749 * The ItemInfo.id is not set through this function.
750 */
751 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
752 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
753 final ContentResolver cr = context.getContentResolver();
754 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
755 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
756 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
757 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
758
759 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
760 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
761 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
762 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
763 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
764 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
765 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
766
767 try {
768 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700769 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700770 item.cellX = c.getInt(cellXIndex);
771 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700772 item.spanX = Math.max(1, c.getInt(spanXIndex));
773 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700774 item.container = c.getInt(containerIndex);
775 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700776 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700777
778 items.add(item);
779 }
780 } catch (Exception e) {
781 items.clear();
782 } finally {
783 c.close();
784 }
785
786 return items;
787 }
788
789 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
791 */
792 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
793 final ContentResolver cr = context.getContentResolver();
794 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
795 "_id=? and (itemType=? or itemType=?)",
796 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700797 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700798
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 try {
800 if (c.moveToFirst()) {
801 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
802 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
803 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
804 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
805 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
806 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 FolderInfo folderInfo = null;
809 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700810 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
811 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700813 }
814
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 folderInfo.title = c.getString(titleIndex);
816 folderInfo.id = id;
817 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700818 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700819 folderInfo.cellX = c.getInt(cellXIndex);
820 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821
822 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 } finally {
825 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700826 }
827
828 return null;
829 }
830
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 /**
832 * Add an item to the database in a specified container. Sets the container, screen, cellX and
833 * cellY fields of the item. Also assigns an ID to the item.
834 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700835 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700836 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 item.cellX = cellX;
839 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700840 // We store hotseat items in canonical form which is this orientation invariant position
841 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848
849 final ContentValues values = new ContentValues();
850 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851 item.onAddToDatabase(values);
852
Michael Jurka414300a2013-08-27 15:42:35 +0200853 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700854 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700856
Michael Jurkac9d95c52011-08-29 14:03:34 -0700857 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700858 public void run() {
859 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
860 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700862 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700863 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700864 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700865 sBgItemsIdMap.put(item.id, item);
866 switch (item.itemType) {
867 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
868 sBgFolders.put(item.id, (FolderInfo) item);
869 // Fall through
870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
872 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
873 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
874 sBgWorkspaceItems.add(item);
875 } else {
876 if (!sBgFolders.containsKey(item.container)) {
877 // Adding an item to a folder that doesn't exist.
878 String msg = "adding item: " + item + " to a folder that " +
879 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700880 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700881 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700882 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 break;
884 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
885 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
886 break;
887 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700888 }
889 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700890 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700891 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700892 }
893
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700895 * Creates a new unique child id, for a given cell span across all layouts.
896 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700897 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700898 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700899 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700900 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700901 }
902
Winson Chungaafa03c2010-06-11 17:34:16 -0700903 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700904 * Removes the specified item from the database
905 * @param context
906 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700908 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700910 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700911
Michael Jurka83df1882011-08-31 20:59:26 -0700912 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700913 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915
916 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700917 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700918 switch (item.itemType) {
919 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
920 sBgFolders.remove(item.id);
921 for (ItemInfo info: sBgItemsIdMap.values()) {
922 if (info.container == item.id) {
923 // We are deleting a folder which still contains items that
924 // think they are contained by that folder.
925 String msg = "deleting a folder (" + item + ") which still " +
926 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700927 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700929 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 sBgWorkspaceItems.remove(item);
931 break;
932 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
933 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
934 sBgWorkspaceItems.remove(item);
935 break;
936 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
937 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
938 break;
939 }
940 sBgItemsIdMap.remove(item.id);
941 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700942 }
943 }
Michael Jurka83df1882011-08-31 20:59:26 -0700944 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700945 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 }
947
948 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 * Update the order of the workspace screens in the database. The array list contains
950 * a list of screen ids in the order that they should appear.
951 */
Winson Chungc9168342013-06-26 14:54:55 -0700952 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700953 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 final ContentResolver cr = context.getContentResolver();
955 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
956
957 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700958 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 while (iter.hasNext()) {
960 long id = iter.next();
961 if (id < 0) {
962 iter.remove();
963 }
964 }
965
966 Runnable r = new Runnable() {
967 @Override
968 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700969 // Clear the table
970 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700971 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700972 ContentValues[] values = new ContentValues[count];
973 for (int i = 0; i < count; i++) {
974 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700975 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
977 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 values[i] = v;
979 }
980 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700981
Winson Chungba9c37f2013-08-30 14:11:37 -0700982 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700983 sBgWorkspaceScreens.clear();
984 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700985 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 }
987 };
988 runOnWorkerThread(r);
989 }
990
991 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 * Remove the contents of the specified folder from the database
993 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700994 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 final ContentResolver cr = context.getContentResolver();
996
Michael Jurkac9d95c52011-08-29 14:03:34 -0700997 Runnable r = new Runnable() {
998 public void run() {
999 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001001 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001002 sBgItemsIdMap.remove(info.id);
1003 sBgFolders.remove(info.id);
1004 sBgDbIconCache.remove(info);
1005 sBgWorkspaceItems.remove(info);
1006 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001007
Michael Jurkac9d95c52011-08-29 14:03:34 -07001008 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1009 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001011 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 for (ItemInfo childInfo : info.contents) {
1013 sBgItemsIdMap.remove(childInfo.id);
1014 sBgDbIconCache.remove(childInfo);
1015 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001016 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001017 }
1018 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 }
1021
1022 /**
1023 * Set this as the current Launcher activity object for the loader.
1024 */
1025 public void initialize(Callbacks callbacks) {
1026 synchronized (mLock) {
1027 mCallbacks = new WeakReference<Callbacks>(callbacks);
1028 }
1029 }
1030
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001031 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1033 * ACTION_PACKAGE_CHANGED.
1034 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001035 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001036 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001037 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001038
Joe Onorato36115782010-06-17 13:28:48 -04001039 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001040
Joe Onorato36115782010-06-17 13:28:48 -04001041 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1042 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1043 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1044 final String packageName = intent.getData().getSchemeSpecificPart();
1045 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046
Joe Onorato36115782010-06-17 13:28:48 -04001047 int op = PackageUpdatedTask.OP_NONE;
1048
1049 if (packageName == null || packageName.length() == 0) {
1050 // they sent us a bad intent
1051 return;
1052 }
1053
1054 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1055 op = PackageUpdatedTask.OP_UPDATE;
1056 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1057 if (!replacing) {
1058 op = PackageUpdatedTask.OP_REMOVE;
1059 }
1060 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1061 // later, we will update the package at this time
1062 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1063 if (!replacing) {
1064 op = PackageUpdatedTask.OP_ADD;
1065 } else {
1066 op = PackageUpdatedTask.OP_UPDATE;
1067 }
1068 }
1069
1070 if (op != PackageUpdatedTask.OP_NONE) {
1071 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1072 }
1073
1074 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001075 // First, schedule to add these apps back in.
1076 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1077 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1078 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001079 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001080 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1081 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1082 enqueuePackageUpdated(new PackageUpdatedTask(
1083 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001084 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001085 // If we have changed locale we need to clear out the labels in all apps/workspace.
1086 forceReload();
1087 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1088 // Check if configuration change was an mcc/mnc change which would affect app resources
1089 // and we would need to clear out the labels in all apps/workspace. Same handling as
1090 // above for ACTION_LOCALE_CHANGED
1091 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001092 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001093 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001094 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001095 forceReload();
1096 }
1097 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001098 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001099 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1100 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001101 if (mCallbacks != null) {
1102 Callbacks callbacks = mCallbacks.get();
1103 if (callbacks != null) {
1104 callbacks.bindSearchablesChanged();
1105 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001106 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001107 }
1108 }
1109
Reena Lee93f824a2011-09-23 17:20:28 -07001110 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001111 resetLoadedState(true, true);
1112
Reena Lee93f824a2011-09-23 17:20:28 -07001113 // Do this here because if the launcher activity is running it will be restarted.
1114 // If it's not running startLoaderFromBackground will merely tell it that it needs
1115 // to reload.
1116 startLoaderFromBackground();
1117 }
1118
Winson Chungf0c6ae02012-03-21 16:10:31 -07001119 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1120 synchronized (mLock) {
1121 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1122 // mWorkspaceLoaded to true later
1123 stopLoaderLocked();
1124 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1125 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1126 }
1127 }
1128
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001129 /**
1130 * When the launcher is in the background, it's possible for it to miss paired
1131 * configuration changes. So whenever we trigger the loader from the background
1132 * tell the launcher that it needs to re-run the loader when it comes back instead
1133 * of doing it now.
1134 */
1135 public void startLoaderFromBackground() {
1136 boolean runLoader = false;
1137 if (mCallbacks != null) {
1138 Callbacks callbacks = mCallbacks.get();
1139 if (callbacks != null) {
1140 // Only actually run the loader if they're not paused.
1141 if (!callbacks.setLoadOnResume()) {
1142 runLoader = true;
1143 }
1144 }
1145 }
1146 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001147 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001148 }
Joe Onorato36115782010-06-17 13:28:48 -04001149 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001150
Reena Lee93f824a2011-09-23 17:20:28 -07001151 // If there is already a loader task running, tell it to stop.
1152 // returns true if isLaunching() was true on the old task
1153 private boolean stopLoaderLocked() {
1154 boolean isLaunching = false;
1155 LoaderTask oldTask = mLoaderTask;
1156 if (oldTask != null) {
1157 if (oldTask.isLaunching()) {
1158 isLaunching = true;
1159 }
1160 oldTask.stopLocked();
1161 }
1162 return isLaunching;
1163 }
1164
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001165 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001166 synchronized (mLock) {
1167 if (DEBUG_LOADERS) {
1168 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1169 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001171 // Clear any deferred bind-runnables from the synchronized load process
1172 // We must do this before any loading/binding is scheduled below.
1173 mDeferredBindRunnables.clear();
1174
Joe Onorato36115782010-06-17 13:28:48 -04001175 // Don't bother to start the thread if we know it's not going to do anything
1176 if (mCallbacks != null && mCallbacks.get() != null) {
1177 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001178 // also, don't downgrade isLaunching if we're already running
1179 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001180 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1182 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1183 } else {
1184 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1185 sWorker.post(mLoaderTask);
1186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 }
1189 }
1190
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 void bindRemainingSynchronousPages() {
1192 // Post the remaining side pages to be loaded
1193 if (!mDeferredBindRunnables.isEmpty()) {
1194 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001195 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 }
1197 mDeferredBindRunnables.clear();
1198 }
1199 }
1200
Joe Onorato36115782010-06-17 13:28:48 -04001201 public void stopLoader() {
1202 synchronized (mLock) {
1203 if (mLoaderTask != null) {
1204 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 }
1206 }
Joe Onorato36115782010-06-17 13:28:48 -04001207 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208
Winson Chung76828c82013-08-19 15:43:29 -07001209 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1210 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1211 final ContentResolver contentResolver = context.getContentResolver();
1212 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1213 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1214 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1215
1216 try {
1217 final int idIndex = sc.getColumnIndexOrThrow(
1218 LauncherSettings.WorkspaceScreens._ID);
1219 final int rankIndex = sc.getColumnIndexOrThrow(
1220 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1221 while (sc.moveToNext()) {
1222 try {
1223 long screenId = sc.getLong(idIndex);
1224 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001225 orderedScreens.put(rank, screenId);
1226 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001227 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001228 }
1229 }
1230 } finally {
1231 sc.close();
1232 }
1233 return orderedScreens;
1234 }
1235
Michael Jurkac57b7a82011-08-09 22:02:20 -07001236 public boolean isAllAppsLoaded() {
1237 return mAllAppsLoaded;
1238 }
1239
Winson Chung36a62fe2012-05-06 18:04:42 -07001240 boolean isLoadingWorkspace() {
1241 synchronized (mLock) {
1242 if (mLoaderTask != null) {
1243 return mLoaderTask.isLoadingWorkspace();
1244 }
1245 }
1246 return false;
1247 }
1248
Joe Onorato36115782010-06-17 13:28:48 -04001249 /**
1250 * Runnable for the thread that loads the contents of the launcher:
1251 * - workspace icons
1252 * - widgets
1253 * - all apps icons
1254 */
1255 private class LoaderTask implements Runnable {
1256 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001257 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001258 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001259 private boolean mStopped;
1260 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001261
Winson Chungc3eecff2011-07-11 17:44:15 -07001262 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001263
1264 LoaderTask(Context context, boolean isLaunching) {
1265 mContext = context;
1266 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001267 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 }
1269
Joe Onorato36115782010-06-17 13:28:48 -04001270 boolean isLaunching() {
1271 return mIsLaunching;
1272 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273
Winson Chung36a62fe2012-05-06 18:04:42 -07001274 boolean isLoadingWorkspace() {
1275 return mIsLoadingAndBindingWorkspace;
1276 }
1277
Winson Chungc763c4e2013-07-19 13:49:06 -07001278 /** Returns whether this is an upgrade path */
1279 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001280 mIsLoadingAndBindingWorkspace = true;
1281
Joe Onorato36115782010-06-17 13:28:48 -04001282 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001283 if (DEBUG_LOADERS) {
1284 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001286
Winson Chungc763c4e2013-07-19 13:49:06 -07001287 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001288 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001289 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001290 synchronized (LoaderTask.this) {
1291 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001293 }
1294 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001295 }
1296 }
1297
Joe Onorato36115782010-06-17 13:28:48 -04001298 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001299 bindWorkspace(-1, isUpgradePath);
1300 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001301 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001302
Joe Onorato36115782010-06-17 13:28:48 -04001303 private void waitForIdle() {
1304 // Wait until the either we're stopped or the other threads are done.
1305 // This way we don't start loading all apps until the workspace has settled
1306 // down.
1307 synchronized (LoaderTask.this) {
1308 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001309
Joe Onorato36115782010-06-17 13:28:48 -04001310 mHandler.postIdle(new Runnable() {
1311 public void run() {
1312 synchronized (LoaderTask.this) {
1313 mLoadAndBindStepFinished = true;
1314 if (DEBUG_LOADERS) {
1315 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001316 }
Joe Onorato36115782010-06-17 13:28:48 -04001317 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001318 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001319 }
Joe Onorato36115782010-06-17 13:28:48 -04001320 });
1321
Michael Jurkac7700af2013-05-14 20:17:58 +02001322 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001323 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001324 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1325 // wait no longer than 1sec at a time
1326 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001327 } catch (InterruptedException ex) {
1328 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001329 }
1330 }
Joe Onorato36115782010-06-17 13:28:48 -04001331 if (DEBUG_LOADERS) {
1332 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001333 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001334 + "ms for previous step to finish binding");
1335 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001336 }
Joe Onorato36115782010-06-17 13:28:48 -04001337 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001338
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001339 void runBindSynchronousPage(int synchronousBindPage) {
1340 if (synchronousBindPage < 0) {
1341 // Ensure that we have a valid page index to load synchronously
1342 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1343 "valid page index");
1344 }
1345 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1346 // Ensure that we don't try and bind a specified page when the pages have not been
1347 // loaded already (we should load everything asynchronously in that case)
1348 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1349 }
1350 synchronized (mLock) {
1351 if (mIsLoaderTaskRunning) {
1352 // Ensure that we are never running the background loading at this point since
1353 // we also touch the background collections
1354 throw new RuntimeException("Error! Background loading is already running");
1355 }
1356 }
1357
1358 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1359 // data structures, we can't allow any other thread to touch that data, but because
1360 // this call is synchronous, we can get away with not locking).
1361
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001362 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001363 // operations from the previous activity. We need to ensure that all queued operations
1364 // are executed before any synchronous binding work is done.
1365 mHandler.flush();
1366
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367 // Divide the set of loaded items into those that we are binding synchronously, and
1368 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001369 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1371 // arise from that.
1372 onlyBindAllApps();
1373 }
1374
Joe Onorato36115782010-06-17 13:28:48 -04001375 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001376 boolean isUpgrade = false;
1377
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 synchronized (mLock) {
1379 mIsLoaderTaskRunning = true;
1380 }
Joe Onorato36115782010-06-17 13:28:48 -04001381 // Optimize for end-user experience: if the Launcher is up and // running with the
1382 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1383 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001384 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001385 // Elevate priority when Home launches for the first time to avoid
1386 // starving at boot time. Staring at a blank home is not cool.
1387 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001388 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1389 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 android.os.Process.setThreadPriority(mIsLaunching
1391 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1392 }
Winson Chung64359a52013-07-08 17:17:08 -07001393 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001394 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001395
Joe Onorato36115782010-06-17 13:28:48 -04001396 if (mStopped) {
1397 break keep_running;
1398 }
1399
1400 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1401 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001402 synchronized (mLock) {
1403 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001404 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1406 }
1407 }
Joe Onorato36115782010-06-17 13:28:48 -04001408 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001409
1410 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001411 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1412 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001413
1414 // Restore the default thread priority after we are done loading items
1415 synchronized (mLock) {
1416 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1417 }
Joe Onorato36115782010-06-17 13:28:48 -04001418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419
Winson Chungaac01e12011-08-17 10:37:13 -07001420 // Update the saved icons if necessary
1421 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001422 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001423 for (Object key : sBgDbIconCache.keySet()) {
1424 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1425 }
1426 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001427 }
Winson Chungaac01e12011-08-17 10:37:13 -07001428
Winson Chungc58497e2013-09-03 17:48:37 -07001429 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1430 // Ensure that all the applications that are in the system are
1431 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001432 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001433 verifyApplications();
1434 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001435 }
1436
Joe Onorato36115782010-06-17 13:28:48 -04001437 // Clear out this reference, otherwise we end up holding it until all of the
1438 // callback runnables are done.
1439 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440
Joe Onorato36115782010-06-17 13:28:48 -04001441 synchronized (mLock) {
1442 // If we are still the last one to be scheduled, remove ourselves.
1443 if (mLoaderTask == this) {
1444 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001447 }
Joe Onorato36115782010-06-17 13:28:48 -04001448 }
1449
1450 public void stopLocked() {
1451 synchronized (LoaderTask.this) {
1452 mStopped = true;
1453 this.notify();
1454 }
1455 }
1456
1457 /**
1458 * Gets the callbacks object. If we've been stopped, or if the launcher object
1459 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1460 * object that was around when the deferred message was scheduled, and if there's
1461 * a new Callbacks object around then also return null. This will save us from
1462 * calling onto it with data that will be ignored.
1463 */
1464 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1465 synchronized (mLock) {
1466 if (mStopped) {
1467 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001468 }
Joe Onorato36115782010-06-17 13:28:48 -04001469
1470 if (mCallbacks == null) {
1471 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473
1474 final Callbacks callbacks = mCallbacks.get();
1475 if (callbacks != oldCallbacks) {
1476 return null;
1477 }
1478 if (callbacks == null) {
1479 Log.w(TAG, "no mCallbacks");
1480 return null;
1481 }
1482
1483 return callbacks;
1484 }
1485 }
1486
Winson Chungc763c4e2013-07-19 13:49:06 -07001487 private void verifyApplications() {
1488 final Context context = mApp.getContext();
1489
1490 // Cross reference all the applications in our apps list with items in the workspace
1491 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001492 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001493 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001494 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001495 tmpInfos = getItemInfoForComponentName(app.componentName);
1496 if (tmpInfos.isEmpty()) {
1497 // We are missing an application icon, so add this to the workspace
1498 added.add(app);
1499 // This is a rare event, so lets log it
1500 Log.e(TAG, "Missing Application on load: " + app);
1501 }
1502 }
1503 }
1504 if (!added.isEmpty()) {
1505 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001506 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001507 }
1508 }
1509
Winson Chung5f8afe62013-08-12 16:19:28 -07001510 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001511 LauncherAppState app = LauncherAppState.getInstance();
1512 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1513 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1514 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001515 }
1516
Joe Onorato36115782010-06-17 13:28:48 -04001517 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001518 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1519 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001520 LauncherAppState app = LauncherAppState.getInstance();
1521 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1522 int countX = (int) grid.numColumns;
1523 int countY = (int) grid.numRows;
1524
Adam Cohendcd297f2013-06-18 13:13:40 -07001525 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001526 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001527 // Return early if we detect that an item is under the hotseat button
1528 if (mCallbacks == null ||
1529 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1530 deleteOnItemOverlap.set(true);
1531 return false;
1532 }
1533
Adam Cohendcd297f2013-06-18 13:13:40 -07001534 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1535 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1536 [(int) item.screenId][0] != null) {
1537 Log.e(TAG, "Error loading shortcut into hotseat " + item
1538 + " into position (" + item.screenId + ":" + item.cellX + ","
1539 + item.cellY + ") occupied by "
1540 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1541 [(int) item.screenId][0]);
1542 return false;
1543 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001544 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001545 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 items[(int) item.screenId][0] = item;
1547 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001548 return true;
1549 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001550 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1551 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001552 return true;
1553 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001554
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001556 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 occupied.put(item.screenId, items);
1558 }
1559
1560 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001561 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001562 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1563 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001565 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001566 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001567 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001568 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001570 return false;
1571 }
1572 }
1573 }
1574 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1575 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001577 }
1578 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001579
Joe Onorato36115782010-06-17 13:28:48 -04001580 return true;
1581 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001582
Winson Chungba9c37f2013-08-30 14:11:37 -07001583 /** Clears all the sBg data structures */
1584 private void clearSBgDataStructures() {
1585 synchronized (sBgLock) {
1586 sBgWorkspaceItems.clear();
1587 sBgAppWidgets.clear();
1588 sBgFolders.clear();
1589 sBgItemsIdMap.clear();
1590 sBgDbIconCache.clear();
1591 sBgWorkspaceScreens.clear();
1592 }
1593 }
1594
Winson Chungc763c4e2013-07-19 13:49:06 -07001595 /** Returns whether this is an upgradge path */
1596 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001597 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001598
Joe Onorato36115782010-06-17 13:28:48 -04001599 final Context context = mContext;
1600 final ContentResolver contentResolver = context.getContentResolver();
1601 final PackageManager manager = context.getPackageManager();
1602 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1603 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001604
Winson Chung892c74d2013-08-22 16:15:50 -07001605 LauncherAppState app = LauncherAppState.getInstance();
1606 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1607 int countX = (int) grid.numColumns;
1608 int countY = (int) grid.numRows;
1609
Michael Jurkab85f8a42012-04-25 15:48:32 -07001610 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001611 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001612
Winson Chungc763c4e2013-07-19 13:49:06 -07001613 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001614 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001615
Winson Chung2abf94d2012-07-18 18:16:38 -07001616 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001617 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001618
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001619 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001620 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001621 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001622
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 // +1 for the hotseat (it can be larger than the workspace)
1624 // Load workspace in reverse order to ensure that latest items are loaded first (and
1625 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001626 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001628 try {
1629 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1630 final int intentIndex = c.getColumnIndexOrThrow
1631 (LauncherSettings.Favorites.INTENT);
1632 final int titleIndex = c.getColumnIndexOrThrow
1633 (LauncherSettings.Favorites.TITLE);
1634 final int iconTypeIndex = c.getColumnIndexOrThrow(
1635 LauncherSettings.Favorites.ICON_TYPE);
1636 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1637 final int iconPackageIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.ICON_PACKAGE);
1639 final int iconResourceIndex = c.getColumnIndexOrThrow(
1640 LauncherSettings.Favorites.ICON_RESOURCE);
1641 final int containerIndex = c.getColumnIndexOrThrow(
1642 LauncherSettings.Favorites.CONTAINER);
1643 final int itemTypeIndex = c.getColumnIndexOrThrow(
1644 LauncherSettings.Favorites.ITEM_TYPE);
1645 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1646 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001647 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1648 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001649 final int screenIndex = c.getColumnIndexOrThrow(
1650 LauncherSettings.Favorites.SCREEN);
1651 final int cellXIndex = c.getColumnIndexOrThrow
1652 (LauncherSettings.Favorites.CELLX);
1653 final int cellYIndex = c.getColumnIndexOrThrow
1654 (LauncherSettings.Favorites.CELLY);
1655 final int spanXIndex = c.getColumnIndexOrThrow
1656 (LauncherSettings.Favorites.SPANX);
1657 final int spanYIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.SPANY);
1659 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1660 //final int displayModeIndex = c.getColumnIndexOrThrow(
1661 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001663 ShortcutInfo info;
1664 String intentDescription;
1665 LauncherAppWidgetInfo appWidgetInfo;
1666 int container;
1667 long id;
1668 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001669
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001670 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001671 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 try {
1673 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 switch (itemType) {
1676 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1677 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001678 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 intentDescription = c.getString(intentIndex);
1680 try {
1681 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001682 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001683 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001684 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001685 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001686 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1687 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001688 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001689 // If apps can be on external storage, then we just
1690 // leave them for the user to remove (maybe add
1691 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001692 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001693 }
1694 continue;
1695 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001696 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001697 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001698 continue;
1699 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1702 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1703 titleIndex, mLabelCache);
1704 } else {
1705 info = getShortcutInfo(c, context, iconTypeIndex,
1706 iconPackageIndex, iconResourceIndex, iconIndex,
1707 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001708
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 // App shortcuts that used to be automatically added to Launcher
1710 // didn't always have the correct intent flags set, so do that
1711 // here
1712 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001713 intent.getCategories() != null &&
1714 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001715 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 intent.addFlags(
1717 Intent.FLAG_ACTIVITY_NEW_TASK |
1718 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1719 }
Michael Jurka96879562012-03-22 05:54:33 -07001720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001721
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001723 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 container = c.getInt(containerIndex);
1726 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001727 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 info.cellX = c.getInt(cellXIndex);
1729 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001730 info.spanX = 1;
1731 info.spanY = 1;
1732 // Skip loading items that are out of bounds
1733 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1734 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001735 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1736 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001737 continue;
1738 }
1739 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001741 deleteOnItemOverlap.set(false);
1742 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1743 if (deleteOnItemOverlap.get()) {
1744 itemsToRemove.add(id);
1745 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 break;
1747 }
1748
1749 switch (container) {
1750 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1751 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1752 sBgWorkspaceItems.add(info);
1753 break;
1754 default:
1755 // Item is in a user folder
1756 FolderInfo folderInfo =
1757 findOrMakeFolder(sBgFolders, container);
1758 folderInfo.add(info);
1759 break;
1760 }
1761 sBgItemsIdMap.put(info.id, info);
1762
1763 // now that we've loaded everthing re-save it with the
1764 // icon in case it disappears somehow.
1765 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001766 } else {
1767 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 }
1769 break;
1770
1771 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1772 id = c.getLong(idIndex);
1773 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1774
1775 folderInfo.title = c.getString(titleIndex);
1776 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001778 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 folderInfo.cellX = c.getInt(cellXIndex);
1781 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001782 folderInfo.spanX = 1;
1783 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784
Winson Chung5f8afe62013-08-12 16:19:28 -07001785 // Skip loading items that are out of bounds
1786 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001787 if (checkItemDimensions(folderInfo)) {
1788 Log.d(TAG, "Skipped loading out of bounds folder");
1789 continue;
1790 }
1791 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001792 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001793 deleteOnItemOverlap.set(false);
1794 if (!checkItemPlacement(occupied, folderInfo,
1795 deleteOnItemOverlap)) {
1796 if (deleteOnItemOverlap.get()) {
1797 itemsToRemove.add(id);
1798 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001799 break;
1800 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001801
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001803 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1804 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1805 sBgWorkspaceItems.add(folderInfo);
1806 break;
Joe Onorato36115782010-06-17 13:28:48 -04001807 }
Joe Onorato17a89222011-02-08 17:26:11 -08001808
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1810 sBgFolders.put(folderInfo.id, folderInfo);
1811 break;
1812
1813 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1814 // Read all Launcher-specific widget details
1815 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001816 String savedProvider = c.getString(appWidgetProviderIndex);
1817
Joe Onorato36115782010-06-17 13:28:48 -04001818 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001819
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001820 final AppWidgetProviderInfo provider =
1821 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 if (!isSafeMode && (provider == null || provider.provider == null ||
1824 provider.provider.getPackageName() == null)) {
1825 String log = "Deleting widget that isn't installed anymore: id="
1826 + id + " appWidgetId=" + appWidgetId;
1827 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001828 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 itemsToRemove.add(id);
1830 } else {
1831 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1832 provider.provider);
1833 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 appWidgetInfo.cellX = c.getInt(cellXIndex);
1836 appWidgetInfo.cellY = c.getInt(cellYIndex);
1837 appWidgetInfo.spanX = c.getInt(spanXIndex);
1838 appWidgetInfo.spanY = c.getInt(spanYIndex);
1839 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1840 appWidgetInfo.minSpanX = minSpan[0];
1841 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 container = c.getInt(containerIndex);
1844 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1845 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1846 Log.e(TAG, "Widget found where container != " +
1847 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1848 continue;
1849 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850
Adam Cohene25af792013-06-06 23:08:25 -07001851 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001852 // Skip loading items that are out of bounds
1853 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1854 if (checkItemDimensions(appWidgetInfo)) {
1855 Log.d(TAG, "Skipped loading out of bounds app widget");
1856 continue;
1857 }
1858 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001860 deleteOnItemOverlap.set(false);
1861 if (!checkItemPlacement(occupied, appWidgetInfo,
1862 deleteOnItemOverlap)) {
1863 if (deleteOnItemOverlap.get()) {
1864 itemsToRemove.add(id);
1865 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 break;
1867 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001868 String providerName = provider.provider.flattenToString();
1869 if (!providerName.equals(savedProvider)) {
1870 ContentValues values = new ContentValues();
1871 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1872 providerName);
1873 String where = BaseColumns._ID + "= ?";
1874 String[] args = {Integer.toString(c.getInt(idIndex))};
1875 contentResolver.update(contentUri, values, where, args);
1876 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1878 sBgAppWidgets.add(appWidgetInfo);
1879 }
Joe Onorato36115782010-06-17 13:28:48 -04001880 break;
1881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001883 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001884 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 }
1886 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001887 if (c != null) {
1888 c.close();
1889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 }
1891
Winson Chungba9c37f2013-08-30 14:11:37 -07001892 // Break early if we've stopped loading
1893 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001894 clearSBgDataStructures();
1895 return false;
1896 }
1897
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 if (itemsToRemove.size() > 0) {
1899 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001900 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 // Remove dead items
1902 for (long id : itemsToRemove) {
1903 if (DEBUG_LOADERS) {
1904 Log.d(TAG, "Removed id = " + id);
1905 }
1906 // Don't notify content observers
1907 try {
1908 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1909 null, null);
1910 } catch (RemoteException e) {
1911 Log.w(TAG, "Could not remove id = " + id);
1912 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001913 }
1914 }
1915
Winson Chungc763c4e2013-07-19 13:49:06 -07001916 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 long maxScreenId = 0;
1918 // If we're importing we use the old screen order.
1919 for (ItemInfo item: sBgItemsIdMap.values()) {
1920 long screenId = item.screenId;
1921 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1922 !sBgWorkspaceScreens.contains(screenId)) {
1923 sBgWorkspaceScreens.add(screenId);
1924 if (screenId > maxScreenId) {
1925 maxScreenId = screenId;
1926 }
1927 }
1928 }
1929 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001930
Michael Jurka414300a2013-08-27 15:42:35 +02001931 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001932 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001933
1934 // Update the max item id after we load an old db
1935 long maxItemId = 0;
1936 // If we're importing we use the old screen order.
1937 for (ItemInfo item: sBgItemsIdMap.values()) {
1938 maxItemId = Math.max(maxItemId, item.id);
1939 }
Michael Jurka414300a2013-08-27 15:42:35 +02001940 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001942 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1943 for (Integer i : orderedScreens.keySet()) {
1944 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 }
1946
1947 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001948 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 for (ItemInfo item: sBgItemsIdMap.values()) {
1950 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001951 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1952 unusedScreens.contains(screenId)) {
1953 unusedScreens.remove(screenId);
1954 }
1955 }
1956
1957 // If there are any empty screens remove them, and update.
1958 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001959 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1961 }
1962 }
1963
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001964 if (DEBUG_LOADERS) {
1965 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1966 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001968 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001969 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001970
Daniel Sandler566da102013-06-25 23:43:45 -04001971 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001972 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001974 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 line += " | ";
1976 }
Winson Chung892c74d2013-08-22 16:15:50 -07001977 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 }
Joe Onorato36115782010-06-17 13:28:48 -04001980 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 }
Joe Onorato36115782010-06-17 13:28:48 -04001984 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001985 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001986 }
1987
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 /** Filters the set of items who are directly or indirectly (via another container) on the
1989 * specified screen. */
1990 private void filterCurrentWorkspaceItems(int currentScreen,
1991 ArrayList<ItemInfo> allWorkspaceItems,
1992 ArrayList<ItemInfo> currentScreenItems,
1993 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001994 // Purge any null ItemInfos
1995 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1996 while (iter.hasNext()) {
1997 ItemInfo i = iter.next();
1998 if (i == null) {
1999 iter.remove();
2000 }
2001 }
2002
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002003 // If we aren't filtering on a screen, then the set of items to load is the full set of
2004 // items given.
2005 if (currentScreen < 0) {
2006 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002007 }
2008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 // Order the set of items by their containers first, this allows use to walk through the
2010 // list sequentially, build up a list of containers that are in the specified screen,
2011 // as well as all items in those containers.
2012 Set<Long> itemsOnScreen = new HashSet<Long>();
2013 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2014 @Override
2015 public int compare(ItemInfo lhs, ItemInfo rhs) {
2016 return (int) (lhs.container - rhs.container);
2017 }
2018 });
2019 for (ItemInfo info : allWorkspaceItems) {
2020 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 currentScreenItems.add(info);
2023 itemsOnScreen.add(info.id);
2024 } else {
2025 otherScreenItems.add(info);
2026 }
2027 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2028 currentScreenItems.add(info);
2029 itemsOnScreen.add(info.id);
2030 } else {
2031 if (itemsOnScreen.contains(info.container)) {
2032 currentScreenItems.add(info);
2033 itemsOnScreen.add(info.id);
2034 } else {
2035 otherScreenItems.add(info);
2036 }
2037 }
2038 }
2039 }
2040
2041 /** Filters the set of widgets which are on the specified screen. */
2042 private void filterCurrentAppWidgets(int currentScreen,
2043 ArrayList<LauncherAppWidgetInfo> appWidgets,
2044 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2045 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2046 // If we aren't filtering on a screen, then the set of items to load is the full set of
2047 // widgets given.
2048 if (currentScreen < 0) {
2049 currentScreenWidgets.addAll(appWidgets);
2050 }
2051
2052 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002053 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002054 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002055 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002056 currentScreenWidgets.add(widget);
2057 } else {
2058 otherScreenWidgets.add(widget);
2059 }
2060 }
2061 }
2062
2063 /** Filters the set of folders which are on the specified screen. */
2064 private void filterCurrentFolders(int currentScreen,
2065 HashMap<Long, ItemInfo> itemsIdMap,
2066 HashMap<Long, FolderInfo> folders,
2067 HashMap<Long, FolderInfo> currentScreenFolders,
2068 HashMap<Long, FolderInfo> otherScreenFolders) {
2069 // If we aren't filtering on a screen, then the set of items to load is the full set of
2070 // widgets given.
2071 if (currentScreen < 0) {
2072 currentScreenFolders.putAll(folders);
2073 }
2074
2075 for (long id : folders.keySet()) {
2076 ItemInfo info = itemsIdMap.get(id);
2077 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002078 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002080 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081 currentScreenFolders.put(id, folder);
2082 } else {
2083 otherScreenFolders.put(id, folder);
2084 }
2085 }
2086 }
2087
2088 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2089 * right) */
2090 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002091 final LauncherAppState app = LauncherAppState.getInstance();
2092 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 // XXX: review this
2094 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002095 @Override
2096 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002097 int cellCountX = (int) grid.numColumns;
2098 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002099 int screenOffset = cellCountX * cellCountY;
2100 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002102 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002103 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002104 rhs.cellY * cellCountX + rhs.cellX);
2105 return (int) (lr - rr);
2106 }
2107 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002109
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2111 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002112 final Runnable r = new Runnable() {
2113 @Override
2114 public void run() {
2115 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2116 if (callbacks != null) {
2117 callbacks.bindScreens(orderedScreens);
2118 }
2119 }
2120 };
2121 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2122 }
2123
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2125 final ArrayList<ItemInfo> workspaceItems,
2126 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2127 final HashMap<Long, FolderInfo> folders,
2128 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002129
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 final boolean postOnMainThread = (deferredBindRunnables != null);
2131
2132 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002133 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002135 final int start = i;
2136 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 final Runnable r = new Runnable() {
2138 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002140 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002142 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2143 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 }
2145 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002146 };
2147 if (postOnMainThread) {
2148 deferredBindRunnables.add(r);
2149 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002150 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 }
Joe Onorato36115782010-06-17 13:28:48 -04002152 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153
2154 // Bind the folders
2155 if (!folders.isEmpty()) {
2156 final Runnable r = new Runnable() {
2157 public void run() {
2158 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2159 if (callbacks != null) {
2160 callbacks.bindFolders(folders);
2161 }
2162 }
2163 };
2164 if (postOnMainThread) {
2165 deferredBindRunnables.add(r);
2166 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002167 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002168 }
2169 }
2170
2171 // Bind the widgets, one at a time
2172 N = appWidgets.size();
2173 for (int i = 0; i < N; i++) {
2174 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2175 final Runnable r = new Runnable() {
2176 public void run() {
2177 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2178 if (callbacks != null) {
2179 callbacks.bindAppWidget(widget);
2180 }
2181 }
2182 };
2183 if (postOnMainThread) {
2184 deferredBindRunnables.add(r);
2185 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002186 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 }
2188 }
2189 }
2190
2191 /**
2192 * Binds all loaded data to actual views on the main thread.
2193 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002194 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 final long t = SystemClock.uptimeMillis();
2196 Runnable r;
2197
2198 // Don't use these two variables in any of the callback runnables.
2199 // Otherwise we hold a reference to them.
2200 final Callbacks oldCallbacks = mCallbacks.get();
2201 if (oldCallbacks == null) {
2202 // This launcher has exited and nobody bothered to tell us. Just bail.
2203 Log.w(TAG, "LoaderTask running with no launcher");
2204 return;
2205 }
2206
Winson Chung4a2afa32012-07-19 14:53:05 -07002207 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2208 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 oldCallbacks.getCurrentWorkspaceScreen();
2210
2211 // Load all the items that are on the current page first (and in the process, unbind
2212 // all the existing workspace items before we call startBinding() below.
2213 unbindWorkspaceItemsOnMainThread();
2214 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2215 ArrayList<LauncherAppWidgetInfo> appWidgets =
2216 new ArrayList<LauncherAppWidgetInfo>();
2217 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2218 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002220 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 workspaceItems.addAll(sBgWorkspaceItems);
2222 appWidgets.addAll(sBgAppWidgets);
2223 folders.putAll(sBgFolders);
2224 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 }
2227
2228 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2229 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2230 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2231 new ArrayList<LauncherAppWidgetInfo>();
2232 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2233 new ArrayList<LauncherAppWidgetInfo>();
2234 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2235 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2236
2237 // Separate the items that are on the current screen, and all the other remaining items
2238 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2239 otherWorkspaceItems);
2240 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2241 otherAppWidgets);
2242 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2243 otherFolders);
2244 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2245 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2246
2247 // Tell the workspace that we're about to start binding items
2248 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002249 public void run() {
2250 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2251 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002253 }
2254 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 };
Winson Chung81b52252012-08-27 15:34:29 -07002256 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2259
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 // Load items on the current page
2261 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2262 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002263 if (isLoadingSynchronously) {
2264 r = new Runnable() {
2265 public void run() {
2266 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2267 if (callbacks != null) {
2268 callbacks.onPageBoundSynchronously(currentScreen);
2269 }
2270 }
2271 };
Winson Chung81b52252012-08-27 15:34:29 -07002272 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002273 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274
Winson Chung4a2afa32012-07-19 14:53:05 -07002275 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2276 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 mDeferredBindRunnables.clear();
2278 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002279 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280
2281 // Tell the workspace that we're done binding items
2282 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002283 public void run() {
2284 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2285 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002286 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002287 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288
Winson Chung98e030b2012-05-07 16:01:11 -07002289 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002290 if (DEBUG_LOADERS) {
2291 Log.d(TAG, "bound workspace in "
2292 + (SystemClock.uptimeMillis()-t) + "ms");
2293 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002294
2295 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002296 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002298 if (isLoadingSynchronously) {
2299 mDeferredBindRunnables.add(r);
2300 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002301 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002302 }
Joe Onorato36115782010-06-17 13:28:48 -04002303 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002304
Joe Onorato36115782010-06-17 13:28:48 -04002305 private void loadAndBindAllApps() {
2306 if (DEBUG_LOADERS) {
2307 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2308 }
2309 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002310 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002311 synchronized (LoaderTask.this) {
2312 if (mStopped) {
2313 return;
2314 }
2315 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002316 }
Joe Onorato36115782010-06-17 13:28:48 -04002317 } else {
2318 onlyBindAllApps();
2319 }
2320 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002321
Joe Onorato36115782010-06-17 13:28:48 -04002322 private void onlyBindAllApps() {
2323 final Callbacks oldCallbacks = mCallbacks.get();
2324 if (oldCallbacks == null) {
2325 // This launcher has exited and nobody bothered to tell us. Just bail.
2326 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2327 return;
2328 }
2329
2330 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002331 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002332 final ArrayList<AppInfo> list
2333 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002334 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002335 public void run() {
2336 final long t = SystemClock.uptimeMillis();
2337 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2338 if (callbacks != null) {
2339 callbacks.bindAllApplications(list);
2340 }
2341 if (DEBUG_LOADERS) {
2342 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2343 + (SystemClock.uptimeMillis()-t) + "ms");
2344 }
2345 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002346 };
2347 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002348 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002349 r.run();
2350 } else {
2351 mHandler.post(r);
2352 }
Joe Onorato36115782010-06-17 13:28:48 -04002353 }
2354
Winson Chung64359a52013-07-08 17:17:08 -07002355 private void loadAllApps() {
2356 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002357
Joe Onorato36115782010-06-17 13:28:48 -04002358 final Callbacks oldCallbacks = mCallbacks.get();
2359 if (oldCallbacks == null) {
2360 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002361 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002362 return;
2363 }
2364
Winson Chung64359a52013-07-08 17:17:08 -07002365 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002366 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2367 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2368
Winson Chung64359a52013-07-08 17:17:08 -07002369 // Clear the list of apps
2370 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002371
Winson Chung64359a52013-07-08 17:17:08 -07002372 // Query for the set of apps
2373 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2374 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2375 if (DEBUG_LOADERS) {
2376 Log.d(TAG, "queryIntentActivities took "
2377 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2378 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2379 }
2380 // Fail if we don't have any apps
2381 if (apps == null || apps.isEmpty()) {
2382 return;
2383 }
2384 // Sort the applications by name
2385 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2386 Collections.sort(apps,
2387 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2388 if (DEBUG_LOADERS) {
2389 Log.d(TAG, "sort took "
2390 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002391 }
2392
Winson Chung64359a52013-07-08 17:17:08 -07002393 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002394 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002395 ResolveInfo app = apps.get(i);
2396 if (oldCallbacks.shouldShowApp(app)) {
2397 // This builds the icon bitmaps.
2398 mBgAllAppsList.add(new AppInfo(packageManager, app,
2399 mIconCache, mLabelCache));
2400 }
Winson Chung64359a52013-07-08 17:17:08 -07002401 }
2402
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002403 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002404 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2405 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002406
2407 // Post callback on main thread
2408 mHandler.post(new Runnable() {
2409 public void run() {
2410 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002411 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002412 if (callbacks != null) {
2413 callbacks.bindAllApplications(added);
2414 if (DEBUG_LOADERS) {
2415 Log.d(TAG, "bound " + added.size() + " apps in "
2416 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2417 }
2418 } else {
2419 Log.i(TAG, "not binding apps: no Launcher activity");
2420 }
2421 }
2422 });
2423
Joe Onorato36115782010-06-17 13:28:48 -04002424 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002425 Log.d(TAG, "Icons processed in "
2426 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002427 }
2428 }
2429
2430 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002431 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002432 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2433 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2434 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2435 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2436 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2437 }
Joe Onorato36115782010-06-17 13:28:48 -04002438 }
2439 }
2440
2441 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002442 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002443 }
2444
2445 private class PackageUpdatedTask implements Runnable {
2446 int mOp;
2447 String[] mPackages;
2448
2449 public static final int OP_NONE = 0;
2450 public static final int OP_ADD = 1;
2451 public static final int OP_UPDATE = 2;
2452 public static final int OP_REMOVE = 3; // uninstlled
2453 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2454
2455
2456 public PackageUpdatedTask(int op, String[] packages) {
2457 mOp = op;
2458 mPackages = packages;
2459 }
2460
2461 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002462 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002463
2464 final String[] packages = mPackages;
2465 final int N = packages.length;
2466 switch (mOp) {
2467 case OP_ADD:
2468 for (int i=0; i<N; i++) {
2469 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002470 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002471 }
2472 break;
2473 case OP_UPDATE:
2474 for (int i=0; i<N; i++) {
2475 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002476 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002477 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002478 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002479 }
2480 break;
2481 case OP_REMOVE:
2482 case OP_UNAVAILABLE:
2483 for (int i=0; i<N; i++) {
2484 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002485 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002486 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002487 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002488 }
2489 break;
2490 }
2491
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002492 ArrayList<AppInfo> added = null;
2493 ArrayList<AppInfo> modified = null;
2494 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002495
Adam Cohen487f7dd2012-06-28 18:12:10 -07002496 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002497 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002498 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002499 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002500 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002501 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002502 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002503 }
Winson Chung5d55f332012-07-16 20:45:03 -07002504 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002505 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002506 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002507 }
2508
Joe Onorato36115782010-06-17 13:28:48 -04002509 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2510 if (callbacks == null) {
2511 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2512 return;
2513 }
2514
2515 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002516 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002517 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002518 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07002519 addAndBindAddedApps(context, addedInfos, cb, added);
Joe Onorato36115782010-06-17 13:28:48 -04002520 }
2521 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002522 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002523
2524 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002525 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002526 ArrayList<ItemInfo> infos =
2527 getItemInfoForComponentName(a.componentName);
2528 for (ItemInfo i : infos) {
2529 if (isShortcutInfoUpdateable(i)) {
2530 ShortcutInfo info = (ShortcutInfo) i;
2531 info.title = a.title.toString();
2532 updateItemInDatabase(context, info);
2533 }
2534 }
2535 }
2536
Joe Onorato36115782010-06-17 13:28:48 -04002537 mHandler.post(new Runnable() {
2538 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002539 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2540 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002541 callbacks.bindAppsUpdated(modifiedFinal);
2542 }
2543 }
2544 });
2545 }
Winson Chung83892cc2013-05-01 16:53:33 -07002546 // If a package has been removed, or an app has been removed as a result of
2547 // an update (for example), make the removed callback.
2548 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002549 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002550 final ArrayList<String> removedPackageNames =
2551 new ArrayList<String>(Arrays.asList(packages));
2552
Winson Chung64359a52013-07-08 17:17:08 -07002553 // Update the launcher db to reflect the removal of apps
2554 if (packageRemoved) {
2555 for (String pn : removedPackageNames) {
2556 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2557 for (ItemInfo i : infos) {
2558 deleteItemFromDatabase(context, i);
2559 }
2560 }
2561 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002562 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002563 ArrayList<ItemInfo> infos =
2564 getItemInfoForComponentName(a.componentName);
2565 for (ItemInfo i : infos) {
2566 deleteItemFromDatabase(context, i);
2567 }
2568 }
2569 }
2570
Joe Onorato36115782010-06-17 13:28:48 -04002571 mHandler.post(new Runnable() {
2572 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002573 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2574 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002575 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002576 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002577 }
2578 }
2579 });
Joe Onoratobe386092009-11-17 17:32:16 -08002580 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002581
Michael Jurkac402cd92013-05-20 15:49:32 +02002582 final ArrayList<Object> widgetsAndShortcuts =
2583 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002584 mHandler.post(new Runnable() {
2585 @Override
2586 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002587 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2588 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002589 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002590 }
2591 }
2592 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002593
2594 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002595 mHandler.post(new Runnable() {
2596 public void run() {
2597 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2598 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002599 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002600 }
2601 }
2602 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002603 }
2604 }
2605
Michael Jurkac402cd92013-05-20 15:49:32 +02002606 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2607 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2608 PackageManager packageManager = context.getPackageManager();
2609 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2610 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2611 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2612 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2613 Collections.sort(widgetsAndShortcuts,
2614 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2615 return widgetsAndShortcuts;
2616 }
2617
Winson Chung1323b482013-08-05 12:41:55 -07002618 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002619 if (cn == null) {
2620 return false;
2621 }
2622
2623 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002624 // Skip if the application is disabled
2625 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2626 if (!pi.applicationInfo.enabled) {
2627 return false;
2628 }
2629
2630 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002631 return (pm.getActivityInfo(cn, 0) != null);
2632 } catch (NameNotFoundException e) {
2633 return false;
2634 }
2635 }
2636
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002638 * This is called from the code that adds shortcuts from the intent receiver. This
2639 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002640 */
Joe Onorato56d82912010-03-07 14:32:10 -05002641 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002642 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002644
Joe Onorato56d82912010-03-07 14:32:10 -05002645 /**
2646 * Make an ShortcutInfo object for a shortcut that is an application.
2647 *
2648 * If c is not null, then it will be used to fill in missing data like the title and icon.
2649 */
2650 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002651 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002652 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002653 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002654 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002655 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002656 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002657 } else {
2658 try {
2659 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2660 info.initFlagsAndFirstInstallTime(pi);
2661 } catch (NameNotFoundException e) {
2662 Log.d(TAG, "getPackInfo failed for package " +
2663 componentName.getPackageName());
2664 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002665 }
2666
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002667 // TODO: See if the PackageManager knows about this case. If it doesn't
2668 // then return null & delete this.
2669
Joe Onorato56d82912010-03-07 14:32:10 -05002670 // the resource -- This may implicitly give us back the fallback icon,
2671 // but don't worry about that. All we're doing with usingFallbackIcon is
2672 // to avoid saving lots of copies of that in the database, and most apps
2673 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002674
2675 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2676 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2677 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002678 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002679 ResolveInfo resolveInfo = null;
2680 ComponentName oldComponent = intent.getComponent();
2681 Intent newIntent = new Intent(intent.getAction(), null);
2682 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2683 newIntent.setPackage(oldComponent.getPackageName());
2684 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2685 for (ResolveInfo i : infos) {
2686 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2687 i.activityInfo.name);
2688 if (cn.equals(oldComponent)) {
2689 resolveInfo = i;
2690 }
2691 }
2692 if (resolveInfo == null) {
2693 resolveInfo = manager.resolveActivity(intent, 0);
2694 }
Joe Onorato56d82912010-03-07 14:32:10 -05002695 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002696 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002697 }
Joe Onorato56d82912010-03-07 14:32:10 -05002698 // the db
2699 if (icon == null) {
2700 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002701 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002702 }
2703 }
2704 // the fallback icon
2705 if (icon == null) {
2706 icon = getFallbackIcon();
2707 info.usingFallbackIcon = true;
2708 }
2709 info.setIcon(icon);
2710
2711 // from the resource
2712 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002713 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2714 if (labelCache != null && labelCache.containsKey(key)) {
2715 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002716 } else {
2717 info.title = resolveInfo.activityInfo.loadLabel(manager);
2718 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002719 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002720 }
2721 }
Joe Onorato56d82912010-03-07 14:32:10 -05002722 }
2723 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002724 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002725 if (c != null) {
2726 info.title = c.getString(titleIndex);
2727 }
2728 }
2729 // fall back to the class name of the activity
2730 if (info.title == null) {
2731 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2734 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002736
Winson Chung64359a52013-07-08 17:17:08 -07002737 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2738 ItemInfoFilter f) {
2739 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2740 for (ItemInfo i : infos) {
2741 if (i instanceof ShortcutInfo) {
2742 ShortcutInfo info = (ShortcutInfo) i;
2743 ComponentName cn = info.intent.getComponent();
2744 if (cn != null && f.filterItem(null, info, cn)) {
2745 filtered.add(info);
2746 }
2747 } else if (i instanceof FolderInfo) {
2748 FolderInfo info = (FolderInfo) i;
2749 for (ShortcutInfo s : info.contents) {
2750 ComponentName cn = s.intent.getComponent();
2751 if (cn != null && f.filterItem(info, s, cn)) {
2752 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002753 }
2754 }
Winson Chung64359a52013-07-08 17:17:08 -07002755 } else if (i instanceof LauncherAppWidgetInfo) {
2756 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2757 ComponentName cn = info.providerName;
2758 if (cn != null && f.filterItem(null, info, cn)) {
2759 filtered.add(info);
2760 }
Winson Chung8a435102012-08-30 17:16:53 -07002761 }
2762 }
Winson Chung64359a52013-07-08 17:17:08 -07002763 return new ArrayList<ItemInfo>(filtered);
2764 }
2765
2766 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002767 ItemInfoFilter filter = new ItemInfoFilter() {
2768 @Override
2769 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2770 return cn.getPackageName().equals(pn);
2771 }
2772 };
2773 return filterItemInfos(sBgItemsIdMap.values(), filter);
2774 }
2775
2776 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002777 ItemInfoFilter filter = new ItemInfoFilter() {
2778 @Override
2779 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2780 return cn.equals(cname);
2781 }
2782 };
2783 return filterItemInfos(sBgItemsIdMap.values(), filter);
2784 }
2785
2786 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2787 if (i instanceof ShortcutInfo) {
2788 ShortcutInfo info = (ShortcutInfo) i;
2789 // We need to check for ACTION_MAIN otherwise getComponent() might
2790 // return null for some shortcuts (for instance, for shortcuts to
2791 // web pages.)
2792 Intent intent = info.intent;
2793 ComponentName name = intent.getComponent();
2794 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2795 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2796 return true;
2797 }
2798 }
2799 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002800 }
2801
2802 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002803 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002804 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002805 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002806 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2807 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002808
Joe Onorato56d82912010-03-07 14:32:10 -05002809 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002810 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002811 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002813 // TODO: If there's an explicit component and we can't install that, delete it.
2814
Joe Onorato56d82912010-03-07 14:32:10 -05002815 info.title = c.getString(titleIndex);
2816
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 int iconType = c.getInt(iconTypeIndex);
2818 switch (iconType) {
2819 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2820 String packageName = c.getString(iconPackageIndex);
2821 String resourceName = c.getString(iconResourceIndex);
2822 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002823 info.customIcon = false;
2824 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002827 if (resources != null) {
2828 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002829 icon = Utilities.createIconBitmap(
2830 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002833 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 }
Joe Onorato56d82912010-03-07 14:32:10 -05002835 // the db
2836 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002837 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002838 }
2839 // the fallback icon
2840 if (icon == null) {
2841 icon = getFallbackIcon();
2842 info.usingFallbackIcon = true;
2843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 break;
2845 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002846 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002847 if (icon == null) {
2848 icon = getFallbackIcon();
2849 info.customIcon = false;
2850 info.usingFallbackIcon = true;
2851 } else {
2852 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 break;
2855 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002856 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002857 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 info.customIcon = false;
2859 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002861 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 return info;
2863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864
Michael Jurka931dc972011-08-05 15:08:15 -07002865 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002866 @SuppressWarnings("all") // suppress dead code warning
2867 final boolean debug = false;
2868 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002869 Log.d(TAG, "getIconFromCursor app="
2870 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2871 }
2872 byte[] data = c.getBlob(iconIndex);
2873 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002874 return Utilities.createIconBitmap(
2875 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002876 } catch (Exception e) {
2877 return null;
2878 }
2879 }
2880
Winson Chung3d503fb2011-07-13 17:25:49 -07002881 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2882 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002883 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002884 if (info == null) {
2885 return null;
2886 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002887 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002888
2889 return info;
2890 }
2891
Winson Chunga9abd0e2010-10-27 17:18:37 -07002892 /**
Winson Chung55cef262010-10-28 14:14:18 -07002893 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2894 */
2895 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2896 ComponentName component) {
2897 List<AppWidgetProviderInfo> widgets =
2898 AppWidgetManager.getInstance(context).getInstalledProviders();
2899 for (AppWidgetProviderInfo info : widgets) {
2900 if (info.provider.equals(component)) {
2901 return info;
2902 }
2903 }
2904 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002905 }
2906
Winson Chung68846fd2010-10-29 11:00:27 -07002907 /**
2908 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2909 */
2910 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2911 final PackageManager packageManager = context.getPackageManager();
2912 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2913 new ArrayList<WidgetMimeTypeHandlerData>();
2914
2915 final Intent supportsIntent =
2916 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2917 supportsIntent.setType(mimeType);
2918
2919 // Create a set of widget configuration components that we can test against
2920 final List<AppWidgetProviderInfo> widgets =
2921 AppWidgetManager.getInstance(context).getInstalledProviders();
2922 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2923 new HashMap<ComponentName, AppWidgetProviderInfo>();
2924 for (AppWidgetProviderInfo info : widgets) {
2925 configurationComponentToWidget.put(info.configure, info);
2926 }
2927
2928 // Run through each of the intents that can handle this type of clip data, and cross
2929 // reference them with the components that are actual configuration components
2930 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2931 PackageManager.MATCH_DEFAULT_ONLY);
2932 for (ResolveInfo info : activities) {
2933 final ActivityInfo activityInfo = info.activityInfo;
2934 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2935 activityInfo.name);
2936 if (configurationComponentToWidget.containsKey(infoComponent)) {
2937 supportedConfigurationActivities.add(
2938 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2939 configurationComponentToWidget.get(infoComponent)));
2940 }
2941 }
2942 return supportedConfigurationActivities;
2943 }
2944
Winson Chunga9abd0e2010-10-27 17:18:37 -07002945 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002946 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2947 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2948 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2949
Adam Cohend9198822011-11-22 16:42:47 -08002950 if (intent == null) {
2951 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2952 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2953 return null;
2954 }
2955
Joe Onorato0589f0f2010-02-08 13:44:00 -08002956 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002957 boolean customIcon = false;
2958 ShortcutIconResource iconResource = null;
2959
2960 if (bitmap != null && bitmap instanceof Bitmap) {
2961 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002962 customIcon = true;
2963 } else {
2964 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2965 if (extra != null && extra instanceof ShortcutIconResource) {
2966 try {
2967 iconResource = (ShortcutIconResource) extra;
2968 final PackageManager packageManager = context.getPackageManager();
2969 Resources resources = packageManager.getResourcesForApplication(
2970 iconResource.packageName);
2971 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002972 icon = Utilities.createIconBitmap(
2973 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002974 } catch (Exception e) {
2975 Log.w(TAG, "Could not load shortcut icon: " + extra);
2976 }
2977 }
2978 }
2979
Michael Jurkac9d95c52011-08-29 14:03:34 -07002980 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002981
2982 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002983 if (fallbackIcon != null) {
2984 icon = fallbackIcon;
2985 } else {
2986 icon = getFallbackIcon();
2987 info.usingFallbackIcon = true;
2988 }
Joe Onorato56d82912010-03-07 14:32:10 -05002989 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002990 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002991
Joe Onorato0589f0f2010-02-08 13:44:00 -08002992 info.title = name;
2993 info.intent = intent;
2994 info.customIcon = customIcon;
2995 info.iconResource = iconResource;
2996
2997 return info;
2998 }
2999
Winson Chungaac01e12011-08-17 10:37:13 -07003000 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3001 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003002 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003003 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003004 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003005 }
Joe Onorato56d82912010-03-07 14:32:10 -05003006 // If this icon doesn't have a custom icon, check to see
3007 // what's stored in the DB, and if it doesn't match what
3008 // we're going to show, store what we are going to show back
3009 // into the DB. We do this so when we're loading, if the
3010 // package manager can't find an icon (for example because
3011 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003012 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003013 cache.put(info, c.getBlob(iconIndex));
3014 return true;
3015 }
3016 return false;
3017 }
3018 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3019 boolean needSave = false;
3020 try {
3021 if (data != null) {
3022 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3023 Bitmap loaded = info.getIcon(mIconCache);
3024 needSave = !saved.sameAs(loaded);
3025 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003026 needSave = true;
3027 }
Winson Chungaac01e12011-08-17 10:37:13 -07003028 } catch (Exception e) {
3029 needSave = true;
3030 }
3031 if (needSave) {
3032 Log.d(TAG, "going to save icon bitmap for info=" + info);
3033 // This is slower than is ideal, but this only happens once
3034 // or when the app is updated with a new icon.
3035 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003036 }
3037 }
3038
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003040 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 * or make a new one.
3042 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003043 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044 // See if a placeholder was created for us already
3045 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003046 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003048 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 folders.put(id, folderInfo);
3050 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003051 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 }
3053
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003054 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003055 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003056 return new Comparator<AppInfo>() {
3057 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003058 int result = collator.compare(a.title.toString(), b.title.toString());
3059 if (result == 0) {
3060 result = a.componentName.compareTo(b.componentName);
3061 }
3062 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003063 }
Winson Chung11904872012-09-17 16:58:46 -07003064 };
3065 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003066 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3067 = new Comparator<AppInfo>() {
3068 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003069 if (a.firstInstallTime < b.firstInstallTime) return 1;
3070 if (a.firstInstallTime > b.firstInstallTime) return -1;
3071 return 0;
3072 }
3073 };
Winson Chung11904872012-09-17 16:58:46 -07003074 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3075 final Collator collator = Collator.getInstance();
3076 return new Comparator<AppWidgetProviderInfo>() {
3077 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3078 return collator.compare(a.label.toString(), b.label.toString());
3079 }
3080 };
3081 }
Winson Chung5308f242011-08-18 12:12:41 -07003082 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3083 if (info.activityInfo != null) {
3084 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3085 } else {
3086 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3087 }
3088 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003089 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003090 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003091 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003092 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003093 ShortcutNameComparator(PackageManager pm) {
3094 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003095 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003096 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003097 }
3098 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3099 mPackageManager = pm;
3100 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003101 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003102 }
3103 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003104 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003105 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3106 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3107 if (mLabelCache.containsKey(keyA)) {
3108 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003109 } else {
3110 labelA = a.loadLabel(mPackageManager).toString();
3111
Winson Chung5308f242011-08-18 12:12:41 -07003112 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003113 }
Winson Chung5308f242011-08-18 12:12:41 -07003114 if (mLabelCache.containsKey(keyB)) {
3115 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003116 } else {
3117 labelB = b.loadLabel(mPackageManager).toString();
3118
Winson Chung5308f242011-08-18 12:12:41 -07003119 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003120 }
Winson Chung11904872012-09-17 16:58:46 -07003121 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003122 }
3123 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003124 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003125 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003126 private PackageManager mPackageManager;
3127 private HashMap<Object, String> mLabelCache;
3128 WidgetAndShortcutNameComparator(PackageManager pm) {
3129 mPackageManager = pm;
3130 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003131 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003132 }
3133 public final int compare(Object a, Object b) {
3134 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003135 if (mLabelCache.containsKey(a)) {
3136 labelA = mLabelCache.get(a);
3137 } else {
3138 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003139 ((AppWidgetProviderInfo) a).label :
3140 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003141 mLabelCache.put(a, labelA);
3142 }
3143 if (mLabelCache.containsKey(b)) {
3144 labelB = mLabelCache.get(b);
3145 } else {
3146 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003147 ((AppWidgetProviderInfo) b).label :
3148 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 mLabelCache.put(b, labelB);
3150 }
Winson Chung11904872012-09-17 16:58:46 -07003151 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003152 }
3153 };
Joe Onoratobe386092009-11-17 17:32:16 -08003154
3155 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003156 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003157 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3158 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3159 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3160 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003161 if (mLoaderTask != null) {
3162 mLoaderTask.dumpState();
3163 } else {
3164 Log.d(TAG, "mLoaderTask=null");
3165 }
Joe Onoratobe386092009-11-17 17:32:16 -08003166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167}