blob: cd37a167c92fadb5a4e61f6c572ee7ab5b056e74 [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);
Joe Onorato64e6be72010-03-05 15:05:52 -0500159 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700160 public void bindComponentsRemoved(ArrayList<String> packageNames,
161 ArrayList<ApplicationInfo> appInfos,
162 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200163 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100164 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700165 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung64359a52013-07-08 17:17:08 -0700168 public interface ItemInfoFilter {
169 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
170 }
171
Daniel Sandlere4f98912013-06-25 15:13:26 -0400172 LauncherModel(LauncherAppState app, IconCache iconCache) {
173 final Context context = app.getContext();
174
Winson Chungee055712013-07-30 14:46:24 -0700175 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400176 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700177 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800178 mIconCache = iconCache;
179
180 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400181 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400182
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700184 Configuration config = res.getConfiguration();
185 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800186 }
187
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700188 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
189 * posted on the main thread handler. */
190 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700191 runOnMainThread(r, 0);
192 }
193 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 if (sWorkerThread.getThreadId() == Process.myTid()) {
195 // If we are on the worker thread, post onto the main handler
196 mHandler.post(r);
197 } else {
198 r.run();
199 }
200 }
201
202 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
203 * posted on the worker thread handler. */
204 private static void runOnWorkerThread(Runnable r) {
205 if (sWorkerThread.getThreadId() == Process.myTid()) {
206 r.run();
207 } else {
208 // If we are not on the worker thread, then post to the worker handler
209 sWorker.post(r);
210 }
211 }
212
Winson Chungc9168342013-06-26 14:54:55 -0700213 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
214 long screen) {
215 final int xCount = LauncherModel.getCellCountX();
216 final int yCount = LauncherModel.getCellCountY();
217 boolean[][] occupied = new boolean[xCount][yCount];
218
219 int cellX, cellY, spanX, spanY;
220 for (int i = 0; i < items.size(); ++i) {
221 final ItemInfo item = items.get(i);
222 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
223 if (item.screenId == screen) {
224 cellX = item.cellX;
225 cellY = item.cellY;
226 spanX = item.spanX;
227 spanY = item.spanY;
228 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
229 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
230 occupied[x][y] = true;
231 }
232 }
233 }
234 }
235 }
236
237 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
238 }
239 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700240 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700241 int firstScreenIndex,
242 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700243 // Lock on the app so that we don't try and get the items while apps are being added
244 LauncherAppState app = LauncherAppState.getInstance();
245 LauncherModel model = app.getModel();
246 boolean found = false;
247 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700248 if (sWorkerThread.getThreadId() != Process.myTid()) {
249 // Flush the LauncherModel worker thread, so that if we just did another
250 // processInstallShortcut, we give it time for its shortcut to get added to the
251 // database (getItemsInLocalCoordinates reads the database)
252 model.flushWorkerThread();
253 }
Winson Chungc9168342013-06-26 14:54:55 -0700254 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700255
256 // Try adding to the workspace screens incrementally, starting at the default or center
257 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700258 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
259 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700260 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700261 int[] tmpCoordinates = new int[2];
262 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700263 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700264 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700265 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700266 }
267 }
268 }
Winson Chungc9168342013-06-26 14:54:55 -0700269 return null;
270 }
271
Winson Chung997a9232013-07-24 15:33:46 -0700272 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
273 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
274 addAndBindAddedApps(context, added, cb);
275 }
276 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chung64359a52013-07-08 17:17:08 -0700277 final Callbacks callbacks) {
Winson Chung997a9232013-07-24 15:33:46 -0700278 if (added.isEmpty()) {
279 throw new RuntimeException("EMPTY ADDED ARRAY?");
280 }
Winson Chung64359a52013-07-08 17:17:08 -0700281 // Process the newly added applications and add them to the database first
282 Runnable r = new Runnable() {
283 public void run() {
284 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
285 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
286
Winson Chung76828c82013-08-19 15:43:29 -0700287 // Get the list of workspace screens. We need to append to this list and
288 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
289 // called.
290 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
291 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
292 for (Integer i : orderedScreens.keySet()) {
293 long screenId = orderedScreens.get(i);
294 workspaceScreens.add(screenId);
295 }
296
Winson Chung64359a52013-07-08 17:17:08 -0700297 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700298 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700299 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700300 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700301 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700302 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700303
304 // Short-circuit this logic if the icon exists somewhere on the workspace
305 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
306 continue;
307 }
308
309 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700310 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700311 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700312 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700313 if (coords == null) {
Winson Chung64359a52013-07-08 17:17:08 -0700314 LauncherAppState appState = LauncherAppState.getInstance();
315 LauncherProvider lp = appState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700316
317 // If we can't find a valid position, then just add a new screen.
318 // This takes time so we need to re-queue the add until the new
319 // page is added. Create as many screens as necessary to satisfy
320 // the startSearchPageIndex.
321 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700322 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700323 while (numPagesToAdd > 0) {
324 long screenId = lp.generateNewScreenId();
Winson Chung76828c82013-08-19 15:43:29 -0700325 Log.w(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -0700326 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700327 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700328 addedWorkspaceScreensFinal.add(screenId);
329 numPagesToAdd--;
330 }
Winson Chung76828c82013-08-19 15:43:29 -0700331
Winson Chung64359a52013-07-08 17:17:08 -0700332 // Find the coordinate again
333 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700334 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700335 }
336 if (coords == null) {
337 throw new RuntimeException("Coordinates should not be null");
338 }
339
Winson Chung997a9232013-07-24 15:33:46 -0700340 ShortcutInfo shortcutInfo;
341 if (a instanceof ShortcutInfo) {
342 shortcutInfo = (ShortcutInfo) a;
343 } else if (a instanceof ApplicationInfo) {
344 shortcutInfo = ((ApplicationInfo) a).makeShortcut();
345 } else {
346 throw new RuntimeException("Unexpected info type");
347 }
Winson Chung64359a52013-07-08 17:17:08 -0700348 // Add the shortcut to the db
349 addItemToDatabase(context, shortcutInfo,
350 LauncherSettings.Favorites.CONTAINER_DESKTOP,
351 coords.first, coords.second[0], coords.second[1], false);
352 // Save the ShortcutInfo for binding in the workspace
353 addedShortcutsFinal.add(shortcutInfo);
354 }
355 }
356
Winson Chung76828c82013-08-19 15:43:29 -0700357 // Update the workspace screens
358 updateWorkspaceScreenOrder(context, workspaceScreens);
359
Winson Chung997a9232013-07-24 15:33:46 -0700360 if (!addedShortcutsFinal.isEmpty()) {
361 runOnMainThread(new Runnable() {
362 public void run() {
363 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
364 if (callbacks == cb && cb != null) {
365 callbacks.bindAddScreens(addedWorkspaceScreensFinal);
366
367 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
368 long lastScreenId = info.screenId;
369 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
370 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
371 for (ItemInfo i : addedShortcutsFinal) {
372 if (i.screenId == lastScreenId) {
373 addAnimated.add(i);
374 } else {
375 addNotAnimated.add(i);
376 }
377 }
378 // We add the items without animation on non-visible pages, and with
379 // animations on the new page (which we will try and snap to).
380 if (!addNotAnimated.isEmpty()) {
381 callbacks.bindItems(addNotAnimated, 0,
382 addNotAnimated.size(), false);
383 }
384 if (!addAnimated.isEmpty()) {
385 callbacks.bindItems(addAnimated, 0,
386 addAnimated.size(), true);
387 }
388 }
Winson Chung64359a52013-07-08 17:17:08 -0700389 }
Winson Chung997a9232013-07-24 15:33:46 -0700390 });
391 }
Winson Chung64359a52013-07-08 17:17:08 -0700392 }
393 };
394 runOnWorkerThread(r);
395 }
396
Joe Onorato56d82912010-03-07 14:32:10 -0500397 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800398 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400
Winson Chung81b52252012-08-27 15:34:29 -0700401 public void unbindItemInfosAndClearQueuedBindRunnables() {
402 if (sWorkerThread.getThreadId() == Process.myTid()) {
403 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
404 "main thread");
405 }
406
407 // Clear any deferred bind runnables
408 mDeferredBindRunnables.clear();
409 // Remove any queued bind runnables
410 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
411 // Unbind all the workspace items
412 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700413 }
414
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700415 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700416 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700417 // Ensure that we don't use the same workspace items data structure on the main thread
418 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700419 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
420 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700421 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
423 tmpAppWidgets.addAll(sBgAppWidgets);
424 }
425 Runnable r = new Runnable() {
426 @Override
427 public void run() {
428 for (ItemInfo item : tmpWorkspaceItems) {
429 item.unbind();
430 }
431 for (ItemInfo item : tmpAppWidgets) {
432 item.unbind();
433 }
434 }
435 };
436 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700437 }
438
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439 /**
440 * Adds an item to the DB if it was not created previously, or move it to a new
441 * <container, screen, cellX, cellY>
442 */
443 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700444 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 if (item.container == ItemInfo.NO_ID) {
446 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700447 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 } else {
449 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700450 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452 }
453
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700454 static void checkItemInfoLocked(
455 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
456 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
457 if (modelItem != null && item != modelItem) {
458 // check all the data is consistent
459 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
460 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
461 ShortcutInfo shortcut = (ShortcutInfo) item;
462 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
463 modelShortcut.intent.filterEquals(shortcut.intent) &&
464 modelShortcut.id == shortcut.id &&
465 modelShortcut.itemType == shortcut.itemType &&
466 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700467 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700468 modelShortcut.cellX == shortcut.cellX &&
469 modelShortcut.cellY == shortcut.cellY &&
470 modelShortcut.spanX == shortcut.spanX &&
471 modelShortcut.spanY == shortcut.spanY &&
472 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
473 (modelShortcut.dropPos != null &&
474 shortcut.dropPos != null &&
475 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
476 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
477 // For all intents and purposes, this is the same object
478 return;
479 }
480 }
481
482 // the modelItem needs to match up perfectly with item if our model is
483 // to be consistent with the database-- for now, just require
484 // modelItem == item or the equality check above
485 String msg = "item: " + ((item != null) ? item.toString() : "null") +
486 "modelItem: " +
487 ((modelItem != null) ? modelItem.toString() : "null") +
488 "Error: ItemInfo passed to checkItemInfo doesn't match original";
489 RuntimeException e = new RuntimeException(msg);
490 if (stackTrace != null) {
491 e.setStackTrace(stackTrace);
492 }
Adam Cohene25af792013-06-06 23:08:25 -0700493 // TODO: something breaks this in the upgrade path
494 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700495 }
496 }
497
Michael Jurka816474f2012-06-25 14:49:02 -0700498 static void checkItemInfo(final ItemInfo item) {
499 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
500 final long itemId = item.id;
501 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700502 public void run() {
503 synchronized (sBgLock) {
504 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700505 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700506 }
507 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700508 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700509 }
510
Michael Jurkac9d95c52011-08-29 14:03:34 -0700511 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
512 final ItemInfo item, final String callingFunction) {
513 final long itemId = item.id;
514 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
515 final ContentResolver cr = context.getContentResolver();
516
Adam Cohen487f7dd2012-06-28 18:12:10 -0700517 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700518 Runnable r = new Runnable() {
519 public void run() {
520 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700521 updateItemArrays(item, itemId, stackTrace);
522 }
523 };
524 runOnWorkerThread(r);
525 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700526
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700527 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
528 final ArrayList<ItemInfo> items, final String callingFunction) {
529 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700530
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700531 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
532 Runnable r = new Runnable() {
533 public void run() {
534 ArrayList<ContentProviderOperation> ops =
535 new ArrayList<ContentProviderOperation>();
536 int count = items.size();
537 for (int i = 0; i < count; i++) {
538 ItemInfo item = items.get(i);
539 final long itemId = item.id;
540 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
541 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700542
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700543 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
544 updateItemArrays(item, itemId, stackTrace);
545
546 }
547 try {
548 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
549 } catch (Exception e) {
550 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700551 }
552 }
553 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700554 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700555 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700556
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700557 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
558 // Lock on mBgLock *after* the db operation
559 synchronized (sBgLock) {
560 checkItemInfoLocked(itemId, item, stackTrace);
561
562 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
563 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
564 // Item is in a folder, make sure this folder exists
565 if (!sBgFolders.containsKey(item.container)) {
566 // An items container is being set to a that of an item which is not in
567 // the list of Folders.
568 String msg = "item: " + item + " container being set to: " +
569 item.container + ", not in the list of folders";
570 Log.e(TAG, msg);
571 Launcher.dumpDebugLogsToConsole();
572 }
573 }
574
575 // Items are added/removed from the corresponding FolderInfo elsewhere, such
576 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
577 // that are on the desktop, as appropriate
578 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
579 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
580 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
581 switch (modelItem.itemType) {
582 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
583 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
584 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
585 if (!sBgWorkspaceItems.contains(modelItem)) {
586 sBgWorkspaceItems.add(modelItem);
587 }
588 break;
589 default:
590 break;
591 }
592 } else {
593 sBgWorkspaceItems.remove(modelItem);
594 }
595 }
596 }
597
Michael Jurkac7700af2013-05-14 20:17:58 +0200598 public void flushWorkerThread() {
599 mFlushingWorkerThread = true;
600 Runnable waiter = new Runnable() {
601 public void run() {
602 synchronized (this) {
603 notifyAll();
604 mFlushingWorkerThread = false;
605 }
606 }
607 };
608
609 synchronized(waiter) {
610 runOnWorkerThread(waiter);
611 if (mLoaderTask != null) {
612 synchronized(mLoaderTask) {
613 mLoaderTask.notify();
614 }
615 }
616 boolean success = false;
617 while (!success) {
618 try {
619 waiter.wait();
620 success = true;
621 } catch (InterruptedException e) {
622 }
623 }
624 }
625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700629 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700630 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700632 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
634 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700635 Launcher.sDumpLogs.add(transaction);
636 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 item.cellX = cellX;
639 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700640
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 // We store hotseat items in canonical form which is this orientation invariant position
642 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649
650 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
653 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700657 }
658
659 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
661 * cellX, cellY have already been updated on the ItemInfos.
662 */
663 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
664 final long container, final int screen) {
665
666 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
667 int count = items.size();
668
669 for (int i = 0; i < count; i++) {
670 ItemInfo item = items.get(i);
671 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700672 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
674 + item.cellX + ", " + item.cellY + ")";
675 Launcher.sDumpLogs.add(transaction);
676 item.container = container;
677
678 // We store hotseat items in canonical form which is this orientation invariant position
679 // in the hotseat
680 if (context instanceof Launcher && screen < 0 &&
681 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683 item.cellY);
684 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 }
687
688 final ContentValues values = new ContentValues();
689 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
690 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
691 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700692 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700693
694 contentValues.add(values);
695 }
696 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
697 }
698
699 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800701 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700704 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
706 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700707 Launcher.sDumpLogs.add(transaction);
708 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800709 item.cellX = cellX;
710 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700711 item.spanX = spanX;
712 item.spanY = spanY;
713
714 // We store hotseat items in canonical form which is this orientation invariant position
715 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700719 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700721 }
Adam Cohend4844c32011-02-18 19:25:06 -0800722
Adam Cohend4844c32011-02-18 19:25:06 -0800723 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800724 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700725 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
726 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
727 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
728 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700729 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800730
Michael Jurka816474f2012-06-25 14:49:02 -0700731 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700732 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700733
734 /**
735 * Update an item to the database in a specified container.
736 */
737 static void updateItemInDatabase(Context context, final ItemInfo item) {
738 final ContentValues values = new ContentValues();
739 item.onAddToDatabase(values);
740 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
741 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800742 }
743
744 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 * Returns true if the shortcuts already exists in the database.
746 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 static boolean shortcutExists(Context context, String title, Intent intent) {
749 final ContentResolver cr = context.getContentResolver();
750 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
751 new String[] { "title", "intent" }, "title=? and intent=?",
752 new String[] { title, intent.toUri(0) }, null);
753 boolean result = false;
754 try {
755 result = c.moveToFirst();
756 } finally {
757 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700760 }
761
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700763 * Returns an ItemInfo array containing all the items in the LauncherModel.
764 * The ItemInfo.id is not set through this function.
765 */
766 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
767 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
768 final ContentResolver cr = context.getContentResolver();
769 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
770 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
771 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
772 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
773
774 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
775 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
776 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
777 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
778 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
779 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
780 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
781
782 try {
783 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700784 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700785 item.cellX = c.getInt(cellXIndex);
786 item.cellY = c.getInt(cellYIndex);
787 item.spanX = c.getInt(spanXIndex);
788 item.spanY = c.getInt(spanYIndex);
789 item.container = c.getInt(containerIndex);
790 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700791 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700792
793 items.add(item);
794 }
795 } catch (Exception e) {
796 items.clear();
797 } finally {
798 c.close();
799 }
800
801 return items;
802 }
803
804 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
806 */
807 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
808 final ContentResolver cr = context.getContentResolver();
809 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
810 "_id=? and (itemType=? or itemType=?)",
811 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700812 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700813
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 try {
815 if (c.moveToFirst()) {
816 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
817 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
818 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
819 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
820 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
821 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 FolderInfo folderInfo = null;
824 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700825 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
826 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700828 }
829
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 folderInfo.title = c.getString(titleIndex);
831 folderInfo.id = id;
832 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700833 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700834 folderInfo.cellX = c.getInt(cellXIndex);
835 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836
837 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 } finally {
840 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700841 }
842
843 return null;
844 }
845
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 /**
847 * Add an item to the database in a specified container. Sets the container, screen, cellX and
848 * cellY fields of the item. Also assigns an ID to the item.
849 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 item.cellX = cellX;
854 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 // We store hotseat items in canonical form which is this orientation invariant position
856 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700859 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863
864 final ContentValues values = new ContentValues();
865 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 item.onAddToDatabase(values);
867
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400868 LauncherAppState app = LauncherAppState.getInstance();
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 item.id = app.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700872
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700874 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700875 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700877 + cellY + ")";
878 Launcher.sDumpLogs.add(transaction);
879 Log.d(TAG, transaction);
880
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700881 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
882 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700885 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700886 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 sBgItemsIdMap.put(item.id, item);
888 switch (item.itemType) {
889 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
890 sBgFolders.put(item.id, (FolderInfo) item);
891 // Fall through
892 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
893 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
894 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
895 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
896 sBgWorkspaceItems.add(item);
897 } else {
898 if (!sBgFolders.containsKey(item.container)) {
899 // Adding an item to a folder that doesn't exist.
900 String msg = "adding item: " + item + " to a folder that " +
901 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700902 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 break;
907 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
909 break;
910 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 }
912 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700915 }
916
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700918 * Creates a new unique child id, for a given cell span across all layouts.
919 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700920 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700922 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700924 }
925
Adam Cohend22015c2010-07-26 22:02:18 -0700926 static int getCellCountX() {
927 return mCellCountX;
Winson Chungaafa03c2010-06-11 17:34:16 -0700928 }
929
Adam Cohend22015c2010-07-26 22:02:18 -0700930 static int getCellCountY() {
931 return mCellCountY;
Winson Chungaafa03c2010-06-11 17:34:16 -0700932 }
933
934 /**
935 * Updates the model orientation helper to take into account the current layout dimensions
936 * when performing local/canonical coordinate transformations.
937 */
938 static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) {
Adam Cohend22015c2010-07-26 22:02:18 -0700939 mCellCountX = shortAxisCellCount;
940 mCellCountY = longAxisCellCount;
Winson Chungaafa03c2010-06-11 17:34:16 -0700941 }
942
943 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700944 * Removes the specified item from the database
945 * @param context
946 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700948 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700950 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700951
Michael Jurka83df1882011-08-31 20:59:26 -0700952 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700953 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700954 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700956 ", " + item.cellY + ")";
957 Launcher.sDumpLogs.add(transaction);
958 Log.d(TAG, transaction);
959
Michael Jurkac9d95c52011-08-29 14:03:34 -0700960 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700961
962 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700963 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700964 switch (item.itemType) {
965 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
966 sBgFolders.remove(item.id);
967 for (ItemInfo info: sBgItemsIdMap.values()) {
968 if (info.container == item.id) {
969 // We are deleting a folder which still contains items that
970 // think they are contained by that folder.
971 String msg = "deleting a folder (" + item + ") which still " +
972 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700973 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700976 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700977 sBgWorkspaceItems.remove(item);
978 break;
979 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
980 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
981 sBgWorkspaceItems.remove(item);
982 break;
983 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
984 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
985 break;
986 }
987 sBgItemsIdMap.remove(item.id);
988 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700989 }
990 }
Michael Jurka83df1882011-08-31 20:59:26 -0700991 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700992 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 }
994
995 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 * Update the order of the workspace screens in the database. The array list contains
997 * a list of screen ids in the order that they should appear.
998 */
Winson Chungc9168342013-06-26 14:54:55 -0700999 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -07001000 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 final ContentResolver cr = context.getContentResolver();
1002 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1003
1004 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001005 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 while (iter.hasNext()) {
1007 long id = iter.next();
1008 if (id < 0) {
1009 iter.remove();
1010 }
1011 }
1012
1013 Runnable r = new Runnable() {
1014 @Override
1015 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 // Clear the table
1017 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001018 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 ContentValues[] values = new ContentValues[count];
1020 for (int i = 0; i < count; i++) {
1021 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001022 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1024 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Winson Chung76828c82013-08-19 15:43:29 -07001025 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001026 values[i] = v;
1027 }
1028 cr.bulkInsert(uri, values);
1029 sBgWorkspaceScreens.clear();
1030 sBgWorkspaceScreens.addAll(screensCopy);
1031 }
1032 };
1033 runOnWorkerThread(r);
1034 }
1035
1036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 * Remove the contents of the specified folder from the database
1038 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001039 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 final ContentResolver cr = context.getContentResolver();
1041
Michael Jurkac9d95c52011-08-29 14:03:34 -07001042 Runnable r = new Runnable() {
1043 public void run() {
1044 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001046 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001047 sBgItemsIdMap.remove(info.id);
1048 sBgFolders.remove(info.id);
1049 sBgDbIconCache.remove(info);
1050 sBgWorkspaceItems.remove(info);
1051 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001052
Michael Jurkac9d95c52011-08-29 14:03:34 -07001053 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1054 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001055 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001056 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001057 for (ItemInfo childInfo : info.contents) {
1058 sBgItemsIdMap.remove(childInfo.id);
1059 sBgDbIconCache.remove(childInfo);
1060 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001061 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001062 }
1063 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001064 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
1066
1067 /**
1068 * Set this as the current Launcher activity object for the loader.
1069 */
1070 public void initialize(Callbacks callbacks) {
1071 synchronized (mLock) {
1072 mCallbacks = new WeakReference<Callbacks>(callbacks);
1073 }
1074 }
1075
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001076 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1078 * ACTION_PACKAGE_CHANGED.
1079 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001080 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001081 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001082 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001083
Joe Onorato36115782010-06-17 13:28:48 -04001084 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001085
Joe Onorato36115782010-06-17 13:28:48 -04001086 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1087 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1088 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1089 final String packageName = intent.getData().getSchemeSpecificPart();
1090 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091
Joe Onorato36115782010-06-17 13:28:48 -04001092 int op = PackageUpdatedTask.OP_NONE;
1093
1094 if (packageName == null || packageName.length() == 0) {
1095 // they sent us a bad intent
1096 return;
1097 }
1098
1099 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1100 op = PackageUpdatedTask.OP_UPDATE;
1101 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1102 if (!replacing) {
1103 op = PackageUpdatedTask.OP_REMOVE;
1104 }
1105 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1106 // later, we will update the package at this time
1107 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1108 if (!replacing) {
1109 op = PackageUpdatedTask.OP_ADD;
1110 } else {
1111 op = PackageUpdatedTask.OP_UPDATE;
1112 }
1113 }
1114
1115 if (op != PackageUpdatedTask.OP_NONE) {
1116 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1117 }
1118
1119 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001120 // First, schedule to add these apps back in.
1121 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1122 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1123 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001124 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001125 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1126 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1127 enqueuePackageUpdated(new PackageUpdatedTask(
1128 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001129 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001130 // If we have changed locale we need to clear out the labels in all apps/workspace.
1131 forceReload();
1132 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1133 // Check if configuration change was an mcc/mnc change which would affect app resources
1134 // and we would need to clear out the labels in all apps/workspace. Same handling as
1135 // above for ACTION_LOCALE_CHANGED
1136 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001137 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001138 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001139 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001140 forceReload();
1141 }
1142 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001143 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001144 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1145 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001146 if (mCallbacks != null) {
1147 Callbacks callbacks = mCallbacks.get();
1148 if (callbacks != null) {
1149 callbacks.bindSearchablesChanged();
1150 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001151 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001152 }
1153 }
1154
Reena Lee93f824a2011-09-23 17:20:28 -07001155 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001156 resetLoadedState(true, true);
1157
Reena Lee93f824a2011-09-23 17:20:28 -07001158 // Do this here because if the launcher activity is running it will be restarted.
1159 // If it's not running startLoaderFromBackground will merely tell it that it needs
1160 // to reload.
1161 startLoaderFromBackground();
1162 }
1163
Winson Chungf0c6ae02012-03-21 16:10:31 -07001164 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1165 synchronized (mLock) {
1166 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1167 // mWorkspaceLoaded to true later
1168 stopLoaderLocked();
1169 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1170 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1171 }
1172 }
1173
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001174 /**
1175 * When the launcher is in the background, it's possible for it to miss paired
1176 * configuration changes. So whenever we trigger the loader from the background
1177 * tell the launcher that it needs to re-run the loader when it comes back instead
1178 * of doing it now.
1179 */
1180 public void startLoaderFromBackground() {
1181 boolean runLoader = false;
1182 if (mCallbacks != null) {
1183 Callbacks callbacks = mCallbacks.get();
1184 if (callbacks != null) {
1185 // Only actually run the loader if they're not paused.
1186 if (!callbacks.setLoadOnResume()) {
1187 runLoader = true;
1188 }
1189 }
1190 }
1191 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001192 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001193 }
Joe Onorato36115782010-06-17 13:28:48 -04001194 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001195
Reena Lee93f824a2011-09-23 17:20:28 -07001196 // If there is already a loader task running, tell it to stop.
1197 // returns true if isLaunching() was true on the old task
1198 private boolean stopLoaderLocked() {
1199 boolean isLaunching = false;
1200 LoaderTask oldTask = mLoaderTask;
1201 if (oldTask != null) {
1202 if (oldTask.isLaunching()) {
1203 isLaunching = true;
1204 }
1205 oldTask.stopLocked();
1206 }
1207 return isLaunching;
1208 }
1209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001211 synchronized (mLock) {
1212 if (DEBUG_LOADERS) {
1213 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 // Clear any deferred bind-runnables from the synchronized load process
1217 // We must do this before any loading/binding is scheduled below.
1218 mDeferredBindRunnables.clear();
1219
Joe Onorato36115782010-06-17 13:28:48 -04001220 // Don't bother to start the thread if we know it's not going to do anything
1221 if (mCallbacks != null && mCallbacks.get() != null) {
1222 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001223 // also, don't downgrade isLaunching if we're already running
1224 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001225 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001226 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1227 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1228 } else {
1229 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1230 sWorker.post(mLoaderTask);
1231 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 }
1234 }
1235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001236 void bindRemainingSynchronousPages() {
1237 // Post the remaining side pages to be loaded
1238 if (!mDeferredBindRunnables.isEmpty()) {
1239 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001240 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001241 }
1242 mDeferredBindRunnables.clear();
1243 }
1244 }
1245
Joe Onorato36115782010-06-17 13:28:48 -04001246 public void stopLoader() {
1247 synchronized (mLock) {
1248 if (mLoaderTask != null) {
1249 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 }
1251 }
Joe Onorato36115782010-06-17 13:28:48 -04001252 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253
Winson Chung76828c82013-08-19 15:43:29 -07001254 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1255 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1256 final ContentResolver contentResolver = context.getContentResolver();
1257 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1258 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1259 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1260
1261 try {
1262 final int idIndex = sc.getColumnIndexOrThrow(
1263 LauncherSettings.WorkspaceScreens._ID);
1264 final int rankIndex = sc.getColumnIndexOrThrow(
1265 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1266 while (sc.moveToNext()) {
1267 try {
1268 long screenId = sc.getLong(idIndex);
1269 int rank = sc.getInt(rankIndex);
1270
1271 Log.w(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")");
1272
1273 orderedScreens.put(rank, screenId);
1274 } catch (Exception e) {
1275 Log.w(TAG, "Desktop items loading interrupted - invalid screens: ", e);
1276 }
1277 }
1278 } finally {
1279 sc.close();
1280 }
1281 return orderedScreens;
1282 }
1283
Michael Jurkac57b7a82011-08-09 22:02:20 -07001284 public boolean isAllAppsLoaded() {
1285 return mAllAppsLoaded;
1286 }
1287
Winson Chung36a62fe2012-05-06 18:04:42 -07001288 boolean isLoadingWorkspace() {
1289 synchronized (mLock) {
1290 if (mLoaderTask != null) {
1291 return mLoaderTask.isLoadingWorkspace();
1292 }
1293 }
1294 return false;
1295 }
1296
Joe Onorato36115782010-06-17 13:28:48 -04001297 /**
1298 * Runnable for the thread that loads the contents of the launcher:
1299 * - workspace icons
1300 * - widgets
1301 * - all apps icons
1302 */
1303 private class LoaderTask implements Runnable {
1304 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001305 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001306 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001307 private boolean mStopped;
1308 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001309
Winson Chungc3eecff2011-07-11 17:44:15 -07001310 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001311
1312 LoaderTask(Context context, boolean isLaunching) {
1313 mContext = context;
1314 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001315 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 }
1317
Joe Onorato36115782010-06-17 13:28:48 -04001318 boolean isLaunching() {
1319 return mIsLaunching;
1320 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321
Winson Chung36a62fe2012-05-06 18:04:42 -07001322 boolean isLoadingWorkspace() {
1323 return mIsLoadingAndBindingWorkspace;
1324 }
1325
Winson Chungc763c4e2013-07-19 13:49:06 -07001326 /** Returns whether this is an upgrade path */
1327 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001328 mIsLoadingAndBindingWorkspace = true;
1329
Joe Onorato36115782010-06-17 13:28:48 -04001330 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001331 if (DEBUG_LOADERS) {
1332 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001334
Winson Chungc763c4e2013-07-19 13:49:06 -07001335 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001336 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001337 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001338 synchronized (LoaderTask.this) {
1339 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001340 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001341 }
1342 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 }
1344 }
1345
Joe Onorato36115782010-06-17 13:28:48 -04001346 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001347 bindWorkspace(-1, isUpgradePath);
1348 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001349 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001350
Joe Onorato36115782010-06-17 13:28:48 -04001351 private void waitForIdle() {
1352 // Wait until the either we're stopped or the other threads are done.
1353 // This way we don't start loading all apps until the workspace has settled
1354 // down.
1355 synchronized (LoaderTask.this) {
1356 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001357
Joe Onorato36115782010-06-17 13:28:48 -04001358 mHandler.postIdle(new Runnable() {
1359 public void run() {
1360 synchronized (LoaderTask.this) {
1361 mLoadAndBindStepFinished = true;
1362 if (DEBUG_LOADERS) {
1363 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001364 }
Joe Onorato36115782010-06-17 13:28:48 -04001365 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001366 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001367 }
Joe Onorato36115782010-06-17 13:28:48 -04001368 });
1369
Michael Jurkac7700af2013-05-14 20:17:58 +02001370 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001371 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001372 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1373 // wait no longer than 1sec at a time
1374 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001375 } catch (InterruptedException ex) {
1376 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001377 }
1378 }
Joe Onorato36115782010-06-17 13:28:48 -04001379 if (DEBUG_LOADERS) {
1380 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001381 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001382 + "ms for previous step to finish binding");
1383 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001384 }
Joe Onorato36115782010-06-17 13:28:48 -04001385 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001386
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001387 void runBindSynchronousPage(int synchronousBindPage) {
1388 if (synchronousBindPage < 0) {
1389 // Ensure that we have a valid page index to load synchronously
1390 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1391 "valid page index");
1392 }
1393 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1394 // Ensure that we don't try and bind a specified page when the pages have not been
1395 // loaded already (we should load everything asynchronously in that case)
1396 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1397 }
1398 synchronized (mLock) {
1399 if (mIsLoaderTaskRunning) {
1400 // Ensure that we are never running the background loading at this point since
1401 // we also touch the background collections
1402 throw new RuntimeException("Error! Background loading is already running");
1403 }
1404 }
1405
1406 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1407 // data structures, we can't allow any other thread to touch that data, but because
1408 // this call is synchronous, we can get away with not locking).
1409
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001410 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001411 // operations from the previous activity. We need to ensure that all queued operations
1412 // are executed before any synchronous binding work is done.
1413 mHandler.flush();
1414
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001415 // Divide the set of loaded items into those that we are binding synchronously, and
1416 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001417 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1419 // arise from that.
1420 onlyBindAllApps();
1421 }
1422
Joe Onorato36115782010-06-17 13:28:48 -04001423 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001424 boolean isUpgrade = false;
1425
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426 synchronized (mLock) {
1427 mIsLoaderTaskRunning = true;
1428 }
Joe Onorato36115782010-06-17 13:28:48 -04001429 // Optimize for end-user experience: if the Launcher is up and // running with the
1430 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1431 // workspace first (default).
1432 final Callbacks cbk = mCallbacks.get();
Joe Onorato36115782010-06-17 13:28:48 -04001433 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001434 // Elevate priority when Home launches for the first time to avoid
1435 // starving at boot time. Staring at a blank home is not cool.
1436 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001437 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1438 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001439 android.os.Process.setThreadPriority(mIsLaunching
1440 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1441 }
Winson Chung64359a52013-07-08 17:17:08 -07001442 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001443 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001444
Joe Onorato36115782010-06-17 13:28:48 -04001445 if (mStopped) {
1446 break keep_running;
1447 }
1448
1449 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1450 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001451 synchronized (mLock) {
1452 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001453 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001454 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1455 }
1456 }
Joe Onorato36115782010-06-17 13:28:48 -04001457 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001458
1459 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001460 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1461 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001462
1463 // Restore the default thread priority after we are done loading items
1464 synchronized (mLock) {
1465 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1466 }
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468
Winson Chungaac01e12011-08-17 10:37:13 -07001469 // Update the saved icons if necessary
1470 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001471 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001472 for (Object key : sBgDbIconCache.keySet()) {
1473 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1474 }
1475 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001476 }
Winson Chungaac01e12011-08-17 10:37:13 -07001477
Winson Chungc763c4e2013-07-19 13:49:06 -07001478 // Ensure that all the applications that are in the system are represented on the home
1479 // screen.
Winson Chung76828c82013-08-19 15:43:29 -07001480 Log.w(TAG, "10249126 - verifyApplications(" + isUpgrade + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -07001481 if (!isUpgrade) {
1482 verifyApplications();
1483 }
1484
Joe Onorato36115782010-06-17 13:28:48 -04001485 // Clear out this reference, otherwise we end up holding it until all of the
1486 // callback runnables are done.
1487 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001488
Joe Onorato36115782010-06-17 13:28:48 -04001489 synchronized (mLock) {
1490 // If we are still the last one to be scheduled, remove ourselves.
1491 if (mLoaderTask == this) {
1492 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001494 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001495 }
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
1497
1498 public void stopLocked() {
1499 synchronized (LoaderTask.this) {
1500 mStopped = true;
1501 this.notify();
1502 }
1503 }
1504
1505 /**
1506 * Gets the callbacks object. If we've been stopped, or if the launcher object
1507 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1508 * object that was around when the deferred message was scheduled, and if there's
1509 * a new Callbacks object around then also return null. This will save us from
1510 * calling onto it with data that will be ignored.
1511 */
1512 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1513 synchronized (mLock) {
1514 if (mStopped) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 if (mCallbacks == null) {
1519 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001520 }
Joe Onorato36115782010-06-17 13:28:48 -04001521
1522 final Callbacks callbacks = mCallbacks.get();
1523 if (callbacks != oldCallbacks) {
1524 return null;
1525 }
1526 if (callbacks == null) {
1527 Log.w(TAG, "no mCallbacks");
1528 return null;
1529 }
1530
1531 return callbacks;
1532 }
1533 }
1534
Winson Chungc763c4e2013-07-19 13:49:06 -07001535 private void verifyApplications() {
1536 final Context context = mApp.getContext();
1537
1538 // Cross reference all the applications in our apps list with items in the workspace
1539 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001540 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001541 synchronized (sBgLock) {
1542 for (ApplicationInfo app : mBgAllAppsList.data) {
1543 tmpInfos = getItemInfoForComponentName(app.componentName);
Winson Chung76828c82013-08-19 15:43:29 -07001544 Log.w(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty());
Winson Chungc763c4e2013-07-19 13:49:06 -07001545 if (tmpInfos.isEmpty()) {
1546 // We are missing an application icon, so add this to the workspace
1547 added.add(app);
1548 // This is a rare event, so lets log it
1549 Log.e(TAG, "Missing Application on load: " + app);
1550 }
1551 }
1552 }
1553 if (!added.isEmpty()) {
1554 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1555 addAndBindAddedApps(context, added, cb);
1556 }
1557 }
1558
Winson Chung5f8afe62013-08-12 16:19:28 -07001559 private boolean checkItemDimensions(ItemInfo info) {
1560 return (info.cellX + info.spanX) > mCellCountX ||
1561 (info.cellY + info.spanY) > mCellCountY;
1562 }
1563
Joe Onorato36115782010-06-17 13:28:48 -04001564 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
1566 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001567 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1569 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1570 [(int) item.screenId][0] != null) {
1571 Log.e(TAG, "Error loading shortcut into hotseat " + item
1572 + " into position (" + item.screenId + ":" + item.cellX + ","
1573 + item.cellY + ") occupied by "
1574 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1575 [(int) item.screenId][0]);
1576 return false;
1577 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001578 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1580 items[(int) item.screenId][0] = item;
1581 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1585 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001586 return true;
1587 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001588
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 if (!occupied.containsKey(item.screenId)) {
1590 ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1];
1591 occupied.put(item.screenId, items);
1592 }
1593
1594 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001595 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001596 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1597 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001598 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001599 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001600 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001601 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001602 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001604 return false;
1605 }
1606 }
1607 }
1608 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1609 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001611 }
1612 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001613
Joe Onorato36115782010-06-17 13:28:48 -04001614 return true;
1615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616
Winson Chungc763c4e2013-07-19 13:49:06 -07001617 /** Returns whether this is an upgradge path */
1618 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001619 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001620
Joe Onorato36115782010-06-17 13:28:48 -04001621 final Context context = mContext;
1622 final ContentResolver contentResolver = context.getContentResolver();
1623 final PackageManager manager = context.getPackageManager();
1624 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1625 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001626
Michael Jurkab85f8a42012-04-25 15:48:32 -07001627 // Make sure the default workspace is loaded, if needed
Winson Chungc763c4e2013-07-19 13:49:06 -07001628 mApp.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001629
Winson Chungc763c4e2013-07-19 13:49:06 -07001630 // Check if we need to do any upgrade-path logic
1631 boolean loadedOldDb = mApp.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001632
Winson Chung2abf94d2012-07-18 18:16:38 -07001633 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001634 sBgWorkspaceItems.clear();
1635 sBgAppWidgets.clear();
1636 sBgFolders.clear();
1637 sBgItemsIdMap.clear();
1638 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001639 sBgWorkspaceScreens.clear();
Winson Chung76828c82013-08-19 15:43:29 -07001640 Log.w(TAG, "10249126 - loadWorkspace()");
Romain Guy5c16f3e2010-01-12 17:24:58 -08001641
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001643 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001644 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001645
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001646 // +1 for the hotseat (it can be larger than the workspace)
1647 // Load workspace in reverse order to ensure that latest items are loaded first (and
1648 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001651 try {
1652 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1653 final int intentIndex = c.getColumnIndexOrThrow
1654 (LauncherSettings.Favorites.INTENT);
1655 final int titleIndex = c.getColumnIndexOrThrow
1656 (LauncherSettings.Favorites.TITLE);
1657 final int iconTypeIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.ICON_TYPE);
1659 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1660 final int iconPackageIndex = c.getColumnIndexOrThrow(
1661 LauncherSettings.Favorites.ICON_PACKAGE);
1662 final int iconResourceIndex = c.getColumnIndexOrThrow(
1663 LauncherSettings.Favorites.ICON_RESOURCE);
1664 final int containerIndex = c.getColumnIndexOrThrow(
1665 LauncherSettings.Favorites.CONTAINER);
1666 final int itemTypeIndex = c.getColumnIndexOrThrow(
1667 LauncherSettings.Favorites.ITEM_TYPE);
1668 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1669 LauncherSettings.Favorites.APPWIDGET_ID);
1670 final int screenIndex = c.getColumnIndexOrThrow(
1671 LauncherSettings.Favorites.SCREEN);
1672 final int cellXIndex = c.getColumnIndexOrThrow
1673 (LauncherSettings.Favorites.CELLX);
1674 final int cellYIndex = c.getColumnIndexOrThrow
1675 (LauncherSettings.Favorites.CELLY);
1676 final int spanXIndex = c.getColumnIndexOrThrow
1677 (LauncherSettings.Favorites.SPANX);
1678 final int spanYIndex = c.getColumnIndexOrThrow(
1679 LauncherSettings.Favorites.SPANY);
1680 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1681 //final int displayModeIndex = c.getColumnIndexOrThrow(
1682 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 ShortcutInfo info;
1685 String intentDescription;
1686 LauncherAppWidgetInfo appWidgetInfo;
1687 int container;
1688 long id;
1689 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 while (!mStopped && c.moveToNext()) {
1692 try {
1693 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 switch (itemType) {
1696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001698 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 intentDescription = c.getString(intentIndex);
1700 try {
1701 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001702 ComponentName cn = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07001703 if (!isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001704 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001705 // Log the invalid package, and remove it from the db
1706 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1707 false);
Winson Chungee055712013-07-30 14:46:24 -07001708 contentResolver.delete(uri, null, null);
Winson Chung1323b482013-08-05 12:41:55 -07001709 Log.e(TAG, "Invalid package removed: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001710 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001711 // If apps can be on external storage, then we just
1712 // leave them for the user to remove (maybe add
1713 // visual treatment to it)
1714 Log.e(TAG, "Invalid package found: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001715 }
1716 continue;
1717 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 } catch (URISyntaxException e) {
1719 continue;
1720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001721
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1723 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1724 titleIndex, mLabelCache);
1725 } else {
1726 info = getShortcutInfo(c, context, iconTypeIndex,
1727 iconPackageIndex, iconResourceIndex, iconIndex,
1728 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001729
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 // App shortcuts that used to be automatically added to Launcher
1731 // didn't always have the correct intent flags set, so do that
1732 // here
1733 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001734 intent.getCategories() != null &&
1735 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001736 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 intent.addFlags(
1738 Intent.FLAG_ACTIVITY_NEW_TASK |
1739 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1740 }
Michael Jurka96879562012-03-22 05:54:33 -07001741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001744 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 container = c.getInt(containerIndex);
1747 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001748 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 info.cellX = c.getInt(cellXIndex);
1750 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001751 info.spanX = 1;
1752 info.spanY = 1;
1753 // Skip loading items that are out of bounds
1754 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1755 if (checkItemDimensions(info)) {
1756 Log.d(TAG, "Skipped loading out of bounds shortcut: "
1757 + info.intent);
1758 continue;
1759 }
1760 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 // check & update map of what's occupied
1762 if (!checkItemPlacement(occupied, info)) {
1763 break;
1764 }
1765
1766 switch (container) {
1767 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1768 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1769 sBgWorkspaceItems.add(info);
1770 break;
1771 default:
1772 // Item is in a user folder
1773 FolderInfo folderInfo =
1774 findOrMakeFolder(sBgFolders, container);
1775 folderInfo.add(info);
1776 break;
1777 }
1778 sBgItemsIdMap.put(info.id, info);
1779
1780 // now that we've loaded everthing re-save it with the
1781 // icon in case it disappears somehow.
1782 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001783 } else {
1784 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 }
1786 break;
1787
1788 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1789 id = c.getLong(idIndex);
1790 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1791
1792 folderInfo.title = c.getString(titleIndex);
1793 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001797 folderInfo.cellX = c.getInt(cellXIndex);
1798 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001799 folderInfo.spanX = 1;
1800 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801
Winson Chung5f8afe62013-08-12 16:19:28 -07001802 // Skip loading items that are out of bounds
1803 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1804 int iconSpan = 1;
1805 if (checkItemDimensions(folderInfo)) {
1806 Log.d(TAG, "Skipped loading out of bounds folder");
1807 continue;
1808 }
1809 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001810 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001812 break;
1813 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001814
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1817 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1818 sBgWorkspaceItems.add(folderInfo);
1819 break;
Joe Onorato36115782010-06-17 13:28:48 -04001820 }
Joe Onorato17a89222011-02-08 17:26:11 -08001821
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001822 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1823 sBgFolders.put(folderInfo.id, folderInfo);
1824 break;
1825
1826 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1827 // Read all Launcher-specific widget details
1828 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001829 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001830
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 final AppWidgetProviderInfo provider =
1832 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 if (!isSafeMode && (provider == null || provider.provider == null ||
1835 provider.provider.getPackageName() == null)) {
1836 String log = "Deleting widget that isn't installed anymore: id="
1837 + id + " appWidgetId=" + appWidgetId;
1838 Log.e(TAG, log);
1839 Launcher.sDumpLogs.add(log);
1840 itemsToRemove.add(id);
1841 } else {
1842 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1843 provider.provider);
1844 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001845 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 appWidgetInfo.cellX = c.getInt(cellXIndex);
1847 appWidgetInfo.cellY = c.getInt(cellYIndex);
1848 appWidgetInfo.spanX = c.getInt(spanXIndex);
1849 appWidgetInfo.spanY = c.getInt(spanYIndex);
1850 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1851 appWidgetInfo.minSpanX = minSpan[0];
1852 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001853
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 container = c.getInt(containerIndex);
1855 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1856 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1857 Log.e(TAG, "Widget found where container != " +
1858 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1859 continue;
1860 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861
Adam Cohene25af792013-06-06 23:08:25 -07001862 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001863 // Skip loading items that are out of bounds
1864 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1865 if (checkItemDimensions(appWidgetInfo)) {
1866 Log.d(TAG, "Skipped loading out of bounds app widget");
1867 continue;
1868 }
1869 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 // check & update map of what's occupied
1871 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1872 break;
1873 }
1874 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1875 sBgAppWidgets.add(appWidgetInfo);
1876 }
Joe Onorato36115782010-06-17 13:28:48 -04001877 break;
1878 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879 } catch (Exception e) {
1880 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 }
1883 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001884 if (c != null) {
1885 c.close();
1886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 }
1888
1889 if (itemsToRemove.size() > 0) {
1890 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1891 LauncherSettings.Favorites.CONTENT_URI);
1892 // Remove dead items
1893 for (long id : itemsToRemove) {
1894 if (DEBUG_LOADERS) {
1895 Log.d(TAG, "Removed id = " + id);
1896 }
1897 // Don't notify content observers
1898 try {
1899 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1900 null, null);
1901 } catch (RemoteException e) {
1902 Log.w(TAG, "Could not remove id = " + id);
1903 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001904 }
1905 }
1906
Winson Chungc763c4e2013-07-19 13:49:06 -07001907 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001908 long maxScreenId = 0;
1909 // If we're importing we use the old screen order.
1910 for (ItemInfo item: sBgItemsIdMap.values()) {
1911 long screenId = item.screenId;
1912 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1913 !sBgWorkspaceScreens.contains(screenId)) {
Winson Chung76828c82013-08-19 15:43:29 -07001914 Log.w(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 sBgWorkspaceScreens.add(screenId);
1916 if (screenId > maxScreenId) {
1917 maxScreenId = screenId;
1918 }
1919 }
1920 }
1921 Collections.sort(sBgWorkspaceScreens);
1922 mApp.getLauncherProvider().updateMaxScreenId(maxScreenId);
1923 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001924
1925 // Update the max item id after we load an old db
1926 long maxItemId = 0;
1927 // If we're importing we use the old screen order.
1928 for (ItemInfo item: sBgItemsIdMap.values()) {
1929 maxItemId = Math.max(maxItemId, item.id);
1930 }
1931 LauncherAppState app = LauncherAppState.getInstance();
1932 app.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001934 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1935 for (Integer i : orderedScreens.keySet()) {
1936 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 }
1938
1939 // Remove any empty screens
1940 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1941 unusedScreens.addAll(sBgWorkspaceScreens);
1942
1943 for (ItemInfo item: sBgItemsIdMap.values()) {
1944 long screenId = item.screenId;
1945
1946 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1947 unusedScreens.contains(screenId)) {
1948 unusedScreens.remove(screenId);
1949 }
1950 }
1951
1952 // If there are any empty screens remove them, and update.
1953 if (unusedScreens.size() != 0) {
1954 sBgWorkspaceScreens.removeAll(unusedScreens);
1955 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1956 }
1957 }
1958
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001959 if (DEBUG_LOADERS) {
1960 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1961 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 int nScreens = occupied.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001963 for (int y = 0; y < mCellCountY; y++) {
1964 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001965
Daniel Sandler566da102013-06-25 23:43:45 -04001966 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001967 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001969 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 line += " | ";
1971 }
1972 for (int x = 0; x < mCellCountX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 }
Joe Onorato36115782010-06-17 13:28:48 -04001975 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001977 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 }
Joe Onorato36115782010-06-17 13:28:48 -04001979 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001980 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001981 }
1982
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 /** Filters the set of items who are directly or indirectly (via another container) on the
1984 * specified screen. */
1985 private void filterCurrentWorkspaceItems(int currentScreen,
1986 ArrayList<ItemInfo> allWorkspaceItems,
1987 ArrayList<ItemInfo> currentScreenItems,
1988 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001989 // Purge any null ItemInfos
1990 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1991 while (iter.hasNext()) {
1992 ItemInfo i = iter.next();
1993 if (i == null) {
1994 iter.remove();
1995 }
1996 }
1997
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 // If we aren't filtering on a screen, then the set of items to load is the full set of
1999 // items given.
2000 if (currentScreen < 0) {
2001 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002002 }
2003
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002004 // Order the set of items by their containers first, this allows use to walk through the
2005 // list sequentially, build up a list of containers that are in the specified screen,
2006 // as well as all items in those containers.
2007 Set<Long> itemsOnScreen = new HashSet<Long>();
2008 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2009 @Override
2010 public int compare(ItemInfo lhs, ItemInfo rhs) {
2011 return (int) (lhs.container - rhs.container);
2012 }
2013 });
2014 for (ItemInfo info : allWorkspaceItems) {
2015 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 currentScreenItems.add(info);
2018 itemsOnScreen.add(info.id);
2019 } else {
2020 otherScreenItems.add(info);
2021 }
2022 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2023 currentScreenItems.add(info);
2024 itemsOnScreen.add(info.id);
2025 } else {
2026 if (itemsOnScreen.contains(info.container)) {
2027 currentScreenItems.add(info);
2028 itemsOnScreen.add(info.id);
2029 } else {
2030 otherScreenItems.add(info);
2031 }
2032 }
2033 }
2034 }
2035
2036 /** Filters the set of widgets which are on the specified screen. */
2037 private void filterCurrentAppWidgets(int currentScreen,
2038 ArrayList<LauncherAppWidgetInfo> appWidgets,
2039 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2040 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2041 // If we aren't filtering on a screen, then the set of items to load is the full set of
2042 // widgets given.
2043 if (currentScreen < 0) {
2044 currentScreenWidgets.addAll(appWidgets);
2045 }
2046
2047 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002048 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002050 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 currentScreenWidgets.add(widget);
2052 } else {
2053 otherScreenWidgets.add(widget);
2054 }
2055 }
2056 }
2057
2058 /** Filters the set of folders which are on the specified screen. */
2059 private void filterCurrentFolders(int currentScreen,
2060 HashMap<Long, ItemInfo> itemsIdMap,
2061 HashMap<Long, FolderInfo> folders,
2062 HashMap<Long, FolderInfo> currentScreenFolders,
2063 HashMap<Long, FolderInfo> otherScreenFolders) {
2064 // If we aren't filtering on a screen, then the set of items to load is the full set of
2065 // widgets given.
2066 if (currentScreen < 0) {
2067 currentScreenFolders.putAll(folders);
2068 }
2069
2070 for (long id : folders.keySet()) {
2071 ItemInfo info = itemsIdMap.get(id);
2072 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002073 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002075 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 currentScreenFolders.put(id, folder);
2077 } else {
2078 otherScreenFolders.put(id, folder);
2079 }
2080 }
2081 }
2082
2083 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2084 * right) */
2085 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
2086 // XXX: review this
2087 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002088 @Override
2089 public int compare(ItemInfo lhs, ItemInfo rhs) {
2090 int cellCountX = LauncherModel.getCellCountX();
2091 int cellCountY = LauncherModel.getCellCountY();
2092 int screenOffset = cellCountX * cellCountY;
2093 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002094 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002095 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002096 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002097 rhs.cellY * cellCountX + rhs.cellX);
2098 return (int) (lr - rr);
2099 }
2100 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002102
Adam Cohendcd297f2013-06-18 13:13:40 -07002103 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2104 final ArrayList<Long> orderedScreens) {
Winson Chung76828c82013-08-19 15:43:29 -07002105 Log.w(TAG, "10249126 - bindWorkspaceScreens()");
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 final Runnable r = new Runnable() {
2107 @Override
2108 public void run() {
2109 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2110 if (callbacks != null) {
2111 callbacks.bindScreens(orderedScreens);
2112 }
2113 }
2114 };
2115 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2116 }
2117
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2119 final ArrayList<ItemInfo> workspaceItems,
2120 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2121 final HashMap<Long, FolderInfo> folders,
2122 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002123
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002124 final boolean postOnMainThread = (deferredBindRunnables != null);
2125
2126 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002127 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002129 final int start = i;
2130 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002131 final Runnable r = new Runnable() {
2132 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002134 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002136 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2137 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 }
2139 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 };
2141 if (postOnMainThread) {
2142 deferredBindRunnables.add(r);
2143 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002144 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 }
Joe Onorato36115782010-06-17 13:28:48 -04002146 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002147
2148 // Bind the folders
2149 if (!folders.isEmpty()) {
2150 final Runnable r = new Runnable() {
2151 public void run() {
2152 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2153 if (callbacks != null) {
2154 callbacks.bindFolders(folders);
2155 }
2156 }
2157 };
2158 if (postOnMainThread) {
2159 deferredBindRunnables.add(r);
2160 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002161 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 }
2163 }
2164
2165 // Bind the widgets, one at a time
2166 N = appWidgets.size();
2167 for (int i = 0; i < N; i++) {
2168 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2169 final Runnable r = new Runnable() {
2170 public void run() {
2171 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2172 if (callbacks != null) {
2173 callbacks.bindAppWidget(widget);
2174 }
2175 }
2176 };
2177 if (postOnMainThread) {
2178 deferredBindRunnables.add(r);
2179 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002180 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002181 }
2182 }
2183 }
2184
2185 /**
2186 * Binds all loaded data to actual views on the main thread.
2187 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002188 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 final long t = SystemClock.uptimeMillis();
2190 Runnable r;
2191
2192 // Don't use these two variables in any of the callback runnables.
2193 // Otherwise we hold a reference to them.
2194 final Callbacks oldCallbacks = mCallbacks.get();
2195 if (oldCallbacks == null) {
2196 // This launcher has exited and nobody bothered to tell us. Just bail.
2197 Log.w(TAG, "LoaderTask running with no launcher");
2198 return;
2199 }
2200
Winson Chung4a2afa32012-07-19 14:53:05 -07002201 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2202 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 oldCallbacks.getCurrentWorkspaceScreen();
2204
2205 // Load all the items that are on the current page first (and in the process, unbind
2206 // all the existing workspace items before we call startBinding() below.
2207 unbindWorkspaceItemsOnMainThread();
2208 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2209 ArrayList<LauncherAppWidgetInfo> appWidgets =
2210 new ArrayList<LauncherAppWidgetInfo>();
2211 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2212 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002213 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002214 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 workspaceItems.addAll(sBgWorkspaceItems);
2216 appWidgets.addAll(sBgAppWidgets);
2217 folders.putAll(sBgFolders);
2218 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 }
2221
2222 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2223 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2224 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2225 new ArrayList<LauncherAppWidgetInfo>();
2226 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2227 new ArrayList<LauncherAppWidgetInfo>();
2228 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2229 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2230
2231 // Separate the items that are on the current screen, and all the other remaining items
2232 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2233 otherWorkspaceItems);
2234 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2235 otherAppWidgets);
2236 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2237 otherFolders);
2238 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2239 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2240
2241 // Tell the workspace that we're about to start binding items
2242 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002243 public void run() {
2244 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2245 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002247 }
2248 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 };
Winson Chung81b52252012-08-27 15:34:29 -07002250 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2253
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 // Load items on the current page
2255 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2256 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002257 if (isLoadingSynchronously) {
2258 r = new Runnable() {
2259 public void run() {
2260 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2261 if (callbacks != null) {
2262 callbacks.onPageBoundSynchronously(currentScreen);
2263 }
2264 }
2265 };
Winson Chung81b52252012-08-27 15:34:29 -07002266 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002267 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268
Winson Chung4a2afa32012-07-19 14:53:05 -07002269 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2270 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002271 mDeferredBindRunnables.clear();
2272 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002273 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274
2275 // Tell the workspace that we're done binding items
2276 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002277 public void run() {
2278 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2279 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002280 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002281 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282
Winson Chung98e030b2012-05-07 16:01:11 -07002283 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002284 if (DEBUG_LOADERS) {
2285 Log.d(TAG, "bound workspace in "
2286 + (SystemClock.uptimeMillis()-t) + "ms");
2287 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002288
2289 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002290 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002292 if (isLoadingSynchronously) {
2293 mDeferredBindRunnables.add(r);
2294 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002295 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002296 }
Joe Onorato36115782010-06-17 13:28:48 -04002297 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002298
Joe Onorato36115782010-06-17 13:28:48 -04002299 private void loadAndBindAllApps() {
2300 if (DEBUG_LOADERS) {
2301 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2302 }
2303 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002304 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002305 synchronized (LoaderTask.this) {
2306 if (mStopped) {
2307 return;
2308 }
2309 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002310 }
Joe Onorato36115782010-06-17 13:28:48 -04002311 } else {
2312 onlyBindAllApps();
2313 }
2314 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002315
Joe Onorato36115782010-06-17 13:28:48 -04002316 private void onlyBindAllApps() {
2317 final Callbacks oldCallbacks = mCallbacks.get();
2318 if (oldCallbacks == null) {
2319 // This launcher has exited and nobody bothered to tell us. Just bail.
2320 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2321 return;
2322 }
2323
2324 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002325 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002326 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002327 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002328 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002329 public void run() {
2330 final long t = SystemClock.uptimeMillis();
2331 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2332 if (callbacks != null) {
2333 callbacks.bindAllApplications(list);
2334 }
2335 if (DEBUG_LOADERS) {
2336 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2337 + (SystemClock.uptimeMillis()-t) + "ms");
2338 }
2339 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002340 };
2341 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002342 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002343 r.run();
2344 } else {
2345 mHandler.post(r);
2346 }
Joe Onorato36115782010-06-17 13:28:48 -04002347 }
2348
Winson Chung64359a52013-07-08 17:17:08 -07002349 private void loadAllApps() {
2350 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002351
2352 // Don't use these two variables in any of the callback runnables.
2353 // Otherwise we hold a reference to them.
2354 final Callbacks oldCallbacks = mCallbacks.get();
2355 if (oldCallbacks == null) {
2356 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002357 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002358 return;
2359 }
2360
Winson Chung64359a52013-07-08 17:17:08 -07002361 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002362 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2363 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2364
Winson Chung64359a52013-07-08 17:17:08 -07002365 // Clear the list of apps
2366 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002367
Winson Chung64359a52013-07-08 17:17:08 -07002368 // Query for the set of apps
2369 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2370 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2371 if (DEBUG_LOADERS) {
2372 Log.d(TAG, "queryIntentActivities took "
2373 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2374 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2375 }
2376 // Fail if we don't have any apps
2377 if (apps == null || apps.isEmpty()) {
2378 return;
2379 }
2380 // Sort the applications by name
2381 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2382 Collections.sort(apps,
2383 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2384 if (DEBUG_LOADERS) {
2385 Log.d(TAG, "sort took "
2386 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 }
2388
Winson Chung64359a52013-07-08 17:17:08 -07002389 // Create the ApplicationInfos
2390 final long addTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2391 for (int i = 0; i < apps.size(); i++) {
2392 // This builds the icon bitmaps.
2393 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
2394 mIconCache, mLabelCache));
2395 }
2396
2397 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2398 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2399 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
2400
2401 // Post callback on main thread
2402 mHandler.post(new Runnable() {
2403 public void run() {
2404 final long bindTime = SystemClock.uptimeMillis();
2405 if (callbacks != null) {
2406 callbacks.bindAllApplications(added);
2407 if (DEBUG_LOADERS) {
2408 Log.d(TAG, "bound " + added.size() + " apps in "
2409 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2410 }
2411 } else {
2412 Log.i(TAG, "not binding apps: no Launcher activity");
2413 }
2414 }
2415 });
2416
Joe Onorato36115782010-06-17 13:28:48 -04002417 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002418 Log.d(TAG, "Icons processed in "
2419 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002420 }
2421 }
2422
2423 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002424 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002425 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2426 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2427 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2428 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2429 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2430 }
Joe Onorato36115782010-06-17 13:28:48 -04002431 }
2432 }
2433
2434 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002435 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002436 }
2437
2438 private class PackageUpdatedTask implements Runnable {
2439 int mOp;
2440 String[] mPackages;
2441
2442 public static final int OP_NONE = 0;
2443 public static final int OP_ADD = 1;
2444 public static final int OP_UPDATE = 2;
2445 public static final int OP_REMOVE = 3; // uninstlled
2446 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2447
2448
2449 public PackageUpdatedTask(int op, String[] packages) {
2450 mOp = op;
2451 mPackages = packages;
2452 }
2453
2454 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002455 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002456
2457 final String[] packages = mPackages;
2458 final int N = packages.length;
2459 switch (mOp) {
2460 case OP_ADD:
2461 for (int i=0; i<N; i++) {
2462 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002463 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002464 }
2465 break;
2466 case OP_UPDATE:
2467 for (int i=0; i<N; i++) {
2468 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002469 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002470 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002471 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002472 }
2473 break;
2474 case OP_REMOVE:
2475 case OP_UNAVAILABLE:
2476 for (int i=0; i<N; i++) {
2477 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002478 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002479 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002480 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002481 }
2482 break;
2483 }
2484
2485 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002486 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002487 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002488
Adam Cohen487f7dd2012-06-28 18:12:10 -07002489 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002490 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2491 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002493 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002494 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2495 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002496 }
Winson Chung5d55f332012-07-16 20:45:03 -07002497 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002498 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002499 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002500 }
2501
Joe Onorato36115782010-06-17 13:28:48 -04002502 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2503 if (callbacks == null) {
2504 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2505 return;
2506 }
2507
2508 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002509 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002510 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002511 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung997a9232013-07-24 15:33:46 -07002512 addAndBindAddedApps(context, addedInfos, cb);
Joe Onorato36115782010-06-17 13:28:48 -04002513 }
2514 if (modified != null) {
2515 final ArrayList<ApplicationInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002516
2517 // Update the launcher db to reflect the changes
2518 for (ApplicationInfo a : modifiedFinal) {
2519 ArrayList<ItemInfo> infos =
2520 getItemInfoForComponentName(a.componentName);
2521 for (ItemInfo i : infos) {
2522 if (isShortcutInfoUpdateable(i)) {
2523 ShortcutInfo info = (ShortcutInfo) i;
2524 info.title = a.title.toString();
2525 updateItemInDatabase(context, info);
2526 }
2527 }
2528 }
2529
Joe Onorato36115782010-06-17 13:28:48 -04002530 mHandler.post(new Runnable() {
2531 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002532 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2533 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002534 callbacks.bindAppsUpdated(modifiedFinal);
2535 }
2536 }
2537 });
2538 }
Winson Chung83892cc2013-05-01 16:53:33 -07002539 // If a package has been removed, or an app has been removed as a result of
2540 // an update (for example), make the removed callback.
2541 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002542 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002543 final ArrayList<String> removedPackageNames =
2544 new ArrayList<String>(Arrays.asList(packages));
2545
Winson Chung64359a52013-07-08 17:17:08 -07002546 // Update the launcher db to reflect the removal of apps
2547 if (packageRemoved) {
2548 for (String pn : removedPackageNames) {
2549 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2550 for (ItemInfo i : infos) {
2551 deleteItemFromDatabase(context, i);
2552 }
2553 }
2554 } else {
2555 for (ApplicationInfo a : removedApps) {
2556 ArrayList<ItemInfo> infos =
2557 getItemInfoForComponentName(a.componentName);
2558 for (ItemInfo i : infos) {
2559 deleteItemFromDatabase(context, i);
2560 }
2561 }
2562 }
2563
Joe Onorato36115782010-06-17 13:28:48 -04002564 mHandler.post(new Runnable() {
2565 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002566 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2567 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002568 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002569 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002570 }
2571 }
2572 });
Joe Onoratobe386092009-11-17 17:32:16 -08002573 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002574
Michael Jurkac402cd92013-05-20 15:49:32 +02002575 final ArrayList<Object> widgetsAndShortcuts =
2576 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002577 mHandler.post(new Runnable() {
2578 @Override
2579 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002580 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2581 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002582 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002583 }
2584 }
2585 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002586 }
2587 }
2588
Michael Jurkac402cd92013-05-20 15:49:32 +02002589 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2590 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2591 PackageManager packageManager = context.getPackageManager();
2592 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2593 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2594 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2595 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2596 Collections.sort(widgetsAndShortcuts,
2597 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2598 return widgetsAndShortcuts;
2599 }
2600
Winson Chung1323b482013-08-05 12:41:55 -07002601 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002602 if (cn == null) {
2603 return false;
2604 }
2605
2606 try {
2607 return (pm.getActivityInfo(cn, 0) != null);
2608 } catch (NameNotFoundException e) {
2609 return false;
2610 }
2611 }
2612
Joe Onorato9c1289c2009-08-17 11:03:03 -04002613 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002614 * This is called from the code that adds shortcuts from the intent receiver. This
2615 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 */
Joe Onorato56d82912010-03-07 14:32:10 -05002617 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002618 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002620
Joe Onorato56d82912010-03-07 14:32:10 -05002621 /**
2622 * Make an ShortcutInfo object for a shortcut that is an application.
2623 *
2624 * If c is not null, then it will be used to fill in missing data like the title and icon.
2625 */
2626 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002627 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002628 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002629 final ShortcutInfo info = new ShortcutInfo();
2630 if (!isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002631 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002632 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002633 } else {
2634 try {
2635 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2636 info.initFlagsAndFirstInstallTime(pi);
2637 } catch (NameNotFoundException e) {
2638 Log.d(TAG, "getPackInfo failed for package " +
2639 componentName.getPackageName());
2640 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002641 }
2642
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002643 // TODO: See if the PackageManager knows about this case. If it doesn't
2644 // then return null & delete this.
2645
Joe Onorato56d82912010-03-07 14:32:10 -05002646 // the resource -- This may implicitly give us back the fallback icon,
2647 // but don't worry about that. All we're doing with usingFallbackIcon is
2648 // to avoid saving lots of copies of that in the database, and most apps
2649 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002650
2651 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2652 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2653 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002654 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002655 ResolveInfo resolveInfo = null;
2656 ComponentName oldComponent = intent.getComponent();
2657 Intent newIntent = new Intent(intent.getAction(), null);
2658 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2659 newIntent.setPackage(oldComponent.getPackageName());
2660 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2661 for (ResolveInfo i : infos) {
2662 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2663 i.activityInfo.name);
2664 if (cn.equals(oldComponent)) {
2665 resolveInfo = i;
2666 }
2667 }
2668 if (resolveInfo == null) {
2669 resolveInfo = manager.resolveActivity(intent, 0);
2670 }
Joe Onorato56d82912010-03-07 14:32:10 -05002671 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002672 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002673 }
Joe Onorato56d82912010-03-07 14:32:10 -05002674 // the db
2675 if (icon == null) {
2676 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002677 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002678 }
2679 }
2680 // the fallback icon
2681 if (icon == null) {
2682 icon = getFallbackIcon();
2683 info.usingFallbackIcon = true;
2684 }
2685 info.setIcon(icon);
2686
2687 // from the resource
2688 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002689 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2690 if (labelCache != null && labelCache.containsKey(key)) {
2691 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002692 } else {
2693 info.title = resolveInfo.activityInfo.loadLabel(manager);
2694 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002695 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002696 }
2697 }
Joe Onorato56d82912010-03-07 14:32:10 -05002698 }
2699 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002701 if (c != null) {
2702 info.title = c.getString(titleIndex);
2703 }
2704 }
2705 // fall back to the class name of the activity
2706 if (info.title == null) {
2707 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002709 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2710 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002712
Winson Chung64359a52013-07-08 17:17:08 -07002713 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2714 ItemInfoFilter f) {
2715 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2716 for (ItemInfo i : infos) {
2717 if (i instanceof ShortcutInfo) {
2718 ShortcutInfo info = (ShortcutInfo) i;
2719 ComponentName cn = info.intent.getComponent();
2720 if (cn != null && f.filterItem(null, info, cn)) {
2721 filtered.add(info);
2722 }
2723 } else if (i instanceof FolderInfo) {
2724 FolderInfo info = (FolderInfo) i;
2725 for (ShortcutInfo s : info.contents) {
2726 ComponentName cn = s.intent.getComponent();
2727 if (cn != null && f.filterItem(info, s, cn)) {
2728 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002729 }
2730 }
Winson Chung64359a52013-07-08 17:17:08 -07002731 } else if (i instanceof LauncherAppWidgetInfo) {
2732 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2733 ComponentName cn = info.providerName;
2734 if (cn != null && f.filterItem(null, info, cn)) {
2735 filtered.add(info);
2736 }
Winson Chung8a435102012-08-30 17:16:53 -07002737 }
2738 }
Winson Chung64359a52013-07-08 17:17:08 -07002739 return new ArrayList<ItemInfo>(filtered);
2740 }
2741
2742 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
2743 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2744 ItemInfoFilter filter = new ItemInfoFilter() {
2745 @Override
2746 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2747 return cn.getPackageName().equals(pn);
2748 }
2749 };
2750 return filterItemInfos(sBgItemsIdMap.values(), filter);
2751 }
2752
2753 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
2754 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2755 ItemInfoFilter filter = new ItemInfoFilter() {
2756 @Override
2757 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2758 return cn.equals(cname);
2759 }
2760 };
2761 return filterItemInfos(sBgItemsIdMap.values(), filter);
2762 }
2763
2764 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2765 if (i instanceof ShortcutInfo) {
2766 ShortcutInfo info = (ShortcutInfo) i;
2767 // We need to check for ACTION_MAIN otherwise getComponent() might
2768 // return null for some shortcuts (for instance, for shortcuts to
2769 // web pages.)
2770 Intent intent = info.intent;
2771 ComponentName name = intent.getComponent();
2772 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2773 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2774 return true;
2775 }
2776 }
2777 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002778 }
2779
2780 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002781 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002783 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002784 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2785 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786
Joe Onorato56d82912010-03-07 14:32:10 -05002787 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002788 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002789 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002791 // TODO: If there's an explicit component and we can't install that, delete it.
2792
Joe Onorato56d82912010-03-07 14:32:10 -05002793 info.title = c.getString(titleIndex);
2794
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 int iconType = c.getInt(iconTypeIndex);
2796 switch (iconType) {
2797 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2798 String packageName = c.getString(iconPackageIndex);
2799 String resourceName = c.getString(iconResourceIndex);
2800 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002801 info.customIcon = false;
2802 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002805 if (resources != null) {
2806 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002807 icon = Utilities.createIconBitmap(
2808 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002809 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002810 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002811 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 }
Joe Onorato56d82912010-03-07 14:32:10 -05002813 // the db
2814 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002815 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002816 }
2817 // the fallback icon
2818 if (icon == null) {
2819 icon = getFallbackIcon();
2820 info.usingFallbackIcon = true;
2821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 break;
2823 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002824 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002825 if (icon == null) {
2826 icon = getFallbackIcon();
2827 info.customIcon = false;
2828 info.usingFallbackIcon = true;
2829 } else {
2830 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 break;
2833 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002834 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002835 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 info.customIcon = false;
2837 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002839 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 return info;
2841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842
Michael Jurka931dc972011-08-05 15:08:15 -07002843 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002844 @SuppressWarnings("all") // suppress dead code warning
2845 final boolean debug = false;
2846 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002847 Log.d(TAG, "getIconFromCursor app="
2848 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2849 }
2850 byte[] data = c.getBlob(iconIndex);
2851 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002852 return Utilities.createIconBitmap(
2853 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002854 } catch (Exception e) {
2855 return null;
2856 }
2857 }
2858
Winson Chung3d503fb2011-07-13 17:25:49 -07002859 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2860 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002861 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002862 if (info == null) {
2863 return null;
2864 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002865 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002866
2867 return info;
2868 }
2869
Winson Chunga9abd0e2010-10-27 17:18:37 -07002870 /**
Winson Chung55cef262010-10-28 14:14:18 -07002871 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2872 */
2873 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2874 ComponentName component) {
2875 List<AppWidgetProviderInfo> widgets =
2876 AppWidgetManager.getInstance(context).getInstalledProviders();
2877 for (AppWidgetProviderInfo info : widgets) {
2878 if (info.provider.equals(component)) {
2879 return info;
2880 }
2881 }
2882 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002883 }
2884
Winson Chung68846fd2010-10-29 11:00:27 -07002885 /**
2886 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2887 */
2888 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2889 final PackageManager packageManager = context.getPackageManager();
2890 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2891 new ArrayList<WidgetMimeTypeHandlerData>();
2892
2893 final Intent supportsIntent =
2894 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2895 supportsIntent.setType(mimeType);
2896
2897 // Create a set of widget configuration components that we can test against
2898 final List<AppWidgetProviderInfo> widgets =
2899 AppWidgetManager.getInstance(context).getInstalledProviders();
2900 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2901 new HashMap<ComponentName, AppWidgetProviderInfo>();
2902 for (AppWidgetProviderInfo info : widgets) {
2903 configurationComponentToWidget.put(info.configure, info);
2904 }
2905
2906 // Run through each of the intents that can handle this type of clip data, and cross
2907 // reference them with the components that are actual configuration components
2908 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2909 PackageManager.MATCH_DEFAULT_ONLY);
2910 for (ResolveInfo info : activities) {
2911 final ActivityInfo activityInfo = info.activityInfo;
2912 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2913 activityInfo.name);
2914 if (configurationComponentToWidget.containsKey(infoComponent)) {
2915 supportedConfigurationActivities.add(
2916 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2917 configurationComponentToWidget.get(infoComponent)));
2918 }
2919 }
2920 return supportedConfigurationActivities;
2921 }
2922
Winson Chunga9abd0e2010-10-27 17:18:37 -07002923 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002924 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2925 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2926 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2927
Adam Cohend9198822011-11-22 16:42:47 -08002928 if (intent == null) {
2929 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2930 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2931 return null;
2932 }
2933
Joe Onorato0589f0f2010-02-08 13:44:00 -08002934 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002935 boolean customIcon = false;
2936 ShortcutIconResource iconResource = null;
2937
2938 if (bitmap != null && bitmap instanceof Bitmap) {
2939 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002940 customIcon = true;
2941 } else {
2942 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2943 if (extra != null && extra instanceof ShortcutIconResource) {
2944 try {
2945 iconResource = (ShortcutIconResource) extra;
2946 final PackageManager packageManager = context.getPackageManager();
2947 Resources resources = packageManager.getResourcesForApplication(
2948 iconResource.packageName);
2949 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002950 icon = Utilities.createIconBitmap(
2951 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002952 } catch (Exception e) {
2953 Log.w(TAG, "Could not load shortcut icon: " + extra);
2954 }
2955 }
2956 }
2957
Michael Jurkac9d95c52011-08-29 14:03:34 -07002958 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002959
2960 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002961 if (fallbackIcon != null) {
2962 icon = fallbackIcon;
2963 } else {
2964 icon = getFallbackIcon();
2965 info.usingFallbackIcon = true;
2966 }
Joe Onorato56d82912010-03-07 14:32:10 -05002967 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002968 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002969
Joe Onorato0589f0f2010-02-08 13:44:00 -08002970 info.title = name;
2971 info.intent = intent;
2972 info.customIcon = customIcon;
2973 info.iconResource = iconResource;
2974
2975 return info;
2976 }
2977
Winson Chungaac01e12011-08-17 10:37:13 -07002978 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2979 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002980 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07002981 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002982 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002983 }
Joe Onorato56d82912010-03-07 14:32:10 -05002984 // If this icon doesn't have a custom icon, check to see
2985 // what's stored in the DB, and if it doesn't match what
2986 // we're going to show, store what we are going to show back
2987 // into the DB. We do this so when we're loading, if the
2988 // package manager can't find an icon (for example because
2989 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002990 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002991 cache.put(info, c.getBlob(iconIndex));
2992 return true;
2993 }
2994 return false;
2995 }
2996 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2997 boolean needSave = false;
2998 try {
2999 if (data != null) {
3000 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3001 Bitmap loaded = info.getIcon(mIconCache);
3002 needSave = !saved.sameAs(loaded);
3003 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003004 needSave = true;
3005 }
Winson Chungaac01e12011-08-17 10:37:13 -07003006 } catch (Exception e) {
3007 needSave = true;
3008 }
3009 if (needSave) {
3010 Log.d(TAG, "going to save icon bitmap for info=" + info);
3011 // This is slower than is ideal, but this only happens once
3012 // or when the app is updated with a new icon.
3013 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003014 }
3015 }
3016
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003018 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 * or make a new one.
3020 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003021 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 // See if a placeholder was created for us already
3023 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003024 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003026 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 folders.put(id, folderInfo);
3028 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003029 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 }
3031
Winson Chung11904872012-09-17 16:58:46 -07003032 public static final Comparator<ApplicationInfo> getAppNameComparator() {
3033 final Collator collator = Collator.getInstance();
3034 return new Comparator<ApplicationInfo>() {
3035 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3036 int result = collator.compare(a.title.toString(), b.title.toString());
3037 if (result == 0) {
3038 result = a.componentName.compareTo(b.componentName);
3039 }
3040 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003041 }
Winson Chung11904872012-09-17 16:58:46 -07003042 };
3043 }
Winson Chung78403fe2011-01-21 15:38:02 -08003044 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
3045 = new Comparator<ApplicationInfo>() {
3046 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3047 if (a.firstInstallTime < b.firstInstallTime) return 1;
3048 if (a.firstInstallTime > b.firstInstallTime) return -1;
3049 return 0;
3050 }
3051 };
Winson Chung11904872012-09-17 16:58:46 -07003052 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3053 final Collator collator = Collator.getInstance();
3054 return new Comparator<AppWidgetProviderInfo>() {
3055 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3056 return collator.compare(a.label.toString(), b.label.toString());
3057 }
3058 };
3059 }
Winson Chung5308f242011-08-18 12:12:41 -07003060 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3061 if (info.activityInfo != null) {
3062 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3063 } else {
3064 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3065 }
3066 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003067 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003068 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003070 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003071 ShortcutNameComparator(PackageManager pm) {
3072 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003073 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003074 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003075 }
3076 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3077 mPackageManager = pm;
3078 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003079 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003080 }
3081 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003082 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003083 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3084 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3085 if (mLabelCache.containsKey(keyA)) {
3086 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003087 } else {
3088 labelA = a.loadLabel(mPackageManager).toString();
3089
Winson Chung5308f242011-08-18 12:12:41 -07003090 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003091 }
Winson Chung5308f242011-08-18 12:12:41 -07003092 if (mLabelCache.containsKey(keyB)) {
3093 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003094 } else {
3095 labelB = b.loadLabel(mPackageManager).toString();
3096
Winson Chung5308f242011-08-18 12:12:41 -07003097 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003098 }
Winson Chung11904872012-09-17 16:58:46 -07003099 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003100 }
3101 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003102 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003103 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003104 private PackageManager mPackageManager;
3105 private HashMap<Object, String> mLabelCache;
3106 WidgetAndShortcutNameComparator(PackageManager pm) {
3107 mPackageManager = pm;
3108 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003109 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003110 }
3111 public final int compare(Object a, Object b) {
3112 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003113 if (mLabelCache.containsKey(a)) {
3114 labelA = mLabelCache.get(a);
3115 } else {
3116 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003117 ((AppWidgetProviderInfo) a).label :
3118 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003119 mLabelCache.put(a, labelA);
3120 }
3121 if (mLabelCache.containsKey(b)) {
3122 labelB = mLabelCache.get(b);
3123 } else {
3124 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003125 ((AppWidgetProviderInfo) b).label :
3126 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003127 mLabelCache.put(b, labelB);
3128 }
Winson Chung11904872012-09-17 16:58:46 -07003129 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003130 }
3131 };
Joe Onoratobe386092009-11-17 17:32:16 -08003132
3133 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003134 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07003135 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3136 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3137 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3138 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003139 if (mLoaderTask != null) {
3140 mLoaderTask.dumpState();
3141 } else {
3142 Log.d(TAG, "mLoaderTask=null");
3143 }
Joe Onoratobe386092009-11-17 17:32:16 -08003144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145}