blob: ba1bcdb1ea031d46b808861ba841c19017578c2f [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,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800285 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
286 if (workspaceApps == null || allAppsApps == null) {
287 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
288 }
Winson Chung94d67682013-09-25 16:29:40 -0700289 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700290 return;
Winson Chung997a9232013-07-24 15:33:46 -0700291 }
Winson Chung64359a52013-07-08 17:17:08 -0700292 // Process the newly added applications and add them to the database first
293 Runnable r = new Runnable() {
294 public void run() {
295 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
296 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
297
Winson Chung76828c82013-08-19 15:43:29 -0700298 // Get the list of workspace screens. We need to append to this list and
299 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
300 // called.
301 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
302 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
303 for (Integer i : orderedScreens.keySet()) {
304 long screenId = orderedScreens.get(i);
305 workspaceScreens.add(screenId);
306 }
307
Winson Chung64359a52013-07-08 17:17:08 -0700308 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700309 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700310 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700311 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700312 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700313 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700314
315 // Short-circuit this logic if the icon exists somewhere on the workspace
316 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
317 continue;
318 }
319
Winson Chung87412982013-10-03 18:34:14 -0700320 // Add this icon to the db, creating a new page if necessary. If there
321 // is only the empty page then we just add items to the first page.
322 // Otherwise, we add them to the next pages.
323 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700324 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700325 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700326 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200327 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700328
329 // If we can't find a valid position, then just add a new screen.
330 // This takes time so we need to re-queue the add until the new
331 // page is added. Create as many screens as necessary to satisfy
332 // the startSearchPageIndex.
333 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700334 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 while (numPagesToAdd > 0) {
336 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700338 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700339 addedWorkspaceScreensFinal.add(screenId);
340 numPagesToAdd--;
341 }
Winson Chung76828c82013-08-19 15:43:29 -0700342
Winson Chung64359a52013-07-08 17:17:08 -0700343 // Find the coordinate again
344 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700345 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700346 }
347 if (coords == null) {
348 throw new RuntimeException("Coordinates should not be null");
349 }
350
Winson Chung997a9232013-07-24 15:33:46 -0700351 ShortcutInfo shortcutInfo;
352 if (a instanceof ShortcutInfo) {
353 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200354 } else if (a instanceof AppInfo) {
355 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700356 } else {
357 throw new RuntimeException("Unexpected info type");
358 }
Winson Chung94d67682013-09-25 16:29:40 -0700359
Winson Chung64359a52013-07-08 17:17:08 -0700360 // Add the shortcut to the db
361 addItemToDatabase(context, shortcutInfo,
362 LauncherSettings.Favorites.CONTAINER_DESKTOP,
363 coords.first, coords.second[0], coords.second[1], false);
364 // Save the ShortcutInfo for binding in the workspace
365 addedShortcutsFinal.add(shortcutInfo);
366 }
367 }
368
Winson Chung76828c82013-08-19 15:43:29 -0700369 // Update the workspace screens
370 updateWorkspaceScreenOrder(context, workspaceScreens);
371
Winson Chung94d67682013-09-25 16:29:40 -0700372 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700373 runOnMainThread(new Runnable() {
374 public void run() {
375 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
376 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700377 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
378 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700379 if (!addedShortcutsFinal.isEmpty()) {
380 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
381 long lastScreenId = info.screenId;
382 for (ItemInfo i : addedShortcutsFinal) {
383 if (i.screenId == lastScreenId) {
384 addAnimated.add(i);
385 } else {
386 addNotAnimated.add(i);
387 }
Winson Chung997a9232013-07-24 15:33:46 -0700388 }
389 }
Winson Chungd64d1762013-08-20 14:37:16 -0700390 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700391 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700392 }
Winson Chung64359a52013-07-08 17:17:08 -0700393 }
Winson Chung997a9232013-07-24 15:33:46 -0700394 });
395 }
Winson Chung64359a52013-07-08 17:17:08 -0700396 }
397 };
398 runOnWorkerThread(r);
399 }
400
Joe Onorato56d82912010-03-07 14:32:10 -0500401 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700402 if (mDefaultIcon == null) {
403 final Context context = LauncherAppState.getInstance().getContext();
404 mDefaultIcon = Utilities.createIconBitmap(
405 mIconCache.getFullResDefaultActivityIcon(), context);
406 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409
Winson Chung81b52252012-08-27 15:34:29 -0700410 public void unbindItemInfosAndClearQueuedBindRunnables() {
411 if (sWorkerThread.getThreadId() == Process.myTid()) {
412 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
413 "main thread");
414 }
415
416 // Clear any deferred bind runnables
417 mDeferredBindRunnables.clear();
418 // Remove any queued bind runnables
419 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
420 // Unbind all the workspace items
421 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700422 }
423
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700424 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700425 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700426 // Ensure that we don't use the same workspace items data structure on the main thread
427 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700428 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
429 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700430 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
432 tmpAppWidgets.addAll(sBgAppWidgets);
433 }
434 Runnable r = new Runnable() {
435 @Override
436 public void run() {
437 for (ItemInfo item : tmpWorkspaceItems) {
438 item.unbind();
439 }
440 for (ItemInfo item : tmpAppWidgets) {
441 item.unbind();
442 }
443 }
444 };
445 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700446 }
447
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 /**
449 * Adds an item to the DB if it was not created previously, or move it to a new
450 * <container, screen, cellX, cellY>
451 */
452 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700453 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400454 if (item.container == ItemInfo.NO_ID) {
455 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700456 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 } else {
458 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700459 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461 }
462
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700463 static void checkItemInfoLocked(
464 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
465 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
466 if (modelItem != null && item != modelItem) {
467 // check all the data is consistent
468 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
469 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
470 ShortcutInfo shortcut = (ShortcutInfo) item;
471 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
472 modelShortcut.intent.filterEquals(shortcut.intent) &&
473 modelShortcut.id == shortcut.id &&
474 modelShortcut.itemType == shortcut.itemType &&
475 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700476 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700477 modelShortcut.cellX == shortcut.cellX &&
478 modelShortcut.cellY == shortcut.cellY &&
479 modelShortcut.spanX == shortcut.spanX &&
480 modelShortcut.spanY == shortcut.spanY &&
481 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
482 (modelShortcut.dropPos != null &&
483 shortcut.dropPos != null &&
484 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
485 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
486 // For all intents and purposes, this is the same object
487 return;
488 }
489 }
490
491 // the modelItem needs to match up perfectly with item if our model is
492 // to be consistent with the database-- for now, just require
493 // modelItem == item or the equality check above
494 String msg = "item: " + ((item != null) ? item.toString() : "null") +
495 "modelItem: " +
496 ((modelItem != null) ? modelItem.toString() : "null") +
497 "Error: ItemInfo passed to checkItemInfo doesn't match original";
498 RuntimeException e = new RuntimeException(msg);
499 if (stackTrace != null) {
500 e.setStackTrace(stackTrace);
501 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800502 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700503 }
504 }
505
Michael Jurka816474f2012-06-25 14:49:02 -0700506 static void checkItemInfo(final ItemInfo item) {
507 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
508 final long itemId = item.id;
509 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700510 public void run() {
511 synchronized (sBgLock) {
512 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700513 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700514 }
515 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700516 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700517 }
518
Michael Jurkac9d95c52011-08-29 14:03:34 -0700519 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
520 final ItemInfo item, final String callingFunction) {
521 final long itemId = item.id;
522 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
523 final ContentResolver cr = context.getContentResolver();
524
Adam Cohen487f7dd2012-06-28 18:12:10 -0700525 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700526 Runnable r = new Runnable() {
527 public void run() {
528 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700529 updateItemArrays(item, itemId, stackTrace);
530 }
531 };
532 runOnWorkerThread(r);
533 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700534
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700535 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
536 final ArrayList<ItemInfo> items, final String callingFunction) {
537 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700538
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700539 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
540 Runnable r = new Runnable() {
541 public void run() {
542 ArrayList<ContentProviderOperation> ops =
543 new ArrayList<ContentProviderOperation>();
544 int count = items.size();
545 for (int i = 0; i < count; i++) {
546 ItemInfo item = items.get(i);
547 final long itemId = item.id;
548 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
549 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700550
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700551 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
552 updateItemArrays(item, itemId, stackTrace);
553
554 }
555 try {
556 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
557 } catch (Exception e) {
558 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700559 }
560 }
561 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700562 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700563 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700564
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700565 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
566 // Lock on mBgLock *after* the db operation
567 synchronized (sBgLock) {
568 checkItemInfoLocked(itemId, item, stackTrace);
569
570 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
571 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
572 // Item is in a folder, make sure this folder exists
573 if (!sBgFolders.containsKey(item.container)) {
574 // An items container is being set to a that of an item which is not in
575 // the list of Folders.
576 String msg = "item: " + item + " container being set to: " +
577 item.container + ", not in the list of folders";
578 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700579 }
580 }
581
582 // Items are added/removed from the corresponding FolderInfo elsewhere, such
583 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
584 // that are on the desktop, as appropriate
585 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
586 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
587 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
588 switch (modelItem.itemType) {
589 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
590 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
591 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
592 if (!sBgWorkspaceItems.contains(modelItem)) {
593 sBgWorkspaceItems.add(modelItem);
594 }
595 break;
596 default:
597 break;
598 }
599 } else {
600 sBgWorkspaceItems.remove(modelItem);
601 }
602 }
603 }
604
Michael Jurkac7700af2013-05-14 20:17:58 +0200605 public void flushWorkerThread() {
606 mFlushingWorkerThread = true;
607 Runnable waiter = new Runnable() {
608 public void run() {
609 synchronized (this) {
610 notifyAll();
611 mFlushingWorkerThread = false;
612 }
613 }
614 };
615
616 synchronized(waiter) {
617 runOnWorkerThread(waiter);
618 if (mLoaderTask != null) {
619 synchronized(mLoaderTask) {
620 mLoaderTask.notify();
621 }
622 }
623 boolean success = false;
624 while (!success) {
625 try {
626 waiter.wait();
627 success = true;
628 } catch (InterruptedException e) {
629 }
630 }
631 }
632 }
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700636 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700637 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700638 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 item.cellX = cellX;
641 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 // We store hotseat items in canonical form which is this orientation invariant position
644 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651
652 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
655 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700656 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657
Michael Jurkac9d95c52011-08-29 14:03:34 -0700658 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700659 }
660
661 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
663 * cellX, cellY have already been updated on the ItemInfos.
664 */
665 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
666 final long container, final int screen) {
667
668 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
669 int count = items.size();
670
671 for (int i = 0; i < count; i++) {
672 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700673 item.container = container;
674
675 // We store hotseat items in canonical form which is this orientation invariant position
676 // in the hotseat
677 if (context instanceof Launcher && screen < 0 &&
678 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700679 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 item.cellY);
681 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683 }
684
685 final ContentValues values = new ContentValues();
686 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
687 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
688 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700689 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690
691 contentValues.add(values);
692 }
693 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
694 }
695
696 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700697 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800698 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700700 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700701 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800702 item.cellX = cellX;
703 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700704 item.spanX = spanX;
705 item.spanY = spanY;
706
707 // We store hotseat items in canonical form which is this orientation invariant position
708 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 }
Adam Cohend4844c32011-02-18 19:25:06 -0800715
Adam Cohend4844c32011-02-18 19:25:06 -0800716 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800717 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
719 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
720 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
721 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800723
Michael Jurka816474f2012-06-25 14:49:02 -0700724 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700725 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700726
727 /**
728 * Update an item to the database in a specified container.
729 */
730 static void updateItemInDatabase(Context context, final ItemInfo item) {
731 final ContentValues values = new ContentValues();
732 item.onAddToDatabase(values);
733 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
734 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800735 }
736
737 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 * Returns true if the shortcuts already exists in the database.
739 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 static boolean shortcutExists(Context context, String title, Intent intent) {
742 final ContentResolver cr = context.getContentResolver();
743 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
744 new String[] { "title", "intent" }, "title=? and intent=?",
745 new String[] { title, intent.toUri(0) }, null);
746 boolean result = false;
747 try {
748 result = c.moveToFirst();
749 } finally {
750 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700753 }
754
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700756 * Returns an ItemInfo array containing all the items in the LauncherModel.
757 * The ItemInfo.id is not set through this function.
758 */
759 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
760 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
761 final ContentResolver cr = context.getContentResolver();
762 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
763 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
764 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
765 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
766
767 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
768 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
769 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
770 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
771 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
772 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
773 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
774
775 try {
776 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700777 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700778 item.cellX = c.getInt(cellXIndex);
779 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700780 item.spanX = Math.max(1, c.getInt(spanXIndex));
781 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700782 item.container = c.getInt(containerIndex);
783 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700784 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700785
786 items.add(item);
787 }
788 } catch (Exception e) {
789 items.clear();
790 } finally {
791 c.close();
792 }
793
794 return items;
795 }
796
797 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
799 */
800 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
801 final ContentResolver cr = context.getContentResolver();
802 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
803 "_id=? and (itemType=? or itemType=?)",
804 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700805 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700806
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807 try {
808 if (c.moveToFirst()) {
809 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
810 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
811 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
812 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
813 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
814 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816 FolderInfo folderInfo = null;
817 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700818 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
819 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700821 }
822
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 folderInfo.title = c.getString(titleIndex);
824 folderInfo.id = id;
825 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700827 folderInfo.cellX = c.getInt(cellXIndex);
828 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829
830 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832 } finally {
833 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700834 }
835
836 return null;
837 }
838
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839 /**
840 * Add an item to the database in a specified container. Sets the container, screen, cellX and
841 * cellY fields of the item. Also assigns an ID to the item.
842 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 item.cellX = cellX;
847 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 // We store hotseat items in canonical form which is this orientation invariant position
849 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856
857 final ContentValues values = new ContentValues();
858 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 item.onAddToDatabase(values);
860
Michael Jurka414300a2013-08-27 15:42:35 +0200861 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700862 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700864
Michael Jurkac9d95c52011-08-29 14:03:34 -0700865 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700866 public void run() {
867 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
868 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700871 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700872 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700873 sBgItemsIdMap.put(item.id, item);
874 switch (item.itemType) {
875 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
876 sBgFolders.put(item.id, (FolderInfo) item);
877 // Fall through
878 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
879 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
880 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
881 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
882 sBgWorkspaceItems.add(item);
883 } else {
884 if (!sBgFolders.containsKey(item.container)) {
885 // Adding an item to a folder that doesn't exist.
886 String msg = "adding item: " + item + " to a folder that " +
887 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700888 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700891 break;
892 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
893 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
894 break;
895 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700896 }
897 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700898 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700899 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700900 }
901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700903 * Creates a new unique child id, for a given cell span across all layouts.
904 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700905 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700907 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700908 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700909 }
910
Winson Chungaafa03c2010-06-11 17:34:16 -0700911 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 * Removes the specified item from the database
913 * @param context
914 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700916 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700918 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700919
Michael Jurka83df1882011-08-31 20:59:26 -0700920 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700921 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700922 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923
924 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700925 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700926 switch (item.itemType) {
927 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
928 sBgFolders.remove(item.id);
929 for (ItemInfo info: sBgItemsIdMap.values()) {
930 if (info.container == item.id) {
931 // We are deleting a folder which still contains items that
932 // think they are contained by that folder.
933 String msg = "deleting a folder (" + item + ") which still " +
934 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700935 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700937 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700938 sBgWorkspaceItems.remove(item);
939 break;
940 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
941 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
942 sBgWorkspaceItems.remove(item);
943 break;
944 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
945 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
946 break;
947 }
948 sBgItemsIdMap.remove(item.id);
949 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700950 }
951 }
Michael Jurka83df1882011-08-31 20:59:26 -0700952 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700953 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400954 }
955
956 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 * Update the order of the workspace screens in the database. The array list contains
958 * a list of screen ids in the order that they should appear.
959 */
Winson Chungc9168342013-06-26 14:54:55 -0700960 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700961 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 final ContentResolver cr = context.getContentResolver();
963 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
964
965 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700966 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 while (iter.hasNext()) {
968 long id = iter.next();
969 if (id < 0) {
970 iter.remove();
971 }
972 }
973
974 Runnable r = new Runnable() {
975 @Override
976 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 // Clear the table
978 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700979 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700980 ContentValues[] values = new ContentValues[count];
981 for (int i = 0; i < count; i++) {
982 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700983 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
985 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 values[i] = v;
987 }
988 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700989
Winson Chungba9c37f2013-08-30 14:11:37 -0700990 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700991 sBgWorkspaceScreens.clear();
992 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700993 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 }
995 };
996 runOnWorkerThread(r);
997 }
998
999 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 * Remove the contents of the specified folder from the database
1001 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001002 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 final ContentResolver cr = context.getContentResolver();
1004
Michael Jurkac9d95c52011-08-29 14:03:34 -07001005 Runnable r = new Runnable() {
1006 public void run() {
1007 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001008 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001009 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 sBgItemsIdMap.remove(info.id);
1011 sBgFolders.remove(info.id);
1012 sBgDbIconCache.remove(info);
1013 sBgWorkspaceItems.remove(info);
1014 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001015
Michael Jurkac9d95c52011-08-29 14:03:34 -07001016 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1017 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001019 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 for (ItemInfo childInfo : info.contents) {
1021 sBgItemsIdMap.remove(childInfo.id);
1022 sBgDbIconCache.remove(childInfo);
1023 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001024 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001025 }
1026 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 }
1029
1030 /**
1031 * Set this as the current Launcher activity object for the loader.
1032 */
1033 public void initialize(Callbacks callbacks) {
1034 synchronized (mLock) {
1035 mCallbacks = new WeakReference<Callbacks>(callbacks);
1036 }
1037 }
1038
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001039 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1041 * ACTION_PACKAGE_CHANGED.
1042 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001043 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001044 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001045 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001046
Joe Onorato36115782010-06-17 13:28:48 -04001047 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001048
Joe Onorato36115782010-06-17 13:28:48 -04001049 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1050 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1051 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1052 final String packageName = intent.getData().getSchemeSpecificPart();
1053 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 int op = PackageUpdatedTask.OP_NONE;
1056
1057 if (packageName == null || packageName.length() == 0) {
1058 // they sent us a bad intent
1059 return;
1060 }
1061
1062 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1063 op = PackageUpdatedTask.OP_UPDATE;
1064 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1065 if (!replacing) {
1066 op = PackageUpdatedTask.OP_REMOVE;
1067 }
1068 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1069 // later, we will update the package at this time
1070 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1071 if (!replacing) {
1072 op = PackageUpdatedTask.OP_ADD;
1073 } else {
1074 op = PackageUpdatedTask.OP_UPDATE;
1075 }
1076 }
1077
1078 if (op != PackageUpdatedTask.OP_NONE) {
1079 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1080 }
1081
1082 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001083 // First, schedule to add these apps back in.
1084 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1085 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1086 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001087 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001088 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1089 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1090 enqueuePackageUpdated(new PackageUpdatedTask(
1091 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001092 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001093 // If we have changed locale we need to clear out the labels in all apps/workspace.
1094 forceReload();
1095 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1096 // Check if configuration change was an mcc/mnc change which would affect app resources
1097 // and we would need to clear out the labels in all apps/workspace. Same handling as
1098 // above for ACTION_LOCALE_CHANGED
1099 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001100 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001101 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001102 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001103 forceReload();
1104 }
1105 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001106 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001107 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1108 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001109 if (mCallbacks != null) {
1110 Callbacks callbacks = mCallbacks.get();
1111 if (callbacks != null) {
1112 callbacks.bindSearchablesChanged();
1113 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001114 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001115 }
1116 }
1117
Reena Lee93f824a2011-09-23 17:20:28 -07001118 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001119 resetLoadedState(true, true);
1120
Reena Lee93f824a2011-09-23 17:20:28 -07001121 // Do this here because if the launcher activity is running it will be restarted.
1122 // If it's not running startLoaderFromBackground will merely tell it that it needs
1123 // to reload.
1124 startLoaderFromBackground();
1125 }
1126
Winson Chungf0c6ae02012-03-21 16:10:31 -07001127 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1128 synchronized (mLock) {
1129 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1130 // mWorkspaceLoaded to true later
1131 stopLoaderLocked();
1132 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1133 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1134 }
1135 }
1136
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001137 /**
1138 * When the launcher is in the background, it's possible for it to miss paired
1139 * configuration changes. So whenever we trigger the loader from the background
1140 * tell the launcher that it needs to re-run the loader when it comes back instead
1141 * of doing it now.
1142 */
1143 public void startLoaderFromBackground() {
1144 boolean runLoader = false;
1145 if (mCallbacks != null) {
1146 Callbacks callbacks = mCallbacks.get();
1147 if (callbacks != null) {
1148 // Only actually run the loader if they're not paused.
1149 if (!callbacks.setLoadOnResume()) {
1150 runLoader = true;
1151 }
1152 }
1153 }
1154 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001155 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001156 }
Joe Onorato36115782010-06-17 13:28:48 -04001157 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001158
Reena Lee93f824a2011-09-23 17:20:28 -07001159 // If there is already a loader task running, tell it to stop.
1160 // returns true if isLaunching() was true on the old task
1161 private boolean stopLoaderLocked() {
1162 boolean isLaunching = false;
1163 LoaderTask oldTask = mLoaderTask;
1164 if (oldTask != null) {
1165 if (oldTask.isLaunching()) {
1166 isLaunching = true;
1167 }
1168 oldTask.stopLocked();
1169 }
1170 return isLaunching;
1171 }
1172
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001173 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001174 synchronized (mLock) {
1175 if (DEBUG_LOADERS) {
1176 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 // Clear any deferred bind-runnables from the synchronized load process
1180 // We must do this before any loading/binding is scheduled below.
1181 mDeferredBindRunnables.clear();
1182
Joe Onorato36115782010-06-17 13:28:48 -04001183 // Don't bother to start the thread if we know it's not going to do anything
1184 if (mCallbacks != null && mCallbacks.get() != null) {
1185 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001186 // also, don't downgrade isLaunching if we're already running
1187 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001188 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001189 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1190 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1191 } else {
1192 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1193 sWorker.post(mLoaderTask);
1194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001196 }
1197 }
1198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 void bindRemainingSynchronousPages() {
1200 // Post the remaining side pages to be loaded
1201 if (!mDeferredBindRunnables.isEmpty()) {
1202 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001203 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204 }
1205 mDeferredBindRunnables.clear();
1206 }
1207 }
1208
Joe Onorato36115782010-06-17 13:28:48 -04001209 public void stopLoader() {
1210 synchronized (mLock) {
1211 if (mLoaderTask != null) {
1212 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 }
1214 }
Joe Onorato36115782010-06-17 13:28:48 -04001215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001216
Winson Chung76828c82013-08-19 15:43:29 -07001217 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1218 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1219 final ContentResolver contentResolver = context.getContentResolver();
1220 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1221 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1222 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1223
1224 try {
1225 final int idIndex = sc.getColumnIndexOrThrow(
1226 LauncherSettings.WorkspaceScreens._ID);
1227 final int rankIndex = sc.getColumnIndexOrThrow(
1228 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1229 while (sc.moveToNext()) {
1230 try {
1231 long screenId = sc.getLong(idIndex);
1232 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001233 orderedScreens.put(rank, screenId);
1234 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001235 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001236 }
1237 }
1238 } finally {
1239 sc.close();
1240 }
1241 return orderedScreens;
1242 }
1243
Michael Jurkac57b7a82011-08-09 22:02:20 -07001244 public boolean isAllAppsLoaded() {
1245 return mAllAppsLoaded;
1246 }
1247
Winson Chung36a62fe2012-05-06 18:04:42 -07001248 boolean isLoadingWorkspace() {
1249 synchronized (mLock) {
1250 if (mLoaderTask != null) {
1251 return mLoaderTask.isLoadingWorkspace();
1252 }
1253 }
1254 return false;
1255 }
1256
Joe Onorato36115782010-06-17 13:28:48 -04001257 /**
1258 * Runnable for the thread that loads the contents of the launcher:
1259 * - workspace icons
1260 * - widgets
1261 * - all apps icons
1262 */
1263 private class LoaderTask implements Runnable {
1264 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001265 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001266 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001267 private boolean mStopped;
1268 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001269
Winson Chungc3eecff2011-07-11 17:44:15 -07001270 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001271
1272 LoaderTask(Context context, boolean isLaunching) {
1273 mContext = context;
1274 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001275 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 }
1277
Joe Onorato36115782010-06-17 13:28:48 -04001278 boolean isLaunching() {
1279 return mIsLaunching;
1280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281
Winson Chung36a62fe2012-05-06 18:04:42 -07001282 boolean isLoadingWorkspace() {
1283 return mIsLoadingAndBindingWorkspace;
1284 }
1285
Winson Chungc763c4e2013-07-19 13:49:06 -07001286 /** Returns whether this is an upgrade path */
1287 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001288 mIsLoadingAndBindingWorkspace = true;
1289
Joe Onorato36115782010-06-17 13:28:48 -04001290 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001291 if (DEBUG_LOADERS) {
1292 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001293 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001294
Winson Chungc763c4e2013-07-19 13:49:06 -07001295 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001296 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001297 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001298 synchronized (LoaderTask.this) {
1299 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001300 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001301 }
1302 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 }
1304 }
1305
Joe Onorato36115782010-06-17 13:28:48 -04001306 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001307 bindWorkspace(-1, isUpgradePath);
1308 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001309 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001310
Joe Onorato36115782010-06-17 13:28:48 -04001311 private void waitForIdle() {
1312 // Wait until the either we're stopped or the other threads are done.
1313 // This way we don't start loading all apps until the workspace has settled
1314 // down.
1315 synchronized (LoaderTask.this) {
1316 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001317
Joe Onorato36115782010-06-17 13:28:48 -04001318 mHandler.postIdle(new Runnable() {
1319 public void run() {
1320 synchronized (LoaderTask.this) {
1321 mLoadAndBindStepFinished = true;
1322 if (DEBUG_LOADERS) {
1323 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001326 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001327 }
Joe Onorato36115782010-06-17 13:28:48 -04001328 });
1329
Michael Jurkac7700af2013-05-14 20:17:58 +02001330 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001331 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001332 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1333 // wait no longer than 1sec at a time
1334 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001335 } catch (InterruptedException ex) {
1336 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001337 }
1338 }
Joe Onorato36115782010-06-17 13:28:48 -04001339 if (DEBUG_LOADERS) {
1340 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001341 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001342 + "ms for previous step to finish binding");
1343 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001344 }
Joe Onorato36115782010-06-17 13:28:48 -04001345 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001346
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001347 void runBindSynchronousPage(int synchronousBindPage) {
1348 if (synchronousBindPage < 0) {
1349 // Ensure that we have a valid page index to load synchronously
1350 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1351 "valid page index");
1352 }
1353 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1354 // Ensure that we don't try and bind a specified page when the pages have not been
1355 // loaded already (we should load everything asynchronously in that case)
1356 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1357 }
1358 synchronized (mLock) {
1359 if (mIsLoaderTaskRunning) {
1360 // Ensure that we are never running the background loading at this point since
1361 // we also touch the background collections
1362 throw new RuntimeException("Error! Background loading is already running");
1363 }
1364 }
1365
1366 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1367 // data structures, we can't allow any other thread to touch that data, but because
1368 // this call is synchronous, we can get away with not locking).
1369
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001370 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001371 // operations from the previous activity. We need to ensure that all queued operations
1372 // are executed before any synchronous binding work is done.
1373 mHandler.flush();
1374
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 // Divide the set of loaded items into those that we are binding synchronously, and
1376 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001377 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001378 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1379 // arise from that.
1380 onlyBindAllApps();
1381 }
1382
Joe Onorato36115782010-06-17 13:28:48 -04001383 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001384 boolean isUpgrade = false;
1385
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 synchronized (mLock) {
1387 mIsLoaderTaskRunning = true;
1388 }
Joe Onorato36115782010-06-17 13:28:48 -04001389 // Optimize for end-user experience: if the Launcher is up and // running with the
1390 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1391 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001392 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001393 // Elevate priority when Home launches for the first time to avoid
1394 // starving at boot time. Staring at a blank home is not cool.
1395 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001396 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1397 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001398 android.os.Process.setThreadPriority(mIsLaunching
1399 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1400 }
Winson Chung64359a52013-07-08 17:17:08 -07001401 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001402 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001403
Joe Onorato36115782010-06-17 13:28:48 -04001404 if (mStopped) {
1405 break keep_running;
1406 }
1407
1408 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1409 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 synchronized (mLock) {
1411 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001412 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001413 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1414 }
1415 }
Joe Onorato36115782010-06-17 13:28:48 -04001416 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001417
1418 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001419 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1420 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001421
1422 // Restore the default thread priority after we are done loading items
1423 synchronized (mLock) {
1424 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1425 }
Joe Onorato36115782010-06-17 13:28:48 -04001426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001427
Winson Chungaac01e12011-08-17 10:37:13 -07001428 // Update the saved icons if necessary
1429 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001430 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 for (Object key : sBgDbIconCache.keySet()) {
1432 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1433 }
1434 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001435 }
Winson Chungaac01e12011-08-17 10:37:13 -07001436
Winson Chungc58497e2013-09-03 17:48:37 -07001437 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1438 // Ensure that all the applications that are in the system are
1439 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001440 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001441 verifyApplications();
1442 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001443 }
1444
Joe Onorato36115782010-06-17 13:28:48 -04001445 // Clear out this reference, otherwise we end up holding it until all of the
1446 // callback runnables are done.
1447 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448
Joe Onorato36115782010-06-17 13:28:48 -04001449 synchronized (mLock) {
1450 // If we are still the last one to be scheduled, remove ourselves.
1451 if (mLoaderTask == this) {
1452 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001454 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001455 }
Joe Onorato36115782010-06-17 13:28:48 -04001456 }
1457
1458 public void stopLocked() {
1459 synchronized (LoaderTask.this) {
1460 mStopped = true;
1461 this.notify();
1462 }
1463 }
1464
1465 /**
1466 * Gets the callbacks object. If we've been stopped, or if the launcher object
1467 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1468 * object that was around when the deferred message was scheduled, and if there's
1469 * a new Callbacks object around then also return null. This will save us from
1470 * calling onto it with data that will be ignored.
1471 */
1472 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1473 synchronized (mLock) {
1474 if (mStopped) {
1475 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001476 }
Joe Onorato36115782010-06-17 13:28:48 -04001477
1478 if (mCallbacks == null) {
1479 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001480 }
Joe Onorato36115782010-06-17 13:28:48 -04001481
1482 final Callbacks callbacks = mCallbacks.get();
1483 if (callbacks != oldCallbacks) {
1484 return null;
1485 }
1486 if (callbacks == null) {
1487 Log.w(TAG, "no mCallbacks");
1488 return null;
1489 }
1490
1491 return callbacks;
1492 }
1493 }
1494
Winson Chungc763c4e2013-07-19 13:49:06 -07001495 private void verifyApplications() {
1496 final Context context = mApp.getContext();
1497
1498 // Cross reference all the applications in our apps list with items in the workspace
1499 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001500 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001501 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001502 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001503 tmpInfos = getItemInfoForComponentName(app.componentName);
1504 if (tmpInfos.isEmpty()) {
1505 // We are missing an application icon, so add this to the workspace
1506 added.add(app);
1507 // This is a rare event, so lets log it
1508 Log.e(TAG, "Missing Application on load: " + app);
1509 }
1510 }
1511 }
1512 if (!added.isEmpty()) {
1513 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001514 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001515 }
1516 }
1517
Winson Chung5f8afe62013-08-12 16:19:28 -07001518 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001519 LauncherAppState app = LauncherAppState.getInstance();
1520 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1521 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1522 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001523 }
1524
Joe Onorato36115782010-06-17 13:28:48 -04001525 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001526 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1527 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001528 LauncherAppState app = LauncherAppState.getInstance();
1529 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1530 int countX = (int) grid.numColumns;
1531 int countY = (int) grid.numRows;
1532
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001534 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001535 // Return early if we detect that an item is under the hotseat button
1536 if (mCallbacks == null ||
1537 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1538 deleteOnItemOverlap.set(true);
1539 return false;
1540 }
1541
Adam Cohendcd297f2013-06-18 13:13:40 -07001542 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1543 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1544 [(int) item.screenId][0] != null) {
1545 Log.e(TAG, "Error loading shortcut into hotseat " + item
1546 + " into position (" + item.screenId + ":" + item.cellX + ","
1547 + item.cellY + ") occupied by "
1548 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1549 [(int) item.screenId][0]);
1550 return false;
1551 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001552 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001553 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 items[(int) item.screenId][0] = item;
1555 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001556 return true;
1557 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001558 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1559 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001560 return true;
1561 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001562
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001564 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 occupied.put(item.screenId, items);
1566 }
1567
1568 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001569 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001570 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1571 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001573 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001575 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001576 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001578 return false;
1579 }
1580 }
1581 }
1582 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1583 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001585 }
1586 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001587
Joe Onorato36115782010-06-17 13:28:48 -04001588 return true;
1589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001590
Winson Chungba9c37f2013-08-30 14:11:37 -07001591 /** Clears all the sBg data structures */
1592 private void clearSBgDataStructures() {
1593 synchronized (sBgLock) {
1594 sBgWorkspaceItems.clear();
1595 sBgAppWidgets.clear();
1596 sBgFolders.clear();
1597 sBgItemsIdMap.clear();
1598 sBgDbIconCache.clear();
1599 sBgWorkspaceScreens.clear();
1600 }
1601 }
1602
Winson Chungc763c4e2013-07-19 13:49:06 -07001603 /** Returns whether this is an upgradge path */
1604 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001605 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001606
Joe Onorato36115782010-06-17 13:28:48 -04001607 final Context context = mContext;
1608 final ContentResolver contentResolver = context.getContentResolver();
1609 final PackageManager manager = context.getPackageManager();
1610 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1611 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001612
Winson Chung892c74d2013-08-22 16:15:50 -07001613 LauncherAppState app = LauncherAppState.getInstance();
1614 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1615 int countX = (int) grid.numColumns;
1616 int countY = (int) grid.numRows;
1617
Michael Jurkab85f8a42012-04-25 15:48:32 -07001618 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001619 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001620
Winson Chungc763c4e2013-07-19 13:49:06 -07001621 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001622 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001623
Winson Chung2abf94d2012-07-18 18:16:38 -07001624 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001625 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001626
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001627 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001628 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001629 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001630 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001631
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001632 // +1 for the hotseat (it can be larger than the workspace)
1633 // Load workspace in reverse order to ensure that latest items are loaded first (and
1634 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001635 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001637 try {
1638 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1639 final int intentIndex = c.getColumnIndexOrThrow
1640 (LauncherSettings.Favorites.INTENT);
1641 final int titleIndex = c.getColumnIndexOrThrow
1642 (LauncherSettings.Favorites.TITLE);
1643 final int iconTypeIndex = c.getColumnIndexOrThrow(
1644 LauncherSettings.Favorites.ICON_TYPE);
1645 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1646 final int iconPackageIndex = c.getColumnIndexOrThrow(
1647 LauncherSettings.Favorites.ICON_PACKAGE);
1648 final int iconResourceIndex = c.getColumnIndexOrThrow(
1649 LauncherSettings.Favorites.ICON_RESOURCE);
1650 final int containerIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.CONTAINER);
1652 final int itemTypeIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.ITEM_TYPE);
1654 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1655 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001656 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1657 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001658 final int screenIndex = c.getColumnIndexOrThrow(
1659 LauncherSettings.Favorites.SCREEN);
1660 final int cellXIndex = c.getColumnIndexOrThrow
1661 (LauncherSettings.Favorites.CELLX);
1662 final int cellYIndex = c.getColumnIndexOrThrow
1663 (LauncherSettings.Favorites.CELLY);
1664 final int spanXIndex = c.getColumnIndexOrThrow
1665 (LauncherSettings.Favorites.SPANX);
1666 final int spanYIndex = c.getColumnIndexOrThrow(
1667 LauncherSettings.Favorites.SPANY);
1668 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1669 //final int displayModeIndex = c.getColumnIndexOrThrow(
1670 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001671
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 ShortcutInfo info;
1673 String intentDescription;
1674 LauncherAppWidgetInfo appWidgetInfo;
1675 int container;
1676 long id;
1677 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001680 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 try {
1682 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 switch (itemType) {
1685 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1686 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001687 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 intentDescription = c.getString(intentIndex);
1689 try {
1690 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001691 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001692 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001693 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001694 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001695 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1696 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001697 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001698 // If apps can be on external storage, then we just
1699 // leave them for the user to remove (maybe add
1700 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001701 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001702 }
1703 continue;
1704 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001706 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001707 continue;
1708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1711 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1712 titleIndex, mLabelCache);
1713 } else {
1714 info = getShortcutInfo(c, context, iconTypeIndex,
1715 iconPackageIndex, iconResourceIndex, iconIndex,
1716 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001717
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 // App shortcuts that used to be automatically added to Launcher
1719 // didn't always have the correct intent flags set, so do that
1720 // here
1721 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001722 intent.getCategories() != null &&
1723 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001724 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 intent.addFlags(
1726 Intent.FLAG_ACTIVITY_NEW_TASK |
1727 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1728 }
Michael Jurka96879562012-03-22 05:54:33 -07001729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001732 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001734 container = c.getInt(containerIndex);
1735 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001736 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 info.cellX = c.getInt(cellXIndex);
1738 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001739 info.spanX = 1;
1740 info.spanY = 1;
1741 // Skip loading items that are out of bounds
1742 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1743 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001744 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1745 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001746 continue;
1747 }
1748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001750 deleteOnItemOverlap.set(false);
1751 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1752 if (deleteOnItemOverlap.get()) {
1753 itemsToRemove.add(id);
1754 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 break;
1756 }
1757
1758 switch (container) {
1759 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1760 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1761 sBgWorkspaceItems.add(info);
1762 break;
1763 default:
1764 // Item is in a user folder
1765 FolderInfo folderInfo =
1766 findOrMakeFolder(sBgFolders, container);
1767 folderInfo.add(info);
1768 break;
1769 }
1770 sBgItemsIdMap.put(info.id, info);
1771
1772 // now that we've loaded everthing re-save it with the
1773 // icon in case it disappears somehow.
1774 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001775 } else {
1776 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 }
1778 break;
1779
1780 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1781 id = c.getLong(idIndex);
1782 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1783
1784 folderInfo.title = c.getString(titleIndex);
1785 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001788 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001789 folderInfo.cellX = c.getInt(cellXIndex);
1790 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001791 folderInfo.spanX = 1;
1792 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793
Winson Chung5f8afe62013-08-12 16:19:28 -07001794 // Skip loading items that are out of bounds
1795 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001796 if (checkItemDimensions(folderInfo)) {
1797 Log.d(TAG, "Skipped loading out of bounds folder");
1798 continue;
1799 }
1800 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001801 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001802 deleteOnItemOverlap.set(false);
1803 if (!checkItemPlacement(occupied, folderInfo,
1804 deleteOnItemOverlap)) {
1805 if (deleteOnItemOverlap.get()) {
1806 itemsToRemove.add(id);
1807 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001808 break;
1809 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001810
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1813 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1814 sBgWorkspaceItems.add(folderInfo);
1815 break;
Joe Onorato36115782010-06-17 13:28:48 -04001816 }
Joe Onorato17a89222011-02-08 17:26:11 -08001817
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001818 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1819 sBgFolders.put(folderInfo.id, folderInfo);
1820 break;
1821
1822 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1823 // Read all Launcher-specific widget details
1824 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001825 String savedProvider = c.getString(appWidgetProviderIndex);
1826
Joe Onorato36115782010-06-17 13:28:48 -04001827 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 final AppWidgetProviderInfo provider =
1830 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001831
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 if (!isSafeMode && (provider == null || provider.provider == null ||
1833 provider.provider.getPackageName() == null)) {
1834 String log = "Deleting widget that isn't installed anymore: id="
1835 + id + " appWidgetId=" + appWidgetId;
1836 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001837 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 itemsToRemove.add(id);
1839 } else {
1840 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1841 provider.provider);
1842 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001843 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 appWidgetInfo.cellX = c.getInt(cellXIndex);
1845 appWidgetInfo.cellY = c.getInt(cellYIndex);
1846 appWidgetInfo.spanX = c.getInt(spanXIndex);
1847 appWidgetInfo.spanY = c.getInt(spanYIndex);
1848 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1849 appWidgetInfo.minSpanX = minSpan[0];
1850 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001851
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852 container = c.getInt(containerIndex);
1853 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1854 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1855 Log.e(TAG, "Widget found where container != " +
1856 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1857 continue;
1858 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859
Adam Cohene25af792013-06-06 23:08:25 -07001860 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001861 // Skip loading items that are out of bounds
1862 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1863 if (checkItemDimensions(appWidgetInfo)) {
1864 Log.d(TAG, "Skipped loading out of bounds app widget");
1865 continue;
1866 }
1867 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001869 deleteOnItemOverlap.set(false);
1870 if (!checkItemPlacement(occupied, appWidgetInfo,
1871 deleteOnItemOverlap)) {
1872 if (deleteOnItemOverlap.get()) {
1873 itemsToRemove.add(id);
1874 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 break;
1876 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001877 String providerName = provider.provider.flattenToString();
1878 if (!providerName.equals(savedProvider)) {
1879 ContentValues values = new ContentValues();
1880 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1881 providerName);
1882 String where = BaseColumns._ID + "= ?";
1883 String[] args = {Integer.toString(c.getInt(idIndex))};
1884 contentResolver.update(contentUri, values, where, args);
1885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1887 sBgAppWidgets.add(appWidgetInfo);
1888 }
Joe Onorato36115782010-06-17 13:28:48 -04001889 break;
1890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001892 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001894 }
1895 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001896 if (c != null) {
1897 c.close();
1898 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 }
1900
Winson Chungba9c37f2013-08-30 14:11:37 -07001901 // Break early if we've stopped loading
1902 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001903 clearSBgDataStructures();
1904 return false;
1905 }
1906
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 if (itemsToRemove.size() > 0) {
1908 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001909 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 // Remove dead items
1911 for (long id : itemsToRemove) {
1912 if (DEBUG_LOADERS) {
1913 Log.d(TAG, "Removed id = " + id);
1914 }
1915 // Don't notify content observers
1916 try {
1917 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1918 null, null);
1919 } catch (RemoteException e) {
1920 Log.w(TAG, "Could not remove id = " + id);
1921 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001922 }
1923 }
1924
Winson Chungc763c4e2013-07-19 13:49:06 -07001925 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001926 long maxScreenId = 0;
1927 // If we're importing we use the old screen order.
1928 for (ItemInfo item: sBgItemsIdMap.values()) {
1929 long screenId = item.screenId;
1930 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1931 !sBgWorkspaceScreens.contains(screenId)) {
1932 sBgWorkspaceScreens.add(screenId);
1933 if (screenId > maxScreenId) {
1934 maxScreenId = screenId;
1935 }
1936 }
1937 }
1938 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001939
Michael Jurka414300a2013-08-27 15:42:35 +02001940 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001941 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001942
1943 // Update the max item id after we load an old db
1944 long maxItemId = 0;
1945 // If we're importing we use the old screen order.
1946 for (ItemInfo item: sBgItemsIdMap.values()) {
1947 maxItemId = Math.max(maxItemId, item.id);
1948 }
Michael Jurka414300a2013-08-27 15:42:35 +02001949 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001951 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1952 for (Integer i : orderedScreens.keySet()) {
1953 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 }
1955
1956 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001957 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 for (ItemInfo item: sBgItemsIdMap.values()) {
1959 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1961 unusedScreens.contains(screenId)) {
1962 unusedScreens.remove(screenId);
1963 }
1964 }
1965
1966 // If there are any empty screens remove them, and update.
1967 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001968 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1970 }
1971 }
1972
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001973 if (DEBUG_LOADERS) {
1974 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1975 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001977 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001979
Daniel Sandler566da102013-06-25 23:43:45 -04001980 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001981 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001983 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 line += " | ";
1985 }
Winson Chung892c74d2013-08-22 16:15:50 -07001986 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001988 }
Joe Onorato36115782010-06-17 13:28:48 -04001989 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001990 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 }
Joe Onorato36115782010-06-17 13:28:48 -04001993 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001994 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001995 }
1996
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001997 /** Filters the set of items who are directly or indirectly (via another container) on the
1998 * specified screen. */
1999 private void filterCurrentWorkspaceItems(int currentScreen,
2000 ArrayList<ItemInfo> allWorkspaceItems,
2001 ArrayList<ItemInfo> currentScreenItems,
2002 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002003 // Purge any null ItemInfos
2004 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2005 while (iter.hasNext()) {
2006 ItemInfo i = iter.next();
2007 if (i == null) {
2008 iter.remove();
2009 }
2010 }
2011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 // If we aren't filtering on a screen, then the set of items to load is the full set of
2013 // items given.
2014 if (currentScreen < 0) {
2015 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002016 }
2017
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 // Order the set of items by their containers first, this allows use to walk through the
2019 // list sequentially, build up a list of containers that are in the specified screen,
2020 // as well as all items in those containers.
2021 Set<Long> itemsOnScreen = new HashSet<Long>();
2022 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2023 @Override
2024 public int compare(ItemInfo lhs, ItemInfo rhs) {
2025 return (int) (lhs.container - rhs.container);
2026 }
2027 });
2028 for (ItemInfo info : allWorkspaceItems) {
2029 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 currentScreenItems.add(info);
2032 itemsOnScreen.add(info.id);
2033 } else {
2034 otherScreenItems.add(info);
2035 }
2036 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2037 currentScreenItems.add(info);
2038 itemsOnScreen.add(info.id);
2039 } else {
2040 if (itemsOnScreen.contains(info.container)) {
2041 currentScreenItems.add(info);
2042 itemsOnScreen.add(info.id);
2043 } else {
2044 otherScreenItems.add(info);
2045 }
2046 }
2047 }
2048 }
2049
2050 /** Filters the set of widgets which are on the specified screen. */
2051 private void filterCurrentAppWidgets(int currentScreen,
2052 ArrayList<LauncherAppWidgetInfo> appWidgets,
2053 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2054 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2055 // If we aren't filtering on a screen, then the set of items to load is the full set of
2056 // widgets given.
2057 if (currentScreen < 0) {
2058 currentScreenWidgets.addAll(appWidgets);
2059 }
2060
2061 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002062 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 currentScreenWidgets.add(widget);
2066 } else {
2067 otherScreenWidgets.add(widget);
2068 }
2069 }
2070 }
2071
2072 /** Filters the set of folders which are on the specified screen. */
2073 private void filterCurrentFolders(int currentScreen,
2074 HashMap<Long, ItemInfo> itemsIdMap,
2075 HashMap<Long, FolderInfo> folders,
2076 HashMap<Long, FolderInfo> currentScreenFolders,
2077 HashMap<Long, FolderInfo> otherScreenFolders) {
2078 // If we aren't filtering on a screen, then the set of items to load is the full set of
2079 // widgets given.
2080 if (currentScreen < 0) {
2081 currentScreenFolders.putAll(folders);
2082 }
2083
2084 for (long id : folders.keySet()) {
2085 ItemInfo info = itemsIdMap.get(id);
2086 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002087 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 currentScreenFolders.put(id, folder);
2091 } else {
2092 otherScreenFolders.put(id, folder);
2093 }
2094 }
2095 }
2096
2097 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2098 * right) */
2099 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002100 final LauncherAppState app = LauncherAppState.getInstance();
2101 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 // XXX: review this
2103 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002104 @Override
2105 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002106 int cellCountX = (int) grid.numColumns;
2107 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002108 int screenOffset = cellCountX * cellCountY;
2109 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002111 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002112 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002113 rhs.cellY * cellCountX + rhs.cellX);
2114 return (int) (lr - rr);
2115 }
2116 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002118
Adam Cohendcd297f2013-06-18 13:13:40 -07002119 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2120 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002121 final Runnable r = new Runnable() {
2122 @Override
2123 public void run() {
2124 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2125 if (callbacks != null) {
2126 callbacks.bindScreens(orderedScreens);
2127 }
2128 }
2129 };
2130 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2131 }
2132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2134 final ArrayList<ItemInfo> workspaceItems,
2135 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2136 final HashMap<Long, FolderInfo> folders,
2137 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002138
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 final boolean postOnMainThread = (deferredBindRunnables != null);
2140
2141 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002142 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002144 final int start = i;
2145 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002146 final Runnable r = new Runnable() {
2147 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002149 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002151 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2152 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 }
2154 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 };
2156 if (postOnMainThread) {
2157 deferredBindRunnables.add(r);
2158 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002159 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 }
Joe Onorato36115782010-06-17 13:28:48 -04002161 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162
2163 // Bind the folders
2164 if (!folders.isEmpty()) {
2165 final Runnable r = new Runnable() {
2166 public void run() {
2167 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2168 if (callbacks != null) {
2169 callbacks.bindFolders(folders);
2170 }
2171 }
2172 };
2173 if (postOnMainThread) {
2174 deferredBindRunnables.add(r);
2175 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002176 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 }
2178 }
2179
2180 // Bind the widgets, one at a time
2181 N = appWidgets.size();
2182 for (int i = 0; i < N; i++) {
2183 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2184 final Runnable r = new Runnable() {
2185 public void run() {
2186 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2187 if (callbacks != null) {
2188 callbacks.bindAppWidget(widget);
2189 }
2190 }
2191 };
2192 if (postOnMainThread) {
2193 deferredBindRunnables.add(r);
2194 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002195 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 }
2197 }
2198 }
2199
2200 /**
2201 * Binds all loaded data to actual views on the main thread.
2202 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002203 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 final long t = SystemClock.uptimeMillis();
2205 Runnable r;
2206
2207 // Don't use these two variables in any of the callback runnables.
2208 // Otherwise we hold a reference to them.
2209 final Callbacks oldCallbacks = mCallbacks.get();
2210 if (oldCallbacks == null) {
2211 // This launcher has exited and nobody bothered to tell us. Just bail.
2212 Log.w(TAG, "LoaderTask running with no launcher");
2213 return;
2214 }
2215
Winson Chung4a2afa32012-07-19 14:53:05 -07002216 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2217 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 oldCallbacks.getCurrentWorkspaceScreen();
2219
2220 // Load all the items that are on the current page first (and in the process, unbind
2221 // all the existing workspace items before we call startBinding() below.
2222 unbindWorkspaceItemsOnMainThread();
2223 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2224 ArrayList<LauncherAppWidgetInfo> appWidgets =
2225 new ArrayList<LauncherAppWidgetInfo>();
2226 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2227 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002229 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002230 workspaceItems.addAll(sBgWorkspaceItems);
2231 appWidgets.addAll(sBgAppWidgets);
2232 folders.putAll(sBgFolders);
2233 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 }
2236
2237 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2238 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2239 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2240 new ArrayList<LauncherAppWidgetInfo>();
2241 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2242 new ArrayList<LauncherAppWidgetInfo>();
2243 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2244 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2245
2246 // Separate the items that are on the current screen, and all the other remaining items
2247 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2248 otherWorkspaceItems);
2249 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2250 otherAppWidgets);
2251 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2252 otherFolders);
2253 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2254 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2255
2256 // Tell the workspace that we're about to start binding items
2257 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002258 public void run() {
2259 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2260 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002262 }
2263 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 };
Winson Chung81b52252012-08-27 15:34:29 -07002265 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2268
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002269 // Load items on the current page
2270 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2271 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002272 if (isLoadingSynchronously) {
2273 r = new Runnable() {
2274 public void run() {
2275 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2276 if (callbacks != null) {
2277 callbacks.onPageBoundSynchronously(currentScreen);
2278 }
2279 }
2280 };
Winson Chung81b52252012-08-27 15:34:29 -07002281 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002282 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283
Winson Chung4a2afa32012-07-19 14:53:05 -07002284 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2285 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 mDeferredBindRunnables.clear();
2287 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002288 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289
2290 // Tell the workspace that we're done binding items
2291 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002292 public void run() {
2293 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2294 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002295 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002296 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297
Winson Chung98e030b2012-05-07 16:01:11 -07002298 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002299 if (DEBUG_LOADERS) {
2300 Log.d(TAG, "bound workspace in "
2301 + (SystemClock.uptimeMillis()-t) + "ms");
2302 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002303
2304 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002305 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002307 if (isLoadingSynchronously) {
2308 mDeferredBindRunnables.add(r);
2309 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002310 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002311 }
Joe Onorato36115782010-06-17 13:28:48 -04002312 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002313
Joe Onorato36115782010-06-17 13:28:48 -04002314 private void loadAndBindAllApps() {
2315 if (DEBUG_LOADERS) {
2316 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2317 }
2318 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002319 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002320 synchronized (LoaderTask.this) {
2321 if (mStopped) {
2322 return;
2323 }
2324 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002325 }
Joe Onorato36115782010-06-17 13:28:48 -04002326 } else {
2327 onlyBindAllApps();
2328 }
2329 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002330
Joe Onorato36115782010-06-17 13:28:48 -04002331 private void onlyBindAllApps() {
2332 final Callbacks oldCallbacks = mCallbacks.get();
2333 if (oldCallbacks == null) {
2334 // This launcher has exited and nobody bothered to tell us. Just bail.
2335 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2336 return;
2337 }
2338
2339 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002340 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002341 final ArrayList<AppInfo> list
2342 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002343 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002344 public void run() {
2345 final long t = SystemClock.uptimeMillis();
2346 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2347 if (callbacks != null) {
2348 callbacks.bindAllApplications(list);
2349 }
2350 if (DEBUG_LOADERS) {
2351 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2352 + (SystemClock.uptimeMillis()-t) + "ms");
2353 }
2354 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002355 };
2356 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002357 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002358 r.run();
2359 } else {
2360 mHandler.post(r);
2361 }
Joe Onorato36115782010-06-17 13:28:48 -04002362 }
2363
Winson Chung64359a52013-07-08 17:17:08 -07002364 private void loadAllApps() {
2365 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002366
Joe Onorato36115782010-06-17 13:28:48 -04002367 final Callbacks oldCallbacks = mCallbacks.get();
2368 if (oldCallbacks == null) {
2369 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002370 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002371 return;
2372 }
2373
Winson Chung64359a52013-07-08 17:17:08 -07002374 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002375 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2376 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2377
Winson Chung64359a52013-07-08 17:17:08 -07002378 // Clear the list of apps
2379 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002380
Winson Chung64359a52013-07-08 17:17:08 -07002381 // Query for the set of apps
2382 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2383 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2384 if (DEBUG_LOADERS) {
2385 Log.d(TAG, "queryIntentActivities took "
2386 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2387 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2388 }
2389 // Fail if we don't have any apps
2390 if (apps == null || apps.isEmpty()) {
2391 return;
2392 }
2393 // Sort the applications by name
2394 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2395 Collections.sort(apps,
2396 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2397 if (DEBUG_LOADERS) {
2398 Log.d(TAG, "sort took "
2399 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 }
2401
Winson Chung64359a52013-07-08 17:17:08 -07002402 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002403 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002404 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002405 // This builds the icon bitmaps.
2406 mBgAllAppsList.add(new AppInfo(packageManager, app,
2407 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002408 }
2409
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002410 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002411 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2412 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002413
2414 // Post callback on main thread
2415 mHandler.post(new Runnable() {
2416 public void run() {
2417 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002418 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002419 if (callbacks != null) {
2420 callbacks.bindAllApplications(added);
2421 if (DEBUG_LOADERS) {
2422 Log.d(TAG, "bound " + added.size() + " apps in "
2423 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2424 }
2425 } else {
2426 Log.i(TAG, "not binding apps: no Launcher activity");
2427 }
2428 }
2429 });
2430
Joe Onorato36115782010-06-17 13:28:48 -04002431 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002432 Log.d(TAG, "Icons processed in "
2433 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002434 }
2435 }
2436
2437 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002438 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002439 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2440 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2441 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2442 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2443 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2444 }
Joe Onorato36115782010-06-17 13:28:48 -04002445 }
2446 }
2447
2448 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002449 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002450 }
2451
2452 private class PackageUpdatedTask implements Runnable {
2453 int mOp;
2454 String[] mPackages;
2455
2456 public static final int OP_NONE = 0;
2457 public static final int OP_ADD = 1;
2458 public static final int OP_UPDATE = 2;
2459 public static final int OP_REMOVE = 3; // uninstlled
2460 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2461
2462
2463 public PackageUpdatedTask(int op, String[] packages) {
2464 mOp = op;
2465 mPackages = packages;
2466 }
2467
2468 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002469 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002470
2471 final String[] packages = mPackages;
2472 final int N = packages.length;
2473 switch (mOp) {
2474 case OP_ADD:
2475 for (int i=0; i<N; i++) {
2476 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002477 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002478 }
2479 break;
2480 case OP_UPDATE:
2481 for (int i=0; i<N; i++) {
2482 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002483 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002484 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002485 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002486 }
2487 break;
2488 case OP_REMOVE:
2489 case OP_UNAVAILABLE:
2490 for (int i=0; i<N; i++) {
2491 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002492 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002493 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002494 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002495 }
2496 break;
2497 }
2498
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002499 ArrayList<AppInfo> added = null;
2500 ArrayList<AppInfo> modified = null;
2501 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002502
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002504 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002505 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002507 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002508 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002509 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002510 }
Winson Chung5d55f332012-07-16 20:45:03 -07002511 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002512 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002513 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002514 }
2515
Joe Onorato36115782010-06-17 13:28:48 -04002516 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2517 if (callbacks == null) {
2518 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2519 return;
2520 }
2521
2522 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002523 // Ensure that we add all the workspace applications to the db
2524 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002525 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2526 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2527 } else {
2528 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2529 addAndBindAddedApps(context, addedInfos, cb, added);
2530 }
Joe Onorato36115782010-06-17 13:28:48 -04002531 }
2532 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002533 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002534
2535 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002536 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002537 ArrayList<ItemInfo> infos =
2538 getItemInfoForComponentName(a.componentName);
2539 for (ItemInfo i : infos) {
2540 if (isShortcutInfoUpdateable(i)) {
2541 ShortcutInfo info = (ShortcutInfo) i;
2542 info.title = a.title.toString();
2543 updateItemInDatabase(context, info);
2544 }
2545 }
2546 }
2547
Joe Onorato36115782010-06-17 13:28:48 -04002548 mHandler.post(new Runnable() {
2549 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002550 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2551 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002552 callbacks.bindAppsUpdated(modifiedFinal);
2553 }
2554 }
2555 });
2556 }
Winson Chung83892cc2013-05-01 16:53:33 -07002557
Winson Chungdf95eb12013-10-16 14:57:07 -07002558 final ArrayList<String> removedPackageNames =
2559 new ArrayList<String>();
2560 if (mOp == OP_REMOVE) {
2561 // Mark all packages in the broadcast to be removed
2562 removedPackageNames.addAll(Arrays.asList(packages));
2563 } else if (mOp == OP_UPDATE) {
2564 // Mark disabled packages in the broadcast to be removed
2565 final PackageManager pm = context.getPackageManager();
2566 for (int i=0; i<N; i++) {
2567 if (isPackageDisabled(pm, packages[i])) {
2568 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002569 }
2570 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002571 }
2572 // Remove all the components associated with this package
2573 for (String pn : removedPackageNames) {
2574 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2575 for (ItemInfo i : infos) {
2576 deleteItemFromDatabase(context, i);
2577 }
2578 }
2579 // Remove all the specific components
2580 for (AppInfo a : removedApps) {
2581 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2582 for (ItemInfo i : infos) {
2583 deleteItemFromDatabase(context, i);
2584 }
2585 }
2586 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2587 // Remove any queued items from the install queue
2588 String spKey = LauncherAppState.getSharedPreferencesKey();
2589 SharedPreferences sp =
2590 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2591 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2592 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002593 mHandler.post(new Runnable() {
2594 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002595 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2596 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002597 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002598 }
2599 }
2600 });
Joe Onoratobe386092009-11-17 17:32:16 -08002601 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002602
Michael Jurkac402cd92013-05-20 15:49:32 +02002603 final ArrayList<Object> widgetsAndShortcuts =
2604 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002605 mHandler.post(new Runnable() {
2606 @Override
2607 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002608 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2609 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002610 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002611 }
2612 }
2613 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002614
2615 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002616 mHandler.post(new Runnable() {
2617 public void run() {
2618 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2619 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002620 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002621 }
2622 }
2623 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002624 }
2625 }
2626
Michael Jurkac402cd92013-05-20 15:49:32 +02002627 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2628 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2629 PackageManager packageManager = context.getPackageManager();
2630 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2631 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2632 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2633 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2634 Collections.sort(widgetsAndShortcuts,
2635 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2636 return widgetsAndShortcuts;
2637 }
2638
Winson Chungdf95eb12013-10-16 14:57:07 -07002639 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2640 try {
2641 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2642 return !pi.applicationInfo.enabled;
2643 } catch (NameNotFoundException e) {
2644 // Fall through
2645 }
2646 return false;
2647 }
Winson Chung1323b482013-08-05 12:41:55 -07002648 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002649 if (cn == null) {
2650 return false;
2651 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002652 if (isPackageDisabled(pm, cn.getPackageName())) {
2653 return false;
2654 }
Winson Chungee055712013-07-30 14:46:24 -07002655
2656 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002657 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002658 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002659 return (pm.getActivityInfo(cn, 0) != null);
2660 } catch (NameNotFoundException e) {
2661 return false;
2662 }
2663 }
2664
Joe Onorato9c1289c2009-08-17 11:03:03 -04002665 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002666 * This is called from the code that adds shortcuts from the intent receiver. This
2667 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002668 */
Joe Onorato56d82912010-03-07 14:32:10 -05002669 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002670 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002671 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672
Joe Onorato56d82912010-03-07 14:32:10 -05002673 /**
2674 * Make an ShortcutInfo object for a shortcut that is an application.
2675 *
2676 * If c is not null, then it will be used to fill in missing data like the title and icon.
2677 */
2678 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002679 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002680 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002681 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002682 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002683 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002684 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002685 } else {
2686 try {
2687 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2688 info.initFlagsAndFirstInstallTime(pi);
2689 } catch (NameNotFoundException e) {
2690 Log.d(TAG, "getPackInfo failed for package " +
2691 componentName.getPackageName());
2692 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002693 }
2694
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002695 // TODO: See if the PackageManager knows about this case. If it doesn't
2696 // then return null & delete this.
2697
Joe Onorato56d82912010-03-07 14:32:10 -05002698 // the resource -- This may implicitly give us back the fallback icon,
2699 // but don't worry about that. All we're doing with usingFallbackIcon is
2700 // to avoid saving lots of copies of that in the database, and most apps
2701 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002702
2703 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2704 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2705 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002706 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002707 ResolveInfo resolveInfo = null;
2708 ComponentName oldComponent = intent.getComponent();
2709 Intent newIntent = new Intent(intent.getAction(), null);
2710 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2711 newIntent.setPackage(oldComponent.getPackageName());
2712 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2713 for (ResolveInfo i : infos) {
2714 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2715 i.activityInfo.name);
2716 if (cn.equals(oldComponent)) {
2717 resolveInfo = i;
2718 }
2719 }
2720 if (resolveInfo == null) {
2721 resolveInfo = manager.resolveActivity(intent, 0);
2722 }
Joe Onorato56d82912010-03-07 14:32:10 -05002723 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002724 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002725 }
Joe Onorato56d82912010-03-07 14:32:10 -05002726 // the db
2727 if (icon == null) {
2728 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002729 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002730 }
2731 }
2732 // the fallback icon
2733 if (icon == null) {
2734 icon = getFallbackIcon();
2735 info.usingFallbackIcon = true;
2736 }
2737 info.setIcon(icon);
2738
2739 // from the resource
2740 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002741 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2742 if (labelCache != null && labelCache.containsKey(key)) {
2743 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002744 } else {
2745 info.title = resolveInfo.activityInfo.loadLabel(manager);
2746 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002747 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002748 }
2749 }
Joe Onorato56d82912010-03-07 14:32:10 -05002750 }
2751 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002752 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002753 if (c != null) {
2754 info.title = c.getString(titleIndex);
2755 }
2756 }
2757 // fall back to the class name of the activity
2758 if (info.title == null) {
2759 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002761 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2762 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002764
Winson Chung64359a52013-07-08 17:17:08 -07002765 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2766 ItemInfoFilter f) {
2767 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2768 for (ItemInfo i : infos) {
2769 if (i instanceof ShortcutInfo) {
2770 ShortcutInfo info = (ShortcutInfo) i;
2771 ComponentName cn = info.intent.getComponent();
2772 if (cn != null && f.filterItem(null, info, cn)) {
2773 filtered.add(info);
2774 }
2775 } else if (i instanceof FolderInfo) {
2776 FolderInfo info = (FolderInfo) i;
2777 for (ShortcutInfo s : info.contents) {
2778 ComponentName cn = s.intent.getComponent();
2779 if (cn != null && f.filterItem(info, s, cn)) {
2780 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002781 }
2782 }
Winson Chung64359a52013-07-08 17:17:08 -07002783 } else if (i instanceof LauncherAppWidgetInfo) {
2784 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2785 ComponentName cn = info.providerName;
2786 if (cn != null && f.filterItem(null, info, cn)) {
2787 filtered.add(info);
2788 }
Winson Chung8a435102012-08-30 17:16:53 -07002789 }
2790 }
Winson Chung64359a52013-07-08 17:17:08 -07002791 return new ArrayList<ItemInfo>(filtered);
2792 }
2793
2794 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002795 ItemInfoFilter filter = new ItemInfoFilter() {
2796 @Override
2797 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2798 return cn.getPackageName().equals(pn);
2799 }
2800 };
2801 return filterItemInfos(sBgItemsIdMap.values(), filter);
2802 }
2803
2804 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002805 ItemInfoFilter filter = new ItemInfoFilter() {
2806 @Override
2807 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2808 return cn.equals(cname);
2809 }
2810 };
2811 return filterItemInfos(sBgItemsIdMap.values(), filter);
2812 }
2813
2814 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2815 if (i instanceof ShortcutInfo) {
2816 ShortcutInfo info = (ShortcutInfo) i;
2817 // We need to check for ACTION_MAIN otherwise getComponent() might
2818 // return null for some shortcuts (for instance, for shortcuts to
2819 // web pages.)
2820 Intent intent = info.intent;
2821 ComponentName name = intent.getComponent();
2822 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2823 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2824 return true;
2825 }
2826 }
2827 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002828 }
2829
2830 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002831 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002833 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002834 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2835 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836
Joe Onorato56d82912010-03-07 14:32:10 -05002837 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002838 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002841 // TODO: If there's an explicit component and we can't install that, delete it.
2842
Joe Onorato56d82912010-03-07 14:32:10 -05002843 info.title = c.getString(titleIndex);
2844
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 int iconType = c.getInt(iconTypeIndex);
2846 switch (iconType) {
2847 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2848 String packageName = c.getString(iconPackageIndex);
2849 String resourceName = c.getString(iconResourceIndex);
2850 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002851 info.customIcon = false;
2852 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002855 if (resources != null) {
2856 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002857 icon = Utilities.createIconBitmap(
2858 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002861 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 }
Joe Onorato56d82912010-03-07 14:32:10 -05002863 // the db
2864 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002865 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002866 }
2867 // the fallback icon
2868 if (icon == null) {
2869 icon = getFallbackIcon();
2870 info.usingFallbackIcon = true;
2871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 break;
2873 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002874 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002875 if (icon == null) {
2876 icon = getFallbackIcon();
2877 info.customIcon = false;
2878 info.usingFallbackIcon = true;
2879 } else {
2880 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 break;
2883 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002884 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002885 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 info.customIcon = false;
2887 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002889 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 return info;
2891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892
Michael Jurka931dc972011-08-05 15:08:15 -07002893 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002894 @SuppressWarnings("all") // suppress dead code warning
2895 final boolean debug = false;
2896 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002897 Log.d(TAG, "getIconFromCursor app="
2898 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2899 }
2900 byte[] data = c.getBlob(iconIndex);
2901 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002902 return Utilities.createIconBitmap(
2903 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002904 } catch (Exception e) {
2905 return null;
2906 }
2907 }
2908
Winson Chung3d503fb2011-07-13 17:25:49 -07002909 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2910 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002911 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002912 if (info == null) {
2913 return null;
2914 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002915 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002916
2917 return info;
2918 }
2919
Winson Chunga9abd0e2010-10-27 17:18:37 -07002920 /**
Winson Chung55cef262010-10-28 14:14:18 -07002921 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2922 */
2923 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2924 ComponentName component) {
2925 List<AppWidgetProviderInfo> widgets =
2926 AppWidgetManager.getInstance(context).getInstalledProviders();
2927 for (AppWidgetProviderInfo info : widgets) {
2928 if (info.provider.equals(component)) {
2929 return info;
2930 }
2931 }
2932 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002933 }
2934
Winson Chung68846fd2010-10-29 11:00:27 -07002935 /**
2936 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2937 */
2938 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2939 final PackageManager packageManager = context.getPackageManager();
2940 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2941 new ArrayList<WidgetMimeTypeHandlerData>();
2942
2943 final Intent supportsIntent =
2944 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2945 supportsIntent.setType(mimeType);
2946
2947 // Create a set of widget configuration components that we can test against
2948 final List<AppWidgetProviderInfo> widgets =
2949 AppWidgetManager.getInstance(context).getInstalledProviders();
2950 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2951 new HashMap<ComponentName, AppWidgetProviderInfo>();
2952 for (AppWidgetProviderInfo info : widgets) {
2953 configurationComponentToWidget.put(info.configure, info);
2954 }
2955
2956 // Run through each of the intents that can handle this type of clip data, and cross
2957 // reference them with the components that are actual configuration components
2958 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2959 PackageManager.MATCH_DEFAULT_ONLY);
2960 for (ResolveInfo info : activities) {
2961 final ActivityInfo activityInfo = info.activityInfo;
2962 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2963 activityInfo.name);
2964 if (configurationComponentToWidget.containsKey(infoComponent)) {
2965 supportedConfigurationActivities.add(
2966 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2967 configurationComponentToWidget.get(infoComponent)));
2968 }
2969 }
2970 return supportedConfigurationActivities;
2971 }
2972
Winson Chunga9abd0e2010-10-27 17:18:37 -07002973 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002974 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2975 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2976 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2977
Adam Cohend9198822011-11-22 16:42:47 -08002978 if (intent == null) {
2979 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2980 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2981 return null;
2982 }
2983
Joe Onorato0589f0f2010-02-08 13:44:00 -08002984 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002985 boolean customIcon = false;
2986 ShortcutIconResource iconResource = null;
2987
2988 if (bitmap != null && bitmap instanceof Bitmap) {
2989 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002990 customIcon = true;
2991 } else {
2992 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2993 if (extra != null && extra instanceof ShortcutIconResource) {
2994 try {
2995 iconResource = (ShortcutIconResource) extra;
2996 final PackageManager packageManager = context.getPackageManager();
2997 Resources resources = packageManager.getResourcesForApplication(
2998 iconResource.packageName);
2999 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003000 icon = Utilities.createIconBitmap(
3001 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003002 } catch (Exception e) {
3003 Log.w(TAG, "Could not load shortcut icon: " + extra);
3004 }
3005 }
3006 }
3007
Michael Jurkac9d95c52011-08-29 14:03:34 -07003008 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003009
3010 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003011 if (fallbackIcon != null) {
3012 icon = fallbackIcon;
3013 } else {
3014 icon = getFallbackIcon();
3015 info.usingFallbackIcon = true;
3016 }
Joe Onorato56d82912010-03-07 14:32:10 -05003017 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003018 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003019
Joe Onorato0589f0f2010-02-08 13:44:00 -08003020 info.title = name;
3021 info.intent = intent;
3022 info.customIcon = customIcon;
3023 info.iconResource = iconResource;
3024
3025 return info;
3026 }
3027
Winson Chungaac01e12011-08-17 10:37:13 -07003028 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3029 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003030 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003031 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003032 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003033 }
Joe Onorato56d82912010-03-07 14:32:10 -05003034 // If this icon doesn't have a custom icon, check to see
3035 // what's stored in the DB, and if it doesn't match what
3036 // we're going to show, store what we are going to show back
3037 // into the DB. We do this so when we're loading, if the
3038 // package manager can't find an icon (for example because
3039 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003040 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003041 cache.put(info, c.getBlob(iconIndex));
3042 return true;
3043 }
3044 return false;
3045 }
3046 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3047 boolean needSave = false;
3048 try {
3049 if (data != null) {
3050 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3051 Bitmap loaded = info.getIcon(mIconCache);
3052 needSave = !saved.sameAs(loaded);
3053 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003054 needSave = true;
3055 }
Winson Chungaac01e12011-08-17 10:37:13 -07003056 } catch (Exception e) {
3057 needSave = true;
3058 }
3059 if (needSave) {
3060 Log.d(TAG, "going to save icon bitmap for info=" + info);
3061 // This is slower than is ideal, but this only happens once
3062 // or when the app is updated with a new icon.
3063 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003064 }
3065 }
3066
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003068 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 * or make a new one.
3070 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003071 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 // See if a placeholder was created for us already
3073 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003074 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003076 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077 folders.put(id, folderInfo);
3078 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003079 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 }
3081
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003082 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003083 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003084 return new Comparator<AppInfo>() {
3085 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003086 int result = collator.compare(a.title.toString().trim(),
3087 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003088 if (result == 0) {
3089 result = a.componentName.compareTo(b.componentName);
3090 }
3091 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003092 }
Winson Chung11904872012-09-17 16:58:46 -07003093 };
3094 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003095 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3096 = new Comparator<AppInfo>() {
3097 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003098 if (a.firstInstallTime < b.firstInstallTime) return 1;
3099 if (a.firstInstallTime > b.firstInstallTime) return -1;
3100 return 0;
3101 }
3102 };
Winson Chung11904872012-09-17 16:58:46 -07003103 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3104 final Collator collator = Collator.getInstance();
3105 return new Comparator<AppWidgetProviderInfo>() {
3106 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003107 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003108 }
3109 };
3110 }
Winson Chung5308f242011-08-18 12:12:41 -07003111 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3112 if (info.activityInfo != null) {
3113 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3114 } else {
3115 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3116 }
3117 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003118 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003119 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003120 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003121 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003122 ShortcutNameComparator(PackageManager pm) {
3123 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003124 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003125 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003126 }
3127 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3128 mPackageManager = pm;
3129 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003130 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003131 }
3132 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003133 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003134 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3135 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3136 if (mLabelCache.containsKey(keyA)) {
3137 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003138 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003139 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003140
Winson Chung5308f242011-08-18 12:12:41 -07003141 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003142 }
Winson Chung5308f242011-08-18 12:12:41 -07003143 if (mLabelCache.containsKey(keyB)) {
3144 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003145 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003146 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003147
Winson Chung5308f242011-08-18 12:12:41 -07003148 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 }
Winson Chung11904872012-09-17 16:58:46 -07003150 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003151 }
3152 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003153 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003154 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003155 private PackageManager mPackageManager;
3156 private HashMap<Object, String> mLabelCache;
3157 WidgetAndShortcutNameComparator(PackageManager pm) {
3158 mPackageManager = pm;
3159 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003160 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003161 }
3162 public final int compare(Object a, Object b) {
3163 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003164 if (mLabelCache.containsKey(a)) {
3165 labelA = mLabelCache.get(a);
3166 } else {
3167 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003168 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003169 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003170 mLabelCache.put(a, labelA);
3171 }
3172 if (mLabelCache.containsKey(b)) {
3173 labelB = mLabelCache.get(b);
3174 } else {
3175 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003176 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003177 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003178 mLabelCache.put(b, labelB);
3179 }
Winson Chung11904872012-09-17 16:58:46 -07003180 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003181 }
3182 };
Joe Onoratobe386092009-11-17 17:32:16 -08003183
3184 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003185 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003186 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3187 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3188 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3189 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003190 if (mLoaderTask != null) {
3191 mLoaderTask.dumpState();
3192 } else {
3193 Log.d(TAG, "mLoaderTask=null");
3194 }
Joe Onoratobe386092009-11-17 17:32:16 -08003195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196}