blob: c76b5533249e60d847e044c78169bffab525ab91 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070043import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040044import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080045
Michael Jurkac2f801e2011-07-12 14:19:46 -070046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
49import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070050import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070051import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070052import java.util.Collections;
53import java.util.Comparator;
54import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070055import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070056import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070057import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070059import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Maintains in-memory state of the Launcher. It is expected that there should be only one
63 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070064 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040066public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080067 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070069
Daniel Sandler8707e0f2013-08-15 15:54:18 -070070 // true = use a "More Apps" folder for non-workspace apps on upgrade
71 // false = strew non-workspace apps across the workspace on upgrade
72 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
73
Joe Onorato36115782010-06-17 13:28:48 -040074 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070075 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040076
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078 private final Object mLock = new Object();
79 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040080 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020082 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Winson Chung81b52252012-08-27 15:34:29 -070084 // Specific runnable types that are run on the main thread deferred handler, this allows us to
85 // clear all queued binding runnables when the Launcher activity is destroyed.
86 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
87 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
88
89
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070090 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
91 static {
92 sWorkerThread.start();
93 }
94 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
95
Joe Onoratocc67f472010-06-08 10:54:30 -070096 // We start off with everything not loaded. After that, we assume that
97 // our monitoring of the package manager provides all updates and we never
98 // need to do a requery. These are only ever touched from the loader thread.
99 private boolean mWorkspaceLoaded;
100 private boolean mAllAppsLoaded;
101
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700102 // When we are loading pages synchronously, we can't just post the binding of items on the side
103 // pages as this delays the rotation process. Instead, we wait for a callback from the first
104 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
105 // a normal load, we also clear this set of Runnables.
106 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700110 // < only access in worker thread >
Adam Cohen487f7dd2012-06-28 18:12:10 -0700111 private AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // The lock that must be acquired before referencing any static bg data structures. Unlike
114 // other locks, this one can generally be held long-term because we never expect any of these
115 // static data structures to be referenced outside of the worker thread except on the first
116 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700117 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118
Adam Cohen487f7dd2012-06-28 18:12:10 -0700119 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700120 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
124 // created by LauncherModel that are directly on the home screen (however, no widgets or
125 // shortcuts within folders).
126 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
129 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130 new ArrayList<LauncherAppWidgetInfo>();
131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
133 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
136 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700137
138 // sBgWorkspaceScreens is the ordered set of workspace screens.
139 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // </ only access in worker thread >
142
143 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
153 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700154 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500156 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700157 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 public void bindAppWidget(LauncherAppWidgetInfo info);
159 public void bindAllApplications(ArrayList<ApplicationInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700160 public void bindAppsAdded(ArrayList<Long> newScreens,
161 ArrayList<ItemInfo> addNotAnimated,
162 ArrayList<ItemInfo> addAnimated);
Joe Onorato64e6be72010-03-05 15:05:52 -0500163 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700164 public void bindComponentsRemoved(ArrayList<String> packageNames,
165 ArrayList<ApplicationInfo> appInfos,
166 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200167 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100168 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700169 public void onPageBoundSynchronously(int page);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung64359a52013-07-08 17:17:08 -0700172 public interface ItemInfoFilter {
173 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
174 }
175
Daniel Sandlere4f98912013-06-25 15:13:26 -0400176 LauncherModel(LauncherAppState app, IconCache iconCache) {
177 final Context context = app.getContext();
178
Winson Chungee055712013-07-30 14:46:24 -0700179 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700181 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800182 mIconCache = iconCache;
183
184 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400185 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400186
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400187 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700188 Configuration config = res.getConfiguration();
189 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800190 }
191
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700192 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
193 * posted on the main thread handler. */
194 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700195 runOnMainThread(r, 0);
196 }
197 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700198 if (sWorkerThread.getThreadId() == Process.myTid()) {
199 // If we are on the worker thread, post onto the main handler
200 mHandler.post(r);
201 } else {
202 r.run();
203 }
204 }
205
206 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
207 * posted on the worker thread handler. */
208 private static void runOnWorkerThread(Runnable r) {
209 if (sWorkerThread.getThreadId() == Process.myTid()) {
210 r.run();
211 } else {
212 // If we are not on the worker thread, then post to the worker handler
213 sWorker.post(r);
214 }
215 }
216
Winson Chungc9168342013-06-26 14:54:55 -0700217 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
218 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700219 LauncherAppState app = LauncherAppState.getInstance();
220 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
221 final int xCount = (int) grid.numColumns;
222 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700223 boolean[][] occupied = new boolean[xCount][yCount];
224
225 int cellX, cellY, spanX, spanY;
226 for (int i = 0; i < items.size(); ++i) {
227 final ItemInfo item = items.get(i);
228 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
229 if (item.screenId == screen) {
230 cellX = item.cellX;
231 cellY = item.cellY;
232 spanX = item.spanX;
233 spanY = item.spanY;
234 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
235 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
236 occupied[x][y] = true;
237 }
238 }
239 }
240 }
241 }
242
243 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
244 }
245 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700246 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700247 int firstScreenIndex,
248 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700249 // Lock on the app so that we don't try and get the items while apps are being added
250 LauncherAppState app = LauncherAppState.getInstance();
251 LauncherModel model = app.getModel();
252 boolean found = false;
253 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700254 if (sWorkerThread.getThreadId() != Process.myTid()) {
255 // Flush the LauncherModel worker thread, so that if we just did another
256 // processInstallShortcut, we give it time for its shortcut to get added to the
257 // database (getItemsInLocalCoordinates reads the database)
258 model.flushWorkerThread();
259 }
Winson Chungc9168342013-06-26 14:54:55 -0700260 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700261
262 // Try adding to the workspace screens incrementally, starting at the default or center
263 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700264 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
265 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700266 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700267 int[] tmpCoordinates = new int[2];
268 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700269 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700270 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700271 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700272 }
273 }
274 }
Winson Chungc9168342013-06-26 14:54:55 -0700275 return null;
276 }
277
Winson Chung997a9232013-07-24 15:33:46 -0700278 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added) {
279 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
280 addAndBindAddedApps(context, added, cb);
281 }
282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chung64359a52013-07-08 17:17:08 -0700283 final Callbacks callbacks) {
Winson Chungd64d1762013-08-20 14:37:16 -0700284 Log.w(TAG, "10249126 - addAndBindAddedApps()");
Winson Chung997a9232013-07-24 15:33:46 -0700285 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700286 return;
Winson Chung997a9232013-07-24 15:33:46 -0700287 }
Winson Chung64359a52013-07-08 17:17:08 -0700288 // Process the newly added applications and add them to the database first
289 Runnable r = new Runnable() {
290 public void run() {
291 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
292 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
293
Winson Chung76828c82013-08-19 15:43:29 -0700294 // Get the list of workspace screens. We need to append to this list and
295 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
296 // called.
297 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
298 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
299 for (Integer i : orderedScreens.keySet()) {
300 long screenId = orderedScreens.get(i);
301 workspaceScreens.add(screenId);
302 }
303
Winson Chung64359a52013-07-08 17:17:08 -0700304 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700305 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700306 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700307 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700308 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700309 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700310
311 // Short-circuit this logic if the icon exists somewhere on the workspace
312 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
313 continue;
314 }
315
316 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700317 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700318 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700319 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700320 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200321 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700322
323 // If we can't find a valid position, then just add a new screen.
324 // This takes time so we need to re-queue the add until the new
325 // page is added. Create as many screens as necessary to satisfy
326 // the startSearchPageIndex.
327 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700328 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700329 while (numPagesToAdd > 0) {
330 long screenId = lp.generateNewScreenId();
Winson Chung76828c82013-08-19 15:43:29 -0700331 Log.w(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")");
Winson Chungc763c4e2013-07-19 13:49:06 -0700332 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700333 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700334 addedWorkspaceScreensFinal.add(screenId);
335 numPagesToAdd--;
336 }
Winson Chung76828c82013-08-19 15:43:29 -0700337
Winson Chung64359a52013-07-08 17:17:08 -0700338 // Find the coordinate again
339 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700340 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700341 }
342 if (coords == null) {
343 throw new RuntimeException("Coordinates should not be null");
344 }
345
Winson Chung997a9232013-07-24 15:33:46 -0700346 ShortcutInfo shortcutInfo;
347 if (a instanceof ShortcutInfo) {
348 shortcutInfo = (ShortcutInfo) a;
349 } else if (a instanceof ApplicationInfo) {
350 shortcutInfo = ((ApplicationInfo) a).makeShortcut();
351 } else {
352 throw new RuntimeException("Unexpected info type");
353 }
Winson Chung64359a52013-07-08 17:17:08 -0700354 // Add the shortcut to the db
355 addItemToDatabase(context, shortcutInfo,
356 LauncherSettings.Favorites.CONTAINER_DESKTOP,
357 coords.first, coords.second[0], coords.second[1], false);
358 // Save the ShortcutInfo for binding in the workspace
359 addedShortcutsFinal.add(shortcutInfo);
360 }
361 }
362
Winson Chungd64d1762013-08-20 14:37:16 -0700363 Log.w(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")");
364
Winson Chung76828c82013-08-19 15:43:29 -0700365 // Update the workspace screens
366 updateWorkspaceScreenOrder(context, workspaceScreens);
367
Winson Chung997a9232013-07-24 15:33:46 -0700368 if (!addedShortcutsFinal.isEmpty()) {
369 runOnMainThread(new Runnable() {
370 public void run() {
371 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
372 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700373 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
374 long lastScreenId = info.screenId;
375 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
376 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
377 for (ItemInfo i : addedShortcutsFinal) {
378 if (i.screenId == lastScreenId) {
379 addAnimated.add(i);
380 } else {
381 addNotAnimated.add(i);
382 }
383 }
Winson Chungd64d1762013-08-20 14:37:16 -0700384 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
385 addNotAnimated, addAnimated);
Winson Chung997a9232013-07-24 15:33:46 -0700386 }
Winson Chung64359a52013-07-08 17:17:08 -0700387 }
Winson Chung997a9232013-07-24 15:33:46 -0700388 });
389 }
Winson Chung64359a52013-07-08 17:17:08 -0700390 }
391 };
392 runOnWorkerThread(r);
393 }
394
Joe Onorato56d82912010-03-07 14:32:10 -0500395 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Winson Chung81b52252012-08-27 15:34:29 -0700399 public void unbindItemInfosAndClearQueuedBindRunnables() {
400 if (sWorkerThread.getThreadId() == Process.myTid()) {
401 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
402 "main thread");
403 }
404
405 // Clear any deferred bind runnables
406 mDeferredBindRunnables.clear();
407 // Remove any queued bind runnables
408 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
409 // Unbind all the workspace items
410 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700411 }
412
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700413 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700414 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700415 // Ensure that we don't use the same workspace items data structure on the main thread
416 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700417 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
418 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700419 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
421 tmpAppWidgets.addAll(sBgAppWidgets);
422 }
423 Runnable r = new Runnable() {
424 @Override
425 public void run() {
426 for (ItemInfo item : tmpWorkspaceItems) {
427 item.unbind();
428 }
429 for (ItemInfo item : tmpAppWidgets) {
430 item.unbind();
431 }
432 }
433 };
434 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700435 }
436
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 /**
438 * Adds an item to the DB if it was not created previously, or move it to a new
439 * <container, screen, cellX, cellY>
440 */
441 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700442 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400443 if (item.container == ItemInfo.NO_ID) {
444 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700445 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 } else {
447 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700448 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450 }
451
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700452 static void checkItemInfoLocked(
453 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
454 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
455 if (modelItem != null && item != modelItem) {
456 // check all the data is consistent
457 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
458 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
459 ShortcutInfo shortcut = (ShortcutInfo) item;
460 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
461 modelShortcut.intent.filterEquals(shortcut.intent) &&
462 modelShortcut.id == shortcut.id &&
463 modelShortcut.itemType == shortcut.itemType &&
464 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700465 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700466 modelShortcut.cellX == shortcut.cellX &&
467 modelShortcut.cellY == shortcut.cellY &&
468 modelShortcut.spanX == shortcut.spanX &&
469 modelShortcut.spanY == shortcut.spanY &&
470 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
471 (modelShortcut.dropPos != null &&
472 shortcut.dropPos != null &&
473 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
474 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
475 // For all intents and purposes, this is the same object
476 return;
477 }
478 }
479
480 // the modelItem needs to match up perfectly with item if our model is
481 // to be consistent with the database-- for now, just require
482 // modelItem == item or the equality check above
483 String msg = "item: " + ((item != null) ? item.toString() : "null") +
484 "modelItem: " +
485 ((modelItem != null) ? modelItem.toString() : "null") +
486 "Error: ItemInfo passed to checkItemInfo doesn't match original";
487 RuntimeException e = new RuntimeException(msg);
488 if (stackTrace != null) {
489 e.setStackTrace(stackTrace);
490 }
Adam Cohene25af792013-06-06 23:08:25 -0700491 // TODO: something breaks this in the upgrade path
492 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700493 }
494 }
495
Michael Jurka816474f2012-06-25 14:49:02 -0700496 static void checkItemInfo(final ItemInfo item) {
497 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
498 final long itemId = item.id;
499 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700500 public void run() {
501 synchronized (sBgLock) {
502 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700503 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700504 }
505 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700506 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700507 }
508
Michael Jurkac9d95c52011-08-29 14:03:34 -0700509 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
510 final ItemInfo item, final String callingFunction) {
511 final long itemId = item.id;
512 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
513 final ContentResolver cr = context.getContentResolver();
514
Adam Cohen487f7dd2012-06-28 18:12:10 -0700515 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700516 Runnable r = new Runnable() {
517 public void run() {
518 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700519 updateItemArrays(item, itemId, stackTrace);
520 }
521 };
522 runOnWorkerThread(r);
523 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700524
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700525 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
526 final ArrayList<ItemInfo> items, final String callingFunction) {
527 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700528
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700529 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
530 Runnable r = new Runnable() {
531 public void run() {
532 ArrayList<ContentProviderOperation> ops =
533 new ArrayList<ContentProviderOperation>();
534 int count = items.size();
535 for (int i = 0; i < count; i++) {
536 ItemInfo item = items.get(i);
537 final long itemId = item.id;
538 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
539 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700540
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700541 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
542 updateItemArrays(item, itemId, stackTrace);
543
544 }
545 try {
546 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
547 } catch (Exception e) {
548 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700549 }
550 }
551 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700552 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700553 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700554
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700555 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
556 // Lock on mBgLock *after* the db operation
557 synchronized (sBgLock) {
558 checkItemInfoLocked(itemId, item, stackTrace);
559
560 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
561 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
562 // Item is in a folder, make sure this folder exists
563 if (!sBgFolders.containsKey(item.container)) {
564 // An items container is being set to a that of an item which is not in
565 // the list of Folders.
566 String msg = "item: " + item + " container being set to: " +
567 item.container + ", not in the list of folders";
568 Log.e(TAG, msg);
569 Launcher.dumpDebugLogsToConsole();
570 }
571 }
572
573 // Items are added/removed from the corresponding FolderInfo elsewhere, such
574 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
575 // that are on the desktop, as appropriate
576 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
577 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
578 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
579 switch (modelItem.itemType) {
580 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
581 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
582 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
583 if (!sBgWorkspaceItems.contains(modelItem)) {
584 sBgWorkspaceItems.add(modelItem);
585 }
586 break;
587 default:
588 break;
589 }
590 } else {
591 sBgWorkspaceItems.remove(modelItem);
592 }
593 }
594 }
595
Michael Jurkac7700af2013-05-14 20:17:58 +0200596 public void flushWorkerThread() {
597 mFlushingWorkerThread = true;
598 Runnable waiter = new Runnable() {
599 public void run() {
600 synchronized (this) {
601 notifyAll();
602 mFlushingWorkerThread = false;
603 }
604 }
605 };
606
607 synchronized(waiter) {
608 runOnWorkerThread(waiter);
609 if (mLoaderTask != null) {
610 synchronized(mLoaderTask) {
611 mLoaderTask.notify();
612 }
613 }
614 boolean success = false;
615 while (!success) {
616 try {
617 waiter.wait();
618 success = true;
619 } catch (InterruptedException e) {
620 }
621 }
622 }
623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700627 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700628 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700629 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700630 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700631 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
632 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700633 Launcher.sDumpLogs.add(transaction);
634 Log.d(TAG, transaction);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.cellX = cellX;
637 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700638
Winson Chung3d503fb2011-07-13 17:25:49 -0700639 // We store hotseat items in canonical form which is this orientation invariant position
640 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647
648 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
651 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700652 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653
Michael Jurkac9d95c52011-08-29 14:03:34 -0700654 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700655 }
656
657 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700658 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
659 * cellX, cellY have already been updated on the ItemInfos.
660 */
661 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
662 final long container, final int screen) {
663
664 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
665 int count = items.size();
666
667 for (int i = 0; i < count; i++) {
668 ItemInfo item = items.get(i);
669 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700670 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700671 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
672 + item.cellX + ", " + item.cellY + ")";
673 Launcher.sDumpLogs.add(transaction);
674 item.container = container;
675
676 // We store hotseat items in canonical form which is this orientation invariant position
677 // in the hotseat
678 if (context instanceof Launcher && screen < 0 &&
679 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681 item.cellY);
682 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 }
685
686 final ContentValues values = new ContentValues();
687 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
688 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
689 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700690 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691
692 contentValues.add(values);
693 }
694 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
695 }
696
697 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700698 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800699 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700702 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
704 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen487f7dd2012-06-28 18:12:10 -0700705 Launcher.sDumpLogs.add(transaction);
706 Log.d(TAG, transaction);
Adam Cohend4844c32011-02-18 19:25:06 -0800707 item.cellX = cellX;
708 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 item.spanX = spanX;
710 item.spanY = spanY;
711
712 // We store hotseat items in canonical form which is this orientation invariant position
713 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700719 }
Adam Cohend4844c32011-02-18 19:25:06 -0800720
Adam Cohend4844c32011-02-18 19:25:06 -0800721 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800722 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700723 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
724 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
725 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
726 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800728
Michael Jurka816474f2012-06-25 14:49:02 -0700729 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700730 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700731
732 /**
733 * Update an item to the database in a specified container.
734 */
735 static void updateItemInDatabase(Context context, final ItemInfo item) {
736 final ContentValues values = new ContentValues();
737 item.onAddToDatabase(values);
738 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
739 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800740 }
741
742 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 * Returns true if the shortcuts already exists in the database.
744 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 static boolean shortcutExists(Context context, String title, Intent intent) {
747 final ContentResolver cr = context.getContentResolver();
748 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
749 new String[] { "title", "intent" }, "title=? and intent=?",
750 new String[] { title, intent.toUri(0) }, null);
751 boolean result = false;
752 try {
753 result = c.moveToFirst();
754 } finally {
755 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700758 }
759
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700761 * Returns an ItemInfo array containing all the items in the LauncherModel.
762 * The ItemInfo.id is not set through this function.
763 */
764 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
765 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
766 final ContentResolver cr = context.getContentResolver();
767 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
768 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
769 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
770 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
771
772 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
773 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
774 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
775 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
776 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
777 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
778 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
779
780 try {
781 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700782 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700783 item.cellX = c.getInt(cellXIndex);
784 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700785 item.spanX = Math.max(1, c.getInt(spanXIndex));
786 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700787 item.container = c.getInt(containerIndex);
788 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700789 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700790
791 items.add(item);
792 }
793 } catch (Exception e) {
794 items.clear();
795 } finally {
796 c.close();
797 }
798
799 return items;
800 }
801
802 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
804 */
805 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
806 final ContentResolver cr = context.getContentResolver();
807 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
808 "_id=? and (itemType=? or itemType=?)",
809 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700810 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700811
Joe Onorato9c1289c2009-08-17 11:03:03 -0400812 try {
813 if (c.moveToFirst()) {
814 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
815 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
816 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
817 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
818 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
819 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821 FolderInfo folderInfo = null;
822 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700823 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
824 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700826 }
827
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828 folderInfo.title = c.getString(titleIndex);
829 folderInfo.id = id;
830 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700832 folderInfo.cellX = c.getInt(cellXIndex);
833 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834
835 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 } finally {
838 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700839 }
840
841 return null;
842 }
843
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 /**
845 * Add an item to the database in a specified container. Sets the container, screen, cellX and
846 * cellY fields of the item. Also assigns an ID to the item.
847 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851 item.cellX = cellX;
852 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 // We store hotseat items in canonical form which is this orientation invariant position
854 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700859 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861
862 final ContentValues values = new ContentValues();
863 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864 item.onAddToDatabase(values);
865
Michael Jurka414300a2013-08-27 15:42:35 +0200866 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700867 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700869
Michael Jurkac9d95c52011-08-29 14:03:34 -0700870 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700871 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700872 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700873 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700874 + cellY + ")";
875 Launcher.sDumpLogs.add(transaction);
876 Log.d(TAG, transaction);
877
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 + ")";
937 Launcher.sDumpLogs.add(transaction);
938 Log.d(TAG, transaction);
939
Michael Jurkac9d95c52011-08-29 14:03:34 -0700940 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700941
942 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700943 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700944 switch (item.itemType) {
945 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
946 sBgFolders.remove(item.id);
947 for (ItemInfo info: sBgItemsIdMap.values()) {
948 if (info.container == item.id) {
949 // We are deleting a folder which still contains items that
950 // think they are contained by that folder.
951 String msg = "deleting a folder (" + item + ") which still " +
952 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700953 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700956 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 sBgWorkspaceItems.remove(item);
958 break;
959 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
960 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
961 sBgWorkspaceItems.remove(item);
962 break;
963 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
964 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
965 break;
966 }
967 sBgItemsIdMap.remove(item.id);
968 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700969 }
970 }
Michael Jurka83df1882011-08-31 20:59:26 -0700971 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700972 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 }
974
975 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 * Update the order of the workspace screens in the database. The array list contains
977 * a list of screen ids in the order that they should appear.
978 */
Winson Chungc9168342013-06-26 14:54:55 -0700979 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700980 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder()");
Winson Chung64359a52013-07-08 17:17:08 -0700981 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 final ContentResolver cr = context.getContentResolver();
983 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
984
985 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700986 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700987 while (iter.hasNext()) {
988 long id = iter.next();
989 if (id < 0) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700990 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 iter.remove();
992 }
993 }
994
Winson Chung9e6a0a22013-08-27 11:58:12 -0700995 // Dump the screens copy
996 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy");
997 for (Long l : screensCopy) {
998 Log.w(TAG, "10249126\t- " + l);
999 }
1000
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 Runnable r = new Runnable() {
1002 @Override
1003 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 // Clear the table
1005 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001006 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001007 ContentValues[] values = new ContentValues[count];
1008 for (int i = 0; i < count; i++) {
1009 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001010 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001011 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1012 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001013 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - add: " +
1014 screenId + ", " + i + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001015 values[i] = v;
1016 }
1017 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001018
1019 // Dump the sBgWorkspaceScreens
1020 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens");
1021 for (Long l : sBgWorkspaceScreens) {
1022 Log.w(TAG, "10249126\t- " + l);
1023 }
1024
Adam Cohendcd297f2013-06-18 13:13:40 -07001025 sBgWorkspaceScreens.clear();
1026 sBgWorkspaceScreens.addAll(screensCopy);
1027 }
1028 };
1029 runOnWorkerThread(r);
1030 }
1031
1032 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 * Remove the contents of the specified folder from the database
1034 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001035 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 final ContentResolver cr = context.getContentResolver();
1037
Michael Jurkac9d95c52011-08-29 14:03:34 -07001038 Runnable r = new Runnable() {
1039 public void run() {
1040 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001041 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001042 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001043 sBgItemsIdMap.remove(info.id);
1044 sBgFolders.remove(info.id);
1045 sBgDbIconCache.remove(info);
1046 sBgWorkspaceItems.remove(info);
1047 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048
Michael Jurkac9d95c52011-08-29 14:03:34 -07001049 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1050 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001052 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 for (ItemInfo childInfo : info.contents) {
1054 sBgItemsIdMap.remove(childInfo.id);
1055 sBgDbIconCache.remove(childInfo);
1056 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001057 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001058 }
1059 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 }
1062
1063 /**
1064 * Set this as the current Launcher activity object for the loader.
1065 */
1066 public void initialize(Callbacks callbacks) {
1067 synchronized (mLock) {
1068 mCallbacks = new WeakReference<Callbacks>(callbacks);
1069 }
1070 }
1071
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001072 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1074 * ACTION_PACKAGE_CHANGED.
1075 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001076 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001077 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001078 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001079
Joe Onorato36115782010-06-17 13:28:48 -04001080 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001081
Joe Onorato36115782010-06-17 13:28:48 -04001082 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1083 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1084 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1085 final String packageName = intent.getData().getSchemeSpecificPart();
1086 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Joe Onorato36115782010-06-17 13:28:48 -04001088 int op = PackageUpdatedTask.OP_NONE;
1089
1090 if (packageName == null || packageName.length() == 0) {
1091 // they sent us a bad intent
1092 return;
1093 }
1094
1095 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1096 op = PackageUpdatedTask.OP_UPDATE;
1097 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1098 if (!replacing) {
1099 op = PackageUpdatedTask.OP_REMOVE;
1100 }
1101 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1102 // later, we will update the package at this time
1103 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1104 if (!replacing) {
1105 op = PackageUpdatedTask.OP_ADD;
1106 } else {
1107 op = PackageUpdatedTask.OP_UPDATE;
1108 }
1109 }
1110
1111 if (op != PackageUpdatedTask.OP_NONE) {
1112 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1113 }
1114
1115 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001116 // First, schedule to add these apps back in.
1117 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1118 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1119 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001120 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001121 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1122 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1123 enqueuePackageUpdated(new PackageUpdatedTask(
1124 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001125 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001126 // If we have changed locale we need to clear out the labels in all apps/workspace.
1127 forceReload();
1128 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1129 // Check if configuration change was an mcc/mnc change which would affect app resources
1130 // and we would need to clear out the labels in all apps/workspace. Same handling as
1131 // above for ACTION_LOCALE_CHANGED
1132 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001133 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001134 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001135 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001136 forceReload();
1137 }
1138 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001139 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001140 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1141 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001142 if (mCallbacks != null) {
1143 Callbacks callbacks = mCallbacks.get();
1144 if (callbacks != null) {
1145 callbacks.bindSearchablesChanged();
1146 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001147 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001148 }
1149 }
1150
Reena Lee93f824a2011-09-23 17:20:28 -07001151 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001152 resetLoadedState(true, true);
1153
Reena Lee93f824a2011-09-23 17:20:28 -07001154 // Do this here because if the launcher activity is running it will be restarted.
1155 // If it's not running startLoaderFromBackground will merely tell it that it needs
1156 // to reload.
1157 startLoaderFromBackground();
1158 }
1159
Winson Chungf0c6ae02012-03-21 16:10:31 -07001160 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1161 synchronized (mLock) {
1162 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1163 // mWorkspaceLoaded to true later
1164 stopLoaderLocked();
1165 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1166 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1167 }
1168 }
1169
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001170 /**
1171 * When the launcher is in the background, it's possible for it to miss paired
1172 * configuration changes. So whenever we trigger the loader from the background
1173 * tell the launcher that it needs to re-run the loader when it comes back instead
1174 * of doing it now.
1175 */
1176 public void startLoaderFromBackground() {
1177 boolean runLoader = false;
1178 if (mCallbacks != null) {
1179 Callbacks callbacks = mCallbacks.get();
1180 if (callbacks != null) {
1181 // Only actually run the loader if they're not paused.
1182 if (!callbacks.setLoadOnResume()) {
1183 runLoader = true;
1184 }
1185 }
1186 }
1187 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001189 }
Joe Onorato36115782010-06-17 13:28:48 -04001190 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001191
Reena Lee93f824a2011-09-23 17:20:28 -07001192 // If there is already a loader task running, tell it to stop.
1193 // returns true if isLaunching() was true on the old task
1194 private boolean stopLoaderLocked() {
1195 boolean isLaunching = false;
1196 LoaderTask oldTask = mLoaderTask;
1197 if (oldTask != null) {
1198 if (oldTask.isLaunching()) {
1199 isLaunching = true;
1200 }
1201 oldTask.stopLocked();
1202 }
1203 return isLaunching;
1204 }
1205
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001207 synchronized (mLock) {
1208 if (DEBUG_LOADERS) {
1209 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 // Clear any deferred bind-runnables from the synchronized load process
1213 // We must do this before any loading/binding is scheduled below.
1214 mDeferredBindRunnables.clear();
1215
Joe Onorato36115782010-06-17 13:28:48 -04001216 // Don't bother to start the thread if we know it's not going to do anything
1217 if (mCallbacks != null && mCallbacks.get() != null) {
1218 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001219 // also, don't downgrade isLaunching if we're already running
1220 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001221 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1223 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1224 } else {
1225 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1226 sWorker.post(mLoaderTask);
1227 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229 }
1230 }
1231
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001232 void bindRemainingSynchronousPages() {
1233 // Post the remaining side pages to be loaded
1234 if (!mDeferredBindRunnables.isEmpty()) {
1235 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001236 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001237 }
1238 mDeferredBindRunnables.clear();
1239 }
1240 }
1241
Joe Onorato36115782010-06-17 13:28:48 -04001242 public void stopLoader() {
1243 synchronized (mLock) {
1244 if (mLoaderTask != null) {
1245 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 }
1247 }
Joe Onorato36115782010-06-17 13:28:48 -04001248 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249
Winson Chung76828c82013-08-19 15:43:29 -07001250 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1251 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1252 final ContentResolver contentResolver = context.getContentResolver();
1253 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1254 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1255 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1256
1257 try {
1258 final int idIndex = sc.getColumnIndexOrThrow(
1259 LauncherSettings.WorkspaceScreens._ID);
1260 final int rankIndex = sc.getColumnIndexOrThrow(
1261 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1262 while (sc.moveToNext()) {
1263 try {
1264 long screenId = sc.getLong(idIndex);
1265 int rank = sc.getInt(rankIndex);
1266
1267 Log.w(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")");
1268
1269 orderedScreens.put(rank, screenId);
1270 } catch (Exception e) {
1271 Log.w(TAG, "Desktop items loading interrupted - invalid screens: ", e);
1272 }
1273 }
1274 } finally {
1275 sc.close();
1276 }
1277 return orderedScreens;
1278 }
1279
Michael Jurkac57b7a82011-08-09 22:02:20 -07001280 public boolean isAllAppsLoaded() {
1281 return mAllAppsLoaded;
1282 }
1283
Winson Chung36a62fe2012-05-06 18:04:42 -07001284 boolean isLoadingWorkspace() {
1285 synchronized (mLock) {
1286 if (mLoaderTask != null) {
1287 return mLoaderTask.isLoadingWorkspace();
1288 }
1289 }
1290 return false;
1291 }
1292
Joe Onorato36115782010-06-17 13:28:48 -04001293 /**
1294 * Runnable for the thread that loads the contents of the launcher:
1295 * - workspace icons
1296 * - widgets
1297 * - all apps icons
1298 */
1299 private class LoaderTask implements Runnable {
1300 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001301 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001302 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001303 private boolean mStopped;
1304 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001305
Winson Chungc3eecff2011-07-11 17:44:15 -07001306 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001307
1308 LoaderTask(Context context, boolean isLaunching) {
1309 mContext = context;
1310 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001311 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312 }
1313
Joe Onorato36115782010-06-17 13:28:48 -04001314 boolean isLaunching() {
1315 return mIsLaunching;
1316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317
Winson Chung36a62fe2012-05-06 18:04:42 -07001318 boolean isLoadingWorkspace() {
1319 return mIsLoadingAndBindingWorkspace;
1320 }
1321
Winson Chungc763c4e2013-07-19 13:49:06 -07001322 /** Returns whether this is an upgrade path */
1323 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001324 mIsLoadingAndBindingWorkspace = true;
1325
Joe Onorato36115782010-06-17 13:28:48 -04001326 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001327 if (DEBUG_LOADERS) {
1328 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001330
Winson Chungc763c4e2013-07-19 13:49:06 -07001331 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001332 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001333 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001334 synchronized (LoaderTask.this) {
1335 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001336 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001337 }
1338 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
1340 }
1341
Joe Onorato36115782010-06-17 13:28:48 -04001342 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001343 bindWorkspace(-1, isUpgradePath);
1344 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001345 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001346
Joe Onorato36115782010-06-17 13:28:48 -04001347 private void waitForIdle() {
1348 // Wait until the either we're stopped or the other threads are done.
1349 // This way we don't start loading all apps until the workspace has settled
1350 // down.
1351 synchronized (LoaderTask.this) {
1352 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001353
Joe Onorato36115782010-06-17 13:28:48 -04001354 mHandler.postIdle(new Runnable() {
1355 public void run() {
1356 synchronized (LoaderTask.this) {
1357 mLoadAndBindStepFinished = true;
1358 if (DEBUG_LOADERS) {
1359 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001360 }
Joe Onorato36115782010-06-17 13:28:48 -04001361 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001362 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001363 }
Joe Onorato36115782010-06-17 13:28:48 -04001364 });
1365
Michael Jurkac7700af2013-05-14 20:17:58 +02001366 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001367 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001368 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1369 // wait no longer than 1sec at a time
1370 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001371 } catch (InterruptedException ex) {
1372 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001373 }
1374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375 if (DEBUG_LOADERS) {
1376 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001377 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001378 + "ms for previous step to finish binding");
1379 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001380 }
Joe Onorato36115782010-06-17 13:28:48 -04001381 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 void runBindSynchronousPage(int synchronousBindPage) {
1384 if (synchronousBindPage < 0) {
1385 // Ensure that we have a valid page index to load synchronously
1386 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1387 "valid page index");
1388 }
1389 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1390 // Ensure that we don't try and bind a specified page when the pages have not been
1391 // loaded already (we should load everything asynchronously in that case)
1392 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1393 }
1394 synchronized (mLock) {
1395 if (mIsLoaderTaskRunning) {
1396 // Ensure that we are never running the background loading at this point since
1397 // we also touch the background collections
1398 throw new RuntimeException("Error! Background loading is already running");
1399 }
1400 }
1401
1402 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1403 // data structures, we can't allow any other thread to touch that data, but because
1404 // this call is synchronous, we can get away with not locking).
1405
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001406 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001407 // operations from the previous activity. We need to ensure that all queued operations
1408 // are executed before any synchronous binding work is done.
1409 mHandler.flush();
1410
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001411 // Divide the set of loaded items into those that we are binding synchronously, and
1412 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001413 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001414 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1415 // arise from that.
1416 onlyBindAllApps();
1417 }
1418
Joe Onorato36115782010-06-17 13:28:48 -04001419 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001420 boolean isUpgrade = false;
1421
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 synchronized (mLock) {
1423 mIsLoaderTaskRunning = true;
1424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 // Optimize for end-user experience: if the Launcher is up and // running with the
1426 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1427 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001428 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001429 // Elevate priority when Home launches for the first time to avoid
1430 // starving at boot time. Staring at a blank home is not cool.
1431 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001432 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1433 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001434 android.os.Process.setThreadPriority(mIsLaunching
1435 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1436 }
Winson Chung64359a52013-07-08 17:17:08 -07001437 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001438 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001439
Joe Onorato36115782010-06-17 13:28:48 -04001440 if (mStopped) {
1441 break keep_running;
1442 }
1443
1444 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1445 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001446 synchronized (mLock) {
1447 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001448 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001449 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1450 }
1451 }
Joe Onorato36115782010-06-17 13:28:48 -04001452 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001453
1454 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001455 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1456 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001457
1458 // Restore the default thread priority after we are done loading items
1459 synchronized (mLock) {
1460 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463
Winson Chungaac01e12011-08-17 10:37:13 -07001464 // Update the saved icons if necessary
1465 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001466 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001467 for (Object key : sBgDbIconCache.keySet()) {
1468 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1469 }
1470 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001471 }
Winson Chungaac01e12011-08-17 10:37:13 -07001472
Winson Chungc763c4e2013-07-19 13:49:06 -07001473 // Ensure that all the applications that are in the system are represented on the home
1474 // screen.
Daniel Sandler8707e0f2013-08-15 15:54:18 -07001475 Log.w(TAG, "10249126 - verifyApplications - useMoreApps="
1476 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade);
1477 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001478 verifyApplications();
1479 }
1480
Joe Onorato36115782010-06-17 13:28:48 -04001481 // Clear out this reference, otherwise we end up holding it until all of the
1482 // callback runnables are done.
1483 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484
Joe Onorato36115782010-06-17 13:28:48 -04001485 synchronized (mLock) {
1486 // If we are still the last one to be scheduled, remove ourselves.
1487 if (mLoaderTask == this) {
1488 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001490 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492 }
1493
1494 public void stopLocked() {
1495 synchronized (LoaderTask.this) {
1496 mStopped = true;
1497 this.notify();
1498 }
1499 }
1500
1501 /**
1502 * Gets the callbacks object. If we've been stopped, or if the launcher object
1503 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1504 * object that was around when the deferred message was scheduled, and if there's
1505 * a new Callbacks object around then also return null. This will save us from
1506 * calling onto it with data that will be ignored.
1507 */
1508 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1509 synchronized (mLock) {
1510 if (mStopped) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 if (mCallbacks == null) {
1515 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001516 }
Joe Onorato36115782010-06-17 13:28:48 -04001517
1518 final Callbacks callbacks = mCallbacks.get();
1519 if (callbacks != oldCallbacks) {
1520 return null;
1521 }
1522 if (callbacks == null) {
1523 Log.w(TAG, "no mCallbacks");
1524 return null;
1525 }
1526
1527 return callbacks;
1528 }
1529 }
1530
Winson Chungc763c4e2013-07-19 13:49:06 -07001531 private void verifyApplications() {
1532 final Context context = mApp.getContext();
1533
1534 // Cross reference all the applications in our apps list with items in the workspace
1535 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001536 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001537 synchronized (sBgLock) {
1538 for (ApplicationInfo app : mBgAllAppsList.data) {
1539 tmpInfos = getItemInfoForComponentName(app.componentName);
Winson Chung76828c82013-08-19 15:43:29 -07001540 Log.w(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty());
Winson Chungc763c4e2013-07-19 13:49:06 -07001541 if (tmpInfos.isEmpty()) {
1542 // We are missing an application icon, so add this to the workspace
1543 added.add(app);
1544 // This is a rare event, so lets log it
1545 Log.e(TAG, "Missing Application on load: " + app);
1546 }
1547 }
1548 }
1549 if (!added.isEmpty()) {
1550 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
1551 addAndBindAddedApps(context, added, cb);
1552 }
1553 }
1554
Winson Chung5f8afe62013-08-12 16:19:28 -07001555 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001556 LauncherAppState app = LauncherAppState.getInstance();
1557 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1558 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1559 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001560 }
1561
Joe Onorato36115782010-06-17 13:28:48 -04001562 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001564 LauncherAppState app = LauncherAppState.getInstance();
1565 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1566 int countX = (int) grid.numColumns;
1567 int countY = (int) grid.numRows;
1568
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001570 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1572 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1573 [(int) item.screenId][0] != null) {
1574 Log.e(TAG, "Error loading shortcut into hotseat " + item
1575 + " into position (" + item.screenId + ":" + item.cellX + ","
1576 + item.cellY + ") occupied by "
1577 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1578 [(int) item.screenId][0]);
1579 return false;
1580 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001581 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001582 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 items[(int) item.screenId][0] = item;
1584 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001585 return true;
1586 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001587 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1588 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001589 return true;
1590 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001591
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001593 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 occupied.put(item.screenId, items);
1595 }
1596
1597 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001598 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001599 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1600 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001601 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001602 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001604 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001605 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001607 return false;
1608 }
1609 }
1610 }
1611 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1612 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001614 }
1615 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001616
Joe Onorato36115782010-06-17 13:28:48 -04001617 return true;
1618 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001619
Winson Chungc763c4e2013-07-19 13:49:06 -07001620 /** Returns whether this is an upgradge path */
1621 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001622 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623
Joe Onorato36115782010-06-17 13:28:48 -04001624 final Context context = mContext;
1625 final ContentResolver contentResolver = context.getContentResolver();
1626 final PackageManager manager = context.getPackageManager();
1627 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1628 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001629
Winson Chung892c74d2013-08-22 16:15:50 -07001630 LauncherAppState app = LauncherAppState.getInstance();
1631 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1632 int countX = (int) grid.numColumns;
1633 int countY = (int) grid.numRows;
1634
Michael Jurkab85f8a42012-04-25 15:48:32 -07001635 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001636 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001637
Winson Chungc763c4e2013-07-19 13:49:06 -07001638 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001639 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001640
Winson Chung2abf94d2012-07-18 18:16:38 -07001641 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001642 sBgWorkspaceItems.clear();
1643 sBgAppWidgets.clear();
1644 sBgFolders.clear();
1645 sBgItemsIdMap.clear();
1646 sBgDbIconCache.clear();
Adam Cohendcd297f2013-06-18 13:13:40 -07001647 sBgWorkspaceScreens.clear();
Winson Chung76828c82013-08-19 15:43:29 -07001648 Log.w(TAG, "10249126 - loadWorkspace()");
Romain Guy5c16f3e2010-01-12 17:24:58 -08001649
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001650 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001651 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001652 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001653
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001654 // +1 for the hotseat (it can be larger than the workspace)
1655 // Load workspace in reverse order to ensure that latest items are loaded first (and
1656 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001657 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001659 try {
1660 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1661 final int intentIndex = c.getColumnIndexOrThrow
1662 (LauncherSettings.Favorites.INTENT);
1663 final int titleIndex = c.getColumnIndexOrThrow
1664 (LauncherSettings.Favorites.TITLE);
1665 final int iconTypeIndex = c.getColumnIndexOrThrow(
1666 LauncherSettings.Favorites.ICON_TYPE);
1667 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1668 final int iconPackageIndex = c.getColumnIndexOrThrow(
1669 LauncherSettings.Favorites.ICON_PACKAGE);
1670 final int iconResourceIndex = c.getColumnIndexOrThrow(
1671 LauncherSettings.Favorites.ICON_RESOURCE);
1672 final int containerIndex = c.getColumnIndexOrThrow(
1673 LauncherSettings.Favorites.CONTAINER);
1674 final int itemTypeIndex = c.getColumnIndexOrThrow(
1675 LauncherSettings.Favorites.ITEM_TYPE);
1676 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1677 LauncherSettings.Favorites.APPWIDGET_ID);
1678 final int screenIndex = c.getColumnIndexOrThrow(
1679 LauncherSettings.Favorites.SCREEN);
1680 final int cellXIndex = c.getColumnIndexOrThrow
1681 (LauncherSettings.Favorites.CELLX);
1682 final int cellYIndex = c.getColumnIndexOrThrow
1683 (LauncherSettings.Favorites.CELLY);
1684 final int spanXIndex = c.getColumnIndexOrThrow
1685 (LauncherSettings.Favorites.SPANX);
1686 final int spanYIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.SPANY);
1688 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1689 //final int displayModeIndex = c.getColumnIndexOrThrow(
1690 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001691
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001692 ShortcutInfo info;
1693 String intentDescription;
1694 LauncherAppWidgetInfo appWidgetInfo;
1695 int container;
1696 long id;
1697 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 while (!mStopped && c.moveToNext()) {
1700 try {
1701 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 switch (itemType) {
1704 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1705 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001706 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 intentDescription = c.getString(intentIndex);
1708 try {
1709 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001710 ComponentName cn = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07001711 if (!isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001712 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001713 // Log the invalid package, and remove it from the db
1714 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1715 false);
Winson Chungee055712013-07-30 14:46:24 -07001716 contentResolver.delete(uri, null, null);
Winson Chung1323b482013-08-05 12:41:55 -07001717 Log.e(TAG, "Invalid package removed: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001718 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001719 // If apps can be on external storage, then we just
1720 // leave them for the user to remove (maybe add
1721 // visual treatment to it)
1722 Log.e(TAG, "Invalid package found: " + cn);
Winson Chungee055712013-07-30 14:46:24 -07001723 }
1724 continue;
1725 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001726 } catch (URISyntaxException e) {
1727 continue;
1728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1731 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1732 titleIndex, mLabelCache);
1733 } else {
1734 info = getShortcutInfo(c, context, iconTypeIndex,
1735 iconPackageIndex, iconResourceIndex, iconIndex,
1736 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001737
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001738 // App shortcuts that used to be automatically added to Launcher
1739 // didn't always have the correct intent flags set, so do that
1740 // here
1741 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001742 intent.getCategories() != null &&
1743 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001744 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 intent.addFlags(
1746 Intent.FLAG_ACTIVITY_NEW_TASK |
1747 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1748 }
Michael Jurka96879562012-03-22 05:54:33 -07001749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001752 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 container = c.getInt(containerIndex);
1755 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001756 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 info.cellX = c.getInt(cellXIndex);
1758 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001759 info.spanX = 1;
1760 info.spanY = 1;
1761 // Skip loading items that are out of bounds
1762 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1763 if (checkItemDimensions(info)) {
1764 Log.d(TAG, "Skipped loading out of bounds shortcut: "
1765 + info.intent);
1766 continue;
1767 }
1768 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 // check & update map of what's occupied
1770 if (!checkItemPlacement(occupied, info)) {
1771 break;
1772 }
1773
1774 switch (container) {
1775 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1776 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1777 sBgWorkspaceItems.add(info);
1778 break;
1779 default:
1780 // Item is in a user folder
1781 FolderInfo folderInfo =
1782 findOrMakeFolder(sBgFolders, container);
1783 folderInfo.add(info);
1784 break;
1785 }
1786 sBgItemsIdMap.put(info.id, info);
1787
1788 // now that we've loaded everthing re-save it with the
1789 // icon in case it disappears somehow.
1790 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001791 } else {
1792 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001793 }
1794 break;
1795
1796 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1797 id = c.getLong(idIndex);
1798 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1799
1800 folderInfo.title = c.getString(titleIndex);
1801 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001803 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001804 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001805 folderInfo.cellX = c.getInt(cellXIndex);
1806 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001807 folderInfo.spanX = 1;
1808 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809
Winson Chung5f8afe62013-08-12 16:19:28 -07001810 // Skip loading items that are out of bounds
1811 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001812 if (checkItemDimensions(folderInfo)) {
1813 Log.d(TAG, "Skipped loading out of bounds folder");
1814 continue;
1815 }
1816 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001817 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001819 break;
1820 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001821
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1824 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1825 sBgWorkspaceItems.add(folderInfo);
1826 break;
Joe Onorato36115782010-06-17 13:28:48 -04001827 }
Joe Onorato17a89222011-02-08 17:26:11 -08001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1830 sBgFolders.put(folderInfo.id, folderInfo);
1831 break;
1832
1833 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1834 // Read all Launcher-specific widget details
1835 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001836 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001837
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 final AppWidgetProviderInfo provider =
1839 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001840
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 if (!isSafeMode && (provider == null || provider.provider == null ||
1842 provider.provider.getPackageName() == null)) {
1843 String log = "Deleting widget that isn't installed anymore: id="
1844 + id + " appWidgetId=" + appWidgetId;
1845 Log.e(TAG, log);
1846 Launcher.sDumpLogs.add(log);
1847 itemsToRemove.add(id);
1848 } else {
1849 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1850 provider.provider);
1851 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001852 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 appWidgetInfo.cellX = c.getInt(cellXIndex);
1854 appWidgetInfo.cellY = c.getInt(cellYIndex);
1855 appWidgetInfo.spanX = c.getInt(spanXIndex);
1856 appWidgetInfo.spanY = c.getInt(spanYIndex);
1857 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1858 appWidgetInfo.minSpanX = minSpan[0];
1859 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 container = c.getInt(containerIndex);
1862 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1863 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1864 Log.e(TAG, "Widget found where container != " +
1865 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1866 continue;
1867 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868
Adam Cohene25af792013-06-06 23:08:25 -07001869 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001870 // Skip loading items that are out of bounds
1871 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1872 if (checkItemDimensions(appWidgetInfo)) {
1873 Log.d(TAG, "Skipped loading out of bounds app widget");
1874 continue;
1875 }
1876 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 // check & update map of what's occupied
1878 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1879 break;
1880 }
1881 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1882 sBgAppWidgets.add(appWidgetInfo);
1883 }
Joe Onorato36115782010-06-17 13:28:48 -04001884 break;
1885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 } catch (Exception e) {
1887 Log.w(TAG, "Desktop items loading interrupted:", e);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 }
1890 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001891 if (c != null) {
1892 c.close();
1893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 }
1895
1896 if (itemsToRemove.size() > 0) {
1897 ContentProviderClient client = contentResolver.acquireContentProviderClient(
1898 LauncherSettings.Favorites.CONTENT_URI);
1899 // Remove dead items
1900 for (long id : itemsToRemove) {
1901 if (DEBUG_LOADERS) {
1902 Log.d(TAG, "Removed id = " + id);
1903 }
1904 // Don't notify content observers
1905 try {
1906 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1907 null, null);
1908 } catch (RemoteException e) {
1909 Log.w(TAG, "Could not remove id = " + id);
1910 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001911 }
1912 }
1913
Winson Chungc763c4e2013-07-19 13:49:06 -07001914 if (loadedOldDb) {
Winson Chungd64d1762013-08-20 14:37:16 -07001915 Log.w(TAG, "10249126 - loadWorkspace - loadedOldDb");
Adam Cohendcd297f2013-06-18 13:13:40 -07001916 long maxScreenId = 0;
1917 // If we're importing we use the old screen order.
1918 for (ItemInfo item: sBgItemsIdMap.values()) {
1919 long screenId = item.screenId;
1920 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1921 !sBgWorkspaceScreens.contains(screenId)) {
Winson Chung76828c82013-08-19 15:43:29 -07001922 Log.w(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")");
Adam Cohendcd297f2013-06-18 13:13:40 -07001923 sBgWorkspaceScreens.add(screenId);
1924 if (screenId > maxScreenId) {
1925 maxScreenId = screenId;
1926 }
1927 }
1928 }
1929 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001930
1931 // Dump the sBgWorkspaceScreens
1932 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens");
1933 for (Long l : sBgWorkspaceScreens) {
1934 Log.w(TAG, "10249126\t- " + l);
1935 }
1936
Michael Jurka414300a2013-08-27 15:42:35 +02001937 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001939
1940 // Update the max item id after we load an old db
1941 long maxItemId = 0;
1942 // If we're importing we use the old screen order.
1943 for (ItemInfo item: sBgItemsIdMap.values()) {
1944 maxItemId = Math.max(maxItemId, item.id);
1945 }
Michael Jurka414300a2013-08-27 15:42:35 +02001946 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 } else {
Winson Chungd64d1762013-08-20 14:37:16 -07001948 Log.w(TAG, "10249126 - loadWorkspace - !loadedOldDb");
Winson Chung76828c82013-08-19 15:43:29 -07001949 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1950 for (Integer i : orderedScreens.keySet()) {
1951 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 }
1953
1954 // Remove any empty screens
1955 ArrayList<Long> unusedScreens = new ArrayList<Long>();
1956 unusedScreens.addAll(sBgWorkspaceScreens);
1957
1958 for (ItemInfo item: sBgItemsIdMap.values()) {
1959 long screenId = item.screenId;
1960
1961 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1962 unusedScreens.contains(screenId)) {
1963 unusedScreens.remove(screenId);
1964 }
1965 }
1966
1967 // If there are any empty screens remove them, and update.
1968 if (unusedScreens.size() != 0) {
1969 sBgWorkspaceScreens.removeAll(unusedScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001970
1971 // Dump the sBgWorkspaceScreens
1972 Log.w(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens");
1973 for (Long l : sBgWorkspaceScreens) {
1974 Log.w(TAG, "10249126\t- " + l);
1975 }
1976
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1978 }
1979 }
1980
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 if (DEBUG_LOADERS) {
1982 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1983 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001985 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001987
Daniel Sandler566da102013-06-25 23:43:45 -04001988 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001989 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001991 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001992 line += " | ";
1993 }
Winson Chung892c74d2013-08-22 16:15:50 -07001994 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 }
Joe Onorato36115782010-06-17 13:28:48 -04001997 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 }
Joe Onorato36115782010-06-17 13:28:48 -04002001 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002002 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002003 }
2004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 /** Filters the set of items who are directly or indirectly (via another container) on the
2006 * specified screen. */
2007 private void filterCurrentWorkspaceItems(int currentScreen,
2008 ArrayList<ItemInfo> allWorkspaceItems,
2009 ArrayList<ItemInfo> currentScreenItems,
2010 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002011 // Purge any null ItemInfos
2012 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2013 while (iter.hasNext()) {
2014 ItemInfo i = iter.next();
2015 if (i == null) {
2016 iter.remove();
2017 }
2018 }
2019
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 // If we aren't filtering on a screen, then the set of items to load is the full set of
2021 // items given.
2022 if (currentScreen < 0) {
2023 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002024 }
2025
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 // Order the set of items by their containers first, this allows use to walk through the
2027 // list sequentially, build up a list of containers that are in the specified screen,
2028 // as well as all items in those containers.
2029 Set<Long> itemsOnScreen = new HashSet<Long>();
2030 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2031 @Override
2032 public int compare(ItemInfo lhs, ItemInfo rhs) {
2033 return (int) (lhs.container - rhs.container);
2034 }
2035 });
2036 for (ItemInfo info : allWorkspaceItems) {
2037 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002038 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002039 currentScreenItems.add(info);
2040 itemsOnScreen.add(info.id);
2041 } else {
2042 otherScreenItems.add(info);
2043 }
2044 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2045 currentScreenItems.add(info);
2046 itemsOnScreen.add(info.id);
2047 } else {
2048 if (itemsOnScreen.contains(info.container)) {
2049 currentScreenItems.add(info);
2050 itemsOnScreen.add(info.id);
2051 } else {
2052 otherScreenItems.add(info);
2053 }
2054 }
2055 }
2056 }
2057
2058 /** Filters the set of widgets which are on the specified screen. */
2059 private void filterCurrentAppWidgets(int currentScreen,
2060 ArrayList<LauncherAppWidgetInfo> appWidgets,
2061 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2062 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2063 // If we aren't filtering on a screen, then the set of items to load is the full set of
2064 // widgets given.
2065 if (currentScreen < 0) {
2066 currentScreenWidgets.addAll(appWidgets);
2067 }
2068
2069 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002070 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002072 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 currentScreenWidgets.add(widget);
2074 } else {
2075 otherScreenWidgets.add(widget);
2076 }
2077 }
2078 }
2079
2080 /** Filters the set of folders which are on the specified screen. */
2081 private void filterCurrentFolders(int currentScreen,
2082 HashMap<Long, ItemInfo> itemsIdMap,
2083 HashMap<Long, FolderInfo> folders,
2084 HashMap<Long, FolderInfo> currentScreenFolders,
2085 HashMap<Long, FolderInfo> otherScreenFolders) {
2086 // If we aren't filtering on a screen, then the set of items to load is the full set of
2087 // widgets given.
2088 if (currentScreen < 0) {
2089 currentScreenFolders.putAll(folders);
2090 }
2091
2092 for (long id : folders.keySet()) {
2093 ItemInfo info = itemsIdMap.get(id);
2094 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002095 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002097 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002098 currentScreenFolders.put(id, folder);
2099 } else {
2100 otherScreenFolders.put(id, folder);
2101 }
2102 }
2103 }
2104
2105 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2106 * right) */
2107 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002108 final LauncherAppState app = LauncherAppState.getInstance();
2109 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 // XXX: review this
2111 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002112 @Override
2113 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002114 int cellCountX = (int) grid.numColumns;
2115 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002116 int screenOffset = cellCountX * cellCountY;
2117 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002119 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002121 rhs.cellY * cellCountX + rhs.cellX);
2122 return (int) (lr - rr);
2123 }
2124 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002125 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002126
Adam Cohendcd297f2013-06-18 13:13:40 -07002127 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2128 final ArrayList<Long> orderedScreens) {
Winson Chung76828c82013-08-19 15:43:29 -07002129 Log.w(TAG, "10249126 - bindWorkspaceScreens()");
Winson Chung9e6a0a22013-08-27 11:58:12 -07002130
2131 // Dump the orderedScreens
2132 Log.w(TAG, "10249126 - orderedScreens");
2133 for (Long l : sBgWorkspaceScreens) {
2134 Log.w(TAG, "10249126\t- " + l);
2135 }
2136
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 final Runnable r = new Runnable() {
2138 @Override
2139 public void run() {
2140 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2141 if (callbacks != null) {
2142 callbacks.bindScreens(orderedScreens);
2143 }
2144 }
2145 };
2146 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2147 }
2148
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2150 final ArrayList<ItemInfo> workspaceItems,
2151 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2152 final HashMap<Long, FolderInfo> folders,
2153 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002154
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 final boolean postOnMainThread = (deferredBindRunnables != null);
2156
2157 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002158 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002160 final int start = i;
2161 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 final Runnable r = new Runnable() {
2163 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002165 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002167 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2168 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 }
2170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 };
2172 if (postOnMainThread) {
2173 deferredBindRunnables.add(r);
2174 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002175 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 }
Joe Onorato36115782010-06-17 13:28:48 -04002177 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178
2179 // Bind the folders
2180 if (!folders.isEmpty()) {
2181 final Runnable r = new Runnable() {
2182 public void run() {
2183 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2184 if (callbacks != null) {
2185 callbacks.bindFolders(folders);
2186 }
2187 }
2188 };
2189 if (postOnMainThread) {
2190 deferredBindRunnables.add(r);
2191 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002192 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 }
2194 }
2195
2196 // Bind the widgets, one at a time
2197 N = appWidgets.size();
2198 for (int i = 0; i < N; i++) {
2199 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2200 final Runnable r = new Runnable() {
2201 public void run() {
2202 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2203 if (callbacks != null) {
2204 callbacks.bindAppWidget(widget);
2205 }
2206 }
2207 };
2208 if (postOnMainThread) {
2209 deferredBindRunnables.add(r);
2210 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002211 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 }
2213 }
2214 }
2215
2216 /**
2217 * Binds all loaded data to actual views on the main thread.
2218 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002219 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 final long t = SystemClock.uptimeMillis();
2221 Runnable r;
2222
2223 // Don't use these two variables in any of the callback runnables.
2224 // Otherwise we hold a reference to them.
2225 final Callbacks oldCallbacks = mCallbacks.get();
2226 if (oldCallbacks == null) {
2227 // This launcher has exited and nobody bothered to tell us. Just bail.
2228 Log.w(TAG, "LoaderTask running with no launcher");
2229 return;
2230 }
2231
Winson Chung4a2afa32012-07-19 14:53:05 -07002232 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2233 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 oldCallbacks.getCurrentWorkspaceScreen();
2235
2236 // Load all the items that are on the current page first (and in the process, unbind
2237 // all the existing workspace items before we call startBinding() below.
2238 unbindWorkspaceItemsOnMainThread();
2239 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2240 ArrayList<LauncherAppWidgetInfo> appWidgets =
2241 new ArrayList<LauncherAppWidgetInfo>();
2242 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2243 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002245 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 workspaceItems.addAll(sBgWorkspaceItems);
2247 appWidgets.addAll(sBgAppWidgets);
2248 folders.putAll(sBgFolders);
2249 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 }
2252
2253 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2254 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2255 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2256 new ArrayList<LauncherAppWidgetInfo>();
2257 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2258 new ArrayList<LauncherAppWidgetInfo>();
2259 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2260 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2261
2262 // Separate the items that are on the current screen, and all the other remaining items
2263 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2264 otherWorkspaceItems);
2265 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2266 otherAppWidgets);
2267 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2268 otherFolders);
2269 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2270 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2271
2272 // Tell the workspace that we're about to start binding items
2273 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002274 public void run() {
2275 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2276 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002278 }
2279 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 };
Winson Chung81b52252012-08-27 15:34:29 -07002281 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282
Adam Cohendcd297f2013-06-18 13:13:40 -07002283 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2284
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 // Load items on the current page
2286 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2287 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002288 if (isLoadingSynchronously) {
2289 r = new Runnable() {
2290 public void run() {
2291 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2292 if (callbacks != null) {
2293 callbacks.onPageBoundSynchronously(currentScreen);
2294 }
2295 }
2296 };
Winson Chung81b52252012-08-27 15:34:29 -07002297 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002298 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299
Winson Chung4a2afa32012-07-19 14:53:05 -07002300 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2301 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 mDeferredBindRunnables.clear();
2303 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002304 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305
2306 // Tell the workspace that we're done binding items
2307 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002308 public void run() {
2309 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2310 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002311 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002312 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313
Winson Chung98e030b2012-05-07 16:01:11 -07002314 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002315 if (DEBUG_LOADERS) {
2316 Log.d(TAG, "bound workspace in "
2317 + (SystemClock.uptimeMillis()-t) + "ms");
2318 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002319
2320 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002321 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002323 if (isLoadingSynchronously) {
2324 mDeferredBindRunnables.add(r);
2325 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002326 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002327 }
Joe Onorato36115782010-06-17 13:28:48 -04002328 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002329
Joe Onorato36115782010-06-17 13:28:48 -04002330 private void loadAndBindAllApps() {
2331 if (DEBUG_LOADERS) {
2332 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2333 }
2334 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002335 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002336 synchronized (LoaderTask.this) {
2337 if (mStopped) {
2338 return;
2339 }
2340 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002341 }
Joe Onorato36115782010-06-17 13:28:48 -04002342 } else {
2343 onlyBindAllApps();
2344 }
2345 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002346
Joe Onorato36115782010-06-17 13:28:48 -04002347 private void onlyBindAllApps() {
2348 final Callbacks oldCallbacks = mCallbacks.get();
2349 if (oldCallbacks == null) {
2350 // This launcher has exited and nobody bothered to tell us. Just bail.
2351 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2352 return;
2353 }
2354
2355 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002356 @SuppressWarnings("unchecked")
Joe Onorato36115782010-06-17 13:28:48 -04002357 final ArrayList<ApplicationInfo> list
Adam Cohen487f7dd2012-06-28 18:12:10 -07002358 = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002359 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002360 public void run() {
2361 final long t = SystemClock.uptimeMillis();
2362 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2363 if (callbacks != null) {
2364 callbacks.bindAllApplications(list);
2365 }
2366 if (DEBUG_LOADERS) {
2367 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2368 + (SystemClock.uptimeMillis()-t) + "ms");
2369 }
2370 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002371 };
2372 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002373 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002374 r.run();
2375 } else {
2376 mHandler.post(r);
2377 }
Joe Onorato36115782010-06-17 13:28:48 -04002378 }
2379
Winson Chung64359a52013-07-08 17:17:08 -07002380 private void loadAllApps() {
2381 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002382
2383 // Don't use these two variables in any of the callback runnables.
2384 // Otherwise we hold a reference to them.
2385 final Callbacks oldCallbacks = mCallbacks.get();
2386 if (oldCallbacks == null) {
2387 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002388 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002389 return;
2390 }
2391
Winson Chung64359a52013-07-08 17:17:08 -07002392 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002393 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2394 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2395
Winson Chung64359a52013-07-08 17:17:08 -07002396 // Clear the list of apps
2397 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002398
Winson Chung64359a52013-07-08 17:17:08 -07002399 // Query for the set of apps
2400 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2401 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2402 if (DEBUG_LOADERS) {
2403 Log.d(TAG, "queryIntentActivities took "
2404 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2405 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2406 }
2407 // Fail if we don't have any apps
2408 if (apps == null || apps.isEmpty()) {
2409 return;
2410 }
2411 // Sort the applications by name
2412 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2413 Collections.sort(apps,
2414 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2415 if (DEBUG_LOADERS) {
2416 Log.d(TAG, "sort took "
2417 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002418 }
2419
Winson Chung64359a52013-07-08 17:17:08 -07002420 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002421 for (int i = 0; i < apps.size(); i++) {
2422 // This builds the icon bitmaps.
2423 mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i),
2424 mIconCache, mLabelCache));
2425 }
2426
2427 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2428 final ArrayList<ApplicationInfo> added = mBgAllAppsList.added;
2429 mBgAllAppsList.added = new ArrayList<ApplicationInfo>();
2430
2431 // Post callback on main thread
2432 mHandler.post(new Runnable() {
2433 public void run() {
2434 final long bindTime = SystemClock.uptimeMillis();
2435 if (callbacks != null) {
2436 callbacks.bindAllApplications(added);
2437 if (DEBUG_LOADERS) {
2438 Log.d(TAG, "bound " + added.size() + " apps in "
2439 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2440 }
2441 } else {
2442 Log.i(TAG, "not binding apps: no Launcher activity");
2443 }
2444 }
2445 });
2446
Joe Onorato36115782010-06-17 13:28:48 -04002447 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002448 Log.d(TAG, "Icons processed in "
2449 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002450 }
2451 }
2452
2453 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002454 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002455 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2456 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2457 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2458 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2459 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2460 }
Joe Onorato36115782010-06-17 13:28:48 -04002461 }
2462 }
2463
2464 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002465 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002466 }
2467
2468 private class PackageUpdatedTask implements Runnable {
2469 int mOp;
2470 String[] mPackages;
2471
2472 public static final int OP_NONE = 0;
2473 public static final int OP_ADD = 1;
2474 public static final int OP_UPDATE = 2;
2475 public static final int OP_REMOVE = 3; // uninstlled
2476 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2477
2478
2479 public PackageUpdatedTask(int op, String[] packages) {
2480 mOp = op;
2481 mPackages = packages;
2482 }
2483
2484 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002485 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002486
2487 final String[] packages = mPackages;
2488 final int N = packages.length;
2489 switch (mOp) {
2490 case OP_ADD:
2491 for (int i=0; i<N; i++) {
2492 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002493 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002494 }
2495 break;
2496 case OP_UPDATE:
2497 for (int i=0; i<N; i++) {
2498 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002499 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002500 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002501 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002502 }
2503 break;
2504 case OP_REMOVE:
2505 case OP_UNAVAILABLE:
2506 for (int i=0; i<N; i++) {
2507 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002508 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002509 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002510 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002511 }
2512 break;
2513 }
2514
2515 ArrayList<ApplicationInfo> added = null;
Joe Onorato36115782010-06-17 13:28:48 -04002516 ArrayList<ApplicationInfo> modified = null;
Winson Chung83892cc2013-05-01 16:53:33 -07002517 final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002518
Adam Cohen487f7dd2012-06-28 18:12:10 -07002519 if (mBgAllAppsList.added.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002520 added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added);
2521 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002522 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002523 if (mBgAllAppsList.modified.size() > 0) {
Winson Chung5d55f332012-07-16 20:45:03 -07002524 modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified);
2525 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002526 }
Winson Chung5d55f332012-07-16 20:45:03 -07002527 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002528 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002529 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002530 }
2531
Joe Onorato36115782010-06-17 13:28:48 -04002532 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2533 if (callbacks == null) {
2534 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2535 return;
2536 }
2537
2538 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002539 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002540 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002541 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung997a9232013-07-24 15:33:46 -07002542 addAndBindAddedApps(context, addedInfos, cb);
Joe Onorato36115782010-06-17 13:28:48 -04002543 }
2544 if (modified != null) {
2545 final ArrayList<ApplicationInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002546
2547 // Update the launcher db to reflect the changes
2548 for (ApplicationInfo a : modifiedFinal) {
2549 ArrayList<ItemInfo> infos =
2550 getItemInfoForComponentName(a.componentName);
2551 for (ItemInfo i : infos) {
2552 if (isShortcutInfoUpdateable(i)) {
2553 ShortcutInfo info = (ShortcutInfo) i;
2554 info.title = a.title.toString();
2555 updateItemInDatabase(context, info);
2556 }
2557 }
2558 }
2559
Joe Onorato36115782010-06-17 13:28:48 -04002560 mHandler.post(new Runnable() {
2561 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002562 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2563 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002564 callbacks.bindAppsUpdated(modifiedFinal);
2565 }
2566 }
2567 });
2568 }
Winson Chung83892cc2013-05-01 16:53:33 -07002569 // If a package has been removed, or an app has been removed as a result of
2570 // an update (for example), make the removed callback.
2571 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002572 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002573 final ArrayList<String> removedPackageNames =
2574 new ArrayList<String>(Arrays.asList(packages));
2575
Winson Chung64359a52013-07-08 17:17:08 -07002576 // Update the launcher db to reflect the removal of apps
2577 if (packageRemoved) {
2578 for (String pn : removedPackageNames) {
2579 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2580 for (ItemInfo i : infos) {
2581 deleteItemFromDatabase(context, i);
2582 }
2583 }
2584 } else {
2585 for (ApplicationInfo a : removedApps) {
2586 ArrayList<ItemInfo> infos =
2587 getItemInfoForComponentName(a.componentName);
2588 for (ItemInfo i : infos) {
2589 deleteItemFromDatabase(context, i);
2590 }
2591 }
2592 }
2593
Joe Onorato36115782010-06-17 13:28:48 -04002594 mHandler.post(new Runnable() {
2595 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002596 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2597 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002598 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002599 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002600 }
2601 }
2602 });
Joe Onoratobe386092009-11-17 17:32:16 -08002603 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002604
Michael Jurkac402cd92013-05-20 15:49:32 +02002605 final ArrayList<Object> widgetsAndShortcuts =
2606 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002607 mHandler.post(new Runnable() {
2608 @Override
2609 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002610 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2611 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002612 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002613 }
2614 }
2615 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 }
2617 }
2618
Michael Jurkac402cd92013-05-20 15:49:32 +02002619 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2620 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2621 PackageManager packageManager = context.getPackageManager();
2622 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2623 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2624 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2625 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2626 Collections.sort(widgetsAndShortcuts,
2627 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2628 return widgetsAndShortcuts;
2629 }
2630
Winson Chung1323b482013-08-05 12:41:55 -07002631 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002632 if (cn == null) {
2633 return false;
2634 }
2635
2636 try {
2637 return (pm.getActivityInfo(cn, 0) != null);
2638 } catch (NameNotFoundException e) {
2639 return false;
2640 }
2641 }
2642
Joe Onorato9c1289c2009-08-17 11:03:03 -04002643 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002644 * This is called from the code that adds shortcuts from the intent receiver. This
2645 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002646 */
Joe Onorato56d82912010-03-07 14:32:10 -05002647 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002648 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650
Joe Onorato56d82912010-03-07 14:32:10 -05002651 /**
2652 * Make an ShortcutInfo object for a shortcut that is an application.
2653 *
2654 * If c is not null, then it will be used to fill in missing data like the title and icon.
2655 */
2656 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002657 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002658 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002659 final ShortcutInfo info = new ShortcutInfo();
2660 if (!isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002661 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002662 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002663 } else {
2664 try {
2665 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2666 info.initFlagsAndFirstInstallTime(pi);
2667 } catch (NameNotFoundException e) {
2668 Log.d(TAG, "getPackInfo failed for package " +
2669 componentName.getPackageName());
2670 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002671 }
2672
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002673 // TODO: See if the PackageManager knows about this case. If it doesn't
2674 // then return null & delete this.
2675
Joe Onorato56d82912010-03-07 14:32:10 -05002676 // the resource -- This may implicitly give us back the fallback icon,
2677 // but don't worry about that. All we're doing with usingFallbackIcon is
2678 // to avoid saving lots of copies of that in the database, and most apps
2679 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002680
2681 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2682 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2683 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002684 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002685 ResolveInfo resolveInfo = null;
2686 ComponentName oldComponent = intent.getComponent();
2687 Intent newIntent = new Intent(intent.getAction(), null);
2688 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2689 newIntent.setPackage(oldComponent.getPackageName());
2690 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2691 for (ResolveInfo i : infos) {
2692 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2693 i.activityInfo.name);
2694 if (cn.equals(oldComponent)) {
2695 resolveInfo = i;
2696 }
2697 }
2698 if (resolveInfo == null) {
2699 resolveInfo = manager.resolveActivity(intent, 0);
2700 }
Joe Onorato56d82912010-03-07 14:32:10 -05002701 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002702 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002703 }
Joe Onorato56d82912010-03-07 14:32:10 -05002704 // the db
2705 if (icon == null) {
2706 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002707 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002708 }
2709 }
2710 // the fallback icon
2711 if (icon == null) {
2712 icon = getFallbackIcon();
2713 info.usingFallbackIcon = true;
2714 }
2715 info.setIcon(icon);
2716
2717 // from the resource
2718 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002719 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2720 if (labelCache != null && labelCache.containsKey(key)) {
2721 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002722 } else {
2723 info.title = resolveInfo.activityInfo.loadLabel(manager);
2724 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002725 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002726 }
2727 }
Joe Onorato56d82912010-03-07 14:32:10 -05002728 }
2729 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002731 if (c != null) {
2732 info.title = c.getString(titleIndex);
2733 }
2734 }
2735 // fall back to the class name of the activity
2736 if (info.title == null) {
2737 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002739 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2740 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002742
Winson Chung64359a52013-07-08 17:17:08 -07002743 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2744 ItemInfoFilter f) {
2745 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2746 for (ItemInfo i : infos) {
2747 if (i instanceof ShortcutInfo) {
2748 ShortcutInfo info = (ShortcutInfo) i;
2749 ComponentName cn = info.intent.getComponent();
2750 if (cn != null && f.filterItem(null, info, cn)) {
2751 filtered.add(info);
2752 }
2753 } else if (i instanceof FolderInfo) {
2754 FolderInfo info = (FolderInfo) i;
2755 for (ShortcutInfo s : info.contents) {
2756 ComponentName cn = s.intent.getComponent();
2757 if (cn != null && f.filterItem(info, s, cn)) {
2758 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002759 }
2760 }
Winson Chung64359a52013-07-08 17:17:08 -07002761 } else if (i instanceof LauncherAppWidgetInfo) {
2762 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2763 ComponentName cn = info.providerName;
2764 if (cn != null && f.filterItem(null, info, cn)) {
2765 filtered.add(info);
2766 }
Winson Chung8a435102012-08-30 17:16:53 -07002767 }
2768 }
Winson Chung64359a52013-07-08 17:17:08 -07002769 return new ArrayList<ItemInfo>(filtered);
2770 }
2771
2772 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
2773 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2774 ItemInfoFilter filter = new ItemInfoFilter() {
2775 @Override
2776 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2777 return cn.getPackageName().equals(pn);
2778 }
2779 };
2780 return filterItemInfos(sBgItemsIdMap.values(), filter);
2781 }
2782
2783 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
2784 HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
2785 ItemInfoFilter filter = new ItemInfoFilter() {
2786 @Override
2787 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2788 return cn.equals(cname);
2789 }
2790 };
2791 return filterItemInfos(sBgItemsIdMap.values(), filter);
2792 }
2793
2794 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2795 if (i instanceof ShortcutInfo) {
2796 ShortcutInfo info = (ShortcutInfo) i;
2797 // We need to check for ACTION_MAIN otherwise getComponent() might
2798 // return null for some shortcuts (for instance, for shortcuts to
2799 // web pages.)
2800 Intent intent = info.intent;
2801 ComponentName name = intent.getComponent();
2802 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2803 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2804 return true;
2805 }
2806 }
2807 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002808 }
2809
2810 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002811 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002813 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002814 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2815 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816
Joe Onorato56d82912010-03-07 14:32:10 -05002817 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002818 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002819 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002821 // TODO: If there's an explicit component and we can't install that, delete it.
2822
Joe Onorato56d82912010-03-07 14:32:10 -05002823 info.title = c.getString(titleIndex);
2824
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 int iconType = c.getInt(iconTypeIndex);
2826 switch (iconType) {
2827 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2828 String packageName = c.getString(iconPackageIndex);
2829 String resourceName = c.getString(iconResourceIndex);
2830 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002831 info.customIcon = false;
2832 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002835 if (resources != null) {
2836 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002837 icon = Utilities.createIconBitmap(
2838 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002841 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002842 }
Joe Onorato56d82912010-03-07 14:32:10 -05002843 // the db
2844 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002845 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002846 }
2847 // the fallback icon
2848 if (icon == null) {
2849 icon = getFallbackIcon();
2850 info.usingFallbackIcon = true;
2851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 break;
2853 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002854 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002855 if (icon == null) {
2856 icon = getFallbackIcon();
2857 info.customIcon = false;
2858 info.usingFallbackIcon = true;
2859 } else {
2860 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 break;
2863 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002864 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002865 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 info.customIcon = false;
2867 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002869 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 return info;
2871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872
Michael Jurka931dc972011-08-05 15:08:15 -07002873 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002874 @SuppressWarnings("all") // suppress dead code warning
2875 final boolean debug = false;
2876 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002877 Log.d(TAG, "getIconFromCursor app="
2878 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2879 }
2880 byte[] data = c.getBlob(iconIndex);
2881 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002882 return Utilities.createIconBitmap(
2883 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002884 } catch (Exception e) {
2885 return null;
2886 }
2887 }
2888
Winson Chung3d503fb2011-07-13 17:25:49 -07002889 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2890 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002891 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002892 if (info == null) {
2893 return null;
2894 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002895 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002896
2897 return info;
2898 }
2899
Winson Chunga9abd0e2010-10-27 17:18:37 -07002900 /**
Winson Chung55cef262010-10-28 14:14:18 -07002901 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2902 */
2903 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2904 ComponentName component) {
2905 List<AppWidgetProviderInfo> widgets =
2906 AppWidgetManager.getInstance(context).getInstalledProviders();
2907 for (AppWidgetProviderInfo info : widgets) {
2908 if (info.provider.equals(component)) {
2909 return info;
2910 }
2911 }
2912 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002913 }
2914
Winson Chung68846fd2010-10-29 11:00:27 -07002915 /**
2916 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2917 */
2918 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2919 final PackageManager packageManager = context.getPackageManager();
2920 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2921 new ArrayList<WidgetMimeTypeHandlerData>();
2922
2923 final Intent supportsIntent =
2924 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2925 supportsIntent.setType(mimeType);
2926
2927 // Create a set of widget configuration components that we can test against
2928 final List<AppWidgetProviderInfo> widgets =
2929 AppWidgetManager.getInstance(context).getInstalledProviders();
2930 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2931 new HashMap<ComponentName, AppWidgetProviderInfo>();
2932 for (AppWidgetProviderInfo info : widgets) {
2933 configurationComponentToWidget.put(info.configure, info);
2934 }
2935
2936 // Run through each of the intents that can handle this type of clip data, and cross
2937 // reference them with the components that are actual configuration components
2938 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2939 PackageManager.MATCH_DEFAULT_ONLY);
2940 for (ResolveInfo info : activities) {
2941 final ActivityInfo activityInfo = info.activityInfo;
2942 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2943 activityInfo.name);
2944 if (configurationComponentToWidget.containsKey(infoComponent)) {
2945 supportedConfigurationActivities.add(
2946 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2947 configurationComponentToWidget.get(infoComponent)));
2948 }
2949 }
2950 return supportedConfigurationActivities;
2951 }
2952
Winson Chunga9abd0e2010-10-27 17:18:37 -07002953 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002954 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2955 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2956 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2957
Adam Cohend9198822011-11-22 16:42:47 -08002958 if (intent == null) {
2959 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2960 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2961 return null;
2962 }
2963
Joe Onorato0589f0f2010-02-08 13:44:00 -08002964 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002965 boolean customIcon = false;
2966 ShortcutIconResource iconResource = null;
2967
2968 if (bitmap != null && bitmap instanceof Bitmap) {
2969 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002970 customIcon = true;
2971 } else {
2972 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2973 if (extra != null && extra instanceof ShortcutIconResource) {
2974 try {
2975 iconResource = (ShortcutIconResource) extra;
2976 final PackageManager packageManager = context.getPackageManager();
2977 Resources resources = packageManager.getResourcesForApplication(
2978 iconResource.packageName);
2979 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002980 icon = Utilities.createIconBitmap(
2981 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002982 } catch (Exception e) {
2983 Log.w(TAG, "Could not load shortcut icon: " + extra);
2984 }
2985 }
2986 }
2987
Michael Jurkac9d95c52011-08-29 14:03:34 -07002988 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002989
2990 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002991 if (fallbackIcon != null) {
2992 icon = fallbackIcon;
2993 } else {
2994 icon = getFallbackIcon();
2995 info.usingFallbackIcon = true;
2996 }
Joe Onorato56d82912010-03-07 14:32:10 -05002997 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002998 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002999
Joe Onorato0589f0f2010-02-08 13:44:00 -08003000 info.title = name;
3001 info.intent = intent;
3002 info.customIcon = customIcon;
3003 info.iconResource = iconResource;
3004
3005 return info;
3006 }
3007
Winson Chungaac01e12011-08-17 10:37:13 -07003008 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3009 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003010 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003011 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003012 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003013 }
Joe Onorato56d82912010-03-07 14:32:10 -05003014 // If this icon doesn't have a custom icon, check to see
3015 // what's stored in the DB, and if it doesn't match what
3016 // we're going to show, store what we are going to show back
3017 // into the DB. We do this so when we're loading, if the
3018 // package manager can't find an icon (for example because
3019 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003020 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003021 cache.put(info, c.getBlob(iconIndex));
3022 return true;
3023 }
3024 return false;
3025 }
3026 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3027 boolean needSave = false;
3028 try {
3029 if (data != null) {
3030 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3031 Bitmap loaded = info.getIcon(mIconCache);
3032 needSave = !saved.sameAs(loaded);
3033 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003034 needSave = true;
3035 }
Winson Chungaac01e12011-08-17 10:37:13 -07003036 } catch (Exception e) {
3037 needSave = true;
3038 }
3039 if (needSave) {
3040 Log.d(TAG, "going to save icon bitmap for info=" + info);
3041 // This is slower than is ideal, but this only happens once
3042 // or when the app is updated with a new icon.
3043 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003044 }
3045 }
3046
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003048 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 * or make a new one.
3050 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003051 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 // See if a placeholder was created for us already
3053 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003054 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003056 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 folders.put(id, folderInfo);
3058 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003059 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 }
3061
Winson Chung11904872012-09-17 16:58:46 -07003062 public static final Comparator<ApplicationInfo> getAppNameComparator() {
3063 final Collator collator = Collator.getInstance();
3064 return new Comparator<ApplicationInfo>() {
3065 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3066 int result = collator.compare(a.title.toString(), b.title.toString());
3067 if (result == 0) {
3068 result = a.componentName.compareTo(b.componentName);
3069 }
3070 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003071 }
Winson Chung11904872012-09-17 16:58:46 -07003072 };
3073 }
Winson Chung78403fe2011-01-21 15:38:02 -08003074 public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR
3075 = new Comparator<ApplicationInfo>() {
3076 public final int compare(ApplicationInfo a, ApplicationInfo b) {
3077 if (a.firstInstallTime < b.firstInstallTime) return 1;
3078 if (a.firstInstallTime > b.firstInstallTime) return -1;
3079 return 0;
3080 }
3081 };
Winson Chung11904872012-09-17 16:58:46 -07003082 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3083 final Collator collator = Collator.getInstance();
3084 return new Comparator<AppWidgetProviderInfo>() {
3085 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3086 return collator.compare(a.label.toString(), b.label.toString());
3087 }
3088 };
3089 }
Winson Chung5308f242011-08-18 12:12:41 -07003090 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3091 if (info.activityInfo != null) {
3092 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3093 } else {
3094 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3095 }
3096 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003097 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003098 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003099 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003100 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003101 ShortcutNameComparator(PackageManager pm) {
3102 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003103 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003104 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003105 }
3106 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3107 mPackageManager = pm;
3108 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003109 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003110 }
3111 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003112 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003113 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3114 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3115 if (mLabelCache.containsKey(keyA)) {
3116 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003117 } else {
3118 labelA = a.loadLabel(mPackageManager).toString();
3119
Winson Chung5308f242011-08-18 12:12:41 -07003120 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003121 }
Winson Chung5308f242011-08-18 12:12:41 -07003122 if (mLabelCache.containsKey(keyB)) {
3123 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003124 } else {
3125 labelB = b.loadLabel(mPackageManager).toString();
3126
Winson Chung5308f242011-08-18 12:12:41 -07003127 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003128 }
Winson Chung11904872012-09-17 16:58:46 -07003129 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003130 }
3131 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003132 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003133 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003134 private PackageManager mPackageManager;
3135 private HashMap<Object, String> mLabelCache;
3136 WidgetAndShortcutNameComparator(PackageManager pm) {
3137 mPackageManager = pm;
3138 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003139 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003140 }
3141 public final int compare(Object a, Object b) {
3142 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003143 if (mLabelCache.containsKey(a)) {
3144 labelA = mLabelCache.get(a);
3145 } else {
3146 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003147 ((AppWidgetProviderInfo) a).label :
3148 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 mLabelCache.put(a, labelA);
3150 }
3151 if (mLabelCache.containsKey(b)) {
3152 labelB = mLabelCache.get(b);
3153 } else {
3154 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003155 ((AppWidgetProviderInfo) b).label :
3156 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003157 mLabelCache.put(b, labelB);
3158 }
Winson Chung11904872012-09-17 16:58:46 -07003159 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003160 }
3161 };
Joe Onoratobe386092009-11-17 17:32:16 -08003162
3163 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003164 Log.d(TAG, "mCallbacks=" + mCallbacks);
Adam Cohen487f7dd2012-06-28 18:12:10 -07003165 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3166 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3167 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3168 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003169 if (mLoaderTask != null) {
3170 mLoaderTask.dumpState();
3171 } else {
3172 Log.d(TAG, "mLoaderTask=null");
3173 }
Joe Onoratobe386092009-11-17 17:32:16 -08003174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175}