blob: 179c8aa76f40ca423d7c1b2b7eebbbc043dccc2c [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;
Winson Chungaafa03c2010-06-11 17:34:16 -070042import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070043import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040044import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080045
Michael Jurkac2f801e2011-07-12 14:19:46 -070046import java.lang.ref.WeakReference;
47import java.net.URISyntaxException;
48import java.text.Collator;
49import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070050import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070051import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070052import java.util.Collections;
53import java.util.Comparator;
54import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070055import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070056import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070057import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070059import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061/**
62 * Maintains in-memory state of the Launcher. It is expected that there should be only one
63 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070064 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040066public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080067 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070069
Daniel Sandler8707e0f2013-08-15 15:54:18 -070070 // true = use a "More Apps" folder for non-workspace apps on upgrade
71 // false = strew non-workspace apps across the workspace on upgrade
72 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
73
Joe Onorato36115782010-06-17 13:28:48 -040074 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070075 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040076
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078 private final Object mLock = new Object();
79 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040080 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020082 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
Winson Chung81b52252012-08-27 15:34:29 -070084 // Specific runnable types that are run on the main thread deferred handler, this allows us to
85 // clear all queued binding runnables when the Launcher activity is destroyed.
86 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
87 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
88
89
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070090 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
91 static {
92 sWorkerThread.start();
93 }
94 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
95
Joe Onoratocc67f472010-06-08 10:54:30 -070096 // We start off with everything not loaded. After that, we assume that
97 // our monitoring of the package manager provides all updates and we never
98 // need to do a requery. These are only ever touched from the loader thread.
99 private boolean mWorkspaceLoaded;
100 private boolean mAllAppsLoaded;
101
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700102 // When we are loading pages synchronously, we can't just post the binding of items on the side
103 // pages as this delays the rotation process. Instead, we wait for a callback from the first
104 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
105 // a normal load, we also clear this set of Runnables.
106 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700110 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700111 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800112
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700113 // The lock that must be acquired before referencing any static bg data structures. Unlike
114 // other locks, this one can generally be held long-term because we never expect any of these
115 // static data structures to be referenced outside of the worker thread except on the first
116 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700117 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118
Adam Cohen487f7dd2012-06-28 18:12:10 -0700119 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700120 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
124 // created by LauncherModel that are directly on the home screen (however, no widgets or
125 // shortcuts within folders).
126 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
129 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130 new ArrayList<LauncherAppWidgetInfo>();
131
Adam Cohen487f7dd2012-06-28 18:12:10 -0700132 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
133 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
136 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700137
138 // sBgWorkspaceScreens is the ordered set of workspace screens.
139 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // </ only access in worker thread >
142
143 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Reena Lee99a73f32011-10-24 17:27:37 -0700146 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700147
Joe Onorato9c1289c2009-08-17 11:03:03 -0400148 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700149 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public int getCurrentWorkspaceScreen();
151 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700152 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
153 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700154 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500156 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700157 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 public void bindAppWidget(LauncherAppWidgetInfo info);
Bjorn Bringert85f418d2013-09-06 12:50:05 +0100159 public boolean shouldShowApp(ResolveInfo app);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200160 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700161 public void bindAppsAdded(ArrayList<Long> newScreens,
162 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700163 ArrayList<ItemInfo> addAnimated,
164 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200165 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700166 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700168 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200169 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100170 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700171 public void onPageBoundSynchronously(int page);
Adam Cohen4caf2982013-08-20 18:54:31 -0700172 public void dumpLogsToLocalData(boolean email);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung64359a52013-07-08 17:17:08 -0700175 public interface ItemInfoFilter {
176 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
177 }
178
Daniel Sandlere4f98912013-06-25 15:13:26 -0400179 LauncherModel(LauncherAppState app, IconCache iconCache) {
180 final Context context = app.getContext();
181
Winson Chungee055712013-07-30 14:46:24 -0700182 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400183 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700184 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800185 mIconCache = iconCache;
186
187 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400189
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700191 Configuration config = res.getConfiguration();
192 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800193 }
194
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700195 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
196 * posted on the main thread handler. */
197 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700198 runOnMainThread(r, 0);
199 }
200 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700201 if (sWorkerThread.getThreadId() == Process.myTid()) {
202 // If we are on the worker thread, post onto the main handler
203 mHandler.post(r);
204 } else {
205 r.run();
206 }
207 }
208
209 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
210 * posted on the worker thread handler. */
211 private static void runOnWorkerThread(Runnable r) {
212 if (sWorkerThread.getThreadId() == Process.myTid()) {
213 r.run();
214 } else {
215 // If we are not on the worker thread, then post to the worker handler
216 sWorker.post(r);
217 }
218 }
219
Winson Chungc9168342013-06-26 14:54:55 -0700220 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
221 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700222 LauncherAppState app = LauncherAppState.getInstance();
223 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
224 final int xCount = (int) grid.numColumns;
225 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700226 boolean[][] occupied = new boolean[xCount][yCount];
227
228 int cellX, cellY, spanX, spanY;
229 for (int i = 0; i < items.size(); ++i) {
230 final ItemInfo item = items.get(i);
231 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
232 if (item.screenId == screen) {
233 cellX = item.cellX;
234 cellY = item.cellY;
235 spanX = item.spanX;
236 spanY = item.spanY;
237 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
238 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
239 occupied[x][y] = true;
240 }
241 }
242 }
243 }
244 }
245
246 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
247 }
248 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700249 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700250 int firstScreenIndex,
251 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700252 // Lock on the app so that we don't try and get the items while apps are being added
253 LauncherAppState app = LauncherAppState.getInstance();
254 LauncherModel model = app.getModel();
255 boolean found = false;
256 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700257 if (sWorkerThread.getThreadId() != Process.myTid()) {
258 // Flush the LauncherModel worker thread, so that if we just did another
259 // processInstallShortcut, we give it time for its shortcut to get added to the
260 // database (getItemsInLocalCoordinates reads the database)
261 model.flushWorkerThread();
262 }
Winson Chungc9168342013-06-26 14:54:55 -0700263 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700264
265 // Try adding to the workspace screens incrementally, starting at the default or center
266 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700267 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
268 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700269 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700270 int[] tmpCoordinates = new int[2];
271 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700272 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700273 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700274 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700275 }
276 }
277 }
Winson Chungc9168342013-06-26 14:54:55 -0700278 return null;
279 }
280
Winson Chungc58497e2013-09-03 17:48:37 -0700281 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
282 final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700283 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -0700284 addAndBindAddedApps(context, added, cb, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700285 }
286 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chungc58497e2013-09-03 17:48:37 -0700287 final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700288 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps()", true);
Winson Chung997a9232013-07-24 15:33:46 -0700289 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700290 return;
Winson Chung997a9232013-07-24 15:33:46 -0700291 }
Winson Chung64359a52013-07-08 17:17:08 -0700292 // Process the newly added applications and add them to the database first
293 Runnable r = new Runnable() {
294 public void run() {
295 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
296 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
297
Winson Chung76828c82013-08-19 15:43:29 -0700298 // Get the list of workspace screens. We need to append to this list and
299 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
300 // called.
301 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
302 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
303 for (Integer i : orderedScreens.keySet()) {
304 long screenId = orderedScreens.get(i);
305 workspaceScreens.add(screenId);
306 }
307
Winson Chung64359a52013-07-08 17:17:08 -0700308 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700309 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700310 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700311 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700312 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700313 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700314
315 // Short-circuit this logic if the icon exists somewhere on the workspace
316 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
317 continue;
318 }
319
320 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700321 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700322 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700323 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700324 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200325 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700326
327 // If we can't find a valid position, then just add a new screen.
328 // This takes time so we need to re-queue the add until the new
329 // page is added. Create as many screens as necessary to satisfy
330 // the startSearchPageIndex.
331 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700332 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700333 while (numPagesToAdd > 0) {
334 long screenId = lp.generateNewScreenId();
Adam Cohen4caf2982013-08-20 18:54:31 -0700335 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")", true);
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700337 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 addedWorkspaceScreensFinal.add(screenId);
339 numPagesToAdd--;
340 }
Winson Chung76828c82013-08-19 15:43:29 -0700341
Winson Chung64359a52013-07-08 17:17:08 -0700342 // Find the coordinate again
343 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700344 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700345 }
346 if (coords == null) {
347 throw new RuntimeException("Coordinates should not be null");
348 }
349
Winson Chung997a9232013-07-24 15:33:46 -0700350 ShortcutInfo shortcutInfo;
351 if (a instanceof ShortcutInfo) {
352 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200353 } else if (a instanceof AppInfo) {
354 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700355 } else {
356 throw new RuntimeException("Unexpected info type");
357 }
Winson Chung64359a52013-07-08 17:17:08 -0700358 // Add the shortcut to the db
359 addItemToDatabase(context, shortcutInfo,
360 LauncherSettings.Favorites.CONTAINER_DESKTOP,
361 coords.first, coords.second[0], coords.second[1], false);
362 // Save the ShortcutInfo for binding in the workspace
363 addedShortcutsFinal.add(shortcutInfo);
364 }
365 }
366
Adam Cohen4caf2982013-08-20 18:54:31 -0700367 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -0700368
Winson Chung76828c82013-08-19 15:43:29 -0700369 // Update the workspace screens
370 updateWorkspaceScreenOrder(context, workspaceScreens);
371
Winson Chung997a9232013-07-24 15:33:46 -0700372 if (!addedShortcutsFinal.isEmpty()) {
373 runOnMainThread(new Runnable() {
374 public void run() {
375 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
376 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700377 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
378 long lastScreenId = info.screenId;
379 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
380 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
381 for (ItemInfo i : addedShortcutsFinal) {
382 if (i.screenId == lastScreenId) {
383 addAnimated.add(i);
384 } else {
385 addNotAnimated.add(i);
386 }
387 }
Winson Chungd64d1762013-08-20 14:37:16 -0700388 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chungc58497e2013-09-03 17:48:37 -0700389 addNotAnimated, addAnimated, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 }
Winson Chung997a9232013-07-24 15:33:46 -0700392 });
393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
395 };
396 runOnWorkerThread(r);
397 }
398
Joe Onorato56d82912010-03-07 14:32:10 -0500399 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800400 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402
Winson Chung81b52252012-08-27 15:34:29 -0700403 public void unbindItemInfosAndClearQueuedBindRunnables() {
404 if (sWorkerThread.getThreadId() == Process.myTid()) {
405 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
406 "main thread");
407 }
408
409 // Clear any deferred bind runnables
410 mDeferredBindRunnables.clear();
411 // Remove any queued bind runnables
412 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
413 // Unbind all the workspace items
414 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700415 }
416
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700417 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700418 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700419 // Ensure that we don't use the same workspace items data structure on the main thread
420 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700421 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
422 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700423 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700424 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
425 tmpAppWidgets.addAll(sBgAppWidgets);
426 }
427 Runnable r = new Runnable() {
428 @Override
429 public void run() {
430 for (ItemInfo item : tmpWorkspaceItems) {
431 item.unbind();
432 }
433 for (ItemInfo item : tmpAppWidgets) {
434 item.unbind();
435 }
436 }
437 };
438 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700439 }
440
Joe Onorato9c1289c2009-08-17 11:03:03 -0400441 /**
442 * Adds an item to the DB if it was not created previously, or move it to a new
443 * <container, screen, cellX, cellY>
444 */
445 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700446 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400447 if (item.container == ItemInfo.NO_ID) {
448 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 } else {
451 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700452 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454 }
455
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700456 static void checkItemInfoLocked(
457 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
458 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
459 if (modelItem != null && item != modelItem) {
460 // check all the data is consistent
461 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
462 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
463 ShortcutInfo shortcut = (ShortcutInfo) item;
464 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
465 modelShortcut.intent.filterEquals(shortcut.intent) &&
466 modelShortcut.id == shortcut.id &&
467 modelShortcut.itemType == shortcut.itemType &&
468 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700469 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700470 modelShortcut.cellX == shortcut.cellX &&
471 modelShortcut.cellY == shortcut.cellY &&
472 modelShortcut.spanX == shortcut.spanX &&
473 modelShortcut.spanY == shortcut.spanY &&
474 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
475 (modelShortcut.dropPos != null &&
476 shortcut.dropPos != null &&
477 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
478 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
479 // For all intents and purposes, this is the same object
480 return;
481 }
482 }
483
484 // the modelItem needs to match up perfectly with item if our model is
485 // to be consistent with the database-- for now, just require
486 // modelItem == item or the equality check above
487 String msg = "item: " + ((item != null) ? item.toString() : "null") +
488 "modelItem: " +
489 ((modelItem != null) ? modelItem.toString() : "null") +
490 "Error: ItemInfo passed to checkItemInfo doesn't match original";
491 RuntimeException e = new RuntimeException(msg);
492 if (stackTrace != null) {
493 e.setStackTrace(stackTrace);
494 }
Adam Cohene25af792013-06-06 23:08:25 -0700495 // TODO: something breaks this in the upgrade path
496 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700497 }
498 }
499
Michael Jurka816474f2012-06-25 14:49:02 -0700500 static void checkItemInfo(final ItemInfo item) {
501 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
502 final long itemId = item.id;
503 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700504 public void run() {
505 synchronized (sBgLock) {
506 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700507 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700508 }
509 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700510 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700511 }
512
Michael Jurkac9d95c52011-08-29 14:03:34 -0700513 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
514 final ItemInfo item, final String callingFunction) {
515 final long itemId = item.id;
516 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
517 final ContentResolver cr = context.getContentResolver();
518
Adam Cohen487f7dd2012-06-28 18:12:10 -0700519 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700520 Runnable r = new Runnable() {
521 public void run() {
522 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700523 updateItemArrays(item, itemId, stackTrace);
524 }
525 };
526 runOnWorkerThread(r);
527 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700528
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700529 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
530 final ArrayList<ItemInfo> items, final String callingFunction) {
531 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700532
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700533 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
534 Runnable r = new Runnable() {
535 public void run() {
536 ArrayList<ContentProviderOperation> ops =
537 new ArrayList<ContentProviderOperation>();
538 int count = items.size();
539 for (int i = 0; i < count; i++) {
540 ItemInfo item = items.get(i);
541 final long itemId = item.id;
542 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
543 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700544
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700545 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
546 updateItemArrays(item, itemId, stackTrace);
547
548 }
549 try {
550 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
551 } catch (Exception e) {
552 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700553 }
554 }
555 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700556 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700557 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700558
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700559 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
560 // Lock on mBgLock *after* the db operation
561 synchronized (sBgLock) {
562 checkItemInfoLocked(itemId, item, stackTrace);
563
564 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
565 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
566 // Item is in a folder, make sure this folder exists
567 if (!sBgFolders.containsKey(item.container)) {
568 // An items container is being set to a that of an item which is not in
569 // the list of Folders.
570 String msg = "item: " + item + " container being set to: " +
571 item.container + ", not in the list of folders";
572 Log.e(TAG, msg);
573 Launcher.dumpDebugLogsToConsole();
574 }
575 }
576
577 // Items are added/removed from the corresponding FolderInfo elsewhere, such
578 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
579 // that are on the desktop, as appropriate
580 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
581 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
582 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
583 switch (modelItem.itemType) {
584 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
585 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
586 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
587 if (!sBgWorkspaceItems.contains(modelItem)) {
588 sBgWorkspaceItems.add(modelItem);
589 }
590 break;
591 default:
592 break;
593 }
594 } else {
595 sBgWorkspaceItems.remove(modelItem);
596 }
597 }
598 }
599
Michael Jurkac7700af2013-05-14 20:17:58 +0200600 public void flushWorkerThread() {
601 mFlushingWorkerThread = true;
602 Runnable waiter = new Runnable() {
603 public void run() {
604 synchronized (this) {
605 notifyAll();
606 mFlushingWorkerThread = false;
607 }
608 }
609 };
610
611 synchronized(waiter) {
612 runOnWorkerThread(waiter);
613 if (mLoaderTask != null) {
614 synchronized(mLoaderTask) {
615 mLoaderTask.notify();
616 }
617 }
618 boolean success = false;
619 while (!success) {
620 try {
621 waiter.wait();
622 success = true;
623 } catch (InterruptedException e) {
624 }
625 }
626 }
627 }
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700631 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700632 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700634 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
636 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700637 Launcher.addDumpLog(TAG, transaction, true);
638
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 item.cellX = cellX;
641 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700642
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 // We store hotseat items in canonical form which is this orientation invariant position
644 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651
652 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
655 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700656 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657
Michael Jurkac9d95c52011-08-29 14:03:34 -0700658 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700659 }
660
661 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700662 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
663 * cellX, cellY have already been updated on the ItemInfos.
664 */
665 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
666 final long container, final int screen) {
667
668 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
669 int count = items.size();
670
671 for (int i = 0; i < count; i++) {
672 ItemInfo item = items.get(i);
673 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700674 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
676 + item.cellX + ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700677 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678 item.container = container;
679
680 // We store hotseat items in canonical form which is this orientation invariant position
681 // in the hotseat
682 if (context instanceof Launcher && screen < 0 &&
683 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685 item.cellY);
686 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700687 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688 }
689
690 final ContentValues values = new ContentValues();
691 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
692 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
693 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700695
696 contentValues.add(values);
697 }
698 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
699 }
700
701 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800703 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700704 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700706 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
708 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700709 Launcher.addDumpLog(TAG, transaction, true);
710
Adam Cohend4844c32011-02-18 19:25:06 -0800711 item.cellX = cellX;
712 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 item.spanX = spanX;
714 item.spanY = spanY;
715
716 // We store hotseat items in canonical form which is this orientation invariant position
717 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700719 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700721 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700723 }
Adam Cohend4844c32011-02-18 19:25:06 -0800724
Adam Cohend4844c32011-02-18 19:25:06 -0800725 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800726 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700727 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
728 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
729 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
730 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700731 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800732
Michael Jurka816474f2012-06-25 14:49:02 -0700733 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700734 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700735
736 /**
737 * Update an item to the database in a specified container.
738 */
739 static void updateItemInDatabase(Context context, final ItemInfo item) {
740 final ContentValues values = new ContentValues();
741 item.onAddToDatabase(values);
742 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
743 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800744 }
745
746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 * Returns true if the shortcuts already exists in the database.
748 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 static boolean shortcutExists(Context context, String title, Intent intent) {
751 final ContentResolver cr = context.getContentResolver();
752 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
753 new String[] { "title", "intent" }, "title=? and intent=?",
754 new String[] { title, intent.toUri(0) }, null);
755 boolean result = false;
756 try {
757 result = c.moveToFirst();
758 } finally {
759 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700762 }
763
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700765 * Returns an ItemInfo array containing all the items in the LauncherModel.
766 * The ItemInfo.id is not set through this function.
767 */
768 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
769 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
770 final ContentResolver cr = context.getContentResolver();
771 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
772 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
773 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
774 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
775
776 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
777 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
778 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
779 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
780 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
781 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
782 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
783
784 try {
785 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700786 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700787 item.cellX = c.getInt(cellXIndex);
788 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700789 item.spanX = Math.max(1, c.getInt(spanXIndex));
790 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700791 item.container = c.getInt(containerIndex);
792 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700793 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700794
795 items.add(item);
796 }
797 } catch (Exception e) {
798 items.clear();
799 } finally {
800 c.close();
801 }
802
803 return items;
804 }
805
806 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
808 */
809 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
810 final ContentResolver cr = context.getContentResolver();
811 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
812 "_id=? and (itemType=? or itemType=?)",
813 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700814 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700815
Joe Onorato9c1289c2009-08-17 11:03:03 -0400816 try {
817 if (c.moveToFirst()) {
818 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
819 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
820 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
821 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
822 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
823 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 FolderInfo folderInfo = null;
826 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700827 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
828 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700830 }
831
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832 folderInfo.title = c.getString(titleIndex);
833 folderInfo.id = id;
834 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700835 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700836 folderInfo.cellX = c.getInt(cellXIndex);
837 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838
839 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 } finally {
842 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700843 }
844
845 return null;
846 }
847
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 /**
849 * Add an item to the database in a specified container. Sets the container, screen, cellX and
850 * cellY fields of the item. Also assigns an ID to the item.
851 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 item.cellX = cellX;
856 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 // We store hotseat items in canonical form which is this orientation invariant position
858 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700859 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865
866 final ContentValues values = new ContentValues();
867 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868 item.onAddToDatabase(values);
869
Michael Jurka414300a2013-08-27 15:42:35 +0200870 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700871 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700873
Michael Jurkac9d95c52011-08-29 14:03:34 -0700874 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700875 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700876 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700878 + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700879 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700880
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700881 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
882 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700885 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700886 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 sBgItemsIdMap.put(item.id, item);
888 switch (item.itemType) {
889 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
890 sBgFolders.put(item.id, (FolderInfo) item);
891 // Fall through
892 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
893 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
894 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
895 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
896 sBgWorkspaceItems.add(item);
897 } else {
898 if (!sBgFolders.containsKey(item.container)) {
899 // Adding an item to a folder that doesn't exist.
900 String msg = "adding item: " + item + " to a folder that " +
901 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700902 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700905 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700906 break;
907 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
908 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
909 break;
910 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700911 }
912 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700914 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700915 }
916
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700918 * Creates a new unique child id, for a given cell span across all layouts.
919 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700920 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700922 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700924 }
925
Winson Chungaafa03c2010-06-11 17:34:16 -0700926 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700927 * Removes the specified item from the database
928 * @param context
929 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700931 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700933 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700934
Michael Jurka83df1882011-08-31 20:59:26 -0700935 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700936 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700937 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700939 ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700940 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700941
Michael Jurkac9d95c52011-08-29 14:03:34 -0700942 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700943
944 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700945 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700946 switch (item.itemType) {
947 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
948 sBgFolders.remove(item.id);
949 for (ItemInfo info: sBgItemsIdMap.values()) {
950 if (info.container == item.id) {
951 // We are deleting a folder which still contains items that
952 // think they are contained by that folder.
953 String msg = "deleting a folder (" + item + ") which still " +
954 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700955 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700958 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700959 sBgWorkspaceItems.remove(item);
960 break;
961 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
962 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
963 sBgWorkspaceItems.remove(item);
964 break;
965 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
966 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
967 break;
968 }
969 sBgItemsIdMap.remove(item.id);
970 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700971 }
972 }
Michael Jurka83df1882011-08-31 20:59:26 -0700973 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700974 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 }
976
977 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700978 * Update the order of the workspace screens in the database. The array list contains
979 * a list of screen ids in the order that they should appear.
980 */
Winson Chungc9168342013-06-26 14:54:55 -0700981 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700982 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder()", true);
Winson Chung64359a52013-07-08 17:17:08 -0700983 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700984 final ContentResolver cr = context.getContentResolver();
985 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
986
987 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700988 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 while (iter.hasNext()) {
990 long id = iter.next();
991 if (id < 0) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700992 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 iter.remove();
994 }
995 }
996
Winson Chung9e6a0a22013-08-27 11:58:12 -0700997 // Dump the screens copy
Adam Cohen4caf2982013-08-20 18:54:31 -0700998 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700999 for (Long l : screensCopy) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001000 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001001 }
1002
Adam Cohendcd297f2013-06-18 13:13:40 -07001003 Runnable r = new Runnable() {
1004 @Override
1005 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 // Clear the table
1007 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001008 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001009 ContentValues[] values = new ContentValues[count];
1010 for (int i = 0; i < count; i++) {
1011 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001012 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001013 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1014 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohen4caf2982013-08-20 18:54:31 -07001015 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001016 values[i] = v;
1017 }
1018 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001019
Winson Chungba9c37f2013-08-30 14:11:37 -07001020 synchronized (sBgLock) {
1021 // Dump the sBgWorkspaceScreens
1022 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre clear", true);
1023 for (Long l : sBgWorkspaceScreens) {
1024 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1025 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001026
Winson Chungba9c37f2013-08-30 14:11:37 -07001027 sBgWorkspaceScreens.clear();
1028 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001029
Winson Chungba9c37f2013-08-30 14:11:37 -07001030 // Dump the sBgWorkspaceScreens
1031 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post clear", true);
1032 for (Long l : sBgWorkspaceScreens) {
1033 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1034 }
Adam Cohen4caf2982013-08-20 18:54:31 -07001035 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001036 }
1037 };
1038 runOnWorkerThread(r);
1039 }
1040
1041 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 * Remove the contents of the specified folder from the database
1043 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001044 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 final ContentResolver cr = context.getContentResolver();
1046
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 Runnable r = new Runnable() {
1048 public void run() {
1049 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001050 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001051 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001052 sBgItemsIdMap.remove(info.id);
1053 sBgFolders.remove(info.id);
1054 sBgDbIconCache.remove(info);
1055 sBgWorkspaceItems.remove(info);
1056 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001057
Michael Jurkac9d95c52011-08-29 14:03:34 -07001058 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1059 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001060 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001061 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001062 for (ItemInfo childInfo : info.contents) {
1063 sBgItemsIdMap.remove(childInfo.id);
1064 sBgDbIconCache.remove(childInfo);
1065 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001066 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001067 }
1068 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001069 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 }
1071
1072 /**
1073 * Set this as the current Launcher activity object for the loader.
1074 */
1075 public void initialize(Callbacks callbacks) {
1076 synchronized (mLock) {
1077 mCallbacks = new WeakReference<Callbacks>(callbacks);
1078 }
1079 }
1080
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001081 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1083 * ACTION_PACKAGE_CHANGED.
1084 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001085 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001086 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001087 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001088
Joe Onorato36115782010-06-17 13:28:48 -04001089 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001090
Joe Onorato36115782010-06-17 13:28:48 -04001091 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1092 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1093 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1094 final String packageName = intent.getData().getSchemeSpecificPart();
1095 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096
Joe Onorato36115782010-06-17 13:28:48 -04001097 int op = PackageUpdatedTask.OP_NONE;
1098
1099 if (packageName == null || packageName.length() == 0) {
1100 // they sent us a bad intent
1101 return;
1102 }
1103
1104 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1105 op = PackageUpdatedTask.OP_UPDATE;
1106 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1107 if (!replacing) {
1108 op = PackageUpdatedTask.OP_REMOVE;
1109 }
1110 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1111 // later, we will update the package at this time
1112 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1113 if (!replacing) {
1114 op = PackageUpdatedTask.OP_ADD;
1115 } else {
1116 op = PackageUpdatedTask.OP_UPDATE;
1117 }
1118 }
1119
1120 if (op != PackageUpdatedTask.OP_NONE) {
1121 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1122 }
1123
1124 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001125 // First, schedule to add these apps back in.
1126 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1127 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1128 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001129 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001130 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1131 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1132 enqueuePackageUpdated(new PackageUpdatedTask(
1133 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001134 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001135 // If we have changed locale we need to clear out the labels in all apps/workspace.
1136 forceReload();
1137 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1138 // Check if configuration change was an mcc/mnc change which would affect app resources
1139 // and we would need to clear out the labels in all apps/workspace. Same handling as
1140 // above for ACTION_LOCALE_CHANGED
1141 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001142 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001143 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001144 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001145 forceReload();
1146 }
1147 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001148 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001149 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1150 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001151 if (mCallbacks != null) {
1152 Callbacks callbacks = mCallbacks.get();
1153 if (callbacks != null) {
1154 callbacks.bindSearchablesChanged();
1155 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001156 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001157 }
1158 }
1159
Reena Lee93f824a2011-09-23 17:20:28 -07001160 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001161 resetLoadedState(true, true);
1162
Reena Lee93f824a2011-09-23 17:20:28 -07001163 // Do this here because if the launcher activity is running it will be restarted.
1164 // If it's not running startLoaderFromBackground will merely tell it that it needs
1165 // to reload.
1166 startLoaderFromBackground();
1167 }
1168
Winson Chungf0c6ae02012-03-21 16:10:31 -07001169 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1170 synchronized (mLock) {
1171 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1172 // mWorkspaceLoaded to true later
1173 stopLoaderLocked();
1174 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1175 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1176 }
1177 }
1178
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001179 /**
1180 * When the launcher is in the background, it's possible for it to miss paired
1181 * configuration changes. So whenever we trigger the loader from the background
1182 * tell the launcher that it needs to re-run the loader when it comes back instead
1183 * of doing it now.
1184 */
1185 public void startLoaderFromBackground() {
1186 boolean runLoader = false;
1187 if (mCallbacks != null) {
1188 Callbacks callbacks = mCallbacks.get();
1189 if (callbacks != null) {
1190 // Only actually run the loader if they're not paused.
1191 if (!callbacks.setLoadOnResume()) {
1192 runLoader = true;
1193 }
1194 }
1195 }
1196 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001197 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001198 }
Joe Onorato36115782010-06-17 13:28:48 -04001199 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001200
Reena Lee93f824a2011-09-23 17:20:28 -07001201 // If there is already a loader task running, tell it to stop.
1202 // returns true if isLaunching() was true on the old task
1203 private boolean stopLoaderLocked() {
1204 boolean isLaunching = false;
1205 LoaderTask oldTask = mLoaderTask;
1206 if (oldTask != null) {
1207 if (oldTask.isLaunching()) {
1208 isLaunching = true;
1209 }
1210 oldTask.stopLocked();
1211 }
1212 return isLaunching;
1213 }
1214
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001215 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001216 synchronized (mLock) {
1217 if (DEBUG_LOADERS) {
1218 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001221 // Clear any deferred bind-runnables from the synchronized load process
1222 // We must do this before any loading/binding is scheduled below.
1223 mDeferredBindRunnables.clear();
1224
Joe Onorato36115782010-06-17 13:28:48 -04001225 // Don't bother to start the thread if we know it's not going to do anything
1226 if (mCallbacks != null && mCallbacks.get() != null) {
1227 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001228 // also, don't downgrade isLaunching if we're already running
1229 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001230 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001231 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1232 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1233 } else {
1234 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1235 sWorker.post(mLoaderTask);
1236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 }
1239 }
1240
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001241 void bindRemainingSynchronousPages() {
1242 // Post the remaining side pages to be loaded
1243 if (!mDeferredBindRunnables.isEmpty()) {
1244 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001245 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001246 }
1247 mDeferredBindRunnables.clear();
1248 }
1249 }
1250
Joe Onorato36115782010-06-17 13:28:48 -04001251 public void stopLoader() {
1252 synchronized (mLock) {
1253 if (mLoaderTask != null) {
1254 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 }
1256 }
Joe Onorato36115782010-06-17 13:28:48 -04001257 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258
Winson Chung76828c82013-08-19 15:43:29 -07001259 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1260 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1261 final ContentResolver contentResolver = context.getContentResolver();
1262 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1263 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1264 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1265
1266 try {
1267 final int idIndex = sc.getColumnIndexOrThrow(
1268 LauncherSettings.WorkspaceScreens._ID);
1269 final int rankIndex = sc.getColumnIndexOrThrow(
1270 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1271 while (sc.moveToNext()) {
1272 try {
1273 long screenId = sc.getLong(idIndex);
1274 int rank = sc.getInt(rankIndex);
1275
Adam Cohen4caf2982013-08-20 18:54:31 -07001276 Launcher.addDumpLog(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")", true);
Winson Chung76828c82013-08-19 15:43:29 -07001277
1278 orderedScreens.put(rank, screenId);
1279 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001280 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001281 }
1282 }
1283 } finally {
1284 sc.close();
1285 }
1286 return orderedScreens;
1287 }
1288
Michael Jurkac57b7a82011-08-09 22:02:20 -07001289 public boolean isAllAppsLoaded() {
1290 return mAllAppsLoaded;
1291 }
1292
Winson Chung36a62fe2012-05-06 18:04:42 -07001293 boolean isLoadingWorkspace() {
1294 synchronized (mLock) {
1295 if (mLoaderTask != null) {
1296 return mLoaderTask.isLoadingWorkspace();
1297 }
1298 }
1299 return false;
1300 }
1301
Joe Onorato36115782010-06-17 13:28:48 -04001302 /**
1303 * Runnable for the thread that loads the contents of the launcher:
1304 * - workspace icons
1305 * - widgets
1306 * - all apps icons
1307 */
1308 private class LoaderTask implements Runnable {
1309 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001310 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001311 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001312 private boolean mStopped;
1313 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001314
Winson Chungc3eecff2011-07-11 17:44:15 -07001315 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001316
1317 LoaderTask(Context context, boolean isLaunching) {
1318 mContext = context;
1319 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001320 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 }
1322
Joe Onorato36115782010-06-17 13:28:48 -04001323 boolean isLaunching() {
1324 return mIsLaunching;
1325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326
Winson Chung36a62fe2012-05-06 18:04:42 -07001327 boolean isLoadingWorkspace() {
1328 return mIsLoadingAndBindingWorkspace;
1329 }
1330
Winson Chungc763c4e2013-07-19 13:49:06 -07001331 /** Returns whether this is an upgrade path */
1332 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001333 mIsLoadingAndBindingWorkspace = true;
1334
Joe Onorato36115782010-06-17 13:28:48 -04001335 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001336 if (DEBUG_LOADERS) {
1337 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001338 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001339
Winson Chungc763c4e2013-07-19 13:49:06 -07001340 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001341 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001342 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001343 synchronized (LoaderTask.this) {
1344 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001345 Launcher.addDumpLog(TAG, "10249126 - loadAndBindWorkspace() stopped", true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001346 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001347 }
1348 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 }
1350 }
1351
Joe Onorato36115782010-06-17 13:28:48 -04001352 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001353 bindWorkspace(-1, isUpgradePath);
1354 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001355 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001356
Joe Onorato36115782010-06-17 13:28:48 -04001357 private void waitForIdle() {
1358 // Wait until the either we're stopped or the other threads are done.
1359 // This way we don't start loading all apps until the workspace has settled
1360 // down.
1361 synchronized (LoaderTask.this) {
1362 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001363
Joe Onorato36115782010-06-17 13:28:48 -04001364 mHandler.postIdle(new Runnable() {
1365 public void run() {
1366 synchronized (LoaderTask.this) {
1367 mLoadAndBindStepFinished = true;
1368 if (DEBUG_LOADERS) {
1369 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001370 }
Joe Onorato36115782010-06-17 13:28:48 -04001371 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001372 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001373 }
Joe Onorato36115782010-06-17 13:28:48 -04001374 });
1375
Michael Jurkac7700af2013-05-14 20:17:58 +02001376 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001377 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001378 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1379 // wait no longer than 1sec at a time
1380 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001381 } catch (InterruptedException ex) {
1382 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001383 }
1384 }
Joe Onorato36115782010-06-17 13:28:48 -04001385 if (DEBUG_LOADERS) {
1386 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001387 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001388 + "ms for previous step to finish binding");
1389 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 }
Joe Onorato36115782010-06-17 13:28:48 -04001391 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001392
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 void runBindSynchronousPage(int synchronousBindPage) {
1394 if (synchronousBindPage < 0) {
1395 // Ensure that we have a valid page index to load synchronously
1396 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1397 "valid page index");
1398 }
1399 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1400 // Ensure that we don't try and bind a specified page when the pages have not been
1401 // loaded already (we should load everything asynchronously in that case)
1402 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1403 }
1404 synchronized (mLock) {
1405 if (mIsLoaderTaskRunning) {
1406 // Ensure that we are never running the background loading at this point since
1407 // we also touch the background collections
1408 throw new RuntimeException("Error! Background loading is already running");
1409 }
1410 }
1411
1412 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1413 // data structures, we can't allow any other thread to touch that data, but because
1414 // this call is synchronous, we can get away with not locking).
1415
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001416 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001417 // operations from the previous activity. We need to ensure that all queued operations
1418 // are executed before any synchronous binding work is done.
1419 mHandler.flush();
1420
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001421 // Divide the set of loaded items into those that we are binding synchronously, and
1422 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001423 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001424 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1425 // arise from that.
1426 onlyBindAllApps();
1427 }
1428
Joe Onorato36115782010-06-17 13:28:48 -04001429 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001430 boolean isUpgrade = false;
1431
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001432 synchronized (mLock) {
1433 mIsLoaderTaskRunning = true;
1434 }
Joe Onorato36115782010-06-17 13:28:48 -04001435 // Optimize for end-user experience: if the Launcher is up and // running with the
1436 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1437 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001438 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001439 // Elevate priority when Home launches for the first time to avoid
1440 // starving at boot time. Staring at a blank home is not cool.
1441 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001442 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1443 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001444 android.os.Process.setThreadPriority(mIsLaunching
1445 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1446 }
Winson Chung64359a52013-07-08 17:17:08 -07001447 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001448 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001449
Joe Onorato36115782010-06-17 13:28:48 -04001450 if (mStopped) {
1451 break keep_running;
1452 }
1453
1454 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1455 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001456 synchronized (mLock) {
1457 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001458 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001459 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1460 }
1461 }
Joe Onorato36115782010-06-17 13:28:48 -04001462 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001463
1464 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001465 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1466 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001467
1468 // Restore the default thread priority after we are done loading items
1469 synchronized (mLock) {
1470 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1471 }
Joe Onorato36115782010-06-17 13:28:48 -04001472 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473
Winson Chungaac01e12011-08-17 10:37:13 -07001474 // Update the saved icons if necessary
1475 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001476 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001477 for (Object key : sBgDbIconCache.keySet()) {
1478 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1479 }
1480 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001481 }
Winson Chungaac01e12011-08-17 10:37:13 -07001482
Winson Chungc58497e2013-09-03 17:48:37 -07001483 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1484 // Ensure that all the applications that are in the system are
1485 // represented on the home screen.
1486 Launcher.addDumpLog(TAG, "10249126 - verifyApplications - useMoreApps="
1487 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade, true);
1488 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1489 Launcher.addDumpLog(TAG, "10249126 - verifyApplications(" + isUpgrade + ")", true);
1490 verifyApplications();
1491 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001492 }
1493
Joe Onorato36115782010-06-17 13:28:48 -04001494 // Clear out this reference, otherwise we end up holding it until all of the
1495 // callback runnables are done.
1496 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497
Joe Onorato36115782010-06-17 13:28:48 -04001498 synchronized (mLock) {
1499 // If we are still the last one to be scheduled, remove ourselves.
1500 if (mLoaderTask == this) {
1501 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001503 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001504 }
Joe Onorato36115782010-06-17 13:28:48 -04001505 }
1506
1507 public void stopLocked() {
1508 synchronized (LoaderTask.this) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001509 Launcher.addDumpLog(TAG, "10249126 - STOPPED", true);
Joe Onorato36115782010-06-17 13:28:48 -04001510 mStopped = true;
1511 this.notify();
1512 }
1513 }
1514
1515 /**
1516 * Gets the callbacks object. If we've been stopped, or if the launcher object
1517 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1518 * object that was around when the deferred message was scheduled, and if there's
1519 * a new Callbacks object around then also return null. This will save us from
1520 * calling onto it with data that will be ignored.
1521 */
1522 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1523 synchronized (mLock) {
1524 if (mStopped) {
1525 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001526 }
Joe Onorato36115782010-06-17 13:28:48 -04001527
1528 if (mCallbacks == null) {
1529 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001530 }
Joe Onorato36115782010-06-17 13:28:48 -04001531
1532 final Callbacks callbacks = mCallbacks.get();
1533 if (callbacks != oldCallbacks) {
1534 return null;
1535 }
1536 if (callbacks == null) {
1537 Log.w(TAG, "no mCallbacks");
1538 return null;
1539 }
1540
1541 return callbacks;
1542 }
1543 }
1544
Winson Chungc763c4e2013-07-19 13:49:06 -07001545 private void verifyApplications() {
1546 final Context context = mApp.getContext();
1547
1548 // Cross reference all the applications in our apps list with items in the workspace
1549 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001550 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001551 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001552 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001553 tmpInfos = getItemInfoForComponentName(app.componentName);
Adam Cohen4caf2982013-08-20 18:54:31 -07001554 Launcher.addDumpLog(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty(), true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001555 if (tmpInfos.isEmpty()) {
1556 // We are missing an application icon, so add this to the workspace
1557 added.add(app);
1558 // This is a rare event, so lets log it
1559 Log.e(TAG, "Missing Application on load: " + app);
1560 }
1561 }
1562 }
1563 if (!added.isEmpty()) {
1564 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001565 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001566 }
1567 }
1568
Winson Chung5f8afe62013-08-12 16:19:28 -07001569 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001570 LauncherAppState app = LauncherAppState.getInstance();
1571 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1572 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1573 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001574 }
1575
Joe Onorato36115782010-06-17 13:28:48 -04001576 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001578 LauncherAppState app = LauncherAppState.getInstance();
1579 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1580 int countX = (int) grid.numColumns;
1581 int countY = (int) grid.numRows;
1582
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1586 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1587 [(int) item.screenId][0] != null) {
1588 Log.e(TAG, "Error loading shortcut into hotseat " + item
1589 + " into position (" + item.screenId + ":" + item.cellX + ","
1590 + item.cellY + ") occupied by "
1591 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1592 [(int) item.screenId][0]);
1593 return false;
1594 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001595 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001596 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 items[(int) item.screenId][0] = item;
1598 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001599 return true;
1600 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001601 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1602 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001603 return true;
1604 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001605
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001607 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001608 occupied.put(item.screenId, items);
1609 }
1610
1611 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001612 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001613 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1614 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001615 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001616 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001617 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001618 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001619 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001620 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001621 return false;
1622 }
1623 }
1624 }
1625 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1626 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001627 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001628 }
1629 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001630
Joe Onorato36115782010-06-17 13:28:48 -04001631 return true;
1632 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001633
Winson Chungba9c37f2013-08-30 14:11:37 -07001634 /** Clears all the sBg data structures */
1635 private void clearSBgDataStructures() {
1636 synchronized (sBgLock) {
1637 sBgWorkspaceItems.clear();
1638 sBgAppWidgets.clear();
1639 sBgFolders.clear();
1640 sBgItemsIdMap.clear();
1641 sBgDbIconCache.clear();
1642 sBgWorkspaceScreens.clear();
1643 }
1644 }
1645
Winson Chungc763c4e2013-07-19 13:49:06 -07001646 /** Returns whether this is an upgradge path */
1647 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001648 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001649
Joe Onorato36115782010-06-17 13:28:48 -04001650 final Context context = mContext;
1651 final ContentResolver contentResolver = context.getContentResolver();
1652 final PackageManager manager = context.getPackageManager();
1653 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1654 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001655
Winson Chung892c74d2013-08-22 16:15:50 -07001656 LauncherAppState app = LauncherAppState.getInstance();
1657 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1658 int countX = (int) grid.numColumns;
1659 int countY = (int) grid.numRows;
1660
Michael Jurkab85f8a42012-04-25 15:48:32 -07001661 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001662 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001663
Winson Chungc763c4e2013-07-19 13:49:06 -07001664 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001665 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001666
Winson Chung2abf94d2012-07-18 18:16:38 -07001667 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001668 clearSBgDataStructures();
Adam Cohen4caf2982013-08-20 18:54:31 -07001669 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace()", true);
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;
Adam Cohene25af792013-06-06 23:08:25 -07001673 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001674
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 // +1 for the hotseat (it can be larger than the workspace)
1676 // Load workspace in reverse order to ensure that latest items are loaded first (and
1677 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001678 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 try {
1681 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1682 final int intentIndex = c.getColumnIndexOrThrow
1683 (LauncherSettings.Favorites.INTENT);
1684 final int titleIndex = c.getColumnIndexOrThrow
1685 (LauncherSettings.Favorites.TITLE);
1686 final int iconTypeIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.ICON_TYPE);
1688 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1689 final int iconPackageIndex = c.getColumnIndexOrThrow(
1690 LauncherSettings.Favorites.ICON_PACKAGE);
1691 final int iconResourceIndex = c.getColumnIndexOrThrow(
1692 LauncherSettings.Favorites.ICON_RESOURCE);
1693 final int containerIndex = c.getColumnIndexOrThrow(
1694 LauncherSettings.Favorites.CONTAINER);
1695 final int itemTypeIndex = c.getColumnIndexOrThrow(
1696 LauncherSettings.Favorites.ITEM_TYPE);
1697 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1698 LauncherSettings.Favorites.APPWIDGET_ID);
1699 final int screenIndex = c.getColumnIndexOrThrow(
1700 LauncherSettings.Favorites.SCREEN);
1701 final int cellXIndex = c.getColumnIndexOrThrow
1702 (LauncherSettings.Favorites.CELLX);
1703 final int cellYIndex = c.getColumnIndexOrThrow
1704 (LauncherSettings.Favorites.CELLY);
1705 final int spanXIndex = c.getColumnIndexOrThrow
1706 (LauncherSettings.Favorites.SPANX);
1707 final int spanYIndex = c.getColumnIndexOrThrow(
1708 LauncherSettings.Favorites.SPANY);
1709 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1710 //final int displayModeIndex = c.getColumnIndexOrThrow(
1711 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001713 ShortcutInfo info;
1714 String intentDescription;
1715 LauncherAppWidgetInfo appWidgetInfo;
1716 int container;
1717 long id;
1718 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001719
Winson Chungba9c37f2013-08-30 14:11:37 -07001720 Launcher.addDumpLog(TAG, "10249126 - Num rows: " + c.getCount(), true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 while (!mStopped && c.moveToNext()) {
1722 try {
1723 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 switch (itemType) {
1726 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1727 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001728 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 intentDescription = c.getString(intentIndex);
1730 try {
1731 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001732 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001733 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001734 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001735 // Log the invalid package, and remove it from the db
1736 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1737 false);
Winson Chungee055712013-07-30 14:46:24 -07001738 contentResolver.delete(uri, null, null);
Winson Chung933bae62013-08-29 11:42:30 -07001739 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001740 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001741 // If apps can be on external storage, then we just
1742 // leave them for the user to remove (maybe add
1743 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001744 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001745 }
1746 continue;
1747 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001749 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001750 continue;
1751 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1754 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1755 titleIndex, mLabelCache);
1756 } else {
1757 info = getShortcutInfo(c, context, iconTypeIndex,
1758 iconPackageIndex, iconResourceIndex, iconIndex,
1759 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001760
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 // App shortcuts that used to be automatically added to Launcher
1762 // didn't always have the correct intent flags set, so do that
1763 // here
1764 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001765 intent.getCategories() != null &&
1766 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001767 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 intent.addFlags(
1769 Intent.FLAG_ACTIVITY_NEW_TASK |
1770 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1771 }
Michael Jurka96879562012-03-22 05:54:33 -07001772 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001774 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001775 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 container = c.getInt(containerIndex);
1778 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 info.cellX = c.getInt(cellXIndex);
1781 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001782 info.spanX = 1;
1783 info.spanY = 1;
1784 // Skip loading items that are out of bounds
1785 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1786 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001787 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1788 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001789 continue;
1790 }
1791 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 // check & update map of what's occupied
1793 if (!checkItemPlacement(occupied, info)) {
1794 break;
1795 }
1796
1797 switch (container) {
1798 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1799 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1800 sBgWorkspaceItems.add(info);
1801 break;
1802 default:
1803 // Item is in a user folder
1804 FolderInfo folderInfo =
1805 findOrMakeFolder(sBgFolders, container);
1806 folderInfo.add(info);
1807 break;
1808 }
1809 sBgItemsIdMap.put(info.id, info);
1810
1811 // now that we've loaded everthing re-save it with the
1812 // icon in case it disappears somehow.
1813 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001814 } else {
1815 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001816 }
1817 break;
1818
1819 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1820 id = c.getLong(idIndex);
1821 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1822
1823 folderInfo.title = c.getString(titleIndex);
1824 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001827 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 folderInfo.cellX = c.getInt(cellXIndex);
1829 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001830 folderInfo.spanX = 1;
1831 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832
Winson Chung5f8afe62013-08-12 16:19:28 -07001833 // Skip loading items that are out of bounds
1834 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001835 if (checkItemDimensions(folderInfo)) {
1836 Log.d(TAG, "Skipped loading out of bounds folder");
1837 continue;
1838 }
1839 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001840 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001841 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001842 break;
1843 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001844
Joe Onorato9c1289c2009-08-17 11:03:03 -04001845 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1847 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1848 sBgWorkspaceItems.add(folderInfo);
1849 break;
Joe Onorato36115782010-06-17 13:28:48 -04001850 }
Joe Onorato17a89222011-02-08 17:26:11 -08001851
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001852 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1853 sBgFolders.put(folderInfo.id, folderInfo);
1854 break;
1855
1856 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1857 // Read all Launcher-specific widget details
1858 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001859 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001860
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001861 final AppWidgetProviderInfo provider =
1862 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001863
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 if (!isSafeMode && (provider == null || provider.provider == null ||
1865 provider.provider.getPackageName() == null)) {
1866 String log = "Deleting widget that isn't installed anymore: id="
1867 + id + " appWidgetId=" + appWidgetId;
1868 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001869 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 itemsToRemove.add(id);
1871 } else {
1872 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1873 provider.provider);
1874 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001875 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 appWidgetInfo.cellX = c.getInt(cellXIndex);
1877 appWidgetInfo.cellY = c.getInt(cellYIndex);
1878 appWidgetInfo.spanX = c.getInt(spanXIndex);
1879 appWidgetInfo.spanY = c.getInt(spanYIndex);
1880 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1881 appWidgetInfo.minSpanX = minSpan[0];
1882 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001883
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001884 container = c.getInt(containerIndex);
1885 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1886 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1887 Log.e(TAG, "Widget found where container != " +
1888 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1889 continue;
1890 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001891
Adam Cohene25af792013-06-06 23:08:25 -07001892 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001893 // Skip loading items that are out of bounds
1894 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1895 if (checkItemDimensions(appWidgetInfo)) {
1896 Log.d(TAG, "Skipped loading out of bounds app widget");
1897 continue;
1898 }
1899 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001900 // check & update map of what's occupied
1901 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1902 break;
1903 }
1904 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1905 sBgAppWidgets.add(appWidgetInfo);
1906 }
Joe Onorato36115782010-06-17 13:28:48 -04001907 break;
1908 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001910 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001911 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001912 }
1913 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001914 if (c != null) {
1915 c.close();
1916 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001917 }
1918
Winson Chungba9c37f2013-08-30 14:11:37 -07001919 // Break early if we've stopped loading
1920 if (mStopped) {
1921 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace() - Stopped", true);
1922 clearSBgDataStructures();
1923 return false;
1924 }
1925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 if (itemsToRemove.size() > 0) {
1927 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001928 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 // Remove dead items
1930 for (long id : itemsToRemove) {
1931 if (DEBUG_LOADERS) {
1932 Log.d(TAG, "Removed id = " + id);
1933 }
1934 // Don't notify content observers
1935 try {
1936 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1937 null, null);
1938 } catch (RemoteException e) {
1939 Log.w(TAG, "Could not remove id = " + id);
1940 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001941 }
1942 }
1943
Winson Chungc763c4e2013-07-19 13:49:06 -07001944 if (loadedOldDb) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001945 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - loadedOldDb", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 long maxScreenId = 0;
1947 // If we're importing we use the old screen order.
1948 for (ItemInfo item: sBgItemsIdMap.values()) {
1949 long screenId = item.screenId;
1950 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1951 !sBgWorkspaceScreens.contains(screenId)) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001952 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 sBgWorkspaceScreens.add(screenId);
1954 if (screenId > maxScreenId) {
1955 maxScreenId = screenId;
1956 }
1957 }
1958 }
1959 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001960
1961 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001962 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001963 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001964 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001965 }
1966
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 Chung933bae62013-08-29 11:42:30 -07001978 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - !loadedOldDb [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Winson Chung76828c82013-08-19 15:43:29 -07001979 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1980 for (Integer i : orderedScreens.keySet()) {
Winson Chung933bae62013-08-29 11:42:30 -07001981 Launcher.addDumpLog(TAG, "10249126 - adding to sBgWorkspaceScreens: " + orderedScreens.get(i), true);
Winson Chung76828c82013-08-19 15:43:29 -07001982 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001983 }
1984
1985 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001986 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
1987 for (Long l : unusedScreens) {
1988 Launcher.addDumpLog(TAG, "10249126 - unused screens: " + l, true);
1989 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001990
Winson Chung933bae62013-08-29 11:42:30 -07001991 Launcher.addDumpLog(TAG, "10249126 - sBgItemsIdMap [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 for (ItemInfo item: sBgItemsIdMap.values()) {
1993 long screenId = item.screenId;
Winson Chung933bae62013-08-29 11:42:30 -07001994 Launcher.addDumpLog(TAG, "10249126 - \t" + item.container + ", " + screenId + " - " + unusedScreens.contains(screenId) + " | " + item, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001995
1996 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1997 unusedScreens.contains(screenId)) {
1998 unusedScreens.remove(screenId);
Winson Chung933bae62013-08-29 11:42:30 -07001999 Launcher.addDumpLog(TAG, "10249126 - \t\tRemoving " + screenId, true);
2000 for (Long l : unusedScreens) {
2001 Launcher.addDumpLog(TAG, "10249126 - \t\t\t unused screens: " + l, true);
2002 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002003 }
2004 }
2005
2006 // If there are any empty screens remove them, and update.
2007 if (unusedScreens.size() != 0) {
Winson Chung9e6a0a22013-08-27 11:58:12 -07002008 // Dump the sBgWorkspaceScreens
Winson Chung933bae62013-08-29 11:42:30 -07002009 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre removeAll", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002010 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002011 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002012 }
Winson Chung933bae62013-08-29 11:42:30 -07002013
2014 sBgWorkspaceScreens.removeAll(unusedScreens);
2015
2016 // Dump the sBgWorkspaceScreens
2017 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post removeAll", true);
2018 for (Long l : sBgWorkspaceScreens) {
2019 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2020 }
2021
Adam Cohendcd297f2013-06-18 13:13:40 -07002022 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2023 }
2024 }
2025
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 if (DEBUG_LOADERS) {
2027 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2028 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002030 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002032
Daniel Sandler566da102013-06-25 23:43:45 -04002033 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002034 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002035 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002036 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002037 line += " | ";
2038 }
Winson Chung892c74d2013-08-22 16:15:50 -07002039 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002040 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 }
Joe Onorato36115782010-06-17 13:28:48 -04002042 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002043 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002045 }
Joe Onorato36115782010-06-17 13:28:48 -04002046 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002047 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002048 }
2049
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002050 /** Filters the set of items who are directly or indirectly (via another container) on the
2051 * specified screen. */
2052 private void filterCurrentWorkspaceItems(int currentScreen,
2053 ArrayList<ItemInfo> allWorkspaceItems,
2054 ArrayList<ItemInfo> currentScreenItems,
2055 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002056 // Purge any null ItemInfos
2057 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2058 while (iter.hasNext()) {
2059 ItemInfo i = iter.next();
2060 if (i == null) {
2061 iter.remove();
2062 }
2063 }
2064
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002065 // If we aren't filtering on a screen, then the set of items to load is the full set of
2066 // items given.
2067 if (currentScreen < 0) {
2068 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002069 }
2070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 // Order the set of items by their containers first, this allows use to walk through the
2072 // list sequentially, build up a list of containers that are in the specified screen,
2073 // as well as all items in those containers.
2074 Set<Long> itemsOnScreen = new HashSet<Long>();
2075 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2076 @Override
2077 public int compare(ItemInfo lhs, ItemInfo rhs) {
2078 return (int) (lhs.container - rhs.container);
2079 }
2080 });
2081 for (ItemInfo info : allWorkspaceItems) {
2082 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002083 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 currentScreenItems.add(info);
2085 itemsOnScreen.add(info.id);
2086 } else {
2087 otherScreenItems.add(info);
2088 }
2089 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2090 currentScreenItems.add(info);
2091 itemsOnScreen.add(info.id);
2092 } else {
2093 if (itemsOnScreen.contains(info.container)) {
2094 currentScreenItems.add(info);
2095 itemsOnScreen.add(info.id);
2096 } else {
2097 otherScreenItems.add(info);
2098 }
2099 }
2100 }
2101 }
2102
2103 /** Filters the set of widgets which are on the specified screen. */
2104 private void filterCurrentAppWidgets(int currentScreen,
2105 ArrayList<LauncherAppWidgetInfo> appWidgets,
2106 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2107 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2108 // If we aren't filtering on a screen, then the set of items to load is the full set of
2109 // widgets given.
2110 if (currentScreen < 0) {
2111 currentScreenWidgets.addAll(appWidgets);
2112 }
2113
2114 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002115 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 currentScreenWidgets.add(widget);
2119 } else {
2120 otherScreenWidgets.add(widget);
2121 }
2122 }
2123 }
2124
2125 /** Filters the set of folders which are on the specified screen. */
2126 private void filterCurrentFolders(int currentScreen,
2127 HashMap<Long, ItemInfo> itemsIdMap,
2128 HashMap<Long, FolderInfo> folders,
2129 HashMap<Long, FolderInfo> currentScreenFolders,
2130 HashMap<Long, FolderInfo> otherScreenFolders) {
2131 // If we aren't filtering on a screen, then the set of items to load is the full set of
2132 // widgets given.
2133 if (currentScreen < 0) {
2134 currentScreenFolders.putAll(folders);
2135 }
2136
2137 for (long id : folders.keySet()) {
2138 ItemInfo info = itemsIdMap.get(id);
2139 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002140 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002141 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002143 currentScreenFolders.put(id, folder);
2144 } else {
2145 otherScreenFolders.put(id, folder);
2146 }
2147 }
2148 }
2149
2150 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2151 * right) */
2152 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002153 final LauncherAppState app = LauncherAppState.getInstance();
2154 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 // XXX: review this
2156 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002157 @Override
2158 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002159 int cellCountX = (int) grid.numColumns;
2160 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002161 int screenOffset = cellCountX * cellCountY;
2162 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002163 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002164 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002166 rhs.cellY * cellCountX + rhs.cellX);
2167 return (int) (lr - rr);
2168 }
2169 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002170 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002171
Adam Cohendcd297f2013-06-18 13:13:40 -07002172 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2173 final ArrayList<Long> orderedScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002174 Launcher.addDumpLog(TAG, "10249126 - bindWorkspaceScreens()", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002175
2176 // Dump the orderedScreens
Winson Chungba9c37f2013-08-30 14:11:37 -07002177 synchronized (sBgLock) {
2178 Launcher.addDumpLog(TAG, "10249126 - orderedScreens", true);
2179 for (Long l : sBgWorkspaceScreens) {
2180 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2181 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07002182 }
2183
Adam Cohendcd297f2013-06-18 13:13:40 -07002184 final Runnable r = new Runnable() {
2185 @Override
2186 public void run() {
2187 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2188 if (callbacks != null) {
2189 callbacks.bindScreens(orderedScreens);
2190 }
2191 }
2192 };
2193 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2194 }
2195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2197 final ArrayList<ItemInfo> workspaceItems,
2198 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2199 final HashMap<Long, FolderInfo> folders,
2200 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002201
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 final boolean postOnMainThread = (deferredBindRunnables != null);
2203
2204 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002205 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002206 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002207 final int start = i;
2208 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 final Runnable r = new Runnable() {
2210 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002212 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002214 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2215 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 }
2217 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002218 };
2219 if (postOnMainThread) {
2220 deferredBindRunnables.add(r);
2221 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002222 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 }
Joe Onorato36115782010-06-17 13:28:48 -04002224 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002225
2226 // Bind the folders
2227 if (!folders.isEmpty()) {
2228 final Runnable r = new Runnable() {
2229 public void run() {
2230 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2231 if (callbacks != null) {
2232 callbacks.bindFolders(folders);
2233 }
2234 }
2235 };
2236 if (postOnMainThread) {
2237 deferredBindRunnables.add(r);
2238 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002239 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 }
2241 }
2242
2243 // Bind the widgets, one at a time
2244 N = appWidgets.size();
2245 for (int i = 0; i < N; i++) {
2246 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2247 final Runnable r = new Runnable() {
2248 public void run() {
2249 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2250 if (callbacks != null) {
2251 callbacks.bindAppWidget(widget);
2252 }
2253 }
2254 };
2255 if (postOnMainThread) {
2256 deferredBindRunnables.add(r);
2257 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002258 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002259 }
2260 }
2261 }
2262
2263 /**
2264 * Binds all loaded data to actual views on the main thread.
2265 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002266 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002267 Launcher.addDumpLog(TAG, "10249126 - bindWorkspace(" + synchronizeBindPage + ", " + isUpgradePath + ")", true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 final long t = SystemClock.uptimeMillis();
2269 Runnable r;
2270
2271 // Don't use these two variables in any of the callback runnables.
2272 // Otherwise we hold a reference to them.
2273 final Callbacks oldCallbacks = mCallbacks.get();
2274 if (oldCallbacks == null) {
2275 // This launcher has exited and nobody bothered to tell us. Just bail.
2276 Log.w(TAG, "LoaderTask running with no launcher");
2277 return;
2278 }
2279
Winson Chung4a2afa32012-07-19 14:53:05 -07002280 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2281 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 oldCallbacks.getCurrentWorkspaceScreen();
2283
2284 // Load all the items that are on the current page first (and in the process, unbind
2285 // all the existing workspace items before we call startBinding() below.
2286 unbindWorkspaceItemsOnMainThread();
2287 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2288 ArrayList<LauncherAppWidgetInfo> appWidgets =
2289 new ArrayList<LauncherAppWidgetInfo>();
2290 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2291 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002293 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 workspaceItems.addAll(sBgWorkspaceItems);
2295 appWidgets.addAll(sBgAppWidgets);
2296 folders.putAll(sBgFolders);
2297 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002299 }
2300
2301 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2302 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2303 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2304 new ArrayList<LauncherAppWidgetInfo>();
2305 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2306 new ArrayList<LauncherAppWidgetInfo>();
2307 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2308 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2309
2310 // Separate the items that are on the current screen, and all the other remaining items
2311 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2312 otherWorkspaceItems);
2313 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2314 otherAppWidgets);
2315 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2316 otherFolders);
2317 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2318 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2319
2320 // Tell the workspace that we're about to start binding items
2321 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002322 public void run() {
2323 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2324 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002326 }
2327 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 };
Winson Chung81b52252012-08-27 15:34:29 -07002329 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002330
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2332
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002333 // Load items on the current page
2334 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2335 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002336 if (isLoadingSynchronously) {
2337 r = new Runnable() {
2338 public void run() {
2339 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2340 if (callbacks != null) {
2341 callbacks.onPageBoundSynchronously(currentScreen);
2342 }
2343 }
2344 };
Winson Chung81b52252012-08-27 15:34:29 -07002345 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002346 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347
Winson Chung4a2afa32012-07-19 14:53:05 -07002348 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2349 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002350 mDeferredBindRunnables.clear();
2351 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002352 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002353
2354 // Tell the workspace that we're done binding items
2355 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002356 public void run() {
2357 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2358 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002359 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361
Winson Chung98e030b2012-05-07 16:01:11 -07002362 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002363 if (DEBUG_LOADERS) {
2364 Log.d(TAG, "bound workspace in "
2365 + (SystemClock.uptimeMillis()-t) + "ms");
2366 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002367
2368 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002369 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002370 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002371 if (isLoadingSynchronously) {
2372 mDeferredBindRunnables.add(r);
2373 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002374 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002375 }
Joe Onorato36115782010-06-17 13:28:48 -04002376 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002377
Joe Onorato36115782010-06-17 13:28:48 -04002378 private void loadAndBindAllApps() {
2379 if (DEBUG_LOADERS) {
2380 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2381 }
2382 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002383 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002384 synchronized (LoaderTask.this) {
2385 if (mStopped) {
2386 return;
2387 }
2388 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002389 }
Joe Onorato36115782010-06-17 13:28:48 -04002390 } else {
2391 onlyBindAllApps();
2392 }
2393 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002394
Joe Onorato36115782010-06-17 13:28:48 -04002395 private void onlyBindAllApps() {
2396 final Callbacks oldCallbacks = mCallbacks.get();
2397 if (oldCallbacks == null) {
2398 // This launcher has exited and nobody bothered to tell us. Just bail.
2399 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2400 return;
2401 }
2402
2403 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002404 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 final ArrayList<AppInfo> list
2406 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002407 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002408 public void run() {
2409 final long t = SystemClock.uptimeMillis();
2410 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2411 if (callbacks != null) {
2412 callbacks.bindAllApplications(list);
2413 }
2414 if (DEBUG_LOADERS) {
2415 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2416 + (SystemClock.uptimeMillis()-t) + "ms");
2417 }
2418 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002419 };
2420 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002421 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002422 r.run();
2423 } else {
2424 mHandler.post(r);
2425 }
Joe Onorato36115782010-06-17 13:28:48 -04002426 }
2427
Winson Chung64359a52013-07-08 17:17:08 -07002428 private void loadAllApps() {
2429 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002430
Joe Onorato36115782010-06-17 13:28:48 -04002431 final Callbacks oldCallbacks = mCallbacks.get();
2432 if (oldCallbacks == null) {
2433 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002434 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002435 return;
2436 }
2437
Winson Chung64359a52013-07-08 17:17:08 -07002438 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002439 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2440 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2441
Winson Chung64359a52013-07-08 17:17:08 -07002442 // Clear the list of apps
2443 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002444
Winson Chung64359a52013-07-08 17:17:08 -07002445 // Query for the set of apps
2446 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2447 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2448 if (DEBUG_LOADERS) {
2449 Log.d(TAG, "queryIntentActivities took "
2450 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2451 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2452 }
2453 // Fail if we don't have any apps
2454 if (apps == null || apps.isEmpty()) {
2455 return;
2456 }
2457 // Sort the applications by name
2458 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2459 Collections.sort(apps,
2460 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2461 if (DEBUG_LOADERS) {
2462 Log.d(TAG, "sort took "
2463 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464 }
2465
Winson Chung64359a52013-07-08 17:17:08 -07002466 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002467 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002468 ResolveInfo app = apps.get(i);
2469 if (oldCallbacks.shouldShowApp(app)) {
2470 // This builds the icon bitmaps.
2471 mBgAllAppsList.add(new AppInfo(packageManager, app,
2472 mIconCache, mLabelCache));
2473 }
Winson Chung64359a52013-07-08 17:17:08 -07002474 }
2475
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002476 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002477 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2478 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002479
2480 // Post callback on main thread
2481 mHandler.post(new Runnable() {
2482 public void run() {
2483 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002484 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002485 if (callbacks != null) {
2486 callbacks.bindAllApplications(added);
2487 if (DEBUG_LOADERS) {
2488 Log.d(TAG, "bound " + added.size() + " apps in "
2489 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2490 }
2491 } else {
2492 Log.i(TAG, "not binding apps: no Launcher activity");
2493 }
2494 }
2495 });
2496
Joe Onorato36115782010-06-17 13:28:48 -04002497 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002498 Log.d(TAG, "Icons processed in "
2499 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002500 }
2501 }
2502
2503 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002504 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002505 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2506 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2507 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2508 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2509 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2510 }
Joe Onorato36115782010-06-17 13:28:48 -04002511 }
2512 }
2513
2514 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002515 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002516 }
2517
2518 private class PackageUpdatedTask implements Runnable {
2519 int mOp;
2520 String[] mPackages;
2521
2522 public static final int OP_NONE = 0;
2523 public static final int OP_ADD = 1;
2524 public static final int OP_UPDATE = 2;
2525 public static final int OP_REMOVE = 3; // uninstlled
2526 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2527
2528
2529 public PackageUpdatedTask(int op, String[] packages) {
2530 mOp = op;
2531 mPackages = packages;
2532 }
2533
2534 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002535 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002536
2537 final String[] packages = mPackages;
2538 final int N = packages.length;
2539 switch (mOp) {
2540 case OP_ADD:
2541 for (int i=0; i<N; i++) {
2542 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002543 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002544 }
2545 break;
2546 case OP_UPDATE:
2547 for (int i=0; i<N; i++) {
2548 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002549 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002550 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002551 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
2553 break;
2554 case OP_REMOVE:
2555 case OP_UNAVAILABLE:
2556 for (int i=0; i<N; i++) {
2557 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002558 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002559 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002560 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002561 }
2562 break;
2563 }
2564
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002565 ArrayList<AppInfo> added = null;
2566 ArrayList<AppInfo> modified = null;
2567 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002568
Adam Cohen487f7dd2012-06-28 18:12:10 -07002569 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002570 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002571 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002572 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002573 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002574 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002575 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002576 }
Winson Chung5d55f332012-07-16 20:45:03 -07002577 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002578 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002579 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002580 }
2581
Joe Onorato36115782010-06-17 13:28:48 -04002582 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2583 if (callbacks == null) {
2584 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2585 return;
2586 }
2587
2588 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002589 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002590 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002591 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07002592 addAndBindAddedApps(context, addedInfos, cb, added);
Joe Onorato36115782010-06-17 13:28:48 -04002593 }
2594 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002595 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002596
2597 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002598 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002599 ArrayList<ItemInfo> infos =
2600 getItemInfoForComponentName(a.componentName);
2601 for (ItemInfo i : infos) {
2602 if (isShortcutInfoUpdateable(i)) {
2603 ShortcutInfo info = (ShortcutInfo) i;
2604 info.title = a.title.toString();
2605 updateItemInDatabase(context, info);
2606 }
2607 }
2608 }
2609
Joe Onorato36115782010-06-17 13:28:48 -04002610 mHandler.post(new Runnable() {
2611 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002612 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2613 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002614 callbacks.bindAppsUpdated(modifiedFinal);
2615 }
2616 }
2617 });
2618 }
Winson Chung83892cc2013-05-01 16:53:33 -07002619 // If a package has been removed, or an app has been removed as a result of
2620 // an update (for example), make the removed callback.
2621 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002622 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002623 final ArrayList<String> removedPackageNames =
2624 new ArrayList<String>(Arrays.asList(packages));
2625
Winson Chung64359a52013-07-08 17:17:08 -07002626 // Update the launcher db to reflect the removal of apps
2627 if (packageRemoved) {
2628 for (String pn : removedPackageNames) {
2629 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2630 for (ItemInfo i : infos) {
2631 deleteItemFromDatabase(context, i);
2632 }
2633 }
2634 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002635 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002636 ArrayList<ItemInfo> infos =
2637 getItemInfoForComponentName(a.componentName);
2638 for (ItemInfo i : infos) {
2639 deleteItemFromDatabase(context, i);
2640 }
2641 }
2642 }
2643
Joe Onorato36115782010-06-17 13:28:48 -04002644 mHandler.post(new Runnable() {
2645 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002646 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2647 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002648 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002649 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002650 }
2651 }
2652 });
Joe Onoratobe386092009-11-17 17:32:16 -08002653 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002654
Michael Jurkac402cd92013-05-20 15:49:32 +02002655 final ArrayList<Object> widgetsAndShortcuts =
2656 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002657 mHandler.post(new Runnable() {
2658 @Override
2659 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002660 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2661 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002662 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002663 }
2664 }
2665 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002666
2667 // Write all the logs to disk
2668 Launcher.addDumpLog(TAG, "10249126 - PackageUpdatedTask - dumping logs to disk", true);
2669 mHandler.post(new Runnable() {
2670 public void run() {
2671 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2672 if (callbacks == cb && cb != null) {
2673 callbacks.dumpLogsToLocalData(false);
2674 }
2675 }
2676 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002677 }
2678 }
2679
Michael Jurkac402cd92013-05-20 15:49:32 +02002680 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2681 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2682 PackageManager packageManager = context.getPackageManager();
2683 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2684 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2685 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2686 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2687 Collections.sort(widgetsAndShortcuts,
2688 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2689 return widgetsAndShortcuts;
2690 }
2691
Winson Chung1323b482013-08-05 12:41:55 -07002692 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002693 if (cn == null) {
2694 return false;
2695 }
2696
2697 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002698 // Skip if the application is disabled
2699 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2700 if (!pi.applicationInfo.enabled) {
2701 return false;
2702 }
2703
2704 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002705 return (pm.getActivityInfo(cn, 0) != null);
2706 } catch (NameNotFoundException e) {
2707 return false;
2708 }
2709 }
2710
Joe Onorato9c1289c2009-08-17 11:03:03 -04002711 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002712 * This is called from the code that adds shortcuts from the intent receiver. This
2713 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002714 */
Joe Onorato56d82912010-03-07 14:32:10 -05002715 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002716 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002718
Joe Onorato56d82912010-03-07 14:32:10 -05002719 /**
2720 * Make an ShortcutInfo object for a shortcut that is an application.
2721 *
2722 * If c is not null, then it will be used to fill in missing data like the title and icon.
2723 */
2724 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002725 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002726 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002727 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002728 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002729 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002730 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002731 } else {
2732 try {
2733 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2734 info.initFlagsAndFirstInstallTime(pi);
2735 } catch (NameNotFoundException e) {
2736 Log.d(TAG, "getPackInfo failed for package " +
2737 componentName.getPackageName());
2738 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002739 }
2740
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002741 // TODO: See if the PackageManager knows about this case. If it doesn't
2742 // then return null & delete this.
2743
Joe Onorato56d82912010-03-07 14:32:10 -05002744 // the resource -- This may implicitly give us back the fallback icon,
2745 // but don't worry about that. All we're doing with usingFallbackIcon is
2746 // to avoid saving lots of copies of that in the database, and most apps
2747 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002748
2749 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2750 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2751 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002752 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002753 ResolveInfo resolveInfo = null;
2754 ComponentName oldComponent = intent.getComponent();
2755 Intent newIntent = new Intent(intent.getAction(), null);
2756 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2757 newIntent.setPackage(oldComponent.getPackageName());
2758 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2759 for (ResolveInfo i : infos) {
2760 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2761 i.activityInfo.name);
2762 if (cn.equals(oldComponent)) {
2763 resolveInfo = i;
2764 }
2765 }
2766 if (resolveInfo == null) {
2767 resolveInfo = manager.resolveActivity(intent, 0);
2768 }
Joe Onorato56d82912010-03-07 14:32:10 -05002769 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002770 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002771 }
Joe Onorato56d82912010-03-07 14:32:10 -05002772 // the db
2773 if (icon == null) {
2774 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002775 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002776 }
2777 }
2778 // the fallback icon
2779 if (icon == null) {
2780 icon = getFallbackIcon();
2781 info.usingFallbackIcon = true;
2782 }
2783 info.setIcon(icon);
2784
2785 // from the resource
2786 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002787 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2788 if (labelCache != null && labelCache.containsKey(key)) {
2789 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002790 } else {
2791 info.title = resolveInfo.activityInfo.loadLabel(manager);
2792 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002793 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002794 }
2795 }
Joe Onorato56d82912010-03-07 14:32:10 -05002796 }
2797 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002799 if (c != null) {
2800 info.title = c.getString(titleIndex);
2801 }
2802 }
2803 // fall back to the class name of the activity
2804 if (info.title == null) {
2805 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002807 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2808 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002810
Winson Chung64359a52013-07-08 17:17:08 -07002811 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2812 ItemInfoFilter f) {
2813 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2814 for (ItemInfo i : infos) {
2815 if (i instanceof ShortcutInfo) {
2816 ShortcutInfo info = (ShortcutInfo) i;
2817 ComponentName cn = info.intent.getComponent();
2818 if (cn != null && f.filterItem(null, info, cn)) {
2819 filtered.add(info);
2820 }
2821 } else if (i instanceof FolderInfo) {
2822 FolderInfo info = (FolderInfo) i;
2823 for (ShortcutInfo s : info.contents) {
2824 ComponentName cn = s.intent.getComponent();
2825 if (cn != null && f.filterItem(info, s, cn)) {
2826 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002827 }
2828 }
Winson Chung64359a52013-07-08 17:17:08 -07002829 } else if (i instanceof LauncherAppWidgetInfo) {
2830 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2831 ComponentName cn = info.providerName;
2832 if (cn != null && f.filterItem(null, info, cn)) {
2833 filtered.add(info);
2834 }
Winson Chung8a435102012-08-30 17:16:53 -07002835 }
2836 }
Winson Chung64359a52013-07-08 17:17:08 -07002837 return new ArrayList<ItemInfo>(filtered);
2838 }
2839
2840 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002841 ItemInfoFilter filter = new ItemInfoFilter() {
2842 @Override
2843 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2844 return cn.getPackageName().equals(pn);
2845 }
2846 };
2847 return filterItemInfos(sBgItemsIdMap.values(), filter);
2848 }
2849
2850 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002851 ItemInfoFilter filter = new ItemInfoFilter() {
2852 @Override
2853 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2854 return cn.equals(cname);
2855 }
2856 };
2857 return filterItemInfos(sBgItemsIdMap.values(), filter);
2858 }
2859
2860 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2861 if (i instanceof ShortcutInfo) {
2862 ShortcutInfo info = (ShortcutInfo) i;
2863 // We need to check for ACTION_MAIN otherwise getComponent() might
2864 // return null for some shortcuts (for instance, for shortcuts to
2865 // web pages.)
2866 Intent intent = info.intent;
2867 ComponentName name = intent.getComponent();
2868 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2869 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2870 return true;
2871 }
2872 }
2873 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002874 }
2875
2876 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002877 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002879 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002880 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2881 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882
Joe Onorato56d82912010-03-07 14:32:10 -05002883 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002884 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002887 // TODO: If there's an explicit component and we can't install that, delete it.
2888
Joe Onorato56d82912010-03-07 14:32:10 -05002889 info.title = c.getString(titleIndex);
2890
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 int iconType = c.getInt(iconTypeIndex);
2892 switch (iconType) {
2893 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2894 String packageName = c.getString(iconPackageIndex);
2895 String resourceName = c.getString(iconResourceIndex);
2896 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002897 info.customIcon = false;
2898 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002901 if (resources != null) {
2902 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002903 icon = Utilities.createIconBitmap(
2904 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002907 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 }
Joe Onorato56d82912010-03-07 14:32:10 -05002909 // the db
2910 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002911 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002912 }
2913 // the fallback icon
2914 if (icon == null) {
2915 icon = getFallbackIcon();
2916 info.usingFallbackIcon = true;
2917 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002918 break;
2919 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002920 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002921 if (icon == null) {
2922 icon = getFallbackIcon();
2923 info.customIcon = false;
2924 info.usingFallbackIcon = true;
2925 } else {
2926 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 break;
2929 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002930 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002931 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 info.customIcon = false;
2933 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002935 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 return info;
2937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938
Michael Jurka931dc972011-08-05 15:08:15 -07002939 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002940 @SuppressWarnings("all") // suppress dead code warning
2941 final boolean debug = false;
2942 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002943 Log.d(TAG, "getIconFromCursor app="
2944 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2945 }
2946 byte[] data = c.getBlob(iconIndex);
2947 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002948 return Utilities.createIconBitmap(
2949 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002950 } catch (Exception e) {
2951 return null;
2952 }
2953 }
2954
Winson Chung3d503fb2011-07-13 17:25:49 -07002955 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2956 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002957 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002958 if (info == null) {
2959 return null;
2960 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002961 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002962
2963 return info;
2964 }
2965
Winson Chunga9abd0e2010-10-27 17:18:37 -07002966 /**
Winson Chung55cef262010-10-28 14:14:18 -07002967 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2968 */
2969 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2970 ComponentName component) {
2971 List<AppWidgetProviderInfo> widgets =
2972 AppWidgetManager.getInstance(context).getInstalledProviders();
2973 for (AppWidgetProviderInfo info : widgets) {
2974 if (info.provider.equals(component)) {
2975 return info;
2976 }
2977 }
2978 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002979 }
2980
Winson Chung68846fd2010-10-29 11:00:27 -07002981 /**
2982 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2983 */
2984 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2985 final PackageManager packageManager = context.getPackageManager();
2986 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2987 new ArrayList<WidgetMimeTypeHandlerData>();
2988
2989 final Intent supportsIntent =
2990 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2991 supportsIntent.setType(mimeType);
2992
2993 // Create a set of widget configuration components that we can test against
2994 final List<AppWidgetProviderInfo> widgets =
2995 AppWidgetManager.getInstance(context).getInstalledProviders();
2996 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2997 new HashMap<ComponentName, AppWidgetProviderInfo>();
2998 for (AppWidgetProviderInfo info : widgets) {
2999 configurationComponentToWidget.put(info.configure, info);
3000 }
3001
3002 // Run through each of the intents that can handle this type of clip data, and cross
3003 // reference them with the components that are actual configuration components
3004 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3005 PackageManager.MATCH_DEFAULT_ONLY);
3006 for (ResolveInfo info : activities) {
3007 final ActivityInfo activityInfo = info.activityInfo;
3008 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3009 activityInfo.name);
3010 if (configurationComponentToWidget.containsKey(infoComponent)) {
3011 supportedConfigurationActivities.add(
3012 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3013 configurationComponentToWidget.get(infoComponent)));
3014 }
3015 }
3016 return supportedConfigurationActivities;
3017 }
3018
Winson Chunga9abd0e2010-10-27 17:18:37 -07003019 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003020 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3021 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3022 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3023
Adam Cohend9198822011-11-22 16:42:47 -08003024 if (intent == null) {
3025 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3026 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3027 return null;
3028 }
3029
Joe Onorato0589f0f2010-02-08 13:44:00 -08003030 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003031 boolean customIcon = false;
3032 ShortcutIconResource iconResource = null;
3033
3034 if (bitmap != null && bitmap instanceof Bitmap) {
3035 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003036 customIcon = true;
3037 } else {
3038 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3039 if (extra != null && extra instanceof ShortcutIconResource) {
3040 try {
3041 iconResource = (ShortcutIconResource) extra;
3042 final PackageManager packageManager = context.getPackageManager();
3043 Resources resources = packageManager.getResourcesForApplication(
3044 iconResource.packageName);
3045 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003046 icon = Utilities.createIconBitmap(
3047 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003048 } catch (Exception e) {
3049 Log.w(TAG, "Could not load shortcut icon: " + extra);
3050 }
3051 }
3052 }
3053
Michael Jurkac9d95c52011-08-29 14:03:34 -07003054 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003055
3056 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003057 if (fallbackIcon != null) {
3058 icon = fallbackIcon;
3059 } else {
3060 icon = getFallbackIcon();
3061 info.usingFallbackIcon = true;
3062 }
Joe Onorato56d82912010-03-07 14:32:10 -05003063 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003064 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003065
Joe Onorato0589f0f2010-02-08 13:44:00 -08003066 info.title = name;
3067 info.intent = intent;
3068 info.customIcon = customIcon;
3069 info.iconResource = iconResource;
3070
3071 return info;
3072 }
3073
Winson Chungaac01e12011-08-17 10:37:13 -07003074 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3075 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003076 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003077 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003078 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003079 }
Joe Onorato56d82912010-03-07 14:32:10 -05003080 // If this icon doesn't have a custom icon, check to see
3081 // what's stored in the DB, and if it doesn't match what
3082 // we're going to show, store what we are going to show back
3083 // into the DB. We do this so when we're loading, if the
3084 // package manager can't find an icon (for example because
3085 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003086 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003087 cache.put(info, c.getBlob(iconIndex));
3088 return true;
3089 }
3090 return false;
3091 }
3092 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3093 boolean needSave = false;
3094 try {
3095 if (data != null) {
3096 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3097 Bitmap loaded = info.getIcon(mIconCache);
3098 needSave = !saved.sameAs(loaded);
3099 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003100 needSave = true;
3101 }
Winson Chungaac01e12011-08-17 10:37:13 -07003102 } catch (Exception e) {
3103 needSave = true;
3104 }
3105 if (needSave) {
3106 Log.d(TAG, "going to save icon bitmap for info=" + info);
3107 // This is slower than is ideal, but this only happens once
3108 // or when the app is updated with a new icon.
3109 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003110 }
3111 }
3112
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003114 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 * or make a new one.
3116 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003117 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 // See if a placeholder was created for us already
3119 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003120 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003122 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 folders.put(id, folderInfo);
3124 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003125 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 }
3127
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003128 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003129 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003130 return new Comparator<AppInfo>() {
3131 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003132 int result = collator.compare(a.title.toString(), b.title.toString());
3133 if (result == 0) {
3134 result = a.componentName.compareTo(b.componentName);
3135 }
3136 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003137 }
Winson Chung11904872012-09-17 16:58:46 -07003138 };
3139 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003140 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3141 = new Comparator<AppInfo>() {
3142 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003143 if (a.firstInstallTime < b.firstInstallTime) return 1;
3144 if (a.firstInstallTime > b.firstInstallTime) return -1;
3145 return 0;
3146 }
3147 };
Winson Chung11904872012-09-17 16:58:46 -07003148 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3149 final Collator collator = Collator.getInstance();
3150 return new Comparator<AppWidgetProviderInfo>() {
3151 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3152 return collator.compare(a.label.toString(), b.label.toString());
3153 }
3154 };
3155 }
Winson Chung5308f242011-08-18 12:12:41 -07003156 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3157 if (info.activityInfo != null) {
3158 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3159 } else {
3160 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3161 }
3162 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003163 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003164 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003165 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003166 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003167 ShortcutNameComparator(PackageManager pm) {
3168 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003169 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003170 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003171 }
3172 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3173 mPackageManager = pm;
3174 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003175 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003176 }
3177 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003178 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003179 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3180 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3181 if (mLabelCache.containsKey(keyA)) {
3182 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003183 } else {
3184 labelA = a.loadLabel(mPackageManager).toString();
3185
Winson Chung5308f242011-08-18 12:12:41 -07003186 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003187 }
Winson Chung5308f242011-08-18 12:12:41 -07003188 if (mLabelCache.containsKey(keyB)) {
3189 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003190 } else {
3191 labelB = b.loadLabel(mPackageManager).toString();
3192
Winson Chung5308f242011-08-18 12:12:41 -07003193 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003194 }
Winson Chung11904872012-09-17 16:58:46 -07003195 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003196 }
3197 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003198 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003199 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003200 private PackageManager mPackageManager;
3201 private HashMap<Object, String> mLabelCache;
3202 WidgetAndShortcutNameComparator(PackageManager pm) {
3203 mPackageManager = pm;
3204 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003205 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003206 }
3207 public final int compare(Object a, Object b) {
3208 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003209 if (mLabelCache.containsKey(a)) {
3210 labelA = mLabelCache.get(a);
3211 } else {
3212 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003213 ((AppWidgetProviderInfo) a).label :
3214 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003215 mLabelCache.put(a, labelA);
3216 }
3217 if (mLabelCache.containsKey(b)) {
3218 labelB = mLabelCache.get(b);
3219 } else {
3220 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003221 ((AppWidgetProviderInfo) b).label :
3222 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003223 mLabelCache.put(b, labelB);
3224 }
Winson Chung11904872012-09-17 16:58:46 -07003225 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003226 }
3227 };
Joe Onoratobe386092009-11-17 17:32:16 -08003228
3229 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003230 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003231 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3232 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3233 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3234 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003235 if (mLoaderTask != null) {
3236 mLoaderTask.dumpState();
3237 } else {
3238 Log.d(TAG, "mLoaderTask=null");
3239 }
Joe Onoratobe386092009-11-17 17:32:16 -08003240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241}