blob: 1598e2b47906ecdb54c15b94e98f2472750763c6 [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 Chung94d67682013-09-25 16:29:40 -0700284 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
285 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700286 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700287 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700288 }
Winson Chung94d67682013-09-25 16:29:40 -0700289 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
290 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
291 if (workspaceApps.isEmpty() && allAppsApps.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 Chung94d67682013-09-25 16:29:40 -0700311 Iterator<ItemInfo> iter = workspaceApps.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 Chung94d67682013-09-25 16:29:40 -0700359
Winson Chung64359a52013-07-08 17:17:08 -0700360 // Add the shortcut to the db
361 addItemToDatabase(context, shortcutInfo,
362 LauncherSettings.Favorites.CONTAINER_DESKTOP,
363 coords.first, coords.second[0], coords.second[1], false);
364 // Save the ShortcutInfo for binding in the workspace
365 addedShortcutsFinal.add(shortcutInfo);
366 }
367 }
368
Winson Chung76828c82013-08-19 15:43:29 -0700369 // Update the workspace screens
370 updateWorkspaceScreenOrder(context, workspaceScreens);
371
Winson Chung94d67682013-09-25 16:29:40 -0700372 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700373 runOnMainThread(new Runnable() {
374 public void run() {
375 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
376 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700377 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
378 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700379 if (!addedShortcutsFinal.isEmpty()) {
380 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
381 long lastScreenId = info.screenId;
382 for (ItemInfo i : addedShortcutsFinal) {
383 if (i.screenId == lastScreenId) {
384 addAnimated.add(i);
385 } else {
386 addNotAnimated.add(i);
387 }
Winson Chung997a9232013-07-24 15:33:46 -0700388 }
389 }
Winson Chungd64d1762013-08-20 14:37:16 -0700390 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700391 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700392 }
Winson Chung64359a52013-07-08 17:17:08 -0700393 }
Winson Chung997a9232013-07-24 15:33:46 -0700394 });
395 }
Winson Chung64359a52013-07-08 17:17:08 -0700396 }
397 };
398 runOnWorkerThread(r);
399 }
400
Joe Onorato56d82912010-03-07 14:32:10 -0500401 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800402 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404
Winson Chung81b52252012-08-27 15:34:29 -0700405 public void unbindItemInfosAndClearQueuedBindRunnables() {
406 if (sWorkerThread.getThreadId() == Process.myTid()) {
407 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
408 "main thread");
409 }
410
411 // Clear any deferred bind runnables
412 mDeferredBindRunnables.clear();
413 // Remove any queued bind runnables
414 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
415 // Unbind all the workspace items
416 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700417 }
418
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700419 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700420 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700421 // Ensure that we don't use the same workspace items data structure on the main thread
422 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700423 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
424 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700425 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
427 tmpAppWidgets.addAll(sBgAppWidgets);
428 }
429 Runnable r = new Runnable() {
430 @Override
431 public void run() {
432 for (ItemInfo item : tmpWorkspaceItems) {
433 item.unbind();
434 }
435 for (ItemInfo item : tmpAppWidgets) {
436 item.unbind();
437 }
438 }
439 };
440 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700441 }
442
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 /**
444 * Adds an item to the DB if it was not created previously, or move it to a new
445 * <container, screen, cellX, cellY>
446 */
447 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700448 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 if (item.container == ItemInfo.NO_ID) {
450 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700451 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400452 } else {
453 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456 }
457
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700458 static void checkItemInfoLocked(
459 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
460 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
461 if (modelItem != null && item != modelItem) {
462 // check all the data is consistent
463 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
464 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
465 ShortcutInfo shortcut = (ShortcutInfo) item;
466 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
467 modelShortcut.intent.filterEquals(shortcut.intent) &&
468 modelShortcut.id == shortcut.id &&
469 modelShortcut.itemType == shortcut.itemType &&
470 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700471 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700472 modelShortcut.cellX == shortcut.cellX &&
473 modelShortcut.cellY == shortcut.cellY &&
474 modelShortcut.spanX == shortcut.spanX &&
475 modelShortcut.spanY == shortcut.spanY &&
476 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
477 (modelShortcut.dropPos != null &&
478 shortcut.dropPos != null &&
479 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
480 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
481 // For all intents and purposes, this is the same object
482 return;
483 }
484 }
485
486 // the modelItem needs to match up perfectly with item if our model is
487 // to be consistent with the database-- for now, just require
488 // modelItem == item or the equality check above
489 String msg = "item: " + ((item != null) ? item.toString() : "null") +
490 "modelItem: " +
491 ((modelItem != null) ? modelItem.toString() : "null") +
492 "Error: ItemInfo passed to checkItemInfo doesn't match original";
493 RuntimeException e = new RuntimeException(msg);
494 if (stackTrace != null) {
495 e.setStackTrace(stackTrace);
496 }
Adam Cohene25af792013-06-06 23:08:25 -0700497 // TODO: something breaks this in the upgrade path
498 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700499 }
500 }
501
Michael Jurka816474f2012-06-25 14:49:02 -0700502 static void checkItemInfo(final ItemInfo item) {
503 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
504 final long itemId = item.id;
505 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700506 public void run() {
507 synchronized (sBgLock) {
508 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700509 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700510 }
511 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700512 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700513 }
514
Michael Jurkac9d95c52011-08-29 14:03:34 -0700515 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
516 final ItemInfo item, final String callingFunction) {
517 final long itemId = item.id;
518 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
519 final ContentResolver cr = context.getContentResolver();
520
Adam Cohen487f7dd2012-06-28 18:12:10 -0700521 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700522 Runnable r = new Runnable() {
523 public void run() {
524 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700525 updateItemArrays(item, itemId, stackTrace);
526 }
527 };
528 runOnWorkerThread(r);
529 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700530
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700531 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
532 final ArrayList<ItemInfo> items, final String callingFunction) {
533 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700534
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700535 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
536 Runnable r = new Runnable() {
537 public void run() {
538 ArrayList<ContentProviderOperation> ops =
539 new ArrayList<ContentProviderOperation>();
540 int count = items.size();
541 for (int i = 0; i < count; i++) {
542 ItemInfo item = items.get(i);
543 final long itemId = item.id;
544 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
545 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700546
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700547 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
548 updateItemArrays(item, itemId, stackTrace);
549
550 }
551 try {
552 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
553 } catch (Exception e) {
554 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700555 }
556 }
557 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700558 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700559 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700560
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700561 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
562 // Lock on mBgLock *after* the db operation
563 synchronized (sBgLock) {
564 checkItemInfoLocked(itemId, item, stackTrace);
565
566 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
567 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
568 // Item is in a folder, make sure this folder exists
569 if (!sBgFolders.containsKey(item.container)) {
570 // An items container is being set to a that of an item which is not in
571 // the list of Folders.
572 String msg = "item: " + item + " container being set to: " +
573 item.container + ", not in the list of folders";
574 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700575 }
576 }
577
578 // Items are added/removed from the corresponding FolderInfo elsewhere, such
579 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
580 // that are on the desktop, as appropriate
581 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
582 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
583 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
584 switch (modelItem.itemType) {
585 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
586 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
587 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
588 if (!sBgWorkspaceItems.contains(modelItem)) {
589 sBgWorkspaceItems.add(modelItem);
590 }
591 break;
592 default:
593 break;
594 }
595 } else {
596 sBgWorkspaceItems.remove(modelItem);
597 }
598 }
599 }
600
Michael Jurkac7700af2013-05-14 20:17:58 +0200601 public void flushWorkerThread() {
602 mFlushingWorkerThread = true;
603 Runnable waiter = new Runnable() {
604 public void run() {
605 synchronized (this) {
606 notifyAll();
607 mFlushingWorkerThread = false;
608 }
609 }
610 };
611
612 synchronized(waiter) {
613 runOnWorkerThread(waiter);
614 if (mLoaderTask != null) {
615 synchronized(mLoaderTask) {
616 mLoaderTask.notify();
617 }
618 }
619 boolean success = false;
620 while (!success) {
621 try {
622 waiter.wait();
623 success = true;
624 } catch (InterruptedException e) {
625 }
626 }
627 }
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700632 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700633 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.cellX = cellX;
637 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700638
Winson Chung3d503fb2011-07-13 17:25:49 -0700639 // We store hotseat items in canonical form which is this orientation invariant position
640 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647
648 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
651 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700652 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653
Michael Jurkac9d95c52011-08-29 14:03:34 -0700654 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700655 }
656
657 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
659 * cellX, cellY have already been updated on the ItemInfos.
660 */
661 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
662 final long container, final int screen) {
663
664 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
665 int count = items.size();
666
667 for (int i = 0; i < count; i++) {
668 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 item.container = container;
670
671 // We store hotseat items in canonical form which is this orientation invariant position
672 // in the hotseat
673 if (context instanceof Launcher && screen < 0 &&
674 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700675 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 item.cellY);
677 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 }
680
681 final ContentValues values = new ContentValues();
682 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
683 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
684 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686
687 contentValues.add(values);
688 }
689 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
690 }
691
692 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700693 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800694 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700695 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700696 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800697 item.cellX = cellX;
698 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 item.spanX = spanX;
700 item.spanY = spanY;
701
702 // We store hotseat items in canonical form which is this orientation invariant position
703 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700705 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 }
Adam Cohend4844c32011-02-18 19:25:06 -0800710
Adam Cohend4844c32011-02-18 19:25:06 -0800711 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800712 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
714 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
715 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
716 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800718
Michael Jurka816474f2012-06-25 14:49:02 -0700719 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721
722 /**
723 * Update an item to the database in a specified container.
724 */
725 static void updateItemInDatabase(Context context, final ItemInfo item) {
726 final ContentValues values = new ContentValues();
727 item.onAddToDatabase(values);
728 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
729 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800730 }
731
732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 * Returns true if the shortcuts already exists in the database.
734 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 static boolean shortcutExists(Context context, String title, Intent intent) {
737 final ContentResolver cr = context.getContentResolver();
738 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
739 new String[] { "title", "intent" }, "title=? and intent=?",
740 new String[] { title, intent.toUri(0) }, null);
741 boolean result = false;
742 try {
743 result = c.moveToFirst();
744 } finally {
745 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700748 }
749
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700751 * Returns an ItemInfo array containing all the items in the LauncherModel.
752 * The ItemInfo.id is not set through this function.
753 */
754 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
755 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
756 final ContentResolver cr = context.getContentResolver();
757 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
758 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
759 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
760 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
761
762 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
763 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
764 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
765 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
766 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
767 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
768 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
769
770 try {
771 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700772 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700773 item.cellX = c.getInt(cellXIndex);
774 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700775 item.spanX = Math.max(1, c.getInt(spanXIndex));
776 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 item.container = c.getInt(containerIndex);
778 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700780
781 items.add(item);
782 }
783 } catch (Exception e) {
784 items.clear();
785 } finally {
786 c.close();
787 }
788
789 return items;
790 }
791
792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
794 */
795 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
796 final ContentResolver cr = context.getContentResolver();
797 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
798 "_id=? and (itemType=? or itemType=?)",
799 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700800 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700801
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 try {
803 if (c.moveToFirst()) {
804 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
805 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
806 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
807 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
808 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
809 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 FolderInfo folderInfo = null;
812 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700813 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
814 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700816 }
817
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 folderInfo.title = c.getString(titleIndex);
819 folderInfo.id = id;
820 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700821 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700822 folderInfo.cellX = c.getInt(cellXIndex);
823 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824
825 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 } finally {
828 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700829 }
830
831 return null;
832 }
833
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 /**
835 * Add an item to the database in a specified container. Sets the container, screen, cellX and
836 * cellY fields of the item. Also assigns an ID to the item.
837 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700838 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 item.cellX = cellX;
842 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 // We store hotseat items in canonical form which is this orientation invariant position
844 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851
852 final ContentValues values = new ContentValues();
853 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 item.onAddToDatabase(values);
855
Michael Jurka414300a2013-08-27 15:42:35 +0200856 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700857 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700859
Michael Jurkac9d95c52011-08-29 14:03:34 -0700860 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700861 public void run() {
862 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
863 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700865 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700866 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700867 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 sBgItemsIdMap.put(item.id, item);
869 switch (item.itemType) {
870 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
871 sBgFolders.put(item.id, (FolderInfo) item);
872 // Fall through
873 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
874 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
875 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
876 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
877 sBgWorkspaceItems.add(item);
878 } else {
879 if (!sBgFolders.containsKey(item.container)) {
880 // Adding an item to a folder that doesn't exist.
881 String msg = "adding item: " + item + " to a folder that " +
882 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700883 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 break;
887 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
888 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
889 break;
890 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700891 }
892 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700893 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700895 }
896
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700898 * Creates a new unique child id, for a given cell span across all layouts.
899 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700900 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700901 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700902 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 }
905
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700907 * Removes the specified item from the database
908 * @param context
909 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700911 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700914
Michael Jurka83df1882011-08-31 20:59:26 -0700915 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700916 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700917 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700918
919 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700920 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921 switch (item.itemType) {
922 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
923 sBgFolders.remove(item.id);
924 for (ItemInfo info: sBgItemsIdMap.values()) {
925 if (info.container == item.id) {
926 // We are deleting a folder which still contains items that
927 // think they are contained by that folder.
928 String msg = "deleting a folder (" + item + ") which still " +
929 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700930 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700931 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700932 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 sBgWorkspaceItems.remove(item);
934 break;
935 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
936 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
937 sBgWorkspaceItems.remove(item);
938 break;
939 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
940 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
941 break;
942 }
943 sBgItemsIdMap.remove(item.id);
944 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700945 }
946 }
Michael Jurka83df1882011-08-31 20:59:26 -0700947 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700948 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 }
950
951 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 * Update the order of the workspace screens in the database. The array list contains
953 * a list of screen ids in the order that they should appear.
954 */
Winson Chungc9168342013-06-26 14:54:55 -0700955 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700956 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 final ContentResolver cr = context.getContentResolver();
958 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
959
960 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700961 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 while (iter.hasNext()) {
963 long id = iter.next();
964 if (id < 0) {
965 iter.remove();
966 }
967 }
968
969 Runnable r = new Runnable() {
970 @Override
971 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700972 // Clear the table
973 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700974 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 ContentValues[] values = new ContentValues[count];
976 for (int i = 0; i < count; i++) {
977 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700978 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
980 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 values[i] = v;
982 }
983 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700984
Winson Chungba9c37f2013-08-30 14:11:37 -0700985 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700986 sBgWorkspaceScreens.clear();
987 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700988 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 }
990 };
991 runOnWorkerThread(r);
992 }
993
994 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 * Remove the contents of the specified folder from the database
996 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700997 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 final ContentResolver cr = context.getContentResolver();
999
Michael Jurkac9d95c52011-08-29 14:03:34 -07001000 Runnable r = new Runnable() {
1001 public void run() {
1002 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001003 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001004 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 sBgItemsIdMap.remove(info.id);
1006 sBgFolders.remove(info.id);
1007 sBgDbIconCache.remove(info);
1008 sBgWorkspaceItems.remove(info);
1009 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1012 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001014 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 for (ItemInfo childInfo : info.contents) {
1016 sBgItemsIdMap.remove(childInfo.id);
1017 sBgDbIconCache.remove(childInfo);
1018 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001020 }
1021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024
1025 /**
1026 * Set this as the current Launcher activity object for the loader.
1027 */
1028 public void initialize(Callbacks callbacks) {
1029 synchronized (mLock) {
1030 mCallbacks = new WeakReference<Callbacks>(callbacks);
1031 }
1032 }
1033
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001034 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1036 * ACTION_PACKAGE_CHANGED.
1037 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001038 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001039 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001040 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001041
Joe Onorato36115782010-06-17 13:28:48 -04001042 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001043
Joe Onorato36115782010-06-17 13:28:48 -04001044 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1045 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1046 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1047 final String packageName = intent.getData().getSchemeSpecificPart();
1048 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049
Joe Onorato36115782010-06-17 13:28:48 -04001050 int op = PackageUpdatedTask.OP_NONE;
1051
1052 if (packageName == null || packageName.length() == 0) {
1053 // they sent us a bad intent
1054 return;
1055 }
1056
1057 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1058 op = PackageUpdatedTask.OP_UPDATE;
1059 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1060 if (!replacing) {
1061 op = PackageUpdatedTask.OP_REMOVE;
1062 }
1063 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1064 // later, we will update the package at this time
1065 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1066 if (!replacing) {
1067 op = PackageUpdatedTask.OP_ADD;
1068 } else {
1069 op = PackageUpdatedTask.OP_UPDATE;
1070 }
1071 }
1072
1073 if (op != PackageUpdatedTask.OP_NONE) {
1074 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1075 }
1076
1077 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001078 // First, schedule to add these apps back in.
1079 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1080 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1081 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001082 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001083 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1084 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1085 enqueuePackageUpdated(new PackageUpdatedTask(
1086 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001087 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001088 // If we have changed locale we need to clear out the labels in all apps/workspace.
1089 forceReload();
1090 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1091 // Check if configuration change was an mcc/mnc change which would affect app resources
1092 // and we would need to clear out the labels in all apps/workspace. Same handling as
1093 // above for ACTION_LOCALE_CHANGED
1094 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001095 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001096 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001097 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001098 forceReload();
1099 }
1100 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001101 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001102 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1103 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001104 if (mCallbacks != null) {
1105 Callbacks callbacks = mCallbacks.get();
1106 if (callbacks != null) {
1107 callbacks.bindSearchablesChanged();
1108 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001109 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001110 }
1111 }
1112
Reena Lee93f824a2011-09-23 17:20:28 -07001113 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001114 resetLoadedState(true, true);
1115
Reena Lee93f824a2011-09-23 17:20:28 -07001116 // Do this here because if the launcher activity is running it will be restarted.
1117 // If it's not running startLoaderFromBackground will merely tell it that it needs
1118 // to reload.
1119 startLoaderFromBackground();
1120 }
1121
Winson Chungf0c6ae02012-03-21 16:10:31 -07001122 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1123 synchronized (mLock) {
1124 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1125 // mWorkspaceLoaded to true later
1126 stopLoaderLocked();
1127 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1128 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1129 }
1130 }
1131
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001132 /**
1133 * When the launcher is in the background, it's possible for it to miss paired
1134 * configuration changes. So whenever we trigger the loader from the background
1135 * tell the launcher that it needs to re-run the loader when it comes back instead
1136 * of doing it now.
1137 */
1138 public void startLoaderFromBackground() {
1139 boolean runLoader = false;
1140 if (mCallbacks != null) {
1141 Callbacks callbacks = mCallbacks.get();
1142 if (callbacks != null) {
1143 // Only actually run the loader if they're not paused.
1144 if (!callbacks.setLoadOnResume()) {
1145 runLoader = true;
1146 }
1147 }
1148 }
1149 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001150 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001151 }
Joe Onorato36115782010-06-17 13:28:48 -04001152 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001153
Reena Lee93f824a2011-09-23 17:20:28 -07001154 // If there is already a loader task running, tell it to stop.
1155 // returns true if isLaunching() was true on the old task
1156 private boolean stopLoaderLocked() {
1157 boolean isLaunching = false;
1158 LoaderTask oldTask = mLoaderTask;
1159 if (oldTask != null) {
1160 if (oldTask.isLaunching()) {
1161 isLaunching = true;
1162 }
1163 oldTask.stopLocked();
1164 }
1165 return isLaunching;
1166 }
1167
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001168 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001169 synchronized (mLock) {
1170 if (DEBUG_LOADERS) {
1171 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1172 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 // Clear any deferred bind-runnables from the synchronized load process
1175 // We must do this before any loading/binding is scheduled below.
1176 mDeferredBindRunnables.clear();
1177
Joe Onorato36115782010-06-17 13:28:48 -04001178 // Don't bother to start the thread if we know it's not going to do anything
1179 if (mCallbacks != null && mCallbacks.get() != null) {
1180 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001181 // also, don't downgrade isLaunching if we're already running
1182 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001183 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1185 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1186 } else {
1187 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1188 sWorker.post(mLoaderTask);
1189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 }
1192 }
1193
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 void bindRemainingSynchronousPages() {
1195 // Post the remaining side pages to be loaded
1196 if (!mDeferredBindRunnables.isEmpty()) {
1197 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001198 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 }
1200 mDeferredBindRunnables.clear();
1201 }
1202 }
1203
Joe Onorato36115782010-06-17 13:28:48 -04001204 public void stopLoader() {
1205 synchronized (mLock) {
1206 if (mLoaderTask != null) {
1207 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 }
1209 }
Joe Onorato36115782010-06-17 13:28:48 -04001210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211
Winson Chung76828c82013-08-19 15:43:29 -07001212 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1213 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1214 final ContentResolver contentResolver = context.getContentResolver();
1215 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1216 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1217 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1218
1219 try {
1220 final int idIndex = sc.getColumnIndexOrThrow(
1221 LauncherSettings.WorkspaceScreens._ID);
1222 final int rankIndex = sc.getColumnIndexOrThrow(
1223 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1224 while (sc.moveToNext()) {
1225 try {
1226 long screenId = sc.getLong(idIndex);
1227 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001228 orderedScreens.put(rank, screenId);
1229 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001230 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001231 }
1232 }
1233 } finally {
1234 sc.close();
1235 }
1236 return orderedScreens;
1237 }
1238
Michael Jurkac57b7a82011-08-09 22:02:20 -07001239 public boolean isAllAppsLoaded() {
1240 return mAllAppsLoaded;
1241 }
1242
Winson Chung36a62fe2012-05-06 18:04:42 -07001243 boolean isLoadingWorkspace() {
1244 synchronized (mLock) {
1245 if (mLoaderTask != null) {
1246 return mLoaderTask.isLoadingWorkspace();
1247 }
1248 }
1249 return false;
1250 }
1251
Joe Onorato36115782010-06-17 13:28:48 -04001252 /**
1253 * Runnable for the thread that loads the contents of the launcher:
1254 * - workspace icons
1255 * - widgets
1256 * - all apps icons
1257 */
1258 private class LoaderTask implements Runnable {
1259 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001260 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001261 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001262 private boolean mStopped;
1263 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001264
Winson Chungc3eecff2011-07-11 17:44:15 -07001265 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001266
1267 LoaderTask(Context context, boolean isLaunching) {
1268 mContext = context;
1269 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001270 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 }
1272
Joe Onorato36115782010-06-17 13:28:48 -04001273 boolean isLaunching() {
1274 return mIsLaunching;
1275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276
Winson Chung36a62fe2012-05-06 18:04:42 -07001277 boolean isLoadingWorkspace() {
1278 return mIsLoadingAndBindingWorkspace;
1279 }
1280
Winson Chungc763c4e2013-07-19 13:49:06 -07001281 /** Returns whether this is an upgrade path */
1282 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001283 mIsLoadingAndBindingWorkspace = true;
1284
Joe Onorato36115782010-06-17 13:28:48 -04001285 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001286 if (DEBUG_LOADERS) {
1287 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001288 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001289
Winson Chungc763c4e2013-07-19 13:49:06 -07001290 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001291 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001293 synchronized (LoaderTask.this) {
1294 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001295 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001296 }
1297 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 }
1299 }
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001302 bindWorkspace(-1, isUpgradePath);
1303 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001304 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001305
Joe Onorato36115782010-06-17 13:28:48 -04001306 private void waitForIdle() {
1307 // Wait until the either we're stopped or the other threads are done.
1308 // This way we don't start loading all apps until the workspace has settled
1309 // down.
1310 synchronized (LoaderTask.this) {
1311 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001312
Joe Onorato36115782010-06-17 13:28:48 -04001313 mHandler.postIdle(new Runnable() {
1314 public void run() {
1315 synchronized (LoaderTask.this) {
1316 mLoadAndBindStepFinished = true;
1317 if (DEBUG_LOADERS) {
1318 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001319 }
Joe Onorato36115782010-06-17 13:28:48 -04001320 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001321 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
Joe Onorato36115782010-06-17 13:28:48 -04001323 });
1324
Michael Jurkac7700af2013-05-14 20:17:58 +02001325 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001326 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001327 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1328 // wait no longer than 1sec at a time
1329 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001330 } catch (InterruptedException ex) {
1331 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001332 }
1333 }
Joe Onorato36115782010-06-17 13:28:48 -04001334 if (DEBUG_LOADERS) {
1335 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001336 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001337 + "ms for previous step to finish binding");
1338 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001341
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 void runBindSynchronousPage(int synchronousBindPage) {
1343 if (synchronousBindPage < 0) {
1344 // Ensure that we have a valid page index to load synchronously
1345 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1346 "valid page index");
1347 }
1348 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1349 // Ensure that we don't try and bind a specified page when the pages have not been
1350 // loaded already (we should load everything asynchronously in that case)
1351 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1352 }
1353 synchronized (mLock) {
1354 if (mIsLoaderTaskRunning) {
1355 // Ensure that we are never running the background loading at this point since
1356 // we also touch the background collections
1357 throw new RuntimeException("Error! Background loading is already running");
1358 }
1359 }
1360
1361 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1362 // data structures, we can't allow any other thread to touch that data, but because
1363 // this call is synchronous, we can get away with not locking).
1364
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001365 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001366 // operations from the previous activity. We need to ensure that all queued operations
1367 // are executed before any synchronous binding work is done.
1368 mHandler.flush();
1369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 // Divide the set of loaded items into those that we are binding synchronously, and
1371 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001372 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1374 // arise from that.
1375 onlyBindAllApps();
1376 }
1377
Joe Onorato36115782010-06-17 13:28:48 -04001378 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001379 boolean isUpgrade = false;
1380
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001381 synchronized (mLock) {
1382 mIsLoaderTaskRunning = true;
1383 }
Joe Onorato36115782010-06-17 13:28:48 -04001384 // Optimize for end-user experience: if the Launcher is up and // running with the
1385 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1386 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001387 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 // Elevate priority when Home launches for the first time to avoid
1389 // starving at boot time. Staring at a blank home is not cool.
1390 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001391 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1392 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001393 android.os.Process.setThreadPriority(mIsLaunching
1394 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1395 }
Winson Chung64359a52013-07-08 17:17:08 -07001396 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001397 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001398
Joe Onorato36115782010-06-17 13:28:48 -04001399 if (mStopped) {
1400 break keep_running;
1401 }
1402
1403 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1404 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 synchronized (mLock) {
1406 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001407 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1409 }
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001412
1413 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001414 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1415 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001416
1417 // Restore the default thread priority after we are done loading items
1418 synchronized (mLock) {
1419 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1420 }
Joe Onorato36115782010-06-17 13:28:48 -04001421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422
Winson Chungaac01e12011-08-17 10:37:13 -07001423 // Update the saved icons if necessary
1424 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001425 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426 for (Object key : sBgDbIconCache.keySet()) {
1427 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1428 }
1429 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001430 }
Winson Chungaac01e12011-08-17 10:37:13 -07001431
Winson Chungc58497e2013-09-03 17:48:37 -07001432 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1433 // Ensure that all the applications that are in the system are
1434 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001435 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001436 verifyApplications();
1437 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001438 }
1439
Joe Onorato36115782010-06-17 13:28:48 -04001440 // Clear out this reference, otherwise we end up holding it until all of the
1441 // callback runnables are done.
1442 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443
Joe Onorato36115782010-06-17 13:28:48 -04001444 synchronized (mLock) {
1445 // If we are still the last one to be scheduled, remove ourselves.
1446 if (mLoaderTask == this) {
1447 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001450 }
Joe Onorato36115782010-06-17 13:28:48 -04001451 }
1452
1453 public void stopLocked() {
1454 synchronized (LoaderTask.this) {
1455 mStopped = true;
1456 this.notify();
1457 }
1458 }
1459
1460 /**
1461 * Gets the callbacks object. If we've been stopped, or if the launcher object
1462 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1463 * object that was around when the deferred message was scheduled, and if there's
1464 * a new Callbacks object around then also return null. This will save us from
1465 * calling onto it with data that will be ignored.
1466 */
1467 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1468 synchronized (mLock) {
1469 if (mStopped) {
1470 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001471 }
Joe Onorato36115782010-06-17 13:28:48 -04001472
1473 if (mCallbacks == null) {
1474 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001475 }
Joe Onorato36115782010-06-17 13:28:48 -04001476
1477 final Callbacks callbacks = mCallbacks.get();
1478 if (callbacks != oldCallbacks) {
1479 return null;
1480 }
1481 if (callbacks == null) {
1482 Log.w(TAG, "no mCallbacks");
1483 return null;
1484 }
1485
1486 return callbacks;
1487 }
1488 }
1489
Winson Chungc763c4e2013-07-19 13:49:06 -07001490 private void verifyApplications() {
1491 final Context context = mApp.getContext();
1492
1493 // Cross reference all the applications in our apps list with items in the workspace
1494 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001495 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001496 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001497 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001498 tmpInfos = getItemInfoForComponentName(app.componentName);
1499 if (tmpInfos.isEmpty()) {
1500 // We are missing an application icon, so add this to the workspace
1501 added.add(app);
1502 // This is a rare event, so lets log it
1503 Log.e(TAG, "Missing Application on load: " + app);
1504 }
1505 }
1506 }
1507 if (!added.isEmpty()) {
1508 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001509 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001510 }
1511 }
1512
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001514 LauncherAppState app = LauncherAppState.getInstance();
1515 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1516 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1517 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001518 }
1519
Joe Onorato36115782010-06-17 13:28:48 -04001520 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001521 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1522 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001523 LauncherAppState app = LauncherAppState.getInstance();
1524 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1525 int countX = (int) grid.numColumns;
1526 int countY = (int) grid.numRows;
1527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001529 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001530 // Return early if we detect that an item is under the hotseat button
1531 if (mCallbacks == null ||
1532 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1533 deleteOnItemOverlap.set(true);
1534 return false;
1535 }
1536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1538 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1539 [(int) item.screenId][0] != null) {
1540 Log.e(TAG, "Error loading shortcut into hotseat " + item
1541 + " into position (" + item.screenId + ":" + item.cellX + ","
1542 + item.cellY + ") occupied by "
1543 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1544 [(int) item.screenId][0]);
1545 return false;
1546 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001547 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001548 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 items[(int) item.screenId][0] = item;
1550 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001551 return true;
1552 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001553 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1554 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001555 return true;
1556 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001559 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 occupied.put(item.screenId, items);
1561 }
1562
1563 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001564 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001565 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1566 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001568 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001570 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001571 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001573 return false;
1574 }
1575 }
1576 }
1577 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1578 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001580 }
1581 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 return true;
1584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585
Winson Chungba9c37f2013-08-30 14:11:37 -07001586 /** Clears all the sBg data structures */
1587 private void clearSBgDataStructures() {
1588 synchronized (sBgLock) {
1589 sBgWorkspaceItems.clear();
1590 sBgAppWidgets.clear();
1591 sBgFolders.clear();
1592 sBgItemsIdMap.clear();
1593 sBgDbIconCache.clear();
1594 sBgWorkspaceScreens.clear();
1595 }
1596 }
1597
Winson Chungc763c4e2013-07-19 13:49:06 -07001598 /** Returns whether this is an upgradge path */
1599 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001600 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601
Joe Onorato36115782010-06-17 13:28:48 -04001602 final Context context = mContext;
1603 final ContentResolver contentResolver = context.getContentResolver();
1604 final PackageManager manager = context.getPackageManager();
1605 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1606 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001607
Winson Chung892c74d2013-08-22 16:15:50 -07001608 LauncherAppState app = LauncherAppState.getInstance();
1609 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1610 int countX = (int) grid.numColumns;
1611 int countY = (int) grid.numRows;
1612
Michael Jurkab85f8a42012-04-25 15:48:32 -07001613 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001614 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001615
Winson Chungc763c4e2013-07-19 13:49:06 -07001616 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001617 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001618
Winson Chung2abf94d2012-07-18 18:16:38 -07001619 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001620 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001621
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001622 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001623 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001624 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001625
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001626 // +1 for the hotseat (it can be larger than the workspace)
1627 // Load workspace in reverse order to ensure that latest items are loaded first (and
1628 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001631 try {
1632 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1633 final int intentIndex = c.getColumnIndexOrThrow
1634 (LauncherSettings.Favorites.INTENT);
1635 final int titleIndex = c.getColumnIndexOrThrow
1636 (LauncherSettings.Favorites.TITLE);
1637 final int iconTypeIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.ICON_TYPE);
1639 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1640 final int iconPackageIndex = c.getColumnIndexOrThrow(
1641 LauncherSettings.Favorites.ICON_PACKAGE);
1642 final int iconResourceIndex = c.getColumnIndexOrThrow(
1643 LauncherSettings.Favorites.ICON_RESOURCE);
1644 final int containerIndex = c.getColumnIndexOrThrow(
1645 LauncherSettings.Favorites.CONTAINER);
1646 final int itemTypeIndex = c.getColumnIndexOrThrow(
1647 LauncherSettings.Favorites.ITEM_TYPE);
1648 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1649 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001650 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001652 final int screenIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.SCREEN);
1654 final int cellXIndex = c.getColumnIndexOrThrow
1655 (LauncherSettings.Favorites.CELLX);
1656 final int cellYIndex = c.getColumnIndexOrThrow
1657 (LauncherSettings.Favorites.CELLY);
1658 final int spanXIndex = c.getColumnIndexOrThrow
1659 (LauncherSettings.Favorites.SPANX);
1660 final int spanYIndex = c.getColumnIndexOrThrow(
1661 LauncherSettings.Favorites.SPANY);
1662 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1663 //final int displayModeIndex = c.getColumnIndexOrThrow(
1664 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001666 ShortcutInfo info;
1667 String intentDescription;
1668 LauncherAppWidgetInfo appWidgetInfo;
1669 int container;
1670 long id;
1671 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001672
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001673 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001674 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 try {
1676 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 switch (itemType) {
1679 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1680 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001681 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001682 intentDescription = c.getString(intentIndex);
1683 try {
1684 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001685 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001686 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001687 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001688 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001689 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1690 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001691 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001692 // If apps can be on external storage, then we just
1693 // leave them for the user to remove (maybe add
1694 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001695 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001696 }
1697 continue;
1698 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001700 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 continue;
1702 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1705 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1706 titleIndex, mLabelCache);
1707 } else {
1708 info = getShortcutInfo(c, context, iconTypeIndex,
1709 iconPackageIndex, iconResourceIndex, iconIndex,
1710 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 // App shortcuts that used to be automatically added to Launcher
1713 // didn't always have the correct intent flags set, so do that
1714 // here
1715 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001716 intent.getCategories() != null &&
1717 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001718 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 intent.addFlags(
1720 Intent.FLAG_ACTIVITY_NEW_TASK |
1721 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1722 }
Michael Jurka96879562012-03-22 05:54:33 -07001723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001726 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 container = c.getInt(containerIndex);
1729 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001730 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 info.cellX = c.getInt(cellXIndex);
1732 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001733 info.spanX = 1;
1734 info.spanY = 1;
1735 // Skip loading items that are out of bounds
1736 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1737 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001738 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1739 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001740 continue;
1741 }
1742 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001744 deleteOnItemOverlap.set(false);
1745 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1746 if (deleteOnItemOverlap.get()) {
1747 itemsToRemove.add(id);
1748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 break;
1750 }
1751
1752 switch (container) {
1753 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1754 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1755 sBgWorkspaceItems.add(info);
1756 break;
1757 default:
1758 // Item is in a user folder
1759 FolderInfo folderInfo =
1760 findOrMakeFolder(sBgFolders, container);
1761 folderInfo.add(info);
1762 break;
1763 }
1764 sBgItemsIdMap.put(info.id, info);
1765
1766 // now that we've loaded everthing re-save it with the
1767 // icon in case it disappears somehow.
1768 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001769 } else {
1770 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 }
1772 break;
1773
1774 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1775 id = c.getLong(idIndex);
1776 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1777
1778 folderInfo.title = c.getString(titleIndex);
1779 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 folderInfo.cellX = c.getInt(cellXIndex);
1784 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001785 folderInfo.spanX = 1;
1786 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787
Winson Chung5f8afe62013-08-12 16:19:28 -07001788 // Skip loading items that are out of bounds
1789 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001790 if (checkItemDimensions(folderInfo)) {
1791 Log.d(TAG, "Skipped loading out of bounds folder");
1792 continue;
1793 }
1794 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001795 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001796 deleteOnItemOverlap.set(false);
1797 if (!checkItemPlacement(occupied, folderInfo,
1798 deleteOnItemOverlap)) {
1799 if (deleteOnItemOverlap.get()) {
1800 itemsToRemove.add(id);
1801 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001802 break;
1803 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001804
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1807 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1808 sBgWorkspaceItems.add(folderInfo);
1809 break;
Joe Onorato36115782010-06-17 13:28:48 -04001810 }
Joe Onorato17a89222011-02-08 17:26:11 -08001811
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1813 sBgFolders.put(folderInfo.id, folderInfo);
1814 break;
1815
1816 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1817 // Read all Launcher-specific widget details
1818 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001819 String savedProvider = c.getString(appWidgetProviderIndex);
1820
Joe Onorato36115782010-06-17 13:28:48 -04001821 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 final AppWidgetProviderInfo provider =
1824 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 if (!isSafeMode && (provider == null || provider.provider == null ||
1827 provider.provider.getPackageName() == null)) {
1828 String log = "Deleting widget that isn't installed anymore: id="
1829 + id + " appWidgetId=" + appWidgetId;
1830 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001831 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 itemsToRemove.add(id);
1833 } else {
1834 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1835 provider.provider);
1836 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001837 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 appWidgetInfo.cellX = c.getInt(cellXIndex);
1839 appWidgetInfo.cellY = c.getInt(cellYIndex);
1840 appWidgetInfo.spanX = c.getInt(spanXIndex);
1841 appWidgetInfo.spanY = c.getInt(spanYIndex);
1842 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1843 appWidgetInfo.minSpanX = minSpan[0];
1844 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 container = c.getInt(containerIndex);
1847 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1848 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1849 Log.e(TAG, "Widget found where container != " +
1850 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1851 continue;
1852 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853
Adam Cohene25af792013-06-06 23:08:25 -07001854 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001855 // Skip loading items that are out of bounds
1856 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1857 if (checkItemDimensions(appWidgetInfo)) {
1858 Log.d(TAG, "Skipped loading out of bounds app widget");
1859 continue;
1860 }
1861 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001863 deleteOnItemOverlap.set(false);
1864 if (!checkItemPlacement(occupied, appWidgetInfo,
1865 deleteOnItemOverlap)) {
1866 if (deleteOnItemOverlap.get()) {
1867 itemsToRemove.add(id);
1868 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 break;
1870 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001871 String providerName = provider.provider.flattenToString();
1872 if (!providerName.equals(savedProvider)) {
1873 ContentValues values = new ContentValues();
1874 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1875 providerName);
1876 String where = BaseColumns._ID + "= ?";
1877 String[] args = {Integer.toString(c.getInt(idIndex))};
1878 contentResolver.update(contentUri, values, where, args);
1879 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1881 sBgAppWidgets.add(appWidgetInfo);
1882 }
Joe Onorato36115782010-06-17 13:28:48 -04001883 break;
1884 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001886 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 }
1889 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001890 if (c != null) {
1891 c.close();
1892 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001893 }
1894
Winson Chungba9c37f2013-08-30 14:11:37 -07001895 // Break early if we've stopped loading
1896 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001897 clearSBgDataStructures();
1898 return false;
1899 }
1900
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 if (itemsToRemove.size() > 0) {
1902 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001903 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 // Remove dead items
1905 for (long id : itemsToRemove) {
1906 if (DEBUG_LOADERS) {
1907 Log.d(TAG, "Removed id = " + id);
1908 }
1909 // Don't notify content observers
1910 try {
1911 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1912 null, null);
1913 } catch (RemoteException e) {
1914 Log.w(TAG, "Could not remove id = " + id);
1915 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001916 }
1917 }
1918
Winson Chungc763c4e2013-07-19 13:49:06 -07001919 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 long maxScreenId = 0;
1921 // If we're importing we use the old screen order.
1922 for (ItemInfo item: sBgItemsIdMap.values()) {
1923 long screenId = item.screenId;
1924 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1925 !sBgWorkspaceScreens.contains(screenId)) {
1926 sBgWorkspaceScreens.add(screenId);
1927 if (screenId > maxScreenId) {
1928 maxScreenId = screenId;
1929 }
1930 }
1931 }
1932 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001933
Michael Jurka414300a2013-08-27 15:42:35 +02001934 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001936
1937 // Update the max item id after we load an old db
1938 long maxItemId = 0;
1939 // If we're importing we use the old screen order.
1940 for (ItemInfo item: sBgItemsIdMap.values()) {
1941 maxItemId = Math.max(maxItemId, item.id);
1942 }
Michael Jurka414300a2013-08-27 15:42:35 +02001943 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001945 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1946 for (Integer i : orderedScreens.keySet()) {
1947 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 }
1949
1950 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001951 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 for (ItemInfo item: sBgItemsIdMap.values()) {
1953 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1955 unusedScreens.contains(screenId)) {
1956 unusedScreens.remove(screenId);
1957 }
1958 }
1959
1960 // If there are any empty screens remove them, and update.
1961 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001962 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1964 }
1965 }
1966
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 if (DEBUG_LOADERS) {
1968 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1969 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001971 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001973
Daniel Sandler566da102013-06-25 23:43:45 -04001974 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001975 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001977 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 line += " | ";
1979 }
Winson Chung892c74d2013-08-22 16:15:50 -07001980 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 }
Joe Onorato36115782010-06-17 13:28:48 -04001983 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 }
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001988 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001989 }
1990
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 /** Filters the set of items who are directly or indirectly (via another container) on the
1992 * specified screen. */
1993 private void filterCurrentWorkspaceItems(int currentScreen,
1994 ArrayList<ItemInfo> allWorkspaceItems,
1995 ArrayList<ItemInfo> currentScreenItems,
1996 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001997 // Purge any null ItemInfos
1998 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1999 while (iter.hasNext()) {
2000 ItemInfo i = iter.next();
2001 if (i == null) {
2002 iter.remove();
2003 }
2004 }
2005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 // If we aren't filtering on a screen, then the set of items to load is the full set of
2007 // items given.
2008 if (currentScreen < 0) {
2009 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002010 }
2011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 // Order the set of items by their containers first, this allows use to walk through the
2013 // list sequentially, build up a list of containers that are in the specified screen,
2014 // as well as all items in those containers.
2015 Set<Long> itemsOnScreen = new HashSet<Long>();
2016 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2017 @Override
2018 public int compare(ItemInfo lhs, ItemInfo rhs) {
2019 return (int) (lhs.container - rhs.container);
2020 }
2021 });
2022 for (ItemInfo info : allWorkspaceItems) {
2023 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 currentScreenItems.add(info);
2026 itemsOnScreen.add(info.id);
2027 } else {
2028 otherScreenItems.add(info);
2029 }
2030 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2031 currentScreenItems.add(info);
2032 itemsOnScreen.add(info.id);
2033 } else {
2034 if (itemsOnScreen.contains(info.container)) {
2035 currentScreenItems.add(info);
2036 itemsOnScreen.add(info.id);
2037 } else {
2038 otherScreenItems.add(info);
2039 }
2040 }
2041 }
2042 }
2043
2044 /** Filters the set of widgets which are on the specified screen. */
2045 private void filterCurrentAppWidgets(int currentScreen,
2046 ArrayList<LauncherAppWidgetInfo> appWidgets,
2047 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2048 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2049 // If we aren't filtering on a screen, then the set of items to load is the full set of
2050 // widgets given.
2051 if (currentScreen < 0) {
2052 currentScreenWidgets.addAll(appWidgets);
2053 }
2054
2055 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002056 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002058 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 currentScreenWidgets.add(widget);
2060 } else {
2061 otherScreenWidgets.add(widget);
2062 }
2063 }
2064 }
2065
2066 /** Filters the set of folders which are on the specified screen. */
2067 private void filterCurrentFolders(int currentScreen,
2068 HashMap<Long, ItemInfo> itemsIdMap,
2069 HashMap<Long, FolderInfo> folders,
2070 HashMap<Long, FolderInfo> currentScreenFolders,
2071 HashMap<Long, FolderInfo> otherScreenFolders) {
2072 // If we aren't filtering on a screen, then the set of items to load is the full set of
2073 // widgets given.
2074 if (currentScreen < 0) {
2075 currentScreenFolders.putAll(folders);
2076 }
2077
2078 for (long id : folders.keySet()) {
2079 ItemInfo info = itemsIdMap.get(id);
2080 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002081 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002083 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 currentScreenFolders.put(id, folder);
2085 } else {
2086 otherScreenFolders.put(id, folder);
2087 }
2088 }
2089 }
2090
2091 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2092 * right) */
2093 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002094 final LauncherAppState app = LauncherAppState.getInstance();
2095 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 // XXX: review this
2097 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002098 @Override
2099 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002100 int cellCountX = (int) grid.numColumns;
2101 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002102 int screenOffset = cellCountX * cellCountY;
2103 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002104 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002105 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002107 rhs.cellY * cellCountX + rhs.cellX);
2108 return (int) (lr - rr);
2109 }
2110 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002112
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2114 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002115 final Runnable r = new Runnable() {
2116 @Override
2117 public void run() {
2118 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2119 if (callbacks != null) {
2120 callbacks.bindScreens(orderedScreens);
2121 }
2122 }
2123 };
2124 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2125 }
2126
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2128 final ArrayList<ItemInfo> workspaceItems,
2129 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2130 final HashMap<Long, FolderInfo> folders,
2131 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 final boolean postOnMainThread = (deferredBindRunnables != null);
2134
2135 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002136 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002138 final int start = i;
2139 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 final Runnable r = new Runnable() {
2141 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002143 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002145 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2146 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 }
2148 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 };
2150 if (postOnMainThread) {
2151 deferredBindRunnables.add(r);
2152 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002153 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 }
Joe Onorato36115782010-06-17 13:28:48 -04002155 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156
2157 // Bind the folders
2158 if (!folders.isEmpty()) {
2159 final Runnable r = new Runnable() {
2160 public void run() {
2161 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2162 if (callbacks != null) {
2163 callbacks.bindFolders(folders);
2164 }
2165 }
2166 };
2167 if (postOnMainThread) {
2168 deferredBindRunnables.add(r);
2169 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002170 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 }
2172 }
2173
2174 // Bind the widgets, one at a time
2175 N = appWidgets.size();
2176 for (int i = 0; i < N; i++) {
2177 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2178 final Runnable r = new Runnable() {
2179 public void run() {
2180 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2181 if (callbacks != null) {
2182 callbacks.bindAppWidget(widget);
2183 }
2184 }
2185 };
2186 if (postOnMainThread) {
2187 deferredBindRunnables.add(r);
2188 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002189 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 }
2191 }
2192 }
2193
2194 /**
2195 * Binds all loaded data to actual views on the main thread.
2196 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002197 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 final long t = SystemClock.uptimeMillis();
2199 Runnable r;
2200
2201 // Don't use these two variables in any of the callback runnables.
2202 // Otherwise we hold a reference to them.
2203 final Callbacks oldCallbacks = mCallbacks.get();
2204 if (oldCallbacks == null) {
2205 // This launcher has exited and nobody bothered to tell us. Just bail.
2206 Log.w(TAG, "LoaderTask running with no launcher");
2207 return;
2208 }
2209
Winson Chung4a2afa32012-07-19 14:53:05 -07002210 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2211 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 oldCallbacks.getCurrentWorkspaceScreen();
2213
2214 // Load all the items that are on the current page first (and in the process, unbind
2215 // all the existing workspace items before we call startBinding() below.
2216 unbindWorkspaceItemsOnMainThread();
2217 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2218 ArrayList<LauncherAppWidgetInfo> appWidgets =
2219 new ArrayList<LauncherAppWidgetInfo>();
2220 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2221 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002223 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 workspaceItems.addAll(sBgWorkspaceItems);
2225 appWidgets.addAll(sBgAppWidgets);
2226 folders.putAll(sBgFolders);
2227 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 }
2230
2231 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2232 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2233 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2234 new ArrayList<LauncherAppWidgetInfo>();
2235 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2238 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2239
2240 // Separate the items that are on the current screen, and all the other remaining items
2241 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2242 otherWorkspaceItems);
2243 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2244 otherAppWidgets);
2245 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2246 otherFolders);
2247 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2248 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2249
2250 // Tell the workspace that we're about to start binding items
2251 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002252 public void run() {
2253 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2254 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002256 }
2257 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 };
Winson Chung81b52252012-08-27 15:34:29 -07002259 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260
Adam Cohendcd297f2013-06-18 13:13:40 -07002261 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2262
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 // Load items on the current page
2264 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2265 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002266 if (isLoadingSynchronously) {
2267 r = new Runnable() {
2268 public void run() {
2269 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2270 if (callbacks != null) {
2271 callbacks.onPageBoundSynchronously(currentScreen);
2272 }
2273 }
2274 };
Winson Chung81b52252012-08-27 15:34:29 -07002275 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002276 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277
Winson Chung4a2afa32012-07-19 14:53:05 -07002278 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2279 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 mDeferredBindRunnables.clear();
2281 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002282 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283
2284 // Tell the workspace that we're done binding items
2285 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002286 public void run() {
2287 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2288 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002289 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002290 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291
Winson Chung98e030b2012-05-07 16:01:11 -07002292 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002293 if (DEBUG_LOADERS) {
2294 Log.d(TAG, "bound workspace in "
2295 + (SystemClock.uptimeMillis()-t) + "ms");
2296 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002297
2298 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002299 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002301 if (isLoadingSynchronously) {
2302 mDeferredBindRunnables.add(r);
2303 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002304 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002305 }
Joe Onorato36115782010-06-17 13:28:48 -04002306 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002307
Joe Onorato36115782010-06-17 13:28:48 -04002308 private void loadAndBindAllApps() {
2309 if (DEBUG_LOADERS) {
2310 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2311 }
2312 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002313 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002314 synchronized (LoaderTask.this) {
2315 if (mStopped) {
2316 return;
2317 }
2318 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002319 }
Joe Onorato36115782010-06-17 13:28:48 -04002320 } else {
2321 onlyBindAllApps();
2322 }
2323 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002324
Joe Onorato36115782010-06-17 13:28:48 -04002325 private void onlyBindAllApps() {
2326 final Callbacks oldCallbacks = mCallbacks.get();
2327 if (oldCallbacks == null) {
2328 // This launcher has exited and nobody bothered to tell us. Just bail.
2329 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2330 return;
2331 }
2332
2333 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002334 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002335 final ArrayList<AppInfo> list
2336 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002337 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002338 public void run() {
2339 final long t = SystemClock.uptimeMillis();
2340 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2341 if (callbacks != null) {
2342 callbacks.bindAllApplications(list);
2343 }
2344 if (DEBUG_LOADERS) {
2345 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2346 + (SystemClock.uptimeMillis()-t) + "ms");
2347 }
2348 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002349 };
2350 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002351 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002352 r.run();
2353 } else {
2354 mHandler.post(r);
2355 }
Joe Onorato36115782010-06-17 13:28:48 -04002356 }
2357
Winson Chung64359a52013-07-08 17:17:08 -07002358 private void loadAllApps() {
2359 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002360
Joe Onorato36115782010-06-17 13:28:48 -04002361 final Callbacks oldCallbacks = mCallbacks.get();
2362 if (oldCallbacks == null) {
2363 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002364 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002365 return;
2366 }
2367
Winson Chung64359a52013-07-08 17:17:08 -07002368 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002369 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2370 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2371
Winson Chung64359a52013-07-08 17:17:08 -07002372 // Clear the list of apps
2373 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002374
Winson Chung64359a52013-07-08 17:17:08 -07002375 // Query for the set of apps
2376 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2377 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2378 if (DEBUG_LOADERS) {
2379 Log.d(TAG, "queryIntentActivities took "
2380 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2381 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2382 }
2383 // Fail if we don't have any apps
2384 if (apps == null || apps.isEmpty()) {
2385 return;
2386 }
2387 // Sort the applications by name
2388 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2389 Collections.sort(apps,
2390 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2391 if (DEBUG_LOADERS) {
2392 Log.d(TAG, "sort took "
2393 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 }
2395
Winson Chung64359a52013-07-08 17:17:08 -07002396 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002397 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002398 ResolveInfo app = apps.get(i);
2399 if (oldCallbacks.shouldShowApp(app)) {
2400 // This builds the icon bitmaps.
2401 mBgAllAppsList.add(new AppInfo(packageManager, app,
2402 mIconCache, mLabelCache));
2403 }
Winson Chung64359a52013-07-08 17:17:08 -07002404 }
2405
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002406 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002407 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2408 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002409
2410 // Post callback on main thread
2411 mHandler.post(new Runnable() {
2412 public void run() {
2413 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002414 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002415 if (callbacks != null) {
2416 callbacks.bindAllApplications(added);
2417 if (DEBUG_LOADERS) {
2418 Log.d(TAG, "bound " + added.size() + " apps in "
2419 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2420 }
2421 } else {
2422 Log.i(TAG, "not binding apps: no Launcher activity");
2423 }
2424 }
2425 });
2426
Joe Onorato36115782010-06-17 13:28:48 -04002427 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002428 Log.d(TAG, "Icons processed in "
2429 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002430 }
2431 }
2432
2433 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002434 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2436 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2437 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2438 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2439 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2440 }
Joe Onorato36115782010-06-17 13:28:48 -04002441 }
2442 }
2443
2444 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002445 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002446 }
2447
2448 private class PackageUpdatedTask implements Runnable {
2449 int mOp;
2450 String[] mPackages;
2451
2452 public static final int OP_NONE = 0;
2453 public static final int OP_ADD = 1;
2454 public static final int OP_UPDATE = 2;
2455 public static final int OP_REMOVE = 3; // uninstlled
2456 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2457
2458
2459 public PackageUpdatedTask(int op, String[] packages) {
2460 mOp = op;
2461 mPackages = packages;
2462 }
2463
2464 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002465 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002466
2467 final String[] packages = mPackages;
2468 final int N = packages.length;
2469 switch (mOp) {
2470 case OP_ADD:
2471 for (int i=0; i<N; i++) {
2472 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002473 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002474 }
2475 break;
2476 case OP_UPDATE:
2477 for (int i=0; i<N; i++) {
2478 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002479 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002480 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002481 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002482 }
2483 break;
2484 case OP_REMOVE:
2485 case OP_UNAVAILABLE:
2486 for (int i=0; i<N; i++) {
2487 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002488 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002489 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002490 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002491 }
2492 break;
2493 }
2494
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002495 ArrayList<AppInfo> added = null;
2496 ArrayList<AppInfo> modified = null;
2497 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002498
Adam Cohen487f7dd2012-06-28 18:12:10 -07002499 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002500 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002501 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002502 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002504 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002505 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
Winson Chung5d55f332012-07-16 20:45:03 -07002507 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002508 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002509 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002510 }
2511
Joe Onorato36115782010-06-17 13:28:48 -04002512 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2513 if (callbacks == null) {
2514 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2515 return;
2516 }
2517
2518 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002519 // Ensure that we add all the workspace applications to the db
2520 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002521 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2522 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2523 } else {
2524 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2525 addAndBindAddedApps(context, addedInfos, cb, added);
2526 }
Joe Onorato36115782010-06-17 13:28:48 -04002527 }
2528 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002529 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002530
2531 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002532 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002533 ArrayList<ItemInfo> infos =
2534 getItemInfoForComponentName(a.componentName);
2535 for (ItemInfo i : infos) {
2536 if (isShortcutInfoUpdateable(i)) {
2537 ShortcutInfo info = (ShortcutInfo) i;
2538 info.title = a.title.toString();
2539 updateItemInDatabase(context, info);
2540 }
2541 }
2542 }
2543
Joe Onorato36115782010-06-17 13:28:48 -04002544 mHandler.post(new Runnable() {
2545 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002546 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2547 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002548 callbacks.bindAppsUpdated(modifiedFinal);
2549 }
2550 }
2551 });
2552 }
Winson Chung83892cc2013-05-01 16:53:33 -07002553 // If a package has been removed, or an app has been removed as a result of
2554 // an update (for example), make the removed callback.
2555 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002556 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002557 final ArrayList<String> removedPackageNames =
2558 new ArrayList<String>(Arrays.asList(packages));
2559
Winson Chung64359a52013-07-08 17:17:08 -07002560 // Update the launcher db to reflect the removal of apps
2561 if (packageRemoved) {
2562 for (String pn : removedPackageNames) {
2563 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2564 for (ItemInfo i : infos) {
2565 deleteItemFromDatabase(context, i);
2566 }
2567 }
2568 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002569 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002570 ArrayList<ItemInfo> infos =
2571 getItemInfoForComponentName(a.componentName);
2572 for (ItemInfo i : infos) {
2573 deleteItemFromDatabase(context, i);
2574 }
2575 }
2576 }
2577
Joe Onorato36115782010-06-17 13:28:48 -04002578 mHandler.post(new Runnable() {
2579 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002580 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2581 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002582 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002583 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002584 }
2585 }
2586 });
Joe Onoratobe386092009-11-17 17:32:16 -08002587 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002588
Michael Jurkac402cd92013-05-20 15:49:32 +02002589 final ArrayList<Object> widgetsAndShortcuts =
2590 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002591 mHandler.post(new Runnable() {
2592 @Override
2593 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002594 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2595 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002596 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002597 }
2598 }
2599 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002600
2601 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002602 mHandler.post(new Runnable() {
2603 public void run() {
2604 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2605 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002606 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002607 }
2608 }
2609 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002610 }
2611 }
2612
Michael Jurkac402cd92013-05-20 15:49:32 +02002613 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2614 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2615 PackageManager packageManager = context.getPackageManager();
2616 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2617 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2618 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2619 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2620 Collections.sort(widgetsAndShortcuts,
2621 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2622 return widgetsAndShortcuts;
2623 }
2624
Winson Chung1323b482013-08-05 12:41:55 -07002625 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002626 if (cn == null) {
2627 return false;
2628 }
2629
2630 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002631 // Skip if the application is disabled
2632 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2633 if (!pi.applicationInfo.enabled) {
2634 return false;
2635 }
2636
2637 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002638 return (pm.getActivityInfo(cn, 0) != null);
2639 } catch (NameNotFoundException e) {
2640 return false;
2641 }
2642 }
2643
Joe Onorato9c1289c2009-08-17 11:03:03 -04002644 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002645 * This is called from the code that adds shortcuts from the intent receiver. This
2646 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002647 */
Joe Onorato56d82912010-03-07 14:32:10 -05002648 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002649 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002650 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651
Joe Onorato56d82912010-03-07 14:32:10 -05002652 /**
2653 * Make an ShortcutInfo object for a shortcut that is an application.
2654 *
2655 * If c is not null, then it will be used to fill in missing data like the title and icon.
2656 */
2657 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002658 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002659 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002660 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002661 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002662 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002663 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002664 } else {
2665 try {
2666 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2667 info.initFlagsAndFirstInstallTime(pi);
2668 } catch (NameNotFoundException e) {
2669 Log.d(TAG, "getPackInfo failed for package " +
2670 componentName.getPackageName());
2671 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002672 }
2673
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002674 // TODO: See if the PackageManager knows about this case. If it doesn't
2675 // then return null & delete this.
2676
Joe Onorato56d82912010-03-07 14:32:10 -05002677 // the resource -- This may implicitly give us back the fallback icon,
2678 // but don't worry about that. All we're doing with usingFallbackIcon is
2679 // to avoid saving lots of copies of that in the database, and most apps
2680 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002681
2682 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2683 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2684 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002685 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002686 ResolveInfo resolveInfo = null;
2687 ComponentName oldComponent = intent.getComponent();
2688 Intent newIntent = new Intent(intent.getAction(), null);
2689 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2690 newIntent.setPackage(oldComponent.getPackageName());
2691 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2692 for (ResolveInfo i : infos) {
2693 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2694 i.activityInfo.name);
2695 if (cn.equals(oldComponent)) {
2696 resolveInfo = i;
2697 }
2698 }
2699 if (resolveInfo == null) {
2700 resolveInfo = manager.resolveActivity(intent, 0);
2701 }
Joe Onorato56d82912010-03-07 14:32:10 -05002702 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002703 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002704 }
Joe Onorato56d82912010-03-07 14:32:10 -05002705 // the db
2706 if (icon == null) {
2707 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002708 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002709 }
2710 }
2711 // the fallback icon
2712 if (icon == null) {
2713 icon = getFallbackIcon();
2714 info.usingFallbackIcon = true;
2715 }
2716 info.setIcon(icon);
2717
2718 // from the resource
2719 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002720 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2721 if (labelCache != null && labelCache.containsKey(key)) {
2722 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002723 } else {
2724 info.title = resolveInfo.activityInfo.loadLabel(manager);
2725 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002726 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002727 }
2728 }
Joe Onorato56d82912010-03-07 14:32:10 -05002729 }
2730 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002731 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002732 if (c != null) {
2733 info.title = c.getString(titleIndex);
2734 }
2735 }
2736 // fall back to the class name of the activity
2737 if (info.title == null) {
2738 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2741 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002743
Winson Chung64359a52013-07-08 17:17:08 -07002744 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2745 ItemInfoFilter f) {
2746 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2747 for (ItemInfo i : infos) {
2748 if (i instanceof ShortcutInfo) {
2749 ShortcutInfo info = (ShortcutInfo) i;
2750 ComponentName cn = info.intent.getComponent();
2751 if (cn != null && f.filterItem(null, info, cn)) {
2752 filtered.add(info);
2753 }
2754 } else if (i instanceof FolderInfo) {
2755 FolderInfo info = (FolderInfo) i;
2756 for (ShortcutInfo s : info.contents) {
2757 ComponentName cn = s.intent.getComponent();
2758 if (cn != null && f.filterItem(info, s, cn)) {
2759 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002760 }
2761 }
Winson Chung64359a52013-07-08 17:17:08 -07002762 } else if (i instanceof LauncherAppWidgetInfo) {
2763 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2764 ComponentName cn = info.providerName;
2765 if (cn != null && f.filterItem(null, info, cn)) {
2766 filtered.add(info);
2767 }
Winson Chung8a435102012-08-30 17:16:53 -07002768 }
2769 }
Winson Chung64359a52013-07-08 17:17:08 -07002770 return new ArrayList<ItemInfo>(filtered);
2771 }
2772
2773 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002774 ItemInfoFilter filter = new ItemInfoFilter() {
2775 @Override
2776 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2777 return cn.getPackageName().equals(pn);
2778 }
2779 };
2780 return filterItemInfos(sBgItemsIdMap.values(), filter);
2781 }
2782
2783 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002784 ItemInfoFilter filter = new ItemInfoFilter() {
2785 @Override
2786 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2787 return cn.equals(cname);
2788 }
2789 };
2790 return filterItemInfos(sBgItemsIdMap.values(), filter);
2791 }
2792
2793 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2794 if (i instanceof ShortcutInfo) {
2795 ShortcutInfo info = (ShortcutInfo) i;
2796 // We need to check for ACTION_MAIN otherwise getComponent() might
2797 // return null for some shortcuts (for instance, for shortcuts to
2798 // web pages.)
2799 Intent intent = info.intent;
2800 ComponentName name = intent.getComponent();
2801 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2802 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2803 return true;
2804 }
2805 }
2806 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002807 }
2808
2809 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002810 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002812 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002813 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2814 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815
Joe Onorato56d82912010-03-07 14:32:10 -05002816 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002817 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002819
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002820 // TODO: If there's an explicit component and we can't install that, delete it.
2821
Joe Onorato56d82912010-03-07 14:32:10 -05002822 info.title = c.getString(titleIndex);
2823
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 int iconType = c.getInt(iconTypeIndex);
2825 switch (iconType) {
2826 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2827 String packageName = c.getString(iconPackageIndex);
2828 String resourceName = c.getString(iconResourceIndex);
2829 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002830 info.customIcon = false;
2831 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002834 if (resources != null) {
2835 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002836 icon = Utilities.createIconBitmap(
2837 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002840 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 }
Joe Onorato56d82912010-03-07 14:32:10 -05002842 // the db
2843 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002844 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002845 }
2846 // the fallback icon
2847 if (icon == null) {
2848 icon = getFallbackIcon();
2849 info.usingFallbackIcon = true;
2850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 break;
2852 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002853 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002854 if (icon == null) {
2855 icon = getFallbackIcon();
2856 info.customIcon = false;
2857 info.usingFallbackIcon = true;
2858 } else {
2859 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 break;
2862 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002863 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002864 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 info.customIcon = false;
2866 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002868 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 return info;
2870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871
Michael Jurka931dc972011-08-05 15:08:15 -07002872 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002873 @SuppressWarnings("all") // suppress dead code warning
2874 final boolean debug = false;
2875 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002876 Log.d(TAG, "getIconFromCursor app="
2877 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2878 }
2879 byte[] data = c.getBlob(iconIndex);
2880 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002881 return Utilities.createIconBitmap(
2882 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002883 } catch (Exception e) {
2884 return null;
2885 }
2886 }
2887
Winson Chung3d503fb2011-07-13 17:25:49 -07002888 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2889 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002890 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002891 if (info == null) {
2892 return null;
2893 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002894 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002895
2896 return info;
2897 }
2898
Winson Chunga9abd0e2010-10-27 17:18:37 -07002899 /**
Winson Chung55cef262010-10-28 14:14:18 -07002900 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2901 */
2902 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2903 ComponentName component) {
2904 List<AppWidgetProviderInfo> widgets =
2905 AppWidgetManager.getInstance(context).getInstalledProviders();
2906 for (AppWidgetProviderInfo info : widgets) {
2907 if (info.provider.equals(component)) {
2908 return info;
2909 }
2910 }
2911 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002912 }
2913
Winson Chung68846fd2010-10-29 11:00:27 -07002914 /**
2915 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2916 */
2917 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2918 final PackageManager packageManager = context.getPackageManager();
2919 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2920 new ArrayList<WidgetMimeTypeHandlerData>();
2921
2922 final Intent supportsIntent =
2923 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2924 supportsIntent.setType(mimeType);
2925
2926 // Create a set of widget configuration components that we can test against
2927 final List<AppWidgetProviderInfo> widgets =
2928 AppWidgetManager.getInstance(context).getInstalledProviders();
2929 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2930 new HashMap<ComponentName, AppWidgetProviderInfo>();
2931 for (AppWidgetProviderInfo info : widgets) {
2932 configurationComponentToWidget.put(info.configure, info);
2933 }
2934
2935 // Run through each of the intents that can handle this type of clip data, and cross
2936 // reference them with the components that are actual configuration components
2937 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2938 PackageManager.MATCH_DEFAULT_ONLY);
2939 for (ResolveInfo info : activities) {
2940 final ActivityInfo activityInfo = info.activityInfo;
2941 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2942 activityInfo.name);
2943 if (configurationComponentToWidget.containsKey(infoComponent)) {
2944 supportedConfigurationActivities.add(
2945 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2946 configurationComponentToWidget.get(infoComponent)));
2947 }
2948 }
2949 return supportedConfigurationActivities;
2950 }
2951
Winson Chunga9abd0e2010-10-27 17:18:37 -07002952 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002953 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2954 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2955 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2956
Adam Cohend9198822011-11-22 16:42:47 -08002957 if (intent == null) {
2958 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2959 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2960 return null;
2961 }
2962
Joe Onorato0589f0f2010-02-08 13:44:00 -08002963 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002964 boolean customIcon = false;
2965 ShortcutIconResource iconResource = null;
2966
2967 if (bitmap != null && bitmap instanceof Bitmap) {
2968 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002969 customIcon = true;
2970 } else {
2971 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2972 if (extra != null && extra instanceof ShortcutIconResource) {
2973 try {
2974 iconResource = (ShortcutIconResource) extra;
2975 final PackageManager packageManager = context.getPackageManager();
2976 Resources resources = packageManager.getResourcesForApplication(
2977 iconResource.packageName);
2978 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002979 icon = Utilities.createIconBitmap(
2980 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002981 } catch (Exception e) {
2982 Log.w(TAG, "Could not load shortcut icon: " + extra);
2983 }
2984 }
2985 }
2986
Michael Jurkac9d95c52011-08-29 14:03:34 -07002987 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002988
2989 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002990 if (fallbackIcon != null) {
2991 icon = fallbackIcon;
2992 } else {
2993 icon = getFallbackIcon();
2994 info.usingFallbackIcon = true;
2995 }
Joe Onorato56d82912010-03-07 14:32:10 -05002996 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002997 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002998
Joe Onorato0589f0f2010-02-08 13:44:00 -08002999 info.title = name;
3000 info.intent = intent;
3001 info.customIcon = customIcon;
3002 info.iconResource = iconResource;
3003
3004 return info;
3005 }
3006
Winson Chungaac01e12011-08-17 10:37:13 -07003007 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3008 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003009 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003010 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003011 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003012 }
Joe Onorato56d82912010-03-07 14:32:10 -05003013 // If this icon doesn't have a custom icon, check to see
3014 // what's stored in the DB, and if it doesn't match what
3015 // we're going to show, store what we are going to show back
3016 // into the DB. We do this so when we're loading, if the
3017 // package manager can't find an icon (for example because
3018 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003019 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003020 cache.put(info, c.getBlob(iconIndex));
3021 return true;
3022 }
3023 return false;
3024 }
3025 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3026 boolean needSave = false;
3027 try {
3028 if (data != null) {
3029 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3030 Bitmap loaded = info.getIcon(mIconCache);
3031 needSave = !saved.sameAs(loaded);
3032 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003033 needSave = true;
3034 }
Winson Chungaac01e12011-08-17 10:37:13 -07003035 } catch (Exception e) {
3036 needSave = true;
3037 }
3038 if (needSave) {
3039 Log.d(TAG, "going to save icon bitmap for info=" + info);
3040 // This is slower than is ideal, but this only happens once
3041 // or when the app is updated with a new icon.
3042 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003043 }
3044 }
3045
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003047 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 * or make a new one.
3049 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003050 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 // See if a placeholder was created for us already
3052 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003053 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003054 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003055 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 folders.put(id, folderInfo);
3057 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003058 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 }
3060
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003061 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003062 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003063 return new Comparator<AppInfo>() {
3064 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003065 int result = collator.compare(a.title.toString(), b.title.toString());
3066 if (result == 0) {
3067 result = a.componentName.compareTo(b.componentName);
3068 }
3069 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003070 }
Winson Chung11904872012-09-17 16:58:46 -07003071 };
3072 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003073 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3074 = new Comparator<AppInfo>() {
3075 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003076 if (a.firstInstallTime < b.firstInstallTime) return 1;
3077 if (a.firstInstallTime > b.firstInstallTime) return -1;
3078 return 0;
3079 }
3080 };
Winson Chung11904872012-09-17 16:58:46 -07003081 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3082 final Collator collator = Collator.getInstance();
3083 return new Comparator<AppWidgetProviderInfo>() {
3084 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3085 return collator.compare(a.label.toString(), b.label.toString());
3086 }
3087 };
3088 }
Winson Chung5308f242011-08-18 12:12:41 -07003089 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3090 if (info.activityInfo != null) {
3091 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3092 } else {
3093 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3094 }
3095 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003096 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003097 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003098 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003099 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003100 ShortcutNameComparator(PackageManager pm) {
3101 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003102 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003103 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003104 }
3105 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3106 mPackageManager = pm;
3107 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003108 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003109 }
3110 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003111 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003112 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3113 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3114 if (mLabelCache.containsKey(keyA)) {
3115 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003116 } else {
3117 labelA = a.loadLabel(mPackageManager).toString();
3118
Winson Chung5308f242011-08-18 12:12:41 -07003119 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003120 }
Winson Chung5308f242011-08-18 12:12:41 -07003121 if (mLabelCache.containsKey(keyB)) {
3122 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003123 } else {
3124 labelB = b.loadLabel(mPackageManager).toString();
3125
Winson Chung5308f242011-08-18 12:12:41 -07003126 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003127 }
Winson Chung11904872012-09-17 16:58:46 -07003128 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003129 }
3130 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003131 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003132 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003133 private PackageManager mPackageManager;
3134 private HashMap<Object, String> mLabelCache;
3135 WidgetAndShortcutNameComparator(PackageManager pm) {
3136 mPackageManager = pm;
3137 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003138 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003139 }
3140 public final int compare(Object a, Object b) {
3141 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003142 if (mLabelCache.containsKey(a)) {
3143 labelA = mLabelCache.get(a);
3144 } else {
3145 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003146 ((AppWidgetProviderInfo) a).label :
3147 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003148 mLabelCache.put(a, labelA);
3149 }
3150 if (mLabelCache.containsKey(b)) {
3151 labelB = mLabelCache.get(b);
3152 } else {
3153 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003154 ((AppWidgetProviderInfo) b).label :
3155 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003156 mLabelCache.put(b, labelB);
3157 }
Winson Chung11904872012-09-17 16:58:46 -07003158 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003159 }
3160 };
Joe Onoratobe386092009-11-17 17:32:16 -08003161
3162 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003163 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003164 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3165 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3166 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3167 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003168 if (mLoaderTask != null) {
3169 mLoaderTask.dumpState();
3170 } else {
3171 Log.d(TAG, "mLoaderTask=null");
3172 }
Joe Onoratobe386092009-11-17 17:32:16 -08003173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174}