blob: b4f7f5dbd70fa892eee6c5ee7def41686f12fa36 [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;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Joe Onorato36115782010-06-17 13:28:48 -040078 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050079 private static final long INVALID_SCREEN_ID = -1L;
Winson Chungee055712013-07-30 14:46:24 -070080 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040081
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 private final Object mLock = new Object();
84 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040085 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070086 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020087 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Winson Chung81b52252012-08-27 15:34:29 -070089 // Specific runnable types that are run on the main thread deferred handler, this allows us to
90 // clear all queued binding runnables when the Launcher activity is destroyed.
91 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
92 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
93
94
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070095 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
96 static {
97 sWorkerThread.start();
98 }
99 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
100
Joe Onoratocc67f472010-06-08 10:54:30 -0700101 // We start off with everything not loaded. After that, we assume that
102 // our monitoring of the package manager provides all updates and we never
103 // need to do a requery. These are only ever touched from the loader thread.
104 private boolean mWorkspaceLoaded;
105 private boolean mAllAppsLoaded;
106
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700107 // When we are loading pages synchronously, we can't just post the binding of items on the side
108 // pages as this delays the rotation process. Instead, we wait for a callback from the first
109 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
110 // a normal load, we also clear this set of Runnables.
111 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
112
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700116 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800117
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118 // The lock that must be acquired before referencing any static bg data structures. Unlike
119 // other locks, this one can generally be held long-term because we never expect any of these
120 // static data structures to be referenced outside of the worker thread except on the first
121 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700122 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
129 // created by LauncherModel that are directly on the home screen (however, no widgets or
130 // shortcuts within folders).
131 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
134 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700135 new ArrayList<LauncherAppWidgetInfo>();
136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
138 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700139
Adam Cohen487f7dd2012-06-28 18:12:10 -0700140 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
141 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700142
143 // sBgWorkspaceScreens is the ordered set of workspace screens.
144 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
145
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146 // </ only access in worker thread >
147
148 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Reena Lee99a73f32011-10-24 17:27:37 -0700151 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700154 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 public int getCurrentWorkspaceScreen();
156 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700157 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
158 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700159 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700160 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500161 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700162 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200164 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700165 public void bindAppsAdded(ArrayList<Long> newScreens,
166 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700167 ArrayList<ItemInfo> addAnimated,
168 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200169 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700170 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700171 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200172 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100173 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700174 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700175 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700176 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung64359a52013-07-08 17:17:08 -0700179 public interface ItemInfoFilter {
180 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
181 }
182
Bjorn Bringert1307f632013-10-03 22:31:03 +0100183 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 final Context context = app.getContext();
185
Winson Chungee055712013-07-30 14:46:24 -0700186 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400187 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100188 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 mIconCache = iconCache;
190
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700192 Configuration config = res.getConfiguration();
193 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 }
195
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700196 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
197 * posted on the main thread handler. */
198 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700199 runOnMainThread(r, 0);
200 }
201 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700202 if (sWorkerThread.getThreadId() == Process.myTid()) {
203 // If we are on the worker thread, post onto the main handler
204 mHandler.post(r);
205 } else {
206 r.run();
207 }
208 }
209
210 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
211 * posted on the worker thread handler. */
212 private static void runOnWorkerThread(Runnable r) {
213 if (sWorkerThread.getThreadId() == Process.myTid()) {
214 r.run();
215 } else {
216 // If we are not on the worker thread, then post to the worker handler
217 sWorker.post(r);
218 }
219 }
220
Winson Chungc9168342013-06-26 14:54:55 -0700221 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
222 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700223 LauncherAppState app = LauncherAppState.getInstance();
224 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
225 final int xCount = (int) grid.numColumns;
226 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700227 boolean[][] occupied = new boolean[xCount][yCount];
228
229 int cellX, cellY, spanX, spanY;
230 for (int i = 0; i < items.size(); ++i) {
231 final ItemInfo item = items.get(i);
232 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
233 if (item.screenId == screen) {
234 cellX = item.cellX;
235 cellY = item.cellY;
236 spanX = item.spanX;
237 spanY = item.spanY;
238 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
239 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
240 occupied[x][y] = true;
241 }
242 }
243 }
244 }
245 }
246
247 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
248 }
249 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700250 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700251 int firstScreenIndex,
252 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700253 // Lock on the app so that we don't try and get the items while apps are being added
254 LauncherAppState app = LauncherAppState.getInstance();
255 LauncherModel model = app.getModel();
256 boolean found = false;
257 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700258 if (sWorkerThread.getThreadId() != Process.myTid()) {
259 // Flush the LauncherModel worker thread, so that if we just did another
260 // processInstallShortcut, we give it time for its shortcut to get added to the
261 // database (getItemsInLocalCoordinates reads the database)
262 model.flushWorkerThread();
263 }
Winson Chungc9168342013-06-26 14:54:55 -0700264 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700265
266 // Try adding to the workspace screens incrementally, starting at the default or center
267 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700268 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
269 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700270 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 int[] tmpCoordinates = new int[2];
272 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700273 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700274 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700275 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700276 }
277 }
278 }
Winson Chungc9168342013-06-26 14:54:55 -0700279 return null;
280 }
281
Winson Chung94d67682013-09-25 16:29:40 -0700282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
283 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700284 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700285 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700286 }
Winson Chung94d67682013-09-25 16:29:40 -0700287 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800288 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
289 if (workspaceApps == null || allAppsApps == null) {
290 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
291 }
Winson Chung94d67682013-09-25 16:29:40 -0700292 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700293 return;
Winson Chung997a9232013-07-24 15:33:46 -0700294 }
Winson Chung64359a52013-07-08 17:17:08 -0700295 // Process the newly added applications and add them to the database first
296 Runnable r = new Runnable() {
297 public void run() {
298 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
299 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
300
Winson Chung76828c82013-08-19 15:43:29 -0700301 // Get the list of workspace screens. We need to append to this list and
302 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
303 // called.
304 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
305 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
306 for (Integer i : orderedScreens.keySet()) {
307 long screenId = orderedScreens.get(i);
308 workspaceScreens.add(screenId);
309 }
310
Winson Chung64359a52013-07-08 17:17:08 -0700311 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700312 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700313 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700314 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700315 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700316 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700317
318 // Short-circuit this logic if the icon exists somewhere on the workspace
319 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
320 continue;
321 }
322
Winson Chung87412982013-10-03 18:34:14 -0700323 // Add this icon to the db, creating a new page if necessary. If there
324 // is only the empty page then we just add items to the first page.
325 // Otherwise, we add them to the next pages.
326 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700327 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700328 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700329 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200330 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700331
332 // If we can't find a valid position, then just add a new screen.
333 // This takes time so we need to re-queue the add until the new
334 // page is added. Create as many screens as necessary to satisfy
335 // the startSearchPageIndex.
336 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700337 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 while (numPagesToAdd > 0) {
339 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700341 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700342 addedWorkspaceScreensFinal.add(screenId);
343 numPagesToAdd--;
344 }
Winson Chung76828c82013-08-19 15:43:29 -0700345
Winson Chung64359a52013-07-08 17:17:08 -0700346 // Find the coordinate again
347 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700348 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700349 }
350 if (coords == null) {
351 throw new RuntimeException("Coordinates should not be null");
352 }
353
Winson Chung997a9232013-07-24 15:33:46 -0700354 ShortcutInfo shortcutInfo;
355 if (a instanceof ShortcutInfo) {
356 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200357 } else if (a instanceof AppInfo) {
358 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700359 } else {
360 throw new RuntimeException("Unexpected info type");
361 }
Winson Chung94d67682013-09-25 16:29:40 -0700362
Winson Chung64359a52013-07-08 17:17:08 -0700363 // Add the shortcut to the db
364 addItemToDatabase(context, shortcutInfo,
365 LauncherSettings.Favorites.CONTAINER_DESKTOP,
366 coords.first, coords.second[0], coords.second[1], false);
367 // Save the ShortcutInfo for binding in the workspace
368 addedShortcutsFinal.add(shortcutInfo);
369 }
370 }
371
Winson Chung76828c82013-08-19 15:43:29 -0700372 // Update the workspace screens
373 updateWorkspaceScreenOrder(context, workspaceScreens);
374
Winson Chung94d67682013-09-25 16:29:40 -0700375 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700376 runOnMainThread(new Runnable() {
377 public void run() {
378 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
379 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700380 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
381 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700382 if (!addedShortcutsFinal.isEmpty()) {
383 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
384 long lastScreenId = info.screenId;
385 for (ItemInfo i : addedShortcutsFinal) {
386 if (i.screenId == lastScreenId) {
387 addAnimated.add(i);
388 } else {
389 addNotAnimated.add(i);
390 }
Winson Chung997a9232013-07-24 15:33:46 -0700391 }
392 }
Winson Chungd64d1762013-08-20 14:37:16 -0700393 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700394 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700395 }
Winson Chung64359a52013-07-08 17:17:08 -0700396 }
Winson Chung997a9232013-07-24 15:33:46 -0700397 });
398 }
Winson Chung64359a52013-07-08 17:17:08 -0700399 }
400 };
401 runOnWorkerThread(r);
402 }
403
Joe Onorato56d82912010-03-07 14:32:10 -0500404 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700405 if (mDefaultIcon == null) {
406 final Context context = LauncherAppState.getInstance().getContext();
407 mDefaultIcon = Utilities.createIconBitmap(
408 mIconCache.getFullResDefaultActivityIcon(), context);
409 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800410 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Winson Chung81b52252012-08-27 15:34:29 -0700413 public void unbindItemInfosAndClearQueuedBindRunnables() {
414 if (sWorkerThread.getThreadId() == Process.myTid()) {
415 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
416 "main thread");
417 }
418
419 // Clear any deferred bind runnables
420 mDeferredBindRunnables.clear();
421 // Remove any queued bind runnables
422 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
423 // Unbind all the workspace items
424 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700425 }
426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700428 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700429 // Ensure that we don't use the same workspace items data structure on the main thread
430 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
432 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700433 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700434 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
435 tmpAppWidgets.addAll(sBgAppWidgets);
436 }
437 Runnable r = new Runnable() {
438 @Override
439 public void run() {
440 for (ItemInfo item : tmpWorkspaceItems) {
441 item.unbind();
442 }
443 for (ItemInfo item : tmpAppWidgets) {
444 item.unbind();
445 }
446 }
447 };
448 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700449 }
450
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 /**
452 * Adds an item to the DB if it was not created previously, or move it to a new
453 * <container, screen, cellX, cellY>
454 */
455 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700456 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 if (item.container == ItemInfo.NO_ID) {
458 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700459 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400460 } else {
461 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700462 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464 }
465
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700466 static void checkItemInfoLocked(
467 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
468 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
469 if (modelItem != null && item != modelItem) {
470 // check all the data is consistent
471 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
472 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
473 ShortcutInfo shortcut = (ShortcutInfo) item;
474 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
475 modelShortcut.intent.filterEquals(shortcut.intent) &&
476 modelShortcut.id == shortcut.id &&
477 modelShortcut.itemType == shortcut.itemType &&
478 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700479 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700480 modelShortcut.cellX == shortcut.cellX &&
481 modelShortcut.cellY == shortcut.cellY &&
482 modelShortcut.spanX == shortcut.spanX &&
483 modelShortcut.spanY == shortcut.spanY &&
484 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
485 (modelShortcut.dropPos != null &&
486 shortcut.dropPos != null &&
487 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
488 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
489 // For all intents and purposes, this is the same object
490 return;
491 }
492 }
493
494 // the modelItem needs to match up perfectly with item if our model is
495 // to be consistent with the database-- for now, just require
496 // modelItem == item or the equality check above
497 String msg = "item: " + ((item != null) ? item.toString() : "null") +
498 "modelItem: " +
499 ((modelItem != null) ? modelItem.toString() : "null") +
500 "Error: ItemInfo passed to checkItemInfo doesn't match original";
501 RuntimeException e = new RuntimeException(msg);
502 if (stackTrace != null) {
503 e.setStackTrace(stackTrace);
504 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800505 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700506 }
507 }
508
Michael Jurka816474f2012-06-25 14:49:02 -0700509 static void checkItemInfo(final ItemInfo item) {
510 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
511 final long itemId = item.id;
512 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700513 public void run() {
514 synchronized (sBgLock) {
515 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700516 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700517 }
518 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700519 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700520 }
521
Michael Jurkac9d95c52011-08-29 14:03:34 -0700522 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
523 final ItemInfo item, final String callingFunction) {
524 final long itemId = item.id;
525 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
526 final ContentResolver cr = context.getContentResolver();
527
Adam Cohen487f7dd2012-06-28 18:12:10 -0700528 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700529 Runnable r = new Runnable() {
530 public void run() {
531 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 updateItemArrays(item, itemId, stackTrace);
533 }
534 };
535 runOnWorkerThread(r);
536 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700537
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700538 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
539 final ArrayList<ItemInfo> items, final String callingFunction) {
540 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700541
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700542 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
543 Runnable r = new Runnable() {
544 public void run() {
545 ArrayList<ContentProviderOperation> ops =
546 new ArrayList<ContentProviderOperation>();
547 int count = items.size();
548 for (int i = 0; i < count; i++) {
549 ItemInfo item = items.get(i);
550 final long itemId = item.id;
551 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
552 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700554 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
555 updateItemArrays(item, itemId, stackTrace);
556
557 }
558 try {
559 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
560 } catch (Exception e) {
561 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700562 }
563 }
564 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700565 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700566 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700567
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700568 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
569 // Lock on mBgLock *after* the db operation
570 synchronized (sBgLock) {
571 checkItemInfoLocked(itemId, item, stackTrace);
572
573 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
574 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
575 // Item is in a folder, make sure this folder exists
576 if (!sBgFolders.containsKey(item.container)) {
577 // An items container is being set to a that of an item which is not in
578 // the list of Folders.
579 String msg = "item: " + item + " container being set to: " +
580 item.container + ", not in the list of folders";
581 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700582 }
583 }
584
585 // Items are added/removed from the corresponding FolderInfo elsewhere, such
586 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
587 // that are on the desktop, as appropriate
588 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800589 if (modelItem != null &&
590 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
591 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700592 switch (modelItem.itemType) {
593 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
594 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
595 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
596 if (!sBgWorkspaceItems.contains(modelItem)) {
597 sBgWorkspaceItems.add(modelItem);
598 }
599 break;
600 default:
601 break;
602 }
603 } else {
604 sBgWorkspaceItems.remove(modelItem);
605 }
606 }
607 }
608
Michael Jurkac7700af2013-05-14 20:17:58 +0200609 public void flushWorkerThread() {
610 mFlushingWorkerThread = true;
611 Runnable waiter = new Runnable() {
612 public void run() {
613 synchronized (this) {
614 notifyAll();
615 mFlushingWorkerThread = false;
616 }
617 }
618 };
619
620 synchronized(waiter) {
621 runOnWorkerThread(waiter);
622 if (mLoaderTask != null) {
623 synchronized(mLoaderTask) {
624 mLoaderTask.notify();
625 }
626 }
627 boolean success = false;
628 while (!success) {
629 try {
630 waiter.wait();
631 success = true;
632 } catch (InterruptedException e) {
633 }
634 }
635 }
636 }
637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700640 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700641 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 item.cellX = cellX;
645 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 // We store hotseat items in canonical form which is this orientation invariant position
648 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
656 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700658 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
659 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700663 }
664
665 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
667 * cellX, cellY have already been updated on the ItemInfos.
668 */
669 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
670 final long container, final int screen) {
671
672 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
673 int count = items.size();
674
675 for (int i = 0; i < count; i++) {
676 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 item.container = container;
678
679 // We store hotseat items in canonical form which is this orientation invariant position
680 // in the hotseat
681 if (context instanceof Launcher && screen < 0 &&
682 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 item.cellY);
685 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 }
688
689 final ContentValues values = new ContentValues();
690 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
691 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
692 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694
695 contentValues.add(values);
696 }
697 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
698 }
699
700 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800702 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700703 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700705 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800706 item.cellX = cellX;
707 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 item.spanX = spanX;
709 item.spanY = spanY;
710
711 // We store hotseat items in canonical form which is this orientation invariant position
712 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 }
Adam Cohend4844c32011-02-18 19:25:06 -0800719
Adam Cohend4844c32011-02-18 19:25:06 -0800720 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800721 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
723 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
724 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
725 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800727
Michael Jurka816474f2012-06-25 14:49:02 -0700728 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700729 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730
731 /**
732 * Update an item to the database in a specified container.
733 */
734 static void updateItemInDatabase(Context context, final ItemInfo item) {
735 final ContentValues values = new ContentValues();
736 item.onAddToDatabase(values);
737 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
738 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800739 }
740
741 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 * Returns true if the shortcuts already exists in the database.
743 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 static boolean shortcutExists(Context context, String title, Intent intent) {
746 final ContentResolver cr = context.getContentResolver();
747 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
748 new String[] { "title", "intent" }, "title=? and intent=?",
749 new String[] { title, intent.toUri(0) }, null);
750 boolean result = false;
751 try {
752 result = c.moveToFirst();
753 } finally {
754 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700757 }
758
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 * Returns an ItemInfo array containing all the items in the LauncherModel.
761 * The ItemInfo.id is not set through this function.
762 */
763 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
764 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
765 final ContentResolver cr = context.getContentResolver();
766 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
767 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
768 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
769 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
770
771 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
772 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
773 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
774 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
775 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
776 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
777 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
778
779 try {
780 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700782 item.cellX = c.getInt(cellXIndex);
783 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700784 item.spanX = Math.max(1, c.getInt(spanXIndex));
785 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 item.container = c.getInt(containerIndex);
787 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700789
790 items.add(item);
791 }
792 } catch (Exception e) {
793 items.clear();
794 } finally {
795 c.close();
796 }
797
798 return items;
799 }
800
801 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
803 */
804 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
805 final ContentResolver cr = context.getContentResolver();
806 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
807 "_id=? and (itemType=? or itemType=?)",
808 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700809 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700810
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 try {
812 if (c.moveToFirst()) {
813 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
814 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
815 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
816 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
817 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
818 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 FolderInfo folderInfo = null;
821 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700822 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
823 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700825 }
826
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 folderInfo.title = c.getString(titleIndex);
828 folderInfo.id = id;
829 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700831 folderInfo.cellX = c.getInt(cellXIndex);
832 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833
834 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700835 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 } finally {
837 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700838 }
839
840 return null;
841 }
842
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 /**
844 * Add an item to the database in a specified container. Sets the container, screen, cellX and
845 * cellY fields of the item. Also assigns an ID to the item.
846 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 item.cellX = cellX;
851 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 // We store hotseat items in canonical form which is this orientation invariant position
853 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860
861 final ContentValues values = new ContentValues();
862 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 item.onAddToDatabase(values);
864
Michael Jurka414300a2013-08-27 15:42:35 +0200865 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700866 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700868
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 public void run() {
871 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
872 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700874 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700875 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700876 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700877 sBgItemsIdMap.put(item.id, item);
878 switch (item.itemType) {
879 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
880 sBgFolders.put(item.id, (FolderInfo) item);
881 // Fall through
882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
884 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
885 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
886 sBgWorkspaceItems.add(item);
887 } else {
888 if (!sBgFolders.containsKey(item.container)) {
889 // Adding an item to a folder that doesn't exist.
890 String msg = "adding item: " + item + " to a folder that " +
891 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700892 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700894 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700895 break;
896 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
897 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
898 break;
899 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 }
901 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700902 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700904 }
905
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700907 * Creates a new unique child id, for a given cell span across all layouts.
908 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700909 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700913 }
914
Winson Chungaafa03c2010-06-11 17:34:16 -0700915 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700916 * Removes the specified item from the database
917 * @param context
918 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700922 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700923
Michael Jurka83df1882011-08-31 20:59:26 -0700924 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700926 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700927
928 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700929 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 switch (item.itemType) {
931 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
932 sBgFolders.remove(item.id);
933 for (ItemInfo info: sBgItemsIdMap.values()) {
934 if (info.container == item.id) {
935 // We are deleting a folder which still contains items that
936 // think they are contained by that folder.
937 String msg = "deleting a folder (" + item + ") which still " +
938 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700939 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700941 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942 sBgWorkspaceItems.remove(item);
943 break;
944 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
945 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
946 sBgWorkspaceItems.remove(item);
947 break;
948 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
949 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
950 break;
951 }
952 sBgItemsIdMap.remove(item.id);
953 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700954 }
955 }
Michael Jurka83df1882011-08-31 20:59:26 -0700956 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 }
959
960 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 * Update the order of the workspace screens in the database. The array list contains
962 * a list of screen ids in the order that they should appear.
963 */
Winson Chungc9168342013-06-26 14:54:55 -0700964 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800965 // Log to disk
966 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
967 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
968
Winson Chung64359a52013-07-08 17:17:08 -0700969 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700970 final ContentResolver cr = context.getContentResolver();
971 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
972
973 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700974 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 while (iter.hasNext()) {
976 long id = iter.next();
977 if (id < 0) {
978 iter.remove();
979 }
980 }
981
982 Runnable r = new Runnable() {
983 @Override
984 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 // Clear the table
986 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700987 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 ContentValues[] values = new ContentValues[count];
989 for (int i = 0; i < count; i++) {
990 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700991 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
993 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 values[i] = v;
995 }
996 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700997
Winson Chungba9c37f2013-08-30 14:11:37 -0700998 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700999 sBgWorkspaceScreens.clear();
1000 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001001 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 }
1003 };
1004 runOnWorkerThread(r);
1005 }
1006
1007 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 * Remove the contents of the specified folder from the database
1009 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 final ContentResolver cr = context.getContentResolver();
1012
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 Runnable r = new Runnable() {
1014 public void run() {
1015 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001017 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 sBgItemsIdMap.remove(info.id);
1019 sBgFolders.remove(info.id);
1020 sBgDbIconCache.remove(info);
1021 sBgWorkspaceItems.remove(info);
1022 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001023
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1025 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001027 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 for (ItemInfo childInfo : info.contents) {
1029 sBgItemsIdMap.remove(childInfo.id);
1030 sBgDbIconCache.remove(childInfo);
1031 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001032 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 }
1034 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 }
1037
1038 /**
1039 * Set this as the current Launcher activity object for the loader.
1040 */
1041 public void initialize(Callbacks callbacks) {
1042 synchronized (mLock) {
1043 mCallbacks = new WeakReference<Callbacks>(callbacks);
1044 }
1045 }
1046
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001047 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1049 * ACTION_PACKAGE_CHANGED.
1050 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001051 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001052 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001053 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001056
Joe Onorato36115782010-06-17 13:28:48 -04001057 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1058 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1059 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1060 final String packageName = intent.getData().getSchemeSpecificPart();
1061 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062
Joe Onorato36115782010-06-17 13:28:48 -04001063 int op = PackageUpdatedTask.OP_NONE;
1064
1065 if (packageName == null || packageName.length() == 0) {
1066 // they sent us a bad intent
1067 return;
1068 }
1069
1070 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1071 op = PackageUpdatedTask.OP_UPDATE;
1072 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1073 if (!replacing) {
1074 op = PackageUpdatedTask.OP_REMOVE;
1075 }
1076 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1077 // later, we will update the package at this time
1078 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1079 if (!replacing) {
1080 op = PackageUpdatedTask.OP_ADD;
1081 } else {
1082 op = PackageUpdatedTask.OP_UPDATE;
1083 }
1084 }
1085
1086 if (op != PackageUpdatedTask.OP_NONE) {
1087 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1088 }
1089
1090 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001091 // First, schedule to add these apps back in.
1092 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1093 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1094 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001095 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001096 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1097 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1098 enqueuePackageUpdated(new PackageUpdatedTask(
1099 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001100 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001101 // If we have changed locale we need to clear out the labels in all apps/workspace.
1102 forceReload();
1103 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1104 // Check if configuration change was an mcc/mnc change which would affect app resources
1105 // and we would need to clear out the labels in all apps/workspace. Same handling as
1106 // above for ACTION_LOCALE_CHANGED
1107 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001108 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001109 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001110 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001111 forceReload();
1112 }
1113 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001114 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001115 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1116 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001117 if (mCallbacks != null) {
1118 Callbacks callbacks = mCallbacks.get();
1119 if (callbacks != null) {
1120 callbacks.bindSearchablesChanged();
1121 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001122 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001123 }
1124 }
1125
Reena Lee93f824a2011-09-23 17:20:28 -07001126 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001127 resetLoadedState(true, true);
1128
Reena Lee93f824a2011-09-23 17:20:28 -07001129 // Do this here because if the launcher activity is running it will be restarted.
1130 // If it's not running startLoaderFromBackground will merely tell it that it needs
1131 // to reload.
1132 startLoaderFromBackground();
1133 }
1134
Winson Chungf0c6ae02012-03-21 16:10:31 -07001135 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1136 synchronized (mLock) {
1137 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1138 // mWorkspaceLoaded to true later
1139 stopLoaderLocked();
1140 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1141 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1142 }
1143 }
1144
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001145 /**
1146 * When the launcher is in the background, it's possible for it to miss paired
1147 * configuration changes. So whenever we trigger the loader from the background
1148 * tell the launcher that it needs to re-run the loader when it comes back instead
1149 * of doing it now.
1150 */
1151 public void startLoaderFromBackground() {
1152 boolean runLoader = false;
1153 if (mCallbacks != null) {
1154 Callbacks callbacks = mCallbacks.get();
1155 if (callbacks != null) {
1156 // Only actually run the loader if they're not paused.
1157 if (!callbacks.setLoadOnResume()) {
1158 runLoader = true;
1159 }
1160 }
1161 }
1162 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001163 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001164 }
Joe Onorato36115782010-06-17 13:28:48 -04001165 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001166
Reena Lee93f824a2011-09-23 17:20:28 -07001167 // If there is already a loader task running, tell it to stop.
1168 // returns true if isLaunching() was true on the old task
1169 private boolean stopLoaderLocked() {
1170 boolean isLaunching = false;
1171 LoaderTask oldTask = mLoaderTask;
1172 if (oldTask != null) {
1173 if (oldTask.isLaunching()) {
1174 isLaunching = true;
1175 }
1176 oldTask.stopLocked();
1177 }
1178 return isLaunching;
1179 }
1180
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001182 synchronized (mLock) {
1183 if (DEBUG_LOADERS) {
1184 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001187 // Clear any deferred bind-runnables from the synchronized load process
1188 // We must do this before any loading/binding is scheduled below.
1189 mDeferredBindRunnables.clear();
1190
Joe Onorato36115782010-06-17 13:28:48 -04001191 // Don't bother to start the thread if we know it's not going to do anything
1192 if (mCallbacks != null && mCallbacks.get() != null) {
1193 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001194 // also, don't downgrade isLaunching if we're already running
1195 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001196 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Derek Prothro7aff3992013-12-10 14:00:37 -05001197 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1198 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1200 } else {
1201 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1202 sWorker.post(mLoaderTask);
1203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 }
1206 }
1207
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001208 void bindRemainingSynchronousPages() {
1209 // Post the remaining side pages to be loaded
1210 if (!mDeferredBindRunnables.isEmpty()) {
1211 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001212 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 }
1214 mDeferredBindRunnables.clear();
1215 }
1216 }
1217
Joe Onorato36115782010-06-17 13:28:48 -04001218 public void stopLoader() {
1219 synchronized (mLock) {
1220 if (mLoaderTask != null) {
1221 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 }
1223 }
Joe Onorato36115782010-06-17 13:28:48 -04001224 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225
Winson Chung76828c82013-08-19 15:43:29 -07001226 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1227 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1228 final ContentResolver contentResolver = context.getContentResolver();
1229 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1230 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1231 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1232
1233 try {
1234 final int idIndex = sc.getColumnIndexOrThrow(
1235 LauncherSettings.WorkspaceScreens._ID);
1236 final int rankIndex = sc.getColumnIndexOrThrow(
1237 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1238 while (sc.moveToNext()) {
1239 try {
1240 long screenId = sc.getLong(idIndex);
1241 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001242 orderedScreens.put(rank, screenId);
1243 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001244 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001245 }
1246 }
1247 } finally {
1248 sc.close();
1249 }
Winson Chunga90303b2013-11-15 13:05:06 -08001250
1251 // Log to disk
1252 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1253 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1254 for (Integer i : orderedScreens.keySet()) {
1255 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1256 }
1257 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1258 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001259 return orderedScreens;
1260 }
1261
Michael Jurkac57b7a82011-08-09 22:02:20 -07001262 public boolean isAllAppsLoaded() {
1263 return mAllAppsLoaded;
1264 }
1265
Winson Chung36a62fe2012-05-06 18:04:42 -07001266 boolean isLoadingWorkspace() {
1267 synchronized (mLock) {
1268 if (mLoaderTask != null) {
1269 return mLoaderTask.isLoadingWorkspace();
1270 }
1271 }
1272 return false;
1273 }
1274
Joe Onorato36115782010-06-17 13:28:48 -04001275 /**
1276 * Runnable for the thread that loads the contents of the launcher:
1277 * - workspace icons
1278 * - widgets
1279 * - all apps icons
1280 */
1281 private class LoaderTask implements Runnable {
1282 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001283 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001284 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001285 private boolean mStopped;
1286 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001287
Winson Chungc3eecff2011-07-11 17:44:15 -07001288 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001289
1290 LoaderTask(Context context, boolean isLaunching) {
1291 mContext = context;
1292 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001293 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001294 }
1295
Joe Onorato36115782010-06-17 13:28:48 -04001296 boolean isLaunching() {
1297 return mIsLaunching;
1298 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001299
Winson Chung36a62fe2012-05-06 18:04:42 -07001300 boolean isLoadingWorkspace() {
1301 return mIsLoadingAndBindingWorkspace;
1302 }
1303
Winson Chungc763c4e2013-07-19 13:49:06 -07001304 /** Returns whether this is an upgrade path */
1305 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001306 mIsLoadingAndBindingWorkspace = true;
1307
Joe Onorato36115782010-06-17 13:28:48 -04001308 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001309 if (DEBUG_LOADERS) {
1310 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001312
Winson Chungc763c4e2013-07-19 13:49:06 -07001313 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001314 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001315 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001316 synchronized (LoaderTask.this) {
1317 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001318 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001319 }
1320 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 }
1322 }
1323
Joe Onorato36115782010-06-17 13:28:48 -04001324 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001325 bindWorkspace(-1, isUpgradePath);
1326 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001327 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001328
Joe Onorato36115782010-06-17 13:28:48 -04001329 private void waitForIdle() {
1330 // Wait until the either we're stopped or the other threads are done.
1331 // This way we don't start loading all apps until the workspace has settled
1332 // down.
1333 synchronized (LoaderTask.this) {
1334 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001335
Joe Onorato36115782010-06-17 13:28:48 -04001336 mHandler.postIdle(new Runnable() {
1337 public void run() {
1338 synchronized (LoaderTask.this) {
1339 mLoadAndBindStepFinished = true;
1340 if (DEBUG_LOADERS) {
1341 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001344 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001345 }
Joe Onorato36115782010-06-17 13:28:48 -04001346 });
1347
Michael Jurkac7700af2013-05-14 20:17:58 +02001348 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001349 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001350 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1351 // wait no longer than 1sec at a time
1352 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001353 } catch (InterruptedException ex) {
1354 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001355 }
1356 }
Joe Onorato36115782010-06-17 13:28:48 -04001357 if (DEBUG_LOADERS) {
1358 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001359 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001360 + "ms for previous step to finish binding");
1361 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001362 }
Joe Onorato36115782010-06-17 13:28:48 -04001363 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001364
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001365 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001366 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001367 // Ensure that we have a valid page index to load synchronously
1368 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1369 "valid page index");
1370 }
1371 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1372 // Ensure that we don't try and bind a specified page when the pages have not been
1373 // loaded already (we should load everything asynchronously in that case)
1374 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1375 }
1376 synchronized (mLock) {
1377 if (mIsLoaderTaskRunning) {
1378 // Ensure that we are never running the background loading at this point since
1379 // we also touch the background collections
1380 throw new RuntimeException("Error! Background loading is already running");
1381 }
1382 }
1383
1384 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1385 // data structures, we can't allow any other thread to touch that data, but because
1386 // this call is synchronous, we can get away with not locking).
1387
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001388 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001389 // operations from the previous activity. We need to ensure that all queued operations
1390 // are executed before any synchronous binding work is done.
1391 mHandler.flush();
1392
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 // Divide the set of loaded items into those that we are binding synchronously, and
1394 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001395 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001396 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1397 // arise from that.
1398 onlyBindAllApps();
1399 }
1400
Joe Onorato36115782010-06-17 13:28:48 -04001401 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001402 boolean isUpgrade = false;
1403
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001404 synchronized (mLock) {
1405 mIsLoaderTaskRunning = true;
1406 }
Joe Onorato36115782010-06-17 13:28:48 -04001407 // Optimize for end-user experience: if the Launcher is up and // running with the
1408 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1409 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001410 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001411 // Elevate priority when Home launches for the first time to avoid
1412 // starving at boot time. Staring at a blank home is not cool.
1413 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001414 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1415 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001416 android.os.Process.setThreadPriority(mIsLaunching
1417 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1418 }
Winson Chung64359a52013-07-08 17:17:08 -07001419 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001420 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001421
Joe Onorato36115782010-06-17 13:28:48 -04001422 if (mStopped) {
1423 break keep_running;
1424 }
1425
1426 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1427 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001428 synchronized (mLock) {
1429 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001430 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001431 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1432 }
1433 }
Joe Onorato36115782010-06-17 13:28:48 -04001434 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001435
1436 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001437 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1438 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001439
1440 // Restore the default thread priority after we are done loading items
1441 synchronized (mLock) {
1442 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1443 }
Joe Onorato36115782010-06-17 13:28:48 -04001444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445
Winson Chungaac01e12011-08-17 10:37:13 -07001446 // Update the saved icons if necessary
1447 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001448 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 for (Object key : sBgDbIconCache.keySet()) {
1450 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1451 }
1452 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001453 }
Winson Chungaac01e12011-08-17 10:37:13 -07001454
Winson Chungc58497e2013-09-03 17:48:37 -07001455 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1456 // Ensure that all the applications that are in the system are
1457 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001458 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001459 verifyApplications();
1460 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001461 }
1462
Joe Onorato36115782010-06-17 13:28:48 -04001463 // Clear out this reference, otherwise we end up holding it until all of the
1464 // callback runnables are done.
1465 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466
Joe Onorato36115782010-06-17 13:28:48 -04001467 synchronized (mLock) {
1468 // If we are still the last one to be scheduled, remove ourselves.
1469 if (mLoaderTask == this) {
1470 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001471 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001472 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001473 }
Joe Onorato36115782010-06-17 13:28:48 -04001474 }
1475
1476 public void stopLocked() {
1477 synchronized (LoaderTask.this) {
1478 mStopped = true;
1479 this.notify();
1480 }
1481 }
1482
1483 /**
1484 * Gets the callbacks object. If we've been stopped, or if the launcher object
1485 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1486 * object that was around when the deferred message was scheduled, and if there's
1487 * a new Callbacks object around then also return null. This will save us from
1488 * calling onto it with data that will be ignored.
1489 */
1490 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1491 synchronized (mLock) {
1492 if (mStopped) {
1493 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001494 }
Joe Onorato36115782010-06-17 13:28:48 -04001495
1496 if (mCallbacks == null) {
1497 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001498 }
Joe Onorato36115782010-06-17 13:28:48 -04001499
1500 final Callbacks callbacks = mCallbacks.get();
1501 if (callbacks != oldCallbacks) {
1502 return null;
1503 }
1504 if (callbacks == null) {
1505 Log.w(TAG, "no mCallbacks");
1506 return null;
1507 }
1508
1509 return callbacks;
1510 }
1511 }
1512
Winson Chungc763c4e2013-07-19 13:49:06 -07001513 private void verifyApplications() {
1514 final Context context = mApp.getContext();
1515
1516 // Cross reference all the applications in our apps list with items in the workspace
1517 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001518 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001519 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001520 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001521 tmpInfos = getItemInfoForComponentName(app.componentName);
1522 if (tmpInfos.isEmpty()) {
1523 // We are missing an application icon, so add this to the workspace
1524 added.add(app);
1525 // This is a rare event, so lets log it
1526 Log.e(TAG, "Missing Application on load: " + app);
1527 }
1528 }
1529 }
1530 if (!added.isEmpty()) {
1531 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001532 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001533 }
1534 }
1535
Joe Onorato36115782010-06-17 13:28:48 -04001536 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001537 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001538 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001539 LauncherAppState app = LauncherAppState.getInstance();
1540 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001541 final int countX = (int) grid.numColumns;
1542 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001545 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001546 // Return early if we detect that an item is under the hotseat button
1547 if (mCallbacks == null ||
1548 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001549 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001550 Log.e(TAG, "Error loading shortcut into hotseat " + item
1551 + " into position (" + item.screenId + ":" + item.cellX + ","
1552 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001553 return false;
1554 }
1555
Dan Sandler295ae182013-12-10 16:05:47 -05001556 final ItemInfo[][] hotseatItems =
1557 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1558
Adam Cohenae4409d2013-11-26 10:34:59 -08001559 if (item.screenId >= grid.numHotseatIcons) {
1560 Log.e(TAG, "Error loading shortcut " + item
1561 + " into hotseat position " + item.screenId
1562 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1563 + ")");
1564 return false;
1565 }
1566
Dan Sandler295ae182013-12-10 16:05:47 -05001567 if (hotseatItems != null) {
1568 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 Log.e(TAG, "Error loading shortcut into hotseat " + item
1570 + " into position (" + item.screenId + ":" + item.cellX + ","
1571 + item.cellY + ") occupied by "
1572 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1573 [(int) item.screenId][0]);
1574 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001575 } else {
1576 hotseatItems[(int) item.screenId][0] = item;
1577 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001579 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001580 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 items[(int) item.screenId][0] = item;
1582 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001583 return true;
1584 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001585 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1586 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001587 return true;
1588 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001589
Adam Cohendcd297f2013-06-18 13:13:40 -07001590 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001591 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 occupied.put(item.screenId, items);
1593 }
1594
Dan Sandler295ae182013-12-10 16:05:47 -05001595 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001596 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1597 item.cellX < 0 || item.cellY < 0 ||
1598 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1599 Log.e(TAG, "Error loading shortcut " + item
1600 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1601 + item.cellX + "," + item.cellY
1602 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1603 return false;
1604 }
1605
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001606 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001607 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1608 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001610 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001611 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001612 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001613 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001614 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001615 return false;
1616 }
1617 }
1618 }
1619 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1620 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001622 }
1623 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001624
Joe Onorato36115782010-06-17 13:28:48 -04001625 return true;
1626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627
Winson Chungba9c37f2013-08-30 14:11:37 -07001628 /** Clears all the sBg data structures */
1629 private void clearSBgDataStructures() {
1630 synchronized (sBgLock) {
1631 sBgWorkspaceItems.clear();
1632 sBgAppWidgets.clear();
1633 sBgFolders.clear();
1634 sBgItemsIdMap.clear();
1635 sBgDbIconCache.clear();
1636 sBgWorkspaceScreens.clear();
1637 }
1638 }
1639
Winson Chungc763c4e2013-07-19 13:49:06 -07001640 /** Returns whether this is an upgradge path */
1641 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001642 // Log to disk
1643 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1644
Joe Onorato36115782010-06-17 13:28:48 -04001645 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646
Joe Onorato36115782010-06-17 13:28:48 -04001647 final Context context = mContext;
1648 final ContentResolver contentResolver = context.getContentResolver();
1649 final PackageManager manager = context.getPackageManager();
1650 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1651 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001652
Winson Chung892c74d2013-08-22 16:15:50 -07001653 LauncherAppState app = LauncherAppState.getInstance();
1654 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1655 int countX = (int) grid.numColumns;
1656 int countY = (int) grid.numRows;
1657
Michael Jurkab85f8a42012-04-25 15:48:32 -07001658 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001659 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001660
Winson Chungc763c4e2013-07-19 13:49:06 -07001661 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001662 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001663 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001664
Winson Chung9f9f00b2013-11-15 13:27:00 -08001665 // Log to disk
1666 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001667
Winson Chung2abf94d2012-07-18 18:16:38 -07001668 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001669 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001670
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001671 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001672 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001673 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001674 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001675
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001676 // +1 for the hotseat (it can be larger than the workspace)
1677 // Load workspace in reverse order to ensure that latest items are loaded first (and
1678 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 try {
1682 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1683 final int intentIndex = c.getColumnIndexOrThrow
1684 (LauncherSettings.Favorites.INTENT);
1685 final int titleIndex = c.getColumnIndexOrThrow
1686 (LauncherSettings.Favorites.TITLE);
1687 final int iconTypeIndex = c.getColumnIndexOrThrow(
1688 LauncherSettings.Favorites.ICON_TYPE);
1689 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1690 final int iconPackageIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.ICON_PACKAGE);
1692 final int iconResourceIndex = c.getColumnIndexOrThrow(
1693 LauncherSettings.Favorites.ICON_RESOURCE);
1694 final int containerIndex = c.getColumnIndexOrThrow(
1695 LauncherSettings.Favorites.CONTAINER);
1696 final int itemTypeIndex = c.getColumnIndexOrThrow(
1697 LauncherSettings.Favorites.ITEM_TYPE);
1698 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1699 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001700 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1701 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 final int screenIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.SCREEN);
1704 final int cellXIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.CELLX);
1706 final int cellYIndex = c.getColumnIndexOrThrow
1707 (LauncherSettings.Favorites.CELLY);
1708 final int spanXIndex = c.getColumnIndexOrThrow
1709 (LauncherSettings.Favorites.SPANX);
1710 final int spanYIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.SPANY);
1712 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1713 //final int displayModeIndex = c.getColumnIndexOrThrow(
1714 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 ShortcutInfo info;
1717 String intentDescription;
1718 LauncherAppWidgetInfo appWidgetInfo;
1719 int container;
1720 long id;
1721 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001723 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001724 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 try {
1726 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 switch (itemType) {
1729 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1730 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001731 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 intentDescription = c.getString(intentIndex);
1733 try {
1734 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001735 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001736 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001737 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001738 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001739 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1740 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001741 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001742 // If apps can be on external storage, then we just
1743 // leave them for the user to remove (maybe add
1744 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001745 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001746 }
1747 continue;
1748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001750 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 continue;
1752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1755 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1756 titleIndex, mLabelCache);
1757 } else {
1758 info = getShortcutInfo(c, context, iconTypeIndex,
1759 iconPackageIndex, iconResourceIndex, iconIndex,
1760 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001761
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001762 // App shortcuts that used to be automatically added to Launcher
1763 // didn't always have the correct intent flags set, so do that
1764 // here
1765 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001766 intent.getCategories() != null &&
1767 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001768 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001769 intent.addFlags(
1770 Intent.FLAG_ACTIVITY_NEW_TASK |
1771 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1772 }
Michael Jurka96879562012-03-22 05:54:33 -07001773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001776 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001778 container = c.getInt(containerIndex);
1779 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001780 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 info.cellX = c.getInt(cellXIndex);
1782 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001783 info.spanX = 1;
1784 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001785
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001787 deleteOnInvalidPlacement.set(false);
1788 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1789 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001790 itemsToRemove.add(id);
1791 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 break;
1793 }
1794
1795 switch (container) {
1796 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1797 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1798 sBgWorkspaceItems.add(info);
1799 break;
1800 default:
1801 // Item is in a user folder
1802 FolderInfo folderInfo =
1803 findOrMakeFolder(sBgFolders, container);
1804 folderInfo.add(info);
1805 break;
1806 }
1807 sBgItemsIdMap.put(info.id, info);
1808
1809 // now that we've loaded everthing re-save it with the
1810 // icon in case it disappears somehow.
1811 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001812 } else {
1813 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001814 }
1815 break;
1816
1817 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1818 id = c.getLong(idIndex);
1819 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1820
1821 folderInfo.title = c.getString(titleIndex);
1822 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001824 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001825 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 folderInfo.cellX = c.getInt(cellXIndex);
1827 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001828 folderInfo.spanX = 1;
1829 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830
Daniel Sandler8802e962010-05-26 16:28:16 -04001831 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001832 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001833 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001834 deleteOnInvalidPlacement)) {
1835 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001836 itemsToRemove.add(id);
1837 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001838 break;
1839 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1843 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1844 sBgWorkspaceItems.add(folderInfo);
1845 break;
Joe Onorato36115782010-06-17 13:28:48 -04001846 }
Joe Onorato17a89222011-02-08 17:26:11 -08001847
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1849 sBgFolders.put(folderInfo.id, folderInfo);
1850 break;
1851
1852 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1853 // Read all Launcher-specific widget details
1854 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001855 String savedProvider = c.getString(appWidgetProviderIndex);
1856
Joe Onorato36115782010-06-17 13:28:48 -04001857 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 final AppWidgetProviderInfo provider =
1860 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 if (!isSafeMode && (provider == null || provider.provider == null ||
1863 provider.provider.getPackageName() == null)) {
1864 String log = "Deleting widget that isn't installed anymore: id="
1865 + id + " appWidgetId=" + appWidgetId;
1866 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001867 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 itemsToRemove.add(id);
1869 } else {
1870 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1871 provider.provider);
1872 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001873 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 appWidgetInfo.cellX = c.getInt(cellXIndex);
1875 appWidgetInfo.cellY = c.getInt(cellYIndex);
1876 appWidgetInfo.spanX = c.getInt(spanXIndex);
1877 appWidgetInfo.spanY = c.getInt(spanYIndex);
1878 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1879 appWidgetInfo.minSpanX = minSpan[0];
1880 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001881
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 container = c.getInt(containerIndex);
1883 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1884 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1885 Log.e(TAG, "Widget found where container != " +
1886 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1887 continue;
1888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889
Adam Cohene25af792013-06-06 23:08:25 -07001890 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001892 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001893 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001894 deleteOnInvalidPlacement)) {
1895 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001896 itemsToRemove.add(id);
1897 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 break;
1899 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001900 String providerName = provider.provider.flattenToString();
1901 if (!providerName.equals(savedProvider)) {
1902 ContentValues values = new ContentValues();
1903 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1904 providerName);
1905 String where = BaseColumns._ID + "= ?";
1906 String[] args = {Integer.toString(c.getInt(idIndex))};
1907 contentResolver.update(contentUri, values, where, args);
1908 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1910 sBgAppWidgets.add(appWidgetInfo);
1911 }
Joe Onorato36115782010-06-17 13:28:48 -04001912 break;
1913 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05001915 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001916 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 }
1918 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001919 if (c != null) {
1920 c.close();
1921 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001922 }
1923
Winson Chungba9c37f2013-08-30 14:11:37 -07001924 // Break early if we've stopped loading
1925 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001926 clearSBgDataStructures();
1927 return false;
1928 }
1929
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001930 if (itemsToRemove.size() > 0) {
1931 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001932 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001933 // Remove dead items
1934 for (long id : itemsToRemove) {
1935 if (DEBUG_LOADERS) {
1936 Log.d(TAG, "Removed id = " + id);
1937 }
1938 // Don't notify content observers
1939 try {
1940 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1941 null, null);
1942 } catch (RemoteException e) {
1943 Log.w(TAG, "Could not remove id = " + id);
1944 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001945 }
1946 }
1947
Winson Chungc763c4e2013-07-19 13:49:06 -07001948 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 long maxScreenId = 0;
1950 // If we're importing we use the old screen order.
1951 for (ItemInfo item: sBgItemsIdMap.values()) {
1952 long screenId = item.screenId;
1953 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1954 !sBgWorkspaceScreens.contains(screenId)) {
1955 sBgWorkspaceScreens.add(screenId);
1956 if (screenId > maxScreenId) {
1957 maxScreenId = screenId;
1958 }
1959 }
1960 }
1961 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001962 // Log to disk
1963 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1964 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1965 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001966
Michael Jurka414300a2013-08-27 15:42:35 +02001967 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001969
1970 // Update the max item id after we load an old db
1971 long maxItemId = 0;
1972 // If we're importing we use the old screen order.
1973 for (ItemInfo item: sBgItemsIdMap.values()) {
1974 maxItemId = Math.max(maxItemId, item.id);
1975 }
Michael Jurka414300a2013-08-27 15:42:35 +02001976 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001978 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1979 for (Integer i : orderedScreens.keySet()) {
1980 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001982 // Log to disk
1983 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1984 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001985
1986 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001987 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 for (ItemInfo item: sBgItemsIdMap.values()) {
1989 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1991 unusedScreens.contains(screenId)) {
1992 unusedScreens.remove(screenId);
1993 }
1994 }
1995
1996 // If there are any empty screens remove them, and update.
1997 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001998 // Log to disk
1999 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2000 TextUtils.join(", ", unusedScreens), true);
2001
Winson Chung933bae62013-08-29 11:42:30 -07002002 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002003 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2004 }
2005 }
2006
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002007 if (DEBUG_LOADERS) {
2008 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2009 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002010 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002011 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002013
Daniel Sandler566da102013-06-25 23:43:45 -04002014 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002015 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002017 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002018 line += " | ";
2019 }
Winson Chung892c74d2013-08-22 16:15:50 -07002020 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 }
Joe Onorato36115782010-06-17 13:28:48 -04002023 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002024 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002025 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002026 }
Joe Onorato36115782010-06-17 13:28:48 -04002027 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002028 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002029 }
2030
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 /** Filters the set of items who are directly or indirectly (via another container) on the
2032 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002033 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002034 ArrayList<ItemInfo> allWorkspaceItems,
2035 ArrayList<ItemInfo> currentScreenItems,
2036 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002037 // Purge any null ItemInfos
2038 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2039 while (iter.hasNext()) {
2040 ItemInfo i = iter.next();
2041 if (i == null) {
2042 iter.remove();
2043 }
2044 }
2045
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002046 // Order the set of items by their containers first, this allows use to walk through the
2047 // list sequentially, build up a list of containers that are in the specified screen,
2048 // as well as all items in those containers.
2049 Set<Long> itemsOnScreen = new HashSet<Long>();
2050 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2051 @Override
2052 public int compare(ItemInfo lhs, ItemInfo rhs) {
2053 return (int) (lhs.container - rhs.container);
2054 }
2055 });
2056 for (ItemInfo info : allWorkspaceItems) {
2057 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002058 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 currentScreenItems.add(info);
2060 itemsOnScreen.add(info.id);
2061 } else {
2062 otherScreenItems.add(info);
2063 }
2064 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2065 currentScreenItems.add(info);
2066 itemsOnScreen.add(info.id);
2067 } else {
2068 if (itemsOnScreen.contains(info.container)) {
2069 currentScreenItems.add(info);
2070 itemsOnScreen.add(info.id);
2071 } else {
2072 otherScreenItems.add(info);
2073 }
2074 }
2075 }
2076 }
2077
2078 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002079 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002080 ArrayList<LauncherAppWidgetInfo> appWidgets,
2081 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2082 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083
2084 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002085 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002087 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002088 currentScreenWidgets.add(widget);
2089 } else {
2090 otherScreenWidgets.add(widget);
2091 }
2092 }
2093 }
2094
2095 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002096 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097 HashMap<Long, ItemInfo> itemsIdMap,
2098 HashMap<Long, FolderInfo> folders,
2099 HashMap<Long, FolderInfo> currentScreenFolders,
2100 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101
2102 for (long id : folders.keySet()) {
2103 ItemInfo info = itemsIdMap.get(id);
2104 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002105 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002107 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108 currentScreenFolders.put(id, folder);
2109 } else {
2110 otherScreenFolders.put(id, folder);
2111 }
2112 }
2113 }
2114
2115 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2116 * right) */
2117 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002118 final LauncherAppState app = LauncherAppState.getInstance();
2119 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 // XXX: review this
2121 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002122 @Override
2123 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002124 int cellCountX = (int) grid.numColumns;
2125 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002126 int screenOffset = cellCountX * cellCountY;
2127 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002128 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002129 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002130 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002131 rhs.cellY * cellCountX + rhs.cellX);
2132 return (int) (lr - rr);
2133 }
2134 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002135 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002136
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2138 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 final Runnable r = new Runnable() {
2140 @Override
2141 public void run() {
2142 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2143 if (callbacks != null) {
2144 callbacks.bindScreens(orderedScreens);
2145 }
2146 }
2147 };
2148 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2149 }
2150
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2152 final ArrayList<ItemInfo> workspaceItems,
2153 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2154 final HashMap<Long, FolderInfo> folders,
2155 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002156
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 final boolean postOnMainThread = (deferredBindRunnables != null);
2158
2159 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002160 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002162 final int start = i;
2163 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 final Runnable r = new Runnable() {
2165 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002167 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002169 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2170 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002171 }
2172 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 };
2174 if (postOnMainThread) {
2175 deferredBindRunnables.add(r);
2176 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002177 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 }
Joe Onorato36115782010-06-17 13:28:48 -04002179 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180
2181 // Bind the folders
2182 if (!folders.isEmpty()) {
2183 final Runnable r = new Runnable() {
2184 public void run() {
2185 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2186 if (callbacks != null) {
2187 callbacks.bindFolders(folders);
2188 }
2189 }
2190 };
2191 if (postOnMainThread) {
2192 deferredBindRunnables.add(r);
2193 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002194 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 }
2196 }
2197
2198 // Bind the widgets, one at a time
2199 N = appWidgets.size();
2200 for (int i = 0; i < N; i++) {
2201 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2202 final Runnable r = new Runnable() {
2203 public void run() {
2204 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2205 if (callbacks != null) {
2206 callbacks.bindAppWidget(widget);
2207 }
2208 }
2209 };
2210 if (postOnMainThread) {
2211 deferredBindRunnables.add(r);
2212 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002213 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 }
2215 }
2216 }
2217
2218 /**
2219 * Binds all loaded data to actual views on the main thread.
2220 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002221 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 final long t = SystemClock.uptimeMillis();
2223 Runnable r;
2224
2225 // Don't use these two variables in any of the callback runnables.
2226 // Otherwise we hold a reference to them.
2227 final Callbacks oldCallbacks = mCallbacks.get();
2228 if (oldCallbacks == null) {
2229 // This launcher has exited and nobody bothered to tell us. Just bail.
2230 Log.w(TAG, "LoaderTask running with no launcher");
2231 return;
2232 }
2233
Winson Chung9b9fb962013-11-15 15:39:34 -08002234 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002235 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2236 ArrayList<LauncherAppWidgetInfo> appWidgets =
2237 new ArrayList<LauncherAppWidgetInfo>();
2238 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2239 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002241 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 workspaceItems.addAll(sBgWorkspaceItems);
2243 appWidgets.addAll(sBgAppWidgets);
2244 folders.putAll(sBgFolders);
2245 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 }
2248
Derek Prothro7aff3992013-12-10 14:00:37 -05002249 final boolean isLoadingSynchronously =
2250 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002251 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002252 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002253 if (currScreen >= orderedScreenIds.size()) {
2254 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002255 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002256 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002257 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002258 final long currentScreenId = currentScreen < 0
2259 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002260
2261 // Load all the items that are on the current page first (and in the process, unbind
2262 // all the existing workspace items before we call startBinding() below.
2263 unbindWorkspaceItemsOnMainThread();
2264
2265 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002266 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2267 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2268 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2269 new ArrayList<LauncherAppWidgetInfo>();
2270 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2271 new ArrayList<LauncherAppWidgetInfo>();
2272 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2273 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2274
Winson Chung9b9fb962013-11-15 15:39:34 -08002275 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002277 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002279 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 otherFolders);
2281 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2282 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2283
2284 // Tell the workspace that we're about to start binding items
2285 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002286 public void run() {
2287 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2288 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002290 }
2291 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 };
Winson Chung81b52252012-08-27 15:34:29 -07002293 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2296
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 // Load items on the current page
2298 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2299 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002300 if (isLoadingSynchronously) {
2301 r = new Runnable() {
2302 public void run() {
2303 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002304 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002305 callbacks.onPageBoundSynchronously(currentScreen);
2306 }
2307 }
2308 };
Winson Chung81b52252012-08-27 15:34:29 -07002309 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002310 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311
Winson Chung4a2afa32012-07-19 14:53:05 -07002312 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2313 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314 mDeferredBindRunnables.clear();
2315 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002316 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317
2318 // Tell the workspace that we're done binding items
2319 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002320 public void run() {
2321 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2322 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002323 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002324 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325
Winson Chung98e030b2012-05-07 16:01:11 -07002326 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002327 if (DEBUG_LOADERS) {
2328 Log.d(TAG, "bound workspace in "
2329 + (SystemClock.uptimeMillis()-t) + "ms");
2330 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002331
2332 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002333 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002334 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002335 if (isLoadingSynchronously) {
2336 mDeferredBindRunnables.add(r);
2337 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002338 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002339 }
Joe Onorato36115782010-06-17 13:28:48 -04002340 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002341
Joe Onorato36115782010-06-17 13:28:48 -04002342 private void loadAndBindAllApps() {
2343 if (DEBUG_LOADERS) {
2344 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2345 }
2346 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002347 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002348 synchronized (LoaderTask.this) {
2349 if (mStopped) {
2350 return;
2351 }
2352 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 } else {
2355 onlyBindAllApps();
2356 }
2357 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002358
Joe Onorato36115782010-06-17 13:28:48 -04002359 private void onlyBindAllApps() {
2360 final Callbacks oldCallbacks = mCallbacks.get();
2361 if (oldCallbacks == null) {
2362 // This launcher has exited and nobody bothered to tell us. Just bail.
2363 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2364 return;
2365 }
2366
2367 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002368 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002369 final ArrayList<AppInfo> list
2370 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002371 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002372 public void run() {
2373 final long t = SystemClock.uptimeMillis();
2374 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2375 if (callbacks != null) {
2376 callbacks.bindAllApplications(list);
2377 }
2378 if (DEBUG_LOADERS) {
2379 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2380 + (SystemClock.uptimeMillis()-t) + "ms");
2381 }
2382 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002383 };
2384 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002385 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002386 r.run();
2387 } else {
2388 mHandler.post(r);
2389 }
Joe Onorato36115782010-06-17 13:28:48 -04002390 }
2391
Winson Chung64359a52013-07-08 17:17:08 -07002392 private void loadAllApps() {
2393 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002394
Joe Onorato36115782010-06-17 13:28:48 -04002395 final Callbacks oldCallbacks = mCallbacks.get();
2396 if (oldCallbacks == null) {
2397 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002398 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002399 return;
2400 }
2401
Winson Chung64359a52013-07-08 17:17:08 -07002402 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002403 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2404 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2405
Winson Chung64359a52013-07-08 17:17:08 -07002406 // Clear the list of apps
2407 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002408
Winson Chung64359a52013-07-08 17:17:08 -07002409 // Query for the set of apps
2410 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2411 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2412 if (DEBUG_LOADERS) {
2413 Log.d(TAG, "queryIntentActivities took "
2414 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2415 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2416 }
2417 // Fail if we don't have any apps
2418 if (apps == null || apps.isEmpty()) {
2419 return;
2420 }
2421 // Sort the applications by name
2422 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2423 Collections.sort(apps,
2424 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2425 if (DEBUG_LOADERS) {
2426 Log.d(TAG, "sort took "
2427 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 }
2429
Winson Chung64359a52013-07-08 17:17:08 -07002430 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002431 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002432 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002433 // This builds the icon bitmaps.
2434 mBgAllAppsList.add(new AppInfo(packageManager, app,
2435 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002436 }
2437
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002438 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002439 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2440 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002441
2442 // Post callback on main thread
2443 mHandler.post(new Runnable() {
2444 public void run() {
2445 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002446 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002447 if (callbacks != null) {
2448 callbacks.bindAllApplications(added);
2449 if (DEBUG_LOADERS) {
2450 Log.d(TAG, "bound " + added.size() + " apps in "
2451 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2452 }
2453 } else {
2454 Log.i(TAG, "not binding apps: no Launcher activity");
2455 }
2456 }
2457 });
2458
Joe Onorato36115782010-06-17 13:28:48 -04002459 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002460 Log.d(TAG, "Icons processed in "
2461 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002462 }
2463 }
2464
2465 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002466 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2468 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2469 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2470 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2471 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2472 }
Joe Onorato36115782010-06-17 13:28:48 -04002473 }
2474 }
2475
2476 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002477 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002478 }
2479
2480 private class PackageUpdatedTask implements Runnable {
2481 int mOp;
2482 String[] mPackages;
2483
2484 public static final int OP_NONE = 0;
2485 public static final int OP_ADD = 1;
2486 public static final int OP_UPDATE = 2;
2487 public static final int OP_REMOVE = 3; // uninstlled
2488 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2489
2490
2491 public PackageUpdatedTask(int op, String[] packages) {
2492 mOp = op;
2493 mPackages = packages;
2494 }
2495
2496 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002497 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002498
2499 final String[] packages = mPackages;
2500 final int N = packages.length;
2501 switch (mOp) {
2502 case OP_ADD:
2503 for (int i=0; i<N; i++) {
2504 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002505 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
2507 break;
2508 case OP_UPDATE:
2509 for (int i=0; i<N; i++) {
2510 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002511 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002512 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002513 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002514 }
2515 break;
2516 case OP_REMOVE:
2517 case OP_UNAVAILABLE:
2518 for (int i=0; i<N; i++) {
2519 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002520 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002521 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002522 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002523 }
2524 break;
2525 }
2526
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002527 ArrayList<AppInfo> added = null;
2528 ArrayList<AppInfo> modified = null;
2529 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002530
Adam Cohen487f7dd2012-06-28 18:12:10 -07002531 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002532 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002533 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002534 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002535 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002536 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002537 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002538 }
Winson Chung5d55f332012-07-16 20:45:03 -07002539 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002540 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002541 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002542 }
2543
Joe Onorato36115782010-06-17 13:28:48 -04002544 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2545 if (callbacks == null) {
2546 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2547 return;
2548 }
2549
2550 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002551 // Ensure that we add all the workspace applications to the db
2552 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002553 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2554 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2555 } else {
2556 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2557 addAndBindAddedApps(context, addedInfos, cb, added);
2558 }
Joe Onorato36115782010-06-17 13:28:48 -04002559 }
2560 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002561 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002562
2563 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002564 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002565 ArrayList<ItemInfo> infos =
2566 getItemInfoForComponentName(a.componentName);
2567 for (ItemInfo i : infos) {
2568 if (isShortcutInfoUpdateable(i)) {
2569 ShortcutInfo info = (ShortcutInfo) i;
2570 info.title = a.title.toString();
2571 updateItemInDatabase(context, info);
2572 }
2573 }
2574 }
2575
Joe Onorato36115782010-06-17 13:28:48 -04002576 mHandler.post(new Runnable() {
2577 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002578 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2579 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002580 callbacks.bindAppsUpdated(modifiedFinal);
2581 }
2582 }
2583 });
2584 }
Winson Chung83892cc2013-05-01 16:53:33 -07002585
Winson Chungdf95eb12013-10-16 14:57:07 -07002586 final ArrayList<String> removedPackageNames =
2587 new ArrayList<String>();
2588 if (mOp == OP_REMOVE) {
2589 // Mark all packages in the broadcast to be removed
2590 removedPackageNames.addAll(Arrays.asList(packages));
2591 } else if (mOp == OP_UPDATE) {
2592 // Mark disabled packages in the broadcast to be removed
2593 final PackageManager pm = context.getPackageManager();
2594 for (int i=0; i<N; i++) {
2595 if (isPackageDisabled(pm, packages[i])) {
2596 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002597 }
2598 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002599 }
2600 // Remove all the components associated with this package
2601 for (String pn : removedPackageNames) {
2602 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2603 for (ItemInfo i : infos) {
2604 deleteItemFromDatabase(context, i);
2605 }
2606 }
2607 // Remove all the specific components
2608 for (AppInfo a : removedApps) {
2609 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2610 for (ItemInfo i : infos) {
2611 deleteItemFromDatabase(context, i);
2612 }
2613 }
2614 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2615 // Remove any queued items from the install queue
2616 String spKey = LauncherAppState.getSharedPreferencesKey();
2617 SharedPreferences sp =
2618 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2619 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2620 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002621 mHandler.post(new Runnable() {
2622 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002623 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2624 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002625 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002626 }
2627 }
2628 });
Joe Onoratobe386092009-11-17 17:32:16 -08002629 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002630
Michael Jurkac402cd92013-05-20 15:49:32 +02002631 final ArrayList<Object> widgetsAndShortcuts =
2632 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002633 mHandler.post(new Runnable() {
2634 @Override
2635 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002636 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2637 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002638 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002639 }
2640 }
2641 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002642
2643 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002644 mHandler.post(new Runnable() {
2645 public void run() {
2646 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2647 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002648 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002649 }
2650 }
2651 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002652 }
2653 }
2654
Michael Jurkac402cd92013-05-20 15:49:32 +02002655 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2656 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2657 PackageManager packageManager = context.getPackageManager();
2658 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2659 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2660 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2661 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2662 Collections.sort(widgetsAndShortcuts,
2663 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2664 return widgetsAndShortcuts;
2665 }
2666
Winson Chungdf95eb12013-10-16 14:57:07 -07002667 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2668 try {
2669 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2670 return !pi.applicationInfo.enabled;
2671 } catch (NameNotFoundException e) {
2672 // Fall through
2673 }
2674 return false;
2675 }
Winson Chung1323b482013-08-05 12:41:55 -07002676 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002677 if (cn == null) {
2678 return false;
2679 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002680 if (isPackageDisabled(pm, cn.getPackageName())) {
2681 return false;
2682 }
Winson Chungee055712013-07-30 14:46:24 -07002683
2684 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002685 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002686 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002687 return (pm.getActivityInfo(cn, 0) != null);
2688 } catch (NameNotFoundException e) {
2689 return false;
2690 }
2691 }
2692
Joe Onorato9c1289c2009-08-17 11:03:03 -04002693 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002694 * This is called from the code that adds shortcuts from the intent receiver. This
2695 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002696 */
Joe Onorato56d82912010-03-07 14:32:10 -05002697 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002698 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002699 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700
Joe Onorato56d82912010-03-07 14:32:10 -05002701 /**
2702 * Make an ShortcutInfo object for a shortcut that is an application.
2703 *
2704 * If c is not null, then it will be used to fill in missing data like the title and icon.
2705 */
2706 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002707 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002708 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002709 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002710 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002711 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002712 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002713 } else {
2714 try {
2715 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2716 info.initFlagsAndFirstInstallTime(pi);
2717 } catch (NameNotFoundException e) {
2718 Log.d(TAG, "getPackInfo failed for package " +
2719 componentName.getPackageName());
2720 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002721 }
2722
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002723 // TODO: See if the PackageManager knows about this case. If it doesn't
2724 // then return null & delete this.
2725
Joe Onorato56d82912010-03-07 14:32:10 -05002726 // the resource -- This may implicitly give us back the fallback icon,
2727 // but don't worry about that. All we're doing with usingFallbackIcon is
2728 // to avoid saving lots of copies of that in the database, and most apps
2729 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002730
2731 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2732 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2733 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002734 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002735 ResolveInfo resolveInfo = null;
2736 ComponentName oldComponent = intent.getComponent();
2737 Intent newIntent = new Intent(intent.getAction(), null);
2738 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2739 newIntent.setPackage(oldComponent.getPackageName());
2740 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2741 for (ResolveInfo i : infos) {
2742 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2743 i.activityInfo.name);
2744 if (cn.equals(oldComponent)) {
2745 resolveInfo = i;
2746 }
2747 }
2748 if (resolveInfo == null) {
2749 resolveInfo = manager.resolveActivity(intent, 0);
2750 }
Joe Onorato56d82912010-03-07 14:32:10 -05002751 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002752 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002753 }
Joe Onorato56d82912010-03-07 14:32:10 -05002754 // the db
2755 if (icon == null) {
2756 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002757 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002758 }
2759 }
2760 // the fallback icon
2761 if (icon == null) {
2762 icon = getFallbackIcon();
2763 info.usingFallbackIcon = true;
2764 }
2765 info.setIcon(icon);
2766
2767 // from the resource
2768 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002769 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2770 if (labelCache != null && labelCache.containsKey(key)) {
2771 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002772 } else {
2773 info.title = resolveInfo.activityInfo.loadLabel(manager);
2774 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002775 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002776 }
2777 }
Joe Onorato56d82912010-03-07 14:32:10 -05002778 }
2779 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002781 if (c != null) {
2782 info.title = c.getString(titleIndex);
2783 }
2784 }
2785 // fall back to the class name of the activity
2786 if (info.title == null) {
2787 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002789 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2790 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002792
Winson Chung64359a52013-07-08 17:17:08 -07002793 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2794 ItemInfoFilter f) {
2795 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2796 for (ItemInfo i : infos) {
2797 if (i instanceof ShortcutInfo) {
2798 ShortcutInfo info = (ShortcutInfo) i;
2799 ComponentName cn = info.intent.getComponent();
2800 if (cn != null && f.filterItem(null, info, cn)) {
2801 filtered.add(info);
2802 }
2803 } else if (i instanceof FolderInfo) {
2804 FolderInfo info = (FolderInfo) i;
2805 for (ShortcutInfo s : info.contents) {
2806 ComponentName cn = s.intent.getComponent();
2807 if (cn != null && f.filterItem(info, s, cn)) {
2808 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002809 }
2810 }
Winson Chung64359a52013-07-08 17:17:08 -07002811 } else if (i instanceof LauncherAppWidgetInfo) {
2812 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2813 ComponentName cn = info.providerName;
2814 if (cn != null && f.filterItem(null, info, cn)) {
2815 filtered.add(info);
2816 }
Winson Chung8a435102012-08-30 17:16:53 -07002817 }
2818 }
Winson Chung64359a52013-07-08 17:17:08 -07002819 return new ArrayList<ItemInfo>(filtered);
2820 }
2821
2822 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002823 ItemInfoFilter filter = new ItemInfoFilter() {
2824 @Override
2825 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2826 return cn.getPackageName().equals(pn);
2827 }
2828 };
2829 return filterItemInfos(sBgItemsIdMap.values(), filter);
2830 }
2831
2832 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002833 ItemInfoFilter filter = new ItemInfoFilter() {
2834 @Override
2835 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2836 return cn.equals(cname);
2837 }
2838 };
2839 return filterItemInfos(sBgItemsIdMap.values(), filter);
2840 }
2841
2842 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2843 if (i instanceof ShortcutInfo) {
2844 ShortcutInfo info = (ShortcutInfo) i;
2845 // We need to check for ACTION_MAIN otherwise getComponent() might
2846 // return null for some shortcuts (for instance, for shortcuts to
2847 // web pages.)
2848 Intent intent = info.intent;
2849 ComponentName name = intent.getComponent();
2850 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2851 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2852 return true;
2853 }
2854 }
2855 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002856 }
2857
2858 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002859 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002861 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002862 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2863 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864
Joe Onorato56d82912010-03-07 14:32:10 -05002865 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002866 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002869 // TODO: If there's an explicit component and we can't install that, delete it.
2870
Joe Onorato56d82912010-03-07 14:32:10 -05002871 info.title = c.getString(titleIndex);
2872
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 int iconType = c.getInt(iconTypeIndex);
2874 switch (iconType) {
2875 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2876 String packageName = c.getString(iconPackageIndex);
2877 String resourceName = c.getString(iconResourceIndex);
2878 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002879 info.customIcon = false;
2880 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002883 if (resources != null) {
2884 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002885 icon = Utilities.createIconBitmap(
2886 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002889 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 }
Joe Onorato56d82912010-03-07 14:32:10 -05002891 // the db
2892 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002893 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002894 }
2895 // the fallback icon
2896 if (icon == null) {
2897 icon = getFallbackIcon();
2898 info.usingFallbackIcon = true;
2899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 break;
2901 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002902 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002903 if (icon == null) {
2904 icon = getFallbackIcon();
2905 info.customIcon = false;
2906 info.usingFallbackIcon = true;
2907 } else {
2908 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 break;
2911 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002912 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002913 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 info.customIcon = false;
2915 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002917 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002918 return info;
2919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920
Michael Jurka931dc972011-08-05 15:08:15 -07002921 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002922 @SuppressWarnings("all") // suppress dead code warning
2923 final boolean debug = false;
2924 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002925 Log.d(TAG, "getIconFromCursor app="
2926 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2927 }
2928 byte[] data = c.getBlob(iconIndex);
2929 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002930 return Utilities.createIconBitmap(
2931 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002932 } catch (Exception e) {
2933 return null;
2934 }
2935 }
2936
Winson Chung3d503fb2011-07-13 17:25:49 -07002937 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2938 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002939 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002940 if (info == null) {
2941 return null;
2942 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002943 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002944
2945 return info;
2946 }
2947
Winson Chunga9abd0e2010-10-27 17:18:37 -07002948 /**
Winson Chung55cef262010-10-28 14:14:18 -07002949 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2950 */
2951 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2952 ComponentName component) {
2953 List<AppWidgetProviderInfo> widgets =
2954 AppWidgetManager.getInstance(context).getInstalledProviders();
2955 for (AppWidgetProviderInfo info : widgets) {
2956 if (info.provider.equals(component)) {
2957 return info;
2958 }
2959 }
2960 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002961 }
2962
Winson Chung68846fd2010-10-29 11:00:27 -07002963 /**
2964 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2965 */
2966 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2967 final PackageManager packageManager = context.getPackageManager();
2968 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2969 new ArrayList<WidgetMimeTypeHandlerData>();
2970
2971 final Intent supportsIntent =
2972 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2973 supportsIntent.setType(mimeType);
2974
2975 // Create a set of widget configuration components that we can test against
2976 final List<AppWidgetProviderInfo> widgets =
2977 AppWidgetManager.getInstance(context).getInstalledProviders();
2978 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2979 new HashMap<ComponentName, AppWidgetProviderInfo>();
2980 for (AppWidgetProviderInfo info : widgets) {
2981 configurationComponentToWidget.put(info.configure, info);
2982 }
2983
2984 // Run through each of the intents that can handle this type of clip data, and cross
2985 // reference them with the components that are actual configuration components
2986 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2987 PackageManager.MATCH_DEFAULT_ONLY);
2988 for (ResolveInfo info : activities) {
2989 final ActivityInfo activityInfo = info.activityInfo;
2990 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2991 activityInfo.name);
2992 if (configurationComponentToWidget.containsKey(infoComponent)) {
2993 supportedConfigurationActivities.add(
2994 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2995 configurationComponentToWidget.get(infoComponent)));
2996 }
2997 }
2998 return supportedConfigurationActivities;
2999 }
3000
Winson Chunga9abd0e2010-10-27 17:18:37 -07003001 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003002 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3003 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3004 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3005
Adam Cohend9198822011-11-22 16:42:47 -08003006 if (intent == null) {
3007 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3008 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3009 return null;
3010 }
3011
Joe Onorato0589f0f2010-02-08 13:44:00 -08003012 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003013 boolean customIcon = false;
3014 ShortcutIconResource iconResource = null;
3015
3016 if (bitmap != null && bitmap instanceof Bitmap) {
3017 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003018 customIcon = true;
3019 } else {
3020 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3021 if (extra != null && extra instanceof ShortcutIconResource) {
3022 try {
3023 iconResource = (ShortcutIconResource) extra;
3024 final PackageManager packageManager = context.getPackageManager();
3025 Resources resources = packageManager.getResourcesForApplication(
3026 iconResource.packageName);
3027 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003028 icon = Utilities.createIconBitmap(
3029 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003030 } catch (Exception e) {
3031 Log.w(TAG, "Could not load shortcut icon: " + extra);
3032 }
3033 }
3034 }
3035
Michael Jurkac9d95c52011-08-29 14:03:34 -07003036 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003037
3038 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003039 if (fallbackIcon != null) {
3040 icon = fallbackIcon;
3041 } else {
3042 icon = getFallbackIcon();
3043 info.usingFallbackIcon = true;
3044 }
Joe Onorato56d82912010-03-07 14:32:10 -05003045 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003046 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003047
Joe Onorato0589f0f2010-02-08 13:44:00 -08003048 info.title = name;
3049 info.intent = intent;
3050 info.customIcon = customIcon;
3051 info.iconResource = iconResource;
3052
3053 return info;
3054 }
3055
Winson Chungaac01e12011-08-17 10:37:13 -07003056 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3057 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003058 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003059 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003060 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003061 }
Joe Onorato56d82912010-03-07 14:32:10 -05003062 // If this icon doesn't have a custom icon, check to see
3063 // what's stored in the DB, and if it doesn't match what
3064 // we're going to show, store what we are going to show back
3065 // into the DB. We do this so when we're loading, if the
3066 // package manager can't find an icon (for example because
3067 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003068 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003069 cache.put(info, c.getBlob(iconIndex));
3070 return true;
3071 }
3072 return false;
3073 }
3074 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3075 boolean needSave = false;
3076 try {
3077 if (data != null) {
3078 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3079 Bitmap loaded = info.getIcon(mIconCache);
3080 needSave = !saved.sameAs(loaded);
3081 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003082 needSave = true;
3083 }
Winson Chungaac01e12011-08-17 10:37:13 -07003084 } catch (Exception e) {
3085 needSave = true;
3086 }
3087 if (needSave) {
3088 Log.d(TAG, "going to save icon bitmap for info=" + info);
3089 // This is slower than is ideal, but this only happens once
3090 // or when the app is updated with a new icon.
3091 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003092 }
3093 }
3094
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003096 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 * or make a new one.
3098 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003099 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 // See if a placeholder was created for us already
3101 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003102 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003104 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 folders.put(id, folderInfo);
3106 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003107 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 }
3109
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003110 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003111 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003112 return new Comparator<AppInfo>() {
3113 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003114 int result = collator.compare(a.title.toString().trim(),
3115 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003116 if (result == 0) {
3117 result = a.componentName.compareTo(b.componentName);
3118 }
3119 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003120 }
Winson Chung11904872012-09-17 16:58:46 -07003121 };
3122 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003123 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3124 = new Comparator<AppInfo>() {
3125 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003126 if (a.firstInstallTime < b.firstInstallTime) return 1;
3127 if (a.firstInstallTime > b.firstInstallTime) return -1;
3128 return 0;
3129 }
3130 };
Winson Chung11904872012-09-17 16:58:46 -07003131 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3132 final Collator collator = Collator.getInstance();
3133 return new Comparator<AppWidgetProviderInfo>() {
3134 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003135 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003136 }
3137 };
3138 }
Winson Chung5308f242011-08-18 12:12:41 -07003139 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3140 if (info.activityInfo != null) {
3141 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3142 } else {
3143 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3144 }
3145 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003147 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003150 ShortcutNameComparator(PackageManager pm) {
3151 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003152 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003153 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003154 }
3155 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3156 mPackageManager = pm;
3157 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003158 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003159 }
3160 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003161 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003162 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3163 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3164 if (mLabelCache.containsKey(keyA)) {
3165 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003166 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003167 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003168
Winson Chung5308f242011-08-18 12:12:41 -07003169 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003170 }
Winson Chung5308f242011-08-18 12:12:41 -07003171 if (mLabelCache.containsKey(keyB)) {
3172 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003173 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003174 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003175
Winson Chung5308f242011-08-18 12:12:41 -07003176 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003177 }
Winson Chung11904872012-09-17 16:58:46 -07003178 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003179 }
3180 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003181 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003182 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003183 private PackageManager mPackageManager;
3184 private HashMap<Object, String> mLabelCache;
3185 WidgetAndShortcutNameComparator(PackageManager pm) {
3186 mPackageManager = pm;
3187 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003188 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003189 }
3190 public final int compare(Object a, Object b) {
3191 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003192 if (mLabelCache.containsKey(a)) {
3193 labelA = mLabelCache.get(a);
3194 } else {
3195 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003196 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003197 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003198 mLabelCache.put(a, labelA);
3199 }
3200 if (mLabelCache.containsKey(b)) {
3201 labelB = mLabelCache.get(b);
3202 } else {
3203 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003204 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003205 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003206 mLabelCache.put(b, labelB);
3207 }
Winson Chung11904872012-09-17 16:58:46 -07003208 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003209 }
3210 };
Joe Onoratobe386092009-11-17 17:32:16 -08003211
3212 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003213 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003214 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3215 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3216 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3217 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003218 if (mLoaderTask != null) {
3219 mLoaderTask.dumpState();
3220 } else {
3221 Log.d(TAG, "mLoaderTask=null");
3222 }
Joe Onoratobe386092009-11-17 17:32:16 -08003223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224}