blob: bc0d1bcc1da2bbb07c14116858cd94bc43d6dc26 [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;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070044import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080046
Michael Jurkac2f801e2011-07-12 14:19:46 -070047import java.lang.ref.WeakReference;
48import java.net.URISyntaxException;
49import java.text.Collator;
50import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070051import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070052import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070056import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070057import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070058import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070059import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070060import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * Maintains in-memory state of the Launcher. It is expected that there should be only one
64 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070065 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040067public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080068 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070070
Daniel Sandler8707e0f2013-08-15 15:54:18 -070071 // true = use a "More Apps" folder for non-workspace apps on upgrade
72 // false = strew non-workspace apps across the workspace on upgrade
73 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
74
Joe Onorato36115782010-06-17 13:28:48 -040075 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070076 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040077
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 private final Object mLock = new Object();
80 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040081 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020083 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Winson Chung81b52252012-08-27 15:34:29 -070085 // Specific runnable types that are run on the main thread deferred handler, this allows us to
86 // clear all queued binding runnables when the Launcher activity is destroyed.
87 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
88 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
89
90
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070091 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
92 static {
93 sWorkerThread.start();
94 }
95 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
96
Joe Onoratocc67f472010-06-08 10:54:30 -070097 // We start off with everything not loaded. After that, we assume that
98 // our monitoring of the package manager provides all updates and we never
99 // need to do a requery. These are only ever touched from the loader thread.
100 private boolean mWorkspaceLoaded;
101 private boolean mAllAppsLoaded;
102
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700103 // When we are loading pages synchronously, we can't just post the binding of items on the side
104 // pages as this delays the rotation process. Instead, we wait for a callback from the first
105 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
106 // a normal load, we also clear this set of Runnables.
107 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700111 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700112 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700114 // The lock that must be acquired before referencing any static bg data structures. Unlike
115 // other locks, this one can generally be held long-term because we never expect any of these
116 // static data structures to be referenced outside of the worker thread except on the first
117 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700118 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700119
Adam Cohen487f7dd2012-06-28 18:12:10 -0700120 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
125 // created by LauncherModel that are directly on the home screen (however, no widgets or
126 // shortcuts within folders).
127 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700128
Adam Cohen487f7dd2012-06-28 18:12:10 -0700129 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
130 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 new ArrayList<LauncherAppWidgetInfo>();
132
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
134 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
137 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700138
139 // sBgWorkspaceScreens is the ordered set of workspace screens.
140 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // </ only access in worker thread >
143
144 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Reena Lee99a73f32011-10-24 17:27:37 -0700147 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700148
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700150 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public int getCurrentWorkspaceScreen();
152 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700153 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
154 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700155 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700156 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500157 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700158 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public void bindAppWidget(LauncherAppWidgetInfo info);
Bjorn Bringert85f418d2013-09-06 12:50:05 +0100160 public boolean shouldShowApp(ResolveInfo app);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200161 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700162 public void bindAppsAdded(ArrayList<Long> newScreens,
163 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700164 ArrayList<ItemInfo> addAnimated,
165 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200166 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700167 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200168 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700169 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700172 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700173 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Winson Chung64359a52013-07-08 17:17:08 -0700176 public interface ItemInfoFilter {
177 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
178 }
179
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 LauncherModel(LauncherAppState app, IconCache iconCache) {
181 final Context context = app.getContext();
182
Winson Chungee055712013-07-30 14:46:24 -0700183 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700185 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800186 mIconCache = iconCache;
187
188 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400190
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700192 Configuration config = res.getConfiguration();
193 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 }
195
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700196 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
197 * posted on the main thread handler. */
198 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700199 runOnMainThread(r, 0);
200 }
201 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700202 if (sWorkerThread.getThreadId() == Process.myTid()) {
203 // If we are on the worker thread, post onto the main handler
204 mHandler.post(r);
205 } else {
206 r.run();
207 }
208 }
209
210 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
211 * posted on the worker thread handler. */
212 private static void runOnWorkerThread(Runnable r) {
213 if (sWorkerThread.getThreadId() == Process.myTid()) {
214 r.run();
215 } else {
216 // If we are not on the worker thread, then post to the worker handler
217 sWorker.post(r);
218 }
219 }
220
Winson Chungc9168342013-06-26 14:54:55 -0700221 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
222 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700223 LauncherAppState app = LauncherAppState.getInstance();
224 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
225 final int xCount = (int) grid.numColumns;
226 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700227 boolean[][] occupied = new boolean[xCount][yCount];
228
229 int cellX, cellY, spanX, spanY;
230 for (int i = 0; i < items.size(); ++i) {
231 final ItemInfo item = items.get(i);
232 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
233 if (item.screenId == screen) {
234 cellX = item.cellX;
235 cellY = item.cellY;
236 spanX = item.spanX;
237 spanY = item.spanY;
238 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
239 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
240 occupied[x][y] = true;
241 }
242 }
243 }
244 }
245 }
246
247 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
248 }
249 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700250 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700251 int firstScreenIndex,
252 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700253 // Lock on the app so that we don't try and get the items while apps are being added
254 LauncherAppState app = LauncherAppState.getInstance();
255 LauncherModel model = app.getModel();
256 boolean found = false;
257 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700258 if (sWorkerThread.getThreadId() != Process.myTid()) {
259 // Flush the LauncherModel worker thread, so that if we just did another
260 // processInstallShortcut, we give it time for its shortcut to get added to the
261 // database (getItemsInLocalCoordinates reads the database)
262 model.flushWorkerThread();
263 }
Winson Chungc9168342013-06-26 14:54:55 -0700264 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700265
266 // Try adding to the workspace screens incrementally, starting at the default or center
267 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700268 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
269 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700270 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 int[] tmpCoordinates = new int[2];
272 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700273 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700274 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700275 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700276 }
277 }
278 }
Winson Chungc9168342013-06-26 14:54:55 -0700279 return null;
280 }
281
Winson Chungc58497e2013-09-03 17:48:37 -0700282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
283 final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700284 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -0700285 addAndBindAddedApps(context, added, cb, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700286 }
287 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chungc58497e2013-09-03 17:48:37 -0700288 final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700289 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700290 return;
Winson Chung997a9232013-07-24 15:33:46 -0700291 }
Winson Chung64359a52013-07-08 17:17:08 -0700292 // Process the newly added applications and add them to the database first
293 Runnable r = new Runnable() {
294 public void run() {
295 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
296 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
297
Winson Chung76828c82013-08-19 15:43:29 -0700298 // Get the list of workspace screens. We need to append to this list and
299 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
300 // called.
301 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
302 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
303 for (Integer i : orderedScreens.keySet()) {
304 long screenId = orderedScreens.get(i);
305 workspaceScreens.add(screenId);
306 }
307
Winson Chung64359a52013-07-08 17:17:08 -0700308 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700309 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700310 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700311 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700312 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700313 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700314
315 // Short-circuit this logic if the icon exists somewhere on the workspace
316 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
317 continue;
318 }
319
320 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700321 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700322 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700323 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700324 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200325 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700326
327 // If we can't find a valid position, then just add a new screen.
328 // This takes time so we need to re-queue the add until the new
329 // page is added. Create as many screens as necessary to satisfy
330 // the startSearchPageIndex.
331 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700332 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700333 while (numPagesToAdd > 0) {
334 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700336 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 addedWorkspaceScreensFinal.add(screenId);
338 numPagesToAdd--;
339 }
Winson Chung76828c82013-08-19 15:43:29 -0700340
Winson Chung64359a52013-07-08 17:17:08 -0700341 // Find the coordinate again
342 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700343 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700344 }
345 if (coords == null) {
346 throw new RuntimeException("Coordinates should not be null");
347 }
348
Winson Chung997a9232013-07-24 15:33:46 -0700349 ShortcutInfo shortcutInfo;
350 if (a instanceof ShortcutInfo) {
351 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200352 } else if (a instanceof AppInfo) {
353 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700354 } else {
355 throw new RuntimeException("Unexpected info type");
356 }
Winson Chung64359a52013-07-08 17:17:08 -0700357 // Add the shortcut to the db
358 addItemToDatabase(context, shortcutInfo,
359 LauncherSettings.Favorites.CONTAINER_DESKTOP,
360 coords.first, coords.second[0], coords.second[1], false);
361 // Save the ShortcutInfo for binding in the workspace
362 addedShortcutsFinal.add(shortcutInfo);
363 }
364 }
365
Winson Chung76828c82013-08-19 15:43:29 -0700366 // Update the workspace screens
367 updateWorkspaceScreenOrder(context, workspaceScreens);
368
Winson Chung997a9232013-07-24 15:33:46 -0700369 if (!addedShortcutsFinal.isEmpty()) {
370 runOnMainThread(new Runnable() {
371 public void run() {
372 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
373 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
375 long lastScreenId = info.screenId;
376 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
377 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
378 for (ItemInfo i : addedShortcutsFinal) {
379 if (i.screenId == lastScreenId) {
380 addAnimated.add(i);
381 } else {
382 addNotAnimated.add(i);
383 }
384 }
Winson Chungd64d1762013-08-20 14:37:16 -0700385 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chungc58497e2013-09-03 17:48:37 -0700386 addNotAnimated, addAnimated, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700387 }
Winson Chung64359a52013-07-08 17:17:08 -0700388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 });
390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 }
392 };
393 runOnWorkerThread(r);
394 }
395
Joe Onorato56d82912010-03-07 14:32:10 -0500396 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
Winson Chung81b52252012-08-27 15:34:29 -0700400 public void unbindItemInfosAndClearQueuedBindRunnables() {
401 if (sWorkerThread.getThreadId() == Process.myTid()) {
402 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
403 "main thread");
404 }
405
406 // Clear any deferred bind runnables
407 mDeferredBindRunnables.clear();
408 // Remove any queued bind runnables
409 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
410 // Unbind all the workspace items
411 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700412 }
413
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700414 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700415 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700416 // Ensure that we don't use the same workspace items data structure on the main thread
417 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
419 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700420 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
422 tmpAppWidgets.addAll(sBgAppWidgets);
423 }
424 Runnable r = new Runnable() {
425 @Override
426 public void run() {
427 for (ItemInfo item : tmpWorkspaceItems) {
428 item.unbind();
429 }
430 for (ItemInfo item : tmpAppWidgets) {
431 item.unbind();
432 }
433 }
434 };
435 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700436 }
437
Joe Onorato9c1289c2009-08-17 11:03:03 -0400438 /**
439 * Adds an item to the DB if it was not created previously, or move it to a new
440 * <container, screen, cellX, cellY>
441 */
442 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700443 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 if (item.container == ItemInfo.NO_ID) {
445 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 } else {
448 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451 }
452
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700453 static void checkItemInfoLocked(
454 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
455 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
456 if (modelItem != null && item != modelItem) {
457 // check all the data is consistent
458 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
459 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
460 ShortcutInfo shortcut = (ShortcutInfo) item;
461 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
462 modelShortcut.intent.filterEquals(shortcut.intent) &&
463 modelShortcut.id == shortcut.id &&
464 modelShortcut.itemType == shortcut.itemType &&
465 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700466 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700467 modelShortcut.cellX == shortcut.cellX &&
468 modelShortcut.cellY == shortcut.cellY &&
469 modelShortcut.spanX == shortcut.spanX &&
470 modelShortcut.spanY == shortcut.spanY &&
471 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
472 (modelShortcut.dropPos != null &&
473 shortcut.dropPos != null &&
474 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
475 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
476 // For all intents and purposes, this is the same object
477 return;
478 }
479 }
480
481 // the modelItem needs to match up perfectly with item if our model is
482 // to be consistent with the database-- for now, just require
483 // modelItem == item or the equality check above
484 String msg = "item: " + ((item != null) ? item.toString() : "null") +
485 "modelItem: " +
486 ((modelItem != null) ? modelItem.toString() : "null") +
487 "Error: ItemInfo passed to checkItemInfo doesn't match original";
488 RuntimeException e = new RuntimeException(msg);
489 if (stackTrace != null) {
490 e.setStackTrace(stackTrace);
491 }
Adam Cohene25af792013-06-06 23:08:25 -0700492 // TODO: something breaks this in the upgrade path
493 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700494 }
495 }
496
Michael Jurka816474f2012-06-25 14:49:02 -0700497 static void checkItemInfo(final ItemInfo item) {
498 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
499 final long itemId = item.id;
500 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700501 public void run() {
502 synchronized (sBgLock) {
503 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700504 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 }
506 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700507 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700508 }
509
Michael Jurkac9d95c52011-08-29 14:03:34 -0700510 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
511 final ItemInfo item, final String callingFunction) {
512 final long itemId = item.id;
513 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
514 final ContentResolver cr = context.getContentResolver();
515
Adam Cohen487f7dd2012-06-28 18:12:10 -0700516 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700517 Runnable r = new Runnable() {
518 public void run() {
519 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700520 updateItemArrays(item, itemId, stackTrace);
521 }
522 };
523 runOnWorkerThread(r);
524 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700526 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
527 final ArrayList<ItemInfo> items, final String callingFunction) {
528 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700529
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
531 Runnable r = new Runnable() {
532 public void run() {
533 ArrayList<ContentProviderOperation> ops =
534 new ArrayList<ContentProviderOperation>();
535 int count = items.size();
536 for (int i = 0; i < count; i++) {
537 ItemInfo item = items.get(i);
538 final long itemId = item.id;
539 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
540 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700541
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700542 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
543 updateItemArrays(item, itemId, stackTrace);
544
545 }
546 try {
547 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
548 } catch (Exception e) {
549 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700550 }
551 }
552 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700555
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700556 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
557 // Lock on mBgLock *after* the db operation
558 synchronized (sBgLock) {
559 checkItemInfoLocked(itemId, item, stackTrace);
560
561 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
562 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
563 // Item is in a folder, make sure this folder exists
564 if (!sBgFolders.containsKey(item.container)) {
565 // An items container is being set to a that of an item which is not in
566 // the list of Folders.
567 String msg = "item: " + item + " container being set to: " +
568 item.container + ", not in the list of folders";
569 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700570 }
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) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 item.cellX = cellX;
632 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700633
Winson Chung3d503fb2011-07-13 17:25:49 -0700634 // We store hotseat items in canonical form which is this orientation invariant position
635 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700637 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700639 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642
643 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
646 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
Michael Jurkac9d95c52011-08-29 14:03:34 -0700649 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700650 }
651
652 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700653 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
654 * cellX, cellY have already been updated on the ItemInfos.
655 */
656 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
657 final long container, final int screen) {
658
659 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
660 int count = items.size();
661
662 for (int i = 0; i < count; i++) {
663 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664 item.container = container;
665
666 // We store hotseat items in canonical form which is this orientation invariant position
667 // in the hotseat
668 if (context instanceof Launcher && screen < 0 &&
669 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700670 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700671 item.cellY);
672 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700673 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 }
675
676 final ContentValues values = new ContentValues();
677 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
678 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
679 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681
682 contentValues.add(values);
683 }
684 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
685 }
686
687 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700688 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800689 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700690 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Adam Cohend4844c32011-02-18 19:25:06 -0800692 item.cellX = cellX;
693 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700694 item.spanX = spanX;
695 item.spanY = spanY;
696
697 // We store hotseat items in canonical form which is this orientation invariant position
698 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700704 }
Adam Cohend4844c32011-02-18 19:25:06 -0800705
Adam Cohend4844c32011-02-18 19:25:06 -0800706 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800707 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
709 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
710 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
711 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800713
Michael Jurka816474f2012-06-25 14:49:02 -0700714 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700716
717 /**
718 * Update an item to the database in a specified container.
719 */
720 static void updateItemInDatabase(Context context, final ItemInfo item) {
721 final ContentValues values = new ContentValues();
722 item.onAddToDatabase(values);
723 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
724 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800725 }
726
727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 * Returns true if the shortcuts already exists in the database.
729 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 static boolean shortcutExists(Context context, String title, Intent intent) {
732 final ContentResolver cr = context.getContentResolver();
733 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
734 new String[] { "title", "intent" }, "title=? and intent=?",
735 new String[] { title, intent.toUri(0) }, null);
736 boolean result = false;
737 try {
738 result = c.moveToFirst();
739 } finally {
740 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700743 }
744
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700746 * Returns an ItemInfo array containing all the items in the LauncherModel.
747 * The ItemInfo.id is not set through this function.
748 */
749 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
750 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
751 final ContentResolver cr = context.getContentResolver();
752 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
753 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
754 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
755 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
756
757 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
758 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
759 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
760 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
761 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
762 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
763 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
764
765 try {
766 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700767 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700768 item.cellX = c.getInt(cellXIndex);
769 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700770 item.spanX = Math.max(1, c.getInt(spanXIndex));
771 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700772 item.container = c.getInt(containerIndex);
773 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700774 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700775
776 items.add(item);
777 }
778 } catch (Exception e) {
779 items.clear();
780 } finally {
781 c.close();
782 }
783
784 return items;
785 }
786
787 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
789 */
790 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
791 final ContentResolver cr = context.getContentResolver();
792 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
793 "_id=? and (itemType=? or itemType=?)",
794 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700795 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700796
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 try {
798 if (c.moveToFirst()) {
799 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
800 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
801 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
802 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
803 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
804 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806 FolderInfo folderInfo = null;
807 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700808 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
809 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700811 }
812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 folderInfo.title = c.getString(titleIndex);
814 folderInfo.id = id;
815 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700816 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700817 folderInfo.cellX = c.getInt(cellXIndex);
818 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819
820 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 } finally {
823 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700824 }
825
826 return null;
827 }
828
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 /**
830 * Add an item to the database in a specified container. Sets the container, screen, cellX and
831 * cellY fields of the item. Also assigns an ID to the item.
832 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700833 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 item.cellX = cellX;
837 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700838 // We store hotseat items in canonical form which is this orientation invariant position
839 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700841 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846
847 final ContentValues values = new ContentValues();
848 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 item.onAddToDatabase(values);
850
Michael Jurka414300a2013-08-27 15:42:35 +0200851 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700852 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700854
Michael Jurkac9d95c52011-08-29 14:03:34 -0700855 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700856 public void run() {
857 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
858 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700860 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700861 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700862 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700863 sBgItemsIdMap.put(item.id, item);
864 switch (item.itemType) {
865 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
866 sBgFolders.put(item.id, (FolderInfo) item);
867 // Fall through
868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
869 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
870 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
871 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
872 sBgWorkspaceItems.add(item);
873 } else {
874 if (!sBgFolders.containsKey(item.container)) {
875 // Adding an item to a folder that doesn't exist.
876 String msg = "adding item: " + item + " to a folder that " +
877 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700878 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700879 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700880 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700881 break;
882 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
883 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
884 break;
885 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700886 }
887 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700888 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700890 }
891
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700893 * Creates a new unique child id, for a given cell span across all layouts.
894 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700895 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700896 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700897 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700898 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700899 }
900
Winson Chungaafa03c2010-06-11 17:34:16 -0700901 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700902 * Removes the specified item from the database
903 * @param context
904 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700906 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700908 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700909
Michael Jurka83df1882011-08-31 20:59:26 -0700910 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913
914 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700915 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916 switch (item.itemType) {
917 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
918 sBgFolders.remove(item.id);
919 for (ItemInfo info: sBgItemsIdMap.values()) {
920 if (info.container == item.id) {
921 // We are deleting a folder which still contains items that
922 // think they are contained by that folder.
923 String msg = "deleting a folder (" + item + ") which still " +
924 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700925 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700926 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700927 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 sBgWorkspaceItems.remove(item);
929 break;
930 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
931 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
932 sBgWorkspaceItems.remove(item);
933 break;
934 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
935 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
936 break;
937 }
938 sBgItemsIdMap.remove(item.id);
939 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700940 }
941 }
Michael Jurka83df1882011-08-31 20:59:26 -0700942 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 }
945
946 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 * Update the order of the workspace screens in the database. The array list contains
948 * a list of screen ids in the order that they should appear.
949 */
Winson Chungc9168342013-06-26 14:54:55 -0700950 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700951 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 final ContentResolver cr = context.getContentResolver();
953 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
954
955 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700956 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 while (iter.hasNext()) {
958 long id = iter.next();
959 if (id < 0) {
960 iter.remove();
961 }
962 }
963
964 Runnable r = new Runnable() {
965 @Override
966 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 // Clear the table
968 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700969 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700970 ContentValues[] values = new ContentValues[count];
971 for (int i = 0; i < count; i++) {
972 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700973 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
975 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 values[i] = v;
977 }
978 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700979
Winson Chungba9c37f2013-08-30 14:11:37 -0700980 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700981 sBgWorkspaceScreens.clear();
982 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700983 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 }
985 };
986 runOnWorkerThread(r);
987 }
988
989 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 * Remove the contents of the specified folder from the database
991 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700992 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 final ContentResolver cr = context.getContentResolver();
994
Michael Jurkac9d95c52011-08-29 14:03:34 -0700995 Runnable r = new Runnable() {
996 public void run() {
997 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700998 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700999 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001000 sBgItemsIdMap.remove(info.id);
1001 sBgFolders.remove(info.id);
1002 sBgDbIconCache.remove(info);
1003 sBgWorkspaceItems.remove(info);
1004 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001005
Michael Jurkac9d95c52011-08-29 14:03:34 -07001006 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1007 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001009 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 for (ItemInfo childInfo : info.contents) {
1011 sBgItemsIdMap.remove(childInfo.id);
1012 sBgDbIconCache.remove(childInfo);
1013 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001014 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001015 }
1016 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 }
1019
1020 /**
1021 * Set this as the current Launcher activity object for the loader.
1022 */
1023 public void initialize(Callbacks callbacks) {
1024 synchronized (mLock) {
1025 mCallbacks = new WeakReference<Callbacks>(callbacks);
1026 }
1027 }
1028
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001029 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1031 * ACTION_PACKAGE_CHANGED.
1032 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001033 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001034 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001035 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001036
Joe Onorato36115782010-06-17 13:28:48 -04001037 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001038
Joe Onorato36115782010-06-17 13:28:48 -04001039 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1040 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1041 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1042 final String packageName = intent.getData().getSchemeSpecificPart();
1043 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044
Joe Onorato36115782010-06-17 13:28:48 -04001045 int op = PackageUpdatedTask.OP_NONE;
1046
1047 if (packageName == null || packageName.length() == 0) {
1048 // they sent us a bad intent
1049 return;
1050 }
1051
1052 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1053 op = PackageUpdatedTask.OP_UPDATE;
1054 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1055 if (!replacing) {
1056 op = PackageUpdatedTask.OP_REMOVE;
1057 }
1058 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1059 // later, we will update the package at this time
1060 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1061 if (!replacing) {
1062 op = PackageUpdatedTask.OP_ADD;
1063 } else {
1064 op = PackageUpdatedTask.OP_UPDATE;
1065 }
1066 }
1067
1068 if (op != PackageUpdatedTask.OP_NONE) {
1069 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1070 }
1071
1072 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001073 // First, schedule to add these apps back in.
1074 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1075 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1076 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001077 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001078 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1079 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1080 enqueuePackageUpdated(new PackageUpdatedTask(
1081 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001082 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001083 // If we have changed locale we need to clear out the labels in all apps/workspace.
1084 forceReload();
1085 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1086 // Check if configuration change was an mcc/mnc change which would affect app resources
1087 // and we would need to clear out the labels in all apps/workspace. Same handling as
1088 // above for ACTION_LOCALE_CHANGED
1089 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001090 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001091 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001092 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001093 forceReload();
1094 }
1095 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001096 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001097 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1098 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001099 if (mCallbacks != null) {
1100 Callbacks callbacks = mCallbacks.get();
1101 if (callbacks != null) {
1102 callbacks.bindSearchablesChanged();
1103 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001104 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001105 }
1106 }
1107
Reena Lee93f824a2011-09-23 17:20:28 -07001108 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001109 resetLoadedState(true, true);
1110
Reena Lee93f824a2011-09-23 17:20:28 -07001111 // Do this here because if the launcher activity is running it will be restarted.
1112 // If it's not running startLoaderFromBackground will merely tell it that it needs
1113 // to reload.
1114 startLoaderFromBackground();
1115 }
1116
Winson Chungf0c6ae02012-03-21 16:10:31 -07001117 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1118 synchronized (mLock) {
1119 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1120 // mWorkspaceLoaded to true later
1121 stopLoaderLocked();
1122 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1123 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1124 }
1125 }
1126
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001127 /**
1128 * When the launcher is in the background, it's possible for it to miss paired
1129 * configuration changes. So whenever we trigger the loader from the background
1130 * tell the launcher that it needs to re-run the loader when it comes back instead
1131 * of doing it now.
1132 */
1133 public void startLoaderFromBackground() {
1134 boolean runLoader = false;
1135 if (mCallbacks != null) {
1136 Callbacks callbacks = mCallbacks.get();
1137 if (callbacks != null) {
1138 // Only actually run the loader if they're not paused.
1139 if (!callbacks.setLoadOnResume()) {
1140 runLoader = true;
1141 }
1142 }
1143 }
1144 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001145 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001146 }
Joe Onorato36115782010-06-17 13:28:48 -04001147 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001148
Reena Lee93f824a2011-09-23 17:20:28 -07001149 // If there is already a loader task running, tell it to stop.
1150 // returns true if isLaunching() was true on the old task
1151 private boolean stopLoaderLocked() {
1152 boolean isLaunching = false;
1153 LoaderTask oldTask = mLoaderTask;
1154 if (oldTask != null) {
1155 if (oldTask.isLaunching()) {
1156 isLaunching = true;
1157 }
1158 oldTask.stopLocked();
1159 }
1160 return isLaunching;
1161 }
1162
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001163 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001164 synchronized (mLock) {
1165 if (DEBUG_LOADERS) {
1166 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001169 // Clear any deferred bind-runnables from the synchronized load process
1170 // We must do this before any loading/binding is scheduled below.
1171 mDeferredBindRunnables.clear();
1172
Joe Onorato36115782010-06-17 13:28:48 -04001173 // Don't bother to start the thread if we know it's not going to do anything
1174 if (mCallbacks != null && mCallbacks.get() != null) {
1175 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001176 // also, don't downgrade isLaunching if we're already running
1177 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001178 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1180 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1181 } else {
1182 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1183 sWorker.post(mLoaderTask);
1184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 }
1187 }
1188
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 void bindRemainingSynchronousPages() {
1190 // Post the remaining side pages to be loaded
1191 if (!mDeferredBindRunnables.isEmpty()) {
1192 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001193 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 }
1195 mDeferredBindRunnables.clear();
1196 }
1197 }
1198
Joe Onorato36115782010-06-17 13:28:48 -04001199 public void stopLoader() {
1200 synchronized (mLock) {
1201 if (mLoaderTask != null) {
1202 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 }
1204 }
Joe Onorato36115782010-06-17 13:28:48 -04001205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206
Winson Chung76828c82013-08-19 15:43:29 -07001207 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1208 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1209 final ContentResolver contentResolver = context.getContentResolver();
1210 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1211 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1212 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1213
1214 try {
1215 final int idIndex = sc.getColumnIndexOrThrow(
1216 LauncherSettings.WorkspaceScreens._ID);
1217 final int rankIndex = sc.getColumnIndexOrThrow(
1218 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1219 while (sc.moveToNext()) {
1220 try {
1221 long screenId = sc.getLong(idIndex);
1222 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001223 orderedScreens.put(rank, screenId);
1224 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001225 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001226 }
1227 }
1228 } finally {
1229 sc.close();
1230 }
1231 return orderedScreens;
1232 }
1233
Michael Jurkac57b7a82011-08-09 22:02:20 -07001234 public boolean isAllAppsLoaded() {
1235 return mAllAppsLoaded;
1236 }
1237
Winson Chung36a62fe2012-05-06 18:04:42 -07001238 boolean isLoadingWorkspace() {
1239 synchronized (mLock) {
1240 if (mLoaderTask != null) {
1241 return mLoaderTask.isLoadingWorkspace();
1242 }
1243 }
1244 return false;
1245 }
1246
Joe Onorato36115782010-06-17 13:28:48 -04001247 /**
1248 * Runnable for the thread that loads the contents of the launcher:
1249 * - workspace icons
1250 * - widgets
1251 * - all apps icons
1252 */
1253 private class LoaderTask implements Runnable {
1254 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001255 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001256 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001257 private boolean mStopped;
1258 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001259
Winson Chungc3eecff2011-07-11 17:44:15 -07001260 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001261
1262 LoaderTask(Context context, boolean isLaunching) {
1263 mContext = context;
1264 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001265 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 }
1267
Joe Onorato36115782010-06-17 13:28:48 -04001268 boolean isLaunching() {
1269 return mIsLaunching;
1270 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271
Winson Chung36a62fe2012-05-06 18:04:42 -07001272 boolean isLoadingWorkspace() {
1273 return mIsLoadingAndBindingWorkspace;
1274 }
1275
Winson Chungc763c4e2013-07-19 13:49:06 -07001276 /** Returns whether this is an upgrade path */
1277 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001278 mIsLoadingAndBindingWorkspace = true;
1279
Joe Onorato36115782010-06-17 13:28:48 -04001280 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001281 if (DEBUG_LOADERS) {
1282 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001284
Winson Chungc763c4e2013-07-19 13:49:06 -07001285 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001286 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001287 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001288 synchronized (LoaderTask.this) {
1289 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001290 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001291 }
1292 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001293 }
1294 }
1295
Joe Onorato36115782010-06-17 13:28:48 -04001296 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001297 bindWorkspace(-1, isUpgradePath);
1298 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001299 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001300
Joe Onorato36115782010-06-17 13:28:48 -04001301 private void waitForIdle() {
1302 // Wait until the either we're stopped or the other threads are done.
1303 // This way we don't start loading all apps until the workspace has settled
1304 // down.
1305 synchronized (LoaderTask.this) {
1306 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001307
Joe Onorato36115782010-06-17 13:28:48 -04001308 mHandler.postIdle(new Runnable() {
1309 public void run() {
1310 synchronized (LoaderTask.this) {
1311 mLoadAndBindStepFinished = true;
1312 if (DEBUG_LOADERS) {
1313 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001314 }
Joe Onorato36115782010-06-17 13:28:48 -04001315 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001316 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001317 }
Joe Onorato36115782010-06-17 13:28:48 -04001318 });
1319
Michael Jurkac7700af2013-05-14 20:17:58 +02001320 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001321 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001322 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1323 // wait no longer than 1sec at a time
1324 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001325 } catch (InterruptedException ex) {
1326 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001327 }
1328 }
Joe Onorato36115782010-06-17 13:28:48 -04001329 if (DEBUG_LOADERS) {
1330 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001331 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001332 + "ms for previous step to finish binding");
1333 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001334 }
Joe Onorato36115782010-06-17 13:28:48 -04001335 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001336
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001337 void runBindSynchronousPage(int synchronousBindPage) {
1338 if (synchronousBindPage < 0) {
1339 // Ensure that we have a valid page index to load synchronously
1340 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1341 "valid page index");
1342 }
1343 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1344 // Ensure that we don't try and bind a specified page when the pages have not been
1345 // loaded already (we should load everything asynchronously in that case)
1346 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1347 }
1348 synchronized (mLock) {
1349 if (mIsLoaderTaskRunning) {
1350 // Ensure that we are never running the background loading at this point since
1351 // we also touch the background collections
1352 throw new RuntimeException("Error! Background loading is already running");
1353 }
1354 }
1355
1356 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1357 // data structures, we can't allow any other thread to touch that data, but because
1358 // this call is synchronous, we can get away with not locking).
1359
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001360 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001361 // operations from the previous activity. We need to ensure that all queued operations
1362 // are executed before any synchronous binding work is done.
1363 mHandler.flush();
1364
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365 // Divide the set of loaded items into those that we are binding synchronously, and
1366 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001367 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001368 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1369 // arise from that.
1370 onlyBindAllApps();
1371 }
1372
Joe Onorato36115782010-06-17 13:28:48 -04001373 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001374 boolean isUpgrade = false;
1375
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 synchronized (mLock) {
1377 mIsLoaderTaskRunning = true;
1378 }
Joe Onorato36115782010-06-17 13:28:48 -04001379 // Optimize for end-user experience: if the Launcher is up and // running with the
1380 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1381 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001382 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001383 // Elevate priority when Home launches for the first time to avoid
1384 // starving at boot time. Staring at a blank home is not cool.
1385 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001386 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1387 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 android.os.Process.setThreadPriority(mIsLaunching
1389 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1390 }
Winson Chung64359a52013-07-08 17:17:08 -07001391 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001392 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001393
Joe Onorato36115782010-06-17 13:28:48 -04001394 if (mStopped) {
1395 break keep_running;
1396 }
1397
1398 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1399 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001400 synchronized (mLock) {
1401 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001402 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001403 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1404 }
1405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001407
1408 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001409 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1410 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001411
1412 // Restore the default thread priority after we are done loading items
1413 synchronized (mLock) {
1414 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417
Winson Chungaac01e12011-08-17 10:37:13 -07001418 // Update the saved icons if necessary
1419 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001420 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 for (Object key : sBgDbIconCache.keySet()) {
1422 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1423 }
1424 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001425 }
Winson Chungaac01e12011-08-17 10:37:13 -07001426
Winson Chungc58497e2013-09-03 17:48:37 -07001427 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1428 // Ensure that all the applications that are in the system are
1429 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001430 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001431 verifyApplications();
1432 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001433 }
1434
Joe Onorato36115782010-06-17 13:28:48 -04001435 // Clear out this reference, otherwise we end up holding it until all of the
1436 // callback runnables are done.
1437 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001438
Joe Onorato36115782010-06-17 13:28:48 -04001439 synchronized (mLock) {
1440 // If we are still the last one to be scheduled, remove ourselves.
1441 if (mLoaderTask == this) {
1442 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001444 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001445 }
Joe Onorato36115782010-06-17 13:28:48 -04001446 }
1447
1448 public void stopLocked() {
1449 synchronized (LoaderTask.this) {
1450 mStopped = true;
1451 this.notify();
1452 }
1453 }
1454
1455 /**
1456 * Gets the callbacks object. If we've been stopped, or if the launcher object
1457 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1458 * object that was around when the deferred message was scheduled, and if there's
1459 * a new Callbacks object around then also return null. This will save us from
1460 * calling onto it with data that will be ignored.
1461 */
1462 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1463 synchronized (mLock) {
1464 if (mStopped) {
1465 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001466 }
Joe Onorato36115782010-06-17 13:28:48 -04001467
1468 if (mCallbacks == null) {
1469 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471
1472 final Callbacks callbacks = mCallbacks.get();
1473 if (callbacks != oldCallbacks) {
1474 return null;
1475 }
1476 if (callbacks == null) {
1477 Log.w(TAG, "no mCallbacks");
1478 return null;
1479 }
1480
1481 return callbacks;
1482 }
1483 }
1484
Winson Chungc763c4e2013-07-19 13:49:06 -07001485 private void verifyApplications() {
1486 final Context context = mApp.getContext();
1487
1488 // Cross reference all the applications in our apps list with items in the workspace
1489 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001490 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001491 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001492 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001493 tmpInfos = getItemInfoForComponentName(app.componentName);
1494 if (tmpInfos.isEmpty()) {
1495 // We are missing an application icon, so add this to the workspace
1496 added.add(app);
1497 // This is a rare event, so lets log it
1498 Log.e(TAG, "Missing Application on load: " + app);
1499 }
1500 }
1501 }
1502 if (!added.isEmpty()) {
1503 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001504 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001505 }
1506 }
1507
Winson Chung5f8afe62013-08-12 16:19:28 -07001508 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001509 LauncherAppState app = LauncherAppState.getInstance();
1510 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1511 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1512 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 }
1514
Joe Onorato36115782010-06-17 13:28:48 -04001515 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001517 LauncherAppState app = LauncherAppState.getInstance();
1518 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1519 int countX = (int) grid.numColumns;
1520 int countY = (int) grid.numRows;
1521
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001523 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1525 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1526 [(int) item.screenId][0] != null) {
1527 Log.e(TAG, "Error loading shortcut into hotseat " + item
1528 + " into position (" + item.screenId + ":" + item.cellX + ","
1529 + item.cellY + ") occupied by "
1530 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1531 [(int) item.screenId][0]);
1532 return false;
1533 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001534 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001535 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001536 items[(int) item.screenId][0] = item;
1537 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001538 return true;
1539 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001540 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1541 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001542 return true;
1543 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001544
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001546 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 occupied.put(item.screenId, items);
1548 }
1549
1550 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001551 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001552 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1553 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001555 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001557 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001558 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001560 return false;
1561 }
1562 }
1563 }
1564 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1565 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001566 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001567 }
1568 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001569
Joe Onorato36115782010-06-17 13:28:48 -04001570 return true;
1571 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572
Winson Chungba9c37f2013-08-30 14:11:37 -07001573 /** Clears all the sBg data structures */
1574 private void clearSBgDataStructures() {
1575 synchronized (sBgLock) {
1576 sBgWorkspaceItems.clear();
1577 sBgAppWidgets.clear();
1578 sBgFolders.clear();
1579 sBgItemsIdMap.clear();
1580 sBgDbIconCache.clear();
1581 sBgWorkspaceScreens.clear();
1582 }
1583 }
1584
Winson Chungc763c4e2013-07-19 13:49:06 -07001585 /** Returns whether this is an upgradge path */
1586 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001587 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588
Joe Onorato36115782010-06-17 13:28:48 -04001589 final Context context = mContext;
1590 final ContentResolver contentResolver = context.getContentResolver();
1591 final PackageManager manager = context.getPackageManager();
1592 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1593 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001594
Winson Chung892c74d2013-08-22 16:15:50 -07001595 LauncherAppState app = LauncherAppState.getInstance();
1596 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1597 int countX = (int) grid.numColumns;
1598 int countY = (int) grid.numRows;
1599
Michael Jurkab85f8a42012-04-25 15:48:32 -07001600 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001601 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001602
Winson Chungc763c4e2013-07-19 13:49:06 -07001603 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001604 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001605
Winson Chung2abf94d2012-07-18 18:16:38 -07001606 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001607 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001608
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001609 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001610 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001611 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001612
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001613 // +1 for the hotseat (it can be larger than the workspace)
1614 // Load workspace in reverse order to ensure that latest items are loaded first (and
1615 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001618 try {
1619 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1620 final int intentIndex = c.getColumnIndexOrThrow
1621 (LauncherSettings.Favorites.INTENT);
1622 final int titleIndex = c.getColumnIndexOrThrow
1623 (LauncherSettings.Favorites.TITLE);
1624 final int iconTypeIndex = c.getColumnIndexOrThrow(
1625 LauncherSettings.Favorites.ICON_TYPE);
1626 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1627 final int iconPackageIndex = c.getColumnIndexOrThrow(
1628 LauncherSettings.Favorites.ICON_PACKAGE);
1629 final int iconResourceIndex = c.getColumnIndexOrThrow(
1630 LauncherSettings.Favorites.ICON_RESOURCE);
1631 final int containerIndex = c.getColumnIndexOrThrow(
1632 LauncherSettings.Favorites.CONTAINER);
1633 final int itemTypeIndex = c.getColumnIndexOrThrow(
1634 LauncherSettings.Favorites.ITEM_TYPE);
1635 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1636 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001637 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001639 final int screenIndex = c.getColumnIndexOrThrow(
1640 LauncherSettings.Favorites.SCREEN);
1641 final int cellXIndex = c.getColumnIndexOrThrow
1642 (LauncherSettings.Favorites.CELLX);
1643 final int cellYIndex = c.getColumnIndexOrThrow
1644 (LauncherSettings.Favorites.CELLY);
1645 final int spanXIndex = c.getColumnIndexOrThrow
1646 (LauncherSettings.Favorites.SPANX);
1647 final int spanYIndex = c.getColumnIndexOrThrow(
1648 LauncherSettings.Favorites.SPANY);
1649 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1650 //final int displayModeIndex = c.getColumnIndexOrThrow(
1651 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001653 ShortcutInfo info;
1654 String intentDescription;
1655 LauncherAppWidgetInfo appWidgetInfo;
1656 int container;
1657 long id;
1658 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001660 while (!mStopped && c.moveToNext()) {
1661 try {
1662 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001664 switch (itemType) {
1665 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1666 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001667 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001668 intentDescription = c.getString(intentIndex);
1669 try {
1670 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001671 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001672 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001673 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001674 // Log the invalid package, and remove it from the db
1675 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1676 false);
Winson Chungee055712013-07-30 14:46:24 -07001677 contentResolver.delete(uri, null, null);
Winson Chungee055712013-07-30 14:46:24 -07001678 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001679 // If apps can be on external storage, then we just
1680 // leave them for the user to remove (maybe add
1681 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001682 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001683 }
1684 continue;
1685 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001687 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 continue;
1689 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001691 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1692 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1693 titleIndex, mLabelCache);
1694 } else {
1695 info = getShortcutInfo(c, context, iconTypeIndex,
1696 iconPackageIndex, iconResourceIndex, iconIndex,
1697 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001698
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 // App shortcuts that used to be automatically added to Launcher
1700 // didn't always have the correct intent flags set, so do that
1701 // here
1702 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001703 intent.getCategories() != null &&
1704 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001705 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 intent.addFlags(
1707 Intent.FLAG_ACTIVITY_NEW_TASK |
1708 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1709 }
Michael Jurka96879562012-03-22 05:54:33 -07001710 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001713 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 container = c.getInt(containerIndex);
1716 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001717 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 info.cellX = c.getInt(cellXIndex);
1719 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001720 info.spanX = 1;
1721 info.spanY = 1;
1722 // Skip loading items that are out of bounds
1723 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1724 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001725 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1726 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001727 continue;
1728 }
1729 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 // check & update map of what's occupied
1731 if (!checkItemPlacement(occupied, info)) {
1732 break;
1733 }
1734
1735 switch (container) {
1736 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1737 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1738 sBgWorkspaceItems.add(info);
1739 break;
1740 default:
1741 // Item is in a user folder
1742 FolderInfo folderInfo =
1743 findOrMakeFolder(sBgFolders, container);
1744 folderInfo.add(info);
1745 break;
1746 }
1747 sBgItemsIdMap.put(info.id, info);
1748
1749 // now that we've loaded everthing re-save it with the
1750 // icon in case it disappears somehow.
1751 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001752 } else {
1753 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 }
1755 break;
1756
1757 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1758 id = c.getLong(idIndex);
1759 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1760
1761 folderInfo.title = c.getString(titleIndex);
1762 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001765 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 folderInfo.cellX = c.getInt(cellXIndex);
1767 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001768 folderInfo.spanX = 1;
1769 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770
Winson Chung5f8afe62013-08-12 16:19:28 -07001771 // Skip loading items that are out of bounds
1772 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001773 if (checkItemDimensions(folderInfo)) {
1774 Log.d(TAG, "Skipped loading out of bounds folder");
1775 continue;
1776 }
1777 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001778 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001780 break;
1781 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001782
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1785 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1786 sBgWorkspaceItems.add(folderInfo);
1787 break;
Joe Onorato36115782010-06-17 13:28:48 -04001788 }
Joe Onorato17a89222011-02-08 17:26:11 -08001789
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001790 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1791 sBgFolders.put(folderInfo.id, folderInfo);
1792 break;
1793
1794 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1795 // Read all Launcher-specific widget details
1796 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001797 String savedProvider = c.getString(appWidgetProviderIndex);
1798
Joe Onorato36115782010-06-17 13:28:48 -04001799 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001800
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001801 final AppWidgetProviderInfo provider =
1802 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001803
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001804 if (!isSafeMode && (provider == null || provider.provider == null ||
1805 provider.provider.getPackageName() == null)) {
1806 String log = "Deleting widget that isn't installed anymore: id="
1807 + id + " appWidgetId=" + appWidgetId;
1808 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001809 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 itemsToRemove.add(id);
1811 } else {
1812 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1813 provider.provider);
1814 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001815 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 appWidgetInfo.cellX = c.getInt(cellXIndex);
1817 appWidgetInfo.cellY = c.getInt(cellYIndex);
1818 appWidgetInfo.spanX = c.getInt(spanXIndex);
1819 appWidgetInfo.spanY = c.getInt(spanYIndex);
1820 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1821 appWidgetInfo.minSpanX = minSpan[0];
1822 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001823
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 container = c.getInt(containerIndex);
1825 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1826 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1827 Log.e(TAG, "Widget found where container != " +
1828 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1829 continue;
1830 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831
Adam Cohene25af792013-06-06 23:08:25 -07001832 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001833 // Skip loading items that are out of bounds
1834 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1835 if (checkItemDimensions(appWidgetInfo)) {
1836 Log.d(TAG, "Skipped loading out of bounds app widget");
1837 continue;
1838 }
1839 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 // check & update map of what's occupied
1841 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1842 break;
1843 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001844 String providerName = provider.provider.flattenToString();
1845 if (!providerName.equals(savedProvider)) {
1846 ContentValues values = new ContentValues();
1847 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1848 providerName);
1849 String where = BaseColumns._ID + "= ?";
1850 String[] args = {Integer.toString(c.getInt(idIndex))};
1851 contentResolver.update(contentUri, values, where, args);
1852 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1854 sBgAppWidgets.add(appWidgetInfo);
1855 }
Joe Onorato36115782010-06-17 13:28:48 -04001856 break;
1857 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001859 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001860 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 }
1862 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001863 if (c != null) {
1864 c.close();
1865 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 }
1867
Winson Chungba9c37f2013-08-30 14:11:37 -07001868 // Break early if we've stopped loading
1869 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001870 clearSBgDataStructures();
1871 return false;
1872 }
1873
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 if (itemsToRemove.size() > 0) {
1875 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001876 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 // Remove dead items
1878 for (long id : itemsToRemove) {
1879 if (DEBUG_LOADERS) {
1880 Log.d(TAG, "Removed id = " + id);
1881 }
1882 // Don't notify content observers
1883 try {
1884 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1885 null, null);
1886 } catch (RemoteException e) {
1887 Log.w(TAG, "Could not remove id = " + id);
1888 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001889 }
1890 }
1891
Winson Chungc763c4e2013-07-19 13:49:06 -07001892 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001893 long maxScreenId = 0;
1894 // If we're importing we use the old screen order.
1895 for (ItemInfo item: sBgItemsIdMap.values()) {
1896 long screenId = item.screenId;
1897 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1898 !sBgWorkspaceScreens.contains(screenId)) {
1899 sBgWorkspaceScreens.add(screenId);
1900 if (screenId > maxScreenId) {
1901 maxScreenId = screenId;
1902 }
1903 }
1904 }
1905 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001906
Michael Jurka414300a2013-08-27 15:42:35 +02001907 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001908 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001909
1910 // Update the max item id after we load an old db
1911 long maxItemId = 0;
1912 // If we're importing we use the old screen order.
1913 for (ItemInfo item: sBgItemsIdMap.values()) {
1914 maxItemId = Math.max(maxItemId, item.id);
1915 }
Michael Jurka414300a2013-08-27 15:42:35 +02001916 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001918 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1919 for (Integer i : orderedScreens.keySet()) {
1920 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 }
1922
1923 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001924 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 for (ItemInfo item: sBgItemsIdMap.values()) {
1926 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1928 unusedScreens.contains(screenId)) {
1929 unusedScreens.remove(screenId);
1930 }
1931 }
1932
1933 // If there are any empty screens remove them, and update.
1934 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001935 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001936 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1937 }
1938 }
1939
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 if (DEBUG_LOADERS) {
1941 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1942 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001944 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001945 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001946
Daniel Sandler566da102013-06-25 23:43:45 -04001947 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001948 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001950 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001951 line += " | ";
1952 }
Winson Chung892c74d2013-08-22 16:15:50 -07001953 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001955 }
Joe Onorato36115782010-06-17 13:28:48 -04001956 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001957 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001959 }
Joe Onorato36115782010-06-17 13:28:48 -04001960 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001961 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001962 }
1963
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001964 /** Filters the set of items who are directly or indirectly (via another container) on the
1965 * specified screen. */
1966 private void filterCurrentWorkspaceItems(int currentScreen,
1967 ArrayList<ItemInfo> allWorkspaceItems,
1968 ArrayList<ItemInfo> currentScreenItems,
1969 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001970 // Purge any null ItemInfos
1971 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1972 while (iter.hasNext()) {
1973 ItemInfo i = iter.next();
1974 if (i == null) {
1975 iter.remove();
1976 }
1977 }
1978
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001979 // If we aren't filtering on a screen, then the set of items to load is the full set of
1980 // items given.
1981 if (currentScreen < 0) {
1982 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04001983 }
1984
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 // Order the set of items by their containers first, this allows use to walk through the
1986 // list sequentially, build up a list of containers that are in the specified screen,
1987 // as well as all items in those containers.
1988 Set<Long> itemsOnScreen = new HashSet<Long>();
1989 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
1990 @Override
1991 public int compare(ItemInfo lhs, ItemInfo rhs) {
1992 return (int) (lhs.container - rhs.container);
1993 }
1994 });
1995 for (ItemInfo info : allWorkspaceItems) {
1996 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001998 currentScreenItems.add(info);
1999 itemsOnScreen.add(info.id);
2000 } else {
2001 otherScreenItems.add(info);
2002 }
2003 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2004 currentScreenItems.add(info);
2005 itemsOnScreen.add(info.id);
2006 } else {
2007 if (itemsOnScreen.contains(info.container)) {
2008 currentScreenItems.add(info);
2009 itemsOnScreen.add(info.id);
2010 } else {
2011 otherScreenItems.add(info);
2012 }
2013 }
2014 }
2015 }
2016
2017 /** Filters the set of widgets which are on the specified screen. */
2018 private void filterCurrentAppWidgets(int currentScreen,
2019 ArrayList<LauncherAppWidgetInfo> appWidgets,
2020 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2021 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2022 // If we aren't filtering on a screen, then the set of items to load is the full set of
2023 // widgets given.
2024 if (currentScreen < 0) {
2025 currentScreenWidgets.addAll(appWidgets);
2026 }
2027
2028 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002029 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002031 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 currentScreenWidgets.add(widget);
2033 } else {
2034 otherScreenWidgets.add(widget);
2035 }
2036 }
2037 }
2038
2039 /** Filters the set of folders which are on the specified screen. */
2040 private void filterCurrentFolders(int currentScreen,
2041 HashMap<Long, ItemInfo> itemsIdMap,
2042 HashMap<Long, FolderInfo> folders,
2043 HashMap<Long, FolderInfo> currentScreenFolders,
2044 HashMap<Long, FolderInfo> otherScreenFolders) {
2045 // If we aren't filtering on a screen, then the set of items to load is the full set of
2046 // widgets given.
2047 if (currentScreen < 0) {
2048 currentScreenFolders.putAll(folders);
2049 }
2050
2051 for (long id : folders.keySet()) {
2052 ItemInfo info = itemsIdMap.get(id);
2053 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002054 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002056 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 currentScreenFolders.put(id, folder);
2058 } else {
2059 otherScreenFolders.put(id, folder);
2060 }
2061 }
2062 }
2063
2064 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2065 * right) */
2066 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002067 final LauncherAppState app = LauncherAppState.getInstance();
2068 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002069 // XXX: review this
2070 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002071 @Override
2072 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002073 int cellCountX = (int) grid.numColumns;
2074 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002075 int screenOffset = cellCountX * cellCountY;
2076 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002077 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002078 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002079 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002080 rhs.cellY * cellCountX + rhs.cellX);
2081 return (int) (lr - rr);
2082 }
2083 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002085
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2087 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 final Runnable r = new Runnable() {
2089 @Override
2090 public void run() {
2091 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2092 if (callbacks != null) {
2093 callbacks.bindScreens(orderedScreens);
2094 }
2095 }
2096 };
2097 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2098 }
2099
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2101 final ArrayList<ItemInfo> workspaceItems,
2102 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2103 final HashMap<Long, FolderInfo> folders,
2104 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002105
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 final boolean postOnMainThread = (deferredBindRunnables != null);
2107
2108 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002109 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002111 final int start = i;
2112 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 final Runnable r = new Runnable() {
2114 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002116 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002118 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2119 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 }
2121 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 };
2123 if (postOnMainThread) {
2124 deferredBindRunnables.add(r);
2125 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002126 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 }
Joe Onorato36115782010-06-17 13:28:48 -04002128 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129
2130 // Bind the folders
2131 if (!folders.isEmpty()) {
2132 final Runnable r = new Runnable() {
2133 public void run() {
2134 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2135 if (callbacks != null) {
2136 callbacks.bindFolders(folders);
2137 }
2138 }
2139 };
2140 if (postOnMainThread) {
2141 deferredBindRunnables.add(r);
2142 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002143 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 }
2145 }
2146
2147 // Bind the widgets, one at a time
2148 N = appWidgets.size();
2149 for (int i = 0; i < N; i++) {
2150 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2151 final Runnable r = new Runnable() {
2152 public void run() {
2153 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2154 if (callbacks != null) {
2155 callbacks.bindAppWidget(widget);
2156 }
2157 }
2158 };
2159 if (postOnMainThread) {
2160 deferredBindRunnables.add(r);
2161 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002162 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 }
2164 }
2165 }
2166
2167 /**
2168 * Binds all loaded data to actual views on the main thread.
2169 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002170 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 final long t = SystemClock.uptimeMillis();
2172 Runnable r;
2173
2174 // Don't use these two variables in any of the callback runnables.
2175 // Otherwise we hold a reference to them.
2176 final Callbacks oldCallbacks = mCallbacks.get();
2177 if (oldCallbacks == null) {
2178 // This launcher has exited and nobody bothered to tell us. Just bail.
2179 Log.w(TAG, "LoaderTask running with no launcher");
2180 return;
2181 }
2182
Winson Chung4a2afa32012-07-19 14:53:05 -07002183 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2184 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002185 oldCallbacks.getCurrentWorkspaceScreen();
2186
2187 // Load all the items that are on the current page first (and in the process, unbind
2188 // all the existing workspace items before we call startBinding() below.
2189 unbindWorkspaceItemsOnMainThread();
2190 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2191 ArrayList<LauncherAppWidgetInfo> appWidgets =
2192 new ArrayList<LauncherAppWidgetInfo>();
2193 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2194 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002195 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002196 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002197 workspaceItems.addAll(sBgWorkspaceItems);
2198 appWidgets.addAll(sBgAppWidgets);
2199 folders.putAll(sBgFolders);
2200 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002201 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 }
2203
2204 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2205 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2206 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2207 new ArrayList<LauncherAppWidgetInfo>();
2208 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2209 new ArrayList<LauncherAppWidgetInfo>();
2210 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2211 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2212
2213 // Separate the items that are on the current screen, and all the other remaining items
2214 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2215 otherWorkspaceItems);
2216 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2217 otherAppWidgets);
2218 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2219 otherFolders);
2220 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2221 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2222
2223 // Tell the workspace that we're about to start binding items
2224 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002225 public void run() {
2226 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2227 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002229 }
2230 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 };
Winson Chung81b52252012-08-27 15:34:29 -07002232 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2235
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 // Load items on the current page
2237 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2238 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002239 if (isLoadingSynchronously) {
2240 r = new Runnable() {
2241 public void run() {
2242 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2243 if (callbacks != null) {
2244 callbacks.onPageBoundSynchronously(currentScreen);
2245 }
2246 }
2247 };
Winson Chung81b52252012-08-27 15:34:29 -07002248 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002249 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002250
Winson Chung4a2afa32012-07-19 14:53:05 -07002251 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2252 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002253 mDeferredBindRunnables.clear();
2254 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002255 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256
2257 // Tell the workspace that we're done binding items
2258 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002259 public void run() {
2260 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2261 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002262 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002263 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264
Winson Chung98e030b2012-05-07 16:01:11 -07002265 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002266 if (DEBUG_LOADERS) {
2267 Log.d(TAG, "bound workspace in "
2268 + (SystemClock.uptimeMillis()-t) + "ms");
2269 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002270
2271 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002272 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002274 if (isLoadingSynchronously) {
2275 mDeferredBindRunnables.add(r);
2276 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002277 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002278 }
Joe Onorato36115782010-06-17 13:28:48 -04002279 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002280
Joe Onorato36115782010-06-17 13:28:48 -04002281 private void loadAndBindAllApps() {
2282 if (DEBUG_LOADERS) {
2283 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2284 }
2285 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002286 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002287 synchronized (LoaderTask.this) {
2288 if (mStopped) {
2289 return;
2290 }
2291 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002292 }
Joe Onorato36115782010-06-17 13:28:48 -04002293 } else {
2294 onlyBindAllApps();
2295 }
2296 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002297
Joe Onorato36115782010-06-17 13:28:48 -04002298 private void onlyBindAllApps() {
2299 final Callbacks oldCallbacks = mCallbacks.get();
2300 if (oldCallbacks == null) {
2301 // This launcher has exited and nobody bothered to tell us. Just bail.
2302 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2303 return;
2304 }
2305
2306 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002307 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002308 final ArrayList<AppInfo> list
2309 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002310 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002311 public void run() {
2312 final long t = SystemClock.uptimeMillis();
2313 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2314 if (callbacks != null) {
2315 callbacks.bindAllApplications(list);
2316 }
2317 if (DEBUG_LOADERS) {
2318 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2319 + (SystemClock.uptimeMillis()-t) + "ms");
2320 }
2321 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002322 };
2323 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002324 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002325 r.run();
2326 } else {
2327 mHandler.post(r);
2328 }
Joe Onorato36115782010-06-17 13:28:48 -04002329 }
2330
Winson Chung64359a52013-07-08 17:17:08 -07002331 private void loadAllApps() {
2332 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002333
Joe Onorato36115782010-06-17 13:28:48 -04002334 final Callbacks oldCallbacks = mCallbacks.get();
2335 if (oldCallbacks == null) {
2336 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002337 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002338 return;
2339 }
2340
Winson Chung64359a52013-07-08 17:17:08 -07002341 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002342 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2343 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2344
Winson Chung64359a52013-07-08 17:17:08 -07002345 // Clear the list of apps
2346 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002347
Winson Chung64359a52013-07-08 17:17:08 -07002348 // Query for the set of apps
2349 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2350 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2351 if (DEBUG_LOADERS) {
2352 Log.d(TAG, "queryIntentActivities took "
2353 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2354 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2355 }
2356 // Fail if we don't have any apps
2357 if (apps == null || apps.isEmpty()) {
2358 return;
2359 }
2360 // Sort the applications by name
2361 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2362 Collections.sort(apps,
2363 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2364 if (DEBUG_LOADERS) {
2365 Log.d(TAG, "sort took "
2366 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002367 }
2368
Winson Chung64359a52013-07-08 17:17:08 -07002369 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002370 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002371 ResolveInfo app = apps.get(i);
2372 if (oldCallbacks.shouldShowApp(app)) {
2373 // This builds the icon bitmaps.
2374 mBgAllAppsList.add(new AppInfo(packageManager, app,
2375 mIconCache, mLabelCache));
2376 }
Winson Chung64359a52013-07-08 17:17:08 -07002377 }
2378
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002379 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002380 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2381 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002382
2383 // Post callback on main thread
2384 mHandler.post(new Runnable() {
2385 public void run() {
2386 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002387 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002388 if (callbacks != null) {
2389 callbacks.bindAllApplications(added);
2390 if (DEBUG_LOADERS) {
2391 Log.d(TAG, "bound " + added.size() + " apps in "
2392 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2393 }
2394 } else {
2395 Log.i(TAG, "not binding apps: no Launcher activity");
2396 }
2397 }
2398 });
2399
Joe Onorato36115782010-06-17 13:28:48 -04002400 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002401 Log.d(TAG, "Icons processed in "
2402 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002403 }
2404 }
2405
2406 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002407 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002408 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2409 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2410 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2411 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2412 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2413 }
Joe Onorato36115782010-06-17 13:28:48 -04002414 }
2415 }
2416
2417 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002418 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002419 }
2420
2421 private class PackageUpdatedTask implements Runnable {
2422 int mOp;
2423 String[] mPackages;
2424
2425 public static final int OP_NONE = 0;
2426 public static final int OP_ADD = 1;
2427 public static final int OP_UPDATE = 2;
2428 public static final int OP_REMOVE = 3; // uninstlled
2429 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2430
2431
2432 public PackageUpdatedTask(int op, String[] packages) {
2433 mOp = op;
2434 mPackages = packages;
2435 }
2436
2437 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002438 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002439
2440 final String[] packages = mPackages;
2441 final int N = packages.length;
2442 switch (mOp) {
2443 case OP_ADD:
2444 for (int i=0; i<N; i++) {
2445 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002446 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002447 }
2448 break;
2449 case OP_UPDATE:
2450 for (int i=0; i<N; i++) {
2451 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002452 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002453 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002454 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002455 }
2456 break;
2457 case OP_REMOVE:
2458 case OP_UNAVAILABLE:
2459 for (int i=0; i<N; i++) {
2460 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002461 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002462 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002463 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002464 }
2465 break;
2466 }
2467
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002468 ArrayList<AppInfo> added = null;
2469 ArrayList<AppInfo> modified = null;
2470 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002471
Adam Cohen487f7dd2012-06-28 18:12:10 -07002472 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002473 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002474 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002475 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002476 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002477 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002478 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002479 }
Winson Chung5d55f332012-07-16 20:45:03 -07002480 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002481 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002482 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002483 }
2484
Joe Onorato36115782010-06-17 13:28:48 -04002485 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2486 if (callbacks == null) {
2487 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2488 return;
2489 }
2490
2491 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002492 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002493 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002494 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07002495 addAndBindAddedApps(context, addedInfos, cb, added);
Joe Onorato36115782010-06-17 13:28:48 -04002496 }
2497 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002498 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002499
2500 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002501 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002502 ArrayList<ItemInfo> infos =
2503 getItemInfoForComponentName(a.componentName);
2504 for (ItemInfo i : infos) {
2505 if (isShortcutInfoUpdateable(i)) {
2506 ShortcutInfo info = (ShortcutInfo) i;
2507 info.title = a.title.toString();
2508 updateItemInDatabase(context, info);
2509 }
2510 }
2511 }
2512
Joe Onorato36115782010-06-17 13:28:48 -04002513 mHandler.post(new Runnable() {
2514 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002515 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2516 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002517 callbacks.bindAppsUpdated(modifiedFinal);
2518 }
2519 }
2520 });
2521 }
Winson Chung83892cc2013-05-01 16:53:33 -07002522 // If a package has been removed, or an app has been removed as a result of
2523 // an update (for example), make the removed callback.
2524 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002525 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002526 final ArrayList<String> removedPackageNames =
2527 new ArrayList<String>(Arrays.asList(packages));
2528
Winson Chung64359a52013-07-08 17:17:08 -07002529 // Update the launcher db to reflect the removal of apps
2530 if (packageRemoved) {
2531 for (String pn : removedPackageNames) {
2532 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2533 for (ItemInfo i : infos) {
2534 deleteItemFromDatabase(context, i);
2535 }
2536 }
2537 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002538 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002539 ArrayList<ItemInfo> infos =
2540 getItemInfoForComponentName(a.componentName);
2541 for (ItemInfo i : infos) {
2542 deleteItemFromDatabase(context, i);
2543 }
2544 }
2545 }
2546
Joe Onorato36115782010-06-17 13:28:48 -04002547 mHandler.post(new Runnable() {
2548 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002549 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2550 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002551 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002552 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002553 }
2554 }
2555 });
Joe Onoratobe386092009-11-17 17:32:16 -08002556 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002557
Michael Jurkac402cd92013-05-20 15:49:32 +02002558 final ArrayList<Object> widgetsAndShortcuts =
2559 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002560 mHandler.post(new Runnable() {
2561 @Override
2562 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002563 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2564 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002565 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002566 }
2567 }
2568 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002569
2570 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002571 mHandler.post(new Runnable() {
2572 public void run() {
2573 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2574 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002575 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002576 }
2577 }
2578 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002579 }
2580 }
2581
Michael Jurkac402cd92013-05-20 15:49:32 +02002582 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2583 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2584 PackageManager packageManager = context.getPackageManager();
2585 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2586 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2587 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2588 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2589 Collections.sort(widgetsAndShortcuts,
2590 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2591 return widgetsAndShortcuts;
2592 }
2593
Winson Chung1323b482013-08-05 12:41:55 -07002594 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002595 if (cn == null) {
2596 return false;
2597 }
2598
2599 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002600 // Skip if the application is disabled
2601 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2602 if (!pi.applicationInfo.enabled) {
2603 return false;
2604 }
2605
2606 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002607 return (pm.getActivityInfo(cn, 0) != null);
2608 } catch (NameNotFoundException e) {
2609 return false;
2610 }
2611 }
2612
Joe Onorato9c1289c2009-08-17 11:03:03 -04002613 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002614 * This is called from the code that adds shortcuts from the intent receiver. This
2615 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 */
Joe Onorato56d82912010-03-07 14:32:10 -05002617 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002618 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002620
Joe Onorato56d82912010-03-07 14:32:10 -05002621 /**
2622 * Make an ShortcutInfo object for a shortcut that is an application.
2623 *
2624 * If c is not null, then it will be used to fill in missing data like the title and icon.
2625 */
2626 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002627 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002628 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002629 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002630 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002631 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002632 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002633 } else {
2634 try {
2635 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2636 info.initFlagsAndFirstInstallTime(pi);
2637 } catch (NameNotFoundException e) {
2638 Log.d(TAG, "getPackInfo failed for package " +
2639 componentName.getPackageName());
2640 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002641 }
2642
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002643 // TODO: See if the PackageManager knows about this case. If it doesn't
2644 // then return null & delete this.
2645
Joe Onorato56d82912010-03-07 14:32:10 -05002646 // the resource -- This may implicitly give us back the fallback icon,
2647 // but don't worry about that. All we're doing with usingFallbackIcon is
2648 // to avoid saving lots of copies of that in the database, and most apps
2649 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002650
2651 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2652 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2653 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002654 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002655 ResolveInfo resolveInfo = null;
2656 ComponentName oldComponent = intent.getComponent();
2657 Intent newIntent = new Intent(intent.getAction(), null);
2658 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2659 newIntent.setPackage(oldComponent.getPackageName());
2660 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2661 for (ResolveInfo i : infos) {
2662 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2663 i.activityInfo.name);
2664 if (cn.equals(oldComponent)) {
2665 resolveInfo = i;
2666 }
2667 }
2668 if (resolveInfo == null) {
2669 resolveInfo = manager.resolveActivity(intent, 0);
2670 }
Joe Onorato56d82912010-03-07 14:32:10 -05002671 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002672 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002673 }
Joe Onorato56d82912010-03-07 14:32:10 -05002674 // the db
2675 if (icon == null) {
2676 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002677 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002678 }
2679 }
2680 // the fallback icon
2681 if (icon == null) {
2682 icon = getFallbackIcon();
2683 info.usingFallbackIcon = true;
2684 }
2685 info.setIcon(icon);
2686
2687 // from the resource
2688 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002689 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2690 if (labelCache != null && labelCache.containsKey(key)) {
2691 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002692 } else {
2693 info.title = resolveInfo.activityInfo.loadLabel(manager);
2694 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002695 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002696 }
2697 }
Joe Onorato56d82912010-03-07 14:32:10 -05002698 }
2699 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002701 if (c != null) {
2702 info.title = c.getString(titleIndex);
2703 }
2704 }
2705 // fall back to the class name of the activity
2706 if (info.title == null) {
2707 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002709 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2710 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002712
Winson Chung64359a52013-07-08 17:17:08 -07002713 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2714 ItemInfoFilter f) {
2715 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2716 for (ItemInfo i : infos) {
2717 if (i instanceof ShortcutInfo) {
2718 ShortcutInfo info = (ShortcutInfo) i;
2719 ComponentName cn = info.intent.getComponent();
2720 if (cn != null && f.filterItem(null, info, cn)) {
2721 filtered.add(info);
2722 }
2723 } else if (i instanceof FolderInfo) {
2724 FolderInfo info = (FolderInfo) i;
2725 for (ShortcutInfo s : info.contents) {
2726 ComponentName cn = s.intent.getComponent();
2727 if (cn != null && f.filterItem(info, s, cn)) {
2728 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002729 }
2730 }
Winson Chung64359a52013-07-08 17:17:08 -07002731 } else if (i instanceof LauncherAppWidgetInfo) {
2732 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2733 ComponentName cn = info.providerName;
2734 if (cn != null && f.filterItem(null, info, cn)) {
2735 filtered.add(info);
2736 }
Winson Chung8a435102012-08-30 17:16:53 -07002737 }
2738 }
Winson Chung64359a52013-07-08 17:17:08 -07002739 return new ArrayList<ItemInfo>(filtered);
2740 }
2741
2742 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002743 ItemInfoFilter filter = new ItemInfoFilter() {
2744 @Override
2745 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2746 return cn.getPackageName().equals(pn);
2747 }
2748 };
2749 return filterItemInfos(sBgItemsIdMap.values(), filter);
2750 }
2751
2752 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002753 ItemInfoFilter filter = new ItemInfoFilter() {
2754 @Override
2755 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2756 return cn.equals(cname);
2757 }
2758 };
2759 return filterItemInfos(sBgItemsIdMap.values(), filter);
2760 }
2761
2762 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2763 if (i instanceof ShortcutInfo) {
2764 ShortcutInfo info = (ShortcutInfo) i;
2765 // We need to check for ACTION_MAIN otherwise getComponent() might
2766 // return null for some shortcuts (for instance, for shortcuts to
2767 // web pages.)
2768 Intent intent = info.intent;
2769 ComponentName name = intent.getComponent();
2770 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2771 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2772 return true;
2773 }
2774 }
2775 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002776 }
2777
2778 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002779 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002781 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002782 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2783 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784
Joe Onorato56d82912010-03-07 14:32:10 -05002785 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002786 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002789 // TODO: If there's an explicit component and we can't install that, delete it.
2790
Joe Onorato56d82912010-03-07 14:32:10 -05002791 info.title = c.getString(titleIndex);
2792
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 int iconType = c.getInt(iconTypeIndex);
2794 switch (iconType) {
2795 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2796 String packageName = c.getString(iconPackageIndex);
2797 String resourceName = c.getString(iconResourceIndex);
2798 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002799 info.customIcon = false;
2800 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002802 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002803 if (resources != null) {
2804 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002805 icon = Utilities.createIconBitmap(
2806 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002809 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 }
Joe Onorato56d82912010-03-07 14:32:10 -05002811 // the db
2812 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002813 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002814 }
2815 // the fallback icon
2816 if (icon == null) {
2817 icon = getFallbackIcon();
2818 info.usingFallbackIcon = true;
2819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 break;
2821 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002822 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002823 if (icon == null) {
2824 icon = getFallbackIcon();
2825 info.customIcon = false;
2826 info.usingFallbackIcon = true;
2827 } else {
2828 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 break;
2831 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002832 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002833 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 info.customIcon = false;
2835 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002837 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 return info;
2839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840
Michael Jurka931dc972011-08-05 15:08:15 -07002841 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002842 @SuppressWarnings("all") // suppress dead code warning
2843 final boolean debug = false;
2844 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002845 Log.d(TAG, "getIconFromCursor app="
2846 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2847 }
2848 byte[] data = c.getBlob(iconIndex);
2849 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002850 return Utilities.createIconBitmap(
2851 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002852 } catch (Exception e) {
2853 return null;
2854 }
2855 }
2856
Winson Chung3d503fb2011-07-13 17:25:49 -07002857 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2858 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002859 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002860 if (info == null) {
2861 return null;
2862 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002863 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002864
2865 return info;
2866 }
2867
Winson Chunga9abd0e2010-10-27 17:18:37 -07002868 /**
Winson Chung55cef262010-10-28 14:14:18 -07002869 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2870 */
2871 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2872 ComponentName component) {
2873 List<AppWidgetProviderInfo> widgets =
2874 AppWidgetManager.getInstance(context).getInstalledProviders();
2875 for (AppWidgetProviderInfo info : widgets) {
2876 if (info.provider.equals(component)) {
2877 return info;
2878 }
2879 }
2880 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002881 }
2882
Winson Chung68846fd2010-10-29 11:00:27 -07002883 /**
2884 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2885 */
2886 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2887 final PackageManager packageManager = context.getPackageManager();
2888 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2889 new ArrayList<WidgetMimeTypeHandlerData>();
2890
2891 final Intent supportsIntent =
2892 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2893 supportsIntent.setType(mimeType);
2894
2895 // Create a set of widget configuration components that we can test against
2896 final List<AppWidgetProviderInfo> widgets =
2897 AppWidgetManager.getInstance(context).getInstalledProviders();
2898 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2899 new HashMap<ComponentName, AppWidgetProviderInfo>();
2900 for (AppWidgetProviderInfo info : widgets) {
2901 configurationComponentToWidget.put(info.configure, info);
2902 }
2903
2904 // Run through each of the intents that can handle this type of clip data, and cross
2905 // reference them with the components that are actual configuration components
2906 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2907 PackageManager.MATCH_DEFAULT_ONLY);
2908 for (ResolveInfo info : activities) {
2909 final ActivityInfo activityInfo = info.activityInfo;
2910 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2911 activityInfo.name);
2912 if (configurationComponentToWidget.containsKey(infoComponent)) {
2913 supportedConfigurationActivities.add(
2914 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2915 configurationComponentToWidget.get(infoComponent)));
2916 }
2917 }
2918 return supportedConfigurationActivities;
2919 }
2920
Winson Chunga9abd0e2010-10-27 17:18:37 -07002921 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002922 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2923 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2924 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2925
Adam Cohend9198822011-11-22 16:42:47 -08002926 if (intent == null) {
2927 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2928 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2929 return null;
2930 }
2931
Joe Onorato0589f0f2010-02-08 13:44:00 -08002932 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002933 boolean customIcon = false;
2934 ShortcutIconResource iconResource = null;
2935
2936 if (bitmap != null && bitmap instanceof Bitmap) {
2937 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002938 customIcon = true;
2939 } else {
2940 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2941 if (extra != null && extra instanceof ShortcutIconResource) {
2942 try {
2943 iconResource = (ShortcutIconResource) extra;
2944 final PackageManager packageManager = context.getPackageManager();
2945 Resources resources = packageManager.getResourcesForApplication(
2946 iconResource.packageName);
2947 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002948 icon = Utilities.createIconBitmap(
2949 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002950 } catch (Exception e) {
2951 Log.w(TAG, "Could not load shortcut icon: " + extra);
2952 }
2953 }
2954 }
2955
Michael Jurkac9d95c52011-08-29 14:03:34 -07002956 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002957
2958 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002959 if (fallbackIcon != null) {
2960 icon = fallbackIcon;
2961 } else {
2962 icon = getFallbackIcon();
2963 info.usingFallbackIcon = true;
2964 }
Joe Onorato56d82912010-03-07 14:32:10 -05002965 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08002966 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05002967
Joe Onorato0589f0f2010-02-08 13:44:00 -08002968 info.title = name;
2969 info.intent = intent;
2970 info.customIcon = customIcon;
2971 info.iconResource = iconResource;
2972
2973 return info;
2974 }
2975
Winson Chungaac01e12011-08-17 10:37:13 -07002976 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
2977 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08002978 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07002979 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07002980 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08002981 }
Joe Onorato56d82912010-03-07 14:32:10 -05002982 // If this icon doesn't have a custom icon, check to see
2983 // what's stored in the DB, and if it doesn't match what
2984 // we're going to show, store what we are going to show back
2985 // into the DB. We do this so when we're loading, if the
2986 // package manager can't find an icon (for example because
2987 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07002988 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07002989 cache.put(info, c.getBlob(iconIndex));
2990 return true;
2991 }
2992 return false;
2993 }
2994 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
2995 boolean needSave = false;
2996 try {
2997 if (data != null) {
2998 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
2999 Bitmap loaded = info.getIcon(mIconCache);
3000 needSave = !saved.sameAs(loaded);
3001 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003002 needSave = true;
3003 }
Winson Chungaac01e12011-08-17 10:37:13 -07003004 } catch (Exception e) {
3005 needSave = true;
3006 }
3007 if (needSave) {
3008 Log.d(TAG, "going to save icon bitmap for info=" + info);
3009 // This is slower than is ideal, but this only happens once
3010 // or when the app is updated with a new icon.
3011 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003012 }
3013 }
3014
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003016 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 * or make a new one.
3018 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003019 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 // See if a placeholder was created for us already
3021 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003022 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003024 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 folders.put(id, folderInfo);
3026 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003027 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003030 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003031 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003032 return new Comparator<AppInfo>() {
3033 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003034 int result = collator.compare(a.title.toString(), b.title.toString());
3035 if (result == 0) {
3036 result = a.componentName.compareTo(b.componentName);
3037 }
3038 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003039 }
Winson Chung11904872012-09-17 16:58:46 -07003040 };
3041 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003042 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3043 = new Comparator<AppInfo>() {
3044 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003045 if (a.firstInstallTime < b.firstInstallTime) return 1;
3046 if (a.firstInstallTime > b.firstInstallTime) return -1;
3047 return 0;
3048 }
3049 };
Winson Chung11904872012-09-17 16:58:46 -07003050 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3051 final Collator collator = Collator.getInstance();
3052 return new Comparator<AppWidgetProviderInfo>() {
3053 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3054 return collator.compare(a.label.toString(), b.label.toString());
3055 }
3056 };
3057 }
Winson Chung5308f242011-08-18 12:12:41 -07003058 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3059 if (info.activityInfo != null) {
3060 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3061 } else {
3062 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3063 }
3064 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003065 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003066 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003067 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003068 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 ShortcutNameComparator(PackageManager pm) {
3070 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003071 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003072 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003073 }
3074 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3075 mPackageManager = pm;
3076 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003077 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003078 }
3079 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003080 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003081 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3082 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3083 if (mLabelCache.containsKey(keyA)) {
3084 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003085 } else {
3086 labelA = a.loadLabel(mPackageManager).toString();
3087
Winson Chung5308f242011-08-18 12:12:41 -07003088 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003089 }
Winson Chung5308f242011-08-18 12:12:41 -07003090 if (mLabelCache.containsKey(keyB)) {
3091 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003092 } else {
3093 labelB = b.loadLabel(mPackageManager).toString();
3094
Winson Chung5308f242011-08-18 12:12:41 -07003095 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003096 }
Winson Chung11904872012-09-17 16:58:46 -07003097 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003098 }
3099 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003100 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003101 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003102 private PackageManager mPackageManager;
3103 private HashMap<Object, String> mLabelCache;
3104 WidgetAndShortcutNameComparator(PackageManager pm) {
3105 mPackageManager = pm;
3106 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003107 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003108 }
3109 public final int compare(Object a, Object b) {
3110 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003111 if (mLabelCache.containsKey(a)) {
3112 labelA = mLabelCache.get(a);
3113 } else {
3114 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003115 ((AppWidgetProviderInfo) a).label :
3116 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003117 mLabelCache.put(a, labelA);
3118 }
3119 if (mLabelCache.containsKey(b)) {
3120 labelB = mLabelCache.get(b);
3121 } else {
3122 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003123 ((AppWidgetProviderInfo) b).label :
3124 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003125 mLabelCache.put(b, labelB);
3126 }
Winson Chung11904872012-09-17 16:58:46 -07003127 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003128 }
3129 };
Joe Onoratobe386092009-11-17 17:32:16 -08003130
3131 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003132 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003133 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3134 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3135 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3136 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003137 if (mLoaderTask != null) {
3138 mLoaderTask.dumpState();
3139 } else {
3140 Log.d(TAG, "mLoaderTask=null");
3141 }
Joe Onoratobe386092009-11-17 17:32:16 -08003142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143}