blob: edfc07059dbb5c9e41b5f77338a2e2f2545d5093 [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
321 // Add this icon to the db, creating a new page if necessary
Winson Chung156ab5b2013-07-12 14:14:16 -0700322 int startSearchPageIndex = 1;
Winson Chung64359a52013-07-08 17:17:08 -0700323 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700324 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700325 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200326 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700327
328 // If we can't find a valid position, then just add a new screen.
329 // This takes time so we need to re-queue the add until the new
330 // page is added. Create as many screens as necessary to satisfy
331 // the startSearchPageIndex.
332 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700333 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700334 while (numPagesToAdd > 0) {
335 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700337 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 addedWorkspaceScreensFinal.add(screenId);
339 numPagesToAdd--;
340 }
Winson Chung76828c82013-08-19 15:43:29 -0700341
Winson Chung64359a52013-07-08 17:17:08 -0700342 // Find the coordinate again
343 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700344 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700345 }
346 if (coords == null) {
347 throw new RuntimeException("Coordinates should not be null");
348 }
349
Winson Chung997a9232013-07-24 15:33:46 -0700350 ShortcutInfo shortcutInfo;
351 if (a instanceof ShortcutInfo) {
352 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200353 } else if (a instanceof AppInfo) {
354 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700355 } else {
356 throw new RuntimeException("Unexpected info type");
357 }
Winson Chung94d67682013-09-25 16:29:40 -0700358
Winson Chung64359a52013-07-08 17:17:08 -0700359 // Add the shortcut to the db
360 addItemToDatabase(context, shortcutInfo,
361 LauncherSettings.Favorites.CONTAINER_DESKTOP,
362 coords.first, coords.second[0], coords.second[1], false);
363 // Save the ShortcutInfo for binding in the workspace
364 addedShortcutsFinal.add(shortcutInfo);
365 }
366 }
367
Winson Chung76828c82013-08-19 15:43:29 -0700368 // Update the workspace screens
369 updateWorkspaceScreenOrder(context, workspaceScreens);
370
Winson Chung94d67682013-09-25 16:29:40 -0700371 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700372 runOnMainThread(new Runnable() {
373 public void run() {
374 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
375 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700376 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
377 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700378 if (!addedShortcutsFinal.isEmpty()) {
379 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
380 long lastScreenId = info.screenId;
381 for (ItemInfo i : addedShortcutsFinal) {
382 if (i.screenId == lastScreenId) {
383 addAnimated.add(i);
384 } else {
385 addNotAnimated.add(i);
386 }
Winson Chung997a9232013-07-24 15:33:46 -0700387 }
388 }
Winson Chungd64d1762013-08-20 14:37:16 -0700389 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700390 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700391 }
Winson Chung64359a52013-07-08 17:17:08 -0700392 }
Winson Chung997a9232013-07-24 15:33:46 -0700393 });
394 }
Winson Chung64359a52013-07-08 17:17:08 -0700395 }
396 };
397 runOnWorkerThread(r);
398 }
399
Joe Onorato56d82912010-03-07 14:32:10 -0500400 public Bitmap getFallbackIcon() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800401 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403
Winson Chung81b52252012-08-27 15:34:29 -0700404 public void unbindItemInfosAndClearQueuedBindRunnables() {
405 if (sWorkerThread.getThreadId() == Process.myTid()) {
406 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
407 "main thread");
408 }
409
410 // Clear any deferred bind runnables
411 mDeferredBindRunnables.clear();
412 // Remove any queued bind runnables
413 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
414 // Unbind all the workspace items
415 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700416 }
417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700419 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700420 // Ensure that we don't use the same workspace items data structure on the main thread
421 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700422 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
423 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700424 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
426 tmpAppWidgets.addAll(sBgAppWidgets);
427 }
428 Runnable r = new Runnable() {
429 @Override
430 public void run() {
431 for (ItemInfo item : tmpWorkspaceItems) {
432 item.unbind();
433 }
434 for (ItemInfo item : tmpAppWidgets) {
435 item.unbind();
436 }
437 }
438 };
439 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700440 }
441
Joe Onorato9c1289c2009-08-17 11:03:03 -0400442 /**
443 * Adds an item to the DB if it was not created previously, or move it to a new
444 * <container, screen, cellX, cellY>
445 */
446 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700447 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400448 if (item.container == ItemInfo.NO_ID) {
449 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700450 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 } else {
452 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700453 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455 }
456
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700457 static void checkItemInfoLocked(
458 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
459 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
460 if (modelItem != null && item != modelItem) {
461 // check all the data is consistent
462 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
463 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
464 ShortcutInfo shortcut = (ShortcutInfo) item;
465 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
466 modelShortcut.intent.filterEquals(shortcut.intent) &&
467 modelShortcut.id == shortcut.id &&
468 modelShortcut.itemType == shortcut.itemType &&
469 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700470 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700471 modelShortcut.cellX == shortcut.cellX &&
472 modelShortcut.cellY == shortcut.cellY &&
473 modelShortcut.spanX == shortcut.spanX &&
474 modelShortcut.spanY == shortcut.spanY &&
475 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
476 (modelShortcut.dropPos != null &&
477 shortcut.dropPos != null &&
478 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
479 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
480 // For all intents and purposes, this is the same object
481 return;
482 }
483 }
484
485 // the modelItem needs to match up perfectly with item if our model is
486 // to be consistent with the database-- for now, just require
487 // modelItem == item or the equality check above
488 String msg = "item: " + ((item != null) ? item.toString() : "null") +
489 "modelItem: " +
490 ((modelItem != null) ? modelItem.toString() : "null") +
491 "Error: ItemInfo passed to checkItemInfo doesn't match original";
492 RuntimeException e = new RuntimeException(msg);
493 if (stackTrace != null) {
494 e.setStackTrace(stackTrace);
495 }
Adam Cohene25af792013-06-06 23:08:25 -0700496 // TODO: something breaks this in the upgrade path
497 //throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700498 }
499 }
500
Michael Jurka816474f2012-06-25 14:49:02 -0700501 static void checkItemInfo(final ItemInfo item) {
502 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
503 final long itemId = item.id;
504 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700505 public void run() {
506 synchronized (sBgLock) {
507 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700508 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700509 }
510 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700511 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700512 }
513
Michael Jurkac9d95c52011-08-29 14:03:34 -0700514 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
515 final ItemInfo item, final String callingFunction) {
516 final long itemId = item.id;
517 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
518 final ContentResolver cr = context.getContentResolver();
519
Adam Cohen487f7dd2012-06-28 18:12:10 -0700520 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700521 Runnable r = new Runnable() {
522 public void run() {
523 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700524 updateItemArrays(item, itemId, stackTrace);
525 }
526 };
527 runOnWorkerThread(r);
528 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700529
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
531 final ArrayList<ItemInfo> items, final String callingFunction) {
532 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700533
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700534 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
535 Runnable r = new Runnable() {
536 public void run() {
537 ArrayList<ContentProviderOperation> ops =
538 new ArrayList<ContentProviderOperation>();
539 int count = items.size();
540 for (int i = 0; i < count; i++) {
541 ItemInfo item = items.get(i);
542 final long itemId = item.id;
543 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
544 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700545
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700546 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
547 updateItemArrays(item, itemId, stackTrace);
548
549 }
550 try {
551 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
552 } catch (Exception e) {
553 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700554 }
555 }
556 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700557 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700558 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700559
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700560 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
561 // Lock on mBgLock *after* the db operation
562 synchronized (sBgLock) {
563 checkItemInfoLocked(itemId, item, stackTrace);
564
565 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
566 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
567 // Item is in a folder, make sure this folder exists
568 if (!sBgFolders.containsKey(item.container)) {
569 // An items container is being set to a that of an item which is not in
570 // the list of Folders.
571 String msg = "item: " + item + " container being set to: " +
572 item.container + ", not in the list of folders";
573 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700574 }
575 }
576
577 // Items are added/removed from the corresponding FolderInfo elsewhere, such
578 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
579 // that are on the desktop, as appropriate
580 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
581 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
582 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
583 switch (modelItem.itemType) {
584 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
585 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
586 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
587 if (!sBgWorkspaceItems.contains(modelItem)) {
588 sBgWorkspaceItems.add(modelItem);
589 }
590 break;
591 default:
592 break;
593 }
594 } else {
595 sBgWorkspaceItems.remove(modelItem);
596 }
597 }
598 }
599
Michael Jurkac7700af2013-05-14 20:17:58 +0200600 public void flushWorkerThread() {
601 mFlushingWorkerThread = true;
602 Runnable waiter = new Runnable() {
603 public void run() {
604 synchronized (this) {
605 notifyAll();
606 mFlushingWorkerThread = false;
607 }
608 }
609 };
610
611 synchronized(waiter) {
612 runOnWorkerThread(waiter);
613 if (mLoaderTask != null) {
614 synchronized(mLoaderTask) {
615 mLoaderTask.notify();
616 }
617 }
618 boolean success = false;
619 while (!success) {
620 try {
621 waiter.wait();
622 success = true;
623 } catch (InterruptedException e) {
624 }
625 }
626 }
627 }
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700631 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700632 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400634 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 item.cellX = cellX;
636 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700637
Winson Chung3d503fb2011-07-13 17:25:49 -0700638 // We store hotseat items in canonical form which is this orientation invariant position
639 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700641 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700644 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400646
647 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
650 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
Michael Jurkac9d95c52011-08-29 14:03:34 -0700653 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700654 }
655
656 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700657 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
658 * cellX, cellY have already been updated on the ItemInfos.
659 */
660 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
661 final long container, final int screen) {
662
663 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
664 int count = items.size();
665
666 for (int i = 0; i < count; i++) {
667 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700668 item.container = container;
669
670 // We store hotseat items in canonical form which is this orientation invariant position
671 // in the hotseat
672 if (context instanceof Launcher && screen < 0 &&
673 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700674 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 item.cellY);
676 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700678 }
679
680 final ContentValues values = new ContentValues();
681 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
682 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
683 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685
686 contentValues.add(values);
687 }
688 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
689 }
690
691 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700692 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800693 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700694 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700696 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800697 item.cellX = cellX;
698 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 item.spanX = spanX;
700 item.spanY = spanY;
701
702 // We store hotseat items in canonical form which is this orientation invariant position
703 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700705 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700707 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700709 }
Adam Cohend4844c32011-02-18 19:25:06 -0800710
Adam Cohend4844c32011-02-18 19:25:06 -0800711 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800712 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
714 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
715 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
716 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800718
Michael Jurka816474f2012-06-25 14:49:02 -0700719 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721
722 /**
723 * Update an item to the database in a specified container.
724 */
725 static void updateItemInDatabase(Context context, final ItemInfo item) {
726 final ContentValues values = new ContentValues();
727 item.onAddToDatabase(values);
728 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
729 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800730 }
731
732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 * Returns true if the shortcuts already exists in the database.
734 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 static boolean shortcutExists(Context context, String title, Intent intent) {
737 final ContentResolver cr = context.getContentResolver();
738 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
739 new String[] { "title", "intent" }, "title=? and intent=?",
740 new String[] { title, intent.toUri(0) }, null);
741 boolean result = false;
742 try {
743 result = c.moveToFirst();
744 } finally {
745 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700748 }
749
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700751 * Returns an ItemInfo array containing all the items in the LauncherModel.
752 * The ItemInfo.id is not set through this function.
753 */
754 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
755 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
756 final ContentResolver cr = context.getContentResolver();
757 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
758 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
759 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
760 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
761
762 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
763 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
764 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
765 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
766 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
767 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
768 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
769
770 try {
771 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700772 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700773 item.cellX = c.getInt(cellXIndex);
774 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700775 item.spanX = Math.max(1, c.getInt(spanXIndex));
776 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700777 item.container = c.getInt(containerIndex);
778 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700779 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700780
781 items.add(item);
782 }
783 } catch (Exception e) {
784 items.clear();
785 } finally {
786 c.close();
787 }
788
789 return items;
790 }
791
792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400793 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
794 */
795 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
796 final ContentResolver cr = context.getContentResolver();
797 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
798 "_id=? and (itemType=? or itemType=?)",
799 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700800 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700801
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 try {
803 if (c.moveToFirst()) {
804 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
805 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
806 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
807 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
808 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
809 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 FolderInfo folderInfo = null;
812 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700813 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
814 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700816 }
817
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 folderInfo.title = c.getString(titleIndex);
819 folderInfo.id = id;
820 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700821 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700822 folderInfo.cellX = c.getInt(cellXIndex);
823 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824
825 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 } finally {
828 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700829 }
830
831 return null;
832 }
833
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 /**
835 * Add an item to the database in a specified container. Sets the container, screen, cellX and
836 * cellY fields of the item. Also assigns an ID to the item.
837 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700838 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 item.cellX = cellX;
842 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 // We store hotseat items in canonical form which is this orientation invariant position
844 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700849 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851
852 final ContentValues values = new ContentValues();
853 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 item.onAddToDatabase(values);
855
Michael Jurka414300a2013-08-27 15:42:35 +0200856 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700857 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700859
Michael Jurkac9d95c52011-08-29 14:03:34 -0700860 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700861 public void run() {
862 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
863 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700865 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700866 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700867 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700868 sBgItemsIdMap.put(item.id, item);
869 switch (item.itemType) {
870 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
871 sBgFolders.put(item.id, (FolderInfo) item);
872 // Fall through
873 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
874 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
875 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
876 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
877 sBgWorkspaceItems.add(item);
878 } else {
879 if (!sBgFolders.containsKey(item.container)) {
880 // Adding an item to a folder that doesn't exist.
881 String msg = "adding item: " + item + " to a folder that " +
882 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700883 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700884 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700885 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700886 break;
887 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
888 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
889 break;
890 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700891 }
892 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700893 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700894 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700895 }
896
Joe Onorato9c1289c2009-08-17 11:03:03 -0400897 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700898 * Creates a new unique child id, for a given cell span across all layouts.
899 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700900 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700901 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700902 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700903 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 }
905
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700907 * Removes the specified item from the database
908 * @param context
909 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700911 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700914
Michael Jurka83df1882011-08-31 20:59:26 -0700915 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700916 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700917 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700918
919 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700920 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700921 switch (item.itemType) {
922 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
923 sBgFolders.remove(item.id);
924 for (ItemInfo info: sBgItemsIdMap.values()) {
925 if (info.container == item.id) {
926 // We are deleting a folder which still contains items that
927 // think they are contained by that folder.
928 String msg = "deleting a folder (" + item + ") which still " +
929 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700930 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700931 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700932 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700933 sBgWorkspaceItems.remove(item);
934 break;
935 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
936 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
937 sBgWorkspaceItems.remove(item);
938 break;
939 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
940 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
941 break;
942 }
943 sBgItemsIdMap.remove(item.id);
944 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700945 }
946 }
Michael Jurka83df1882011-08-31 20:59:26 -0700947 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700948 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 }
950
951 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700952 * Update the order of the workspace screens in the database. The array list contains
953 * a list of screen ids in the order that they should appear.
954 */
Winson Chungc9168342013-06-26 14:54:55 -0700955 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chung64359a52013-07-08 17:17:08 -0700956 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 final ContentResolver cr = context.getContentResolver();
958 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
959
960 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700961 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 while (iter.hasNext()) {
963 long id = iter.next();
964 if (id < 0) {
965 iter.remove();
966 }
967 }
968
969 Runnable r = new Runnable() {
970 @Override
971 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700972 // Clear the table
973 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700974 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 ContentValues[] values = new ContentValues[count];
976 for (int i = 0; i < count; i++) {
977 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700978 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700979 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
980 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 values[i] = v;
982 }
983 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700984
Winson Chungba9c37f2013-08-30 14:11:37 -0700985 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700986 sBgWorkspaceScreens.clear();
987 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700988 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 }
990 };
991 runOnWorkerThread(r);
992 }
993
994 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 * Remove the contents of the specified folder from the database
996 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700997 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 final ContentResolver cr = context.getContentResolver();
999
Michael Jurkac9d95c52011-08-29 14:03:34 -07001000 Runnable r = new Runnable() {
1001 public void run() {
1002 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001003 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001004 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001005 sBgItemsIdMap.remove(info.id);
1006 sBgFolders.remove(info.id);
1007 sBgDbIconCache.remove(info);
1008 sBgWorkspaceItems.remove(info);
1009 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1012 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001014 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 for (ItemInfo childInfo : info.contents) {
1016 sBgItemsIdMap.remove(childInfo.id);
1017 sBgDbIconCache.remove(childInfo);
1018 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001020 }
1021 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001022 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 }
1024
1025 /**
1026 * Set this as the current Launcher activity object for the loader.
1027 */
1028 public void initialize(Callbacks callbacks) {
1029 synchronized (mLock) {
1030 mCallbacks = new WeakReference<Callbacks>(callbacks);
1031 }
1032 }
1033
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001034 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001035 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1036 * ACTION_PACKAGE_CHANGED.
1037 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001038 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001039 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001040 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001041
Joe Onorato36115782010-06-17 13:28:48 -04001042 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001043
Joe Onorato36115782010-06-17 13:28:48 -04001044 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1045 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1046 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1047 final String packageName = intent.getData().getSchemeSpecificPart();
1048 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049
Joe Onorato36115782010-06-17 13:28:48 -04001050 int op = PackageUpdatedTask.OP_NONE;
1051
1052 if (packageName == null || packageName.length() == 0) {
1053 // they sent us a bad intent
1054 return;
1055 }
1056
1057 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1058 op = PackageUpdatedTask.OP_UPDATE;
1059 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1060 if (!replacing) {
1061 op = PackageUpdatedTask.OP_REMOVE;
1062 }
1063 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1064 // later, we will update the package at this time
1065 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1066 if (!replacing) {
1067 op = PackageUpdatedTask.OP_ADD;
1068 } else {
1069 op = PackageUpdatedTask.OP_UPDATE;
1070 }
1071 }
1072
1073 if (op != PackageUpdatedTask.OP_NONE) {
1074 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1075 }
1076
1077 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001078 // First, schedule to add these apps back in.
1079 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1080 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1081 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001082 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001083 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1084 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1085 enqueuePackageUpdated(new PackageUpdatedTask(
1086 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001087 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001088 // If we have changed locale we need to clear out the labels in all apps/workspace.
1089 forceReload();
1090 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1091 // Check if configuration change was an mcc/mnc change which would affect app resources
1092 // and we would need to clear out the labels in all apps/workspace. Same handling as
1093 // above for ACTION_LOCALE_CHANGED
1094 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001095 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001096 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001097 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001098 forceReload();
1099 }
1100 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001101 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001102 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1103 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001104 if (mCallbacks != null) {
1105 Callbacks callbacks = mCallbacks.get();
1106 if (callbacks != null) {
1107 callbacks.bindSearchablesChanged();
1108 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001109 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001110 }
1111 }
1112
Reena Lee93f824a2011-09-23 17:20:28 -07001113 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001114 resetLoadedState(true, true);
1115
Reena Lee93f824a2011-09-23 17:20:28 -07001116 // Do this here because if the launcher activity is running it will be restarted.
1117 // If it's not running startLoaderFromBackground will merely tell it that it needs
1118 // to reload.
1119 startLoaderFromBackground();
1120 }
1121
Winson Chungf0c6ae02012-03-21 16:10:31 -07001122 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1123 synchronized (mLock) {
1124 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1125 // mWorkspaceLoaded to true later
1126 stopLoaderLocked();
1127 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1128 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1129 }
1130 }
1131
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001132 /**
1133 * When the launcher is in the background, it's possible for it to miss paired
1134 * configuration changes. So whenever we trigger the loader from the background
1135 * tell the launcher that it needs to re-run the loader when it comes back instead
1136 * of doing it now.
1137 */
1138 public void startLoaderFromBackground() {
1139 boolean runLoader = false;
1140 if (mCallbacks != null) {
1141 Callbacks callbacks = mCallbacks.get();
1142 if (callbacks != null) {
1143 // Only actually run the loader if they're not paused.
1144 if (!callbacks.setLoadOnResume()) {
1145 runLoader = true;
1146 }
1147 }
1148 }
1149 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001150 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001151 }
Joe Onorato36115782010-06-17 13:28:48 -04001152 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001153
Reena Lee93f824a2011-09-23 17:20:28 -07001154 // If there is already a loader task running, tell it to stop.
1155 // returns true if isLaunching() was true on the old task
1156 private boolean stopLoaderLocked() {
1157 boolean isLaunching = false;
1158 LoaderTask oldTask = mLoaderTask;
1159 if (oldTask != null) {
1160 if (oldTask.isLaunching()) {
1161 isLaunching = true;
1162 }
1163 oldTask.stopLocked();
1164 }
1165 return isLaunching;
1166 }
1167
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001168 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001169 synchronized (mLock) {
1170 if (DEBUG_LOADERS) {
1171 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1172 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001174 // Clear any deferred bind-runnables from the synchronized load process
1175 // We must do this before any loading/binding is scheduled below.
1176 mDeferredBindRunnables.clear();
1177
Joe Onorato36115782010-06-17 13:28:48 -04001178 // Don't bother to start the thread if we know it's not going to do anything
1179 if (mCallbacks != null && mCallbacks.get() != null) {
1180 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001181 // also, don't downgrade isLaunching if we're already running
1182 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001183 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1185 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1186 } else {
1187 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1188 sWorker.post(mLoaderTask);
1189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 }
1192 }
1193
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 void bindRemainingSynchronousPages() {
1195 // Post the remaining side pages to be loaded
1196 if (!mDeferredBindRunnables.isEmpty()) {
1197 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001198 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001199 }
1200 mDeferredBindRunnables.clear();
1201 }
1202 }
1203
Joe Onorato36115782010-06-17 13:28:48 -04001204 public void stopLoader() {
1205 synchronized (mLock) {
1206 if (mLoaderTask != null) {
1207 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 }
1209 }
Joe Onorato36115782010-06-17 13:28:48 -04001210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211
Winson Chung76828c82013-08-19 15:43:29 -07001212 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1213 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1214 final ContentResolver contentResolver = context.getContentResolver();
1215 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1216 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1217 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1218
1219 try {
1220 final int idIndex = sc.getColumnIndexOrThrow(
1221 LauncherSettings.WorkspaceScreens._ID);
1222 final int rankIndex = sc.getColumnIndexOrThrow(
1223 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1224 while (sc.moveToNext()) {
1225 try {
1226 long screenId = sc.getLong(idIndex);
1227 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001228 orderedScreens.put(rank, screenId);
1229 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001230 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001231 }
1232 }
1233 } finally {
1234 sc.close();
1235 }
1236 return orderedScreens;
1237 }
1238
Michael Jurkac57b7a82011-08-09 22:02:20 -07001239 public boolean isAllAppsLoaded() {
1240 return mAllAppsLoaded;
1241 }
1242
Winson Chung36a62fe2012-05-06 18:04:42 -07001243 boolean isLoadingWorkspace() {
1244 synchronized (mLock) {
1245 if (mLoaderTask != null) {
1246 return mLoaderTask.isLoadingWorkspace();
1247 }
1248 }
1249 return false;
1250 }
1251
Joe Onorato36115782010-06-17 13:28:48 -04001252 /**
1253 * Runnable for the thread that loads the contents of the launcher:
1254 * - workspace icons
1255 * - widgets
1256 * - all apps icons
1257 */
1258 private class LoaderTask implements Runnable {
1259 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001260 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001261 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001262 private boolean mStopped;
1263 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001264
Winson Chungc3eecff2011-07-11 17:44:15 -07001265 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001266
1267 LoaderTask(Context context, boolean isLaunching) {
1268 mContext = context;
1269 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001270 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 }
1272
Joe Onorato36115782010-06-17 13:28:48 -04001273 boolean isLaunching() {
1274 return mIsLaunching;
1275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276
Winson Chung36a62fe2012-05-06 18:04:42 -07001277 boolean isLoadingWorkspace() {
1278 return mIsLoadingAndBindingWorkspace;
1279 }
1280
Winson Chungc763c4e2013-07-19 13:49:06 -07001281 /** Returns whether this is an upgrade path */
1282 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001283 mIsLoadingAndBindingWorkspace = true;
1284
Joe Onorato36115782010-06-17 13:28:48 -04001285 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001286 if (DEBUG_LOADERS) {
1287 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001288 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001289
Winson Chungc763c4e2013-07-19 13:49:06 -07001290 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001291 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001293 synchronized (LoaderTask.this) {
1294 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001295 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001296 }
1297 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 }
1299 }
1300
Joe Onorato36115782010-06-17 13:28:48 -04001301 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001302 bindWorkspace(-1, isUpgradePath);
1303 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001304 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001305
Joe Onorato36115782010-06-17 13:28:48 -04001306 private void waitForIdle() {
1307 // Wait until the either we're stopped or the other threads are done.
1308 // This way we don't start loading all apps until the workspace has settled
1309 // down.
1310 synchronized (LoaderTask.this) {
1311 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001312
Joe Onorato36115782010-06-17 13:28:48 -04001313 mHandler.postIdle(new Runnable() {
1314 public void run() {
1315 synchronized (LoaderTask.this) {
1316 mLoadAndBindStepFinished = true;
1317 if (DEBUG_LOADERS) {
1318 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001319 }
Joe Onorato36115782010-06-17 13:28:48 -04001320 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001321 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001322 }
Joe Onorato36115782010-06-17 13:28:48 -04001323 });
1324
Michael Jurkac7700af2013-05-14 20:17:58 +02001325 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001326 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001327 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1328 // wait no longer than 1sec at a time
1329 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001330 } catch (InterruptedException ex) {
1331 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001332 }
1333 }
Joe Onorato36115782010-06-17 13:28:48 -04001334 if (DEBUG_LOADERS) {
1335 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001336 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001337 + "ms for previous step to finish binding");
1338 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001341
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001342 void runBindSynchronousPage(int synchronousBindPage) {
1343 if (synchronousBindPage < 0) {
1344 // Ensure that we have a valid page index to load synchronously
1345 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1346 "valid page index");
1347 }
1348 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1349 // Ensure that we don't try and bind a specified page when the pages have not been
1350 // loaded already (we should load everything asynchronously in that case)
1351 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1352 }
1353 synchronized (mLock) {
1354 if (mIsLoaderTaskRunning) {
1355 // Ensure that we are never running the background loading at this point since
1356 // we also touch the background collections
1357 throw new RuntimeException("Error! Background loading is already running");
1358 }
1359 }
1360
1361 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1362 // data structures, we can't allow any other thread to touch that data, but because
1363 // this call is synchronous, we can get away with not locking).
1364
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001365 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001366 // operations from the previous activity. We need to ensure that all queued operations
1367 // are executed before any synchronous binding work is done.
1368 mHandler.flush();
1369
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001370 // Divide the set of loaded items into those that we are binding synchronously, and
1371 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001372 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001373 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1374 // arise from that.
1375 onlyBindAllApps();
1376 }
1377
Joe Onorato36115782010-06-17 13:28:48 -04001378 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001379 boolean isUpgrade = false;
1380
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001381 synchronized (mLock) {
1382 mIsLoaderTaskRunning = true;
1383 }
Joe Onorato36115782010-06-17 13:28:48 -04001384 // Optimize for end-user experience: if the Launcher is up and // running with the
1385 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1386 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001387 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001388 // Elevate priority when Home launches for the first time to avoid
1389 // starving at boot time. Staring at a blank home is not cool.
1390 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001391 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1392 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001393 android.os.Process.setThreadPriority(mIsLaunching
1394 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1395 }
Winson Chung64359a52013-07-08 17:17:08 -07001396 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001397 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001398
Joe Onorato36115782010-06-17 13:28:48 -04001399 if (mStopped) {
1400 break keep_running;
1401 }
1402
1403 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1404 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001405 synchronized (mLock) {
1406 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001407 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1409 }
1410 }
Joe Onorato36115782010-06-17 13:28:48 -04001411 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001412
1413 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001414 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1415 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001416
1417 // Restore the default thread priority after we are done loading items
1418 synchronized (mLock) {
1419 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1420 }
Joe Onorato36115782010-06-17 13:28:48 -04001421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422
Winson Chungaac01e12011-08-17 10:37:13 -07001423 // Update the saved icons if necessary
1424 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001425 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001426 for (Object key : sBgDbIconCache.keySet()) {
1427 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1428 }
1429 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001430 }
Winson Chungaac01e12011-08-17 10:37:13 -07001431
Winson Chungc58497e2013-09-03 17:48:37 -07001432 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1433 // Ensure that all the applications that are in the system are
1434 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001435 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001436 verifyApplications();
1437 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001438 }
1439
Joe Onorato36115782010-06-17 13:28:48 -04001440 // Clear out this reference, otherwise we end up holding it until all of the
1441 // callback runnables are done.
1442 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443
Joe Onorato36115782010-06-17 13:28:48 -04001444 synchronized (mLock) {
1445 // If we are still the last one to be scheduled, remove ourselves.
1446 if (mLoaderTask == this) {
1447 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001449 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001450 }
Joe Onorato36115782010-06-17 13:28:48 -04001451 }
1452
1453 public void stopLocked() {
1454 synchronized (LoaderTask.this) {
1455 mStopped = true;
1456 this.notify();
1457 }
1458 }
1459
1460 /**
1461 * Gets the callbacks object. If we've been stopped, or if the launcher object
1462 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1463 * object that was around when the deferred message was scheduled, and if there's
1464 * a new Callbacks object around then also return null. This will save us from
1465 * calling onto it with data that will be ignored.
1466 */
1467 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1468 synchronized (mLock) {
1469 if (mStopped) {
1470 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001471 }
Joe Onorato36115782010-06-17 13:28:48 -04001472
1473 if (mCallbacks == null) {
1474 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001475 }
Joe Onorato36115782010-06-17 13:28:48 -04001476
1477 final Callbacks callbacks = mCallbacks.get();
1478 if (callbacks != oldCallbacks) {
1479 return null;
1480 }
1481 if (callbacks == null) {
1482 Log.w(TAG, "no mCallbacks");
1483 return null;
1484 }
1485
1486 return callbacks;
1487 }
1488 }
1489
Winson Chungc763c4e2013-07-19 13:49:06 -07001490 private void verifyApplications() {
1491 final Context context = mApp.getContext();
1492
1493 // Cross reference all the applications in our apps list with items in the workspace
1494 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001495 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001496 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001497 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001498 tmpInfos = getItemInfoForComponentName(app.componentName);
1499 if (tmpInfos.isEmpty()) {
1500 // We are missing an application icon, so add this to the workspace
1501 added.add(app);
1502 // This is a rare event, so lets log it
1503 Log.e(TAG, "Missing Application on load: " + app);
1504 }
1505 }
1506 }
1507 if (!added.isEmpty()) {
1508 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungc58497e2013-09-03 17:48:37 -07001509 addAndBindAddedApps(context, added, cb, null);
Winson Chungc763c4e2013-07-19 13:49:06 -07001510 }
1511 }
1512
Winson Chung5f8afe62013-08-12 16:19:28 -07001513 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001514 LauncherAppState app = LauncherAppState.getInstance();
1515 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1516 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1517 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001518 }
1519
Joe Onorato36115782010-06-17 13:28:48 -04001520 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001521 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1522 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001523 LauncherAppState app = LauncherAppState.getInstance();
1524 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1525 int countX = (int) grid.numColumns;
1526 int countY = (int) grid.numRows;
1527
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001529 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001530 // Return early if we detect that an item is under the hotseat button
1531 if (mCallbacks == null ||
1532 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1533 deleteOnItemOverlap.set(true);
1534 return false;
1535 }
1536
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1538 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1539 [(int) item.screenId][0] != null) {
1540 Log.e(TAG, "Error loading shortcut into hotseat " + item
1541 + " into position (" + item.screenId + ":" + item.cellX + ","
1542 + item.cellY + ") occupied by "
1543 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1544 [(int) item.screenId][0]);
1545 return false;
1546 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001547 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001548 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 items[(int) item.screenId][0] = item;
1550 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001551 return true;
1552 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001553 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1554 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001555 return true;
1556 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001559 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 occupied.put(item.screenId, items);
1561 }
1562
1563 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001564 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001565 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1566 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001568 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001570 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001571 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001573 return false;
1574 }
1575 }
1576 }
1577 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1578 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001580 }
1581 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001582
Joe Onorato36115782010-06-17 13:28:48 -04001583 return true;
1584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585
Winson Chungba9c37f2013-08-30 14:11:37 -07001586 /** Clears all the sBg data structures */
1587 private void clearSBgDataStructures() {
1588 synchronized (sBgLock) {
1589 sBgWorkspaceItems.clear();
1590 sBgAppWidgets.clear();
1591 sBgFolders.clear();
1592 sBgItemsIdMap.clear();
1593 sBgDbIconCache.clear();
1594 sBgWorkspaceScreens.clear();
1595 }
1596 }
1597
Winson Chungc763c4e2013-07-19 13:49:06 -07001598 /** Returns whether this is an upgradge path */
1599 private boolean loadWorkspace() {
Joe Onorato36115782010-06-17 13:28:48 -04001600 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601
Joe Onorato36115782010-06-17 13:28:48 -04001602 final Context context = mContext;
1603 final ContentResolver contentResolver = context.getContentResolver();
1604 final PackageManager manager = context.getPackageManager();
1605 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1606 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001607
Winson Chung892c74d2013-08-22 16:15:50 -07001608 LauncherAppState app = LauncherAppState.getInstance();
1609 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1610 int countX = (int) grid.numColumns;
1611 int countY = (int) grid.numRows;
1612
Michael Jurkab85f8a42012-04-25 15:48:32 -07001613 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001614 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001615
Winson Chungc763c4e2013-07-19 13:49:06 -07001616 // Check if we need to do any upgrade-path logic
Michael Jurka414300a2013-08-27 15:42:35 +02001617 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -07001618
Winson Chung2abf94d2012-07-18 18:16:38 -07001619 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001620 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001621
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001622 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001623 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Adam Cohene25af792013-06-06 23:08:25 -07001624 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001625
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001626 // +1 for the hotseat (it can be larger than the workspace)
1627 // Load workspace in reverse order to ensure that latest items are loaded first (and
1628 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001631 try {
1632 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1633 final int intentIndex = c.getColumnIndexOrThrow
1634 (LauncherSettings.Favorites.INTENT);
1635 final int titleIndex = c.getColumnIndexOrThrow
1636 (LauncherSettings.Favorites.TITLE);
1637 final int iconTypeIndex = c.getColumnIndexOrThrow(
1638 LauncherSettings.Favorites.ICON_TYPE);
1639 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1640 final int iconPackageIndex = c.getColumnIndexOrThrow(
1641 LauncherSettings.Favorites.ICON_PACKAGE);
1642 final int iconResourceIndex = c.getColumnIndexOrThrow(
1643 LauncherSettings.Favorites.ICON_RESOURCE);
1644 final int containerIndex = c.getColumnIndexOrThrow(
1645 LauncherSettings.Favorites.CONTAINER);
1646 final int itemTypeIndex = c.getColumnIndexOrThrow(
1647 LauncherSettings.Favorites.ITEM_TYPE);
1648 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1649 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001650 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1651 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001652 final int screenIndex = c.getColumnIndexOrThrow(
1653 LauncherSettings.Favorites.SCREEN);
1654 final int cellXIndex = c.getColumnIndexOrThrow
1655 (LauncherSettings.Favorites.CELLX);
1656 final int cellYIndex = c.getColumnIndexOrThrow
1657 (LauncherSettings.Favorites.CELLY);
1658 final int spanXIndex = c.getColumnIndexOrThrow
1659 (LauncherSettings.Favorites.SPANX);
1660 final int spanYIndex = c.getColumnIndexOrThrow(
1661 LauncherSettings.Favorites.SPANY);
1662 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1663 //final int displayModeIndex = c.getColumnIndexOrThrow(
1664 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001666 ShortcutInfo info;
1667 String intentDescription;
1668 LauncherAppWidgetInfo appWidgetInfo;
1669 int container;
1670 long id;
1671 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001672
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001673 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001674 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001675 try {
1676 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001678 switch (itemType) {
1679 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1680 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001681 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001682 intentDescription = c.getString(intentIndex);
1683 try {
1684 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001685 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001686 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001687 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001688 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001689 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1690 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001691 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001692 // If apps can be on external storage, then we just
1693 // leave them for the user to remove (maybe add
1694 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001695 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001696 }
1697 continue;
1698 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001699 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001700 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 continue;
1702 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001704 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1705 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1706 titleIndex, mLabelCache);
1707 } else {
1708 info = getShortcutInfo(c, context, iconTypeIndex,
1709 iconPackageIndex, iconResourceIndex, iconIndex,
1710 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001711
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001712 // App shortcuts that used to be automatically added to Launcher
1713 // didn't always have the correct intent flags set, so do that
1714 // here
1715 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001716 intent.getCategories() != null &&
1717 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001718 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001719 intent.addFlags(
1720 Intent.FLAG_ACTIVITY_NEW_TASK |
1721 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1722 }
Michael Jurka96879562012-03-22 05:54:33 -07001723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001725 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001726 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 container = c.getInt(containerIndex);
1729 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001730 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 info.cellX = c.getInt(cellXIndex);
1732 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001733 info.spanX = 1;
1734 info.spanY = 1;
1735 // Skip loading items that are out of bounds
1736 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1737 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001738 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1739 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001740 continue;
1741 }
1742 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001743 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001744 deleteOnItemOverlap.set(false);
1745 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1746 if (deleteOnItemOverlap.get()) {
1747 itemsToRemove.add(id);
1748 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001749 break;
1750 }
1751
1752 switch (container) {
1753 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1754 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1755 sBgWorkspaceItems.add(info);
1756 break;
1757 default:
1758 // Item is in a user folder
1759 FolderInfo folderInfo =
1760 findOrMakeFolder(sBgFolders, container);
1761 folderInfo.add(info);
1762 break;
1763 }
1764 sBgItemsIdMap.put(info.id, info);
1765
1766 // now that we've loaded everthing re-save it with the
1767 // icon in case it disappears somehow.
1768 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001769 } else {
1770 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 }
1772 break;
1773
1774 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1775 id = c.getLong(idIndex);
1776 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1777
1778 folderInfo.title = c.getString(titleIndex);
1779 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001781 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001782 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 folderInfo.cellX = c.getInt(cellXIndex);
1784 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001785 folderInfo.spanX = 1;
1786 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787
Winson Chung5f8afe62013-08-12 16:19:28 -07001788 // Skip loading items that are out of bounds
1789 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001790 if (checkItemDimensions(folderInfo)) {
1791 Log.d(TAG, "Skipped loading out of bounds folder");
1792 continue;
1793 }
1794 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001795 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001796 deleteOnItemOverlap.set(false);
1797 if (!checkItemPlacement(occupied, folderInfo,
1798 deleteOnItemOverlap)) {
1799 if (deleteOnItemOverlap.get()) {
1800 itemsToRemove.add(id);
1801 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001802 break;
1803 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001804
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1807 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1808 sBgWorkspaceItems.add(folderInfo);
1809 break;
Joe Onorato36115782010-06-17 13:28:48 -04001810 }
Joe Onorato17a89222011-02-08 17:26:11 -08001811
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001812 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1813 sBgFolders.put(folderInfo.id, folderInfo);
1814 break;
1815
1816 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1817 // Read all Launcher-specific widget details
1818 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001819 String savedProvider = c.getString(appWidgetProviderIndex);
1820
Joe Onorato36115782010-06-17 13:28:48 -04001821 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001822
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001823 final AppWidgetProviderInfo provider =
1824 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001825
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001826 if (!isSafeMode && (provider == null || provider.provider == null ||
1827 provider.provider.getPackageName() == null)) {
1828 String log = "Deleting widget that isn't installed anymore: id="
1829 + id + " appWidgetId=" + appWidgetId;
1830 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001831 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001832 itemsToRemove.add(id);
1833 } else {
1834 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1835 provider.provider);
1836 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001837 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 appWidgetInfo.cellX = c.getInt(cellXIndex);
1839 appWidgetInfo.cellY = c.getInt(cellYIndex);
1840 appWidgetInfo.spanX = c.getInt(spanXIndex);
1841 appWidgetInfo.spanY = c.getInt(spanYIndex);
1842 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1843 appWidgetInfo.minSpanX = minSpan[0];
1844 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001845
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001846 container = c.getInt(containerIndex);
1847 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1848 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1849 Log.e(TAG, "Widget found where container != " +
1850 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1851 continue;
1852 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853
Adam Cohene25af792013-06-06 23:08:25 -07001854 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001855 // Skip loading items that are out of bounds
1856 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1857 if (checkItemDimensions(appWidgetInfo)) {
1858 Log.d(TAG, "Skipped loading out of bounds app widget");
1859 continue;
1860 }
1861 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001863 deleteOnItemOverlap.set(false);
1864 if (!checkItemPlacement(occupied, appWidgetInfo,
1865 deleteOnItemOverlap)) {
1866 if (deleteOnItemOverlap.get()) {
1867 itemsToRemove.add(id);
1868 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001869 break;
1870 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001871 String providerName = provider.provider.flattenToString();
1872 if (!providerName.equals(savedProvider)) {
1873 ContentValues values = new ContentValues();
1874 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1875 providerName);
1876 String where = BaseColumns._ID + "= ?";
1877 String[] args = {Integer.toString(c.getInt(idIndex))};
1878 contentResolver.update(contentUri, values, where, args);
1879 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1881 sBgAppWidgets.add(appWidgetInfo);
1882 }
Joe Onorato36115782010-06-17 13:28:48 -04001883 break;
1884 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001885 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001886 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001887 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 }
1889 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001890 if (c != null) {
1891 c.close();
1892 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001893 }
1894
Winson Chungba9c37f2013-08-30 14:11:37 -07001895 // Break early if we've stopped loading
1896 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001897 clearSBgDataStructures();
1898 return false;
1899 }
1900
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001901 if (itemsToRemove.size() > 0) {
1902 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001903 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 // Remove dead items
1905 for (long id : itemsToRemove) {
1906 if (DEBUG_LOADERS) {
1907 Log.d(TAG, "Removed id = " + id);
1908 }
1909 // Don't notify content observers
1910 try {
1911 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1912 null, null);
1913 } catch (RemoteException e) {
1914 Log.w(TAG, "Could not remove id = " + id);
1915 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001916 }
1917 }
1918
Winson Chungc763c4e2013-07-19 13:49:06 -07001919 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 long maxScreenId = 0;
1921 // If we're importing we use the old screen order.
1922 for (ItemInfo item: sBgItemsIdMap.values()) {
1923 long screenId = item.screenId;
1924 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1925 !sBgWorkspaceScreens.contains(screenId)) {
1926 sBgWorkspaceScreens.add(screenId);
1927 if (screenId > maxScreenId) {
1928 maxScreenId = screenId;
1929 }
1930 }
1931 }
1932 Collections.sort(sBgWorkspaceScreens);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001933
Michael Jurka414300a2013-08-27 15:42:35 +02001934 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001936
1937 // Update the max item id after we load an old db
1938 long maxItemId = 0;
1939 // If we're importing we use the old screen order.
1940 for (ItemInfo item: sBgItemsIdMap.values()) {
1941 maxItemId = Math.max(maxItemId, item.id);
1942 }
Michael Jurka414300a2013-08-27 15:42:35 +02001943 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001945 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1946 for (Integer i : orderedScreens.keySet()) {
1947 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 }
1949
1950 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001951 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 for (ItemInfo item: sBgItemsIdMap.values()) {
1953 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1955 unusedScreens.contains(screenId)) {
1956 unusedScreens.remove(screenId);
1957 }
1958 }
1959
1960 // If there are any empty screens remove them, and update.
1961 if (unusedScreens.size() != 0) {
Winson Chung933bae62013-08-29 11:42:30 -07001962 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
1964 }
1965 }
1966
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001967 if (DEBUG_LOADERS) {
1968 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
1969 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07001970 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07001971 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001972 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07001973
Daniel Sandler566da102013-06-25 23:43:45 -04001974 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07001975 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07001977 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001978 line += " | ";
1979 }
Winson Chung892c74d2013-08-22 16:15:50 -07001980 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001982 }
Joe Onorato36115782010-06-17 13:28:48 -04001983 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001984 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04001985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 }
Joe Onorato36115782010-06-17 13:28:48 -04001987 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001988 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07001989 }
1990
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001991 /** Filters the set of items who are directly or indirectly (via another container) on the
1992 * specified screen. */
1993 private void filterCurrentWorkspaceItems(int currentScreen,
1994 ArrayList<ItemInfo> allWorkspaceItems,
1995 ArrayList<ItemInfo> currentScreenItems,
1996 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07001997 // Purge any null ItemInfos
1998 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
1999 while (iter.hasNext()) {
2000 ItemInfo i = iter.next();
2001 if (i == null) {
2002 iter.remove();
2003 }
2004 }
2005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 // If we aren't filtering on a screen, then the set of items to load is the full set of
2007 // items given.
2008 if (currentScreen < 0) {
2009 currentScreenItems.addAll(allWorkspaceItems);
Joe Onorato36115782010-06-17 13:28:48 -04002010 }
2011
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002012 // Order the set of items by their containers first, this allows use to walk through the
2013 // list sequentially, build up a list of containers that are in the specified screen,
2014 // as well as all items in those containers.
2015 Set<Long> itemsOnScreen = new HashSet<Long>();
2016 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2017 @Override
2018 public int compare(ItemInfo lhs, ItemInfo rhs) {
2019 return (int) (lhs.container - rhs.container);
2020 }
2021 });
2022 for (ItemInfo info : allWorkspaceItems) {
2023 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 if (info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 currentScreenItems.add(info);
2026 itemsOnScreen.add(info.id);
2027 } else {
2028 otherScreenItems.add(info);
2029 }
2030 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2031 currentScreenItems.add(info);
2032 itemsOnScreen.add(info.id);
2033 } else {
2034 if (itemsOnScreen.contains(info.container)) {
2035 currentScreenItems.add(info);
2036 itemsOnScreen.add(info.id);
2037 } else {
2038 otherScreenItems.add(info);
2039 }
2040 }
2041 }
2042 }
2043
2044 /** Filters the set of widgets which are on the specified screen. */
2045 private void filterCurrentAppWidgets(int currentScreen,
2046 ArrayList<LauncherAppWidgetInfo> appWidgets,
2047 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2048 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
2049 // If we aren't filtering on a screen, then the set of items to load is the full set of
2050 // widgets given.
2051 if (currentScreen < 0) {
2052 currentScreenWidgets.addAll(appWidgets);
2053 }
2054
2055 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002056 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002058 widget.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002059 currentScreenWidgets.add(widget);
2060 } else {
2061 otherScreenWidgets.add(widget);
2062 }
2063 }
2064 }
2065
2066 /** Filters the set of folders which are on the specified screen. */
2067 private void filterCurrentFolders(int currentScreen,
2068 HashMap<Long, ItemInfo> itemsIdMap,
2069 HashMap<Long, FolderInfo> folders,
2070 HashMap<Long, FolderInfo> currentScreenFolders,
2071 HashMap<Long, FolderInfo> otherScreenFolders) {
2072 // If we aren't filtering on a screen, then the set of items to load is the full set of
2073 // widgets given.
2074 if (currentScreen < 0) {
2075 currentScreenFolders.putAll(folders);
2076 }
2077
2078 for (long id : folders.keySet()) {
2079 ItemInfo info = itemsIdMap.get(id);
2080 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002081 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Adam Cohendcd297f2013-06-18 13:13:40 -07002083 info.screenId == currentScreen) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 currentScreenFolders.put(id, folder);
2085 } else {
2086 otherScreenFolders.put(id, folder);
2087 }
2088 }
2089 }
2090
2091 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2092 * right) */
2093 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002094 final LauncherAppState app = LauncherAppState.getInstance();
2095 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 // XXX: review this
2097 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002098 @Override
2099 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002100 int cellCountX = (int) grid.numColumns;
2101 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002102 int screenOffset = cellCountX * cellCountY;
2103 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002104 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002105 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002106 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002107 rhs.cellY * cellCountX + rhs.cellX);
2108 return (int) (lr - rr);
2109 }
2110 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002112
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2114 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002115 final Runnable r = new Runnable() {
2116 @Override
2117 public void run() {
2118 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2119 if (callbacks != null) {
2120 callbacks.bindScreens(orderedScreens);
2121 }
2122 }
2123 };
2124 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2125 }
2126
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2128 final ArrayList<ItemInfo> workspaceItems,
2129 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2130 final HashMap<Long, FolderInfo> folders,
2131 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002132
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 final boolean postOnMainThread = (deferredBindRunnables != null);
2134
2135 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002136 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002137 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002138 final int start = i;
2139 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002140 final Runnable r = new Runnable() {
2141 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002143 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002144 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002145 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2146 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 }
2148 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 };
2150 if (postOnMainThread) {
2151 deferredBindRunnables.add(r);
2152 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002153 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 }
Joe Onorato36115782010-06-17 13:28:48 -04002155 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156
2157 // Bind the folders
2158 if (!folders.isEmpty()) {
2159 final Runnable r = new Runnable() {
2160 public void run() {
2161 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2162 if (callbacks != null) {
2163 callbacks.bindFolders(folders);
2164 }
2165 }
2166 };
2167 if (postOnMainThread) {
2168 deferredBindRunnables.add(r);
2169 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002170 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 }
2172 }
2173
2174 // Bind the widgets, one at a time
2175 N = appWidgets.size();
2176 for (int i = 0; i < N; i++) {
2177 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2178 final Runnable r = new Runnable() {
2179 public void run() {
2180 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2181 if (callbacks != null) {
2182 callbacks.bindAppWidget(widget);
2183 }
2184 }
2185 };
2186 if (postOnMainThread) {
2187 deferredBindRunnables.add(r);
2188 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002189 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002190 }
2191 }
2192 }
2193
2194 /**
2195 * Binds all loaded data to actual views on the main thread.
2196 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002197 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002198 final long t = SystemClock.uptimeMillis();
2199 Runnable r;
2200
2201 // Don't use these two variables in any of the callback runnables.
2202 // Otherwise we hold a reference to them.
2203 final Callbacks oldCallbacks = mCallbacks.get();
2204 if (oldCallbacks == null) {
2205 // This launcher has exited and nobody bothered to tell us. Just bail.
2206 Log.w(TAG, "LoaderTask running with no launcher");
2207 return;
2208 }
2209
Winson Chung4a2afa32012-07-19 14:53:05 -07002210 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
2211 final int currentScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 oldCallbacks.getCurrentWorkspaceScreen();
2213
2214 // Load all the items that are on the current page first (and in the process, unbind
2215 // all the existing workspace items before we call startBinding() below.
2216 unbindWorkspaceItemsOnMainThread();
2217 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2218 ArrayList<LauncherAppWidgetInfo> appWidgets =
2219 new ArrayList<LauncherAppWidgetInfo>();
2220 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2221 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002223 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 workspaceItems.addAll(sBgWorkspaceItems);
2225 appWidgets.addAll(sBgAppWidgets);
2226 folders.putAll(sBgFolders);
2227 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 }
2230
2231 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2232 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2233 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2234 new ArrayList<LauncherAppWidgetInfo>();
2235 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2238 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2239
2240 // Separate the items that are on the current screen, and all the other remaining items
2241 filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems,
2242 otherWorkspaceItems);
2243 filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets,
2244 otherAppWidgets);
2245 filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders,
2246 otherFolders);
2247 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2248 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2249
2250 // Tell the workspace that we're about to start binding items
2251 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002252 public void run() {
2253 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2254 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002255 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002256 }
2257 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 };
Winson Chung81b52252012-08-27 15:34:29 -07002259 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002260
Adam Cohendcd297f2013-06-18 13:13:40 -07002261 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2262
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 // Load items on the current page
2264 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2265 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002266 if (isLoadingSynchronously) {
2267 r = new Runnable() {
2268 public void run() {
2269 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2270 if (callbacks != null) {
2271 callbacks.onPageBoundSynchronously(currentScreen);
2272 }
2273 }
2274 };
Winson Chung81b52252012-08-27 15:34:29 -07002275 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002276 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277
Winson Chung4a2afa32012-07-19 14:53:05 -07002278 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2279 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 mDeferredBindRunnables.clear();
2281 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002282 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002283
2284 // Tell the workspace that we're done binding items
2285 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002286 public void run() {
2287 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2288 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002289 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002290 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291
Winson Chung98e030b2012-05-07 16:01:11 -07002292 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002293 if (DEBUG_LOADERS) {
2294 Log.d(TAG, "bound workspace in "
2295 + (SystemClock.uptimeMillis()-t) + "ms");
2296 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002297
2298 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002299 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002301 if (isLoadingSynchronously) {
2302 mDeferredBindRunnables.add(r);
2303 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002304 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002305 }
Joe Onorato36115782010-06-17 13:28:48 -04002306 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002307
Joe Onorato36115782010-06-17 13:28:48 -04002308 private void loadAndBindAllApps() {
2309 if (DEBUG_LOADERS) {
2310 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2311 }
2312 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002313 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002314 synchronized (LoaderTask.this) {
2315 if (mStopped) {
2316 return;
2317 }
2318 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002319 }
Joe Onorato36115782010-06-17 13:28:48 -04002320 } else {
2321 onlyBindAllApps();
2322 }
2323 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002324
Joe Onorato36115782010-06-17 13:28:48 -04002325 private void onlyBindAllApps() {
2326 final Callbacks oldCallbacks = mCallbacks.get();
2327 if (oldCallbacks == null) {
2328 // This launcher has exited and nobody bothered to tell us. Just bail.
2329 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2330 return;
2331 }
2332
2333 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002334 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002335 final ArrayList<AppInfo> list
2336 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002337 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002338 public void run() {
2339 final long t = SystemClock.uptimeMillis();
2340 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2341 if (callbacks != null) {
2342 callbacks.bindAllApplications(list);
2343 }
2344 if (DEBUG_LOADERS) {
2345 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2346 + (SystemClock.uptimeMillis()-t) + "ms");
2347 }
2348 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002349 };
2350 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002351 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002352 r.run();
2353 } else {
2354 mHandler.post(r);
2355 }
Joe Onorato36115782010-06-17 13:28:48 -04002356 }
2357
Winson Chung64359a52013-07-08 17:17:08 -07002358 private void loadAllApps() {
2359 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002360
Joe Onorato36115782010-06-17 13:28:48 -04002361 final Callbacks oldCallbacks = mCallbacks.get();
2362 if (oldCallbacks == null) {
2363 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002364 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002365 return;
2366 }
2367
Winson Chung64359a52013-07-08 17:17:08 -07002368 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002369 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2370 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2371
Winson Chung64359a52013-07-08 17:17:08 -07002372 // Clear the list of apps
2373 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002374
Winson Chung64359a52013-07-08 17:17:08 -07002375 // Query for the set of apps
2376 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2377 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2378 if (DEBUG_LOADERS) {
2379 Log.d(TAG, "queryIntentActivities took "
2380 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2381 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2382 }
2383 // Fail if we don't have any apps
2384 if (apps == null || apps.isEmpty()) {
2385 return;
2386 }
2387 // Sort the applications by name
2388 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2389 Collections.sort(apps,
2390 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2391 if (DEBUG_LOADERS) {
2392 Log.d(TAG, "sort took "
2393 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002394 }
2395
Winson Chung64359a52013-07-08 17:17:08 -07002396 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002397 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002398 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002399 // This builds the icon bitmaps.
2400 mBgAllAppsList.add(new AppInfo(packageManager, app,
2401 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002402 }
2403
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002404 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2406 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002407
2408 // Post callback on main thread
2409 mHandler.post(new Runnable() {
2410 public void run() {
2411 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002412 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002413 if (callbacks != null) {
2414 callbacks.bindAllApplications(added);
2415 if (DEBUG_LOADERS) {
2416 Log.d(TAG, "bound " + added.size() + " apps in "
2417 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2418 }
2419 } else {
2420 Log.i(TAG, "not binding apps: no Launcher activity");
2421 }
2422 }
2423 });
2424
Joe Onorato36115782010-06-17 13:28:48 -04002425 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002426 Log.d(TAG, "Icons processed in "
2427 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002428 }
2429 }
2430
2431 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002432 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002433 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2434 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2435 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2436 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2437 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2438 }
Joe Onorato36115782010-06-17 13:28:48 -04002439 }
2440 }
2441
2442 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002443 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002444 }
2445
2446 private class PackageUpdatedTask implements Runnable {
2447 int mOp;
2448 String[] mPackages;
2449
2450 public static final int OP_NONE = 0;
2451 public static final int OP_ADD = 1;
2452 public static final int OP_UPDATE = 2;
2453 public static final int OP_REMOVE = 3; // uninstlled
2454 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2455
2456
2457 public PackageUpdatedTask(int op, String[] packages) {
2458 mOp = op;
2459 mPackages = packages;
2460 }
2461
2462 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002463 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002464
2465 final String[] packages = mPackages;
2466 final int N = packages.length;
2467 switch (mOp) {
2468 case OP_ADD:
2469 for (int i=0; i<N; i++) {
2470 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002471 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002472 }
2473 break;
2474 case OP_UPDATE:
2475 for (int i=0; i<N; i++) {
2476 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002477 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002478 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002479 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002480 }
2481 break;
2482 case OP_REMOVE:
2483 case OP_UNAVAILABLE:
2484 for (int i=0; i<N; i++) {
2485 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002486 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002487 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002488 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002489 }
2490 break;
2491 }
2492
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002493 ArrayList<AppInfo> added = null;
2494 ArrayList<AppInfo> modified = null;
2495 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002496
Adam Cohen487f7dd2012-06-28 18:12:10 -07002497 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002498 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002499 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002500 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002501 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002502 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002503 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002504 }
Winson Chung5d55f332012-07-16 20:45:03 -07002505 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002506 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002507 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002508 }
2509
Joe Onorato36115782010-06-17 13:28:48 -04002510 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2511 if (callbacks == null) {
2512 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2513 return;
2514 }
2515
2516 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002517 // Ensure that we add all the workspace applications to the db
2518 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002519 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2520 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2521 } else {
2522 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2523 addAndBindAddedApps(context, addedInfos, cb, added);
2524 }
Joe Onorato36115782010-06-17 13:28:48 -04002525 }
2526 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002527 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002528
2529 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002530 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002531 ArrayList<ItemInfo> infos =
2532 getItemInfoForComponentName(a.componentName);
2533 for (ItemInfo i : infos) {
2534 if (isShortcutInfoUpdateable(i)) {
2535 ShortcutInfo info = (ShortcutInfo) i;
2536 info.title = a.title.toString();
2537 updateItemInDatabase(context, info);
2538 }
2539 }
2540 }
2541
Joe Onorato36115782010-06-17 13:28:48 -04002542 mHandler.post(new Runnable() {
2543 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002544 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2545 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002546 callbacks.bindAppsUpdated(modifiedFinal);
2547 }
2548 }
2549 });
2550 }
Winson Chung83892cc2013-05-01 16:53:33 -07002551 // If a package has been removed, or an app has been removed as a result of
2552 // an update (for example), make the removed callback.
2553 if (mOp == OP_REMOVE || !removedApps.isEmpty()) {
Winson Chung64359a52013-07-08 17:17:08 -07002554 final boolean packageRemoved = (mOp == OP_REMOVE);
Winson Chung83892cc2013-05-01 16:53:33 -07002555 final ArrayList<String> removedPackageNames =
2556 new ArrayList<String>(Arrays.asList(packages));
2557
Winson Chung64359a52013-07-08 17:17:08 -07002558 // Update the launcher db to reflect the removal of apps
2559 if (packageRemoved) {
2560 for (String pn : removedPackageNames) {
2561 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2562 for (ItemInfo i : infos) {
2563 deleteItemFromDatabase(context, i);
2564 }
2565 }
Winson Chung780fe592013-09-26 14:48:44 -07002566
2567 // Remove any queued items from the install queue
2568 String spKey = LauncherAppState.getSharedPreferencesKey();
2569 SharedPreferences sp =
2570 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2571 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
Winson Chung64359a52013-07-08 17:17:08 -07002572 } else {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002573 for (AppInfo a : removedApps) {
Winson Chung64359a52013-07-08 17:17:08 -07002574 ArrayList<ItemInfo> infos =
2575 getItemInfoForComponentName(a.componentName);
2576 for (ItemInfo i : infos) {
2577 deleteItemFromDatabase(context, i);
2578 }
2579 }
2580 }
2581
Joe Onorato36115782010-06-17 13:28:48 -04002582 mHandler.post(new Runnable() {
2583 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002584 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2585 if (callbacks == cb && cb != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07002586 callbacks.bindComponentsRemoved(removedPackageNames,
Winson Chung64359a52013-07-08 17:17:08 -07002587 removedApps, packageRemoved);
Joe Onorato36115782010-06-17 13:28:48 -04002588 }
2589 }
2590 });
Joe Onoratobe386092009-11-17 17:32:16 -08002591 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002592
Michael Jurkac402cd92013-05-20 15:49:32 +02002593 final ArrayList<Object> widgetsAndShortcuts =
2594 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002595 mHandler.post(new Runnable() {
2596 @Override
2597 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002598 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2599 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002600 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002601 }
2602 }
2603 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002604
2605 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002606 mHandler.post(new Runnable() {
2607 public void run() {
2608 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2609 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002610 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002611 }
2612 }
2613 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 }
2615 }
2616
Michael Jurkac402cd92013-05-20 15:49:32 +02002617 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2618 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2619 PackageManager packageManager = context.getPackageManager();
2620 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2621 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2622 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2623 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2624 Collections.sort(widgetsAndShortcuts,
2625 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2626 return widgetsAndShortcuts;
2627 }
2628
Winson Chung1323b482013-08-05 12:41:55 -07002629 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002630 if (cn == null) {
2631 return false;
2632 }
2633
2634 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002635 // Skip if the application is disabled
2636 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
2637 if (!pi.applicationInfo.enabled) {
2638 return false;
2639 }
2640
2641 // Check the activity
Winson Chungee055712013-07-30 14:46:24 -07002642 return (pm.getActivityInfo(cn, 0) != null);
2643 } catch (NameNotFoundException e) {
2644 return false;
2645 }
2646 }
2647
Joe Onorato9c1289c2009-08-17 11:03:03 -04002648 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002649 * This is called from the code that adds shortcuts from the intent receiver. This
2650 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651 */
Joe Onorato56d82912010-03-07 14:32:10 -05002652 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002653 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002655
Joe Onorato56d82912010-03-07 14:32:10 -05002656 /**
2657 * Make an ShortcutInfo object for a shortcut that is an application.
2658 *
2659 * If c is not null, then it will be used to fill in missing data like the title and icon.
2660 */
2661 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002662 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002663 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002664 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002665 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002666 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002667 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002668 } else {
2669 try {
2670 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2671 info.initFlagsAndFirstInstallTime(pi);
2672 } catch (NameNotFoundException e) {
2673 Log.d(TAG, "getPackInfo failed for package " +
2674 componentName.getPackageName());
2675 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002676 }
2677
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002678 // TODO: See if the PackageManager knows about this case. If it doesn't
2679 // then return null & delete this.
2680
Joe Onorato56d82912010-03-07 14:32:10 -05002681 // the resource -- This may implicitly give us back the fallback icon,
2682 // but don't worry about that. All we're doing with usingFallbackIcon is
2683 // to avoid saving lots of copies of that in the database, and most apps
2684 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002685
2686 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2687 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2688 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002689 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002690 ResolveInfo resolveInfo = null;
2691 ComponentName oldComponent = intent.getComponent();
2692 Intent newIntent = new Intent(intent.getAction(), null);
2693 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2694 newIntent.setPackage(oldComponent.getPackageName());
2695 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2696 for (ResolveInfo i : infos) {
2697 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2698 i.activityInfo.name);
2699 if (cn.equals(oldComponent)) {
2700 resolveInfo = i;
2701 }
2702 }
2703 if (resolveInfo == null) {
2704 resolveInfo = manager.resolveActivity(intent, 0);
2705 }
Joe Onorato56d82912010-03-07 14:32:10 -05002706 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002707 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002708 }
Joe Onorato56d82912010-03-07 14:32:10 -05002709 // the db
2710 if (icon == null) {
2711 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002712 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002713 }
2714 }
2715 // the fallback icon
2716 if (icon == null) {
2717 icon = getFallbackIcon();
2718 info.usingFallbackIcon = true;
2719 }
2720 info.setIcon(icon);
2721
2722 // from the resource
2723 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002724 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2725 if (labelCache != null && labelCache.containsKey(key)) {
2726 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002727 } else {
2728 info.title = resolveInfo.activityInfo.loadLabel(manager);
2729 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002730 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002731 }
2732 }
Joe Onorato56d82912010-03-07 14:32:10 -05002733 }
2734 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002735 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002736 if (c != null) {
2737 info.title = c.getString(titleIndex);
2738 }
2739 }
2740 // fall back to the class name of the activity
2741 if (info.title == null) {
2742 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002743 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002744 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2745 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002747
Winson Chung64359a52013-07-08 17:17:08 -07002748 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2749 ItemInfoFilter f) {
2750 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2751 for (ItemInfo i : infos) {
2752 if (i instanceof ShortcutInfo) {
2753 ShortcutInfo info = (ShortcutInfo) i;
2754 ComponentName cn = info.intent.getComponent();
2755 if (cn != null && f.filterItem(null, info, cn)) {
2756 filtered.add(info);
2757 }
2758 } else if (i instanceof FolderInfo) {
2759 FolderInfo info = (FolderInfo) i;
2760 for (ShortcutInfo s : info.contents) {
2761 ComponentName cn = s.intent.getComponent();
2762 if (cn != null && f.filterItem(info, s, cn)) {
2763 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002764 }
2765 }
Winson Chung64359a52013-07-08 17:17:08 -07002766 } else if (i instanceof LauncherAppWidgetInfo) {
2767 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2768 ComponentName cn = info.providerName;
2769 if (cn != null && f.filterItem(null, info, cn)) {
2770 filtered.add(info);
2771 }
Winson Chung8a435102012-08-30 17:16:53 -07002772 }
2773 }
Winson Chung64359a52013-07-08 17:17:08 -07002774 return new ArrayList<ItemInfo>(filtered);
2775 }
2776
2777 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002778 ItemInfoFilter filter = new ItemInfoFilter() {
2779 @Override
2780 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2781 return cn.getPackageName().equals(pn);
2782 }
2783 };
2784 return filterItemInfos(sBgItemsIdMap.values(), filter);
2785 }
2786
2787 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002788 ItemInfoFilter filter = new ItemInfoFilter() {
2789 @Override
2790 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2791 return cn.equals(cname);
2792 }
2793 };
2794 return filterItemInfos(sBgItemsIdMap.values(), filter);
2795 }
2796
2797 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2798 if (i instanceof ShortcutInfo) {
2799 ShortcutInfo info = (ShortcutInfo) i;
2800 // We need to check for ACTION_MAIN otherwise getComponent() might
2801 // return null for some shortcuts (for instance, for shortcuts to
2802 // web pages.)
2803 Intent intent = info.intent;
2804 ComponentName name = intent.getComponent();
2805 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2806 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2807 return true;
2808 }
2809 }
2810 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002811 }
2812
2813 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002814 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002816 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002817 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2818 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002819
Joe Onorato56d82912010-03-07 14:32:10 -05002820 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002821 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002824 // TODO: If there's an explicit component and we can't install that, delete it.
2825
Joe Onorato56d82912010-03-07 14:32:10 -05002826 info.title = c.getString(titleIndex);
2827
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 int iconType = c.getInt(iconTypeIndex);
2829 switch (iconType) {
2830 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2831 String packageName = c.getString(iconPackageIndex);
2832 String resourceName = c.getString(iconResourceIndex);
2833 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002834 info.customIcon = false;
2835 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002838 if (resources != null) {
2839 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002840 icon = Utilities.createIconBitmap(
2841 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002844 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
Joe Onorato56d82912010-03-07 14:32:10 -05002846 // the db
2847 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002848 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002849 }
2850 // the fallback icon
2851 if (icon == null) {
2852 icon = getFallbackIcon();
2853 info.usingFallbackIcon = true;
2854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 break;
2856 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002857 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002858 if (icon == null) {
2859 icon = getFallbackIcon();
2860 info.customIcon = false;
2861 info.usingFallbackIcon = true;
2862 } else {
2863 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 break;
2866 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002867 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002868 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 info.customIcon = false;
2870 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002872 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 return info;
2874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875
Michael Jurka931dc972011-08-05 15:08:15 -07002876 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002877 @SuppressWarnings("all") // suppress dead code warning
2878 final boolean debug = false;
2879 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002880 Log.d(TAG, "getIconFromCursor app="
2881 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2882 }
2883 byte[] data = c.getBlob(iconIndex);
2884 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002885 return Utilities.createIconBitmap(
2886 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002887 } catch (Exception e) {
2888 return null;
2889 }
2890 }
2891
Winson Chung3d503fb2011-07-13 17:25:49 -07002892 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2893 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002894 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002895 if (info == null) {
2896 return null;
2897 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002898 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002899
2900 return info;
2901 }
2902
Winson Chunga9abd0e2010-10-27 17:18:37 -07002903 /**
Winson Chung55cef262010-10-28 14:14:18 -07002904 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2905 */
2906 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2907 ComponentName component) {
2908 List<AppWidgetProviderInfo> widgets =
2909 AppWidgetManager.getInstance(context).getInstalledProviders();
2910 for (AppWidgetProviderInfo info : widgets) {
2911 if (info.provider.equals(component)) {
2912 return info;
2913 }
2914 }
2915 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002916 }
2917
Winson Chung68846fd2010-10-29 11:00:27 -07002918 /**
2919 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2920 */
2921 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2922 final PackageManager packageManager = context.getPackageManager();
2923 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2924 new ArrayList<WidgetMimeTypeHandlerData>();
2925
2926 final Intent supportsIntent =
2927 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2928 supportsIntent.setType(mimeType);
2929
2930 // Create a set of widget configuration components that we can test against
2931 final List<AppWidgetProviderInfo> widgets =
2932 AppWidgetManager.getInstance(context).getInstalledProviders();
2933 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2934 new HashMap<ComponentName, AppWidgetProviderInfo>();
2935 for (AppWidgetProviderInfo info : widgets) {
2936 configurationComponentToWidget.put(info.configure, info);
2937 }
2938
2939 // Run through each of the intents that can handle this type of clip data, and cross
2940 // reference them with the components that are actual configuration components
2941 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2942 PackageManager.MATCH_DEFAULT_ONLY);
2943 for (ResolveInfo info : activities) {
2944 final ActivityInfo activityInfo = info.activityInfo;
2945 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2946 activityInfo.name);
2947 if (configurationComponentToWidget.containsKey(infoComponent)) {
2948 supportedConfigurationActivities.add(
2949 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2950 configurationComponentToWidget.get(infoComponent)));
2951 }
2952 }
2953 return supportedConfigurationActivities;
2954 }
2955
Winson Chunga9abd0e2010-10-27 17:18:37 -07002956 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002957 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
2958 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
2959 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
2960
Adam Cohend9198822011-11-22 16:42:47 -08002961 if (intent == null) {
2962 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
2963 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
2964 return null;
2965 }
2966
Joe Onorato0589f0f2010-02-08 13:44:00 -08002967 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08002968 boolean customIcon = false;
2969 ShortcutIconResource iconResource = null;
2970
2971 if (bitmap != null && bitmap instanceof Bitmap) {
2972 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002973 customIcon = true;
2974 } else {
2975 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
2976 if (extra != null && extra instanceof ShortcutIconResource) {
2977 try {
2978 iconResource = (ShortcutIconResource) extra;
2979 final PackageManager packageManager = context.getPackageManager();
2980 Resources resources = packageManager.getResourcesForApplication(
2981 iconResource.packageName);
2982 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002983 icon = Utilities.createIconBitmap(
2984 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002985 } catch (Exception e) {
2986 Log.w(TAG, "Could not load shortcut icon: " + extra);
2987 }
2988 }
2989 }
2990
Michael Jurkac9d95c52011-08-29 14:03:34 -07002991 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05002992
2993 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002994 if (fallbackIcon != null) {
2995 icon = fallbackIcon;
2996 } else {
2997 icon = getFallbackIcon();
2998 info.usingFallbackIcon = true;
2999 }
Joe Onorato56d82912010-03-07 14:32:10 -05003000 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003001 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003002
Joe Onorato0589f0f2010-02-08 13:44:00 -08003003 info.title = name;
3004 info.intent = intent;
3005 info.customIcon = customIcon;
3006 info.iconResource = iconResource;
3007
3008 return info;
3009 }
3010
Winson Chungaac01e12011-08-17 10:37:13 -07003011 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3012 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003013 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003014 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003015 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003016 }
Joe Onorato56d82912010-03-07 14:32:10 -05003017 // If this icon doesn't have a custom icon, check to see
3018 // what's stored in the DB, and if it doesn't match what
3019 // we're going to show, store what we are going to show back
3020 // into the DB. We do this so when we're loading, if the
3021 // package manager can't find an icon (for example because
3022 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003023 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003024 cache.put(info, c.getBlob(iconIndex));
3025 return true;
3026 }
3027 return false;
3028 }
3029 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3030 boolean needSave = false;
3031 try {
3032 if (data != null) {
3033 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3034 Bitmap loaded = info.getIcon(mIconCache);
3035 needSave = !saved.sameAs(loaded);
3036 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003037 needSave = true;
3038 }
Winson Chungaac01e12011-08-17 10:37:13 -07003039 } catch (Exception e) {
3040 needSave = true;
3041 }
3042 if (needSave) {
3043 Log.d(TAG, "going to save icon bitmap for info=" + info);
3044 // This is slower than is ideal, but this only happens once
3045 // or when the app is updated with a new icon.
3046 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003047 }
3048 }
3049
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003051 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 * or make a new one.
3053 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003054 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 // See if a placeholder was created for us already
3056 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003057 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003058 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003059 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 folders.put(id, folderInfo);
3061 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003062 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 }
3064
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003065 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003066 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003067 return new Comparator<AppInfo>() {
3068 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003069 int result = collator.compare(a.title.toString().trim(),
3070 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003071 if (result == 0) {
3072 result = a.componentName.compareTo(b.componentName);
3073 }
3074 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003075 }
Winson Chung11904872012-09-17 16:58:46 -07003076 };
3077 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003078 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3079 = new Comparator<AppInfo>() {
3080 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003081 if (a.firstInstallTime < b.firstInstallTime) return 1;
3082 if (a.firstInstallTime > b.firstInstallTime) return -1;
3083 return 0;
3084 }
3085 };
Winson Chung11904872012-09-17 16:58:46 -07003086 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3087 final Collator collator = Collator.getInstance();
3088 return new Comparator<AppWidgetProviderInfo>() {
3089 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003090 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003091 }
3092 };
3093 }
Winson Chung5308f242011-08-18 12:12:41 -07003094 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3095 if (info.activityInfo != null) {
3096 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3097 } else {
3098 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3099 }
3100 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003101 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003102 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003103 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003104 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003105 ShortcutNameComparator(PackageManager pm) {
3106 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003107 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003108 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003109 }
3110 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3111 mPackageManager = pm;
3112 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003113 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003114 }
3115 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003116 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003117 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3118 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3119 if (mLabelCache.containsKey(keyA)) {
3120 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003121 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003122 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003123
Winson Chung5308f242011-08-18 12:12:41 -07003124 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003125 }
Winson Chung5308f242011-08-18 12:12:41 -07003126 if (mLabelCache.containsKey(keyB)) {
3127 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003128 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003129 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003130
Winson Chung5308f242011-08-18 12:12:41 -07003131 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003132 }
Winson Chung11904872012-09-17 16:58:46 -07003133 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003134 }
3135 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003136 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003137 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003138 private PackageManager mPackageManager;
3139 private HashMap<Object, String> mLabelCache;
3140 WidgetAndShortcutNameComparator(PackageManager pm) {
3141 mPackageManager = pm;
3142 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003143 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003144 }
3145 public final int compare(Object a, Object b) {
3146 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003147 if (mLabelCache.containsKey(a)) {
3148 labelA = mLabelCache.get(a);
3149 } else {
3150 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003151 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003152 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003153 mLabelCache.put(a, labelA);
3154 }
3155 if (mLabelCache.containsKey(b)) {
3156 labelB = mLabelCache.get(b);
3157 } else {
3158 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003159 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003160 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003161 mLabelCache.put(b, labelB);
3162 }
Winson Chung11904872012-09-17 16:58:46 -07003163 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003164 }
3165 };
Joe Onoratobe386092009-11-17 17:32:16 -08003166
3167 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003168 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003169 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3170 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3171 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3172 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003173 if (mLoaderTask != null) {
3174 mLoaderTask.dumpState();
3175 } else {
3176 Log.d(TAG, "mLoaderTask=null");
3177 }
Joe Onoratobe386092009-11-17 17:32:16 -08003178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179}