blob: 50e84b70f340222f3fcbc4f021cc6020c3454079 [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
Joe Onorato36115782010-06-17 13:28:48 -040070 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070071 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040072
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074 private final Object mLock = new Object();
75 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040076 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020078 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Winson Chung81b52252012-08-27 15:34:29 -070080 // Specific runnable types that are run on the main thread deferred handler, this allows us to
81 // clear all queued binding runnables when the Launcher activity is destroyed.
82 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
83 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
84
85
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070086 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
87 static {
88 sWorkerThread.start();
89 }
90 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
91
Joe Onoratocc67f472010-06-08 10:54:30 -070092 // We start off with everything not loaded. After that, we assume that
93 // our monitoring of the package manager provides all updates and we never
94 // need to do a requery. These are only ever touched from the loader thread.
95 private boolean mWorkspaceLoaded;
96 private boolean mAllAppsLoaded;
97
Winson Chungb8b2a5a2012-07-12 17:55:31 -070098 // When we are loading pages synchronously, we can't just post the binding of items on the side
99 // pages as this delays the rotation process. Instead, we wait for a callback from the first
100 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
101 // a normal load, we also clear this set of Runnables.
102 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
103
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700106 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700107 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800108
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700109 // The lock that must be acquired before referencing any static bg data structures. Unlike
110 // other locks, this one can generally be held long-term because we never expect any of these
111 // static data structures to be referenced outside of the worker thread except on the first
112 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700113 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700114
Adam Cohen487f7dd2012-06-28 18:12:10 -0700115 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700116 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700117 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700118
Adam Cohen487f7dd2012-06-28 18:12:10 -0700119 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
120 // created by LauncherModel that are directly on the home screen (however, no widgets or
121 // shortcuts within folders).
122 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
125 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700126 new ArrayList<LauncherAppWidgetInfo>();
127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
129 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
132 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700133
134 // sBgWorkspaceScreens is the ordered set of workspace screens.
135 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
136
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 // </ only access in worker thread >
138
139 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800140 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Adam Cohend22015c2010-07-26 22:02:18 -0700142 private static int mCellCountX;
143 private static int mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700144
Reena Lee99a73f32011-10-24 17:27:37 -0700145 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700146
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700148 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 public int getCurrentWorkspaceScreen();
150 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700151 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
152 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700153 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700154 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500155 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700156 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 public void bindAppWidget(LauncherAppWidgetInfo info);
158 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700159 public void bindAppsAdded(ArrayList<Long> newScreens,
160 ArrayList<ItemInfo> addNotAnimated,
161 ArrayList<ItemInfo> addAnimated);
Joe Onorato64e6be72010-03-05 15:05:52 -0500162 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700163 public void bindComponentsRemoved(ArrayList<String> packageNames,
164 ArrayList<ApplicationInfo> appInfos,
165 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200166 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100167 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700168 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung64359a52013-07-08 17:17:08 -0700171 public interface ItemInfoFilter {
172 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
173 }
174
Daniel Sandlere4f98912013-06-25 15:13:26 -0400175 LauncherModel(LauncherAppState app, IconCache iconCache) {
176 final Context context = app.getContext();
177
Winson Chungee055712013-07-30 14:46:24 -0700178 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400179 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700180 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800181 mIconCache = iconCache;
182
183 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400185
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700187 Configuration config = res.getConfiguration();
188 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 }
190
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700191 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
192 * posted on the main thread handler. */
193 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700194 runOnMainThread(r, 0);
195 }
196 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700197 if (sWorkerThread.getThreadId() == Process.myTid()) {
198 // If we are on the worker thread, post onto the main handler
199 mHandler.post(r);
200 } else {
201 r.run();
202 }
203 }
204
205 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
206 * posted on the worker thread handler. */
207 private static void runOnWorkerThread(Runnable r) {
208 if (sWorkerThread.getThreadId() == Process.myTid()) {
209 r.run();
210 } else {
211 // If we are not on the worker thread, then post to the worker handler
212 sWorker.post(r);
213 }
214 }
215
Winson Chungc9168342013-06-26 14:54:55 -0700216 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
217 long screen) {
218 final int xCount = LauncherModel.getCellCountX();
219 final int yCount = LauncherModel.getCellCountY();
220 boolean[][] occupied = new boolean[xCount][yCount];
221
222 int cellX, cellY, spanX, spanY;
223 for (int i = 0; i < items.size(); ++i) {
224 final ItemInfo item = items.get(i);
225 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
226 if (item.screenId == screen) {
227 cellX = item.cellX;
228 cellY = item.cellY;
229 spanX = item.spanX;
230 spanY = item.spanY;
231 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
232 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
233 occupied[x][y] = true;
234 }
235 }
236 }
237 }
238 }
239
240 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
241 }
242 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700243 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700244 int firstScreenIndex,
245 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700246 // Lock on the app so that we don't try and get the items while apps are being added
247 LauncherAppState app = LauncherAppState.getInstance();
248 LauncherModel model = app.getModel();
249 boolean found = false;
250 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700251 if (sWorkerThread.getThreadId() != Process.myTid()) {
252 // Flush the LauncherModel worker thread, so that if we just did another
253 // processInstallShortcut, we give it time for its shortcut to get added to the
254 // database (getItemsInLocalCoordinates reads the database)
255 model.flushWorkerThread();
256 }
Winson Chungc9168342013-06-26 14:54:55 -0700257 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700258
259 // Try adding to the workspace screens incrementally, starting at the default or center
260 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700261 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
262 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700263 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700264 int[] tmpCoordinates = new int[2];
265 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700266 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700267 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700268 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700269 }
270 }
271 }
Winson Chungc9168342013-06-26 14:54:55 -0700272 return null;
273 }
274
Winson Chung997a9232013-07-24 15:33:46 -0700275 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
276 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
277 addAndBindAddedApps(context, added, cb);
278 }
279 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chung64359a52013-07-08 17:17:08 -0700280 final Callbacks callbacks) {
Winson Chungd64d1762013-08-20 14:37:16 -0700281 Log.w(TAG, "10249126 - addAndBindAddedApps()");
Winson Chung997a9232013-07-24 15:33:46 -0700282 if (added.isEmpty()) {
283 throw new RuntimeException("EMPTY ADDED ARRAY?");
284 }
Winson Chung64359a52013-07-08 17:17:08 -0700285 // Process the newly added applications and add them to the database first
286 Runnable r = new Runnable() {
287 public void run() {
288 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
289 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
290
Winson Chung76828c82013-08-19 15:43:29 -0700291 // Get the list of workspace screens. We need to append to this list and
292 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
293 // called.
294 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
295 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
296 for (Integer i : orderedScreens.keySet()) {
297 long screenId = orderedScreens.get(i);
298 workspaceScreens.add(screenId);
299 }
300
Winson Chung64359a52013-07-08 17:17:08 -0700301 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700302 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700303 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700304 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700305 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700306 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700307
308 // Short-circuit this logic if the icon exists somewhere on the workspace
309 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
310 continue;
311 }
312
313 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700314 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700315 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700316 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700317 if (coords == null) {
Winson Chung64359a52013-07-08 17:17:08 -0700318 LauncherAppState appState = LauncherAppState.getInstance();
319 LauncherProvider lp = appState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700320
321 // If we can't find a valid position, then just add a new screen.
322 // This takes time so we need to re-queue the add until the new
323 // page is added. Create as many screens as necessary to satisfy
324 // the startSearchPageIndex.
325 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700326 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700327 while (numPagesToAdd > 0) {
328 long screenId = lp.generateNewScreenId();
Winson Chung76828c82013-08-19 15:43:29 -0700329 Log.w(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -0700330 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700331 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700332 addedWorkspaceScreensFinal.add(screenId);
333 numPagesToAdd--;
334 }
Winson Chung76828c82013-08-19 15:43:29 -0700335
Winson Chung64359a52013-07-08 17:17:08 -0700336 // Find the coordinate again
337 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700338 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700339 }
340 if (coords == null) {
341 throw new RuntimeException("Coordinates should not be null");
342 }
343
Winson Chung997a9232013-07-24 15:33:46 -0700344 ShortcutInfo shortcutInfo;
345 if (a instanceof ShortcutInfo) {
346 shortcutInfo = (ShortcutInfo) a;
347 } else if (a instanceof ApplicationInfo) {
348 shortcutInfo = ((ApplicationInfo) a).makeShortcut();
349 } else {
350 throw new RuntimeException("Unexpected info type");
351 }
Winson Chung64359a52013-07-08 17:17:08 -0700352 // Add the shortcut to the db
353 addItemToDatabase(context, shortcutInfo,
354 LauncherSettings.Favorites.CONTAINER_DESKTOP,
355 coords.first, coords.second[0], coords.second[1], false);
356 // Save the ShortcutInfo for binding in the workspace
357 addedShortcutsFinal.add(shortcutInfo);
358 }
359 }
360
Winson Chungd64d1762013-08-20 14:37:16 -0700361 Log.w(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")");
362
Winson Chung76828c82013-08-19 15:43:29 -0700363 // Update the workspace screens
364 updateWorkspaceScreenOrder(context, workspaceScreens);
365
Winson Chung997a9232013-07-24 15:33:46 -0700366 if (!addedShortcutsFinal.isEmpty()) {
367 runOnMainThread(new Runnable() {
368 public void run() {
369 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
370 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700371 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
372 long lastScreenId = info.screenId;
373 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
374 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
375 for (ItemInfo i : addedShortcutsFinal) {
376 if (i.screenId == lastScreenId) {
377 addAnimated.add(i);
378 } else {
379 addNotAnimated.add(i);
380 }
381 }
Winson Chungd64d1762013-08-20 14:37:16 -0700382 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
383 addNotAnimated, addAnimated);
Winson Chung997a9232013-07-24 15:33:46 -0700384 }
Winson Chung64359a52013-07-08 17:17:08 -0700385 }
Winson Chung997a9232013-07-24 15:33:46 -0700386 });
387 }
Winson Chung64359a52013-07-08 17:17:08 -0700388 }
389 };
390 runOnWorkerThread(r);
391 }
392
Joe Onorato56d82912010-03-07 14:32:10 -0500393 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800394 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Winson Chung81b52252012-08-27 15:34:29 -0700397 public void unbindItemInfosAndClearQueuedBindRunnables() {
398 if (sWorkerThread.getThreadId() == Process.myTid()) {
399 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
400 "main thread");
401 }
402
403 // Clear any deferred bind runnables
404 mDeferredBindRunnables.clear();
405 // Remove any queued bind runnables
406 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
407 // Unbind all the workspace items
408 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700409 }
410
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700411 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700412 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700413 // Ensure that we don't use the same workspace items data structure on the main thread
414 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700415 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
416 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700417 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
419 tmpAppWidgets.addAll(sBgAppWidgets);
420 }
421 Runnable r = new Runnable() {
422 @Override
423 public void run() {
424 for (ItemInfo item : tmpWorkspaceItems) {
425 item.unbind();
426 }
427 for (ItemInfo item : tmpAppWidgets) {
428 item.unbind();
429 }
430 }
431 };
432 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700433 }
434
Joe Onorato9c1289c2009-08-17 11:03:03 -0400435 /**
436 * Adds an item to the DB if it was not created previously, or move it to a new
437 * <container, screen, cellX, cellY>
438 */
439 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700440 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 if (item.container == ItemInfo.NO_ID) {
442 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700443 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 } else {
445 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448 }
449
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700450 static void checkItemInfoLocked(
451 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
452 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
453 if (modelItem != null && item != modelItem) {
454 // check all the data is consistent
455 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
456 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
457 ShortcutInfo shortcut = (ShortcutInfo) item;
458 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
459 modelShortcut.intent.filterEquals(shortcut.intent) &&
460 modelShortcut.id == shortcut.id &&
461 modelShortcut.itemType == shortcut.itemType &&
462 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700463 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700464 modelShortcut.cellX == shortcut.cellX &&
465 modelShortcut.cellY == shortcut.cellY &&
466 modelShortcut.spanX == shortcut.spanX &&
467 modelShortcut.spanY == shortcut.spanY &&
468 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
469 (modelShortcut.dropPos != null &&
470 shortcut.dropPos != null &&
471 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
472 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
473 // For all intents and purposes, this is the same object
474 return;
475 }
476 }
477
478 // the modelItem needs to match up perfectly with item if our model is
479 // to be consistent with the database-- for now, just require
480 // modelItem == item or the equality check above
481 String msg = "item: " + ((item != null) ? item.toString() : "null") +
482 "modelItem: " +
483 ((modelItem != null) ? modelItem.toString() : "null") +
484 "Error: ItemInfo passed to checkItemInfo doesn't match original";
485 RuntimeException e = new RuntimeException(msg);
486 if (stackTrace != null) {
487 e.setStackTrace(stackTrace);
488 }
Adam Cohene25af792013-06-06 23:08:25 -0700489 // TODO: something breaks this in the upgrade path
490 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700491 }
492 }
493
Michael Jurka816474f2012-06-25 14:49:02 -0700494 static void checkItemInfo(final ItemInfo item) {
495 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
496 final long itemId = item.id;
497 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700498 public void run() {
499 synchronized (sBgLock) {
500 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700501 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700502 }
503 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700504 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700505 }
506
Michael Jurkac9d95c52011-08-29 14:03:34 -0700507 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
508 final ItemInfo item, final String callingFunction) {
509 final long itemId = item.id;
510 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
511 final ContentResolver cr = context.getContentResolver();
512
Adam Cohen487f7dd2012-06-28 18:12:10 -0700513 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700514 Runnable r = new Runnable() {
515 public void run() {
516 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700517 updateItemArrays(item, itemId, stackTrace);
518 }
519 };
520 runOnWorkerThread(r);
521 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700522
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700523 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
524 final ArrayList<ItemInfo> items, final String callingFunction) {
525 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700526
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700527 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
528 Runnable r = new Runnable() {
529 public void run() {
530 ArrayList<ContentProviderOperation> ops =
531 new ArrayList<ContentProviderOperation>();
532 int count = items.size();
533 for (int i = 0; i < count; i++) {
534 ItemInfo item = items.get(i);
535 final long itemId = item.id;
536 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
537 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700538
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700539 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
540 updateItemArrays(item, itemId, stackTrace);
541
542 }
543 try {
544 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
545 } catch (Exception e) {
546 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700547 }
548 }
549 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700550 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700551 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700552
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700553 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
554 // Lock on mBgLock *after* the db operation
555 synchronized (sBgLock) {
556 checkItemInfoLocked(itemId, item, stackTrace);
557
558 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
559 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
560 // Item is in a folder, make sure this folder exists
561 if (!sBgFolders.containsKey(item.container)) {
562 // An items container is being set to a that of an item which is not in
563 // the list of Folders.
564 String msg = "item: " + item + " container being set to: " +
565 item.container + ", not in the list of folders";
566 Log.e(TAG, msg);
567 Launcher.dumpDebugLogsToConsole();
568 }
569 }
570
571 // Items are added/removed from the corresponding FolderInfo elsewhere, such
572 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
573 // that are on the desktop, as appropriate
574 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
575 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
576 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
577 switch (modelItem.itemType) {
578 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
579 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
580 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
581 if (!sBgWorkspaceItems.contains(modelItem)) {
582 sBgWorkspaceItems.add(modelItem);
583 }
584 break;
585 default:
586 break;
587 }
588 } else {
589 sBgWorkspaceItems.remove(modelItem);
590 }
591 }
592 }
593
Michael Jurkac7700af2013-05-14 20:17:58 +0200594 public void flushWorkerThread() {
595 mFlushingWorkerThread = true;
596 Runnable waiter = new Runnable() {
597 public void run() {
598 synchronized (this) {
599 notifyAll();
600 mFlushingWorkerThread = false;
601 }
602 }
603 };
604
605 synchronized(waiter) {
606 runOnWorkerThread(waiter);
607 if (mLoaderTask != null) {
608 synchronized(mLoaderTask) {
609 mLoaderTask.notify();
610 }
611 }
612 boolean success = false;
613 while (!success) {
614 try {
615 waiter.wait();
616 success = true;
617 } catch (InterruptedException e) {
618 }
619 }
620 }
621 }
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400624 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700625 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700626 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700628 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
630 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700631 Launcher.sDumpLogs.add(transaction);
632 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634 item.cellX = cellX;
635 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700636
Winson Chung3d503fb2011-07-13 17:25:49 -0700637 // We store hotseat items in canonical form which is this orientation invariant position
638 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645
646 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
649 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651
Michael Jurkac9d95c52011-08-29 14:03:34 -0700652 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700653 }
654
655 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700656 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
657 * cellX, cellY have already been updated on the ItemInfos.
658 */
659 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
660 final long container, final int screen) {
661
662 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
663 int count = items.size();
664
665 for (int i = 0; i < count; i++) {
666 ItemInfo item = items.get(i);
667 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700668 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700669 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
670 + item.cellX + ", " + item.cellY + ")";
671 Launcher.sDumpLogs.add(transaction);
672 item.container = container;
673
674 // We store hotseat items in canonical form which is this orientation invariant position
675 // in the hotseat
676 if (context instanceof Launcher && screen < 0 &&
677 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 item.cellY);
680 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 }
683
684 final ContentValues values = new ContentValues();
685 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
686 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
687 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689
690 contentValues.add(values);
691 }
692 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
693 }
694
695 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700696 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800697 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700698 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700700 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
702 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700703 Launcher.sDumpLogs.add(transaction);
704 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800705 item.cellX = cellX;
706 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 item.spanX = spanX;
708 item.spanY = spanY;
709
710 // We store hotseat items in canonical form which is this orientation invariant position
711 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 }
Adam Cohend4844c32011-02-18 19:25:06 -0800718
Adam Cohend4844c32011-02-18 19:25:06 -0800719 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800720 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700721 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
722 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
723 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
724 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700725 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800726
Michael Jurka816474f2012-06-25 14:49:02 -0700727 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700728 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729
730 /**
731 * Update an item to the database in a specified container.
732 */
733 static void updateItemInDatabase(Context context, final ItemInfo item) {
734 final ContentValues values = new ContentValues();
735 item.onAddToDatabase(values);
736 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
737 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800738 }
739
740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 * Returns true if the shortcuts already exists in the database.
742 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 static boolean shortcutExists(Context context, String title, Intent intent) {
745 final ContentResolver cr = context.getContentResolver();
746 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
747 new String[] { "title", "intent" }, "title=? and intent=?",
748 new String[] { title, intent.toUri(0) }, null);
749 boolean result = false;
750 try {
751 result = c.moveToFirst();
752 } finally {
753 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700756 }
757
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 * Returns an ItemInfo array containing all the items in the LauncherModel.
760 * The ItemInfo.id is not set through this function.
761 */
762 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
763 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
764 final ContentResolver cr = context.getContentResolver();
765 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
766 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
767 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
768 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
769
770 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
771 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
772 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
773 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
774 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
775 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
776 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
777
778 try {
779 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700780 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700781 item.cellX = c.getInt(cellXIndex);
782 item.cellY = c.getInt(cellYIndex);
783 item.spanX = c.getInt(spanXIndex);
784 item.spanY = c.getInt(spanYIndex);
785 item.container = c.getInt(containerIndex);
786 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700788
789 items.add(item);
790 }
791 } catch (Exception e) {
792 items.clear();
793 } finally {
794 c.close();
795 }
796
797 return items;
798 }
799
800 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
802 */
803 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
804 final ContentResolver cr = context.getContentResolver();
805 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
806 "_id=? and (itemType=? or itemType=?)",
807 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700808 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700809
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 try {
811 if (c.moveToFirst()) {
812 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
813 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
814 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
815 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
816 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
817 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 FolderInfo folderInfo = null;
820 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700821 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
822 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700824 }
825
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 folderInfo.title = c.getString(titleIndex);
827 folderInfo.id = id;
828 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700830 folderInfo.cellX = c.getInt(cellXIndex);
831 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
833 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835 } finally {
836 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700837 }
838
839 return null;
840 }
841
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 /**
843 * Add an item to the database in a specified container. Sets the container, screen, cellX and
844 * cellY fields of the item. Also assigns an ID to the item.
845 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 item.cellX = cellX;
850 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 // We store hotseat items in canonical form which is this orientation invariant position
852 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859
860 final ContentValues values = new ContentValues();
861 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 item.onAddToDatabase(values);
863
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400864 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 item.id = app.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700866 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700868
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700871 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700872 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700873 + cellY + ")";
874 Launcher.sDumpLogs.add(transaction);
875 Log.d(TAG, transaction);
876
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700877 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
878 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700880 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700881 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700882 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 sBgItemsIdMap.put(item.id, item);
884 switch (item.itemType) {
885 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
886 sBgFolders.put(item.id, (FolderInfo) item);
887 // Fall through
888 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
889 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
890 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
891 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
892 sBgWorkspaceItems.add(item);
893 } else {
894 if (!sBgFolders.containsKey(item.container)) {
895 // Adding an item to a folder that doesn't exist.
896 String msg = "adding item: " + item + " to a folder that " +
897 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700898 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700899 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700900 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700901 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700902 break;
903 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
904 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
905 break;
906 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700907 }
908 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700909 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700910 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700911 }
912
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700914 * Creates a new unique child id, for a given cell span across all layouts.
915 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700916 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700918 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700920 }
921
Adam Cohend22015c2010-07-26 22:02:18 -0700922 static int getCellCountX() {
923 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700924 }
925
Adam Cohend22015c2010-07-26 22:02:18 -0700926 static int getCellCountY() {
927 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700928 }
929
930 /**
931 * Updates the model orientation helper to take into account the current layout dimensions
932 * when performing local/canonical coordinate transformations.
933 */
934 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700935 mCellCountX = shortAxisCellCount;
936 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700937 }
938
939 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700940 * Removes the specified item from the database
941 * @param context
942 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700944 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700946 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700947
Michael Jurka83df1882011-08-31 20:59:26 -0700948 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700949 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700950 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700952 ", " + item.cellY + ")";
953 Launcher.sDumpLogs.add(transaction);
954 Log.d(TAG, transaction);
955
Michael Jurkac9d95c52011-08-29 14:03:34 -0700956 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957
958 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700959 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700960 switch (item.itemType) {
961 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
962 sBgFolders.remove(item.id);
963 for (ItemInfo info: sBgItemsIdMap.values()) {
964 if (info.container == item.id) {
965 // We are deleting a folder which still contains items that
966 // think they are contained by that folder.
967 String msg = "deleting a folder (" + item + ") which still " +
968 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700969 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700970 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700971 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700972 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 sBgWorkspaceItems.remove(item);
974 break;
975 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
976 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
977 sBgWorkspaceItems.remove(item);
978 break;
979 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
980 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
981 break;
982 }
983 sBgItemsIdMap.remove(item.id);
984 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700985 }
986 }
Michael Jurka83df1882011-08-31 20:59:26 -0700987 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700988 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 }
990
991 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 * Update the order of the workspace screens in the database. The array list contains
993 * a list of screen ids in the order that they should appear.
994 */
Winson Chungc9168342013-06-26 14:54:55 -0700995 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700996 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700997 final ContentResolver cr = context.getContentResolver();
998 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
999
1000 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001001 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 while (iter.hasNext()) {
1003 long id = iter.next();
1004 if (id < 0) {
1005 iter.remove();
1006 }
1007 }
1008
1009 Runnable r = new Runnable() {
1010 @Override
1011 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001012 // Clear the table
1013 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001014 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001015 ContentValues[] values = new ContentValues[count];
1016 for (int i = 0; i < count; i++) {
1017 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001018 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1020 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Winson Chung76828c82013-08-19 15:43:29 -07001021 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001022 values[i] = v;
1023 }
1024 cr.bulkInsert(uri, values);
1025 sBgWorkspaceScreens.clear();
1026 sBgWorkspaceScreens.addAll(screensCopy);
1027 }
1028 };
1029 runOnWorkerThread(r);
1030 }
1031
1032 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 * Remove the contents of the specified folder from the database
1034 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 final ContentResolver cr = context.getContentResolver();
1037
Michael Jurkac9d95c52011-08-29 14:03:34 -07001038 Runnable r = new Runnable() {
1039 public void run() {
1040 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001041 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001042 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001043 sBgItemsIdMap.remove(info.id);
1044 sBgFolders.remove(info.id);
1045 sBgDbIconCache.remove(info);
1046 sBgWorkspaceItems.remove(info);
1047 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048
Michael Jurkac9d95c52011-08-29 14:03:34 -07001049 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1050 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001052 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 for (ItemInfo childInfo : info.contents) {
1054 sBgItemsIdMap.remove(childInfo.id);
1055 sBgDbIconCache.remove(childInfo);
1056 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001057 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001058 }
1059 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 }
1062
1063 /**
1064 * Set this as the current Launcher activity object for the loader.
1065 */
1066 public void initialize(Callbacks callbacks) {
1067 synchronized (mLock) {
1068 mCallbacks = new WeakReference<Callbacks>(callbacks);
1069 }
1070 }
1071
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001072 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1074 * ACTION_PACKAGE_CHANGED.
1075 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001076 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001077 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001078 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001079
Joe Onorato36115782010-06-17 13:28:48 -04001080 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001081
Joe Onorato36115782010-06-17 13:28:48 -04001082 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1083 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1084 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1085 final String packageName = intent.getData().getSchemeSpecificPart();
1086 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Joe Onorato36115782010-06-17 13:28:48 -04001088 int op = PackageUpdatedTask.OP_NONE;
1089
1090 if (packageName == null || packageName.length() == 0) {
1091 // they sent us a bad intent
1092 return;
1093 }
1094
1095 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1096 op = PackageUpdatedTask.OP_UPDATE;
1097 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1098 if (!replacing) {
1099 op = PackageUpdatedTask.OP_REMOVE;
1100 }
1101 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1102 // later, we will update the package at this time
1103 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1104 if (!replacing) {
1105 op = PackageUpdatedTask.OP_ADD;
1106 } else {
1107 op = PackageUpdatedTask.OP_UPDATE;
1108 }
1109 }
1110
1111 if (op != PackageUpdatedTask.OP_NONE) {
1112 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1113 }
1114
1115 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001116 // First, schedule to add these apps back in.
1117 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1118 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1119 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001120 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001121 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1122 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1123 enqueuePackageUpdated(new PackageUpdatedTask(
1124 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001125 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001126 // If we have changed locale we need to clear out the labels in all apps/workspace.
1127 forceReload();
1128 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1129 // Check if configuration change was an mcc/mnc change which would affect app resources
1130 // and we would need to clear out the labels in all apps/workspace. Same handling as
1131 // above for ACTION_LOCALE_CHANGED
1132 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001133 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001134 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001135 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001136 forceReload();
1137 }
1138 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001139 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001140 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1141 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001142 if (mCallbacks != null) {
1143 Callbacks callbacks = mCallbacks.get();
1144 if (callbacks != null) {
1145 callbacks.bindSearchablesChanged();
1146 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001147 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001148 }
1149 }
1150
Reena Lee93f824a2011-09-23 17:20:28 -07001151 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001152 resetLoadedState(true, true);
1153
Reena Lee93f824a2011-09-23 17:20:28 -07001154 // Do this here because if the launcher activity is running it will be restarted.
1155 // If it's not running startLoaderFromBackground will merely tell it that it needs
1156 // to reload.
1157 startLoaderFromBackground();
1158 }
1159
Winson Chungf0c6ae02012-03-21 16:10:31 -07001160 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1161 synchronized (mLock) {
1162 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1163 // mWorkspaceLoaded to true later
1164 stopLoaderLocked();
1165 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1166 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1167 }
1168 }
1169
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001170 /**
1171 * When the launcher is in the background, it's possible for it to miss paired
1172 * configuration changes. So whenever we trigger the loader from the background
1173 * tell the launcher that it needs to re-run the loader when it comes back instead
1174 * of doing it now.
1175 */
1176 public void startLoaderFromBackground() {
1177 boolean runLoader = false;
1178 if (mCallbacks != null) {
1179 Callbacks callbacks = mCallbacks.get();
1180 if (callbacks != null) {
1181 // Only actually run the loader if they're not paused.
1182 if (!callbacks.setLoadOnResume()) {
1183 runLoader = true;
1184 }
1185 }
1186 }
1187 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001189 }
Joe Onorato36115782010-06-17 13:28:48 -04001190 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001191
Reena Lee93f824a2011-09-23 17:20:28 -07001192 // If there is already a loader task running, tell it to stop.
1193 // returns true if isLaunching() was true on the old task
1194 private boolean stopLoaderLocked() {
1195 boolean isLaunching = false;
1196 LoaderTask oldTask = mLoaderTask;
1197 if (oldTask != null) {
1198 if (oldTask.isLaunching()) {
1199 isLaunching = true;
1200 }
1201 oldTask.stopLocked();
1202 }
1203 return isLaunching;
1204 }
1205
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001207 synchronized (mLock) {
1208 if (DEBUG_LOADERS) {
1209 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 // Clear any deferred bind-runnables from the synchronized load process
1213 // We must do this before any loading/binding is scheduled below.
1214 mDeferredBindRunnables.clear();
1215
Joe Onorato36115782010-06-17 13:28:48 -04001216 // Don't bother to start the thread if we know it's not going to do anything
1217 if (mCallbacks != null && mCallbacks.get() != null) {
1218 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001219 // also, don't downgrade isLaunching if we're already running
1220 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001221 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1223 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1224 } else {
1225 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1226 sWorker.post(mLoaderTask);
1227 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229 }
1230 }
1231
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001232 void bindRemainingSynchronousPages() {
1233 // Post the remaining side pages to be loaded
1234 if (!mDeferredBindRunnables.isEmpty()) {
1235 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001236 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001237 }
1238 mDeferredBindRunnables.clear();
1239 }
1240 }
1241
Joe Onorato36115782010-06-17 13:28:48 -04001242 public void stopLoader() {
1243 synchronized (mLock) {
1244 if (mLoaderTask != null) {
1245 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 }
1247 }
Joe Onorato36115782010-06-17 13:28:48 -04001248 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249
Winson Chung76828c82013-08-19 15:43:29 -07001250 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1251 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1252 final ContentResolver contentResolver = context.getContentResolver();
1253 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1254 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1255 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1256
1257 try {
1258 final int idIndex = sc.getColumnIndexOrThrow(
1259 LauncherSettings.WorkspaceScreens._ID);
1260 final int rankIndex = sc.getColumnIndexOrThrow(
1261 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1262 while (sc.moveToNext()) {
1263 try {
1264 long screenId = sc.getLong(idIndex);
1265 int rank = sc.getInt(rankIndex);
1266
1267 Log.w(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")");
1268
1269 orderedScreens.put(rank, screenId);
1270 } catch (Exception e) {
1271 Log.w(TAG, "Desktop items loading interrupted - invalid screens: ", e);
1272 }
1273 }
1274 } finally {
1275 sc.close();
1276 }
1277 return orderedScreens;
1278 }
1279
Michael Jurkac57b7a82011-08-09 22:02:20 -07001280 public boolean isAllAppsLoaded() {
1281 return mAllAppsLoaded;
1282 }
1283
Winson Chung36a62fe2012-05-06 18:04:42 -07001284 boolean isLoadingWorkspace() {
1285 synchronized (mLock) {
1286 if (mLoaderTask != null) {
1287 return mLoaderTask.isLoadingWorkspace();
1288 }
1289 }
1290 return false;
1291 }
1292
Joe Onorato36115782010-06-17 13:28:48 -04001293 /**
1294 * Runnable for the thread that loads the contents of the launcher:
1295 * - workspace icons
1296 * - widgets
1297 * - all apps icons
1298 */
1299 private class LoaderTask implements Runnable {
1300 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001301 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001302 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001303 private boolean mStopped;
1304 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305
Winson Chungc3eecff2011-07-11 17:44:15 -07001306 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001307
1308 LoaderTask(Context context, boolean isLaunching) {
1309 mContext = context;
1310 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001311 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312 }
1313
Joe Onorato36115782010-06-17 13:28:48 -04001314 boolean isLaunching() {
1315 return mIsLaunching;
1316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Winson Chung36a62fe2012-05-06 18:04:42 -07001318 boolean isLoadingWorkspace() {
1319 return mIsLoadingAndBindingWorkspace;
1320 }
1321
Winson Chungc763c4e2013-07-19 13:49:06 -07001322 /** Returns whether this is an upgrade path */
1323 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001324 mIsLoadingAndBindingWorkspace = true;
1325
Joe Onorato36115782010-06-17 13:28:48 -04001326 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001327 if (DEBUG_LOADERS) {
1328 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001330
Winson Chungc763c4e2013-07-19 13:49:06 -07001331 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001332 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001333 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001334 synchronized (LoaderTask.this) {
1335 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001336 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001337 }
1338 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
1341
Joe Onorato36115782010-06-17 13:28:48 -04001342 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001343 bindWorkspace(-1, isUpgradePath);
1344 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001345 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001346
Joe Onorato36115782010-06-17 13:28:48 -04001347 private void waitForIdle() {
1348 // Wait until the either we're stopped or the other threads are done.
1349 // This way we don't start loading all apps until the workspace has settled
1350 // down.
1351 synchronized (LoaderTask.this) {
1352 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001353
Joe Onorato36115782010-06-17 13:28:48 -04001354 mHandler.postIdle(new Runnable() {
1355 public void run() {
1356 synchronized (LoaderTask.this) {
1357 mLoadAndBindStepFinished = true;
1358 if (DEBUG_LOADERS) {
1359 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001360 }
Joe Onorato36115782010-06-17 13:28:48 -04001361 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001362 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001363 }
Joe Onorato36115782010-06-17 13:28:48 -04001364 });
1365
Michael Jurkac7700af2013-05-14 20:17:58 +02001366 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001367 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001368 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1369 // wait no longer than 1sec at a time
1370 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001371 } catch (InterruptedException ex) {
1372 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001373 }
1374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375 if (DEBUG_LOADERS) {
1376 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001377 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001378 + "ms for previous step to finish binding");
1379 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001380 }
Joe Onorato36115782010-06-17 13:28:48 -04001381 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 void runBindSynchronousPage(int synchronousBindPage) {
1384 if (synchronousBindPage < 0) {
1385 // Ensure that we have a valid page index to load synchronously
1386 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1387 "valid page index");
1388 }
1389 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1390 // Ensure that we don't try and bind a specified page when the pages have not been
1391 // loaded already (we should load everything asynchronously in that case)
1392 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1393 }
1394 synchronized (mLock) {
1395 if (mIsLoaderTaskRunning) {
1396 // Ensure that we are never running the background loading at this point since
1397 // we also touch the background collections
1398 throw new RuntimeException("Error! Background loading is already running");
1399 }
1400 }
1401
1402 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1403 // data structures, we can't allow any other thread to touch that data, but because
1404 // this call is synchronous, we can get away with not locking).
1405
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001406 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001407 // operations from the previous activity. We need to ensure that all queued operations
1408 // are executed before any synchronous binding work is done.
1409 mHandler.flush();
1410
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 // Divide the set of loaded items into those that we are binding synchronously, and
1412 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001413 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1415 // arise from that.
1416 onlyBindAllApps();
1417 }
1418
Joe Onorato36115782010-06-17 13:28:48 -04001419 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001420 boolean isUpgrade = false;
1421
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 synchronized (mLock) {
1423 mIsLoaderTaskRunning = true;
1424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 // Optimize for end-user experience: if the Launcher is up and // running with the
1426 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1427 // workspace first (default).
1428 final Callbacks cbk = mCallbacks.get();
Joe Onorato36115782010-06-17 13:28:48 -04001429 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 // Elevate priority when Home launches for the first time to avoid
1431 // starving at boot time. Staring at a blank home is not cool.
1432 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001433 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1434 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001435 android.os.Process.setThreadPriority(mIsLaunching
1436 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1437 }
Winson Chung64359a52013-07-08 17:17:08 -07001438 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001439 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001440
Joe Onorato36115782010-06-17 13:28:48 -04001441 if (mStopped) {
1442 break keep_running;
1443 }
1444
1445 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1446 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001447 synchronized (mLock) {
1448 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001449 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001450 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1451 }
1452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001454
1455 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001456 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1457 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001458
1459 // Restore the default thread priority after we are done loading items
1460 synchronized (mLock) {
1461 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1462 }
Joe Onorato36115782010-06-17 13:28:48 -04001463 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464
Winson Chungaac01e12011-08-17 10:37:13 -07001465 // Update the saved icons if necessary
1466 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001467 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 for (Object key : sBgDbIconCache.keySet()) {
1469 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1470 }
1471 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001472 }
Winson Chungaac01e12011-08-17 10:37:13 -07001473
Winson Chungc763c4e2013-07-19 13:49:06 -07001474 // Ensure that all the applications that are in the system are represented on the home
1475 // screen.
Winson Chung76828c82013-08-19 15:43:29 -07001476 Log.w(TAG, "10249126 - verifyApplications(" + isUpgrade + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -07001477 if (!isUpgrade) {
1478 verifyApplications();
1479 }
1480
Joe Onorato36115782010-06-17 13:28:48 -04001481 // Clear out this reference, otherwise we end up holding it until all of the
1482 // callback runnables are done.
1483 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 synchronized (mLock) {
1486 // If we are still the last one to be scheduled, remove ourselves.
1487 if (mLoaderTask == this) {
1488 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001490 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
1493
1494 public void stopLocked() {
1495 synchronized (LoaderTask.this) {
1496 mStopped = true;
1497 this.notify();
1498 }
1499 }
1500
1501 /**
1502 * Gets the callbacks object. If we've been stopped, or if the launcher object
1503 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1504 * object that was around when the deferred message was scheduled, and if there's
1505 * a new Callbacks object around then also return null. This will save us from
1506 * calling onto it with data that will be ignored.
1507 */
1508 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1509 synchronized (mLock) {
1510 if (mStopped) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 if (mCallbacks == null) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 final Callbacks callbacks = mCallbacks.get();
1519 if (callbacks != oldCallbacks) {
1520 return null;
1521 }
1522 if (callbacks == null) {
1523 Log.w(TAG, "no mCallbacks");
1524 return null;
1525 }
1526
1527 return callbacks;
1528 }
1529 }
1530
Winson Chungc763c4e2013-07-19 13:49:06 -07001531 private void verifyApplications() {
1532 final Context context = mApp.getContext();
1533
1534 // Cross reference all the applications in our apps list with items in the workspace
1535 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001536 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001537 synchronized (sBgLock) {
1538 for (ApplicationInfo app : mBgAllAppsList.data) {
1539 tmpInfos = getItemInfoForComponentName(app.componentName);
Winson Chung76828c82013-08-19 15:43:29 -07001540 Log.w(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty());
Winson Chungc763c4e2013-07-19 13:49:06 -07001541 if (tmpInfos.isEmpty()) {
1542 // We are missing an application icon, so add this to the workspace
1543 added.add(app);
1544 // This is a rare event, so lets log it
1545 Log.e(TAG, "Missing Application on load: " + app);
1546 }
1547 }
1548 }
1549 if (!added.isEmpty()) {
1550 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1551 addAndBindAddedApps(context, added, cb);
1552 }
1553 }
1554
Winson Chung5f8afe62013-08-12 16:19:28 -07001555 private boolean checkItemDimensions(ItemInfo info) {
1556 return (info.cellX + info.spanX) > mCellCountX ||
1557 (info.cellY + info.spanY) > mCellCountY;
1558 }
1559
Joe Onorato36115782010-06-17 13:28:48 -04001560 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1562 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001563 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1565 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1566 [(int) item.screenId][0] != null) {
1567 Log.e(TAG, "Error loading shortcut into hotseat " + item
1568 + " into position (" + item.screenId + ":" + item.cellX + ","
1569 + item.cellY + ") occupied by "
1570 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1571 [(int) item.screenId][0]);
1572 return false;
1573 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001574 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1576 items[(int) item.screenId][0] = item;
1577 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001578 return true;
1579 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001580 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1581 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 if (!occupied.containsKey(item.screenId)) {
1586 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1587 occupied.put(item.screenId, items);
1588 }
1589
1590 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001591 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001592 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1593 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001595 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001596 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001597 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001598 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001600 return false;
1601 }
1602 }
1603 }
1604 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1605 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001607 }
1608 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001609
Joe Onorato36115782010-06-17 13:28:48 -04001610 return true;
1611 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612
Winson Chungc763c4e2013-07-19 13:49:06 -07001613 /** Returns whether this is an upgradge path */
1614 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001615 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616
Joe Onorato36115782010-06-17 13:28:48 -04001617 final Context context = mContext;
1618 final ContentResolver contentResolver = context.getContentResolver();
1619 final PackageManager manager = context.getPackageManager();
1620 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1621 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001622
Michael Jurkab85f8a42012-04-25 15:48:32 -07001623 // Make sure the default workspace is loaded, if needed
Winson Chungc763c4e2013-07-19 13:49:06 -07001624 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001625
Winson Chungc763c4e2013-07-19 13:49:06 -07001626 // Check if we need to do any upgrade-path logic
1627 boolean loadedOldDb = mApp.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001628
Winson Chung2abf94d2012-07-18 18:16:38 -07001629 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001630 sBgWorkspaceItems.clear();
1631 sBgAppWidgets.clear();
1632 sBgFolders.clear();
1633 sBgItemsIdMap.clear();
1634 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001635 sBgWorkspaceScreens.clear();
Winson Chung76828c82013-08-19 15:43:29 -07001636 Log.w(TAG, "10249126 - loadWorkspace()");
Romain Guy5c16f3e2010-01-12 17:24:58 -08001637
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001638 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001639 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001640 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001641
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 // +1 for the hotseat (it can be larger than the workspace)
1643 // Load workspace in reverse order to ensure that latest items are loaded first (and
1644 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 try {
1648 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1649 final int intentIndex = c.getColumnIndexOrThrow
1650 (LauncherSettings.Favorites.INTENT);
1651 final int titleIndex = c.getColumnIndexOrThrow
1652 (LauncherSettings.Favorites.TITLE);
1653 final int iconTypeIndex = c.getColumnIndexOrThrow(
1654 LauncherSettings.Favorites.ICON_TYPE);
1655 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1656 final int iconPackageIndex = c.getColumnIndexOrThrow(
1657 LauncherSettings.Favorites.ICON_PACKAGE);
1658 final int iconResourceIndex = c.getColumnIndexOrThrow(
1659 LauncherSettings.Favorites.ICON_RESOURCE);
1660 final int containerIndex = c.getColumnIndexOrThrow(
1661 LauncherSettings.Favorites.CONTAINER);
1662 final int itemTypeIndex = c.getColumnIndexOrThrow(
1663 LauncherSettings.Favorites.ITEM_TYPE);
1664 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1665 LauncherSettings.Favorites.APPWIDGET_ID);
1666 final int screenIndex = c.getColumnIndexOrThrow(
1667 LauncherSettings.Favorites.SCREEN);
1668 final int cellXIndex = c.getColumnIndexOrThrow
1669 (LauncherSettings.Favorites.CELLX);
1670 final int cellYIndex = c.getColumnIndexOrThrow
1671 (LauncherSettings.Favorites.CELLY);
1672 final int spanXIndex = c.getColumnIndexOrThrow
1673 (LauncherSettings.Favorites.SPANX);
1674 final int spanYIndex = c.getColumnIndexOrThrow(
1675 LauncherSettings.Favorites.SPANY);
1676 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1677 //final int displayModeIndex = c.getColumnIndexOrThrow(
1678 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 ShortcutInfo info;
1681 String intentDescription;
1682 LauncherAppWidgetInfo appWidgetInfo;
1683 int container;
1684 long id;
1685 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001687 while (!mStopped && c.moveToNext()) {
1688 try {
1689 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 switch (itemType) {
1692 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1693 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001694 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 intentDescription = c.getString(intentIndex);
1696 try {
1697 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001698 ComponentName cn = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07001699 if (!isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001700 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001701 // Log the invalid package, and remove it from the db
1702 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1703 false);
Winson Chungee055712013-07-30 14:46:24 -07001704 contentResolver.delete(uri, null, null);
Winson Chung1323b482013-08-05 12:41:55 -07001705 Log.e(TAG, "Invalid package removed: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001706 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001707 // If apps can be on external storage, then we just
1708 // leave them for the user to remove (maybe add
1709 // visual treatment to it)
1710 Log.e(TAG, "Invalid package found: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001711 }
1712 continue;
1713 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 } catch (URISyntaxException e) {
1715 continue;
1716 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1719 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1720 titleIndex, mLabelCache);
1721 } else {
1722 info = getShortcutInfo(c, context, iconTypeIndex,
1723 iconPackageIndex, iconResourceIndex, iconIndex,
1724 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001725
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001726 // App shortcuts that used to be automatically added to Launcher
1727 // didn't always have the correct intent flags set, so do that
1728 // here
1729 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001730 intent.getCategories() != null &&
1731 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001732 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 intent.addFlags(
1734 Intent.FLAG_ACTIVITY_NEW_TASK |
1735 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1736 }
Michael Jurka96879562012-03-22 05:54:33 -07001737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001740 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001741 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 container = c.getInt(containerIndex);
1743 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001744 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 info.cellX = c.getInt(cellXIndex);
1746 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001747 info.spanX = 1;
1748 info.spanY = 1;
1749 // Skip loading items that are out of bounds
1750 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1751 if (checkItemDimensions(info)) {
1752 Log.d(TAG, "Skipped loading out of bounds shortcut: "
1753 + info.intent);
1754 continue;
1755 }
1756 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 // check & update map of what's occupied
1758 if (!checkItemPlacement(occupied, info)) {
1759 break;
1760 }
1761
1762 switch (container) {
1763 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1764 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1765 sBgWorkspaceItems.add(info);
1766 break;
1767 default:
1768 // Item is in a user folder
1769 FolderInfo folderInfo =
1770 findOrMakeFolder(sBgFolders, container);
1771 folderInfo.add(info);
1772 break;
1773 }
1774 sBgItemsIdMap.put(info.id, info);
1775
1776 // now that we've loaded everthing re-save it with the
1777 // icon in case it disappears somehow.
1778 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001779 } else {
1780 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 }
1782 break;
1783
1784 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1785 id = c.getLong(idIndex);
1786 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1787
1788 folderInfo.title = c.getString(titleIndex);
1789 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001792 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 folderInfo.cellX = c.getInt(cellXIndex);
1794 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001795 folderInfo.spanX = 1;
1796 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797
Winson Chung5f8afe62013-08-12 16:19:28 -07001798 // Skip loading items that are out of bounds
1799 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1800 int iconSpan = 1;
1801 if (checkItemDimensions(folderInfo)) {
1802 Log.d(TAG, "Skipped loading out of bounds folder");
1803 continue;
1804 }
1805 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001806 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001808 break;
1809 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001810
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1813 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1814 sBgWorkspaceItems.add(folderInfo);
1815 break;
Joe Onorato36115782010-06-17 13:28:48 -04001816 }
Joe Onorato17a89222011-02-08 17:26:11 -08001817
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1819 sBgFolders.put(folderInfo.id, folderInfo);
1820 break;
1821
1822 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1823 // Read all Launcher-specific widget details
1824 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001825 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 final AppWidgetProviderInfo provider =
1828 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 if (!isSafeMode && (provider == null || provider.provider == null ||
1831 provider.provider.getPackageName() == null)) {
1832 String log = "Deleting widget that isn't installed anymore: id="
1833 + id + " appWidgetId=" + appWidgetId;
1834 Log.e(TAG, log);
1835 Launcher.sDumpLogs.add(log);
1836 itemsToRemove.add(id);
1837 } else {
1838 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1839 provider.provider);
1840 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001841 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 appWidgetInfo.cellX = c.getInt(cellXIndex);
1843 appWidgetInfo.cellY = c.getInt(cellYIndex);
1844 appWidgetInfo.spanX = c.getInt(spanXIndex);
1845 appWidgetInfo.spanY = c.getInt(spanYIndex);
1846 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1847 appWidgetInfo.minSpanX = minSpan[0];
1848 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001849
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 container = c.getInt(containerIndex);
1851 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1852 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1853 Log.e(TAG, "Widget found where container != " +
1854 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1855 continue;
1856 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857
Adam Cohene25af792013-06-06 23:08:25 -07001858 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001859 // Skip loading items that are out of bounds
1860 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1861 if (checkItemDimensions(appWidgetInfo)) {
1862 Log.d(TAG, "Skipped loading out of bounds app widget");
1863 continue;
1864 }
1865 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 // check & update map of what's occupied
1867 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1868 break;
1869 }
1870 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1871 sBgAppWidgets.add(appWidgetInfo);
1872 }
Joe Onorato36115782010-06-17 13:28:48 -04001873 break;
1874 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 } catch (Exception e) {
1876 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001877 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001878 }
1879 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001880 if (c != null) {
1881 c.close();
1882 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 }
1884
1885 if (itemsToRemove.size() > 0) {
1886 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1887 LauncherSettings.Favorites.CONTENT_URI);
1888 // Remove dead items
1889 for (long id : itemsToRemove) {
1890 if (DEBUG_LOADERS) {
1891 Log.d(TAG, "Removed id = " + id);
1892 }
1893 // Don't notify content observers
1894 try {
1895 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1896 null, null);
1897 } catch (RemoteException e) {
1898 Log.w(TAG, "Could not remove id = " + id);
1899 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001900 }
1901 }
1902
Winson Chungc763c4e2013-07-19 13:49:06 -07001903 if (loadedOldDb) {
Winson Chungd64d1762013-08-20 14:37:16 -07001904 Log.w(TAG, "10249126 - loadWorkspace - loadedOldDb");
Adam Cohendcd297f2013-06-18 13:13:40 -07001905 long maxScreenId = 0;
1906 // If we're importing we use the old screen order.
1907 for (ItemInfo item: sBgItemsIdMap.values()) {
1908 long screenId = item.screenId;
1909 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1910 !sBgWorkspaceScreens.contains(screenId)) {
Winson Chung76828c82013-08-19 15:43:29 -07001911 Log.w(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001912 sBgWorkspaceScreens.add(screenId);
1913 if (screenId > maxScreenId) {
1914 maxScreenId = screenId;
1915 }
1916 }
1917 }
1918 Collections.sort(sBgWorkspaceScreens);
1919 mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1920 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001921
1922 // Update the max item id after we load an old db
1923 long maxItemId = 0;
1924 // If we're importing we use the old screen order.
1925 for (ItemInfo item: sBgItemsIdMap.values()) {
1926 maxItemId = Math.max(maxItemId, item.id);
1927 }
1928 LauncherAppState app = LauncherAppState.getInstance();
1929 app.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001930 } else {
Winson Chungd64d1762013-08-20 14:37:16 -07001931 Log.w(TAG, "10249126 - loadWorkspace - !loadedOldDb");
Winson Chung76828c82013-08-19 15:43:29 -07001932 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1933 for (Integer i : orderedScreens.keySet()) {
1934 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 }
1936
1937 // Remove any empty screens
1938 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1939 unusedScreens.addAll(sBgWorkspaceScreens);
1940
1941 for (ItemInfo item: sBgItemsIdMap.values()) {
1942 long screenId = item.screenId;
1943
1944 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1945 unusedScreens.contains(screenId)) {
1946 unusedScreens.remove(screenId);
1947 }
1948 }
1949
1950 // If there are any empty screens remove them, and update.
1951 if (unusedScreens.size() != 0) {
1952 sBgWorkspaceScreens.removeAll(unusedScreens);
1953 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1954 }
1955 }
1956
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 if (DEBUG_LOADERS) {
1958 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1959 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 int nScreens = occupied.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001961 for (int y = 0; y < mCellCountY; y++) {
1962 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001963
Daniel Sandler566da102013-06-25 23:43:45 -04001964 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001965 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001967 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001968 line += " | ";
1969 }
1970 for (int x = 0; x < mCellCountX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 }
Joe Onorato36115782010-06-17 13:28:48 -04001973 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001975 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976 }
Joe Onorato36115782010-06-17 13:28:48 -04001977 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001978 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001979 }
1980
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 /** Filters the set of items who are directly or indirectly (via another container) on the
1982 * specified screen. */
1983 private void filterCurrentWorkspaceItems(int currentScreen,
1984 ArrayList<ItemInfo> allWorkspaceItems,
1985 ArrayList<ItemInfo> currentScreenItems,
1986 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001987 // Purge any null ItemInfos
1988 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1989 while (iter.hasNext()) {
1990 ItemInfo i = iter.next();
1991 if (i == null) {
1992 iter.remove();
1993 }
1994 }
1995
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 // If we aren't filtering on a screen, then the set of items to load is the full set of
1997 // items given.
1998 if (currentScreen < 0) {
1999 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002000 }
2001
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 // Order the set of items by their containers first, this allows use to walk through the
2003 // list sequentially, build up a list of containers that are in the specified screen,
2004 // as well as all items in those containers.
2005 Set<Long> itemsOnScreen = new HashSet<Long>();
2006 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2007 @Override
2008 public int compare(ItemInfo lhs, ItemInfo rhs) {
2009 return (int) (lhs.container - rhs.container);
2010 }
2011 });
2012 for (ItemInfo info : allWorkspaceItems) {
2013 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002014 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 currentScreenItems.add(info);
2016 itemsOnScreen.add(info.id);
2017 } else {
2018 otherScreenItems.add(info);
2019 }
2020 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2021 currentScreenItems.add(info);
2022 itemsOnScreen.add(info.id);
2023 } else {
2024 if (itemsOnScreen.contains(info.container)) {
2025 currentScreenItems.add(info);
2026 itemsOnScreen.add(info.id);
2027 } else {
2028 otherScreenItems.add(info);
2029 }
2030 }
2031 }
2032 }
2033
2034 /** Filters the set of widgets which are on the specified screen. */
2035 private void filterCurrentAppWidgets(int currentScreen,
2036 ArrayList<LauncherAppWidgetInfo> appWidgets,
2037 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2038 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2039 // If we aren't filtering on a screen, then the set of items to load is the full set of
2040 // widgets given.
2041 if (currentScreen < 0) {
2042 currentScreenWidgets.addAll(appWidgets);
2043 }
2044
2045 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002046 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002047 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002048 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 currentScreenWidgets.add(widget);
2050 } else {
2051 otherScreenWidgets.add(widget);
2052 }
2053 }
2054 }
2055
2056 /** Filters the set of folders which are on the specified screen. */
2057 private void filterCurrentFolders(int currentScreen,
2058 HashMap<Long, ItemInfo> itemsIdMap,
2059 HashMap<Long, FolderInfo> folders,
2060 HashMap<Long, FolderInfo> currentScreenFolders,
2061 HashMap<Long, FolderInfo> otherScreenFolders) {
2062 // If we aren't filtering on a screen, then the set of items to load is the full set of
2063 // widgets given.
2064 if (currentScreen < 0) {
2065 currentScreenFolders.putAll(folders);
2066 }
2067
2068 for (long id : folders.keySet()) {
2069 ItemInfo info = itemsIdMap.get(id);
2070 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002071 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002072 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002073 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 currentScreenFolders.put(id, folder);
2075 } else {
2076 otherScreenFolders.put(id, folder);
2077 }
2078 }
2079 }
2080
2081 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2082 * right) */
2083 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2084 // 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) {
2088 int cellCountX = LauncherModel.getCellCountX();
2089 int cellCountY = LauncherModel.getCellCountY();
2090 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}