blob: d1819e14d86d4d7713b54e2401600f3bb917b8ba [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
189 mDefaultIcon = Utilities.createIconBitmap(
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 mIconCache.getFullResDefaultActivityIcon(), context);
Daniel Sandler2ff10b32010-04-16 15:06:06 -0400191
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400192 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700193 Configuration config = res.getConfiguration();
194 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 }
196
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700197 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
198 * posted on the main thread handler. */
199 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700200 runOnMainThread(r, 0);
201 }
202 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700203 if (sWorkerThread.getThreadId() == Process.myTid()) {
204 // If we are on the worker thread, post onto the main handler
205 mHandler.post(r);
206 } else {
207 r.run();
208 }
209 }
210
211 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
212 * posted on the worker thread handler. */
213 private static void runOnWorkerThread(Runnable r) {
214 if (sWorkerThread.getThreadId() == Process.myTid()) {
215 r.run();
216 } else {
217 // If we are not on the worker thread, then post to the worker handler
218 sWorker.post(r);
219 }
220 }
221
Winson Chungc9168342013-06-26 14:54:55 -0700222 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
223 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700224 LauncherAppState app = LauncherAppState.getInstance();
225 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
226 final int xCount = (int) grid.numColumns;
227 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700228 boolean[][] occupied = new boolean[xCount][yCount];
229
230 int cellX, cellY, spanX, spanY;
231 for (int i = 0; i < items.size(); ++i) {
232 final ItemInfo item = items.get(i);
233 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
234 if (item.screenId == screen) {
235 cellX = item.cellX;
236 cellY = item.cellY;
237 spanX = item.spanX;
238 spanY = item.spanY;
239 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
240 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
241 occupied[x][y] = true;
242 }
243 }
244 }
245 }
246 }
247
248 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
249 }
250 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700251 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700252 int firstScreenIndex,
253 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700254 // Lock on the app so that we don't try and get the items while apps are being added
255 LauncherAppState app = LauncherAppState.getInstance();
256 LauncherModel model = app.getModel();
257 boolean found = false;
258 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700259 if (sWorkerThread.getThreadId() != Process.myTid()) {
260 // Flush the LauncherModel worker thread, so that if we just did another
261 // processInstallShortcut, we give it time for its shortcut to get added to the
262 // database (getItemsInLocalCoordinates reads the database)
263 model.flushWorkerThread();
264 }
Winson Chungc9168342013-06-26 14:54:55 -0700265 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700266
267 // Try adding to the workspace screens incrementally, starting at the default or center
268 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700269 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
270 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700271 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 int[] tmpCoordinates = new int[2];
273 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700274 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700275 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700276 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700277 }
278 }
279 }
Winson Chungc9168342013-06-26 14:54:55 -0700280 return null;
281 }
282
Winson Chung94d67682013-09-25 16:29:40 -0700283 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
284 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700285 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700286 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700287 }
Winson Chung94d67682013-09-25 16:29:40 -0700288 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
289 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
290 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700291 return;
Winson Chung997a9232013-07-24 15:33:46 -0700292 }
Winson Chung64359a52013-07-08 17:17:08 -0700293 // Process the newly added applications and add them to the database first
294 Runnable r = new Runnable() {
295 public void run() {
296 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
297 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
298
Winson Chung76828c82013-08-19 15:43:29 -0700299 // Get the list of workspace screens. We need to append to this list and
300 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
301 // called.
302 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
303 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
304 for (Integer i : orderedScreens.keySet()) {
305 long screenId = orderedScreens.get(i);
306 workspaceScreens.add(screenId);
307 }
308
Winson Chung64359a52013-07-08 17:17:08 -0700309 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700310 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700311 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700312 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700313 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700314 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700315
316 // Short-circuit this logic if the icon exists somewhere on the workspace
317 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
318 continue;
319 }
320
Winson Chung87412982013-10-03 18:34:14 -0700321 // Add this icon to the db, creating a new page if necessary. If there
322 // is only the empty page then we just add items to the first page.
323 // Otherwise, we add them to the next pages.
324 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700325 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700326 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700327 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200328 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700329
330 // If we can't find a valid position, then just add a new screen.
331 // This takes time so we need to re-queue the add until the new
332 // page is added. Create as many screens as necessary to satisfy
333 // the startSearchPageIndex.
334 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700335 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 while (numPagesToAdd > 0) {
337 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700339 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 addedWorkspaceScreensFinal.add(screenId);
341 numPagesToAdd--;
342 }
Winson Chung76828c82013-08-19 15:43:29 -0700343
Winson Chung64359a52013-07-08 17:17:08 -0700344 // Find the coordinate again
345 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700346 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700347 }
348 if (coords == null) {
349 throw new RuntimeException("Coordinates should not be null");
350 }
351
Winson Chung997a9232013-07-24 15:33:46 -0700352 ShortcutInfo shortcutInfo;
353 if (a instanceof ShortcutInfo) {
354 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200355 } else if (a instanceof AppInfo) {
356 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700357 } else {
358 throw new RuntimeException("Unexpected info type");
359 }
Winson Chung94d67682013-09-25 16:29:40 -0700360
Winson Chung64359a52013-07-08 17:17:08 -0700361 // Add the shortcut to the db
362 addItemToDatabase(context, shortcutInfo,
363 LauncherSettings.Favorites.CONTAINER_DESKTOP,
364 coords.first, coords.second[0], coords.second[1], false);
365 // Save the ShortcutInfo for binding in the workspace
366 addedShortcutsFinal.add(shortcutInfo);
367 }
368 }
369
Winson Chung76828c82013-08-19 15:43:29 -0700370 // Update the workspace screens
371 updateWorkspaceScreenOrder(context, workspaceScreens);
372
Winson Chung94d67682013-09-25 16:29:40 -0700373 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 runOnMainThread(new Runnable() {
375 public void run() {
376 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
377 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700380 if (!addedShortcutsFinal.isEmpty()) {
381 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
382 long lastScreenId = info.screenId;
383 for (ItemInfo i : addedShortcutsFinal) {
384 if (i.screenId == lastScreenId) {
385 addAnimated.add(i);
386 } else {
387 addNotAnimated.add(i);
388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
390 }
Winson Chungd64d1762013-08-20 14:37:16 -0700391 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700392 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
Winson Chung997a9232013-07-24 15:33:46 -0700395 });
396 }
Winson Chung64359a52013-07-08 17:17:08 -0700397 }
398 };
399 runOnWorkerThread(r);
400 }
401
Joe Onorato56d82912010-03-07 14:32:10 -0500402 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405
Winson Chung81b52252012-08-27 15:34:29 -0700406 public void unbindItemInfosAndClearQueuedBindRunnables() {
407 if (sWorkerThread.getThreadId() == Process.myTid()) {
408 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
409 "main thread");
410 }
411
412 // Clear any deferred bind runnables
413 mDeferredBindRunnables.clear();
414 // Remove any queued bind runnables
415 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
416 // Unbind all the workspace items
417 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700418 }
419
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700420 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700421 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700422 // Ensure that we don't use the same workspace items data structure on the main thread
423 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700424 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
425 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700426 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
428 tmpAppWidgets.addAll(sBgAppWidgets);
429 }
430 Runnable r = new Runnable() {
431 @Override
432 public void run() {
433 for (ItemInfo item : tmpWorkspaceItems) {
434 item.unbind();
435 }
436 for (ItemInfo item : tmpAppWidgets) {
437 item.unbind();
438 }
439 }
440 };
441 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700442 }
443
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 /**
445 * Adds an item to the DB if it was not created previously, or move it to a new
446 * <container, screen, cellX, cellY>
447 */
448 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400450 if (item.container == ItemInfo.NO_ID) {
451 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700452 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400453 } else {
454 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700455 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
458
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700459 static void checkItemInfoLocked(
460 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
461 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
462 if (modelItem != null && item != modelItem) {
463 // check all the data is consistent
464 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
465 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
466 ShortcutInfo shortcut = (ShortcutInfo) item;
467 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
468 modelShortcut.intent.filterEquals(shortcut.intent) &&
469 modelShortcut.id == shortcut.id &&
470 modelShortcut.itemType == shortcut.itemType &&
471 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700472 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700473 modelShortcut.cellX == shortcut.cellX &&
474 modelShortcut.cellY == shortcut.cellY &&
475 modelShortcut.spanX == shortcut.spanX &&
476 modelShortcut.spanY == shortcut.spanY &&
477 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
478 (modelShortcut.dropPos != null &&
479 shortcut.dropPos != null &&
480 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
481 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
482 // For all intents and purposes, this is the same object
483 return;
484 }
485 }
486
487 // the modelItem needs to match up perfectly with item if our model is
488 // to be consistent with the database-- for now, just require
489 // modelItem == item or the equality check above
490 String msg = "item: " + ((item != null) ? item.toString() : "null") +
491 "modelItem: " +
492 ((modelItem != null) ? modelItem.toString() : "null") +
493 "Error: ItemInfo passed to checkItemInfo doesn't match original";
494 RuntimeException e = new RuntimeException(msg);
495 if (stackTrace != null) {
496 e.setStackTrace(stackTrace);
497 }
Adam Cohene25af792013-06-06 23:08:25 -0700498 // TODO: something breaks this in the upgrade path
499 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700500 }
501 }
502
Michael Jurka816474f2012-06-25 14:49:02 -0700503 static void checkItemInfo(final ItemInfo item) {
504 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
505 final long itemId = item.id;
506 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700507 public void run() {
508 synchronized (sBgLock) {
509 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700510 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700511 }
512 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700513 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700514 }
515
Michael Jurkac9d95c52011-08-29 14:03:34 -0700516 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
517 final ItemInfo item, final String callingFunction) {
518 final long itemId = item.id;
519 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
520 final ContentResolver cr = context.getContentResolver();
521
Adam Cohen487f7dd2012-06-28 18:12:10 -0700522 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700523 Runnable r = new Runnable() {
524 public void run() {
525 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700526 updateItemArrays(item, itemId, stackTrace);
527 }
528 };
529 runOnWorkerThread(r);
530 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700531
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
533 final ArrayList<ItemInfo> items, final String callingFunction) {
534 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700535
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700536 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
537 Runnable r = new Runnable() {
538 public void run() {
539 ArrayList<ContentProviderOperation> ops =
540 new ArrayList<ContentProviderOperation>();
541 int count = items.size();
542 for (int i = 0; i < count; i++) {
543 ItemInfo item = items.get(i);
544 final long itemId = item.id;
545 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
546 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700547
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700548 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
549 updateItemArrays(item, itemId, stackTrace);
550
551 }
552 try {
553 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
554 } catch (Exception e) {
555 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700556 }
557 }
558 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700559 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700561
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700562 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
563 // Lock on mBgLock *after* the db operation
564 synchronized (sBgLock) {
565 checkItemInfoLocked(itemId, item, stackTrace);
566
567 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
568 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
569 // Item is in a folder, make sure this folder exists
570 if (!sBgFolders.containsKey(item.container)) {
571 // An items container is being set to a that of an item which is not in
572 // the list of Folders.
573 String msg = "item: " + item + " container being set to: " +
574 item.container + ", not in the list of folders";
575 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700576 }
577 }
578
579 // Items are added/removed from the corresponding FolderInfo elsewhere, such
580 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
581 // that are on the desktop, as appropriate
582 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
583 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
584 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
585 switch (modelItem.itemType) {
586 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
587 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
588 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
589 if (!sBgWorkspaceItems.contains(modelItem)) {
590 sBgWorkspaceItems.add(modelItem);
591 }
592 break;
593 default:
594 break;
595 }
596 } else {
597 sBgWorkspaceItems.remove(modelItem);
598 }
599 }
600 }
601
Michael Jurkac7700af2013-05-14 20:17:58 +0200602 public void flushWorkerThread() {
603 mFlushingWorkerThread = true;
604 Runnable waiter = new Runnable() {
605 public void run() {
606 synchronized (this) {
607 notifyAll();
608 mFlushingWorkerThread = false;
609 }
610 }
611 };
612
613 synchronized(waiter) {
614 runOnWorkerThread(waiter);
615 if (mLoaderTask != null) {
616 synchronized(mLoaderTask) {
617 mLoaderTask.notify();
618 }
619 }
620 boolean success = false;
621 while (!success) {
622 try {
623 waiter.wait();
624 success = true;
625 } catch (InterruptedException e) {
626 }
627 }
628 }
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400632 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700633 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700634 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 item.cellX = cellX;
638 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700639
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 // We store hotseat items in canonical form which is this orientation invariant position
641 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648
649 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
652 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654
Michael Jurkac9d95c52011-08-29 14:03:34 -0700655 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700656 }
657
658 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
660 * cellX, cellY have already been updated on the ItemInfos.
661 */
662 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
663 final long container, final int screen) {
664
665 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
666 int count = items.size();
667
668 for (int i = 0; i < count; i++) {
669 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700670 item.container = container;
671
672 // We store hotseat items in canonical form which is this orientation invariant position
673 // in the hotseat
674 if (context instanceof Launcher && screen < 0 &&
675 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700676 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 item.cellY);
678 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700679 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700680 }
681
682 final ContentValues values = new ContentValues();
683 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
684 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
685 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687
688 contentValues.add(values);
689 }
690 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
691 }
692
693 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700694 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800695 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700696 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700697 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700698 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800699 item.cellX = cellX;
700 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 item.spanX = spanX;
702 item.spanY = spanY;
703
704 // We store hotseat items in canonical form which is this orientation invariant position
705 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700711 }
Adam Cohend4844c32011-02-18 19:25:06 -0800712
Adam Cohend4844c32011-02-18 19:25:06 -0800713 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800714 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
716 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
717 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
718 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800720
Michael Jurka816474f2012-06-25 14:49:02 -0700721 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700723
724 /**
725 * Update an item to the database in a specified container.
726 */
727 static void updateItemInDatabase(Context context, final ItemInfo item) {
728 final ContentValues values = new ContentValues();
729 item.onAddToDatabase(values);
730 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
731 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800732 }
733
734 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 * Returns true if the shortcuts already exists in the database.
736 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 static boolean shortcutExists(Context context, String title, Intent intent) {
739 final ContentResolver cr = context.getContentResolver();
740 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
741 new String[] { "title", "intent" }, "title=? and intent=?",
742 new String[] { title, intent.toUri(0) }, null);
743 boolean result = false;
744 try {
745 result = c.moveToFirst();
746 } finally {
747 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400749 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700750 }
751
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 * Returns an ItemInfo array containing all the items in the LauncherModel.
754 * The ItemInfo.id is not set through this function.
755 */
756 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
757 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
758 final ContentResolver cr = context.getContentResolver();
759 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
760 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
761 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
762 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
763
764 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
765 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
766 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
767 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
768 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
769 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
770 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
771
772 try {
773 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700774 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700775 item.cellX = c.getInt(cellXIndex);
776 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700777 item.spanX = Math.max(1, c.getInt(spanXIndex));
778 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 item.container = c.getInt(containerIndex);
780 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700781 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
783 items.add(item);
784 }
785 } catch (Exception e) {
786 items.clear();
787 } finally {
788 c.close();
789 }
790
791 return items;
792 }
793
794 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400795 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
796 */
797 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
798 final ContentResolver cr = context.getContentResolver();
799 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
800 "_id=? and (itemType=? or itemType=?)",
801 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700802 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700803
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804 try {
805 if (c.moveToFirst()) {
806 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
807 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
808 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
809 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
810 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
811 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 FolderInfo folderInfo = null;
814 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700815 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
816 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700818 }
819
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 folderInfo.title = c.getString(titleIndex);
821 folderInfo.id = id;
822 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700824 folderInfo.cellX = c.getInt(cellXIndex);
825 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826
827 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 } finally {
830 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700831 }
832
833 return null;
834 }
835
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 /**
837 * Add an item to the database in a specified container. Sets the container, screen, cellX and
838 * cellY fields of the item. Also assigns an ID to the item.
839 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700840 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700841 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 item.cellX = cellX;
844 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 // We store hotseat items in canonical form which is this orientation invariant position
846 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853
854 final ContentValues values = new ContentValues();
855 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 item.onAddToDatabase(values);
857
Michael Jurka414300a2013-08-27 15:42:35 +0200858 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700859 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700861
Michael Jurkac9d95c52011-08-29 14:03:34 -0700862 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700863 public void run() {
864 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
865 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700867 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700868 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700869 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700870 sBgItemsIdMap.put(item.id, item);
871 switch (item.itemType) {
872 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
873 sBgFolders.put(item.id, (FolderInfo) item);
874 // Fall through
875 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
876 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
877 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
878 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
879 sBgWorkspaceItems.add(item);
880 } else {
881 if (!sBgFolders.containsKey(item.container)) {
882 // Adding an item to a folder that doesn't exist.
883 String msg = "adding item: " + item + " to a folder that " +
884 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700885 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700888 break;
889 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
890 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
891 break;
892 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700893 }
894 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700895 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700896 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700897 }
898
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700900 * Creates a new unique child id, for a given cell span across all layouts.
901 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700902 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700904 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 }
907
Winson Chungaafa03c2010-06-11 17:34:16 -0700908 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700909 * Removes the specified item from the database
910 * @param context
911 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700915 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700916
Michael Jurka83df1882011-08-31 20:59:26 -0700917 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700918 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700919 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700920
921 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700922 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700923 switch (item.itemType) {
924 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
925 sBgFolders.remove(item.id);
926 for (ItemInfo info: sBgItemsIdMap.values()) {
927 if (info.container == item.id) {
928 // We are deleting a folder which still contains items that
929 // think they are contained by that folder.
930 String msg = "deleting a folder (" + item + ") which still " +
931 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700932 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700934 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700935 sBgWorkspaceItems.remove(item);
936 break;
937 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
938 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
939 sBgWorkspaceItems.remove(item);
940 break;
941 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
942 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
943 break;
944 }
945 sBgItemsIdMap.remove(item.id);
946 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700947 }
948 }
Michael Jurka83df1882011-08-31 20:59:26 -0700949 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700950 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 }
952
953 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 * Update the order of the workspace screens in the database. The array list contains
955 * a list of screen ids in the order that they should appear.
956 */
Winson Chungc9168342013-06-26 14:54:55 -0700957 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700958 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 final ContentResolver cr = context.getContentResolver();
960 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
961
962 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700963 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 while (iter.hasNext()) {
965 long id = iter.next();
966 if (id < 0) {
967 iter.remove();
968 }
969 }
970
971 Runnable r = new Runnable() {
972 @Override
973 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700974 // Clear the table
975 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700976 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 ContentValues[] values = new ContentValues[count];
978 for (int i = 0; i < count; i++) {
979 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700980 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
982 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 values[i] = v;
984 }
985 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700986
Winson Chungba9c37f2013-08-30 14:11:37 -0700987 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700988 sBgWorkspaceScreens.clear();
989 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700990 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 }
992 };
993 runOnWorkerThread(r);
994 }
995
996 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 * Remove the contents of the specified folder from the database
998 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 final ContentResolver cr = context.getContentResolver();
1001
Michael Jurkac9d95c52011-08-29 14:03:34 -07001002 Runnable r = new Runnable() {
1003 public void run() {
1004 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001006 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001007 sBgItemsIdMap.remove(info.id);
1008 sBgFolders.remove(info.id);
1009 sBgDbIconCache.remove(info);
1010 sBgWorkspaceItems.remove(info);
1011 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001012
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1014 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001016 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001017 for (ItemInfo childInfo : info.contents) {
1018 sBgItemsIdMap.remove(childInfo.id);
1019 sBgDbIconCache.remove(childInfo);
1020 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001021 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 }
1023 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 }
1026
1027 /**
1028 * Set this as the current Launcher activity object for the loader.
1029 */
1030 public void initialize(Callbacks callbacks) {
1031 synchronized (mLock) {
1032 mCallbacks = new WeakReference<Callbacks>(callbacks);
1033 }
1034 }
1035
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001036 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001037 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1038 * ACTION_PACKAGE_CHANGED.
1039 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001040 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001041 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001042 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001043
Joe Onorato36115782010-06-17 13:28:48 -04001044 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001045
Joe Onorato36115782010-06-17 13:28:48 -04001046 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1047 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1048 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1049 final String packageName = intent.getData().getSchemeSpecificPart();
1050 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051
Joe Onorato36115782010-06-17 13:28:48 -04001052 int op = PackageUpdatedTask.OP_NONE;
1053
1054 if (packageName == null || packageName.length() == 0) {
1055 // they sent us a bad intent
1056 return;
1057 }
1058
1059 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1060 op = PackageUpdatedTask.OP_UPDATE;
1061 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1062 if (!replacing) {
1063 op = PackageUpdatedTask.OP_REMOVE;
1064 }
1065 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1066 // later, we will update the package at this time
1067 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1068 if (!replacing) {
1069 op = PackageUpdatedTask.OP_ADD;
1070 } else {
1071 op = PackageUpdatedTask.OP_UPDATE;
1072 }
1073 }
1074
1075 if (op != PackageUpdatedTask.OP_NONE) {
1076 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1077 }
1078
1079 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001080 // First, schedule to add these apps back in.
1081 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1082 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1083 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001084 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001085 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1086 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1087 enqueuePackageUpdated(new PackageUpdatedTask(
1088 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001089 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001090 // If we have changed locale we need to clear out the labels in all apps/workspace.
1091 forceReload();
1092 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1093 // Check if configuration change was an mcc/mnc change which would affect app resources
1094 // and we would need to clear out the labels in all apps/workspace. Same handling as
1095 // above for ACTION_LOCALE_CHANGED
1096 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001097 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001098 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001099 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001100 forceReload();
1101 }
1102 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001103 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001104 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1105 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001106 if (mCallbacks != null) {
1107 Callbacks callbacks = mCallbacks.get();
1108 if (callbacks != null) {
1109 callbacks.bindSearchablesChanged();
1110 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001111 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001112 }
1113 }
1114
Reena Lee93f824a2011-09-23 17:20:28 -07001115 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001116 resetLoadedState(true, true);
1117
Reena Lee93f824a2011-09-23 17:20:28 -07001118 // Do this here because if the launcher activity is running it will be restarted.
1119 // If it's not running startLoaderFromBackground will merely tell it that it needs
1120 // to reload.
1121 startLoaderFromBackground();
1122 }
1123
Winson Chungf0c6ae02012-03-21 16:10:31 -07001124 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1125 synchronized (mLock) {
1126 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1127 // mWorkspaceLoaded to true later
1128 stopLoaderLocked();
1129 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1130 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1131 }
1132 }
1133
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001134 /**
1135 * When the launcher is in the background, it's possible for it to miss paired
1136 * configuration changes. So whenever we trigger the loader from the background
1137 * tell the launcher that it needs to re-run the loader when it comes back instead
1138 * of doing it now.
1139 */
1140 public void startLoaderFromBackground() {
1141 boolean runLoader = false;
1142 if (mCallbacks != null) {
1143 Callbacks callbacks = mCallbacks.get();
1144 if (callbacks != null) {
1145 // Only actually run the loader if they're not paused.
1146 if (!callbacks.setLoadOnResume()) {
1147 runLoader = true;
1148 }
1149 }
1150 }
1151 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001152 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001153 }
Joe Onorato36115782010-06-17 13:28:48 -04001154 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001155
Reena Lee93f824a2011-09-23 17:20:28 -07001156 // If there is already a loader task running, tell it to stop.
1157 // returns true if isLaunching() was true on the old task
1158 private boolean stopLoaderLocked() {
1159 boolean isLaunching = false;
1160 LoaderTask oldTask = mLoaderTask;
1161 if (oldTask != null) {
1162 if (oldTask.isLaunching()) {
1163 isLaunching = true;
1164 }
1165 oldTask.stopLocked();
1166 }
1167 return isLaunching;
1168 }
1169
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001170 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001171 synchronized (mLock) {
1172 if (DEBUG_LOADERS) {
1173 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001176 // Clear any deferred bind-runnables from the synchronized load process
1177 // We must do this before any loading/binding is scheduled below.
1178 mDeferredBindRunnables.clear();
1179
Joe Onorato36115782010-06-17 13:28:48 -04001180 // Don't bother to start the thread if we know it's not going to do anything
1181 if (mCallbacks != null && mCallbacks.get() != null) {
1182 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001183 // also, don't downgrade isLaunching if we're already running
1184 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001185 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001186 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1187 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1188 } else {
1189 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1190 sWorker.post(mLoaderTask);
1191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 }
1194 }
1195
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001196 void bindRemainingSynchronousPages() {
1197 // Post the remaining side pages to be loaded
1198 if (!mDeferredBindRunnables.isEmpty()) {
1199 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001200 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 }
1202 mDeferredBindRunnables.clear();
1203 }
1204 }
1205
Joe Onorato36115782010-06-17 13:28:48 -04001206 public void stopLoader() {
1207 synchronized (mLock) {
1208 if (mLoaderTask != null) {
1209 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 }
1211 }
Joe Onorato36115782010-06-17 13:28:48 -04001212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213
Winson Chung76828c82013-08-19 15:43:29 -07001214 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1215 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1216 final ContentResolver contentResolver = context.getContentResolver();
1217 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1218 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1219 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1220
1221 try {
1222 final int idIndex = sc.getColumnIndexOrThrow(
1223 LauncherSettings.WorkspaceScreens._ID);
1224 final int rankIndex = sc.getColumnIndexOrThrow(
1225 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1226 while (sc.moveToNext()) {
1227 try {
1228 long screenId = sc.getLong(idIndex);
1229 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001230 orderedScreens.put(rank, screenId);
1231 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001232 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001233 }
1234 }
1235 } finally {
1236 sc.close();
1237 }
1238 return orderedScreens;
1239 }
1240
Michael Jurkac57b7a82011-08-09 22:02:20 -07001241 public boolean isAllAppsLoaded() {
1242 return mAllAppsLoaded;
1243 }
1244
Winson Chung36a62fe2012-05-06 18:04:42 -07001245 boolean isLoadingWorkspace() {
1246 synchronized (mLock) {
1247 if (mLoaderTask != null) {
1248 return mLoaderTask.isLoadingWorkspace();
1249 }
1250 }
1251 return false;
1252 }
1253
Joe Onorato36115782010-06-17 13:28:48 -04001254 /**
1255 * Runnable for the thread that loads the contents of the launcher:
1256 * - workspace icons
1257 * - widgets
1258 * - all apps icons
1259 */
1260 private class LoaderTask implements Runnable {
1261 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001262 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001263 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001264 private boolean mStopped;
1265 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001266
Winson Chungc3eecff2011-07-11 17:44:15 -07001267 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001268
1269 LoaderTask(Context context, boolean isLaunching) {
1270 mContext = context;
1271 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001272 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 }
1274
Joe Onorato36115782010-06-17 13:28:48 -04001275 boolean isLaunching() {
1276 return mIsLaunching;
1277 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278
Winson Chung36a62fe2012-05-06 18:04:42 -07001279 boolean isLoadingWorkspace() {
1280 return mIsLoadingAndBindingWorkspace;
1281 }
1282
Winson Chungc763c4e2013-07-19 13:49:06 -07001283 /** Returns whether this is an upgrade path */
1284 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001285 mIsLoadingAndBindingWorkspace = true;
1286
Joe Onorato36115782010-06-17 13:28:48 -04001287 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001288 if (DEBUG_LOADERS) {
1289 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001291
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001293 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001294 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001295 synchronized (LoaderTask.this) {
1296 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001297 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001298 }
1299 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001300 }
1301 }
1302
Joe Onorato36115782010-06-17 13:28:48 -04001303 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001304 bindWorkspace(-1, isUpgradePath);
1305 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001306 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001307
Joe Onorato36115782010-06-17 13:28:48 -04001308 private void waitForIdle() {
1309 // Wait until the either we're stopped or the other threads are done.
1310 // This way we don't start loading all apps until the workspace has settled
1311 // down.
1312 synchronized (LoaderTask.this) {
1313 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001314
Joe Onorato36115782010-06-17 13:28:48 -04001315 mHandler.postIdle(new Runnable() {
1316 public void run() {
1317 synchronized (LoaderTask.this) {
1318 mLoadAndBindStepFinished = true;
1319 if (DEBUG_LOADERS) {
1320 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001321 }
Joe Onorato36115782010-06-17 13:28:48 -04001322 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001323 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001324 }
Joe Onorato36115782010-06-17 13:28:48 -04001325 });
1326
Michael Jurkac7700af2013-05-14 20:17:58 +02001327 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001328 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001329 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1330 // wait no longer than 1sec at a time
1331 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001332 } catch (InterruptedException ex) {
1333 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001334 }
1335 }
Joe Onorato36115782010-06-17 13:28:48 -04001336 if (DEBUG_LOADERS) {
1337 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001338 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001339 + "ms for previous step to finish binding");
1340 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Joe Onorato36115782010-06-17 13:28:48 -04001342 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001343
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001344 void runBindSynchronousPage(int synchronousBindPage) {
1345 if (synchronousBindPage < 0) {
1346 // Ensure that we have a valid page index to load synchronously
1347 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1348 "valid page index");
1349 }
1350 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1351 // Ensure that we don't try and bind a specified page when the pages have not been
1352 // loaded already (we should load everything asynchronously in that case)
1353 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1354 }
1355 synchronized (mLock) {
1356 if (mIsLoaderTaskRunning) {
1357 // Ensure that we are never running the background loading at this point since
1358 // we also touch the background collections
1359 throw new RuntimeException("Error! Background loading is already running");
1360 }
1361 }
1362
1363 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1364 // data structures, we can't allow any other thread to touch that data, but because
1365 // this call is synchronous, we can get away with not locking).
1366
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001367 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001368 // operations from the previous activity. We need to ensure that all queued operations
1369 // are executed before any synchronous binding work is done.
1370 mHandler.flush();
1371
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001372 // Divide the set of loaded items into those that we are binding synchronously, and
1373 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001374 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001375 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1376 // arise from that.
1377 onlyBindAllApps();
1378 }
1379
Joe Onorato36115782010-06-17 13:28:48 -04001380 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001381 boolean isUpgrade = false;
1382
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001383 synchronized (mLock) {
1384 mIsLoaderTaskRunning = true;
1385 }
Joe Onorato36115782010-06-17 13:28:48 -04001386 // Optimize for end-user experience: if the Launcher is up and // running with the
1387 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1388 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001389 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001390 // Elevate priority when Home launches for the first time to avoid
1391 // starving at boot time. Staring at a blank home is not cool.
1392 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001393 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1394 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001395 android.os.Process.setThreadPriority(mIsLaunching
1396 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1397 }
Winson Chung64359a52013-07-08 17:17:08 -07001398 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001399 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001400
Joe Onorato36115782010-06-17 13:28:48 -04001401 if (mStopped) {
1402 break keep_running;
1403 }
1404
1405 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1406 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 synchronized (mLock) {
1408 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001409 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001410 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1411 }
1412 }
Joe Onorato36115782010-06-17 13:28:48 -04001413 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001414
1415 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001416 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1417 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001418
1419 // Restore the default thread priority after we are done loading items
1420 synchronized (mLock) {
1421 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1422 }
Joe Onorato36115782010-06-17 13:28:48 -04001423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001424
Winson Chungaac01e12011-08-17 10:37:13 -07001425 // Update the saved icons if necessary
1426 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001427 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001428 for (Object key : sBgDbIconCache.keySet()) {
1429 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1430 }
1431 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001432 }
Winson Chungaac01e12011-08-17 10:37:13 -07001433
Winson Chungc58497e2013-09-03 17:48:37 -07001434 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1435 // Ensure that all the applications that are in the system are
1436 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001437 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001438 verifyApplications();
1439 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001440 }
1441
Joe Onorato36115782010-06-17 13:28:48 -04001442 // Clear out this reference, otherwise we end up holding it until all of the
1443 // callback runnables are done.
1444 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445
Joe Onorato36115782010-06-17 13:28:48 -04001446 synchronized (mLock) {
1447 // If we are still the last one to be scheduled, remove ourselves.
1448 if (mLoaderTask == this) {
1449 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001451 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001452 }
Joe Onorato36115782010-06-17 13:28:48 -04001453 }
1454
1455 public void stopLocked() {
1456 synchronized (LoaderTask.this) {
1457 mStopped = true;
1458 this.notify();
1459 }
1460 }
1461
1462 /**
1463 * Gets the callbacks object. If we've been stopped, or if the launcher object
1464 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1465 * object that was around when the deferred message was scheduled, and if there's
1466 * a new Callbacks object around then also return null. This will save us from
1467 * calling onto it with data that will be ignored.
1468 */
1469 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1470 synchronized (mLock) {
1471 if (mStopped) {
1472 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001473 }
Joe Onorato36115782010-06-17 13:28:48 -04001474
1475 if (mCallbacks == null) {
1476 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001477 }
Joe Onorato36115782010-06-17 13:28:48 -04001478
1479 final Callbacks callbacks = mCallbacks.get();
1480 if (callbacks != oldCallbacks) {
1481 return null;
1482 }
1483 if (callbacks == null) {
1484 Log.w(TAG, "no mCallbacks");
1485 return null;
1486 }
1487
1488 return callbacks;
1489 }
1490 }
1491
Winson Chungc763c4e2013-07-19 13:49:06 -07001492 private void verifyApplications() {
1493 final Context context = mApp.getContext();
1494
1495 // Cross reference all the applications in our apps list with items in the workspace
1496 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001497 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001498 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001499 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001500 tmpInfos = getItemInfoForComponentName(app.componentName);
1501 if (tmpInfos.isEmpty()) {
1502 // We are missing an application icon, so add this to the workspace
1503 added.add(app);
1504 // This is a rare event, so lets log it
1505 Log.e(TAG, "Missing Application on load: " + app);
1506 }
1507 }
1508 }
1509 if (!added.isEmpty()) {
1510 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001511 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001512 }
1513 }
1514
Winson Chung5f8afe62013-08-12 16:19:28 -07001515 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001516 LauncherAppState app = LauncherAppState.getInstance();
1517 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1518 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1519 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001520 }
1521
Joe Onorato36115782010-06-17 13:28:48 -04001522 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001523 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1524 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001525 LauncherAppState app = LauncherAppState.getInstance();
1526 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1527 int countX = (int) grid.numColumns;
1528 int countY = (int) grid.numRows;
1529
Adam Cohendcd297f2013-06-18 13:13:40 -07001530 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001531 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001532 // Return early if we detect that an item is under the hotseat button
1533 if (mCallbacks == null ||
1534 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1535 deleteOnItemOverlap.set(true);
1536 return false;
1537 }
1538
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1540 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1541 [(int) item.screenId][0] != null) {
1542 Log.e(TAG, "Error loading shortcut into hotseat " + item
1543 + " into position (" + item.screenId + ":" + item.cellX + ","
1544 + item.cellY + ") occupied by "
1545 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1546 [(int) item.screenId][0]);
1547 return false;
1548 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001549 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001550 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 items[(int) item.screenId][0] = item;
1552 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001553 return true;
1554 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001555 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1556 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001557 return true;
1558 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001561 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 occupied.put(item.screenId, items);
1563 }
1564
1565 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001566 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001567 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1568 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001570 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001572 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001573 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001575 return false;
1576 }
1577 }
1578 }
1579 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1580 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001581 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001582 }
1583 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001584
Joe Onorato36115782010-06-17 13:28:48 -04001585 return true;
1586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001587
Winson Chungba9c37f2013-08-30 14:11:37 -07001588 /** Clears all the sBg data structures */
1589 private void clearSBgDataStructures() {
1590 synchronized (sBgLock) {
1591 sBgWorkspaceItems.clear();
1592 sBgAppWidgets.clear();
1593 sBgFolders.clear();
1594 sBgItemsIdMap.clear();
1595 sBgDbIconCache.clear();
1596 sBgWorkspaceScreens.clear();
1597 }
1598 }
1599
Winson Chungc763c4e2013-07-19 13:49:06 -07001600 /** Returns whether this is an upgradge path */
1601 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001602 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603
Joe Onorato36115782010-06-17 13:28:48 -04001604 final Context context = mContext;
1605 final ContentResolver contentResolver = context.getContentResolver();
1606 final PackageManager manager = context.getPackageManager();
1607 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1608 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001609
Winson Chung892c74d2013-08-22 16:15:50 -07001610 LauncherAppState app = LauncherAppState.getInstance();
1611 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1612 int countX = (int) grid.numColumns;
1613 int countY = (int) grid.numRows;
1614
Michael Jurkab85f8a42012-04-25 15:48:32 -07001615 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001616 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001617
Winson Chungc763c4e2013-07-19 13:49:06 -07001618 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001619 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001620
Winson Chung2abf94d2012-07-18 18:16:38 -07001621 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001622 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001623
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001624 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001625 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001626 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001627
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001628 // +1 for the hotseat (it can be larger than the workspace)
1629 // Load workspace in reverse order to ensure that latest items are loaded first (and
1630 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001631 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001632
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001633 try {
1634 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1635 final int intentIndex = c.getColumnIndexOrThrow
1636 (LauncherSettings.Favorites.INTENT);
1637 final int titleIndex = c.getColumnIndexOrThrow
1638 (LauncherSettings.Favorites.TITLE);
1639 final int iconTypeIndex = c.getColumnIndexOrThrow(
1640 LauncherSettings.Favorites.ICON_TYPE);
1641 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1642 final int iconPackageIndex = c.getColumnIndexOrThrow(
1643 LauncherSettings.Favorites.ICON_PACKAGE);
1644 final int iconResourceIndex = c.getColumnIndexOrThrow(
1645 LauncherSettings.Favorites.ICON_RESOURCE);
1646 final int containerIndex = c.getColumnIndexOrThrow(
1647 LauncherSettings.Favorites.CONTAINER);
1648 final int itemTypeIndex = c.getColumnIndexOrThrow(
1649 LauncherSettings.Favorites.ITEM_TYPE);
1650 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001652 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001654 final int screenIndex = c.getColumnIndexOrThrow(
1655 LauncherSettings.Favorites.SCREEN);
1656 final int cellXIndex = c.getColumnIndexOrThrow
1657 (LauncherSettings.Favorites.CELLX);
1658 final int cellYIndex = c.getColumnIndexOrThrow
1659 (LauncherSettings.Favorites.CELLY);
1660 final int spanXIndex = c.getColumnIndexOrThrow
1661 (LauncherSettings.Favorites.SPANX);
1662 final int spanYIndex = c.getColumnIndexOrThrow(
1663 LauncherSettings.Favorites.SPANY);
1664 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1665 //final int displayModeIndex = c.getColumnIndexOrThrow(
1666 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001668 ShortcutInfo info;
1669 String intentDescription;
1670 LauncherAppWidgetInfo appWidgetInfo;
1671 int container;
1672 long id;
1673 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001674
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001676 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001677 try {
1678 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 switch (itemType) {
1681 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1682 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001683 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001684 intentDescription = c.getString(intentIndex);
1685 try {
1686 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001687 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001688 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001689 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001690 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001691 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1692 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001693 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001694 // If apps can be on external storage, then we just
1695 // leave them for the user to remove (maybe add
1696 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001697 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001698 }
1699 continue;
1700 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001702 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 continue;
1704 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1707 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1708 titleIndex, mLabelCache);
1709 } else {
1710 info = getShortcutInfo(c, context, iconTypeIndex,
1711 iconPackageIndex, iconResourceIndex, iconIndex,
1712 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001713
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 // App shortcuts that used to be automatically added to Launcher
1715 // didn't always have the correct intent flags set, so do that
1716 // here
1717 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001718 intent.getCategories() != null &&
1719 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001720 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001721 intent.addFlags(
1722 Intent.FLAG_ACTIVITY_NEW_TASK |
1723 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1724 }
Michael Jurka96879562012-03-22 05:54:33 -07001725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001726
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001728 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 container = c.getInt(containerIndex);
1731 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001733 info.cellX = c.getInt(cellXIndex);
1734 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001735 info.spanX = 1;
1736 info.spanY = 1;
1737 // Skip loading items that are out of bounds
1738 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1739 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001740 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1741 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001742 continue;
1743 }
1744 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001745 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001746 deleteOnItemOverlap.set(false);
1747 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1748 if (deleteOnItemOverlap.get()) {
1749 itemsToRemove.add(id);
1750 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001751 break;
1752 }
1753
1754 switch (container) {
1755 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1756 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1757 sBgWorkspaceItems.add(info);
1758 break;
1759 default:
1760 // Item is in a user folder
1761 FolderInfo folderInfo =
1762 findOrMakeFolder(sBgFolders, container);
1763 folderInfo.add(info);
1764 break;
1765 }
1766 sBgItemsIdMap.put(info.id, info);
1767
1768 // now that we've loaded everthing re-save it with the
1769 // icon in case it disappears somehow.
1770 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001771 } else {
1772 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001773 }
1774 break;
1775
1776 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1777 id = c.getLong(idIndex);
1778 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1779
1780 folderInfo.title = c.getString(titleIndex);
1781 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001782 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001784 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 folderInfo.cellX = c.getInt(cellXIndex);
1786 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001787 folderInfo.spanX = 1;
1788 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789
Winson Chung5f8afe62013-08-12 16:19:28 -07001790 // Skip loading items that are out of bounds
1791 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001792 if (checkItemDimensions(folderInfo)) {
1793 Log.d(TAG, "Skipped loading out of bounds folder");
1794 continue;
1795 }
1796 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001797 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001798 deleteOnItemOverlap.set(false);
1799 if (!checkItemPlacement(occupied, folderInfo,
1800 deleteOnItemOverlap)) {
1801 if (deleteOnItemOverlap.get()) {
1802 itemsToRemove.add(id);
1803 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001804 break;
1805 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001806
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1809 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1810 sBgWorkspaceItems.add(folderInfo);
1811 break;
Joe Onorato36115782010-06-17 13:28:48 -04001812 }
Joe Onorato17a89222011-02-08 17:26:11 -08001813
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001814 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1815 sBgFolders.put(folderInfo.id, folderInfo);
1816 break;
1817
1818 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1819 // Read all Launcher-specific widget details
1820 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001821 String savedProvider = c.getString(appWidgetProviderIndex);
1822
Joe Onorato36115782010-06-17 13:28:48 -04001823 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001824
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001825 final AppWidgetProviderInfo provider =
1826 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001827
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 if (!isSafeMode && (provider == null || provider.provider == null ||
1829 provider.provider.getPackageName() == null)) {
1830 String log = "Deleting widget that isn't installed anymore: id="
1831 + id + " appWidgetId=" + appWidgetId;
1832 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001833 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 itemsToRemove.add(id);
1835 } else {
1836 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1837 provider.provider);
1838 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001839 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 appWidgetInfo.cellX = c.getInt(cellXIndex);
1841 appWidgetInfo.cellY = c.getInt(cellYIndex);
1842 appWidgetInfo.spanX = c.getInt(spanXIndex);
1843 appWidgetInfo.spanY = c.getInt(spanYIndex);
1844 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1845 appWidgetInfo.minSpanX = minSpan[0];
1846 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001847
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 container = c.getInt(containerIndex);
1849 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1850 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1851 Log.e(TAG, "Widget found where container != " +
1852 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1853 continue;
1854 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001855
Adam Cohene25af792013-06-06 23:08:25 -07001856 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001857 // Skip loading items that are out of bounds
1858 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1859 if (checkItemDimensions(appWidgetInfo)) {
1860 Log.d(TAG, "Skipped loading out of bounds app widget");
1861 continue;
1862 }
1863 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001864 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001865 deleteOnItemOverlap.set(false);
1866 if (!checkItemPlacement(occupied, appWidgetInfo,
1867 deleteOnItemOverlap)) {
1868 if (deleteOnItemOverlap.get()) {
1869 itemsToRemove.add(id);
1870 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001871 break;
1872 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001873 String providerName = provider.provider.flattenToString();
1874 if (!providerName.equals(savedProvider)) {
1875 ContentValues values = new ContentValues();
1876 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1877 providerName);
1878 String where = BaseColumns._ID + "= ?";
1879 String[] args = {Integer.toString(c.getInt(idIndex))};
1880 contentResolver.update(contentUri, values, where, args);
1881 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1883 sBgAppWidgets.add(appWidgetInfo);
1884 }
Joe Onorato36115782010-06-17 13:28:48 -04001885 break;
1886 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001887 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001888 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 }
1891 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001892 if (c != null) {
1893 c.close();
1894 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001895 }
1896
Winson Chungba9c37f2013-08-30 14:11:37 -07001897 // Break early if we've stopped loading
1898 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001899 clearSBgDataStructures();
1900 return false;
1901 }
1902
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903 if (itemsToRemove.size() > 0) {
1904 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001905 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001906 // Remove dead items
1907 for (long id : itemsToRemove) {
1908 if (DEBUG_LOADERS) {
1909 Log.d(TAG, "Removed id = " + id);
1910 }
1911 // Don't notify content observers
1912 try {
1913 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1914 null, null);
1915 } catch (RemoteException e) {
1916 Log.w(TAG, "Could not remove id = " + id);
1917 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001918 }
1919 }
1920
Winson Chungc763c4e2013-07-19 13:49:06 -07001921 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 long maxScreenId = 0;
1923 // If we're importing we use the old screen order.
1924 for (ItemInfo item: sBgItemsIdMap.values()) {
1925 long screenId = item.screenId;
1926 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1927 !sBgWorkspaceScreens.contains(screenId)) {
1928 sBgWorkspaceScreens.add(screenId);
1929 if (screenId > maxScreenId) {
1930 maxScreenId = screenId;
1931 }
1932 }
1933 }
1934 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001935
Michael Jurka414300a2013-08-27 15:42:35 +02001936 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001937 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001938
1939 // Update the max item id after we load an old db
1940 long maxItemId = 0;
1941 // If we're importing we use the old screen order.
1942 for (ItemInfo item: sBgItemsIdMap.values()) {
1943 maxItemId = Math.max(maxItemId, item.id);
1944 }
Michael Jurka414300a2013-08-27 15:42:35 +02001945 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001947 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1948 for (Integer i : orderedScreens.keySet()) {
1949 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 }
1951
1952 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001953 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 for (ItemInfo item: sBgItemsIdMap.values()) {
1955 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1957 unusedScreens.contains(screenId)) {
1958 unusedScreens.remove(screenId);
1959 }
1960 }
1961
1962 // If there are any empty screens remove them, and update.
1963 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001964 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1966 }
1967 }
1968
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001969 if (DEBUG_LOADERS) {
1970 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1971 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001973 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001974 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001975
Daniel Sandler566da102013-06-25 23:43:45 -04001976 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001977 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001979 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001980 line += " | ";
1981 }
Winson Chung892c74d2013-08-22 16:15:50 -07001982 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001983 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 }
Joe Onorato36115782010-06-17 13:28:48 -04001985 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001986 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 }
Joe Onorato36115782010-06-17 13:28:48 -04001989 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001990 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001991 }
1992
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001993 /** Filters the set of items who are directly or indirectly (via another container) on the
1994 * specified screen. */
1995 private void filterCurrentWorkspaceItems(int currentScreen,
1996 ArrayList<ItemInfo> allWorkspaceItems,
1997 ArrayList<ItemInfo> currentScreenItems,
1998 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001999 // Purge any null ItemInfos
2000 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2001 while (iter.hasNext()) {
2002 ItemInfo i = iter.next();
2003 if (i == null) {
2004 iter.remove();
2005 }
2006 }
2007
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002008 // If we aren't filtering on a screen, then the set of items to load is the full set of
2009 // items given.
2010 if (currentScreen < 0) {
2011 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002012 }
2013
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 // Order the set of items by their containers first, this allows use to walk through the
2015 // list sequentially, build up a list of containers that are in the specified screen,
2016 // as well as all items in those containers.
2017 Set<Long> itemsOnScreen = new HashSet<Long>();
2018 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2019 @Override
2020 public int compare(ItemInfo lhs, ItemInfo rhs) {
2021 return (int) (lhs.container - rhs.container);
2022 }
2023 });
2024 for (ItemInfo info : allWorkspaceItems) {
2025 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002027 currentScreenItems.add(info);
2028 itemsOnScreen.add(info.id);
2029 } else {
2030 otherScreenItems.add(info);
2031 }
2032 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2033 currentScreenItems.add(info);
2034 itemsOnScreen.add(info.id);
2035 } else {
2036 if (itemsOnScreen.contains(info.container)) {
2037 currentScreenItems.add(info);
2038 itemsOnScreen.add(info.id);
2039 } else {
2040 otherScreenItems.add(info);
2041 }
2042 }
2043 }
2044 }
2045
2046 /** Filters the set of widgets which are on the specified screen. */
2047 private void filterCurrentAppWidgets(int currentScreen,
2048 ArrayList<LauncherAppWidgetInfo> appWidgets,
2049 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2050 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2051 // If we aren't filtering on a screen, then the set of items to load is the full set of
2052 // widgets given.
2053 if (currentScreen < 0) {
2054 currentScreenWidgets.addAll(appWidgets);
2055 }
2056
2057 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002058 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002061 currentScreenWidgets.add(widget);
2062 } else {
2063 otherScreenWidgets.add(widget);
2064 }
2065 }
2066 }
2067
2068 /** Filters the set of folders which are on the specified screen. */
2069 private void filterCurrentFolders(int currentScreen,
2070 HashMap<Long, ItemInfo> itemsIdMap,
2071 HashMap<Long, FolderInfo> folders,
2072 HashMap<Long, FolderInfo> currentScreenFolders,
2073 HashMap<Long, FolderInfo> otherScreenFolders) {
2074 // If we aren't filtering on a screen, then the set of items to load is the full set of
2075 // widgets given.
2076 if (currentScreen < 0) {
2077 currentScreenFolders.putAll(folders);
2078 }
2079
2080 for (long id : folders.keySet()) {
2081 ItemInfo info = itemsIdMap.get(id);
2082 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002083 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002085 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 currentScreenFolders.put(id, folder);
2087 } else {
2088 otherScreenFolders.put(id, folder);
2089 }
2090 }
2091 }
2092
2093 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2094 * right) */
2095 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002096 final LauncherAppState app = LauncherAppState.getInstance();
2097 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002098 // XXX: review this
2099 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002100 @Override
2101 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002102 int cellCountX = (int) grid.numColumns;
2103 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002104 int screenOffset = cellCountX * cellCountY;
2105 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002107 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002108 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002109 rhs.cellY * cellCountX + rhs.cellX);
2110 return (int) (lr - rr);
2111 }
2112 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002114
Adam Cohendcd297f2013-06-18 13:13:40 -07002115 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2116 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 final Runnable r = new Runnable() {
2118 @Override
2119 public void run() {
2120 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2121 if (callbacks != null) {
2122 callbacks.bindScreens(orderedScreens);
2123 }
2124 }
2125 };
2126 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2127 }
2128
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2130 final ArrayList<ItemInfo> workspaceItems,
2131 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2132 final HashMap<Long, FolderInfo> folders,
2133 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002134
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002135 final boolean postOnMainThread = (deferredBindRunnables != null);
2136
2137 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002138 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002139 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002140 final int start = i;
2141 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 final Runnable r = new Runnable() {
2143 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002145 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002147 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2148 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 }
2150 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002151 };
2152 if (postOnMainThread) {
2153 deferredBindRunnables.add(r);
2154 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002155 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 }
Joe Onorato36115782010-06-17 13:28:48 -04002157 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158
2159 // Bind the folders
2160 if (!folders.isEmpty()) {
2161 final Runnable r = new Runnable() {
2162 public void run() {
2163 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2164 if (callbacks != null) {
2165 callbacks.bindFolders(folders);
2166 }
2167 }
2168 };
2169 if (postOnMainThread) {
2170 deferredBindRunnables.add(r);
2171 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002172 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002173 }
2174 }
2175
2176 // Bind the widgets, one at a time
2177 N = appWidgets.size();
2178 for (int i = 0; i < N; i++) {
2179 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2180 final Runnable r = new Runnable() {
2181 public void run() {
2182 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2183 if (callbacks != null) {
2184 callbacks.bindAppWidget(widget);
2185 }
2186 }
2187 };
2188 if (postOnMainThread) {
2189 deferredBindRunnables.add(r);
2190 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002191 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 }
2193 }
2194 }
2195
2196 /**
2197 * Binds all loaded data to actual views on the main thread.
2198 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002199 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002200 final long t = SystemClock.uptimeMillis();
2201 Runnable r;
2202
2203 // Don't use these two variables in any of the callback runnables.
2204 // Otherwise we hold a reference to them.
2205 final Callbacks oldCallbacks = mCallbacks.get();
2206 if (oldCallbacks == null) {
2207 // This launcher has exited and nobody bothered to tell us. Just bail.
2208 Log.w(TAG, "LoaderTask running with no launcher");
2209 return;
2210 }
2211
Winson Chung4a2afa32012-07-19 14:53:05 -07002212 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2213 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002214 oldCallbacks.getCurrentWorkspaceScreen();
2215
2216 // Load all the items that are on the current page first (and in the process, unbind
2217 // all the existing workspace items before we call startBinding() below.
2218 unbindWorkspaceItemsOnMainThread();
2219 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2220 ArrayList<LauncherAppWidgetInfo> appWidgets =
2221 new ArrayList<LauncherAppWidgetInfo>();
2222 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2223 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002225 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002226 workspaceItems.addAll(sBgWorkspaceItems);
2227 appWidgets.addAll(sBgAppWidgets);
2228 folders.putAll(sBgFolders);
2229 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002231 }
2232
2233 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2234 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2235 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2238 new ArrayList<LauncherAppWidgetInfo>();
2239 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2240 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2241
2242 // Separate the items that are on the current screen, and all the other remaining items
2243 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2244 otherWorkspaceItems);
2245 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2246 otherAppWidgets);
2247 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2248 otherFolders);
2249 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2250 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2251
2252 // Tell the workspace that we're about to start binding items
2253 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002254 public void run() {
2255 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2256 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002257 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002258 }
2259 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260 };
Winson Chung81b52252012-08-27 15:34:29 -07002261 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002262
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2264
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265 // Load items on the current page
2266 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2267 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002268 if (isLoadingSynchronously) {
2269 r = new Runnable() {
2270 public void run() {
2271 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2272 if (callbacks != null) {
2273 callbacks.onPageBoundSynchronously(currentScreen);
2274 }
2275 }
2276 };
Winson Chung81b52252012-08-27 15:34:29 -07002277 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002278 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002279
Winson Chung4a2afa32012-07-19 14:53:05 -07002280 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2281 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 mDeferredBindRunnables.clear();
2283 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002284 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002285
2286 // Tell the workspace that we're done binding items
2287 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002288 public void run() {
2289 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2290 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002291 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002292 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293
Winson Chung98e030b2012-05-07 16:01:11 -07002294 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002295 if (DEBUG_LOADERS) {
2296 Log.d(TAG, "bound workspace in "
2297 + (SystemClock.uptimeMillis()-t) + "ms");
2298 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002299
2300 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002301 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002302 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002303 if (isLoadingSynchronously) {
2304 mDeferredBindRunnables.add(r);
2305 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002306 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002307 }
Joe Onorato36115782010-06-17 13:28:48 -04002308 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002309
Joe Onorato36115782010-06-17 13:28:48 -04002310 private void loadAndBindAllApps() {
2311 if (DEBUG_LOADERS) {
2312 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2313 }
2314 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002315 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002316 synchronized (LoaderTask.this) {
2317 if (mStopped) {
2318 return;
2319 }
2320 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002321 }
Joe Onorato36115782010-06-17 13:28:48 -04002322 } else {
2323 onlyBindAllApps();
2324 }
2325 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002326
Joe Onorato36115782010-06-17 13:28:48 -04002327 private void onlyBindAllApps() {
2328 final Callbacks oldCallbacks = mCallbacks.get();
2329 if (oldCallbacks == null) {
2330 // This launcher has exited and nobody bothered to tell us. Just bail.
2331 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2332 return;
2333 }
2334
2335 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002336 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002337 final ArrayList<AppInfo> list
2338 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002339 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002340 public void run() {
2341 final long t = SystemClock.uptimeMillis();
2342 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2343 if (callbacks != null) {
2344 callbacks.bindAllApplications(list);
2345 }
2346 if (DEBUG_LOADERS) {
2347 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2348 + (SystemClock.uptimeMillis()-t) + "ms");
2349 }
2350 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002351 };
2352 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002353 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002354 r.run();
2355 } else {
2356 mHandler.post(r);
2357 }
Joe Onorato36115782010-06-17 13:28:48 -04002358 }
2359
Winson Chung64359a52013-07-08 17:17:08 -07002360 private void loadAllApps() {
2361 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002362
Joe Onorato36115782010-06-17 13:28:48 -04002363 final Callbacks oldCallbacks = mCallbacks.get();
2364 if (oldCallbacks == null) {
2365 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002366 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002367 return;
2368 }
2369
Winson Chung64359a52013-07-08 17:17:08 -07002370 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002371 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2372 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2373
Winson Chung64359a52013-07-08 17:17:08 -07002374 // Clear the list of apps
2375 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002376
Winson Chung64359a52013-07-08 17:17:08 -07002377 // Query for the set of apps
2378 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2379 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2380 if (DEBUG_LOADERS) {
2381 Log.d(TAG, "queryIntentActivities took "
2382 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2383 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2384 }
2385 // Fail if we don't have any apps
2386 if (apps == null || apps.isEmpty()) {
2387 return;
2388 }
2389 // Sort the applications by name
2390 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2391 Collections.sort(apps,
2392 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2393 if (DEBUG_LOADERS) {
2394 Log.d(TAG, "sort took "
2395 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 }
2397
Winson Chung64359a52013-07-08 17:17:08 -07002398 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002399 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002400 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002401 // This builds the icon bitmaps.
2402 mBgAllAppsList.add(new AppInfo(packageManager, app,
2403 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002404 }
2405
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002406 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002407 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2408 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002409
2410 // Post callback on main thread
2411 mHandler.post(new Runnable() {
2412 public void run() {
2413 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002414 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002415 if (callbacks != null) {
2416 callbacks.bindAllApplications(added);
2417 if (DEBUG_LOADERS) {
2418 Log.d(TAG, "bound " + added.size() + " apps in "
2419 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2420 }
2421 } else {
2422 Log.i(TAG, "not binding apps: no Launcher activity");
2423 }
2424 }
2425 });
2426
Joe Onorato36115782010-06-17 13:28:48 -04002427 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002428 Log.d(TAG, "Icons processed in "
2429 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002430 }
2431 }
2432
2433 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002434 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002435 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2436 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2437 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2438 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2439 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2440 }
Joe Onorato36115782010-06-17 13:28:48 -04002441 }
2442 }
2443
2444 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002445 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002446 }
2447
2448 private class PackageUpdatedTask implements Runnable {
2449 int mOp;
2450 String[] mPackages;
2451
2452 public static final int OP_NONE = 0;
2453 public static final int OP_ADD = 1;
2454 public static final int OP_UPDATE = 2;
2455 public static final int OP_REMOVE = 3; // uninstlled
2456 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2457
2458
2459 public PackageUpdatedTask(int op, String[] packages) {
2460 mOp = op;
2461 mPackages = packages;
2462 }
2463
2464 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002465 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002466
2467 final String[] packages = mPackages;
2468 final int N = packages.length;
2469 switch (mOp) {
2470 case OP_ADD:
2471 for (int i=0; i<N; i++) {
2472 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002473 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002474 }
2475 break;
2476 case OP_UPDATE:
2477 for (int i=0; i<N; i++) {
2478 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002479 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002480 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002481 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002482 }
2483 break;
2484 case OP_REMOVE:
2485 case OP_UNAVAILABLE:
2486 for (int i=0; i<N; i++) {
2487 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002488 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002489 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002490 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002491 }
2492 break;
2493 }
2494
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002495 ArrayList<AppInfo> added = null;
2496 ArrayList<AppInfo> modified = null;
2497 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002498
Adam Cohen487f7dd2012-06-28 18:12:10 -07002499 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002500 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002501 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002502 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002504 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002505 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002506 }
Winson Chung5d55f332012-07-16 20:45:03 -07002507 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002508 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002509 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002510 }
2511
Joe Onorato36115782010-06-17 13:28:48 -04002512 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2513 if (callbacks == null) {
2514 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2515 return;
2516 }
2517
2518 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002519 // Ensure that we add all the workspace applications to the db
2520 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002521 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2522 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2523 } else {
2524 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2525 addAndBindAddedApps(context, addedInfos, cb, added);
2526 }
Joe Onorato36115782010-06-17 13:28:48 -04002527 }
2528 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002529 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002530
2531 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002532 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002533 ArrayList<ItemInfo> infos =
2534 getItemInfoForComponentName(a.componentName);
2535 for (ItemInfo i : infos) {
2536 if (isShortcutInfoUpdateable(i)) {
2537 ShortcutInfo info = (ShortcutInfo) i;
2538 info.title = a.title.toString();
2539 updateItemInDatabase(context, info);
2540 }
2541 }
2542 }
2543
Joe Onorato36115782010-06-17 13:28:48 -04002544 mHandler.post(new Runnable() {
2545 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002546 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2547 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002548 callbacks.bindAppsUpdated(modifiedFinal);
2549 }
2550 }
2551 });
2552 }
Winson Chung83892cc2013-05-01 16:53:33 -07002553 // If a package has been removed, or an app has been removed as a result of
2554 // an update (for example), make the removed callback.
2555 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002556 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002557 final ArrayList<String> removedPackageNames =
2558 new ArrayList<String>(Arrays.asList(packages));
2559
Winson Chung64359a52013-07-08 17:17:08 -07002560 // Update the launcher db to reflect the removal of apps
2561 if (packageRemoved) {
2562 for (String pn : removedPackageNames) {
2563 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2564 for (ItemInfo i : infos) {
2565 deleteItemFromDatabase(context, i);
2566 }
2567 }
Winson Chung780fe592013-09-26 14:48:44 -07002568
2569 // Remove any queued items from the install queue
2570 String spKey = LauncherAppState.getSharedPreferencesKey();
2571 SharedPreferences sp =
2572 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2573 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
Winson Chung64359a52013-07-08 17:17:08 -07002574 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002575 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002576 ArrayList<ItemInfo> infos =
2577 getItemInfoForComponentName(a.componentName);
2578 for (ItemInfo i : infos) {
2579 deleteItemFromDatabase(context, i);
2580 }
2581 }
2582 }
2583
Joe Onorato36115782010-06-17 13:28:48 -04002584 mHandler.post(new Runnable() {
2585 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002586 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2587 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002588 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002589 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002590 }
2591 }
2592 });
Joe Onoratobe386092009-11-17 17:32:16 -08002593 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002594
Michael Jurkac402cd92013-05-20 15:49:32 +02002595 final ArrayList<Object> widgetsAndShortcuts =
2596 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002597 mHandler.post(new Runnable() {
2598 @Override
2599 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002600 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2601 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002602 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002603 }
2604 }
2605 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002606
2607 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002608 mHandler.post(new Runnable() {
2609 public void run() {
2610 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2611 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002612 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002613 }
2614 }
2615 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002616 }
2617 }
2618
Michael Jurkac402cd92013-05-20 15:49:32 +02002619 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2620 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2621 PackageManager packageManager = context.getPackageManager();
2622 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2623 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2624 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2625 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2626 Collections.sort(widgetsAndShortcuts,
2627 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2628 return widgetsAndShortcuts;
2629 }
2630
Winson Chung1323b482013-08-05 12:41:55 -07002631 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002632 if (cn == null) {
2633 return false;
2634 }
2635
2636 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002637 // Skip if the application is disabled
2638 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2639 if (!pi.applicationInfo.enabled) {
2640 return false;
2641 }
2642
2643 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002644 return (pm.getActivityInfo(cn, 0) != null);
2645 } catch (NameNotFoundException e) {
2646 return false;
2647 }
2648 }
2649
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002651 * This is called from the code that adds shortcuts from the intent receiver. This
2652 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002653 */
Joe Onorato56d82912010-03-07 14:32:10 -05002654 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002655 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002656 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002657
Joe Onorato56d82912010-03-07 14:32:10 -05002658 /**
2659 * Make an ShortcutInfo object for a shortcut that is an application.
2660 *
2661 * If c is not null, then it will be used to fill in missing data like the title and icon.
2662 */
2663 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002664 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002665 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002666 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002667 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002668 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002669 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002670 } else {
2671 try {
2672 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2673 info.initFlagsAndFirstInstallTime(pi);
2674 } catch (NameNotFoundException e) {
2675 Log.d(TAG, "getPackInfo failed for package " +
2676 componentName.getPackageName());
2677 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002678 }
2679
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002680 // TODO: See if the PackageManager knows about this case. If it doesn't
2681 // then return null & delete this.
2682
Joe Onorato56d82912010-03-07 14:32:10 -05002683 // the resource -- This may implicitly give us back the fallback icon,
2684 // but don't worry about that. All we're doing with usingFallbackIcon is
2685 // to avoid saving lots of copies of that in the database, and most apps
2686 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002687
2688 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2689 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2690 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002691 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002692 ResolveInfo resolveInfo = null;
2693 ComponentName oldComponent = intent.getComponent();
2694 Intent newIntent = new Intent(intent.getAction(), null);
2695 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2696 newIntent.setPackage(oldComponent.getPackageName());
2697 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2698 for (ResolveInfo i : infos) {
2699 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2700 i.activityInfo.name);
2701 if (cn.equals(oldComponent)) {
2702 resolveInfo = i;
2703 }
2704 }
2705 if (resolveInfo == null) {
2706 resolveInfo = manager.resolveActivity(intent, 0);
2707 }
Joe Onorato56d82912010-03-07 14:32:10 -05002708 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002709 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002710 }
Joe Onorato56d82912010-03-07 14:32:10 -05002711 // the db
2712 if (icon == null) {
2713 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002714 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002715 }
2716 }
2717 // the fallback icon
2718 if (icon == null) {
2719 icon = getFallbackIcon();
2720 info.usingFallbackIcon = true;
2721 }
2722 info.setIcon(icon);
2723
2724 // from the resource
2725 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002726 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2727 if (labelCache != null && labelCache.containsKey(key)) {
2728 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002729 } else {
2730 info.title = resolveInfo.activityInfo.loadLabel(manager);
2731 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002732 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002733 }
2734 }
Joe Onorato56d82912010-03-07 14:32:10 -05002735 }
2736 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002737 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002738 if (c != null) {
2739 info.title = c.getString(titleIndex);
2740 }
2741 }
2742 // fall back to the class name of the activity
2743 if (info.title == null) {
2744 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002745 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2747 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002749
Winson Chung64359a52013-07-08 17:17:08 -07002750 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2751 ItemInfoFilter f) {
2752 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2753 for (ItemInfo i : infos) {
2754 if (i instanceof ShortcutInfo) {
2755 ShortcutInfo info = (ShortcutInfo) i;
2756 ComponentName cn = info.intent.getComponent();
2757 if (cn != null && f.filterItem(null, info, cn)) {
2758 filtered.add(info);
2759 }
2760 } else if (i instanceof FolderInfo) {
2761 FolderInfo info = (FolderInfo) i;
2762 for (ShortcutInfo s : info.contents) {
2763 ComponentName cn = s.intent.getComponent();
2764 if (cn != null && f.filterItem(info, s, cn)) {
2765 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002766 }
2767 }
Winson Chung64359a52013-07-08 17:17:08 -07002768 } else if (i instanceof LauncherAppWidgetInfo) {
2769 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2770 ComponentName cn = info.providerName;
2771 if (cn != null && f.filterItem(null, info, cn)) {
2772 filtered.add(info);
2773 }
Winson Chung8a435102012-08-30 17:16:53 -07002774 }
2775 }
Winson Chung64359a52013-07-08 17:17:08 -07002776 return new ArrayList<ItemInfo>(filtered);
2777 }
2778
2779 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002780 ItemInfoFilter filter = new ItemInfoFilter() {
2781 @Override
2782 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2783 return cn.getPackageName().equals(pn);
2784 }
2785 };
2786 return filterItemInfos(sBgItemsIdMap.values(), filter);
2787 }
2788
2789 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002790 ItemInfoFilter filter = new ItemInfoFilter() {
2791 @Override
2792 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2793 return cn.equals(cname);
2794 }
2795 };
2796 return filterItemInfos(sBgItemsIdMap.values(), filter);
2797 }
2798
2799 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2800 if (i instanceof ShortcutInfo) {
2801 ShortcutInfo info = (ShortcutInfo) i;
2802 // We need to check for ACTION_MAIN otherwise getComponent() might
2803 // return null for some shortcuts (for instance, for shortcuts to
2804 // web pages.)
2805 Intent intent = info.intent;
2806 ComponentName name = intent.getComponent();
2807 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2808 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2809 return true;
2810 }
2811 }
2812 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002813 }
2814
2815 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002816 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002818 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002819 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2820 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821
Joe Onorato56d82912010-03-07 14:32:10 -05002822 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002823 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002826 // TODO: If there's an explicit component and we can't install that, delete it.
2827
Joe Onorato56d82912010-03-07 14:32:10 -05002828 info.title = c.getString(titleIndex);
2829
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 int iconType = c.getInt(iconTypeIndex);
2831 switch (iconType) {
2832 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2833 String packageName = c.getString(iconPackageIndex);
2834 String resourceName = c.getString(iconResourceIndex);
2835 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002836 info.customIcon = false;
2837 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002840 if (resources != null) {
2841 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002842 icon = Utilities.createIconBitmap(
2843 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002844 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002846 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 }
Joe Onorato56d82912010-03-07 14:32:10 -05002848 // the db
2849 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002850 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002851 }
2852 // the fallback icon
2853 if (icon == null) {
2854 icon = getFallbackIcon();
2855 info.usingFallbackIcon = true;
2856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 break;
2858 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002859 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002860 if (icon == null) {
2861 icon = getFallbackIcon();
2862 info.customIcon = false;
2863 info.usingFallbackIcon = true;
2864 } else {
2865 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 break;
2868 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002869 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002870 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 info.customIcon = false;
2872 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002874 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 return info;
2876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877
Michael Jurka931dc972011-08-05 15:08:15 -07002878 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002879 @SuppressWarnings("all") // suppress dead code warning
2880 final boolean debug = false;
2881 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002882 Log.d(TAG, "getIconFromCursor app="
2883 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2884 }
2885 byte[] data = c.getBlob(iconIndex);
2886 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002887 return Utilities.createIconBitmap(
2888 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002889 } catch (Exception e) {
2890 return null;
2891 }
2892 }
2893
Winson Chung3d503fb2011-07-13 17:25:49 -07002894 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2895 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002896 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002897 if (info == null) {
2898 return null;
2899 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002900 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002901
2902 return info;
2903 }
2904
Winson Chunga9abd0e2010-10-27 17:18:37 -07002905 /**
Winson Chung55cef262010-10-28 14:14:18 -07002906 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2907 */
2908 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2909 ComponentName component) {
2910 List<AppWidgetProviderInfo> widgets =
2911 AppWidgetManager.getInstance(context).getInstalledProviders();
2912 for (AppWidgetProviderInfo info : widgets) {
2913 if (info.provider.equals(component)) {
2914 return info;
2915 }
2916 }
2917 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002918 }
2919
Winson Chung68846fd2010-10-29 11:00:27 -07002920 /**
2921 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2922 */
2923 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2924 final PackageManager packageManager = context.getPackageManager();
2925 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2926 new ArrayList<WidgetMimeTypeHandlerData>();
2927
2928 final Intent supportsIntent =
2929 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2930 supportsIntent.setType(mimeType);
2931
2932 // Create a set of widget configuration components that we can test against
2933 final List<AppWidgetProviderInfo> widgets =
2934 AppWidgetManager.getInstance(context).getInstalledProviders();
2935 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2936 new HashMap<ComponentName, AppWidgetProviderInfo>();
2937 for (AppWidgetProviderInfo info : widgets) {
2938 configurationComponentToWidget.put(info.configure, info);
2939 }
2940
2941 // Run through each of the intents that can handle this type of clip data, and cross
2942 // reference them with the components that are actual configuration components
2943 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2944 PackageManager.MATCH_DEFAULT_ONLY);
2945 for (ResolveInfo info : activities) {
2946 final ActivityInfo activityInfo = info.activityInfo;
2947 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2948 activityInfo.name);
2949 if (configurationComponentToWidget.containsKey(infoComponent)) {
2950 supportedConfigurationActivities.add(
2951 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2952 configurationComponentToWidget.get(infoComponent)));
2953 }
2954 }
2955 return supportedConfigurationActivities;
2956 }
2957
Winson Chunga9abd0e2010-10-27 17:18:37 -07002958 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002959 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2960 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2961 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2962
Adam Cohend9198822011-11-22 16:42:47 -08002963 if (intent == null) {
2964 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2965 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2966 return null;
2967 }
2968
Joe Onorato0589f0f2010-02-08 13:44:00 -08002969 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002970 boolean customIcon = false;
2971 ShortcutIconResource iconResource = null;
2972
2973 if (bitmap != null && bitmap instanceof Bitmap) {
2974 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002975 customIcon = true;
2976 } else {
2977 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2978 if (extra != null && extra instanceof ShortcutIconResource) {
2979 try {
2980 iconResource = (ShortcutIconResource) extra;
2981 final PackageManager packageManager = context.getPackageManager();
2982 Resources resources = packageManager.getResourcesForApplication(
2983 iconResource.packageName);
2984 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002985 icon = Utilities.createIconBitmap(
2986 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002987 } catch (Exception e) {
2988 Log.w(TAG, "Could not load shortcut icon: " + extra);
2989 }
2990 }
2991 }
2992
Michael Jurkac9d95c52011-08-29 14:03:34 -07002993 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002994
2995 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002996 if (fallbackIcon != null) {
2997 icon = fallbackIcon;
2998 } else {
2999 icon = getFallbackIcon();
3000 info.usingFallbackIcon = true;
3001 }
Joe Onorato56d82912010-03-07 14:32:10 -05003002 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003003 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003004
Joe Onorato0589f0f2010-02-08 13:44:00 -08003005 info.title = name;
3006 info.intent = intent;
3007 info.customIcon = customIcon;
3008 info.iconResource = iconResource;
3009
3010 return info;
3011 }
3012
Winson Chungaac01e12011-08-17 10:37:13 -07003013 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3014 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003015 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003016 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003017 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003018 }
Joe Onorato56d82912010-03-07 14:32:10 -05003019 // If this icon doesn't have a custom icon, check to see
3020 // what's stored in the DB, and if it doesn't match what
3021 // we're going to show, store what we are going to show back
3022 // into the DB. We do this so when we're loading, if the
3023 // package manager can't find an icon (for example because
3024 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003025 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003026 cache.put(info, c.getBlob(iconIndex));
3027 return true;
3028 }
3029 return false;
3030 }
3031 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3032 boolean needSave = false;
3033 try {
3034 if (data != null) {
3035 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3036 Bitmap loaded = info.getIcon(mIconCache);
3037 needSave = !saved.sameAs(loaded);
3038 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003039 needSave = true;
3040 }
Winson Chungaac01e12011-08-17 10:37:13 -07003041 } catch (Exception e) {
3042 needSave = true;
3043 }
3044 if (needSave) {
3045 Log.d(TAG, "going to save icon bitmap for info=" + info);
3046 // This is slower than is ideal, but this only happens once
3047 // or when the app is updated with a new icon.
3048 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003049 }
3050 }
3051
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003053 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003054 * or make a new one.
3055 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003056 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 // See if a placeholder was created for us already
3058 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003059 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003061 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 folders.put(id, folderInfo);
3063 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003064 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 }
3066
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003067 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003068 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003069 return new Comparator<AppInfo>() {
3070 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003071 int result = collator.compare(a.title.toString().trim(),
3072 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003073 if (result == 0) {
3074 result = a.componentName.compareTo(b.componentName);
3075 }
3076 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003077 }
Winson Chung11904872012-09-17 16:58:46 -07003078 };
3079 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003080 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3081 = new Comparator<AppInfo>() {
3082 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003083 if (a.firstInstallTime < b.firstInstallTime) return 1;
3084 if (a.firstInstallTime > b.firstInstallTime) return -1;
3085 return 0;
3086 }
3087 };
Winson Chung11904872012-09-17 16:58:46 -07003088 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3089 final Collator collator = Collator.getInstance();
3090 return new Comparator<AppWidgetProviderInfo>() {
3091 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003092 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003093 }
3094 };
3095 }
Winson Chung5308f242011-08-18 12:12:41 -07003096 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3097 if (info.activityInfo != null) {
3098 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3099 } else {
3100 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3101 }
3102 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003103 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003104 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003105 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003106 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003107 ShortcutNameComparator(PackageManager pm) {
3108 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003109 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003110 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003111 }
3112 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3113 mPackageManager = pm;
3114 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003115 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003116 }
3117 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003118 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003119 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3120 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3121 if (mLabelCache.containsKey(keyA)) {
3122 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003123 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003124 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003125
Winson Chung5308f242011-08-18 12:12:41 -07003126 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003127 }
Winson Chung5308f242011-08-18 12:12:41 -07003128 if (mLabelCache.containsKey(keyB)) {
3129 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003130 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003131 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003132
Winson Chung5308f242011-08-18 12:12:41 -07003133 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003134 }
Winson Chung11904872012-09-17 16:58:46 -07003135 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003136 }
3137 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003138 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003139 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003140 private PackageManager mPackageManager;
3141 private HashMap<Object, String> mLabelCache;
3142 WidgetAndShortcutNameComparator(PackageManager pm) {
3143 mPackageManager = pm;
3144 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003145 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003146 }
3147 public final int compare(Object a, Object b) {
3148 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 if (mLabelCache.containsKey(a)) {
3150 labelA = mLabelCache.get(a);
3151 } else {
3152 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003153 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003154 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003155 mLabelCache.put(a, labelA);
3156 }
3157 if (mLabelCache.containsKey(b)) {
3158 labelB = mLabelCache.get(b);
3159 } else {
3160 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003161 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003162 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003163 mLabelCache.put(b, labelB);
3164 }
Winson Chung11904872012-09-17 16:58:46 -07003165 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003166 }
3167 };
Joe Onoratobe386092009-11-17 17:32:16 -08003168
3169 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003170 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003171 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3172 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3173 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3174 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003175 if (mLoaderTask != null) {
3176 mLoaderTask.dumpState();
3177 } else {
3178 Log.d(TAG, "mLoaderTask=null");
3179 }
Joe Onoratobe386092009-11-17 17:32:16 -08003180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181}