blob: 8676b0915747b74e2e389175dc7dfafa254807f6 [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;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070043import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040044import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080045
Michael Jurkac2f801e2011-07-12 14:19:46 -070046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
49import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070050import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070051import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070052import java.util.Collections;
53import java.util.Comparator;
54import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070055import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070056import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070057import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070059import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Maintains in-memory state of the Launcher. It is expected that there should be only one
63 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070064 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040066public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080067 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070069
Daniel Sandler8707e0f2013-08-15 15:54:18 -070070 // true = use a "More Apps" folder for non-workspace apps on upgrade
71 // false = strew non-workspace apps across the workspace on upgrade
72 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
73
Joe Onorato36115782010-06-17 13:28:48 -040074 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070075 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040076
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078 private final Object mLock = new Object();
79 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040080 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020082 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Winson Chung81b52252012-08-27 15:34:29 -070084 // Specific runnable types that are run on the main thread deferred handler, this allows us to
85 // clear all queued binding runnables when the Launcher activity is destroyed.
86 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
87 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
88
89
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070090 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
91 static {
92 sWorkerThread.start();
93 }
94 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
95
Joe Onoratocc67f472010-06-08 10:54:30 -070096 // We start off with everything not loaded. After that, we assume that
97 // our monitoring of the package manager provides all updates and we never
98 // need to do a requery. These are only ever touched from the loader thread.
99 private boolean mWorkspaceLoaded;
100 private boolean mAllAppsLoaded;
101
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700102 // When we are loading pages synchronously, we can't just post the binding of items on the side
103 // pages as this delays the rotation process. Instead, we wait for a callback from the first
104 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
105 // a normal load, we also clear this set of Runnables.
106 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700110 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700111 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // The lock that must be acquired before referencing any static bg data structures. Unlike
114 // other locks, this one can generally be held long-term because we never expect any of these
115 // static data structures to be referenced outside of the worker thread except on the first
116 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700117 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118
Adam Cohen487f7dd2012-06-28 18:12:10 -0700119 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700120 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
124 // created by LauncherModel that are directly on the home screen (however, no widgets or
125 // shortcuts within folders).
126 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
129 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130 new ArrayList<LauncherAppWidgetInfo>();
131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
133 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
136 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700137
138 // sBgWorkspaceScreens is the ordered set of workspace screens.
139 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // </ only access in worker thread >
142
143 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
153 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700154 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500156 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700157 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 public void bindAppWidget(LauncherAppWidgetInfo info);
159 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700160 public void bindAppsAdded(ArrayList<Long> newScreens,
161 ArrayList<ItemInfo> addNotAnimated,
162 ArrayList<ItemInfo> addAnimated);
Joe Onorato64e6be72010-03-05 15:05:52 -0500163 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700164 public void bindComponentsRemoved(ArrayList<String> packageNames,
165 ArrayList<ApplicationInfo> appInfos,
166 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200167 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100168 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700169 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung64359a52013-07-08 17:17:08 -0700172 public interface ItemInfoFilter {
173 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
174 }
175
Daniel Sandlere4f98912013-06-25 15:13:26 -0400176 LauncherModel(LauncherAppState app, IconCache iconCache) {
177 final Context context = app.getContext();
178
Winson Chungee055712013-07-30 14:46:24 -0700179 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700181 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800182 mIconCache = iconCache;
183
184 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400185 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400186
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400187 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700188 Configuration config = res.getConfiguration();
189 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 }
191
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700192 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
193 * posted on the main thread handler. */
194 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700195 runOnMainThread(r, 0);
196 }
197 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700198 if (sWorkerThread.getThreadId() == Process.myTid()) {
199 // If we are on the worker thread, post onto the main handler
200 mHandler.post(r);
201 } else {
202 r.run();
203 }
204 }
205
206 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
207 * posted on the worker thread handler. */
208 private static void runOnWorkerThread(Runnable r) {
209 if (sWorkerThread.getThreadId() == Process.myTid()) {
210 r.run();
211 } else {
212 // If we are not on the worker thread, then post to the worker handler
213 sWorker.post(r);
214 }
215 }
216
Winson Chungc9168342013-06-26 14:54:55 -0700217 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
218 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700219 LauncherAppState app = LauncherAppState.getInstance();
220 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
221 final int xCount = (int) grid.numColumns;
222 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700223 boolean[][] occupied = new boolean[xCount][yCount];
224
225 int cellX, cellY, spanX, spanY;
226 for (int i = 0; i < items.size(); ++i) {
227 final ItemInfo item = items.get(i);
228 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
229 if (item.screenId == screen) {
230 cellX = item.cellX;
231 cellY = item.cellY;
232 spanX = item.spanX;
233 spanY = item.spanY;
234 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
235 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
236 occupied[x][y] = true;
237 }
238 }
239 }
240 }
241 }
242
243 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
244 }
245 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700246 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700247 int firstScreenIndex,
248 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700249 // Lock on the app so that we don't try and get the items while apps are being added
250 LauncherAppState app = LauncherAppState.getInstance();
251 LauncherModel model = app.getModel();
252 boolean found = false;
253 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700254 if (sWorkerThread.getThreadId() != Process.myTid()) {
255 // Flush the LauncherModel worker thread, so that if we just did another
256 // processInstallShortcut, we give it time for its shortcut to get added to the
257 // database (getItemsInLocalCoordinates reads the database)
258 model.flushWorkerThread();
259 }
Winson Chungc9168342013-06-26 14:54:55 -0700260 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700261
262 // Try adding to the workspace screens incrementally, starting at the default or center
263 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700264 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
265 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700266 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700267 int[] tmpCoordinates = new int[2];
268 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700269 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700270 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700271 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700272 }
273 }
274 }
Winson Chungc9168342013-06-26 14:54:55 -0700275 return null;
276 }
277
Winson Chung997a9232013-07-24 15:33:46 -0700278 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
279 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
280 addAndBindAddedApps(context, added, cb);
281 }
282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chung64359a52013-07-08 17:17:08 -0700283 final Callbacks callbacks) {
Winson Chungd64d1762013-08-20 14:37:16 -0700284 Log.w(TAG, "10249126 - addAndBindAddedApps()");
Winson Chung997a9232013-07-24 15:33:46 -0700285 if (added.isEmpty()) {
286 throw new RuntimeException("EMPTY ADDED ARRAY?");
287 }
Winson Chung64359a52013-07-08 17:17:08 -0700288 // Process the newly added applications and add them to the database first
289 Runnable r = new Runnable() {
290 public void run() {
291 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
292 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
293
Winson Chung76828c82013-08-19 15:43:29 -0700294 // Get the list of workspace screens. We need to append to this list and
295 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
296 // called.
297 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
298 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
299 for (Integer i : orderedScreens.keySet()) {
300 long screenId = orderedScreens.get(i);
301 workspaceScreens.add(screenId);
302 }
303
Winson Chung64359a52013-07-08 17:17:08 -0700304 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700305 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700306 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700307 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700308 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700309 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700310
311 // Short-circuit this logic if the icon exists somewhere on the workspace
312 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
313 continue;
314 }
315
316 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700317 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700318 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700319 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700320 if (coords == null) {
Winson Chung64359a52013-07-08 17:17:08 -0700321 LauncherAppState appState = LauncherAppState.getInstance();
322 LauncherProvider lp = appState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700323
324 // If we can't find a valid position, then just add a new screen.
325 // This takes time so we need to re-queue the add until the new
326 // page is added. Create as many screens as necessary to satisfy
327 // the startSearchPageIndex.
328 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700329 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700330 while (numPagesToAdd > 0) {
331 long screenId = lp.generateNewScreenId();
Winson Chung76828c82013-08-19 15:43:29 -0700332 Log.w(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -0700333 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700334 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 addedWorkspaceScreensFinal.add(screenId);
336 numPagesToAdd--;
337 }
Winson Chung76828c82013-08-19 15:43:29 -0700338
Winson Chung64359a52013-07-08 17:17:08 -0700339 // Find the coordinate again
340 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700341 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700342 }
343 if (coords == null) {
344 throw new RuntimeException("Coordinates should not be null");
345 }
346
Winson Chung997a9232013-07-24 15:33:46 -0700347 ShortcutInfo shortcutInfo;
348 if (a instanceof ShortcutInfo) {
349 shortcutInfo = (ShortcutInfo) a;
350 } else if (a instanceof ApplicationInfo) {
351 shortcutInfo = ((ApplicationInfo) a).makeShortcut();
352 } else {
353 throw new RuntimeException("Unexpected info type");
354 }
Winson Chung64359a52013-07-08 17:17:08 -0700355 // Add the shortcut to the db
356 addItemToDatabase(context, shortcutInfo,
357 LauncherSettings.Favorites.CONTAINER_DESKTOP,
358 coords.first, coords.second[0], coords.second[1], false);
359 // Save the ShortcutInfo for binding in the workspace
360 addedShortcutsFinal.add(shortcutInfo);
361 }
362 }
363
Winson Chungd64d1762013-08-20 14:37:16 -0700364 Log.w(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")");
365
Winson Chung76828c82013-08-19 15:43:29 -0700366 // Update the workspace screens
367 updateWorkspaceScreenOrder(context, workspaceScreens);
368
Winson Chung997a9232013-07-24 15:33:46 -0700369 if (!addedShortcutsFinal.isEmpty()) {
370 runOnMainThread(new Runnable() {
371 public void run() {
372 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
373 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
375 long lastScreenId = info.screenId;
376 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
377 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
378 for (ItemInfo i : addedShortcutsFinal) {
379 if (i.screenId == lastScreenId) {
380 addAnimated.add(i);
381 } else {
382 addNotAnimated.add(i);
383 }
384 }
Winson Chungd64d1762013-08-20 14:37:16 -0700385 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
386 addNotAnimated, addAnimated);
Winson Chung997a9232013-07-24 15:33:46 -0700387 }
Winson Chung64359a52013-07-08 17:17:08 -0700388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 });
390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 }
392 };
393 runOnWorkerThread(r);
394 }
395
Joe Onorato56d82912010-03-07 14:32:10 -0500396 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Winson Chung81b52252012-08-27 15:34:29 -0700400 public void unbindItemInfosAndClearQueuedBindRunnables() {
401 if (sWorkerThread.getThreadId() == Process.myTid()) {
402 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
403 "main thread");
404 }
405
406 // Clear any deferred bind runnables
407 mDeferredBindRunnables.clear();
408 // Remove any queued bind runnables
409 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
410 // Unbind all the workspace items
411 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700412 }
413
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700414 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700415 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700416 // Ensure that we don't use the same workspace items data structure on the main thread
417 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
419 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700420 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
422 tmpAppWidgets.addAll(sBgAppWidgets);
423 }
424 Runnable r = new Runnable() {
425 @Override
426 public void run() {
427 for (ItemInfo item : tmpWorkspaceItems) {
428 item.unbind();
429 }
430 for (ItemInfo item : tmpAppWidgets) {
431 item.unbind();
432 }
433 }
434 };
435 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700436 }
437
Joe Onorato9c1289c2009-08-17 11:03:03 -0400438 /**
439 * Adds an item to the DB if it was not created previously, or move it to a new
440 * <container, screen, cellX, cellY>
441 */
442 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700443 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 if (item.container == ItemInfo.NO_ID) {
445 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 } else {
448 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451 }
452
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700453 static void checkItemInfoLocked(
454 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
455 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
456 if (modelItem != null && item != modelItem) {
457 // check all the data is consistent
458 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
459 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
460 ShortcutInfo shortcut = (ShortcutInfo) item;
461 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
462 modelShortcut.intent.filterEquals(shortcut.intent) &&
463 modelShortcut.id == shortcut.id &&
464 modelShortcut.itemType == shortcut.itemType &&
465 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700466 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700467 modelShortcut.cellX == shortcut.cellX &&
468 modelShortcut.cellY == shortcut.cellY &&
469 modelShortcut.spanX == shortcut.spanX &&
470 modelShortcut.spanY == shortcut.spanY &&
471 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
472 (modelShortcut.dropPos != null &&
473 shortcut.dropPos != null &&
474 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
475 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
476 // For all intents and purposes, this is the same object
477 return;
478 }
479 }
480
481 // the modelItem needs to match up perfectly with item if our model is
482 // to be consistent with the database-- for now, just require
483 // modelItem == item or the equality check above
484 String msg = "item: " + ((item != null) ? item.toString() : "null") +
485 "modelItem: " +
486 ((modelItem != null) ? modelItem.toString() : "null") +
487 "Error: ItemInfo passed to checkItemInfo doesn't match original";
488 RuntimeException e = new RuntimeException(msg);
489 if (stackTrace != null) {
490 e.setStackTrace(stackTrace);
491 }
Adam Cohene25af792013-06-06 23:08:25 -0700492 // TODO: something breaks this in the upgrade path
493 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700494 }
495 }
496
Michael Jurka816474f2012-06-25 14:49:02 -0700497 static void checkItemInfo(final ItemInfo item) {
498 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
499 final long itemId = item.id;
500 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700501 public void run() {
502 synchronized (sBgLock) {
503 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700504 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 }
506 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700507 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700508 }
509
Michael Jurkac9d95c52011-08-29 14:03:34 -0700510 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
511 final ItemInfo item, final String callingFunction) {
512 final long itemId = item.id;
513 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
514 final ContentResolver cr = context.getContentResolver();
515
Adam Cohen487f7dd2012-06-28 18:12:10 -0700516 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700517 Runnable r = new Runnable() {
518 public void run() {
519 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700520 updateItemArrays(item, itemId, stackTrace);
521 }
522 };
523 runOnWorkerThread(r);
524 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700526 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
527 final ArrayList<ItemInfo> items, final String callingFunction) {
528 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700529
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
531 Runnable r = new Runnable() {
532 public void run() {
533 ArrayList<ContentProviderOperation> ops =
534 new ArrayList<ContentProviderOperation>();
535 int count = items.size();
536 for (int i = 0; i < count; i++) {
537 ItemInfo item = items.get(i);
538 final long itemId = item.id;
539 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
540 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700541
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700542 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
543 updateItemArrays(item, itemId, stackTrace);
544
545 }
546 try {
547 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
548 } catch (Exception e) {
549 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700550 }
551 }
552 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700555
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700556 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
557 // Lock on mBgLock *after* the db operation
558 synchronized (sBgLock) {
559 checkItemInfoLocked(itemId, item, stackTrace);
560
561 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
562 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
563 // Item is in a folder, make sure this folder exists
564 if (!sBgFolders.containsKey(item.container)) {
565 // An items container is being set to a that of an item which is not in
566 // the list of Folders.
567 String msg = "item: " + item + " container being set to: " +
568 item.container + ", not in the list of folders";
569 Log.e(TAG, msg);
570 Launcher.dumpDebugLogsToConsole();
571 }
572 }
573
574 // Items are added/removed from the corresponding FolderInfo elsewhere, such
575 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
576 // that are on the desktop, as appropriate
577 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
578 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
579 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
580 switch (modelItem.itemType) {
581 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
582 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
583 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
584 if (!sBgWorkspaceItems.contains(modelItem)) {
585 sBgWorkspaceItems.add(modelItem);
586 }
587 break;
588 default:
589 break;
590 }
591 } else {
592 sBgWorkspaceItems.remove(modelItem);
593 }
594 }
595 }
596
Michael Jurkac7700af2013-05-14 20:17:58 +0200597 public void flushWorkerThread() {
598 mFlushingWorkerThread = true;
599 Runnable waiter = new Runnable() {
600 public void run() {
601 synchronized (this) {
602 notifyAll();
603 mFlushingWorkerThread = false;
604 }
605 }
606 };
607
608 synchronized(waiter) {
609 runOnWorkerThread(waiter);
610 if (mLoaderTask != null) {
611 synchronized(mLoaderTask) {
612 mLoaderTask.notify();
613 }
614 }
615 boolean success = false;
616 while (!success) {
617 try {
618 waiter.wait();
619 success = true;
620 } catch (InterruptedException e) {
621 }
622 }
623 }
624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700628 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700629 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700631 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
633 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700634 Launcher.sDumpLogs.add(transaction);
635 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.cellX = cellX;
638 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 // We store hotseat items in canonical form which is this orientation invariant position
641 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
649 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
652 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700656 }
657
658 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
660 * cellX, cellY have already been updated on the ItemInfos.
661 */
662 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
663 final long container, final int screen) {
664
665 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
666 int count = items.size();
667
668 for (int i = 0; i < count; i++) {
669 ItemInfo item = items.get(i);
670 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700671 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
673 + item.cellX + ", " + item.cellY + ")";
674 Launcher.sDumpLogs.add(transaction);
675 item.container = container;
676
677 // We store hotseat items in canonical form which is this orientation invariant position
678 // in the hotseat
679 if (context instanceof Launcher && screen < 0 &&
680 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 item.cellY);
683 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685 }
686
687 final ContentValues values = new ContentValues();
688 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
689 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
690 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692
693 contentValues.add(values);
694 }
695 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
696 }
697
698 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800700 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700703 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
705 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700706 Launcher.sDumpLogs.add(transaction);
707 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800708 item.cellX = cellX;
709 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 item.spanX = spanX;
711 item.spanY = spanY;
712
713 // We store hotseat items in canonical form which is this orientation invariant position
714 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 }
Adam Cohend4844c32011-02-18 19:25:06 -0800721
Adam Cohend4844c32011-02-18 19:25:06 -0800722 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800723 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700724 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
725 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
726 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
727 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800729
Michael Jurka816474f2012-06-25 14:49:02 -0700730 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700731 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700732
733 /**
734 * Update an item to the database in a specified container.
735 */
736 static void updateItemInDatabase(Context context, final ItemInfo item) {
737 final ContentValues values = new ContentValues();
738 item.onAddToDatabase(values);
739 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
740 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800741 }
742
743 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 * Returns true if the shortcuts already exists in the database.
745 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 static boolean shortcutExists(Context context, String title, Intent intent) {
748 final ContentResolver cr = context.getContentResolver();
749 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
750 new String[] { "title", "intent" }, "title=? and intent=?",
751 new String[] { title, intent.toUri(0) }, null);
752 boolean result = false;
753 try {
754 result = c.moveToFirst();
755 } finally {
756 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700759 }
760
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 * Returns an ItemInfo array containing all the items in the LauncherModel.
763 * The ItemInfo.id is not set through this function.
764 */
765 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
766 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
767 final ContentResolver cr = context.getContentResolver();
768 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
769 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
770 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
771 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
772
773 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
774 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
775 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
776 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
777 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
778 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
779 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
780
781 try {
782 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700783 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 item.cellX = c.getInt(cellXIndex);
785 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700786 item.spanX = Math.max(1, c.getInt(spanXIndex));
787 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700788 item.container = c.getInt(containerIndex);
789 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700791
792 items.add(item);
793 }
794 } catch (Exception e) {
795 items.clear();
796 } finally {
797 c.close();
798 }
799
800 return items;
801 }
802
803 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
805 */
806 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
807 final ContentResolver cr = context.getContentResolver();
808 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
809 "_id=? and (itemType=? or itemType=?)",
810 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700811 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 try {
814 if (c.moveToFirst()) {
815 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
816 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
817 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
818 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
819 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
820 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 FolderInfo folderInfo = null;
823 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700824 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
825 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700827 }
828
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 folderInfo.title = c.getString(titleIndex);
830 folderInfo.id = id;
831 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700832 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700833 folderInfo.cellX = c.getInt(cellXIndex);
834 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835
836 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 } finally {
839 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700840 }
841
842 return null;
843 }
844
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 /**
846 * Add an item to the database in a specified container. Sets the container, screen, cellX and
847 * cellY fields of the item. Also assigns an ID to the item.
848 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 item.cellX = cellX;
853 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 // We store hotseat items in canonical form which is this orientation invariant position
855 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862
863 final ContentValues values = new ContentValues();
864 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 item.onAddToDatabase(values);
866
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400867 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohendcd297f2013-06-18 13:13:40 -0700868 item.id = app.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700869 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700871
Michael Jurkac9d95c52011-08-29 14:03:34 -0700872 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700873 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700874 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700876 + cellY + ")";
877 Launcher.sDumpLogs.add(transaction);
878 Log.d(TAG, transaction);
879
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700880 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
881 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700884 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700885 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 sBgItemsIdMap.put(item.id, item);
887 switch (item.itemType) {
888 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
889 sBgFolders.put(item.id, (FolderInfo) item);
890 // Fall through
891 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
892 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
893 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
894 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
895 sBgWorkspaceItems.add(item);
896 } else {
897 if (!sBgFolders.containsKey(item.container)) {
898 // Adding an item to a folder that doesn't exist.
899 String msg = "adding item: " + item + " to a folder that " +
900 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700901 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700902 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 break;
906 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
907 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
908 break;
909 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700910 }
911 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700914 }
915
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700917 * Creates a new unique child id, for a given cell span across all layouts.
918 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700919 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700923 }
924
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700926 * Removes the specified item from the database
927 * @param context
928 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700930 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700932 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700933
Michael Jurka83df1882011-08-31 20:59:26 -0700934 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700935 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700936 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700938 ", " + item.cellY + ")";
939 Launcher.sDumpLogs.add(transaction);
940 Log.d(TAG, transaction);
941
Michael Jurkac9d95c52011-08-29 14:03:34 -0700942 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943
944 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700945 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700946 switch (item.itemType) {
947 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
948 sBgFolders.remove(item.id);
949 for (ItemInfo info: sBgItemsIdMap.values()) {
950 if (info.container == item.id) {
951 // We are deleting a folder which still contains items that
952 // think they are contained by that folder.
953 String msg = "deleting a folder (" + item + ") which still " +
954 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700955 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700958 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700959 sBgWorkspaceItems.remove(item);
960 break;
961 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
962 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
963 sBgWorkspaceItems.remove(item);
964 break;
965 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
966 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
967 break;
968 }
969 sBgItemsIdMap.remove(item.id);
970 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700971 }
972 }
Michael Jurka83df1882011-08-31 20:59:26 -0700973 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 }
976
977 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 * Update the order of the workspace screens in the database. The array list contains
979 * a list of screen ids in the order that they should appear.
980 */
Winson Chungc9168342013-06-26 14:54:55 -0700981 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700982 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 final ContentResolver cr = context.getContentResolver();
984 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
985
986 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700987 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 while (iter.hasNext()) {
989 long id = iter.next();
990 if (id < 0) {
991 iter.remove();
992 }
993 }
994
995 Runnable r = new Runnable() {
996 @Override
997 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 // Clear the table
999 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001000 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 ContentValues[] values = new ContentValues[count];
1002 for (int i = 0; i < count; i++) {
1003 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001004 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1006 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Winson Chung76828c82013-08-19 15:43:29 -07001007 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 values[i] = v;
1009 }
1010 cr.bulkInsert(uri, values);
1011 sBgWorkspaceScreens.clear();
1012 sBgWorkspaceScreens.addAll(screensCopy);
1013 }
1014 };
1015 runOnWorkerThread(r);
1016 }
1017
1018 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019 * Remove the contents of the specified folder from the database
1020 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 final ContentResolver cr = context.getContentResolver();
1023
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 Runnable r = new Runnable() {
1025 public void run() {
1026 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001028 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001029 sBgItemsIdMap.remove(info.id);
1030 sBgFolders.remove(info.id);
1031 sBgDbIconCache.remove(info);
1032 sBgWorkspaceItems.remove(info);
1033 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001034
Michael Jurkac9d95c52011-08-29 14:03:34 -07001035 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1036 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001037 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001038 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001039 for (ItemInfo childInfo : info.contents) {
1040 sBgItemsIdMap.remove(childInfo.id);
1041 sBgDbIconCache.remove(childInfo);
1042 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001043 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001044 }
1045 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001046 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 }
1048
1049 /**
1050 * Set this as the current Launcher activity object for the loader.
1051 */
1052 public void initialize(Callbacks callbacks) {
1053 synchronized (mLock) {
1054 mCallbacks = new WeakReference<Callbacks>(callbacks);
1055 }
1056 }
1057
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001058 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1060 * ACTION_PACKAGE_CHANGED.
1061 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001062 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001063 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001064 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001065
Joe Onorato36115782010-06-17 13:28:48 -04001066 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001067
Joe Onorato36115782010-06-17 13:28:48 -04001068 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1069 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1070 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1071 final String packageName = intent.getData().getSchemeSpecificPart();
1072 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073
Joe Onorato36115782010-06-17 13:28:48 -04001074 int op = PackageUpdatedTask.OP_NONE;
1075
1076 if (packageName == null || packageName.length() == 0) {
1077 // they sent us a bad intent
1078 return;
1079 }
1080
1081 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1082 op = PackageUpdatedTask.OP_UPDATE;
1083 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1084 if (!replacing) {
1085 op = PackageUpdatedTask.OP_REMOVE;
1086 }
1087 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1088 // later, we will update the package at this time
1089 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1090 if (!replacing) {
1091 op = PackageUpdatedTask.OP_ADD;
1092 } else {
1093 op = PackageUpdatedTask.OP_UPDATE;
1094 }
1095 }
1096
1097 if (op != PackageUpdatedTask.OP_NONE) {
1098 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1099 }
1100
1101 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001102 // First, schedule to add these apps back in.
1103 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1104 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1105 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001106 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001107 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1108 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1109 enqueuePackageUpdated(new PackageUpdatedTask(
1110 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001111 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001112 // If we have changed locale we need to clear out the labels in all apps/workspace.
1113 forceReload();
1114 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1115 // Check if configuration change was an mcc/mnc change which would affect app resources
1116 // and we would need to clear out the labels in all apps/workspace. Same handling as
1117 // above for ACTION_LOCALE_CHANGED
1118 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001119 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001120 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001121 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001122 forceReload();
1123 }
1124 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001125 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001126 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1127 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001128 if (mCallbacks != null) {
1129 Callbacks callbacks = mCallbacks.get();
1130 if (callbacks != null) {
1131 callbacks.bindSearchablesChanged();
1132 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001133 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001134 }
1135 }
1136
Reena Lee93f824a2011-09-23 17:20:28 -07001137 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001138 resetLoadedState(true, true);
1139
Reena Lee93f824a2011-09-23 17:20:28 -07001140 // Do this here because if the launcher activity is running it will be restarted.
1141 // If it's not running startLoaderFromBackground will merely tell it that it needs
1142 // to reload.
1143 startLoaderFromBackground();
1144 }
1145
Winson Chungf0c6ae02012-03-21 16:10:31 -07001146 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1147 synchronized (mLock) {
1148 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1149 // mWorkspaceLoaded to true later
1150 stopLoaderLocked();
1151 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1152 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1153 }
1154 }
1155
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001156 /**
1157 * When the launcher is in the background, it's possible for it to miss paired
1158 * configuration changes. So whenever we trigger the loader from the background
1159 * tell the launcher that it needs to re-run the loader when it comes back instead
1160 * of doing it now.
1161 */
1162 public void startLoaderFromBackground() {
1163 boolean runLoader = false;
1164 if (mCallbacks != null) {
1165 Callbacks callbacks = mCallbacks.get();
1166 if (callbacks != null) {
1167 // Only actually run the loader if they're not paused.
1168 if (!callbacks.setLoadOnResume()) {
1169 runLoader = true;
1170 }
1171 }
1172 }
1173 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001175 }
Joe Onorato36115782010-06-17 13:28:48 -04001176 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001177
Reena Lee93f824a2011-09-23 17:20:28 -07001178 // If there is already a loader task running, tell it to stop.
1179 // returns true if isLaunching() was true on the old task
1180 private boolean stopLoaderLocked() {
1181 boolean isLaunching = false;
1182 LoaderTask oldTask = mLoaderTask;
1183 if (oldTask != null) {
1184 if (oldTask.isLaunching()) {
1185 isLaunching = true;
1186 }
1187 oldTask.stopLocked();
1188 }
1189 return isLaunching;
1190 }
1191
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001193 synchronized (mLock) {
1194 if (DEBUG_LOADERS) {
1195 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 // Clear any deferred bind-runnables from the synchronized load process
1199 // We must do this before any loading/binding is scheduled below.
1200 mDeferredBindRunnables.clear();
1201
Joe Onorato36115782010-06-17 13:28:48 -04001202 // Don't bother to start the thread if we know it's not going to do anything
1203 if (mCallbacks != null && mCallbacks.get() != null) {
1204 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001205 // also, don't downgrade isLaunching if we're already running
1206 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001207 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1209 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1210 } else {
1211 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1212 sWorker.post(mLoaderTask);
1213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 }
1216 }
1217
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001218 void bindRemainingSynchronousPages() {
1219 // Post the remaining side pages to be loaded
1220 if (!mDeferredBindRunnables.isEmpty()) {
1221 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001222 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001223 }
1224 mDeferredBindRunnables.clear();
1225 }
1226 }
1227
Joe Onorato36115782010-06-17 13:28:48 -04001228 public void stopLoader() {
1229 synchronized (mLock) {
1230 if (mLoaderTask != null) {
1231 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
1233 }
Joe Onorato36115782010-06-17 13:28:48 -04001234 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001235
Winson Chung76828c82013-08-19 15:43:29 -07001236 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1237 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1238 final ContentResolver contentResolver = context.getContentResolver();
1239 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1240 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1241 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1242
1243 try {
1244 final int idIndex = sc.getColumnIndexOrThrow(
1245 LauncherSettings.WorkspaceScreens._ID);
1246 final int rankIndex = sc.getColumnIndexOrThrow(
1247 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1248 while (sc.moveToNext()) {
1249 try {
1250 long screenId = sc.getLong(idIndex);
1251 int rank = sc.getInt(rankIndex);
1252
1253 Log.w(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")");
1254
1255 orderedScreens.put(rank, screenId);
1256 } catch (Exception e) {
1257 Log.w(TAG, "Desktop items loading interrupted - invalid screens: ", e);
1258 }
1259 }
1260 } finally {
1261 sc.close();
1262 }
1263 return orderedScreens;
1264 }
1265
Michael Jurkac57b7a82011-08-09 22:02:20 -07001266 public boolean isAllAppsLoaded() {
1267 return mAllAppsLoaded;
1268 }
1269
Winson Chung36a62fe2012-05-06 18:04:42 -07001270 boolean isLoadingWorkspace() {
1271 synchronized (mLock) {
1272 if (mLoaderTask != null) {
1273 return mLoaderTask.isLoadingWorkspace();
1274 }
1275 }
1276 return false;
1277 }
1278
Joe Onorato36115782010-06-17 13:28:48 -04001279 /**
1280 * Runnable for the thread that loads the contents of the launcher:
1281 * - workspace icons
1282 * - widgets
1283 * - all apps icons
1284 */
1285 private class LoaderTask implements Runnable {
1286 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001287 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001288 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001289 private boolean mStopped;
1290 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001291
Winson Chungc3eecff2011-07-11 17:44:15 -07001292 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001293
1294 LoaderTask(Context context, boolean isLaunching) {
1295 mContext = context;
1296 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001297 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 }
1299
Joe Onorato36115782010-06-17 13:28:48 -04001300 boolean isLaunching() {
1301 return mIsLaunching;
1302 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303
Winson Chung36a62fe2012-05-06 18:04:42 -07001304 boolean isLoadingWorkspace() {
1305 return mIsLoadingAndBindingWorkspace;
1306 }
1307
Winson Chungc763c4e2013-07-19 13:49:06 -07001308 /** Returns whether this is an upgrade path */
1309 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001310 mIsLoadingAndBindingWorkspace = true;
1311
Joe Onorato36115782010-06-17 13:28:48 -04001312 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001313 if (DEBUG_LOADERS) {
1314 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001316
Winson Chungc763c4e2013-07-19 13:49:06 -07001317 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001318 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001319 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001320 synchronized (LoaderTask.this) {
1321 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001322 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001323 }
1324 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 }
1326 }
1327
Joe Onorato36115782010-06-17 13:28:48 -04001328 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001329 bindWorkspace(-1, isUpgradePath);
1330 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001331 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001332
Joe Onorato36115782010-06-17 13:28:48 -04001333 private void waitForIdle() {
1334 // Wait until the either we're stopped or the other threads are done.
1335 // This way we don't start loading all apps until the workspace has settled
1336 // down.
1337 synchronized (LoaderTask.this) {
1338 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001339
Joe Onorato36115782010-06-17 13:28:48 -04001340 mHandler.postIdle(new Runnable() {
1341 public void run() {
1342 synchronized (LoaderTask.this) {
1343 mLoadAndBindStepFinished = true;
1344 if (DEBUG_LOADERS) {
1345 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001346 }
Joe Onorato36115782010-06-17 13:28:48 -04001347 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001348 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 });
1351
Michael Jurkac7700af2013-05-14 20:17:58 +02001352 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001353 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001354 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1355 // wait no longer than 1sec at a time
1356 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001357 } catch (InterruptedException ex) {
1358 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001359 }
1360 }
Joe Onorato36115782010-06-17 13:28:48 -04001361 if (DEBUG_LOADERS) {
1362 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001363 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001364 + "ms for previous step to finish binding");
1365 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001366 }
Joe Onorato36115782010-06-17 13:28:48 -04001367 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001368
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001369 void runBindSynchronousPage(int synchronousBindPage) {
1370 if (synchronousBindPage < 0) {
1371 // Ensure that we have a valid page index to load synchronously
1372 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1373 "valid page index");
1374 }
1375 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1376 // Ensure that we don't try and bind a specified page when the pages have not been
1377 // loaded already (we should load everything asynchronously in that case)
1378 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1379 }
1380 synchronized (mLock) {
1381 if (mIsLoaderTaskRunning) {
1382 // Ensure that we are never running the background loading at this point since
1383 // we also touch the background collections
1384 throw new RuntimeException("Error! Background loading is already running");
1385 }
1386 }
1387
1388 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1389 // data structures, we can't allow any other thread to touch that data, but because
1390 // this call is synchronous, we can get away with not locking).
1391
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001392 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001393 // operations from the previous activity. We need to ensure that all queued operations
1394 // are executed before any synchronous binding work is done.
1395 mHandler.flush();
1396
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 // Divide the set of loaded items into those that we are binding synchronously, and
1398 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001399 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1401 // arise from that.
1402 onlyBindAllApps();
1403 }
1404
Joe Onorato36115782010-06-17 13:28:48 -04001405 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001406 boolean isUpgrade = false;
1407
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001408 synchronized (mLock) {
1409 mIsLoaderTaskRunning = true;
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 // Optimize for end-user experience: if the Launcher is up and // running with the
1412 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1413 // workspace first (default).
1414 final Callbacks cbk = mCallbacks.get();
Joe Onorato36115782010-06-17 13:28:48 -04001415 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001416 // Elevate priority when Home launches for the first time to avoid
1417 // starving at boot time. Staring at a blank home is not cool.
1418 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001419 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1420 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001421 android.os.Process.setThreadPriority(mIsLaunching
1422 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1423 }
Winson Chung64359a52013-07-08 17:17:08 -07001424 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001425 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001426
Joe Onorato36115782010-06-17 13:28:48 -04001427 if (mStopped) {
1428 break keep_running;
1429 }
1430
1431 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1432 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001433 synchronized (mLock) {
1434 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001435 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001436 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1437 }
1438 }
Joe Onorato36115782010-06-17 13:28:48 -04001439 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001440
1441 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001442 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1443 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001444
1445 // Restore the default thread priority after we are done loading items
1446 synchronized (mLock) {
1447 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1448 }
Joe Onorato36115782010-06-17 13:28:48 -04001449 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450
Winson Chungaac01e12011-08-17 10:37:13 -07001451 // Update the saved icons if necessary
1452 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001453 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 for (Object key : sBgDbIconCache.keySet()) {
1455 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1456 }
1457 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001458 }
Winson Chungaac01e12011-08-17 10:37:13 -07001459
Winson Chungc763c4e2013-07-19 13:49:06 -07001460 // Ensure that all the applications that are in the system are represented on the home
1461 // screen.
Daniel Sandler8707e0f2013-08-15 15:54:18 -07001462 Log.w(TAG, "10249126 - verifyApplications - useMoreApps="
1463 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade);
1464 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001465 verifyApplications();
1466 }
1467
Joe Onorato36115782010-06-17 13:28:48 -04001468 // Clear out this reference, otherwise we end up holding it until all of the
1469 // callback runnables are done.
1470 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001471
Joe Onorato36115782010-06-17 13:28:48 -04001472 synchronized (mLock) {
1473 // If we are still the last one to be scheduled, remove ourselves.
1474 if (mLoaderTask == this) {
1475 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001478 }
Joe Onorato36115782010-06-17 13:28:48 -04001479 }
1480
1481 public void stopLocked() {
1482 synchronized (LoaderTask.this) {
1483 mStopped = true;
1484 this.notify();
1485 }
1486 }
1487
1488 /**
1489 * Gets the callbacks object. If we've been stopped, or if the launcher object
1490 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1491 * object that was around when the deferred message was scheduled, and if there's
1492 * a new Callbacks object around then also return null. This will save us from
1493 * calling onto it with data that will be ignored.
1494 */
1495 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1496 synchronized (mLock) {
1497 if (mStopped) {
1498 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001499 }
Joe Onorato36115782010-06-17 13:28:48 -04001500
1501 if (mCallbacks == null) {
1502 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504
1505 final Callbacks callbacks = mCallbacks.get();
1506 if (callbacks != oldCallbacks) {
1507 return null;
1508 }
1509 if (callbacks == null) {
1510 Log.w(TAG, "no mCallbacks");
1511 return null;
1512 }
1513
1514 return callbacks;
1515 }
1516 }
1517
Winson Chungc763c4e2013-07-19 13:49:06 -07001518 private void verifyApplications() {
1519 final Context context = mApp.getContext();
1520
1521 // Cross reference all the applications in our apps list with items in the workspace
1522 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001523 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001524 synchronized (sBgLock) {
1525 for (ApplicationInfo app : mBgAllAppsList.data) {
1526 tmpInfos = getItemInfoForComponentName(app.componentName);
Winson Chung76828c82013-08-19 15:43:29 -07001527 Log.w(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty());
Winson Chungc763c4e2013-07-19 13:49:06 -07001528 if (tmpInfos.isEmpty()) {
1529 // We are missing an application icon, so add this to the workspace
1530 added.add(app);
1531 // This is a rare event, so lets log it
1532 Log.e(TAG, "Missing Application on load: " + app);
1533 }
1534 }
1535 }
1536 if (!added.isEmpty()) {
1537 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1538 addAndBindAddedApps(context, added, cb);
1539 }
1540 }
1541
Winson Chung5f8afe62013-08-12 16:19:28 -07001542 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001543 LauncherAppState app = LauncherAppState.getInstance();
1544 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1545 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1546 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001547 }
1548
Joe Onorato36115782010-06-17 13:28:48 -04001549 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001551 LauncherAppState app = LauncherAppState.getInstance();
1552 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1553 int countX = (int) grid.numColumns;
1554 int countY = (int) grid.numRows;
1555
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001557 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1559 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1560 [(int) item.screenId][0] != null) {
1561 Log.e(TAG, "Error loading shortcut into hotseat " + item
1562 + " into position (" + item.screenId + ":" + item.cellX + ","
1563 + item.cellY + ") occupied by "
1564 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1565 [(int) item.screenId][0]);
1566 return false;
1567 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001568 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001569 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 items[(int) item.screenId][0] = item;
1571 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001572 return true;
1573 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001574 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1575 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001576 return true;
1577 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001578
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001580 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 occupied.put(item.screenId, items);
1582 }
1583
1584 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001585 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001586 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1587 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001589 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001591 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001592 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001594 return false;
1595 }
1596 }
1597 }
1598 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1599 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001600 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001601 }
1602 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603
Joe Onorato36115782010-06-17 13:28:48 -04001604 return true;
1605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606
Winson Chungc763c4e2013-07-19 13:49:06 -07001607 /** Returns whether this is an upgradge path */
1608 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001609 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001610
Joe Onorato36115782010-06-17 13:28:48 -04001611 final Context context = mContext;
1612 final ContentResolver contentResolver = context.getContentResolver();
1613 final PackageManager manager = context.getPackageManager();
1614 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1615 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001616
Winson Chung892c74d2013-08-22 16:15:50 -07001617 LauncherAppState app = LauncherAppState.getInstance();
1618 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1619 int countX = (int) grid.numColumns;
1620 int countY = (int) grid.numRows;
1621
Michael Jurkab85f8a42012-04-25 15:48:32 -07001622 // Make sure the default workspace is loaded, if needed
Winson Chungc763c4e2013-07-19 13:49:06 -07001623 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001624
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 // Check if we need to do any upgrade-path logic
1626 boolean loadedOldDb = mApp.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001627
Winson Chung2abf94d2012-07-18 18:16:38 -07001628 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001629 sBgWorkspaceItems.clear();
1630 sBgAppWidgets.clear();
1631 sBgFolders.clear();
1632 sBgItemsIdMap.clear();
1633 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001634 sBgWorkspaceScreens.clear();
Winson Chung76828c82013-08-19 15:43:29 -07001635 Log.w(TAG, "10249126 - loadWorkspace()");
Romain Guy5c16f3e2010-01-12 17:24:58 -08001636
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001638 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001639 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001640
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001641 // +1 for the hotseat (it can be larger than the workspace)
1642 // Load workspace in reverse order to ensure that latest items are loaded first (and
1643 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001644 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001646 try {
1647 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1648 final int intentIndex = c.getColumnIndexOrThrow
1649 (LauncherSettings.Favorites.INTENT);
1650 final int titleIndex = c.getColumnIndexOrThrow
1651 (LauncherSettings.Favorites.TITLE);
1652 final int iconTypeIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.ICON_TYPE);
1654 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1655 final int iconPackageIndex = c.getColumnIndexOrThrow(
1656 LauncherSettings.Favorites.ICON_PACKAGE);
1657 final int iconResourceIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.ICON_RESOURCE);
1659 final int containerIndex = c.getColumnIndexOrThrow(
1660 LauncherSettings.Favorites.CONTAINER);
1661 final int itemTypeIndex = c.getColumnIndexOrThrow(
1662 LauncherSettings.Favorites.ITEM_TYPE);
1663 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1664 LauncherSettings.Favorites.APPWIDGET_ID);
1665 final int screenIndex = c.getColumnIndexOrThrow(
1666 LauncherSettings.Favorites.SCREEN);
1667 final int cellXIndex = c.getColumnIndexOrThrow
1668 (LauncherSettings.Favorites.CELLX);
1669 final int cellYIndex = c.getColumnIndexOrThrow
1670 (LauncherSettings.Favorites.CELLY);
1671 final int spanXIndex = c.getColumnIndexOrThrow
1672 (LauncherSettings.Favorites.SPANX);
1673 final int spanYIndex = c.getColumnIndexOrThrow(
1674 LauncherSettings.Favorites.SPANY);
1675 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1676 //final int displayModeIndex = c.getColumnIndexOrThrow(
1677 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 ShortcutInfo info;
1680 String intentDescription;
1681 LauncherAppWidgetInfo appWidgetInfo;
1682 int container;
1683 long id;
1684 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 while (!mStopped && c.moveToNext()) {
1687 try {
1688 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 switch (itemType) {
1691 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1692 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001693 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 intentDescription = c.getString(intentIndex);
1695 try {
1696 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001697 ComponentName cn = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07001698 if (!isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001699 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001700 // Log the invalid package, and remove it from the db
1701 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1702 false);
Winson Chungee055712013-07-30 14:46:24 -07001703 contentResolver.delete(uri, null, null);
Winson Chung1323b482013-08-05 12:41:55 -07001704 Log.e(TAG, "Invalid package removed: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001705 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001706 // If apps can be on external storage, then we just
1707 // leave them for the user to remove (maybe add
1708 // visual treatment to it)
1709 Log.e(TAG, "Invalid package found: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001710 }
1711 continue;
1712 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 } catch (URISyntaxException e) {
1714 continue;
1715 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001716
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1718 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1719 titleIndex, mLabelCache);
1720 } else {
1721 info = getShortcutInfo(c, context, iconTypeIndex,
1722 iconPackageIndex, iconResourceIndex, iconIndex,
1723 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001724
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 // App shortcuts that used to be automatically added to Launcher
1726 // didn't always have the correct intent flags set, so do that
1727 // here
1728 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001729 intent.getCategories() != null &&
1730 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001731 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 intent.addFlags(
1733 Intent.FLAG_ACTIVITY_NEW_TASK |
1734 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1735 }
Michael Jurka96879562012-03-22 05:54:33 -07001736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001738 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001739 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001741 container = c.getInt(containerIndex);
1742 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001743 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001744 info.cellX = c.getInt(cellXIndex);
1745 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001746 info.spanX = 1;
1747 info.spanY = 1;
1748 // Skip loading items that are out of bounds
1749 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1750 if (checkItemDimensions(info)) {
1751 Log.d(TAG, "Skipped loading out of bounds shortcut: "
1752 + info.intent);
1753 continue;
1754 }
1755 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 // check & update map of what's occupied
1757 if (!checkItemPlacement(occupied, info)) {
1758 break;
1759 }
1760
1761 switch (container) {
1762 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1763 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1764 sBgWorkspaceItems.add(info);
1765 break;
1766 default:
1767 // Item is in a user folder
1768 FolderInfo folderInfo =
1769 findOrMakeFolder(sBgFolders, container);
1770 folderInfo.add(info);
1771 break;
1772 }
1773 sBgItemsIdMap.put(info.id, info);
1774
1775 // now that we've loaded everthing re-save it with the
1776 // icon in case it disappears somehow.
1777 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001778 } else {
1779 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 }
1781 break;
1782
1783 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1784 id = c.getLong(idIndex);
1785 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1786
1787 folderInfo.title = c.getString(titleIndex);
1788 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001790 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001791 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 folderInfo.cellX = c.getInt(cellXIndex);
1793 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001794 folderInfo.spanX = 1;
1795 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796
Winson Chung5f8afe62013-08-12 16:19:28 -07001797 // Skip loading items that are out of bounds
1798 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1799 int iconSpan = 1;
1800 if (checkItemDimensions(folderInfo)) {
1801 Log.d(TAG, "Skipped loading out of bounds folder");
1802 continue;
1803 }
1804 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001805 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001807 break;
1808 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001809
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1812 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1813 sBgWorkspaceItems.add(folderInfo);
1814 break;
Joe Onorato36115782010-06-17 13:28:48 -04001815 }
Joe Onorato17a89222011-02-08 17:26:11 -08001816
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1818 sBgFolders.put(folderInfo.id, folderInfo);
1819 break;
1820
1821 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1822 // Read all Launcher-specific widget details
1823 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001824 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 final AppWidgetProviderInfo provider =
1827 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 if (!isSafeMode && (provider == null || provider.provider == null ||
1830 provider.provider.getPackageName() == null)) {
1831 String log = "Deleting widget that isn't installed anymore: id="
1832 + id + " appWidgetId=" + appWidgetId;
1833 Log.e(TAG, log);
1834 Launcher.sDumpLogs.add(log);
1835 itemsToRemove.add(id);
1836 } else {
1837 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1838 provider.provider);
1839 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001840 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 appWidgetInfo.cellX = c.getInt(cellXIndex);
1842 appWidgetInfo.cellY = c.getInt(cellYIndex);
1843 appWidgetInfo.spanX = c.getInt(spanXIndex);
1844 appWidgetInfo.spanY = c.getInt(spanYIndex);
1845 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1846 appWidgetInfo.minSpanX = minSpan[0];
1847 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 container = c.getInt(containerIndex);
1850 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1851 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1852 Log.e(TAG, "Widget found where container != " +
1853 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1854 continue;
1855 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856
Adam Cohene25af792013-06-06 23:08:25 -07001857 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001858 // Skip loading items that are out of bounds
1859 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1860 if (checkItemDimensions(appWidgetInfo)) {
1861 Log.d(TAG, "Skipped loading out of bounds app widget");
1862 continue;
1863 }
1864 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 // check & update map of what's occupied
1866 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1867 break;
1868 }
1869 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1870 sBgAppWidgets.add(appWidgetInfo);
1871 }
Joe Onorato36115782010-06-17 13:28:48 -04001872 break;
1873 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 } catch (Exception e) {
1875 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001876 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 }
1878 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001879 if (c != null) {
1880 c.close();
1881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 }
1883
1884 if (itemsToRemove.size() > 0) {
1885 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1886 LauncherSettings.Favorites.CONTENT_URI);
1887 // Remove dead items
1888 for (long id : itemsToRemove) {
1889 if (DEBUG_LOADERS) {
1890 Log.d(TAG, "Removed id = " + id);
1891 }
1892 // Don't notify content observers
1893 try {
1894 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1895 null, null);
1896 } catch (RemoteException e) {
1897 Log.w(TAG, "Could not remove id = " + id);
1898 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001899 }
1900 }
1901
Winson Chungc763c4e2013-07-19 13:49:06 -07001902 if (loadedOldDb) {
Winson Chungd64d1762013-08-20 14:37:16 -07001903 Log.w(TAG, "10249126 - loadWorkspace - loadedOldDb");
Adam Cohendcd297f2013-06-18 13:13:40 -07001904 long maxScreenId = 0;
1905 // If we're importing we use the old screen order.
1906 for (ItemInfo item: sBgItemsIdMap.values()) {
1907 long screenId = item.screenId;
1908 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1909 !sBgWorkspaceScreens.contains(screenId)) {
Winson Chung76828c82013-08-19 15:43:29 -07001910 Log.w(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 sBgWorkspaceScreens.add(screenId);
1912 if (screenId > maxScreenId) {
1913 maxScreenId = screenId;
1914 }
1915 }
1916 }
1917 Collections.sort(sBgWorkspaceScreens);
1918 mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1919 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001920
1921 // Update the max item id after we load an old db
1922 long maxItemId = 0;
1923 // If we're importing we use the old screen order.
1924 for (ItemInfo item: sBgItemsIdMap.values()) {
1925 maxItemId = Math.max(maxItemId, item.id);
1926 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001927 app.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 } else {
Winson Chungd64d1762013-08-20 14:37:16 -07001929 Log.w(TAG, "10249126 - loadWorkspace - !loadedOldDb");
Winson Chung76828c82013-08-19 15:43:29 -07001930 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1931 for (Integer i : orderedScreens.keySet()) {
1932 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 }
1934
1935 // Remove any empty screens
1936 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1937 unusedScreens.addAll(sBgWorkspaceScreens);
1938
1939 for (ItemInfo item: sBgItemsIdMap.values()) {
1940 long screenId = item.screenId;
1941
1942 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1943 unusedScreens.contains(screenId)) {
1944 unusedScreens.remove(screenId);
1945 }
1946 }
1947
1948 // If there are any empty screens remove them, and update.
1949 if (unusedScreens.size() != 0) {
1950 sBgWorkspaceScreens.removeAll(unusedScreens);
1951 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1952 }
1953 }
1954
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001955 if (DEBUG_LOADERS) {
1956 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1957 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001959 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001960 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001961
Daniel Sandler566da102013-06-25 23:43:45 -04001962 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001963 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001965 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001966 line += " | ";
1967 }
Winson Chung892c74d2013-08-22 16:15:50 -07001968 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 }
Joe Onorato36115782010-06-17 13:28:48 -04001971 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 }
Joe Onorato36115782010-06-17 13:28:48 -04001975 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001976 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001977 }
1978
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 /** Filters the set of items who are directly or indirectly (via another container) on the
1980 * specified screen. */
1981 private void filterCurrentWorkspaceItems(int currentScreen,
1982 ArrayList<ItemInfo> allWorkspaceItems,
1983 ArrayList<ItemInfo> currentScreenItems,
1984 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001985 // Purge any null ItemInfos
1986 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1987 while (iter.hasNext()) {
1988 ItemInfo i = iter.next();
1989 if (i == null) {
1990 iter.remove();
1991 }
1992 }
1993
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 // If we aren't filtering on a screen, then the set of items to load is the full set of
1995 // items given.
1996 if (currentScreen < 0) {
1997 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001998 }
1999
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 // Order the set of items by their containers first, this allows use to walk through the
2001 // list sequentially, build up a list of containers that are in the specified screen,
2002 // as well as all items in those containers.
2003 Set<Long> itemsOnScreen = new HashSet<Long>();
2004 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2005 @Override
2006 public int compare(ItemInfo lhs, ItemInfo rhs) {
2007 return (int) (lhs.container - rhs.container);
2008 }
2009 });
2010 for (ItemInfo info : allWorkspaceItems) {
2011 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002013 currentScreenItems.add(info);
2014 itemsOnScreen.add(info.id);
2015 } else {
2016 otherScreenItems.add(info);
2017 }
2018 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2019 currentScreenItems.add(info);
2020 itemsOnScreen.add(info.id);
2021 } else {
2022 if (itemsOnScreen.contains(info.container)) {
2023 currentScreenItems.add(info);
2024 itemsOnScreen.add(info.id);
2025 } else {
2026 otherScreenItems.add(info);
2027 }
2028 }
2029 }
2030 }
2031
2032 /** Filters the set of widgets which are on the specified screen. */
2033 private void filterCurrentAppWidgets(int currentScreen,
2034 ArrayList<LauncherAppWidgetInfo> appWidgets,
2035 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2036 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2037 // If we aren't filtering on a screen, then the set of items to load is the full set of
2038 // widgets given.
2039 if (currentScreen < 0) {
2040 currentScreenWidgets.addAll(appWidgets);
2041 }
2042
2043 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002044 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 currentScreenWidgets.add(widget);
2048 } else {
2049 otherScreenWidgets.add(widget);
2050 }
2051 }
2052 }
2053
2054 /** Filters the set of folders which are on the specified screen. */
2055 private void filterCurrentFolders(int currentScreen,
2056 HashMap<Long, ItemInfo> itemsIdMap,
2057 HashMap<Long, FolderInfo> folders,
2058 HashMap<Long, FolderInfo> currentScreenFolders,
2059 HashMap<Long, FolderInfo> otherScreenFolders) {
2060 // If we aren't filtering on a screen, then the set of items to load is the full set of
2061 // widgets given.
2062 if (currentScreen < 0) {
2063 currentScreenFolders.putAll(folders);
2064 }
2065
2066 for (long id : folders.keySet()) {
2067 ItemInfo info = itemsIdMap.get(id);
2068 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002069 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002071 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 currentScreenFolders.put(id, folder);
2073 } else {
2074 otherScreenFolders.put(id, folder);
2075 }
2076 }
2077 }
2078
2079 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2080 * right) */
2081 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002082 final LauncherAppState app = LauncherAppState.getInstance();
2083 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 // XXX: review this
2085 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002086 @Override
2087 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002088 int cellCountX = (int) grid.numColumns;
2089 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002090 int screenOffset = cellCountX * cellCountY;
2091 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002092 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002093 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002095 rhs.cellY * cellCountX + rhs.cellX);
2096 return (int) (lr - rr);
2097 }
2098 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002100
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2102 final ArrayList<Long> orderedScreens) {
Winson Chung76828c82013-08-19 15:43:29 -07002103 Log.w(TAG, "10249126 - bindWorkspaceScreens()");
Adam Cohendcd297f2013-06-18 13:13:40 -07002104 final Runnable r = new Runnable() {
2105 @Override
2106 public void run() {
2107 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2108 if (callbacks != null) {
2109 callbacks.bindScreens(orderedScreens);
2110 }
2111 }
2112 };
2113 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2114 }
2115
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2117 final ArrayList<ItemInfo> workspaceItems,
2118 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2119 final HashMap<Long, FolderInfo> folders,
2120 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002121
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 final boolean postOnMainThread = (deferredBindRunnables != null);
2123
2124 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002125 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002127 final int start = i;
2128 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 final Runnable r = new Runnable() {
2130 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002132 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002134 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2135 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 }
2137 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 };
2139 if (postOnMainThread) {
2140 deferredBindRunnables.add(r);
2141 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002142 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 }
Joe Onorato36115782010-06-17 13:28:48 -04002144 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145
2146 // Bind the folders
2147 if (!folders.isEmpty()) {
2148 final Runnable r = new Runnable() {
2149 public void run() {
2150 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2151 if (callbacks != null) {
2152 callbacks.bindFolders(folders);
2153 }
2154 }
2155 };
2156 if (postOnMainThread) {
2157 deferredBindRunnables.add(r);
2158 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002159 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 }
2161 }
2162
2163 // Bind the widgets, one at a time
2164 N = appWidgets.size();
2165 for (int i = 0; i < N; i++) {
2166 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2167 final Runnable r = new Runnable() {
2168 public void run() {
2169 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2170 if (callbacks != null) {
2171 callbacks.bindAppWidget(widget);
2172 }
2173 }
2174 };
2175 if (postOnMainThread) {
2176 deferredBindRunnables.add(r);
2177 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002178 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179 }
2180 }
2181 }
2182
2183 /**
2184 * Binds all loaded data to actual views on the main thread.
2185 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002186 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 final long t = SystemClock.uptimeMillis();
2188 Runnable r;
2189
2190 // Don't use these two variables in any of the callback runnables.
2191 // Otherwise we hold a reference to them.
2192 final Callbacks oldCallbacks = mCallbacks.get();
2193 if (oldCallbacks == null) {
2194 // This launcher has exited and nobody bothered to tell us. Just bail.
2195 Log.w(TAG, "LoaderTask running with no launcher");
2196 return;
2197 }
2198
Winson Chung4a2afa32012-07-19 14:53:05 -07002199 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2200 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 oldCallbacks.getCurrentWorkspaceScreen();
2202
2203 // Load all the items that are on the current page first (and in the process, unbind
2204 // all the existing workspace items before we call startBinding() below.
2205 unbindWorkspaceItemsOnMainThread();
2206 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2207 ArrayList<LauncherAppWidgetInfo> appWidgets =
2208 new ArrayList<LauncherAppWidgetInfo>();
2209 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2210 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002212 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 workspaceItems.addAll(sBgWorkspaceItems);
2214 appWidgets.addAll(sBgAppWidgets);
2215 folders.putAll(sBgFolders);
2216 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002217 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 }
2219
2220 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2221 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2222 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2223 new ArrayList<LauncherAppWidgetInfo>();
2224 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2225 new ArrayList<LauncherAppWidgetInfo>();
2226 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2227 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2228
2229 // Separate the items that are on the current screen, and all the other remaining items
2230 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2231 otherWorkspaceItems);
2232 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2233 otherAppWidgets);
2234 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2235 otherFolders);
2236 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2237 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2238
2239 // Tell the workspace that we're about to start binding items
2240 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002241 public void run() {
2242 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2243 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002245 }
2246 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 };
Winson Chung81b52252012-08-27 15:34:29 -07002248 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2251
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002252 // Load items on the current page
2253 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2254 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002255 if (isLoadingSynchronously) {
2256 r = new Runnable() {
2257 public void run() {
2258 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2259 if (callbacks != null) {
2260 callbacks.onPageBoundSynchronously(currentScreen);
2261 }
2262 }
2263 };
Winson Chung81b52252012-08-27 15:34:29 -07002264 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002265 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266
Winson Chung4a2afa32012-07-19 14:53:05 -07002267 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2268 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 mDeferredBindRunnables.clear();
2270 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002271 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272
2273 // Tell the workspace that we're done binding items
2274 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002275 public void run() {
2276 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2277 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002278 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002279 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280
Winson Chung98e030b2012-05-07 16:01:11 -07002281 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002282 if (DEBUG_LOADERS) {
2283 Log.d(TAG, "bound workspace in "
2284 + (SystemClock.uptimeMillis()-t) + "ms");
2285 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002286
2287 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002290 if (isLoadingSynchronously) {
2291 mDeferredBindRunnables.add(r);
2292 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002293 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002294 }
Joe Onorato36115782010-06-17 13:28:48 -04002295 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002296
Joe Onorato36115782010-06-17 13:28:48 -04002297 private void loadAndBindAllApps() {
2298 if (DEBUG_LOADERS) {
2299 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2300 }
2301 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002302 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002303 synchronized (LoaderTask.this) {
2304 if (mStopped) {
2305 return;
2306 }
2307 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002308 }
Joe Onorato36115782010-06-17 13:28:48 -04002309 } else {
2310 onlyBindAllApps();
2311 }
2312 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002313
Joe Onorato36115782010-06-17 13:28:48 -04002314 private void onlyBindAllApps() {
2315 final Callbacks oldCallbacks = mCallbacks.get();
2316 if (oldCallbacks == null) {
2317 // This launcher has exited and nobody bothered to tell us. Just bail.
2318 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2319 return;
2320 }
2321
2322 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002323 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002324 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002325 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002326 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002327 public void run() {
2328 final long t = SystemClock.uptimeMillis();
2329 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2330 if (callbacks != null) {
2331 callbacks.bindAllApplications(list);
2332 }
2333 if (DEBUG_LOADERS) {
2334 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2335 + (SystemClock.uptimeMillis()-t) + "ms");
2336 }
2337 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002338 };
2339 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002340 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002341 r.run();
2342 } else {
2343 mHandler.post(r);
2344 }
Joe Onorato36115782010-06-17 13:28:48 -04002345 }
2346
Winson Chung64359a52013-07-08 17:17:08 -07002347 private void loadAllApps() {
2348 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002349
2350 // Don't use these two variables in any of the callback runnables.
2351 // Otherwise we hold a reference to them.
2352 final Callbacks oldCallbacks = mCallbacks.get();
2353 if (oldCallbacks == null) {
2354 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002355 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002356 return;
2357 }
2358
Winson Chung64359a52013-07-08 17:17:08 -07002359 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002360 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2361 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2362
Winson Chung64359a52013-07-08 17:17:08 -07002363 // Clear the list of apps
2364 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002365
Winson Chung64359a52013-07-08 17:17:08 -07002366 // Query for the set of apps
2367 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2368 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2369 if (DEBUG_LOADERS) {
2370 Log.d(TAG, "queryIntentActivities took "
2371 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2372 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2373 }
2374 // Fail if we don't have any apps
2375 if (apps == null || apps.isEmpty()) {
2376 return;
2377 }
2378 // Sort the applications by name
2379 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2380 Collections.sort(apps,
2381 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2382 if (DEBUG_LOADERS) {
2383 Log.d(TAG, "sort took "
2384 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 }
2386
Winson Chung64359a52013-07-08 17:17:08 -07002387 // Create the ApplicationInfos
2388 final long addTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2389 for (int i = 0; i < apps.size(); i++) {
2390 // This builds the icon bitmaps.
2391 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
2392 mIconCache, mLabelCache));
2393 }
2394
2395 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2396 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2397 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
2398
2399 // Post callback on main thread
2400 mHandler.post(new Runnable() {
2401 public void run() {
2402 final long bindTime = SystemClock.uptimeMillis();
2403 if (callbacks != null) {
2404 callbacks.bindAllApplications(added);
2405 if (DEBUG_LOADERS) {
2406 Log.d(TAG, "bound " + added.size() + " apps in "
2407 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2408 }
2409 } else {
2410 Log.i(TAG, "not binding apps: no Launcher activity");
2411 }
2412 }
2413 });
2414
Joe Onorato36115782010-06-17 13:28:48 -04002415 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002416 Log.d(TAG, "Icons processed in "
2417 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002418 }
2419 }
2420
2421 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002422 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002423 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2424 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2425 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2426 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2427 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2428 }
Joe Onorato36115782010-06-17 13:28:48 -04002429 }
2430 }
2431
2432 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002433 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002434 }
2435
2436 private class PackageUpdatedTask implements Runnable {
2437 int mOp;
2438 String[] mPackages;
2439
2440 public static final int OP_NONE = 0;
2441 public static final int OP_ADD = 1;
2442 public static final int OP_UPDATE = 2;
2443 public static final int OP_REMOVE = 3; // uninstlled
2444 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2445
2446
2447 public PackageUpdatedTask(int op, String[] packages) {
2448 mOp = op;
2449 mPackages = packages;
2450 }
2451
2452 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002453 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002454
2455 final String[] packages = mPackages;
2456 final int N = packages.length;
2457 switch (mOp) {
2458 case OP_ADD:
2459 for (int i=0; i<N; i++) {
2460 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002461 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002462 }
2463 break;
2464 case OP_UPDATE:
2465 for (int i=0; i<N; i++) {
2466 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002467 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002468 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002469 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002470 }
2471 break;
2472 case OP_REMOVE:
2473 case OP_UNAVAILABLE:
2474 for (int i=0; i<N; i++) {
2475 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002476 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002477 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002478 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002479 }
2480 break;
2481 }
2482
2483 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002484 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002485 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002486
Adam Cohen487f7dd2012-06-28 18:12:10 -07002487 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002488 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2489 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002490 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002491 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002492 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2493 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002494 }
Winson Chung5d55f332012-07-16 20:45:03 -07002495 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002496 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002497 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002498 }
2499
Joe Onorato36115782010-06-17 13:28:48 -04002500 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2501 if (callbacks == null) {
2502 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2503 return;
2504 }
2505
2506 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002507 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002508 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002509 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung997a9232013-07-24 15:33:46 -07002510 addAndBindAddedApps(context, addedInfos, cb);
Joe Onorato36115782010-06-17 13:28:48 -04002511 }
2512 if (modified != null) {
2513 final ArrayList<ApplicationInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002514
2515 // Update the launcher db to reflect the changes
2516 for (ApplicationInfo a : modifiedFinal) {
2517 ArrayList<ItemInfo> infos =
2518 getItemInfoForComponentName(a.componentName);
2519 for (ItemInfo i : infos) {
2520 if (isShortcutInfoUpdateable(i)) {
2521 ShortcutInfo info = (ShortcutInfo) i;
2522 info.title = a.title.toString();
2523 updateItemInDatabase(context, info);
2524 }
2525 }
2526 }
2527
Joe Onorato36115782010-06-17 13:28:48 -04002528 mHandler.post(new Runnable() {
2529 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002530 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2531 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002532 callbacks.bindAppsUpdated(modifiedFinal);
2533 }
2534 }
2535 });
2536 }
Winson Chung83892cc2013-05-01 16:53:33 -07002537 // If a package has been removed, or an app has been removed as a result of
2538 // an update (for example), make the removed callback.
2539 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002540 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002541 final ArrayList<String> removedPackageNames =
2542 new ArrayList<String>(Arrays.asList(packages));
2543
Winson Chung64359a52013-07-08 17:17:08 -07002544 // Update the launcher db to reflect the removal of apps
2545 if (packageRemoved) {
2546 for (String pn : removedPackageNames) {
2547 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2548 for (ItemInfo i : infos) {
2549 deleteItemFromDatabase(context, i);
2550 }
2551 }
2552 } else {
2553 for (ApplicationInfo a : removedApps) {
2554 ArrayList<ItemInfo> infos =
2555 getItemInfoForComponentName(a.componentName);
2556 for (ItemInfo i : infos) {
2557 deleteItemFromDatabase(context, i);
2558 }
2559 }
2560 }
2561
Joe Onorato36115782010-06-17 13:28:48 -04002562 mHandler.post(new Runnable() {
2563 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002564 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2565 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002566 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002567 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002568 }
2569 }
2570 });
Joe Onoratobe386092009-11-17 17:32:16 -08002571 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002572
Michael Jurkac402cd92013-05-20 15:49:32 +02002573 final ArrayList<Object> widgetsAndShortcuts =
2574 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002575 mHandler.post(new Runnable() {
2576 @Override
2577 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002578 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2579 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002580 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002581 }
2582 }
2583 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002584 }
2585 }
2586
Michael Jurkac402cd92013-05-20 15:49:32 +02002587 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2588 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2589 PackageManager packageManager = context.getPackageManager();
2590 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2591 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2592 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2593 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2594 Collections.sort(widgetsAndShortcuts,
2595 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2596 return widgetsAndShortcuts;
2597 }
2598
Winson Chung1323b482013-08-05 12:41:55 -07002599 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002600 if (cn == null) {
2601 return false;
2602 }
2603
2604 try {
2605 return (pm.getActivityInfo(cn, 0) != null);
2606 } catch (NameNotFoundException e) {
2607 return false;
2608 }
2609 }
2610
Joe Onorato9c1289c2009-08-17 11:03:03 -04002611 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002612 * This is called from the code that adds shortcuts from the intent receiver. This
2613 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 */
Joe Onorato56d82912010-03-07 14:32:10 -05002615 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002616 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618
Joe Onorato56d82912010-03-07 14:32:10 -05002619 /**
2620 * Make an ShortcutInfo object for a shortcut that is an application.
2621 *
2622 * If c is not null, then it will be used to fill in missing data like the title and icon.
2623 */
2624 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002625 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002626 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002627 final ShortcutInfo info = new ShortcutInfo();
2628 if (!isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002629 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002630 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002631 } else {
2632 try {
2633 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2634 info.initFlagsAndFirstInstallTime(pi);
2635 } catch (NameNotFoundException e) {
2636 Log.d(TAG, "getPackInfo failed for package " +
2637 componentName.getPackageName());
2638 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002639 }
2640
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002641 // TODO: See if the PackageManager knows about this case. If it doesn't
2642 // then return null & delete this.
2643
Joe Onorato56d82912010-03-07 14:32:10 -05002644 // the resource -- This may implicitly give us back the fallback icon,
2645 // but don't worry about that. All we're doing with usingFallbackIcon is
2646 // to avoid saving lots of copies of that in the database, and most apps
2647 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002648
2649 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2650 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2651 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002652 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002653 ResolveInfo resolveInfo = null;
2654 ComponentName oldComponent = intent.getComponent();
2655 Intent newIntent = new Intent(intent.getAction(), null);
2656 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2657 newIntent.setPackage(oldComponent.getPackageName());
2658 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2659 for (ResolveInfo i : infos) {
2660 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2661 i.activityInfo.name);
2662 if (cn.equals(oldComponent)) {
2663 resolveInfo = i;
2664 }
2665 }
2666 if (resolveInfo == null) {
2667 resolveInfo = manager.resolveActivity(intent, 0);
2668 }
Joe Onorato56d82912010-03-07 14:32:10 -05002669 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002670 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002671 }
Joe Onorato56d82912010-03-07 14:32:10 -05002672 // the db
2673 if (icon == null) {
2674 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002675 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002676 }
2677 }
2678 // the fallback icon
2679 if (icon == null) {
2680 icon = getFallbackIcon();
2681 info.usingFallbackIcon = true;
2682 }
2683 info.setIcon(icon);
2684
2685 // from the resource
2686 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002687 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2688 if (labelCache != null && labelCache.containsKey(key)) {
2689 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002690 } else {
2691 info.title = resolveInfo.activityInfo.loadLabel(manager);
2692 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002693 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002694 }
2695 }
Joe Onorato56d82912010-03-07 14:32:10 -05002696 }
2697 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002698 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002699 if (c != null) {
2700 info.title = c.getString(titleIndex);
2701 }
2702 }
2703 // fall back to the class name of the activity
2704 if (info.title == null) {
2705 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2708 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002710
Winson Chung64359a52013-07-08 17:17:08 -07002711 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2712 ItemInfoFilter f) {
2713 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2714 for (ItemInfo i : infos) {
2715 if (i instanceof ShortcutInfo) {
2716 ShortcutInfo info = (ShortcutInfo) i;
2717 ComponentName cn = info.intent.getComponent();
2718 if (cn != null && f.filterItem(null, info, cn)) {
2719 filtered.add(info);
2720 }
2721 } else if (i instanceof FolderInfo) {
2722 FolderInfo info = (FolderInfo) i;
2723 for (ShortcutInfo s : info.contents) {
2724 ComponentName cn = s.intent.getComponent();
2725 if (cn != null && f.filterItem(info, s, cn)) {
2726 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002727 }
2728 }
Winson Chung64359a52013-07-08 17:17:08 -07002729 } else if (i instanceof LauncherAppWidgetInfo) {
2730 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2731 ComponentName cn = info.providerName;
2732 if (cn != null && f.filterItem(null, info, cn)) {
2733 filtered.add(info);
2734 }
Winson Chung8a435102012-08-30 17:16:53 -07002735 }
2736 }
Winson Chung64359a52013-07-08 17:17:08 -07002737 return new ArrayList<ItemInfo>(filtered);
2738 }
2739
2740 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
2741 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2742 ItemInfoFilter filter = new ItemInfoFilter() {
2743 @Override
2744 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2745 return cn.getPackageName().equals(pn);
2746 }
2747 };
2748 return filterItemInfos(sBgItemsIdMap.values(), filter);
2749 }
2750
2751 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
2752 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2753 ItemInfoFilter filter = new ItemInfoFilter() {
2754 @Override
2755 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2756 return cn.equals(cname);
2757 }
2758 };
2759 return filterItemInfos(sBgItemsIdMap.values(), filter);
2760 }
2761
2762 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2763 if (i instanceof ShortcutInfo) {
2764 ShortcutInfo info = (ShortcutInfo) i;
2765 // We need to check for ACTION_MAIN otherwise getComponent() might
2766 // return null for some shortcuts (for instance, for shortcuts to
2767 // web pages.)
2768 Intent intent = info.intent;
2769 ComponentName name = intent.getComponent();
2770 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2771 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2772 return true;
2773 }
2774 }
2775 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002776 }
2777
2778 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002779 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002781 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002782 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2783 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784
Joe Onorato56d82912010-03-07 14:32:10 -05002785 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002786 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002789 // TODO: If there's an explicit component and we can't install that, delete it.
2790
Joe Onorato56d82912010-03-07 14:32:10 -05002791 info.title = c.getString(titleIndex);
2792
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 int iconType = c.getInt(iconTypeIndex);
2794 switch (iconType) {
2795 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2796 String packageName = c.getString(iconPackageIndex);
2797 String resourceName = c.getString(iconResourceIndex);
2798 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002799 info.customIcon = false;
2800 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002802 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002803 if (resources != null) {
2804 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002805 icon = Utilities.createIconBitmap(
2806 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002809 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 }
Joe Onorato56d82912010-03-07 14:32:10 -05002811 // the db
2812 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002813 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002814 }
2815 // the fallback icon
2816 if (icon == null) {
2817 icon = getFallbackIcon();
2818 info.usingFallbackIcon = true;
2819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 break;
2821 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002822 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002823 if (icon == null) {
2824 icon = getFallbackIcon();
2825 info.customIcon = false;
2826 info.usingFallbackIcon = true;
2827 } else {
2828 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 break;
2831 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002832 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002833 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 info.customIcon = false;
2835 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002837 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 return info;
2839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840
Michael Jurka931dc972011-08-05 15:08:15 -07002841 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002842 @SuppressWarnings("all") // suppress dead code warning
2843 final boolean debug = false;
2844 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002845 Log.d(TAG, "getIconFromCursor app="
2846 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2847 }
2848 byte[] data = c.getBlob(iconIndex);
2849 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002850 return Utilities.createIconBitmap(
2851 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002852 } catch (Exception e) {
2853 return null;
2854 }
2855 }
2856
Winson Chung3d503fb2011-07-13 17:25:49 -07002857 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2858 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002859 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002860 if (info == null) {
2861 return null;
2862 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002863 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002864
2865 return info;
2866 }
2867
Winson Chunga9abd0e2010-10-27 17:18:37 -07002868 /**
Winson Chung55cef262010-10-28 14:14:18 -07002869 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2870 */
2871 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2872 ComponentName component) {
2873 List<AppWidgetProviderInfo> widgets =
2874 AppWidgetManager.getInstance(context).getInstalledProviders();
2875 for (AppWidgetProviderInfo info : widgets) {
2876 if (info.provider.equals(component)) {
2877 return info;
2878 }
2879 }
2880 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002881 }
2882
Winson Chung68846fd2010-10-29 11:00:27 -07002883 /**
2884 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2885 */
2886 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2887 final PackageManager packageManager = context.getPackageManager();
2888 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2889 new ArrayList<WidgetMimeTypeHandlerData>();
2890
2891 final Intent supportsIntent =
2892 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2893 supportsIntent.setType(mimeType);
2894
2895 // Create a set of widget configuration components that we can test against
2896 final List<AppWidgetProviderInfo> widgets =
2897 AppWidgetManager.getInstance(context).getInstalledProviders();
2898 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2899 new HashMap<ComponentName, AppWidgetProviderInfo>();
2900 for (AppWidgetProviderInfo info : widgets) {
2901 configurationComponentToWidget.put(info.configure, info);
2902 }
2903
2904 // Run through each of the intents that can handle this type of clip data, and cross
2905 // reference them with the components that are actual configuration components
2906 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2907 PackageManager.MATCH_DEFAULT_ONLY);
2908 for (ResolveInfo info : activities) {
2909 final ActivityInfo activityInfo = info.activityInfo;
2910 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2911 activityInfo.name);
2912 if (configurationComponentToWidget.containsKey(infoComponent)) {
2913 supportedConfigurationActivities.add(
2914 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2915 configurationComponentToWidget.get(infoComponent)));
2916 }
2917 }
2918 return supportedConfigurationActivities;
2919 }
2920
Winson Chunga9abd0e2010-10-27 17:18:37 -07002921 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002922 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2923 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2924 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2925
Adam Cohend9198822011-11-22 16:42:47 -08002926 if (intent == null) {
2927 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2928 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2929 return null;
2930 }
2931
Joe Onorato0589f0f2010-02-08 13:44:00 -08002932 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002933 boolean customIcon = false;
2934 ShortcutIconResource iconResource = null;
2935
2936 if (bitmap != null && bitmap instanceof Bitmap) {
2937 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002938 customIcon = true;
2939 } else {
2940 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2941 if (extra != null && extra instanceof ShortcutIconResource) {
2942 try {
2943 iconResource = (ShortcutIconResource) extra;
2944 final PackageManager packageManager = context.getPackageManager();
2945 Resources resources = packageManager.getResourcesForApplication(
2946 iconResource.packageName);
2947 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002948 icon = Utilities.createIconBitmap(
2949 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002950 } catch (Exception e) {
2951 Log.w(TAG, "Could not load shortcut icon: " + extra);
2952 }
2953 }
2954 }
2955
Michael Jurkac9d95c52011-08-29 14:03:34 -07002956 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002957
2958 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002959 if (fallbackIcon != null) {
2960 icon = fallbackIcon;
2961 } else {
2962 icon = getFallbackIcon();
2963 info.usingFallbackIcon = true;
2964 }
Joe Onorato56d82912010-03-07 14:32:10 -05002965 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002966 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002967
Joe Onorato0589f0f2010-02-08 13:44:00 -08002968 info.title = name;
2969 info.intent = intent;
2970 info.customIcon = customIcon;
2971 info.iconResource = iconResource;
2972
2973 return info;
2974 }
2975
Winson Chungaac01e12011-08-17 10:37:13 -07002976 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2977 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002978 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07002979 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002980 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002981 }
Joe Onorato56d82912010-03-07 14:32:10 -05002982 // If this icon doesn't have a custom icon, check to see
2983 // what's stored in the DB, and if it doesn't match what
2984 // we're going to show, store what we are going to show back
2985 // into the DB. We do this so when we're loading, if the
2986 // package manager can't find an icon (for example because
2987 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002988 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002989 cache.put(info, c.getBlob(iconIndex));
2990 return true;
2991 }
2992 return false;
2993 }
2994 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2995 boolean needSave = false;
2996 try {
2997 if (data != null) {
2998 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2999 Bitmap loaded = info.getIcon(mIconCache);
3000 needSave = !saved.sameAs(loaded);
3001 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003002 needSave = true;
3003 }
Winson Chungaac01e12011-08-17 10:37:13 -07003004 } catch (Exception e) {
3005 needSave = true;
3006 }
3007 if (needSave) {
3008 Log.d(TAG, "going to save icon bitmap for info=" + info);
3009 // This is slower than is ideal, but this only happens once
3010 // or when the app is updated with a new icon.
3011 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003012 }
3013 }
3014
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003016 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 * or make a new one.
3018 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003019 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 // See if a placeholder was created for us already
3021 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003022 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003024 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 folders.put(id, folderInfo);
3026 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003027 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Winson Chung11904872012-09-17 16:58:46 -07003030 public static final Comparator<ApplicationInfo> getAppNameComparator() {
3031 final Collator collator = Collator.getInstance();
3032 return new Comparator<ApplicationInfo>() {
3033 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3034 int result = collator.compare(a.title.toString(), b.title.toString());
3035 if (result == 0) {
3036 result = a.componentName.compareTo(b.componentName);
3037 }
3038 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003039 }
Winson Chung11904872012-09-17 16:58:46 -07003040 };
3041 }
Winson Chung78403fe2011-01-21 15:38:02 -08003042 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
3043 = new Comparator<ApplicationInfo>() {
3044 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3045 if (a.firstInstallTime < b.firstInstallTime) return 1;
3046 if (a.firstInstallTime > b.firstInstallTime) return -1;
3047 return 0;
3048 }
3049 };
Winson Chung11904872012-09-17 16:58:46 -07003050 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3051 final Collator collator = Collator.getInstance();
3052 return new Comparator<AppWidgetProviderInfo>() {
3053 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3054 return collator.compare(a.label.toString(), b.label.toString());
3055 }
3056 };
3057 }
Winson Chung5308f242011-08-18 12:12:41 -07003058 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3059 if (info.activityInfo != null) {
3060 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3061 } else {
3062 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3063 }
3064 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003065 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003066 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003067 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003068 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 ShortcutNameComparator(PackageManager pm) {
3070 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003071 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003072 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003073 }
3074 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3075 mPackageManager = pm;
3076 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003077 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003078 }
3079 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003080 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003081 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3082 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3083 if (mLabelCache.containsKey(keyA)) {
3084 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003085 } else {
3086 labelA = a.loadLabel(mPackageManager).toString();
3087
Winson Chung5308f242011-08-18 12:12:41 -07003088 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003089 }
Winson Chung5308f242011-08-18 12:12:41 -07003090 if (mLabelCache.containsKey(keyB)) {
3091 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003092 } else {
3093 labelB = b.loadLabel(mPackageManager).toString();
3094
Winson Chung5308f242011-08-18 12:12:41 -07003095 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003096 }
Winson Chung11904872012-09-17 16:58:46 -07003097 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003098 }
3099 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003100 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003101 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003102 private PackageManager mPackageManager;
3103 private HashMap<Object, String> mLabelCache;
3104 WidgetAndShortcutNameComparator(PackageManager pm) {
3105 mPackageManager = pm;
3106 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003107 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003108 }
3109 public final int compare(Object a, Object b) {
3110 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003111 if (mLabelCache.containsKey(a)) {
3112 labelA = mLabelCache.get(a);
3113 } else {
3114 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003115 ((AppWidgetProviderInfo) a).label :
3116 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003117 mLabelCache.put(a, labelA);
3118 }
3119 if (mLabelCache.containsKey(b)) {
3120 labelB = mLabelCache.get(b);
3121 } else {
3122 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003123 ((AppWidgetProviderInfo) b).label :
3124 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003125 mLabelCache.put(b, labelB);
3126 }
Winson Chung11904872012-09-17 16:58:46 -07003127 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003128 }
3129 };
Joe Onoratobe386092009-11-17 17:32:16 -08003130
3131 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003132 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07003133 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3134 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3135 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3136 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003137 if (mLoaderTask != null) {
3138 mLoaderTask.dumpState();
3139 } else {
3140 Log.d(TAG, "mLoaderTask=null");
3141 }
Joe Onoratobe386092009-11-17 17:32:16 -08003142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143}