blob: a76378ebf14f5542ae7f6f1c7d227b0f39ac99d3 [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);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200159 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700160 public void bindAppsAdded(ArrayList<Long> newScreens,
161 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700162 ArrayList<ItemInfo> addAnimated,
163 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200164 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700165 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200166 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700167 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200168 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100169 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700170 public void onPageBoundSynchronously(int page);
Adam Cohen4caf2982013-08-20 18:54:31 -0700171 public void dumpLogsToLocalData(boolean email);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung64359a52013-07-08 17:17:08 -0700174 public interface ItemInfoFilter {
175 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
176 }
177
Daniel Sandlere4f98912013-06-25 15:13:26 -0400178 LauncherModel(LauncherAppState app, IconCache iconCache) {
179 final Context context = app.getContext();
180
Winson Chungee055712013-07-30 14:46:24 -0700181 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700183 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800184 mIconCache = iconCache;
185
186 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400187 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700190 Configuration config = res.getConfiguration();
191 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 }
193
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
195 * posted on the main thread handler. */
196 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700197 runOnMainThread(r, 0);
198 }
199 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 // If we are on the worker thread, post onto the main handler
202 mHandler.post(r);
203 } else {
204 r.run();
205 }
206 }
207
208 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
209 * posted on the worker thread handler. */
210 private static void runOnWorkerThread(Runnable r) {
211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 r.run();
213 } else {
214 // If we are not on the worker thread, then post to the worker handler
215 sWorker.post(r);
216 }
217 }
218
Winson Chungc9168342013-06-26 14:54:55 -0700219 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
220 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700221 LauncherAppState app = LauncherAppState.getInstance();
222 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
223 final int xCount = (int) grid.numColumns;
224 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700225 boolean[][] occupied = new boolean[xCount][yCount];
226
227 int cellX, cellY, spanX, spanY;
228 for (int i = 0; i < items.size(); ++i) {
229 final ItemInfo item = items.get(i);
230 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
231 if (item.screenId == screen) {
232 cellX = item.cellX;
233 cellY = item.cellY;
234 spanX = item.spanX;
235 spanY = item.spanY;
236 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
237 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
238 occupied[x][y] = true;
239 }
240 }
241 }
242 }
243 }
244
245 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
246 }
247 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700248 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700249 int firstScreenIndex,
250 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700251 // Lock on the app so that we don't try and get the items while apps are being added
252 LauncherAppState app = LauncherAppState.getInstance();
253 LauncherModel model = app.getModel();
254 boolean found = false;
255 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700256 if (sWorkerThread.getThreadId() != Process.myTid()) {
257 // Flush the LauncherModel worker thread, so that if we just did another
258 // processInstallShortcut, we give it time for its shortcut to get added to the
259 // database (getItemsInLocalCoordinates reads the database)
260 model.flushWorkerThread();
261 }
Winson Chungc9168342013-06-26 14:54:55 -0700262 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700263
264 // Try adding to the workspace screens incrementally, starting at the default or center
265 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700266 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
267 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700269 int[] tmpCoordinates = new int[2];
270 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700271 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700273 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700274 }
275 }
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 return null;
278 }
279
Winson Chungc58497e2013-09-03 17:48:37 -0700280 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
281 final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700282 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -0700283 addAndBindAddedApps(context, added, cb, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700284 }
285 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chungc58497e2013-09-03 17:48:37 -0700286 final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700287 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps()", true);
Winson Chung997a9232013-07-24 15:33:46 -0700288 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700289 return;
Winson Chung997a9232013-07-24 15:33:46 -0700290 }
Winson Chung64359a52013-07-08 17:17:08 -0700291 // Process the newly added applications and add them to the database first
292 Runnable r = new Runnable() {
293 public void run() {
294 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
295 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
296
Winson Chung76828c82013-08-19 15:43:29 -0700297 // Get the list of workspace screens. We need to append to this list and
298 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
299 // called.
300 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
301 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
302 for (Integer i : orderedScreens.keySet()) {
303 long screenId = orderedScreens.get(i);
304 workspaceScreens.add(screenId);
305 }
306
Winson Chung64359a52013-07-08 17:17:08 -0700307 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700308 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700309 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700310 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700311 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700312 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700313
314 // Short-circuit this logic if the icon exists somewhere on the workspace
315 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
316 continue;
317 }
318
319 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700320 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700321 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700322 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700323 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200324 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700325
326 // If we can't find a valid position, then just add a new screen.
327 // This takes time so we need to re-queue the add until the new
328 // page is added. Create as many screens as necessary to satisfy
329 // the startSearchPageIndex.
330 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700331 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700332 while (numPagesToAdd > 0) {
333 long screenId = lp.generateNewScreenId();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")", true);
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700336 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 addedWorkspaceScreensFinal.add(screenId);
338 numPagesToAdd--;
339 }
Winson Chung76828c82013-08-19 15:43:29 -0700340
Winson Chung64359a52013-07-08 17:17:08 -0700341 // Find the coordinate again
342 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700343 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700344 }
345 if (coords == null) {
346 throw new RuntimeException("Coordinates should not be null");
347 }
348
Winson Chung997a9232013-07-24 15:33:46 -0700349 ShortcutInfo shortcutInfo;
350 if (a instanceof ShortcutInfo) {
351 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200352 } else if (a instanceof AppInfo) {
353 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700354 } else {
355 throw new RuntimeException("Unexpected info type");
356 }
Winson Chung64359a52013-07-08 17:17:08 -0700357 // Add the shortcut to the db
358 addItemToDatabase(context, shortcutInfo,
359 LauncherSettings.Favorites.CONTAINER_DESKTOP,
360 coords.first, coords.second[0], coords.second[1], false);
361 // Save the ShortcutInfo for binding in the workspace
362 addedShortcutsFinal.add(shortcutInfo);
363 }
364 }
365
Adam Cohen4caf2982013-08-20 18:54:31 -0700366 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -0700367
Winson Chung76828c82013-08-19 15:43:29 -0700368 // Update the workspace screens
369 updateWorkspaceScreenOrder(context, workspaceScreens);
370
Winson Chung997a9232013-07-24 15:33:46 -0700371 if (!addedShortcutsFinal.isEmpty()) {
372 runOnMainThread(new Runnable() {
373 public void run() {
374 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
375 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700376 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
377 long lastScreenId = info.screenId;
378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
380 for (ItemInfo i : addedShortcutsFinal) {
381 if (i.screenId == lastScreenId) {
382 addAnimated.add(i);
383 } else {
384 addNotAnimated.add(i);
385 }
386 }
Winson Chungd64d1762013-08-20 14:37:16 -0700387 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chungc58497e2013-09-03 17:48:37 -0700388 addNotAnimated, addAnimated, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
Winson Chung64359a52013-07-08 17:17:08 -0700390 }
Winson Chung997a9232013-07-24 15:33:46 -0700391 });
392 }
Winson Chung64359a52013-07-08 17:17:08 -0700393 }
394 };
395 runOnWorkerThread(r);
396 }
397
Joe Onorato56d82912010-03-07 14:32:10 -0500398 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401
Winson Chung81b52252012-08-27 15:34:29 -0700402 public void unbindItemInfosAndClearQueuedBindRunnables() {
403 if (sWorkerThread.getThreadId() == Process.myTid()) {
404 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
405 "main thread");
406 }
407
408 // Clear any deferred bind runnables
409 mDeferredBindRunnables.clear();
410 // Remove any queued bind runnables
411 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
412 // Unbind all the workspace items
413 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700414 }
415
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700416 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700417 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700418 // Ensure that we don't use the same workspace items data structure on the main thread
419 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
421 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700422 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700423 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
424 tmpAppWidgets.addAll(sBgAppWidgets);
425 }
426 Runnable r = new Runnable() {
427 @Override
428 public void run() {
429 for (ItemInfo item : tmpWorkspaceItems) {
430 item.unbind();
431 }
432 for (ItemInfo item : tmpAppWidgets) {
433 item.unbind();
434 }
435 }
436 };
437 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700438 }
439
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 /**
441 * Adds an item to the DB if it was not created previously, or move it to a new
442 * <container, screen, cellX, cellY>
443 */
444 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700445 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 if (item.container == ItemInfo.NO_ID) {
447 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700448 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 } else {
450 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700451 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453 }
454
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700455 static void checkItemInfoLocked(
456 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
457 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
458 if (modelItem != null && item != modelItem) {
459 // check all the data is consistent
460 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
461 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
462 ShortcutInfo shortcut = (ShortcutInfo) item;
463 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
464 modelShortcut.intent.filterEquals(shortcut.intent) &&
465 modelShortcut.id == shortcut.id &&
466 modelShortcut.itemType == shortcut.itemType &&
467 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700468 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700469 modelShortcut.cellX == shortcut.cellX &&
470 modelShortcut.cellY == shortcut.cellY &&
471 modelShortcut.spanX == shortcut.spanX &&
472 modelShortcut.spanY == shortcut.spanY &&
473 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
474 (modelShortcut.dropPos != null &&
475 shortcut.dropPos != null &&
476 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
477 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
478 // For all intents and purposes, this is the same object
479 return;
480 }
481 }
482
483 // the modelItem needs to match up perfectly with item if our model is
484 // to be consistent with the database-- for now, just require
485 // modelItem == item or the equality check above
486 String msg = "item: " + ((item != null) ? item.toString() : "null") +
487 "modelItem: " +
488 ((modelItem != null) ? modelItem.toString() : "null") +
489 "Error: ItemInfo passed to checkItemInfo doesn't match original";
490 RuntimeException e = new RuntimeException(msg);
491 if (stackTrace != null) {
492 e.setStackTrace(stackTrace);
493 }
Adam Cohene25af792013-06-06 23:08:25 -0700494 // TODO: something breaks this in the upgrade path
495 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700496 }
497 }
498
Michael Jurka816474f2012-06-25 14:49:02 -0700499 static void checkItemInfo(final ItemInfo item) {
500 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
501 final long itemId = item.id;
502 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700503 public void run() {
504 synchronized (sBgLock) {
505 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700506 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700507 }
508 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700509 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700510 }
511
Michael Jurkac9d95c52011-08-29 14:03:34 -0700512 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
513 final ItemInfo item, final String callingFunction) {
514 final long itemId = item.id;
515 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
516 final ContentResolver cr = context.getContentResolver();
517
Adam Cohen487f7dd2012-06-28 18:12:10 -0700518 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700519 Runnable r = new Runnable() {
520 public void run() {
521 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700522 updateItemArrays(item, itemId, stackTrace);
523 }
524 };
525 runOnWorkerThread(r);
526 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700528 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
529 final ArrayList<ItemInfo> items, final String callingFunction) {
530 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700531
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
533 Runnable r = new Runnable() {
534 public void run() {
535 ArrayList<ContentProviderOperation> ops =
536 new ArrayList<ContentProviderOperation>();
537 int count = items.size();
538 for (int i = 0; i < count; i++) {
539 ItemInfo item = items.get(i);
540 final long itemId = item.id;
541 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
542 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700543
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700544 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
545 updateItemArrays(item, itemId, stackTrace);
546
547 }
548 try {
549 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
550 } catch (Exception e) {
551 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700552 }
553 }
554 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700555 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700556 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700557
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700558 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
559 // Lock on mBgLock *after* the db operation
560 synchronized (sBgLock) {
561 checkItemInfoLocked(itemId, item, stackTrace);
562
563 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
564 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
565 // Item is in a folder, make sure this folder exists
566 if (!sBgFolders.containsKey(item.container)) {
567 // An items container is being set to a that of an item which is not in
568 // the list of Folders.
569 String msg = "item: " + item + " container being set to: " +
570 item.container + ", not in the list of folders";
571 Log.e(TAG, msg);
572 Launcher.dumpDebugLogsToConsole();
573 }
574 }
575
576 // Items are added/removed from the corresponding FolderInfo elsewhere, such
577 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
578 // that are on the desktop, as appropriate
579 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
580 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
581 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
582 switch (modelItem.itemType) {
583 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
584 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
585 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
586 if (!sBgWorkspaceItems.contains(modelItem)) {
587 sBgWorkspaceItems.add(modelItem);
588 }
589 break;
590 default:
591 break;
592 }
593 } else {
594 sBgWorkspaceItems.remove(modelItem);
595 }
596 }
597 }
598
Michael Jurkac7700af2013-05-14 20:17:58 +0200599 public void flushWorkerThread() {
600 mFlushingWorkerThread = true;
601 Runnable waiter = new Runnable() {
602 public void run() {
603 synchronized (this) {
604 notifyAll();
605 mFlushingWorkerThread = false;
606 }
607 }
608 };
609
610 synchronized(waiter) {
611 runOnWorkerThread(waiter);
612 if (mLoaderTask != null) {
613 synchronized(mLoaderTask) {
614 mLoaderTask.notify();
615 }
616 }
617 boolean success = false;
618 while (!success) {
619 try {
620 waiter.wait();
621 success = true;
622 } catch (InterruptedException e) {
623 }
624 }
625 }
626 }
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700630 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700631 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700632 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700633 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
635 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700636 Launcher.addDumpLog(TAG, transaction, true);
637
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 item.cellX = cellX;
640 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700641
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 // We store hotseat items in canonical form which is this orientation invariant position
643 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
651 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700653 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
654 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700655 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656
Michael Jurkac9d95c52011-08-29 14:03:34 -0700657 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700658 }
659
660 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700661 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
662 * cellX, cellY have already been updated on the ItemInfos.
663 */
664 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
665 final long container, final int screen) {
666
667 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
668 int count = items.size();
669
670 for (int i = 0; i < count; i++) {
671 ItemInfo item = items.get(i);
672 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700673 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
675 + item.cellX + ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700676 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 item.container = container;
678
679 // We store hotseat items in canonical form which is this orientation invariant position
680 // in the hotseat
681 if (context instanceof Launcher && screen < 0 &&
682 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 item.cellY);
685 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 }
688
689 final ContentValues values = new ContentValues();
690 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
691 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
692 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694
695 contentValues.add(values);
696 }
697 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
698 }
699
700 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800702 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700703 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700705 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
707 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700708 Launcher.addDumpLog(TAG, transaction, true);
709
Adam Cohend4844c32011-02-18 19:25:06 -0800710 item.cellX = cellX;
711 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 item.spanX = spanX;
713 item.spanY = spanY;
714
715 // We store hotseat items in canonical form which is this orientation invariant position
716 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 }
Adam Cohend4844c32011-02-18 19:25:06 -0800723
Adam Cohend4844c32011-02-18 19:25:06 -0800724 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800725 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700726 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
727 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
728 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
729 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800731
Michael Jurka816474f2012-06-25 14:49:02 -0700732 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700733 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700734
735 /**
736 * Update an item to the database in a specified container.
737 */
738 static void updateItemInDatabase(Context context, final ItemInfo item) {
739 final ContentValues values = new ContentValues();
740 item.onAddToDatabase(values);
741 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
742 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800743 }
744
745 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 * Returns true if the shortcuts already exists in the database.
747 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 static boolean shortcutExists(Context context, String title, Intent intent) {
750 final ContentResolver cr = context.getContentResolver();
751 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
752 new String[] { "title", "intent" }, "title=? and intent=?",
753 new String[] { title, intent.toUri(0) }, null);
754 boolean result = false;
755 try {
756 result = c.moveToFirst();
757 } finally {
758 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700761 }
762
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700764 * Returns an ItemInfo array containing all the items in the LauncherModel.
765 * The ItemInfo.id is not set through this function.
766 */
767 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
768 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
769 final ContentResolver cr = context.getContentResolver();
770 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
771 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
772 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
773 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
774
775 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
776 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
777 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
778 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
779 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
780 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
781 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
782
783 try {
784 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700785 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 item.cellX = c.getInt(cellXIndex);
787 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700788 item.spanX = Math.max(1, c.getInt(spanXIndex));
789 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700790 item.container = c.getInt(containerIndex);
791 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700792 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700793
794 items.add(item);
795 }
796 } catch (Exception e) {
797 items.clear();
798 } finally {
799 c.close();
800 }
801
802 return items;
803 }
804
805 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
807 */
808 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
809 final ContentResolver cr = context.getContentResolver();
810 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
811 "_id=? and (itemType=? or itemType=?)",
812 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700813 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700814
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 try {
816 if (c.moveToFirst()) {
817 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
818 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
819 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
820 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
821 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
822 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 FolderInfo folderInfo = null;
825 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700826 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
827 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700829 }
830
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 folderInfo.title = c.getString(titleIndex);
832 folderInfo.id = id;
833 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700834 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700835 folderInfo.cellX = c.getInt(cellXIndex);
836 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837
838 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 } finally {
841 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700842 }
843
844 return null;
845 }
846
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 /**
848 * Add an item to the database in a specified container. Sets the container, screen, cellX and
849 * cellY fields of the item. Also assigns an ID to the item.
850 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 item.cellX = cellX;
855 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 // We store hotseat items in canonical form which is this orientation invariant position
857 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
865 final ContentValues values = new ContentValues();
866 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867 item.onAddToDatabase(values);
868
Michael Jurka414300a2013-08-27 15:42:35 +0200869 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700872
Michael Jurkac9d95c52011-08-29 14:03:34 -0700873 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700874 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700875 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700877 + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700878 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700879
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700880 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
881 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700883 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700884 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700885 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 sBgItemsIdMap.put(item.id, item);
887 switch (item.itemType) {
888 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
889 sBgFolders.put(item.id, (FolderInfo) item);
890 // Fall through
891 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
892 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
893 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
894 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
895 sBgWorkspaceItems.add(item);
896 } else {
897 if (!sBgFolders.containsKey(item.container)) {
898 // Adding an item to a folder that doesn't exist.
899 String msg = "adding item: " + item + " to a folder that " +
900 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700901 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700902 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 break;
906 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
907 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
908 break;
909 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700910 }
911 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700912 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700913 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700914 }
915
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700917 * Creates a new unique child id, for a given cell span across all layouts.
918 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700919 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700923 }
924
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700926 * Removes the specified item from the database
927 * @param context
928 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700930 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700932 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700933
Michael Jurka83df1882011-08-31 20:59:26 -0700934 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700935 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700936 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700938 ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700939 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700940
Michael Jurkac9d95c52011-08-29 14:03:34 -0700941 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942
943 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700944 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700945 switch (item.itemType) {
946 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
947 sBgFolders.remove(item.id);
948 for (ItemInfo info: sBgItemsIdMap.values()) {
949 if (info.container == item.id) {
950 // We are deleting a folder which still contains items that
951 // think they are contained by that folder.
952 String msg = "deleting a folder (" + item + ") which still " +
953 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700954 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700956 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700957 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 sBgWorkspaceItems.remove(item);
959 break;
960 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
961 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
962 sBgWorkspaceItems.remove(item);
963 break;
964 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
965 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
966 break;
967 }
968 sBgItemsIdMap.remove(item.id);
969 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700970 }
971 }
Michael Jurka83df1882011-08-31 20:59:26 -0700972 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700973 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 }
975
976 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 * Update the order of the workspace screens in the database. The array list contains
978 * a list of screen ids in the order that they should appear.
979 */
Winson Chungc9168342013-06-26 14:54:55 -0700980 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700981 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder()", true);
Winson Chung64359a52013-07-08 17:17:08 -0700982 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 final ContentResolver cr = context.getContentResolver();
984 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
985
986 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700987 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 while (iter.hasNext()) {
989 long id = iter.next();
990 if (id < 0) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700991 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 iter.remove();
993 }
994 }
995
Winson Chung9e6a0a22013-08-27 11:58:12 -0700996 // Dump the screens copy
Adam Cohen4caf2982013-08-20 18:54:31 -0700997 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700998 for (Long l : screensCopy) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700999 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001000 }
1001
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 Runnable r = new Runnable() {
1003 @Override
1004 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001005 // Clear the table
1006 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001007 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 ContentValues[] values = new ContentValues[count];
1009 for (int i = 0; i < count; i++) {
1010 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001011 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001012 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1013 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohen4caf2982013-08-20 18:54:31 -07001014 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001015 values[i] = v;
1016 }
1017 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001018
Winson Chungba9c37f2013-08-30 14:11:37 -07001019 synchronized (sBgLock) {
1020 // Dump the sBgWorkspaceScreens
1021 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre clear", true);
1022 for (Long l : sBgWorkspaceScreens) {
1023 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1024 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001025
Winson Chungba9c37f2013-08-30 14:11:37 -07001026 sBgWorkspaceScreens.clear();
1027 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001028
Winson Chungba9c37f2013-08-30 14:11:37 -07001029 // Dump the sBgWorkspaceScreens
1030 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post clear", true);
1031 for (Long l : sBgWorkspaceScreens) {
1032 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1033 }
Adam Cohen4caf2982013-08-20 18:54:31 -07001034 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001035 }
1036 };
1037 runOnWorkerThread(r);
1038 }
1039
1040 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 * Remove the contents of the specified folder from the database
1042 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 final ContentResolver cr = context.getContentResolver();
1045
Michael Jurkac9d95c52011-08-29 14:03:34 -07001046 Runnable r = new Runnable() {
1047 public void run() {
1048 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001049 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001050 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 sBgItemsIdMap.remove(info.id);
1052 sBgFolders.remove(info.id);
1053 sBgDbIconCache.remove(info);
1054 sBgWorkspaceItems.remove(info);
1055 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001056
Michael Jurkac9d95c52011-08-29 14:03:34 -07001057 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1058 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001059 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001060 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 for (ItemInfo childInfo : info.contents) {
1062 sBgItemsIdMap.remove(childInfo.id);
1063 sBgDbIconCache.remove(childInfo);
1064 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001065 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001066 }
1067 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001068 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 }
1070
1071 /**
1072 * Set this as the current Launcher activity object for the loader.
1073 */
1074 public void initialize(Callbacks callbacks) {
1075 synchronized (mLock) {
1076 mCallbacks = new WeakReference<Callbacks>(callbacks);
1077 }
1078 }
1079
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001080 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1082 * ACTION_PACKAGE_CHANGED.
1083 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001084 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001085 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001086 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001087
Joe Onorato36115782010-06-17 13:28:48 -04001088 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001089
Joe Onorato36115782010-06-17 13:28:48 -04001090 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1091 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1092 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1093 final String packageName = intent.getData().getSchemeSpecificPart();
1094 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095
Joe Onorato36115782010-06-17 13:28:48 -04001096 int op = PackageUpdatedTask.OP_NONE;
1097
1098 if (packageName == null || packageName.length() == 0) {
1099 // they sent us a bad intent
1100 return;
1101 }
1102
1103 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1104 op = PackageUpdatedTask.OP_UPDATE;
1105 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1106 if (!replacing) {
1107 op = PackageUpdatedTask.OP_REMOVE;
1108 }
1109 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1110 // later, we will update the package at this time
1111 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1112 if (!replacing) {
1113 op = PackageUpdatedTask.OP_ADD;
1114 } else {
1115 op = PackageUpdatedTask.OP_UPDATE;
1116 }
1117 }
1118
1119 if (op != PackageUpdatedTask.OP_NONE) {
1120 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1121 }
1122
1123 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001124 // First, schedule to add these apps back in.
1125 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1126 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1127 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001128 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001129 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1130 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1131 enqueuePackageUpdated(new PackageUpdatedTask(
1132 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001133 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001134 // If we have changed locale we need to clear out the labels in all apps/workspace.
1135 forceReload();
1136 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1137 // Check if configuration change was an mcc/mnc change which would affect app resources
1138 // and we would need to clear out the labels in all apps/workspace. Same handling as
1139 // above for ACTION_LOCALE_CHANGED
1140 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001141 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001142 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001143 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001144 forceReload();
1145 }
1146 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001147 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001148 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1149 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001150 if (mCallbacks != null) {
1151 Callbacks callbacks = mCallbacks.get();
1152 if (callbacks != null) {
1153 callbacks.bindSearchablesChanged();
1154 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001155 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001156 }
1157 }
1158
Reena Lee93f824a2011-09-23 17:20:28 -07001159 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001160 resetLoadedState(true, true);
1161
Reena Lee93f824a2011-09-23 17:20:28 -07001162 // Do this here because if the launcher activity is running it will be restarted.
1163 // If it's not running startLoaderFromBackground will merely tell it that it needs
1164 // to reload.
1165 startLoaderFromBackground();
1166 }
1167
Winson Chungf0c6ae02012-03-21 16:10:31 -07001168 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1169 synchronized (mLock) {
1170 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1171 // mWorkspaceLoaded to true later
1172 stopLoaderLocked();
1173 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1174 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1175 }
1176 }
1177
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001178 /**
1179 * When the launcher is in the background, it's possible for it to miss paired
1180 * configuration changes. So whenever we trigger the loader from the background
1181 * tell the launcher that it needs to re-run the loader when it comes back instead
1182 * of doing it now.
1183 */
1184 public void startLoaderFromBackground() {
1185 boolean runLoader = false;
1186 if (mCallbacks != null) {
1187 Callbacks callbacks = mCallbacks.get();
1188 if (callbacks != null) {
1189 // Only actually run the loader if they're not paused.
1190 if (!callbacks.setLoadOnResume()) {
1191 runLoader = true;
1192 }
1193 }
1194 }
1195 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001197 }
Joe Onorato36115782010-06-17 13:28:48 -04001198 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001199
Reena Lee93f824a2011-09-23 17:20:28 -07001200 // If there is already a loader task running, tell it to stop.
1201 // returns true if isLaunching() was true on the old task
1202 private boolean stopLoaderLocked() {
1203 boolean isLaunching = false;
1204 LoaderTask oldTask = mLoaderTask;
1205 if (oldTask != null) {
1206 if (oldTask.isLaunching()) {
1207 isLaunching = true;
1208 }
1209 oldTask.stopLocked();
1210 }
1211 return isLaunching;
1212 }
1213
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001214 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001215 synchronized (mLock) {
1216 if (DEBUG_LOADERS) {
1217 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001220 // Clear any deferred bind-runnables from the synchronized load process
1221 // We must do this before any loading/binding is scheduled below.
1222 mDeferredBindRunnables.clear();
1223
Joe Onorato36115782010-06-17 13:28:48 -04001224 // Don't bother to start the thread if we know it's not going to do anything
1225 if (mCallbacks != null && mCallbacks.get() != null) {
1226 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001227 // also, don't downgrade isLaunching if we're already running
1228 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001229 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001230 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1231 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1232 } else {
1233 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1234 sWorker.post(mLoaderTask);
1235 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 }
1238 }
1239
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001240 void bindRemainingSynchronousPages() {
1241 // Post the remaining side pages to be loaded
1242 if (!mDeferredBindRunnables.isEmpty()) {
1243 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001244 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001245 }
1246 mDeferredBindRunnables.clear();
1247 }
1248 }
1249
Joe Onorato36115782010-06-17 13:28:48 -04001250 public void stopLoader() {
1251 synchronized (mLock) {
1252 if (mLoaderTask != null) {
1253 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 }
1255 }
Joe Onorato36115782010-06-17 13:28:48 -04001256 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257
Winson Chung76828c82013-08-19 15:43:29 -07001258 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1259 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1260 final ContentResolver contentResolver = context.getContentResolver();
1261 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1262 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1263 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1264
1265 try {
1266 final int idIndex = sc.getColumnIndexOrThrow(
1267 LauncherSettings.WorkspaceScreens._ID);
1268 final int rankIndex = sc.getColumnIndexOrThrow(
1269 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1270 while (sc.moveToNext()) {
1271 try {
1272 long screenId = sc.getLong(idIndex);
1273 int rank = sc.getInt(rankIndex);
1274
Adam Cohen4caf2982013-08-20 18:54:31 -07001275 Launcher.addDumpLog(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")", true);
Winson Chung76828c82013-08-19 15:43:29 -07001276
1277 orderedScreens.put(rank, screenId);
1278 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001279 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001280 }
1281 }
1282 } finally {
1283 sc.close();
1284 }
1285 return orderedScreens;
1286 }
1287
Michael Jurkac57b7a82011-08-09 22:02:20 -07001288 public boolean isAllAppsLoaded() {
1289 return mAllAppsLoaded;
1290 }
1291
Winson Chung36a62fe2012-05-06 18:04:42 -07001292 boolean isLoadingWorkspace() {
1293 synchronized (mLock) {
1294 if (mLoaderTask != null) {
1295 return mLoaderTask.isLoadingWorkspace();
1296 }
1297 }
1298 return false;
1299 }
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 /**
1302 * Runnable for the thread that loads the contents of the launcher:
1303 * - workspace icons
1304 * - widgets
1305 * - all apps icons
1306 */
1307 private class LoaderTask implements Runnable {
1308 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001309 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001310 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001311 private boolean mStopped;
1312 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001313
Winson Chungc3eecff2011-07-11 17:44:15 -07001314 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001315
1316 LoaderTask(Context context, boolean isLaunching) {
1317 mContext = context;
1318 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001319 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 }
1321
Joe Onorato36115782010-06-17 13:28:48 -04001322 boolean isLaunching() {
1323 return mIsLaunching;
1324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325
Winson Chung36a62fe2012-05-06 18:04:42 -07001326 boolean isLoadingWorkspace() {
1327 return mIsLoadingAndBindingWorkspace;
1328 }
1329
Winson Chungc763c4e2013-07-19 13:49:06 -07001330 /** Returns whether this is an upgrade path */
1331 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001332 mIsLoadingAndBindingWorkspace = true;
1333
Joe Onorato36115782010-06-17 13:28:48 -04001334 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001335 if (DEBUG_LOADERS) {
1336 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001337 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001338
Winson Chungc763c4e2013-07-19 13:49:06 -07001339 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001340 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001341 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001342 synchronized (LoaderTask.this) {
1343 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001344 Launcher.addDumpLog(TAG, "10249126 - loadAndBindWorkspace() stopped", true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001345 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001346 }
1347 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 }
1349 }
1350
Joe Onorato36115782010-06-17 13:28:48 -04001351 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001352 bindWorkspace(-1, isUpgradePath);
1353 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001354 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001355
Joe Onorato36115782010-06-17 13:28:48 -04001356 private void waitForIdle() {
1357 // Wait until the either we're stopped or the other threads are done.
1358 // This way we don't start loading all apps until the workspace has settled
1359 // down.
1360 synchronized (LoaderTask.this) {
1361 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001362
Joe Onorato36115782010-06-17 13:28:48 -04001363 mHandler.postIdle(new Runnable() {
1364 public void run() {
1365 synchronized (LoaderTask.this) {
1366 mLoadAndBindStepFinished = true;
1367 if (DEBUG_LOADERS) {
1368 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001369 }
Joe Onorato36115782010-06-17 13:28:48 -04001370 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001371 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001372 }
Joe Onorato36115782010-06-17 13:28:48 -04001373 });
1374
Michael Jurkac7700af2013-05-14 20:17:58 +02001375 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001376 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001377 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1378 // wait no longer than 1sec at a time
1379 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001380 } catch (InterruptedException ex) {
1381 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001382 }
1383 }
Joe Onorato36115782010-06-17 13:28:48 -04001384 if (DEBUG_LOADERS) {
1385 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001386 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001387 + "ms for previous step to finish binding");
1388 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001389 }
Joe Onorato36115782010-06-17 13:28:48 -04001390 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001391
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 void runBindSynchronousPage(int synchronousBindPage) {
1393 if (synchronousBindPage < 0) {
1394 // Ensure that we have a valid page index to load synchronously
1395 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1396 "valid page index");
1397 }
1398 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1399 // Ensure that we don't try and bind a specified page when the pages have not been
1400 // loaded already (we should load everything asynchronously in that case)
1401 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1402 }
1403 synchronized (mLock) {
1404 if (mIsLoaderTaskRunning) {
1405 // Ensure that we are never running the background loading at this point since
1406 // we also touch the background collections
1407 throw new RuntimeException("Error! Background loading is already running");
1408 }
1409 }
1410
1411 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1412 // data structures, we can't allow any other thread to touch that data, but because
1413 // this call is synchronous, we can get away with not locking).
1414
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001415 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001416 // operations from the previous activity. We need to ensure that all queued operations
1417 // are executed before any synchronous binding work is done.
1418 mHandler.flush();
1419
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001420 // Divide the set of loaded items into those that we are binding synchronously, and
1421 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001422 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001423 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1424 // arise from that.
1425 onlyBindAllApps();
1426 }
1427
Joe Onorato36115782010-06-17 13:28:48 -04001428 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001429 boolean isUpgrade = false;
1430
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001431 synchronized (mLock) {
1432 mIsLoaderTaskRunning = true;
1433 }
Joe Onorato36115782010-06-17 13:28:48 -04001434 // Optimize for end-user experience: if the Launcher is up and // running with the
1435 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1436 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001437 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001438 // Elevate priority when Home launches for the first time to avoid
1439 // starving at boot time. Staring at a blank home is not cool.
1440 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001441 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1442 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001443 android.os.Process.setThreadPriority(mIsLaunching
1444 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1445 }
Winson Chung64359a52013-07-08 17:17:08 -07001446 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001447 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001448
Joe Onorato36115782010-06-17 13:28:48 -04001449 if (mStopped) {
1450 break keep_running;
1451 }
1452
1453 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1454 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001455 synchronized (mLock) {
1456 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001457 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001458 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1459 }
1460 }
Joe Onorato36115782010-06-17 13:28:48 -04001461 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001462
1463 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001464 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1465 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001466
1467 // Restore the default thread priority after we are done loading items
1468 synchronized (mLock) {
1469 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472
Winson Chungaac01e12011-08-17 10:37:13 -07001473 // Update the saved icons if necessary
1474 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001475 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001476 for (Object key : sBgDbIconCache.keySet()) {
1477 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1478 }
1479 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001480 }
Winson Chungaac01e12011-08-17 10:37:13 -07001481
Winson Chungc58497e2013-09-03 17:48:37 -07001482 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1483 // Ensure that all the applications that are in the system are
1484 // represented on the home screen.
1485 Launcher.addDumpLog(TAG, "10249126 - verifyApplications - useMoreApps="
1486 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade, true);
1487 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1488 Launcher.addDumpLog(TAG, "10249126 - verifyApplications(" + isUpgrade + ")", true);
1489 verifyApplications();
1490 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001491 }
1492
Joe Onorato36115782010-06-17 13:28:48 -04001493 // Clear out this reference, otherwise we end up holding it until all of the
1494 // callback runnables are done.
1495 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496
Joe Onorato36115782010-06-17 13:28:48 -04001497 synchronized (mLock) {
1498 // If we are still the last one to be scheduled, remove ourselves.
1499 if (mLoaderTask == this) {
1500 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001502 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001503 }
Joe Onorato36115782010-06-17 13:28:48 -04001504 }
1505
1506 public void stopLocked() {
1507 synchronized (LoaderTask.this) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001508 Launcher.addDumpLog(TAG, "10249126 - STOPPED", true);
Joe Onorato36115782010-06-17 13:28:48 -04001509 mStopped = true;
1510 this.notify();
1511 }
1512 }
1513
1514 /**
1515 * Gets the callbacks object. If we've been stopped, or if the launcher object
1516 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1517 * object that was around when the deferred message was scheduled, and if there's
1518 * a new Callbacks object around then also return null. This will save us from
1519 * calling onto it with data that will be ignored.
1520 */
1521 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1522 synchronized (mLock) {
1523 if (mStopped) {
1524 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001525 }
Joe Onorato36115782010-06-17 13:28:48 -04001526
1527 if (mCallbacks == null) {
1528 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001529 }
Joe Onorato36115782010-06-17 13:28:48 -04001530
1531 final Callbacks callbacks = mCallbacks.get();
1532 if (callbacks != oldCallbacks) {
1533 return null;
1534 }
1535 if (callbacks == null) {
1536 Log.w(TAG, "no mCallbacks");
1537 return null;
1538 }
1539
1540 return callbacks;
1541 }
1542 }
1543
Winson Chungc763c4e2013-07-19 13:49:06 -07001544 private void verifyApplications() {
1545 final Context context = mApp.getContext();
1546
1547 // Cross reference all the applications in our apps list with items in the workspace
1548 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001549 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001550 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001551 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001552 tmpInfos = getItemInfoForComponentName(app.componentName);
Adam Cohen4caf2982013-08-20 18:54:31 -07001553 Launcher.addDumpLog(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty(), true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001554 if (tmpInfos.isEmpty()) {
1555 // We are missing an application icon, so add this to the workspace
1556 added.add(app);
1557 // This is a rare event, so lets log it
1558 Log.e(TAG, "Missing Application on load: " + app);
1559 }
1560 }
1561 }
1562 if (!added.isEmpty()) {
1563 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001564 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001565 }
1566 }
1567
Winson Chung5f8afe62013-08-12 16:19:28 -07001568 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001569 LauncherAppState app = LauncherAppState.getInstance();
1570 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1571 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1572 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001573 }
1574
Joe Onorato36115782010-06-17 13:28:48 -04001575 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001577 LauncherAppState app = LauncherAppState.getInstance();
1578 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1579 int countX = (int) grid.numColumns;
1580 int countY = (int) grid.numRows;
1581
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001583 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1585 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1586 [(int) item.screenId][0] != null) {
1587 Log.e(TAG, "Error loading shortcut into hotseat " + item
1588 + " into position (" + item.screenId + ":" + item.cellX + ","
1589 + item.cellY + ") occupied by "
1590 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1591 [(int) item.screenId][0]);
1592 return false;
1593 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001594 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001595 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001596 items[(int) item.screenId][0] = item;
1597 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001598 return true;
1599 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001600 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1601 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001602 return true;
1603 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001604
Adam Cohendcd297f2013-06-18 13:13:40 -07001605 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001606 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 occupied.put(item.screenId, items);
1608 }
1609
1610 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001611 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001612 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1613 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001614 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001615 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001617 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001618 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001619 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001620 return false;
1621 }
1622 }
1623 }
1624 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1625 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001626 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001627 }
1628 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001629
Joe Onorato36115782010-06-17 13:28:48 -04001630 return true;
1631 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001632
Winson Chungba9c37f2013-08-30 14:11:37 -07001633 /** Clears all the sBg data structures */
1634 private void clearSBgDataStructures() {
1635 synchronized (sBgLock) {
1636 sBgWorkspaceItems.clear();
1637 sBgAppWidgets.clear();
1638 sBgFolders.clear();
1639 sBgItemsIdMap.clear();
1640 sBgDbIconCache.clear();
1641 sBgWorkspaceScreens.clear();
1642 }
1643 }
1644
Winson Chungc763c4e2013-07-19 13:49:06 -07001645 /** Returns whether this is an upgradge path */
1646 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001647 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001648
Joe Onorato36115782010-06-17 13:28:48 -04001649 final Context context = mContext;
1650 final ContentResolver contentResolver = context.getContentResolver();
1651 final PackageManager manager = context.getPackageManager();
1652 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1653 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001654
Winson Chung892c74d2013-08-22 16:15:50 -07001655 LauncherAppState app = LauncherAppState.getInstance();
1656 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1657 int countX = (int) grid.numColumns;
1658 int countY = (int) grid.numRows;
1659
Michael Jurkab85f8a42012-04-25 15:48:32 -07001660 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001661 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001662
Winson Chungc763c4e2013-07-19 13:49:06 -07001663 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001664 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001665
Winson Chung2abf94d2012-07-18 18:16:38 -07001666 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001667 clearSBgDataStructures();
Adam Cohen4caf2982013-08-20 18:54:31 -07001668 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace()", true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001669
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001670 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001671 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001672 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001673
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001674 // +1 for the hotseat (it can be larger than the workspace)
1675 // Load workspace in reverse order to ensure that latest items are loaded first (and
1676 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001677 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 try {
1680 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1681 final int intentIndex = c.getColumnIndexOrThrow
1682 (LauncherSettings.Favorites.INTENT);
1683 final int titleIndex = c.getColumnIndexOrThrow
1684 (LauncherSettings.Favorites.TITLE);
1685 final int iconTypeIndex = c.getColumnIndexOrThrow(
1686 LauncherSettings.Favorites.ICON_TYPE);
1687 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1688 final int iconPackageIndex = c.getColumnIndexOrThrow(
1689 LauncherSettings.Favorites.ICON_PACKAGE);
1690 final int iconResourceIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.ICON_RESOURCE);
1692 final int containerIndex = c.getColumnIndexOrThrow(
1693 LauncherSettings.Favorites.CONTAINER);
1694 final int itemTypeIndex = c.getColumnIndexOrThrow(
1695 LauncherSettings.Favorites.ITEM_TYPE);
1696 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1697 LauncherSettings.Favorites.APPWIDGET_ID);
1698 final int screenIndex = c.getColumnIndexOrThrow(
1699 LauncherSettings.Favorites.SCREEN);
1700 final int cellXIndex = c.getColumnIndexOrThrow
1701 (LauncherSettings.Favorites.CELLX);
1702 final int cellYIndex = c.getColumnIndexOrThrow
1703 (LauncherSettings.Favorites.CELLY);
1704 final int spanXIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.SPANX);
1706 final int spanYIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.SPANY);
1708 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1709 //final int displayModeIndex = c.getColumnIndexOrThrow(
1710 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 ShortcutInfo info;
1713 String intentDescription;
1714 LauncherAppWidgetInfo appWidgetInfo;
1715 int container;
1716 long id;
1717 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718
Winson Chungba9c37f2013-08-30 14:11:37 -07001719 Launcher.addDumpLog(TAG, "10249126 - Num rows: " + c.getCount(), true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001720 while (!mStopped && c.moveToNext()) {
1721 try {
1722 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001723
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 switch (itemType) {
1725 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1726 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001727 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 intentDescription = c.getString(intentIndex);
1729 try {
1730 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001731 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001732 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001733 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001734 // Log the invalid package, and remove it from the db
1735 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1736 false);
Winson Chungee055712013-07-30 14:46:24 -07001737 contentResolver.delete(uri, null, null);
Winson Chung933bae62013-08-29 11:42:30 -07001738 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001739 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001740 // If apps can be on external storage, then we just
1741 // leave them for the user to remove (maybe add
1742 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001743 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001744 }
1745 continue;
1746 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001747 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001748 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 continue;
1750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1753 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1754 titleIndex, mLabelCache);
1755 } else {
1756 info = getShortcutInfo(c, context, iconTypeIndex,
1757 iconPackageIndex, iconResourceIndex, iconIndex,
1758 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001759
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001760 // App shortcuts that used to be automatically added to Launcher
1761 // didn't always have the correct intent flags set, so do that
1762 // here
1763 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001764 intent.getCategories() != null &&
1765 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001766 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 intent.addFlags(
1768 Intent.FLAG_ACTIVITY_NEW_TASK |
1769 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1770 }
Michael Jurka96879562012-03-22 05:54:33 -07001771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001774 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001775 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 container = c.getInt(containerIndex);
1777 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001778 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 info.cellX = c.getInt(cellXIndex);
1780 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001781 info.spanX = 1;
1782 info.spanY = 1;
1783 // Skip loading items that are out of bounds
1784 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1785 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001786 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1787 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001788 continue;
1789 }
1790 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 // check & update map of what's occupied
1792 if (!checkItemPlacement(occupied, info)) {
1793 break;
1794 }
1795
1796 switch (container) {
1797 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1798 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1799 sBgWorkspaceItems.add(info);
1800 break;
1801 default:
1802 // Item is in a user folder
1803 FolderInfo folderInfo =
1804 findOrMakeFolder(sBgFolders, container);
1805 folderInfo.add(info);
1806 break;
1807 }
1808 sBgItemsIdMap.put(info.id, info);
1809
1810 // now that we've loaded everthing re-save it with the
1811 // icon in case it disappears somehow.
1812 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001813 } else {
1814 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001815 }
1816 break;
1817
1818 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1819 id = c.getLong(idIndex);
1820 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1821
1822 folderInfo.title = c.getString(titleIndex);
1823 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001824 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001826 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001827 folderInfo.cellX = c.getInt(cellXIndex);
1828 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001829 folderInfo.spanX = 1;
1830 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831
Winson Chung5f8afe62013-08-12 16:19:28 -07001832 // Skip loading items that are out of bounds
1833 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001834 if (checkItemDimensions(folderInfo)) {
1835 Log.d(TAG, "Skipped loading out of bounds folder");
1836 continue;
1837 }
1838 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001839 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001841 break;
1842 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001843
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001845 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1846 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1847 sBgWorkspaceItems.add(folderInfo);
1848 break;
Joe Onorato36115782010-06-17 13:28:48 -04001849 }
Joe Onorato17a89222011-02-08 17:26:11 -08001850
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1852 sBgFolders.put(folderInfo.id, folderInfo);
1853 break;
1854
1855 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1856 // Read all Launcher-specific widget details
1857 int appWidgetId = c.getInt(appWidgetIdIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001858 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001859
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 final AppWidgetProviderInfo provider =
1861 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001862
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 if (!isSafeMode && (provider == null || provider.provider == null ||
1864 provider.provider.getPackageName() == null)) {
1865 String log = "Deleting widget that isn't installed anymore: id="
1866 + id + " appWidgetId=" + appWidgetId;
1867 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001868 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 itemsToRemove.add(id);
1870 } else {
1871 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1872 provider.provider);
1873 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001874 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 appWidgetInfo.cellX = c.getInt(cellXIndex);
1876 appWidgetInfo.cellY = c.getInt(cellYIndex);
1877 appWidgetInfo.spanX = c.getInt(spanXIndex);
1878 appWidgetInfo.spanY = c.getInt(spanYIndex);
1879 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1880 appWidgetInfo.minSpanX = minSpan[0];
1881 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001882
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001883 container = c.getInt(containerIndex);
1884 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1885 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1886 Log.e(TAG, "Widget found where container != " +
1887 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1888 continue;
1889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890
Adam Cohene25af792013-06-06 23:08:25 -07001891 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001892 // Skip loading items that are out of bounds
1893 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1894 if (checkItemDimensions(appWidgetInfo)) {
1895 Log.d(TAG, "Skipped loading out of bounds app widget");
1896 continue;
1897 }
1898 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001899 // check & update map of what's occupied
1900 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1901 break;
1902 }
1903 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1904 sBgAppWidgets.add(appWidgetInfo);
1905 }
Joe Onorato36115782010-06-17 13:28:48 -04001906 break;
1907 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001909 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001910 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001911 }
1912 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001913 if (c != null) {
1914 c.close();
1915 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 }
1917
Winson Chungba9c37f2013-08-30 14:11:37 -07001918 // Break early if we've stopped loading
1919 if (mStopped) {
1920 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace() - Stopped", true);
1921 clearSBgDataStructures();
1922 return false;
1923 }
1924
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001925 if (itemsToRemove.size() > 0) {
1926 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001927 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 // Remove dead items
1929 for (long id : itemsToRemove) {
1930 if (DEBUG_LOADERS) {
1931 Log.d(TAG, "Removed id = " + id);
1932 }
1933 // Don't notify content observers
1934 try {
1935 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1936 null, null);
1937 } catch (RemoteException e) {
1938 Log.w(TAG, "Could not remove id = " + id);
1939 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001940 }
1941 }
1942
Winson Chungc763c4e2013-07-19 13:49:06 -07001943 if (loadedOldDb) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001944 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - loadedOldDb", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001945 long maxScreenId = 0;
1946 // If we're importing we use the old screen order.
1947 for (ItemInfo item: sBgItemsIdMap.values()) {
1948 long screenId = item.screenId;
1949 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1950 !sBgWorkspaceScreens.contains(screenId)) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001951 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 sBgWorkspaceScreens.add(screenId);
1953 if (screenId > maxScreenId) {
1954 maxScreenId = screenId;
1955 }
1956 }
1957 }
1958 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001959
1960 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001961 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001962 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001963 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001964 }
1965
Michael Jurka414300a2013-08-27 15:42:35 +02001966 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001968
1969 // Update the max item id after we load an old db
1970 long maxItemId = 0;
1971 // If we're importing we use the old screen order.
1972 for (ItemInfo item: sBgItemsIdMap.values()) {
1973 maxItemId = Math.max(maxItemId, item.id);
1974 }
Michael Jurka414300a2013-08-27 15:42:35 +02001975 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 } else {
Winson Chung933bae62013-08-29 11:42:30 -07001977 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - !loadedOldDb [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Winson Chung76828c82013-08-19 15:43:29 -07001978 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1979 for (Integer i : orderedScreens.keySet()) {
Winson Chung933bae62013-08-29 11:42:30 -07001980 Launcher.addDumpLog(TAG, "10249126 - adding to sBgWorkspaceScreens: " + orderedScreens.get(i), true);
Winson Chung76828c82013-08-19 15:43:29 -07001981 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 }
1983
1984 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001985 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
1986 for (Long l : unusedScreens) {
1987 Launcher.addDumpLog(TAG, "10249126 - unused screens: " + l, true);
1988 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001989
Winson Chung933bae62013-08-29 11:42:30 -07001990 Launcher.addDumpLog(TAG, "10249126 - sBgItemsIdMap [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 for (ItemInfo item: sBgItemsIdMap.values()) {
1992 long screenId = item.screenId;
Winson Chung933bae62013-08-29 11:42:30 -07001993 Launcher.addDumpLog(TAG, "10249126 - \t" + item.container + ", " + screenId + " - " + unusedScreens.contains(screenId) + " | " + item, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001994
1995 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1996 unusedScreens.contains(screenId)) {
1997 unusedScreens.remove(screenId);
Winson Chung933bae62013-08-29 11:42:30 -07001998 Launcher.addDumpLog(TAG, "10249126 - \t\tRemoving " + screenId, true);
1999 for (Long l : unusedScreens) {
2000 Launcher.addDumpLog(TAG, "10249126 - \t\t\t unused screens: " + l, true);
2001 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 }
2003 }
2004
2005 // If there are any empty screens remove them, and update.
2006 if (unusedScreens.size() != 0) {
Winson Chung9e6a0a22013-08-27 11:58:12 -07002007 // Dump the sBgWorkspaceScreens
Winson Chung933bae62013-08-29 11:42:30 -07002008 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre removeAll", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002009 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002010 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002011 }
Winson Chung933bae62013-08-29 11:42:30 -07002012
2013 sBgWorkspaceScreens.removeAll(unusedScreens);
2014
2015 // Dump the sBgWorkspaceScreens
2016 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post removeAll", true);
2017 for (Long l : sBgWorkspaceScreens) {
2018 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2019 }
2020
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2022 }
2023 }
2024
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 if (DEBUG_LOADERS) {
2026 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2027 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002029 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002031
Daniel Sandler566da102013-06-25 23:43:45 -04002032 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002033 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002034 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002035 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 line += " | ";
2037 }
Winson Chung892c74d2013-08-22 16:15:50 -07002038 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002039 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 }
Joe Onorato36115782010-06-17 13:28:48 -04002041 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 }
Joe Onorato36115782010-06-17 13:28:48 -04002045 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002046 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002047 }
2048
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002049 /** Filters the set of items who are directly or indirectly (via another container) on the
2050 * specified screen. */
2051 private void filterCurrentWorkspaceItems(int currentScreen,
2052 ArrayList<ItemInfo> allWorkspaceItems,
2053 ArrayList<ItemInfo> currentScreenItems,
2054 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002055 // Purge any null ItemInfos
2056 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2057 while (iter.hasNext()) {
2058 ItemInfo i = iter.next();
2059 if (i == null) {
2060 iter.remove();
2061 }
2062 }
2063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 // If we aren't filtering on a screen, then the set of items to load is the full set of
2065 // items given.
2066 if (currentScreen < 0) {
2067 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002068 }
2069
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002070 // Order the set of items by their containers first, this allows use to walk through the
2071 // list sequentially, build up a list of containers that are in the specified screen,
2072 // as well as all items in those containers.
2073 Set<Long> itemsOnScreen = new HashSet<Long>();
2074 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2075 @Override
2076 public int compare(ItemInfo lhs, ItemInfo rhs) {
2077 return (int) (lhs.container - rhs.container);
2078 }
2079 });
2080 for (ItemInfo info : allWorkspaceItems) {
2081 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002082 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002083 currentScreenItems.add(info);
2084 itemsOnScreen.add(info.id);
2085 } else {
2086 otherScreenItems.add(info);
2087 }
2088 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2089 currentScreenItems.add(info);
2090 itemsOnScreen.add(info.id);
2091 } else {
2092 if (itemsOnScreen.contains(info.container)) {
2093 currentScreenItems.add(info);
2094 itemsOnScreen.add(info.id);
2095 } else {
2096 otherScreenItems.add(info);
2097 }
2098 }
2099 }
2100 }
2101
2102 /** Filters the set of widgets which are on the specified screen. */
2103 private void filterCurrentAppWidgets(int currentScreen,
2104 ArrayList<LauncherAppWidgetInfo> appWidgets,
2105 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2106 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2107 // If we aren't filtering on a screen, then the set of items to load is the full set of
2108 // widgets given.
2109 if (currentScreen < 0) {
2110 currentScreenWidgets.addAll(appWidgets);
2111 }
2112
2113 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002114 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002116 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002117 currentScreenWidgets.add(widget);
2118 } else {
2119 otherScreenWidgets.add(widget);
2120 }
2121 }
2122 }
2123
2124 /** Filters the set of folders which are on the specified screen. */
2125 private void filterCurrentFolders(int currentScreen,
2126 HashMap<Long, ItemInfo> itemsIdMap,
2127 HashMap<Long, FolderInfo> folders,
2128 HashMap<Long, FolderInfo> currentScreenFolders,
2129 HashMap<Long, FolderInfo> otherScreenFolders) {
2130 // If we aren't filtering on a screen, then the set of items to load is the full set of
2131 // widgets given.
2132 if (currentScreen < 0) {
2133 currentScreenFolders.putAll(folders);
2134 }
2135
2136 for (long id : folders.keySet()) {
2137 ItemInfo info = itemsIdMap.get(id);
2138 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002139 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002141 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 currentScreenFolders.put(id, folder);
2143 } else {
2144 otherScreenFolders.put(id, folder);
2145 }
2146 }
2147 }
2148
2149 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2150 * right) */
2151 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002152 final LauncherAppState app = LauncherAppState.getInstance();
2153 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 // XXX: review this
2155 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002156 @Override
2157 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002158 int cellCountX = (int) grid.numColumns;
2159 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002160 int screenOffset = cellCountX * cellCountY;
2161 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002162 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002163 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002165 rhs.cellY * cellCountX + rhs.cellX);
2166 return (int) (lr - rr);
2167 }
2168 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002170
Adam Cohendcd297f2013-06-18 13:13:40 -07002171 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2172 final ArrayList<Long> orderedScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002173 Launcher.addDumpLog(TAG, "10249126 - bindWorkspaceScreens()", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002174
2175 // Dump the orderedScreens
Winson Chungba9c37f2013-08-30 14:11:37 -07002176 synchronized (sBgLock) {
2177 Launcher.addDumpLog(TAG, "10249126 - orderedScreens", true);
2178 for (Long l : sBgWorkspaceScreens) {
2179 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2180 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07002181 }
2182
Adam Cohendcd297f2013-06-18 13:13:40 -07002183 final Runnable r = new Runnable() {
2184 @Override
2185 public void run() {
2186 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2187 if (callbacks != null) {
2188 callbacks.bindScreens(orderedScreens);
2189 }
2190 }
2191 };
2192 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2193 }
2194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2196 final ArrayList<ItemInfo> workspaceItems,
2197 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2198 final HashMap<Long, FolderInfo> folders,
2199 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002201 final boolean postOnMainThread = (deferredBindRunnables != null);
2202
2203 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002204 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002205 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002206 final int start = i;
2207 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002208 final Runnable r = new Runnable() {
2209 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002211 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002213 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2214 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215 }
2216 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 };
2218 if (postOnMainThread) {
2219 deferredBindRunnables.add(r);
2220 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002221 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 }
Joe Onorato36115782010-06-17 13:28:48 -04002223 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224
2225 // Bind the folders
2226 if (!folders.isEmpty()) {
2227 final Runnable r = new Runnable() {
2228 public void run() {
2229 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2230 if (callbacks != null) {
2231 callbacks.bindFolders(folders);
2232 }
2233 }
2234 };
2235 if (postOnMainThread) {
2236 deferredBindRunnables.add(r);
2237 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002238 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239 }
2240 }
2241
2242 // Bind the widgets, one at a time
2243 N = appWidgets.size();
2244 for (int i = 0; i < N; i++) {
2245 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2246 final Runnable r = new Runnable() {
2247 public void run() {
2248 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2249 if (callbacks != null) {
2250 callbacks.bindAppWidget(widget);
2251 }
2252 }
2253 };
2254 if (postOnMainThread) {
2255 deferredBindRunnables.add(r);
2256 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002257 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 }
2259 }
2260 }
2261
2262 /**
2263 * Binds all loaded data to actual views on the main thread.
2264 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002265 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002266 Launcher.addDumpLog(TAG, "10249126 - bindWorkspace(" + synchronizeBindPage + ", " + isUpgradePath + ")", true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 final long t = SystemClock.uptimeMillis();
2268 Runnable r;
2269
2270 // Don't use these two variables in any of the callback runnables.
2271 // Otherwise we hold a reference to them.
2272 final Callbacks oldCallbacks = mCallbacks.get();
2273 if (oldCallbacks == null) {
2274 // This launcher has exited and nobody bothered to tell us. Just bail.
2275 Log.w(TAG, "LoaderTask running with no launcher");
2276 return;
2277 }
2278
Winson Chung4a2afa32012-07-19 14:53:05 -07002279 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2280 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002281 oldCallbacks.getCurrentWorkspaceScreen();
2282
2283 // Load all the items that are on the current page first (and in the process, unbind
2284 // all the existing workspace items before we call startBinding() below.
2285 unbindWorkspaceItemsOnMainThread();
2286 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2287 ArrayList<LauncherAppWidgetInfo> appWidgets =
2288 new ArrayList<LauncherAppWidgetInfo>();
2289 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2290 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002292 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293 workspaceItems.addAll(sBgWorkspaceItems);
2294 appWidgets.addAll(sBgAppWidgets);
2295 folders.putAll(sBgFolders);
2296 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 }
2299
2300 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2301 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2302 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2303 new ArrayList<LauncherAppWidgetInfo>();
2304 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2305 new ArrayList<LauncherAppWidgetInfo>();
2306 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2307 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2308
2309 // Separate the items that are on the current screen, and all the other remaining items
2310 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2311 otherWorkspaceItems);
2312 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2313 otherAppWidgets);
2314 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2315 otherFolders);
2316 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2317 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2318
2319 // Tell the workspace that we're about to start binding items
2320 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002321 public void run() {
2322 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2323 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002324 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002325 }
2326 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002327 };
Winson Chung81b52252012-08-27 15:34:29 -07002328 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002329
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2331
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 // Load items on the current page
2333 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2334 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002335 if (isLoadingSynchronously) {
2336 r = new Runnable() {
2337 public void run() {
2338 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2339 if (callbacks != null) {
2340 callbacks.onPageBoundSynchronously(currentScreen);
2341 }
2342 }
2343 };
Winson Chung81b52252012-08-27 15:34:29 -07002344 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002345 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002346
Winson Chung4a2afa32012-07-19 14:53:05 -07002347 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2348 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002349 mDeferredBindRunnables.clear();
2350 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002351 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002352
2353 // Tell the workspace that we're done binding items
2354 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002355 public void run() {
2356 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2357 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002358 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002359 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002360
Winson Chung98e030b2012-05-07 16:01:11 -07002361 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002362 if (DEBUG_LOADERS) {
2363 Log.d(TAG, "bound workspace in "
2364 + (SystemClock.uptimeMillis()-t) + "ms");
2365 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002366
2367 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002368 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002369 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002370 if (isLoadingSynchronously) {
2371 mDeferredBindRunnables.add(r);
2372 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002373 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002374 }
Joe Onorato36115782010-06-17 13:28:48 -04002375 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002376
Joe Onorato36115782010-06-17 13:28:48 -04002377 private void loadAndBindAllApps() {
2378 if (DEBUG_LOADERS) {
2379 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2380 }
2381 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002382 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002383 synchronized (LoaderTask.this) {
2384 if (mStopped) {
2385 return;
2386 }
2387 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002388 }
Joe Onorato36115782010-06-17 13:28:48 -04002389 } else {
2390 onlyBindAllApps();
2391 }
2392 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002393
Joe Onorato36115782010-06-17 13:28:48 -04002394 private void onlyBindAllApps() {
2395 final Callbacks oldCallbacks = mCallbacks.get();
2396 if (oldCallbacks == null) {
2397 // This launcher has exited and nobody bothered to tell us. Just bail.
2398 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2399 return;
2400 }
2401
2402 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002403 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002404 final ArrayList<AppInfo> list
2405 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002406 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002407 public void run() {
2408 final long t = SystemClock.uptimeMillis();
2409 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2410 if (callbacks != null) {
2411 callbacks.bindAllApplications(list);
2412 }
2413 if (DEBUG_LOADERS) {
2414 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2415 + (SystemClock.uptimeMillis()-t) + "ms");
2416 }
2417 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002418 };
2419 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002420 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002421 r.run();
2422 } else {
2423 mHandler.post(r);
2424 }
Joe Onorato36115782010-06-17 13:28:48 -04002425 }
2426
Winson Chung64359a52013-07-08 17:17:08 -07002427 private void loadAllApps() {
2428 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002429
2430 // Don't use these two variables in any of the callback runnables.
2431 // Otherwise we hold a reference to them.
2432 final Callbacks oldCallbacks = mCallbacks.get();
2433 if (oldCallbacks == null) {
2434 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002435 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002436 return;
2437 }
2438
Winson Chung64359a52013-07-08 17:17:08 -07002439 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002440 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2441 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2442
Winson Chung64359a52013-07-08 17:17:08 -07002443 // Clear the list of apps
2444 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002445
Winson Chung64359a52013-07-08 17:17:08 -07002446 // Query for the set of apps
2447 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2448 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2449 if (DEBUG_LOADERS) {
2450 Log.d(TAG, "queryIntentActivities took "
2451 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2452 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2453 }
2454 // Fail if we don't have any apps
2455 if (apps == null || apps.isEmpty()) {
2456 return;
2457 }
2458 // Sort the applications by name
2459 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2460 Collections.sort(apps,
2461 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2462 if (DEBUG_LOADERS) {
2463 Log.d(TAG, "sort took "
2464 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002465 }
2466
Winson Chung64359a52013-07-08 17:17:08 -07002467 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002468 for (int i = 0; i < apps.size(); i++) {
2469 // This builds the icon bitmaps.
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002470 mBgAllAppsList.add(new AppInfo(packageManager, apps.get(i),
Winson Chung64359a52013-07-08 17:17:08 -07002471 mIconCache, mLabelCache));
2472 }
2473
2474 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002475 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2476 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002477
2478 // Post callback on main thread
2479 mHandler.post(new Runnable() {
2480 public void run() {
2481 final long bindTime = SystemClock.uptimeMillis();
2482 if (callbacks != null) {
2483 callbacks.bindAllApplications(added);
2484 if (DEBUG_LOADERS) {
2485 Log.d(TAG, "bound " + added.size() + " apps in "
2486 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2487 }
2488 } else {
2489 Log.i(TAG, "not binding apps: no Launcher activity");
2490 }
2491 }
2492 });
2493
Joe Onorato36115782010-06-17 13:28:48 -04002494 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002495 Log.d(TAG, "Icons processed in "
2496 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002497 }
2498 }
2499
2500 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002501 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002502 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2503 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2504 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2505 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2506 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2507 }
Joe Onorato36115782010-06-17 13:28:48 -04002508 }
2509 }
2510
2511 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002512 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002513 }
2514
2515 private class PackageUpdatedTask implements Runnable {
2516 int mOp;
2517 String[] mPackages;
2518
2519 public static final int OP_NONE = 0;
2520 public static final int OP_ADD = 1;
2521 public static final int OP_UPDATE = 2;
2522 public static final int OP_REMOVE = 3; // uninstlled
2523 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2524
2525
2526 public PackageUpdatedTask(int op, String[] packages) {
2527 mOp = op;
2528 mPackages = packages;
2529 }
2530
2531 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002532 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002533
2534 final String[] packages = mPackages;
2535 final int N = packages.length;
2536 switch (mOp) {
2537 case OP_ADD:
2538 for (int i=0; i<N; i++) {
2539 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002540 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002541 }
2542 break;
2543 case OP_UPDATE:
2544 for (int i=0; i<N; i++) {
2545 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002546 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002547 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002548 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002549 }
2550 break;
2551 case OP_REMOVE:
2552 case OP_UNAVAILABLE:
2553 for (int i=0; i<N; i++) {
2554 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002555 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002556 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002557 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002558 }
2559 break;
2560 }
2561
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002562 ArrayList<AppInfo> added = null;
2563 ArrayList<AppInfo> modified = null;
2564 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002565
Adam Cohen487f7dd2012-06-28 18:12:10 -07002566 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002567 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002568 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002569 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002570 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002571 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002572 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002573 }
Winson Chung5d55f332012-07-16 20:45:03 -07002574 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002575 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002576 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002577 }
2578
Joe Onorato36115782010-06-17 13:28:48 -04002579 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2580 if (callbacks == null) {
2581 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2582 return;
2583 }
2584
2585 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002586 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002587 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002588 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07002589 addAndBindAddedApps(context, addedInfos, cb, added);
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
2591 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002592 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002593
2594 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002595 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002596 ArrayList<ItemInfo> infos =
2597 getItemInfoForComponentName(a.componentName);
2598 for (ItemInfo i : infos) {
2599 if (isShortcutInfoUpdateable(i)) {
2600 ShortcutInfo info = (ShortcutInfo) i;
2601 info.title = a.title.toString();
2602 updateItemInDatabase(context, info);
2603 }
2604 }
2605 }
2606
Joe Onorato36115782010-06-17 13:28:48 -04002607 mHandler.post(new Runnable() {
2608 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002609 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2610 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002611 callbacks.bindAppsUpdated(modifiedFinal);
2612 }
2613 }
2614 });
2615 }
Winson Chung83892cc2013-05-01 16:53:33 -07002616 // If a package has been removed, or an app has been removed as a result of
2617 // an update (for example), make the removed callback.
2618 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002619 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002620 final ArrayList<String> removedPackageNames =
2621 new ArrayList<String>(Arrays.asList(packages));
2622
Winson Chung64359a52013-07-08 17:17:08 -07002623 // Update the launcher db to reflect the removal of apps
2624 if (packageRemoved) {
2625 for (String pn : removedPackageNames) {
2626 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2627 for (ItemInfo i : infos) {
2628 deleteItemFromDatabase(context, i);
2629 }
2630 }
2631 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002632 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002633 ArrayList<ItemInfo> infos =
2634 getItemInfoForComponentName(a.componentName);
2635 for (ItemInfo i : infos) {
2636 deleteItemFromDatabase(context, i);
2637 }
2638 }
2639 }
2640
Joe Onorato36115782010-06-17 13:28:48 -04002641 mHandler.post(new Runnable() {
2642 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002643 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2644 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002645 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002646 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002647 }
2648 }
2649 });
Joe Onoratobe386092009-11-17 17:32:16 -08002650 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002651
Michael Jurkac402cd92013-05-20 15:49:32 +02002652 final ArrayList<Object> widgetsAndShortcuts =
2653 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002654 mHandler.post(new Runnable() {
2655 @Override
2656 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002657 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2658 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002659 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002660 }
2661 }
2662 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002663
2664 // Write all the logs to disk
2665 Launcher.addDumpLog(TAG, "10249126 - PackageUpdatedTask - dumping logs to disk", true);
2666 mHandler.post(new Runnable() {
2667 public void run() {
2668 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2669 if (callbacks == cb && cb != null) {
2670 callbacks.dumpLogsToLocalData(false);
2671 }
2672 }
2673 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002674 }
2675 }
2676
Michael Jurkac402cd92013-05-20 15:49:32 +02002677 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2678 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2679 PackageManager packageManager = context.getPackageManager();
2680 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2681 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2682 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2683 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2684 Collections.sort(widgetsAndShortcuts,
2685 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2686 return widgetsAndShortcuts;
2687 }
2688
Winson Chung1323b482013-08-05 12:41:55 -07002689 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002690 if (cn == null) {
2691 return false;
2692 }
2693
2694 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002695 // Skip if the application is disabled
2696 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2697 if (!pi.applicationInfo.enabled) {
2698 return false;
2699 }
2700
2701 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002702 return (pm.getActivityInfo(cn, 0) != null);
2703 } catch (NameNotFoundException e) {
2704 return false;
2705 }
2706 }
2707
Joe Onorato9c1289c2009-08-17 11:03:03 -04002708 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002709 * This is called from the code that adds shortcuts from the intent receiver. This
2710 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002711 */
Joe Onorato56d82912010-03-07 14:32:10 -05002712 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002713 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002714 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002715
Joe Onorato56d82912010-03-07 14:32:10 -05002716 /**
2717 * Make an ShortcutInfo object for a shortcut that is an application.
2718 *
2719 * If c is not null, then it will be used to fill in missing data like the title and icon.
2720 */
2721 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002722 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002723 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002724 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002725 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002726 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002727 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002728 } else {
2729 try {
2730 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2731 info.initFlagsAndFirstInstallTime(pi);
2732 } catch (NameNotFoundException e) {
2733 Log.d(TAG, "getPackInfo failed for package " +
2734 componentName.getPackageName());
2735 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002736 }
2737
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002738 // TODO: See if the PackageManager knows about this case. If it doesn't
2739 // then return null & delete this.
2740
Joe Onorato56d82912010-03-07 14:32:10 -05002741 // the resource -- This may implicitly give us back the fallback icon,
2742 // but don't worry about that. All we're doing with usingFallbackIcon is
2743 // to avoid saving lots of copies of that in the database, and most apps
2744 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002745
2746 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2747 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2748 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002749 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002750 ResolveInfo resolveInfo = null;
2751 ComponentName oldComponent = intent.getComponent();
2752 Intent newIntent = new Intent(intent.getAction(), null);
2753 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2754 newIntent.setPackage(oldComponent.getPackageName());
2755 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2756 for (ResolveInfo i : infos) {
2757 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2758 i.activityInfo.name);
2759 if (cn.equals(oldComponent)) {
2760 resolveInfo = i;
2761 }
2762 }
2763 if (resolveInfo == null) {
2764 resolveInfo = manager.resolveActivity(intent, 0);
2765 }
Joe Onorato56d82912010-03-07 14:32:10 -05002766 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002767 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002768 }
Joe Onorato56d82912010-03-07 14:32:10 -05002769 // the db
2770 if (icon == null) {
2771 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002772 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002773 }
2774 }
2775 // the fallback icon
2776 if (icon == null) {
2777 icon = getFallbackIcon();
2778 info.usingFallbackIcon = true;
2779 }
2780 info.setIcon(icon);
2781
2782 // from the resource
2783 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002784 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2785 if (labelCache != null && labelCache.containsKey(key)) {
2786 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002787 } else {
2788 info.title = resolveInfo.activityInfo.loadLabel(manager);
2789 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002790 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002791 }
2792 }
Joe Onorato56d82912010-03-07 14:32:10 -05002793 }
2794 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002796 if (c != null) {
2797 info.title = c.getString(titleIndex);
2798 }
2799 }
2800 // fall back to the class name of the activity
2801 if (info.title == null) {
2802 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2805 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002807
Winson Chung64359a52013-07-08 17:17:08 -07002808 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2809 ItemInfoFilter f) {
2810 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2811 for (ItemInfo i : infos) {
2812 if (i instanceof ShortcutInfo) {
2813 ShortcutInfo info = (ShortcutInfo) i;
2814 ComponentName cn = info.intent.getComponent();
2815 if (cn != null && f.filterItem(null, info, cn)) {
2816 filtered.add(info);
2817 }
2818 } else if (i instanceof FolderInfo) {
2819 FolderInfo info = (FolderInfo) i;
2820 for (ShortcutInfo s : info.contents) {
2821 ComponentName cn = s.intent.getComponent();
2822 if (cn != null && f.filterItem(info, s, cn)) {
2823 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002824 }
2825 }
Winson Chung64359a52013-07-08 17:17:08 -07002826 } else if (i instanceof LauncherAppWidgetInfo) {
2827 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2828 ComponentName cn = info.providerName;
2829 if (cn != null && f.filterItem(null, info, cn)) {
2830 filtered.add(info);
2831 }
Winson Chung8a435102012-08-30 17:16:53 -07002832 }
2833 }
Winson Chung64359a52013-07-08 17:17:08 -07002834 return new ArrayList<ItemInfo>(filtered);
2835 }
2836
2837 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002838 ItemInfoFilter filter = new ItemInfoFilter() {
2839 @Override
2840 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2841 return cn.getPackageName().equals(pn);
2842 }
2843 };
2844 return filterItemInfos(sBgItemsIdMap.values(), filter);
2845 }
2846
2847 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002848 ItemInfoFilter filter = new ItemInfoFilter() {
2849 @Override
2850 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2851 return cn.equals(cname);
2852 }
2853 };
2854 return filterItemInfos(sBgItemsIdMap.values(), filter);
2855 }
2856
2857 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2858 if (i instanceof ShortcutInfo) {
2859 ShortcutInfo info = (ShortcutInfo) i;
2860 // We need to check for ACTION_MAIN otherwise getComponent() might
2861 // return null for some shortcuts (for instance, for shortcuts to
2862 // web pages.)
2863 Intent intent = info.intent;
2864 ComponentName name = intent.getComponent();
2865 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2866 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2867 return true;
2868 }
2869 }
2870 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002871 }
2872
2873 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002874 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002876 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002877 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2878 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879
Joe Onorato56d82912010-03-07 14:32:10 -05002880 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002881 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002884 // TODO: If there's an explicit component and we can't install that, delete it.
2885
Joe Onorato56d82912010-03-07 14:32:10 -05002886 info.title = c.getString(titleIndex);
2887
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 int iconType = c.getInt(iconTypeIndex);
2889 switch (iconType) {
2890 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2891 String packageName = c.getString(iconPackageIndex);
2892 String resourceName = c.getString(iconResourceIndex);
2893 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002894 info.customIcon = false;
2895 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002898 if (resources != null) {
2899 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002900 icon = Utilities.createIconBitmap(
2901 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002904 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 }
Joe Onorato56d82912010-03-07 14:32:10 -05002906 // the db
2907 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002908 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002909 }
2910 // the fallback icon
2911 if (icon == null) {
2912 icon = getFallbackIcon();
2913 info.usingFallbackIcon = true;
2914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 break;
2916 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002917 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002918 if (icon == null) {
2919 icon = getFallbackIcon();
2920 info.customIcon = false;
2921 info.usingFallbackIcon = true;
2922 } else {
2923 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 break;
2926 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002927 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002928 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 info.customIcon = false;
2930 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002932 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002933 return info;
2934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935
Michael Jurka931dc972011-08-05 15:08:15 -07002936 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002937 @SuppressWarnings("all") // suppress dead code warning
2938 final boolean debug = false;
2939 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002940 Log.d(TAG, "getIconFromCursor app="
2941 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2942 }
2943 byte[] data = c.getBlob(iconIndex);
2944 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002945 return Utilities.createIconBitmap(
2946 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002947 } catch (Exception e) {
2948 return null;
2949 }
2950 }
2951
Winson Chung3d503fb2011-07-13 17:25:49 -07002952 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2953 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002954 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002955 if (info == null) {
2956 return null;
2957 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002958 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002959
2960 return info;
2961 }
2962
Winson Chunga9abd0e2010-10-27 17:18:37 -07002963 /**
Winson Chung55cef262010-10-28 14:14:18 -07002964 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2965 */
2966 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2967 ComponentName component) {
2968 List<AppWidgetProviderInfo> widgets =
2969 AppWidgetManager.getInstance(context).getInstalledProviders();
2970 for (AppWidgetProviderInfo info : widgets) {
2971 if (info.provider.equals(component)) {
2972 return info;
2973 }
2974 }
2975 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002976 }
2977
Winson Chung68846fd2010-10-29 11:00:27 -07002978 /**
2979 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2980 */
2981 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2982 final PackageManager packageManager = context.getPackageManager();
2983 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2984 new ArrayList<WidgetMimeTypeHandlerData>();
2985
2986 final Intent supportsIntent =
2987 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2988 supportsIntent.setType(mimeType);
2989
2990 // Create a set of widget configuration components that we can test against
2991 final List<AppWidgetProviderInfo> widgets =
2992 AppWidgetManager.getInstance(context).getInstalledProviders();
2993 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2994 new HashMap<ComponentName, AppWidgetProviderInfo>();
2995 for (AppWidgetProviderInfo info : widgets) {
2996 configurationComponentToWidget.put(info.configure, info);
2997 }
2998
2999 // Run through each of the intents that can handle this type of clip data, and cross
3000 // reference them with the components that are actual configuration components
3001 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3002 PackageManager.MATCH_DEFAULT_ONLY);
3003 for (ResolveInfo info : activities) {
3004 final ActivityInfo activityInfo = info.activityInfo;
3005 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3006 activityInfo.name);
3007 if (configurationComponentToWidget.containsKey(infoComponent)) {
3008 supportedConfigurationActivities.add(
3009 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3010 configurationComponentToWidget.get(infoComponent)));
3011 }
3012 }
3013 return supportedConfigurationActivities;
3014 }
3015
Winson Chunga9abd0e2010-10-27 17:18:37 -07003016 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003017 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3018 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3019 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3020
Adam Cohend9198822011-11-22 16:42:47 -08003021 if (intent == null) {
3022 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3023 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3024 return null;
3025 }
3026
Joe Onorato0589f0f2010-02-08 13:44:00 -08003027 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003028 boolean customIcon = false;
3029 ShortcutIconResource iconResource = null;
3030
3031 if (bitmap != null && bitmap instanceof Bitmap) {
3032 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003033 customIcon = true;
3034 } else {
3035 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3036 if (extra != null && extra instanceof ShortcutIconResource) {
3037 try {
3038 iconResource = (ShortcutIconResource) extra;
3039 final PackageManager packageManager = context.getPackageManager();
3040 Resources resources = packageManager.getResourcesForApplication(
3041 iconResource.packageName);
3042 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003043 icon = Utilities.createIconBitmap(
3044 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003045 } catch (Exception e) {
3046 Log.w(TAG, "Could not load shortcut icon: " + extra);
3047 }
3048 }
3049 }
3050
Michael Jurkac9d95c52011-08-29 14:03:34 -07003051 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003052
3053 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003054 if (fallbackIcon != null) {
3055 icon = fallbackIcon;
3056 } else {
3057 icon = getFallbackIcon();
3058 info.usingFallbackIcon = true;
3059 }
Joe Onorato56d82912010-03-07 14:32:10 -05003060 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003061 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003062
Joe Onorato0589f0f2010-02-08 13:44:00 -08003063 info.title = name;
3064 info.intent = intent;
3065 info.customIcon = customIcon;
3066 info.iconResource = iconResource;
3067
3068 return info;
3069 }
3070
Winson Chungaac01e12011-08-17 10:37:13 -07003071 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3072 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003073 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003074 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003075 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003076 }
Joe Onorato56d82912010-03-07 14:32:10 -05003077 // If this icon doesn't have a custom icon, check to see
3078 // what's stored in the DB, and if it doesn't match what
3079 // we're going to show, store what we are going to show back
3080 // into the DB. We do this so when we're loading, if the
3081 // package manager can't find an icon (for example because
3082 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003083 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003084 cache.put(info, c.getBlob(iconIndex));
3085 return true;
3086 }
3087 return false;
3088 }
3089 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3090 boolean needSave = false;
3091 try {
3092 if (data != null) {
3093 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3094 Bitmap loaded = info.getIcon(mIconCache);
3095 needSave = !saved.sameAs(loaded);
3096 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003097 needSave = true;
3098 }
Winson Chungaac01e12011-08-17 10:37:13 -07003099 } catch (Exception e) {
3100 needSave = true;
3101 }
3102 if (needSave) {
3103 Log.d(TAG, "going to save icon bitmap for info=" + info);
3104 // This is slower than is ideal, but this only happens once
3105 // or when the app is updated with a new icon.
3106 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003107 }
3108 }
3109
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003111 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 * or make a new one.
3113 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003114 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 // See if a placeholder was created for us already
3116 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003117 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003119 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 folders.put(id, folderInfo);
3121 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003122 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 }
3124
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003125 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003126 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003127 return new Comparator<AppInfo>() {
3128 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003129 int result = collator.compare(a.title.toString(), b.title.toString());
3130 if (result == 0) {
3131 result = a.componentName.compareTo(b.componentName);
3132 }
3133 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003134 }
Winson Chung11904872012-09-17 16:58:46 -07003135 };
3136 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003137 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3138 = new Comparator<AppInfo>() {
3139 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003140 if (a.firstInstallTime < b.firstInstallTime) return 1;
3141 if (a.firstInstallTime > b.firstInstallTime) return -1;
3142 return 0;
3143 }
3144 };
Winson Chung11904872012-09-17 16:58:46 -07003145 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3146 final Collator collator = Collator.getInstance();
3147 return new Comparator<AppWidgetProviderInfo>() {
3148 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3149 return collator.compare(a.label.toString(), b.label.toString());
3150 }
3151 };
3152 }
Winson Chung5308f242011-08-18 12:12:41 -07003153 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3154 if (info.activityInfo != null) {
3155 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3156 } else {
3157 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3158 }
3159 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003160 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003161 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003162 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003163 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003164 ShortcutNameComparator(PackageManager pm) {
3165 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003166 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003167 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003168 }
3169 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3170 mPackageManager = pm;
3171 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003172 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003173 }
3174 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003176 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3177 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3178 if (mLabelCache.containsKey(keyA)) {
3179 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003180 } else {
3181 labelA = a.loadLabel(mPackageManager).toString();
3182
Winson Chung5308f242011-08-18 12:12:41 -07003183 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003184 }
Winson Chung5308f242011-08-18 12:12:41 -07003185 if (mLabelCache.containsKey(keyB)) {
3186 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003187 } else {
3188 labelB = b.loadLabel(mPackageManager).toString();
3189
Winson Chung5308f242011-08-18 12:12:41 -07003190 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003191 }
Winson Chung11904872012-09-17 16:58:46 -07003192 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003193 }
3194 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003195 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003196 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003197 private PackageManager mPackageManager;
3198 private HashMap<Object, String> mLabelCache;
3199 WidgetAndShortcutNameComparator(PackageManager pm) {
3200 mPackageManager = pm;
3201 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003202 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003203 }
3204 public final int compare(Object a, Object b) {
3205 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003206 if (mLabelCache.containsKey(a)) {
3207 labelA = mLabelCache.get(a);
3208 } else {
3209 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003210 ((AppWidgetProviderInfo) a).label :
3211 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003212 mLabelCache.put(a, labelA);
3213 }
3214 if (mLabelCache.containsKey(b)) {
3215 labelB = mLabelCache.get(b);
3216 } else {
3217 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003218 ((AppWidgetProviderInfo) b).label :
3219 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003220 mLabelCache.put(b, labelB);
3221 }
Winson Chung11904872012-09-17 16:58:46 -07003222 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003223 }
3224 };
Joe Onoratobe386092009-11-17 17:32:16 -08003225
3226 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003227 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003228 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3229 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3230 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3231 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003232 if (mLoaderTask != null) {
3233 mLoaderTask.dumpState();
3234 } else {
3235 Log.d(TAG, "mLoaderTask=null");
3236 }
Joe Onoratobe386092009-11-17 17:32:16 -08003237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238}