blob: 1c60861ec1d29cab3a0226ae94ef63b14774f57a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070043import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040044import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080045
Michael Jurkac2f801e2011-07-12 14:19:46 -070046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
49import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070050import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070051import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070052import java.util.Collections;
53import java.util.Comparator;
54import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070055import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070056import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070057import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070059import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Maintains in-memory state of the Launcher. It is expected that there should be only one
63 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070064 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040066public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080067 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070069
Daniel Sandler8707e0f2013-08-15 15:54:18 -070070 // true = use a "More Apps" folder for non-workspace apps on upgrade
71 // false = strew non-workspace apps across the workspace on upgrade
72 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
73
Joe Onorato36115782010-06-17 13:28:48 -040074 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070075 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040076
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078 private final Object mLock = new Object();
79 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040080 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020082 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Winson Chung81b52252012-08-27 15:34:29 -070084 // Specific runnable types that are run on the main thread deferred handler, this allows us to
85 // clear all queued binding runnables when the Launcher activity is destroyed.
86 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
87 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
88
89
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070090 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
91 static {
92 sWorkerThread.start();
93 }
94 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
95
Joe Onoratocc67f472010-06-08 10:54:30 -070096 // We start off with everything not loaded. After that, we assume that
97 // our monitoring of the package manager provides all updates and we never
98 // need to do a requery. These are only ever touched from the loader thread.
99 private boolean mWorkspaceLoaded;
100 private boolean mAllAppsLoaded;
101
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700102 // When we are loading pages synchronously, we can't just post the binding of items on the side
103 // pages as this delays the rotation process. Instead, we wait for a callback from the first
104 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
105 // a normal load, we also clear this set of Runnables.
106 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700110 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700111 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // The lock that must be acquired before referencing any static bg data structures. Unlike
114 // other locks, this one can generally be held long-term because we never expect any of these
115 // static data structures to be referenced outside of the worker thread except on the first
116 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700117 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118
Adam Cohen487f7dd2012-06-28 18:12:10 -0700119 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700120 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
124 // created by LauncherModel that are directly on the home screen (however, no widgets or
125 // shortcuts within folders).
126 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
129 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130 new ArrayList<LauncherAppWidgetInfo>();
131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
133 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
136 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700137
138 // sBgWorkspaceScreens is the ordered set of workspace screens.
139 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // </ only access in worker thread >
142
143 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
153 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700154 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500156 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700157 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 public void bindAppWidget(LauncherAppWidgetInfo info);
159 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700160 public void bindAppsAdded(ArrayList<Long> newScreens,
161 ArrayList<ItemInfo> addNotAnimated,
162 ArrayList<ItemInfo> addAnimated);
Joe Onorato64e6be72010-03-05 15:05:52 -0500163 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700164 public void bindComponentsRemoved(ArrayList<String> packageNames,
165 ArrayList<ApplicationInfo> appInfos,
166 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200167 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100168 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700169 public void onPageBoundSynchronously(int page);
Adam Cohen4caf2982013-08-20 18:54:31 -0700170 public void dumpLogsToLocalData(boolean email);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung64359a52013-07-08 17:17:08 -0700173 public interface ItemInfoFilter {
174 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
175 }
176
Daniel Sandlere4f98912013-06-25 15:13:26 -0400177 LauncherModel(LauncherAppState app, IconCache iconCache) {
178 final Context context = app.getContext();
179
Winson Chungee055712013-07-30 14:46:24 -0700180 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400181 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700182 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800183 mIconCache = iconCache;
184
185 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400187
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700189 Configuration config = res.getConfiguration();
190 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 }
192
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700193 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
194 * posted on the main thread handler. */
195 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700196 runOnMainThread(r, 0);
197 }
198 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700199 if (sWorkerThread.getThreadId() == Process.myTid()) {
200 // If we are on the worker thread, post onto the main handler
201 mHandler.post(r);
202 } else {
203 r.run();
204 }
205 }
206
207 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
208 * posted on the worker thread handler. */
209 private static void runOnWorkerThread(Runnable r) {
210 if (sWorkerThread.getThreadId() == Process.myTid()) {
211 r.run();
212 } else {
213 // If we are not on the worker thread, then post to the worker handler
214 sWorker.post(r);
215 }
216 }
217
Winson Chungc9168342013-06-26 14:54:55 -0700218 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
219 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700220 LauncherAppState app = LauncherAppState.getInstance();
221 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
222 final int xCount = (int) grid.numColumns;
223 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700224 boolean[][] occupied = new boolean[xCount][yCount];
225
226 int cellX, cellY, spanX, spanY;
227 for (int i = 0; i < items.size(); ++i) {
228 final ItemInfo item = items.get(i);
229 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
230 if (item.screenId == screen) {
231 cellX = item.cellX;
232 cellY = item.cellY;
233 spanX = item.spanX;
234 spanY = item.spanY;
235 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
236 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
237 occupied[x][y] = true;
238 }
239 }
240 }
241 }
242 }
243
244 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
245 }
246 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700247 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700248 int firstScreenIndex,
249 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700250 // Lock on the app so that we don't try and get the items while apps are being added
251 LauncherAppState app = LauncherAppState.getInstance();
252 LauncherModel model = app.getModel();
253 boolean found = false;
254 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700255 if (sWorkerThread.getThreadId() != Process.myTid()) {
256 // Flush the LauncherModel worker thread, so that if we just did another
257 // processInstallShortcut, we give it time for its shortcut to get added to the
258 // database (getItemsInLocalCoordinates reads the database)
259 model.flushWorkerThread();
260 }
Winson Chungc9168342013-06-26 14:54:55 -0700261 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700262
263 // Try adding to the workspace screens incrementally, starting at the default or center
264 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700265 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
266 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700267 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700268 int[] tmpCoordinates = new int[2];
269 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700270 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700272 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700273 }
274 }
275 }
Winson Chungc9168342013-06-26 14:54:55 -0700276 return null;
277 }
278
Winson Chung997a9232013-07-24 15:33:46 -0700279 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
280 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
281 addAndBindAddedApps(context, added, cb);
282 }
283 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chung64359a52013-07-08 17:17:08 -0700284 final Callbacks callbacks) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700285 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps()", true);
Winson Chung997a9232013-07-24 15:33:46 -0700286 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700287 return;
Winson Chung997a9232013-07-24 15:33:46 -0700288 }
Winson Chung64359a52013-07-08 17:17:08 -0700289 // Process the newly added applications and add them to the database first
290 Runnable r = new Runnable() {
291 public void run() {
292 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
293 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
294
Winson Chung76828c82013-08-19 15:43:29 -0700295 // Get the list of workspace screens. We need to append to this list and
296 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
297 // called.
298 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
299 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
300 for (Integer i : orderedScreens.keySet()) {
301 long screenId = orderedScreens.get(i);
302 workspaceScreens.add(screenId);
303 }
304
Winson Chung64359a52013-07-08 17:17:08 -0700305 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700306 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700307 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700308 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700309 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700310 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700311
312 // Short-circuit this logic if the icon exists somewhere on the workspace
313 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
314 continue;
315 }
316
317 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700318 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700319 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700320 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700321 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200322 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700323
324 // If we can't find a valid position, then just add a new screen.
325 // This takes time so we need to re-queue the add until the new
326 // page is added. Create as many screens as necessary to satisfy
327 // the startSearchPageIndex.
328 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700329 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700330 while (numPagesToAdd > 0) {
331 long screenId = lp.generateNewScreenId();
Adam Cohen4caf2982013-08-20 18:54:31 -0700332 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")", true);
Winson Chungc763c4e2013-07-19 13:49:06 -0700333 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700334 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 addedWorkspaceScreensFinal.add(screenId);
336 numPagesToAdd--;
337 }
Winson Chung76828c82013-08-19 15:43:29 -0700338
Winson Chung64359a52013-07-08 17:17:08 -0700339 // Find the coordinate again
340 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700341 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700342 }
343 if (coords == null) {
344 throw new RuntimeException("Coordinates should not be null");
345 }
346
Winson Chung997a9232013-07-24 15:33:46 -0700347 ShortcutInfo shortcutInfo;
348 if (a instanceof ShortcutInfo) {
349 shortcutInfo = (ShortcutInfo) a;
350 } else if (a instanceof ApplicationInfo) {
351 shortcutInfo = ((ApplicationInfo) a).makeShortcut();
352 } else {
353 throw new RuntimeException("Unexpected info type");
354 }
Winson Chung64359a52013-07-08 17:17:08 -0700355 // Add the shortcut to the db
356 addItemToDatabase(context, shortcutInfo,
357 LauncherSettings.Favorites.CONTAINER_DESKTOP,
358 coords.first, coords.second[0], coords.second[1], false);
359 // Save the ShortcutInfo for binding in the workspace
360 addedShortcutsFinal.add(shortcutInfo);
361 }
362 }
363
Adam Cohen4caf2982013-08-20 18:54:31 -0700364 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -0700365
Winson Chung76828c82013-08-19 15:43:29 -0700366 // Update the workspace screens
367 updateWorkspaceScreenOrder(context, workspaceScreens);
368
Winson Chung997a9232013-07-24 15:33:46 -0700369 if (!addedShortcutsFinal.isEmpty()) {
370 runOnMainThread(new Runnable() {
371 public void run() {
372 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
373 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
375 long lastScreenId = info.screenId;
376 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
377 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
378 for (ItemInfo i : addedShortcutsFinal) {
379 if (i.screenId == lastScreenId) {
380 addAnimated.add(i);
381 } else {
382 addNotAnimated.add(i);
383 }
384 }
Winson Chungd64d1762013-08-20 14:37:16 -0700385 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
386 addNotAnimated, addAnimated);
Winson Chung997a9232013-07-24 15:33:46 -0700387 }
Winson Chung64359a52013-07-08 17:17:08 -0700388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 });
390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 }
392 };
393 runOnWorkerThread(r);
394 }
395
Joe Onorato56d82912010-03-07 14:32:10 -0500396 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Winson Chung81b52252012-08-27 15:34:29 -0700400 public void unbindItemInfosAndClearQueuedBindRunnables() {
401 if (sWorkerThread.getThreadId() == Process.myTid()) {
402 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
403 "main thread");
404 }
405
406 // Clear any deferred bind runnables
407 mDeferredBindRunnables.clear();
408 // Remove any queued bind runnables
409 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
410 // Unbind all the workspace items
411 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700412 }
413
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700414 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700415 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700416 // Ensure that we don't use the same workspace items data structure on the main thread
417 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
419 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700420 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
422 tmpAppWidgets.addAll(sBgAppWidgets);
423 }
424 Runnable r = new Runnable() {
425 @Override
426 public void run() {
427 for (ItemInfo item : tmpWorkspaceItems) {
428 item.unbind();
429 }
430 for (ItemInfo item : tmpAppWidgets) {
431 item.unbind();
432 }
433 }
434 };
435 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700436 }
437
Joe Onorato9c1289c2009-08-17 11:03:03 -0400438 /**
439 * Adds an item to the DB if it was not created previously, or move it to a new
440 * <container, screen, cellX, cellY>
441 */
442 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700443 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 if (item.container == ItemInfo.NO_ID) {
445 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 } else {
448 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451 }
452
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700453 static void checkItemInfoLocked(
454 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
455 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
456 if (modelItem != null && item != modelItem) {
457 // check all the data is consistent
458 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
459 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
460 ShortcutInfo shortcut = (ShortcutInfo) item;
461 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
462 modelShortcut.intent.filterEquals(shortcut.intent) &&
463 modelShortcut.id == shortcut.id &&
464 modelShortcut.itemType == shortcut.itemType &&
465 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700466 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700467 modelShortcut.cellX == shortcut.cellX &&
468 modelShortcut.cellY == shortcut.cellY &&
469 modelShortcut.spanX == shortcut.spanX &&
470 modelShortcut.spanY == shortcut.spanY &&
471 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
472 (modelShortcut.dropPos != null &&
473 shortcut.dropPos != null &&
474 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
475 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
476 // For all intents and purposes, this is the same object
477 return;
478 }
479 }
480
481 // the modelItem needs to match up perfectly with item if our model is
482 // to be consistent with the database-- for now, just require
483 // modelItem == item or the equality check above
484 String msg = "item: " + ((item != null) ? item.toString() : "null") +
485 "modelItem: " +
486 ((modelItem != null) ? modelItem.toString() : "null") +
487 "Error: ItemInfo passed to checkItemInfo doesn't match original";
488 RuntimeException e = new RuntimeException(msg);
489 if (stackTrace != null) {
490 e.setStackTrace(stackTrace);
491 }
Adam Cohene25af792013-06-06 23:08:25 -0700492 // TODO: something breaks this in the upgrade path
493 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700494 }
495 }
496
Michael Jurka816474f2012-06-25 14:49:02 -0700497 static void checkItemInfo(final ItemInfo item) {
498 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
499 final long itemId = item.id;
500 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700501 public void run() {
502 synchronized (sBgLock) {
503 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700504 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 }
506 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700507 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700508 }
509
Michael Jurkac9d95c52011-08-29 14:03:34 -0700510 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
511 final ItemInfo item, final String callingFunction) {
512 final long itemId = item.id;
513 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
514 final ContentResolver cr = context.getContentResolver();
515
Adam Cohen487f7dd2012-06-28 18:12:10 -0700516 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700517 Runnable r = new Runnable() {
518 public void run() {
519 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700520 updateItemArrays(item, itemId, stackTrace);
521 }
522 };
523 runOnWorkerThread(r);
524 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700526 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
527 final ArrayList<ItemInfo> items, final String callingFunction) {
528 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700529
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
531 Runnable r = new Runnable() {
532 public void run() {
533 ArrayList<ContentProviderOperation> ops =
534 new ArrayList<ContentProviderOperation>();
535 int count = items.size();
536 for (int i = 0; i < count; i++) {
537 ItemInfo item = items.get(i);
538 final long itemId = item.id;
539 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
540 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700541
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700542 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
543 updateItemArrays(item, itemId, stackTrace);
544
545 }
546 try {
547 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
548 } catch (Exception e) {
549 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700550 }
551 }
552 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700555
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700556 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
557 // Lock on mBgLock *after* the db operation
558 synchronized (sBgLock) {
559 checkItemInfoLocked(itemId, item, stackTrace);
560
561 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
562 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
563 // Item is in a folder, make sure this folder exists
564 if (!sBgFolders.containsKey(item.container)) {
565 // An items container is being set to a that of an item which is not in
566 // the list of Folders.
567 String msg = "item: " + item + " container being set to: " +
568 item.container + ", not in the list of folders";
569 Log.e(TAG, msg);
570 Launcher.dumpDebugLogsToConsole();
571 }
572 }
573
574 // Items are added/removed from the corresponding FolderInfo elsewhere, such
575 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
576 // that are on the desktop, as appropriate
577 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
578 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
579 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
580 switch (modelItem.itemType) {
581 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
582 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
583 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
584 if (!sBgWorkspaceItems.contains(modelItem)) {
585 sBgWorkspaceItems.add(modelItem);
586 }
587 break;
588 default:
589 break;
590 }
591 } else {
592 sBgWorkspaceItems.remove(modelItem);
593 }
594 }
595 }
596
Michael Jurkac7700af2013-05-14 20:17:58 +0200597 public void flushWorkerThread() {
598 mFlushingWorkerThread = true;
599 Runnable waiter = new Runnable() {
600 public void run() {
601 synchronized (this) {
602 notifyAll();
603 mFlushingWorkerThread = false;
604 }
605 }
606 };
607
608 synchronized(waiter) {
609 runOnWorkerThread(waiter);
610 if (mLoaderTask != null) {
611 synchronized(mLoaderTask) {
612 mLoaderTask.notify();
613 }
614 }
615 boolean success = false;
616 while (!success) {
617 try {
618 waiter.wait();
619 success = true;
620 } catch (InterruptedException e) {
621 }
622 }
623 }
624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700628 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700629 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700631 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
633 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700634 Launcher.addDumpLog(TAG, transaction, true);
635
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.cellX = cellX;
638 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 // We store hotseat items in canonical form which is this orientation invariant position
641 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
649 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
652 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700656 }
657
658 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
660 * cellX, cellY have already been updated on the ItemInfos.
661 */
662 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
663 final long container, final int screen) {
664
665 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
666 int count = items.size();
667
668 for (int i = 0; i < count; i++) {
669 ItemInfo item = items.get(i);
670 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700671 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
673 + item.cellX + ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700674 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 item.container = container;
676
677 // We store hotseat items in canonical form which is this orientation invariant position
678 // in the hotseat
679 if (context instanceof Launcher && screen < 0 &&
680 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 item.cellY);
683 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685 }
686
687 final ContentValues values = new ContentValues();
688 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
689 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
690 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692
693 contentValues.add(values);
694 }
695 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
696 }
697
698 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800700 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700703 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
705 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700706 Launcher.addDumpLog(TAG, transaction, true);
707
Adam Cohend4844c32011-02-18 19:25:06 -0800708 item.cellX = cellX;
709 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 item.spanX = spanX;
711 item.spanY = spanY;
712
713 // We store hotseat items in canonical form which is this orientation invariant position
714 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 }
Adam Cohend4844c32011-02-18 19:25:06 -0800721
Adam Cohend4844c32011-02-18 19:25:06 -0800722 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800723 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700724 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
725 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
726 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
727 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800729
Michael Jurka816474f2012-06-25 14:49:02 -0700730 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700731 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700732
733 /**
734 * Update an item to the database in a specified container.
735 */
736 static void updateItemInDatabase(Context context, final ItemInfo item) {
737 final ContentValues values = new ContentValues();
738 item.onAddToDatabase(values);
739 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
740 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800741 }
742
743 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 * Returns true if the shortcuts already exists in the database.
745 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 static boolean shortcutExists(Context context, String title, Intent intent) {
748 final ContentResolver cr = context.getContentResolver();
749 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
750 new String[] { "title", "intent" }, "title=? and intent=?",
751 new String[] { title, intent.toUri(0) }, null);
752 boolean result = false;
753 try {
754 result = c.moveToFirst();
755 } finally {
756 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700759 }
760
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700762 * Returns an ItemInfo array containing all the items in the LauncherModel.
763 * The ItemInfo.id is not set through this function.
764 */
765 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
766 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
767 final ContentResolver cr = context.getContentResolver();
768 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
769 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
770 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
771 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
772
773 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
774 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
775 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
776 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
777 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
778 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
779 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
780
781 try {
782 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700783 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 item.cellX = c.getInt(cellXIndex);
785 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700786 item.spanX = Math.max(1, c.getInt(spanXIndex));
787 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700788 item.container = c.getInt(containerIndex);
789 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700790 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700791
792 items.add(item);
793 }
794 } catch (Exception e) {
795 items.clear();
796 } finally {
797 c.close();
798 }
799
800 return items;
801 }
802
803 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
805 */
806 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
807 final ContentResolver cr = context.getContentResolver();
808 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
809 "_id=? and (itemType=? or itemType=?)",
810 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700811 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 try {
814 if (c.moveToFirst()) {
815 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
816 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
817 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
818 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
819 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
820 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 FolderInfo folderInfo = null;
823 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700824 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
825 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700827 }
828
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 folderInfo.title = c.getString(titleIndex);
830 folderInfo.id = id;
831 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700832 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700833 folderInfo.cellX = c.getInt(cellXIndex);
834 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835
836 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 } finally {
839 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700840 }
841
842 return null;
843 }
844
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 /**
846 * Add an item to the database in a specified container. Sets the container, screen, cellX and
847 * cellY fields of the item. Also assigns an ID to the item.
848 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 item.cellX = cellX;
853 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 // We store hotseat items in canonical form which is this orientation invariant position
855 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862
863 final ContentValues values = new ContentValues();
864 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865 item.onAddToDatabase(values);
866
Michael Jurka414300a2013-08-27 15:42:35 +0200867 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700868 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700870
Michael Jurkac9d95c52011-08-29 14:03:34 -0700871 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700872 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700873 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700875 + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700876 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700877
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700878 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
879 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700881 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700882 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700883 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 sBgItemsIdMap.put(item.id, item);
885 switch (item.itemType) {
886 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
887 sBgFolders.put(item.id, (FolderInfo) item);
888 // Fall through
889 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
890 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
891 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
892 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
893 sBgWorkspaceItems.add(item);
894 } else {
895 if (!sBgFolders.containsKey(item.container)) {
896 // Adding an item to a folder that doesn't exist.
897 String msg = "adding item: " + item + " to a folder that " +
898 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700899 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700900 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700901 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700902 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 break;
904 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
905 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
906 break;
907 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700908 }
909 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700910 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700911 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700912 }
913
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700915 * Creates a new unique child id, for a given cell span across all layouts.
916 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700917 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700918 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700921 }
922
Winson Chungaafa03c2010-06-11 17:34:16 -0700923 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700924 * Removes the specified item from the database
925 * @param context
926 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400927 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700928 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700930 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700931
Michael Jurka83df1882011-08-31 20:59:26 -0700932 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700933 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700934 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700936 ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700937 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700938
Michael Jurkac9d95c52011-08-29 14:03:34 -0700939 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940
941 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700942 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943 switch (item.itemType) {
944 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
945 sBgFolders.remove(item.id);
946 for (ItemInfo info: sBgItemsIdMap.values()) {
947 if (info.container == item.id) {
948 // We are deleting a folder which still contains items that
949 // think they are contained by that folder.
950 String msg = "deleting a folder (" + item + ") which still " +
951 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700952 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700953 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700955 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 sBgWorkspaceItems.remove(item);
957 break;
958 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
959 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
960 sBgWorkspaceItems.remove(item);
961 break;
962 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
963 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
964 break;
965 }
966 sBgItemsIdMap.remove(item.id);
967 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700968 }
969 }
Michael Jurka83df1882011-08-31 20:59:26 -0700970 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700971 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 }
973
974 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 * Update the order of the workspace screens in the database. The array list contains
976 * a list of screen ids in the order that they should appear.
977 */
Winson Chungc9168342013-06-26 14:54:55 -0700978 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700979 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder()", true);
Winson Chung64359a52013-07-08 17:17:08 -0700980 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 final ContentResolver cr = context.getContentResolver();
982 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
983
984 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700985 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 while (iter.hasNext()) {
987 long id = iter.next();
988 if (id < 0) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700989 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 iter.remove();
991 }
992 }
993
Winson Chung9e6a0a22013-08-27 11:58:12 -0700994 // Dump the screens copy
Adam Cohen4caf2982013-08-20 18:54:31 -0700995 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700996 for (Long l : screensCopy) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700997 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700998 }
999
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 Runnable r = new Runnable() {
1001 @Override
1002 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 // Clear the table
1004 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001005 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 ContentValues[] values = new ContentValues[count];
1007 for (int i = 0; i < count; i++) {
1008 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001009 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1011 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohen4caf2982013-08-20 18:54:31 -07001012 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001013 values[i] = v;
1014 }
1015 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001016
1017 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001018 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001019 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001020 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001021 }
1022
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 sBgWorkspaceScreens.clear();
1024 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001025
1026 // Dump the sBgWorkspaceScreens
1027 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
1028 for (Long l : sBgWorkspaceScreens) {
1029 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1030 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001031 }
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
Adam Cohen4caf2982013-08-20 18:54:31 -07001271 Launcher.addDumpLog(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")", true);
Winson Chung76828c82013-08-19 15:43:29 -07001272
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).
Joe Onorato36115782010-06-17 13:28:48 -04001432 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001433 // Elevate priority when Home launches for the first time to avoid
1434 // starving at boot time. Staring at a blank home is not cool.
1435 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001436 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1437 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001438 android.os.Process.setThreadPriority(mIsLaunching
1439 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1440 }
Winson Chung64359a52013-07-08 17:17:08 -07001441 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001442 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001443
Joe Onorato36115782010-06-17 13:28:48 -04001444 if (mStopped) {
1445 break keep_running;
1446 }
1447
1448 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1449 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001450 synchronized (mLock) {
1451 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001452 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001453 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1454 }
1455 }
Joe Onorato36115782010-06-17 13:28:48 -04001456 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001457
1458 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001459 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1460 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001461
1462 // Restore the default thread priority after we are done loading items
1463 synchronized (mLock) {
1464 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1465 }
Joe Onorato36115782010-06-17 13:28:48 -04001466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467
Winson Chungaac01e12011-08-17 10:37:13 -07001468 // Update the saved icons if necessary
1469 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001470 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 for (Object key : sBgDbIconCache.keySet()) {
1472 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1473 }
1474 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001475 }
Winson Chungaac01e12011-08-17 10:37:13 -07001476
Winson Chungc763c4e2013-07-19 13:49:06 -07001477 // Ensure that all the applications that are in the system are represented on the home
1478 // screen.
Adam Cohen4caf2982013-08-20 18:54:31 -07001479 Launcher.addDumpLog(TAG, "10249126 - verifyApplications - useMoreApps="
1480 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade, true);
Daniel Sandler8707e0f2013-08-15 15:54:18 -07001481 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001482 Launcher.addDumpLog(TAG, "10249126 - verifyApplications(" + isUpgrade + ")", true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001483 verifyApplications();
1484 }
1485
Joe Onorato36115782010-06-17 13:28:48 -04001486 // Clear out this reference, otherwise we end up holding it until all of the
1487 // callback runnables are done.
1488 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489
Joe Onorato36115782010-06-17 13:28:48 -04001490 synchronized (mLock) {
1491 // If we are still the last one to be scheduled, remove ourselves.
1492 if (mLoaderTask == this) {
1493 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001494 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001495 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001496 }
Joe Onorato36115782010-06-17 13:28:48 -04001497 }
1498
1499 public void stopLocked() {
1500 synchronized (LoaderTask.this) {
1501 mStopped = true;
1502 this.notify();
1503 }
1504 }
1505
1506 /**
1507 * Gets the callbacks object. If we've been stopped, or if the launcher object
1508 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1509 * object that was around when the deferred message was scheduled, and if there's
1510 * a new Callbacks object around then also return null. This will save us from
1511 * calling onto it with data that will be ignored.
1512 */
1513 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1514 synchronized (mLock) {
1515 if (mStopped) {
1516 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001517 }
Joe Onorato36115782010-06-17 13:28:48 -04001518
1519 if (mCallbacks == null) {
1520 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001521 }
Joe Onorato36115782010-06-17 13:28:48 -04001522
1523 final Callbacks callbacks = mCallbacks.get();
1524 if (callbacks != oldCallbacks) {
1525 return null;
1526 }
1527 if (callbacks == null) {
1528 Log.w(TAG, "no mCallbacks");
1529 return null;
1530 }
1531
1532 return callbacks;
1533 }
1534 }
1535
Winson Chungc763c4e2013-07-19 13:49:06 -07001536 private void verifyApplications() {
1537 final Context context = mApp.getContext();
1538
1539 // Cross reference all the applications in our apps list with items in the workspace
1540 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001541 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001542 synchronized (sBgLock) {
1543 for (ApplicationInfo app : mBgAllAppsList.data) {
1544 tmpInfos = getItemInfoForComponentName(app.componentName);
Adam Cohen4caf2982013-08-20 18:54:31 -07001545 Launcher.addDumpLog(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty(), true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001546 if (tmpInfos.isEmpty()) {
1547 // We are missing an application icon, so add this to the workspace
1548 added.add(app);
1549 // This is a rare event, so lets log it
1550 Log.e(TAG, "Missing Application on load: " + app);
1551 }
1552 }
1553 }
1554 if (!added.isEmpty()) {
1555 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1556 addAndBindAddedApps(context, added, cb);
1557 }
1558 }
1559
Winson Chung5f8afe62013-08-12 16:19:28 -07001560 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001561 LauncherAppState app = LauncherAppState.getInstance();
1562 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1563 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1564 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001565 }
1566
Joe Onorato36115782010-06-17 13:28:48 -04001567 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001569 LauncherAppState app = LauncherAppState.getInstance();
1570 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1571 int countX = (int) grid.numColumns;
1572 int countY = (int) grid.numRows;
1573
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001575 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1577 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1578 [(int) item.screenId][0] != null) {
1579 Log.e(TAG, "Error loading shortcut into hotseat " + item
1580 + " into position (" + item.screenId + ":" + item.cellX + ","
1581 + item.cellY + ") occupied by "
1582 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1583 [(int) item.screenId][0]);
1584 return false;
1585 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001586 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001587 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 items[(int) item.screenId][0] = item;
1589 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001590 return true;
1591 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001592 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1593 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001594 return true;
1595 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001596
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001598 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 occupied.put(item.screenId, items);
1600 }
1601
1602 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001603 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001604 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1605 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001607 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001608 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001609 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001610 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001611 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001612 return false;
1613 }
1614 }
1615 }
1616 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1617 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001619 }
1620 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001621
Joe Onorato36115782010-06-17 13:28:48 -04001622 return true;
1623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 /** Returns whether this is an upgradge path */
1626 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001627 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628
Joe Onorato36115782010-06-17 13:28:48 -04001629 final Context context = mContext;
1630 final ContentResolver contentResolver = context.getContentResolver();
1631 final PackageManager manager = context.getPackageManager();
1632 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1633 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001634
Winson Chung892c74d2013-08-22 16:15:50 -07001635 LauncherAppState app = LauncherAppState.getInstance();
1636 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1637 int countX = (int) grid.numColumns;
1638 int countY = (int) grid.numRows;
1639
Michael Jurkab85f8a42012-04-25 15:48:32 -07001640 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001641 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001642
Winson Chungc763c4e2013-07-19 13:49:06 -07001643 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001644 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001645
Winson Chung2abf94d2012-07-18 18:16:38 -07001646 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001647 sBgWorkspaceItems.clear();
1648 sBgAppWidgets.clear();
1649 sBgFolders.clear();
1650 sBgItemsIdMap.clear();
1651 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001652 sBgWorkspaceScreens.clear();
Adam Cohen4caf2982013-08-20 18:54:31 -07001653 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace()", true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001654
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001655 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001656 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001657 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001658
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001659 // +1 for the hotseat (it can be larger than the workspace)
1660 // Load workspace in reverse order to ensure that latest items are loaded first (and
1661 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001662 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001664 try {
1665 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1666 final int intentIndex = c.getColumnIndexOrThrow
1667 (LauncherSettings.Favorites.INTENT);
1668 final int titleIndex = c.getColumnIndexOrThrow
1669 (LauncherSettings.Favorites.TITLE);
1670 final int iconTypeIndex = c.getColumnIndexOrThrow(
1671 LauncherSettings.Favorites.ICON_TYPE);
1672 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1673 final int iconPackageIndex = c.getColumnIndexOrThrow(
1674 LauncherSettings.Favorites.ICON_PACKAGE);
1675 final int iconResourceIndex = c.getColumnIndexOrThrow(
1676 LauncherSettings.Favorites.ICON_RESOURCE);
1677 final int containerIndex = c.getColumnIndexOrThrow(
1678 LauncherSettings.Favorites.CONTAINER);
1679 final int itemTypeIndex = c.getColumnIndexOrThrow(
1680 LauncherSettings.Favorites.ITEM_TYPE);
1681 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1682 LauncherSettings.Favorites.APPWIDGET_ID);
1683 final int screenIndex = c.getColumnIndexOrThrow(
1684 LauncherSettings.Favorites.SCREEN);
1685 final int cellXIndex = c.getColumnIndexOrThrow
1686 (LauncherSettings.Favorites.CELLX);
1687 final int cellYIndex = c.getColumnIndexOrThrow
1688 (LauncherSettings.Favorites.CELLY);
1689 final int spanXIndex = c.getColumnIndexOrThrow
1690 (LauncherSettings.Favorites.SPANX);
1691 final int spanYIndex = c.getColumnIndexOrThrow(
1692 LauncherSettings.Favorites.SPANY);
1693 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1694 //final int displayModeIndex = c.getColumnIndexOrThrow(
1695 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001697 ShortcutInfo info;
1698 String intentDescription;
1699 LauncherAppWidgetInfo appWidgetInfo;
1700 int container;
1701 long id;
1702 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 while (!mStopped && c.moveToNext()) {
1705 try {
1706 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 switch (itemType) {
1709 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1710 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001711 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 intentDescription = c.getString(intentIndex);
1713 try {
1714 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001715 ComponentName cn = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07001716 if (!isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001717 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001718 // Log the invalid package, and remove it from the db
1719 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1720 false);
Winson Chungee055712013-07-30 14:46:24 -07001721 contentResolver.delete(uri, null, null);
Winson Chung1323b482013-08-05 12:41:55 -07001722 Log.e(TAG, "Invalid package removed: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001723 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001724 // If apps can be on external storage, then we just
1725 // leave them for the user to remove (maybe add
1726 // visual treatment to it)
1727 Log.e(TAG, "Invalid package found: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001728 }
1729 continue;
1730 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 } catch (URISyntaxException e) {
1732 continue;
1733 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001734
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1736 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1737 titleIndex, mLabelCache);
1738 } else {
1739 info = getShortcutInfo(c, context, iconTypeIndex,
1740 iconPackageIndex, iconResourceIndex, iconIndex,
1741 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001742
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 // App shortcuts that used to be automatically added to Launcher
1744 // didn't always have the correct intent flags set, so do that
1745 // here
1746 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001747 intent.getCategories() != null &&
1748 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001749 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 intent.addFlags(
1751 Intent.FLAG_ACTIVITY_NEW_TASK |
1752 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1753 }
Michael Jurka96879562012-03-22 05:54:33 -07001754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001757 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001759 container = c.getInt(containerIndex);
1760 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001761 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 info.cellX = c.getInt(cellXIndex);
1763 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001764 info.spanX = 1;
1765 info.spanY = 1;
1766 // Skip loading items that are out of bounds
1767 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1768 if (checkItemDimensions(info)) {
1769 Log.d(TAG, "Skipped loading out of bounds shortcut: "
1770 + info.intent);
1771 continue;
1772 }
1773 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 // check & update map of what's occupied
1775 if (!checkItemPlacement(occupied, info)) {
1776 break;
1777 }
1778
1779 switch (container) {
1780 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1781 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1782 sBgWorkspaceItems.add(info);
1783 break;
1784 default:
1785 // Item is in a user folder
1786 FolderInfo folderInfo =
1787 findOrMakeFolder(sBgFolders, container);
1788 folderInfo.add(info);
1789 break;
1790 }
1791 sBgItemsIdMap.put(info.id, info);
1792
1793 // now that we've loaded everthing re-save it with the
1794 // icon in case it disappears somehow.
1795 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001796 } else {
1797 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 }
1799 break;
1800
1801 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1802 id = c.getLong(idIndex);
1803 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1804
1805 folderInfo.title = c.getString(titleIndex);
1806 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 folderInfo.cellX = c.getInt(cellXIndex);
1811 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001812 folderInfo.spanX = 1;
1813 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814
Winson Chung5f8afe62013-08-12 16:19:28 -07001815 // Skip loading items that are out of bounds
1816 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001817 if (checkItemDimensions(folderInfo)) {
1818 Log.d(TAG, "Skipped loading out of bounds folder");
1819 continue;
1820 }
1821 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001822 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001824 break;
1825 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001826
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1829 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1830 sBgWorkspaceItems.add(folderInfo);
1831 break;
Joe Onorato36115782010-06-17 13:28:48 -04001832 }
Joe Onorato17a89222011-02-08 17:26:11 -08001833
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1835 sBgFolders.put(folderInfo.id, folderInfo);
1836 break;
1837
1838 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1839 // Read all Launcher-specific widget details
1840 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001841 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 final AppWidgetProviderInfo provider =
1844 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 if (!isSafeMode && (provider == null || provider.provider == null ||
1847 provider.provider.getPackageName() == null)) {
1848 String log = "Deleting widget that isn't installed anymore: id="
1849 + id + " appWidgetId=" + appWidgetId;
1850 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001851 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852 itemsToRemove.add(id);
1853 } else {
1854 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1855 provider.provider);
1856 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001857 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 appWidgetInfo.cellX = c.getInt(cellXIndex);
1859 appWidgetInfo.cellY = c.getInt(cellYIndex);
1860 appWidgetInfo.spanX = c.getInt(spanXIndex);
1861 appWidgetInfo.spanY = c.getInt(spanYIndex);
1862 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1863 appWidgetInfo.minSpanX = minSpan[0];
1864 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001865
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 container = c.getInt(containerIndex);
1867 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1868 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1869 Log.e(TAG, "Widget found where container != " +
1870 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1871 continue;
1872 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873
Adam Cohene25af792013-06-06 23:08:25 -07001874 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001875 // Skip loading items that are out of bounds
1876 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1877 if (checkItemDimensions(appWidgetInfo)) {
1878 Log.d(TAG, "Skipped loading out of bounds app widget");
1879 continue;
1880 }
1881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 // check & update map of what's occupied
1883 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1884 break;
1885 }
1886 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1887 sBgAppWidgets.add(appWidgetInfo);
1888 }
Joe Onorato36115782010-06-17 13:28:48 -04001889 break;
1890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 } catch (Exception e) {
1892 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 }
1895 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001896 if (c != null) {
1897 c.close();
1898 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 }
1900
1901 if (itemsToRemove.size() > 0) {
1902 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001903 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 // Remove dead items
1905 for (long id : itemsToRemove) {
1906 if (DEBUG_LOADERS) {
1907 Log.d(TAG, "Removed id = " + id);
1908 }
1909 // Don't notify content observers
1910 try {
1911 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1912 null, null);
1913 } catch (RemoteException e) {
1914 Log.w(TAG, "Could not remove id = " + id);
1915 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001916 }
1917 }
1918
Winson Chungc763c4e2013-07-19 13:49:06 -07001919 if (loadedOldDb) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001920 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - loadedOldDb", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 long maxScreenId = 0;
1922 // If we're importing we use the old screen order.
1923 for (ItemInfo item: sBgItemsIdMap.values()) {
1924 long screenId = item.screenId;
1925 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1926 !sBgWorkspaceScreens.contains(screenId)) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001927 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 sBgWorkspaceScreens.add(screenId);
1929 if (screenId > maxScreenId) {
1930 maxScreenId = screenId;
1931 }
1932 }
1933 }
1934 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001935
1936 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001937 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001938 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001939 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001940 }
1941
Michael Jurka414300a2013-08-27 15:42:35 +02001942 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001944
1945 // Update the max item id after we load an old db
1946 long maxItemId = 0;
1947 // If we're importing we use the old screen order.
1948 for (ItemInfo item: sBgItemsIdMap.values()) {
1949 maxItemId = Math.max(maxItemId, item.id);
1950 }
Michael Jurka414300a2013-08-27 15:42:35 +02001951 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 } else {
Adam Cohen4caf2982013-08-20 18:54:31 -07001953 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - !loadedOldDb", true);
Winson Chung76828c82013-08-19 15:43:29 -07001954 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1955 for (Integer i : orderedScreens.keySet()) {
1956 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 }
1958
1959 // Remove any empty screens
1960 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1961 unusedScreens.addAll(sBgWorkspaceScreens);
1962
1963 for (ItemInfo item: sBgItemsIdMap.values()) {
1964 long screenId = item.screenId;
1965
1966 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1967 unusedScreens.contains(screenId)) {
1968 unusedScreens.remove(screenId);
1969 }
1970 }
1971
1972 // If there are any empty screens remove them, and update.
1973 if (unusedScreens.size() != 0) {
1974 sBgWorkspaceScreens.removeAll(unusedScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001975
1976 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001977 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001978 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001979 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001980 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1982 }
1983 }
1984
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 if (DEBUG_LOADERS) {
1986 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1987 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001989 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001991
Daniel Sandler566da102013-06-25 23:43:45 -04001992 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001993 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001994 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001995 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 line += " | ";
1997 }
Winson Chung892c74d2013-08-22 16:15:50 -07001998 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001999 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002000 }
Joe Onorato36115782010-06-17 13:28:48 -04002001 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002002 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 }
Joe Onorato36115782010-06-17 13:28:48 -04002005 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002006 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002007 }
2008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 /** Filters the set of items who are directly or indirectly (via another container) on the
2010 * specified screen. */
2011 private void filterCurrentWorkspaceItems(int currentScreen,
2012 ArrayList<ItemInfo> allWorkspaceItems,
2013 ArrayList<ItemInfo> currentScreenItems,
2014 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002015 // Purge any null ItemInfos
2016 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2017 while (iter.hasNext()) {
2018 ItemInfo i = iter.next();
2019 if (i == null) {
2020 iter.remove();
2021 }
2022 }
2023
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 // If we aren't filtering on a screen, then the set of items to load is the full set of
2025 // items given.
2026 if (currentScreen < 0) {
2027 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002028 }
2029
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 // Order the set of items by their containers first, this allows use to walk through the
2031 // list sequentially, build up a list of containers that are in the specified screen,
2032 // as well as all items in those containers.
2033 Set<Long> itemsOnScreen = new HashSet<Long>();
2034 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2035 @Override
2036 public int compare(ItemInfo lhs, ItemInfo rhs) {
2037 return (int) (lhs.container - rhs.container);
2038 }
2039 });
2040 for (ItemInfo info : allWorkspaceItems) {
2041 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002042 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 currentScreenItems.add(info);
2044 itemsOnScreen.add(info.id);
2045 } else {
2046 otherScreenItems.add(info);
2047 }
2048 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2049 currentScreenItems.add(info);
2050 itemsOnScreen.add(info.id);
2051 } else {
2052 if (itemsOnScreen.contains(info.container)) {
2053 currentScreenItems.add(info);
2054 itemsOnScreen.add(info.id);
2055 } else {
2056 otherScreenItems.add(info);
2057 }
2058 }
2059 }
2060 }
2061
2062 /** Filters the set of widgets which are on the specified screen. */
2063 private void filterCurrentAppWidgets(int currentScreen,
2064 ArrayList<LauncherAppWidgetInfo> appWidgets,
2065 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2066 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2067 // If we aren't filtering on a screen, then the set of items to load is the full set of
2068 // widgets given.
2069 if (currentScreen < 0) {
2070 currentScreenWidgets.addAll(appWidgets);
2071 }
2072
2073 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002074 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002075 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002076 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002077 currentScreenWidgets.add(widget);
2078 } else {
2079 otherScreenWidgets.add(widget);
2080 }
2081 }
2082 }
2083
2084 /** Filters the set of folders which are on the specified screen. */
2085 private void filterCurrentFolders(int currentScreen,
2086 HashMap<Long, ItemInfo> itemsIdMap,
2087 HashMap<Long, FolderInfo> folders,
2088 HashMap<Long, FolderInfo> currentScreenFolders,
2089 HashMap<Long, FolderInfo> otherScreenFolders) {
2090 // If we aren't filtering on a screen, then the set of items to load is the full set of
2091 // widgets given.
2092 if (currentScreen < 0) {
2093 currentScreenFolders.putAll(folders);
2094 }
2095
2096 for (long id : folders.keySet()) {
2097 ItemInfo info = itemsIdMap.get(id);
2098 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002099 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 currentScreenFolders.put(id, folder);
2103 } else {
2104 otherScreenFolders.put(id, folder);
2105 }
2106 }
2107 }
2108
2109 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2110 * right) */
2111 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002112 final LauncherAppState app = LauncherAppState.getInstance();
2113 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 // XXX: review this
2115 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002116 @Override
2117 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002118 int cellCountX = (int) grid.numColumns;
2119 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002120 int screenOffset = cellCountX * cellCountY;
2121 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002122 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002123 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002124 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002125 rhs.cellY * cellCountX + rhs.cellX);
2126 return (int) (lr - rr);
2127 }
2128 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002130
Adam Cohendcd297f2013-06-18 13:13:40 -07002131 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2132 final ArrayList<Long> orderedScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002133 Launcher.addDumpLog(TAG, "10249126 - bindWorkspaceScreens()", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002134
2135 // Dump the orderedScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07002136 Launcher.addDumpLog(TAG, "10249126 - orderedScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002137 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002138 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002139 }
2140
Adam Cohendcd297f2013-06-18 13:13:40 -07002141 final Runnable r = new Runnable() {
2142 @Override
2143 public void run() {
2144 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2145 if (callbacks != null) {
2146 callbacks.bindScreens(orderedScreens);
2147 }
2148 }
2149 };
2150 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2151 }
2152
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2154 final ArrayList<ItemInfo> workspaceItems,
2155 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2156 final HashMap<Long, FolderInfo> folders,
2157 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002158
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 final boolean postOnMainThread = (deferredBindRunnables != null);
2160
2161 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002162 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002164 final int start = i;
2165 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 final Runnable r = new Runnable() {
2167 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002169 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002171 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2172 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 }
2174 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 };
2176 if (postOnMainThread) {
2177 deferredBindRunnables.add(r);
2178 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002179 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 }
Joe Onorato36115782010-06-17 13:28:48 -04002181 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182
2183 // Bind the folders
2184 if (!folders.isEmpty()) {
2185 final Runnable r = new Runnable() {
2186 public void run() {
2187 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2188 if (callbacks != null) {
2189 callbacks.bindFolders(folders);
2190 }
2191 }
2192 };
2193 if (postOnMainThread) {
2194 deferredBindRunnables.add(r);
2195 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002196 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 }
2198 }
2199
2200 // Bind the widgets, one at a time
2201 N = appWidgets.size();
2202 for (int i = 0; i < N; i++) {
2203 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2204 final Runnable r = new Runnable() {
2205 public void run() {
2206 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2207 if (callbacks != null) {
2208 callbacks.bindAppWidget(widget);
2209 }
2210 }
2211 };
2212 if (postOnMainThread) {
2213 deferredBindRunnables.add(r);
2214 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002215 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 }
2217 }
2218 }
2219
2220 /**
2221 * Binds all loaded data to actual views on the main thread.
2222 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002223 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 final long t = SystemClock.uptimeMillis();
2225 Runnable r;
2226
2227 // Don't use these two variables in any of the callback runnables.
2228 // Otherwise we hold a reference to them.
2229 final Callbacks oldCallbacks = mCallbacks.get();
2230 if (oldCallbacks == null) {
2231 // This launcher has exited and nobody bothered to tell us. Just bail.
2232 Log.w(TAG, "LoaderTask running with no launcher");
2233 return;
2234 }
2235
Winson Chung4a2afa32012-07-19 14:53:05 -07002236 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2237 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002238 oldCallbacks.getCurrentWorkspaceScreen();
2239
2240 // Load all the items that are on the current page first (and in the process, unbind
2241 // all the existing workspace items before we call startBinding() below.
2242 unbindWorkspaceItemsOnMainThread();
2243 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2244 ArrayList<LauncherAppWidgetInfo> appWidgets =
2245 new ArrayList<LauncherAppWidgetInfo>();
2246 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2247 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002249 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250 workspaceItems.addAll(sBgWorkspaceItems);
2251 appWidgets.addAll(sBgAppWidgets);
2252 folders.putAll(sBgFolders);
2253 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 }
2256
2257 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2258 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2259 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2260 new ArrayList<LauncherAppWidgetInfo>();
2261 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2262 new ArrayList<LauncherAppWidgetInfo>();
2263 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2264 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2265
2266 // Separate the items that are on the current screen, and all the other remaining items
2267 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2268 otherWorkspaceItems);
2269 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2270 otherAppWidgets);
2271 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2272 otherFolders);
2273 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2274 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2275
2276 // Tell the workspace that we're about to start binding items
2277 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002278 public void run() {
2279 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2280 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002282 }
2283 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 };
Winson Chung81b52252012-08-27 15:34:29 -07002285 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286
Adam Cohendcd297f2013-06-18 13:13:40 -07002287 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2288
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 // Load items on the current page
2290 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2291 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002292 if (isLoadingSynchronously) {
2293 r = new Runnable() {
2294 public void run() {
2295 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2296 if (callbacks != null) {
2297 callbacks.onPageBoundSynchronously(currentScreen);
2298 }
2299 }
2300 };
Winson Chung81b52252012-08-27 15:34:29 -07002301 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002302 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303
Winson Chung4a2afa32012-07-19 14:53:05 -07002304 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2305 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 mDeferredBindRunnables.clear();
2307 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002308 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309
2310 // Tell the workspace that we're done binding items
2311 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002312 public void run() {
2313 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2314 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002315 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002316 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317
Winson Chung98e030b2012-05-07 16:01:11 -07002318 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002319 if (DEBUG_LOADERS) {
2320 Log.d(TAG, "bound workspace in "
2321 + (SystemClock.uptimeMillis()-t) + "ms");
2322 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002323
2324 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002325 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002327 if (isLoadingSynchronously) {
2328 mDeferredBindRunnables.add(r);
2329 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002330 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002331 }
Joe Onorato36115782010-06-17 13:28:48 -04002332 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002333
Joe Onorato36115782010-06-17 13:28:48 -04002334 private void loadAndBindAllApps() {
2335 if (DEBUG_LOADERS) {
2336 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2337 }
2338 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002339 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002340 synchronized (LoaderTask.this) {
2341 if (mStopped) {
2342 return;
2343 }
2344 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002345 }
Joe Onorato36115782010-06-17 13:28:48 -04002346 } else {
2347 onlyBindAllApps();
2348 }
2349 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002350
Joe Onorato36115782010-06-17 13:28:48 -04002351 private void onlyBindAllApps() {
2352 final Callbacks oldCallbacks = mCallbacks.get();
2353 if (oldCallbacks == null) {
2354 // This launcher has exited and nobody bothered to tell us. Just bail.
2355 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2356 return;
2357 }
2358
2359 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002360 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002361 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002362 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002363 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002364 public void run() {
2365 final long t = SystemClock.uptimeMillis();
2366 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2367 if (callbacks != null) {
2368 callbacks.bindAllApplications(list);
2369 }
2370 if (DEBUG_LOADERS) {
2371 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2372 + (SystemClock.uptimeMillis()-t) + "ms");
2373 }
2374 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002375 };
2376 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002377 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002378 r.run();
2379 } else {
2380 mHandler.post(r);
2381 }
Joe Onorato36115782010-06-17 13:28:48 -04002382 }
2383
Winson Chung64359a52013-07-08 17:17:08 -07002384 private void loadAllApps() {
2385 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002386
2387 // Don't use these two variables in any of the callback runnables.
2388 // Otherwise we hold a reference to them.
2389 final Callbacks oldCallbacks = mCallbacks.get();
2390 if (oldCallbacks == null) {
2391 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002392 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002393 return;
2394 }
2395
Winson Chung64359a52013-07-08 17:17:08 -07002396 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002397 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2398 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2399
Winson Chung64359a52013-07-08 17:17:08 -07002400 // Clear the list of apps
2401 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002402
Winson Chung64359a52013-07-08 17:17:08 -07002403 // Query for the set of apps
2404 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2405 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2406 if (DEBUG_LOADERS) {
2407 Log.d(TAG, "queryIntentActivities took "
2408 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2409 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2410 }
2411 // Fail if we don't have any apps
2412 if (apps == null || apps.isEmpty()) {
2413 return;
2414 }
2415 // Sort the applications by name
2416 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2417 Collections.sort(apps,
2418 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2419 if (DEBUG_LOADERS) {
2420 Log.d(TAG, "sort took "
2421 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 }
2423
Winson Chung64359a52013-07-08 17:17:08 -07002424 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002425 for (int i = 0; i < apps.size(); i++) {
2426 // This builds the icon bitmaps.
2427 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
2428 mIconCache, mLabelCache));
2429 }
2430
2431 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2432 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2433 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
2434
2435 // Post callback on main thread
2436 mHandler.post(new Runnable() {
2437 public void run() {
2438 final long bindTime = SystemClock.uptimeMillis();
2439 if (callbacks != null) {
2440 callbacks.bindAllApplications(added);
2441 if (DEBUG_LOADERS) {
2442 Log.d(TAG, "bound " + added.size() + " apps in "
2443 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2444 }
2445 } else {
2446 Log.i(TAG, "not binding apps: no Launcher activity");
2447 }
2448 }
2449 });
2450
Joe Onorato36115782010-06-17 13:28:48 -04002451 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002452 Log.d(TAG, "Icons processed in "
2453 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002454 }
2455 }
2456
2457 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002458 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002459 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2460 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2461 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2462 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2463 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2464 }
Joe Onorato36115782010-06-17 13:28:48 -04002465 }
2466 }
2467
2468 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002469 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002470 }
2471
2472 private class PackageUpdatedTask implements Runnable {
2473 int mOp;
2474 String[] mPackages;
2475
2476 public static final int OP_NONE = 0;
2477 public static final int OP_ADD = 1;
2478 public static final int OP_UPDATE = 2;
2479 public static final int OP_REMOVE = 3; // uninstlled
2480 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2481
2482
2483 public PackageUpdatedTask(int op, String[] packages) {
2484 mOp = op;
2485 mPackages = packages;
2486 }
2487
2488 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002489 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002490
2491 final String[] packages = mPackages;
2492 final int N = packages.length;
2493 switch (mOp) {
2494 case OP_ADD:
2495 for (int i=0; i<N; i++) {
2496 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002497 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002498 }
2499 break;
2500 case OP_UPDATE:
2501 for (int i=0; i<N; i++) {
2502 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002504 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002505 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
2507 break;
2508 case OP_REMOVE:
2509 case OP_UNAVAILABLE:
2510 for (int i=0; i<N; i++) {
2511 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002512 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002513 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002514 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002515 }
2516 break;
2517 }
2518
2519 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002520 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002521 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002522
Adam Cohen487f7dd2012-06-28 18:12:10 -07002523 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002524 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2525 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002526 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002527 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002528 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2529 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002530 }
Winson Chung5d55f332012-07-16 20:45:03 -07002531 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002532 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002533 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002534 }
2535
Joe Onorato36115782010-06-17 13:28:48 -04002536 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2537 if (callbacks == null) {
2538 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2539 return;
2540 }
2541
2542 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002543 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002544 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002545 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung997a9232013-07-24 15:33:46 -07002546 addAndBindAddedApps(context, addedInfos, cb);
Joe Onorato36115782010-06-17 13:28:48 -04002547 }
2548 if (modified != null) {
2549 final ArrayList<ApplicationInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002550
2551 // Update the launcher db to reflect the changes
2552 for (ApplicationInfo a : modifiedFinal) {
2553 ArrayList<ItemInfo> infos =
2554 getItemInfoForComponentName(a.componentName);
2555 for (ItemInfo i : infos) {
2556 if (isShortcutInfoUpdateable(i)) {
2557 ShortcutInfo info = (ShortcutInfo) i;
2558 info.title = a.title.toString();
2559 updateItemInDatabase(context, info);
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) {
Joe Onorato36115782010-06-17 13:28:48 -04002568 callbacks.bindAppsUpdated(modifiedFinal);
2569 }
2570 }
2571 });
2572 }
Winson Chung83892cc2013-05-01 16:53:33 -07002573 // If a package has been removed, or an app has been removed as a result of
2574 // an update (for example), make the removed callback.
2575 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002576 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002577 final ArrayList<String> removedPackageNames =
2578 new ArrayList<String>(Arrays.asList(packages));
2579
Winson Chung64359a52013-07-08 17:17:08 -07002580 // Update the launcher db to reflect the removal of apps
2581 if (packageRemoved) {
2582 for (String pn : removedPackageNames) {
2583 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2584 for (ItemInfo i : infos) {
2585 deleteItemFromDatabase(context, i);
2586 }
2587 }
2588 } else {
2589 for (ApplicationInfo a : removedApps) {
2590 ArrayList<ItemInfo> infos =
2591 getItemInfoForComponentName(a.componentName);
2592 for (ItemInfo i : infos) {
2593 deleteItemFromDatabase(context, i);
2594 }
2595 }
2596 }
2597
Joe Onorato36115782010-06-17 13:28:48 -04002598 mHandler.post(new Runnable() {
2599 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002600 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2601 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002602 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002603 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002604 }
2605 }
2606 });
Joe Onoratobe386092009-11-17 17:32:16 -08002607 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002608
Michael Jurkac402cd92013-05-20 15:49:32 +02002609 final ArrayList<Object> widgetsAndShortcuts =
2610 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002611 mHandler.post(new Runnable() {
2612 @Override
2613 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002614 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2615 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002616 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002617 }
2618 }
2619 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002620
2621 // Write all the logs to disk
2622 Launcher.addDumpLog(TAG, "10249126 - PackageUpdatedTask - dumping logs to disk", true);
2623 mHandler.post(new Runnable() {
2624 public void run() {
2625 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2626 if (callbacks == cb && cb != null) {
2627 callbacks.dumpLogsToLocalData(false);
2628 }
2629 }
2630 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002631 }
2632 }
2633
Michael Jurkac402cd92013-05-20 15:49:32 +02002634 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2635 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2636 PackageManager packageManager = context.getPackageManager();
2637 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2638 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2639 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2640 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2641 Collections.sort(widgetsAndShortcuts,
2642 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2643 return widgetsAndShortcuts;
2644 }
2645
Winson Chung1323b482013-08-05 12:41:55 -07002646 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002647 if (cn == null) {
2648 return false;
2649 }
2650
2651 try {
2652 return (pm.getActivityInfo(cn, 0) != null);
2653 } catch (NameNotFoundException e) {
2654 return false;
2655 }
2656 }
2657
Joe Onorato9c1289c2009-08-17 11:03:03 -04002658 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002659 * This is called from the code that adds shortcuts from the intent receiver. This
2660 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002661 */
Joe Onorato56d82912010-03-07 14:32:10 -05002662 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002663 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002664 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002665
Joe Onorato56d82912010-03-07 14:32:10 -05002666 /**
2667 * Make an ShortcutInfo object for a shortcut that is an application.
2668 *
2669 * If c is not null, then it will be used to fill in missing data like the title and icon.
2670 */
2671 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002672 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002673 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002674 final ShortcutInfo info = new ShortcutInfo();
2675 if (!isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002676 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002677 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002678 } else {
2679 try {
2680 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2681 info.initFlagsAndFirstInstallTime(pi);
2682 } catch (NameNotFoundException e) {
2683 Log.d(TAG, "getPackInfo failed for package " +
2684 componentName.getPackageName());
2685 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002686 }
2687
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002688 // TODO: See if the PackageManager knows about this case. If it doesn't
2689 // then return null & delete this.
2690
Joe Onorato56d82912010-03-07 14:32:10 -05002691 // the resource -- This may implicitly give us back the fallback icon,
2692 // but don't worry about that. All we're doing with usingFallbackIcon is
2693 // to avoid saving lots of copies of that in the database, and most apps
2694 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002695
2696 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2697 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2698 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002699 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002700 ResolveInfo resolveInfo = null;
2701 ComponentName oldComponent = intent.getComponent();
2702 Intent newIntent = new Intent(intent.getAction(), null);
2703 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2704 newIntent.setPackage(oldComponent.getPackageName());
2705 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2706 for (ResolveInfo i : infos) {
2707 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2708 i.activityInfo.name);
2709 if (cn.equals(oldComponent)) {
2710 resolveInfo = i;
2711 }
2712 }
2713 if (resolveInfo == null) {
2714 resolveInfo = manager.resolveActivity(intent, 0);
2715 }
Joe Onorato56d82912010-03-07 14:32:10 -05002716 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002717 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002718 }
Joe Onorato56d82912010-03-07 14:32:10 -05002719 // the db
2720 if (icon == null) {
2721 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002722 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002723 }
2724 }
2725 // the fallback icon
2726 if (icon == null) {
2727 icon = getFallbackIcon();
2728 info.usingFallbackIcon = true;
2729 }
2730 info.setIcon(icon);
2731
2732 // from the resource
2733 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002734 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2735 if (labelCache != null && labelCache.containsKey(key)) {
2736 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002737 } else {
2738 info.title = resolveInfo.activityInfo.loadLabel(manager);
2739 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002740 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002741 }
2742 }
Joe Onorato56d82912010-03-07 14:32:10 -05002743 }
2744 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002746 if (c != null) {
2747 info.title = c.getString(titleIndex);
2748 }
2749 }
2750 // fall back to the class name of the activity
2751 if (info.title == null) {
2752 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2755 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002757
Winson Chung64359a52013-07-08 17:17:08 -07002758 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2759 ItemInfoFilter f) {
2760 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2761 for (ItemInfo i : infos) {
2762 if (i instanceof ShortcutInfo) {
2763 ShortcutInfo info = (ShortcutInfo) i;
2764 ComponentName cn = info.intent.getComponent();
2765 if (cn != null && f.filterItem(null, info, cn)) {
2766 filtered.add(info);
2767 }
2768 } else if (i instanceof FolderInfo) {
2769 FolderInfo info = (FolderInfo) i;
2770 for (ShortcutInfo s : info.contents) {
2771 ComponentName cn = s.intent.getComponent();
2772 if (cn != null && f.filterItem(info, s, cn)) {
2773 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002774 }
2775 }
Winson Chung64359a52013-07-08 17:17:08 -07002776 } else if (i instanceof LauncherAppWidgetInfo) {
2777 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2778 ComponentName cn = info.providerName;
2779 if (cn != null && f.filterItem(null, info, cn)) {
2780 filtered.add(info);
2781 }
Winson Chung8a435102012-08-30 17:16:53 -07002782 }
2783 }
Winson Chung64359a52013-07-08 17:17:08 -07002784 return new ArrayList<ItemInfo>(filtered);
2785 }
2786
2787 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
2788 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2789 ItemInfoFilter filter = new ItemInfoFilter() {
2790 @Override
2791 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2792 return cn.getPackageName().equals(pn);
2793 }
2794 };
2795 return filterItemInfos(sBgItemsIdMap.values(), filter);
2796 }
2797
2798 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
2799 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2800 ItemInfoFilter filter = new ItemInfoFilter() {
2801 @Override
2802 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2803 return cn.equals(cname);
2804 }
2805 };
2806 return filterItemInfos(sBgItemsIdMap.values(), filter);
2807 }
2808
2809 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2810 if (i instanceof ShortcutInfo) {
2811 ShortcutInfo info = (ShortcutInfo) i;
2812 // We need to check for ACTION_MAIN otherwise getComponent() might
2813 // return null for some shortcuts (for instance, for shortcuts to
2814 // web pages.)
2815 Intent intent = info.intent;
2816 ComponentName name = intent.getComponent();
2817 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2818 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2819 return true;
2820 }
2821 }
2822 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002823 }
2824
2825 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002826 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002827 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002828 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002829 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2830 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831
Joe Onorato56d82912010-03-07 14:32:10 -05002832 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002833 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002836 // TODO: If there's an explicit component and we can't install that, delete it.
2837
Joe Onorato56d82912010-03-07 14:32:10 -05002838 info.title = c.getString(titleIndex);
2839
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 int iconType = c.getInt(iconTypeIndex);
2841 switch (iconType) {
2842 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2843 String packageName = c.getString(iconPackageIndex);
2844 String resourceName = c.getString(iconResourceIndex);
2845 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002846 info.customIcon = false;
2847 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002849 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002850 if (resources != null) {
2851 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002852 icon = Utilities.createIconBitmap(
2853 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002856 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 }
Joe Onorato56d82912010-03-07 14:32:10 -05002858 // the db
2859 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002860 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002861 }
2862 // the fallback icon
2863 if (icon == null) {
2864 icon = getFallbackIcon();
2865 info.usingFallbackIcon = true;
2866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 break;
2868 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002869 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002870 if (icon == null) {
2871 icon = getFallbackIcon();
2872 info.customIcon = false;
2873 info.usingFallbackIcon = true;
2874 } else {
2875 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 break;
2878 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002879 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002880 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 info.customIcon = false;
2882 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002884 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 return info;
2886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887
Michael Jurka931dc972011-08-05 15:08:15 -07002888 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002889 @SuppressWarnings("all") // suppress dead code warning
2890 final boolean debug = false;
2891 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002892 Log.d(TAG, "getIconFromCursor app="
2893 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2894 }
2895 byte[] data = c.getBlob(iconIndex);
2896 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002897 return Utilities.createIconBitmap(
2898 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002899 } catch (Exception e) {
2900 return null;
2901 }
2902 }
2903
Winson Chung3d503fb2011-07-13 17:25:49 -07002904 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2905 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002906 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002907 if (info == null) {
2908 return null;
2909 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002910 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002911
2912 return info;
2913 }
2914
Winson Chunga9abd0e2010-10-27 17:18:37 -07002915 /**
Winson Chung55cef262010-10-28 14:14:18 -07002916 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2917 */
2918 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2919 ComponentName component) {
2920 List<AppWidgetProviderInfo> widgets =
2921 AppWidgetManager.getInstance(context).getInstalledProviders();
2922 for (AppWidgetProviderInfo info : widgets) {
2923 if (info.provider.equals(component)) {
2924 return info;
2925 }
2926 }
2927 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002928 }
2929
Winson Chung68846fd2010-10-29 11:00:27 -07002930 /**
2931 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2932 */
2933 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2934 final PackageManager packageManager = context.getPackageManager();
2935 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2936 new ArrayList<WidgetMimeTypeHandlerData>();
2937
2938 final Intent supportsIntent =
2939 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2940 supportsIntent.setType(mimeType);
2941
2942 // Create a set of widget configuration components that we can test against
2943 final List<AppWidgetProviderInfo> widgets =
2944 AppWidgetManager.getInstance(context).getInstalledProviders();
2945 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2946 new HashMap<ComponentName, AppWidgetProviderInfo>();
2947 for (AppWidgetProviderInfo info : widgets) {
2948 configurationComponentToWidget.put(info.configure, info);
2949 }
2950
2951 // Run through each of the intents that can handle this type of clip data, and cross
2952 // reference them with the components that are actual configuration components
2953 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2954 PackageManager.MATCH_DEFAULT_ONLY);
2955 for (ResolveInfo info : activities) {
2956 final ActivityInfo activityInfo = info.activityInfo;
2957 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2958 activityInfo.name);
2959 if (configurationComponentToWidget.containsKey(infoComponent)) {
2960 supportedConfigurationActivities.add(
2961 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2962 configurationComponentToWidget.get(infoComponent)));
2963 }
2964 }
2965 return supportedConfigurationActivities;
2966 }
2967
Winson Chunga9abd0e2010-10-27 17:18:37 -07002968 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002969 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2970 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2971 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2972
Adam Cohend9198822011-11-22 16:42:47 -08002973 if (intent == null) {
2974 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2975 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2976 return null;
2977 }
2978
Joe Onorato0589f0f2010-02-08 13:44:00 -08002979 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002980 boolean customIcon = false;
2981 ShortcutIconResource iconResource = null;
2982
2983 if (bitmap != null && bitmap instanceof Bitmap) {
2984 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002985 customIcon = true;
2986 } else {
2987 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2988 if (extra != null && extra instanceof ShortcutIconResource) {
2989 try {
2990 iconResource = (ShortcutIconResource) extra;
2991 final PackageManager packageManager = context.getPackageManager();
2992 Resources resources = packageManager.getResourcesForApplication(
2993 iconResource.packageName);
2994 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002995 icon = Utilities.createIconBitmap(
2996 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002997 } catch (Exception e) {
2998 Log.w(TAG, "Could not load shortcut icon: " + extra);
2999 }
3000 }
3001 }
3002
Michael Jurkac9d95c52011-08-29 14:03:34 -07003003 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003004
3005 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003006 if (fallbackIcon != null) {
3007 icon = fallbackIcon;
3008 } else {
3009 icon = getFallbackIcon();
3010 info.usingFallbackIcon = true;
3011 }
Joe Onorato56d82912010-03-07 14:32:10 -05003012 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003013 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003014
Joe Onorato0589f0f2010-02-08 13:44:00 -08003015 info.title = name;
3016 info.intent = intent;
3017 info.customIcon = customIcon;
3018 info.iconResource = iconResource;
3019
3020 return info;
3021 }
3022
Winson Chungaac01e12011-08-17 10:37:13 -07003023 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3024 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003025 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003026 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003027 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003028 }
Joe Onorato56d82912010-03-07 14:32:10 -05003029 // If this icon doesn't have a custom icon, check to see
3030 // what's stored in the DB, and if it doesn't match what
3031 // we're going to show, store what we are going to show back
3032 // into the DB. We do this so when we're loading, if the
3033 // package manager can't find an icon (for example because
3034 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003035 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003036 cache.put(info, c.getBlob(iconIndex));
3037 return true;
3038 }
3039 return false;
3040 }
3041 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3042 boolean needSave = false;
3043 try {
3044 if (data != null) {
3045 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3046 Bitmap loaded = info.getIcon(mIconCache);
3047 needSave = !saved.sameAs(loaded);
3048 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003049 needSave = true;
3050 }
Winson Chungaac01e12011-08-17 10:37:13 -07003051 } catch (Exception e) {
3052 needSave = true;
3053 }
3054 if (needSave) {
3055 Log.d(TAG, "going to save icon bitmap for info=" + info);
3056 // This is slower than is ideal, but this only happens once
3057 // or when the app is updated with a new icon.
3058 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003059 }
3060 }
3061
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003063 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 * or make a new one.
3065 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003066 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 // See if a placeholder was created for us already
3068 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003069 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003071 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 folders.put(id, folderInfo);
3073 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003074 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075 }
3076
Winson Chung11904872012-09-17 16:58:46 -07003077 public static final Comparator<ApplicationInfo> getAppNameComparator() {
3078 final Collator collator = Collator.getInstance();
3079 return new Comparator<ApplicationInfo>() {
3080 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3081 int result = collator.compare(a.title.toString(), b.title.toString());
3082 if (result == 0) {
3083 result = a.componentName.compareTo(b.componentName);
3084 }
3085 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003086 }
Winson Chung11904872012-09-17 16:58:46 -07003087 };
3088 }
Winson Chung78403fe2011-01-21 15:38:02 -08003089 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
3090 = new Comparator<ApplicationInfo>() {
3091 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3092 if (a.firstInstallTime < b.firstInstallTime) return 1;
3093 if (a.firstInstallTime > b.firstInstallTime) return -1;
3094 return 0;
3095 }
3096 };
Winson Chung11904872012-09-17 16:58:46 -07003097 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3098 final Collator collator = Collator.getInstance();
3099 return new Comparator<AppWidgetProviderInfo>() {
3100 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3101 return collator.compare(a.label.toString(), b.label.toString());
3102 }
3103 };
3104 }
Winson Chung5308f242011-08-18 12:12:41 -07003105 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3106 if (info.activityInfo != null) {
3107 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3108 } else {
3109 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3110 }
3111 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003112 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003113 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003114 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003115 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003116 ShortcutNameComparator(PackageManager pm) {
3117 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003118 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003119 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003120 }
3121 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3122 mPackageManager = pm;
3123 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003124 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003125 }
3126 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003127 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003128 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3129 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3130 if (mLabelCache.containsKey(keyA)) {
3131 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003132 } else {
3133 labelA = a.loadLabel(mPackageManager).toString();
3134
Winson Chung5308f242011-08-18 12:12:41 -07003135 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003136 }
Winson Chung5308f242011-08-18 12:12:41 -07003137 if (mLabelCache.containsKey(keyB)) {
3138 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003139 } else {
3140 labelB = b.loadLabel(mPackageManager).toString();
3141
Winson Chung5308f242011-08-18 12:12:41 -07003142 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003143 }
Winson Chung11904872012-09-17 16:58:46 -07003144 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003145 }
3146 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003147 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003148 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003149 private PackageManager mPackageManager;
3150 private HashMap<Object, String> mLabelCache;
3151 WidgetAndShortcutNameComparator(PackageManager pm) {
3152 mPackageManager = pm;
3153 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003154 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003155 }
3156 public final int compare(Object a, Object b) {
3157 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003158 if (mLabelCache.containsKey(a)) {
3159 labelA = mLabelCache.get(a);
3160 } else {
3161 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003162 ((AppWidgetProviderInfo) a).label :
3163 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003164 mLabelCache.put(a, labelA);
3165 }
3166 if (mLabelCache.containsKey(b)) {
3167 labelB = mLabelCache.get(b);
3168 } else {
3169 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003170 ((AppWidgetProviderInfo) b).label :
3171 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003172 mLabelCache.put(b, labelB);
3173 }
Winson Chung11904872012-09-17 16:58:46 -07003174 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003175 }
3176 };
Joe Onoratobe386092009-11-17 17:32:16 -08003177
3178 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003179 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07003180 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3181 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3182 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3183 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003184 if (mLoaderTask != null) {
3185 mLoaderTask.dumpState();
3186 } else {
3187 Log.d(TAG, "mLoaderTask=null");
3188 }
Joe Onoratobe386092009-11-17 17:32:16 -08003189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190}