blob: aaa901ac18bdc1048a39ddc1847a53cd6905bf8f [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070044import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080046
Michael Jurkac2f801e2011-07-12 14:19:46 -070047import java.lang.ref.WeakReference;
48import java.net.URISyntaxException;
49import java.text.Collator;
50import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070051import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070052import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070056import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070057import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070058import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070059import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070060import java.util.TreeMap;
Michael Jurkac2f801e2011-07-12 14:19:46 -070061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062/**
63 * Maintains in-memory state of the Launcher. It is expected that there should be only one
64 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070065 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040067public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080068 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070070
Daniel Sandler8707e0f2013-08-15 15:54:18 -070071 // true = use a "More Apps" folder for non-workspace apps on upgrade
72 // false = strew non-workspace apps across the workspace on upgrade
73 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
74
Joe Onorato36115782010-06-17 13:28:48 -040075 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070076 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040077
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 private final Object mLock = new Object();
80 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040081 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070082 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020083 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084
Winson Chung81b52252012-08-27 15:34:29 -070085 // Specific runnable types that are run on the main thread deferred handler, this allows us to
86 // clear all queued binding runnables when the Launcher activity is destroyed.
87 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
88 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
89
90
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070091 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
92 static {
93 sWorkerThread.start();
94 }
95 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
96
Joe Onoratocc67f472010-06-08 10:54:30 -070097 // We start off with everything not loaded. After that, we assume that
98 // our monitoring of the package manager provides all updates and we never
99 // need to do a requery. These are only ever touched from the loader thread.
100 private boolean mWorkspaceLoaded;
101 private boolean mAllAppsLoaded;
102
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700103 // When we are loading pages synchronously, we can't just post the binding of items on the side
104 // pages as this delays the rotation process. Instead, we wait for a callback from the first
105 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
106 // a normal load, we also clear this set of Runnables.
107 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700111 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700112 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800113
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700114 // The lock that must be acquired before referencing any static bg data structures. Unlike
115 // other locks, this one can generally be held long-term because we never expect any of these
116 // static data structures to be referenced outside of the worker thread except on the first
117 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700118 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700119
Adam Cohen487f7dd2012-06-28 18:12:10 -0700120 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700121 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
125 // created by LauncherModel that are directly on the home screen (however, no widgets or
126 // shortcuts within folders).
127 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700128
Adam Cohen487f7dd2012-06-28 18:12:10 -0700129 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
130 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700131 new ArrayList<LauncherAppWidgetInfo>();
132
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
134 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700135
Adam Cohen487f7dd2012-06-28 18:12:10 -0700136 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
137 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700138
139 // sBgWorkspaceScreens is the ordered set of workspace screens.
140 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
141
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 // </ only access in worker thread >
143
144 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800145 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Reena Lee99a73f32011-10-24 17:27:37 -0700147 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700148
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700150 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public int getCurrentWorkspaceScreen();
152 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700153 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
154 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700155 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700156 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500157 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700158 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 public void bindAppWidget(LauncherAppWidgetInfo info);
Bjorn Bringert85f418d2013-09-06 12:50:05 +0100160 public boolean shouldShowApp(ResolveInfo app);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200161 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700162 public void bindAppsAdded(ArrayList<Long> newScreens,
163 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700164 ArrayList<ItemInfo> addAnimated,
165 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200166 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700167 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200168 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700169 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
Adam Cohen1462de32012-07-24 22:34:36 -0700172 public void onPageBoundSynchronously(int page);
Adam Cohen4caf2982013-08-20 18:54:31 -0700173 public void dumpLogsToLocalData(boolean email);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Winson Chung64359a52013-07-08 17:17:08 -0700176 public interface ItemInfoFilter {
177 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
178 }
179
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 LauncherModel(LauncherAppState app, IconCache iconCache) {
181 final Context context = app.getContext();
182
Winson Chungee055712013-07-30 14:46:24 -0700183 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 mApp = app;
Adam Cohen487f7dd2012-06-28 18:12:10 -0700185 mBgAllAppsList = new AllAppsList(iconCache);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800186 mIconCache = iconCache;
187
188 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400190
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700192 Configuration config = res.getConfiguration();
193 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 }
195
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700196 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
197 * posted on the main thread handler. */
198 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700199 runOnMainThread(r, 0);
200 }
201 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700202 if (sWorkerThread.getThreadId() == Process.myTid()) {
203 // If we are on the worker thread, post onto the main handler
204 mHandler.post(r);
205 } else {
206 r.run();
207 }
208 }
209
210 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
211 * posted on the worker thread handler. */
212 private static void runOnWorkerThread(Runnable r) {
213 if (sWorkerThread.getThreadId() == Process.myTid()) {
214 r.run();
215 } else {
216 // If we are not on the worker thread, then post to the worker handler
217 sWorker.post(r);
218 }
219 }
220
Winson Chungc9168342013-06-26 14:54:55 -0700221 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
222 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700223 LauncherAppState app = LauncherAppState.getInstance();
224 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
225 final int xCount = (int) grid.numColumns;
226 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700227 boolean[][] occupied = new boolean[xCount][yCount];
228
229 int cellX, cellY, spanX, spanY;
230 for (int i = 0; i < items.size(); ++i) {
231 final ItemInfo item = items.get(i);
232 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
233 if (item.screenId == screen) {
234 cellX = item.cellX;
235 cellY = item.cellY;
236 spanX = item.spanX;
237 spanY = item.spanY;
238 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
239 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
240 occupied[x][y] = true;
241 }
242 }
243 }
244 }
245 }
246
247 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
248 }
249 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700250 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700251 int firstScreenIndex,
252 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700253 // Lock on the app so that we don't try and get the items while apps are being added
254 LauncherAppState app = LauncherAppState.getInstance();
255 LauncherModel model = app.getModel();
256 boolean found = false;
257 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700258 if (sWorkerThread.getThreadId() != Process.myTid()) {
259 // Flush the LauncherModel worker thread, so that if we just did another
260 // processInstallShortcut, we give it time for its shortcut to get added to the
261 // database (getItemsInLocalCoordinates reads the database)
262 model.flushWorkerThread();
263 }
Winson Chungc9168342013-06-26 14:54:55 -0700264 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700265
266 // Try adding to the workspace screens incrementally, starting at the default or center
267 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700268 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
269 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700270 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 int[] tmpCoordinates = new int[2];
272 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700273 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700274 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700275 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700276 }
277 }
278 }
Winson Chungc9168342013-06-26 14:54:55 -0700279 return null;
280 }
281
Winson Chungc58497e2013-09-03 17:48:37 -0700282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
283 final ArrayList<AppInfo> addedApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700284 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -0700285 addAndBindAddedApps(context, added, cb, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700286 }
287 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> added,
Winson Chungc58497e2013-09-03 17:48:37 -0700288 final Callbacks callbacks, final ArrayList<AppInfo> addedApps) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700289 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps()", true);
Winson Chung997a9232013-07-24 15:33:46 -0700290 if (added.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700291 return;
Winson Chung997a9232013-07-24 15:33:46 -0700292 }
Winson Chung64359a52013-07-08 17:17:08 -0700293 // Process the newly added applications and add them to the database first
294 Runnable r = new Runnable() {
295 public void run() {
296 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
297 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
298
Winson Chung76828c82013-08-19 15:43:29 -0700299 // Get the list of workspace screens. We need to append to this list and
300 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
301 // called.
302 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
303 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
304 for (Integer i : orderedScreens.keySet()) {
305 long screenId = orderedScreens.get(i);
306 workspaceScreens.add(screenId);
307 }
308
Winson Chung64359a52013-07-08 17:17:08 -0700309 synchronized(sBgLock) {
Winson Chung997a9232013-07-24 15:33:46 -0700310 Iterator<ItemInfo> iter = added.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700311 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700312 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700313 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700314 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700315
316 // Short-circuit this logic if the icon exists somewhere on the workspace
317 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
318 continue;
319 }
320
321 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700322 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700323 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700324 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700325 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200326 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700327
328 // If we can't find a valid position, then just add a new screen.
329 // This takes time so we need to re-queue the add until the new
330 // page is added. Create as many screens as necessary to satisfy
331 // the startSearchPageIndex.
332 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700333 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700334 while (numPagesToAdd > 0) {
335 long screenId = lp.generateNewScreenId();
Adam Cohen4caf2982013-08-20 18:54:31 -0700336 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps(" + screenId + ")", true);
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700338 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700339 addedWorkspaceScreensFinal.add(screenId);
340 numPagesToAdd--;
341 }
Winson Chung76828c82013-08-19 15:43:29 -0700342
Winson Chung64359a52013-07-08 17:17:08 -0700343 // Find the coordinate again
344 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700345 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700346 }
347 if (coords == null) {
348 throw new RuntimeException("Coordinates should not be null");
349 }
350
Winson Chung997a9232013-07-24 15:33:46 -0700351 ShortcutInfo shortcutInfo;
352 if (a instanceof ShortcutInfo) {
353 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200354 } else if (a instanceof AppInfo) {
355 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700356 } else {
357 throw new RuntimeException("Unexpected info type");
358 }
Winson Chung64359a52013-07-08 17:17:08 -0700359 // Add the shortcut to the db
360 addItemToDatabase(context, shortcutInfo,
361 LauncherSettings.Favorites.CONTAINER_DESKTOP,
362 coords.first, coords.second[0], coords.second[1], false);
363 // Save the ShortcutInfo for binding in the workspace
364 addedShortcutsFinal.add(shortcutInfo);
365 }
366 }
367
Adam Cohen4caf2982013-08-20 18:54:31 -0700368 Launcher.addDumpLog(TAG, "10249126 - addAndBindAddedApps - updateWorkspaceScreenOrder(" + workspaceScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -0700369
Winson Chung76828c82013-08-19 15:43:29 -0700370 // Update the workspace screens
371 updateWorkspaceScreenOrder(context, workspaceScreens);
372
Winson Chung997a9232013-07-24 15:33:46 -0700373 if (!addedShortcutsFinal.isEmpty()) {
374 runOnMainThread(new Runnable() {
375 public void run() {
376 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
377 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700378 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
379 long lastScreenId = info.screenId;
380 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
381 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
382 for (ItemInfo i : addedShortcutsFinal) {
383 if (i.screenId == lastScreenId) {
384 addAnimated.add(i);
385 } else {
386 addNotAnimated.add(i);
387 }
388 }
Winson Chungd64d1762013-08-20 14:37:16 -0700389 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chungc58497e2013-09-03 17:48:37 -0700390 addNotAnimated, addAnimated, addedApps);
Winson Chung997a9232013-07-24 15:33:46 -0700391 }
Winson Chung64359a52013-07-08 17:17:08 -0700392 }
Winson Chung997a9232013-07-24 15:33:46 -0700393 });
394 }
Winson Chung64359a52013-07-08 17:17:08 -0700395 }
396 };
397 runOnWorkerThread(r);
398 }
399
Joe Onorato56d82912010-03-07 14:32:10 -0500400 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800401 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403
Winson Chung81b52252012-08-27 15:34:29 -0700404 public void unbindItemInfosAndClearQueuedBindRunnables() {
405 if (sWorkerThread.getThreadId() == Process.myTid()) {
406 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
407 "main thread");
408 }
409
410 // Clear any deferred bind runnables
411 mDeferredBindRunnables.clear();
412 // Remove any queued bind runnables
413 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
414 // Unbind all the workspace items
415 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700416 }
417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700419 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700420 // Ensure that we don't use the same workspace items data structure on the main thread
421 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
423 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700424 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
426 tmpAppWidgets.addAll(sBgAppWidgets);
427 }
428 Runnable r = new Runnable() {
429 @Override
430 public void run() {
431 for (ItemInfo item : tmpWorkspaceItems) {
432 item.unbind();
433 }
434 for (ItemInfo item : tmpAppWidgets) {
435 item.unbind();
436 }
437 }
438 };
439 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700440 }
441
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 /**
443 * Adds an item to the DB if it was not created previously, or move it to a new
444 * <container, screen, cellX, cellY>
445 */
446 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700447 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 if (item.container == ItemInfo.NO_ID) {
449 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700450 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 } else {
452 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700453 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455 }
456
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700457 static void checkItemInfoLocked(
458 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
459 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
460 if (modelItem != null && item != modelItem) {
461 // check all the data is consistent
462 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
463 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
464 ShortcutInfo shortcut = (ShortcutInfo) item;
465 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
466 modelShortcut.intent.filterEquals(shortcut.intent) &&
467 modelShortcut.id == shortcut.id &&
468 modelShortcut.itemType == shortcut.itemType &&
469 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700470 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700471 modelShortcut.cellX == shortcut.cellX &&
472 modelShortcut.cellY == shortcut.cellY &&
473 modelShortcut.spanX == shortcut.spanX &&
474 modelShortcut.spanY == shortcut.spanY &&
475 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
476 (modelShortcut.dropPos != null &&
477 shortcut.dropPos != null &&
478 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
479 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
480 // For all intents and purposes, this is the same object
481 return;
482 }
483 }
484
485 // the modelItem needs to match up perfectly with item if our model is
486 // to be consistent with the database-- for now, just require
487 // modelItem == item or the equality check above
488 String msg = "item: " + ((item != null) ? item.toString() : "null") +
489 "modelItem: " +
490 ((modelItem != null) ? modelItem.toString() : "null") +
491 "Error: ItemInfo passed to checkItemInfo doesn't match original";
492 RuntimeException e = new RuntimeException(msg);
493 if (stackTrace != null) {
494 e.setStackTrace(stackTrace);
495 }
Adam Cohene25af792013-06-06 23:08:25 -0700496 // TODO: something breaks this in the upgrade path
497 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700498 }
499 }
500
Michael Jurka816474f2012-06-25 14:49:02 -0700501 static void checkItemInfo(final ItemInfo item) {
502 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
503 final long itemId = item.id;
504 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 public void run() {
506 synchronized (sBgLock) {
507 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700508 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700509 }
510 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700511 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700512 }
513
Michael Jurkac9d95c52011-08-29 14:03:34 -0700514 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
515 final ItemInfo item, final String callingFunction) {
516 final long itemId = item.id;
517 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
518 final ContentResolver cr = context.getContentResolver();
519
Adam Cohen487f7dd2012-06-28 18:12:10 -0700520 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521 Runnable r = new Runnable() {
522 public void run() {
523 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700524 updateItemArrays(item, itemId, stackTrace);
525 }
526 };
527 runOnWorkerThread(r);
528 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700529
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
531 final ArrayList<ItemInfo> items, final String callingFunction) {
532 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700533
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700534 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
535 Runnable r = new Runnable() {
536 public void run() {
537 ArrayList<ContentProviderOperation> ops =
538 new ArrayList<ContentProviderOperation>();
539 int count = items.size();
540 for (int i = 0; i < count; i++) {
541 ItemInfo item = items.get(i);
542 final long itemId = item.id;
543 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
544 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700545
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700546 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
547 updateItemArrays(item, itemId, stackTrace);
548
549 }
550 try {
551 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
552 } catch (Exception e) {
553 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
555 }
556 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700557 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700558 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700559
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700560 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
561 // Lock on mBgLock *after* the db operation
562 synchronized (sBgLock) {
563 checkItemInfoLocked(itemId, item, stackTrace);
564
565 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
566 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
567 // Item is in a folder, make sure this folder exists
568 if (!sBgFolders.containsKey(item.container)) {
569 // An items container is being set to a that of an item which is not in
570 // the list of Folders.
571 String msg = "item: " + item + " container being set to: " +
572 item.container + ", not in the list of folders";
573 Log.e(TAG, msg);
574 Launcher.dumpDebugLogsToConsole();
575 }
576 }
577
578 // Items are added/removed from the corresponding FolderInfo elsewhere, such
579 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
580 // that are on the desktop, as appropriate
581 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
582 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
583 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
584 switch (modelItem.itemType) {
585 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
586 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
587 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
588 if (!sBgWorkspaceItems.contains(modelItem)) {
589 sBgWorkspaceItems.add(modelItem);
590 }
591 break;
592 default:
593 break;
594 }
595 } else {
596 sBgWorkspaceItems.remove(modelItem);
597 }
598 }
599 }
600
Michael Jurkac7700af2013-05-14 20:17:58 +0200601 public void flushWorkerThread() {
602 mFlushingWorkerThread = true;
603 Runnable waiter = new Runnable() {
604 public void run() {
605 synchronized (this) {
606 notifyAll();
607 mFlushingWorkerThread = false;
608 }
609 }
610 };
611
612 synchronized(waiter) {
613 runOnWorkerThread(waiter);
614 if (mLoaderTask != null) {
615 synchronized(mLoaderTask) {
616 mLoaderTask.notify();
617 }
618 }
619 boolean success = false;
620 while (!success) {
621 try {
622 waiter.wait();
623 success = true;
624 } catch (InterruptedException e) {
625 }
626 }
627 }
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700632 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700633 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 final long screenId, final int cellX, final int cellY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700635 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
637 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700638 Launcher.addDumpLog(TAG, transaction, true);
639
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 item.cellX = cellX;
642 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 // We store hotseat items in canonical form which is this orientation invariant position
645 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
653 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
656 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700657 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
Michael Jurkac9d95c52011-08-29 14:03:34 -0700659 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700660 }
661
662 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
664 * cellX, cellY have already been updated on the ItemInfos.
665 */
666 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
667 final long container, final int screen) {
668
669 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
670 int count = items.size();
671
672 for (int i = 0; i < count; i++) {
673 ItemInfo item = items.get(i);
674 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700675 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700676 + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", "
677 + item.cellX + ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700678 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 item.container = container;
680
681 // We store hotseat items in canonical form which is this orientation invariant position
682 // in the hotseat
683 if (context instanceof Launcher && screen < 0 &&
684 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700685 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700686 item.cellY);
687 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689 }
690
691 final ContentValues values = new ContentValues();
692 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
693 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
694 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700696
697 contentValues.add(values);
698 }
699 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
700 }
701
702 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700703 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800704 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700705 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Brian Muramatsu5524b492012-10-02 16:55:54 -0700707 String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id +
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY +
709 ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700710 Launcher.addDumpLog(TAG, transaction, true);
711
Adam Cohend4844c32011-02-18 19:25:06 -0800712 item.cellX = cellX;
713 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 item.spanX = spanX;
715 item.spanY = spanY;
716
717 // We store hotseat items in canonical form which is this orientation invariant position
718 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700724 }
Adam Cohend4844c32011-02-18 19:25:06 -0800725
Adam Cohend4844c32011-02-18 19:25:06 -0800726 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800727 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700728 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
729 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
730 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
731 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700732 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800733
Michael Jurka816474f2012-06-25 14:49:02 -0700734 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700735 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700736
737 /**
738 * Update an item to the database in a specified container.
739 */
740 static void updateItemInDatabase(Context context, final ItemInfo item) {
741 final ContentValues values = new ContentValues();
742 item.onAddToDatabase(values);
743 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
744 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800745 }
746
747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 * Returns true if the shortcuts already exists in the database.
749 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 static boolean shortcutExists(Context context, String title, Intent intent) {
752 final ContentResolver cr = context.getContentResolver();
753 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
754 new String[] { "title", "intent" }, "title=? and intent=?",
755 new String[] { title, intent.toUri(0) }, null);
756 boolean result = false;
757 try {
758 result = c.moveToFirst();
759 } finally {
760 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700763 }
764
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700766 * Returns an ItemInfo array containing all the items in the LauncherModel.
767 * The ItemInfo.id is not set through this function.
768 */
769 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
770 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
771 final ContentResolver cr = context.getContentResolver();
772 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
773 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
774 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
775 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
776
777 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
778 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
779 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
780 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
781 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
782 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
783 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
784
785 try {
786 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700787 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700788 item.cellX = c.getInt(cellXIndex);
789 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700790 item.spanX = Math.max(1, c.getInt(spanXIndex));
791 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700792 item.container = c.getInt(containerIndex);
793 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700794 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700795
796 items.add(item);
797 }
798 } catch (Exception e) {
799 items.clear();
800 } finally {
801 c.close();
802 }
803
804 return items;
805 }
806
807 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
809 */
810 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
811 final ContentResolver cr = context.getContentResolver();
812 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
813 "_id=? and (itemType=? or itemType=?)",
814 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700815 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700816
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 try {
818 if (c.moveToFirst()) {
819 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
820 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
821 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
822 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
823 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
824 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826 FolderInfo folderInfo = null;
827 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700828 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
829 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700831 }
832
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833 folderInfo.title = c.getString(titleIndex);
834 folderInfo.id = id;
835 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700836 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700837 folderInfo.cellX = c.getInt(cellXIndex);
838 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
840 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 } finally {
843 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700844 }
845
846 return null;
847 }
848
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 /**
850 * Add an item to the database in a specified container. Sets the container, screen, cellX and
851 * cellY fields of the item. Also assigns an ID to the item.
852 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 item.cellX = cellX;
857 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 // We store hotseat items in canonical form which is this orientation invariant position
859 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
867 final ContentValues values = new ContentValues();
868 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 item.onAddToDatabase(values);
870
Michael Jurka414300a2013-08-27 15:42:35 +0200871 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700872 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700874
Michael Jurkac9d95c52011-08-29 14:03:34 -0700875 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700876 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700877 String transaction = "DbDebug Add item (" + item.title + ") to db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700878 + item.id + " (" + container + ", " + screenId + ", " + cellX + ", "
Adam Cohen487f7dd2012-06-28 18:12:10 -0700879 + cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700880 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700881
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700882 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
883 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700885 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700886 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700887 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 sBgItemsIdMap.put(item.id, item);
889 switch (item.itemType) {
890 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
891 sBgFolders.put(item.id, (FolderInfo) item);
892 // Fall through
893 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
894 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
896 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
897 sBgWorkspaceItems.add(item);
898 } else {
899 if (!sBgFolders.containsKey(item.container)) {
900 // Adding an item to a folder that doesn't exist.
901 String msg = "adding item: " + item + " to a folder that " +
902 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700903 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700904 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700905 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700907 break;
908 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
909 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
910 break;
911 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700912 }
913 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700915 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700916 }
917
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700919 * Creates a new unique child id, for a given cell span across all layouts.
920 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700921 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700924 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700925 }
926
Winson Chungaafa03c2010-06-11 17:34:16 -0700927 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700928 * Removes the specified item from the database
929 * @param context
930 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700932 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700934 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700935
Michael Jurka83df1882011-08-31 20:59:26 -0700936 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700937 public void run() {
Adam Cohen487f7dd2012-06-28 18:12:10 -0700938 String transaction = "DbDebug Delete item (" + item.title + ") from db, id: "
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX +
Adam Cohen487f7dd2012-06-28 18:12:10 -0700940 ", " + item.cellY + ")";
Adam Cohen4caf2982013-08-20 18:54:31 -0700941 Launcher.addDumpLog(TAG, transaction, true);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700942
Michael Jurkac9d95c52011-08-29 14:03:34 -0700943 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700944
945 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700946 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700947 switch (item.itemType) {
948 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
949 sBgFolders.remove(item.id);
950 for (ItemInfo info: sBgItemsIdMap.values()) {
951 if (info.container == item.id) {
952 // We are deleting a folder which still contains items that
953 // think they are contained by that folder.
954 String msg = "deleting a folder (" + item + ") which still " +
955 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700956 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 Launcher.dumpDebugLogsToConsole();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700958 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700959 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700960 sBgWorkspaceItems.remove(item);
961 break;
962 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
963 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
964 sBgWorkspaceItems.remove(item);
965 break;
966 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
967 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
968 break;
969 }
970 sBgItemsIdMap.remove(item.id);
971 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700972 }
973 }
Michael Jurka83df1882011-08-31 20:59:26 -0700974 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700975 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 }
977
978 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 * Update the order of the workspace screens in the database. The array list contains
980 * a list of screen ids in the order that they should appear.
981 */
Winson Chungc9168342013-06-26 14:54:55 -0700982 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700983 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder()", true);
Winson Chung64359a52013-07-08 17:17:08 -0700984 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 final ContentResolver cr = context.getContentResolver();
986 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
987
988 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700989 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 while (iter.hasNext()) {
991 long id = iter.next();
992 if (id < 0) {
Adam Cohen4caf2982013-08-20 18:54:31 -0700993 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - remove: " + id + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 iter.remove();
995 }
996 }
997
Winson Chung9e6a0a22013-08-27 11:58:12 -0700998 // Dump the screens copy
Adam Cohen4caf2982013-08-20 18:54:31 -0700999 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - screensCopy", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001000 for (Long l : screensCopy) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001001 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001002 }
1003
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 Runnable r = new Runnable() {
1005 @Override
1006 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001007 // Clear the table
1008 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -07001009 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001010 ContentValues[] values = new ContentValues[count];
1011 for (int i = 0; i < count; i++) {
1012 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001013 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001014 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1015 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohen4caf2982013-08-20 18:54:31 -07001016 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder(" + screenId + ", " + i + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 values[i] = v;
1018 }
1019 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001020
Winson Chungba9c37f2013-08-30 14:11:37 -07001021 synchronized (sBgLock) {
1022 // Dump the sBgWorkspaceScreens
1023 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre clear", true);
1024 for (Long l : sBgWorkspaceScreens) {
1025 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1026 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001027
Winson Chungba9c37f2013-08-30 14:11:37 -07001028 sBgWorkspaceScreens.clear();
1029 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001030
Winson Chungba9c37f2013-08-30 14:11:37 -07001031 // Dump the sBgWorkspaceScreens
1032 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post clear", true);
1033 for (Long l : sBgWorkspaceScreens) {
1034 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
1035 }
Adam Cohen4caf2982013-08-20 18:54:31 -07001036 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 }
1038 };
1039 runOnWorkerThread(r);
1040 }
1041
1042 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 * Remove the contents of the specified folder from the database
1044 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001045 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 final ContentResolver cr = context.getContentResolver();
1047
Michael Jurkac9d95c52011-08-29 14:03:34 -07001048 Runnable r = new Runnable() {
1049 public void run() {
1050 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001051 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001052 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 sBgItemsIdMap.remove(info.id);
1054 sBgFolders.remove(info.id);
1055 sBgDbIconCache.remove(info);
1056 sBgWorkspaceItems.remove(info);
1057 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001058
Michael Jurkac9d95c52011-08-29 14:03:34 -07001059 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1060 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001061 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001062 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001063 for (ItemInfo childInfo : info.contents) {
1064 sBgItemsIdMap.remove(childInfo.id);
1065 sBgDbIconCache.remove(childInfo);
1066 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001067 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001068 }
1069 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001070 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001071 }
1072
1073 /**
1074 * Set this as the current Launcher activity object for the loader.
1075 */
1076 public void initialize(Callbacks callbacks) {
1077 synchronized (mLock) {
1078 mCallbacks = new WeakReference<Callbacks>(callbacks);
1079 }
1080 }
1081
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001082 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1084 * ACTION_PACKAGE_CHANGED.
1085 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001086 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001087 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001088 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001089
Joe Onorato36115782010-06-17 13:28:48 -04001090 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001091
Joe Onorato36115782010-06-17 13:28:48 -04001092 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1093 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1094 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1095 final String packageName = intent.getData().getSchemeSpecificPart();
1096 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097
Joe Onorato36115782010-06-17 13:28:48 -04001098 int op = PackageUpdatedTask.OP_NONE;
1099
1100 if (packageName == null || packageName.length() == 0) {
1101 // they sent us a bad intent
1102 return;
1103 }
1104
1105 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1106 op = PackageUpdatedTask.OP_UPDATE;
1107 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1108 if (!replacing) {
1109 op = PackageUpdatedTask.OP_REMOVE;
1110 }
1111 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1112 // later, we will update the package at this time
1113 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1114 if (!replacing) {
1115 op = PackageUpdatedTask.OP_ADD;
1116 } else {
1117 op = PackageUpdatedTask.OP_UPDATE;
1118 }
1119 }
1120
1121 if (op != PackageUpdatedTask.OP_NONE) {
1122 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1123 }
1124
1125 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001126 // First, schedule to add these apps back in.
1127 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1128 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1129 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001130 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001131 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1132 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1133 enqueuePackageUpdated(new PackageUpdatedTask(
1134 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001135 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001136 // If we have changed locale we need to clear out the labels in all apps/workspace.
1137 forceReload();
1138 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1139 // Check if configuration change was an mcc/mnc change which would affect app resources
1140 // and we would need to clear out the labels in all apps/workspace. Same handling as
1141 // above for ACTION_LOCALE_CHANGED
1142 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001143 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001144 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001145 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001146 forceReload();
1147 }
1148 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001149 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001150 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1151 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001152 if (mCallbacks != null) {
1153 Callbacks callbacks = mCallbacks.get();
1154 if (callbacks != null) {
1155 callbacks.bindSearchablesChanged();
1156 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001157 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001158 }
1159 }
1160
Reena Lee93f824a2011-09-23 17:20:28 -07001161 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001162 resetLoadedState(true, true);
1163
Reena Lee93f824a2011-09-23 17:20:28 -07001164 // Do this here because if the launcher activity is running it will be restarted.
1165 // If it's not running startLoaderFromBackground will merely tell it that it needs
1166 // to reload.
1167 startLoaderFromBackground();
1168 }
1169
Winson Chungf0c6ae02012-03-21 16:10:31 -07001170 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1171 synchronized (mLock) {
1172 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1173 // mWorkspaceLoaded to true later
1174 stopLoaderLocked();
1175 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1176 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1177 }
1178 }
1179
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001180 /**
1181 * When the launcher is in the background, it's possible for it to miss paired
1182 * configuration changes. So whenever we trigger the loader from the background
1183 * tell the launcher that it needs to re-run the loader when it comes back instead
1184 * of doing it now.
1185 */
1186 public void startLoaderFromBackground() {
1187 boolean runLoader = false;
1188 if (mCallbacks != null) {
1189 Callbacks callbacks = mCallbacks.get();
1190 if (callbacks != null) {
1191 // Only actually run the loader if they're not paused.
1192 if (!callbacks.setLoadOnResume()) {
1193 runLoader = true;
1194 }
1195 }
1196 }
1197 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001199 }
Joe Onorato36115782010-06-17 13:28:48 -04001200 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001201
Reena Lee93f824a2011-09-23 17:20:28 -07001202 // If there is already a loader task running, tell it to stop.
1203 // returns true if isLaunching() was true on the old task
1204 private boolean stopLoaderLocked() {
1205 boolean isLaunching = false;
1206 LoaderTask oldTask = mLoaderTask;
1207 if (oldTask != null) {
1208 if (oldTask.isLaunching()) {
1209 isLaunching = true;
1210 }
1211 oldTask.stopLocked();
1212 }
1213 return isLaunching;
1214 }
1215
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001216 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001217 synchronized (mLock) {
1218 if (DEBUG_LOADERS) {
1219 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 // Clear any deferred bind-runnables from the synchronized load process
1223 // We must do this before any loading/binding is scheduled below.
1224 mDeferredBindRunnables.clear();
1225
Joe Onorato36115782010-06-17 13:28:48 -04001226 // Don't bother to start the thread if we know it's not going to do anything
1227 if (mCallbacks != null && mCallbacks.get() != null) {
1228 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001229 // also, don't downgrade isLaunching if we're already running
1230 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001231 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001232 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1233 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1234 } else {
1235 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1236 sWorker.post(mLoaderTask);
1237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 }
1240 }
1241
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001242 void bindRemainingSynchronousPages() {
1243 // Post the remaining side pages to be loaded
1244 if (!mDeferredBindRunnables.isEmpty()) {
1245 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001246 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001247 }
1248 mDeferredBindRunnables.clear();
1249 }
1250 }
1251
Joe Onorato36115782010-06-17 13:28:48 -04001252 public void stopLoader() {
1253 synchronized (mLock) {
1254 if (mLoaderTask != null) {
1255 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 }
1257 }
Joe Onorato36115782010-06-17 13:28:48 -04001258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259
Winson Chung76828c82013-08-19 15:43:29 -07001260 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1261 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1262 final ContentResolver contentResolver = context.getContentResolver();
1263 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1264 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1265 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1266
1267 try {
1268 final int idIndex = sc.getColumnIndexOrThrow(
1269 LauncherSettings.WorkspaceScreens._ID);
1270 final int rankIndex = sc.getColumnIndexOrThrow(
1271 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1272 while (sc.moveToNext()) {
1273 try {
1274 long screenId = sc.getLong(idIndex);
1275 int rank = sc.getInt(rankIndex);
1276
Adam Cohen4caf2982013-08-20 18:54:31 -07001277 Launcher.addDumpLog(TAG, "10249126 - loadWorkspaceScreensDb(" + screenId + ", " + rank + ")", true);
Winson Chung76828c82013-08-19 15:43:29 -07001278
1279 orderedScreens.put(rank, screenId);
1280 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001281 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001282 }
1283 }
1284 } finally {
1285 sc.close();
1286 }
1287 return orderedScreens;
1288 }
1289
Michael Jurkac57b7a82011-08-09 22:02:20 -07001290 public boolean isAllAppsLoaded() {
1291 return mAllAppsLoaded;
1292 }
1293
Winson Chung36a62fe2012-05-06 18:04:42 -07001294 boolean isLoadingWorkspace() {
1295 synchronized (mLock) {
1296 if (mLoaderTask != null) {
1297 return mLoaderTask.isLoadingWorkspace();
1298 }
1299 }
1300 return false;
1301 }
1302
Joe Onorato36115782010-06-17 13:28:48 -04001303 /**
1304 * Runnable for the thread that loads the contents of the launcher:
1305 * - workspace icons
1306 * - widgets
1307 * - all apps icons
1308 */
1309 private class LoaderTask implements Runnable {
1310 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001311 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001312 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001313 private boolean mStopped;
1314 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001315
Winson Chungc3eecff2011-07-11 17:44:15 -07001316 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001317
1318 LoaderTask(Context context, boolean isLaunching) {
1319 mContext = context;
1320 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001321 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001322 }
1323
Joe Onorato36115782010-06-17 13:28:48 -04001324 boolean isLaunching() {
1325 return mIsLaunching;
1326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327
Winson Chung36a62fe2012-05-06 18:04:42 -07001328 boolean isLoadingWorkspace() {
1329 return mIsLoadingAndBindingWorkspace;
1330 }
1331
Winson Chungc763c4e2013-07-19 13:49:06 -07001332 /** Returns whether this is an upgrade path */
1333 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001334 mIsLoadingAndBindingWorkspace = true;
1335
Joe Onorato36115782010-06-17 13:28:48 -04001336 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001337 if (DEBUG_LOADERS) {
1338 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001340
Winson Chungc763c4e2013-07-19 13:49:06 -07001341 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001342 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001343 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001344 synchronized (LoaderTask.this) {
1345 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001346 Launcher.addDumpLog(TAG, "10249126 - loadAndBindWorkspace() stopped", true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001347 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001348 }
1349 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350 }
1351 }
1352
Joe Onorato36115782010-06-17 13:28:48 -04001353 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001354 bindWorkspace(-1, isUpgradePath);
1355 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001356 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001357
Joe Onorato36115782010-06-17 13:28:48 -04001358 private void waitForIdle() {
1359 // Wait until the either we're stopped or the other threads are done.
1360 // This way we don't start loading all apps until the workspace has settled
1361 // down.
1362 synchronized (LoaderTask.this) {
1363 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001364
Joe Onorato36115782010-06-17 13:28:48 -04001365 mHandler.postIdle(new Runnable() {
1366 public void run() {
1367 synchronized (LoaderTask.this) {
1368 mLoadAndBindStepFinished = true;
1369 if (DEBUG_LOADERS) {
1370 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001371 }
Joe Onorato36115782010-06-17 13:28:48 -04001372 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001373 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001374 }
Joe Onorato36115782010-06-17 13:28:48 -04001375 });
1376
Michael Jurkac7700af2013-05-14 20:17:58 +02001377 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001378 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001379 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1380 // wait no longer than 1sec at a time
1381 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001382 } catch (InterruptedException ex) {
1383 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001384 }
1385 }
Joe Onorato36115782010-06-17 13:28:48 -04001386 if (DEBUG_LOADERS) {
1387 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001388 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001389 + "ms for previous step to finish binding");
1390 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001391 }
Joe Onorato36115782010-06-17 13:28:48 -04001392 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001393
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001394 void runBindSynchronousPage(int synchronousBindPage) {
1395 if (synchronousBindPage < 0) {
1396 // Ensure that we have a valid page index to load synchronously
1397 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1398 "valid page index");
1399 }
1400 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1401 // Ensure that we don't try and bind a specified page when the pages have not been
1402 // loaded already (we should load everything asynchronously in that case)
1403 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1404 }
1405 synchronized (mLock) {
1406 if (mIsLoaderTaskRunning) {
1407 // Ensure that we are never running the background loading at this point since
1408 // we also touch the background collections
1409 throw new RuntimeException("Error! Background loading is already running");
1410 }
1411 }
1412
1413 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1414 // data structures, we can't allow any other thread to touch that data, but because
1415 // this call is synchronous, we can get away with not locking).
1416
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001417 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001418 // operations from the previous activity. We need to ensure that all queued operations
1419 // are executed before any synchronous binding work is done.
1420 mHandler.flush();
1421
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001422 // Divide the set of loaded items into those that we are binding synchronously, and
1423 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001424 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001425 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1426 // arise from that.
1427 onlyBindAllApps();
1428 }
1429
Joe Onorato36115782010-06-17 13:28:48 -04001430 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001431 boolean isUpgrade = false;
1432
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001433 synchronized (mLock) {
1434 mIsLoaderTaskRunning = true;
1435 }
Joe Onorato36115782010-06-17 13:28:48 -04001436 // Optimize for end-user experience: if the Launcher is up and // running with the
1437 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1438 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001439 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001440 // Elevate priority when Home launches for the first time to avoid
1441 // starving at boot time. Staring at a blank home is not cool.
1442 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001443 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1444 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001445 android.os.Process.setThreadPriority(mIsLaunching
1446 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1447 }
Winson Chung64359a52013-07-08 17:17:08 -07001448 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001449 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001450
Joe Onorato36115782010-06-17 13:28:48 -04001451 if (mStopped) {
1452 break keep_running;
1453 }
1454
1455 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1456 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001457 synchronized (mLock) {
1458 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001459 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001460 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1461 }
1462 }
Joe Onorato36115782010-06-17 13:28:48 -04001463 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001464
1465 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001466 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1467 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001468
1469 // Restore the default thread priority after we are done loading items
1470 synchronized (mLock) {
1471 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1472 }
Joe Onorato36115782010-06-17 13:28:48 -04001473 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474
Winson Chungaac01e12011-08-17 10:37:13 -07001475 // Update the saved icons if necessary
1476 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001477 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001478 for (Object key : sBgDbIconCache.keySet()) {
1479 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1480 }
1481 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001482 }
Winson Chungaac01e12011-08-17 10:37:13 -07001483
Winson Chungc58497e2013-09-03 17:48:37 -07001484 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1485 // Ensure that all the applications that are in the system are
1486 // represented on the home screen.
1487 Launcher.addDumpLog(TAG, "10249126 - verifyApplications - useMoreApps="
1488 + UPGRADE_USE_MORE_APPS_FOLDER + " isUpgrade=" + isUpgrade, true);
1489 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
1490 Launcher.addDumpLog(TAG, "10249126 - verifyApplications(" + isUpgrade + ")", true);
1491 verifyApplications();
1492 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001493 }
1494
Joe Onorato36115782010-06-17 13:28:48 -04001495 // Clear out this reference, otherwise we end up holding it until all of the
1496 // callback runnables are done.
1497 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498
Joe Onorato36115782010-06-17 13:28:48 -04001499 synchronized (mLock) {
1500 // If we are still the last one to be scheduled, remove ourselves.
1501 if (mLoaderTask == this) {
1502 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001504 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001505 }
Joe Onorato36115782010-06-17 13:28:48 -04001506 }
1507
1508 public void stopLocked() {
1509 synchronized (LoaderTask.this) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001510 Launcher.addDumpLog(TAG, "10249126 - STOPPED", true);
Joe Onorato36115782010-06-17 13:28:48 -04001511 mStopped = true;
1512 this.notify();
1513 }
1514 }
1515
1516 /**
1517 * Gets the callbacks object. If we've been stopped, or if the launcher object
1518 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1519 * object that was around when the deferred message was scheduled, and if there's
1520 * a new Callbacks object around then also return null. This will save us from
1521 * calling onto it with data that will be ignored.
1522 */
1523 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1524 synchronized (mLock) {
1525 if (mStopped) {
1526 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001527 }
Joe Onorato36115782010-06-17 13:28:48 -04001528
1529 if (mCallbacks == null) {
1530 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001531 }
Joe Onorato36115782010-06-17 13:28:48 -04001532
1533 final Callbacks callbacks = mCallbacks.get();
1534 if (callbacks != oldCallbacks) {
1535 return null;
1536 }
1537 if (callbacks == null) {
1538 Log.w(TAG, "no mCallbacks");
1539 return null;
1540 }
1541
1542 return callbacks;
1543 }
1544 }
1545
Winson Chungc763c4e2013-07-19 13:49:06 -07001546 private void verifyApplications() {
1547 final Context context = mApp.getContext();
1548
1549 // Cross reference all the applications in our apps list with items in the workspace
1550 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001551 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001552 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001553 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001554 tmpInfos = getItemInfoForComponentName(app.componentName);
Adam Cohen4caf2982013-08-20 18:54:31 -07001555 Launcher.addDumpLog(TAG, "10249126 - \t" + app.componentName.getPackageName() + ", " + tmpInfos.isEmpty(), true);
Winson Chungc763c4e2013-07-19 13:49:06 -07001556 if (tmpInfos.isEmpty()) {
1557 // We are missing an application icon, so add this to the workspace
1558 added.add(app);
1559 // This is a rare event, so lets log it
1560 Log.e(TAG, "Missing Application on load: " + app);
1561 }
1562 }
1563 }
1564 if (!added.isEmpty()) {
1565 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001566 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001567 }
1568 }
1569
Winson Chung5f8afe62013-08-12 16:19:28 -07001570 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001571 LauncherAppState app = LauncherAppState.getInstance();
1572 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1573 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1574 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001575 }
1576
Joe Onorato36115782010-06-17 13:28:48 -04001577 // check & update map of what's occupied; used to discard overlapping/invalid items
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001579 LauncherAppState app = LauncherAppState.getInstance();
1580 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1581 int countX = (int) grid.numColumns;
1582 int countY = (int) grid.numRows;
1583
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001585 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1587 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1588 [(int) item.screenId][0] != null) {
1589 Log.e(TAG, "Error loading shortcut into hotseat " + item
1590 + " into position (" + item.screenId + ":" + item.cellX + ","
1591 + item.cellY + ") occupied by "
1592 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1593 [(int) item.screenId][0]);
1594 return false;
1595 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001596 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001597 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001598 items[(int) item.screenId][0] = item;
1599 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001600 return true;
1601 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001602 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1603 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001604 return true;
1605 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001606
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001608 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001609 occupied.put(item.screenId, items);
1610 }
1611
1612 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001613 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001614 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1615 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001616 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001617 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001619 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001620 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001621 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001622 return false;
1623 }
1624 }
1625 }
1626 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1627 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001628 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001629 }
1630 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001631
Joe Onorato36115782010-06-17 13:28:48 -04001632 return true;
1633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634
Winson Chungba9c37f2013-08-30 14:11:37 -07001635 /** Clears all the sBg data structures */
1636 private void clearSBgDataStructures() {
1637 synchronized (sBgLock) {
1638 sBgWorkspaceItems.clear();
1639 sBgAppWidgets.clear();
1640 sBgFolders.clear();
1641 sBgItemsIdMap.clear();
1642 sBgDbIconCache.clear();
1643 sBgWorkspaceScreens.clear();
1644 }
1645 }
1646
Winson Chungc763c4e2013-07-19 13:49:06 -07001647 /** Returns whether this is an upgradge path */
1648 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001649 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650
Joe Onorato36115782010-06-17 13:28:48 -04001651 final Context context = mContext;
1652 final ContentResolver contentResolver = context.getContentResolver();
1653 final PackageManager manager = context.getPackageManager();
1654 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1655 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001656
Winson Chung892c74d2013-08-22 16:15:50 -07001657 LauncherAppState app = LauncherAppState.getInstance();
1658 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1659 int countX = (int) grid.numColumns;
1660 int countY = (int) grid.numRows;
1661
Michael Jurkab85f8a42012-04-25 15:48:32 -07001662 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001663 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001664
Winson Chungc763c4e2013-07-19 13:49:06 -07001665 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001666 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001667
Winson Chung2abf94d2012-07-18 18:16:38 -07001668 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001669 clearSBgDataStructures();
Adam Cohen4caf2982013-08-20 18:54:31 -07001670 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace()", true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001671
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001672 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001673 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001674 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001675
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001676 // +1 for the hotseat (it can be larger than the workspace)
1677 // Load workspace in reverse order to ensure that latest items are loaded first (and
1678 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001681 try {
1682 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1683 final int intentIndex = c.getColumnIndexOrThrow
1684 (LauncherSettings.Favorites.INTENT);
1685 final int titleIndex = c.getColumnIndexOrThrow
1686 (LauncherSettings.Favorites.TITLE);
1687 final int iconTypeIndex = c.getColumnIndexOrThrow(
1688 LauncherSettings.Favorites.ICON_TYPE);
1689 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1690 final int iconPackageIndex = c.getColumnIndexOrThrow(
1691 LauncherSettings.Favorites.ICON_PACKAGE);
1692 final int iconResourceIndex = c.getColumnIndexOrThrow(
1693 LauncherSettings.Favorites.ICON_RESOURCE);
1694 final int containerIndex = c.getColumnIndexOrThrow(
1695 LauncherSettings.Favorites.CONTAINER);
1696 final int itemTypeIndex = c.getColumnIndexOrThrow(
1697 LauncherSettings.Favorites.ITEM_TYPE);
1698 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1699 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001700 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1701 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 final int screenIndex = c.getColumnIndexOrThrow(
1703 LauncherSettings.Favorites.SCREEN);
1704 final int cellXIndex = c.getColumnIndexOrThrow
1705 (LauncherSettings.Favorites.CELLX);
1706 final int cellYIndex = c.getColumnIndexOrThrow
1707 (LauncherSettings.Favorites.CELLY);
1708 final int spanXIndex = c.getColumnIndexOrThrow
1709 (LauncherSettings.Favorites.SPANX);
1710 final int spanYIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.SPANY);
1712 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1713 //final int displayModeIndex = c.getColumnIndexOrThrow(
1714 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 ShortcutInfo info;
1717 String intentDescription;
1718 LauncherAppWidgetInfo appWidgetInfo;
1719 int container;
1720 long id;
1721 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722
Winson Chungba9c37f2013-08-30 14:11:37 -07001723 Launcher.addDumpLog(TAG, "10249126 - Num rows: " + c.getCount(), true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 while (!mStopped && c.moveToNext()) {
1725 try {
1726 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001727
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 switch (itemType) {
1729 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1730 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001731 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 intentDescription = c.getString(intentIndex);
1733 try {
1734 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001735 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001736 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001737 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001738 // Log the invalid package, and remove it from the db
1739 Uri uri = LauncherSettings.Favorites.getContentUri(id,
1740 false);
Winson Chungee055712013-07-30 14:46:24 -07001741 contentResolver.delete(uri, null, null);
Winson Chung933bae62013-08-29 11:42:30 -07001742 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001743 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001744 // If apps can be on external storage, then we just
1745 // leave them for the user to remove (maybe add
1746 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001747 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001748 }
1749 continue;
1750 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001752 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 continue;
1754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1757 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1758 titleIndex, mLabelCache);
1759 } else {
1760 info = getShortcutInfo(c, context, iconTypeIndex,
1761 iconPackageIndex, iconResourceIndex, iconIndex,
1762 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001763
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 // App shortcuts that used to be automatically added to Launcher
1765 // didn't always have the correct intent flags set, so do that
1766 // here
1767 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001768 intent.getCategories() != null &&
1769 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001770 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 intent.addFlags(
1772 Intent.FLAG_ACTIVITY_NEW_TASK |
1773 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1774 }
Michael Jurka96879562012-03-22 05:54:33 -07001775 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001778 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001780 container = c.getInt(containerIndex);
1781 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 info.cellX = c.getInt(cellXIndex);
1784 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001785 info.spanX = 1;
1786 info.spanY = 1;
1787 // Skip loading items that are out of bounds
1788 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1789 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001790 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1791 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001792 continue;
1793 }
1794 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 // check & update map of what's occupied
1796 if (!checkItemPlacement(occupied, info)) {
1797 break;
1798 }
1799
1800 switch (container) {
1801 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1802 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1803 sBgWorkspaceItems.add(info);
1804 break;
1805 default:
1806 // Item is in a user folder
1807 FolderInfo folderInfo =
1808 findOrMakeFolder(sBgFolders, container);
1809 folderInfo.add(info);
1810 break;
1811 }
1812 sBgItemsIdMap.put(info.id, info);
1813
1814 // now that we've loaded everthing re-save it with the
1815 // icon in case it disappears somehow.
1816 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001817 } else {
1818 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 }
1820 break;
1821
1822 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1823 id = c.getLong(idIndex);
1824 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1825
1826 folderInfo.title = c.getString(titleIndex);
1827 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001828 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 folderInfo.cellX = c.getInt(cellXIndex);
1832 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001833 folderInfo.spanX = 1;
1834 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835
Winson Chung5f8afe62013-08-12 16:19:28 -07001836 // Skip loading items that are out of bounds
1837 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001838 if (checkItemDimensions(folderInfo)) {
1839 Log.d(TAG, "Skipped loading out of bounds folder");
1840 continue;
1841 }
1842 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001843 // check & update map of what's occupied
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 if (!checkItemPlacement(occupied, folderInfo)) {
Daniel Sandler8802e962010-05-26 16:28:16 -04001845 break;
1846 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001847
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1850 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1851 sBgWorkspaceItems.add(folderInfo);
1852 break;
Joe Onorato36115782010-06-17 13:28:48 -04001853 }
Joe Onorato17a89222011-02-08 17:26:11 -08001854
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1856 sBgFolders.put(folderInfo.id, folderInfo);
1857 break;
1858
1859 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1860 // Read all Launcher-specific widget details
1861 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001862 String savedProvider = c.getString(appWidgetProviderIndex);
1863
Joe Onorato36115782010-06-17 13:28:48 -04001864 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001865
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 final AppWidgetProviderInfo provider =
1867 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001868
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 if (!isSafeMode && (provider == null || provider.provider == null ||
1870 provider.provider.getPackageName() == null)) {
1871 String log = "Deleting widget that isn't installed anymore: id="
1872 + id + " appWidgetId=" + appWidgetId;
1873 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001874 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001875 itemsToRemove.add(id);
1876 } else {
1877 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1878 provider.provider);
1879 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001880 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001881 appWidgetInfo.cellX = c.getInt(cellXIndex);
1882 appWidgetInfo.cellY = c.getInt(cellYIndex);
1883 appWidgetInfo.spanX = c.getInt(spanXIndex);
1884 appWidgetInfo.spanY = c.getInt(spanYIndex);
1885 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1886 appWidgetInfo.minSpanX = minSpan[0];
1887 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001888
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 container = c.getInt(containerIndex);
1890 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1891 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1892 Log.e(TAG, "Widget found where container != " +
1893 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1894 continue;
1895 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896
Adam Cohene25af792013-06-06 23:08:25 -07001897 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001898 // Skip loading items that are out of bounds
1899 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1900 if (checkItemDimensions(appWidgetInfo)) {
1901 Log.d(TAG, "Skipped loading out of bounds app widget");
1902 continue;
1903 }
1904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 // check & update map of what's occupied
1906 if (!checkItemPlacement(occupied, appWidgetInfo)) {
1907 break;
1908 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001909 String providerName = provider.provider.flattenToString();
1910 if (!providerName.equals(savedProvider)) {
1911 ContentValues values = new ContentValues();
1912 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1913 providerName);
1914 String where = BaseColumns._ID + "= ?";
1915 String[] args = {Integer.toString(c.getInt(idIndex))};
1916 contentResolver.update(contentUri, values, where, args);
1917 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1919 sBgAppWidgets.add(appWidgetInfo);
1920 }
Joe Onorato36115782010-06-17 13:28:48 -04001921 break;
1922 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001924 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001925 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 }
1927 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001928 if (c != null) {
1929 c.close();
1930 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 }
1932
Winson Chungba9c37f2013-08-30 14:11:37 -07001933 // Break early if we've stopped loading
1934 if (mStopped) {
1935 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace() - Stopped", true);
1936 clearSBgDataStructures();
1937 return false;
1938 }
1939
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 if (itemsToRemove.size() > 0) {
1941 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001942 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001943 // Remove dead items
1944 for (long id : itemsToRemove) {
1945 if (DEBUG_LOADERS) {
1946 Log.d(TAG, "Removed id = " + id);
1947 }
1948 // Don't notify content observers
1949 try {
1950 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1951 null, null);
1952 } catch (RemoteException e) {
1953 Log.w(TAG, "Could not remove id = " + id);
1954 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001955 }
1956 }
1957
Winson Chungc763c4e2013-07-19 13:49:06 -07001958 if (loadedOldDb) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001959 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - loadedOldDb", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 long maxScreenId = 0;
1961 // If we're importing we use the old screen order.
1962 for (ItemInfo item: sBgItemsIdMap.values()) {
1963 long screenId = item.screenId;
1964 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1965 !sBgWorkspaceScreens.contains(screenId)) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001966 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace-loadedOldDb(" + screenId + ")", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 sBgWorkspaceScreens.add(screenId);
1968 if (screenId > maxScreenId) {
1969 maxScreenId = screenId;
1970 }
1971 }
1972 }
1973 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001974
1975 // Dump the sBgWorkspaceScreens
Adam Cohen4caf2982013-08-20 18:54:31 -07001976 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001977 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07001978 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001979 }
1980
Michael Jurka414300a2013-08-27 15:42:35 +02001981 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001983
1984 // Update the max item id after we load an old db
1985 long maxItemId = 0;
1986 // If we're importing we use the old screen order.
1987 for (ItemInfo item: sBgItemsIdMap.values()) {
1988 maxItemId = Math.max(maxItemId, item.id);
1989 }
Michael Jurka414300a2013-08-27 15:42:35 +02001990 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 } else {
Winson Chung933bae62013-08-29 11:42:30 -07001992 Launcher.addDumpLog(TAG, "10249126 - loadWorkspace - !loadedOldDb [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Winson Chung76828c82013-08-19 15:43:29 -07001993 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1994 for (Integer i : orderedScreens.keySet()) {
Winson Chung933bae62013-08-29 11:42:30 -07001995 Launcher.addDumpLog(TAG, "10249126 - adding to sBgWorkspaceScreens: " + orderedScreens.get(i), true);
Winson Chung76828c82013-08-19 15:43:29 -07001996 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 }
1998
1999 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002000 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2001 for (Long l : unusedScreens) {
2002 Launcher.addDumpLog(TAG, "10249126 - unused screens: " + l, true);
2003 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002004
Winson Chung933bae62013-08-29 11:42:30 -07002005 Launcher.addDumpLog(TAG, "10249126 - sBgItemsIdMap [" + sWorkerThread.getThreadId() + ", " + Process.myTid() + "]", true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002006 for (ItemInfo item: sBgItemsIdMap.values()) {
2007 long screenId = item.screenId;
Winson Chung933bae62013-08-29 11:42:30 -07002008 Launcher.addDumpLog(TAG, "10249126 - \t" + item.container + ", " + screenId + " - " + unusedScreens.contains(screenId) + " | " + item, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07002009
2010 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2011 unusedScreens.contains(screenId)) {
2012 unusedScreens.remove(screenId);
Winson Chung933bae62013-08-29 11:42:30 -07002013 Launcher.addDumpLog(TAG, "10249126 - \t\tRemoving " + screenId, true);
2014 for (Long l : unusedScreens) {
2015 Launcher.addDumpLog(TAG, "10249126 - \t\t\t unused screens: " + l, true);
2016 }
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 }
2018 }
2019
2020 // If there are any empty screens remove them, and update.
2021 if (unusedScreens.size() != 0) {
Winson Chung9e6a0a22013-08-27 11:58:12 -07002022 // Dump the sBgWorkspaceScreens
Winson Chung933bae62013-08-29 11:42:30 -07002023 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - pre removeAll", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002024 for (Long l : sBgWorkspaceScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002025 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002026 }
Winson Chung933bae62013-08-29 11:42:30 -07002027
2028 sBgWorkspaceScreens.removeAll(unusedScreens);
2029
2030 // Dump the sBgWorkspaceScreens
2031 Launcher.addDumpLog(TAG, "10249126 - updateWorkspaceScreenOrder - sBgWorkspaceScreens - post removeAll", true);
2032 for (Long l : sBgWorkspaceScreens) {
2033 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2034 }
2035
Adam Cohendcd297f2013-06-18 13:13:40 -07002036 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2037 }
2038 }
2039
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002040 if (DEBUG_LOADERS) {
2041 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2042 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002043 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002044 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002046
Daniel Sandler566da102013-06-25 23:43:45 -04002047 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002048 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002049 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002050 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002051 line += " | ";
2052 }
Winson Chung892c74d2013-08-22 16:15:50 -07002053 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002054 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002055 }
Joe Onorato36115782010-06-17 13:28:48 -04002056 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 }
Joe Onorato36115782010-06-17 13:28:48 -04002060 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002061 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002062 }
2063
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 /** Filters the set of items who are directly or indirectly (via another container) on the
2065 * specified screen. */
2066 private void filterCurrentWorkspaceItems(int currentScreen,
2067 ArrayList<ItemInfo> allWorkspaceItems,
2068 ArrayList<ItemInfo> currentScreenItems,
2069 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002070 // Purge any null ItemInfos
2071 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2072 while (iter.hasNext()) {
2073 ItemInfo i = iter.next();
2074 if (i == null) {
2075 iter.remove();
2076 }
2077 }
2078
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 // If we aren't filtering on a screen, then the set of items to load is the full set of
2080 // items given.
2081 if (currentScreen < 0) {
2082 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002083 }
2084
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 // Order the set of items by their containers first, this allows use to walk through the
2086 // list sequentially, build up a list of containers that are in the specified screen,
2087 // as well as all items in those containers.
2088 Set<Long> itemsOnScreen = new HashSet<Long>();
2089 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2090 @Override
2091 public int compare(ItemInfo lhs, ItemInfo rhs) {
2092 return (int) (lhs.container - rhs.container);
2093 }
2094 });
2095 for (ItemInfo info : allWorkspaceItems) {
2096 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002097 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002098 currentScreenItems.add(info);
2099 itemsOnScreen.add(info.id);
2100 } else {
2101 otherScreenItems.add(info);
2102 }
2103 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2104 currentScreenItems.add(info);
2105 itemsOnScreen.add(info.id);
2106 } else {
2107 if (itemsOnScreen.contains(info.container)) {
2108 currentScreenItems.add(info);
2109 itemsOnScreen.add(info.id);
2110 } else {
2111 otherScreenItems.add(info);
2112 }
2113 }
2114 }
2115 }
2116
2117 /** Filters the set of widgets which are on the specified screen. */
2118 private void filterCurrentAppWidgets(int currentScreen,
2119 ArrayList<LauncherAppWidgetInfo> appWidgets,
2120 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2121 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2122 // If we aren't filtering on a screen, then the set of items to load is the full set of
2123 // widgets given.
2124 if (currentScreen < 0) {
2125 currentScreenWidgets.addAll(appWidgets);
2126 }
2127
2128 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002129 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002130 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002131 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 currentScreenWidgets.add(widget);
2133 } else {
2134 otherScreenWidgets.add(widget);
2135 }
2136 }
2137 }
2138
2139 /** Filters the set of folders which are on the specified screen. */
2140 private void filterCurrentFolders(int currentScreen,
2141 HashMap<Long, ItemInfo> itemsIdMap,
2142 HashMap<Long, FolderInfo> folders,
2143 HashMap<Long, FolderInfo> currentScreenFolders,
2144 HashMap<Long, FolderInfo> otherScreenFolders) {
2145 // If we aren't filtering on a screen, then the set of items to load is the full set of
2146 // widgets given.
2147 if (currentScreen < 0) {
2148 currentScreenFolders.putAll(folders);
2149 }
2150
2151 for (long id : folders.keySet()) {
2152 ItemInfo info = itemsIdMap.get(id);
2153 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002154 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002156 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 currentScreenFolders.put(id, folder);
2158 } else {
2159 otherScreenFolders.put(id, folder);
2160 }
2161 }
2162 }
2163
2164 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2165 * right) */
2166 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002167 final LauncherAppState app = LauncherAppState.getInstance();
2168 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002169 // XXX: review this
2170 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002171 @Override
2172 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002173 int cellCountX = (int) grid.numColumns;
2174 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002175 int screenOffset = cellCountX * cellCountY;
2176 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002178 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002180 rhs.cellY * cellCountX + rhs.cellX);
2181 return (int) (lr - rr);
2182 }
2183 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002184 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002185
Adam Cohendcd297f2013-06-18 13:13:40 -07002186 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2187 final ArrayList<Long> orderedScreens) {
Adam Cohen4caf2982013-08-20 18:54:31 -07002188 Launcher.addDumpLog(TAG, "10249126 - bindWorkspaceScreens()", true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002189
2190 // Dump the orderedScreens
Winson Chungba9c37f2013-08-30 14:11:37 -07002191 synchronized (sBgLock) {
2192 Launcher.addDumpLog(TAG, "10249126 - orderedScreens", true);
2193 for (Long l : sBgWorkspaceScreens) {
2194 Launcher.addDumpLog(TAG, "10249126\t- " + l, true);
2195 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07002196 }
2197
Adam Cohendcd297f2013-06-18 13:13:40 -07002198 final Runnable r = new Runnable() {
2199 @Override
2200 public void run() {
2201 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2202 if (callbacks != null) {
2203 callbacks.bindScreens(orderedScreens);
2204 }
2205 }
2206 };
2207 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2208 }
2209
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002210 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2211 final ArrayList<ItemInfo> workspaceItems,
2212 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2213 final HashMap<Long, FolderInfo> folders,
2214 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002215
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002216 final boolean postOnMainThread = (deferredBindRunnables != null);
2217
2218 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002219 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002221 final int start = i;
2222 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002223 final Runnable r = new Runnable() {
2224 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002226 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002228 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2229 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 }
2231 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002232 };
2233 if (postOnMainThread) {
2234 deferredBindRunnables.add(r);
2235 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002236 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002237 }
Joe Onorato36115782010-06-17 13:28:48 -04002238 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002239
2240 // Bind the folders
2241 if (!folders.isEmpty()) {
2242 final Runnable r = new Runnable() {
2243 public void run() {
2244 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2245 if (callbacks != null) {
2246 callbacks.bindFolders(folders);
2247 }
2248 }
2249 };
2250 if (postOnMainThread) {
2251 deferredBindRunnables.add(r);
2252 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002253 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 }
2255 }
2256
2257 // Bind the widgets, one at a time
2258 N = appWidgets.size();
2259 for (int i = 0; i < N; i++) {
2260 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2261 final Runnable r = new Runnable() {
2262 public void run() {
2263 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2264 if (callbacks != null) {
2265 callbacks.bindAppWidget(widget);
2266 }
2267 }
2268 };
2269 if (postOnMainThread) {
2270 deferredBindRunnables.add(r);
2271 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002272 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 }
2274 }
2275 }
2276
2277 /**
2278 * Binds all loaded data to actual views on the main thread.
2279 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002280 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002281 Launcher.addDumpLog(TAG, "10249126 - bindWorkspace(" + synchronizeBindPage + ", " + isUpgradePath + ")", true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 final long t = SystemClock.uptimeMillis();
2283 Runnable r;
2284
2285 // Don't use these two variables in any of the callback runnables.
2286 // Otherwise we hold a reference to them.
2287 final Callbacks oldCallbacks = mCallbacks.get();
2288 if (oldCallbacks == null) {
2289 // This launcher has exited and nobody bothered to tell us. Just bail.
2290 Log.w(TAG, "LoaderTask running with no launcher");
2291 return;
2292 }
2293
Winson Chung4a2afa32012-07-19 14:53:05 -07002294 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2295 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296 oldCallbacks.getCurrentWorkspaceScreen();
2297
2298 // Load all the items that are on the current page first (and in the process, unbind
2299 // all the existing workspace items before we call startBinding() below.
2300 unbindWorkspaceItemsOnMainThread();
2301 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2302 ArrayList<LauncherAppWidgetInfo> appWidgets =
2303 new ArrayList<LauncherAppWidgetInfo>();
2304 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2305 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002307 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 workspaceItems.addAll(sBgWorkspaceItems);
2309 appWidgets.addAll(sBgAppWidgets);
2310 folders.putAll(sBgFolders);
2311 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002313 }
2314
2315 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2316 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2317 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2318 new ArrayList<LauncherAppWidgetInfo>();
2319 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2320 new ArrayList<LauncherAppWidgetInfo>();
2321 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2322 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2323
2324 // Separate the items that are on the current screen, and all the other remaining items
2325 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2326 otherWorkspaceItems);
2327 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2328 otherAppWidgets);
2329 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2330 otherFolders);
2331 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2332 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2333
2334 // Tell the workspace that we're about to start binding items
2335 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002336 public void run() {
2337 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2338 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002340 }
2341 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002342 };
Winson Chung81b52252012-08-27 15:34:29 -07002343 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002344
Adam Cohendcd297f2013-06-18 13:13:40 -07002345 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2346
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002347 // Load items on the current page
2348 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2349 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002350 if (isLoadingSynchronously) {
2351 r = new Runnable() {
2352 public void run() {
2353 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2354 if (callbacks != null) {
2355 callbacks.onPageBoundSynchronously(currentScreen);
2356 }
2357 }
2358 };
Winson Chung81b52252012-08-27 15:34:29 -07002359 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002360 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002361
Winson Chung4a2afa32012-07-19 14:53:05 -07002362 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2363 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002364 mDeferredBindRunnables.clear();
2365 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002366 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002367
2368 // Tell the workspace that we're done binding items
2369 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002370 public void run() {
2371 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2372 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002373 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002374 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002375
Winson Chung98e030b2012-05-07 16:01:11 -07002376 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002377 if (DEBUG_LOADERS) {
2378 Log.d(TAG, "bound workspace in "
2379 + (SystemClock.uptimeMillis()-t) + "ms");
2380 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002381
2382 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002383 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002384 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002385 if (isLoadingSynchronously) {
2386 mDeferredBindRunnables.add(r);
2387 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002388 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002389 }
Joe Onorato36115782010-06-17 13:28:48 -04002390 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002391
Joe Onorato36115782010-06-17 13:28:48 -04002392 private void loadAndBindAllApps() {
2393 if (DEBUG_LOADERS) {
2394 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2395 }
2396 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002397 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002398 synchronized (LoaderTask.this) {
2399 if (mStopped) {
2400 return;
2401 }
2402 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002403 }
Joe Onorato36115782010-06-17 13:28:48 -04002404 } else {
2405 onlyBindAllApps();
2406 }
2407 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002408
Joe Onorato36115782010-06-17 13:28:48 -04002409 private void onlyBindAllApps() {
2410 final Callbacks oldCallbacks = mCallbacks.get();
2411 if (oldCallbacks == null) {
2412 // This launcher has exited and nobody bothered to tell us. Just bail.
2413 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2414 return;
2415 }
2416
2417 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002418 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002419 final ArrayList<AppInfo> list
2420 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002421 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002422 public void run() {
2423 final long t = SystemClock.uptimeMillis();
2424 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2425 if (callbacks != null) {
2426 callbacks.bindAllApplications(list);
2427 }
2428 if (DEBUG_LOADERS) {
2429 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2430 + (SystemClock.uptimeMillis()-t) + "ms");
2431 }
2432 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002433 };
2434 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002435 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002436 r.run();
2437 } else {
2438 mHandler.post(r);
2439 }
Joe Onorato36115782010-06-17 13:28:48 -04002440 }
2441
Winson Chung64359a52013-07-08 17:17:08 -07002442 private void loadAllApps() {
2443 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002444
Joe Onorato36115782010-06-17 13:28:48 -04002445 final Callbacks oldCallbacks = mCallbacks.get();
2446 if (oldCallbacks == null) {
2447 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002448 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002449 return;
2450 }
2451
Winson Chung64359a52013-07-08 17:17:08 -07002452 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002453 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2454 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2455
Winson Chung64359a52013-07-08 17:17:08 -07002456 // Clear the list of apps
2457 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002458
Winson Chung64359a52013-07-08 17:17:08 -07002459 // Query for the set of apps
2460 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2461 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2462 if (DEBUG_LOADERS) {
2463 Log.d(TAG, "queryIntentActivities took "
2464 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2465 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2466 }
2467 // Fail if we don't have any apps
2468 if (apps == null || apps.isEmpty()) {
2469 return;
2470 }
2471 // Sort the applications by name
2472 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2473 Collections.sort(apps,
2474 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2475 if (DEBUG_LOADERS) {
2476 Log.d(TAG, "sort took "
2477 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002478 }
2479
Winson Chung64359a52013-07-08 17:17:08 -07002480 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002481 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002482 ResolveInfo app = apps.get(i);
2483 if (oldCallbacks.shouldShowApp(app)) {
2484 // This builds the icon bitmaps.
2485 mBgAllAppsList.add(new AppInfo(packageManager, app,
2486 mIconCache, mLabelCache));
2487 }
Winson Chung64359a52013-07-08 17:17:08 -07002488 }
2489
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002490 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002491 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2492 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002493
2494 // Post callback on main thread
2495 mHandler.post(new Runnable() {
2496 public void run() {
2497 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002498 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002499 if (callbacks != null) {
2500 callbacks.bindAllApplications(added);
2501 if (DEBUG_LOADERS) {
2502 Log.d(TAG, "bound " + added.size() + " apps in "
2503 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2504 }
2505 } else {
2506 Log.i(TAG, "not binding apps: no Launcher activity");
2507 }
2508 }
2509 });
2510
Joe Onorato36115782010-06-17 13:28:48 -04002511 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002512 Log.d(TAG, "Icons processed in "
2513 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002514 }
2515 }
2516
2517 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002518 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2520 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2521 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2522 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2523 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2524 }
Joe Onorato36115782010-06-17 13:28:48 -04002525 }
2526 }
2527
2528 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002529 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002530 }
2531
2532 private class PackageUpdatedTask implements Runnable {
2533 int mOp;
2534 String[] mPackages;
2535
2536 public static final int OP_NONE = 0;
2537 public static final int OP_ADD = 1;
2538 public static final int OP_UPDATE = 2;
2539 public static final int OP_REMOVE = 3; // uninstlled
2540 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2541
2542
2543 public PackageUpdatedTask(int op, String[] packages) {
2544 mOp = op;
2545 mPackages = packages;
2546 }
2547
2548 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002549 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002550
2551 final String[] packages = mPackages;
2552 final int N = packages.length;
2553 switch (mOp) {
2554 case OP_ADD:
2555 for (int i=0; i<N; i++) {
2556 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002557 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002558 }
2559 break;
2560 case OP_UPDATE:
2561 for (int i=0; i<N; i++) {
2562 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002563 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002564 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002565 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002566 }
2567 break;
2568 case OP_REMOVE:
2569 case OP_UNAVAILABLE:
2570 for (int i=0; i<N; i++) {
2571 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002572 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkad9cb4a12013-03-19 12:01:06 +01002573 WidgetPreviewLoader.removeFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002574 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002575 }
2576 break;
2577 }
2578
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002579 ArrayList<AppInfo> added = null;
2580 ArrayList<AppInfo> modified = null;
2581 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002582
Adam Cohen487f7dd2012-06-28 18:12:10 -07002583 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002584 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002585 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002586 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002587 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002588 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002589 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
Winson Chung5d55f332012-07-16 20:45:03 -07002591 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002592 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002593 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002594 }
2595
Joe Onorato36115782010-06-17 13:28:48 -04002596 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2597 if (callbacks == null) {
2598 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2599 return;
2600 }
2601
2602 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002603 // Ensure that we add all the workspace applications to the db
Winson Chung997a9232013-07-24 15:33:46 -07002604 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Winson Chung64359a52013-07-08 17:17:08 -07002605 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07002606 addAndBindAddedApps(context, addedInfos, cb, added);
Joe Onorato36115782010-06-17 13:28:48 -04002607 }
2608 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002609 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002610
2611 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002612 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002613 ArrayList<ItemInfo> infos =
2614 getItemInfoForComponentName(a.componentName);
2615 for (ItemInfo i : infos) {
2616 if (isShortcutInfoUpdateable(i)) {
2617 ShortcutInfo info = (ShortcutInfo) i;
2618 info.title = a.title.toString();
2619 updateItemInDatabase(context, info);
2620 }
2621 }
2622 }
2623
Joe Onorato36115782010-06-17 13:28:48 -04002624 mHandler.post(new Runnable() {
2625 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002626 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2627 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002628 callbacks.bindAppsUpdated(modifiedFinal);
2629 }
2630 }
2631 });
2632 }
Winson Chung83892cc2013-05-01 16:53:33 -07002633 // If a package has been removed, or an app has been removed as a result of
2634 // an update (for example), make the removed callback.
2635 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002636 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002637 final ArrayList<String> removedPackageNames =
2638 new ArrayList<String>(Arrays.asList(packages));
2639
Winson Chung64359a52013-07-08 17:17:08 -07002640 // Update the launcher db to reflect the removal of apps
2641 if (packageRemoved) {
2642 for (String pn : removedPackageNames) {
2643 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2644 for (ItemInfo i : infos) {
2645 deleteItemFromDatabase(context, i);
2646 }
2647 }
2648 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002649 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002650 ArrayList<ItemInfo> infos =
2651 getItemInfoForComponentName(a.componentName);
2652 for (ItemInfo i : infos) {
2653 deleteItemFromDatabase(context, i);
2654 }
2655 }
2656 }
2657
Joe Onorato36115782010-06-17 13:28:48 -04002658 mHandler.post(new Runnable() {
2659 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002660 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2661 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002662 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002663 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002664 }
2665 }
2666 });
Joe Onoratobe386092009-11-17 17:32:16 -08002667 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002668
Michael Jurkac402cd92013-05-20 15:49:32 +02002669 final ArrayList<Object> widgetsAndShortcuts =
2670 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002671 mHandler.post(new Runnable() {
2672 @Override
2673 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002674 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2675 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002676 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002677 }
2678 }
2679 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002680
2681 // Write all the logs to disk
2682 Launcher.addDumpLog(TAG, "10249126 - PackageUpdatedTask - dumping logs to disk", true);
2683 mHandler.post(new Runnable() {
2684 public void run() {
2685 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2686 if (callbacks == cb && cb != null) {
2687 callbacks.dumpLogsToLocalData(false);
2688 }
2689 }
2690 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002691 }
2692 }
2693
Michael Jurkac402cd92013-05-20 15:49:32 +02002694 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2695 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2696 PackageManager packageManager = context.getPackageManager();
2697 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2698 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2699 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2700 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2701 Collections.sort(widgetsAndShortcuts,
2702 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2703 return widgetsAndShortcuts;
2704 }
2705
Winson Chung1323b482013-08-05 12:41:55 -07002706 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002707 if (cn == null) {
2708 return false;
2709 }
2710
2711 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002712 // Skip if the application is disabled
2713 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2714 if (!pi.applicationInfo.enabled) {
2715 return false;
2716 }
2717
2718 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002719 return (pm.getActivityInfo(cn, 0) != null);
2720 } catch (NameNotFoundException e) {
2721 return false;
2722 }
2723 }
2724
Joe Onorato9c1289c2009-08-17 11:03:03 -04002725 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002726 * This is called from the code that adds shortcuts from the intent receiver. This
2727 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002728 */
Joe Onorato56d82912010-03-07 14:32:10 -05002729 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002730 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002732
Joe Onorato56d82912010-03-07 14:32:10 -05002733 /**
2734 * Make an ShortcutInfo object for a shortcut that is an application.
2735 *
2736 * If c is not null, then it will be used to fill in missing data like the title and icon.
2737 */
2738 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002739 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002740 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002741 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002742 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002743 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002744 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002745 } else {
2746 try {
2747 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2748 info.initFlagsAndFirstInstallTime(pi);
2749 } catch (NameNotFoundException e) {
2750 Log.d(TAG, "getPackInfo failed for package " +
2751 componentName.getPackageName());
2752 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002753 }
2754
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002755 // TODO: See if the PackageManager knows about this case. If it doesn't
2756 // then return null & delete this.
2757
Joe Onorato56d82912010-03-07 14:32:10 -05002758 // the resource -- This may implicitly give us back the fallback icon,
2759 // but don't worry about that. All we're doing with usingFallbackIcon is
2760 // to avoid saving lots of copies of that in the database, and most apps
2761 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002762
2763 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2764 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2765 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002766 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002767 ResolveInfo resolveInfo = null;
2768 ComponentName oldComponent = intent.getComponent();
2769 Intent newIntent = new Intent(intent.getAction(), null);
2770 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2771 newIntent.setPackage(oldComponent.getPackageName());
2772 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2773 for (ResolveInfo i : infos) {
2774 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2775 i.activityInfo.name);
2776 if (cn.equals(oldComponent)) {
2777 resolveInfo = i;
2778 }
2779 }
2780 if (resolveInfo == null) {
2781 resolveInfo = manager.resolveActivity(intent, 0);
2782 }
Joe Onorato56d82912010-03-07 14:32:10 -05002783 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002784 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002785 }
Joe Onorato56d82912010-03-07 14:32:10 -05002786 // the db
2787 if (icon == null) {
2788 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002789 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002790 }
2791 }
2792 // the fallback icon
2793 if (icon == null) {
2794 icon = getFallbackIcon();
2795 info.usingFallbackIcon = true;
2796 }
2797 info.setIcon(icon);
2798
2799 // from the resource
2800 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002801 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2802 if (labelCache != null && labelCache.containsKey(key)) {
2803 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002804 } else {
2805 info.title = resolveInfo.activityInfo.loadLabel(manager);
2806 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002807 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002808 }
2809 }
Joe Onorato56d82912010-03-07 14:32:10 -05002810 }
2811 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002812 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002813 if (c != null) {
2814 info.title = c.getString(titleIndex);
2815 }
2816 }
2817 // fall back to the class name of the activity
2818 if (info.title == null) {
2819 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002820 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002821 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2822 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002824
Winson Chung64359a52013-07-08 17:17:08 -07002825 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2826 ItemInfoFilter f) {
2827 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2828 for (ItemInfo i : infos) {
2829 if (i instanceof ShortcutInfo) {
2830 ShortcutInfo info = (ShortcutInfo) i;
2831 ComponentName cn = info.intent.getComponent();
2832 if (cn != null && f.filterItem(null, info, cn)) {
2833 filtered.add(info);
2834 }
2835 } else if (i instanceof FolderInfo) {
2836 FolderInfo info = (FolderInfo) i;
2837 for (ShortcutInfo s : info.contents) {
2838 ComponentName cn = s.intent.getComponent();
2839 if (cn != null && f.filterItem(info, s, cn)) {
2840 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002841 }
2842 }
Winson Chung64359a52013-07-08 17:17:08 -07002843 } else if (i instanceof LauncherAppWidgetInfo) {
2844 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2845 ComponentName cn = info.providerName;
2846 if (cn != null && f.filterItem(null, info, cn)) {
2847 filtered.add(info);
2848 }
Winson Chung8a435102012-08-30 17:16:53 -07002849 }
2850 }
Winson Chung64359a52013-07-08 17:17:08 -07002851 return new ArrayList<ItemInfo>(filtered);
2852 }
2853
2854 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002855 ItemInfoFilter filter = new ItemInfoFilter() {
2856 @Override
2857 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2858 return cn.getPackageName().equals(pn);
2859 }
2860 };
2861 return filterItemInfos(sBgItemsIdMap.values(), filter);
2862 }
2863
2864 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002865 ItemInfoFilter filter = new ItemInfoFilter() {
2866 @Override
2867 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2868 return cn.equals(cname);
2869 }
2870 };
2871 return filterItemInfos(sBgItemsIdMap.values(), filter);
2872 }
2873
2874 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2875 if (i instanceof ShortcutInfo) {
2876 ShortcutInfo info = (ShortcutInfo) i;
2877 // We need to check for ACTION_MAIN otherwise getComponent() might
2878 // return null for some shortcuts (for instance, for shortcuts to
2879 // web pages.)
2880 Intent intent = info.intent;
2881 ComponentName name = intent.getComponent();
2882 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2883 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2884 return true;
2885 }
2886 }
2887 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002888 }
2889
2890 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002891 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002893 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002894 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2895 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896
Joe Onorato56d82912010-03-07 14:32:10 -05002897 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002898 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002901 // TODO: If there's an explicit component and we can't install that, delete it.
2902
Joe Onorato56d82912010-03-07 14:32:10 -05002903 info.title = c.getString(titleIndex);
2904
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 int iconType = c.getInt(iconTypeIndex);
2906 switch (iconType) {
2907 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2908 String packageName = c.getString(iconPackageIndex);
2909 String resourceName = c.getString(iconResourceIndex);
2910 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002911 info.customIcon = false;
2912 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002915 if (resources != null) {
2916 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002917 icon = Utilities.createIconBitmap(
2918 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002921 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Joe Onorato56d82912010-03-07 14:32:10 -05002923 // the db
2924 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002925 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002926 }
2927 // the fallback icon
2928 if (icon == null) {
2929 icon = getFallbackIcon();
2930 info.usingFallbackIcon = true;
2931 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 break;
2933 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002934 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002935 if (icon == null) {
2936 icon = getFallbackIcon();
2937 info.customIcon = false;
2938 info.usingFallbackIcon = true;
2939 } else {
2940 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 break;
2943 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002944 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002945 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 info.customIcon = false;
2947 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002949 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 return info;
2951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952
Michael Jurka931dc972011-08-05 15:08:15 -07002953 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002954 @SuppressWarnings("all") // suppress dead code warning
2955 final boolean debug = false;
2956 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002957 Log.d(TAG, "getIconFromCursor app="
2958 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2959 }
2960 byte[] data = c.getBlob(iconIndex);
2961 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002962 return Utilities.createIconBitmap(
2963 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002964 } catch (Exception e) {
2965 return null;
2966 }
2967 }
2968
Winson Chung3d503fb2011-07-13 17:25:49 -07002969 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2970 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002971 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002972 if (info == null) {
2973 return null;
2974 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002975 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002976
2977 return info;
2978 }
2979
Winson Chunga9abd0e2010-10-27 17:18:37 -07002980 /**
Winson Chung55cef262010-10-28 14:14:18 -07002981 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2982 */
2983 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2984 ComponentName component) {
2985 List<AppWidgetProviderInfo> widgets =
2986 AppWidgetManager.getInstance(context).getInstalledProviders();
2987 for (AppWidgetProviderInfo info : widgets) {
2988 if (info.provider.equals(component)) {
2989 return info;
2990 }
2991 }
2992 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002993 }
2994
Winson Chung68846fd2010-10-29 11:00:27 -07002995 /**
2996 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2997 */
2998 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2999 final PackageManager packageManager = context.getPackageManager();
3000 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
3001 new ArrayList<WidgetMimeTypeHandlerData>();
3002
3003 final Intent supportsIntent =
3004 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
3005 supportsIntent.setType(mimeType);
3006
3007 // Create a set of widget configuration components that we can test against
3008 final List<AppWidgetProviderInfo> widgets =
3009 AppWidgetManager.getInstance(context).getInstalledProviders();
3010 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
3011 new HashMap<ComponentName, AppWidgetProviderInfo>();
3012 for (AppWidgetProviderInfo info : widgets) {
3013 configurationComponentToWidget.put(info.configure, info);
3014 }
3015
3016 // Run through each of the intents that can handle this type of clip data, and cross
3017 // reference them with the components that are actual configuration components
3018 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3019 PackageManager.MATCH_DEFAULT_ONLY);
3020 for (ResolveInfo info : activities) {
3021 final ActivityInfo activityInfo = info.activityInfo;
3022 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3023 activityInfo.name);
3024 if (configurationComponentToWidget.containsKey(infoComponent)) {
3025 supportedConfigurationActivities.add(
3026 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3027 configurationComponentToWidget.get(infoComponent)));
3028 }
3029 }
3030 return supportedConfigurationActivities;
3031 }
3032
Winson Chunga9abd0e2010-10-27 17:18:37 -07003033 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003034 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3035 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3036 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3037
Adam Cohend9198822011-11-22 16:42:47 -08003038 if (intent == null) {
3039 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3040 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3041 return null;
3042 }
3043
Joe Onorato0589f0f2010-02-08 13:44:00 -08003044 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003045 boolean customIcon = false;
3046 ShortcutIconResource iconResource = null;
3047
3048 if (bitmap != null && bitmap instanceof Bitmap) {
3049 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003050 customIcon = true;
3051 } else {
3052 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3053 if (extra != null && extra instanceof ShortcutIconResource) {
3054 try {
3055 iconResource = (ShortcutIconResource) extra;
3056 final PackageManager packageManager = context.getPackageManager();
3057 Resources resources = packageManager.getResourcesForApplication(
3058 iconResource.packageName);
3059 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003060 icon = Utilities.createIconBitmap(
3061 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003062 } catch (Exception e) {
3063 Log.w(TAG, "Could not load shortcut icon: " + extra);
3064 }
3065 }
3066 }
3067
Michael Jurkac9d95c52011-08-29 14:03:34 -07003068 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003069
3070 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003071 if (fallbackIcon != null) {
3072 icon = fallbackIcon;
3073 } else {
3074 icon = getFallbackIcon();
3075 info.usingFallbackIcon = true;
3076 }
Joe Onorato56d82912010-03-07 14:32:10 -05003077 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003078 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003079
Joe Onorato0589f0f2010-02-08 13:44:00 -08003080 info.title = name;
3081 info.intent = intent;
3082 info.customIcon = customIcon;
3083 info.iconResource = iconResource;
3084
3085 return info;
3086 }
3087
Winson Chungaac01e12011-08-17 10:37:13 -07003088 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3089 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003090 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003091 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003092 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003093 }
Joe Onorato56d82912010-03-07 14:32:10 -05003094 // If this icon doesn't have a custom icon, check to see
3095 // what's stored in the DB, and if it doesn't match what
3096 // we're going to show, store what we are going to show back
3097 // into the DB. We do this so when we're loading, if the
3098 // package manager can't find an icon (for example because
3099 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003100 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003101 cache.put(info, c.getBlob(iconIndex));
3102 return true;
3103 }
3104 return false;
3105 }
3106 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3107 boolean needSave = false;
3108 try {
3109 if (data != null) {
3110 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3111 Bitmap loaded = info.getIcon(mIconCache);
3112 needSave = !saved.sameAs(loaded);
3113 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003114 needSave = true;
3115 }
Winson Chungaac01e12011-08-17 10:37:13 -07003116 } catch (Exception e) {
3117 needSave = true;
3118 }
3119 if (needSave) {
3120 Log.d(TAG, "going to save icon bitmap for info=" + info);
3121 // This is slower than is ideal, but this only happens once
3122 // or when the app is updated with a new icon.
3123 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003124 }
3125 }
3126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003128 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 * or make a new one.
3130 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003131 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 // See if a placeholder was created for us already
3133 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003134 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003135 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003136 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 folders.put(id, folderInfo);
3138 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003139 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 }
3141
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003142 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003143 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003144 return new Comparator<AppInfo>() {
3145 public final int compare(AppInfo a, AppInfo b) {
Winson Chung11904872012-09-17 16:58:46 -07003146 int result = collator.compare(a.title.toString(), b.title.toString());
3147 if (result == 0) {
3148 result = a.componentName.compareTo(b.componentName);
3149 }
3150 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003151 }
Winson Chung11904872012-09-17 16:58:46 -07003152 };
3153 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003154 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3155 = new Comparator<AppInfo>() {
3156 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003157 if (a.firstInstallTime < b.firstInstallTime) return 1;
3158 if (a.firstInstallTime > b.firstInstallTime) return -1;
3159 return 0;
3160 }
3161 };
Winson Chung11904872012-09-17 16:58:46 -07003162 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3163 final Collator collator = Collator.getInstance();
3164 return new Comparator<AppWidgetProviderInfo>() {
3165 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
3166 return collator.compare(a.label.toString(), b.label.toString());
3167 }
3168 };
3169 }
Winson Chung5308f242011-08-18 12:12:41 -07003170 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3171 if (info.activityInfo != null) {
3172 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3173 } else {
3174 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3175 }
3176 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003177 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003178 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003179 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003180 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003181 ShortcutNameComparator(PackageManager pm) {
3182 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003183 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003184 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003185 }
3186 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3187 mPackageManager = pm;
3188 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003189 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003190 }
3191 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003192 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003193 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3194 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3195 if (mLabelCache.containsKey(keyA)) {
3196 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003197 } else {
3198 labelA = a.loadLabel(mPackageManager).toString();
3199
Winson Chung5308f242011-08-18 12:12:41 -07003200 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003201 }
Winson Chung5308f242011-08-18 12:12:41 -07003202 if (mLabelCache.containsKey(keyB)) {
3203 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003204 } else {
3205 labelB = b.loadLabel(mPackageManager).toString();
3206
Winson Chung5308f242011-08-18 12:12:41 -07003207 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003208 }
Winson Chung11904872012-09-17 16:58:46 -07003209 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003210 }
3211 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003212 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003213 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003214 private PackageManager mPackageManager;
3215 private HashMap<Object, String> mLabelCache;
3216 WidgetAndShortcutNameComparator(PackageManager pm) {
3217 mPackageManager = pm;
3218 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003219 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003220 }
3221 public final int compare(Object a, Object b) {
3222 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003223 if (mLabelCache.containsKey(a)) {
3224 labelA = mLabelCache.get(a);
3225 } else {
3226 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003227 ((AppWidgetProviderInfo) a).label :
3228 ((ResolveInfo) a).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003229 mLabelCache.put(a, labelA);
3230 }
3231 if (mLabelCache.containsKey(b)) {
3232 labelB = mLabelCache.get(b);
3233 } else {
3234 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003235 ((AppWidgetProviderInfo) b).label :
3236 ((ResolveInfo) b).loadLabel(mPackageManager).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003237 mLabelCache.put(b, labelB);
3238 }
Winson Chung11904872012-09-17 16:58:46 -07003239 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003240 }
3241 };
Joe Onoratobe386092009-11-17 17:32:16 -08003242
3243 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003244 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003245 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3246 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3247 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3248 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003249 if (mLoaderTask != null) {
3250 mLoaderTask.dumpState();
3251 } else {
3252 Log.d(TAG, "mLoaderTask=null");
3253 }
Joe Onoratobe386092009-11-17 17:32:16 -08003254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255}