blob: cc89d01ac7782116b237413ae160122b7f331e36 [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) {
Winson Chung0f84a602013-09-30 14:30:58 -0700697 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800698 item.cellX = cellX;
699 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 item.spanX = spanX;
701 item.spanY = spanY;
702
703 // We store hotseat items in canonical form which is this orientation invariant position
704 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700706 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 }
Adam Cohend4844c32011-02-18 19:25:06 -0800711
Adam Cohend4844c32011-02-18 19:25:06 -0800712 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800713 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
715 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
716 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
717 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800719
Michael Jurka816474f2012-06-25 14:49:02 -0700720 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700721 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700722
723 /**
724 * Update an item to the database in a specified container.
725 */
726 static void updateItemInDatabase(Context context, final ItemInfo item) {
727 final ContentValues values = new ContentValues();
728 item.onAddToDatabase(values);
729 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
730 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800731 }
732
733 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 * Returns true if the shortcuts already exists in the database.
735 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 static boolean shortcutExists(Context context, String title, Intent intent) {
738 final ContentResolver cr = context.getContentResolver();
739 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
740 new String[] { "title", "intent" }, "title=? and intent=?",
741 new String[] { title, intent.toUri(0) }, null);
742 boolean result = false;
743 try {
744 result = c.moveToFirst();
745 } finally {
746 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700749 }
750
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700752 * Returns an ItemInfo array containing all the items in the LauncherModel.
753 * The ItemInfo.id is not set through this function.
754 */
755 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
756 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
757 final ContentResolver cr = context.getContentResolver();
758 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
759 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
760 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
761 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
762
763 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
764 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
765 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
766 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
767 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
768 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
769 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
770
771 try {
772 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700773 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700774 item.cellX = c.getInt(cellXIndex);
775 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700776 item.spanX = Math.max(1, c.getInt(spanXIndex));
777 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700778 item.container = c.getInt(containerIndex);
779 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700780 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700781
782 items.add(item);
783 }
784 } catch (Exception e) {
785 items.clear();
786 } finally {
787 c.close();
788 }
789
790 return items;
791 }
792
793 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
795 */
796 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
797 final ContentResolver cr = context.getContentResolver();
798 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
799 "_id=? and (itemType=? or itemType=?)",
800 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700801 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700802
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 try {
804 if (c.moveToFirst()) {
805 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
806 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
807 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
808 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
809 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
810 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 FolderInfo folderInfo = null;
813 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700814 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
815 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700817 }
818
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 folderInfo.title = c.getString(titleIndex);
820 folderInfo.id = id;
821 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700823 folderInfo.cellX = c.getInt(cellXIndex);
824 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825
826 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700827 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828 } finally {
829 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700830 }
831
832 return null;
833 }
834
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835 /**
836 * Add an item to the database in a specified container. Sets the container, screen, cellX and
837 * cellY fields of the item. Also assigns an ID to the item.
838 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 item.cellX = cellX;
843 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700844 // We store hotseat items in canonical form which is this orientation invariant position
845 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852
853 final ContentValues values = new ContentValues();
854 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 item.onAddToDatabase(values);
856
Michael Jurka414300a2013-08-27 15:42:35 +0200857 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700858 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700860
Michael Jurkac9d95c52011-08-29 14:03:34 -0700861 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700862 public void run() {
863 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
864 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700866 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700867 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700868 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 sBgItemsIdMap.put(item.id, item);
870 switch (item.itemType) {
871 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
872 sBgFolders.put(item.id, (FolderInfo) item);
873 // Fall through
874 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
875 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
876 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
877 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
878 sBgWorkspaceItems.add(item);
879 } else {
880 if (!sBgFolders.containsKey(item.container)) {
881 // Adding an item to a folder that doesn't exist.
882 String msg = "adding item: " + item + " to a folder that " +
883 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700884 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 break;
888 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
889 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
890 break;
891 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700892 }
893 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700894 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700895 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700896 }
897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700899 * Creates a new unique child id, for a given cell span across all layouts.
900 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700901 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700902 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700903 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700905 }
906
Winson Chungaafa03c2010-06-11 17:34:16 -0700907 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700908 * Removes the specified item from the database
909 * @param context
910 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400911 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700915
Michael Jurka83df1882011-08-31 20:59:26 -0700916 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700917 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700918 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700919
920 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700921 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700922 switch (item.itemType) {
923 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
924 sBgFolders.remove(item.id);
925 for (ItemInfo info: sBgItemsIdMap.values()) {
926 if (info.container == item.id) {
927 // We are deleting a folder which still contains items that
928 // think they are contained by that folder.
929 String msg = "deleting a folder (" + item + ") which still " +
930 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700931 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700932 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700933 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 sBgWorkspaceItems.remove(item);
935 break;
936 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
937 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
938 sBgWorkspaceItems.remove(item);
939 break;
940 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
941 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
942 break;
943 }
944 sBgItemsIdMap.remove(item.id);
945 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700946 }
947 }
Michael Jurka83df1882011-08-31 20:59:26 -0700948 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700949 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400950 }
951
952 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 * Update the order of the workspace screens in the database. The array list contains
954 * a list of screen ids in the order that they should appear.
955 */
Winson Chungc9168342013-06-26 14:54:55 -0700956 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700957 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 final ContentResolver cr = context.getContentResolver();
959 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
960
961 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700962 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 while (iter.hasNext()) {
964 long id = iter.next();
965 if (id < 0) {
966 iter.remove();
967 }
968 }
969
970 Runnable r = new Runnable() {
971 @Override
972 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 // Clear the table
974 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700975 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 ContentValues[] values = new ContentValues[count];
977 for (int i = 0; i < count; i++) {
978 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700979 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700980 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
981 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 values[i] = v;
983 }
984 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700985
Winson Chungba9c37f2013-08-30 14:11:37 -0700986 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700987 sBgWorkspaceScreens.clear();
988 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700989 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 }
991 };
992 runOnWorkerThread(r);
993 }
994
995 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 * Remove the contents of the specified folder from the database
997 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700998 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 final ContentResolver cr = context.getContentResolver();
1000
Michael Jurkac9d95c52011-08-29 14:03:34 -07001001 Runnable r = new Runnable() {
1002 public void run() {
1003 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001004 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001005 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001006 sBgItemsIdMap.remove(info.id);
1007 sBgFolders.remove(info.id);
1008 sBgDbIconCache.remove(info);
1009 sBgWorkspaceItems.remove(info);
1010 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001011
Michael Jurkac9d95c52011-08-29 14:03:34 -07001012 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1013 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001015 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 for (ItemInfo childInfo : info.contents) {
1017 sBgItemsIdMap.remove(childInfo.id);
1018 sBgDbIconCache.remove(childInfo);
1019 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001020 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001021 }
1022 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 }
1025
1026 /**
1027 * Set this as the current Launcher activity object for the loader.
1028 */
1029 public void initialize(Callbacks callbacks) {
1030 synchronized (mLock) {
1031 mCallbacks = new WeakReference<Callbacks>(callbacks);
1032 }
1033 }
1034
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001035 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1037 * ACTION_PACKAGE_CHANGED.
1038 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001039 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001040 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001041 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001042
Joe Onorato36115782010-06-17 13:28:48 -04001043 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001044
Joe Onorato36115782010-06-17 13:28:48 -04001045 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1046 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1047 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1048 final String packageName = intent.getData().getSchemeSpecificPart();
1049 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050
Joe Onorato36115782010-06-17 13:28:48 -04001051 int op = PackageUpdatedTask.OP_NONE;
1052
1053 if (packageName == null || packageName.length() == 0) {
1054 // they sent us a bad intent
1055 return;
1056 }
1057
1058 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1059 op = PackageUpdatedTask.OP_UPDATE;
1060 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1061 if (!replacing) {
1062 op = PackageUpdatedTask.OP_REMOVE;
1063 }
1064 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1065 // later, we will update the package at this time
1066 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1067 if (!replacing) {
1068 op = PackageUpdatedTask.OP_ADD;
1069 } else {
1070 op = PackageUpdatedTask.OP_UPDATE;
1071 }
1072 }
1073
1074 if (op != PackageUpdatedTask.OP_NONE) {
1075 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1076 }
1077
1078 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001079 // First, schedule to add these apps back in.
1080 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1081 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1082 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001083 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001084 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1085 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1086 enqueuePackageUpdated(new PackageUpdatedTask(
1087 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001088 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001089 // If we have changed locale we need to clear out the labels in all apps/workspace.
1090 forceReload();
1091 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1092 // Check if configuration change was an mcc/mnc change which would affect app resources
1093 // and we would need to clear out the labels in all apps/workspace. Same handling as
1094 // above for ACTION_LOCALE_CHANGED
1095 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001096 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001097 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001098 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001099 forceReload();
1100 }
1101 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001102 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001103 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1104 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001105 if (mCallbacks != null) {
1106 Callbacks callbacks = mCallbacks.get();
1107 if (callbacks != null) {
1108 callbacks.bindSearchablesChanged();
1109 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001110 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001111 }
1112 }
1113
Reena Lee93f824a2011-09-23 17:20:28 -07001114 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001115 resetLoadedState(true, true);
1116
Reena Lee93f824a2011-09-23 17:20:28 -07001117 // Do this here because if the launcher activity is running it will be restarted.
1118 // If it's not running startLoaderFromBackground will merely tell it that it needs
1119 // to reload.
1120 startLoaderFromBackground();
1121 }
1122
Winson Chungf0c6ae02012-03-21 16:10:31 -07001123 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1124 synchronized (mLock) {
1125 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1126 // mWorkspaceLoaded to true later
1127 stopLoaderLocked();
1128 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1129 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1130 }
1131 }
1132
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001133 /**
1134 * When the launcher is in the background, it's possible for it to miss paired
1135 * configuration changes. So whenever we trigger the loader from the background
1136 * tell the launcher that it needs to re-run the loader when it comes back instead
1137 * of doing it now.
1138 */
1139 public void startLoaderFromBackground() {
1140 boolean runLoader = false;
1141 if (mCallbacks != null) {
1142 Callbacks callbacks = mCallbacks.get();
1143 if (callbacks != null) {
1144 // Only actually run the loader if they're not paused.
1145 if (!callbacks.setLoadOnResume()) {
1146 runLoader = true;
1147 }
1148 }
1149 }
1150 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001151 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001152 }
Joe Onorato36115782010-06-17 13:28:48 -04001153 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001154
Reena Lee93f824a2011-09-23 17:20:28 -07001155 // If there is already a loader task running, tell it to stop.
1156 // returns true if isLaunching() was true on the old task
1157 private boolean stopLoaderLocked() {
1158 boolean isLaunching = false;
1159 LoaderTask oldTask = mLoaderTask;
1160 if (oldTask != null) {
1161 if (oldTask.isLaunching()) {
1162 isLaunching = true;
1163 }
1164 oldTask.stopLocked();
1165 }
1166 return isLaunching;
1167 }
1168
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001169 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001170 synchronized (mLock) {
1171 if (DEBUG_LOADERS) {
1172 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001175 // Clear any deferred bind-runnables from the synchronized load process
1176 // We must do this before any loading/binding is scheduled below.
1177 mDeferredBindRunnables.clear();
1178
Joe Onorato36115782010-06-17 13:28:48 -04001179 // Don't bother to start the thread if we know it's not going to do anything
1180 if (mCallbacks != null && mCallbacks.get() != null) {
1181 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001182 // also, don't downgrade isLaunching if we're already running
1183 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001184 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1186 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1187 } else {
1188 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1189 sWorker.post(mLoaderTask);
1190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
1193 }
1194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 void bindRemainingSynchronousPages() {
1196 // Post the remaining side pages to be loaded
1197 if (!mDeferredBindRunnables.isEmpty()) {
1198 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001199 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001200 }
1201 mDeferredBindRunnables.clear();
1202 }
1203 }
1204
Joe Onorato36115782010-06-17 13:28:48 -04001205 public void stopLoader() {
1206 synchronized (mLock) {
1207 if (mLoaderTask != null) {
1208 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 }
1210 }
Joe Onorato36115782010-06-17 13:28:48 -04001211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212
Winson Chung76828c82013-08-19 15:43:29 -07001213 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1214 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1215 final ContentResolver contentResolver = context.getContentResolver();
1216 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1217 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1218 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1219
1220 try {
1221 final int idIndex = sc.getColumnIndexOrThrow(
1222 LauncherSettings.WorkspaceScreens._ID);
1223 final int rankIndex = sc.getColumnIndexOrThrow(
1224 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1225 while (sc.moveToNext()) {
1226 try {
1227 long screenId = sc.getLong(idIndex);
1228 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001229 orderedScreens.put(rank, screenId);
1230 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001231 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001232 }
1233 }
1234 } finally {
1235 sc.close();
1236 }
1237 return orderedScreens;
1238 }
1239
Michael Jurkac57b7a82011-08-09 22:02:20 -07001240 public boolean isAllAppsLoaded() {
1241 return mAllAppsLoaded;
1242 }
1243
Winson Chung36a62fe2012-05-06 18:04:42 -07001244 boolean isLoadingWorkspace() {
1245 synchronized (mLock) {
1246 if (mLoaderTask != null) {
1247 return mLoaderTask.isLoadingWorkspace();
1248 }
1249 }
1250 return false;
1251 }
1252
Joe Onorato36115782010-06-17 13:28:48 -04001253 /**
1254 * Runnable for the thread that loads the contents of the launcher:
1255 * - workspace icons
1256 * - widgets
1257 * - all apps icons
1258 */
1259 private class LoaderTask implements Runnable {
1260 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001261 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001262 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001263 private boolean mStopped;
1264 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001265
Winson Chungc3eecff2011-07-11 17:44:15 -07001266 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001267
1268 LoaderTask(Context context, boolean isLaunching) {
1269 mContext = context;
1270 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001271 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 }
1273
Joe Onorato36115782010-06-17 13:28:48 -04001274 boolean isLaunching() {
1275 return mIsLaunching;
1276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277
Winson Chung36a62fe2012-05-06 18:04:42 -07001278 boolean isLoadingWorkspace() {
1279 return mIsLoadingAndBindingWorkspace;
1280 }
1281
Winson Chungc763c4e2013-07-19 13:49:06 -07001282 /** Returns whether this is an upgrade path */
1283 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001284 mIsLoadingAndBindingWorkspace = true;
1285
Joe Onorato36115782010-06-17 13:28:48 -04001286 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001287 if (DEBUG_LOADERS) {
1288 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001290
Winson Chungc763c4e2013-07-19 13:49:06 -07001291 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001292 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001293 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001294 synchronized (LoaderTask.this) {
1295 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001296 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001297 }
1298 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001299 }
1300 }
1301
Joe Onorato36115782010-06-17 13:28:48 -04001302 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001303 bindWorkspace(-1, isUpgradePath);
1304 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001305 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001306
Joe Onorato36115782010-06-17 13:28:48 -04001307 private void waitForIdle() {
1308 // Wait until the either we're stopped or the other threads are done.
1309 // This way we don't start loading all apps until the workspace has settled
1310 // down.
1311 synchronized (LoaderTask.this) {
1312 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001313
Joe Onorato36115782010-06-17 13:28:48 -04001314 mHandler.postIdle(new Runnable() {
1315 public void run() {
1316 synchronized (LoaderTask.this) {
1317 mLoadAndBindStepFinished = true;
1318 if (DEBUG_LOADERS) {
1319 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001320 }
Joe Onorato36115782010-06-17 13:28:48 -04001321 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 });
1325
Michael Jurkac7700af2013-05-14 20:17:58 +02001326 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001327 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001328 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1329 // wait no longer than 1sec at a time
1330 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001331 } catch (InterruptedException ex) {
1332 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001333 }
1334 }
Joe Onorato36115782010-06-17 13:28:48 -04001335 if (DEBUG_LOADERS) {
1336 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001337 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001338 + "ms for previous step to finish binding");
1339 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001340 }
Joe Onorato36115782010-06-17 13:28:48 -04001341 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001342
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001343 void runBindSynchronousPage(int synchronousBindPage) {
1344 if (synchronousBindPage < 0) {
1345 // Ensure that we have a valid page index to load synchronously
1346 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1347 "valid page index");
1348 }
1349 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1350 // Ensure that we don't try and bind a specified page when the pages have not been
1351 // loaded already (we should load everything asynchronously in that case)
1352 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1353 }
1354 synchronized (mLock) {
1355 if (mIsLoaderTaskRunning) {
1356 // Ensure that we are never running the background loading at this point since
1357 // we also touch the background collections
1358 throw new RuntimeException("Error! Background loading is already running");
1359 }
1360 }
1361
1362 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1363 // data structures, we can't allow any other thread to touch that data, but because
1364 // this call is synchronous, we can get away with not locking).
1365
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001366 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001367 // operations from the previous activity. We need to ensure that all queued operations
1368 // are executed before any synchronous binding work is done.
1369 mHandler.flush();
1370
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001371 // Divide the set of loaded items into those that we are binding synchronously, and
1372 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001373 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1375 // arise from that.
1376 onlyBindAllApps();
1377 }
1378
Joe Onorato36115782010-06-17 13:28:48 -04001379 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001380 boolean isUpgrade = false;
1381
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001382 synchronized (mLock) {
1383 mIsLoaderTaskRunning = true;
1384 }
Joe Onorato36115782010-06-17 13:28:48 -04001385 // Optimize for end-user experience: if the Launcher is up and // running with the
1386 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1387 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001388 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001389 // Elevate priority when Home launches for the first time to avoid
1390 // starving at boot time. Staring at a blank home is not cool.
1391 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001392 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1393 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001394 android.os.Process.setThreadPriority(mIsLaunching
1395 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1396 }
Winson Chung64359a52013-07-08 17:17:08 -07001397 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001398 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001399
Joe Onorato36115782010-06-17 13:28:48 -04001400 if (mStopped) {
1401 break keep_running;
1402 }
1403
1404 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1405 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001406 synchronized (mLock) {
1407 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001408 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1410 }
1411 }
Joe Onorato36115782010-06-17 13:28:48 -04001412 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001413
1414 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001415 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1416 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001417
1418 // Restore the default thread priority after we are done loading items
1419 synchronized (mLock) {
1420 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1421 }
Joe Onorato36115782010-06-17 13:28:48 -04001422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001423
Winson Chungaac01e12011-08-17 10:37:13 -07001424 // Update the saved icons if necessary
1425 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001426 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001427 for (Object key : sBgDbIconCache.keySet()) {
1428 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1429 }
1430 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001431 }
Winson Chungaac01e12011-08-17 10:37:13 -07001432
Winson Chungc58497e2013-09-03 17:48:37 -07001433 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1434 // Ensure that all the applications that are in the system are
1435 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001436 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001437 verifyApplications();
1438 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001439 }
1440
Joe Onorato36115782010-06-17 13:28:48 -04001441 // Clear out this reference, otherwise we end up holding it until all of the
1442 // callback runnables are done.
1443 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444
Joe Onorato36115782010-06-17 13:28:48 -04001445 synchronized (mLock) {
1446 // If we are still the last one to be scheduled, remove ourselves.
1447 if (mLoaderTask == this) {
1448 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001450 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001451 }
Joe Onorato36115782010-06-17 13:28:48 -04001452 }
1453
1454 public void stopLocked() {
1455 synchronized (LoaderTask.this) {
1456 mStopped = true;
1457 this.notify();
1458 }
1459 }
1460
1461 /**
1462 * Gets the callbacks object. If we've been stopped, or if the launcher object
1463 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1464 * object that was around when the deferred message was scheduled, and if there's
1465 * a new Callbacks object around then also return null. This will save us from
1466 * calling onto it with data that will be ignored.
1467 */
1468 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1469 synchronized (mLock) {
1470 if (mStopped) {
1471 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473
1474 if (mCallbacks == null) {
1475 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477
1478 final Callbacks callbacks = mCallbacks.get();
1479 if (callbacks != oldCallbacks) {
1480 return null;
1481 }
1482 if (callbacks == null) {
1483 Log.w(TAG, "no mCallbacks");
1484 return null;
1485 }
1486
1487 return callbacks;
1488 }
1489 }
1490
Winson Chungc763c4e2013-07-19 13:49:06 -07001491 private void verifyApplications() {
1492 final Context context = mApp.getContext();
1493
1494 // Cross reference all the applications in our apps list with items in the workspace
1495 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001496 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001497 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001498 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001499 tmpInfos = getItemInfoForComponentName(app.componentName);
1500 if (tmpInfos.isEmpty()) {
1501 // We are missing an application icon, so add this to the workspace
1502 added.add(app);
1503 // This is a rare event, so lets log it
1504 Log.e(TAG, "Missing Application on load: " + app);
1505 }
1506 }
1507 }
1508 if (!added.isEmpty()) {
1509 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001510 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001511 }
1512 }
1513
Winson Chung5f8afe62013-08-12 16:19:28 -07001514 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001515 LauncherAppState app = LauncherAppState.getInstance();
1516 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1517 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1518 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001519 }
1520
Joe Onorato36115782010-06-17 13:28:48 -04001521 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001522 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1523 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001524 LauncherAppState app = LauncherAppState.getInstance();
1525 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1526 int countX = (int) grid.numColumns;
1527 int countY = (int) grid.numRows;
1528
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001530 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001531 // Return early if we detect that an item is under the hotseat button
1532 if (mCallbacks == null ||
1533 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1534 deleteOnItemOverlap.set(true);
1535 return false;
1536 }
1537
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1539 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1540 [(int) item.screenId][0] != null) {
1541 Log.e(TAG, "Error loading shortcut into hotseat " + item
1542 + " into position (" + item.screenId + ":" + item.cellX + ","
1543 + item.cellY + ") occupied by "
1544 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1545 [(int) item.screenId][0]);
1546 return false;
1547 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001548 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001549 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 items[(int) item.screenId][0] = item;
1551 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001552 return true;
1553 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001554 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1555 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001556 return true;
1557 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001558
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001560 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 occupied.put(item.screenId, items);
1562 }
1563
1564 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001565 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001566 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1567 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001569 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001571 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001572 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001573 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001574 return false;
1575 }
1576 }
1577 }
1578 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1579 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001581 }
1582 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001583
Joe Onorato36115782010-06-17 13:28:48 -04001584 return true;
1585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586
Winson Chungba9c37f2013-08-30 14:11:37 -07001587 /** Clears all the sBg data structures */
1588 private void clearSBgDataStructures() {
1589 synchronized (sBgLock) {
1590 sBgWorkspaceItems.clear();
1591 sBgAppWidgets.clear();
1592 sBgFolders.clear();
1593 sBgItemsIdMap.clear();
1594 sBgDbIconCache.clear();
1595 sBgWorkspaceScreens.clear();
1596 }
1597 }
1598
Winson Chungc763c4e2013-07-19 13:49:06 -07001599 /** Returns whether this is an upgradge path */
1600 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001601 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001602
Joe Onorato36115782010-06-17 13:28:48 -04001603 final Context context = mContext;
1604 final ContentResolver contentResolver = context.getContentResolver();
1605 final PackageManager manager = context.getPackageManager();
1606 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1607 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001608
Winson Chung892c74d2013-08-22 16:15:50 -07001609 LauncherAppState app = LauncherAppState.getInstance();
1610 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1611 int countX = (int) grid.numColumns;
1612 int countY = (int) grid.numRows;
1613
Michael Jurkab85f8a42012-04-25 15:48:32 -07001614 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001615 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001616
Winson Chungc763c4e2013-07-19 13:49:06 -07001617 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001618 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001619
Winson Chung2abf94d2012-07-18 18:16:38 -07001620 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001621 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001622
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001623 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001624 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001625 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001626
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001627 // +1 for the hotseat (it can be larger than the workspace)
1628 // Load workspace in reverse order to ensure that latest items are loaded first (and
1629 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001630 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 try {
1633 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1634 final int intentIndex = c.getColumnIndexOrThrow
1635 (LauncherSettings.Favorites.INTENT);
1636 final int titleIndex = c.getColumnIndexOrThrow
1637 (LauncherSettings.Favorites.TITLE);
1638 final int iconTypeIndex = c.getColumnIndexOrThrow(
1639 LauncherSettings.Favorites.ICON_TYPE);
1640 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1641 final int iconPackageIndex = c.getColumnIndexOrThrow(
1642 LauncherSettings.Favorites.ICON_PACKAGE);
1643 final int iconResourceIndex = c.getColumnIndexOrThrow(
1644 LauncherSettings.Favorites.ICON_RESOURCE);
1645 final int containerIndex = c.getColumnIndexOrThrow(
1646 LauncherSettings.Favorites.CONTAINER);
1647 final int itemTypeIndex = c.getColumnIndexOrThrow(
1648 LauncherSettings.Favorites.ITEM_TYPE);
1649 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1650 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001651 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1652 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001653 final int screenIndex = c.getColumnIndexOrThrow(
1654 LauncherSettings.Favorites.SCREEN);
1655 final int cellXIndex = c.getColumnIndexOrThrow
1656 (LauncherSettings.Favorites.CELLX);
1657 final int cellYIndex = c.getColumnIndexOrThrow
1658 (LauncherSettings.Favorites.CELLY);
1659 final int spanXIndex = c.getColumnIndexOrThrow
1660 (LauncherSettings.Favorites.SPANX);
1661 final int spanYIndex = c.getColumnIndexOrThrow(
1662 LauncherSettings.Favorites.SPANY);
1663 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1664 //final int displayModeIndex = c.getColumnIndexOrThrow(
1665 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001666
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001667 ShortcutInfo info;
1668 String intentDescription;
1669 LauncherAppWidgetInfo appWidgetInfo;
1670 int container;
1671 long id;
1672 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001673
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001674 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001675 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001676 try {
1677 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 switch (itemType) {
1680 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1681 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001682 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001683 intentDescription = c.getString(intentIndex);
1684 try {
1685 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001686 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001687 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001688 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001689 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001690 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1691 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001692 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001693 // If apps can be on external storage, then we just
1694 // leave them for the user to remove (maybe add
1695 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001696 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001697 }
1698 continue;
1699 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001701 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 continue;
1703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1706 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1707 titleIndex, mLabelCache);
1708 } else {
1709 info = getShortcutInfo(c, context, iconTypeIndex,
1710 iconPackageIndex, iconResourceIndex, iconIndex,
1711 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 // App shortcuts that used to be automatically added to Launcher
1714 // didn't always have the correct intent flags set, so do that
1715 // here
1716 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001717 intent.getCategories() != null &&
1718 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001719 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001720 intent.addFlags(
1721 Intent.FLAG_ACTIVITY_NEW_TASK |
1722 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1723 }
Michael Jurka96879562012-03-22 05:54:33 -07001724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001725
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001726 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001727 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 container = c.getInt(containerIndex);
1730 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001731 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 info.cellX = c.getInt(cellXIndex);
1733 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001734 info.spanX = 1;
1735 info.spanY = 1;
1736 // Skip loading items that are out of bounds
1737 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1738 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001739 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1740 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001741 continue;
1742 }
1743 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001744 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001745 deleteOnItemOverlap.set(false);
1746 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1747 if (deleteOnItemOverlap.get()) {
1748 itemsToRemove.add(id);
1749 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 break;
1751 }
1752
1753 switch (container) {
1754 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1755 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1756 sBgWorkspaceItems.add(info);
1757 break;
1758 default:
1759 // Item is in a user folder
1760 FolderInfo folderInfo =
1761 findOrMakeFolder(sBgFolders, container);
1762 folderInfo.add(info);
1763 break;
1764 }
1765 sBgItemsIdMap.put(info.id, info);
1766
1767 // now that we've loaded everthing re-save it with the
1768 // icon in case it disappears somehow.
1769 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001770 } else {
1771 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001772 }
1773 break;
1774
1775 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1776 id = c.getLong(idIndex);
1777 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1778
1779 folderInfo.title = c.getString(titleIndex);
1780 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001782 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001783 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 folderInfo.cellX = c.getInt(cellXIndex);
1785 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001786 folderInfo.spanX = 1;
1787 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788
Winson Chung5f8afe62013-08-12 16:19:28 -07001789 // Skip loading items that are out of bounds
1790 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001791 if (checkItemDimensions(folderInfo)) {
1792 Log.d(TAG, "Skipped loading out of bounds folder");
1793 continue;
1794 }
1795 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001796 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001797 deleteOnItemOverlap.set(false);
1798 if (!checkItemPlacement(occupied, folderInfo,
1799 deleteOnItemOverlap)) {
1800 if (deleteOnItemOverlap.get()) {
1801 itemsToRemove.add(id);
1802 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001803 break;
1804 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001805
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1808 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1809 sBgWorkspaceItems.add(folderInfo);
1810 break;
Joe Onorato36115782010-06-17 13:28:48 -04001811 }
Joe Onorato17a89222011-02-08 17:26:11 -08001812
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1814 sBgFolders.put(folderInfo.id, folderInfo);
1815 break;
1816
1817 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1818 // Read all Launcher-specific widget details
1819 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001820 String savedProvider = c.getString(appWidgetProviderIndex);
1821
Joe Onorato36115782010-06-17 13:28:48 -04001822 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001823
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 final AppWidgetProviderInfo provider =
1825 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 if (!isSafeMode && (provider == null || provider.provider == null ||
1828 provider.provider.getPackageName() == null)) {
1829 String log = "Deleting widget that isn't installed anymore: id="
1830 + id + " appWidgetId=" + appWidgetId;
1831 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001832 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 itemsToRemove.add(id);
1834 } else {
1835 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1836 provider.provider);
1837 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001838 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 appWidgetInfo.cellX = c.getInt(cellXIndex);
1840 appWidgetInfo.cellY = c.getInt(cellYIndex);
1841 appWidgetInfo.spanX = c.getInt(spanXIndex);
1842 appWidgetInfo.spanY = c.getInt(spanYIndex);
1843 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1844 appWidgetInfo.minSpanX = minSpan[0];
1845 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001846
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 container = c.getInt(containerIndex);
1848 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1849 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1850 Log.e(TAG, "Widget found where container != " +
1851 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1852 continue;
1853 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854
Adam Cohene25af792013-06-06 23:08:25 -07001855 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001856 // Skip loading items that are out of bounds
1857 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1858 if (checkItemDimensions(appWidgetInfo)) {
1859 Log.d(TAG, "Skipped loading out of bounds app widget");
1860 continue;
1861 }
1862 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001864 deleteOnItemOverlap.set(false);
1865 if (!checkItemPlacement(occupied, appWidgetInfo,
1866 deleteOnItemOverlap)) {
1867 if (deleteOnItemOverlap.get()) {
1868 itemsToRemove.add(id);
1869 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 break;
1871 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001872 String providerName = provider.provider.flattenToString();
1873 if (!providerName.equals(savedProvider)) {
1874 ContentValues values = new ContentValues();
1875 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1876 providerName);
1877 String where = BaseColumns._ID + "= ?";
1878 String[] args = {Integer.toString(c.getInt(idIndex))};
1879 contentResolver.update(contentUri, values, where, args);
1880 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001881 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1882 sBgAppWidgets.add(appWidgetInfo);
1883 }
Joe Onorato36115782010-06-17 13:28:48 -04001884 break;
1885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001887 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 }
1890 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001891 if (c != null) {
1892 c.close();
1893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 }
1895
Winson Chungba9c37f2013-08-30 14:11:37 -07001896 // Break early if we've stopped loading
1897 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001898 clearSBgDataStructures();
1899 return false;
1900 }
1901
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 if (itemsToRemove.size() > 0) {
1903 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001904 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 // Remove dead items
1906 for (long id : itemsToRemove) {
1907 if (DEBUG_LOADERS) {
1908 Log.d(TAG, "Removed id = " + id);
1909 }
1910 // Don't notify content observers
1911 try {
1912 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1913 null, null);
1914 } catch (RemoteException e) {
1915 Log.w(TAG, "Could not remove id = " + id);
1916 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001917 }
1918 }
1919
Winson Chungc763c4e2013-07-19 13:49:06 -07001920 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 long maxScreenId = 0;
1922 // If we're importing we use the old screen order.
1923 for (ItemInfo item: sBgItemsIdMap.values()) {
1924 long screenId = item.screenId;
1925 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1926 !sBgWorkspaceScreens.contains(screenId)) {
1927 sBgWorkspaceScreens.add(screenId);
1928 if (screenId > maxScreenId) {
1929 maxScreenId = screenId;
1930 }
1931 }
1932 }
1933 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001934
Michael Jurka414300a2013-08-27 15:42:35 +02001935 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001936 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001937
1938 // Update the max item id after we load an old db
1939 long maxItemId = 0;
1940 // If we're importing we use the old screen order.
1941 for (ItemInfo item: sBgItemsIdMap.values()) {
1942 maxItemId = Math.max(maxItemId, item.id);
1943 }
Michael Jurka414300a2013-08-27 15:42:35 +02001944 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001946 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1947 for (Integer i : orderedScreens.keySet()) {
1948 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 }
1950
1951 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001952 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 for (ItemInfo item: sBgItemsIdMap.values()) {
1954 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1956 unusedScreens.contains(screenId)) {
1957 unusedScreens.remove(screenId);
1958 }
1959 }
1960
1961 // If there are any empty screens remove them, and update.
1962 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001963 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1965 }
1966 }
1967
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 if (DEBUG_LOADERS) {
1969 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1970 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001972 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001974
Daniel Sandler566da102013-06-25 23:43:45 -04001975 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001976 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001978 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 line += " | ";
1980 }
Winson Chung892c74d2013-08-22 16:15:50 -07001981 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 }
Joe Onorato36115782010-06-17 13:28:48 -04001984 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 }
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001989 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001990 }
1991
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 /** Filters the set of items who are directly or indirectly (via another container) on the
1993 * specified screen. */
1994 private void filterCurrentWorkspaceItems(int currentScreen,
1995 ArrayList<ItemInfo> allWorkspaceItems,
1996 ArrayList<ItemInfo> currentScreenItems,
1997 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001998 // Purge any null ItemInfos
1999 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2000 while (iter.hasNext()) {
2001 ItemInfo i = iter.next();
2002 if (i == null) {
2003 iter.remove();
2004 }
2005 }
2006
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 // If we aren't filtering on a screen, then the set of items to load is the full set of
2008 // items given.
2009 if (currentScreen < 0) {
2010 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002011 }
2012
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 // Order the set of items by their containers first, this allows use to walk through the
2014 // list sequentially, build up a list of containers that are in the specified screen,
2015 // as well as all items in those containers.
2016 Set<Long> itemsOnScreen = new HashSet<Long>();
2017 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2018 @Override
2019 public int compare(ItemInfo lhs, ItemInfo rhs) {
2020 return (int) (lhs.container - rhs.container);
2021 }
2022 });
2023 for (ItemInfo info : allWorkspaceItems) {
2024 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002025 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 currentScreenItems.add(info);
2027 itemsOnScreen.add(info.id);
2028 } else {
2029 otherScreenItems.add(info);
2030 }
2031 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2032 currentScreenItems.add(info);
2033 itemsOnScreen.add(info.id);
2034 } else {
2035 if (itemsOnScreen.contains(info.container)) {
2036 currentScreenItems.add(info);
2037 itemsOnScreen.add(info.id);
2038 } else {
2039 otherScreenItems.add(info);
2040 }
2041 }
2042 }
2043 }
2044
2045 /** Filters the set of widgets which are on the specified screen. */
2046 private void filterCurrentAppWidgets(int currentScreen,
2047 ArrayList<LauncherAppWidgetInfo> appWidgets,
2048 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2049 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2050 // If we aren't filtering on a screen, then the set of items to load is the full set of
2051 // widgets given.
2052 if (currentScreen < 0) {
2053 currentScreenWidgets.addAll(appWidgets);
2054 }
2055
2056 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002057 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002059 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 currentScreenWidgets.add(widget);
2061 } else {
2062 otherScreenWidgets.add(widget);
2063 }
2064 }
2065 }
2066
2067 /** Filters the set of folders which are on the specified screen. */
2068 private void filterCurrentFolders(int currentScreen,
2069 HashMap<Long, ItemInfo> itemsIdMap,
2070 HashMap<Long, FolderInfo> folders,
2071 HashMap<Long, FolderInfo> currentScreenFolders,
2072 HashMap<Long, FolderInfo> otherScreenFolders) {
2073 // If we aren't filtering on a screen, then the set of items to load is the full set of
2074 // widgets given.
2075 if (currentScreen < 0) {
2076 currentScreenFolders.putAll(folders);
2077 }
2078
2079 for (long id : folders.keySet()) {
2080 ItemInfo info = itemsIdMap.get(id);
2081 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002082 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002084 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 currentScreenFolders.put(id, folder);
2086 } else {
2087 otherScreenFolders.put(id, folder);
2088 }
2089 }
2090 }
2091
2092 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2093 * right) */
2094 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002095 final LauncherAppState app = LauncherAppState.getInstance();
2096 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 // XXX: review this
2098 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002099 @Override
2100 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002101 int cellCountX = (int) grid.numColumns;
2102 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002103 int screenOffset = cellCountX * cellCountY;
2104 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002105 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002106 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002107 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002108 rhs.cellY * cellCountX + rhs.cellX);
2109 return (int) (lr - rr);
2110 }
2111 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002113
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2115 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002116 final Runnable r = new Runnable() {
2117 @Override
2118 public void run() {
2119 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2120 if (callbacks != null) {
2121 callbacks.bindScreens(orderedScreens);
2122 }
2123 }
2124 };
2125 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2126 }
2127
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2129 final ArrayList<ItemInfo> workspaceItems,
2130 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2131 final HashMap<Long, FolderInfo> folders,
2132 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002133
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 final boolean postOnMainThread = (deferredBindRunnables != null);
2135
2136 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002137 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002139 final int start = i;
2140 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 final Runnable r = new Runnable() {
2142 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002144 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002146 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2147 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 }
2149 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 };
2151 if (postOnMainThread) {
2152 deferredBindRunnables.add(r);
2153 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002154 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 }
Joe Onorato36115782010-06-17 13:28:48 -04002156 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157
2158 // Bind the folders
2159 if (!folders.isEmpty()) {
2160 final Runnable r = new Runnable() {
2161 public void run() {
2162 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2163 if (callbacks != null) {
2164 callbacks.bindFolders(folders);
2165 }
2166 }
2167 };
2168 if (postOnMainThread) {
2169 deferredBindRunnables.add(r);
2170 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002171 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 }
2173 }
2174
2175 // Bind the widgets, one at a time
2176 N = appWidgets.size();
2177 for (int i = 0; i < N; i++) {
2178 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2179 final Runnable r = new Runnable() {
2180 public void run() {
2181 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2182 if (callbacks != null) {
2183 callbacks.bindAppWidget(widget);
2184 }
2185 }
2186 };
2187 if (postOnMainThread) {
2188 deferredBindRunnables.add(r);
2189 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002190 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002191 }
2192 }
2193 }
2194
2195 /**
2196 * Binds all loaded data to actual views on the main thread.
2197 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002198 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 final long t = SystemClock.uptimeMillis();
2200 Runnable r;
2201
2202 // Don't use these two variables in any of the callback runnables.
2203 // Otherwise we hold a reference to them.
2204 final Callbacks oldCallbacks = mCallbacks.get();
2205 if (oldCallbacks == null) {
2206 // This launcher has exited and nobody bothered to tell us. Just bail.
2207 Log.w(TAG, "LoaderTask running with no launcher");
2208 return;
2209 }
2210
Winson Chung4a2afa32012-07-19 14:53:05 -07002211 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2212 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 oldCallbacks.getCurrentWorkspaceScreen();
2214
2215 // Load all the items that are on the current page first (and in the process, unbind
2216 // all the existing workspace items before we call startBinding() below.
2217 unbindWorkspaceItemsOnMainThread();
2218 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2219 ArrayList<LauncherAppWidgetInfo> appWidgets =
2220 new ArrayList<LauncherAppWidgetInfo>();
2221 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2222 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002224 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225 workspaceItems.addAll(sBgWorkspaceItems);
2226 appWidgets.addAll(sBgAppWidgets);
2227 folders.putAll(sBgFolders);
2228 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 }
2231
2232 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2233 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2234 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2235 new ArrayList<LauncherAppWidgetInfo>();
2236 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2237 new ArrayList<LauncherAppWidgetInfo>();
2238 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2239 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2240
2241 // Separate the items that are on the current screen, and all the other remaining items
2242 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2243 otherWorkspaceItems);
2244 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2245 otherAppWidgets);
2246 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2247 otherFolders);
2248 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2249 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2250
2251 // Tell the workspace that we're about to start binding items
2252 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002253 public void run() {
2254 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2255 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002257 }
2258 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 };
Winson Chung81b52252012-08-27 15:34:29 -07002260 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2263
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 // Load items on the current page
2265 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2266 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002267 if (isLoadingSynchronously) {
2268 r = new Runnable() {
2269 public void run() {
2270 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2271 if (callbacks != null) {
2272 callbacks.onPageBoundSynchronously(currentScreen);
2273 }
2274 }
2275 };
Winson Chung81b52252012-08-27 15:34:29 -07002276 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002277 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278
Winson Chung4a2afa32012-07-19 14:53:05 -07002279 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2280 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 mDeferredBindRunnables.clear();
2282 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002283 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284
2285 // Tell the workspace that we're done binding items
2286 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002287 public void run() {
2288 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2289 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002290 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002291 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292
Winson Chung98e030b2012-05-07 16:01:11 -07002293 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002294 if (DEBUG_LOADERS) {
2295 Log.d(TAG, "bound workspace in "
2296 + (SystemClock.uptimeMillis()-t) + "ms");
2297 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002298
2299 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002300 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002302 if (isLoadingSynchronously) {
2303 mDeferredBindRunnables.add(r);
2304 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002305 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002306 }
Joe Onorato36115782010-06-17 13:28:48 -04002307 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002308
Joe Onorato36115782010-06-17 13:28:48 -04002309 private void loadAndBindAllApps() {
2310 if (DEBUG_LOADERS) {
2311 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2312 }
2313 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002314 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002315 synchronized (LoaderTask.this) {
2316 if (mStopped) {
2317 return;
2318 }
2319 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002320 }
Joe Onorato36115782010-06-17 13:28:48 -04002321 } else {
2322 onlyBindAllApps();
2323 }
2324 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002325
Joe Onorato36115782010-06-17 13:28:48 -04002326 private void onlyBindAllApps() {
2327 final Callbacks oldCallbacks = mCallbacks.get();
2328 if (oldCallbacks == null) {
2329 // This launcher has exited and nobody bothered to tell us. Just bail.
2330 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2331 return;
2332 }
2333
2334 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002335 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002336 final ArrayList<AppInfo> list
2337 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002338 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002339 public void run() {
2340 final long t = SystemClock.uptimeMillis();
2341 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2342 if (callbacks != null) {
2343 callbacks.bindAllApplications(list);
2344 }
2345 if (DEBUG_LOADERS) {
2346 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2347 + (SystemClock.uptimeMillis()-t) + "ms");
2348 }
2349 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002350 };
2351 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002352 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002353 r.run();
2354 } else {
2355 mHandler.post(r);
2356 }
Joe Onorato36115782010-06-17 13:28:48 -04002357 }
2358
Winson Chung64359a52013-07-08 17:17:08 -07002359 private void loadAllApps() {
2360 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002361
Joe Onorato36115782010-06-17 13:28:48 -04002362 final Callbacks oldCallbacks = mCallbacks.get();
2363 if (oldCallbacks == null) {
2364 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002365 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002366 return;
2367 }
2368
Winson Chung64359a52013-07-08 17:17:08 -07002369 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002370 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2371 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2372
Winson Chung64359a52013-07-08 17:17:08 -07002373 // Clear the list of apps
2374 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002375
Winson Chung64359a52013-07-08 17:17:08 -07002376 // Query for the set of apps
2377 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2378 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2379 if (DEBUG_LOADERS) {
2380 Log.d(TAG, "queryIntentActivities took "
2381 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2382 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2383 }
2384 // Fail if we don't have any apps
2385 if (apps == null || apps.isEmpty()) {
2386 return;
2387 }
2388 // Sort the applications by name
2389 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2390 Collections.sort(apps,
2391 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2392 if (DEBUG_LOADERS) {
2393 Log.d(TAG, "sort took "
2394 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 }
2396
Winson Chung64359a52013-07-08 17:17:08 -07002397 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002398 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002399 ResolveInfo app = apps.get(i);
2400 if (oldCallbacks.shouldShowApp(app)) {
2401 // This builds the icon bitmaps.
2402 mBgAllAppsList.add(new AppInfo(packageManager, app,
2403 mIconCache, mLabelCache));
2404 }
Winson Chung64359a52013-07-08 17:17:08 -07002405 }
2406
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002407 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002408 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2409 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002410
2411 // Post callback on main thread
2412 mHandler.post(new Runnable() {
2413 public void run() {
2414 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002415 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002416 if (callbacks != null) {
2417 callbacks.bindAllApplications(added);
2418 if (DEBUG_LOADERS) {
2419 Log.d(TAG, "bound " + added.size() + " apps in "
2420 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2421 }
2422 } else {
2423 Log.i(TAG, "not binding apps: no Launcher activity");
2424 }
2425 }
2426 });
2427
Joe Onorato36115782010-06-17 13:28:48 -04002428 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002429 Log.d(TAG, "Icons processed in "
2430 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002431 }
2432 }
2433
2434 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002435 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2437 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2438 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2439 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2440 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2441 }
Joe Onorato36115782010-06-17 13:28:48 -04002442 }
2443 }
2444
2445 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002446 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002447 }
2448
2449 private class PackageUpdatedTask implements Runnable {
2450 int mOp;
2451 String[] mPackages;
2452
2453 public static final int OP_NONE = 0;
2454 public static final int OP_ADD = 1;
2455 public static final int OP_UPDATE = 2;
2456 public static final int OP_REMOVE = 3; // uninstlled
2457 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2458
2459
2460 public PackageUpdatedTask(int op, String[] packages) {
2461 mOp = op;
2462 mPackages = packages;
2463 }
2464
2465 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002466 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002467
2468 final String[] packages = mPackages;
2469 final int N = packages.length;
2470 switch (mOp) {
2471 case OP_ADD:
2472 for (int i=0; i<N; i++) {
2473 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002474 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002475 }
2476 break;
2477 case OP_UPDATE:
2478 for (int i=0; i<N; i++) {
2479 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002480 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002481 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002482 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002483 }
2484 break;
2485 case OP_REMOVE:
2486 case OP_UNAVAILABLE:
2487 for (int i=0; i<N; i++) {
2488 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002489 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002490 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002491 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
2493 break;
2494 }
2495
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002496 ArrayList<AppInfo> added = null;
2497 ArrayList<AppInfo> modified = null;
2498 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002499
Adam Cohen487f7dd2012-06-28 18:12:10 -07002500 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002501 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002502 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002503 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002504 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002505 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002506 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002507 }
Winson Chung5d55f332012-07-16 20:45:03 -07002508 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002509 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002510 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002511 }
2512
Joe Onorato36115782010-06-17 13:28:48 -04002513 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2514 if (callbacks == null) {
2515 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2516 return;
2517 }
2518
2519 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002520 // Ensure that we add all the workspace applications to the db
2521 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002522 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2523 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2524 } else {
2525 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2526 addAndBindAddedApps(context, addedInfos, cb, added);
2527 }
Joe Onorato36115782010-06-17 13:28:48 -04002528 }
2529 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002530 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002531
2532 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002533 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002534 ArrayList<ItemInfo> infos =
2535 getItemInfoForComponentName(a.componentName);
2536 for (ItemInfo i : infos) {
2537 if (isShortcutInfoUpdateable(i)) {
2538 ShortcutInfo info = (ShortcutInfo) i;
2539 info.title = a.title.toString();
2540 updateItemInDatabase(context, info);
2541 }
2542 }
2543 }
2544
Joe Onorato36115782010-06-17 13:28:48 -04002545 mHandler.post(new Runnable() {
2546 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002547 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2548 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002549 callbacks.bindAppsUpdated(modifiedFinal);
2550 }
2551 }
2552 });
2553 }
Winson Chung83892cc2013-05-01 16:53:33 -07002554 // If a package has been removed, or an app has been removed as a result of
2555 // an update (for example), make the removed callback.
2556 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002557 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002558 final ArrayList<String> removedPackageNames =
2559 new ArrayList<String>(Arrays.asList(packages));
2560
Winson Chung64359a52013-07-08 17:17:08 -07002561 // Update the launcher db to reflect the removal of apps
2562 if (packageRemoved) {
2563 for (String pn : removedPackageNames) {
2564 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2565 for (ItemInfo i : infos) {
2566 deleteItemFromDatabase(context, i);
2567 }
2568 }
Winson Chung780fe592013-09-26 14:48:44 -07002569
2570 // Remove any queued items from the install queue
2571 String spKey = LauncherAppState.getSharedPreferencesKey();
2572 SharedPreferences sp =
2573 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2574 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
Winson Chung64359a52013-07-08 17:17:08 -07002575 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002576 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002577 ArrayList<ItemInfo> infos =
2578 getItemInfoForComponentName(a.componentName);
2579 for (ItemInfo i : infos) {
2580 deleteItemFromDatabase(context, i);
2581 }
2582 }
2583 }
2584
Joe Onorato36115782010-06-17 13:28:48 -04002585 mHandler.post(new Runnable() {
2586 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002587 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2588 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002589 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002590 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002591 }
2592 }
2593 });
Joe Onoratobe386092009-11-17 17:32:16 -08002594 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002595
Michael Jurkac402cd92013-05-20 15:49:32 +02002596 final ArrayList<Object> widgetsAndShortcuts =
2597 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002598 mHandler.post(new Runnable() {
2599 @Override
2600 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002601 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2602 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002603 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002604 }
2605 }
2606 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002607
2608 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002609 mHandler.post(new Runnable() {
2610 public void run() {
2611 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2612 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002613 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002614 }
2615 }
2616 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002617 }
2618 }
2619
Michael Jurkac402cd92013-05-20 15:49:32 +02002620 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2621 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2622 PackageManager packageManager = context.getPackageManager();
2623 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2624 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2625 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2626 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2627 Collections.sort(widgetsAndShortcuts,
2628 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2629 return widgetsAndShortcuts;
2630 }
2631
Winson Chung1323b482013-08-05 12:41:55 -07002632 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002633 if (cn == null) {
2634 return false;
2635 }
2636
2637 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002638 // Skip if the application is disabled
2639 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2640 if (!pi.applicationInfo.enabled) {
2641 return false;
2642 }
2643
2644 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002645 return (pm.getActivityInfo(cn, 0) != null);
2646 } catch (NameNotFoundException e) {
2647 return false;
2648 }
2649 }
2650
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002652 * This is called from the code that adds shortcuts from the intent receiver. This
2653 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002654 */
Joe Onorato56d82912010-03-07 14:32:10 -05002655 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002656 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002658
Joe Onorato56d82912010-03-07 14:32:10 -05002659 /**
2660 * Make an ShortcutInfo object for a shortcut that is an application.
2661 *
2662 * If c is not null, then it will be used to fill in missing data like the title and icon.
2663 */
2664 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002665 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002666 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002667 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002668 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002669 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002670 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002671 } else {
2672 try {
2673 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2674 info.initFlagsAndFirstInstallTime(pi);
2675 } catch (NameNotFoundException e) {
2676 Log.d(TAG, "getPackInfo failed for package " +
2677 componentName.getPackageName());
2678 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002679 }
2680
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002681 // TODO: See if the PackageManager knows about this case. If it doesn't
2682 // then return null & delete this.
2683
Joe Onorato56d82912010-03-07 14:32:10 -05002684 // the resource -- This may implicitly give us back the fallback icon,
2685 // but don't worry about that. All we're doing with usingFallbackIcon is
2686 // to avoid saving lots of copies of that in the database, and most apps
2687 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002688
2689 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2690 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2691 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002692 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002693 ResolveInfo resolveInfo = null;
2694 ComponentName oldComponent = intent.getComponent();
2695 Intent newIntent = new Intent(intent.getAction(), null);
2696 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2697 newIntent.setPackage(oldComponent.getPackageName());
2698 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2699 for (ResolveInfo i : infos) {
2700 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2701 i.activityInfo.name);
2702 if (cn.equals(oldComponent)) {
2703 resolveInfo = i;
2704 }
2705 }
2706 if (resolveInfo == null) {
2707 resolveInfo = manager.resolveActivity(intent, 0);
2708 }
Joe Onorato56d82912010-03-07 14:32:10 -05002709 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002710 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002711 }
Joe Onorato56d82912010-03-07 14:32:10 -05002712 // the db
2713 if (icon == null) {
2714 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002715 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002716 }
2717 }
2718 // the fallback icon
2719 if (icon == null) {
2720 icon = getFallbackIcon();
2721 info.usingFallbackIcon = true;
2722 }
2723 info.setIcon(icon);
2724
2725 // from the resource
2726 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002727 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2728 if (labelCache != null && labelCache.containsKey(key)) {
2729 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002730 } else {
2731 info.title = resolveInfo.activityInfo.loadLabel(manager);
2732 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002733 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002734 }
2735 }
Joe Onorato56d82912010-03-07 14:32:10 -05002736 }
2737 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002739 if (c != null) {
2740 info.title = c.getString(titleIndex);
2741 }
2742 }
2743 // fall back to the class name of the activity
2744 if (info.title == null) {
2745 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002747 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2748 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002750
Winson Chung64359a52013-07-08 17:17:08 -07002751 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2752 ItemInfoFilter f) {
2753 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2754 for (ItemInfo i : infos) {
2755 if (i instanceof ShortcutInfo) {
2756 ShortcutInfo info = (ShortcutInfo) i;
2757 ComponentName cn = info.intent.getComponent();
2758 if (cn != null && f.filterItem(null, info, cn)) {
2759 filtered.add(info);
2760 }
2761 } else if (i instanceof FolderInfo) {
2762 FolderInfo info = (FolderInfo) i;
2763 for (ShortcutInfo s : info.contents) {
2764 ComponentName cn = s.intent.getComponent();
2765 if (cn != null && f.filterItem(info, s, cn)) {
2766 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002767 }
2768 }
Winson Chung64359a52013-07-08 17:17:08 -07002769 } else if (i instanceof LauncherAppWidgetInfo) {
2770 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2771 ComponentName cn = info.providerName;
2772 if (cn != null && f.filterItem(null, info, cn)) {
2773 filtered.add(info);
2774 }
Winson Chung8a435102012-08-30 17:16:53 -07002775 }
2776 }
Winson Chung64359a52013-07-08 17:17:08 -07002777 return new ArrayList<ItemInfo>(filtered);
2778 }
2779
2780 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002781 ItemInfoFilter filter = new ItemInfoFilter() {
2782 @Override
2783 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2784 return cn.getPackageName().equals(pn);
2785 }
2786 };
2787 return filterItemInfos(sBgItemsIdMap.values(), filter);
2788 }
2789
2790 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002791 ItemInfoFilter filter = new ItemInfoFilter() {
2792 @Override
2793 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2794 return cn.equals(cname);
2795 }
2796 };
2797 return filterItemInfos(sBgItemsIdMap.values(), filter);
2798 }
2799
2800 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2801 if (i instanceof ShortcutInfo) {
2802 ShortcutInfo info = (ShortcutInfo) i;
2803 // We need to check for ACTION_MAIN otherwise getComponent() might
2804 // return null for some shortcuts (for instance, for shortcuts to
2805 // web pages.)
2806 Intent intent = info.intent;
2807 ComponentName name = intent.getComponent();
2808 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2809 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2810 return true;
2811 }
2812 }
2813 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002814 }
2815
2816 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002817 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002819 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002820 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2821 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822
Joe Onorato56d82912010-03-07 14:32:10 -05002823 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002824 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002827 // TODO: If there's an explicit component and we can't install that, delete it.
2828
Joe Onorato56d82912010-03-07 14:32:10 -05002829 info.title = c.getString(titleIndex);
2830
Joe Onorato9c1289c2009-08-17 11:03:03 -04002831 int iconType = c.getInt(iconTypeIndex);
2832 switch (iconType) {
2833 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2834 String packageName = c.getString(iconPackageIndex);
2835 String resourceName = c.getString(iconResourceIndex);
2836 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002837 info.customIcon = false;
2838 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002839 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002841 if (resources != null) {
2842 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002843 icon = Utilities.createIconBitmap(
2844 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002846 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002847 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 }
Joe Onorato56d82912010-03-07 14:32:10 -05002849 // the db
2850 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002851 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002852 }
2853 // the fallback icon
2854 if (icon == null) {
2855 icon = getFallbackIcon();
2856 info.usingFallbackIcon = true;
2857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 break;
2859 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002860 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002861 if (icon == null) {
2862 icon = getFallbackIcon();
2863 info.customIcon = false;
2864 info.usingFallbackIcon = true;
2865 } else {
2866 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 break;
2869 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002870 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002871 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 info.customIcon = false;
2873 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002874 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002875 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 return info;
2877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878
Michael Jurka931dc972011-08-05 15:08:15 -07002879 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002880 @SuppressWarnings("all") // suppress dead code warning
2881 final boolean debug = false;
2882 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002883 Log.d(TAG, "getIconFromCursor app="
2884 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2885 }
2886 byte[] data = c.getBlob(iconIndex);
2887 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002888 return Utilities.createIconBitmap(
2889 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002890 } catch (Exception e) {
2891 return null;
2892 }
2893 }
2894
Winson Chung3d503fb2011-07-13 17:25:49 -07002895 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2896 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002897 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002898 if (info == null) {
2899 return null;
2900 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002902
2903 return info;
2904 }
2905
Winson Chunga9abd0e2010-10-27 17:18:37 -07002906 /**
Winson Chung55cef262010-10-28 14:14:18 -07002907 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2908 */
2909 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2910 ComponentName component) {
2911 List<AppWidgetProviderInfo> widgets =
2912 AppWidgetManager.getInstance(context).getInstalledProviders();
2913 for (AppWidgetProviderInfo info : widgets) {
2914 if (info.provider.equals(component)) {
2915 return info;
2916 }
2917 }
2918 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002919 }
2920
Winson Chung68846fd2010-10-29 11:00:27 -07002921 /**
2922 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2923 */
2924 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2925 final PackageManager packageManager = context.getPackageManager();
2926 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2927 new ArrayList<WidgetMimeTypeHandlerData>();
2928
2929 final Intent supportsIntent =
2930 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2931 supportsIntent.setType(mimeType);
2932
2933 // Create a set of widget configuration components that we can test against
2934 final List<AppWidgetProviderInfo> widgets =
2935 AppWidgetManager.getInstance(context).getInstalledProviders();
2936 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2937 new HashMap<ComponentName, AppWidgetProviderInfo>();
2938 for (AppWidgetProviderInfo info : widgets) {
2939 configurationComponentToWidget.put(info.configure, info);
2940 }
2941
2942 // Run through each of the intents that can handle this type of clip data, and cross
2943 // reference them with the components that are actual configuration components
2944 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2945 PackageManager.MATCH_DEFAULT_ONLY);
2946 for (ResolveInfo info : activities) {
2947 final ActivityInfo activityInfo = info.activityInfo;
2948 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2949 activityInfo.name);
2950 if (configurationComponentToWidget.containsKey(infoComponent)) {
2951 supportedConfigurationActivities.add(
2952 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2953 configurationComponentToWidget.get(infoComponent)));
2954 }
2955 }
2956 return supportedConfigurationActivities;
2957 }
2958
Winson Chunga9abd0e2010-10-27 17:18:37 -07002959 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002960 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2961 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2962 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2963
Adam Cohend9198822011-11-22 16:42:47 -08002964 if (intent == null) {
2965 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2966 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2967 return null;
2968 }
2969
Joe Onorato0589f0f2010-02-08 13:44:00 -08002970 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002971 boolean customIcon = false;
2972 ShortcutIconResource iconResource = null;
2973
2974 if (bitmap != null && bitmap instanceof Bitmap) {
2975 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002976 customIcon = true;
2977 } else {
2978 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2979 if (extra != null && extra instanceof ShortcutIconResource) {
2980 try {
2981 iconResource = (ShortcutIconResource) extra;
2982 final PackageManager packageManager = context.getPackageManager();
2983 Resources resources = packageManager.getResourcesForApplication(
2984 iconResource.packageName);
2985 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002986 icon = Utilities.createIconBitmap(
2987 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002988 } catch (Exception e) {
2989 Log.w(TAG, "Could not load shortcut icon: " + extra);
2990 }
2991 }
2992 }
2993
Michael Jurkac9d95c52011-08-29 14:03:34 -07002994 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002995
2996 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002997 if (fallbackIcon != null) {
2998 icon = fallbackIcon;
2999 } else {
3000 icon = getFallbackIcon();
3001 info.usingFallbackIcon = true;
3002 }
Joe Onorato56d82912010-03-07 14:32:10 -05003003 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003004 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003005
Joe Onorato0589f0f2010-02-08 13:44:00 -08003006 info.title = name;
3007 info.intent = intent;
3008 info.customIcon = customIcon;
3009 info.iconResource = iconResource;
3010
3011 return info;
3012 }
3013
Winson Chungaac01e12011-08-17 10:37:13 -07003014 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3015 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003016 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003017 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003018 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003019 }
Joe Onorato56d82912010-03-07 14:32:10 -05003020 // If this icon doesn't have a custom icon, check to see
3021 // what's stored in the DB, and if it doesn't match what
3022 // we're going to show, store what we are going to show back
3023 // into the DB. We do this so when we're loading, if the
3024 // package manager can't find an icon (for example because
3025 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003026 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003027 cache.put(info, c.getBlob(iconIndex));
3028 return true;
3029 }
3030 return false;
3031 }
3032 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3033 boolean needSave = false;
3034 try {
3035 if (data != null) {
3036 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3037 Bitmap loaded = info.getIcon(mIconCache);
3038 needSave = !saved.sameAs(loaded);
3039 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003040 needSave = true;
3041 }
Winson Chungaac01e12011-08-17 10:37:13 -07003042 } catch (Exception e) {
3043 needSave = true;
3044 }
3045 if (needSave) {
3046 Log.d(TAG, "going to save icon bitmap for info=" + info);
3047 // This is slower than is ideal, but this only happens once
3048 // or when the app is updated with a new icon.
3049 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003050 }
3051 }
3052
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003054 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 * or make a new one.
3056 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003057 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003058 // See if a placeholder was created for us already
3059 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003060 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003062 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 folders.put(id, folderInfo);
3064 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003065 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003068 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003069 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003070 return new Comparator<AppInfo>() {
3071 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003072 int result = collator.compare(a.title.toString().trim(),
3073 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003074 if (result == 0) {
3075 result = a.componentName.compareTo(b.componentName);
3076 }
3077 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003078 }
Winson Chung11904872012-09-17 16:58:46 -07003079 };
3080 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003081 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3082 = new Comparator<AppInfo>() {
3083 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003084 if (a.firstInstallTime < b.firstInstallTime) return 1;
3085 if (a.firstInstallTime > b.firstInstallTime) return -1;
3086 return 0;
3087 }
3088 };
Winson Chung11904872012-09-17 16:58:46 -07003089 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3090 final Collator collator = Collator.getInstance();
3091 return new Comparator<AppWidgetProviderInfo>() {
3092 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003093 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003094 }
3095 };
3096 }
Winson Chung5308f242011-08-18 12:12:41 -07003097 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3098 if (info.activityInfo != null) {
3099 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3100 } else {
3101 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3102 }
3103 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003104 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003105 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003106 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003107 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003108 ShortcutNameComparator(PackageManager pm) {
3109 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003110 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003111 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003112 }
3113 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3114 mPackageManager = pm;
3115 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003116 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003117 }
3118 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003119 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003120 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3121 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3122 if (mLabelCache.containsKey(keyA)) {
3123 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003124 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003125 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003126
Winson Chung5308f242011-08-18 12:12:41 -07003127 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003128 }
Winson Chung5308f242011-08-18 12:12:41 -07003129 if (mLabelCache.containsKey(keyB)) {
3130 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003131 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003132 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003133
Winson Chung5308f242011-08-18 12:12:41 -07003134 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003135 }
Winson Chung11904872012-09-17 16:58:46 -07003136 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003137 }
3138 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003139 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003140 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003141 private PackageManager mPackageManager;
3142 private HashMap<Object, String> mLabelCache;
3143 WidgetAndShortcutNameComparator(PackageManager pm) {
3144 mPackageManager = pm;
3145 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003146 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003147 }
3148 public final int compare(Object a, Object b) {
3149 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003150 if (mLabelCache.containsKey(a)) {
3151 labelA = mLabelCache.get(a);
3152 } else {
3153 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003154 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003155 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003156 mLabelCache.put(a, labelA);
3157 }
3158 if (mLabelCache.containsKey(b)) {
3159 labelB = mLabelCache.get(b);
3160 } else {
3161 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003162 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003163 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003164 mLabelCache.put(b, labelB);
3165 }
Winson Chung11904872012-09-17 16:58:46 -07003166 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003167 }
3168 };
Joe Onoratobe386092009-11-17 17:32:16 -08003169
3170 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003171 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003172 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3173 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3174 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3175 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003176 if (mLoaderTask != null) {
3177 mLoaderTask.dumpState();
3178 } else {
3179 Log.d(TAG, "mLoaderTask=null");
3180 }
Joe Onoratobe386092009-11-17 17:32:16 -08003181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182}