blob: aad768d36b9f5ccc55718fbb42e78e762f9432ab [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070044import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040045import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080046
Michael Jurkac2f801e2011-07-12 14:19:46 -070047import java.lang.ref.WeakReference;
48import java.net.URISyntaxException;
49import java.text.Collator;
50import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070051import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070052import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070053import java.util.Collections;
54import java.util.Comparator;
55import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070056import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070057import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070058import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070059import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070060import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070061import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063/**
64 * Maintains in-memory state of the Launcher. It is expected that there should be only one
65 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070066 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040068public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080069 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070071
Daniel Sandler8707e0f2013-08-15 15:54:18 -070072 // true = use a "More Apps" folder for non-workspace apps on upgrade
73 // false = strew non-workspace apps across the workspace on upgrade
74 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
75
Joe Onorato36115782010-06-17 13:28:48 -040076 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070077 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040078
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080 private final Object mLock = new Object();
81 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040082 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070083 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020084 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Winson Chung81b52252012-08-27 15:34:29 -070086 // Specific runnable types that are run on the main thread deferred handler, this allows us to
87 // clear all queued binding runnables when the Launcher activity is destroyed.
88 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
89 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
90
91
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070092 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
93 static {
94 sWorkerThread.start();
95 }
96 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
97
Joe Onoratocc67f472010-06-08 10:54:30 -070098 // We start off with everything not loaded. After that, we assume that
99 // our monitoring of the package manager provides all updates and we never
100 // need to do a requery. These are only ever touched from the loader thread.
101 private boolean mWorkspaceLoaded;
102 private boolean mAllAppsLoaded;
103
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700104 // When we are loading pages synchronously, we can't just post the binding of items on the side
105 // pages as this delays the rotation process. Instead, we wait for a callback from the first
106 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
107 // a normal load, we also clear this set of Runnables.
108 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700112 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700113 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800114
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700115 // The lock that must be acquired before referencing any static bg data structures. Unlike
116 // other locks, this one can generally be held long-term because we never expect any of these
117 // static data structures to be referenced outside of the worker thread except on the first
118 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700119 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700120
Adam Cohen487f7dd2012-06-28 18:12:10 -0700121 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700122 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700123 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700124
Adam Cohen487f7dd2012-06-28 18:12:10 -0700125 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
126 // created by LauncherModel that are directly on the home screen (however, no widgets or
127 // shortcuts within folders).
128 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700129
Adam Cohen487f7dd2012-06-28 18:12:10 -0700130 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
131 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132 new ArrayList<LauncherAppWidgetInfo>();
133
Adam Cohen487f7dd2012-06-28 18:12:10 -0700134 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
135 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
138 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700139
140 // sBgWorkspaceScreens is the ordered set of workspace screens.
141 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
142
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 // </ only access in worker thread >
144
145 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800146 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Reena Lee99a73f32011-10-24 17:27:37 -0700148 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700149
Joe Onorato9c1289c2009-08-17 11:03:03 -0400150 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700151 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 public int getCurrentWorkspaceScreen();
153 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700154 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
155 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700156 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700157 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500158 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700159 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200161 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700162 public void bindAppsAdded(ArrayList<Long> newScreens,
163 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700164 ArrayList<ItemInfo> addAnimated,
165 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200166 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700167 public void bindComponentsRemoved(ArrayList<String> packageNames,
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200168 ArrayList<AppInfo> appInfos,
Winson Chung83892cc2013-05-01 16:53:33 -0700169 boolean matchPackageNamesOnly);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
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 Cohene25af792013-06-06 23:08:25 -0700500 // TODO: something breaks this in the upgrade path
501 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700502 }
503 }
504
Michael Jurka816474f2012-06-25 14:49:02 -0700505 static void checkItemInfo(final ItemInfo item) {
506 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
507 final long itemId = item.id;
508 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700509 public void run() {
510 synchronized (sBgLock) {
511 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700512 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700513 }
514 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700515 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700516 }
517
Michael Jurkac9d95c52011-08-29 14:03:34 -0700518 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
519 final ItemInfo item, final String callingFunction) {
520 final long itemId = item.id;
521 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
522 final ContentResolver cr = context.getContentResolver();
523
Adam Cohen487f7dd2012-06-28 18:12:10 -0700524 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700525 Runnable r = new Runnable() {
526 public void run() {
527 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700528 updateItemArrays(item, itemId, stackTrace);
529 }
530 };
531 runOnWorkerThread(r);
532 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700533
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700534 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
535 final ArrayList<ItemInfo> items, final String callingFunction) {
536 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700537
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700538 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
539 Runnable r = new Runnable() {
540 public void run() {
541 ArrayList<ContentProviderOperation> ops =
542 new ArrayList<ContentProviderOperation>();
543 int count = items.size();
544 for (int i = 0; i < count; i++) {
545 ItemInfo item = items.get(i);
546 final long itemId = item.id;
547 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
548 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700549
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700550 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
551 updateItemArrays(item, itemId, stackTrace);
552
553 }
554 try {
555 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
556 } catch (Exception e) {
557 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700558 }
559 }
560 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700561 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700562 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700563
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700564 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
565 // Lock on mBgLock *after* the db operation
566 synchronized (sBgLock) {
567 checkItemInfoLocked(itemId, item, stackTrace);
568
569 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
570 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
571 // Item is in a folder, make sure this folder exists
572 if (!sBgFolders.containsKey(item.container)) {
573 // An items container is being set to a that of an item which is not in
574 // the list of Folders.
575 String msg = "item: " + item + " container being set to: " +
576 item.container + ", not in the list of folders";
577 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700578 }
579 }
580
581 // Items are added/removed from the corresponding FolderInfo elsewhere, such
582 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
583 // that are on the desktop, as appropriate
584 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
585 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
586 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
587 switch (modelItem.itemType) {
588 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
589 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
590 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
591 if (!sBgWorkspaceItems.contains(modelItem)) {
592 sBgWorkspaceItems.add(modelItem);
593 }
594 break;
595 default:
596 break;
597 }
598 } else {
599 sBgWorkspaceItems.remove(modelItem);
600 }
601 }
602 }
603
Michael Jurkac7700af2013-05-14 20:17:58 +0200604 public void flushWorkerThread() {
605 mFlushingWorkerThread = true;
606 Runnable waiter = new Runnable() {
607 public void run() {
608 synchronized (this) {
609 notifyAll();
610 mFlushingWorkerThread = false;
611 }
612 }
613 };
614
615 synchronized(waiter) {
616 runOnWorkerThread(waiter);
617 if (mLoaderTask != null) {
618 synchronized(mLoaderTask) {
619 mLoaderTask.notify();
620 }
621 }
622 boolean success = false;
623 while (!success) {
624 try {
625 waiter.wait();
626 success = true;
627 } catch (InterruptedException e) {
628 }
629 }
630 }
631 }
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700635 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700636 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700637 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 item.cellX = cellX;
640 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700641
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 // We store hotseat items in canonical form which is this orientation invariant position
643 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650
651 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700653 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
654 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700655 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656
Michael Jurkac9d95c52011-08-29 14:03:34 -0700657 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700658 }
659
660 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700661 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
662 * cellX, cellY have already been updated on the ItemInfos.
663 */
664 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
665 final long container, final int screen) {
666
667 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
668 int count = items.size();
669
670 for (int i = 0; i < count; i++) {
671 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700672 item.container = container;
673
674 // We store hotseat items in canonical form which is this orientation invariant position
675 // in the hotseat
676 if (context instanceof Launcher && screen < 0 &&
677 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700679 item.cellY);
680 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 }
683
684 final ContentValues values = new ContentValues();
685 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
686 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
687 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700688 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700689
690 contentValues.add(values);
691 }
692 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
693 }
694
695 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700696 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800697 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700698 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700699 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700700 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800701 item.cellX = cellX;
702 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700703 item.spanX = spanX;
704 item.spanY = spanY;
705
706 // We store hotseat items in canonical form which is this orientation invariant position
707 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700711 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 }
Adam Cohend4844c32011-02-18 19:25:06 -0800714
Adam Cohend4844c32011-02-18 19:25:06 -0800715 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800716 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700717 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
718 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
719 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
720 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800722
Michael Jurka816474f2012-06-25 14:49:02 -0700723 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700724 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700725
726 /**
727 * Update an item to the database in a specified container.
728 */
729 static void updateItemInDatabase(Context context, final ItemInfo item) {
730 final ContentValues values = new ContentValues();
731 item.onAddToDatabase(values);
732 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
733 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800734 }
735
736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 * Returns true if the shortcuts already exists in the database.
738 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 static boolean shortcutExists(Context context, String title, Intent intent) {
741 final ContentResolver cr = context.getContentResolver();
742 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
743 new String[] { "title", "intent" }, "title=? and intent=?",
744 new String[] { title, intent.toUri(0) }, null);
745 boolean result = false;
746 try {
747 result = c.moveToFirst();
748 } finally {
749 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700752 }
753
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700755 * Returns an ItemInfo array containing all the items in the LauncherModel.
756 * The ItemInfo.id is not set through this function.
757 */
758 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
759 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
760 final ContentResolver cr = context.getContentResolver();
761 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
762 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
763 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
764 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
765
766 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
767 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
768 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
769 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
770 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
771 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
772 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
773
774 try {
775 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700776 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 item.cellX = c.getInt(cellXIndex);
778 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700779 item.spanX = Math.max(1, c.getInt(spanXIndex));
780 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700781 item.container = c.getInt(containerIndex);
782 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700783 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700784
785 items.add(item);
786 }
787 } catch (Exception e) {
788 items.clear();
789 } finally {
790 c.close();
791 }
792
793 return items;
794 }
795
796 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400797 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
798 */
799 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
800 final ContentResolver cr = context.getContentResolver();
801 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
802 "_id=? and (itemType=? or itemType=?)",
803 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700804 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700805
Joe Onorato9c1289c2009-08-17 11:03:03 -0400806 try {
807 if (c.moveToFirst()) {
808 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
809 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
810 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
811 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
812 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
813 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 FolderInfo folderInfo = null;
816 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700817 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
818 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700820 }
821
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 folderInfo.title = c.getString(titleIndex);
823 folderInfo.id = id;
824 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700825 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700826 folderInfo.cellX = c.getInt(cellXIndex);
827 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828
829 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700830 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 } finally {
832 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700833 }
834
835 return null;
836 }
837
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838 /**
839 * Add an item to the database in a specified container. Sets the container, screen, cellX and
840 * cellY fields of the item. Also assigns an ID to the item.
841 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700842 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700843 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 item.cellX = cellX;
846 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 // We store hotseat items in canonical form which is this orientation invariant position
848 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400855
856 final ContentValues values = new ContentValues();
857 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858 item.onAddToDatabase(values);
859
Michael Jurka414300a2013-08-27 15:42:35 +0200860 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700861 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700863
Michael Jurkac9d95c52011-08-29 14:03:34 -0700864 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700865 public void run() {
866 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
867 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700869 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700870 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700871 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 sBgItemsIdMap.put(item.id, item);
873 switch (item.itemType) {
874 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
875 sBgFolders.put(item.id, (FolderInfo) item);
876 // Fall through
877 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
878 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
879 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
880 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
881 sBgWorkspaceItems.add(item);
882 } else {
883 if (!sBgFolders.containsKey(item.container)) {
884 // Adding an item to a folder that doesn't exist.
885 String msg = "adding item: " + item + " to a folder that " +
886 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700887 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700890 break;
891 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
892 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
893 break;
894 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700895 }
896 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700897 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700898 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700899 }
900
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700902 * Creates a new unique child id, for a given cell span across all layouts.
903 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700904 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700906 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700907 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700908 }
909
Winson Chungaafa03c2010-06-11 17:34:16 -0700910 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700911 * Removes the specified item from the database
912 * @param context
913 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700915 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700917 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700918
Michael Jurka83df1882011-08-31 20:59:26 -0700919 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700920 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700921 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700922
923 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700924 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925 switch (item.itemType) {
926 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
927 sBgFolders.remove(item.id);
928 for (ItemInfo info: sBgItemsIdMap.values()) {
929 if (info.container == item.id) {
930 // We are deleting a folder which still contains items that
931 // think they are contained by that folder.
932 String msg = "deleting a folder (" + item + ") which still " +
933 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700934 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700935 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700936 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700937 sBgWorkspaceItems.remove(item);
938 break;
939 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
940 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
941 sBgWorkspaceItems.remove(item);
942 break;
943 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
944 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
945 break;
946 }
947 sBgItemsIdMap.remove(item.id);
948 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700949 }
950 }
Michael Jurka83df1882011-08-31 20:59:26 -0700951 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700952 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 }
954
955 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700956 * Update the order of the workspace screens in the database. The array list contains
957 * a list of screen ids in the order that they should appear.
958 */
Winson Chungc9168342013-06-26 14:54:55 -0700959 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700960 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 final ContentResolver cr = context.getContentResolver();
962 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
963
964 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700965 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700966 while (iter.hasNext()) {
967 long id = iter.next();
968 if (id < 0) {
969 iter.remove();
970 }
971 }
972
973 Runnable r = new Runnable() {
974 @Override
975 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700976 // Clear the table
977 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700978 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 ContentValues[] values = new ContentValues[count];
980 for (int i = 0; i < count; i++) {
981 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700982 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
984 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 values[i] = v;
986 }
987 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700988
Winson Chungba9c37f2013-08-30 14:11:37 -0700989 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700990 sBgWorkspaceScreens.clear();
991 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700992 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700993 }
994 };
995 runOnWorkerThread(r);
996 }
997
998 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 * Remove the contents of the specified folder from the database
1000 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001001 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 final ContentResolver cr = context.getContentResolver();
1003
Michael Jurkac9d95c52011-08-29 14:03:34 -07001004 Runnable r = new Runnable() {
1005 public void run() {
1006 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001008 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001009 sBgItemsIdMap.remove(info.id);
1010 sBgFolders.remove(info.id);
1011 sBgDbIconCache.remove(info);
1012 sBgWorkspaceItems.remove(info);
1013 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014
Michael Jurkac9d95c52011-08-29 14:03:34 -07001015 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1016 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001018 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001019 for (ItemInfo childInfo : info.contents) {
1020 sBgItemsIdMap.remove(childInfo.id);
1021 sBgDbIconCache.remove(childInfo);
1022 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001023 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 }
1025 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 }
1028
1029 /**
1030 * Set this as the current Launcher activity object for the loader.
1031 */
1032 public void initialize(Callbacks callbacks) {
1033 synchronized (mLock) {
1034 mCallbacks = new WeakReference<Callbacks>(callbacks);
1035 }
1036 }
1037
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001038 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1040 * ACTION_PACKAGE_CHANGED.
1041 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001042 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001043 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001044 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001047
Joe Onorato36115782010-06-17 13:28:48 -04001048 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1049 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1050 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1051 final String packageName = intent.getData().getSchemeSpecificPart();
1052 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053
Joe Onorato36115782010-06-17 13:28:48 -04001054 int op = PackageUpdatedTask.OP_NONE;
1055
1056 if (packageName == null || packageName.length() == 0) {
1057 // they sent us a bad intent
1058 return;
1059 }
1060
1061 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1062 op = PackageUpdatedTask.OP_UPDATE;
1063 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1064 if (!replacing) {
1065 op = PackageUpdatedTask.OP_REMOVE;
1066 }
1067 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1068 // later, we will update the package at this time
1069 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1070 if (!replacing) {
1071 op = PackageUpdatedTask.OP_ADD;
1072 } else {
1073 op = PackageUpdatedTask.OP_UPDATE;
1074 }
1075 }
1076
1077 if (op != PackageUpdatedTask.OP_NONE) {
1078 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1079 }
1080
1081 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001082 // First, schedule to add these apps back in.
1083 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1084 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1085 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001086 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001087 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1088 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1089 enqueuePackageUpdated(new PackageUpdatedTask(
1090 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001091 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001092 // If we have changed locale we need to clear out the labels in all apps/workspace.
1093 forceReload();
1094 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1095 // Check if configuration change was an mcc/mnc change which would affect app resources
1096 // and we would need to clear out the labels in all apps/workspace. Same handling as
1097 // above for ACTION_LOCALE_CHANGED
1098 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001099 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001100 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001101 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001102 forceReload();
1103 }
1104 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001105 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001106 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1107 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001108 if (mCallbacks != null) {
1109 Callbacks callbacks = mCallbacks.get();
1110 if (callbacks != null) {
1111 callbacks.bindSearchablesChanged();
1112 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001113 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001114 }
1115 }
1116
Reena Lee93f824a2011-09-23 17:20:28 -07001117 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001118 resetLoadedState(true, true);
1119
Reena Lee93f824a2011-09-23 17:20:28 -07001120 // Do this here because if the launcher activity is running it will be restarted.
1121 // If it's not running startLoaderFromBackground will merely tell it that it needs
1122 // to reload.
1123 startLoaderFromBackground();
1124 }
1125
Winson Chungf0c6ae02012-03-21 16:10:31 -07001126 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1127 synchronized (mLock) {
1128 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1129 // mWorkspaceLoaded to true later
1130 stopLoaderLocked();
1131 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1132 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1133 }
1134 }
1135
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001136 /**
1137 * When the launcher is in the background, it's possible for it to miss paired
1138 * configuration changes. So whenever we trigger the loader from the background
1139 * tell the launcher that it needs to re-run the loader when it comes back instead
1140 * of doing it now.
1141 */
1142 public void startLoaderFromBackground() {
1143 boolean runLoader = false;
1144 if (mCallbacks != null) {
1145 Callbacks callbacks = mCallbacks.get();
1146 if (callbacks != null) {
1147 // Only actually run the loader if they're not paused.
1148 if (!callbacks.setLoadOnResume()) {
1149 runLoader = true;
1150 }
1151 }
1152 }
1153 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001154 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001155 }
Joe Onorato36115782010-06-17 13:28:48 -04001156 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001157
Reena Lee93f824a2011-09-23 17:20:28 -07001158 // If there is already a loader task running, tell it to stop.
1159 // returns true if isLaunching() was true on the old task
1160 private boolean stopLoaderLocked() {
1161 boolean isLaunching = false;
1162 LoaderTask oldTask = mLoaderTask;
1163 if (oldTask != null) {
1164 if (oldTask.isLaunching()) {
1165 isLaunching = true;
1166 }
1167 oldTask.stopLocked();
1168 }
1169 return isLaunching;
1170 }
1171
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001172 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001173 synchronized (mLock) {
1174 if (DEBUG_LOADERS) {
1175 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1176 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 // Clear any deferred bind-runnables from the synchronized load process
1179 // We must do this before any loading/binding is scheduled below.
1180 mDeferredBindRunnables.clear();
1181
Joe Onorato36115782010-06-17 13:28:48 -04001182 // Don't bother to start the thread if we know it's not going to do anything
1183 if (mCallbacks != null && mCallbacks.get() != null) {
1184 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001185 // also, don't downgrade isLaunching if we're already running
1186 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001187 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001188 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1189 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1190 } else {
1191 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1192 sWorker.post(mLoaderTask);
1193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 }
1196 }
1197
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001198 void bindRemainingSynchronousPages() {
1199 // Post the remaining side pages to be loaded
1200 if (!mDeferredBindRunnables.isEmpty()) {
1201 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001202 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 }
1204 mDeferredBindRunnables.clear();
1205 }
1206 }
1207
Joe Onorato36115782010-06-17 13:28:48 -04001208 public void stopLoader() {
1209 synchronized (mLock) {
1210 if (mLoaderTask != null) {
1211 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
1213 }
Joe Onorato36115782010-06-17 13:28:48 -04001214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215
Winson Chung76828c82013-08-19 15:43:29 -07001216 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1217 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1218 final ContentResolver contentResolver = context.getContentResolver();
1219 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1220 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1221 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1222
1223 try {
1224 final int idIndex = sc.getColumnIndexOrThrow(
1225 LauncherSettings.WorkspaceScreens._ID);
1226 final int rankIndex = sc.getColumnIndexOrThrow(
1227 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1228 while (sc.moveToNext()) {
1229 try {
1230 long screenId = sc.getLong(idIndex);
1231 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001232 orderedScreens.put(rank, screenId);
1233 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001234 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001235 }
1236 }
1237 } finally {
1238 sc.close();
1239 }
1240 return orderedScreens;
1241 }
1242
Michael Jurkac57b7a82011-08-09 22:02:20 -07001243 public boolean isAllAppsLoaded() {
1244 return mAllAppsLoaded;
1245 }
1246
Winson Chung36a62fe2012-05-06 18:04:42 -07001247 boolean isLoadingWorkspace() {
1248 synchronized (mLock) {
1249 if (mLoaderTask != null) {
1250 return mLoaderTask.isLoadingWorkspace();
1251 }
1252 }
1253 return false;
1254 }
1255
Joe Onorato36115782010-06-17 13:28:48 -04001256 /**
1257 * Runnable for the thread that loads the contents of the launcher:
1258 * - workspace icons
1259 * - widgets
1260 * - all apps icons
1261 */
1262 private class LoaderTask implements Runnable {
1263 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001264 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001265 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001266 private boolean mStopped;
1267 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001268
Winson Chungc3eecff2011-07-11 17:44:15 -07001269 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001270
1271 LoaderTask(Context context, boolean isLaunching) {
1272 mContext = context;
1273 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001274 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001275 }
1276
Joe Onorato36115782010-06-17 13:28:48 -04001277 boolean isLaunching() {
1278 return mIsLaunching;
1279 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001280
Winson Chung36a62fe2012-05-06 18:04:42 -07001281 boolean isLoadingWorkspace() {
1282 return mIsLoadingAndBindingWorkspace;
1283 }
1284
Winson Chungc763c4e2013-07-19 13:49:06 -07001285 /** Returns whether this is an upgrade path */
1286 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001287 mIsLoadingAndBindingWorkspace = true;
1288
Joe Onorato36115782010-06-17 13:28:48 -04001289 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001290 if (DEBUG_LOADERS) {
1291 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001293
Winson Chungc763c4e2013-07-19 13:49:06 -07001294 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001295 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001296 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001297 synchronized (LoaderTask.this) {
1298 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001299 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001300 }
1301 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001302 }
1303 }
1304
Joe Onorato36115782010-06-17 13:28:48 -04001305 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001306 bindWorkspace(-1, isUpgradePath);
1307 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001308 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001309
Joe Onorato36115782010-06-17 13:28:48 -04001310 private void waitForIdle() {
1311 // Wait until the either we're stopped or the other threads are done.
1312 // This way we don't start loading all apps until the workspace has settled
1313 // down.
1314 synchronized (LoaderTask.this) {
1315 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001316
Joe Onorato36115782010-06-17 13:28:48 -04001317 mHandler.postIdle(new Runnable() {
1318 public void run() {
1319 synchronized (LoaderTask.this) {
1320 mLoadAndBindStepFinished = true;
1321 if (DEBUG_LOADERS) {
1322 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001323 }
Joe Onorato36115782010-06-17 13:28:48 -04001324 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001325 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001326 }
Joe Onorato36115782010-06-17 13:28:48 -04001327 });
1328
Michael Jurkac7700af2013-05-14 20:17:58 +02001329 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001330 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001331 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1332 // wait no longer than 1sec at a time
1333 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001334 } catch (InterruptedException ex) {
1335 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001336 }
1337 }
Joe Onorato36115782010-06-17 13:28:48 -04001338 if (DEBUG_LOADERS) {
1339 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001340 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001341 + "ms for previous step to finish binding");
1342 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001343 }
Joe Onorato36115782010-06-17 13:28:48 -04001344 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001345
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001346 void runBindSynchronousPage(int synchronousBindPage) {
1347 if (synchronousBindPage < 0) {
1348 // Ensure that we have a valid page index to load synchronously
1349 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1350 "valid page index");
1351 }
1352 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1353 // Ensure that we don't try and bind a specified page when the pages have not been
1354 // loaded already (we should load everything asynchronously in that case)
1355 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1356 }
1357 synchronized (mLock) {
1358 if (mIsLoaderTaskRunning) {
1359 // Ensure that we are never running the background loading at this point since
1360 // we also touch the background collections
1361 throw new RuntimeException("Error! Background loading is already running");
1362 }
1363 }
1364
1365 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1366 // data structures, we can't allow any other thread to touch that data, but because
1367 // this call is synchronous, we can get away with not locking).
1368
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001369 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001370 // operations from the previous activity. We need to ensure that all queued operations
1371 // are executed before any synchronous binding work is done.
1372 mHandler.flush();
1373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 // Divide the set of loaded items into those that we are binding synchronously, and
1375 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001376 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001377 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1378 // arise from that.
1379 onlyBindAllApps();
1380 }
1381
Joe Onorato36115782010-06-17 13:28:48 -04001382 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001383 boolean isUpgrade = false;
1384
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001385 synchronized (mLock) {
1386 mIsLoaderTaskRunning = true;
1387 }
Joe Onorato36115782010-06-17 13:28:48 -04001388 // Optimize for end-user experience: if the Launcher is up and // running with the
1389 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1390 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001391 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001392 // Elevate priority when Home launches for the first time to avoid
1393 // starving at boot time. Staring at a blank home is not cool.
1394 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001395 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1396 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001397 android.os.Process.setThreadPriority(mIsLaunching
1398 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1399 }
Winson Chung64359a52013-07-08 17:17:08 -07001400 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001401 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001402
Joe Onorato36115782010-06-17 13:28:48 -04001403 if (mStopped) {
1404 break keep_running;
1405 }
1406
1407 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1408 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001409 synchronized (mLock) {
1410 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001411 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1413 }
1414 }
Joe Onorato36115782010-06-17 13:28:48 -04001415 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001416
1417 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001418 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1419 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001420
1421 // Restore the default thread priority after we are done loading items
1422 synchronized (mLock) {
1423 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1424 }
Joe Onorato36115782010-06-17 13:28:48 -04001425 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426
Winson Chungaac01e12011-08-17 10:37:13 -07001427 // Update the saved icons if necessary
1428 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001429 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001430 for (Object key : sBgDbIconCache.keySet()) {
1431 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1432 }
1433 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001434 }
Winson Chungaac01e12011-08-17 10:37:13 -07001435
Winson Chungc58497e2013-09-03 17:48:37 -07001436 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1437 // Ensure that all the applications that are in the system are
1438 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001439 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001440 verifyApplications();
1441 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001442 }
1443
Joe Onorato36115782010-06-17 13:28:48 -04001444 // Clear out this reference, otherwise we end up holding it until all of the
1445 // callback runnables are done.
1446 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447
Joe Onorato36115782010-06-17 13:28:48 -04001448 synchronized (mLock) {
1449 // If we are still the last one to be scheduled, remove ourselves.
1450 if (mLoaderTask == this) {
1451 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001453 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001454 }
Joe Onorato36115782010-06-17 13:28:48 -04001455 }
1456
1457 public void stopLocked() {
1458 synchronized (LoaderTask.this) {
1459 mStopped = true;
1460 this.notify();
1461 }
1462 }
1463
1464 /**
1465 * Gets the callbacks object. If we've been stopped, or if the launcher object
1466 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1467 * object that was around when the deferred message was scheduled, and if there's
1468 * a new Callbacks object around then also return null. This will save us from
1469 * calling onto it with data that will be ignored.
1470 */
1471 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1472 synchronized (mLock) {
1473 if (mStopped) {
1474 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001475 }
Joe Onorato36115782010-06-17 13:28:48 -04001476
1477 if (mCallbacks == null) {
1478 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001479 }
Joe Onorato36115782010-06-17 13:28:48 -04001480
1481 final Callbacks callbacks = mCallbacks.get();
1482 if (callbacks != oldCallbacks) {
1483 return null;
1484 }
1485 if (callbacks == null) {
1486 Log.w(TAG, "no mCallbacks");
1487 return null;
1488 }
1489
1490 return callbacks;
1491 }
1492 }
1493
Winson Chungc763c4e2013-07-19 13:49:06 -07001494 private void verifyApplications() {
1495 final Context context = mApp.getContext();
1496
1497 // Cross reference all the applications in our apps list with items in the workspace
1498 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001499 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001500 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001501 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001502 tmpInfos = getItemInfoForComponentName(app.componentName);
1503 if (tmpInfos.isEmpty()) {
1504 // We are missing an application icon, so add this to the workspace
1505 added.add(app);
1506 // This is a rare event, so lets log it
1507 Log.e(TAG, "Missing Application on load: " + app);
1508 }
1509 }
1510 }
1511 if (!added.isEmpty()) {
1512 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001513 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001514 }
1515 }
1516
Winson Chung5f8afe62013-08-12 16:19:28 -07001517 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001518 LauncherAppState app = LauncherAppState.getInstance();
1519 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1520 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1521 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001522 }
1523
Joe Onorato36115782010-06-17 13:28:48 -04001524 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001525 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1526 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001527 LauncherAppState app = LauncherAppState.getInstance();
1528 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1529 int countX = (int) grid.numColumns;
1530 int countY = (int) grid.numRows;
1531
Adam Cohendcd297f2013-06-18 13:13:40 -07001532 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001533 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001534 // Return early if we detect that an item is under the hotseat button
1535 if (mCallbacks == null ||
1536 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1537 deleteOnItemOverlap.set(true);
1538 return false;
1539 }
1540
Adam Cohendcd297f2013-06-18 13:13:40 -07001541 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1542 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1543 [(int) item.screenId][0] != null) {
1544 Log.e(TAG, "Error loading shortcut into hotseat " + item
1545 + " into position (" + item.screenId + ":" + item.cellX + ","
1546 + item.cellY + ") occupied by "
1547 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1548 [(int) item.screenId][0]);
1549 return false;
1550 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001551 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001552 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 items[(int) item.screenId][0] = item;
1554 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001555 return true;
1556 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001557 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1558 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001559 return true;
1560 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001561
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001563 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 occupied.put(item.screenId, items);
1565 }
1566
1567 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001568 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001569 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1570 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001572 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001573 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001574 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001575 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001577 return false;
1578 }
1579 }
1580 }
1581 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 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001584 }
1585 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001586
Joe Onorato36115782010-06-17 13:28:48 -04001587 return true;
1588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589
Winson Chungba9c37f2013-08-30 14:11:37 -07001590 /** Clears all the sBg data structures */
1591 private void clearSBgDataStructures() {
1592 synchronized (sBgLock) {
1593 sBgWorkspaceItems.clear();
1594 sBgAppWidgets.clear();
1595 sBgFolders.clear();
1596 sBgItemsIdMap.clear();
1597 sBgDbIconCache.clear();
1598 sBgWorkspaceScreens.clear();
1599 }
1600 }
1601
Winson Chungc763c4e2013-07-19 13:49:06 -07001602 /** Returns whether this is an upgradge path */
1603 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001604 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605
Joe Onorato36115782010-06-17 13:28:48 -04001606 final Context context = mContext;
1607 final ContentResolver contentResolver = context.getContentResolver();
1608 final PackageManager manager = context.getPackageManager();
1609 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1610 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001611
Winson Chung892c74d2013-08-22 16:15:50 -07001612 LauncherAppState app = LauncherAppState.getInstance();
1613 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1614 int countX = (int) grid.numColumns;
1615 int countY = (int) grid.numRows;
1616
Michael Jurkab85f8a42012-04-25 15:48:32 -07001617 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001618 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001619
Winson Chungc763c4e2013-07-19 13:49:06 -07001620 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001621 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001622
Winson Chung2abf94d2012-07-18 18:16:38 -07001623 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001624 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001625
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001626 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001627 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001628 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001629 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001630
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001631 // +1 for the hotseat (it can be larger than the workspace)
1632 // Load workspace in reverse order to ensure that latest items are loaded first (and
1633 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001634 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001636 try {
1637 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1638 final int intentIndex = c.getColumnIndexOrThrow
1639 (LauncherSettings.Favorites.INTENT);
1640 final int titleIndex = c.getColumnIndexOrThrow
1641 (LauncherSettings.Favorites.TITLE);
1642 final int iconTypeIndex = c.getColumnIndexOrThrow(
1643 LauncherSettings.Favorites.ICON_TYPE);
1644 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1645 final int iconPackageIndex = c.getColumnIndexOrThrow(
1646 LauncherSettings.Favorites.ICON_PACKAGE);
1647 final int iconResourceIndex = c.getColumnIndexOrThrow(
1648 LauncherSettings.Favorites.ICON_RESOURCE);
1649 final int containerIndex = c.getColumnIndexOrThrow(
1650 LauncherSettings.Favorites.CONTAINER);
1651 final int itemTypeIndex = c.getColumnIndexOrThrow(
1652 LauncherSettings.Favorites.ITEM_TYPE);
1653 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1654 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001655 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1656 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 final int screenIndex = c.getColumnIndexOrThrow(
1658 LauncherSettings.Favorites.SCREEN);
1659 final int cellXIndex = c.getColumnIndexOrThrow
1660 (LauncherSettings.Favorites.CELLX);
1661 final int cellYIndex = c.getColumnIndexOrThrow
1662 (LauncherSettings.Favorites.CELLY);
1663 final int spanXIndex = c.getColumnIndexOrThrow
1664 (LauncherSettings.Favorites.SPANX);
1665 final int spanYIndex = c.getColumnIndexOrThrow(
1666 LauncherSettings.Favorites.SPANY);
1667 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1668 //final int displayModeIndex = c.getColumnIndexOrThrow(
1669 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001670
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001671 ShortcutInfo info;
1672 String intentDescription;
1673 LauncherAppWidgetInfo appWidgetInfo;
1674 int container;
1675 long id;
1676 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001679 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 try {
1681 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001682
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001683 switch (itemType) {
1684 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1685 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001686 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001687 intentDescription = c.getString(intentIndex);
1688 try {
1689 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001690 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001691 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001692 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001693 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001694 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1695 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001696 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001697 // If apps can be on external storage, then we just
1698 // leave them for the user to remove (maybe add
1699 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001700 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001701 }
1702 continue;
1703 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001705 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 continue;
1707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1710 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1711 titleIndex, mLabelCache);
1712 } else {
1713 info = getShortcutInfo(c, context, iconTypeIndex,
1714 iconPackageIndex, iconResourceIndex, iconIndex,
1715 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001716
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001717 // App shortcuts that used to be automatically added to Launcher
1718 // didn't always have the correct intent flags set, so do that
1719 // here
1720 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001721 intent.getCategories() != null &&
1722 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001723 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001724 intent.addFlags(
1725 Intent.FLAG_ACTIVITY_NEW_TASK |
1726 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1727 }
Michael Jurka96879562012-03-22 05:54:33 -07001728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001731 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 container = c.getInt(containerIndex);
1734 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001735 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001736 info.cellX = c.getInt(cellXIndex);
1737 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001738 info.spanX = 1;
1739 info.spanY = 1;
1740 // Skip loading items that are out of bounds
1741 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1742 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001743 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1744 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001745 continue;
1746 }
1747 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001749 deleteOnItemOverlap.set(false);
1750 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1751 if (deleteOnItemOverlap.get()) {
1752 itemsToRemove.add(id);
1753 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 break;
1755 }
1756
1757 switch (container) {
1758 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1759 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1760 sBgWorkspaceItems.add(info);
1761 break;
1762 default:
1763 // Item is in a user folder
1764 FolderInfo folderInfo =
1765 findOrMakeFolder(sBgFolders, container);
1766 folderInfo.add(info);
1767 break;
1768 }
1769 sBgItemsIdMap.put(info.id, info);
1770
1771 // now that we've loaded everthing re-save it with the
1772 // icon in case it disappears somehow.
1773 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001774 } else {
1775 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 }
1777 break;
1778
1779 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1780 id = c.getLong(idIndex);
1781 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1782
1783 folderInfo.title = c.getString(titleIndex);
1784 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001786 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001787 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001788 folderInfo.cellX = c.getInt(cellXIndex);
1789 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001790 folderInfo.spanX = 1;
1791 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792
Winson Chung5f8afe62013-08-12 16:19:28 -07001793 // Skip loading items that are out of bounds
1794 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001795 if (checkItemDimensions(folderInfo)) {
1796 Log.d(TAG, "Skipped loading out of bounds folder");
1797 continue;
1798 }
1799 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001800 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001801 deleteOnItemOverlap.set(false);
1802 if (!checkItemPlacement(occupied, folderInfo,
1803 deleteOnItemOverlap)) {
1804 if (deleteOnItemOverlap.get()) {
1805 itemsToRemove.add(id);
1806 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001807 break;
1808 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001809
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1812 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1813 sBgWorkspaceItems.add(folderInfo);
1814 break;
Joe Onorato36115782010-06-17 13:28:48 -04001815 }
Joe Onorato17a89222011-02-08 17:26:11 -08001816
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1818 sBgFolders.put(folderInfo.id, folderInfo);
1819 break;
1820
1821 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1822 // Read all Launcher-specific widget details
1823 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001824 String savedProvider = c.getString(appWidgetProviderIndex);
1825
Joe Onorato36115782010-06-17 13:28:48 -04001826 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001827
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 final AppWidgetProviderInfo provider =
1829 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001830
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001831 if (!isSafeMode && (provider == null || provider.provider == null ||
1832 provider.provider.getPackageName() == null)) {
1833 String log = "Deleting widget that isn't installed anymore: id="
1834 + id + " appWidgetId=" + appWidgetId;
1835 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001836 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001837 itemsToRemove.add(id);
1838 } else {
1839 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1840 provider.provider);
1841 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001842 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001843 appWidgetInfo.cellX = c.getInt(cellXIndex);
1844 appWidgetInfo.cellY = c.getInt(cellYIndex);
1845 appWidgetInfo.spanX = c.getInt(spanXIndex);
1846 appWidgetInfo.spanY = c.getInt(spanYIndex);
1847 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1848 appWidgetInfo.minSpanX = minSpan[0];
1849 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001850
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 container = c.getInt(containerIndex);
1852 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1853 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1854 Log.e(TAG, "Widget found where container != " +
1855 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1856 continue;
1857 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001858
Adam Cohene25af792013-06-06 23:08:25 -07001859 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001860 // Skip loading items that are out of bounds
1861 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1862 if (checkItemDimensions(appWidgetInfo)) {
1863 Log.d(TAG, "Skipped loading out of bounds app widget");
1864 continue;
1865 }
1866 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001867 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001868 deleteOnItemOverlap.set(false);
1869 if (!checkItemPlacement(occupied, appWidgetInfo,
1870 deleteOnItemOverlap)) {
1871 if (deleteOnItemOverlap.get()) {
1872 itemsToRemove.add(id);
1873 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 break;
1875 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001876 String providerName = provider.provider.flattenToString();
1877 if (!providerName.equals(savedProvider)) {
1878 ContentValues values = new ContentValues();
1879 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1880 providerName);
1881 String where = BaseColumns._ID + "= ?";
1882 String[] args = {Integer.toString(c.getInt(idIndex))};
1883 contentResolver.update(contentUri, values, where, args);
1884 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1886 sBgAppWidgets.add(appWidgetInfo);
1887 }
Joe Onorato36115782010-06-17 13:28:48 -04001888 break;
1889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001891 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001892 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001893 }
1894 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001895 if (c != null) {
1896 c.close();
1897 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 }
1899
Winson Chungba9c37f2013-08-30 14:11:37 -07001900 // Break early if we've stopped loading
1901 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001902 clearSBgDataStructures();
1903 return false;
1904 }
1905
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 if (itemsToRemove.size() > 0) {
1907 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001908 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001909 // Remove dead items
1910 for (long id : itemsToRemove) {
1911 if (DEBUG_LOADERS) {
1912 Log.d(TAG, "Removed id = " + id);
1913 }
1914 // Don't notify content observers
1915 try {
1916 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1917 null, null);
1918 } catch (RemoteException e) {
1919 Log.w(TAG, "Could not remove id = " + id);
1920 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001921 }
1922 }
1923
Winson Chungc763c4e2013-07-19 13:49:06 -07001924 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 long maxScreenId = 0;
1926 // If we're importing we use the old screen order.
1927 for (ItemInfo item: sBgItemsIdMap.values()) {
1928 long screenId = item.screenId;
1929 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1930 !sBgWorkspaceScreens.contains(screenId)) {
1931 sBgWorkspaceScreens.add(screenId);
1932 if (screenId > maxScreenId) {
1933 maxScreenId = screenId;
1934 }
1935 }
1936 }
1937 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001938
Michael Jurka414300a2013-08-27 15:42:35 +02001939 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001940 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001941
1942 // Update the max item id after we load an old db
1943 long maxItemId = 0;
1944 // If we're importing we use the old screen order.
1945 for (ItemInfo item: sBgItemsIdMap.values()) {
1946 maxItemId = Math.max(maxItemId, item.id);
1947 }
Michael Jurka414300a2013-08-27 15:42:35 +02001948 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001949 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001950 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1951 for (Integer i : orderedScreens.keySet()) {
1952 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 }
1954
1955 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001956 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 for (ItemInfo item: sBgItemsIdMap.values()) {
1958 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1960 unusedScreens.contains(screenId)) {
1961 unusedScreens.remove(screenId);
1962 }
1963 }
1964
1965 // If there are any empty screens remove them, and update.
1966 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001967 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1969 }
1970 }
1971
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 if (DEBUG_LOADERS) {
1973 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1974 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001975 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001976 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001977 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001978
Daniel Sandler566da102013-06-25 23:43:45 -04001979 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001980 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001982 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001983 line += " | ";
1984 }
Winson Chung892c74d2013-08-22 16:15:50 -07001985 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001987 }
Joe Onorato36115782010-06-17 13:28:48 -04001988 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001989 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001991 }
Joe Onorato36115782010-06-17 13:28:48 -04001992 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001993 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001994 }
1995
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001996 /** Filters the set of items who are directly or indirectly (via another container) on the
1997 * specified screen. */
1998 private void filterCurrentWorkspaceItems(int currentScreen,
1999 ArrayList<ItemInfo> allWorkspaceItems,
2000 ArrayList<ItemInfo> currentScreenItems,
2001 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002002 // Purge any null ItemInfos
2003 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2004 while (iter.hasNext()) {
2005 ItemInfo i = iter.next();
2006 if (i == null) {
2007 iter.remove();
2008 }
2009 }
2010
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 // If we aren't filtering on a screen, then the set of items to load is the full set of
2012 // items given.
2013 if (currentScreen < 0) {
2014 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002015 }
2016
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 // Order the set of items by their containers first, this allows use to walk through the
2018 // list sequentially, build up a list of containers that are in the specified screen,
2019 // as well as all items in those containers.
2020 Set<Long> itemsOnScreen = new HashSet<Long>();
2021 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2022 @Override
2023 public int compare(ItemInfo lhs, ItemInfo rhs) {
2024 return (int) (lhs.container - rhs.container);
2025 }
2026 });
2027 for (ItemInfo info : allWorkspaceItems) {
2028 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 currentScreenItems.add(info);
2031 itemsOnScreen.add(info.id);
2032 } else {
2033 otherScreenItems.add(info);
2034 }
2035 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2036 currentScreenItems.add(info);
2037 itemsOnScreen.add(info.id);
2038 } else {
2039 if (itemsOnScreen.contains(info.container)) {
2040 currentScreenItems.add(info);
2041 itemsOnScreen.add(info.id);
2042 } else {
2043 otherScreenItems.add(info);
2044 }
2045 }
2046 }
2047 }
2048
2049 /** Filters the set of widgets which are on the specified screen. */
2050 private void filterCurrentAppWidgets(int currentScreen,
2051 ArrayList<LauncherAppWidgetInfo> appWidgets,
2052 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2053 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2054 // If we aren't filtering on a screen, then the set of items to load is the full set of
2055 // widgets given.
2056 if (currentScreen < 0) {
2057 currentScreenWidgets.addAll(appWidgets);
2058 }
2059
2060 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002061 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002062 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002064 currentScreenWidgets.add(widget);
2065 } else {
2066 otherScreenWidgets.add(widget);
2067 }
2068 }
2069 }
2070
2071 /** Filters the set of folders which are on the specified screen. */
2072 private void filterCurrentFolders(int currentScreen,
2073 HashMap<Long, ItemInfo> itemsIdMap,
2074 HashMap<Long, FolderInfo> folders,
2075 HashMap<Long, FolderInfo> currentScreenFolders,
2076 HashMap<Long, FolderInfo> otherScreenFolders) {
2077 // If we aren't filtering on a screen, then the set of items to load is the full set of
2078 // widgets given.
2079 if (currentScreen < 0) {
2080 currentScreenFolders.putAll(folders);
2081 }
2082
2083 for (long id : folders.keySet()) {
2084 ItemInfo info = itemsIdMap.get(id);
2085 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002086 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002088 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002089 currentScreenFolders.put(id, folder);
2090 } else {
2091 otherScreenFolders.put(id, folder);
2092 }
2093 }
2094 }
2095
2096 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2097 * right) */
2098 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002099 final LauncherAppState app = LauncherAppState.getInstance();
2100 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 // XXX: review this
2102 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002103 @Override
2104 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002105 int cellCountX = (int) grid.numColumns;
2106 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002107 int screenOffset = cellCountX * cellCountY;
2108 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002110 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002111 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002112 rhs.cellY * cellCountX + rhs.cellX);
2113 return (int) (lr - rr);
2114 }
2115 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002116 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002117
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2119 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 final Runnable r = new Runnable() {
2121 @Override
2122 public void run() {
2123 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2124 if (callbacks != null) {
2125 callbacks.bindScreens(orderedScreens);
2126 }
2127 }
2128 };
2129 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2130 }
2131
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2133 final ArrayList<ItemInfo> workspaceItems,
2134 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2135 final HashMap<Long, FolderInfo> folders,
2136 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002137
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 final boolean postOnMainThread = (deferredBindRunnables != null);
2139
2140 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002141 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002143 final int start = i;
2144 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002145 final Runnable r = new Runnable() {
2146 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002148 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002150 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2151 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002152 }
2153 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 };
2155 if (postOnMainThread) {
2156 deferredBindRunnables.add(r);
2157 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002158 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 }
Joe Onorato36115782010-06-17 13:28:48 -04002160 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161
2162 // Bind the folders
2163 if (!folders.isEmpty()) {
2164 final Runnable r = new Runnable() {
2165 public void run() {
2166 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2167 if (callbacks != null) {
2168 callbacks.bindFolders(folders);
2169 }
2170 }
2171 };
2172 if (postOnMainThread) {
2173 deferredBindRunnables.add(r);
2174 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002175 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 }
2177 }
2178
2179 // Bind the widgets, one at a time
2180 N = appWidgets.size();
2181 for (int i = 0; i < N; i++) {
2182 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2183 final Runnable r = new Runnable() {
2184 public void run() {
2185 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2186 if (callbacks != null) {
2187 callbacks.bindAppWidget(widget);
2188 }
2189 }
2190 };
2191 if (postOnMainThread) {
2192 deferredBindRunnables.add(r);
2193 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002194 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 }
2196 }
2197 }
2198
2199 /**
2200 * Binds all loaded data to actual views on the main thread.
2201 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002202 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002203 final long t = SystemClock.uptimeMillis();
2204 Runnable r;
2205
2206 // Don't use these two variables in any of the callback runnables.
2207 // Otherwise we hold a reference to them.
2208 final Callbacks oldCallbacks = mCallbacks.get();
2209 if (oldCallbacks == null) {
2210 // This launcher has exited and nobody bothered to tell us. Just bail.
2211 Log.w(TAG, "LoaderTask running with no launcher");
2212 return;
2213 }
2214
Winson Chung4a2afa32012-07-19 14:53:05 -07002215 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2216 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002217 oldCallbacks.getCurrentWorkspaceScreen();
2218
2219 // Load all the items that are on the current page first (and in the process, unbind
2220 // all the existing workspace items before we call startBinding() below.
2221 unbindWorkspaceItemsOnMainThread();
2222 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2223 ArrayList<LauncherAppWidgetInfo> appWidgets =
2224 new ArrayList<LauncherAppWidgetInfo>();
2225 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2226 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002228 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 workspaceItems.addAll(sBgWorkspaceItems);
2230 appWidgets.addAll(sBgAppWidgets);
2231 folders.putAll(sBgFolders);
2232 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002233 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 }
2235
2236 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2237 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2238 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2239 new ArrayList<LauncherAppWidgetInfo>();
2240 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2241 new ArrayList<LauncherAppWidgetInfo>();
2242 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2243 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2244
2245 // Separate the items that are on the current screen, and all the other remaining items
2246 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2247 otherWorkspaceItems);
2248 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2249 otherAppWidgets);
2250 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2251 otherFolders);
2252 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2253 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2254
2255 // Tell the workspace that we're about to start binding items
2256 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002257 public void run() {
2258 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2259 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002261 }
2262 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 };
Winson Chung81b52252012-08-27 15:34:29 -07002264 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2267
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002268 // Load items on the current page
2269 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2270 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002271 if (isLoadingSynchronously) {
2272 r = new Runnable() {
2273 public void run() {
2274 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2275 if (callbacks != null) {
2276 callbacks.onPageBoundSynchronously(currentScreen);
2277 }
2278 }
2279 };
Winson Chung81b52252012-08-27 15:34:29 -07002280 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002281 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282
Winson Chung4a2afa32012-07-19 14:53:05 -07002283 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2284 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285 mDeferredBindRunnables.clear();
2286 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002287 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002288
2289 // Tell the workspace that we're done binding items
2290 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002291 public void run() {
2292 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2293 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002294 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002295 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002296
Winson Chung98e030b2012-05-07 16:01:11 -07002297 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002298 if (DEBUG_LOADERS) {
2299 Log.d(TAG, "bound workspace in "
2300 + (SystemClock.uptimeMillis()-t) + "ms");
2301 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002302
2303 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002304 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002305 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002306 if (isLoadingSynchronously) {
2307 mDeferredBindRunnables.add(r);
2308 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002309 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002310 }
Joe Onorato36115782010-06-17 13:28:48 -04002311 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002312
Joe Onorato36115782010-06-17 13:28:48 -04002313 private void loadAndBindAllApps() {
2314 if (DEBUG_LOADERS) {
2315 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2316 }
2317 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002318 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002319 synchronized (LoaderTask.this) {
2320 if (mStopped) {
2321 return;
2322 }
2323 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002324 }
Joe Onorato36115782010-06-17 13:28:48 -04002325 } else {
2326 onlyBindAllApps();
2327 }
2328 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002329
Joe Onorato36115782010-06-17 13:28:48 -04002330 private void onlyBindAllApps() {
2331 final Callbacks oldCallbacks = mCallbacks.get();
2332 if (oldCallbacks == null) {
2333 // This launcher has exited and nobody bothered to tell us. Just bail.
2334 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2335 return;
2336 }
2337
2338 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002339 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002340 final ArrayList<AppInfo> list
2341 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002342 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002343 public void run() {
2344 final long t = SystemClock.uptimeMillis();
2345 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2346 if (callbacks != null) {
2347 callbacks.bindAllApplications(list);
2348 }
2349 if (DEBUG_LOADERS) {
2350 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2351 + (SystemClock.uptimeMillis()-t) + "ms");
2352 }
2353 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002354 };
2355 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002356 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002357 r.run();
2358 } else {
2359 mHandler.post(r);
2360 }
Joe Onorato36115782010-06-17 13:28:48 -04002361 }
2362
Winson Chung64359a52013-07-08 17:17:08 -07002363 private void loadAllApps() {
2364 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002365
Joe Onorato36115782010-06-17 13:28:48 -04002366 final Callbacks oldCallbacks = mCallbacks.get();
2367 if (oldCallbacks == null) {
2368 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002369 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002370 return;
2371 }
2372
Winson Chung64359a52013-07-08 17:17:08 -07002373 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002374 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2375 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2376
Winson Chung64359a52013-07-08 17:17:08 -07002377 // Clear the list of apps
2378 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002379
Winson Chung64359a52013-07-08 17:17:08 -07002380 // Query for the set of apps
2381 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2382 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2383 if (DEBUG_LOADERS) {
2384 Log.d(TAG, "queryIntentActivities took "
2385 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2386 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2387 }
2388 // Fail if we don't have any apps
2389 if (apps == null || apps.isEmpty()) {
2390 return;
2391 }
2392 // Sort the applications by name
2393 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2394 Collections.sort(apps,
2395 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2396 if (DEBUG_LOADERS) {
2397 Log.d(TAG, "sort took "
2398 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 }
2400
Winson Chung64359a52013-07-08 17:17:08 -07002401 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002402 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002403 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002404 // This builds the icon bitmaps.
2405 mBgAllAppsList.add(new AppInfo(packageManager, app,
2406 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002407 }
2408
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002409 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002410 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2411 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002412
2413 // Post callback on main thread
2414 mHandler.post(new Runnable() {
2415 public void run() {
2416 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002417 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002418 if (callbacks != null) {
2419 callbacks.bindAllApplications(added);
2420 if (DEBUG_LOADERS) {
2421 Log.d(TAG, "bound " + added.size() + " apps in "
2422 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2423 }
2424 } else {
2425 Log.i(TAG, "not binding apps: no Launcher activity");
2426 }
2427 }
2428 });
2429
Joe Onorato36115782010-06-17 13:28:48 -04002430 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002431 Log.d(TAG, "Icons processed in "
2432 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002433 }
2434 }
2435
2436 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002437 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002438 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2439 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2440 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2441 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2442 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2443 }
Joe Onorato36115782010-06-17 13:28:48 -04002444 }
2445 }
2446
2447 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002448 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002449 }
2450
2451 private class PackageUpdatedTask implements Runnable {
2452 int mOp;
2453 String[] mPackages;
2454
2455 public static final int OP_NONE = 0;
2456 public static final int OP_ADD = 1;
2457 public static final int OP_UPDATE = 2;
2458 public static final int OP_REMOVE = 3; // uninstlled
2459 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2460
2461
2462 public PackageUpdatedTask(int op, String[] packages) {
2463 mOp = op;
2464 mPackages = packages;
2465 }
2466
2467 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002468 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002469
2470 final String[] packages = mPackages;
2471 final int N = packages.length;
2472 switch (mOp) {
2473 case OP_ADD:
2474 for (int i=0; i<N; i++) {
2475 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002476 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002477 }
2478 break;
2479 case OP_UPDATE:
2480 for (int i=0; i<N; i++) {
2481 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002482 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002483 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002484 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002485 }
2486 break;
2487 case OP_REMOVE:
2488 case OP_UNAVAILABLE:
2489 for (int i=0; i<N; i++) {
2490 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002491 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002492 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002493 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002494 }
2495 break;
2496 }
2497
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002498 ArrayList<AppInfo> added = null;
2499 ArrayList<AppInfo> modified = null;
2500 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002501
Adam Cohen487f7dd2012-06-28 18:12:10 -07002502 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002503 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002504 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002505 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002506 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002507 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002508 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002509 }
Winson Chung5d55f332012-07-16 20:45:03 -07002510 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002511 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002512 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002513 }
2514
Joe Onorato36115782010-06-17 13:28:48 -04002515 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2516 if (callbacks == null) {
2517 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2518 return;
2519 }
2520
2521 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002522 // Ensure that we add all the workspace applications to the db
2523 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002524 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2525 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2526 } else {
2527 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2528 addAndBindAddedApps(context, addedInfos, cb, added);
2529 }
Joe Onorato36115782010-06-17 13:28:48 -04002530 }
2531 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002532 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002533
2534 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002535 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002536 ArrayList<ItemInfo> infos =
2537 getItemInfoForComponentName(a.componentName);
2538 for (ItemInfo i : infos) {
2539 if (isShortcutInfoUpdateable(i)) {
2540 ShortcutInfo info = (ShortcutInfo) i;
2541 info.title = a.title.toString();
2542 updateItemInDatabase(context, info);
2543 }
2544 }
2545 }
2546
Joe Onorato36115782010-06-17 13:28:48 -04002547 mHandler.post(new Runnable() {
2548 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002549 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2550 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002551 callbacks.bindAppsUpdated(modifiedFinal);
2552 }
2553 }
2554 });
2555 }
Winson Chung83892cc2013-05-01 16:53:33 -07002556 // If a package has been removed, or an app has been removed as a result of
2557 // an update (for example), make the removed callback.
2558 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002559 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002560 final ArrayList<String> removedPackageNames =
2561 new ArrayList<String>(Arrays.asList(packages));
2562
Winson Chung64359a52013-07-08 17:17:08 -07002563 // Update the launcher db to reflect the removal of apps
2564 if (packageRemoved) {
2565 for (String pn : removedPackageNames) {
2566 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2567 for (ItemInfo i : infos) {
2568 deleteItemFromDatabase(context, i);
2569 }
2570 }
Winson Chung780fe592013-09-26 14:48:44 -07002571
2572 // Remove any queued items from the install queue
2573 String spKey = LauncherAppState.getSharedPreferencesKey();
2574 SharedPreferences sp =
2575 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2576 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
Winson Chung64359a52013-07-08 17:17:08 -07002577 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002578 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002579 ArrayList<ItemInfo> infos =
2580 getItemInfoForComponentName(a.componentName);
2581 for (ItemInfo i : infos) {
2582 deleteItemFromDatabase(context, i);
2583 }
2584 }
2585 }
2586
Joe Onorato36115782010-06-17 13:28:48 -04002587 mHandler.post(new Runnable() {
2588 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002589 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2590 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002591 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002592 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002593 }
2594 }
2595 });
Joe Onoratobe386092009-11-17 17:32:16 -08002596 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002597
Michael Jurkac402cd92013-05-20 15:49:32 +02002598 final ArrayList<Object> widgetsAndShortcuts =
2599 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002600 mHandler.post(new Runnable() {
2601 @Override
2602 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002603 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2604 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002605 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002606 }
2607 }
2608 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002609
2610 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002611 mHandler.post(new Runnable() {
2612 public void run() {
2613 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2614 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002615 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002616 }
2617 }
2618 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002619 }
2620 }
2621
Michael Jurkac402cd92013-05-20 15:49:32 +02002622 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2623 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2624 PackageManager packageManager = context.getPackageManager();
2625 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2626 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2627 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2628 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2629 Collections.sort(widgetsAndShortcuts,
2630 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2631 return widgetsAndShortcuts;
2632 }
2633
Winson Chung1323b482013-08-05 12:41:55 -07002634 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002635 if (cn == null) {
2636 return false;
2637 }
2638
2639 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002640 // Skip if the application is disabled
2641 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2642 if (!pi.applicationInfo.enabled) {
2643 return false;
2644 }
2645
2646 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002647 return (pm.getActivityInfo(cn, 0) != null);
2648 } catch (NameNotFoundException e) {
2649 return false;
2650 }
2651 }
2652
Joe Onorato9c1289c2009-08-17 11:03:03 -04002653 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002654 * This is called from the code that adds shortcuts from the intent receiver. This
2655 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002656 */
Joe Onorato56d82912010-03-07 14:32:10 -05002657 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002658 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002659 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002660
Joe Onorato56d82912010-03-07 14:32:10 -05002661 /**
2662 * Make an ShortcutInfo object for a shortcut that is an application.
2663 *
2664 * If c is not null, then it will be used to fill in missing data like the title and icon.
2665 */
2666 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002667 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002668 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002669 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002670 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002671 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002672 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002673 } else {
2674 try {
2675 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2676 info.initFlagsAndFirstInstallTime(pi);
2677 } catch (NameNotFoundException e) {
2678 Log.d(TAG, "getPackInfo failed for package " +
2679 componentName.getPackageName());
2680 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002681 }
2682
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002683 // TODO: See if the PackageManager knows about this case. If it doesn't
2684 // then return null & delete this.
2685
Joe Onorato56d82912010-03-07 14:32:10 -05002686 // the resource -- This may implicitly give us back the fallback icon,
2687 // but don't worry about that. All we're doing with usingFallbackIcon is
2688 // to avoid saving lots of copies of that in the database, and most apps
2689 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002690
2691 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2692 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2693 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002694 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002695 ResolveInfo resolveInfo = null;
2696 ComponentName oldComponent = intent.getComponent();
2697 Intent newIntent = new Intent(intent.getAction(), null);
2698 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2699 newIntent.setPackage(oldComponent.getPackageName());
2700 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2701 for (ResolveInfo i : infos) {
2702 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2703 i.activityInfo.name);
2704 if (cn.equals(oldComponent)) {
2705 resolveInfo = i;
2706 }
2707 }
2708 if (resolveInfo == null) {
2709 resolveInfo = manager.resolveActivity(intent, 0);
2710 }
Joe Onorato56d82912010-03-07 14:32:10 -05002711 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002712 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002713 }
Joe Onorato56d82912010-03-07 14:32:10 -05002714 // the db
2715 if (icon == null) {
2716 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002717 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002718 }
2719 }
2720 // the fallback icon
2721 if (icon == null) {
2722 icon = getFallbackIcon();
2723 info.usingFallbackIcon = true;
2724 }
2725 info.setIcon(icon);
2726
2727 // from the resource
2728 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002729 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2730 if (labelCache != null && labelCache.containsKey(key)) {
2731 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002732 } else {
2733 info.title = resolveInfo.activityInfo.loadLabel(manager);
2734 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002735 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002736 }
2737 }
Joe Onorato56d82912010-03-07 14:32:10 -05002738 }
2739 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002741 if (c != null) {
2742 info.title = c.getString(titleIndex);
2743 }
2744 }
2745 // fall back to the class name of the activity
2746 if (info.title == null) {
2747 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002749 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2750 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002752
Winson Chung64359a52013-07-08 17:17:08 -07002753 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2754 ItemInfoFilter f) {
2755 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2756 for (ItemInfo i : infos) {
2757 if (i instanceof ShortcutInfo) {
2758 ShortcutInfo info = (ShortcutInfo) i;
2759 ComponentName cn = info.intent.getComponent();
2760 if (cn != null && f.filterItem(null, info, cn)) {
2761 filtered.add(info);
2762 }
2763 } else if (i instanceof FolderInfo) {
2764 FolderInfo info = (FolderInfo) i;
2765 for (ShortcutInfo s : info.contents) {
2766 ComponentName cn = s.intent.getComponent();
2767 if (cn != null && f.filterItem(info, s, cn)) {
2768 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002769 }
2770 }
Winson Chung64359a52013-07-08 17:17:08 -07002771 } else if (i instanceof LauncherAppWidgetInfo) {
2772 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2773 ComponentName cn = info.providerName;
2774 if (cn != null && f.filterItem(null, info, cn)) {
2775 filtered.add(info);
2776 }
Winson Chung8a435102012-08-30 17:16:53 -07002777 }
2778 }
Winson Chung64359a52013-07-08 17:17:08 -07002779 return new ArrayList<ItemInfo>(filtered);
2780 }
2781
2782 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002783 ItemInfoFilter filter = new ItemInfoFilter() {
2784 @Override
2785 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2786 return cn.getPackageName().equals(pn);
2787 }
2788 };
2789 return filterItemInfos(sBgItemsIdMap.values(), filter);
2790 }
2791
2792 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002793 ItemInfoFilter filter = new ItemInfoFilter() {
2794 @Override
2795 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2796 return cn.equals(cname);
2797 }
2798 };
2799 return filterItemInfos(sBgItemsIdMap.values(), filter);
2800 }
2801
2802 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2803 if (i instanceof ShortcutInfo) {
2804 ShortcutInfo info = (ShortcutInfo) i;
2805 // We need to check for ACTION_MAIN otherwise getComponent() might
2806 // return null for some shortcuts (for instance, for shortcuts to
2807 // web pages.)
2808 Intent intent = info.intent;
2809 ComponentName name = intent.getComponent();
2810 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2811 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2812 return true;
2813 }
2814 }
2815 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002816 }
2817
2818 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002819 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002821 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002822 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2823 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824
Joe Onorato56d82912010-03-07 14:32:10 -05002825 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002826 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002828
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002829 // TODO: If there's an explicit component and we can't install that, delete it.
2830
Joe Onorato56d82912010-03-07 14:32:10 -05002831 info.title = c.getString(titleIndex);
2832
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 int iconType = c.getInt(iconTypeIndex);
2834 switch (iconType) {
2835 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2836 String packageName = c.getString(iconPackageIndex);
2837 String resourceName = c.getString(iconResourceIndex);
2838 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002839 info.customIcon = false;
2840 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002843 if (resources != null) {
2844 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002845 icon = Utilities.createIconBitmap(
2846 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002849 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 }
Joe Onorato56d82912010-03-07 14:32:10 -05002851 // the db
2852 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002853 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002854 }
2855 // the fallback icon
2856 if (icon == null) {
2857 icon = getFallbackIcon();
2858 info.usingFallbackIcon = true;
2859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 break;
2861 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002862 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002863 if (icon == null) {
2864 icon = getFallbackIcon();
2865 info.customIcon = false;
2866 info.usingFallbackIcon = true;
2867 } else {
2868 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 break;
2871 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002872 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002873 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002874 info.customIcon = false;
2875 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002877 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 return info;
2879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880
Michael Jurka931dc972011-08-05 15:08:15 -07002881 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002882 @SuppressWarnings("all") // suppress dead code warning
2883 final boolean debug = false;
2884 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002885 Log.d(TAG, "getIconFromCursor app="
2886 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2887 }
2888 byte[] data = c.getBlob(iconIndex);
2889 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002890 return Utilities.createIconBitmap(
2891 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002892 } catch (Exception e) {
2893 return null;
2894 }
2895 }
2896
Winson Chung3d503fb2011-07-13 17:25:49 -07002897 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2898 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002899 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002900 if (info == null) {
2901 return null;
2902 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002903 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002904
2905 return info;
2906 }
2907
Winson Chunga9abd0e2010-10-27 17:18:37 -07002908 /**
Winson Chung55cef262010-10-28 14:14:18 -07002909 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2910 */
2911 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2912 ComponentName component) {
2913 List<AppWidgetProviderInfo> widgets =
2914 AppWidgetManager.getInstance(context).getInstalledProviders();
2915 for (AppWidgetProviderInfo info : widgets) {
2916 if (info.provider.equals(component)) {
2917 return info;
2918 }
2919 }
2920 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002921 }
2922
Winson Chung68846fd2010-10-29 11:00:27 -07002923 /**
2924 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2925 */
2926 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2927 final PackageManager packageManager = context.getPackageManager();
2928 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2929 new ArrayList<WidgetMimeTypeHandlerData>();
2930
2931 final Intent supportsIntent =
2932 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2933 supportsIntent.setType(mimeType);
2934
2935 // Create a set of widget configuration components that we can test against
2936 final List<AppWidgetProviderInfo> widgets =
2937 AppWidgetManager.getInstance(context).getInstalledProviders();
2938 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2939 new HashMap<ComponentName, AppWidgetProviderInfo>();
2940 for (AppWidgetProviderInfo info : widgets) {
2941 configurationComponentToWidget.put(info.configure, info);
2942 }
2943
2944 // Run through each of the intents that can handle this type of clip data, and cross
2945 // reference them with the components that are actual configuration components
2946 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2947 PackageManager.MATCH_DEFAULT_ONLY);
2948 for (ResolveInfo info : activities) {
2949 final ActivityInfo activityInfo = info.activityInfo;
2950 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2951 activityInfo.name);
2952 if (configurationComponentToWidget.containsKey(infoComponent)) {
2953 supportedConfigurationActivities.add(
2954 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2955 configurationComponentToWidget.get(infoComponent)));
2956 }
2957 }
2958 return supportedConfigurationActivities;
2959 }
2960
Winson Chunga9abd0e2010-10-27 17:18:37 -07002961 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002962 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2963 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2964 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2965
Adam Cohend9198822011-11-22 16:42:47 -08002966 if (intent == null) {
2967 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2968 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2969 return null;
2970 }
2971
Joe Onorato0589f0f2010-02-08 13:44:00 -08002972 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002973 boolean customIcon = false;
2974 ShortcutIconResource iconResource = null;
2975
2976 if (bitmap != null && bitmap instanceof Bitmap) {
2977 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002978 customIcon = true;
2979 } else {
2980 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2981 if (extra != null && extra instanceof ShortcutIconResource) {
2982 try {
2983 iconResource = (ShortcutIconResource) extra;
2984 final PackageManager packageManager = context.getPackageManager();
2985 Resources resources = packageManager.getResourcesForApplication(
2986 iconResource.packageName);
2987 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002988 icon = Utilities.createIconBitmap(
2989 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002990 } catch (Exception e) {
2991 Log.w(TAG, "Could not load shortcut icon: " + extra);
2992 }
2993 }
2994 }
2995
Michael Jurkac9d95c52011-08-29 14:03:34 -07002996 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002997
2998 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002999 if (fallbackIcon != null) {
3000 icon = fallbackIcon;
3001 } else {
3002 icon = getFallbackIcon();
3003 info.usingFallbackIcon = true;
3004 }
Joe Onorato56d82912010-03-07 14:32:10 -05003005 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003006 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003007
Joe Onorato0589f0f2010-02-08 13:44:00 -08003008 info.title = name;
3009 info.intent = intent;
3010 info.customIcon = customIcon;
3011 info.iconResource = iconResource;
3012
3013 return info;
3014 }
3015
Winson Chungaac01e12011-08-17 10:37:13 -07003016 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3017 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003018 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003019 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003020 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003021 }
Joe Onorato56d82912010-03-07 14:32:10 -05003022 // If this icon doesn't have a custom icon, check to see
3023 // what's stored in the DB, and if it doesn't match what
3024 // we're going to show, store what we are going to show back
3025 // into the DB. We do this so when we're loading, if the
3026 // package manager can't find an icon (for example because
3027 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003028 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003029 cache.put(info, c.getBlob(iconIndex));
3030 return true;
3031 }
3032 return false;
3033 }
3034 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3035 boolean needSave = false;
3036 try {
3037 if (data != null) {
3038 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3039 Bitmap loaded = info.getIcon(mIconCache);
3040 needSave = !saved.sameAs(loaded);
3041 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003042 needSave = true;
3043 }
Winson Chungaac01e12011-08-17 10:37:13 -07003044 } catch (Exception e) {
3045 needSave = true;
3046 }
3047 if (needSave) {
3048 Log.d(TAG, "going to save icon bitmap for info=" + info);
3049 // This is slower than is ideal, but this only happens once
3050 // or when the app is updated with a new icon.
3051 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003052 }
3053 }
3054
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003056 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 * or make a new one.
3058 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003059 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 // See if a placeholder was created for us already
3061 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003062 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003064 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 folders.put(id, folderInfo);
3066 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003067 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068 }
3069
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003070 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003071 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003072 return new Comparator<AppInfo>() {
3073 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003074 int result = collator.compare(a.title.toString().trim(),
3075 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003076 if (result == 0) {
3077 result = a.componentName.compareTo(b.componentName);
3078 }
3079 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003080 }
Winson Chung11904872012-09-17 16:58:46 -07003081 };
3082 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003083 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3084 = new Comparator<AppInfo>() {
3085 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003086 if (a.firstInstallTime < b.firstInstallTime) return 1;
3087 if (a.firstInstallTime > b.firstInstallTime) return -1;
3088 return 0;
3089 }
3090 };
Winson Chung11904872012-09-17 16:58:46 -07003091 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3092 final Collator collator = Collator.getInstance();
3093 return new Comparator<AppWidgetProviderInfo>() {
3094 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003095 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003096 }
3097 };
3098 }
Winson Chung5308f242011-08-18 12:12:41 -07003099 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3100 if (info.activityInfo != null) {
3101 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3102 } else {
3103 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3104 }
3105 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003106 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003107 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003108 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003109 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003110 ShortcutNameComparator(PackageManager pm) {
3111 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003112 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003113 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003114 }
3115 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3116 mPackageManager = pm;
3117 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003118 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003119 }
3120 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003121 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003122 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3123 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3124 if (mLabelCache.containsKey(keyA)) {
3125 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003126 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003127 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003128
Winson Chung5308f242011-08-18 12:12:41 -07003129 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003130 }
Winson Chung5308f242011-08-18 12:12:41 -07003131 if (mLabelCache.containsKey(keyB)) {
3132 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003133 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003134 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003135
Winson Chung5308f242011-08-18 12:12:41 -07003136 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003137 }
Winson Chung11904872012-09-17 16:58:46 -07003138 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003139 }
3140 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003141 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003142 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003143 private PackageManager mPackageManager;
3144 private HashMap<Object, String> mLabelCache;
3145 WidgetAndShortcutNameComparator(PackageManager pm) {
3146 mPackageManager = pm;
3147 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003148 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003149 }
3150 public final int compare(Object a, Object b) {
3151 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003152 if (mLabelCache.containsKey(a)) {
3153 labelA = mLabelCache.get(a);
3154 } else {
3155 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003156 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003157 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003158 mLabelCache.put(a, labelA);
3159 }
3160 if (mLabelCache.containsKey(b)) {
3161 labelB = mLabelCache.get(b);
3162 } else {
3163 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003164 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003165 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003166 mLabelCache.put(b, labelB);
3167 }
Winson Chung11904872012-09-17 16:58:46 -07003168 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003169 }
3170 };
Joe Onoratobe386092009-11-17 17:32:16 -08003171
3172 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003173 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003174 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3175 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3176 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3177 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003178 if (mLoaderTask != null) {
3179 mLoaderTask.dumpState();
3180 } else {
3181 Log.d(TAG, "mLoaderTask=null");
3182 }
Joe Onoratobe386092009-11-17 17:32:16 -08003183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184}