blob: da805a05a647a7a332f096a1bdf5a4ef48c2b385 [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
Joe Onorato36115782010-06-17 13:28:48 -04001535 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001536 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001537 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001538 LauncherAppState app = LauncherAppState.getInstance();
1539 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001540 final int countX = (int) grid.numColumns;
1541 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001542
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001544 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001545 // Return early if we detect that an item is under the hotseat button
1546 if (mCallbacks == null ||
1547 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001548 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001549 Log.e(TAG, "Error loading shortcut into hotseat " + item
1550 + " into position (" + item.screenId + ":" + item.cellX + ","
1551 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001552 return false;
1553 }
1554
Dan Sandler295ae182013-12-10 16:05:47 -05001555 final ItemInfo[][] hotseatItems =
1556 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1557
Adam Cohenae4409d2013-11-26 10:34:59 -08001558 if (item.screenId >= grid.numHotseatIcons) {
1559 Log.e(TAG, "Error loading shortcut " + item
1560 + " into hotseat position " + item.screenId
1561 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1562 + ")");
1563 return false;
1564 }
1565
Dan Sandler295ae182013-12-10 16:05:47 -05001566 if (hotseatItems != null) {
1567 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 Log.e(TAG, "Error loading shortcut into hotseat " + item
1569 + " into position (" + item.screenId + ":" + item.cellX + ","
1570 + item.cellY + ") occupied by "
1571 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1572 [(int) item.screenId][0]);
1573 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001574 } else {
1575 hotseatItems[(int) item.screenId][0] = item;
1576 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001578 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001579 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 items[(int) item.screenId][0] = item;
1581 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001582 return true;
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1585 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001586 return true;
1587 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001588
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001590 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 occupied.put(item.screenId, items);
1592 }
1593
Dan Sandler295ae182013-12-10 16:05:47 -05001594 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001595 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1596 item.cellX < 0 || item.cellY < 0 ||
1597 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1598 Log.e(TAG, "Error loading shortcut " + item
1599 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1600 + item.cellX + "," + item.cellY
1601 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1602 return false;
1603 }
1604
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001605 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001606 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1607 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001608 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001609 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001611 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001612 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001614 return false;
1615 }
1616 }
1617 }
1618 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1619 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001620 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001621 }
1622 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001623
Joe Onorato36115782010-06-17 13:28:48 -04001624 return true;
1625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626
Winson Chungba9c37f2013-08-30 14:11:37 -07001627 /** Clears all the sBg data structures */
1628 private void clearSBgDataStructures() {
1629 synchronized (sBgLock) {
1630 sBgWorkspaceItems.clear();
1631 sBgAppWidgets.clear();
1632 sBgFolders.clear();
1633 sBgItemsIdMap.clear();
1634 sBgDbIconCache.clear();
1635 sBgWorkspaceScreens.clear();
1636 }
1637 }
1638
Winson Chungc763c4e2013-07-19 13:49:06 -07001639 /** Returns whether this is an upgradge path */
1640 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001641 // Log to disk
1642 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1643
Joe Onorato36115782010-06-17 13:28:48 -04001644 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645
Joe Onorato36115782010-06-17 13:28:48 -04001646 final Context context = mContext;
1647 final ContentResolver contentResolver = context.getContentResolver();
1648 final PackageManager manager = context.getPackageManager();
1649 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1650 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001651
Winson Chung892c74d2013-08-22 16:15:50 -07001652 LauncherAppState app = LauncherAppState.getInstance();
1653 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1654 int countX = (int) grid.numColumns;
1655 int countY = (int) grid.numRows;
1656
Michael Jurkab85f8a42012-04-25 15:48:32 -07001657 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001658 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001659
Winson Chungc763c4e2013-07-19 13:49:06 -07001660 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001661 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001662 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001663
Winson Chung9f9f00b2013-11-15 13:27:00 -08001664 // Log to disk
1665 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001666
Winson Chung2abf94d2012-07-18 18:16:38 -07001667 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001668 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001669
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001670 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001671 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001672 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001673 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001674
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 // +1 for the hotseat (it can be larger than the workspace)
1676 // Load workspace in reverse order to ensure that latest items are loaded first (and
1677 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 try {
1681 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1682 final int intentIndex = c.getColumnIndexOrThrow
1683 (LauncherSettings.Favorites.INTENT);
1684 final int titleIndex = c.getColumnIndexOrThrow
1685 (LauncherSettings.Favorites.TITLE);
1686 final int iconTypeIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.ICON_TYPE);
1688 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1689 final int iconPackageIndex = c.getColumnIndexOrThrow(
1690 LauncherSettings.Favorites.ICON_PACKAGE);
1691 final int iconResourceIndex = c.getColumnIndexOrThrow(
1692 LauncherSettings.Favorites.ICON_RESOURCE);
1693 final int containerIndex = c.getColumnIndexOrThrow(
1694 LauncherSettings.Favorites.CONTAINER);
1695 final int itemTypeIndex = c.getColumnIndexOrThrow(
1696 LauncherSettings.Favorites.ITEM_TYPE);
1697 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1698 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001699 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1700 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 final int screenIndex = c.getColumnIndexOrThrow(
1702 LauncherSettings.Favorites.SCREEN);
1703 final int cellXIndex = c.getColumnIndexOrThrow
1704 (LauncherSettings.Favorites.CELLX);
1705 final int cellYIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.CELLY);
1707 final int spanXIndex = c.getColumnIndexOrThrow
1708 (LauncherSettings.Favorites.SPANX);
1709 final int spanYIndex = c.getColumnIndexOrThrow(
1710 LauncherSettings.Favorites.SPANY);
1711 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1712 //final int displayModeIndex = c.getColumnIndexOrThrow(
1713 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001715 ShortcutInfo info;
1716 String intentDescription;
1717 LauncherAppWidgetInfo appWidgetInfo;
1718 int container;
1719 long id;
1720 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001721
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001722 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001723 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 try {
1725 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001726
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 switch (itemType) {
1728 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1729 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001730 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 intentDescription = c.getString(intentIndex);
1732 try {
1733 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001734 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001735 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001736 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001737 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001738 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1739 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001740 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001741 // If apps can be on external storage, then we just
1742 // leave them for the user to remove (maybe add
1743 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001744 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001745 }
1746 continue;
1747 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001749 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 continue;
1751 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1754 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1755 titleIndex, mLabelCache);
1756 } else {
1757 info = getShortcutInfo(c, context, iconTypeIndex,
1758 iconPackageIndex, iconResourceIndex, iconIndex,
1759 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001760
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 // App shortcuts that used to be automatically added to Launcher
1762 // didn't always have the correct intent flags set, so do that
1763 // here
1764 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001765 intent.getCategories() != null &&
1766 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001767 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 intent.addFlags(
1769 Intent.FLAG_ACTIVITY_NEW_TASK |
1770 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1771 }
Michael Jurka96879562012-03-22 05:54:33 -07001772 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001775 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 container = c.getInt(containerIndex);
1778 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 info.cellX = c.getInt(cellXIndex);
1781 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001782 info.spanX = 1;
1783 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001784
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001786 deleteOnInvalidPlacement.set(false);
1787 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1788 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001789 itemsToRemove.add(id);
1790 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 break;
1792 }
1793
1794 switch (container) {
1795 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1796 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1797 sBgWorkspaceItems.add(info);
1798 break;
1799 default:
1800 // Item is in a user folder
1801 FolderInfo folderInfo =
1802 findOrMakeFolder(sBgFolders, container);
1803 folderInfo.add(info);
1804 break;
1805 }
1806 sBgItemsIdMap.put(info.id, info);
1807
1808 // now that we've loaded everthing re-save it with the
1809 // icon in case it disappears somehow.
1810 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001811 } else {
1812 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001813 }
1814 break;
1815
1816 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1817 id = c.getLong(idIndex);
1818 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1819
1820 folderInfo.title = c.getString(titleIndex);
1821 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 folderInfo.cellX = c.getInt(cellXIndex);
1826 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001827 folderInfo.spanX = 1;
1828 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829
Daniel Sandler8802e962010-05-26 16:28:16 -04001830 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001831 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001832 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001833 deleteOnInvalidPlacement)) {
1834 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001835 itemsToRemove.add(id);
1836 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001837 break;
1838 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001839
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1842 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1843 sBgWorkspaceItems.add(folderInfo);
1844 break;
Joe Onorato36115782010-06-17 13:28:48 -04001845 }
Joe Onorato17a89222011-02-08 17:26:11 -08001846
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001847 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1848 sBgFolders.put(folderInfo.id, folderInfo);
1849 break;
1850
1851 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1852 // Read all Launcher-specific widget details
1853 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001854 String savedProvider = c.getString(appWidgetProviderIndex);
1855
Joe Onorato36115782010-06-17 13:28:48 -04001856 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001857
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858 final AppWidgetProviderInfo provider =
1859 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 if (!isSafeMode && (provider == null || provider.provider == null ||
1862 provider.provider.getPackageName() == null)) {
1863 String log = "Deleting widget that isn't installed anymore: id="
1864 + id + " appWidgetId=" + appWidgetId;
1865 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001866 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 itemsToRemove.add(id);
1868 } else {
1869 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1870 provider.provider);
1871 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001872 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 appWidgetInfo.cellX = c.getInt(cellXIndex);
1874 appWidgetInfo.cellY = c.getInt(cellYIndex);
1875 appWidgetInfo.spanX = c.getInt(spanXIndex);
1876 appWidgetInfo.spanY = c.getInt(spanYIndex);
1877 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1878 appWidgetInfo.minSpanX = minSpan[0];
1879 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001880
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001881 container = c.getInt(containerIndex);
1882 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1883 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1884 Log.e(TAG, "Widget found where container != " +
1885 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1886 continue;
1887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888
Adam Cohene25af792013-06-06 23:08:25 -07001889 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001891 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001892 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001893 deleteOnInvalidPlacement)) {
1894 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001895 itemsToRemove.add(id);
1896 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 break;
1898 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001899 String providerName = provider.provider.flattenToString();
1900 if (!providerName.equals(savedProvider)) {
1901 ContentValues values = new ContentValues();
1902 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1903 providerName);
1904 String where = BaseColumns._ID + "= ?";
1905 String[] args = {Integer.toString(c.getInt(idIndex))};
1906 contentResolver.update(contentUri, values, where, args);
1907 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1909 sBgAppWidgets.add(appWidgetInfo);
1910 }
Joe Onorato36115782010-06-17 13:28:48 -04001911 break;
1912 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05001914 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001915 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 }
1917 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001918 if (c != null) {
1919 c.close();
1920 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 }
1922
Winson Chungba9c37f2013-08-30 14:11:37 -07001923 // Break early if we've stopped loading
1924 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001925 clearSBgDataStructures();
1926 return false;
1927 }
1928
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 if (itemsToRemove.size() > 0) {
1930 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001931 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 // Remove dead items
1933 for (long id : itemsToRemove) {
1934 if (DEBUG_LOADERS) {
1935 Log.d(TAG, "Removed id = " + id);
1936 }
1937 // Don't notify content observers
1938 try {
1939 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1940 null, null);
1941 } catch (RemoteException e) {
1942 Log.w(TAG, "Could not remove id = " + id);
1943 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001944 }
1945 }
1946
Winson Chungc763c4e2013-07-19 13:49:06 -07001947 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 long maxScreenId = 0;
1949 // If we're importing we use the old screen order.
1950 for (ItemInfo item: sBgItemsIdMap.values()) {
1951 long screenId = item.screenId;
1952 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1953 !sBgWorkspaceScreens.contains(screenId)) {
1954 sBgWorkspaceScreens.add(screenId);
1955 if (screenId > maxScreenId) {
1956 maxScreenId = screenId;
1957 }
1958 }
1959 }
1960 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001961 // Log to disk
1962 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1963 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1964 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001965
Michael Jurka414300a2013-08-27 15:42:35 +02001966 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001968
1969 // Update the max item id after we load an old db
1970 long maxItemId = 0;
1971 // If we're importing we use the old screen order.
1972 for (ItemInfo item: sBgItemsIdMap.values()) {
1973 maxItemId = Math.max(maxItemId, item.id);
1974 }
Michael Jurka414300a2013-08-27 15:42:35 +02001975 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001977 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1978 for (Integer i : orderedScreens.keySet()) {
1979 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001981 // Log to disk
1982 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1983 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001984
1985 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001986 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 for (ItemInfo item: sBgItemsIdMap.values()) {
1988 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001989 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1990 unusedScreens.contains(screenId)) {
1991 unusedScreens.remove(screenId);
1992 }
1993 }
1994
1995 // If there are any empty screens remove them, and update.
1996 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001997 // Log to disk
1998 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
1999 TextUtils.join(", ", unusedScreens), true);
2000
Winson Chung933bae62013-08-29 11:42:30 -07002001 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2003 }
2004 }
2005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 if (DEBUG_LOADERS) {
2007 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2008 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002009 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002010 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002012
Daniel Sandler566da102013-06-25 23:43:45 -04002013 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002014 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002016 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 line += " | ";
2018 }
Winson Chung892c74d2013-08-22 16:15:50 -07002019 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 }
Joe Onorato36115782010-06-17 13:28:48 -04002022 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025 }
Joe Onorato36115782010-06-17 13:28:48 -04002026 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002027 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002028 }
2029
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 /** Filters the set of items who are directly or indirectly (via another container) on the
2031 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002032 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 ArrayList<ItemInfo> allWorkspaceItems,
2034 ArrayList<ItemInfo> currentScreenItems,
2035 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002036 // Purge any null ItemInfos
2037 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2038 while (iter.hasNext()) {
2039 ItemInfo i = iter.next();
2040 if (i == null) {
2041 iter.remove();
2042 }
2043 }
2044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 // Order the set of items by their containers first, this allows use to walk through the
2046 // list sequentially, build up a list of containers that are in the specified screen,
2047 // as well as all items in those containers.
2048 Set<Long> itemsOnScreen = new HashSet<Long>();
2049 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2050 @Override
2051 public int compare(ItemInfo lhs, ItemInfo rhs) {
2052 return (int) (lhs.container - rhs.container);
2053 }
2054 });
2055 for (ItemInfo info : allWorkspaceItems) {
2056 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002057 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 currentScreenItems.add(info);
2059 itemsOnScreen.add(info.id);
2060 } else {
2061 otherScreenItems.add(info);
2062 }
2063 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2064 currentScreenItems.add(info);
2065 itemsOnScreen.add(info.id);
2066 } else {
2067 if (itemsOnScreen.contains(info.container)) {
2068 currentScreenItems.add(info);
2069 itemsOnScreen.add(info.id);
2070 } else {
2071 otherScreenItems.add(info);
2072 }
2073 }
2074 }
2075 }
2076
2077 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002078 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 ArrayList<LauncherAppWidgetInfo> appWidgets,
2080 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2081 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082
2083 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002084 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002086 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 currentScreenWidgets.add(widget);
2088 } else {
2089 otherScreenWidgets.add(widget);
2090 }
2091 }
2092 }
2093
2094 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002095 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 HashMap<Long, ItemInfo> itemsIdMap,
2097 HashMap<Long, FolderInfo> folders,
2098 HashMap<Long, FolderInfo> currentScreenFolders,
2099 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100
2101 for (long id : folders.keySet()) {
2102 ItemInfo info = itemsIdMap.get(id);
2103 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002104 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002106 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 currentScreenFolders.put(id, folder);
2108 } else {
2109 otherScreenFolders.put(id, folder);
2110 }
2111 }
2112 }
2113
2114 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2115 * right) */
2116 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002117 final LauncherAppState app = LauncherAppState.getInstance();
2118 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 // XXX: review this
2120 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002121 @Override
2122 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002123 int cellCountX = (int) grid.numColumns;
2124 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002125 int screenOffset = cellCountX * cellCountY;
2126 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002127 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002128 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002129 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002130 rhs.cellY * cellCountX + rhs.cellX);
2131 return (int) (lr - rr);
2132 }
2133 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002135
Adam Cohendcd297f2013-06-18 13:13:40 -07002136 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2137 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002138 final Runnable r = new Runnable() {
2139 @Override
2140 public void run() {
2141 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2142 if (callbacks != null) {
2143 callbacks.bindScreens(orderedScreens);
2144 }
2145 }
2146 };
2147 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2148 }
2149
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2151 final ArrayList<ItemInfo> workspaceItems,
2152 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2153 final HashMap<Long, FolderInfo> folders,
2154 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002155
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 final boolean postOnMainThread = (deferredBindRunnables != null);
2157
2158 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002159 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002161 final int start = i;
2162 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 final Runnable r = new Runnable() {
2164 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002166 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002168 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2169 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 }
2171 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 };
2173 if (postOnMainThread) {
2174 deferredBindRunnables.add(r);
2175 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002176 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 }
Joe Onorato36115782010-06-17 13:28:48 -04002178 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179
2180 // Bind the folders
2181 if (!folders.isEmpty()) {
2182 final Runnable r = new Runnable() {
2183 public void run() {
2184 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2185 if (callbacks != null) {
2186 callbacks.bindFolders(folders);
2187 }
2188 }
2189 };
2190 if (postOnMainThread) {
2191 deferredBindRunnables.add(r);
2192 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002193 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 }
2195 }
2196
2197 // Bind the widgets, one at a time
2198 N = appWidgets.size();
2199 for (int i = 0; i < N; i++) {
2200 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2201 final Runnable r = new Runnable() {
2202 public void run() {
2203 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2204 if (callbacks != null) {
2205 callbacks.bindAppWidget(widget);
2206 }
2207 }
2208 };
2209 if (postOnMainThread) {
2210 deferredBindRunnables.add(r);
2211 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002212 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 }
2214 }
2215 }
2216
2217 /**
2218 * Binds all loaded data to actual views on the main thread.
2219 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002220 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 final long t = SystemClock.uptimeMillis();
2222 Runnable r;
2223
2224 // Don't use these two variables in any of the callback runnables.
2225 // Otherwise we hold a reference to them.
2226 final Callbacks oldCallbacks = mCallbacks.get();
2227 if (oldCallbacks == null) {
2228 // This launcher has exited and nobody bothered to tell us. Just bail.
2229 Log.w(TAG, "LoaderTask running with no launcher");
2230 return;
2231 }
2232
Winson Chung9b9fb962013-11-15 15:39:34 -08002233 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2235 ArrayList<LauncherAppWidgetInfo> appWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2238 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002240 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 workspaceItems.addAll(sBgWorkspaceItems);
2242 appWidgets.addAll(sBgAppWidgets);
2243 folders.putAll(sBgFolders);
2244 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 }
2247
Derek Prothro7aff3992013-12-10 14:00:37 -05002248 final boolean isLoadingSynchronously =
2249 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002250 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002251 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002252 if (currScreen >= orderedScreenIds.size()) {
2253 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002254 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002255 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002256 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002257 final long currentScreenId = currentScreen < 0
2258 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002259
2260 // Load all the items that are on the current page first (and in the process, unbind
2261 // all the existing workspace items before we call startBinding() below.
2262 unbindWorkspaceItemsOnMainThread();
2263
2264 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2266 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2267 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2268 new ArrayList<LauncherAppWidgetInfo>();
2269 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2270 new ArrayList<LauncherAppWidgetInfo>();
2271 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2272 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2273
Winson Chung9b9fb962013-11-15 15:39:34 -08002274 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002276 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002278 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279 otherFolders);
2280 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2281 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2282
2283 // Tell the workspace that we're about to start binding items
2284 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002285 public void run() {
2286 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2287 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002289 }
2290 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291 };
Winson Chung81b52252012-08-27 15:34:29 -07002292 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293
Adam Cohendcd297f2013-06-18 13:13:40 -07002294 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2295
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 // Load items on the current page
2297 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2298 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002299 if (isLoadingSynchronously) {
2300 r = new Runnable() {
2301 public void run() {
2302 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002303 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002304 callbacks.onPageBoundSynchronously(currentScreen);
2305 }
2306 }
2307 };
Winson Chung81b52252012-08-27 15:34:29 -07002308 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002309 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002310
Winson Chung4a2afa32012-07-19 14:53:05 -07002311 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2312 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 mDeferredBindRunnables.clear();
2314 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002315 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002316
2317 // Tell the workspace that we're done binding items
2318 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002319 public void run() {
2320 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2321 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002322 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002323 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324
Winson Chung98e030b2012-05-07 16:01:11 -07002325 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002326 if (DEBUG_LOADERS) {
2327 Log.d(TAG, "bound workspace in "
2328 + (SystemClock.uptimeMillis()-t) + "ms");
2329 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002330
2331 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002332 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002334 if (isLoadingSynchronously) {
2335 mDeferredBindRunnables.add(r);
2336 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002337 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002338 }
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002340
Joe Onorato36115782010-06-17 13:28:48 -04002341 private void loadAndBindAllApps() {
2342 if (DEBUG_LOADERS) {
2343 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2344 }
2345 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002346 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002347 synchronized (LoaderTask.this) {
2348 if (mStopped) {
2349 return;
2350 }
2351 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002352 }
Joe Onorato36115782010-06-17 13:28:48 -04002353 } else {
2354 onlyBindAllApps();
2355 }
2356 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002357
Joe Onorato36115782010-06-17 13:28:48 -04002358 private void onlyBindAllApps() {
2359 final Callbacks oldCallbacks = mCallbacks.get();
2360 if (oldCallbacks == null) {
2361 // This launcher has exited and nobody bothered to tell us. Just bail.
2362 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2363 return;
2364 }
2365
2366 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002367 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002368 final ArrayList<AppInfo> list
2369 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002370 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002371 public void run() {
2372 final long t = SystemClock.uptimeMillis();
2373 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2374 if (callbacks != null) {
2375 callbacks.bindAllApplications(list);
2376 }
2377 if (DEBUG_LOADERS) {
2378 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2379 + (SystemClock.uptimeMillis()-t) + "ms");
2380 }
2381 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002382 };
2383 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002384 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002385 r.run();
2386 } else {
2387 mHandler.post(r);
2388 }
Joe Onorato36115782010-06-17 13:28:48 -04002389 }
2390
Winson Chung64359a52013-07-08 17:17:08 -07002391 private void loadAllApps() {
2392 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002393
Joe Onorato36115782010-06-17 13:28:48 -04002394 final Callbacks oldCallbacks = mCallbacks.get();
2395 if (oldCallbacks == null) {
2396 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002397 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002398 return;
2399 }
2400
Winson Chung64359a52013-07-08 17:17:08 -07002401 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002402 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2403 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2404
Winson Chung64359a52013-07-08 17:17:08 -07002405 // Clear the list of apps
2406 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002407
Winson Chung64359a52013-07-08 17:17:08 -07002408 // Query for the set of apps
2409 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2410 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2411 if (DEBUG_LOADERS) {
2412 Log.d(TAG, "queryIntentActivities took "
2413 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2414 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2415 }
2416 // Fail if we don't have any apps
2417 if (apps == null || apps.isEmpty()) {
2418 return;
2419 }
2420 // Sort the applications by name
2421 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2422 Collections.sort(apps,
2423 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2424 if (DEBUG_LOADERS) {
2425 Log.d(TAG, "sort took "
2426 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002427 }
2428
Winson Chung64359a52013-07-08 17:17:08 -07002429 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002430 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002431 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002432 // This builds the icon bitmaps.
2433 mBgAllAppsList.add(new AppInfo(packageManager, app,
2434 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002435 }
2436
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002437 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002438 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2439 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002440
2441 // Post callback on main thread
2442 mHandler.post(new Runnable() {
2443 public void run() {
2444 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002445 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002446 if (callbacks != null) {
2447 callbacks.bindAllApplications(added);
2448 if (DEBUG_LOADERS) {
2449 Log.d(TAG, "bound " + added.size() + " apps in "
2450 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2451 }
2452 } else {
2453 Log.i(TAG, "not binding apps: no Launcher activity");
2454 }
2455 }
2456 });
2457
Joe Onorato36115782010-06-17 13:28:48 -04002458 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002459 Log.d(TAG, "Icons processed in "
2460 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002461 }
2462 }
2463
2464 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002465 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002466 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2467 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2468 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2469 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2470 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2471 }
Joe Onorato36115782010-06-17 13:28:48 -04002472 }
2473 }
2474
2475 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002476 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002477 }
2478
2479 private class PackageUpdatedTask implements Runnable {
2480 int mOp;
2481 String[] mPackages;
2482
2483 public static final int OP_NONE = 0;
2484 public static final int OP_ADD = 1;
2485 public static final int OP_UPDATE = 2;
2486 public static final int OP_REMOVE = 3; // uninstlled
2487 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2488
2489
2490 public PackageUpdatedTask(int op, String[] packages) {
2491 mOp = op;
2492 mPackages = packages;
2493 }
2494
2495 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002496 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002497
2498 final String[] packages = mPackages;
2499 final int N = packages.length;
2500 switch (mOp) {
2501 case OP_ADD:
2502 for (int i=0; i<N; i++) {
2503 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002504 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002505 }
2506 break;
2507 case OP_UPDATE:
2508 for (int i=0; i<N; i++) {
2509 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002510 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002511 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002512 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002513 }
2514 break;
2515 case OP_REMOVE:
2516 case OP_UNAVAILABLE:
2517 for (int i=0; i<N; i++) {
2518 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002519 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002520 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002521 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002522 }
2523 break;
2524 }
2525
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002526 ArrayList<AppInfo> added = null;
2527 ArrayList<AppInfo> modified = null;
2528 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002529
Adam Cohen487f7dd2012-06-28 18:12:10 -07002530 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002531 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002532 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002533 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002534 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002535 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002536 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002537 }
Winson Chung5d55f332012-07-16 20:45:03 -07002538 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002539 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002540 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002541 }
2542
Joe Onorato36115782010-06-17 13:28:48 -04002543 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2544 if (callbacks == null) {
2545 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2546 return;
2547 }
2548
2549 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002550 // Ensure that we add all the workspace applications to the db
2551 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002552 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2553 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2554 } else {
2555 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2556 addAndBindAddedApps(context, addedInfos, cb, added);
2557 }
Joe Onorato36115782010-06-17 13:28:48 -04002558 }
2559 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002560 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002561
2562 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002563 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002564 ArrayList<ItemInfo> infos =
2565 getItemInfoForComponentName(a.componentName);
2566 for (ItemInfo i : infos) {
2567 if (isShortcutInfoUpdateable(i)) {
2568 ShortcutInfo info = (ShortcutInfo) i;
2569 info.title = a.title.toString();
2570 updateItemInDatabase(context, info);
2571 }
2572 }
2573 }
2574
Joe Onorato36115782010-06-17 13:28:48 -04002575 mHandler.post(new Runnable() {
2576 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002577 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2578 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002579 callbacks.bindAppsUpdated(modifiedFinal);
2580 }
2581 }
2582 });
2583 }
Winson Chung83892cc2013-05-01 16:53:33 -07002584
Winson Chungdf95eb12013-10-16 14:57:07 -07002585 final ArrayList<String> removedPackageNames =
2586 new ArrayList<String>();
2587 if (mOp == OP_REMOVE) {
2588 // Mark all packages in the broadcast to be removed
2589 removedPackageNames.addAll(Arrays.asList(packages));
2590 } else if (mOp == OP_UPDATE) {
2591 // Mark disabled packages in the broadcast to be removed
2592 final PackageManager pm = context.getPackageManager();
2593 for (int i=0; i<N; i++) {
2594 if (isPackageDisabled(pm, packages[i])) {
2595 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002596 }
2597 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002598 }
2599 // Remove all the components associated with this package
2600 for (String pn : removedPackageNames) {
2601 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2602 for (ItemInfo i : infos) {
2603 deleteItemFromDatabase(context, i);
2604 }
2605 }
2606 // Remove all the specific components
2607 for (AppInfo a : removedApps) {
2608 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2609 for (ItemInfo i : infos) {
2610 deleteItemFromDatabase(context, i);
2611 }
2612 }
2613 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2614 // Remove any queued items from the install queue
2615 String spKey = LauncherAppState.getSharedPreferencesKey();
2616 SharedPreferences sp =
2617 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2618 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2619 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002620 mHandler.post(new Runnable() {
2621 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002622 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2623 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002624 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002625 }
2626 }
2627 });
Joe Onoratobe386092009-11-17 17:32:16 -08002628 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002629
Michael Jurkac402cd92013-05-20 15:49:32 +02002630 final ArrayList<Object> widgetsAndShortcuts =
2631 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002632 mHandler.post(new Runnable() {
2633 @Override
2634 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002635 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2636 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002637 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002638 }
2639 }
2640 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002641
2642 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002643 mHandler.post(new Runnable() {
2644 public void run() {
2645 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2646 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002647 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002648 }
2649 }
2650 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651 }
2652 }
2653
Michael Jurkac402cd92013-05-20 15:49:32 +02002654 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2655 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2656 PackageManager packageManager = context.getPackageManager();
2657 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2658 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2659 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2660 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2661 Collections.sort(widgetsAndShortcuts,
2662 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2663 return widgetsAndShortcuts;
2664 }
2665
Winson Chungdf95eb12013-10-16 14:57:07 -07002666 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2667 try {
2668 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2669 return !pi.applicationInfo.enabled;
2670 } catch (NameNotFoundException e) {
2671 // Fall through
2672 }
2673 return false;
2674 }
Winson Chung1323b482013-08-05 12:41:55 -07002675 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002676 if (cn == null) {
2677 return false;
2678 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002679 if (isPackageDisabled(pm, cn.getPackageName())) {
2680 return false;
2681 }
Winson Chungee055712013-07-30 14:46:24 -07002682
2683 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002684 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002685 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002686 return (pm.getActivityInfo(cn, 0) != null);
2687 } catch (NameNotFoundException e) {
2688 return false;
2689 }
2690 }
2691
Joe Onorato9c1289c2009-08-17 11:03:03 -04002692 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002693 * This is called from the code that adds shortcuts from the intent receiver. This
2694 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002695 */
Joe Onorato56d82912010-03-07 14:32:10 -05002696 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002697 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699
Joe Onorato56d82912010-03-07 14:32:10 -05002700 /**
2701 * Make an ShortcutInfo object for a shortcut that is an application.
2702 *
2703 * If c is not null, then it will be used to fill in missing data like the title and icon.
2704 */
2705 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002706 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002707 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002708 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002709 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002710 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002711 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002712 } else {
2713 try {
2714 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2715 info.initFlagsAndFirstInstallTime(pi);
2716 } catch (NameNotFoundException e) {
2717 Log.d(TAG, "getPackInfo failed for package " +
2718 componentName.getPackageName());
2719 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002720 }
2721
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002722 // TODO: See if the PackageManager knows about this case. If it doesn't
2723 // then return null & delete this.
2724
Joe Onorato56d82912010-03-07 14:32:10 -05002725 // the resource -- This may implicitly give us back the fallback icon,
2726 // but don't worry about that. All we're doing with usingFallbackIcon is
2727 // to avoid saving lots of copies of that in the database, and most apps
2728 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002729
2730 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2731 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2732 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002733 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002734 ResolveInfo resolveInfo = null;
2735 ComponentName oldComponent = intent.getComponent();
2736 Intent newIntent = new Intent(intent.getAction(), null);
2737 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2738 newIntent.setPackage(oldComponent.getPackageName());
2739 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2740 for (ResolveInfo i : infos) {
2741 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2742 i.activityInfo.name);
2743 if (cn.equals(oldComponent)) {
2744 resolveInfo = i;
2745 }
2746 }
2747 if (resolveInfo == null) {
2748 resolveInfo = manager.resolveActivity(intent, 0);
2749 }
Joe Onorato56d82912010-03-07 14:32:10 -05002750 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002751 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002752 }
Joe Onorato56d82912010-03-07 14:32:10 -05002753 // the db
2754 if (icon == null) {
2755 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002756 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002757 }
2758 }
2759 // the fallback icon
2760 if (icon == null) {
2761 icon = getFallbackIcon();
2762 info.usingFallbackIcon = true;
2763 }
2764 info.setIcon(icon);
2765
2766 // from the resource
2767 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002768 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2769 if (labelCache != null && labelCache.containsKey(key)) {
2770 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002771 } else {
2772 info.title = resolveInfo.activityInfo.loadLabel(manager);
2773 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002774 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002775 }
2776 }
Joe Onorato56d82912010-03-07 14:32:10 -05002777 }
2778 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002779 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002780 if (c != null) {
2781 info.title = c.getString(titleIndex);
2782 }
2783 }
2784 // fall back to the class name of the activity
2785 if (info.title == null) {
2786 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2789 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002791
Winson Chung64359a52013-07-08 17:17:08 -07002792 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2793 ItemInfoFilter f) {
2794 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2795 for (ItemInfo i : infos) {
2796 if (i instanceof ShortcutInfo) {
2797 ShortcutInfo info = (ShortcutInfo) i;
2798 ComponentName cn = info.intent.getComponent();
2799 if (cn != null && f.filterItem(null, info, cn)) {
2800 filtered.add(info);
2801 }
2802 } else if (i instanceof FolderInfo) {
2803 FolderInfo info = (FolderInfo) i;
2804 for (ShortcutInfo s : info.contents) {
2805 ComponentName cn = s.intent.getComponent();
2806 if (cn != null && f.filterItem(info, s, cn)) {
2807 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002808 }
2809 }
Winson Chung64359a52013-07-08 17:17:08 -07002810 } else if (i instanceof LauncherAppWidgetInfo) {
2811 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2812 ComponentName cn = info.providerName;
2813 if (cn != null && f.filterItem(null, info, cn)) {
2814 filtered.add(info);
2815 }
Winson Chung8a435102012-08-30 17:16:53 -07002816 }
2817 }
Winson Chung64359a52013-07-08 17:17:08 -07002818 return new ArrayList<ItemInfo>(filtered);
2819 }
2820
2821 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002822 ItemInfoFilter filter = new ItemInfoFilter() {
2823 @Override
2824 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2825 return cn.getPackageName().equals(pn);
2826 }
2827 };
2828 return filterItemInfos(sBgItemsIdMap.values(), filter);
2829 }
2830
2831 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
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.equals(cname);
2836 }
2837 };
2838 return filterItemInfos(sBgItemsIdMap.values(), filter);
2839 }
2840
2841 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2842 if (i instanceof ShortcutInfo) {
2843 ShortcutInfo info = (ShortcutInfo) i;
2844 // We need to check for ACTION_MAIN otherwise getComponent() might
2845 // return null for some shortcuts (for instance, for shortcuts to
2846 // web pages.)
2847 Intent intent = info.intent;
2848 ComponentName name = intent.getComponent();
2849 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2850 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2851 return true;
2852 }
2853 }
2854 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002855 }
2856
2857 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002858 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002860 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002861 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2862 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863
Joe Onorato56d82912010-03-07 14:32:10 -05002864 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002865 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002868 // TODO: If there's an explicit component and we can't install that, delete it.
2869
Joe Onorato56d82912010-03-07 14:32:10 -05002870 info.title = c.getString(titleIndex);
2871
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 int iconType = c.getInt(iconTypeIndex);
2873 switch (iconType) {
2874 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2875 String packageName = c.getString(iconPackageIndex);
2876 String resourceName = c.getString(iconResourceIndex);
2877 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002878 info.customIcon = false;
2879 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002882 if (resources != null) {
2883 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002884 icon = Utilities.createIconBitmap(
2885 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002888 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 }
Joe Onorato56d82912010-03-07 14:32:10 -05002890 // the db
2891 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002892 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002893 }
2894 // the fallback icon
2895 if (icon == null) {
2896 icon = getFallbackIcon();
2897 info.usingFallbackIcon = true;
2898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 break;
2900 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002901 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002902 if (icon == null) {
2903 icon = getFallbackIcon();
2904 info.customIcon = false;
2905 info.usingFallbackIcon = true;
2906 } else {
2907 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 break;
2910 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002911 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002912 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 info.customIcon = false;
2914 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002916 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 return info;
2918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919
Michael Jurka931dc972011-08-05 15:08:15 -07002920 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002921 @SuppressWarnings("all") // suppress dead code warning
2922 final boolean debug = false;
2923 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002924 Log.d(TAG, "getIconFromCursor app="
2925 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2926 }
2927 byte[] data = c.getBlob(iconIndex);
2928 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002929 return Utilities.createIconBitmap(
2930 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002931 } catch (Exception e) {
2932 return null;
2933 }
2934 }
2935
Winson Chung3d503fb2011-07-13 17:25:49 -07002936 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2937 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002938 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002939 if (info == null) {
2940 return null;
2941 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002942 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002943
2944 return info;
2945 }
2946
Winson Chunga9abd0e2010-10-27 17:18:37 -07002947 /**
Winson Chung55cef262010-10-28 14:14:18 -07002948 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2949 */
2950 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2951 ComponentName component) {
2952 List<AppWidgetProviderInfo> widgets =
2953 AppWidgetManager.getInstance(context).getInstalledProviders();
2954 for (AppWidgetProviderInfo info : widgets) {
2955 if (info.provider.equals(component)) {
2956 return info;
2957 }
2958 }
2959 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002960 }
2961
Winson Chung68846fd2010-10-29 11:00:27 -07002962 /**
2963 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2964 */
2965 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2966 final PackageManager packageManager = context.getPackageManager();
2967 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2968 new ArrayList<WidgetMimeTypeHandlerData>();
2969
2970 final Intent supportsIntent =
2971 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2972 supportsIntent.setType(mimeType);
2973
2974 // Create a set of widget configuration components that we can test against
2975 final List<AppWidgetProviderInfo> widgets =
2976 AppWidgetManager.getInstance(context).getInstalledProviders();
2977 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2978 new HashMap<ComponentName, AppWidgetProviderInfo>();
2979 for (AppWidgetProviderInfo info : widgets) {
2980 configurationComponentToWidget.put(info.configure, info);
2981 }
2982
2983 // Run through each of the intents that can handle this type of clip data, and cross
2984 // reference them with the components that are actual configuration components
2985 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2986 PackageManager.MATCH_DEFAULT_ONLY);
2987 for (ResolveInfo info : activities) {
2988 final ActivityInfo activityInfo = info.activityInfo;
2989 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2990 activityInfo.name);
2991 if (configurationComponentToWidget.containsKey(infoComponent)) {
2992 supportedConfigurationActivities.add(
2993 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2994 configurationComponentToWidget.get(infoComponent)));
2995 }
2996 }
2997 return supportedConfigurationActivities;
2998 }
2999
Winson Chunga9abd0e2010-10-27 17:18:37 -07003000 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003001 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3002 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3003 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3004
Adam Cohend9198822011-11-22 16:42:47 -08003005 if (intent == null) {
3006 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3007 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3008 return null;
3009 }
3010
Joe Onorato0589f0f2010-02-08 13:44:00 -08003011 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003012 boolean customIcon = false;
3013 ShortcutIconResource iconResource = null;
3014
3015 if (bitmap != null && bitmap instanceof Bitmap) {
3016 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003017 customIcon = true;
3018 } else {
3019 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3020 if (extra != null && extra instanceof ShortcutIconResource) {
3021 try {
3022 iconResource = (ShortcutIconResource) extra;
3023 final PackageManager packageManager = context.getPackageManager();
3024 Resources resources = packageManager.getResourcesForApplication(
3025 iconResource.packageName);
3026 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003027 icon = Utilities.createIconBitmap(
3028 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003029 } catch (Exception e) {
3030 Log.w(TAG, "Could not load shortcut icon: " + extra);
3031 }
3032 }
3033 }
3034
Michael Jurkac9d95c52011-08-29 14:03:34 -07003035 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003036
3037 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003038 if (fallbackIcon != null) {
3039 icon = fallbackIcon;
3040 } else {
3041 icon = getFallbackIcon();
3042 info.usingFallbackIcon = true;
3043 }
Joe Onorato56d82912010-03-07 14:32:10 -05003044 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003045 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003046
Joe Onorato0589f0f2010-02-08 13:44:00 -08003047 info.title = name;
3048 info.intent = intent;
3049 info.customIcon = customIcon;
3050 info.iconResource = iconResource;
3051
3052 return info;
3053 }
3054
Winson Chungaac01e12011-08-17 10:37:13 -07003055 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3056 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003057 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003058 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003059 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003060 }
Joe Onorato56d82912010-03-07 14:32:10 -05003061 // If this icon doesn't have a custom icon, check to see
3062 // what's stored in the DB, and if it doesn't match what
3063 // we're going to show, store what we are going to show back
3064 // into the DB. We do this so when we're loading, if the
3065 // package manager can't find an icon (for example because
3066 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003067 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003068 cache.put(info, c.getBlob(iconIndex));
3069 return true;
3070 }
3071 return false;
3072 }
3073 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3074 boolean needSave = false;
3075 try {
3076 if (data != null) {
3077 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3078 Bitmap loaded = info.getIcon(mIconCache);
3079 needSave = !saved.sameAs(loaded);
3080 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003081 needSave = true;
3082 }
Winson Chungaac01e12011-08-17 10:37:13 -07003083 } catch (Exception e) {
3084 needSave = true;
3085 }
3086 if (needSave) {
3087 Log.d(TAG, "going to save icon bitmap for info=" + info);
3088 // This is slower than is ideal, but this only happens once
3089 // or when the app is updated with a new icon.
3090 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003091 }
3092 }
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003095 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 * or make a new one.
3097 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003098 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 // See if a placeholder was created for us already
3100 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003101 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003103 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 folders.put(id, folderInfo);
3105 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003106 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 }
3108
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003109 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003110 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003111 return new Comparator<AppInfo>() {
3112 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003113 int result = collator.compare(a.title.toString().trim(),
3114 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003115 if (result == 0) {
3116 result = a.componentName.compareTo(b.componentName);
3117 }
3118 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003119 }
Winson Chung11904872012-09-17 16:58:46 -07003120 };
3121 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003122 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3123 = new Comparator<AppInfo>() {
3124 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003125 if (a.firstInstallTime < b.firstInstallTime) return 1;
3126 if (a.firstInstallTime > b.firstInstallTime) return -1;
3127 return 0;
3128 }
3129 };
Winson Chung11904872012-09-17 16:58:46 -07003130 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3131 final Collator collator = Collator.getInstance();
3132 return new Comparator<AppWidgetProviderInfo>() {
3133 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003134 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003135 }
3136 };
3137 }
Winson Chung5308f242011-08-18 12:12:41 -07003138 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3139 if (info.activityInfo != null) {
3140 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3141 } else {
3142 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3143 }
3144 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003145 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003146 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003147 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003148 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003149 ShortcutNameComparator(PackageManager pm) {
3150 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003151 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003152 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003153 }
3154 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3155 mPackageManager = pm;
3156 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003157 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003158 }
3159 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003160 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003161 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3162 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3163 if (mLabelCache.containsKey(keyA)) {
3164 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003165 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003166 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003167
Winson Chung5308f242011-08-18 12:12:41 -07003168 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003169 }
Winson Chung5308f242011-08-18 12:12:41 -07003170 if (mLabelCache.containsKey(keyB)) {
3171 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003172 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003173 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003174
Winson Chung5308f242011-08-18 12:12:41 -07003175 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003176 }
Winson Chung11904872012-09-17 16:58:46 -07003177 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003178 }
3179 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003180 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003181 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003182 private PackageManager mPackageManager;
3183 private HashMap<Object, String> mLabelCache;
3184 WidgetAndShortcutNameComparator(PackageManager pm) {
3185 mPackageManager = pm;
3186 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003187 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003188 }
3189 public final int compare(Object a, Object b) {
3190 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003191 if (mLabelCache.containsKey(a)) {
3192 labelA = mLabelCache.get(a);
3193 } else {
3194 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003195 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003196 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003197 mLabelCache.put(a, labelA);
3198 }
3199 if (mLabelCache.containsKey(b)) {
3200 labelB = mLabelCache.get(b);
3201 } else {
3202 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003203 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003204 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003205 mLabelCache.put(b, labelB);
3206 }
Winson Chung11904872012-09-17 16:58:46 -07003207 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003208 }
3209 };
Joe Onoratobe386092009-11-17 17:32:16 -08003210
3211 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003212 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003213 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3214 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3215 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3216 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003217 if (mLoaderTask != null) {
3218 mLoaderTask.dumpState();
3219 } else {
3220 Log.d(TAG, "mLoaderTask=null");
3221 }
Joe Onoratobe386092009-11-17 17:32:16 -08003222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223}