blob: b89579e69467a39547237a53d9d49e2c4e5d73d0 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080047
Michael Jurkac2f801e2011-07-12 14:19:46 -070048import java.lang.ref.WeakReference;
49import java.net.URISyntaxException;
50import java.text.Collator;
51import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070052import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070053import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070058import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070061import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070062import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Maintains in-memory state of the Launcher. It is expected that there should be only one
66 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070067 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040069public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070072
Daniel Sandler8707e0f2013-08-15 15:54:18 -070073 // true = use a "More Apps" folder for non-workspace apps on upgrade
74 // false = strew non-workspace apps across the workspace on upgrade
75 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
76
Joe Onorato36115782010-06-17 13:28:48 -040077 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070078 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020085 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung81b52252012-08-27 15:34:29 -070087 // Specific runnable types that are run on the main thread deferred handler, this allows us to
88 // clear all queued binding runnables when the Launcher activity is destroyed.
89 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
90 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
91
92
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070093 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
94 static {
95 sWorkerThread.start();
96 }
97 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
98
Joe Onoratocc67f472010-06-08 10:54:30 -070099 // We start off with everything not loaded. After that, we assume that
100 // our monitoring of the package manager provides all updates and we never
101 // need to do a requery. These are only ever touched from the loader thread.
102 private boolean mWorkspaceLoaded;
103 private boolean mAllAppsLoaded;
104
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700105 // When we are loading pages synchronously, we can't just post the binding of items on the side
106 // pages as this delays the rotation process. Instead, we wait for a callback from the first
107 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
108 // a normal load, we also clear this set of Runnables.
109 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700114 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800115
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700116 // The lock that must be acquired before referencing any static bg data structures. Unlike
117 // other locks, this one can generally be held long-term because we never expect any of these
118 // static data structures to be referenced outside of the worker thread except on the first
119 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700120 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
127 // created by LauncherModel that are directly on the home screen (however, no widgets or
128 // shortcuts within folders).
129 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
132 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 new ArrayList<LauncherAppWidgetInfo>();
134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
136 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
139 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700140
141 // sBgWorkspaceScreens is the ordered set of workspace screens.
142 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // </ only access in worker thread >
145
146 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Reena Lee99a73f32011-10-24 17:27:37 -0700149 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700152 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public int getCurrentWorkspaceScreen();
154 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
156 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700157 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700158 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500159 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700160 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200162 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700163 public void bindAppsAdded(ArrayList<Long> newScreens,
164 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700165 ArrayList<ItemInfo> addAnimated,
166 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700168 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700169 ArrayList<AppInfo> appInfos);
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();
Winson Chunga0b7e862013-09-05 16:03:15 -0700172 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700173 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700174 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung64359a52013-07-08 17:17:08 -0700177 public interface ItemInfoFilter {
178 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
179 }
180
Bjorn Bringert1307f632013-10-03 22:31:03 +0100181 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 final Context context = app.getContext();
183
Winson Chungee055712013-07-30 14:46:24 -0700184 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100186 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 mIconCache = iconCache;
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700190 Configuration config = res.getConfiguration();
191 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 }
193
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
195 * posted on the main thread handler. */
196 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700197 runOnMainThread(r, 0);
198 }
199 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 // If we are on the worker thread, post onto the main handler
202 mHandler.post(r);
203 } else {
204 r.run();
205 }
206 }
207
208 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
209 * posted on the worker thread handler. */
210 private static void runOnWorkerThread(Runnable r) {
211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 r.run();
213 } else {
214 // If we are not on the worker thread, then post to the worker handler
215 sWorker.post(r);
216 }
217 }
218
Winson Chungc9168342013-06-26 14:54:55 -0700219 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
220 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700221 LauncherAppState app = LauncherAppState.getInstance();
222 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
223 final int xCount = (int) grid.numColumns;
224 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700225 boolean[][] occupied = new boolean[xCount][yCount];
226
227 int cellX, cellY, spanX, spanY;
228 for (int i = 0; i < items.size(); ++i) {
229 final ItemInfo item = items.get(i);
230 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
231 if (item.screenId == screen) {
232 cellX = item.cellX;
233 cellY = item.cellY;
234 spanX = item.spanX;
235 spanY = item.spanY;
236 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
237 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
238 occupied[x][y] = true;
239 }
240 }
241 }
242 }
243 }
244
245 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
246 }
247 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700248 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700249 int firstScreenIndex,
250 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700251 // Lock on the app so that we don't try and get the items while apps are being added
252 LauncherAppState app = LauncherAppState.getInstance();
253 LauncherModel model = app.getModel();
254 boolean found = false;
255 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700256 if (sWorkerThread.getThreadId() != Process.myTid()) {
257 // Flush the LauncherModel worker thread, so that if we just did another
258 // processInstallShortcut, we give it time for its shortcut to get added to the
259 // database (getItemsInLocalCoordinates reads the database)
260 model.flushWorkerThread();
261 }
Winson Chungc9168342013-06-26 14:54:55 -0700262 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700263
264 // Try adding to the workspace screens incrementally, starting at the default or center
265 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700266 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
267 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700269 int[] tmpCoordinates = new int[2];
270 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700271 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700273 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700274 }
275 }
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 return null;
278 }
279
Winson Chung94d67682013-09-25 16:29:40 -0700280 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
281 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700282 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700283 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700284 }
Winson Chung94d67682013-09-25 16:29:40 -0700285 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
286 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
287 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700288 return;
Winson Chung997a9232013-07-24 15:33:46 -0700289 }
Winson Chung64359a52013-07-08 17:17:08 -0700290 // Process the newly added applications and add them to the database first
291 Runnable r = new Runnable() {
292 public void run() {
293 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
294 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
295
Winson Chung76828c82013-08-19 15:43:29 -0700296 // Get the list of workspace screens. We need to append to this list and
297 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
298 // called.
299 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
300 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
301 for (Integer i : orderedScreens.keySet()) {
302 long screenId = orderedScreens.get(i);
303 workspaceScreens.add(screenId);
304 }
305
Winson Chung64359a52013-07-08 17:17:08 -0700306 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700307 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700308 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700309 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700310 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700311 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700312
313 // Short-circuit this logic if the icon exists somewhere on the workspace
314 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
315 continue;
316 }
317
Winson Chung87412982013-10-03 18:34:14 -0700318 // Add this icon to the db, creating a new page if necessary. If there
319 // is only the empty page then we just add items to the first page.
320 // Otherwise, we add them to the next pages.
321 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700322 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700323 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700324 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200325 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700326
327 // If we can't find a valid position, then just add a new screen.
328 // This takes time so we need to re-queue the add until the new
329 // page is added. Create as many screens as necessary to satisfy
330 // the startSearchPageIndex.
331 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700332 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700333 while (numPagesToAdd > 0) {
334 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700335 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700336 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700337 addedWorkspaceScreensFinal.add(screenId);
338 numPagesToAdd--;
339 }
Winson Chung76828c82013-08-19 15:43:29 -0700340
Winson Chung64359a52013-07-08 17:17:08 -0700341 // Find the coordinate again
342 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700343 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700344 }
345 if (coords == null) {
346 throw new RuntimeException("Coordinates should not be null");
347 }
348
Winson Chung997a9232013-07-24 15:33:46 -0700349 ShortcutInfo shortcutInfo;
350 if (a instanceof ShortcutInfo) {
351 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200352 } else if (a instanceof AppInfo) {
353 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700354 } else {
355 throw new RuntimeException("Unexpected info type");
356 }
Winson Chung94d67682013-09-25 16:29:40 -0700357
Winson Chung64359a52013-07-08 17:17:08 -0700358 // Add the shortcut to the db
359 addItemToDatabase(context, shortcutInfo,
360 LauncherSettings.Favorites.CONTAINER_DESKTOP,
361 coords.first, coords.second[0], coords.second[1], false);
362 // Save the ShortcutInfo for binding in the workspace
363 addedShortcutsFinal.add(shortcutInfo);
364 }
365 }
366
Winson Chung76828c82013-08-19 15:43:29 -0700367 // Update the workspace screens
368 updateWorkspaceScreenOrder(context, workspaceScreens);
369
Winson Chung94d67682013-09-25 16:29:40 -0700370 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700371 runOnMainThread(new Runnable() {
372 public void run() {
373 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
374 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700375 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
376 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700377 if (!addedShortcutsFinal.isEmpty()) {
378 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
379 long lastScreenId = info.screenId;
380 for (ItemInfo i : addedShortcutsFinal) {
381 if (i.screenId == lastScreenId) {
382 addAnimated.add(i);
383 } else {
384 addNotAnimated.add(i);
385 }
Winson Chung997a9232013-07-24 15:33:46 -0700386 }
387 }
Winson Chungd64d1762013-08-20 14:37:16 -0700388 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700389 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700390 }
Winson Chung64359a52013-07-08 17:17:08 -0700391 }
Winson Chung997a9232013-07-24 15:33:46 -0700392 });
393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
395 };
396 runOnWorkerThread(r);
397 }
398
Joe Onorato56d82912010-03-07 14:32:10 -0500399 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700400 if (mDefaultIcon == null) {
401 final Context context = LauncherAppState.getInstance().getContext();
402 mDefaultIcon = Utilities.createIconBitmap(
403 mIconCache.getFullResDefaultActivityIcon(), context);
404 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800405 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407
Winson Chung81b52252012-08-27 15:34:29 -0700408 public void unbindItemInfosAndClearQueuedBindRunnables() {
409 if (sWorkerThread.getThreadId() == Process.myTid()) {
410 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
411 "main thread");
412 }
413
414 // Clear any deferred bind runnables
415 mDeferredBindRunnables.clear();
416 // Remove any queued bind runnables
417 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
418 // Unbind all the workspace items
419 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700420 }
421
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700423 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700424 // Ensure that we don't use the same workspace items data structure on the main thread
425 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
427 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700428 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
430 tmpAppWidgets.addAll(sBgAppWidgets);
431 }
432 Runnable r = new Runnable() {
433 @Override
434 public void run() {
435 for (ItemInfo item : tmpWorkspaceItems) {
436 item.unbind();
437 }
438 for (ItemInfo item : tmpAppWidgets) {
439 item.unbind();
440 }
441 }
442 };
443 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700444 }
445
Joe Onorato9c1289c2009-08-17 11:03:03 -0400446 /**
447 * Adds an item to the DB if it was not created previously, or move it to a new
448 * <container, screen, cellX, cellY>
449 */
450 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700451 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400452 if (item.container == ItemInfo.NO_ID) {
453 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 } else {
456 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700457 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459 }
460
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700461 static void checkItemInfoLocked(
462 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
463 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
464 if (modelItem != null && item != modelItem) {
465 // check all the data is consistent
466 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
467 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
468 ShortcutInfo shortcut = (ShortcutInfo) item;
469 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
470 modelShortcut.intent.filterEquals(shortcut.intent) &&
471 modelShortcut.id == shortcut.id &&
472 modelShortcut.itemType == shortcut.itemType &&
473 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700474 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700475 modelShortcut.cellX == shortcut.cellX &&
476 modelShortcut.cellY == shortcut.cellY &&
477 modelShortcut.spanX == shortcut.spanX &&
478 modelShortcut.spanY == shortcut.spanY &&
479 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
480 (modelShortcut.dropPos != null &&
481 shortcut.dropPos != null &&
482 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
483 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
484 // For all intents and purposes, this is the same object
485 return;
486 }
487 }
488
489 // the modelItem needs to match up perfectly with item if our model is
490 // to be consistent with the database-- for now, just require
491 // modelItem == item or the equality check above
492 String msg = "item: " + ((item != null) ? item.toString() : "null") +
493 "modelItem: " +
494 ((modelItem != null) ? modelItem.toString() : "null") +
495 "Error: ItemInfo passed to checkItemInfo doesn't match original";
496 RuntimeException e = new RuntimeException(msg);
497 if (stackTrace != null) {
498 e.setStackTrace(stackTrace);
499 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800500 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700501 }
502 }
503
Michael Jurka816474f2012-06-25 14:49:02 -0700504 static void checkItemInfo(final ItemInfo item) {
505 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
506 final long itemId = item.id;
507 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700508 public void run() {
509 synchronized (sBgLock) {
510 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700511 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700512 }
513 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700514 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700515 }
516
Michael Jurkac9d95c52011-08-29 14:03:34 -0700517 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
518 final ItemInfo item, final String callingFunction) {
519 final long itemId = item.id;
520 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
521 final ContentResolver cr = context.getContentResolver();
522
Adam Cohen487f7dd2012-06-28 18:12:10 -0700523 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700524 Runnable r = new Runnable() {
525 public void run() {
526 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700527 updateItemArrays(item, itemId, stackTrace);
528 }
529 };
530 runOnWorkerThread(r);
531 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700532
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700533 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
534 final ArrayList<ItemInfo> items, final String callingFunction) {
535 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700536
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700537 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
538 Runnable r = new Runnable() {
539 public void run() {
540 ArrayList<ContentProviderOperation> ops =
541 new ArrayList<ContentProviderOperation>();
542 int count = items.size();
543 for (int i = 0; i < count; i++) {
544 ItemInfo item = items.get(i);
545 final long itemId = item.id;
546 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
547 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700548
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700549 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
550 updateItemArrays(item, itemId, stackTrace);
551
552 }
553 try {
554 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
555 } catch (Exception e) {
556 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700557 }
558 }
559 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700560 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700561 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700562
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700563 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
564 // Lock on mBgLock *after* the db operation
565 synchronized (sBgLock) {
566 checkItemInfoLocked(itemId, item, stackTrace);
567
568 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
569 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
570 // Item is in a folder, make sure this folder exists
571 if (!sBgFolders.containsKey(item.container)) {
572 // An items container is being set to a that of an item which is not in
573 // the list of Folders.
574 String msg = "item: " + item + " container being set to: " +
575 item.container + ", not in the list of folders";
576 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700577 }
578 }
579
580 // Items are added/removed from the corresponding FolderInfo elsewhere, such
581 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
582 // that are on the desktop, as appropriate
583 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
584 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
585 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
586 switch (modelItem.itemType) {
587 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
588 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
589 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
590 if (!sBgWorkspaceItems.contains(modelItem)) {
591 sBgWorkspaceItems.add(modelItem);
592 }
593 break;
594 default:
595 break;
596 }
597 } else {
598 sBgWorkspaceItems.remove(modelItem);
599 }
600 }
601 }
602
Michael Jurkac7700af2013-05-14 20:17:58 +0200603 public void flushWorkerThread() {
604 mFlushingWorkerThread = true;
605 Runnable waiter = new Runnable() {
606 public void run() {
607 synchronized (this) {
608 notifyAll();
609 mFlushingWorkerThread = false;
610 }
611 }
612 };
613
614 synchronized(waiter) {
615 runOnWorkerThread(waiter);
616 if (mLoaderTask != null) {
617 synchronized(mLoaderTask) {
618 mLoaderTask.notify();
619 }
620 }
621 boolean success = false;
622 while (!success) {
623 try {
624 waiter.wait();
625 success = true;
626 } catch (InterruptedException e) {
627 }
628 }
629 }
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700634 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700635 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700636 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 item.cellX = cellX;
639 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700640
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 // We store hotseat items in canonical form which is this orientation invariant position
642 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700643 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700645 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649
650 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
653 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700654 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
Michael Jurkac9d95c52011-08-29 14:03:34 -0700656 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700657 }
658
659 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700660 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
661 * cellX, cellY have already been updated on the ItemInfos.
662 */
663 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
664 final long container, final int screen) {
665
666 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
667 int count = items.size();
668
669 for (int i = 0; i < count; i++) {
670 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700671 item.container = container;
672
673 // We store hotseat items in canonical form which is this orientation invariant position
674 // in the hotseat
675 if (context instanceof Launcher && screen < 0 &&
676 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678 item.cellY);
679 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681 }
682
683 final ContentValues values = new ContentValues();
684 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
685 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
686 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700687 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700688
689 contentValues.add(values);
690 }
691 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
692 }
693
694 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700695 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800696 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700697 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700698 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700699 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800700 item.cellX = cellX;
701 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700702 item.spanX = spanX;
703 item.spanY = spanY;
704
705 // We store hotseat items in canonical form which is this orientation invariant position
706 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700707 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700710 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 }
Adam Cohend4844c32011-02-18 19:25:06 -0800713
Adam Cohend4844c32011-02-18 19:25:06 -0800714 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800715 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
717 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
718 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
719 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800721
Michael Jurka816474f2012-06-25 14:49:02 -0700722 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700723 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700724
725 /**
726 * Update an item to the database in a specified container.
727 */
728 static void updateItemInDatabase(Context context, final ItemInfo item) {
729 final ContentValues values = new ContentValues();
730 item.onAddToDatabase(values);
731 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
732 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800733 }
734
735 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 * Returns true if the shortcuts already exists in the database.
737 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 static boolean shortcutExists(Context context, String title, Intent intent) {
740 final ContentResolver cr = context.getContentResolver();
741 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
742 new String[] { "title", "intent" }, "title=? and intent=?",
743 new String[] { title, intent.toUri(0) }, null);
744 boolean result = false;
745 try {
746 result = c.moveToFirst();
747 } finally {
748 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700751 }
752
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700754 * Returns an ItemInfo array containing all the items in the LauncherModel.
755 * The ItemInfo.id is not set through this function.
756 */
757 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
758 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
759 final ContentResolver cr = context.getContentResolver();
760 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
761 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
762 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
763 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
764
765 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
766 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
767 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
768 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
769 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
770 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
771 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
772
773 try {
774 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700775 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700776 item.cellX = c.getInt(cellXIndex);
777 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700778 item.spanX = Math.max(1, c.getInt(spanXIndex));
779 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 item.container = c.getInt(containerIndex);
781 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700782 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700783
784 items.add(item);
785 }
786 } catch (Exception e) {
787 items.clear();
788 } finally {
789 c.close();
790 }
791
792 return items;
793 }
794
795 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
797 */
798 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
799 final ContentResolver cr = context.getContentResolver();
800 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
801 "_id=? and (itemType=? or itemType=?)",
802 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700803 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700804
Joe Onorato9c1289c2009-08-17 11:03:03 -0400805 try {
806 if (c.moveToFirst()) {
807 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
808 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
809 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
810 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
811 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
812 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 FolderInfo folderInfo = null;
815 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700816 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
817 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700819 }
820
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821 folderInfo.title = c.getString(titleIndex);
822 folderInfo.id = id;
823 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700825 folderInfo.cellX = c.getInt(cellXIndex);
826 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827
828 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830 } finally {
831 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700832 }
833
834 return null;
835 }
836
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837 /**
838 * Add an item to the database in a specified container. Sets the container, screen, cellX and
839 * cellY fields of the item. Also assigns an ID to the item.
840 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700841 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 item.cellX = cellX;
845 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 // We store hotseat items in canonical form which is this orientation invariant position
847 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854
855 final ContentValues values = new ContentValues();
856 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857 item.onAddToDatabase(values);
858
Michael Jurka414300a2013-08-27 15:42:35 +0200859 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700860 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700862
Michael Jurkac9d95c52011-08-29 14:03:34 -0700863 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700864 public void run() {
865 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
866 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400867
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700869 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700870 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700871 sBgItemsIdMap.put(item.id, item);
872 switch (item.itemType) {
873 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
874 sBgFolders.put(item.id, (FolderInfo) item);
875 // Fall through
876 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
877 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
878 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
879 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
880 sBgWorkspaceItems.add(item);
881 } else {
882 if (!sBgFolders.containsKey(item.container)) {
883 // Adding an item to a folder that doesn't exist.
884 String msg = "adding item: " + item + " to a folder that " +
885 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700886 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700887 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700889 break;
890 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
891 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
892 break;
893 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700894 }
895 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700896 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700897 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700898 }
899
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700901 * Creates a new unique child id, for a given cell span across all layouts.
902 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700903 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700905 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700907 }
908
Winson Chungaafa03c2010-06-11 17:34:16 -0700909 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700910 * Removes the specified item from the database
911 * @param context
912 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700916 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700917
Michael Jurka83df1882011-08-31 20:59:26 -0700918 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700919 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921
922 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700923 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700924 switch (item.itemType) {
925 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
926 sBgFolders.remove(item.id);
927 for (ItemInfo info: sBgItemsIdMap.values()) {
928 if (info.container == item.id) {
929 // We are deleting a folder which still contains items that
930 // think they are contained by that folder.
931 String msg = "deleting a folder (" + item + ") which still " +
932 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700933 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700934 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700936 sBgWorkspaceItems.remove(item);
937 break;
938 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
939 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
940 sBgWorkspaceItems.remove(item);
941 break;
942 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
943 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
944 break;
945 }
946 sBgItemsIdMap.remove(item.id);
947 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700948 }
949 }
Michael Jurka83df1882011-08-31 20:59:26 -0700950 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700951 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 }
953
954 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700955 * Update the order of the workspace screens in the database. The array list contains
956 * a list of screen ids in the order that they should appear.
957 */
Winson Chungc9168342013-06-26 14:54:55 -0700958 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800959 // Log to disk
960 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
961 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
962
Winson Chung64359a52013-07-08 17:17:08 -0700963 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 final ContentResolver cr = context.getContentResolver();
965 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
966
967 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700968 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700969 while (iter.hasNext()) {
970 long id = iter.next();
971 if (id < 0) {
972 iter.remove();
973 }
974 }
975
976 Runnable r = new Runnable() {
977 @Override
978 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 // Clear the table
980 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700981 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 ContentValues[] values = new ContentValues[count];
983 for (int i = 0; i < count; i++) {
984 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700985 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
987 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 values[i] = v;
989 }
990 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700991
Winson Chungba9c37f2013-08-30 14:11:37 -0700992 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700993 sBgWorkspaceScreens.clear();
994 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700995 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700996 }
997 };
998 runOnWorkerThread(r);
999 }
1000
1001 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 * Remove the contents of the specified folder from the database
1003 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001004 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 final ContentResolver cr = context.getContentResolver();
1006
Michael Jurkac9d95c52011-08-29 14:03:34 -07001007 Runnable r = new Runnable() {
1008 public void run() {
1009 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001010 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001011 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001012 sBgItemsIdMap.remove(info.id);
1013 sBgFolders.remove(info.id);
1014 sBgDbIconCache.remove(info);
1015 sBgWorkspaceItems.remove(info);
1016 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001017
Michael Jurkac9d95c52011-08-29 14:03:34 -07001018 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1019 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001020 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001021 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 for (ItemInfo childInfo : info.contents) {
1023 sBgItemsIdMap.remove(childInfo.id);
1024 sBgDbIconCache.remove(childInfo);
1025 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001026 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001027 }
1028 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001029 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 }
1031
1032 /**
1033 * Set this as the current Launcher activity object for the loader.
1034 */
1035 public void initialize(Callbacks callbacks) {
1036 synchronized (mLock) {
1037 mCallbacks = new WeakReference<Callbacks>(callbacks);
1038 }
1039 }
1040
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001041 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1043 * ACTION_PACKAGE_CHANGED.
1044 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001045 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001046 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001047 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001048
Joe Onorato36115782010-06-17 13:28:48 -04001049 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001050
Joe Onorato36115782010-06-17 13:28:48 -04001051 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1052 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1053 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1054 final String packageName = intent.getData().getSchemeSpecificPart();
1055 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056
Joe Onorato36115782010-06-17 13:28:48 -04001057 int op = PackageUpdatedTask.OP_NONE;
1058
1059 if (packageName == null || packageName.length() == 0) {
1060 // they sent us a bad intent
1061 return;
1062 }
1063
1064 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1065 op = PackageUpdatedTask.OP_UPDATE;
1066 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1067 if (!replacing) {
1068 op = PackageUpdatedTask.OP_REMOVE;
1069 }
1070 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1071 // later, we will update the package at this time
1072 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1073 if (!replacing) {
1074 op = PackageUpdatedTask.OP_ADD;
1075 } else {
1076 op = PackageUpdatedTask.OP_UPDATE;
1077 }
1078 }
1079
1080 if (op != PackageUpdatedTask.OP_NONE) {
1081 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1082 }
1083
1084 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001085 // First, schedule to add these apps back in.
1086 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1087 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1088 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001089 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001090 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1091 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1092 enqueuePackageUpdated(new PackageUpdatedTask(
1093 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001094 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001095 // If we have changed locale we need to clear out the labels in all apps/workspace.
1096 forceReload();
1097 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1098 // Check if configuration change was an mcc/mnc change which would affect app resources
1099 // and we would need to clear out the labels in all apps/workspace. Same handling as
1100 // above for ACTION_LOCALE_CHANGED
1101 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001102 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001103 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001104 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001105 forceReload();
1106 }
1107 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001108 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001109 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1110 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001111 if (mCallbacks != null) {
1112 Callbacks callbacks = mCallbacks.get();
1113 if (callbacks != null) {
1114 callbacks.bindSearchablesChanged();
1115 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001116 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001117 }
1118 }
1119
Reena Lee93f824a2011-09-23 17:20:28 -07001120 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001121 resetLoadedState(true, true);
1122
Reena Lee93f824a2011-09-23 17:20:28 -07001123 // Do this here because if the launcher activity is running it will be restarted.
1124 // If it's not running startLoaderFromBackground will merely tell it that it needs
1125 // to reload.
1126 startLoaderFromBackground();
1127 }
1128
Winson Chungf0c6ae02012-03-21 16:10:31 -07001129 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1130 synchronized (mLock) {
1131 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1132 // mWorkspaceLoaded to true later
1133 stopLoaderLocked();
1134 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1135 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1136 }
1137 }
1138
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001139 /**
1140 * When the launcher is in the background, it's possible for it to miss paired
1141 * configuration changes. So whenever we trigger the loader from the background
1142 * tell the launcher that it needs to re-run the loader when it comes back instead
1143 * of doing it now.
1144 */
1145 public void startLoaderFromBackground() {
1146 boolean runLoader = false;
1147 if (mCallbacks != null) {
1148 Callbacks callbacks = mCallbacks.get();
1149 if (callbacks != null) {
1150 // Only actually run the loader if they're not paused.
1151 if (!callbacks.setLoadOnResume()) {
1152 runLoader = true;
1153 }
1154 }
1155 }
1156 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001157 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001158 }
Joe Onorato36115782010-06-17 13:28:48 -04001159 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001160
Reena Lee93f824a2011-09-23 17:20:28 -07001161 // If there is already a loader task running, tell it to stop.
1162 // returns true if isLaunching() was true on the old task
1163 private boolean stopLoaderLocked() {
1164 boolean isLaunching = false;
1165 LoaderTask oldTask = mLoaderTask;
1166 if (oldTask != null) {
1167 if (oldTask.isLaunching()) {
1168 isLaunching = true;
1169 }
1170 oldTask.stopLocked();
1171 }
1172 return isLaunching;
1173 }
1174
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001175 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001176 synchronized (mLock) {
1177 if (DEBUG_LOADERS) {
1178 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001180
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001181 // Clear any deferred bind-runnables from the synchronized load process
1182 // We must do this before any loading/binding is scheduled below.
1183 mDeferredBindRunnables.clear();
1184
Joe Onorato36115782010-06-17 13:28:48 -04001185 // Don't bother to start the thread if we know it's not going to do anything
1186 if (mCallbacks != null && mCallbacks.get() != null) {
1187 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001188 // also, don't downgrade isLaunching if we're already running
1189 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001190 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001191 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1192 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1193 } else {
1194 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1195 sWorker.post(mLoaderTask);
1196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 }
1199 }
1200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 void bindRemainingSynchronousPages() {
1202 // Post the remaining side pages to be loaded
1203 if (!mDeferredBindRunnables.isEmpty()) {
1204 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001205 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001206 }
1207 mDeferredBindRunnables.clear();
1208 }
1209 }
1210
Joe Onorato36115782010-06-17 13:28:48 -04001211 public void stopLoader() {
1212 synchronized (mLock) {
1213 if (mLoaderTask != null) {
1214 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 }
1216 }
Joe Onorato36115782010-06-17 13:28:48 -04001217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218
Winson Chung76828c82013-08-19 15:43:29 -07001219 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1220 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1221 final ContentResolver contentResolver = context.getContentResolver();
1222 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1223 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1224 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1225
1226 try {
1227 final int idIndex = sc.getColumnIndexOrThrow(
1228 LauncherSettings.WorkspaceScreens._ID);
1229 final int rankIndex = sc.getColumnIndexOrThrow(
1230 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1231 while (sc.moveToNext()) {
1232 try {
1233 long screenId = sc.getLong(idIndex);
1234 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001235 orderedScreens.put(rank, screenId);
1236 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001237 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001238 }
1239 }
1240 } finally {
1241 sc.close();
1242 }
Winson Chunga90303b2013-11-15 13:05:06 -08001243
1244 // Log to disk
1245 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1246 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1247 for (Integer i : orderedScreens.keySet()) {
1248 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1249 }
1250 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1251 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001252 return orderedScreens;
1253 }
1254
Michael Jurkac57b7a82011-08-09 22:02:20 -07001255 public boolean isAllAppsLoaded() {
1256 return mAllAppsLoaded;
1257 }
1258
Winson Chung36a62fe2012-05-06 18:04:42 -07001259 boolean isLoadingWorkspace() {
1260 synchronized (mLock) {
1261 if (mLoaderTask != null) {
1262 return mLoaderTask.isLoadingWorkspace();
1263 }
1264 }
1265 return false;
1266 }
1267
Joe Onorato36115782010-06-17 13:28:48 -04001268 /**
1269 * Runnable for the thread that loads the contents of the launcher:
1270 * - workspace icons
1271 * - widgets
1272 * - all apps icons
1273 */
1274 private class LoaderTask implements Runnable {
1275 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001276 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001277 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001278 private boolean mStopped;
1279 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001280
Winson Chungc3eecff2011-07-11 17:44:15 -07001281 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001282
1283 LoaderTask(Context context, boolean isLaunching) {
1284 mContext = context;
1285 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001286 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 }
1288
Joe Onorato36115782010-06-17 13:28:48 -04001289 boolean isLaunching() {
1290 return mIsLaunching;
1291 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292
Winson Chung36a62fe2012-05-06 18:04:42 -07001293 boolean isLoadingWorkspace() {
1294 return mIsLoadingAndBindingWorkspace;
1295 }
1296
Winson Chungc763c4e2013-07-19 13:49:06 -07001297 /** Returns whether this is an upgrade path */
1298 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001299 mIsLoadingAndBindingWorkspace = true;
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001302 if (DEBUG_LOADERS) {
1303 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001305
Winson Chungc763c4e2013-07-19 13:49:06 -07001306 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001307 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001308 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001309 synchronized (LoaderTask.this) {
1310 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001311 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001312 }
1313 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 }
1315 }
1316
Joe Onorato36115782010-06-17 13:28:48 -04001317 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001318 bindWorkspace(-1, isUpgradePath);
1319 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001320 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001321
Joe Onorato36115782010-06-17 13:28:48 -04001322 private void waitForIdle() {
1323 // Wait until the either we're stopped or the other threads are done.
1324 // This way we don't start loading all apps until the workspace has settled
1325 // down.
1326 synchronized (LoaderTask.this) {
1327 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001328
Joe Onorato36115782010-06-17 13:28:48 -04001329 mHandler.postIdle(new Runnable() {
1330 public void run() {
1331 synchronized (LoaderTask.this) {
1332 mLoadAndBindStepFinished = true;
1333 if (DEBUG_LOADERS) {
1334 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001337 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001338 }
Joe Onorato36115782010-06-17 13:28:48 -04001339 });
1340
Michael Jurkac7700af2013-05-14 20:17:58 +02001341 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001342 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001343 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1344 // wait no longer than 1sec at a time
1345 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001346 } catch (InterruptedException ex) {
1347 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001348 }
1349 }
Joe Onorato36115782010-06-17 13:28:48 -04001350 if (DEBUG_LOADERS) {
1351 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001352 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001353 + "ms for previous step to finish binding");
1354 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001355 }
Joe Onorato36115782010-06-17 13:28:48 -04001356 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001357
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001358 void runBindSynchronousPage(int synchronousBindPage) {
1359 if (synchronousBindPage < 0) {
1360 // Ensure that we have a valid page index to load synchronously
1361 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1362 "valid page index");
1363 }
1364 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1365 // Ensure that we don't try and bind a specified page when the pages have not been
1366 // loaded already (we should load everything asynchronously in that case)
1367 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1368 }
1369 synchronized (mLock) {
1370 if (mIsLoaderTaskRunning) {
1371 // Ensure that we are never running the background loading at this point since
1372 // we also touch the background collections
1373 throw new RuntimeException("Error! Background loading is already running");
1374 }
1375 }
1376
1377 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1378 // data structures, we can't allow any other thread to touch that data, but because
1379 // this call is synchronous, we can get away with not locking).
1380
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001381 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001382 // operations from the previous activity. We need to ensure that all queued operations
1383 // are executed before any synchronous binding work is done.
1384 mHandler.flush();
1385
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001386 // Divide the set of loaded items into those that we are binding synchronously, and
1387 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001388 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1390 // arise from that.
1391 onlyBindAllApps();
1392 }
1393
Joe Onorato36115782010-06-17 13:28:48 -04001394 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001395 boolean isUpgrade = false;
1396
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001397 synchronized (mLock) {
1398 mIsLoaderTaskRunning = true;
1399 }
Joe Onorato36115782010-06-17 13:28:48 -04001400 // Optimize for end-user experience: if the Launcher is up and // running with the
1401 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1402 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001403 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001404 // Elevate priority when Home launches for the first time to avoid
1405 // starving at boot time. Staring at a blank home is not cool.
1406 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001407 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1408 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 android.os.Process.setThreadPriority(mIsLaunching
1410 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1411 }
Winson Chung64359a52013-07-08 17:17:08 -07001412 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001413 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001414
Joe Onorato36115782010-06-17 13:28:48 -04001415 if (mStopped) {
1416 break keep_running;
1417 }
1418
1419 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1420 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001421 synchronized (mLock) {
1422 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001423 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001424 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1425 }
1426 }
Joe Onorato36115782010-06-17 13:28:48 -04001427 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001428
1429 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001430 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1431 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001432
1433 // Restore the default thread priority after we are done loading items
1434 synchronized (mLock) {
1435 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1436 }
Joe Onorato36115782010-06-17 13:28:48 -04001437 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001438
Winson Chungaac01e12011-08-17 10:37:13 -07001439 // Update the saved icons if necessary
1440 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001441 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001442 for (Object key : sBgDbIconCache.keySet()) {
1443 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1444 }
1445 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001446 }
Winson Chungaac01e12011-08-17 10:37:13 -07001447
Winson Chungc58497e2013-09-03 17:48:37 -07001448 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1449 // Ensure that all the applications that are in the system are
1450 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001451 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001452 verifyApplications();
1453 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001454 }
1455
Joe Onorato36115782010-06-17 13:28:48 -04001456 // Clear out this reference, otherwise we end up holding it until all of the
1457 // callback runnables are done.
1458 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459
Joe Onorato36115782010-06-17 13:28:48 -04001460 synchronized (mLock) {
1461 // If we are still the last one to be scheduled, remove ourselves.
1462 if (mLoaderTask == this) {
1463 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001465 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001466 }
Joe Onorato36115782010-06-17 13:28:48 -04001467 }
1468
1469 public void stopLocked() {
1470 synchronized (LoaderTask.this) {
1471 mStopped = true;
1472 this.notify();
1473 }
1474 }
1475
1476 /**
1477 * Gets the callbacks object. If we've been stopped, or if the launcher object
1478 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1479 * object that was around when the deferred message was scheduled, and if there's
1480 * a new Callbacks object around then also return null. This will save us from
1481 * calling onto it with data that will be ignored.
1482 */
1483 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1484 synchronized (mLock) {
1485 if (mStopped) {
1486 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001487 }
Joe Onorato36115782010-06-17 13:28:48 -04001488
1489 if (mCallbacks == null) {
1490 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492
1493 final Callbacks callbacks = mCallbacks.get();
1494 if (callbacks != oldCallbacks) {
1495 return null;
1496 }
1497 if (callbacks == null) {
1498 Log.w(TAG, "no mCallbacks");
1499 return null;
1500 }
1501
1502 return callbacks;
1503 }
1504 }
1505
Winson Chungc763c4e2013-07-19 13:49:06 -07001506 private void verifyApplications() {
1507 final Context context = mApp.getContext();
1508
1509 // Cross reference all the applications in our apps list with items in the workspace
1510 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001511 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001512 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001513 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001514 tmpInfos = getItemInfoForComponentName(app.componentName);
1515 if (tmpInfos.isEmpty()) {
1516 // We are missing an application icon, so add this to the workspace
1517 added.add(app);
1518 // This is a rare event, so lets log it
1519 Log.e(TAG, "Missing Application on load: " + app);
1520 }
1521 }
1522 }
1523 if (!added.isEmpty()) {
1524 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001525 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001526 }
1527 }
1528
Winson Chung5f8afe62013-08-12 16:19:28 -07001529 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001530 LauncherAppState app = LauncherAppState.getInstance();
1531 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1532 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1533 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001534 }
1535
Joe Onorato36115782010-06-17 13:28:48 -04001536 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001537 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1538 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001539 LauncherAppState app = LauncherAppState.getInstance();
1540 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1541 int countX = (int) grid.numColumns;
1542 int countY = (int) grid.numRows;
1543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001545 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001546 // Return early if we detect that an item is under the hotseat button
1547 if (mCallbacks == null ||
1548 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1549 deleteOnItemOverlap.set(true);
1550 return false;
1551 }
1552
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1554 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1555 [(int) item.screenId][0] != null) {
1556 Log.e(TAG, "Error loading shortcut into hotseat " + item
1557 + " into position (" + item.screenId + ":" + item.cellX + ","
1558 + item.cellY + ") occupied by "
1559 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1560 [(int) item.screenId][0]);
1561 return false;
1562 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001563 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001564 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 items[(int) item.screenId][0] = item;
1566 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001567 return true;
1568 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001569 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1570 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001571 return true;
1572 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001573
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001575 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 occupied.put(item.screenId, items);
1577 }
1578
1579 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001580 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001581 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1582 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001584 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001586 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001587 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001589 return false;
1590 }
1591 }
1592 }
1593 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1594 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001596 }
1597 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001598
Joe Onorato36115782010-06-17 13:28:48 -04001599 return true;
1600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601
Winson Chungba9c37f2013-08-30 14:11:37 -07001602 /** Clears all the sBg data structures */
1603 private void clearSBgDataStructures() {
1604 synchronized (sBgLock) {
1605 sBgWorkspaceItems.clear();
1606 sBgAppWidgets.clear();
1607 sBgFolders.clear();
1608 sBgItemsIdMap.clear();
1609 sBgDbIconCache.clear();
1610 sBgWorkspaceScreens.clear();
1611 }
1612 }
1613
Winson Chungc763c4e2013-07-19 13:49:06 -07001614 /** Returns whether this is an upgradge path */
1615 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001616 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617
Joe Onorato36115782010-06-17 13:28:48 -04001618 final Context context = mContext;
1619 final ContentResolver contentResolver = context.getContentResolver();
1620 final PackageManager manager = context.getPackageManager();
1621 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1622 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001623
Winson Chung892c74d2013-08-22 16:15:50 -07001624 LauncherAppState app = LauncherAppState.getInstance();
1625 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1626 int countX = (int) grid.numColumns;
1627 int countY = (int) grid.numRows;
1628
Michael Jurkab85f8a42012-04-25 15:48:32 -07001629 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001630 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001631
Winson Chungc763c4e2013-07-19 13:49:06 -07001632 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001633 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001634
Winson Chung2abf94d2012-07-18 18:16:38 -07001635 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001636 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001637
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001638 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001639 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001640 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001641 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001642
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001643 // +1 for the hotseat (it can be larger than the workspace)
1644 // Load workspace in reverse order to ensure that latest items are loaded first (and
1645 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001646 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001648 try {
1649 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1650 final int intentIndex = c.getColumnIndexOrThrow
1651 (LauncherSettings.Favorites.INTENT);
1652 final int titleIndex = c.getColumnIndexOrThrow
1653 (LauncherSettings.Favorites.TITLE);
1654 final int iconTypeIndex = c.getColumnIndexOrThrow(
1655 LauncherSettings.Favorites.ICON_TYPE);
1656 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1657 final int iconPackageIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.ICON_PACKAGE);
1659 final int iconResourceIndex = c.getColumnIndexOrThrow(
1660 LauncherSettings.Favorites.ICON_RESOURCE);
1661 final int containerIndex = c.getColumnIndexOrThrow(
1662 LauncherSettings.Favorites.CONTAINER);
1663 final int itemTypeIndex = c.getColumnIndexOrThrow(
1664 LauncherSettings.Favorites.ITEM_TYPE);
1665 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1666 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001667 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1668 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001669 final int screenIndex = c.getColumnIndexOrThrow(
1670 LauncherSettings.Favorites.SCREEN);
1671 final int cellXIndex = c.getColumnIndexOrThrow
1672 (LauncherSettings.Favorites.CELLX);
1673 final int cellYIndex = c.getColumnIndexOrThrow
1674 (LauncherSettings.Favorites.CELLY);
1675 final int spanXIndex = c.getColumnIndexOrThrow
1676 (LauncherSettings.Favorites.SPANX);
1677 final int spanYIndex = c.getColumnIndexOrThrow(
1678 LauncherSettings.Favorites.SPANY);
1679 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1680 //final int displayModeIndex = c.getColumnIndexOrThrow(
1681 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001682
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001683 ShortcutInfo info;
1684 String intentDescription;
1685 LauncherAppWidgetInfo appWidgetInfo;
1686 int container;
1687 long id;
1688 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001691 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001692 try {
1693 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 switch (itemType) {
1696 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1697 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001698 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 intentDescription = c.getString(intentIndex);
1700 try {
1701 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001702 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001703 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001704 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001705 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001706 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1707 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001708 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001709 // If apps can be on external storage, then we just
1710 // leave them for the user to remove (maybe add
1711 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001712 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001713 }
1714 continue;
1715 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001717 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 continue;
1719 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1722 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1723 titleIndex, mLabelCache);
1724 } else {
1725 info = getShortcutInfo(c, context, iconTypeIndex,
1726 iconPackageIndex, iconResourceIndex, iconIndex,
1727 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001728
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 // App shortcuts that used to be automatically added to Launcher
1730 // didn't always have the correct intent flags set, so do that
1731 // here
1732 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001733 intent.getCategories() != null &&
1734 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001735 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001736 intent.addFlags(
1737 Intent.FLAG_ACTIVITY_NEW_TASK |
1738 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1739 }
Michael Jurka96879562012-03-22 05:54:33 -07001740 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001743 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001744 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 container = c.getInt(containerIndex);
1746 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001747 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 info.cellX = c.getInt(cellXIndex);
1749 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001750 info.spanX = 1;
1751 info.spanY = 1;
1752 // Skip loading items that are out of bounds
1753 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1754 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001755 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1756 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001757 continue;
1758 }
1759 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001760 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001761 deleteOnItemOverlap.set(false);
1762 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1763 if (deleteOnItemOverlap.get()) {
1764 itemsToRemove.add(id);
1765 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001766 break;
1767 }
1768
1769 switch (container) {
1770 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1771 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1772 sBgWorkspaceItems.add(info);
1773 break;
1774 default:
1775 // Item is in a user folder
1776 FolderInfo folderInfo =
1777 findOrMakeFolder(sBgFolders, container);
1778 folderInfo.add(info);
1779 break;
1780 }
1781 sBgItemsIdMap.put(info.id, info);
1782
1783 // now that we've loaded everthing re-save it with the
1784 // icon in case it disappears somehow.
1785 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001786 } else {
1787 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 }
1789 break;
1790
1791 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1792 id = c.getLong(idIndex);
1793 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1794
1795 folderInfo.title = c.getString(titleIndex);
1796 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001799 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 folderInfo.cellX = c.getInt(cellXIndex);
1801 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001802 folderInfo.spanX = 1;
1803 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Winson Chung5f8afe62013-08-12 16:19:28 -07001805 // Skip loading items that are out of bounds
1806 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001807 if (checkItemDimensions(folderInfo)) {
1808 Log.d(TAG, "Skipped loading out of bounds folder");
1809 continue;
1810 }
1811 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001812 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001813 deleteOnItemOverlap.set(false);
1814 if (!checkItemPlacement(occupied, folderInfo,
1815 deleteOnItemOverlap)) {
1816 if (deleteOnItemOverlap.get()) {
1817 itemsToRemove.add(id);
1818 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001819 break;
1820 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001821
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1824 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1825 sBgWorkspaceItems.add(folderInfo);
1826 break;
Joe Onorato36115782010-06-17 13:28:48 -04001827 }
Joe Onorato17a89222011-02-08 17:26:11 -08001828
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001829 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1830 sBgFolders.put(folderInfo.id, folderInfo);
1831 break;
1832
1833 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1834 // Read all Launcher-specific widget details
1835 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001836 String savedProvider = c.getString(appWidgetProviderIndex);
1837
Joe Onorato36115782010-06-17 13:28:48 -04001838 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001839
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 final AppWidgetProviderInfo provider =
1841 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001842
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 if (!isSafeMode && (provider == null || provider.provider == null ||
1844 provider.provider.getPackageName() == null)) {
1845 String log = "Deleting widget that isn't installed anymore: id="
1846 + id + " appWidgetId=" + appWidgetId;
1847 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001848 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 itemsToRemove.add(id);
1850 } else {
1851 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1852 provider.provider);
1853 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001854 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855 appWidgetInfo.cellX = c.getInt(cellXIndex);
1856 appWidgetInfo.cellY = c.getInt(cellYIndex);
1857 appWidgetInfo.spanX = c.getInt(spanXIndex);
1858 appWidgetInfo.spanY = c.getInt(spanYIndex);
1859 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1860 appWidgetInfo.minSpanX = minSpan[0];
1861 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001862
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001863 container = c.getInt(containerIndex);
1864 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1865 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1866 Log.e(TAG, "Widget found where container != " +
1867 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1868 continue;
1869 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870
Adam Cohene25af792013-06-06 23:08:25 -07001871 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001872 // Skip loading items that are out of bounds
1873 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1874 if (checkItemDimensions(appWidgetInfo)) {
1875 Log.d(TAG, "Skipped loading out of bounds app widget");
1876 continue;
1877 }
1878 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001879 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001880 deleteOnItemOverlap.set(false);
1881 if (!checkItemPlacement(occupied, appWidgetInfo,
1882 deleteOnItemOverlap)) {
1883 if (deleteOnItemOverlap.get()) {
1884 itemsToRemove.add(id);
1885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 break;
1887 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001888 String providerName = provider.provider.flattenToString();
1889 if (!providerName.equals(savedProvider)) {
1890 ContentValues values = new ContentValues();
1891 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1892 providerName);
1893 String where = BaseColumns._ID + "= ?";
1894 String[] args = {Integer.toString(c.getInt(idIndex))};
1895 contentResolver.update(contentUri, values, where, args);
1896 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1898 sBgAppWidgets.add(appWidgetInfo);
1899 }
Joe Onorato36115782010-06-17 13:28:48 -04001900 break;
1901 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001902 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001903 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 }
1906 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001907 if (c != null) {
1908 c.close();
1909 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001910 }
1911
Winson Chungba9c37f2013-08-30 14:11:37 -07001912 // Break early if we've stopped loading
1913 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001914 clearSBgDataStructures();
1915 return false;
1916 }
1917
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 if (itemsToRemove.size() > 0) {
1919 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001920 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 // Remove dead items
1922 for (long id : itemsToRemove) {
1923 if (DEBUG_LOADERS) {
1924 Log.d(TAG, "Removed id = " + id);
1925 }
1926 // Don't notify content observers
1927 try {
1928 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1929 null, null);
1930 } catch (RemoteException e) {
1931 Log.w(TAG, "Could not remove id = " + id);
1932 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001933 }
1934 }
1935
Winson Chungc763c4e2013-07-19 13:49:06 -07001936 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 long maxScreenId = 0;
1938 // If we're importing we use the old screen order.
1939 for (ItemInfo item: sBgItemsIdMap.values()) {
1940 long screenId = item.screenId;
1941 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1942 !sBgWorkspaceScreens.contains(screenId)) {
1943 sBgWorkspaceScreens.add(screenId);
1944 if (screenId > maxScreenId) {
1945 maxScreenId = screenId;
1946 }
1947 }
1948 }
1949 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001950
Michael Jurka414300a2013-08-27 15:42:35 +02001951 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001953
1954 // Update the max item id after we load an old db
1955 long maxItemId = 0;
1956 // If we're importing we use the old screen order.
1957 for (ItemInfo item: sBgItemsIdMap.values()) {
1958 maxItemId = Math.max(maxItemId, item.id);
1959 }
Michael Jurka414300a2013-08-27 15:42:35 +02001960 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001962 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1963 for (Integer i : orderedScreens.keySet()) {
1964 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 }
1966
1967 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001968 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 for (ItemInfo item: sBgItemsIdMap.values()) {
1970 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1972 unusedScreens.contains(screenId)) {
1973 unusedScreens.remove(screenId);
1974 }
1975 }
1976
1977 // If there are any empty screens remove them, and update.
1978 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001979 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1981 }
1982 }
1983
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 if (DEBUG_LOADERS) {
1985 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1986 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001988 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001990
Daniel Sandler566da102013-06-25 23:43:45 -04001991 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001992 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001994 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001995 line += " | ";
1996 }
Winson Chung892c74d2013-08-22 16:15:50 -07001997 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001999 }
Joe Onorato36115782010-06-17 13:28:48 -04002000 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002001 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003 }
Joe Onorato36115782010-06-17 13:28:48 -04002004 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002005 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002006 }
2007
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 /** Filters the set of items who are directly or indirectly (via another container) on the
2009 * specified screen. */
2010 private void filterCurrentWorkspaceItems(int currentScreen,
2011 ArrayList<ItemInfo> allWorkspaceItems,
2012 ArrayList<ItemInfo> currentScreenItems,
2013 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002014 // Purge any null ItemInfos
2015 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2016 while (iter.hasNext()) {
2017 ItemInfo i = iter.next();
2018 if (i == null) {
2019 iter.remove();
2020 }
2021 }
2022
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 // If we aren't filtering on a screen, then the set of items to load is the full set of
2024 // items given.
2025 if (currentScreen < 0) {
2026 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002027 }
2028
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 // Order the set of items by their containers first, this allows use to walk through the
2030 // list sequentially, build up a list of containers that are in the specified screen,
2031 // as well as all items in those containers.
2032 Set<Long> itemsOnScreen = new HashSet<Long>();
2033 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2034 @Override
2035 public int compare(ItemInfo lhs, ItemInfo rhs) {
2036 return (int) (lhs.container - rhs.container);
2037 }
2038 });
2039 for (ItemInfo info : allWorkspaceItems) {
2040 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002041 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002042 currentScreenItems.add(info);
2043 itemsOnScreen.add(info.id);
2044 } else {
2045 otherScreenItems.add(info);
2046 }
2047 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2048 currentScreenItems.add(info);
2049 itemsOnScreen.add(info.id);
2050 } else {
2051 if (itemsOnScreen.contains(info.container)) {
2052 currentScreenItems.add(info);
2053 itemsOnScreen.add(info.id);
2054 } else {
2055 otherScreenItems.add(info);
2056 }
2057 }
2058 }
2059 }
2060
2061 /** Filters the set of widgets which are on the specified screen. */
2062 private void filterCurrentAppWidgets(int currentScreen,
2063 ArrayList<LauncherAppWidgetInfo> appWidgets,
2064 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2065 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2066 // If we aren't filtering on a screen, then the set of items to load is the full set of
2067 // widgets given.
2068 if (currentScreen < 0) {
2069 currentScreenWidgets.addAll(appWidgets);
2070 }
2071
2072 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002073 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002074 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002075 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002076 currentScreenWidgets.add(widget);
2077 } else {
2078 otherScreenWidgets.add(widget);
2079 }
2080 }
2081 }
2082
2083 /** Filters the set of folders which are on the specified screen. */
2084 private void filterCurrentFolders(int currentScreen,
2085 HashMap<Long, ItemInfo> itemsIdMap,
2086 HashMap<Long, FolderInfo> folders,
2087 HashMap<Long, FolderInfo> currentScreenFolders,
2088 HashMap<Long, FolderInfo> otherScreenFolders) {
2089 // If we aren't filtering on a screen, then the set of items to load is the full set of
2090 // widgets given.
2091 if (currentScreen < 0) {
2092 currentScreenFolders.putAll(folders);
2093 }
2094
2095 for (long id : folders.keySet()) {
2096 ItemInfo info = itemsIdMap.get(id);
2097 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002098 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002100 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 currentScreenFolders.put(id, folder);
2102 } else {
2103 otherScreenFolders.put(id, folder);
2104 }
2105 }
2106 }
2107
2108 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2109 * right) */
2110 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002111 final LauncherAppState app = LauncherAppState.getInstance();
2112 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 // XXX: review this
2114 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002115 @Override
2116 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002117 int cellCountX = (int) grid.numColumns;
2118 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002119 int screenOffset = cellCountX * cellCountY;
2120 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002121 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002122 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002123 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002124 rhs.cellY * cellCountX + rhs.cellX);
2125 return (int) (lr - rr);
2126 }
2127 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002128 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002129
Adam Cohendcd297f2013-06-18 13:13:40 -07002130 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2131 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002132 final Runnable r = new Runnable() {
2133 @Override
2134 public void run() {
2135 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2136 if (callbacks != null) {
2137 callbacks.bindScreens(orderedScreens);
2138 }
2139 }
2140 };
2141 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2142 }
2143
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002144 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2145 final ArrayList<ItemInfo> workspaceItems,
2146 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2147 final HashMap<Long, FolderInfo> folders,
2148 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002149
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 final boolean postOnMainThread = (deferredBindRunnables != null);
2151
2152 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002153 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002155 final int start = i;
2156 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002157 final Runnable r = new Runnable() {
2158 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002159 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002160 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002161 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002162 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2163 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 }
2165 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 };
2167 if (postOnMainThread) {
2168 deferredBindRunnables.add(r);
2169 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002170 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 }
Joe Onorato36115782010-06-17 13:28:48 -04002172 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173
2174 // Bind the folders
2175 if (!folders.isEmpty()) {
2176 final Runnable r = new Runnable() {
2177 public void run() {
2178 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2179 if (callbacks != null) {
2180 callbacks.bindFolders(folders);
2181 }
2182 }
2183 };
2184 if (postOnMainThread) {
2185 deferredBindRunnables.add(r);
2186 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002187 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002188 }
2189 }
2190
2191 // Bind the widgets, one at a time
2192 N = appWidgets.size();
2193 for (int i = 0; i < N; i++) {
2194 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2195 final Runnable r = new Runnable() {
2196 public void run() {
2197 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2198 if (callbacks != null) {
2199 callbacks.bindAppWidget(widget);
2200 }
2201 }
2202 };
2203 if (postOnMainThread) {
2204 deferredBindRunnables.add(r);
2205 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002206 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002207 }
2208 }
2209 }
2210
2211 /**
2212 * Binds all loaded data to actual views on the main thread.
2213 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002214 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002215 final long t = SystemClock.uptimeMillis();
2216 Runnable r;
2217
2218 // Don't use these two variables in any of the callback runnables.
2219 // Otherwise we hold a reference to them.
2220 final Callbacks oldCallbacks = mCallbacks.get();
2221 if (oldCallbacks == null) {
2222 // This launcher has exited and nobody bothered to tell us. Just bail.
2223 Log.w(TAG, "LoaderTask running with no launcher");
2224 return;
2225 }
2226
Winson Chung4a2afa32012-07-19 14:53:05 -07002227 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2228 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 oldCallbacks.getCurrentWorkspaceScreen();
2230
2231 // Load all the items that are on the current page first (and in the process, unbind
2232 // all the existing workspace items before we call startBinding() below.
2233 unbindWorkspaceItemsOnMainThread();
2234 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2235 ArrayList<LauncherAppWidgetInfo> appWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2238 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002240 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 workspaceItems.addAll(sBgWorkspaceItems);
2242 appWidgets.addAll(sBgAppWidgets);
2243 folders.putAll(sBgFolders);
2244 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 }
2247
2248 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2249 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2250 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2251 new ArrayList<LauncherAppWidgetInfo>();
2252 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2253 new ArrayList<LauncherAppWidgetInfo>();
2254 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2255 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2256
2257 // Separate the items that are on the current screen, and all the other remaining items
2258 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2259 otherWorkspaceItems);
2260 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2261 otherAppWidgets);
2262 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2263 otherFolders);
2264 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2265 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2266
2267 // Tell the workspace that we're about to start binding items
2268 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002269 public void run() {
2270 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2271 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002273 }
2274 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 };
Winson Chung81b52252012-08-27 15:34:29 -07002276 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2279
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 // Load items on the current page
2281 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2282 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002283 if (isLoadingSynchronously) {
2284 r = new Runnable() {
2285 public void run() {
2286 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2287 if (callbacks != null) {
2288 callbacks.onPageBoundSynchronously(currentScreen);
2289 }
2290 }
2291 };
Winson Chung81b52252012-08-27 15:34:29 -07002292 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002293 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294
Winson Chung4a2afa32012-07-19 14:53:05 -07002295 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2296 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002297 mDeferredBindRunnables.clear();
2298 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002299 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300
2301 // Tell the workspace that we're done binding items
2302 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002303 public void run() {
2304 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2305 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002306 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002307 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308
Winson Chung98e030b2012-05-07 16:01:11 -07002309 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002310 if (DEBUG_LOADERS) {
2311 Log.d(TAG, "bound workspace in "
2312 + (SystemClock.uptimeMillis()-t) + "ms");
2313 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002314
2315 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002316 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002318 if (isLoadingSynchronously) {
2319 mDeferredBindRunnables.add(r);
2320 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002321 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002322 }
Joe Onorato36115782010-06-17 13:28:48 -04002323 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002324
Joe Onorato36115782010-06-17 13:28:48 -04002325 private void loadAndBindAllApps() {
2326 if (DEBUG_LOADERS) {
2327 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2328 }
2329 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002330 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002331 synchronized (LoaderTask.this) {
2332 if (mStopped) {
2333 return;
2334 }
2335 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002336 }
Joe Onorato36115782010-06-17 13:28:48 -04002337 } else {
2338 onlyBindAllApps();
2339 }
2340 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002341
Joe Onorato36115782010-06-17 13:28:48 -04002342 private void onlyBindAllApps() {
2343 final Callbacks oldCallbacks = mCallbacks.get();
2344 if (oldCallbacks == null) {
2345 // This launcher has exited and nobody bothered to tell us. Just bail.
2346 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2347 return;
2348 }
2349
2350 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002351 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002352 final ArrayList<AppInfo> list
2353 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002354 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002355 public void run() {
2356 final long t = SystemClock.uptimeMillis();
2357 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2358 if (callbacks != null) {
2359 callbacks.bindAllApplications(list);
2360 }
2361 if (DEBUG_LOADERS) {
2362 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2363 + (SystemClock.uptimeMillis()-t) + "ms");
2364 }
2365 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002366 };
2367 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002368 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002369 r.run();
2370 } else {
2371 mHandler.post(r);
2372 }
Joe Onorato36115782010-06-17 13:28:48 -04002373 }
2374
Winson Chung64359a52013-07-08 17:17:08 -07002375 private void loadAllApps() {
2376 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002377
Joe Onorato36115782010-06-17 13:28:48 -04002378 final Callbacks oldCallbacks = mCallbacks.get();
2379 if (oldCallbacks == null) {
2380 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002381 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002382 return;
2383 }
2384
Winson Chung64359a52013-07-08 17:17:08 -07002385 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002386 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2387 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2388
Winson Chung64359a52013-07-08 17:17:08 -07002389 // Clear the list of apps
2390 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002391
Winson Chung64359a52013-07-08 17:17:08 -07002392 // Query for the set of apps
2393 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2394 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2395 if (DEBUG_LOADERS) {
2396 Log.d(TAG, "queryIntentActivities took "
2397 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2398 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2399 }
2400 // Fail if we don't have any apps
2401 if (apps == null || apps.isEmpty()) {
2402 return;
2403 }
2404 // Sort the applications by name
2405 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2406 Collections.sort(apps,
2407 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2408 if (DEBUG_LOADERS) {
2409 Log.d(TAG, "sort took "
2410 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002411 }
2412
Winson Chung64359a52013-07-08 17:17:08 -07002413 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002414 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002415 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002416 // This builds the icon bitmaps.
2417 mBgAllAppsList.add(new AppInfo(packageManager, app,
2418 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002419 }
2420
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002421 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002422 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2423 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002424
2425 // Post callback on main thread
2426 mHandler.post(new Runnable() {
2427 public void run() {
2428 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002429 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002430 if (callbacks != null) {
2431 callbacks.bindAllApplications(added);
2432 if (DEBUG_LOADERS) {
2433 Log.d(TAG, "bound " + added.size() + " apps in "
2434 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2435 }
2436 } else {
2437 Log.i(TAG, "not binding apps: no Launcher activity");
2438 }
2439 }
2440 });
2441
Joe Onorato36115782010-06-17 13:28:48 -04002442 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002443 Log.d(TAG, "Icons processed in "
2444 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002445 }
2446 }
2447
2448 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002449 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002450 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2451 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2452 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2453 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2454 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2455 }
Joe Onorato36115782010-06-17 13:28:48 -04002456 }
2457 }
2458
2459 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002460 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002461 }
2462
2463 private class PackageUpdatedTask implements Runnable {
2464 int mOp;
2465 String[] mPackages;
2466
2467 public static final int OP_NONE = 0;
2468 public static final int OP_ADD = 1;
2469 public static final int OP_UPDATE = 2;
2470 public static final int OP_REMOVE = 3; // uninstlled
2471 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2472
2473
2474 public PackageUpdatedTask(int op, String[] packages) {
2475 mOp = op;
2476 mPackages = packages;
2477 }
2478
2479 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002480 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002481
2482 final String[] packages = mPackages;
2483 final int N = packages.length;
2484 switch (mOp) {
2485 case OP_ADD:
2486 for (int i=0; i<N; i++) {
2487 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002488 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002489 }
2490 break;
2491 case OP_UPDATE:
2492 for (int i=0; i<N; i++) {
2493 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002494 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002495 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002496 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002497 }
2498 break;
2499 case OP_REMOVE:
2500 case OP_UNAVAILABLE:
2501 for (int i=0; i<N; i++) {
2502 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002504 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002505 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
2507 break;
2508 }
2509
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002510 ArrayList<AppInfo> added = null;
2511 ArrayList<AppInfo> modified = null;
2512 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002513
Adam Cohen487f7dd2012-06-28 18:12:10 -07002514 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002515 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002516 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002517 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002518 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002519 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002520 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002521 }
Winson Chung5d55f332012-07-16 20:45:03 -07002522 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002523 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002524 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002525 }
2526
Joe Onorato36115782010-06-17 13:28:48 -04002527 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2528 if (callbacks == null) {
2529 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2530 return;
2531 }
2532
2533 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002534 // Ensure that we add all the workspace applications to the db
2535 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002536 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2537 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2538 } else {
2539 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2540 addAndBindAddedApps(context, addedInfos, cb, added);
2541 }
Joe Onorato36115782010-06-17 13:28:48 -04002542 }
2543 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002544 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002545
2546 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002547 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002548 ArrayList<ItemInfo> infos =
2549 getItemInfoForComponentName(a.componentName);
2550 for (ItemInfo i : infos) {
2551 if (isShortcutInfoUpdateable(i)) {
2552 ShortcutInfo info = (ShortcutInfo) i;
2553 info.title = a.title.toString();
2554 updateItemInDatabase(context, info);
2555 }
2556 }
2557 }
2558
Joe Onorato36115782010-06-17 13:28:48 -04002559 mHandler.post(new Runnable() {
2560 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002561 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2562 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002563 callbacks.bindAppsUpdated(modifiedFinal);
2564 }
2565 }
2566 });
2567 }
Winson Chung83892cc2013-05-01 16:53:33 -07002568
Winson Chungdf95eb12013-10-16 14:57:07 -07002569 final ArrayList<String> removedPackageNames =
2570 new ArrayList<String>();
2571 if (mOp == OP_REMOVE) {
2572 // Mark all packages in the broadcast to be removed
2573 removedPackageNames.addAll(Arrays.asList(packages));
2574 } else if (mOp == OP_UPDATE) {
2575 // Mark disabled packages in the broadcast to be removed
2576 final PackageManager pm = context.getPackageManager();
2577 for (int i=0; i<N; i++) {
2578 if (isPackageDisabled(pm, packages[i])) {
2579 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002580 }
2581 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002582 }
2583 // Remove all the components associated with this package
2584 for (String pn : removedPackageNames) {
2585 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2586 for (ItemInfo i : infos) {
2587 deleteItemFromDatabase(context, i);
2588 }
2589 }
2590 // Remove all the specific components
2591 for (AppInfo a : removedApps) {
2592 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2593 for (ItemInfo i : infos) {
2594 deleteItemFromDatabase(context, i);
2595 }
2596 }
2597 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2598 // Remove any queued items from the install queue
2599 String spKey = LauncherAppState.getSharedPreferencesKey();
2600 SharedPreferences sp =
2601 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2602 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2603 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002604 mHandler.post(new Runnable() {
2605 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002606 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2607 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002608 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002609 }
2610 }
2611 });
Joe Onoratobe386092009-11-17 17:32:16 -08002612 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002613
Michael Jurkac402cd92013-05-20 15:49:32 +02002614 final ArrayList<Object> widgetsAndShortcuts =
2615 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002616 mHandler.post(new Runnable() {
2617 @Override
2618 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002619 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2620 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002621 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002622 }
2623 }
2624 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002625
2626 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002627 mHandler.post(new Runnable() {
2628 public void run() {
2629 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2630 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002631 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002632 }
2633 }
2634 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002635 }
2636 }
2637
Michael Jurkac402cd92013-05-20 15:49:32 +02002638 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2639 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2640 PackageManager packageManager = context.getPackageManager();
2641 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2642 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2643 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2644 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2645 Collections.sort(widgetsAndShortcuts,
2646 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2647 return widgetsAndShortcuts;
2648 }
2649
Winson Chungdf95eb12013-10-16 14:57:07 -07002650 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2651 try {
2652 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2653 return !pi.applicationInfo.enabled;
2654 } catch (NameNotFoundException e) {
2655 // Fall through
2656 }
2657 return false;
2658 }
Winson Chung1323b482013-08-05 12:41:55 -07002659 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002660 if (cn == null) {
2661 return false;
2662 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002663 if (isPackageDisabled(pm, cn.getPackageName())) {
2664 return false;
2665 }
Winson Chungee055712013-07-30 14:46:24 -07002666
2667 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002668 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002669 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002670 return (pm.getActivityInfo(cn, 0) != null);
2671 } catch (NameNotFoundException e) {
2672 return false;
2673 }
2674 }
2675
Joe Onorato9c1289c2009-08-17 11:03:03 -04002676 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002677 * This is called from the code that adds shortcuts from the intent receiver. This
2678 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002679 */
Joe Onorato56d82912010-03-07 14:32:10 -05002680 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002681 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683
Joe Onorato56d82912010-03-07 14:32:10 -05002684 /**
2685 * Make an ShortcutInfo object for a shortcut that is an application.
2686 *
2687 * If c is not null, then it will be used to fill in missing data like the title and icon.
2688 */
2689 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002690 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002691 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002692 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002693 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002694 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002695 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002696 } else {
2697 try {
2698 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2699 info.initFlagsAndFirstInstallTime(pi);
2700 } catch (NameNotFoundException e) {
2701 Log.d(TAG, "getPackInfo failed for package " +
2702 componentName.getPackageName());
2703 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002704 }
2705
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002706 // TODO: See if the PackageManager knows about this case. If it doesn't
2707 // then return null & delete this.
2708
Joe Onorato56d82912010-03-07 14:32:10 -05002709 // the resource -- This may implicitly give us back the fallback icon,
2710 // but don't worry about that. All we're doing with usingFallbackIcon is
2711 // to avoid saving lots of copies of that in the database, and most apps
2712 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002713
2714 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2715 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2716 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002717 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002718 ResolveInfo resolveInfo = null;
2719 ComponentName oldComponent = intent.getComponent();
2720 Intent newIntent = new Intent(intent.getAction(), null);
2721 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2722 newIntent.setPackage(oldComponent.getPackageName());
2723 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2724 for (ResolveInfo i : infos) {
2725 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2726 i.activityInfo.name);
2727 if (cn.equals(oldComponent)) {
2728 resolveInfo = i;
2729 }
2730 }
2731 if (resolveInfo == null) {
2732 resolveInfo = manager.resolveActivity(intent, 0);
2733 }
Joe Onorato56d82912010-03-07 14:32:10 -05002734 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002735 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002736 }
Joe Onorato56d82912010-03-07 14:32:10 -05002737 // the db
2738 if (icon == null) {
2739 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002740 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002741 }
2742 }
2743 // the fallback icon
2744 if (icon == null) {
2745 icon = getFallbackIcon();
2746 info.usingFallbackIcon = true;
2747 }
2748 info.setIcon(icon);
2749
2750 // from the resource
2751 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002752 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2753 if (labelCache != null && labelCache.containsKey(key)) {
2754 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002755 } else {
2756 info.title = resolveInfo.activityInfo.loadLabel(manager);
2757 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002758 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002759 }
2760 }
Joe Onorato56d82912010-03-07 14:32:10 -05002761 }
2762 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002763 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002764 if (c != null) {
2765 info.title = c.getString(titleIndex);
2766 }
2767 }
2768 // fall back to the class name of the activity
2769 if (info.title == null) {
2770 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002772 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2773 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002775
Winson Chung64359a52013-07-08 17:17:08 -07002776 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2777 ItemInfoFilter f) {
2778 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2779 for (ItemInfo i : infos) {
2780 if (i instanceof ShortcutInfo) {
2781 ShortcutInfo info = (ShortcutInfo) i;
2782 ComponentName cn = info.intent.getComponent();
2783 if (cn != null && f.filterItem(null, info, cn)) {
2784 filtered.add(info);
2785 }
2786 } else if (i instanceof FolderInfo) {
2787 FolderInfo info = (FolderInfo) i;
2788 for (ShortcutInfo s : info.contents) {
2789 ComponentName cn = s.intent.getComponent();
2790 if (cn != null && f.filterItem(info, s, cn)) {
2791 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002792 }
2793 }
Winson Chung64359a52013-07-08 17:17:08 -07002794 } else if (i instanceof LauncherAppWidgetInfo) {
2795 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2796 ComponentName cn = info.providerName;
2797 if (cn != null && f.filterItem(null, info, cn)) {
2798 filtered.add(info);
2799 }
Winson Chung8a435102012-08-30 17:16:53 -07002800 }
2801 }
Winson Chung64359a52013-07-08 17:17:08 -07002802 return new ArrayList<ItemInfo>(filtered);
2803 }
2804
2805 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002806 ItemInfoFilter filter = new ItemInfoFilter() {
2807 @Override
2808 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2809 return cn.getPackageName().equals(pn);
2810 }
2811 };
2812 return filterItemInfos(sBgItemsIdMap.values(), filter);
2813 }
2814
2815 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002816 ItemInfoFilter filter = new ItemInfoFilter() {
2817 @Override
2818 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2819 return cn.equals(cname);
2820 }
2821 };
2822 return filterItemInfos(sBgItemsIdMap.values(), filter);
2823 }
2824
2825 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2826 if (i instanceof ShortcutInfo) {
2827 ShortcutInfo info = (ShortcutInfo) i;
2828 // We need to check for ACTION_MAIN otherwise getComponent() might
2829 // return null for some shortcuts (for instance, for shortcuts to
2830 // web pages.)
2831 Intent intent = info.intent;
2832 ComponentName name = intent.getComponent();
2833 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2834 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2835 return true;
2836 }
2837 }
2838 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002839 }
2840
2841 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002842 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002844 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002845 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2846 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847
Joe Onorato56d82912010-03-07 14:32:10 -05002848 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002849 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002852 // TODO: If there's an explicit component and we can't install that, delete it.
2853
Joe Onorato56d82912010-03-07 14:32:10 -05002854 info.title = c.getString(titleIndex);
2855
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 int iconType = c.getInt(iconTypeIndex);
2857 switch (iconType) {
2858 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2859 String packageName = c.getString(iconPackageIndex);
2860 String resourceName = c.getString(iconResourceIndex);
2861 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002862 info.customIcon = false;
2863 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002866 if (resources != null) {
2867 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002868 icon = Utilities.createIconBitmap(
2869 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002870 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002872 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 }
Joe Onorato56d82912010-03-07 14:32:10 -05002874 // the db
2875 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002876 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002877 }
2878 // the fallback icon
2879 if (icon == null) {
2880 icon = getFallbackIcon();
2881 info.usingFallbackIcon = true;
2882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 break;
2884 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002885 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002886 if (icon == null) {
2887 icon = getFallbackIcon();
2888 info.customIcon = false;
2889 info.usingFallbackIcon = true;
2890 } else {
2891 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 break;
2894 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002895 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002896 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 info.customIcon = false;
2898 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002900 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 return info;
2902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903
Michael Jurka931dc972011-08-05 15:08:15 -07002904 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002905 @SuppressWarnings("all") // suppress dead code warning
2906 final boolean debug = false;
2907 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002908 Log.d(TAG, "getIconFromCursor app="
2909 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2910 }
2911 byte[] data = c.getBlob(iconIndex);
2912 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002913 return Utilities.createIconBitmap(
2914 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002915 } catch (Exception e) {
2916 return null;
2917 }
2918 }
2919
Winson Chung3d503fb2011-07-13 17:25:49 -07002920 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2921 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002922 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002923 if (info == null) {
2924 return null;
2925 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002926 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002927
2928 return info;
2929 }
2930
Winson Chunga9abd0e2010-10-27 17:18:37 -07002931 /**
Winson Chung55cef262010-10-28 14:14:18 -07002932 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2933 */
2934 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2935 ComponentName component) {
2936 List<AppWidgetProviderInfo> widgets =
2937 AppWidgetManager.getInstance(context).getInstalledProviders();
2938 for (AppWidgetProviderInfo info : widgets) {
2939 if (info.provider.equals(component)) {
2940 return info;
2941 }
2942 }
2943 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002944 }
2945
Winson Chung68846fd2010-10-29 11:00:27 -07002946 /**
2947 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2948 */
2949 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2950 final PackageManager packageManager = context.getPackageManager();
2951 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2952 new ArrayList<WidgetMimeTypeHandlerData>();
2953
2954 final Intent supportsIntent =
2955 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2956 supportsIntent.setType(mimeType);
2957
2958 // Create a set of widget configuration components that we can test against
2959 final List<AppWidgetProviderInfo> widgets =
2960 AppWidgetManager.getInstance(context).getInstalledProviders();
2961 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2962 new HashMap<ComponentName, AppWidgetProviderInfo>();
2963 for (AppWidgetProviderInfo info : widgets) {
2964 configurationComponentToWidget.put(info.configure, info);
2965 }
2966
2967 // Run through each of the intents that can handle this type of clip data, and cross
2968 // reference them with the components that are actual configuration components
2969 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2970 PackageManager.MATCH_DEFAULT_ONLY);
2971 for (ResolveInfo info : activities) {
2972 final ActivityInfo activityInfo = info.activityInfo;
2973 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2974 activityInfo.name);
2975 if (configurationComponentToWidget.containsKey(infoComponent)) {
2976 supportedConfigurationActivities.add(
2977 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2978 configurationComponentToWidget.get(infoComponent)));
2979 }
2980 }
2981 return supportedConfigurationActivities;
2982 }
2983
Winson Chunga9abd0e2010-10-27 17:18:37 -07002984 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002985 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2986 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2987 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2988
Adam Cohend9198822011-11-22 16:42:47 -08002989 if (intent == null) {
2990 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2991 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2992 return null;
2993 }
2994
Joe Onorato0589f0f2010-02-08 13:44:00 -08002995 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002996 boolean customIcon = false;
2997 ShortcutIconResource iconResource = null;
2998
2999 if (bitmap != null && bitmap instanceof Bitmap) {
3000 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003001 customIcon = true;
3002 } else {
3003 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3004 if (extra != null && extra instanceof ShortcutIconResource) {
3005 try {
3006 iconResource = (ShortcutIconResource) extra;
3007 final PackageManager packageManager = context.getPackageManager();
3008 Resources resources = packageManager.getResourcesForApplication(
3009 iconResource.packageName);
3010 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003011 icon = Utilities.createIconBitmap(
3012 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003013 } catch (Exception e) {
3014 Log.w(TAG, "Could not load shortcut icon: " + extra);
3015 }
3016 }
3017 }
3018
Michael Jurkac9d95c52011-08-29 14:03:34 -07003019 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003020
3021 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003022 if (fallbackIcon != null) {
3023 icon = fallbackIcon;
3024 } else {
3025 icon = getFallbackIcon();
3026 info.usingFallbackIcon = true;
3027 }
Joe Onorato56d82912010-03-07 14:32:10 -05003028 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003029 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003030
Joe Onorato0589f0f2010-02-08 13:44:00 -08003031 info.title = name;
3032 info.intent = intent;
3033 info.customIcon = customIcon;
3034 info.iconResource = iconResource;
3035
3036 return info;
3037 }
3038
Winson Chungaac01e12011-08-17 10:37:13 -07003039 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3040 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003041 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003042 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003043 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003044 }
Joe Onorato56d82912010-03-07 14:32:10 -05003045 // If this icon doesn't have a custom icon, check to see
3046 // what's stored in the DB, and if it doesn't match what
3047 // we're going to show, store what we are going to show back
3048 // into the DB. We do this so when we're loading, if the
3049 // package manager can't find an icon (for example because
3050 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003051 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003052 cache.put(info, c.getBlob(iconIndex));
3053 return true;
3054 }
3055 return false;
3056 }
3057 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3058 boolean needSave = false;
3059 try {
3060 if (data != null) {
3061 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3062 Bitmap loaded = info.getIcon(mIconCache);
3063 needSave = !saved.sameAs(loaded);
3064 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003065 needSave = true;
3066 }
Winson Chungaac01e12011-08-17 10:37:13 -07003067 } catch (Exception e) {
3068 needSave = true;
3069 }
3070 if (needSave) {
3071 Log.d(TAG, "going to save icon bitmap for info=" + info);
3072 // This is slower than is ideal, but this only happens once
3073 // or when the app is updated with a new icon.
3074 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003075 }
3076 }
3077
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003079 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 * or make a new one.
3081 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003082 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 // See if a placeholder was created for us already
3084 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003085 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003087 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 folders.put(id, folderInfo);
3089 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003090 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
3092
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003094 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003095 return new Comparator<AppInfo>() {
3096 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003097 int result = collator.compare(a.title.toString().trim(),
3098 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003099 if (result == 0) {
3100 result = a.componentName.compareTo(b.componentName);
3101 }
3102 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003103 }
Winson Chung11904872012-09-17 16:58:46 -07003104 };
3105 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003106 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3107 = new Comparator<AppInfo>() {
3108 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003109 if (a.firstInstallTime < b.firstInstallTime) return 1;
3110 if (a.firstInstallTime > b.firstInstallTime) return -1;
3111 return 0;
3112 }
3113 };
Winson Chung11904872012-09-17 16:58:46 -07003114 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3115 final Collator collator = Collator.getInstance();
3116 return new Comparator<AppWidgetProviderInfo>() {
3117 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003118 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003119 }
3120 };
3121 }
Winson Chung5308f242011-08-18 12:12:41 -07003122 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3123 if (info.activityInfo != null) {
3124 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3125 } else {
3126 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3127 }
3128 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003129 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003130 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003131 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003132 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003133 ShortcutNameComparator(PackageManager pm) {
3134 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003135 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003136 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003137 }
3138 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3139 mPackageManager = pm;
3140 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003141 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003142 }
3143 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003144 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003145 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3146 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3147 if (mLabelCache.containsKey(keyA)) {
3148 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003150 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003151
Winson Chung5308f242011-08-18 12:12:41 -07003152 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003153 }
Winson Chung5308f242011-08-18 12:12:41 -07003154 if (mLabelCache.containsKey(keyB)) {
3155 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003156 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003157 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003158
Winson Chung5308f242011-08-18 12:12:41 -07003159 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003160 }
Winson Chung11904872012-09-17 16:58:46 -07003161 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003162 }
3163 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003164 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003165 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003166 private PackageManager mPackageManager;
3167 private HashMap<Object, String> mLabelCache;
3168 WidgetAndShortcutNameComparator(PackageManager pm) {
3169 mPackageManager = pm;
3170 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003171 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003172 }
3173 public final int compare(Object a, Object b) {
3174 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 if (mLabelCache.containsKey(a)) {
3176 labelA = mLabelCache.get(a);
3177 } else {
3178 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003179 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003180 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003181 mLabelCache.put(a, labelA);
3182 }
3183 if (mLabelCache.containsKey(b)) {
3184 labelB = mLabelCache.get(b);
3185 } else {
3186 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003187 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003188 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003189 mLabelCache.put(b, labelB);
3190 }
Winson Chung11904872012-09-17 16:58:46 -07003191 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003192 }
3193 };
Joe Onoratobe386092009-11-17 17:32:16 -08003194
3195 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003196 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003197 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3198 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3199 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3200 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003201 if (mLoaderTask != null) {
3202 mLoaderTask.dumpState();
3203 } else {
3204 Log.d(TAG, "mLoaderTask=null");
3205 }
Joe Onoratobe386092009-11-17 17:32:16 -08003206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207}