blob: c746b4d33f37233cafb543721234e55bce4f9df6 [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 Cohene25af792013-06-06 23:08:25 -0700499 // TODO: something breaks this in the upgrade path
500 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700501 }
502 }
503
Michael Jurka816474f2012-06-25 14:49:02 -0700504 static void checkItemInfo(final ItemInfo item) {
505 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
506 final long itemId = item.id;
507 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700508 public void run() {
509 synchronized (sBgLock) {
510 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700511 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700512 }
513 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700514 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700515 }
516
Michael Jurkac9d95c52011-08-29 14:03:34 -0700517 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
518 final ItemInfo item, final String callingFunction) {
519 final long itemId = item.id;
520 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
521 final ContentResolver cr = context.getContentResolver();
522
Adam Cohen487f7dd2012-06-28 18:12:10 -0700523 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700524 Runnable r = new Runnable() {
525 public void run() {
526 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700527 updateItemArrays(item, itemId, stackTrace);
528 }
529 };
530 runOnWorkerThread(r);
531 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700532
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700533 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
534 final ArrayList<ItemInfo> items, final String callingFunction) {
535 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700536
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700537 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
538 Runnable r = new Runnable() {
539 public void run() {
540 ArrayList<ContentProviderOperation> ops =
541 new ArrayList<ContentProviderOperation>();
542 int count = items.size();
543 for (int i = 0; i < count; i++) {
544 ItemInfo item = items.get(i);
545 final long itemId = item.id;
546 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
547 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700548
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700549 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
550 updateItemArrays(item, itemId, stackTrace);
551
552 }
553 try {
554 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
555 } catch (Exception e) {
556 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700557 }
558 }
559 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700560 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700561 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700562
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700563 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
564 // Lock on mBgLock *after* the db operation
565 synchronized (sBgLock) {
566 checkItemInfoLocked(itemId, item, stackTrace);
567
568 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
569 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
570 // Item is in a folder, make sure this folder exists
571 if (!sBgFolders.containsKey(item.container)) {
572 // An items container is being set to a that of an item which is not in
573 // the list of Folders.
574 String msg = "item: " + item + " container being set to: " +
575 item.container + ", not in the list of folders";
576 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700577 }
578 }
579
580 // Items are added/removed from the corresponding FolderInfo elsewhere, such
581 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
582 // that are on the desktop, as appropriate
583 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
584 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
585 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
586 switch (modelItem.itemType) {
587 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
588 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
589 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
590 if (!sBgWorkspaceItems.contains(modelItem)) {
591 sBgWorkspaceItems.add(modelItem);
592 }
593 break;
594 default:
595 break;
596 }
597 } else {
598 sBgWorkspaceItems.remove(modelItem);
599 }
600 }
601 }
602
Michael Jurkac7700af2013-05-14 20:17:58 +0200603 public void flushWorkerThread() {
604 mFlushingWorkerThread = true;
605 Runnable waiter = new Runnable() {
606 public void run() {
607 synchronized (this) {
608 notifyAll();
609 mFlushingWorkerThread = false;
610 }
611 }
612 };
613
614 synchronized(waiter) {
615 runOnWorkerThread(waiter);
616 if (mLoaderTask != null) {
617 synchronized(mLoaderTask) {
618 mLoaderTask.notify();
619 }
620 }
621 boolean success = false;
622 while (!success) {
623 try {
624 waiter.wait();
625 success = true;
626 } catch (InterruptedException e) {
627 }
628 }
629 }
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700634 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700635 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 item.cellX = cellX;
639 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700640
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 // We store hotseat items in canonical form which is this orientation invariant position
642 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649
650 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
653 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700657 }
658
659 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
661 * cellX, cellY have already been updated on the ItemInfos.
662 */
663 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
664 final long container, final int screen) {
665
666 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
667 int count = items.size();
668
669 for (int i = 0; i < count; i++) {
670 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700671 item.container = container;
672
673 // We store hotseat items in canonical form which is this orientation invariant position
674 // in the hotseat
675 if (context instanceof Launcher && screen < 0 &&
676 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678 item.cellY);
679 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681 }
682
683 final ContentValues values = new ContentValues();
684 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
685 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
686 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700687 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688
689 contentValues.add(values);
690 }
691 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
692 }
693
694 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700695 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800696 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700697 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700698 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700699 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800700 item.cellX = cellX;
701 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 item.spanX = spanX;
703 item.spanY = spanY;
704
705 // We store hotseat items in canonical form which is this orientation invariant position
706 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 }
Adam Cohend4844c32011-02-18 19:25:06 -0800713
Adam Cohend4844c32011-02-18 19:25:06 -0800714 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800715 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
717 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
718 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
719 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800721
Michael Jurka816474f2012-06-25 14:49:02 -0700722 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700723 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
725 /**
726 * Update an item to the database in a specified container.
727 */
728 static void updateItemInDatabase(Context context, final ItemInfo item) {
729 final ContentValues values = new ContentValues();
730 item.onAddToDatabase(values);
731 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
732 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800733 }
734
735 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 * Returns true if the shortcuts already exists in the database.
737 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 static boolean shortcutExists(Context context, String title, Intent intent) {
740 final ContentResolver cr = context.getContentResolver();
741 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
742 new String[] { "title", "intent" }, "title=? and intent=?",
743 new String[] { title, intent.toUri(0) }, null);
744 boolean result = false;
745 try {
746 result = c.moveToFirst();
747 } finally {
748 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700751 }
752
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700754 * Returns an ItemInfo array containing all the items in the LauncherModel.
755 * The ItemInfo.id is not set through this function.
756 */
757 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
758 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
759 final ContentResolver cr = context.getContentResolver();
760 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
761 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
762 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
763 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
764
765 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
766 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
767 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
768 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
769 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
770 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
771 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
772
773 try {
774 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700775 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700776 item.cellX = c.getInt(cellXIndex);
777 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700778 item.spanX = Math.max(1, c.getInt(spanXIndex));
779 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 item.container = c.getInt(containerIndex);
781 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700783
784 items.add(item);
785 }
786 } catch (Exception e) {
787 items.clear();
788 } finally {
789 c.close();
790 }
791
792 return items;
793 }
794
795 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
797 */
798 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
799 final ContentResolver cr = context.getContentResolver();
800 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
801 "_id=? and (itemType=? or itemType=?)",
802 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700803 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700804
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805 try {
806 if (c.moveToFirst()) {
807 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
808 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
809 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
810 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
811 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
812 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 FolderInfo folderInfo = null;
815 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700816 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
817 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700819 }
820
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821 folderInfo.title = c.getString(titleIndex);
822 folderInfo.id = id;
823 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700825 folderInfo.cellX = c.getInt(cellXIndex);
826 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827
828 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 } finally {
831 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700832 }
833
834 return null;
835 }
836
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 /**
838 * Add an item to the database in a specified container. Sets the container, screen, cellX and
839 * cellY fields of the item. Also assigns an ID to the item.
840 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700841 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 item.cellX = cellX;
845 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 // We store hotseat items in canonical form which is this orientation invariant position
847 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
855 final ContentValues values = new ContentValues();
856 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857 item.onAddToDatabase(values);
858
Michael Jurka414300a2013-08-27 15:42:35 +0200859 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700860 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700862
Michael Jurkac9d95c52011-08-29 14:03:34 -0700863 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700864 public void run() {
865 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
866 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700869 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700870 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700871 sBgItemsIdMap.put(item.id, item);
872 switch (item.itemType) {
873 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
874 sBgFolders.put(item.id, (FolderInfo) item);
875 // Fall through
876 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
877 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
878 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
879 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
880 sBgWorkspaceItems.add(item);
881 } else {
882 if (!sBgFolders.containsKey(item.container)) {
883 // Adding an item to a folder that doesn't exist.
884 String msg = "adding item: " + item + " to a folder that " +
885 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700886 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 break;
890 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
891 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
892 break;
893 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700894 }
895 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700896 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700897 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700898 }
899
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700901 * Creates a new unique child id, for a given cell span across all layouts.
902 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700903 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700905 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700907 }
908
Winson Chungaafa03c2010-06-11 17:34:16 -0700909 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700910 * Removes the specified item from the database
911 * @param context
912 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700916 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700917
Michael Jurka83df1882011-08-31 20:59:26 -0700918 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700919 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921
922 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700923 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700924 switch (item.itemType) {
925 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
926 sBgFolders.remove(item.id);
927 for (ItemInfo info: sBgItemsIdMap.values()) {
928 if (info.container == item.id) {
929 // We are deleting a folder which still contains items that
930 // think they are contained by that folder.
931 String msg = "deleting a folder (" + item + ") which still " +
932 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700933 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 sBgWorkspaceItems.remove(item);
937 break;
938 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
939 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
940 sBgWorkspaceItems.remove(item);
941 break;
942 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
943 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
944 break;
945 }
946 sBgItemsIdMap.remove(item.id);
947 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700948 }
949 }
Michael Jurka83df1882011-08-31 20:59:26 -0700950 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700951 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 }
953
954 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 * Update the order of the workspace screens in the database. The array list contains
956 * a list of screen ids in the order that they should appear.
957 */
Winson Chungc9168342013-06-26 14:54:55 -0700958 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700959 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700960 final ContentResolver cr = context.getContentResolver();
961 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
962
963 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700964 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 while (iter.hasNext()) {
966 long id = iter.next();
967 if (id < 0) {
968 iter.remove();
969 }
970 }
971
972 Runnable r = new Runnable() {
973 @Override
974 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 // Clear the table
976 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700977 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 ContentValues[] values = new ContentValues[count];
979 for (int i = 0; i < count; i++) {
980 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700981 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
983 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 values[i] = v;
985 }
986 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700987
Winson Chungba9c37f2013-08-30 14:11:37 -0700988 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700989 sBgWorkspaceScreens.clear();
990 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700991 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 }
993 };
994 runOnWorkerThread(r);
995 }
996
997 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 * Remove the contents of the specified folder from the database
999 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001000 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 final ContentResolver cr = context.getContentResolver();
1002
Michael Jurkac9d95c52011-08-29 14:03:34 -07001003 Runnable r = new Runnable() {
1004 public void run() {
1005 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001006 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001007 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 sBgItemsIdMap.remove(info.id);
1009 sBgFolders.remove(info.id);
1010 sBgDbIconCache.remove(info);
1011 sBgWorkspaceItems.remove(info);
1012 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013
Michael Jurkac9d95c52011-08-29 14:03:34 -07001014 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1015 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001017 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 for (ItemInfo childInfo : info.contents) {
1019 sBgItemsIdMap.remove(childInfo.id);
1020 sBgDbIconCache.remove(childInfo);
1021 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001022 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001023 }
1024 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 }
1027
1028 /**
1029 * Set this as the current Launcher activity object for the loader.
1030 */
1031 public void initialize(Callbacks callbacks) {
1032 synchronized (mLock) {
1033 mCallbacks = new WeakReference<Callbacks>(callbacks);
1034 }
1035 }
1036
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001037 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1039 * ACTION_PACKAGE_CHANGED.
1040 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001041 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001042 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001043 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001044
Joe Onorato36115782010-06-17 13:28:48 -04001045 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001046
Joe Onorato36115782010-06-17 13:28:48 -04001047 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1048 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1049 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1050 final String packageName = intent.getData().getSchemeSpecificPart();
1051 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052
Joe Onorato36115782010-06-17 13:28:48 -04001053 int op = PackageUpdatedTask.OP_NONE;
1054
1055 if (packageName == null || packageName.length() == 0) {
1056 // they sent us a bad intent
1057 return;
1058 }
1059
1060 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1061 op = PackageUpdatedTask.OP_UPDATE;
1062 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1063 if (!replacing) {
1064 op = PackageUpdatedTask.OP_REMOVE;
1065 }
1066 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1067 // later, we will update the package at this time
1068 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1069 if (!replacing) {
1070 op = PackageUpdatedTask.OP_ADD;
1071 } else {
1072 op = PackageUpdatedTask.OP_UPDATE;
1073 }
1074 }
1075
1076 if (op != PackageUpdatedTask.OP_NONE) {
1077 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1078 }
1079
1080 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001081 // First, schedule to add these apps back in.
1082 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1083 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1084 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001085 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001086 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1087 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1088 enqueuePackageUpdated(new PackageUpdatedTask(
1089 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001090 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001091 // If we have changed locale we need to clear out the labels in all apps/workspace.
1092 forceReload();
1093 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1094 // Check if configuration change was an mcc/mnc change which would affect app resources
1095 // and we would need to clear out the labels in all apps/workspace. Same handling as
1096 // above for ACTION_LOCALE_CHANGED
1097 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001098 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001099 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001100 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001101 forceReload();
1102 }
1103 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001104 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001105 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1106 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001107 if (mCallbacks != null) {
1108 Callbacks callbacks = mCallbacks.get();
1109 if (callbacks != null) {
1110 callbacks.bindSearchablesChanged();
1111 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001112 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001113 }
1114 }
1115
Reena Lee93f824a2011-09-23 17:20:28 -07001116 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001117 resetLoadedState(true, true);
1118
Reena Lee93f824a2011-09-23 17:20:28 -07001119 // Do this here because if the launcher activity is running it will be restarted.
1120 // If it's not running startLoaderFromBackground will merely tell it that it needs
1121 // to reload.
1122 startLoaderFromBackground();
1123 }
1124
Winson Chungf0c6ae02012-03-21 16:10:31 -07001125 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1126 synchronized (mLock) {
1127 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1128 // mWorkspaceLoaded to true later
1129 stopLoaderLocked();
1130 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1131 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1132 }
1133 }
1134
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001135 /**
1136 * When the launcher is in the background, it's possible for it to miss paired
1137 * configuration changes. So whenever we trigger the loader from the background
1138 * tell the launcher that it needs to re-run the loader when it comes back instead
1139 * of doing it now.
1140 */
1141 public void startLoaderFromBackground() {
1142 boolean runLoader = false;
1143 if (mCallbacks != null) {
1144 Callbacks callbacks = mCallbacks.get();
1145 if (callbacks != null) {
1146 // Only actually run the loader if they're not paused.
1147 if (!callbacks.setLoadOnResume()) {
1148 runLoader = true;
1149 }
1150 }
1151 }
1152 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001153 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001154 }
Joe Onorato36115782010-06-17 13:28:48 -04001155 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001156
Reena Lee93f824a2011-09-23 17:20:28 -07001157 // If there is already a loader task running, tell it to stop.
1158 // returns true if isLaunching() was true on the old task
1159 private boolean stopLoaderLocked() {
1160 boolean isLaunching = false;
1161 LoaderTask oldTask = mLoaderTask;
1162 if (oldTask != null) {
1163 if (oldTask.isLaunching()) {
1164 isLaunching = true;
1165 }
1166 oldTask.stopLocked();
1167 }
1168 return isLaunching;
1169 }
1170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001171 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001172 synchronized (mLock) {
1173 if (DEBUG_LOADERS) {
1174 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1175 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001177 // Clear any deferred bind-runnables from the synchronized load process
1178 // We must do this before any loading/binding is scheduled below.
1179 mDeferredBindRunnables.clear();
1180
Joe Onorato36115782010-06-17 13:28:48 -04001181 // Don't bother to start the thread if we know it's not going to do anything
1182 if (mCallbacks != null && mCallbacks.get() != null) {
1183 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001184 // also, don't downgrade isLaunching if we're already running
1185 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001186 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1188 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1189 } else {
1190 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1191 sWorker.post(mLoaderTask);
1192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 }
1195 }
1196
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 void bindRemainingSynchronousPages() {
1198 // Post the remaining side pages to be loaded
1199 if (!mDeferredBindRunnables.isEmpty()) {
1200 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001201 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001202 }
1203 mDeferredBindRunnables.clear();
1204 }
1205 }
1206
Joe Onorato36115782010-06-17 13:28:48 -04001207 public void stopLoader() {
1208 synchronized (mLock) {
1209 if (mLoaderTask != null) {
1210 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 }
1212 }
Joe Onorato36115782010-06-17 13:28:48 -04001213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001214
Winson Chung76828c82013-08-19 15:43:29 -07001215 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1216 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1217 final ContentResolver contentResolver = context.getContentResolver();
1218 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1219 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1220 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1221
1222 try {
1223 final int idIndex = sc.getColumnIndexOrThrow(
1224 LauncherSettings.WorkspaceScreens._ID);
1225 final int rankIndex = sc.getColumnIndexOrThrow(
1226 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1227 while (sc.moveToNext()) {
1228 try {
1229 long screenId = sc.getLong(idIndex);
1230 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001231 orderedScreens.put(rank, screenId);
1232 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001233 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001234 }
1235 }
1236 } finally {
1237 sc.close();
1238 }
1239 return orderedScreens;
1240 }
1241
Michael Jurkac57b7a82011-08-09 22:02:20 -07001242 public boolean isAllAppsLoaded() {
1243 return mAllAppsLoaded;
1244 }
1245
Winson Chung36a62fe2012-05-06 18:04:42 -07001246 boolean isLoadingWorkspace() {
1247 synchronized (mLock) {
1248 if (mLoaderTask != null) {
1249 return mLoaderTask.isLoadingWorkspace();
1250 }
1251 }
1252 return false;
1253 }
1254
Joe Onorato36115782010-06-17 13:28:48 -04001255 /**
1256 * Runnable for the thread that loads the contents of the launcher:
1257 * - workspace icons
1258 * - widgets
1259 * - all apps icons
1260 */
1261 private class LoaderTask implements Runnable {
1262 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001263 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001264 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001265 private boolean mStopped;
1266 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001267
Winson Chungc3eecff2011-07-11 17:44:15 -07001268 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001269
1270 LoaderTask(Context context, boolean isLaunching) {
1271 mContext = context;
1272 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001273 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001274 }
1275
Joe Onorato36115782010-06-17 13:28:48 -04001276 boolean isLaunching() {
1277 return mIsLaunching;
1278 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279
Winson Chung36a62fe2012-05-06 18:04:42 -07001280 boolean isLoadingWorkspace() {
1281 return mIsLoadingAndBindingWorkspace;
1282 }
1283
Winson Chungc763c4e2013-07-19 13:49:06 -07001284 /** Returns whether this is an upgrade path */
1285 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001286 mIsLoadingAndBindingWorkspace = true;
1287
Joe Onorato36115782010-06-17 13:28:48 -04001288 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001289 if (DEBUG_LOADERS) {
1290 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001292
Winson Chungc763c4e2013-07-19 13:49:06 -07001293 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001294 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001295 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001296 synchronized (LoaderTask.this) {
1297 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001298 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001299 }
1300 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001301 }
1302 }
1303
Joe Onorato36115782010-06-17 13:28:48 -04001304 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001305 bindWorkspace(-1, isUpgradePath);
1306 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001307 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001308
Joe Onorato36115782010-06-17 13:28:48 -04001309 private void waitForIdle() {
1310 // Wait until the either we're stopped or the other threads are done.
1311 // This way we don't start loading all apps until the workspace has settled
1312 // down.
1313 synchronized (LoaderTask.this) {
1314 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001315
Joe Onorato36115782010-06-17 13:28:48 -04001316 mHandler.postIdle(new Runnable() {
1317 public void run() {
1318 synchronized (LoaderTask.this) {
1319 mLoadAndBindStepFinished = true;
1320 if (DEBUG_LOADERS) {
1321 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
Joe Onorato36115782010-06-17 13:28:48 -04001323 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001324 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001325 }
Joe Onorato36115782010-06-17 13:28:48 -04001326 });
1327
Michael Jurkac7700af2013-05-14 20:17:58 +02001328 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001329 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001330 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1331 // wait no longer than 1sec at a time
1332 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001333 } catch (InterruptedException ex) {
1334 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001335 }
1336 }
Joe Onorato36115782010-06-17 13:28:48 -04001337 if (DEBUG_LOADERS) {
1338 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001339 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001340 + "ms for previous step to finish binding");
1341 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001344
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001345 void runBindSynchronousPage(int synchronousBindPage) {
1346 if (synchronousBindPage < 0) {
1347 // Ensure that we have a valid page index to load synchronously
1348 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1349 "valid page index");
1350 }
1351 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1352 // Ensure that we don't try and bind a specified page when the pages have not been
1353 // loaded already (we should load everything asynchronously in that case)
1354 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1355 }
1356 synchronized (mLock) {
1357 if (mIsLoaderTaskRunning) {
1358 // Ensure that we are never running the background loading at this point since
1359 // we also touch the background collections
1360 throw new RuntimeException("Error! Background loading is already running");
1361 }
1362 }
1363
1364 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1365 // data structures, we can't allow any other thread to touch that data, but because
1366 // this call is synchronous, we can get away with not locking).
1367
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001368 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001369 // operations from the previous activity. We need to ensure that all queued operations
1370 // are executed before any synchronous binding work is done.
1371 mHandler.flush();
1372
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 // Divide the set of loaded items into those that we are binding synchronously, and
1374 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001375 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001376 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1377 // arise from that.
1378 onlyBindAllApps();
1379 }
1380
Joe Onorato36115782010-06-17 13:28:48 -04001381 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001382 boolean isUpgrade = false;
1383
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 synchronized (mLock) {
1385 mIsLoaderTaskRunning = true;
1386 }
Joe Onorato36115782010-06-17 13:28:48 -04001387 // Optimize for end-user experience: if the Launcher is up and // running with the
1388 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1389 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001390 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001391 // Elevate priority when Home launches for the first time to avoid
1392 // starving at boot time. Staring at a blank home is not cool.
1393 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001394 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1395 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001396 android.os.Process.setThreadPriority(mIsLaunching
1397 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1398 }
Winson Chung64359a52013-07-08 17:17:08 -07001399 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001400 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001401
Joe Onorato36115782010-06-17 13:28:48 -04001402 if (mStopped) {
1403 break keep_running;
1404 }
1405
1406 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1407 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 synchronized (mLock) {
1409 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001410 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001411 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1412 }
1413 }
Joe Onorato36115782010-06-17 13:28:48 -04001414 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001415
1416 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001417 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1418 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001419
1420 // Restore the default thread priority after we are done loading items
1421 synchronized (mLock) {
1422 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1423 }
Joe Onorato36115782010-06-17 13:28:48 -04001424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425
Winson Chungaac01e12011-08-17 10:37:13 -07001426 // Update the saved icons if necessary
1427 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001428 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001429 for (Object key : sBgDbIconCache.keySet()) {
1430 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1431 }
1432 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001433 }
Winson Chungaac01e12011-08-17 10:37:13 -07001434
Winson Chungc58497e2013-09-03 17:48:37 -07001435 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1436 // Ensure that all the applications that are in the system are
1437 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001438 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001439 verifyApplications();
1440 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001441 }
1442
Joe Onorato36115782010-06-17 13:28:48 -04001443 // Clear out this reference, otherwise we end up holding it until all of the
1444 // callback runnables are done.
1445 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446
Joe Onorato36115782010-06-17 13:28:48 -04001447 synchronized (mLock) {
1448 // If we are still the last one to be scheduled, remove ourselves.
1449 if (mLoaderTask == this) {
1450 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001452 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001453 }
Joe Onorato36115782010-06-17 13:28:48 -04001454 }
1455
1456 public void stopLocked() {
1457 synchronized (LoaderTask.this) {
1458 mStopped = true;
1459 this.notify();
1460 }
1461 }
1462
1463 /**
1464 * Gets the callbacks object. If we've been stopped, or if the launcher object
1465 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1466 * object that was around when the deferred message was scheduled, and if there's
1467 * a new Callbacks object around then also return null. This will save us from
1468 * calling onto it with data that will be ignored.
1469 */
1470 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1471 synchronized (mLock) {
1472 if (mStopped) {
1473 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001474 }
Joe Onorato36115782010-06-17 13:28:48 -04001475
1476 if (mCallbacks == null) {
1477 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001478 }
Joe Onorato36115782010-06-17 13:28:48 -04001479
1480 final Callbacks callbacks = mCallbacks.get();
1481 if (callbacks != oldCallbacks) {
1482 return null;
1483 }
1484 if (callbacks == null) {
1485 Log.w(TAG, "no mCallbacks");
1486 return null;
1487 }
1488
1489 return callbacks;
1490 }
1491 }
1492
Winson Chungc763c4e2013-07-19 13:49:06 -07001493 private void verifyApplications() {
1494 final Context context = mApp.getContext();
1495
1496 // Cross reference all the applications in our apps list with items in the workspace
1497 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001498 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001499 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001500 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001501 tmpInfos = getItemInfoForComponentName(app.componentName);
1502 if (tmpInfos.isEmpty()) {
1503 // We are missing an application icon, so add this to the workspace
1504 added.add(app);
1505 // This is a rare event, so lets log it
1506 Log.e(TAG, "Missing Application on load: " + app);
1507 }
1508 }
1509 }
1510 if (!added.isEmpty()) {
1511 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001512 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001513 }
1514 }
1515
Winson Chung5f8afe62013-08-12 16:19:28 -07001516 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001517 LauncherAppState app = LauncherAppState.getInstance();
1518 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1519 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1520 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001521 }
1522
Joe Onorato36115782010-06-17 13:28:48 -04001523 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001524 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1525 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001526 LauncherAppState app = LauncherAppState.getInstance();
1527 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1528 int countX = (int) grid.numColumns;
1529 int countY = (int) grid.numRows;
1530
Adam Cohendcd297f2013-06-18 13:13:40 -07001531 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001532 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001533 // Return early if we detect that an item is under the hotseat button
1534 if (mCallbacks == null ||
1535 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1536 deleteOnItemOverlap.set(true);
1537 return false;
1538 }
1539
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1541 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1542 [(int) item.screenId][0] != null) {
1543 Log.e(TAG, "Error loading shortcut into hotseat " + item
1544 + " into position (" + item.screenId + ":" + item.cellX + ","
1545 + item.cellY + ") occupied by "
1546 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1547 [(int) item.screenId][0]);
1548 return false;
1549 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001550 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001551 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 items[(int) item.screenId][0] = item;
1553 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001554 return true;
1555 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001556 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1557 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001558 return true;
1559 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001560
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001562 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 occupied.put(item.screenId, items);
1564 }
1565
1566 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001567 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001568 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1569 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001570 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001571 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001573 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001574 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001576 return false;
1577 }
1578 }
1579 }
1580 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1581 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001583 }
1584 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001585
Joe Onorato36115782010-06-17 13:28:48 -04001586 return true;
1587 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588
Winson Chungba9c37f2013-08-30 14:11:37 -07001589 /** Clears all the sBg data structures */
1590 private void clearSBgDataStructures() {
1591 synchronized (sBgLock) {
1592 sBgWorkspaceItems.clear();
1593 sBgAppWidgets.clear();
1594 sBgFolders.clear();
1595 sBgItemsIdMap.clear();
1596 sBgDbIconCache.clear();
1597 sBgWorkspaceScreens.clear();
1598 }
1599 }
1600
Winson Chungc763c4e2013-07-19 13:49:06 -07001601 /** Returns whether this is an upgradge path */
1602 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001603 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604
Joe Onorato36115782010-06-17 13:28:48 -04001605 final Context context = mContext;
1606 final ContentResolver contentResolver = context.getContentResolver();
1607 final PackageManager manager = context.getPackageManager();
1608 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1609 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001610
Winson Chung892c74d2013-08-22 16:15:50 -07001611 LauncherAppState app = LauncherAppState.getInstance();
1612 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1613 int countX = (int) grid.numColumns;
1614 int countY = (int) grid.numRows;
1615
Michael Jurkab85f8a42012-04-25 15:48:32 -07001616 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001617 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001618
Winson Chungc763c4e2013-07-19 13:49:06 -07001619 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001620 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001621
Winson Chung2abf94d2012-07-18 18:16:38 -07001622 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001623 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001624
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001625 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001626 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001627 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001628 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001629
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001630 // +1 for the hotseat (it can be larger than the workspace)
1631 // Load workspace in reverse order to ensure that latest items are loaded first (and
1632 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001633 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001635 try {
1636 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1637 final int intentIndex = c.getColumnIndexOrThrow
1638 (LauncherSettings.Favorites.INTENT);
1639 final int titleIndex = c.getColumnIndexOrThrow
1640 (LauncherSettings.Favorites.TITLE);
1641 final int iconTypeIndex = c.getColumnIndexOrThrow(
1642 LauncherSettings.Favorites.ICON_TYPE);
1643 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1644 final int iconPackageIndex = c.getColumnIndexOrThrow(
1645 LauncherSettings.Favorites.ICON_PACKAGE);
1646 final int iconResourceIndex = c.getColumnIndexOrThrow(
1647 LauncherSettings.Favorites.ICON_RESOURCE);
1648 final int containerIndex = c.getColumnIndexOrThrow(
1649 LauncherSettings.Favorites.CONTAINER);
1650 final int itemTypeIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.ITEM_TYPE);
1652 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001654 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1655 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 final int screenIndex = c.getColumnIndexOrThrow(
1657 LauncherSettings.Favorites.SCREEN);
1658 final int cellXIndex = c.getColumnIndexOrThrow
1659 (LauncherSettings.Favorites.CELLX);
1660 final int cellYIndex = c.getColumnIndexOrThrow
1661 (LauncherSettings.Favorites.CELLY);
1662 final int spanXIndex = c.getColumnIndexOrThrow
1663 (LauncherSettings.Favorites.SPANX);
1664 final int spanYIndex = c.getColumnIndexOrThrow(
1665 LauncherSettings.Favorites.SPANY);
1666 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1667 //final int displayModeIndex = c.getColumnIndexOrThrow(
1668 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001669
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001670 ShortcutInfo info;
1671 String intentDescription;
1672 LauncherAppWidgetInfo appWidgetInfo;
1673 int container;
1674 long id;
1675 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001676
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001677 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001678 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 try {
1680 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001681
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001682 switch (itemType) {
1683 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1684 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001685 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001686 intentDescription = c.getString(intentIndex);
1687 try {
1688 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001689 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001690 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001691 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001692 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001693 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1694 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001695 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001696 // If apps can be on external storage, then we just
1697 // leave them for the user to remove (maybe add
1698 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001699 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001700 }
1701 continue;
1702 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001704 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 continue;
1706 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001708 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1709 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1710 titleIndex, mLabelCache);
1711 } else {
1712 info = getShortcutInfo(c, context, iconTypeIndex,
1713 iconPackageIndex, iconResourceIndex, iconIndex,
1714 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 // App shortcuts that used to be automatically added to Launcher
1717 // didn't always have the correct intent flags set, so do that
1718 // here
1719 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001720 intent.getCategories() != null &&
1721 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001722 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001723 intent.addFlags(
1724 Intent.FLAG_ACTIVITY_NEW_TASK |
1725 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1726 }
Michael Jurka96879562012-03-22 05:54:33 -07001727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001728
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001730 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 container = c.getInt(containerIndex);
1733 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001734 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 info.cellX = c.getInt(cellXIndex);
1736 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001737 info.spanX = 1;
1738 info.spanY = 1;
1739 // Skip loading items that are out of bounds
1740 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1741 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001742 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1743 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001744 continue;
1745 }
1746 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001747 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001748 deleteOnItemOverlap.set(false);
1749 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1750 if (deleteOnItemOverlap.get()) {
1751 itemsToRemove.add(id);
1752 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 break;
1754 }
1755
1756 switch (container) {
1757 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1758 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1759 sBgWorkspaceItems.add(info);
1760 break;
1761 default:
1762 // Item is in a user folder
1763 FolderInfo folderInfo =
1764 findOrMakeFolder(sBgFolders, container);
1765 folderInfo.add(info);
1766 break;
1767 }
1768 sBgItemsIdMap.put(info.id, info);
1769
1770 // now that we've loaded everthing re-save it with the
1771 // icon in case it disappears somehow.
1772 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001773 } else {
1774 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 }
1776 break;
1777
1778 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1779 id = c.getLong(idIndex);
1780 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1781
1782 folderInfo.title = c.getString(titleIndex);
1783 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001786 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 folderInfo.cellX = c.getInt(cellXIndex);
1788 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001789 folderInfo.spanX = 1;
1790 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791
Winson Chung5f8afe62013-08-12 16:19:28 -07001792 // Skip loading items that are out of bounds
1793 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001794 if (checkItemDimensions(folderInfo)) {
1795 Log.d(TAG, "Skipped loading out of bounds folder");
1796 continue;
1797 }
1798 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001799 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001800 deleteOnItemOverlap.set(false);
1801 if (!checkItemPlacement(occupied, folderInfo,
1802 deleteOnItemOverlap)) {
1803 if (deleteOnItemOverlap.get()) {
1804 itemsToRemove.add(id);
1805 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001806 break;
1807 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001808
Joe Onorato9c1289c2009-08-17 11:03:03 -04001809 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1811 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1812 sBgWorkspaceItems.add(folderInfo);
1813 break;
Joe Onorato36115782010-06-17 13:28:48 -04001814 }
Joe Onorato17a89222011-02-08 17:26:11 -08001815
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1817 sBgFolders.put(folderInfo.id, folderInfo);
1818 break;
1819
1820 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1821 // Read all Launcher-specific widget details
1822 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001823 String savedProvider = c.getString(appWidgetProviderIndex);
1824
Joe Onorato36115782010-06-17 13:28:48 -04001825 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001826
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 final AppWidgetProviderInfo provider =
1828 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001829
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001830 if (!isSafeMode && (provider == null || provider.provider == null ||
1831 provider.provider.getPackageName() == null)) {
1832 String log = "Deleting widget that isn't installed anymore: id="
1833 + id + " appWidgetId=" + appWidgetId;
1834 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001835 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001836 itemsToRemove.add(id);
1837 } else {
1838 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1839 provider.provider);
1840 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001841 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 appWidgetInfo.cellX = c.getInt(cellXIndex);
1843 appWidgetInfo.cellY = c.getInt(cellYIndex);
1844 appWidgetInfo.spanX = c.getInt(spanXIndex);
1845 appWidgetInfo.spanY = c.getInt(spanYIndex);
1846 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1847 appWidgetInfo.minSpanX = minSpan[0];
1848 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001849
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 container = c.getInt(containerIndex);
1851 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1852 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1853 Log.e(TAG, "Widget found where container != " +
1854 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1855 continue;
1856 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001857
Adam Cohene25af792013-06-06 23:08:25 -07001858 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001859 // Skip loading items that are out of bounds
1860 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1861 if (checkItemDimensions(appWidgetInfo)) {
1862 Log.d(TAG, "Skipped loading out of bounds app widget");
1863 continue;
1864 }
1865 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001867 deleteOnItemOverlap.set(false);
1868 if (!checkItemPlacement(occupied, appWidgetInfo,
1869 deleteOnItemOverlap)) {
1870 if (deleteOnItemOverlap.get()) {
1871 itemsToRemove.add(id);
1872 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 break;
1874 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001875 String providerName = provider.provider.flattenToString();
1876 if (!providerName.equals(savedProvider)) {
1877 ContentValues values = new ContentValues();
1878 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1879 providerName);
1880 String where = BaseColumns._ID + "= ?";
1881 String[] args = {Integer.toString(c.getInt(idIndex))};
1882 contentResolver.update(contentUri, values, where, args);
1883 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1885 sBgAppWidgets.add(appWidgetInfo);
1886 }
Joe Onorato36115782010-06-17 13:28:48 -04001887 break;
1888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001890 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001892 }
1893 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001894 if (c != null) {
1895 c.close();
1896 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 }
1898
Winson Chungba9c37f2013-08-30 14:11:37 -07001899 // Break early if we've stopped loading
1900 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001901 clearSBgDataStructures();
1902 return false;
1903 }
1904
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 if (itemsToRemove.size() > 0) {
1906 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001907 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 // Remove dead items
1909 for (long id : itemsToRemove) {
1910 if (DEBUG_LOADERS) {
1911 Log.d(TAG, "Removed id = " + id);
1912 }
1913 // Don't notify content observers
1914 try {
1915 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1916 null, null);
1917 } catch (RemoteException e) {
1918 Log.w(TAG, "Could not remove id = " + id);
1919 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001920 }
1921 }
1922
Winson Chungc763c4e2013-07-19 13:49:06 -07001923 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001924 long maxScreenId = 0;
1925 // If we're importing we use the old screen order.
1926 for (ItemInfo item: sBgItemsIdMap.values()) {
1927 long screenId = item.screenId;
1928 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1929 !sBgWorkspaceScreens.contains(screenId)) {
1930 sBgWorkspaceScreens.add(screenId);
1931 if (screenId > maxScreenId) {
1932 maxScreenId = screenId;
1933 }
1934 }
1935 }
1936 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001937
Michael Jurka414300a2013-08-27 15:42:35 +02001938 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001939 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001940
1941 // Update the max item id after we load an old db
1942 long maxItemId = 0;
1943 // If we're importing we use the old screen order.
1944 for (ItemInfo item: sBgItemsIdMap.values()) {
1945 maxItemId = Math.max(maxItemId, item.id);
1946 }
Michael Jurka414300a2013-08-27 15:42:35 +02001947 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001949 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1950 for (Integer i : orderedScreens.keySet()) {
1951 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 }
1953
1954 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001955 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 for (ItemInfo item: sBgItemsIdMap.values()) {
1957 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1959 unusedScreens.contains(screenId)) {
1960 unusedScreens.remove(screenId);
1961 }
1962 }
1963
1964 // If there are any empty screens remove them, and update.
1965 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001966 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1968 }
1969 }
1970
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001971 if (DEBUG_LOADERS) {
1972 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1973 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001974 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001975 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001976 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001977
Daniel Sandler566da102013-06-25 23:43:45 -04001978 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001979 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001981 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 line += " | ";
1983 }
Winson Chung892c74d2013-08-22 16:15:50 -07001984 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001985 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 }
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001989 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 }
Joe Onorato36115782010-06-17 13:28:48 -04001991 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001992 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001993 }
1994
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001995 /** Filters the set of items who are directly or indirectly (via another container) on the
1996 * specified screen. */
1997 private void filterCurrentWorkspaceItems(int currentScreen,
1998 ArrayList<ItemInfo> allWorkspaceItems,
1999 ArrayList<ItemInfo> currentScreenItems,
2000 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002001 // Purge any null ItemInfos
2002 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2003 while (iter.hasNext()) {
2004 ItemInfo i = iter.next();
2005 if (i == null) {
2006 iter.remove();
2007 }
2008 }
2009
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 // If we aren't filtering on a screen, then the set of items to load is the full set of
2011 // items given.
2012 if (currentScreen < 0) {
2013 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002014 }
2015
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 // Order the set of items by their containers first, this allows use to walk through the
2017 // list sequentially, build up a list of containers that are in the specified screen,
2018 // as well as all items in those containers.
2019 Set<Long> itemsOnScreen = new HashSet<Long>();
2020 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2021 @Override
2022 public int compare(ItemInfo lhs, ItemInfo rhs) {
2023 return (int) (lhs.container - rhs.container);
2024 }
2025 });
2026 for (ItemInfo info : allWorkspaceItems) {
2027 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 currentScreenItems.add(info);
2030 itemsOnScreen.add(info.id);
2031 } else {
2032 otherScreenItems.add(info);
2033 }
2034 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2035 currentScreenItems.add(info);
2036 itemsOnScreen.add(info.id);
2037 } else {
2038 if (itemsOnScreen.contains(info.container)) {
2039 currentScreenItems.add(info);
2040 itemsOnScreen.add(info.id);
2041 } else {
2042 otherScreenItems.add(info);
2043 }
2044 }
2045 }
2046 }
2047
2048 /** Filters the set of widgets which are on the specified screen. */
2049 private void filterCurrentAppWidgets(int currentScreen,
2050 ArrayList<LauncherAppWidgetInfo> appWidgets,
2051 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2052 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2053 // If we aren't filtering on a screen, then the set of items to load is the full set of
2054 // widgets given.
2055 if (currentScreen < 0) {
2056 currentScreenWidgets.addAll(appWidgets);
2057 }
2058
2059 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002060 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002062 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 currentScreenWidgets.add(widget);
2064 } else {
2065 otherScreenWidgets.add(widget);
2066 }
2067 }
2068 }
2069
2070 /** Filters the set of folders which are on the specified screen. */
2071 private void filterCurrentFolders(int currentScreen,
2072 HashMap<Long, ItemInfo> itemsIdMap,
2073 HashMap<Long, FolderInfo> folders,
2074 HashMap<Long, FolderInfo> currentScreenFolders,
2075 HashMap<Long, FolderInfo> otherScreenFolders) {
2076 // If we aren't filtering on a screen, then the set of items to load is the full set of
2077 // widgets given.
2078 if (currentScreen < 0) {
2079 currentScreenFolders.putAll(folders);
2080 }
2081
2082 for (long id : folders.keySet()) {
2083 ItemInfo info = itemsIdMap.get(id);
2084 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002085 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 currentScreenFolders.put(id, folder);
2089 } else {
2090 otherScreenFolders.put(id, folder);
2091 }
2092 }
2093 }
2094
2095 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2096 * right) */
2097 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002098 final LauncherAppState app = LauncherAppState.getInstance();
2099 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 // XXX: review this
2101 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002102 @Override
2103 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002104 int cellCountX = (int) grid.numColumns;
2105 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002106 int screenOffset = cellCountX * cellCountY;
2107 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002108 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002109 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002111 rhs.cellY * cellCountX + rhs.cellX);
2112 return (int) (lr - rr);
2113 }
2114 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002116
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2118 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002119 final Runnable r = new Runnable() {
2120 @Override
2121 public void run() {
2122 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2123 if (callbacks != null) {
2124 callbacks.bindScreens(orderedScreens);
2125 }
2126 }
2127 };
2128 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2129 }
2130
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002131 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2132 final ArrayList<ItemInfo> workspaceItems,
2133 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2134 final HashMap<Long, FolderInfo> folders,
2135 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002136
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 final boolean postOnMainThread = (deferredBindRunnables != null);
2138
2139 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002140 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002142 final int start = i;
2143 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 final Runnable r = new Runnable() {
2145 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002147 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002149 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2150 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 }
2152 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002153 };
2154 if (postOnMainThread) {
2155 deferredBindRunnables.add(r);
2156 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002157 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 }
Joe Onorato36115782010-06-17 13:28:48 -04002159 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160
2161 // Bind the folders
2162 if (!folders.isEmpty()) {
2163 final Runnable r = new Runnable() {
2164 public void run() {
2165 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2166 if (callbacks != null) {
2167 callbacks.bindFolders(folders);
2168 }
2169 }
2170 };
2171 if (postOnMainThread) {
2172 deferredBindRunnables.add(r);
2173 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002174 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 }
2176 }
2177
2178 // Bind the widgets, one at a time
2179 N = appWidgets.size();
2180 for (int i = 0; i < N; i++) {
2181 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2182 final Runnable r = new Runnable() {
2183 public void run() {
2184 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2185 if (callbacks != null) {
2186 callbacks.bindAppWidget(widget);
2187 }
2188 }
2189 };
2190 if (postOnMainThread) {
2191 deferredBindRunnables.add(r);
2192 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002193 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 }
2195 }
2196 }
2197
2198 /**
2199 * Binds all loaded data to actual views on the main thread.
2200 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002201 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 final long t = SystemClock.uptimeMillis();
2203 Runnable r;
2204
2205 // Don't use these two variables in any of the callback runnables.
2206 // Otherwise we hold a reference to them.
2207 final Callbacks oldCallbacks = mCallbacks.get();
2208 if (oldCallbacks == null) {
2209 // This launcher has exited and nobody bothered to tell us. Just bail.
2210 Log.w(TAG, "LoaderTask running with no launcher");
2211 return;
2212 }
2213
Winson Chung4a2afa32012-07-19 14:53:05 -07002214 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2215 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 oldCallbacks.getCurrentWorkspaceScreen();
2217
2218 // Load all the items that are on the current page first (and in the process, unbind
2219 // all the existing workspace items before we call startBinding() below.
2220 unbindWorkspaceItemsOnMainThread();
2221 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2222 ArrayList<LauncherAppWidgetInfo> appWidgets =
2223 new ArrayList<LauncherAppWidgetInfo>();
2224 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2225 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002227 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 workspaceItems.addAll(sBgWorkspaceItems);
2229 appWidgets.addAll(sBgAppWidgets);
2230 folders.putAll(sBgFolders);
2231 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 }
2234
2235 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2236 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2237 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2238 new ArrayList<LauncherAppWidgetInfo>();
2239 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2240 new ArrayList<LauncherAppWidgetInfo>();
2241 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2242 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2243
2244 // Separate the items that are on the current screen, and all the other remaining items
2245 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2246 otherWorkspaceItems);
2247 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2248 otherAppWidgets);
2249 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2250 otherFolders);
2251 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2252 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2253
2254 // Tell the workspace that we're about to start binding items
2255 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002256 public void run() {
2257 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2258 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002260 }
2261 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002262 };
Winson Chung81b52252012-08-27 15:34:29 -07002263 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2266
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 // Load items on the current page
2268 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2269 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002270 if (isLoadingSynchronously) {
2271 r = new Runnable() {
2272 public void run() {
2273 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2274 if (callbacks != null) {
2275 callbacks.onPageBoundSynchronously(currentScreen);
2276 }
2277 }
2278 };
Winson Chung81b52252012-08-27 15:34:29 -07002279 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002280 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281
Winson Chung4a2afa32012-07-19 14:53:05 -07002282 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2283 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 mDeferredBindRunnables.clear();
2285 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002286 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287
2288 // Tell the workspace that we're done binding items
2289 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002290 public void run() {
2291 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2292 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002293 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002294 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295
Winson Chung98e030b2012-05-07 16:01:11 -07002296 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002297 if (DEBUG_LOADERS) {
2298 Log.d(TAG, "bound workspace in "
2299 + (SystemClock.uptimeMillis()-t) + "ms");
2300 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002301
2302 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002303 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002304 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002305 if (isLoadingSynchronously) {
2306 mDeferredBindRunnables.add(r);
2307 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002308 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002309 }
Joe Onorato36115782010-06-17 13:28:48 -04002310 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002311
Joe Onorato36115782010-06-17 13:28:48 -04002312 private void loadAndBindAllApps() {
2313 if (DEBUG_LOADERS) {
2314 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2315 }
2316 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002317 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002318 synchronized (LoaderTask.this) {
2319 if (mStopped) {
2320 return;
2321 }
2322 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002323 }
Joe Onorato36115782010-06-17 13:28:48 -04002324 } else {
2325 onlyBindAllApps();
2326 }
2327 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002328
Joe Onorato36115782010-06-17 13:28:48 -04002329 private void onlyBindAllApps() {
2330 final Callbacks oldCallbacks = mCallbacks.get();
2331 if (oldCallbacks == null) {
2332 // This launcher has exited and nobody bothered to tell us. Just bail.
2333 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2334 return;
2335 }
2336
2337 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002338 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002339 final ArrayList<AppInfo> list
2340 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002341 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002342 public void run() {
2343 final long t = SystemClock.uptimeMillis();
2344 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2345 if (callbacks != null) {
2346 callbacks.bindAllApplications(list);
2347 }
2348 if (DEBUG_LOADERS) {
2349 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2350 + (SystemClock.uptimeMillis()-t) + "ms");
2351 }
2352 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002353 };
2354 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002355 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002356 r.run();
2357 } else {
2358 mHandler.post(r);
2359 }
Joe Onorato36115782010-06-17 13:28:48 -04002360 }
2361
Winson Chung64359a52013-07-08 17:17:08 -07002362 private void loadAllApps() {
2363 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002364
Joe Onorato36115782010-06-17 13:28:48 -04002365 final Callbacks oldCallbacks = mCallbacks.get();
2366 if (oldCallbacks == null) {
2367 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002368 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002369 return;
2370 }
2371
Winson Chung64359a52013-07-08 17:17:08 -07002372 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002373 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2374 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2375
Winson Chung64359a52013-07-08 17:17:08 -07002376 // Clear the list of apps
2377 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002378
Winson Chung64359a52013-07-08 17:17:08 -07002379 // Query for the set of apps
2380 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2381 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2382 if (DEBUG_LOADERS) {
2383 Log.d(TAG, "queryIntentActivities took "
2384 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2385 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2386 }
2387 // Fail if we don't have any apps
2388 if (apps == null || apps.isEmpty()) {
2389 return;
2390 }
2391 // Sort the applications by name
2392 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2393 Collections.sort(apps,
2394 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2395 if (DEBUG_LOADERS) {
2396 Log.d(TAG, "sort took "
2397 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 }
2399
Winson Chung64359a52013-07-08 17:17:08 -07002400 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002401 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002402 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002403 // This builds the icon bitmaps.
2404 mBgAllAppsList.add(new AppInfo(packageManager, app,
2405 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002406 }
2407
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002408 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002409 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2410 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002411
2412 // Post callback on main thread
2413 mHandler.post(new Runnable() {
2414 public void run() {
2415 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002416 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002417 if (callbacks != null) {
2418 callbacks.bindAllApplications(added);
2419 if (DEBUG_LOADERS) {
2420 Log.d(TAG, "bound " + added.size() + " apps in "
2421 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2422 }
2423 } else {
2424 Log.i(TAG, "not binding apps: no Launcher activity");
2425 }
2426 }
2427 });
2428
Joe Onorato36115782010-06-17 13:28:48 -04002429 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002430 Log.d(TAG, "Icons processed in "
2431 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002432 }
2433 }
2434
2435 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002436 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002437 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2438 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2439 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2440 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2441 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2442 }
Joe Onorato36115782010-06-17 13:28:48 -04002443 }
2444 }
2445
2446 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002447 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002448 }
2449
2450 private class PackageUpdatedTask implements Runnable {
2451 int mOp;
2452 String[] mPackages;
2453
2454 public static final int OP_NONE = 0;
2455 public static final int OP_ADD = 1;
2456 public static final int OP_UPDATE = 2;
2457 public static final int OP_REMOVE = 3; // uninstlled
2458 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2459
2460
2461 public PackageUpdatedTask(int op, String[] packages) {
2462 mOp = op;
2463 mPackages = packages;
2464 }
2465
2466 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002467 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002468
2469 final String[] packages = mPackages;
2470 final int N = packages.length;
2471 switch (mOp) {
2472 case OP_ADD:
2473 for (int i=0; i<N; i++) {
2474 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002475 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002476 }
2477 break;
2478 case OP_UPDATE:
2479 for (int i=0; i<N; i++) {
2480 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002481 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002482 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002483 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002484 }
2485 break;
2486 case OP_REMOVE:
2487 case OP_UNAVAILABLE:
2488 for (int i=0; i<N; i++) {
2489 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002490 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002491 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002492 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002493 }
2494 break;
2495 }
2496
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002497 ArrayList<AppInfo> added = null;
2498 ArrayList<AppInfo> modified = null;
2499 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002500
Adam Cohen487f7dd2012-06-28 18:12:10 -07002501 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002502 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002503 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002504 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002505 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002506 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002507 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002508 }
Winson Chung5d55f332012-07-16 20:45:03 -07002509 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002510 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002511 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002512 }
2513
Joe Onorato36115782010-06-17 13:28:48 -04002514 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2515 if (callbacks == null) {
2516 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2517 return;
2518 }
2519
2520 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002521 // Ensure that we add all the workspace applications to the db
2522 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002523 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2524 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2525 } else {
2526 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2527 addAndBindAddedApps(context, addedInfos, cb, added);
2528 }
Joe Onorato36115782010-06-17 13:28:48 -04002529 }
2530 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002531 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002532
2533 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002534 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002535 ArrayList<ItemInfo> infos =
2536 getItemInfoForComponentName(a.componentName);
2537 for (ItemInfo i : infos) {
2538 if (isShortcutInfoUpdateable(i)) {
2539 ShortcutInfo info = (ShortcutInfo) i;
2540 info.title = a.title.toString();
2541 updateItemInDatabase(context, info);
2542 }
2543 }
2544 }
2545
Joe Onorato36115782010-06-17 13:28:48 -04002546 mHandler.post(new Runnable() {
2547 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002548 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2549 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002550 callbacks.bindAppsUpdated(modifiedFinal);
2551 }
2552 }
2553 });
2554 }
Winson Chung83892cc2013-05-01 16:53:33 -07002555
Winson Chungdf95eb12013-10-16 14:57:07 -07002556 final ArrayList<String> removedPackageNames =
2557 new ArrayList<String>();
2558 if (mOp == OP_REMOVE) {
2559 // Mark all packages in the broadcast to be removed
2560 removedPackageNames.addAll(Arrays.asList(packages));
2561 } else if (mOp == OP_UPDATE) {
2562 // Mark disabled packages in the broadcast to be removed
2563 final PackageManager pm = context.getPackageManager();
2564 for (int i=0; i<N; i++) {
2565 if (isPackageDisabled(pm, packages[i])) {
2566 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002567 }
2568 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002569 }
2570 // Remove all the components associated with this package
2571 for (String pn : removedPackageNames) {
2572 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2573 for (ItemInfo i : infos) {
2574 deleteItemFromDatabase(context, i);
2575 }
2576 }
2577 // Remove all the specific components
2578 for (AppInfo a : removedApps) {
2579 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2580 for (ItemInfo i : infos) {
2581 deleteItemFromDatabase(context, i);
2582 }
2583 }
2584 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2585 // Remove any queued items from the install queue
2586 String spKey = LauncherAppState.getSharedPreferencesKey();
2587 SharedPreferences sp =
2588 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2589 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2590 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002591 mHandler.post(new Runnable() {
2592 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002593 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2594 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002595 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002596 }
2597 }
2598 });
Joe Onoratobe386092009-11-17 17:32:16 -08002599 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002600
Michael Jurkac402cd92013-05-20 15:49:32 +02002601 final ArrayList<Object> widgetsAndShortcuts =
2602 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002603 mHandler.post(new Runnable() {
2604 @Override
2605 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002606 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2607 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002608 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002609 }
2610 }
2611 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002612
2613 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002614 mHandler.post(new Runnable() {
2615 public void run() {
2616 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2617 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002618 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002619 }
2620 }
2621 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002622 }
2623 }
2624
Michael Jurkac402cd92013-05-20 15:49:32 +02002625 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2626 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2627 PackageManager packageManager = context.getPackageManager();
2628 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2629 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2630 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2631 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2632 Collections.sort(widgetsAndShortcuts,
2633 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2634 return widgetsAndShortcuts;
2635 }
2636
Winson Chungdf95eb12013-10-16 14:57:07 -07002637 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2638 try {
2639 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2640 return !pi.applicationInfo.enabled;
2641 } catch (NameNotFoundException e) {
2642 // Fall through
2643 }
2644 return false;
2645 }
Winson Chung1323b482013-08-05 12:41:55 -07002646 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002647 if (cn == null) {
2648 return false;
2649 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002650 if (isPackageDisabled(pm, cn.getPackageName())) {
2651 return false;
2652 }
Winson Chungee055712013-07-30 14:46:24 -07002653
2654 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002655 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002656 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002657 return (pm.getActivityInfo(cn, 0) != null);
2658 } catch (NameNotFoundException e) {
2659 return false;
2660 }
2661 }
2662
Joe Onorato9c1289c2009-08-17 11:03:03 -04002663 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002664 * This is called from the code that adds shortcuts from the intent receiver. This
2665 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002666 */
Joe Onorato56d82912010-03-07 14:32:10 -05002667 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002668 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002669 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002670
Joe Onorato56d82912010-03-07 14:32:10 -05002671 /**
2672 * Make an ShortcutInfo object for a shortcut that is an application.
2673 *
2674 * If c is not null, then it will be used to fill in missing data like the title and icon.
2675 */
2676 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002677 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002678 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002679 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002680 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002681 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002682 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002683 } else {
2684 try {
2685 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2686 info.initFlagsAndFirstInstallTime(pi);
2687 } catch (NameNotFoundException e) {
2688 Log.d(TAG, "getPackInfo failed for package " +
2689 componentName.getPackageName());
2690 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002691 }
2692
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002693 // TODO: See if the PackageManager knows about this case. If it doesn't
2694 // then return null & delete this.
2695
Joe Onorato56d82912010-03-07 14:32:10 -05002696 // the resource -- This may implicitly give us back the fallback icon,
2697 // but don't worry about that. All we're doing with usingFallbackIcon is
2698 // to avoid saving lots of copies of that in the database, and most apps
2699 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002700
2701 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2702 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2703 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002704 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002705 ResolveInfo resolveInfo = null;
2706 ComponentName oldComponent = intent.getComponent();
2707 Intent newIntent = new Intent(intent.getAction(), null);
2708 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2709 newIntent.setPackage(oldComponent.getPackageName());
2710 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2711 for (ResolveInfo i : infos) {
2712 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2713 i.activityInfo.name);
2714 if (cn.equals(oldComponent)) {
2715 resolveInfo = i;
2716 }
2717 }
2718 if (resolveInfo == null) {
2719 resolveInfo = manager.resolveActivity(intent, 0);
2720 }
Joe Onorato56d82912010-03-07 14:32:10 -05002721 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002722 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002723 }
Joe Onorato56d82912010-03-07 14:32:10 -05002724 // the db
2725 if (icon == null) {
2726 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002727 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002728 }
2729 }
2730 // the fallback icon
2731 if (icon == null) {
2732 icon = getFallbackIcon();
2733 info.usingFallbackIcon = true;
2734 }
2735 info.setIcon(icon);
2736
2737 // from the resource
2738 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002739 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2740 if (labelCache != null && labelCache.containsKey(key)) {
2741 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002742 } else {
2743 info.title = resolveInfo.activityInfo.loadLabel(manager);
2744 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002745 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002746 }
2747 }
Joe Onorato56d82912010-03-07 14:32:10 -05002748 }
2749 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002751 if (c != null) {
2752 info.title = c.getString(titleIndex);
2753 }
2754 }
2755 // fall back to the class name of the activity
2756 if (info.title == null) {
2757 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2760 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002761 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002762
Winson Chung64359a52013-07-08 17:17:08 -07002763 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2764 ItemInfoFilter f) {
2765 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2766 for (ItemInfo i : infos) {
2767 if (i instanceof ShortcutInfo) {
2768 ShortcutInfo info = (ShortcutInfo) i;
2769 ComponentName cn = info.intent.getComponent();
2770 if (cn != null && f.filterItem(null, info, cn)) {
2771 filtered.add(info);
2772 }
2773 } else if (i instanceof FolderInfo) {
2774 FolderInfo info = (FolderInfo) i;
2775 for (ShortcutInfo s : info.contents) {
2776 ComponentName cn = s.intent.getComponent();
2777 if (cn != null && f.filterItem(info, s, cn)) {
2778 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002779 }
2780 }
Winson Chung64359a52013-07-08 17:17:08 -07002781 } else if (i instanceof LauncherAppWidgetInfo) {
2782 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2783 ComponentName cn = info.providerName;
2784 if (cn != null && f.filterItem(null, info, cn)) {
2785 filtered.add(info);
2786 }
Winson Chung8a435102012-08-30 17:16:53 -07002787 }
2788 }
Winson Chung64359a52013-07-08 17:17:08 -07002789 return new ArrayList<ItemInfo>(filtered);
2790 }
2791
2792 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002793 ItemInfoFilter filter = new ItemInfoFilter() {
2794 @Override
2795 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2796 return cn.getPackageName().equals(pn);
2797 }
2798 };
2799 return filterItemInfos(sBgItemsIdMap.values(), filter);
2800 }
2801
2802 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002803 ItemInfoFilter filter = new ItemInfoFilter() {
2804 @Override
2805 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2806 return cn.equals(cname);
2807 }
2808 };
2809 return filterItemInfos(sBgItemsIdMap.values(), filter);
2810 }
2811
2812 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2813 if (i instanceof ShortcutInfo) {
2814 ShortcutInfo info = (ShortcutInfo) i;
2815 // We need to check for ACTION_MAIN otherwise getComponent() might
2816 // return null for some shortcuts (for instance, for shortcuts to
2817 // web pages.)
2818 Intent intent = info.intent;
2819 ComponentName name = intent.getComponent();
2820 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2821 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2822 return true;
2823 }
2824 }
2825 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002826 }
2827
2828 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002829 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002830 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002831 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002832 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2833 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834
Joe Onorato56d82912010-03-07 14:32:10 -05002835 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002836 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002839 // TODO: If there's an explicit component and we can't install that, delete it.
2840
Joe Onorato56d82912010-03-07 14:32:10 -05002841 info.title = c.getString(titleIndex);
2842
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 int iconType = c.getInt(iconTypeIndex);
2844 switch (iconType) {
2845 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2846 String packageName = c.getString(iconPackageIndex);
2847 String resourceName = c.getString(iconResourceIndex);
2848 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002849 info.customIcon = false;
2850 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002853 if (resources != null) {
2854 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002855 icon = Utilities.createIconBitmap(
2856 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002859 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
Joe Onorato56d82912010-03-07 14:32:10 -05002861 // the db
2862 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002863 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002864 }
2865 // the fallback icon
2866 if (icon == null) {
2867 icon = getFallbackIcon();
2868 info.usingFallbackIcon = true;
2869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 break;
2871 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002872 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002873 if (icon == null) {
2874 icon = getFallbackIcon();
2875 info.customIcon = false;
2876 info.usingFallbackIcon = true;
2877 } else {
2878 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 break;
2881 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002882 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002883 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 info.customIcon = false;
2885 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002887 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 return info;
2889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890
Michael Jurka931dc972011-08-05 15:08:15 -07002891 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002892 @SuppressWarnings("all") // suppress dead code warning
2893 final boolean debug = false;
2894 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002895 Log.d(TAG, "getIconFromCursor app="
2896 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2897 }
2898 byte[] data = c.getBlob(iconIndex);
2899 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002900 return Utilities.createIconBitmap(
2901 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002902 } catch (Exception e) {
2903 return null;
2904 }
2905 }
2906
Winson Chung3d503fb2011-07-13 17:25:49 -07002907 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2908 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002909 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002910 if (info == null) {
2911 return null;
2912 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002913 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002914
2915 return info;
2916 }
2917
Winson Chunga9abd0e2010-10-27 17:18:37 -07002918 /**
Winson Chung55cef262010-10-28 14:14:18 -07002919 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2920 */
2921 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2922 ComponentName component) {
2923 List<AppWidgetProviderInfo> widgets =
2924 AppWidgetManager.getInstance(context).getInstalledProviders();
2925 for (AppWidgetProviderInfo info : widgets) {
2926 if (info.provider.equals(component)) {
2927 return info;
2928 }
2929 }
2930 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002931 }
2932
Winson Chung68846fd2010-10-29 11:00:27 -07002933 /**
2934 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2935 */
2936 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2937 final PackageManager packageManager = context.getPackageManager();
2938 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2939 new ArrayList<WidgetMimeTypeHandlerData>();
2940
2941 final Intent supportsIntent =
2942 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2943 supportsIntent.setType(mimeType);
2944
2945 // Create a set of widget configuration components that we can test against
2946 final List<AppWidgetProviderInfo> widgets =
2947 AppWidgetManager.getInstance(context).getInstalledProviders();
2948 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2949 new HashMap<ComponentName, AppWidgetProviderInfo>();
2950 for (AppWidgetProviderInfo info : widgets) {
2951 configurationComponentToWidget.put(info.configure, info);
2952 }
2953
2954 // Run through each of the intents that can handle this type of clip data, and cross
2955 // reference them with the components that are actual configuration components
2956 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2957 PackageManager.MATCH_DEFAULT_ONLY);
2958 for (ResolveInfo info : activities) {
2959 final ActivityInfo activityInfo = info.activityInfo;
2960 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2961 activityInfo.name);
2962 if (configurationComponentToWidget.containsKey(infoComponent)) {
2963 supportedConfigurationActivities.add(
2964 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2965 configurationComponentToWidget.get(infoComponent)));
2966 }
2967 }
2968 return supportedConfigurationActivities;
2969 }
2970
Winson Chunga9abd0e2010-10-27 17:18:37 -07002971 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002972 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2973 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2974 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2975
Adam Cohend9198822011-11-22 16:42:47 -08002976 if (intent == null) {
2977 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2978 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2979 return null;
2980 }
2981
Joe Onorato0589f0f2010-02-08 13:44:00 -08002982 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002983 boolean customIcon = false;
2984 ShortcutIconResource iconResource = null;
2985
2986 if (bitmap != null && bitmap instanceof Bitmap) {
2987 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002988 customIcon = true;
2989 } else {
2990 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2991 if (extra != null && extra instanceof ShortcutIconResource) {
2992 try {
2993 iconResource = (ShortcutIconResource) extra;
2994 final PackageManager packageManager = context.getPackageManager();
2995 Resources resources = packageManager.getResourcesForApplication(
2996 iconResource.packageName);
2997 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002998 icon = Utilities.createIconBitmap(
2999 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003000 } catch (Exception e) {
3001 Log.w(TAG, "Could not load shortcut icon: " + extra);
3002 }
3003 }
3004 }
3005
Michael Jurkac9d95c52011-08-29 14:03:34 -07003006 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003007
3008 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003009 if (fallbackIcon != null) {
3010 icon = fallbackIcon;
3011 } else {
3012 icon = getFallbackIcon();
3013 info.usingFallbackIcon = true;
3014 }
Joe Onorato56d82912010-03-07 14:32:10 -05003015 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003016 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003017
Joe Onorato0589f0f2010-02-08 13:44:00 -08003018 info.title = name;
3019 info.intent = intent;
3020 info.customIcon = customIcon;
3021 info.iconResource = iconResource;
3022
3023 return info;
3024 }
3025
Winson Chungaac01e12011-08-17 10:37:13 -07003026 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3027 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003028 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003029 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003030 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003031 }
Joe Onorato56d82912010-03-07 14:32:10 -05003032 // If this icon doesn't have a custom icon, check to see
3033 // what's stored in the DB, and if it doesn't match what
3034 // we're going to show, store what we are going to show back
3035 // into the DB. We do this so when we're loading, if the
3036 // package manager can't find an icon (for example because
3037 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003038 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003039 cache.put(info, c.getBlob(iconIndex));
3040 return true;
3041 }
3042 return false;
3043 }
3044 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3045 boolean needSave = false;
3046 try {
3047 if (data != null) {
3048 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3049 Bitmap loaded = info.getIcon(mIconCache);
3050 needSave = !saved.sameAs(loaded);
3051 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003052 needSave = true;
3053 }
Winson Chungaac01e12011-08-17 10:37:13 -07003054 } catch (Exception e) {
3055 needSave = true;
3056 }
3057 if (needSave) {
3058 Log.d(TAG, "going to save icon bitmap for info=" + info);
3059 // This is slower than is ideal, but this only happens once
3060 // or when the app is updated with a new icon.
3061 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003062 }
3063 }
3064
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003066 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 * or make a new one.
3068 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003069 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 // See if a placeholder was created for us already
3071 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003072 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003074 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 folders.put(id, folderInfo);
3076 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003077 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078 }
3079
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003080 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003081 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003082 return new Comparator<AppInfo>() {
3083 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003084 int result = collator.compare(a.title.toString().trim(),
3085 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003086 if (result == 0) {
3087 result = a.componentName.compareTo(b.componentName);
3088 }
3089 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003090 }
Winson Chung11904872012-09-17 16:58:46 -07003091 };
3092 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3094 = new Comparator<AppInfo>() {
3095 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003096 if (a.firstInstallTime < b.firstInstallTime) return 1;
3097 if (a.firstInstallTime > b.firstInstallTime) return -1;
3098 return 0;
3099 }
3100 };
Winson Chung11904872012-09-17 16:58:46 -07003101 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3102 final Collator collator = Collator.getInstance();
3103 return new Comparator<AppWidgetProviderInfo>() {
3104 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003105 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003106 }
3107 };
3108 }
Winson Chung5308f242011-08-18 12:12:41 -07003109 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3110 if (info.activityInfo != null) {
3111 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3112 } else {
3113 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3114 }
3115 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003116 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003117 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003118 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003119 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003120 ShortcutNameComparator(PackageManager pm) {
3121 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003122 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003123 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003124 }
3125 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3126 mPackageManager = pm;
3127 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003128 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003129 }
3130 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003131 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003132 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3133 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3134 if (mLabelCache.containsKey(keyA)) {
3135 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003136 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003137 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003138
Winson Chung5308f242011-08-18 12:12:41 -07003139 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003140 }
Winson Chung5308f242011-08-18 12:12:41 -07003141 if (mLabelCache.containsKey(keyB)) {
3142 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003143 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003144 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003145
Winson Chung5308f242011-08-18 12:12:41 -07003146 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003147 }
Winson Chung11904872012-09-17 16:58:46 -07003148 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003149 }
3150 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003151 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003152 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003153 private PackageManager mPackageManager;
3154 private HashMap<Object, String> mLabelCache;
3155 WidgetAndShortcutNameComparator(PackageManager pm) {
3156 mPackageManager = pm;
3157 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003158 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003159 }
3160 public final int compare(Object a, Object b) {
3161 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003162 if (mLabelCache.containsKey(a)) {
3163 labelA = mLabelCache.get(a);
3164 } else {
3165 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003166 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003167 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003168 mLabelCache.put(a, labelA);
3169 }
3170 if (mLabelCache.containsKey(b)) {
3171 labelB = mLabelCache.get(b);
3172 } else {
3173 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003174 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003175 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003176 mLabelCache.put(b, labelB);
3177 }
Winson Chung11904872012-09-17 16:58:46 -07003178 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003179 }
3180 };
Joe Onoratobe386092009-11-17 17:32:16 -08003181
3182 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003183 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003184 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3185 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3186 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3187 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003188 if (mLoaderTask != null) {
3189 mLoaderTask.dumpState();
3190 } else {
3191 Log.d(TAG, "mLoaderTask=null");
3192 }
Joe Onoratobe386092009-11-17 17:32:16 -08003193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194}