blob: cb2d23b5fbac8724126614f34f48d9b9a0af74f6 [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 Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080047
Michael Jurkac2f801e2011-07-12 14:19:46 -070048import java.lang.ref.WeakReference;
49import java.net.URISyntaxException;
50import java.text.Collator;
51import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070052import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070053import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070058import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070061import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070062import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Maintains in-memory state of the Launcher. It is expected that there should be only one
66 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070067 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040069public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070072
Daniel Sandler8707e0f2013-08-15 15:54:18 -070073 // true = use a "More Apps" folder for non-workspace apps on upgrade
74 // false = strew non-workspace apps across the workspace on upgrade
75 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
76
Joe Onorato36115782010-06-17 13:28:48 -040077 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050078 private static final long INVALID_SCREEN_ID = -1L;
Winson Chungee055712013-07-30 14:46:24 -070079 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040080
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 private final Object mLock = new Object();
83 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040084 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070085 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020086 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Winson Chung81b52252012-08-27 15:34:29 -070088 // Specific runnable types that are run on the main thread deferred handler, this allows us to
89 // clear all queued binding runnables when the Launcher activity is destroyed.
90 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
91 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
92
93
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070094 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
95 static {
96 sWorkerThread.start();
97 }
98 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
99
Joe Onoratocc67f472010-06-08 10:54:30 -0700100 // We start off with everything not loaded. After that, we assume that
101 // our monitoring of the package manager provides all updates and we never
102 // need to do a requery. These are only ever touched from the loader thread.
103 private boolean mWorkspaceLoaded;
104 private boolean mAllAppsLoaded;
105
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700106 // When we are loading pages synchronously, we can't just post the binding of items on the side
107 // pages as this delays the rotation process. Instead, we wait for a callback from the first
108 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
109 // a normal load, we also clear this set of Runnables.
110 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700114 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700115 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800116
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700117 // The lock that must be acquired before referencing any static bg data structures. Unlike
118 // other locks, this one can generally be held long-term because we never expect any of these
119 // static data structures to be referenced outside of the worker thread except on the first
120 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700121 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700126
Adam Cohen487f7dd2012-06-28 18:12:10 -0700127 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
128 // created by LauncherModel that are directly on the home screen (however, no widgets or
129 // shortcuts within folders).
130 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
133 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700134 new ArrayList<LauncherAppWidgetInfo>();
135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
137 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700138
Adam Cohen487f7dd2012-06-28 18:12:10 -0700139 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
140 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700141
142 // sBgWorkspaceScreens is the ordered set of workspace screens.
143 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
144
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 // </ only access in worker thread >
146
147 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800148 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Reena Lee99a73f32011-10-24 17:27:37 -0700150 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700151
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700153 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 public int getCurrentWorkspaceScreen();
155 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700156 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
157 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700158 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700159 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500160 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700161 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200163 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700164 public void bindAppsAdded(ArrayList<Long> newScreens,
165 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700166 ArrayList<ItemInfo> addAnimated,
167 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200168 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700169 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700170 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200171 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100172 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700173 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700174 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700175 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung64359a52013-07-08 17:17:08 -0700178 public interface ItemInfoFilter {
179 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
180 }
181
Bjorn Bringert1307f632013-10-03 22:31:03 +0100182 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400183 final Context context = app.getContext();
184
Winson Chungee055712013-07-30 14:46:24 -0700185 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400186 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100187 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 mIconCache = iconCache;
189
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700191 Configuration config = res.getConfiguration();
192 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800193 }
194
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700195 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
196 * posted on the main thread handler. */
197 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700198 runOnMainThread(r, 0);
199 }
200 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700201 if (sWorkerThread.getThreadId() == Process.myTid()) {
202 // If we are on the worker thread, post onto the main handler
203 mHandler.post(r);
204 } else {
205 r.run();
206 }
207 }
208
209 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
210 * posted on the worker thread handler. */
211 private static void runOnWorkerThread(Runnable r) {
212 if (sWorkerThread.getThreadId() == Process.myTid()) {
213 r.run();
214 } else {
215 // If we are not on the worker thread, then post to the worker handler
216 sWorker.post(r);
217 }
218 }
219
Winson Chungc9168342013-06-26 14:54:55 -0700220 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
221 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700222 LauncherAppState app = LauncherAppState.getInstance();
223 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
224 final int xCount = (int) grid.numColumns;
225 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700226 boolean[][] occupied = new boolean[xCount][yCount];
227
228 int cellX, cellY, spanX, spanY;
229 for (int i = 0; i < items.size(); ++i) {
230 final ItemInfo item = items.get(i);
231 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
232 if (item.screenId == screen) {
233 cellX = item.cellX;
234 cellY = item.cellY;
235 spanX = item.spanX;
236 spanY = item.spanY;
237 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
238 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
239 occupied[x][y] = true;
240 }
241 }
242 }
243 }
244 }
245
246 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
247 }
248 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700249 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700250 int firstScreenIndex,
251 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700252 // Lock on the app so that we don't try and get the items while apps are being added
253 LauncherAppState app = LauncherAppState.getInstance();
254 LauncherModel model = app.getModel();
255 boolean found = false;
256 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700257 if (sWorkerThread.getThreadId() != Process.myTid()) {
258 // Flush the LauncherModel worker thread, so that if we just did another
259 // processInstallShortcut, we give it time for its shortcut to get added to the
260 // database (getItemsInLocalCoordinates reads the database)
261 model.flushWorkerThread();
262 }
Winson Chungc9168342013-06-26 14:54:55 -0700263 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700264
265 // Try adding to the workspace screens incrementally, starting at the default or center
266 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700267 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
268 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700269 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700270 int[] tmpCoordinates = new int[2];
271 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700272 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700273 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700274 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700275 }
276 }
277 }
Winson Chungc9168342013-06-26 14:54:55 -0700278 return null;
279 }
280
Winson Chung94d67682013-09-25 16:29:40 -0700281 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
282 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700283 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700284 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700285 }
Winson Chung94d67682013-09-25 16:29:40 -0700286 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800287 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
288 if (workspaceApps == null || allAppsApps == null) {
289 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
290 }
Winson Chung94d67682013-09-25 16:29:40 -0700291 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700292 return;
Winson Chung997a9232013-07-24 15:33:46 -0700293 }
Winson Chung64359a52013-07-08 17:17:08 -0700294 // Process the newly added applications and add them to the database first
295 Runnable r = new Runnable() {
296 public void run() {
297 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
298 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
299
Winson Chung76828c82013-08-19 15:43:29 -0700300 // Get the list of workspace screens. We need to append to this list and
301 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
302 // called.
303 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
304 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
305 for (Integer i : orderedScreens.keySet()) {
306 long screenId = orderedScreens.get(i);
307 workspaceScreens.add(screenId);
308 }
309
Winson Chung64359a52013-07-08 17:17:08 -0700310 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700311 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700312 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700313 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700314 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700315 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700316
317 // Short-circuit this logic if the icon exists somewhere on the workspace
318 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
319 continue;
320 }
321
Winson Chung87412982013-10-03 18:34:14 -0700322 // Add this icon to the db, creating a new page if necessary. If there
323 // is only the empty page then we just add items to the first page.
324 // Otherwise, we add them to the next pages.
325 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700326 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700327 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700328 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200329 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700330
331 // If we can't find a valid position, then just add a new screen.
332 // This takes time so we need to re-queue the add until the new
333 // page is added. Create as many screens as necessary to satisfy
334 // the startSearchPageIndex.
335 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700336 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 while (numPagesToAdd > 0) {
338 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700339 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700340 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700341 addedWorkspaceScreensFinal.add(screenId);
342 numPagesToAdd--;
343 }
Winson Chung76828c82013-08-19 15:43:29 -0700344
Winson Chung64359a52013-07-08 17:17:08 -0700345 // Find the coordinate again
346 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700347 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700348 }
349 if (coords == null) {
350 throw new RuntimeException("Coordinates should not be null");
351 }
352
Winson Chung997a9232013-07-24 15:33:46 -0700353 ShortcutInfo shortcutInfo;
354 if (a instanceof ShortcutInfo) {
355 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200356 } else if (a instanceof AppInfo) {
357 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700358 } else {
359 throw new RuntimeException("Unexpected info type");
360 }
Winson Chung94d67682013-09-25 16:29:40 -0700361
Winson Chung64359a52013-07-08 17:17:08 -0700362 // Add the shortcut to the db
363 addItemToDatabase(context, shortcutInfo,
364 LauncherSettings.Favorites.CONTAINER_DESKTOP,
365 coords.first, coords.second[0], coords.second[1], false);
366 // Save the ShortcutInfo for binding in the workspace
367 addedShortcutsFinal.add(shortcutInfo);
368 }
369 }
370
Winson Chung76828c82013-08-19 15:43:29 -0700371 // Update the workspace screens
372 updateWorkspaceScreenOrder(context, workspaceScreens);
373
Winson Chung94d67682013-09-25 16:29:40 -0700374 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700375 runOnMainThread(new Runnable() {
376 public void run() {
377 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
378 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700379 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
380 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700381 if (!addedShortcutsFinal.isEmpty()) {
382 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
383 long lastScreenId = info.screenId;
384 for (ItemInfo i : addedShortcutsFinal) {
385 if (i.screenId == lastScreenId) {
386 addAnimated.add(i);
387 } else {
388 addNotAnimated.add(i);
389 }
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
391 }
Winson Chungd64d1762013-08-20 14:37:16 -0700392 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700393 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700394 }
Winson Chung64359a52013-07-08 17:17:08 -0700395 }
Winson Chung997a9232013-07-24 15:33:46 -0700396 });
397 }
Winson Chung64359a52013-07-08 17:17:08 -0700398 }
399 };
400 runOnWorkerThread(r);
401 }
402
Joe Onorato56d82912010-03-07 14:32:10 -0500403 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700404 if (mDefaultIcon == null) {
405 final Context context = LauncherAppState.getInstance().getContext();
406 mDefaultIcon = Utilities.createIconBitmap(
407 mIconCache.getFullResDefaultActivityIcon(), context);
408 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800409 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411
Winson Chung81b52252012-08-27 15:34:29 -0700412 public void unbindItemInfosAndClearQueuedBindRunnables() {
413 if (sWorkerThread.getThreadId() == Process.myTid()) {
414 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
415 "main thread");
416 }
417
418 // Clear any deferred bind runnables
419 mDeferredBindRunnables.clear();
420 // Remove any queued bind runnables
421 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
422 // Unbind all the workspace items
423 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700424 }
425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700427 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700428 // Ensure that we don't use the same workspace items data structure on the main thread
429 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
431 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700432 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
434 tmpAppWidgets.addAll(sBgAppWidgets);
435 }
436 Runnable r = new Runnable() {
437 @Override
438 public void run() {
439 for (ItemInfo item : tmpWorkspaceItems) {
440 item.unbind();
441 }
442 for (ItemInfo item : tmpAppWidgets) {
443 item.unbind();
444 }
445 }
446 };
447 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700448 }
449
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 /**
451 * Adds an item to the DB if it was not created previously, or move it to a new
452 * <container, screen, cellX, cellY>
453 */
454 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700455 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400456 if (item.container == ItemInfo.NO_ID) {
457 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700458 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400459 } else {
460 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700461 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463 }
464
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700465 static void checkItemInfoLocked(
466 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
467 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
468 if (modelItem != null && item != modelItem) {
469 // check all the data is consistent
470 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
471 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
472 ShortcutInfo shortcut = (ShortcutInfo) item;
473 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
474 modelShortcut.intent.filterEquals(shortcut.intent) &&
475 modelShortcut.id == shortcut.id &&
476 modelShortcut.itemType == shortcut.itemType &&
477 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700478 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700479 modelShortcut.cellX == shortcut.cellX &&
480 modelShortcut.cellY == shortcut.cellY &&
481 modelShortcut.spanX == shortcut.spanX &&
482 modelShortcut.spanY == shortcut.spanY &&
483 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
484 (modelShortcut.dropPos != null &&
485 shortcut.dropPos != null &&
486 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
487 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
488 // For all intents and purposes, this is the same object
489 return;
490 }
491 }
492
493 // the modelItem needs to match up perfectly with item if our model is
494 // to be consistent with the database-- for now, just require
495 // modelItem == item or the equality check above
496 String msg = "item: " + ((item != null) ? item.toString() : "null") +
497 "modelItem: " +
498 ((modelItem != null) ? modelItem.toString() : "null") +
499 "Error: ItemInfo passed to checkItemInfo doesn't match original";
500 RuntimeException e = new RuntimeException(msg);
501 if (stackTrace != null) {
502 e.setStackTrace(stackTrace);
503 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800504 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 }
506 }
507
Michael Jurka816474f2012-06-25 14:49:02 -0700508 static void checkItemInfo(final ItemInfo item) {
509 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
510 final long itemId = item.id;
511 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700512 public void run() {
513 synchronized (sBgLock) {
514 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700515 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700516 }
517 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700518 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700519 }
520
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
522 final ItemInfo item, final String callingFunction) {
523 final long itemId = item.id;
524 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
525 final ContentResolver cr = context.getContentResolver();
526
Adam Cohen487f7dd2012-06-28 18:12:10 -0700527 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700528 Runnable r = new Runnable() {
529 public void run() {
530 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700531 updateItemArrays(item, itemId, stackTrace);
532 }
533 };
534 runOnWorkerThread(r);
535 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700536
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700537 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
538 final ArrayList<ItemInfo> items, final String callingFunction) {
539 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700540
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700541 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
542 Runnable r = new Runnable() {
543 public void run() {
544 ArrayList<ContentProviderOperation> ops =
545 new ArrayList<ContentProviderOperation>();
546 int count = items.size();
547 for (int i = 0; i < count; i++) {
548 ItemInfo item = items.get(i);
549 final long itemId = item.id;
550 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
551 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700552
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700553 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
554 updateItemArrays(item, itemId, stackTrace);
555
556 }
557 try {
558 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
559 } catch (Exception e) {
560 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700561 }
562 }
563 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700564 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700565 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700566
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700567 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
568 // Lock on mBgLock *after* the db operation
569 synchronized (sBgLock) {
570 checkItemInfoLocked(itemId, item, stackTrace);
571
572 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
573 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
574 // Item is in a folder, make sure this folder exists
575 if (!sBgFolders.containsKey(item.container)) {
576 // An items container is being set to a that of an item which is not in
577 // the list of Folders.
578 String msg = "item: " + item + " container being set to: " +
579 item.container + ", not in the list of folders";
580 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700581 }
582 }
583
584 // Items are added/removed from the corresponding FolderInfo elsewhere, such
585 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
586 // that are on the desktop, as appropriate
587 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800588 if (modelItem != null &&
589 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
590 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700591 switch (modelItem.itemType) {
592 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
593 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
594 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
595 if (!sBgWorkspaceItems.contains(modelItem)) {
596 sBgWorkspaceItems.add(modelItem);
597 }
598 break;
599 default:
600 break;
601 }
602 } else {
603 sBgWorkspaceItems.remove(modelItem);
604 }
605 }
606 }
607
Michael Jurkac7700af2013-05-14 20:17:58 +0200608 public void flushWorkerThread() {
609 mFlushingWorkerThread = true;
610 Runnable waiter = new Runnable() {
611 public void run() {
612 synchronized (this) {
613 notifyAll();
614 mFlushingWorkerThread = false;
615 }
616 }
617 };
618
619 synchronized(waiter) {
620 runOnWorkerThread(waiter);
621 if (mLoaderTask != null) {
622 synchronized(mLoaderTask) {
623 mLoaderTask.notify();
624 }
625 }
626 boolean success = false;
627 while (!success) {
628 try {
629 waiter.wait();
630 success = true;
631 } catch (InterruptedException e) {
632 }
633 }
634 }
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700639 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700640 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 item.cellX = cellX;
644 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700645
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 // We store hotseat items in canonical form which is this orientation invariant position
647 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700652 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700653 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
655 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700657 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
658 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700659 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660
Michael Jurkac9d95c52011-08-29 14:03:34 -0700661 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700662 }
663
664 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700665 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
666 * cellX, cellY have already been updated on the ItemInfos.
667 */
668 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
669 final long container, final int screen) {
670
671 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
672 int count = items.size();
673
674 for (int i = 0; i < count; i++) {
675 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 item.container = container;
677
678 // We store hotseat items in canonical form which is this orientation invariant position
679 // in the hotseat
680 if (context instanceof Launcher && screen < 0 &&
681 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700682 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700683 item.cellY);
684 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 }
687
688 final ContentValues values = new ContentValues();
689 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
690 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
691 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700692 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700693
694 contentValues.add(values);
695 }
696 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
697 }
698
699 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800701 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700704 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800705 item.cellX = cellX;
706 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 item.spanX = spanX;
708 item.spanY = spanY;
709
710 // We store hotseat items in canonical form which is this orientation invariant position
711 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700716 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 }
Adam Cohend4844c32011-02-18 19:25:06 -0800718
Adam Cohend4844c32011-02-18 19:25:06 -0800719 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800720 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700721 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
722 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
723 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
724 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700725 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800726
Michael Jurka816474f2012-06-25 14:49:02 -0700727 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700728 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700729
730 /**
731 * Update an item to the database in a specified container.
732 */
733 static void updateItemInDatabase(Context context, final ItemInfo item) {
734 final ContentValues values = new ContentValues();
735 item.onAddToDatabase(values);
736 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
737 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800738 }
739
740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 * Returns true if the shortcuts already exists in the database.
742 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400744 static boolean shortcutExists(Context context, String title, Intent intent) {
745 final ContentResolver cr = context.getContentResolver();
746 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
747 new String[] { "title", "intent" }, "title=? and intent=?",
748 new String[] { title, intent.toUri(0) }, null);
749 boolean result = false;
750 try {
751 result = c.moveToFirst();
752 } finally {
753 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700756 }
757
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700759 * Returns an ItemInfo array containing all the items in the LauncherModel.
760 * The ItemInfo.id is not set through this function.
761 */
762 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
763 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
764 final ContentResolver cr = context.getContentResolver();
765 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
766 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
767 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
768 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
769
770 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
771 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
772 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
773 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
774 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
775 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
776 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
777
778 try {
779 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700780 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700781 item.cellX = c.getInt(cellXIndex);
782 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700783 item.spanX = Math.max(1, c.getInt(spanXIndex));
784 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700785 item.container = c.getInt(containerIndex);
786 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700787 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700788
789 items.add(item);
790 }
791 } catch (Exception e) {
792 items.clear();
793 } finally {
794 c.close();
795 }
796
797 return items;
798 }
799
800 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
802 */
803 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
804 final ContentResolver cr = context.getContentResolver();
805 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
806 "_id=? and (itemType=? or itemType=?)",
807 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700808 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700809
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 try {
811 if (c.moveToFirst()) {
812 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
813 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
814 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
815 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
816 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
817 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 FolderInfo folderInfo = null;
820 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700821 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
822 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400823 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700824 }
825
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 folderInfo.title = c.getString(titleIndex);
827 folderInfo.id = id;
828 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700830 folderInfo.cellX = c.getInt(cellXIndex);
831 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
833 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400835 } finally {
836 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700837 }
838
839 return null;
840 }
841
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 /**
843 * Add an item to the database in a specified container. Sets the container, screen, cellX and
844 * cellY fields of the item. Also assigns an ID to the item.
845 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 item.cellX = cellX;
850 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 // We store hotseat items in canonical form which is this orientation invariant position
852 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859
860 final ContentValues values = new ContentValues();
861 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 item.onAddToDatabase(values);
863
Michael Jurka414300a2013-08-27 15:42:35 +0200864 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700865 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700866 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700867
Michael Jurkac9d95c52011-08-29 14:03:34 -0700868 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700869 public void run() {
870 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
871 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700873 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700874 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700875 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700876 sBgItemsIdMap.put(item.id, item);
877 switch (item.itemType) {
878 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
879 sBgFolders.put(item.id, (FolderInfo) item);
880 // Fall through
881 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
882 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
883 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
884 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
885 sBgWorkspaceItems.add(item);
886 } else {
887 if (!sBgFolders.containsKey(item.container)) {
888 // Adding an item to a folder that doesn't exist.
889 String msg = "adding item: " + item + " to a folder that " +
890 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700891 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700892 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700893 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 break;
895 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
896 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
897 break;
898 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700899 }
900 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700901 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700902 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700903 }
904
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 * Creates a new unique child id, for a given cell span across all layouts.
907 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700908 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700909 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700910 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700911 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700912 }
913
Winson Chungaafa03c2010-06-11 17:34:16 -0700914 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700915 * Removes the specified item from the database
916 * @param context
917 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700919 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700921 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700922
Michael Jurka83df1882011-08-31 20:59:26 -0700923 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700924 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700925 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700926
927 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700928 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700929 switch (item.itemType) {
930 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
931 sBgFolders.remove(item.id);
932 for (ItemInfo info: sBgItemsIdMap.values()) {
933 if (info.container == item.id) {
934 // We are deleting a folder which still contains items that
935 // think they are contained by that folder.
936 String msg = "deleting a folder (" + item + ") which still " +
937 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700938 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700939 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700940 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700941 sBgWorkspaceItems.remove(item);
942 break;
943 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
944 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
945 sBgWorkspaceItems.remove(item);
946 break;
947 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
948 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
949 break;
950 }
951 sBgItemsIdMap.remove(item.id);
952 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700953 }
954 }
Michael Jurka83df1882011-08-31 20:59:26 -0700955 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400957 }
958
959 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700960 * Update the order of the workspace screens in the database. The array list contains
961 * a list of screen ids in the order that they should appear.
962 */
Winson Chungc9168342013-06-26 14:54:55 -0700963 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800964 // Log to disk
965 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
966 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
967
Winson Chung64359a52013-07-08 17:17:08 -0700968 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700969 final ContentResolver cr = context.getContentResolver();
970 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
971
972 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700973 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 while (iter.hasNext()) {
975 long id = iter.next();
976 if (id < 0) {
977 iter.remove();
978 }
979 }
980
981 Runnable r = new Runnable() {
982 @Override
983 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 // Clear the table
985 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700986 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700987 ContentValues[] values = new ContentValues[count];
988 for (int i = 0; i < count; i++) {
989 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700990 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
992 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 values[i] = v;
994 }
995 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700996
Winson Chungba9c37f2013-08-30 14:11:37 -0700997 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700998 sBgWorkspaceScreens.clear();
999 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001000 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001001 }
1002 };
1003 runOnWorkerThread(r);
1004 }
1005
1006 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 * Remove the contents of the specified folder from the database
1008 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010 final ContentResolver cr = context.getContentResolver();
1011
Michael Jurkac9d95c52011-08-29 14:03:34 -07001012 Runnable r = new Runnable() {
1013 public void run() {
1014 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001016 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 sBgItemsIdMap.remove(info.id);
1018 sBgFolders.remove(info.id);
1019 sBgDbIconCache.remove(info);
1020 sBgWorkspaceItems.remove(info);
1021 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001022
Michael Jurkac9d95c52011-08-29 14:03:34 -07001023 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1024 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001026 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 for (ItemInfo childInfo : info.contents) {
1028 sBgItemsIdMap.remove(childInfo.id);
1029 sBgDbIconCache.remove(childInfo);
1030 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001031 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001032 }
1033 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001034 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 }
1036
1037 /**
1038 * Set this as the current Launcher activity object for the loader.
1039 */
1040 public void initialize(Callbacks callbacks) {
1041 synchronized (mLock) {
1042 mCallbacks = new WeakReference<Callbacks>(callbacks);
1043 }
1044 }
1045
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001046 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1048 * ACTION_PACKAGE_CHANGED.
1049 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001050 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001051 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001052 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001053
Joe Onorato36115782010-06-17 13:28:48 -04001054 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001055
Joe Onorato36115782010-06-17 13:28:48 -04001056 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1057 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1058 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1059 final String packageName = intent.getData().getSchemeSpecificPart();
1060 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061
Joe Onorato36115782010-06-17 13:28:48 -04001062 int op = PackageUpdatedTask.OP_NONE;
1063
1064 if (packageName == null || packageName.length() == 0) {
1065 // they sent us a bad intent
1066 return;
1067 }
1068
1069 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1070 op = PackageUpdatedTask.OP_UPDATE;
1071 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1072 if (!replacing) {
1073 op = PackageUpdatedTask.OP_REMOVE;
1074 }
1075 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1076 // later, we will update the package at this time
1077 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1078 if (!replacing) {
1079 op = PackageUpdatedTask.OP_ADD;
1080 } else {
1081 op = PackageUpdatedTask.OP_UPDATE;
1082 }
1083 }
1084
1085 if (op != PackageUpdatedTask.OP_NONE) {
1086 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1087 }
1088
1089 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001090 // First, schedule to add these apps back in.
1091 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1092 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1093 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001094 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001095 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1096 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1097 enqueuePackageUpdated(new PackageUpdatedTask(
1098 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001099 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001100 // If we have changed locale we need to clear out the labels in all apps/workspace.
1101 forceReload();
1102 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1103 // Check if configuration change was an mcc/mnc change which would affect app resources
1104 // and we would need to clear out the labels in all apps/workspace. Same handling as
1105 // above for ACTION_LOCALE_CHANGED
1106 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001107 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001108 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001109 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001110 forceReload();
1111 }
1112 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001113 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001114 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1115 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001116 if (mCallbacks != null) {
1117 Callbacks callbacks = mCallbacks.get();
1118 if (callbacks != null) {
1119 callbacks.bindSearchablesChanged();
1120 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001121 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001122 }
1123 }
1124
Reena Lee93f824a2011-09-23 17:20:28 -07001125 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001126 resetLoadedState(true, true);
1127
Reena Lee93f824a2011-09-23 17:20:28 -07001128 // Do this here because if the launcher activity is running it will be restarted.
1129 // If it's not running startLoaderFromBackground will merely tell it that it needs
1130 // to reload.
1131 startLoaderFromBackground();
1132 }
1133
Winson Chungf0c6ae02012-03-21 16:10:31 -07001134 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1135 synchronized (mLock) {
1136 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1137 // mWorkspaceLoaded to true later
1138 stopLoaderLocked();
1139 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1140 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1141 }
1142 }
1143
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001144 /**
1145 * When the launcher is in the background, it's possible for it to miss paired
1146 * configuration changes. So whenever we trigger the loader from the background
1147 * tell the launcher that it needs to re-run the loader when it comes back instead
1148 * of doing it now.
1149 */
1150 public void startLoaderFromBackground() {
1151 boolean runLoader = false;
1152 if (mCallbacks != null) {
1153 Callbacks callbacks = mCallbacks.get();
1154 if (callbacks != null) {
1155 // Only actually run the loader if they're not paused.
1156 if (!callbacks.setLoadOnResume()) {
1157 runLoader = true;
1158 }
1159 }
1160 }
1161 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001162 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001163 }
Joe Onorato36115782010-06-17 13:28:48 -04001164 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001165
Reena Lee93f824a2011-09-23 17:20:28 -07001166 // If there is already a loader task running, tell it to stop.
1167 // returns true if isLaunching() was true on the old task
1168 private boolean stopLoaderLocked() {
1169 boolean isLaunching = false;
1170 LoaderTask oldTask = mLoaderTask;
1171 if (oldTask != null) {
1172 if (oldTask.isLaunching()) {
1173 isLaunching = true;
1174 }
1175 oldTask.stopLocked();
1176 }
1177 return isLaunching;
1178 }
1179
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001180 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001181 synchronized (mLock) {
1182 if (DEBUG_LOADERS) {
1183 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 // Clear any deferred bind-runnables from the synchronized load process
1187 // We must do this before any loading/binding is scheduled below.
1188 mDeferredBindRunnables.clear();
1189
Joe Onorato36115782010-06-17 13:28:48 -04001190 // Don't bother to start the thread if we know it's not going to do anything
1191 if (mCallbacks != null && mCallbacks.get() != null) {
1192 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001193 // also, don't downgrade isLaunching if we're already running
1194 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001195 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Derek Prothro7aff3992013-12-10 14:00:37 -05001196 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1197 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1199 } else {
1200 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1201 sWorker.post(mLoaderTask);
1202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 }
1205 }
1206
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001207 void bindRemainingSynchronousPages() {
1208 // Post the remaining side pages to be loaded
1209 if (!mDeferredBindRunnables.isEmpty()) {
1210 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001211 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001212 }
1213 mDeferredBindRunnables.clear();
1214 }
1215 }
1216
Joe Onorato36115782010-06-17 13:28:48 -04001217 public void stopLoader() {
1218 synchronized (mLock) {
1219 if (mLoaderTask != null) {
1220 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221 }
1222 }
Joe Onorato36115782010-06-17 13:28:48 -04001223 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224
Winson Chung76828c82013-08-19 15:43:29 -07001225 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1226 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1227 final ContentResolver contentResolver = context.getContentResolver();
1228 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1229 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1230 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1231
1232 try {
1233 final int idIndex = sc.getColumnIndexOrThrow(
1234 LauncherSettings.WorkspaceScreens._ID);
1235 final int rankIndex = sc.getColumnIndexOrThrow(
1236 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1237 while (sc.moveToNext()) {
1238 try {
1239 long screenId = sc.getLong(idIndex);
1240 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001241 orderedScreens.put(rank, screenId);
1242 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001243 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001244 }
1245 }
1246 } finally {
1247 sc.close();
1248 }
Winson Chunga90303b2013-11-15 13:05:06 -08001249
1250 // Log to disk
1251 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1252 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1253 for (Integer i : orderedScreens.keySet()) {
1254 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1255 }
1256 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1257 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001258 return orderedScreens;
1259 }
1260
Michael Jurkac57b7a82011-08-09 22:02:20 -07001261 public boolean isAllAppsLoaded() {
1262 return mAllAppsLoaded;
1263 }
1264
Winson Chung36a62fe2012-05-06 18:04:42 -07001265 boolean isLoadingWorkspace() {
1266 synchronized (mLock) {
1267 if (mLoaderTask != null) {
1268 return mLoaderTask.isLoadingWorkspace();
1269 }
1270 }
1271 return false;
1272 }
1273
Joe Onorato36115782010-06-17 13:28:48 -04001274 /**
1275 * Runnable for the thread that loads the contents of the launcher:
1276 * - workspace icons
1277 * - widgets
1278 * - all apps icons
1279 */
1280 private class LoaderTask implements Runnable {
1281 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001282 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001283 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001284 private boolean mStopped;
1285 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001286
Winson Chungc3eecff2011-07-11 17:44:15 -07001287 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001288
1289 LoaderTask(Context context, boolean isLaunching) {
1290 mContext = context;
1291 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001292 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001293 }
1294
Joe Onorato36115782010-06-17 13:28:48 -04001295 boolean isLaunching() {
1296 return mIsLaunching;
1297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298
Winson Chung36a62fe2012-05-06 18:04:42 -07001299 boolean isLoadingWorkspace() {
1300 return mIsLoadingAndBindingWorkspace;
1301 }
1302
Winson Chungc763c4e2013-07-19 13:49:06 -07001303 /** Returns whether this is an upgrade path */
1304 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001305 mIsLoadingAndBindingWorkspace = true;
1306
Joe Onorato36115782010-06-17 13:28:48 -04001307 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001308 if (DEBUG_LOADERS) {
1309 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001311
Winson Chungc763c4e2013-07-19 13:49:06 -07001312 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001313 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001314 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001315 synchronized (LoaderTask.this) {
1316 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001317 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001318 }
1319 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 }
1321 }
1322
Joe Onorato36115782010-06-17 13:28:48 -04001323 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001324 bindWorkspace(-1, isUpgradePath);
1325 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001326 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001327
Joe Onorato36115782010-06-17 13:28:48 -04001328 private void waitForIdle() {
1329 // Wait until the either we're stopped or the other threads are done.
1330 // This way we don't start loading all apps until the workspace has settled
1331 // down.
1332 synchronized (LoaderTask.this) {
1333 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001334
Joe Onorato36115782010-06-17 13:28:48 -04001335 mHandler.postIdle(new Runnable() {
1336 public void run() {
1337 synchronized (LoaderTask.this) {
1338 mLoadAndBindStepFinished = true;
1339 if (DEBUG_LOADERS) {
1340 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Joe Onorato36115782010-06-17 13:28:48 -04001342 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001343 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001344 }
Joe Onorato36115782010-06-17 13:28:48 -04001345 });
1346
Michael Jurkac7700af2013-05-14 20:17:58 +02001347 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001348 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001349 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1350 // wait no longer than 1sec at a time
1351 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001352 } catch (InterruptedException ex) {
1353 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001354 }
1355 }
Joe Onorato36115782010-06-17 13:28:48 -04001356 if (DEBUG_LOADERS) {
1357 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001358 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001359 + "ms for previous step to finish binding");
1360 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001361 }
Joe Onorato36115782010-06-17 13:28:48 -04001362 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001363
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001364 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001365 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001366 // Ensure that we have a valid page index to load synchronously
1367 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1368 "valid page index");
1369 }
1370 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1371 // Ensure that we don't try and bind a specified page when the pages have not been
1372 // loaded already (we should load everything asynchronously in that case)
1373 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1374 }
1375 synchronized (mLock) {
1376 if (mIsLoaderTaskRunning) {
1377 // Ensure that we are never running the background loading at this point since
1378 // we also touch the background collections
1379 throw new RuntimeException("Error! Background loading is already running");
1380 }
1381 }
1382
1383 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1384 // data structures, we can't allow any other thread to touch that data, but because
1385 // this call is synchronous, we can get away with not locking).
1386
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001387 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001388 // operations from the previous activity. We need to ensure that all queued operations
1389 // are executed before any synchronous binding work is done.
1390 mHandler.flush();
1391
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 // Divide the set of loaded items into those that we are binding synchronously, and
1393 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001394 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001395 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1396 // arise from that.
1397 onlyBindAllApps();
1398 }
1399
Joe Onorato36115782010-06-17 13:28:48 -04001400 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001401 boolean isUpgrade = false;
1402
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001403 synchronized (mLock) {
1404 mIsLoaderTaskRunning = true;
1405 }
Joe Onorato36115782010-06-17 13:28:48 -04001406 // Optimize for end-user experience: if the Launcher is up and // running with the
1407 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1408 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001409 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 // Elevate priority when Home launches for the first time to avoid
1411 // starving at boot time. Staring at a blank home is not cool.
1412 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001413 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1414 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001415 android.os.Process.setThreadPriority(mIsLaunching
1416 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1417 }
Winson Chung64359a52013-07-08 17:17:08 -07001418 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001419 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001420
Joe Onorato36115782010-06-17 13:28:48 -04001421 if (mStopped) {
1422 break keep_running;
1423 }
1424
1425 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1426 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001427 synchronized (mLock) {
1428 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001429 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1431 }
1432 }
Joe Onorato36115782010-06-17 13:28:48 -04001433 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001434
1435 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001436 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1437 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001438
1439 // Restore the default thread priority after we are done loading items
1440 synchronized (mLock) {
1441 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1442 }
Joe Onorato36115782010-06-17 13:28:48 -04001443 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444
Winson Chungaac01e12011-08-17 10:37:13 -07001445 // Update the saved icons if necessary
1446 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001447 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001448 for (Object key : sBgDbIconCache.keySet()) {
1449 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1450 }
1451 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001452 }
Winson Chungaac01e12011-08-17 10:37:13 -07001453
Winson Chungc58497e2013-09-03 17:48:37 -07001454 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1455 // Ensure that all the applications that are in the system are
1456 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001457 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001458 verifyApplications();
1459 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001460 }
1461
Joe Onorato36115782010-06-17 13:28:48 -04001462 // Clear out this reference, otherwise we end up holding it until all of the
1463 // callback runnables are done.
1464 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465
Joe Onorato36115782010-06-17 13:28:48 -04001466 synchronized (mLock) {
1467 // If we are still the last one to be scheduled, remove ourselves.
1468 if (mLoaderTask == this) {
1469 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001470 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001471 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473 }
1474
1475 public void stopLocked() {
1476 synchronized (LoaderTask.this) {
1477 mStopped = true;
1478 this.notify();
1479 }
1480 }
1481
1482 /**
1483 * Gets the callbacks object. If we've been stopped, or if the launcher object
1484 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1485 * object that was around when the deferred message was scheduled, and if there's
1486 * a new Callbacks object around then also return null. This will save us from
1487 * calling onto it with data that will be ignored.
1488 */
1489 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1490 synchronized (mLock) {
1491 if (mStopped) {
1492 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001493 }
Joe Onorato36115782010-06-17 13:28:48 -04001494
1495 if (mCallbacks == null) {
1496 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001497 }
Joe Onorato36115782010-06-17 13:28:48 -04001498
1499 final Callbacks callbacks = mCallbacks.get();
1500 if (callbacks != oldCallbacks) {
1501 return null;
1502 }
1503 if (callbacks == null) {
1504 Log.w(TAG, "no mCallbacks");
1505 return null;
1506 }
1507
1508 return callbacks;
1509 }
1510 }
1511
Winson Chungc763c4e2013-07-19 13:49:06 -07001512 private void verifyApplications() {
1513 final Context context = mApp.getContext();
1514
1515 // Cross reference all the applications in our apps list with items in the workspace
1516 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001517 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001518 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001519 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001520 tmpInfos = getItemInfoForComponentName(app.componentName);
1521 if (tmpInfos.isEmpty()) {
1522 // We are missing an application icon, so add this to the workspace
1523 added.add(app);
1524 // This is a rare event, so lets log it
1525 Log.e(TAG, "Missing Application on load: " + app);
1526 }
1527 }
1528 }
1529 if (!added.isEmpty()) {
1530 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001531 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001532 }
1533 }
1534
Winson Chung5f8afe62013-08-12 16:19:28 -07001535 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001536 LauncherAppState app = LauncherAppState.getInstance();
1537 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1538 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1539 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001540 }
1541
Joe Onorato36115782010-06-17 13:28:48 -04001542 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001543 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1544 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001545 LauncherAppState app = LauncherAppState.getInstance();
1546 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001547 final int countX = (int) grid.numColumns;
1548 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001549
Adam Cohendcd297f2013-06-18 13:13:40 -07001550 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001551 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001552 // Return early if we detect that an item is under the hotseat button
1553 if (mCallbacks == null ||
1554 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1555 deleteOnItemOverlap.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001556 Log.e(TAG, "Error loading shortcut into hotseat " + item
1557 + " into position (" + item.screenId + ":" + item.cellX + ","
1558 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001559 return false;
1560 }
1561
Dan Sandler295ae182013-12-10 16:05:47 -05001562 final ItemInfo[][] hotseatItems =
1563 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1564
1565 if (hotseatItems != null) {
1566 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 Log.e(TAG, "Error loading shortcut into hotseat " + item
1568 + " into position (" + item.screenId + ":" + item.cellX + ","
1569 + item.cellY + ") occupied by "
1570 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1571 [(int) item.screenId][0]);
1572 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001573 } else {
1574 hotseatItems[(int) item.screenId][0] = item;
1575 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001577 } else {
Dan Sandler295ae182013-12-10 16:05:47 -05001578 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons + 1][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 items[(int) item.screenId][0] = item;
1580 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001581 return true;
1582 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001583 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1584 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001585 return true;
1586 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001587
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001589 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 occupied.put(item.screenId, items);
1591 }
1592
Dan Sandler295ae182013-12-10 16:05:47 -05001593 final ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001594 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001595 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1596 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001598 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001600 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001601 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001602 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001603 return false;
1604 }
1605 }
1606 }
1607 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1608 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001610 }
1611 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001612
Joe Onorato36115782010-06-17 13:28:48 -04001613 return true;
1614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001615
Winson Chungba9c37f2013-08-30 14:11:37 -07001616 /** Clears all the sBg data structures */
1617 private void clearSBgDataStructures() {
1618 synchronized (sBgLock) {
1619 sBgWorkspaceItems.clear();
1620 sBgAppWidgets.clear();
1621 sBgFolders.clear();
1622 sBgItemsIdMap.clear();
1623 sBgDbIconCache.clear();
1624 sBgWorkspaceScreens.clear();
1625 }
1626 }
1627
Winson Chungc763c4e2013-07-19 13:49:06 -07001628 /** Returns whether this is an upgradge path */
1629 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001630 // Log to disk
1631 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1632
Joe Onorato36115782010-06-17 13:28:48 -04001633 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634
Joe Onorato36115782010-06-17 13:28:48 -04001635 final Context context = mContext;
1636 final ContentResolver contentResolver = context.getContentResolver();
1637 final PackageManager manager = context.getPackageManager();
1638 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1639 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001640
Winson Chung892c74d2013-08-22 16:15:50 -07001641 LauncherAppState app = LauncherAppState.getInstance();
1642 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1643 int countX = (int) grid.numColumns;
1644 int countY = (int) grid.numRows;
1645
Michael Jurkab85f8a42012-04-25 15:48:32 -07001646 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001647 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001648
Winson Chungc763c4e2013-07-19 13:49:06 -07001649 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001650 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001651 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001652
Winson Chung9f9f00b2013-11-15 13:27:00 -08001653 // Log to disk
1654 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001655
Winson Chung2abf94d2012-07-18 18:16:38 -07001656 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001657 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001658
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001659 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001660 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001661 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001662 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001663
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001664 // +1 for the hotseat (it can be larger than the workspace)
1665 // Load workspace in reverse order to ensure that latest items are loaded first (and
1666 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001667 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 try {
1670 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1671 final int intentIndex = c.getColumnIndexOrThrow
1672 (LauncherSettings.Favorites.INTENT);
1673 final int titleIndex = c.getColumnIndexOrThrow
1674 (LauncherSettings.Favorites.TITLE);
1675 final int iconTypeIndex = c.getColumnIndexOrThrow(
1676 LauncherSettings.Favorites.ICON_TYPE);
1677 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1678 final int iconPackageIndex = c.getColumnIndexOrThrow(
1679 LauncherSettings.Favorites.ICON_PACKAGE);
1680 final int iconResourceIndex = c.getColumnIndexOrThrow(
1681 LauncherSettings.Favorites.ICON_RESOURCE);
1682 final int containerIndex = c.getColumnIndexOrThrow(
1683 LauncherSettings.Favorites.CONTAINER);
1684 final int itemTypeIndex = c.getColumnIndexOrThrow(
1685 LauncherSettings.Favorites.ITEM_TYPE);
1686 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001688 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1689 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 final int screenIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.SCREEN);
1692 final int cellXIndex = c.getColumnIndexOrThrow
1693 (LauncherSettings.Favorites.CELLX);
1694 final int cellYIndex = c.getColumnIndexOrThrow
1695 (LauncherSettings.Favorites.CELLY);
1696 final int spanXIndex = c.getColumnIndexOrThrow
1697 (LauncherSettings.Favorites.SPANX);
1698 final int spanYIndex = c.getColumnIndexOrThrow(
1699 LauncherSettings.Favorites.SPANY);
1700 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1701 //final int displayModeIndex = c.getColumnIndexOrThrow(
1702 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 ShortcutInfo info;
1705 String intentDescription;
1706 LauncherAppWidgetInfo appWidgetInfo;
1707 int container;
1708 long id;
1709 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001711 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001712 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 try {
1714 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 switch (itemType) {
1717 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1718 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001719 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001720 intentDescription = c.getString(intentIndex);
1721 try {
1722 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001723 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001724 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001725 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001726 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001727 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1728 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001729 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001730 // If apps can be on external storage, then we just
1731 // leave them for the user to remove (maybe add
1732 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001733 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001734 }
1735 continue;
1736 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001738 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 continue;
1740 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1743 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1744 titleIndex, mLabelCache);
1745 } else {
1746 info = getShortcutInfo(c, context, iconTypeIndex,
1747 iconPackageIndex, iconResourceIndex, iconIndex,
1748 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001749
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 // App shortcuts that used to be automatically added to Launcher
1751 // didn't always have the correct intent flags set, so do that
1752 // here
1753 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001754 intent.getCategories() != null &&
1755 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001756 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001757 intent.addFlags(
1758 Intent.FLAG_ACTIVITY_NEW_TASK |
1759 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1760 }
Michael Jurka96879562012-03-22 05:54:33 -07001761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001764 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 container = c.getInt(containerIndex);
1767 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001768 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 info.cellX = c.getInt(cellXIndex);
1770 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001771 info.spanX = 1;
1772 info.spanY = 1;
1773 // Skip loading items that are out of bounds
1774 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1775 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001776 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1777 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001778 continue;
1779 }
1780 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001782 deleteOnItemOverlap.set(false);
1783 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1784 if (deleteOnItemOverlap.get()) {
1785 itemsToRemove.add(id);
1786 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001787 break;
1788 }
1789
1790 switch (container) {
1791 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1792 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1793 sBgWorkspaceItems.add(info);
1794 break;
1795 default:
1796 // Item is in a user folder
1797 FolderInfo folderInfo =
1798 findOrMakeFolder(sBgFolders, container);
1799 folderInfo.add(info);
1800 break;
1801 }
1802 sBgItemsIdMap.put(info.id, info);
1803
1804 // now that we've loaded everthing re-save it with the
1805 // icon in case it disappears somehow.
1806 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001807 } else {
1808 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 }
1810 break;
1811
1812 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1813 id = c.getLong(idIndex);
1814 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1815
1816 folderInfo.title = c.getString(titleIndex);
1817 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001820 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001821 folderInfo.cellX = c.getInt(cellXIndex);
1822 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001823 folderInfo.spanX = 1;
1824 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825
Winson Chung5f8afe62013-08-12 16:19:28 -07001826 // Skip loading items that are out of bounds
1827 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001828 if (checkItemDimensions(folderInfo)) {
1829 Log.d(TAG, "Skipped loading out of bounds folder");
1830 continue;
1831 }
1832 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001833 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001834 deleteOnItemOverlap.set(false);
1835 if (!checkItemPlacement(occupied, folderInfo,
1836 deleteOnItemOverlap)) {
1837 if (deleteOnItemOverlap.get()) {
1838 itemsToRemove.add(id);
1839 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001840 break;
1841 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001842
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1845 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1846 sBgWorkspaceItems.add(folderInfo);
1847 break;
Joe Onorato36115782010-06-17 13:28:48 -04001848 }
Joe Onorato17a89222011-02-08 17:26:11 -08001849
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1851 sBgFolders.put(folderInfo.id, folderInfo);
1852 break;
1853
1854 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1855 // Read all Launcher-specific widget details
1856 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001857 String savedProvider = c.getString(appWidgetProviderIndex);
1858
Joe Onorato36115782010-06-17 13:28:48 -04001859 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 final AppWidgetProviderInfo provider =
1862 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001863
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 if (!isSafeMode && (provider == null || provider.provider == null ||
1865 provider.provider.getPackageName() == null)) {
1866 String log = "Deleting widget that isn't installed anymore: id="
1867 + id + " appWidgetId=" + appWidgetId;
1868 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001869 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 itemsToRemove.add(id);
1871 } else {
1872 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1873 provider.provider);
1874 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001875 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 appWidgetInfo.cellX = c.getInt(cellXIndex);
1877 appWidgetInfo.cellY = c.getInt(cellYIndex);
1878 appWidgetInfo.spanX = c.getInt(spanXIndex);
1879 appWidgetInfo.spanY = c.getInt(spanYIndex);
1880 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1881 appWidgetInfo.minSpanX = minSpan[0];
1882 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001883
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 container = c.getInt(containerIndex);
1885 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1886 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1887 Log.e(TAG, "Widget found where container != " +
1888 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1889 continue;
1890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891
Adam Cohene25af792013-06-06 23:08:25 -07001892 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001893 // Skip loading items that are out of bounds
1894 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1895 if (checkItemDimensions(appWidgetInfo)) {
1896 Log.d(TAG, "Skipped loading out of bounds app widget");
1897 continue;
1898 }
1899 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001900 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001901 deleteOnItemOverlap.set(false);
1902 if (!checkItemPlacement(occupied, appWidgetInfo,
1903 deleteOnItemOverlap)) {
1904 if (deleteOnItemOverlap.get()) {
1905 itemsToRemove.add(id);
1906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 break;
1908 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001909 String providerName = provider.provider.flattenToString();
1910 if (!providerName.equals(savedProvider)) {
1911 ContentValues values = new ContentValues();
1912 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1913 providerName);
1914 String where = BaseColumns._ID + "= ?";
1915 String[] args = {Integer.toString(c.getInt(idIndex))};
1916 contentResolver.update(contentUri, values, where, args);
1917 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1919 sBgAppWidgets.add(appWidgetInfo);
1920 }
Joe Onorato36115782010-06-17 13:28:48 -04001921 break;
1922 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05001924 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001925 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 }
1927 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001928 if (c != null) {
1929 c.close();
1930 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 }
1932
Winson Chungba9c37f2013-08-30 14:11:37 -07001933 // Break early if we've stopped loading
1934 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001935 clearSBgDataStructures();
1936 return false;
1937 }
1938
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001939 if (itemsToRemove.size() > 0) {
1940 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001941 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001942 // Remove dead items
1943 for (long id : itemsToRemove) {
1944 if (DEBUG_LOADERS) {
1945 Log.d(TAG, "Removed id = " + id);
1946 }
1947 // Don't notify content observers
1948 try {
1949 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1950 null, null);
1951 } catch (RemoteException e) {
1952 Log.w(TAG, "Could not remove id = " + id);
1953 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001954 }
1955 }
1956
Winson Chungc763c4e2013-07-19 13:49:06 -07001957 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 long maxScreenId = 0;
1959 // If we're importing we use the old screen order.
1960 for (ItemInfo item: sBgItemsIdMap.values()) {
1961 long screenId = item.screenId;
1962 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1963 !sBgWorkspaceScreens.contains(screenId)) {
1964 sBgWorkspaceScreens.add(screenId);
1965 if (screenId > maxScreenId) {
1966 maxScreenId = screenId;
1967 }
1968 }
1969 }
1970 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001971 // Log to disk
1972 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1973 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1974 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001975
Michael Jurka414300a2013-08-27 15:42:35 +02001976 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001978
1979 // Update the max item id after we load an old db
1980 long maxItemId = 0;
1981 // If we're importing we use the old screen order.
1982 for (ItemInfo item: sBgItemsIdMap.values()) {
1983 maxItemId = Math.max(maxItemId, item.id);
1984 }
Michael Jurka414300a2013-08-27 15:42:35 +02001985 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001987 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1988 for (Integer i : orderedScreens.keySet()) {
1989 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001991 // Log to disk
1992 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1993 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001994
1995 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001996 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 for (ItemInfo item: sBgItemsIdMap.values()) {
1998 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001999 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2000 unusedScreens.contains(screenId)) {
2001 unusedScreens.remove(screenId);
2002 }
2003 }
2004
2005 // If there are any empty screens remove them, and update.
2006 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002007 // Log to disk
2008 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2009 TextUtils.join(", ", unusedScreens), true);
2010
Winson Chung933bae62013-08-29 11:42:30 -07002011 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002012 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2013 }
2014 }
2015
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 if (DEBUG_LOADERS) {
2017 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2018 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002020 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002022
Daniel Sandler566da102013-06-25 23:43:45 -04002023 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002024 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002025 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002026 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 line += " | ";
2028 }
Winson Chung892c74d2013-08-22 16:15:50 -07002029 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 }
Joe Onorato36115782010-06-17 13:28:48 -04002032 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 }
Joe Onorato36115782010-06-17 13:28:48 -04002036 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002037 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002038 }
2039
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 /** Filters the set of items who are directly or indirectly (via another container) on the
2041 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002042 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 ArrayList<ItemInfo> allWorkspaceItems,
2044 ArrayList<ItemInfo> currentScreenItems,
2045 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002046 // Purge any null ItemInfos
2047 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2048 while (iter.hasNext()) {
2049 ItemInfo i = iter.next();
2050 if (i == null) {
2051 iter.remove();
2052 }
2053 }
2054
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 // Order the set of items by their containers first, this allows use to walk through the
2056 // list sequentially, build up a list of containers that are in the specified screen,
2057 // as well as all items in those containers.
2058 Set<Long> itemsOnScreen = new HashSet<Long>();
2059 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2060 @Override
2061 public int compare(ItemInfo lhs, ItemInfo rhs) {
2062 return (int) (lhs.container - rhs.container);
2063 }
2064 });
2065 for (ItemInfo info : allWorkspaceItems) {
2066 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002067 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002068 currentScreenItems.add(info);
2069 itemsOnScreen.add(info.id);
2070 } else {
2071 otherScreenItems.add(info);
2072 }
2073 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2074 currentScreenItems.add(info);
2075 itemsOnScreen.add(info.id);
2076 } else {
2077 if (itemsOnScreen.contains(info.container)) {
2078 currentScreenItems.add(info);
2079 itemsOnScreen.add(info.id);
2080 } else {
2081 otherScreenItems.add(info);
2082 }
2083 }
2084 }
2085 }
2086
2087 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002088 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 ArrayList<LauncherAppWidgetInfo> appWidgets,
2090 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2091 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002092
2093 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002094 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002096 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 currentScreenWidgets.add(widget);
2098 } else {
2099 otherScreenWidgets.add(widget);
2100 }
2101 }
2102 }
2103
2104 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002105 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 HashMap<Long, ItemInfo> itemsIdMap,
2107 HashMap<Long, FolderInfo> folders,
2108 HashMap<Long, FolderInfo> currentScreenFolders,
2109 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002110
2111 for (long id : folders.keySet()) {
2112 ItemInfo info = itemsIdMap.get(id);
2113 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002114 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002116 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 currentScreenFolders.put(id, folder);
2118 } else {
2119 otherScreenFolders.put(id, folder);
2120 }
2121 }
2122 }
2123
2124 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2125 * right) */
2126 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002127 final LauncherAppState app = LauncherAppState.getInstance();
2128 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 // XXX: review this
2130 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002131 @Override
2132 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002133 int cellCountX = (int) grid.numColumns;
2134 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002135 int screenOffset = cellCountX * cellCountY;
2136 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002138 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002140 rhs.cellY * cellCountX + rhs.cellX);
2141 return (int) (lr - rr);
2142 }
2143 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002145
Adam Cohendcd297f2013-06-18 13:13:40 -07002146 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2147 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002148 final Runnable r = new Runnable() {
2149 @Override
2150 public void run() {
2151 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2152 if (callbacks != null) {
2153 callbacks.bindScreens(orderedScreens);
2154 }
2155 }
2156 };
2157 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2158 }
2159
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2161 final ArrayList<ItemInfo> workspaceItems,
2162 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2163 final HashMap<Long, FolderInfo> folders,
2164 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002165
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 final boolean postOnMainThread = (deferredBindRunnables != null);
2167
2168 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002169 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002170 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002171 final int start = i;
2172 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 final Runnable r = new Runnable() {
2174 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002176 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002178 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2179 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 }
2181 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002182 };
2183 if (postOnMainThread) {
2184 deferredBindRunnables.add(r);
2185 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002186 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 }
Joe Onorato36115782010-06-17 13:28:48 -04002188 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189
2190 // Bind the folders
2191 if (!folders.isEmpty()) {
2192 final Runnable r = new Runnable() {
2193 public void run() {
2194 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2195 if (callbacks != null) {
2196 callbacks.bindFolders(folders);
2197 }
2198 }
2199 };
2200 if (postOnMainThread) {
2201 deferredBindRunnables.add(r);
2202 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002203 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002204 }
2205 }
2206
2207 // Bind the widgets, one at a time
2208 N = appWidgets.size();
2209 for (int i = 0; i < N; i++) {
2210 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2211 final Runnable r = new Runnable() {
2212 public void run() {
2213 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2214 if (callbacks != null) {
2215 callbacks.bindAppWidget(widget);
2216 }
2217 }
2218 };
2219 if (postOnMainThread) {
2220 deferredBindRunnables.add(r);
2221 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002222 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 }
2224 }
2225 }
2226
2227 /**
2228 * Binds all loaded data to actual views on the main thread.
2229 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002230 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 final long t = SystemClock.uptimeMillis();
2232 Runnable r;
2233
2234 // Don't use these two variables in any of the callback runnables.
2235 // Otherwise we hold a reference to them.
2236 final Callbacks oldCallbacks = mCallbacks.get();
2237 if (oldCallbacks == null) {
2238 // This launcher has exited and nobody bothered to tell us. Just bail.
2239 Log.w(TAG, "LoaderTask running with no launcher");
2240 return;
2241 }
2242
Winson Chung9b9fb962013-11-15 15:39:34 -08002243 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002244 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2245 ArrayList<LauncherAppWidgetInfo> appWidgets =
2246 new ArrayList<LauncherAppWidgetInfo>();
2247 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2248 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002250 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002251 workspaceItems.addAll(sBgWorkspaceItems);
2252 appWidgets.addAll(sBgAppWidgets);
2253 folders.putAll(sBgFolders);
2254 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 }
2257
Derek Prothro7aff3992013-12-10 14:00:37 -05002258 final boolean isLoadingSynchronously =
2259 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002260 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002261 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002262 if (currScreen >= orderedScreenIds.size()) {
2263 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002264 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002265 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002266 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002267 final long currentScreenId = currentScreen < 0
2268 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002269
2270 // Load all the items that are on the current page first (and in the process, unbind
2271 // all the existing workspace items before we call startBinding() below.
2272 unbindWorkspaceItemsOnMainThread();
2273
2274 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2276 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2277 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2278 new ArrayList<LauncherAppWidgetInfo>();
2279 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2280 new ArrayList<LauncherAppWidgetInfo>();
2281 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2282 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2283
Winson Chung9b9fb962013-11-15 15:39:34 -08002284 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002286 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002288 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 otherFolders);
2290 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2291 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2292
2293 // Tell the workspace that we're about to start binding items
2294 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002295 public void run() {
2296 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2297 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002299 }
2300 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002301 };
Winson Chung81b52252012-08-27 15:34:29 -07002302 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2305
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 // Load items on the current page
2307 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2308 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002309 if (isLoadingSynchronously) {
2310 r = new Runnable() {
2311 public void run() {
2312 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002313 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002314 callbacks.onPageBoundSynchronously(currentScreen);
2315 }
2316 }
2317 };
Winson Chung81b52252012-08-27 15:34:29 -07002318 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320
Winson Chung4a2afa32012-07-19 14:53:05 -07002321 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2322 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323 mDeferredBindRunnables.clear();
2324 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002325 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002326
2327 // Tell the workspace that we're done binding items
2328 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002329 public void run() {
2330 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2331 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002332 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002333 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334
Winson Chung98e030b2012-05-07 16:01:11 -07002335 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002336 if (DEBUG_LOADERS) {
2337 Log.d(TAG, "bound workspace in "
2338 + (SystemClock.uptimeMillis()-t) + "ms");
2339 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002340
2341 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002342 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002343 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002344 if (isLoadingSynchronously) {
2345 mDeferredBindRunnables.add(r);
2346 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002347 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002348 }
Joe Onorato36115782010-06-17 13:28:48 -04002349 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002350
Joe Onorato36115782010-06-17 13:28:48 -04002351 private void loadAndBindAllApps() {
2352 if (DEBUG_LOADERS) {
2353 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2354 }
2355 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002356 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002357 synchronized (LoaderTask.this) {
2358 if (mStopped) {
2359 return;
2360 }
2361 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002362 }
Joe Onorato36115782010-06-17 13:28:48 -04002363 } else {
2364 onlyBindAllApps();
2365 }
2366 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002367
Joe Onorato36115782010-06-17 13:28:48 -04002368 private void onlyBindAllApps() {
2369 final Callbacks oldCallbacks = mCallbacks.get();
2370 if (oldCallbacks == null) {
2371 // This launcher has exited and nobody bothered to tell us. Just bail.
2372 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2373 return;
2374 }
2375
2376 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002377 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002378 final ArrayList<AppInfo> list
2379 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002380 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002381 public void run() {
2382 final long t = SystemClock.uptimeMillis();
2383 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2384 if (callbacks != null) {
2385 callbacks.bindAllApplications(list);
2386 }
2387 if (DEBUG_LOADERS) {
2388 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2389 + (SystemClock.uptimeMillis()-t) + "ms");
2390 }
2391 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002392 };
2393 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002394 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002395 r.run();
2396 } else {
2397 mHandler.post(r);
2398 }
Joe Onorato36115782010-06-17 13:28:48 -04002399 }
2400
Winson Chung64359a52013-07-08 17:17:08 -07002401 private void loadAllApps() {
2402 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002403
Joe Onorato36115782010-06-17 13:28:48 -04002404 final Callbacks oldCallbacks = mCallbacks.get();
2405 if (oldCallbacks == null) {
2406 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002407 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002408 return;
2409 }
2410
Winson Chung64359a52013-07-08 17:17:08 -07002411 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002412 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2413 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2414
Winson Chung64359a52013-07-08 17:17:08 -07002415 // Clear the list of apps
2416 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002417
Winson Chung64359a52013-07-08 17:17:08 -07002418 // Query for the set of apps
2419 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2420 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2421 if (DEBUG_LOADERS) {
2422 Log.d(TAG, "queryIntentActivities took "
2423 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2424 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2425 }
2426 // Fail if we don't have any apps
2427 if (apps == null || apps.isEmpty()) {
2428 return;
2429 }
2430 // Sort the applications by name
2431 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2432 Collections.sort(apps,
2433 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2434 if (DEBUG_LOADERS) {
2435 Log.d(TAG, "sort took "
2436 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 }
2438
Winson Chung64359a52013-07-08 17:17:08 -07002439 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002440 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002441 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002442 // This builds the icon bitmaps.
2443 mBgAllAppsList.add(new AppInfo(packageManager, app,
2444 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002445 }
2446
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002447 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002448 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2449 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002450
2451 // Post callback on main thread
2452 mHandler.post(new Runnable() {
2453 public void run() {
2454 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002455 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002456 if (callbacks != null) {
2457 callbacks.bindAllApplications(added);
2458 if (DEBUG_LOADERS) {
2459 Log.d(TAG, "bound " + added.size() + " apps in "
2460 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2461 }
2462 } else {
2463 Log.i(TAG, "not binding apps: no Launcher activity");
2464 }
2465 }
2466 });
2467
Joe Onorato36115782010-06-17 13:28:48 -04002468 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002469 Log.d(TAG, "Icons processed in "
2470 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002471 }
2472 }
2473
2474 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002475 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002476 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2477 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2478 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2479 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2480 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2481 }
Joe Onorato36115782010-06-17 13:28:48 -04002482 }
2483 }
2484
2485 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002486 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002487 }
2488
2489 private class PackageUpdatedTask implements Runnable {
2490 int mOp;
2491 String[] mPackages;
2492
2493 public static final int OP_NONE = 0;
2494 public static final int OP_ADD = 1;
2495 public static final int OP_UPDATE = 2;
2496 public static final int OP_REMOVE = 3; // uninstlled
2497 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2498
2499
2500 public PackageUpdatedTask(int op, String[] packages) {
2501 mOp = op;
2502 mPackages = packages;
2503 }
2504
2505 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002506 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002507
2508 final String[] packages = mPackages;
2509 final int N = packages.length;
2510 switch (mOp) {
2511 case OP_ADD:
2512 for (int i=0; i<N; i++) {
2513 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002514 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002515 }
2516 break;
2517 case OP_UPDATE:
2518 for (int i=0; i<N; i++) {
2519 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002520 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002521 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002522 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002523 }
2524 break;
2525 case OP_REMOVE:
2526 case OP_UNAVAILABLE:
2527 for (int i=0; i<N; i++) {
2528 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002529 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002530 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002531 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002532 }
2533 break;
2534 }
2535
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002536 ArrayList<AppInfo> added = null;
2537 ArrayList<AppInfo> modified = null;
2538 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002539
Adam Cohen487f7dd2012-06-28 18:12:10 -07002540 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002541 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002542 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002543 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002544 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002545 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002546 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002547 }
Winson Chung5d55f332012-07-16 20:45:03 -07002548 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002549 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002550 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002551 }
2552
Joe Onorato36115782010-06-17 13:28:48 -04002553 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2554 if (callbacks == null) {
2555 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2556 return;
2557 }
2558
2559 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002560 // Ensure that we add all the workspace applications to the db
2561 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002562 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2563 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2564 } else {
2565 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2566 addAndBindAddedApps(context, addedInfos, cb, added);
2567 }
Joe Onorato36115782010-06-17 13:28:48 -04002568 }
2569 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002570 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002571
2572 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002573 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002574 ArrayList<ItemInfo> infos =
2575 getItemInfoForComponentName(a.componentName);
2576 for (ItemInfo i : infos) {
2577 if (isShortcutInfoUpdateable(i)) {
2578 ShortcutInfo info = (ShortcutInfo) i;
2579 info.title = a.title.toString();
2580 updateItemInDatabase(context, info);
2581 }
2582 }
2583 }
2584
Joe Onorato36115782010-06-17 13:28:48 -04002585 mHandler.post(new Runnable() {
2586 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002587 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2588 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002589 callbacks.bindAppsUpdated(modifiedFinal);
2590 }
2591 }
2592 });
2593 }
Winson Chung83892cc2013-05-01 16:53:33 -07002594
Winson Chungdf95eb12013-10-16 14:57:07 -07002595 final ArrayList<String> removedPackageNames =
2596 new ArrayList<String>();
2597 if (mOp == OP_REMOVE) {
2598 // Mark all packages in the broadcast to be removed
2599 removedPackageNames.addAll(Arrays.asList(packages));
2600 } else if (mOp == OP_UPDATE) {
2601 // Mark disabled packages in the broadcast to be removed
2602 final PackageManager pm = context.getPackageManager();
2603 for (int i=0; i<N; i++) {
2604 if (isPackageDisabled(pm, packages[i])) {
2605 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002606 }
2607 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002608 }
2609 // Remove all the components associated with this package
2610 for (String pn : removedPackageNames) {
2611 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2612 for (ItemInfo i : infos) {
2613 deleteItemFromDatabase(context, i);
2614 }
2615 }
2616 // Remove all the specific components
2617 for (AppInfo a : removedApps) {
2618 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2619 for (ItemInfo i : infos) {
2620 deleteItemFromDatabase(context, i);
2621 }
2622 }
2623 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2624 // Remove any queued items from the install queue
2625 String spKey = LauncherAppState.getSharedPreferencesKey();
2626 SharedPreferences sp =
2627 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2628 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2629 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002630 mHandler.post(new Runnable() {
2631 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002632 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2633 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002634 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002635 }
2636 }
2637 });
Joe Onoratobe386092009-11-17 17:32:16 -08002638 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002639
Michael Jurkac402cd92013-05-20 15:49:32 +02002640 final ArrayList<Object> widgetsAndShortcuts =
2641 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002642 mHandler.post(new Runnable() {
2643 @Override
2644 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002645 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2646 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002647 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002648 }
2649 }
2650 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002651
2652 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002653 mHandler.post(new Runnable() {
2654 public void run() {
2655 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2656 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002657 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002658 }
2659 }
2660 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002661 }
2662 }
2663
Michael Jurkac402cd92013-05-20 15:49:32 +02002664 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2665 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2666 PackageManager packageManager = context.getPackageManager();
2667 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2668 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2669 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2670 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2671 Collections.sort(widgetsAndShortcuts,
2672 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2673 return widgetsAndShortcuts;
2674 }
2675
Winson Chungdf95eb12013-10-16 14:57:07 -07002676 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2677 try {
2678 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2679 return !pi.applicationInfo.enabled;
2680 } catch (NameNotFoundException e) {
2681 // Fall through
2682 }
2683 return false;
2684 }
Winson Chung1323b482013-08-05 12:41:55 -07002685 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002686 if (cn == null) {
2687 return false;
2688 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002689 if (isPackageDisabled(pm, cn.getPackageName())) {
2690 return false;
2691 }
Winson Chungee055712013-07-30 14:46:24 -07002692
2693 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002694 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002695 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002696 return (pm.getActivityInfo(cn, 0) != null);
2697 } catch (NameNotFoundException e) {
2698 return false;
2699 }
2700 }
2701
Joe Onorato9c1289c2009-08-17 11:03:03 -04002702 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002703 * This is called from the code that adds shortcuts from the intent receiver. This
2704 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002705 */
Joe Onorato56d82912010-03-07 14:32:10 -05002706 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002707 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002709
Joe Onorato56d82912010-03-07 14:32:10 -05002710 /**
2711 * Make an ShortcutInfo object for a shortcut that is an application.
2712 *
2713 * If c is not null, then it will be used to fill in missing data like the title and icon.
2714 */
2715 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002716 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002717 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002718 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002719 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002720 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002721 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002722 } else {
2723 try {
2724 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2725 info.initFlagsAndFirstInstallTime(pi);
2726 } catch (NameNotFoundException e) {
2727 Log.d(TAG, "getPackInfo failed for package " +
2728 componentName.getPackageName());
2729 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002730 }
2731
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002732 // TODO: See if the PackageManager knows about this case. If it doesn't
2733 // then return null & delete this.
2734
Joe Onorato56d82912010-03-07 14:32:10 -05002735 // the resource -- This may implicitly give us back the fallback icon,
2736 // but don't worry about that. All we're doing with usingFallbackIcon is
2737 // to avoid saving lots of copies of that in the database, and most apps
2738 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002739
2740 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2741 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2742 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002743 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002744 ResolveInfo resolveInfo = null;
2745 ComponentName oldComponent = intent.getComponent();
2746 Intent newIntent = new Intent(intent.getAction(), null);
2747 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2748 newIntent.setPackage(oldComponent.getPackageName());
2749 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2750 for (ResolveInfo i : infos) {
2751 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2752 i.activityInfo.name);
2753 if (cn.equals(oldComponent)) {
2754 resolveInfo = i;
2755 }
2756 }
2757 if (resolveInfo == null) {
2758 resolveInfo = manager.resolveActivity(intent, 0);
2759 }
Joe Onorato56d82912010-03-07 14:32:10 -05002760 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002761 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002762 }
Joe Onorato56d82912010-03-07 14:32:10 -05002763 // the db
2764 if (icon == null) {
2765 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002766 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002767 }
2768 }
2769 // the fallback icon
2770 if (icon == null) {
2771 icon = getFallbackIcon();
2772 info.usingFallbackIcon = true;
2773 }
2774 info.setIcon(icon);
2775
2776 // from the resource
2777 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002778 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2779 if (labelCache != null && labelCache.containsKey(key)) {
2780 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002781 } else {
2782 info.title = resolveInfo.activityInfo.loadLabel(manager);
2783 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002784 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002785 }
2786 }
Joe Onorato56d82912010-03-07 14:32:10 -05002787 }
2788 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002789 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002790 if (c != null) {
2791 info.title = c.getString(titleIndex);
2792 }
2793 }
2794 // fall back to the class name of the activity
2795 if (info.title == null) {
2796 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2799 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002801
Winson Chung64359a52013-07-08 17:17:08 -07002802 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2803 ItemInfoFilter f) {
2804 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2805 for (ItemInfo i : infos) {
2806 if (i instanceof ShortcutInfo) {
2807 ShortcutInfo info = (ShortcutInfo) i;
2808 ComponentName cn = info.intent.getComponent();
2809 if (cn != null && f.filterItem(null, info, cn)) {
2810 filtered.add(info);
2811 }
2812 } else if (i instanceof FolderInfo) {
2813 FolderInfo info = (FolderInfo) i;
2814 for (ShortcutInfo s : info.contents) {
2815 ComponentName cn = s.intent.getComponent();
2816 if (cn != null && f.filterItem(info, s, cn)) {
2817 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002818 }
2819 }
Winson Chung64359a52013-07-08 17:17:08 -07002820 } else if (i instanceof LauncherAppWidgetInfo) {
2821 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2822 ComponentName cn = info.providerName;
2823 if (cn != null && f.filterItem(null, info, cn)) {
2824 filtered.add(info);
2825 }
Winson Chung8a435102012-08-30 17:16:53 -07002826 }
2827 }
Winson Chung64359a52013-07-08 17:17:08 -07002828 return new ArrayList<ItemInfo>(filtered);
2829 }
2830
2831 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002832 ItemInfoFilter filter = new ItemInfoFilter() {
2833 @Override
2834 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2835 return cn.getPackageName().equals(pn);
2836 }
2837 };
2838 return filterItemInfos(sBgItemsIdMap.values(), filter);
2839 }
2840
2841 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002842 ItemInfoFilter filter = new ItemInfoFilter() {
2843 @Override
2844 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2845 return cn.equals(cname);
2846 }
2847 };
2848 return filterItemInfos(sBgItemsIdMap.values(), filter);
2849 }
2850
2851 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2852 if (i instanceof ShortcutInfo) {
2853 ShortcutInfo info = (ShortcutInfo) i;
2854 // We need to check for ACTION_MAIN otherwise getComponent() might
2855 // return null for some shortcuts (for instance, for shortcuts to
2856 // web pages.)
2857 Intent intent = info.intent;
2858 ComponentName name = intent.getComponent();
2859 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2860 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2861 return true;
2862 }
2863 }
2864 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002865 }
2866
2867 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002868 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002870 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002871 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2872 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873
Joe Onorato56d82912010-03-07 14:32:10 -05002874 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002875 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002878 // TODO: If there's an explicit component and we can't install that, delete it.
2879
Joe Onorato56d82912010-03-07 14:32:10 -05002880 info.title = c.getString(titleIndex);
2881
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 int iconType = c.getInt(iconTypeIndex);
2883 switch (iconType) {
2884 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2885 String packageName = c.getString(iconPackageIndex);
2886 String resourceName = c.getString(iconResourceIndex);
2887 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002888 info.customIcon = false;
2889 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002892 if (resources != null) {
2893 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002894 icon = Utilities.createIconBitmap(
2895 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002898 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 }
Joe Onorato56d82912010-03-07 14:32:10 -05002900 // the db
2901 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002902 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002903 }
2904 // the fallback icon
2905 if (icon == null) {
2906 icon = getFallbackIcon();
2907 info.usingFallbackIcon = true;
2908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 break;
2910 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002911 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002912 if (icon == null) {
2913 icon = getFallbackIcon();
2914 info.customIcon = false;
2915 info.usingFallbackIcon = true;
2916 } else {
2917 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 break;
2920 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002921 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002922 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 info.customIcon = false;
2924 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002926 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 return info;
2928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929
Michael Jurka931dc972011-08-05 15:08:15 -07002930 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002931 @SuppressWarnings("all") // suppress dead code warning
2932 final boolean debug = false;
2933 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002934 Log.d(TAG, "getIconFromCursor app="
2935 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2936 }
2937 byte[] data = c.getBlob(iconIndex);
2938 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002939 return Utilities.createIconBitmap(
2940 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002941 } catch (Exception e) {
2942 return null;
2943 }
2944 }
2945
Winson Chung3d503fb2011-07-13 17:25:49 -07002946 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2947 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002948 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002949 if (info == null) {
2950 return null;
2951 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002952 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002953
2954 return info;
2955 }
2956
Winson Chunga9abd0e2010-10-27 17:18:37 -07002957 /**
Winson Chung55cef262010-10-28 14:14:18 -07002958 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2959 */
2960 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2961 ComponentName component) {
2962 List<AppWidgetProviderInfo> widgets =
2963 AppWidgetManager.getInstance(context).getInstalledProviders();
2964 for (AppWidgetProviderInfo info : widgets) {
2965 if (info.provider.equals(component)) {
2966 return info;
2967 }
2968 }
2969 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002970 }
2971
Winson Chung68846fd2010-10-29 11:00:27 -07002972 /**
2973 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2974 */
2975 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2976 final PackageManager packageManager = context.getPackageManager();
2977 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2978 new ArrayList<WidgetMimeTypeHandlerData>();
2979
2980 final Intent supportsIntent =
2981 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2982 supportsIntent.setType(mimeType);
2983
2984 // Create a set of widget configuration components that we can test against
2985 final List<AppWidgetProviderInfo> widgets =
2986 AppWidgetManager.getInstance(context).getInstalledProviders();
2987 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2988 new HashMap<ComponentName, AppWidgetProviderInfo>();
2989 for (AppWidgetProviderInfo info : widgets) {
2990 configurationComponentToWidget.put(info.configure, info);
2991 }
2992
2993 // Run through each of the intents that can handle this type of clip data, and cross
2994 // reference them with the components that are actual configuration components
2995 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2996 PackageManager.MATCH_DEFAULT_ONLY);
2997 for (ResolveInfo info : activities) {
2998 final ActivityInfo activityInfo = info.activityInfo;
2999 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3000 activityInfo.name);
3001 if (configurationComponentToWidget.containsKey(infoComponent)) {
3002 supportedConfigurationActivities.add(
3003 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3004 configurationComponentToWidget.get(infoComponent)));
3005 }
3006 }
3007 return supportedConfigurationActivities;
3008 }
3009
Winson Chunga9abd0e2010-10-27 17:18:37 -07003010 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003011 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3012 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3013 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3014
Adam Cohend9198822011-11-22 16:42:47 -08003015 if (intent == null) {
3016 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3017 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3018 return null;
3019 }
3020
Joe Onorato0589f0f2010-02-08 13:44:00 -08003021 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003022 boolean customIcon = false;
3023 ShortcutIconResource iconResource = null;
3024
3025 if (bitmap != null && bitmap instanceof Bitmap) {
3026 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003027 customIcon = true;
3028 } else {
3029 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3030 if (extra != null && extra instanceof ShortcutIconResource) {
3031 try {
3032 iconResource = (ShortcutIconResource) extra;
3033 final PackageManager packageManager = context.getPackageManager();
3034 Resources resources = packageManager.getResourcesForApplication(
3035 iconResource.packageName);
3036 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003037 icon = Utilities.createIconBitmap(
3038 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003039 } catch (Exception e) {
3040 Log.w(TAG, "Could not load shortcut icon: " + extra);
3041 }
3042 }
3043 }
3044
Michael Jurkac9d95c52011-08-29 14:03:34 -07003045 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003046
3047 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003048 if (fallbackIcon != null) {
3049 icon = fallbackIcon;
3050 } else {
3051 icon = getFallbackIcon();
3052 info.usingFallbackIcon = true;
3053 }
Joe Onorato56d82912010-03-07 14:32:10 -05003054 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003055 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003056
Joe Onorato0589f0f2010-02-08 13:44:00 -08003057 info.title = name;
3058 info.intent = intent;
3059 info.customIcon = customIcon;
3060 info.iconResource = iconResource;
3061
3062 return info;
3063 }
3064
Winson Chungaac01e12011-08-17 10:37:13 -07003065 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3066 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003067 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003068 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003069 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003070 }
Joe Onorato56d82912010-03-07 14:32:10 -05003071 // If this icon doesn't have a custom icon, check to see
3072 // what's stored in the DB, and if it doesn't match what
3073 // we're going to show, store what we are going to show back
3074 // into the DB. We do this so when we're loading, if the
3075 // package manager can't find an icon (for example because
3076 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003077 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003078 cache.put(info, c.getBlob(iconIndex));
3079 return true;
3080 }
3081 return false;
3082 }
3083 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3084 boolean needSave = false;
3085 try {
3086 if (data != null) {
3087 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3088 Bitmap loaded = info.getIcon(mIconCache);
3089 needSave = !saved.sameAs(loaded);
3090 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003091 needSave = true;
3092 }
Winson Chungaac01e12011-08-17 10:37:13 -07003093 } catch (Exception e) {
3094 needSave = true;
3095 }
3096 if (needSave) {
3097 Log.d(TAG, "going to save icon bitmap for info=" + info);
3098 // This is slower than is ideal, but this only happens once
3099 // or when the app is updated with a new icon.
3100 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003101 }
3102 }
3103
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003105 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 * or make a new one.
3107 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003108 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 // See if a placeholder was created for us already
3110 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003111 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003113 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 folders.put(id, folderInfo);
3115 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003116 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
3118
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003119 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003120 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003121 return new Comparator<AppInfo>() {
3122 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003123 int result = collator.compare(a.title.toString().trim(),
3124 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003125 if (result == 0) {
3126 result = a.componentName.compareTo(b.componentName);
3127 }
3128 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003129 }
Winson Chung11904872012-09-17 16:58:46 -07003130 };
3131 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003132 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3133 = new Comparator<AppInfo>() {
3134 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003135 if (a.firstInstallTime < b.firstInstallTime) return 1;
3136 if (a.firstInstallTime > b.firstInstallTime) return -1;
3137 return 0;
3138 }
3139 };
Winson Chung11904872012-09-17 16:58:46 -07003140 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3141 final Collator collator = Collator.getInstance();
3142 return new Comparator<AppWidgetProviderInfo>() {
3143 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003144 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003145 }
3146 };
3147 }
Winson Chung5308f242011-08-18 12:12:41 -07003148 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3149 if (info.activityInfo != null) {
3150 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3151 } else {
3152 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3153 }
3154 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003155 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003156 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003157 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003158 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003159 ShortcutNameComparator(PackageManager pm) {
3160 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003161 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003162 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003163 }
3164 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3165 mPackageManager = pm;
3166 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003167 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003168 }
3169 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003170 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003171 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3172 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3173 if (mLabelCache.containsKey(keyA)) {
3174 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003176 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003177
Winson Chung5308f242011-08-18 12:12:41 -07003178 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003179 }
Winson Chung5308f242011-08-18 12:12:41 -07003180 if (mLabelCache.containsKey(keyB)) {
3181 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003182 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003183 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003184
Winson Chung5308f242011-08-18 12:12:41 -07003185 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003186 }
Winson Chung11904872012-09-17 16:58:46 -07003187 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003188 }
3189 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003190 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003191 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003192 private PackageManager mPackageManager;
3193 private HashMap<Object, String> mLabelCache;
3194 WidgetAndShortcutNameComparator(PackageManager pm) {
3195 mPackageManager = pm;
3196 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003197 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003198 }
3199 public final int compare(Object a, Object b) {
3200 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003201 if (mLabelCache.containsKey(a)) {
3202 labelA = mLabelCache.get(a);
3203 } else {
3204 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003205 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003206 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003207 mLabelCache.put(a, labelA);
3208 }
3209 if (mLabelCache.containsKey(b)) {
3210 labelB = mLabelCache.get(b);
3211 } else {
3212 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003213 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003214 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003215 mLabelCache.put(b, labelB);
3216 }
Winson Chung11904872012-09-17 16:58:46 -07003217 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003218 }
3219 };
Joe Onoratobe386092009-11-17 17:32:16 -08003220
3221 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003222 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003223 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3224 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3225 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3226 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003227 if (mLoaderTask != null) {
3228 mLoaderTask.dumpState();
3229 } else {
3230 Log.d(TAG, "mLoaderTask=null");
3231 }
Joe Onoratobe386092009-11-17 17:32:16 -08003232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233}