blob: dff1814267bd444509ba89f2fcc25949103aa38d [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;
Winson Chunga0b7e862013-09-05 16:03:15 -070061import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Maintains in-memory state of the Launcher. It is expected that there should be only one
65 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070066 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040068public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080069 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070071
Daniel Sandler8707e0f2013-08-15 15:54:18 -070072 // true = use a "More Apps" folder for non-workspace apps on upgrade
73 // false = strew non-workspace apps across the workspace on upgrade
74 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
75
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070077 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040078
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 private final Object mLock = new Object();
81 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040082 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020084 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Winson Chung81b52252012-08-27 15:34:29 -070086 // Specific runnable types that are run on the main thread deferred handler, this allows us to
87 // clear all queued binding runnables when the Launcher activity is destroyed.
88 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
89 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
90
91
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070092 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
93 static {
94 sWorkerThread.start();
95 }
96 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
97
Joe Onoratocc67f472010-06-08 10:54:30 -070098 // We start off with everything not loaded. After that, we assume that
99 // our monitoring of the package manager provides all updates and we never
100 // need to do a requery. These are only ever touched from the loader thread.
101 private boolean mWorkspaceLoaded;
102 private boolean mAllAppsLoaded;
103
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700104 // When we are loading pages synchronously, we can't just post the binding of items on the side
105 // pages as this delays the rotation process. Instead, we wait for a callback from the first
106 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
107 // a normal load, we also clear this set of Runnables.
108 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700112 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700113 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700115 // The lock that must be acquired before referencing any static bg data structures. Unlike
116 // other locks, this one can generally be held long-term because we never expect any of these
117 // static data structures to be referenced outside of the worker thread except on the first
118 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700119 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700120
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
126 // created by LauncherModel that are directly on the home screen (however, no widgets or
127 // shortcuts within folders).
128 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
131 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 new ArrayList<LauncherAppWidgetInfo>();
133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
135 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
138 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700139
140 // sBgWorkspaceScreens is the ordered set of workspace screens.
141 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
142
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // </ only access in worker thread >
144
145 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Reena Lee99a73f32011-10-24 17:27:37 -0700148 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700151 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 public int getCurrentWorkspaceScreen();
153 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700154 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
155 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700156 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700157 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500158 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700159 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 public void bindAppWidget(LauncherAppWidgetInfo info);
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,
Winson Chungdf95eb12013-10-16 14:57:07 -0700168 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200169 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100170 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700171 public boolean isAllAppsButtonRank(int rank);
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
Bjorn Bringert1307f632013-10-03 22:31:03 +0100180 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400181 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;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100185 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800186 mIconCache = iconCache;
187
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700189 Configuration config = res.getConfiguration();
190 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800191 }
192
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700193 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
194 * posted on the main thread handler. */
195 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700196 runOnMainThread(r, 0);
197 }
198 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700199 if (sWorkerThread.getThreadId() == Process.myTid()) {
200 // If we are on the worker thread, post onto the main handler
201 mHandler.post(r);
202 } else {
203 r.run();
204 }
205 }
206
207 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
208 * posted on the worker thread handler. */
209 private static void runOnWorkerThread(Runnable r) {
210 if (sWorkerThread.getThreadId() == Process.myTid()) {
211 r.run();
212 } else {
213 // If we are not on the worker thread, then post to the worker handler
214 sWorker.post(r);
215 }
216 }
217
Winson Chungc9168342013-06-26 14:54:55 -0700218 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
219 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700220 LauncherAppState app = LauncherAppState.getInstance();
221 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
222 final int xCount = (int) grid.numColumns;
223 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700224 boolean[][] occupied = new boolean[xCount][yCount];
225
226 int cellX, cellY, spanX, spanY;
227 for (int i = 0; i < items.size(); ++i) {
228 final ItemInfo item = items.get(i);
229 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
230 if (item.screenId == screen) {
231 cellX = item.cellX;
232 cellY = item.cellY;
233 spanX = item.spanX;
234 spanY = item.spanY;
235 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
236 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
237 occupied[x][y] = true;
238 }
239 }
240 }
241 }
242 }
243
244 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
245 }
246 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700247 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700248 int firstScreenIndex,
249 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700250 // Lock on the app so that we don't try and get the items while apps are being added
251 LauncherAppState app = LauncherAppState.getInstance();
252 LauncherModel model = app.getModel();
253 boolean found = false;
254 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700255 if (sWorkerThread.getThreadId() != Process.myTid()) {
256 // Flush the LauncherModel worker thread, so that if we just did another
257 // processInstallShortcut, we give it time for its shortcut to get added to the
258 // database (getItemsInLocalCoordinates reads the database)
259 model.flushWorkerThread();
260 }
Winson Chungc9168342013-06-26 14:54:55 -0700261 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700262
263 // Try adding to the workspace screens incrementally, starting at the default or center
264 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700265 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
266 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700267 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700268 int[] tmpCoordinates = new int[2];
269 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700270 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700272 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700273 }
274 }
275 }
Winson Chungc9168342013-06-26 14:54:55 -0700276 return null;
277 }
278
Winson Chung94d67682013-09-25 16:29:40 -0700279 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
280 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700281 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700282 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700283 }
Winson Chung94d67682013-09-25 16:29:40 -0700284 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
285 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
286 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700287 return;
Winson Chung997a9232013-07-24 15:33:46 -0700288 }
Winson Chung64359a52013-07-08 17:17:08 -0700289 // Process the newly added applications and add them to the database first
290 Runnable r = new Runnable() {
291 public void run() {
292 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
293 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
294
Winson Chung76828c82013-08-19 15:43:29 -0700295 // Get the list of workspace screens. We need to append to this list and
296 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
297 // called.
298 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
299 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
300 for (Integer i : orderedScreens.keySet()) {
301 long screenId = orderedScreens.get(i);
302 workspaceScreens.add(screenId);
303 }
304
Winson Chung64359a52013-07-08 17:17:08 -0700305 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700306 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700307 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700308 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700309 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700310 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700311
312 // Short-circuit this logic if the icon exists somewhere on the workspace
313 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
314 continue;
315 }
316
Winson Chung87412982013-10-03 18:34:14 -0700317 // Add this icon to the db, creating a new page if necessary. If there
318 // is only the empty page then we just add items to the first page.
319 // Otherwise, we add them to the next pages.
320 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700321 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700322 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700323 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200324 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700325
326 // If we can't find a valid position, then just add a new screen.
327 // This takes time so we need to re-queue the add until the new
328 // page is added. Create as many screens as necessary to satisfy
329 // the startSearchPageIndex.
330 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700331 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700332 while (numPagesToAdd > 0) {
333 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700334 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700335 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 addedWorkspaceScreensFinal.add(screenId);
337 numPagesToAdd--;
338 }
Winson Chung76828c82013-08-19 15:43:29 -0700339
Winson Chung64359a52013-07-08 17:17:08 -0700340 // Find the coordinate again
341 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700342 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700343 }
344 if (coords == null) {
345 throw new RuntimeException("Coordinates should not be null");
346 }
347
Winson Chung997a9232013-07-24 15:33:46 -0700348 ShortcutInfo shortcutInfo;
349 if (a instanceof ShortcutInfo) {
350 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200351 } else if (a instanceof AppInfo) {
352 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700353 } else {
354 throw new RuntimeException("Unexpected info type");
355 }
Winson Chung94d67682013-09-25 16:29:40 -0700356
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 Chung94d67682013-09-25 16:29:40 -0700369 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700370 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 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
375 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700376 if (!addedShortcutsFinal.isEmpty()) {
377 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
378 long lastScreenId = info.screenId;
379 for (ItemInfo i : addedShortcutsFinal) {
380 if (i.screenId == lastScreenId) {
381 addAnimated.add(i);
382 } else {
383 addNotAnimated.add(i);
384 }
Winson Chung997a9232013-07-24 15:33:46 -0700385 }
386 }
Winson Chungd64d1762013-08-20 14:37:16 -0700387 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700388 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
Winson Chung64359a52013-07-08 17:17:08 -0700390 }
Winson Chung997a9232013-07-24 15:33:46 -0700391 });
392 }
Winson Chung64359a52013-07-08 17:17:08 -0700393 }
394 };
395 runOnWorkerThread(r);
396 }
397
Joe Onorato56d82912010-03-07 14:32:10 -0500398 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700399 if (mDefaultIcon == null) {
400 final Context context = LauncherAppState.getInstance().getContext();
401 mDefaultIcon = Utilities.createIconBitmap(
402 mIconCache.getFullResDefaultActivityIcon(), context);
403 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800404 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406
Winson Chung81b52252012-08-27 15:34:29 -0700407 public void unbindItemInfosAndClearQueuedBindRunnables() {
408 if (sWorkerThread.getThreadId() == Process.myTid()) {
409 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
410 "main thread");
411 }
412
413 // Clear any deferred bind runnables
414 mDeferredBindRunnables.clear();
415 // Remove any queued bind runnables
416 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
417 // Unbind all the workspace items
418 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700419 }
420
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700422 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700423 // Ensure that we don't use the same workspace items data structure on the main thread
424 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
426 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700427 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700428 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
429 tmpAppWidgets.addAll(sBgAppWidgets);
430 }
431 Runnable r = new Runnable() {
432 @Override
433 public void run() {
434 for (ItemInfo item : tmpWorkspaceItems) {
435 item.unbind();
436 }
437 for (ItemInfo item : tmpAppWidgets) {
438 item.unbind();
439 }
440 }
441 };
442 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700443 }
444
Joe Onorato9c1289c2009-08-17 11:03:03 -0400445 /**
446 * Adds an item to the DB if it was not created previously, or move it to a new
447 * <container, screen, cellX, cellY>
448 */
449 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700450 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 if (item.container == ItemInfo.NO_ID) {
452 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700453 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 } else {
455 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700456 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458 }
459
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700460 static void checkItemInfoLocked(
461 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
462 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
463 if (modelItem != null && item != modelItem) {
464 // check all the data is consistent
465 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
466 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
467 ShortcutInfo shortcut = (ShortcutInfo) item;
468 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
469 modelShortcut.intent.filterEquals(shortcut.intent) &&
470 modelShortcut.id == shortcut.id &&
471 modelShortcut.itemType == shortcut.itemType &&
472 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700473 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700474 modelShortcut.cellX == shortcut.cellX &&
475 modelShortcut.cellY == shortcut.cellY &&
476 modelShortcut.spanX == shortcut.spanX &&
477 modelShortcut.spanY == shortcut.spanY &&
478 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
479 (modelShortcut.dropPos != null &&
480 shortcut.dropPos != null &&
481 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
482 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
483 // For all intents and purposes, this is the same object
484 return;
485 }
486 }
487
488 // the modelItem needs to match up perfectly with item if our model is
489 // to be consistent with the database-- for now, just require
490 // modelItem == item or the equality check above
491 String msg = "item: " + ((item != null) ? item.toString() : "null") +
492 "modelItem: " +
493 ((modelItem != null) ? modelItem.toString() : "null") +
494 "Error: ItemInfo passed to checkItemInfo doesn't match original";
495 RuntimeException e = new RuntimeException(msg);
496 if (stackTrace != null) {
497 e.setStackTrace(stackTrace);
498 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800499 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700500 }
501 }
502
Michael Jurka816474f2012-06-25 14:49:02 -0700503 static void checkItemInfo(final ItemInfo item) {
504 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
505 final long itemId = item.id;
506 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700507 public void run() {
508 synchronized (sBgLock) {
509 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700510 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700511 }
512 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700513 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700514 }
515
Michael Jurkac9d95c52011-08-29 14:03:34 -0700516 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
517 final ItemInfo item, final String callingFunction) {
518 final long itemId = item.id;
519 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
520 final ContentResolver cr = context.getContentResolver();
521
Adam Cohen487f7dd2012-06-28 18:12:10 -0700522 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700523 Runnable r = new Runnable() {
524 public void run() {
525 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700526 updateItemArrays(item, itemId, stackTrace);
527 }
528 };
529 runOnWorkerThread(r);
530 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700531
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
533 final ArrayList<ItemInfo> items, final String callingFunction) {
534 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700535
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700536 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
537 Runnable r = new Runnable() {
538 public void run() {
539 ArrayList<ContentProviderOperation> ops =
540 new ArrayList<ContentProviderOperation>();
541 int count = items.size();
542 for (int i = 0; i < count; i++) {
543 ItemInfo item = items.get(i);
544 final long itemId = item.id;
545 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
546 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700547
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700548 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
549 updateItemArrays(item, itemId, stackTrace);
550
551 }
552 try {
553 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
554 } catch (Exception e) {
555 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700556 }
557 }
558 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700559 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700561
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700562 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
563 // Lock on mBgLock *after* the db operation
564 synchronized (sBgLock) {
565 checkItemInfoLocked(itemId, item, stackTrace);
566
567 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
568 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
569 // Item is in a folder, make sure this folder exists
570 if (!sBgFolders.containsKey(item.container)) {
571 // An items container is being set to a that of an item which is not in
572 // the list of Folders.
573 String msg = "item: " + item + " container being set to: " +
574 item.container + ", not in the list of folders";
575 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700576 }
577 }
578
579 // Items are added/removed from the corresponding FolderInfo elsewhere, such
580 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
581 // that are on the desktop, as appropriate
582 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
583 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
584 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
585 switch (modelItem.itemType) {
586 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
587 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
588 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
589 if (!sBgWorkspaceItems.contains(modelItem)) {
590 sBgWorkspaceItems.add(modelItem);
591 }
592 break;
593 default:
594 break;
595 }
596 } else {
597 sBgWorkspaceItems.remove(modelItem);
598 }
599 }
600 }
601
Michael Jurkac7700af2013-05-14 20:17:58 +0200602 public void flushWorkerThread() {
603 mFlushingWorkerThread = true;
604 Runnable waiter = new Runnable() {
605 public void run() {
606 synchronized (this) {
607 notifyAll();
608 mFlushingWorkerThread = false;
609 }
610 }
611 };
612
613 synchronized(waiter) {
614 runOnWorkerThread(waiter);
615 if (mLoaderTask != null) {
616 synchronized(mLoaderTask) {
617 mLoaderTask.notify();
618 }
619 }
620 boolean success = false;
621 while (!success) {
622 try {
623 waiter.wait();
624 success = true;
625 } catch (InterruptedException e) {
626 }
627 }
628 }
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700633 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700634 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.cellX = cellX;
638 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 // We store hotseat items in canonical form which is this orientation invariant position
641 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
649 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
652 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700656 }
657
658 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
660 * cellX, cellY have already been updated on the ItemInfos.
661 */
662 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
663 final long container, final int screen) {
664
665 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
666 int count = items.size();
667
668 for (int i = 0; i < count; i++) {
669 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700670 item.container = container;
671
672 // We store hotseat items in canonical form which is this orientation invariant position
673 // in the hotseat
674 if (context instanceof Launcher && screen < 0 &&
675 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700676 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 item.cellY);
678 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700679 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 }
681
682 final ContentValues values = new ContentValues();
683 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
684 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
685 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687
688 contentValues.add(values);
689 }
690 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
691 }
692
693 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700694 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800695 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700696 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700697 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700698 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800699 item.cellX = cellX;
700 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 item.spanX = spanX;
702 item.spanY = spanY;
703
704 // We store hotseat items in canonical form which is this orientation invariant position
705 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700711 }
Adam Cohend4844c32011-02-18 19:25:06 -0800712
Adam Cohend4844c32011-02-18 19:25:06 -0800713 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800714 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
716 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
717 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
718 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800720
Michael Jurka816474f2012-06-25 14:49:02 -0700721 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723
724 /**
725 * Update an item to the database in a specified container.
726 */
727 static void updateItemInDatabase(Context context, final ItemInfo item) {
728 final ContentValues values = new ContentValues();
729 item.onAddToDatabase(values);
730 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
731 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800732 }
733
734 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 * Returns true if the shortcuts already exists in the database.
736 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 static boolean shortcutExists(Context context, String title, Intent intent) {
739 final ContentResolver cr = context.getContentResolver();
740 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
741 new String[] { "title", "intent" }, "title=? and intent=?",
742 new String[] { title, intent.toUri(0) }, null);
743 boolean result = false;
744 try {
745 result = c.moveToFirst();
746 } finally {
747 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700750 }
751
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 * Returns an ItemInfo array containing all the items in the LauncherModel.
754 * The ItemInfo.id is not set through this function.
755 */
756 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
757 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
758 final ContentResolver cr = context.getContentResolver();
759 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
760 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
761 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
762 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
763
764 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
765 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
766 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
767 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
768 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
769 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
770 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
771
772 try {
773 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700774 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700775 item.cellX = c.getInt(cellXIndex);
776 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700777 item.spanX = Math.max(1, c.getInt(spanXIndex));
778 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 item.container = c.getInt(containerIndex);
780 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
783 items.add(item);
784 }
785 } catch (Exception e) {
786 items.clear();
787 } finally {
788 c.close();
789 }
790
791 return items;
792 }
793
794 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
796 */
797 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
798 final ContentResolver cr = context.getContentResolver();
799 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
800 "_id=? and (itemType=? or itemType=?)",
801 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700802 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700803
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 try {
805 if (c.moveToFirst()) {
806 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
807 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
808 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
809 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
810 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
811 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 FolderInfo folderInfo = null;
814 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700815 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
816 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700818 }
819
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 folderInfo.title = c.getString(titleIndex);
821 folderInfo.id = id;
822 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700824 folderInfo.cellX = c.getInt(cellXIndex);
825 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826
827 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 } finally {
830 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700831 }
832
833 return null;
834 }
835
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 /**
837 * Add an item to the database in a specified container. Sets the container, screen, cellX and
838 * cellY fields of the item. Also assigns an ID to the item.
839 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700840 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 item.cellX = cellX;
844 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 // We store hotseat items in canonical form which is this orientation invariant position
846 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853
854 final ContentValues values = new ContentValues();
855 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 item.onAddToDatabase(values);
857
Michael Jurka414300a2013-08-27 15:42:35 +0200858 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700859 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700861
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700863 public void run() {
864 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
865 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700868 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700869 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 sBgItemsIdMap.put(item.id, item);
871 switch (item.itemType) {
872 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
873 sBgFolders.put(item.id, (FolderInfo) item);
874 // Fall through
875 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
876 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
877 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
878 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
879 sBgWorkspaceItems.add(item);
880 } else {
881 if (!sBgFolders.containsKey(item.container)) {
882 // Adding an item to a folder that doesn't exist.
883 String msg = "adding item: " + item + " to a folder that " +
884 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700885 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 break;
889 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
890 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
891 break;
892 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700893 }
894 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700895 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700896 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700897 }
898
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700900 * Creates a new unique child id, for a given cell span across all layouts.
901 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700902 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700904 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 }
907
Winson Chungaafa03c2010-06-11 17:34:16 -0700908 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700909 * Removes the specified item from the database
910 * @param context
911 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700915 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700916
Michael Jurka83df1882011-08-31 20:59:26 -0700917 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700918 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700919 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920
921 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700922 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923 switch (item.itemType) {
924 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
925 sBgFolders.remove(item.id);
926 for (ItemInfo info: sBgItemsIdMap.values()) {
927 if (info.container == item.id) {
928 // We are deleting a folder which still contains items that
929 // think they are contained by that folder.
930 String msg = "deleting a folder (" + item + ") which still " +
931 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700932 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700934 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700935 sBgWorkspaceItems.remove(item);
936 break;
937 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
938 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
939 sBgWorkspaceItems.remove(item);
940 break;
941 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
942 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
943 break;
944 }
945 sBgItemsIdMap.remove(item.id);
946 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 }
948 }
Michael Jurka83df1882011-08-31 20:59:26 -0700949 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700950 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 }
952
953 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 * Update the order of the workspace screens in the database. The array list contains
955 * a list of screen ids in the order that they should appear.
956 */
Winson Chungc9168342013-06-26 14:54:55 -0700957 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700958 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 final ContentResolver cr = context.getContentResolver();
960 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
961
962 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700963 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 while (iter.hasNext()) {
965 long id = iter.next();
966 if (id < 0) {
967 iter.remove();
968 }
969 }
970
971 Runnable r = new Runnable() {
972 @Override
973 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 // Clear the table
975 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700976 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 ContentValues[] values = new ContentValues[count];
978 for (int i = 0; i < count; i++) {
979 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700980 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
982 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 values[i] = v;
984 }
985 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700986
Winson Chungba9c37f2013-08-30 14:11:37 -0700987 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700988 sBgWorkspaceScreens.clear();
989 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700990 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 }
992 };
993 runOnWorkerThread(r);
994 }
995
996 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 * Remove the contents of the specified folder from the database
998 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 final ContentResolver cr = context.getContentResolver();
1001
Michael Jurkac9d95c52011-08-29 14:03:34 -07001002 Runnable r = new Runnable() {
1003 public void run() {
1004 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001006 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 sBgItemsIdMap.remove(info.id);
1008 sBgFolders.remove(info.id);
1009 sBgDbIconCache.remove(info);
1010 sBgWorkspaceItems.remove(info);
1011 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1014 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001016 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 for (ItemInfo childInfo : info.contents) {
1018 sBgItemsIdMap.remove(childInfo.id);
1019 sBgDbIconCache.remove(childInfo);
1020 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001021 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 }
1023 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 }
1026
1027 /**
1028 * Set this as the current Launcher activity object for the loader.
1029 */
1030 public void initialize(Callbacks callbacks) {
1031 synchronized (mLock) {
1032 mCallbacks = new WeakReference<Callbacks>(callbacks);
1033 }
1034 }
1035
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1038 * ACTION_PACKAGE_CHANGED.
1039 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001040 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001041 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001042 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001043
Joe Onorato36115782010-06-17 13:28:48 -04001044 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1047 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1048 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1049 final String packageName = intent.getData().getSchemeSpecificPart();
1050 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051
Joe Onorato36115782010-06-17 13:28:48 -04001052 int op = PackageUpdatedTask.OP_NONE;
1053
1054 if (packageName == null || packageName.length() == 0) {
1055 // they sent us a bad intent
1056 return;
1057 }
1058
1059 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1060 op = PackageUpdatedTask.OP_UPDATE;
1061 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1062 if (!replacing) {
1063 op = PackageUpdatedTask.OP_REMOVE;
1064 }
1065 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1066 // later, we will update the package at this time
1067 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1068 if (!replacing) {
1069 op = PackageUpdatedTask.OP_ADD;
1070 } else {
1071 op = PackageUpdatedTask.OP_UPDATE;
1072 }
1073 }
1074
1075 if (op != PackageUpdatedTask.OP_NONE) {
1076 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1077 }
1078
1079 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001080 // First, schedule to add these apps back in.
1081 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1082 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1083 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001084 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001085 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1086 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1087 enqueuePackageUpdated(new PackageUpdatedTask(
1088 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001089 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001090 // If we have changed locale we need to clear out the labels in all apps/workspace.
1091 forceReload();
1092 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1093 // Check if configuration change was an mcc/mnc change which would affect app resources
1094 // and we would need to clear out the labels in all apps/workspace. Same handling as
1095 // above for ACTION_LOCALE_CHANGED
1096 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001097 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001098 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001099 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001100 forceReload();
1101 }
1102 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001103 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001104 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1105 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001106 if (mCallbacks != null) {
1107 Callbacks callbacks = mCallbacks.get();
1108 if (callbacks != null) {
1109 callbacks.bindSearchablesChanged();
1110 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001111 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001112 }
1113 }
1114
Reena Lee93f824a2011-09-23 17:20:28 -07001115 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001116 resetLoadedState(true, true);
1117
Reena Lee93f824a2011-09-23 17:20:28 -07001118 // Do this here because if the launcher activity is running it will be restarted.
1119 // If it's not running startLoaderFromBackground will merely tell it that it needs
1120 // to reload.
1121 startLoaderFromBackground();
1122 }
1123
Winson Chungf0c6ae02012-03-21 16:10:31 -07001124 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1125 synchronized (mLock) {
1126 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1127 // mWorkspaceLoaded to true later
1128 stopLoaderLocked();
1129 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1130 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1131 }
1132 }
1133
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001134 /**
1135 * When the launcher is in the background, it's possible for it to miss paired
1136 * configuration changes. So whenever we trigger the loader from the background
1137 * tell the launcher that it needs to re-run the loader when it comes back instead
1138 * of doing it now.
1139 */
1140 public void startLoaderFromBackground() {
1141 boolean runLoader = false;
1142 if (mCallbacks != null) {
1143 Callbacks callbacks = mCallbacks.get();
1144 if (callbacks != null) {
1145 // Only actually run the loader if they're not paused.
1146 if (!callbacks.setLoadOnResume()) {
1147 runLoader = true;
1148 }
1149 }
1150 }
1151 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001152 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001153 }
Joe Onorato36115782010-06-17 13:28:48 -04001154 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001155
Reena Lee93f824a2011-09-23 17:20:28 -07001156 // If there is already a loader task running, tell it to stop.
1157 // returns true if isLaunching() was true on the old task
1158 private boolean stopLoaderLocked() {
1159 boolean isLaunching = false;
1160 LoaderTask oldTask = mLoaderTask;
1161 if (oldTask != null) {
1162 if (oldTask.isLaunching()) {
1163 isLaunching = true;
1164 }
1165 oldTask.stopLocked();
1166 }
1167 return isLaunching;
1168 }
1169
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001170 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001171 synchronized (mLock) {
1172 if (DEBUG_LOADERS) {
1173 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 // Clear any deferred bind-runnables from the synchronized load process
1177 // We must do this before any loading/binding is scheduled below.
1178 mDeferredBindRunnables.clear();
1179
Joe Onorato36115782010-06-17 13:28:48 -04001180 // Don't bother to start the thread if we know it's not going to do anything
1181 if (mCallbacks != null && mCallbacks.get() != null) {
1182 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001183 // also, don't downgrade isLaunching if we're already running
1184 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001185 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1187 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1188 } else {
1189 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1190 sWorker.post(mLoaderTask);
1191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 }
1194 }
1195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 void bindRemainingSynchronousPages() {
1197 // Post the remaining side pages to be loaded
1198 if (!mDeferredBindRunnables.isEmpty()) {
1199 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001200 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 }
1202 mDeferredBindRunnables.clear();
1203 }
1204 }
1205
Joe Onorato36115782010-06-17 13:28:48 -04001206 public void stopLoader() {
1207 synchronized (mLock) {
1208 if (mLoaderTask != null) {
1209 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 }
1211 }
Joe Onorato36115782010-06-17 13:28:48 -04001212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213
Winson Chung76828c82013-08-19 15:43:29 -07001214 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1215 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1216 final ContentResolver contentResolver = context.getContentResolver();
1217 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1218 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1219 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1220
1221 try {
1222 final int idIndex = sc.getColumnIndexOrThrow(
1223 LauncherSettings.WorkspaceScreens._ID);
1224 final int rankIndex = sc.getColumnIndexOrThrow(
1225 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1226 while (sc.moveToNext()) {
1227 try {
1228 long screenId = sc.getLong(idIndex);
1229 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001230 orderedScreens.put(rank, screenId);
1231 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001232 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001233 }
1234 }
1235 } finally {
1236 sc.close();
1237 }
1238 return orderedScreens;
1239 }
1240
Michael Jurkac57b7a82011-08-09 22:02:20 -07001241 public boolean isAllAppsLoaded() {
1242 return mAllAppsLoaded;
1243 }
1244
Winson Chung36a62fe2012-05-06 18:04:42 -07001245 boolean isLoadingWorkspace() {
1246 synchronized (mLock) {
1247 if (mLoaderTask != null) {
1248 return mLoaderTask.isLoadingWorkspace();
1249 }
1250 }
1251 return false;
1252 }
1253
Joe Onorato36115782010-06-17 13:28:48 -04001254 /**
1255 * Runnable for the thread that loads the contents of the launcher:
1256 * - workspace icons
1257 * - widgets
1258 * - all apps icons
1259 */
1260 private class LoaderTask implements Runnable {
1261 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001262 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001263 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001264 private boolean mStopped;
1265 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001266
Winson Chungc3eecff2011-07-11 17:44:15 -07001267 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001268
1269 LoaderTask(Context context, boolean isLaunching) {
1270 mContext = context;
1271 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001272 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 }
1274
Joe Onorato36115782010-06-17 13:28:48 -04001275 boolean isLaunching() {
1276 return mIsLaunching;
1277 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278
Winson Chung36a62fe2012-05-06 18:04:42 -07001279 boolean isLoadingWorkspace() {
1280 return mIsLoadingAndBindingWorkspace;
1281 }
1282
Winson Chungc763c4e2013-07-19 13:49:06 -07001283 /** Returns whether this is an upgrade path */
1284 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001285 mIsLoadingAndBindingWorkspace = true;
1286
Joe Onorato36115782010-06-17 13:28:48 -04001287 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001288 if (DEBUG_LOADERS) {
1289 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001291
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001293 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001294 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001295 synchronized (LoaderTask.this) {
1296 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001297 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001298 }
1299 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300 }
1301 }
1302
Joe Onorato36115782010-06-17 13:28:48 -04001303 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001304 bindWorkspace(-1, isUpgradePath);
1305 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001306 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001307
Joe Onorato36115782010-06-17 13:28:48 -04001308 private void waitForIdle() {
1309 // Wait until the either we're stopped or the other threads are done.
1310 // This way we don't start loading all apps until the workspace has settled
1311 // down.
1312 synchronized (LoaderTask.this) {
1313 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001314
Joe Onorato36115782010-06-17 13:28:48 -04001315 mHandler.postIdle(new Runnable() {
1316 public void run() {
1317 synchronized (LoaderTask.this) {
1318 mLoadAndBindStepFinished = true;
1319 if (DEBUG_LOADERS) {
1320 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001321 }
Joe Onorato36115782010-06-17 13:28:48 -04001322 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001323 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 });
1326
Michael Jurkac7700af2013-05-14 20:17:58 +02001327 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001328 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001329 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1330 // wait no longer than 1sec at a time
1331 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001332 } catch (InterruptedException ex) {
1333 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001334 }
1335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 if (DEBUG_LOADERS) {
1337 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001338 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001339 + "ms for previous step to finish binding");
1340 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Joe Onorato36115782010-06-17 13:28:48 -04001342 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001343
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001344 void runBindSynchronousPage(int synchronousBindPage) {
1345 if (synchronousBindPage < 0) {
1346 // Ensure that we have a valid page index to load synchronously
1347 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1348 "valid page index");
1349 }
1350 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1351 // Ensure that we don't try and bind a specified page when the pages have not been
1352 // loaded already (we should load everything asynchronously in that case)
1353 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1354 }
1355 synchronized (mLock) {
1356 if (mIsLoaderTaskRunning) {
1357 // Ensure that we are never running the background loading at this point since
1358 // we also touch the background collections
1359 throw new RuntimeException("Error! Background loading is already running");
1360 }
1361 }
1362
1363 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1364 // data structures, we can't allow any other thread to touch that data, but because
1365 // this call is synchronous, we can get away with not locking).
1366
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001367 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001368 // operations from the previous activity. We need to ensure that all queued operations
1369 // are executed before any synchronous binding work is done.
1370 mHandler.flush();
1371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372 // Divide the set of loaded items into those that we are binding synchronously, and
1373 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001374 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1376 // arise from that.
1377 onlyBindAllApps();
1378 }
1379
Joe Onorato36115782010-06-17 13:28:48 -04001380 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001381 boolean isUpgrade = false;
1382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 synchronized (mLock) {
1384 mIsLoaderTaskRunning = true;
1385 }
Joe Onorato36115782010-06-17 13:28:48 -04001386 // Optimize for end-user experience: if the Launcher is up and // running with the
1387 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1388 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001389 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 // Elevate priority when Home launches for the first time to avoid
1391 // starving at boot time. Staring at a blank home is not cool.
1392 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001393 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1394 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001395 android.os.Process.setThreadPriority(mIsLaunching
1396 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1397 }
Winson Chung64359a52013-07-08 17:17:08 -07001398 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001399 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001400
Joe Onorato36115782010-06-17 13:28:48 -04001401 if (mStopped) {
1402 break keep_running;
1403 }
1404
1405 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1406 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 synchronized (mLock) {
1408 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001409 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1411 }
1412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001414
1415 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001416 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1417 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001418
1419 // Restore the default thread priority after we are done loading items
1420 synchronized (mLock) {
1421 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1422 }
Joe Onorato36115782010-06-17 13:28:48 -04001423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424
Winson Chungaac01e12011-08-17 10:37:13 -07001425 // Update the saved icons if necessary
1426 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001427 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001428 for (Object key : sBgDbIconCache.keySet()) {
1429 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1430 }
1431 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001432 }
Winson Chungaac01e12011-08-17 10:37:13 -07001433
Winson Chungc58497e2013-09-03 17:48:37 -07001434 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1435 // Ensure that all the applications that are in the system are
1436 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001437 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001438 verifyApplications();
1439 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001440 }
1441
Joe Onorato36115782010-06-17 13:28:48 -04001442 // Clear out this reference, otherwise we end up holding it until all of the
1443 // callback runnables are done.
1444 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445
Joe Onorato36115782010-06-17 13:28:48 -04001446 synchronized (mLock) {
1447 // If we are still the last one to be scheduled, remove ourselves.
1448 if (mLoaderTask == this) {
1449 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 }
1454
1455 public void stopLocked() {
1456 synchronized (LoaderTask.this) {
1457 mStopped = true;
1458 this.notify();
1459 }
1460 }
1461
1462 /**
1463 * Gets the callbacks object. If we've been stopped, or if the launcher object
1464 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1465 * object that was around when the deferred message was scheduled, and if there's
1466 * a new Callbacks object around then also return null. This will save us from
1467 * calling onto it with data that will be ignored.
1468 */
1469 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1470 synchronized (mLock) {
1471 if (mStopped) {
1472 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001473 }
Joe Onorato36115782010-06-17 13:28:48 -04001474
1475 if (mCallbacks == null) {
1476 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478
1479 final Callbacks callbacks = mCallbacks.get();
1480 if (callbacks != oldCallbacks) {
1481 return null;
1482 }
1483 if (callbacks == null) {
1484 Log.w(TAG, "no mCallbacks");
1485 return null;
1486 }
1487
1488 return callbacks;
1489 }
1490 }
1491
Winson Chungc763c4e2013-07-19 13:49:06 -07001492 private void verifyApplications() {
1493 final Context context = mApp.getContext();
1494
1495 // Cross reference all the applications in our apps list with items in the workspace
1496 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001497 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001498 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001499 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001500 tmpInfos = getItemInfoForComponentName(app.componentName);
1501 if (tmpInfos.isEmpty()) {
1502 // We are missing an application icon, so add this to the workspace
1503 added.add(app);
1504 // This is a rare event, so lets log it
1505 Log.e(TAG, "Missing Application on load: " + app);
1506 }
1507 }
1508 }
1509 if (!added.isEmpty()) {
1510 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001511 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001512 }
1513 }
1514
Winson Chung5f8afe62013-08-12 16:19:28 -07001515 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001516 LauncherAppState app = LauncherAppState.getInstance();
1517 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1518 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1519 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001520 }
1521
Joe Onorato36115782010-06-17 13:28:48 -04001522 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001523 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1524 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001525 LauncherAppState app = LauncherAppState.getInstance();
1526 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1527 int countX = (int) grid.numColumns;
1528 int countY = (int) grid.numRows;
1529
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001531 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001532 // Return early if we detect that an item is under the hotseat button
1533 if (mCallbacks == null ||
1534 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1535 deleteOnItemOverlap.set(true);
1536 return false;
1537 }
1538
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1540 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1541 [(int) item.screenId][0] != null) {
1542 Log.e(TAG, "Error loading shortcut into hotseat " + item
1543 + " into position (" + item.screenId + ":" + item.cellX + ","
1544 + item.cellY + ") occupied by "
1545 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1546 [(int) item.screenId][0]);
1547 return false;
1548 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001549 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001550 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 items[(int) item.screenId][0] = item;
1552 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001553 return true;
1554 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001555 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1556 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001557 return true;
1558 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001561 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 occupied.put(item.screenId, items);
1563 }
1564
1565 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001566 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001567 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1568 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001570 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001572 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001573 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001575 return false;
1576 }
1577 }
1578 }
1579 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1580 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001582 }
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Joe Onorato36115782010-06-17 13:28:48 -04001585 return true;
1586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587
Winson Chungba9c37f2013-08-30 14:11:37 -07001588 /** Clears all the sBg data structures */
1589 private void clearSBgDataStructures() {
1590 synchronized (sBgLock) {
1591 sBgWorkspaceItems.clear();
1592 sBgAppWidgets.clear();
1593 sBgFolders.clear();
1594 sBgItemsIdMap.clear();
1595 sBgDbIconCache.clear();
1596 sBgWorkspaceScreens.clear();
1597 }
1598 }
1599
Winson Chungc763c4e2013-07-19 13:49:06 -07001600 /** Returns whether this is an upgradge path */
1601 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001602 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603
Joe Onorato36115782010-06-17 13:28:48 -04001604 final Context context = mContext;
1605 final ContentResolver contentResolver = context.getContentResolver();
1606 final PackageManager manager = context.getPackageManager();
1607 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1608 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001609
Winson Chung892c74d2013-08-22 16:15:50 -07001610 LauncherAppState app = LauncherAppState.getInstance();
1611 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1612 int countX = (int) grid.numColumns;
1613 int countY = (int) grid.numRows;
1614
Michael Jurkab85f8a42012-04-25 15:48:32 -07001615 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001616 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001617
Winson Chungc763c4e2013-07-19 13:49:06 -07001618 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001619 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001620
Winson Chung2abf94d2012-07-18 18:16:38 -07001621 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001622 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001623
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001624 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001626 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001627 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001628
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001629 // +1 for the hotseat (it can be larger than the workspace)
1630 // Load workspace in reverse order to ensure that latest items are loaded first (and
1631 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001632 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001634 try {
1635 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1636 final int intentIndex = c.getColumnIndexOrThrow
1637 (LauncherSettings.Favorites.INTENT);
1638 final int titleIndex = c.getColumnIndexOrThrow
1639 (LauncherSettings.Favorites.TITLE);
1640 final int iconTypeIndex = c.getColumnIndexOrThrow(
1641 LauncherSettings.Favorites.ICON_TYPE);
1642 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1643 final int iconPackageIndex = c.getColumnIndexOrThrow(
1644 LauncherSettings.Favorites.ICON_PACKAGE);
1645 final int iconResourceIndex = c.getColumnIndexOrThrow(
1646 LauncherSettings.Favorites.ICON_RESOURCE);
1647 final int containerIndex = c.getColumnIndexOrThrow(
1648 LauncherSettings.Favorites.CONTAINER);
1649 final int itemTypeIndex = c.getColumnIndexOrThrow(
1650 LauncherSettings.Favorites.ITEM_TYPE);
1651 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1652 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001653 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1654 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001655 final int screenIndex = c.getColumnIndexOrThrow(
1656 LauncherSettings.Favorites.SCREEN);
1657 final int cellXIndex = c.getColumnIndexOrThrow
1658 (LauncherSettings.Favorites.CELLX);
1659 final int cellYIndex = c.getColumnIndexOrThrow
1660 (LauncherSettings.Favorites.CELLY);
1661 final int spanXIndex = c.getColumnIndexOrThrow
1662 (LauncherSettings.Favorites.SPANX);
1663 final int spanYIndex = c.getColumnIndexOrThrow(
1664 LauncherSettings.Favorites.SPANY);
1665 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1666 //final int displayModeIndex = c.getColumnIndexOrThrow(
1667 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 ShortcutInfo info;
1670 String intentDescription;
1671 LauncherAppWidgetInfo appWidgetInfo;
1672 int container;
1673 long id;
1674 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001675
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001676 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001677 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 try {
1679 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 switch (itemType) {
1682 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1683 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001684 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001685 intentDescription = c.getString(intentIndex);
1686 try {
1687 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001688 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001689 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001690 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001691 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001692 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1693 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001694 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001695 // If apps can be on external storage, then we just
1696 // leave them for the user to remove (maybe add
1697 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001698 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001699 }
1700 continue;
1701 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001703 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 continue;
1705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1708 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1709 titleIndex, mLabelCache);
1710 } else {
1711 info = getShortcutInfo(c, context, iconTypeIndex,
1712 iconPackageIndex, iconResourceIndex, iconIndex,
1713 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001714
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 // App shortcuts that used to be automatically added to Launcher
1716 // didn't always have the correct intent flags set, so do that
1717 // here
1718 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001719 intent.getCategories() != null &&
1720 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001721 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 intent.addFlags(
1723 Intent.FLAG_ACTIVITY_NEW_TASK |
1724 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1725 }
Michael Jurka96879562012-03-22 05:54:33 -07001726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001729 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 container = c.getInt(containerIndex);
1732 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001733 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734 info.cellX = c.getInt(cellXIndex);
1735 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001736 info.spanX = 1;
1737 info.spanY = 1;
1738 // Skip loading items that are out of bounds
1739 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1740 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001741 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1742 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001743 continue;
1744 }
1745 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001747 deleteOnItemOverlap.set(false);
1748 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1749 if (deleteOnItemOverlap.get()) {
1750 itemsToRemove.add(id);
1751 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 break;
1753 }
1754
1755 switch (container) {
1756 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1757 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1758 sBgWorkspaceItems.add(info);
1759 break;
1760 default:
1761 // Item is in a user folder
1762 FolderInfo folderInfo =
1763 findOrMakeFolder(sBgFolders, container);
1764 folderInfo.add(info);
1765 break;
1766 }
1767 sBgItemsIdMap.put(info.id, info);
1768
1769 // now that we've loaded everthing re-save it with the
1770 // icon in case it disappears somehow.
1771 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001772 } else {
1773 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 }
1775 break;
1776
1777 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1778 id = c.getLong(idIndex);
1779 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1780
1781 folderInfo.title = c.getString(titleIndex);
1782 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001784 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001785 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 folderInfo.cellX = c.getInt(cellXIndex);
1787 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001788 folderInfo.spanX = 1;
1789 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790
Winson Chung5f8afe62013-08-12 16:19:28 -07001791 // Skip loading items that are out of bounds
1792 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001793 if (checkItemDimensions(folderInfo)) {
1794 Log.d(TAG, "Skipped loading out of bounds folder");
1795 continue;
1796 }
1797 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001798 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001799 deleteOnItemOverlap.set(false);
1800 if (!checkItemPlacement(occupied, folderInfo,
1801 deleteOnItemOverlap)) {
1802 if (deleteOnItemOverlap.get()) {
1803 itemsToRemove.add(id);
1804 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001805 break;
1806 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001807
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1810 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1811 sBgWorkspaceItems.add(folderInfo);
1812 break;
Joe Onorato36115782010-06-17 13:28:48 -04001813 }
Joe Onorato17a89222011-02-08 17:26:11 -08001814
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1816 sBgFolders.put(folderInfo.id, folderInfo);
1817 break;
1818
1819 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1820 // Read all Launcher-specific widget details
1821 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001822 String savedProvider = c.getString(appWidgetProviderIndex);
1823
Joe Onorato36115782010-06-17 13:28:48 -04001824 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 final AppWidgetProviderInfo provider =
1827 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 if (!isSafeMode && (provider == null || provider.provider == null ||
1830 provider.provider.getPackageName() == null)) {
1831 String log = "Deleting widget that isn't installed anymore: id="
1832 + id + " appWidgetId=" + appWidgetId;
1833 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001834 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001835 itemsToRemove.add(id);
1836 } else {
1837 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1838 provider.provider);
1839 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001840 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 appWidgetInfo.cellX = c.getInt(cellXIndex);
1842 appWidgetInfo.cellY = c.getInt(cellYIndex);
1843 appWidgetInfo.spanX = c.getInt(spanXIndex);
1844 appWidgetInfo.spanY = c.getInt(spanYIndex);
1845 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1846 appWidgetInfo.minSpanX = minSpan[0];
1847 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 container = c.getInt(containerIndex);
1850 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1851 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1852 Log.e(TAG, "Widget found where container != " +
1853 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1854 continue;
1855 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856
Adam Cohene25af792013-06-06 23:08:25 -07001857 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001858 // Skip loading items that are out of bounds
1859 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1860 if (checkItemDimensions(appWidgetInfo)) {
1861 Log.d(TAG, "Skipped loading out of bounds app widget");
1862 continue;
1863 }
1864 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001866 deleteOnItemOverlap.set(false);
1867 if (!checkItemPlacement(occupied, appWidgetInfo,
1868 deleteOnItemOverlap)) {
1869 if (deleteOnItemOverlap.get()) {
1870 itemsToRemove.add(id);
1871 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 break;
1873 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001874 String providerName = provider.provider.flattenToString();
1875 if (!providerName.equals(savedProvider)) {
1876 ContentValues values = new ContentValues();
1877 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1878 providerName);
1879 String where = BaseColumns._ID + "= ?";
1880 String[] args = {Integer.toString(c.getInt(idIndex))};
1881 contentResolver.update(contentUri, values, where, args);
1882 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1884 sBgAppWidgets.add(appWidgetInfo);
1885 }
Joe Onorato36115782010-06-17 13:28:48 -04001886 break;
1887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001889 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 }
1892 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001893 if (c != null) {
1894 c.close();
1895 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896 }
1897
Winson Chungba9c37f2013-08-30 14:11:37 -07001898 // Break early if we've stopped loading
1899 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001900 clearSBgDataStructures();
1901 return false;
1902 }
1903
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 if (itemsToRemove.size() > 0) {
1905 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001906 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 // Remove dead items
1908 for (long id : itemsToRemove) {
1909 if (DEBUG_LOADERS) {
1910 Log.d(TAG, "Removed id = " + id);
1911 }
1912 // Don't notify content observers
1913 try {
1914 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1915 null, null);
1916 } catch (RemoteException e) {
1917 Log.w(TAG, "Could not remove id = " + id);
1918 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001919 }
1920 }
1921
Winson Chungc763c4e2013-07-19 13:49:06 -07001922 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001923 long maxScreenId = 0;
1924 // If we're importing we use the old screen order.
1925 for (ItemInfo item: sBgItemsIdMap.values()) {
1926 long screenId = item.screenId;
1927 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1928 !sBgWorkspaceScreens.contains(screenId)) {
1929 sBgWorkspaceScreens.add(screenId);
1930 if (screenId > maxScreenId) {
1931 maxScreenId = screenId;
1932 }
1933 }
1934 }
1935 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001936
Michael Jurka414300a2013-08-27 15:42:35 +02001937 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001939
1940 // Update the max item id after we load an old db
1941 long maxItemId = 0;
1942 // If we're importing we use the old screen order.
1943 for (ItemInfo item: sBgItemsIdMap.values()) {
1944 maxItemId = Math.max(maxItemId, item.id);
1945 }
Michael Jurka414300a2013-08-27 15:42:35 +02001946 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001948 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1949 for (Integer i : orderedScreens.keySet()) {
1950 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001951 }
1952
1953 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001954 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 for (ItemInfo item: sBgItemsIdMap.values()) {
1956 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1958 unusedScreens.contains(screenId)) {
1959 unusedScreens.remove(screenId);
1960 }
1961 }
1962
1963 // If there are any empty screens remove them, and update.
1964 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001965 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1967 }
1968 }
1969
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001970 if (DEBUG_LOADERS) {
1971 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1972 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001974 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001975 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001976
Daniel Sandler566da102013-06-25 23:43:45 -04001977 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001978 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001979 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001980 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001981 line += " | ";
1982 }
Winson Chung892c74d2013-08-22 16:15:50 -07001983 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001985 }
Joe Onorato36115782010-06-17 13:28:48 -04001986 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 }
Joe Onorato36115782010-06-17 13:28:48 -04001990 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001991 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001992 }
1993
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001994 /** Filters the set of items who are directly or indirectly (via another container) on the
1995 * specified screen. */
1996 private void filterCurrentWorkspaceItems(int currentScreen,
1997 ArrayList<ItemInfo> allWorkspaceItems,
1998 ArrayList<ItemInfo> currentScreenItems,
1999 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002000 // Purge any null ItemInfos
2001 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2002 while (iter.hasNext()) {
2003 ItemInfo i = iter.next();
2004 if (i == null) {
2005 iter.remove();
2006 }
2007 }
2008
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002009 // If we aren't filtering on a screen, then the set of items to load is the full set of
2010 // items given.
2011 if (currentScreen < 0) {
2012 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002013 }
2014
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002015 // Order the set of items by their containers first, this allows use to walk through the
2016 // list sequentially, build up a list of containers that are in the specified screen,
2017 // as well as all items in those containers.
2018 Set<Long> itemsOnScreen = new HashSet<Long>();
2019 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2020 @Override
2021 public int compare(ItemInfo lhs, ItemInfo rhs) {
2022 return (int) (lhs.container - rhs.container);
2023 }
2024 });
2025 for (ItemInfo info : allWorkspaceItems) {
2026 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002028 currentScreenItems.add(info);
2029 itemsOnScreen.add(info.id);
2030 } else {
2031 otherScreenItems.add(info);
2032 }
2033 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2034 currentScreenItems.add(info);
2035 itemsOnScreen.add(info.id);
2036 } else {
2037 if (itemsOnScreen.contains(info.container)) {
2038 currentScreenItems.add(info);
2039 itemsOnScreen.add(info.id);
2040 } else {
2041 otherScreenItems.add(info);
2042 }
2043 }
2044 }
2045 }
2046
2047 /** Filters the set of widgets which are on the specified screen. */
2048 private void filterCurrentAppWidgets(int currentScreen,
2049 ArrayList<LauncherAppWidgetInfo> appWidgets,
2050 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2051 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2052 // If we aren't filtering on a screen, then the set of items to load is the full set of
2053 // widgets given.
2054 if (currentScreen < 0) {
2055 currentScreenWidgets.addAll(appWidgets);
2056 }
2057
2058 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002059 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 currentScreenWidgets.add(widget);
2063 } else {
2064 otherScreenWidgets.add(widget);
2065 }
2066 }
2067 }
2068
2069 /** Filters the set of folders which are on the specified screen. */
2070 private void filterCurrentFolders(int currentScreen,
2071 HashMap<Long, ItemInfo> itemsIdMap,
2072 HashMap<Long, FolderInfo> folders,
2073 HashMap<Long, FolderInfo> currentScreenFolders,
2074 HashMap<Long, FolderInfo> otherScreenFolders) {
2075 // If we aren't filtering on a screen, then the set of items to load is the full set of
2076 // widgets given.
2077 if (currentScreen < 0) {
2078 currentScreenFolders.putAll(folders);
2079 }
2080
2081 for (long id : folders.keySet()) {
2082 ItemInfo info = itemsIdMap.get(id);
2083 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002084 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 currentScreenFolders.put(id, folder);
2088 } else {
2089 otherScreenFolders.put(id, folder);
2090 }
2091 }
2092 }
2093
2094 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2095 * right) */
2096 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002097 final LauncherAppState app = LauncherAppState.getInstance();
2098 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 // XXX: review this
2100 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002101 @Override
2102 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002103 int cellCountX = (int) grid.numColumns;
2104 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002105 int screenOffset = cellCountX * cellCountY;
2106 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002107 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002108 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002110 rhs.cellY * cellCountX + rhs.cellX);
2111 return (int) (lr - rr);
2112 }
2113 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002115
Adam Cohendcd297f2013-06-18 13:13:40 -07002116 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2117 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 final Runnable r = new Runnable() {
2119 @Override
2120 public void run() {
2121 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2122 if (callbacks != null) {
2123 callbacks.bindScreens(orderedScreens);
2124 }
2125 }
2126 };
2127 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2128 }
2129
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2131 final ArrayList<ItemInfo> workspaceItems,
2132 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2133 final HashMap<Long, FolderInfo> folders,
2134 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002135
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 final boolean postOnMainThread = (deferredBindRunnables != null);
2137
2138 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002139 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002141 final int start = i;
2142 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 final Runnable r = new Runnable() {
2144 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002145 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002146 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002148 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2149 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 }
2151 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002152 };
2153 if (postOnMainThread) {
2154 deferredBindRunnables.add(r);
2155 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002156 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 }
Joe Onorato36115782010-06-17 13:28:48 -04002158 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159
2160 // Bind the folders
2161 if (!folders.isEmpty()) {
2162 final Runnable r = new Runnable() {
2163 public void run() {
2164 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2165 if (callbacks != null) {
2166 callbacks.bindFolders(folders);
2167 }
2168 }
2169 };
2170 if (postOnMainThread) {
2171 deferredBindRunnables.add(r);
2172 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002173 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002174 }
2175 }
2176
2177 // Bind the widgets, one at a time
2178 N = appWidgets.size();
2179 for (int i = 0; i < N; i++) {
2180 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2181 final Runnable r = new Runnable() {
2182 public void run() {
2183 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2184 if (callbacks != null) {
2185 callbacks.bindAppWidget(widget);
2186 }
2187 }
2188 };
2189 if (postOnMainThread) {
2190 deferredBindRunnables.add(r);
2191 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002192 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 }
2194 }
2195 }
2196
2197 /**
2198 * Binds all loaded data to actual views on the main thread.
2199 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002200 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 final long t = SystemClock.uptimeMillis();
2202 Runnable r;
2203
2204 // Don't use these two variables in any of the callback runnables.
2205 // Otherwise we hold a reference to them.
2206 final Callbacks oldCallbacks = mCallbacks.get();
2207 if (oldCallbacks == null) {
2208 // This launcher has exited and nobody bothered to tell us. Just bail.
2209 Log.w(TAG, "LoaderTask running with no launcher");
2210 return;
2211 }
2212
Winson Chung4a2afa32012-07-19 14:53:05 -07002213 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2214 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 oldCallbacks.getCurrentWorkspaceScreen();
2216
2217 // Load all the items that are on the current page first (and in the process, unbind
2218 // all the existing workspace items before we call startBinding() below.
2219 unbindWorkspaceItemsOnMainThread();
2220 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2221 ArrayList<LauncherAppWidgetInfo> appWidgets =
2222 new ArrayList<LauncherAppWidgetInfo>();
2223 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2224 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002226 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002227 workspaceItems.addAll(sBgWorkspaceItems);
2228 appWidgets.addAll(sBgAppWidgets);
2229 folders.putAll(sBgFolders);
2230 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002231 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 }
2233
2234 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2235 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2236 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2237 new ArrayList<LauncherAppWidgetInfo>();
2238 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2239 new ArrayList<LauncherAppWidgetInfo>();
2240 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2241 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2242
2243 // Separate the items that are on the current screen, and all the other remaining items
2244 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2245 otherWorkspaceItems);
2246 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2247 otherAppWidgets);
2248 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2249 otherFolders);
2250 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2251 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2252
2253 // Tell the workspace that we're about to start binding items
2254 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002255 public void run() {
2256 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2257 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002259 }
2260 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 };
Winson Chung81b52252012-08-27 15:34:29 -07002262 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2265
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 // Load items on the current page
2267 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2268 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002269 if (isLoadingSynchronously) {
2270 r = new Runnable() {
2271 public void run() {
2272 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2273 if (callbacks != null) {
2274 callbacks.onPageBoundSynchronously(currentScreen);
2275 }
2276 }
2277 };
Winson Chung81b52252012-08-27 15:34:29 -07002278 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002279 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280
Winson Chung4a2afa32012-07-19 14:53:05 -07002281 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2282 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283 mDeferredBindRunnables.clear();
2284 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002285 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286
2287 // Tell the workspace that we're done binding items
2288 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002289 public void run() {
2290 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2291 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002292 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002293 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294
Winson Chung98e030b2012-05-07 16:01:11 -07002295 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002296 if (DEBUG_LOADERS) {
2297 Log.d(TAG, "bound workspace in "
2298 + (SystemClock.uptimeMillis()-t) + "ms");
2299 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002300
2301 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002302 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002304 if (isLoadingSynchronously) {
2305 mDeferredBindRunnables.add(r);
2306 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002307 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002308 }
Joe Onorato36115782010-06-17 13:28:48 -04002309 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002310
Joe Onorato36115782010-06-17 13:28:48 -04002311 private void loadAndBindAllApps() {
2312 if (DEBUG_LOADERS) {
2313 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2314 }
2315 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002316 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002317 synchronized (LoaderTask.this) {
2318 if (mStopped) {
2319 return;
2320 }
2321 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002322 }
Joe Onorato36115782010-06-17 13:28:48 -04002323 } else {
2324 onlyBindAllApps();
2325 }
2326 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002327
Joe Onorato36115782010-06-17 13:28:48 -04002328 private void onlyBindAllApps() {
2329 final Callbacks oldCallbacks = mCallbacks.get();
2330 if (oldCallbacks == null) {
2331 // This launcher has exited and nobody bothered to tell us. Just bail.
2332 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2333 return;
2334 }
2335
2336 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002337 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002338 final ArrayList<AppInfo> list
2339 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002340 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002341 public void run() {
2342 final long t = SystemClock.uptimeMillis();
2343 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2344 if (callbacks != null) {
2345 callbacks.bindAllApplications(list);
2346 }
2347 if (DEBUG_LOADERS) {
2348 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2349 + (SystemClock.uptimeMillis()-t) + "ms");
2350 }
2351 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002352 };
2353 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002354 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002355 r.run();
2356 } else {
2357 mHandler.post(r);
2358 }
Joe Onorato36115782010-06-17 13:28:48 -04002359 }
2360
Winson Chung64359a52013-07-08 17:17:08 -07002361 private void loadAllApps() {
2362 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002363
Joe Onorato36115782010-06-17 13:28:48 -04002364 final Callbacks oldCallbacks = mCallbacks.get();
2365 if (oldCallbacks == null) {
2366 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002367 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002368 return;
2369 }
2370
Winson Chung64359a52013-07-08 17:17:08 -07002371 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002372 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2373 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2374
Winson Chung64359a52013-07-08 17:17:08 -07002375 // Clear the list of apps
2376 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002377
Winson Chung64359a52013-07-08 17:17:08 -07002378 // Query for the set of apps
2379 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2380 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2381 if (DEBUG_LOADERS) {
2382 Log.d(TAG, "queryIntentActivities took "
2383 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2384 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2385 }
2386 // Fail if we don't have any apps
2387 if (apps == null || apps.isEmpty()) {
2388 return;
2389 }
2390 // Sort the applications by name
2391 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2392 Collections.sort(apps,
2393 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2394 if (DEBUG_LOADERS) {
2395 Log.d(TAG, "sort took "
2396 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002397 }
2398
Winson Chung64359a52013-07-08 17:17:08 -07002399 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002400 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002401 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002402 // This builds the icon bitmaps.
2403 mBgAllAppsList.add(new AppInfo(packageManager, app,
2404 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002405 }
2406
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002407 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002408 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2409 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002410
2411 // Post callback on main thread
2412 mHandler.post(new Runnable() {
2413 public void run() {
2414 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002415 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002416 if (callbacks != null) {
2417 callbacks.bindAllApplications(added);
2418 if (DEBUG_LOADERS) {
2419 Log.d(TAG, "bound " + added.size() + " apps in "
2420 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2421 }
2422 } else {
2423 Log.i(TAG, "not binding apps: no Launcher activity");
2424 }
2425 }
2426 });
2427
Joe Onorato36115782010-06-17 13:28:48 -04002428 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002429 Log.d(TAG, "Icons processed in "
2430 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002431 }
2432 }
2433
2434 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002435 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002436 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2437 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2438 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2439 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2440 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2441 }
Joe Onorato36115782010-06-17 13:28:48 -04002442 }
2443 }
2444
2445 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002446 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002447 }
2448
2449 private class PackageUpdatedTask implements Runnable {
2450 int mOp;
2451 String[] mPackages;
2452
2453 public static final int OP_NONE = 0;
2454 public static final int OP_ADD = 1;
2455 public static final int OP_UPDATE = 2;
2456 public static final int OP_REMOVE = 3; // uninstlled
2457 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2458
2459
2460 public PackageUpdatedTask(int op, String[] packages) {
2461 mOp = op;
2462 mPackages = packages;
2463 }
2464
2465 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002466 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002467
2468 final String[] packages = mPackages;
2469 final int N = packages.length;
2470 switch (mOp) {
2471 case OP_ADD:
2472 for (int i=0; i<N; i++) {
2473 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002474 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002475 }
2476 break;
2477 case OP_UPDATE:
2478 for (int i=0; i<N; i++) {
2479 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002480 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002481 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002482 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002483 }
2484 break;
2485 case OP_REMOVE:
2486 case OP_UNAVAILABLE:
2487 for (int i=0; i<N; i++) {
2488 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002489 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002490 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002491 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
2493 break;
2494 }
2495
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002496 ArrayList<AppInfo> added = null;
2497 ArrayList<AppInfo> modified = null;
2498 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002499
Adam Cohen487f7dd2012-06-28 18:12:10 -07002500 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002501 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002502 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002503 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002504 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002505 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002506 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002507 }
Winson Chung5d55f332012-07-16 20:45:03 -07002508 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002509 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002510 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002511 }
2512
Joe Onorato36115782010-06-17 13:28:48 -04002513 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2514 if (callbacks == null) {
2515 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2516 return;
2517 }
2518
2519 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002520 // Ensure that we add all the workspace applications to the db
2521 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002522 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2523 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2524 } else {
2525 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2526 addAndBindAddedApps(context, addedInfos, cb, added);
2527 }
Joe Onorato36115782010-06-17 13:28:48 -04002528 }
2529 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002530 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002531
2532 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002533 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002534 ArrayList<ItemInfo> infos =
2535 getItemInfoForComponentName(a.componentName);
2536 for (ItemInfo i : infos) {
2537 if (isShortcutInfoUpdateable(i)) {
2538 ShortcutInfo info = (ShortcutInfo) i;
2539 info.title = a.title.toString();
2540 updateItemInDatabase(context, info);
2541 }
2542 }
2543 }
2544
Joe Onorato36115782010-06-17 13:28:48 -04002545 mHandler.post(new Runnable() {
2546 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002547 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2548 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002549 callbacks.bindAppsUpdated(modifiedFinal);
2550 }
2551 }
2552 });
2553 }
Winson Chung83892cc2013-05-01 16:53:33 -07002554
Winson Chungdf95eb12013-10-16 14:57:07 -07002555 final ArrayList<String> removedPackageNames =
2556 new ArrayList<String>();
2557 if (mOp == OP_REMOVE) {
2558 // Mark all packages in the broadcast to be removed
2559 removedPackageNames.addAll(Arrays.asList(packages));
2560 } else if (mOp == OP_UPDATE) {
2561 // Mark disabled packages in the broadcast to be removed
2562 final PackageManager pm = context.getPackageManager();
2563 for (int i=0; i<N; i++) {
2564 if (isPackageDisabled(pm, packages[i])) {
2565 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002566 }
2567 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002568 }
2569 // Remove all the components associated with this package
2570 for (String pn : removedPackageNames) {
2571 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2572 for (ItemInfo i : infos) {
2573 deleteItemFromDatabase(context, i);
2574 }
2575 }
2576 // Remove all the specific components
2577 for (AppInfo a : removedApps) {
2578 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2579 for (ItemInfo i : infos) {
2580 deleteItemFromDatabase(context, i);
2581 }
2582 }
2583 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2584 // Remove any queued items from the install queue
2585 String spKey = LauncherAppState.getSharedPreferencesKey();
2586 SharedPreferences sp =
2587 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2588 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2589 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002590 mHandler.post(new Runnable() {
2591 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002592 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2593 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002594 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002595 }
2596 }
2597 });
Joe Onoratobe386092009-11-17 17:32:16 -08002598 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002599
Michael Jurkac402cd92013-05-20 15:49:32 +02002600 final ArrayList<Object> widgetsAndShortcuts =
2601 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002602 mHandler.post(new Runnable() {
2603 @Override
2604 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002605 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2606 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002607 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002608 }
2609 }
2610 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002611
2612 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002613 mHandler.post(new Runnable() {
2614 public void run() {
2615 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2616 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002617 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002618 }
2619 }
2620 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002621 }
2622 }
2623
Michael Jurkac402cd92013-05-20 15:49:32 +02002624 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2625 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2626 PackageManager packageManager = context.getPackageManager();
2627 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2628 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2629 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2630 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2631 Collections.sort(widgetsAndShortcuts,
2632 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2633 return widgetsAndShortcuts;
2634 }
2635
Winson Chungdf95eb12013-10-16 14:57:07 -07002636 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2637 try {
2638 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2639 return !pi.applicationInfo.enabled;
2640 } catch (NameNotFoundException e) {
2641 // Fall through
2642 }
2643 return false;
2644 }
Winson Chung1323b482013-08-05 12:41:55 -07002645 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002646 if (cn == null) {
2647 return false;
2648 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002649 if (isPackageDisabled(pm, cn.getPackageName())) {
2650 return false;
2651 }
Winson Chungee055712013-07-30 14:46:24 -07002652
2653 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002654 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002655 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002656 return (pm.getActivityInfo(cn, 0) != null);
2657 } catch (NameNotFoundException e) {
2658 return false;
2659 }
2660 }
2661
Joe Onorato9c1289c2009-08-17 11:03:03 -04002662 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002663 * This is called from the code that adds shortcuts from the intent receiver. This
2664 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002665 */
Joe Onorato56d82912010-03-07 14:32:10 -05002666 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002667 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002668 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002669
Joe Onorato56d82912010-03-07 14:32:10 -05002670 /**
2671 * Make an ShortcutInfo object for a shortcut that is an application.
2672 *
2673 * If c is not null, then it will be used to fill in missing data like the title and icon.
2674 */
2675 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002676 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002677 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002678 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002679 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002680 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002681 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002682 } else {
2683 try {
2684 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2685 info.initFlagsAndFirstInstallTime(pi);
2686 } catch (NameNotFoundException e) {
2687 Log.d(TAG, "getPackInfo failed for package " +
2688 componentName.getPackageName());
2689 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002690 }
2691
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002692 // TODO: See if the PackageManager knows about this case. If it doesn't
2693 // then return null & delete this.
2694
Joe Onorato56d82912010-03-07 14:32:10 -05002695 // the resource -- This may implicitly give us back the fallback icon,
2696 // but don't worry about that. All we're doing with usingFallbackIcon is
2697 // to avoid saving lots of copies of that in the database, and most apps
2698 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002699
2700 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2701 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2702 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002703 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002704 ResolveInfo resolveInfo = null;
2705 ComponentName oldComponent = intent.getComponent();
2706 Intent newIntent = new Intent(intent.getAction(), null);
2707 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2708 newIntent.setPackage(oldComponent.getPackageName());
2709 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2710 for (ResolveInfo i : infos) {
2711 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2712 i.activityInfo.name);
2713 if (cn.equals(oldComponent)) {
2714 resolveInfo = i;
2715 }
2716 }
2717 if (resolveInfo == null) {
2718 resolveInfo = manager.resolveActivity(intent, 0);
2719 }
Joe Onorato56d82912010-03-07 14:32:10 -05002720 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002721 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002722 }
Joe Onorato56d82912010-03-07 14:32:10 -05002723 // the db
2724 if (icon == null) {
2725 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002726 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002727 }
2728 }
2729 // the fallback icon
2730 if (icon == null) {
2731 icon = getFallbackIcon();
2732 info.usingFallbackIcon = true;
2733 }
2734 info.setIcon(icon);
2735
2736 // from the resource
2737 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002738 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2739 if (labelCache != null && labelCache.containsKey(key)) {
2740 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002741 } else {
2742 info.title = resolveInfo.activityInfo.loadLabel(manager);
2743 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002744 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002745 }
2746 }
Joe Onorato56d82912010-03-07 14:32:10 -05002747 }
2748 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002749 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002750 if (c != null) {
2751 info.title = c.getString(titleIndex);
2752 }
2753 }
2754 // fall back to the class name of the activity
2755 if (info.title == null) {
2756 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2759 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002761
Winson Chung64359a52013-07-08 17:17:08 -07002762 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2763 ItemInfoFilter f) {
2764 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2765 for (ItemInfo i : infos) {
2766 if (i instanceof ShortcutInfo) {
2767 ShortcutInfo info = (ShortcutInfo) i;
2768 ComponentName cn = info.intent.getComponent();
2769 if (cn != null && f.filterItem(null, info, cn)) {
2770 filtered.add(info);
2771 }
2772 } else if (i instanceof FolderInfo) {
2773 FolderInfo info = (FolderInfo) i;
2774 for (ShortcutInfo s : info.contents) {
2775 ComponentName cn = s.intent.getComponent();
2776 if (cn != null && f.filterItem(info, s, cn)) {
2777 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002778 }
2779 }
Winson Chung64359a52013-07-08 17:17:08 -07002780 } else if (i instanceof LauncherAppWidgetInfo) {
2781 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2782 ComponentName cn = info.providerName;
2783 if (cn != null && f.filterItem(null, info, cn)) {
2784 filtered.add(info);
2785 }
Winson Chung8a435102012-08-30 17:16:53 -07002786 }
2787 }
Winson Chung64359a52013-07-08 17:17:08 -07002788 return new ArrayList<ItemInfo>(filtered);
2789 }
2790
2791 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002792 ItemInfoFilter filter = new ItemInfoFilter() {
2793 @Override
2794 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2795 return cn.getPackageName().equals(pn);
2796 }
2797 };
2798 return filterItemInfos(sBgItemsIdMap.values(), filter);
2799 }
2800
2801 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002802 ItemInfoFilter filter = new ItemInfoFilter() {
2803 @Override
2804 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2805 return cn.equals(cname);
2806 }
2807 };
2808 return filterItemInfos(sBgItemsIdMap.values(), filter);
2809 }
2810
2811 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2812 if (i instanceof ShortcutInfo) {
2813 ShortcutInfo info = (ShortcutInfo) i;
2814 // We need to check for ACTION_MAIN otherwise getComponent() might
2815 // return null for some shortcuts (for instance, for shortcuts to
2816 // web pages.)
2817 Intent intent = info.intent;
2818 ComponentName name = intent.getComponent();
2819 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2820 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2821 return true;
2822 }
2823 }
2824 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002825 }
2826
2827 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002828 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002830 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002831 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2832 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833
Joe Onorato56d82912010-03-07 14:32:10 -05002834 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002835 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002838 // TODO: If there's an explicit component and we can't install that, delete it.
2839
Joe Onorato56d82912010-03-07 14:32:10 -05002840 info.title = c.getString(titleIndex);
2841
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 int iconType = c.getInt(iconTypeIndex);
2843 switch (iconType) {
2844 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2845 String packageName = c.getString(iconPackageIndex);
2846 String resourceName = c.getString(iconResourceIndex);
2847 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002848 info.customIcon = false;
2849 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002852 if (resources != null) {
2853 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002854 icon = Utilities.createIconBitmap(
2855 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002858 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 }
Joe Onorato56d82912010-03-07 14:32:10 -05002860 // the db
2861 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002862 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002863 }
2864 // the fallback icon
2865 if (icon == null) {
2866 icon = getFallbackIcon();
2867 info.usingFallbackIcon = true;
2868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 break;
2870 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002871 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002872 if (icon == null) {
2873 icon = getFallbackIcon();
2874 info.customIcon = false;
2875 info.usingFallbackIcon = true;
2876 } else {
2877 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 break;
2880 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002881 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002882 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 info.customIcon = false;
2884 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002886 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 return info;
2888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889
Michael Jurka931dc972011-08-05 15:08:15 -07002890 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002891 @SuppressWarnings("all") // suppress dead code warning
2892 final boolean debug = false;
2893 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002894 Log.d(TAG, "getIconFromCursor app="
2895 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2896 }
2897 byte[] data = c.getBlob(iconIndex);
2898 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002899 return Utilities.createIconBitmap(
2900 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002901 } catch (Exception e) {
2902 return null;
2903 }
2904 }
2905
Winson Chung3d503fb2011-07-13 17:25:49 -07002906 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2907 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002908 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002909 if (info == null) {
2910 return null;
2911 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002912 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002913
2914 return info;
2915 }
2916
Winson Chunga9abd0e2010-10-27 17:18:37 -07002917 /**
Winson Chung55cef262010-10-28 14:14:18 -07002918 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2919 */
2920 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2921 ComponentName component) {
2922 List<AppWidgetProviderInfo> widgets =
2923 AppWidgetManager.getInstance(context).getInstalledProviders();
2924 for (AppWidgetProviderInfo info : widgets) {
2925 if (info.provider.equals(component)) {
2926 return info;
2927 }
2928 }
2929 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002930 }
2931
Winson Chung68846fd2010-10-29 11:00:27 -07002932 /**
2933 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2934 */
2935 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2936 final PackageManager packageManager = context.getPackageManager();
2937 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2938 new ArrayList<WidgetMimeTypeHandlerData>();
2939
2940 final Intent supportsIntent =
2941 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2942 supportsIntent.setType(mimeType);
2943
2944 // Create a set of widget configuration components that we can test against
2945 final List<AppWidgetProviderInfo> widgets =
2946 AppWidgetManager.getInstance(context).getInstalledProviders();
2947 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2948 new HashMap<ComponentName, AppWidgetProviderInfo>();
2949 for (AppWidgetProviderInfo info : widgets) {
2950 configurationComponentToWidget.put(info.configure, info);
2951 }
2952
2953 // Run through each of the intents that can handle this type of clip data, and cross
2954 // reference them with the components that are actual configuration components
2955 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2956 PackageManager.MATCH_DEFAULT_ONLY);
2957 for (ResolveInfo info : activities) {
2958 final ActivityInfo activityInfo = info.activityInfo;
2959 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2960 activityInfo.name);
2961 if (configurationComponentToWidget.containsKey(infoComponent)) {
2962 supportedConfigurationActivities.add(
2963 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2964 configurationComponentToWidget.get(infoComponent)));
2965 }
2966 }
2967 return supportedConfigurationActivities;
2968 }
2969
Winson Chunga9abd0e2010-10-27 17:18:37 -07002970 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002971 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2972 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2973 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2974
Adam Cohend9198822011-11-22 16:42:47 -08002975 if (intent == null) {
2976 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2977 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2978 return null;
2979 }
2980
Joe Onorato0589f0f2010-02-08 13:44:00 -08002981 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002982 boolean customIcon = false;
2983 ShortcutIconResource iconResource = null;
2984
2985 if (bitmap != null && bitmap instanceof Bitmap) {
2986 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002987 customIcon = true;
2988 } else {
2989 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2990 if (extra != null && extra instanceof ShortcutIconResource) {
2991 try {
2992 iconResource = (ShortcutIconResource) extra;
2993 final PackageManager packageManager = context.getPackageManager();
2994 Resources resources = packageManager.getResourcesForApplication(
2995 iconResource.packageName);
2996 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002997 icon = Utilities.createIconBitmap(
2998 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002999 } catch (Exception e) {
3000 Log.w(TAG, "Could not load shortcut icon: " + extra);
3001 }
3002 }
3003 }
3004
Michael Jurkac9d95c52011-08-29 14:03:34 -07003005 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003006
3007 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003008 if (fallbackIcon != null) {
3009 icon = fallbackIcon;
3010 } else {
3011 icon = getFallbackIcon();
3012 info.usingFallbackIcon = true;
3013 }
Joe Onorato56d82912010-03-07 14:32:10 -05003014 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003015 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003016
Joe Onorato0589f0f2010-02-08 13:44:00 -08003017 info.title = name;
3018 info.intent = intent;
3019 info.customIcon = customIcon;
3020 info.iconResource = iconResource;
3021
3022 return info;
3023 }
3024
Winson Chungaac01e12011-08-17 10:37:13 -07003025 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3026 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003027 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003028 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003029 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003030 }
Joe Onorato56d82912010-03-07 14:32:10 -05003031 // If this icon doesn't have a custom icon, check to see
3032 // what's stored in the DB, and if it doesn't match what
3033 // we're going to show, store what we are going to show back
3034 // into the DB. We do this so when we're loading, if the
3035 // package manager can't find an icon (for example because
3036 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003037 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003038 cache.put(info, c.getBlob(iconIndex));
3039 return true;
3040 }
3041 return false;
3042 }
3043 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3044 boolean needSave = false;
3045 try {
3046 if (data != null) {
3047 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3048 Bitmap loaded = info.getIcon(mIconCache);
3049 needSave = !saved.sameAs(loaded);
3050 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003051 needSave = true;
3052 }
Winson Chungaac01e12011-08-17 10:37:13 -07003053 } catch (Exception e) {
3054 needSave = true;
3055 }
3056 if (needSave) {
3057 Log.d(TAG, "going to save icon bitmap for info=" + info);
3058 // This is slower than is ideal, but this only happens once
3059 // or when the app is updated with a new icon.
3060 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003061 }
3062 }
3063
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003065 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 * or make a new one.
3067 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003068 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 // See if a placeholder was created for us already
3070 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003071 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003073 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 folders.put(id, folderInfo);
3075 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003076 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 }
3078
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003079 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003080 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003081 return new Comparator<AppInfo>() {
3082 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003083 int result = collator.compare(a.title.toString().trim(),
3084 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003085 if (result == 0) {
3086 result = a.componentName.compareTo(b.componentName);
3087 }
3088 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003089 }
Winson Chung11904872012-09-17 16:58:46 -07003090 };
3091 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003092 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3093 = new Comparator<AppInfo>() {
3094 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003095 if (a.firstInstallTime < b.firstInstallTime) return 1;
3096 if (a.firstInstallTime > b.firstInstallTime) return -1;
3097 return 0;
3098 }
3099 };
Winson Chung11904872012-09-17 16:58:46 -07003100 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3101 final Collator collator = Collator.getInstance();
3102 return new Comparator<AppWidgetProviderInfo>() {
3103 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003104 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003105 }
3106 };
3107 }
Winson Chung5308f242011-08-18 12:12:41 -07003108 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3109 if (info.activityInfo != null) {
3110 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3111 } else {
3112 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3113 }
3114 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003115 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003116 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003117 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003118 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003119 ShortcutNameComparator(PackageManager pm) {
3120 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003121 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003122 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003123 }
3124 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3125 mPackageManager = pm;
3126 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003127 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003128 }
3129 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003130 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003131 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3132 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3133 if (mLabelCache.containsKey(keyA)) {
3134 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003135 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003136 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003137
Winson Chung5308f242011-08-18 12:12:41 -07003138 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003139 }
Winson Chung5308f242011-08-18 12:12:41 -07003140 if (mLabelCache.containsKey(keyB)) {
3141 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003142 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003143 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003144
Winson Chung5308f242011-08-18 12:12:41 -07003145 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003146 }
Winson Chung11904872012-09-17 16:58:46 -07003147 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 }
3149 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003150 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003151 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003152 private PackageManager mPackageManager;
3153 private HashMap<Object, String> mLabelCache;
3154 WidgetAndShortcutNameComparator(PackageManager pm) {
3155 mPackageManager = pm;
3156 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003157 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003158 }
3159 public final int compare(Object a, Object b) {
3160 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003161 if (mLabelCache.containsKey(a)) {
3162 labelA = mLabelCache.get(a);
3163 } else {
3164 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003165 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003166 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003167 mLabelCache.put(a, labelA);
3168 }
3169 if (mLabelCache.containsKey(b)) {
3170 labelB = mLabelCache.get(b);
3171 } else {
3172 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003173 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003174 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 mLabelCache.put(b, labelB);
3176 }
Winson Chung11904872012-09-17 16:58:46 -07003177 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003178 }
3179 };
Joe Onoratobe386092009-11-17 17:32:16 -08003180
3181 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003182 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003183 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3184 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3185 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3186 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003187 if (mLoaderTask != null) {
3188 mLoaderTask.dumpState();
3189 } else {
3190 Log.d(TAG, "mLoaderTask=null");
3191 }
Joe Onoratobe386092009-11-17 17:32:16 -08003192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193}