blob: 3e6cf03c8f6ac9cf6e24ccc04c5bef089efb59d6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080047
Michael Jurkac2f801e2011-07-12 14:19:46 -070048import java.lang.ref.WeakReference;
49import java.net.URISyntaxException;
50import java.text.Collator;
51import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070052import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070053import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070058import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070061import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070062import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Maintains in-memory state of the Launcher. It is expected that there should be only one
66 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070067 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040069public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070072
Daniel Sandler8707e0f2013-08-15 15:54:18 -070073 // true = use a "More Apps" folder for non-workspace apps on upgrade
74 // false = strew non-workspace apps across the workspace on upgrade
75 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
76
Joe Onorato36115782010-06-17 13:28:48 -040077 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070078 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020085 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung81b52252012-08-27 15:34:29 -070087 // Specific runnable types that are run on the main thread deferred handler, this allows us to
88 // clear all queued binding runnables when the Launcher activity is destroyed.
89 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
90 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
91
92
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070093 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
94 static {
95 sWorkerThread.start();
96 }
97 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
98
Joe Onoratocc67f472010-06-08 10:54:30 -070099 // We start off with everything not loaded. After that, we assume that
100 // our monitoring of the package manager provides all updates and we never
101 // need to do a requery. These are only ever touched from the loader thread.
102 private boolean mWorkspaceLoaded;
103 private boolean mAllAppsLoaded;
104
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700105 // When we are loading pages synchronously, we can't just post the binding of items on the side
106 // pages as this delays the rotation process. Instead, we wait for a callback from the first
107 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
108 // a normal load, we also clear this set of Runnables.
109 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700114 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800115
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700116 // The lock that must be acquired before referencing any static bg data structures. Unlike
117 // other locks, this one can generally be held long-term because we never expect any of these
118 // static data structures to be referenced outside of the worker thread except on the first
119 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700120 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
127 // created by LauncherModel that are directly on the home screen (however, no widgets or
128 // shortcuts within folders).
129 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
132 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 new ArrayList<LauncherAppWidgetInfo>();
134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
136 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
139 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700140
141 // sBgWorkspaceScreens is the ordered set of workspace screens.
142 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // </ only access in worker thread >
145
146 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Reena Lee99a73f32011-10-24 17:27:37 -0700149 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700152 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public int getCurrentWorkspaceScreen();
154 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
156 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700157 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700158 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500159 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700160 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200162 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700163 public void bindAppsAdded(ArrayList<Long> newScreens,
164 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700165 ArrayList<ItemInfo> addAnimated,
166 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700168 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700169 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700172 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700173 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700174 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung64359a52013-07-08 17:17:08 -0700177 public interface ItemInfoFilter {
178 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
179 }
180
Bjorn Bringert1307f632013-10-03 22:31:03 +0100181 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 final Context context = app.getContext();
183
Winson Chungee055712013-07-30 14:46:24 -0700184 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100186 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 mIconCache = iconCache;
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700190 Configuration config = res.getConfiguration();
191 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 }
193
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
195 * posted on the main thread handler. */
196 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700197 runOnMainThread(r, 0);
198 }
199 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 // If we are on the worker thread, post onto the main handler
202 mHandler.post(r);
203 } else {
204 r.run();
205 }
206 }
207
208 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
209 * posted on the worker thread handler. */
210 private static void runOnWorkerThread(Runnable r) {
211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 r.run();
213 } else {
214 // If we are not on the worker thread, then post to the worker handler
215 sWorker.post(r);
216 }
217 }
218
Winson Chungc9168342013-06-26 14:54:55 -0700219 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
220 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700221 LauncherAppState app = LauncherAppState.getInstance();
222 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
223 final int xCount = (int) grid.numColumns;
224 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700225 boolean[][] occupied = new boolean[xCount][yCount];
226
227 int cellX, cellY, spanX, spanY;
228 for (int i = 0; i < items.size(); ++i) {
229 final ItemInfo item = items.get(i);
230 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
231 if (item.screenId == screen) {
232 cellX = item.cellX;
233 cellY = item.cellY;
234 spanX = item.spanX;
235 spanY = item.spanY;
236 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
237 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
238 occupied[x][y] = true;
239 }
240 }
241 }
242 }
243 }
244
245 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
246 }
247 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700248 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700249 int firstScreenIndex,
250 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700251 // Lock on the app so that we don't try and get the items while apps are being added
252 LauncherAppState app = LauncherAppState.getInstance();
253 LauncherModel model = app.getModel();
254 boolean found = false;
255 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700256 if (sWorkerThread.getThreadId() != Process.myTid()) {
257 // Flush the LauncherModel worker thread, so that if we just did another
258 // processInstallShortcut, we give it time for its shortcut to get added to the
259 // database (getItemsInLocalCoordinates reads the database)
260 model.flushWorkerThread();
261 }
Winson Chungc9168342013-06-26 14:54:55 -0700262 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700263
264 // Try adding to the workspace screens incrementally, starting at the default or center
265 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700266 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
267 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700269 int[] tmpCoordinates = new int[2];
270 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700271 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700273 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700274 }
275 }
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 return null;
278 }
279
Winson Chung94d67682013-09-25 16:29:40 -0700280 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
281 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700282 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700283 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700284 }
Winson Chung94d67682013-09-25 16:29:40 -0700285 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800286 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
287 if (workspaceApps == null || allAppsApps == null) {
288 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
289 }
Winson Chung94d67682013-09-25 16:29:40 -0700290 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700291 return;
Winson Chung997a9232013-07-24 15:33:46 -0700292 }
Winson Chung64359a52013-07-08 17:17:08 -0700293 // Process the newly added applications and add them to the database first
294 Runnable r = new Runnable() {
295 public void run() {
296 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
297 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
298
Winson Chung76828c82013-08-19 15:43:29 -0700299 // Get the list of workspace screens. We need to append to this list and
300 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
301 // called.
302 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
303 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
304 for (Integer i : orderedScreens.keySet()) {
305 long screenId = orderedScreens.get(i);
306 workspaceScreens.add(screenId);
307 }
308
Winson Chung64359a52013-07-08 17:17:08 -0700309 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700310 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700311 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700312 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700313 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700314 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700315
316 // Short-circuit this logic if the icon exists somewhere on the workspace
317 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
318 continue;
319 }
320
Winson Chung87412982013-10-03 18:34:14 -0700321 // Add this icon to the db, creating a new page if necessary. If there
322 // is only the empty page then we just add items to the first page.
323 // Otherwise, we add them to the next pages.
324 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700325 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700326 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700327 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200328 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700329
330 // If we can't find a valid position, then just add a new screen.
331 // This takes time so we need to re-queue the add until the new
332 // page is added. Create as many screens as necessary to satisfy
333 // the startSearchPageIndex.
334 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700335 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 while (numPagesToAdd > 0) {
337 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700339 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 addedWorkspaceScreensFinal.add(screenId);
341 numPagesToAdd--;
342 }
Winson Chung76828c82013-08-19 15:43:29 -0700343
Winson Chung64359a52013-07-08 17:17:08 -0700344 // Find the coordinate again
345 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700346 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700347 }
348 if (coords == null) {
349 throw new RuntimeException("Coordinates should not be null");
350 }
351
Winson Chung997a9232013-07-24 15:33:46 -0700352 ShortcutInfo shortcutInfo;
353 if (a instanceof ShortcutInfo) {
354 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200355 } else if (a instanceof AppInfo) {
356 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700357 } else {
358 throw new RuntimeException("Unexpected info type");
359 }
Winson Chung94d67682013-09-25 16:29:40 -0700360
Winson Chung64359a52013-07-08 17:17:08 -0700361 // Add the shortcut to the db
362 addItemToDatabase(context, shortcutInfo,
363 LauncherSettings.Favorites.CONTAINER_DESKTOP,
364 coords.first, coords.second[0], coords.second[1], false);
365 // Save the ShortcutInfo for binding in the workspace
366 addedShortcutsFinal.add(shortcutInfo);
367 }
368 }
369
Winson Chung76828c82013-08-19 15:43:29 -0700370 // Update the workspace screens
371 updateWorkspaceScreenOrder(context, workspaceScreens);
372
Winson Chung94d67682013-09-25 16:29:40 -0700373 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 runOnMainThread(new Runnable() {
375 public void run() {
376 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
377 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700380 if (!addedShortcutsFinal.isEmpty()) {
381 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
382 long lastScreenId = info.screenId;
383 for (ItemInfo i : addedShortcutsFinal) {
384 if (i.screenId == lastScreenId) {
385 addAnimated.add(i);
386 } else {
387 addNotAnimated.add(i);
388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
390 }
Winson Chungd64d1762013-08-20 14:37:16 -0700391 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700392 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
Winson Chung997a9232013-07-24 15:33:46 -0700395 });
396 }
Winson Chung64359a52013-07-08 17:17:08 -0700397 }
398 };
399 runOnWorkerThread(r);
400 }
401
Joe Onorato56d82912010-03-07 14:32:10 -0500402 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700403 if (mDefaultIcon == null) {
404 final Context context = LauncherAppState.getInstance().getContext();
405 mDefaultIcon = Utilities.createIconBitmap(
406 mIconCache.getFullResDefaultActivityIcon(), context);
407 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Winson Chung81b52252012-08-27 15:34:29 -0700411 public void unbindItemInfosAndClearQueuedBindRunnables() {
412 if (sWorkerThread.getThreadId() == Process.myTid()) {
413 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
414 "main thread");
415 }
416
417 // Clear any deferred bind runnables
418 mDeferredBindRunnables.clear();
419 // Remove any queued bind runnables
420 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
421 // Unbind all the workspace items
422 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700423 }
424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700426 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700427 // Ensure that we don't use the same workspace items data structure on the main thread
428 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
430 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700431 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
433 tmpAppWidgets.addAll(sBgAppWidgets);
434 }
435 Runnable r = new Runnable() {
436 @Override
437 public void run() {
438 for (ItemInfo item : tmpWorkspaceItems) {
439 item.unbind();
440 }
441 for (ItemInfo item : tmpAppWidgets) {
442 item.unbind();
443 }
444 }
445 };
446 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700447 }
448
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 /**
450 * Adds an item to the DB if it was not created previously, or move it to a new
451 * <container, screen, cellX, cellY>
452 */
453 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 if (item.container == ItemInfo.NO_ID) {
456 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700457 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 } else {
459 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700460 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700464 static void checkItemInfoLocked(
465 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
466 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
467 if (modelItem != null && item != modelItem) {
468 // check all the data is consistent
469 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
470 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
471 ShortcutInfo shortcut = (ShortcutInfo) item;
472 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
473 modelShortcut.intent.filterEquals(shortcut.intent) &&
474 modelShortcut.id == shortcut.id &&
475 modelShortcut.itemType == shortcut.itemType &&
476 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700477 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700478 modelShortcut.cellX == shortcut.cellX &&
479 modelShortcut.cellY == shortcut.cellY &&
480 modelShortcut.spanX == shortcut.spanX &&
481 modelShortcut.spanY == shortcut.spanY &&
482 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
483 (modelShortcut.dropPos != null &&
484 shortcut.dropPos != null &&
485 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
486 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
487 // For all intents and purposes, this is the same object
488 return;
489 }
490 }
491
492 // the modelItem needs to match up perfectly with item if our model is
493 // to be consistent with the database-- for now, just require
494 // modelItem == item or the equality check above
495 String msg = "item: " + ((item != null) ? item.toString() : "null") +
496 "modelItem: " +
497 ((modelItem != null) ? modelItem.toString() : "null") +
498 "Error: ItemInfo passed to checkItemInfo doesn't match original";
499 RuntimeException e = new RuntimeException(msg);
500 if (stackTrace != null) {
501 e.setStackTrace(stackTrace);
502 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800503 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700504 }
505 }
506
Michael Jurka816474f2012-06-25 14:49:02 -0700507 static void checkItemInfo(final ItemInfo item) {
508 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
509 final long itemId = item.id;
510 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700511 public void run() {
512 synchronized (sBgLock) {
513 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700514 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700515 }
516 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700517 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700518 }
519
Michael Jurkac9d95c52011-08-29 14:03:34 -0700520 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
521 final ItemInfo item, final String callingFunction) {
522 final long itemId = item.id;
523 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
524 final ContentResolver cr = context.getContentResolver();
525
Adam Cohen487f7dd2012-06-28 18:12:10 -0700526 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527 Runnable r = new Runnable() {
528 public void run() {
529 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 updateItemArrays(item, itemId, stackTrace);
531 }
532 };
533 runOnWorkerThread(r);
534 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700535
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700536 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
537 final ArrayList<ItemInfo> items, final String callingFunction) {
538 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700539
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700540 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
541 Runnable r = new Runnable() {
542 public void run() {
543 ArrayList<ContentProviderOperation> ops =
544 new ArrayList<ContentProviderOperation>();
545 int count = items.size();
546 for (int i = 0; i < count; i++) {
547 ItemInfo item = items.get(i);
548 final long itemId = item.id;
549 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
550 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700552 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
553 updateItemArrays(item, itemId, stackTrace);
554
555 }
556 try {
557 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
558 } catch (Exception e) {
559 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 }
561 }
562 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700563 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700564 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700565
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700566 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
567 // Lock on mBgLock *after* the db operation
568 synchronized (sBgLock) {
569 checkItemInfoLocked(itemId, item, stackTrace);
570
571 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
572 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
573 // Item is in a folder, make sure this folder exists
574 if (!sBgFolders.containsKey(item.container)) {
575 // An items container is being set to a that of an item which is not in
576 // the list of Folders.
577 String msg = "item: " + item + " container being set to: " +
578 item.container + ", not in the list of folders";
579 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700580 }
581 }
582
583 // Items are added/removed from the corresponding FolderInfo elsewhere, such
584 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
585 // that are on the desktop, as appropriate
586 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800587 if (modelItem != null &&
588 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
589 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700590 switch (modelItem.itemType) {
591 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
592 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
593 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
594 if (!sBgWorkspaceItems.contains(modelItem)) {
595 sBgWorkspaceItems.add(modelItem);
596 }
597 break;
598 default:
599 break;
600 }
601 } else {
602 sBgWorkspaceItems.remove(modelItem);
603 }
604 }
605 }
606
Michael Jurkac7700af2013-05-14 20:17:58 +0200607 public void flushWorkerThread() {
608 mFlushingWorkerThread = true;
609 Runnable waiter = new Runnable() {
610 public void run() {
611 synchronized (this) {
612 notifyAll();
613 mFlushingWorkerThread = false;
614 }
615 }
616 };
617
618 synchronized(waiter) {
619 runOnWorkerThread(waiter);
620 if (mLoaderTask != null) {
621 synchronized(mLoaderTask) {
622 mLoaderTask.notify();
623 }
624 }
625 boolean success = false;
626 while (!success) {
627 try {
628 waiter.wait();
629 success = true;
630 } catch (InterruptedException e) {
631 }
632 }
633 }
634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700638 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700639 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 item.cellX = cellX;
643 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700644
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 // We store hotseat items in canonical form which is this orientation invariant position
646 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653
654 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
657 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700658 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659
Michael Jurkac9d95c52011-08-29 14:03:34 -0700660 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700661 }
662
663 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
665 * cellX, cellY have already been updated on the ItemInfos.
666 */
667 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
668 final long container, final int screen) {
669
670 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
671 int count = items.size();
672
673 for (int i = 0; i < count; i++) {
674 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 item.container = container;
676
677 // We store hotseat items in canonical form which is this orientation invariant position
678 // in the hotseat
679 if (context instanceof Launcher && screen < 0 &&
680 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 item.cellY);
683 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685 }
686
687 final ContentValues values = new ContentValues();
688 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
689 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
690 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692
693 contentValues.add(values);
694 }
695 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
696 }
697
698 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800700 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700703 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800704 item.cellX = cellX;
705 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700706 item.spanX = spanX;
707 item.spanY = spanY;
708
709 // We store hotseat items in canonical form which is this orientation invariant position
710 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 }
Adam Cohend4844c32011-02-18 19:25:06 -0800717
Adam Cohend4844c32011-02-18 19:25:06 -0800718 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800719 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
721 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
722 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
723 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700724 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800725
Michael Jurka816474f2012-06-25 14:49:02 -0700726 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700727 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728
729 /**
730 * Update an item to the database in a specified container.
731 */
732 static void updateItemInDatabase(Context context, final ItemInfo item) {
733 final ContentValues values = new ContentValues();
734 item.onAddToDatabase(values);
735 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
736 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800737 }
738
739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 * Returns true if the shortcuts already exists in the database.
741 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 static boolean shortcutExists(Context context, String title, Intent intent) {
744 final ContentResolver cr = context.getContentResolver();
745 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
746 new String[] { "title", "intent" }, "title=? and intent=?",
747 new String[] { title, intent.toUri(0) }, null);
748 boolean result = false;
749 try {
750 result = c.moveToFirst();
751 } finally {
752 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700755 }
756
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700758 * Returns an ItemInfo array containing all the items in the LauncherModel.
759 * The ItemInfo.id is not set through this function.
760 */
761 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
762 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
763 final ContentResolver cr = context.getContentResolver();
764 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
765 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
766 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
767 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
768
769 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
770 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
771 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
772 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
773 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
774 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
775 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
776
777 try {
778 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700779 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 item.cellX = c.getInt(cellXIndex);
781 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700782 item.spanX = Math.max(1, c.getInt(spanXIndex));
783 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 item.container = c.getInt(containerIndex);
785 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700787
788 items.add(item);
789 }
790 } catch (Exception e) {
791 items.clear();
792 } finally {
793 c.close();
794 }
795
796 return items;
797 }
798
799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
801 */
802 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
803 final ContentResolver cr = context.getContentResolver();
804 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
805 "_id=? and (itemType=? or itemType=?)",
806 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700807 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700808
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 try {
810 if (c.moveToFirst()) {
811 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
812 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
813 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
814 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
815 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
816 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 FolderInfo folderInfo = null;
819 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700820 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
821 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700823 }
824
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 folderInfo.title = c.getString(titleIndex);
826 folderInfo.id = id;
827 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700829 folderInfo.cellX = c.getInt(cellXIndex);
830 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831
832 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 } finally {
835 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700836 }
837
838 return null;
839 }
840
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 /**
842 * Add an item to the database in a specified container. Sets the container, screen, cellX and
843 * cellY fields of the item. Also assigns an ID to the item.
844 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 item.cellX = cellX;
849 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 // We store hotseat items in canonical form which is this orientation invariant position
851 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858
859 final ContentValues values = new ContentValues();
860 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 item.onAddToDatabase(values);
862
Michael Jurka414300a2013-08-27 15:42:35 +0200863 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700864 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700866
Michael Jurkac9d95c52011-08-29 14:03:34 -0700867 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700868 public void run() {
869 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
870 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700873 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700874 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700875 sBgItemsIdMap.put(item.id, item);
876 switch (item.itemType) {
877 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
878 sBgFolders.put(item.id, (FolderInfo) item);
879 // Fall through
880 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
881 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
882 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
883 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
884 sBgWorkspaceItems.add(item);
885 } else {
886 if (!sBgFolders.containsKey(item.container)) {
887 // Adding an item to a folder that doesn't exist.
888 String msg = "adding item: " + item + " to a folder that " +
889 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700890 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700891 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700892 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 break;
894 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
895 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
896 break;
897 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700898 }
899 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700900 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700901 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700902 }
903
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700905 * Creates a new unique child id, for a given cell span across all layouts.
906 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700907 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700908 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700909 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700911 }
912
Winson Chungaafa03c2010-06-11 17:34:16 -0700913 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 * Removes the specified item from the database
915 * @param context
916 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700918 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700921
Michael Jurka83df1882011-08-31 20:59:26 -0700922 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700923 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700924 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925
926 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700927 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 switch (item.itemType) {
929 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
930 sBgFolders.remove(item.id);
931 for (ItemInfo info: sBgItemsIdMap.values()) {
932 if (info.container == item.id) {
933 // We are deleting a folder which still contains items that
934 // think they are contained by that folder.
935 String msg = "deleting a folder (" + item + ") which still " +
936 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700937 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700938 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700939 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940 sBgWorkspaceItems.remove(item);
941 break;
942 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
943 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
944 sBgWorkspaceItems.remove(item);
945 break;
946 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
947 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
948 break;
949 }
950 sBgItemsIdMap.remove(item.id);
951 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 }
953 }
Michael Jurka83df1882011-08-31 20:59:26 -0700954 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 }
957
958 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 * Update the order of the workspace screens in the database. The array list contains
960 * a list of screen ids in the order that they should appear.
961 */
Winson Chungc9168342013-06-26 14:54:55 -0700962 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800963 // Log to disk
964 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
965 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
966
Winson Chung64359a52013-07-08 17:17:08 -0700967 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700968 final ContentResolver cr = context.getContentResolver();
969 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
970
971 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700972 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 while (iter.hasNext()) {
974 long id = iter.next();
975 if (id < 0) {
976 iter.remove();
977 }
978 }
979
980 Runnable r = new Runnable() {
981 @Override
982 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 // Clear the table
984 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700985 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 ContentValues[] values = new ContentValues[count];
987 for (int i = 0; i < count; i++) {
988 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700989 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
991 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 values[i] = v;
993 }
994 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700995
Winson Chungba9c37f2013-08-30 14:11:37 -0700996 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700997 sBgWorkspaceScreens.clear();
998 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700999 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 }
1001 };
1002 runOnWorkerThread(r);
1003 }
1004
1005 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 * Remove the contents of the specified folder from the database
1007 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 final ContentResolver cr = context.getContentResolver();
1010
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 Runnable r = new Runnable() {
1012 public void run() {
1013 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001015 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 sBgItemsIdMap.remove(info.id);
1017 sBgFolders.remove(info.id);
1018 sBgDbIconCache.remove(info);
1019 sBgWorkspaceItems.remove(info);
1020 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1023 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001025 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 for (ItemInfo childInfo : info.contents) {
1027 sBgItemsIdMap.remove(childInfo.id);
1028 sBgDbIconCache.remove(childInfo);
1029 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001030 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001031 }
1032 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001033 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 }
1035
1036 /**
1037 * Set this as the current Launcher activity object for the loader.
1038 */
1039 public void initialize(Callbacks callbacks) {
1040 synchronized (mLock) {
1041 mCallbacks = new WeakReference<Callbacks>(callbacks);
1042 }
1043 }
1044
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001045 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1047 * ACTION_PACKAGE_CHANGED.
1048 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001049 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001050 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001051 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001052
Joe Onorato36115782010-06-17 13:28:48 -04001053 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1056 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1057 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1058 final String packageName = intent.getData().getSchemeSpecificPart();
1059 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060
Joe Onorato36115782010-06-17 13:28:48 -04001061 int op = PackageUpdatedTask.OP_NONE;
1062
1063 if (packageName == null || packageName.length() == 0) {
1064 // they sent us a bad intent
1065 return;
1066 }
1067
1068 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1069 op = PackageUpdatedTask.OP_UPDATE;
1070 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1071 if (!replacing) {
1072 op = PackageUpdatedTask.OP_REMOVE;
1073 }
1074 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1075 // later, we will update the package at this time
1076 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1077 if (!replacing) {
1078 op = PackageUpdatedTask.OP_ADD;
1079 } else {
1080 op = PackageUpdatedTask.OP_UPDATE;
1081 }
1082 }
1083
1084 if (op != PackageUpdatedTask.OP_NONE) {
1085 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1086 }
1087
1088 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001089 // First, schedule to add these apps back in.
1090 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1091 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1092 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001093 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001094 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1095 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1096 enqueuePackageUpdated(new PackageUpdatedTask(
1097 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001098 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001099 // If we have changed locale we need to clear out the labels in all apps/workspace.
1100 forceReload();
1101 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1102 // Check if configuration change was an mcc/mnc change which would affect app resources
1103 // and we would need to clear out the labels in all apps/workspace. Same handling as
1104 // above for ACTION_LOCALE_CHANGED
1105 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001106 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001107 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001108 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001109 forceReload();
1110 }
1111 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001112 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001113 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1114 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001115 if (mCallbacks != null) {
1116 Callbacks callbacks = mCallbacks.get();
1117 if (callbacks != null) {
1118 callbacks.bindSearchablesChanged();
1119 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001120 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001121 }
1122 }
1123
Reena Lee93f824a2011-09-23 17:20:28 -07001124 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001125 resetLoadedState(true, true);
1126
Reena Lee93f824a2011-09-23 17:20:28 -07001127 // Do this here because if the launcher activity is running it will be restarted.
1128 // If it's not running startLoaderFromBackground will merely tell it that it needs
1129 // to reload.
1130 startLoaderFromBackground();
1131 }
1132
Winson Chungf0c6ae02012-03-21 16:10:31 -07001133 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1134 synchronized (mLock) {
1135 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1136 // mWorkspaceLoaded to true later
1137 stopLoaderLocked();
1138 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1139 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1140 }
1141 }
1142
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001143 /**
1144 * When the launcher is in the background, it's possible for it to miss paired
1145 * configuration changes. So whenever we trigger the loader from the background
1146 * tell the launcher that it needs to re-run the loader when it comes back instead
1147 * of doing it now.
1148 */
1149 public void startLoaderFromBackground() {
1150 boolean runLoader = false;
1151 if (mCallbacks != null) {
1152 Callbacks callbacks = mCallbacks.get();
1153 if (callbacks != null) {
1154 // Only actually run the loader if they're not paused.
1155 if (!callbacks.setLoadOnResume()) {
1156 runLoader = true;
1157 }
1158 }
1159 }
1160 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001161 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001162 }
Joe Onorato36115782010-06-17 13:28:48 -04001163 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001164
Reena Lee93f824a2011-09-23 17:20:28 -07001165 // If there is already a loader task running, tell it to stop.
1166 // returns true if isLaunching() was true on the old task
1167 private boolean stopLoaderLocked() {
1168 boolean isLaunching = false;
1169 LoaderTask oldTask = mLoaderTask;
1170 if (oldTask != null) {
1171 if (oldTask.isLaunching()) {
1172 isLaunching = true;
1173 }
1174 oldTask.stopLocked();
1175 }
1176 return isLaunching;
1177 }
1178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001180 synchronized (mLock) {
1181 if (DEBUG_LOADERS) {
1182 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 // Clear any deferred bind-runnables from the synchronized load process
1186 // We must do this before any loading/binding is scheduled below.
1187 mDeferredBindRunnables.clear();
1188
Joe Onorato36115782010-06-17 13:28:48 -04001189 // Don't bother to start the thread if we know it's not going to do anything
1190 if (mCallbacks != null && mCallbacks.get() != null) {
1191 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001192 // also, don't downgrade isLaunching if we're already running
1193 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001194 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1196 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1197 } else {
1198 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1199 sWorker.post(mLoaderTask);
1200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203 }
1204
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 void bindRemainingSynchronousPages() {
1206 // Post the remaining side pages to be loaded
1207 if (!mDeferredBindRunnables.isEmpty()) {
1208 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001209 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 }
1211 mDeferredBindRunnables.clear();
1212 }
1213 }
1214
Joe Onorato36115782010-06-17 13:28:48 -04001215 public void stopLoader() {
1216 synchronized (mLock) {
1217 if (mLoaderTask != null) {
1218 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 }
1220 }
Joe Onorato36115782010-06-17 13:28:48 -04001221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222
Winson Chung76828c82013-08-19 15:43:29 -07001223 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1224 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1225 final ContentResolver contentResolver = context.getContentResolver();
1226 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1227 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1228 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1229
1230 try {
1231 final int idIndex = sc.getColumnIndexOrThrow(
1232 LauncherSettings.WorkspaceScreens._ID);
1233 final int rankIndex = sc.getColumnIndexOrThrow(
1234 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1235 while (sc.moveToNext()) {
1236 try {
1237 long screenId = sc.getLong(idIndex);
1238 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001239 orderedScreens.put(rank, screenId);
1240 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001241 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001242 }
1243 }
1244 } finally {
1245 sc.close();
1246 }
Winson Chunga90303b2013-11-15 13:05:06 -08001247
1248 // Log to disk
1249 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1250 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1251 for (Integer i : orderedScreens.keySet()) {
1252 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1253 }
1254 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1255 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001256 return orderedScreens;
1257 }
1258
Michael Jurkac57b7a82011-08-09 22:02:20 -07001259 public boolean isAllAppsLoaded() {
1260 return mAllAppsLoaded;
1261 }
1262
Winson Chung36a62fe2012-05-06 18:04:42 -07001263 boolean isLoadingWorkspace() {
1264 synchronized (mLock) {
1265 if (mLoaderTask != null) {
1266 return mLoaderTask.isLoadingWorkspace();
1267 }
1268 }
1269 return false;
1270 }
1271
Joe Onorato36115782010-06-17 13:28:48 -04001272 /**
1273 * Runnable for the thread that loads the contents of the launcher:
1274 * - workspace icons
1275 * - widgets
1276 * - all apps icons
1277 */
1278 private class LoaderTask implements Runnable {
1279 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001280 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001281 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001282 private boolean mStopped;
1283 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001284
Winson Chungc3eecff2011-07-11 17:44:15 -07001285 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001286
1287 LoaderTask(Context context, boolean isLaunching) {
1288 mContext = context;
1289 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001290 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 }
1292
Joe Onorato36115782010-06-17 13:28:48 -04001293 boolean isLaunching() {
1294 return mIsLaunching;
1295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296
Winson Chung36a62fe2012-05-06 18:04:42 -07001297 boolean isLoadingWorkspace() {
1298 return mIsLoadingAndBindingWorkspace;
1299 }
1300
Winson Chungc763c4e2013-07-19 13:49:06 -07001301 /** Returns whether this is an upgrade path */
1302 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001303 mIsLoadingAndBindingWorkspace = true;
1304
Joe Onorato36115782010-06-17 13:28:48 -04001305 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001306 if (DEBUG_LOADERS) {
1307 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001309
Winson Chungc763c4e2013-07-19 13:49:06 -07001310 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001311 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001312 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001313 synchronized (LoaderTask.this) {
1314 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001315 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001316 }
1317 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 }
1319 }
1320
Joe Onorato36115782010-06-17 13:28:48 -04001321 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001322 bindWorkspace(-1, isUpgradePath);
1323 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001324 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001325
Joe Onorato36115782010-06-17 13:28:48 -04001326 private void waitForIdle() {
1327 // Wait until the either we're stopped or the other threads are done.
1328 // This way we don't start loading all apps until the workspace has settled
1329 // down.
1330 synchronized (LoaderTask.this) {
1331 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001332
Joe Onorato36115782010-06-17 13:28:48 -04001333 mHandler.postIdle(new Runnable() {
1334 public void run() {
1335 synchronized (LoaderTask.this) {
1336 mLoadAndBindStepFinished = true;
1337 if (DEBUG_LOADERS) {
1338 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 });
1344
Michael Jurkac7700af2013-05-14 20:17:58 +02001345 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001346 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001347 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1348 // wait no longer than 1sec at a time
1349 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001350 } catch (InterruptedException ex) {
1351 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001352 }
1353 }
Joe Onorato36115782010-06-17 13:28:48 -04001354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001356 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001357 + "ms for previous step to finish binding");
1358 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001359 }
Joe Onorato36115782010-06-17 13:28:48 -04001360 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001361
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 void runBindSynchronousPage(int synchronousBindPage) {
1363 if (synchronousBindPage < 0) {
1364 // Ensure that we have a valid page index to load synchronously
1365 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1366 "valid page index");
1367 }
1368 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1369 // Ensure that we don't try and bind a specified page when the pages have not been
1370 // loaded already (we should load everything asynchronously in that case)
1371 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1372 }
1373 synchronized (mLock) {
1374 if (mIsLoaderTaskRunning) {
1375 // Ensure that we are never running the background loading at this point since
1376 // we also touch the background collections
1377 throw new RuntimeException("Error! Background loading is already running");
1378 }
1379 }
1380
1381 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1382 // data structures, we can't allow any other thread to touch that data, but because
1383 // this call is synchronous, we can get away with not locking).
1384
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001385 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001386 // operations from the previous activity. We need to ensure that all queued operations
1387 // are executed before any synchronous binding work is done.
1388 mHandler.flush();
1389
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001390 // Divide the set of loaded items into those that we are binding synchronously, and
1391 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001392 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1394 // arise from that.
1395 onlyBindAllApps();
1396 }
1397
Joe Onorato36115782010-06-17 13:28:48 -04001398 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001399 boolean isUpgrade = false;
1400
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 synchronized (mLock) {
1402 mIsLoaderTaskRunning = true;
1403 }
Joe Onorato36115782010-06-17 13:28:48 -04001404 // Optimize for end-user experience: if the Launcher is up and // running with the
1405 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1406 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001407 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 // Elevate priority when Home launches for the first time to avoid
1409 // starving at boot time. Staring at a blank home is not cool.
1410 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001411 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1412 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001413 android.os.Process.setThreadPriority(mIsLaunching
1414 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1415 }
Winson Chung64359a52013-07-08 17:17:08 -07001416 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001417 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001418
Joe Onorato36115782010-06-17 13:28:48 -04001419 if (mStopped) {
1420 break keep_running;
1421 }
1422
1423 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1424 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001425 synchronized (mLock) {
1426 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001427 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001428 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1429 }
1430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001432
1433 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001434 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1435 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001436
1437 // Restore the default thread priority after we are done loading items
1438 synchronized (mLock) {
1439 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1440 }
Joe Onorato36115782010-06-17 13:28:48 -04001441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442
Winson Chungaac01e12011-08-17 10:37:13 -07001443 // Update the saved icons if necessary
1444 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001445 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 for (Object key : sBgDbIconCache.keySet()) {
1447 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1448 }
1449 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001450 }
Winson Chungaac01e12011-08-17 10:37:13 -07001451
Winson Chungc58497e2013-09-03 17:48:37 -07001452 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1453 // Ensure that all the applications that are in the system are
1454 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001455 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001456 verifyApplications();
1457 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001458 }
1459
Joe Onorato36115782010-06-17 13:28:48 -04001460 // Clear out this reference, otherwise we end up holding it until all of the
1461 // callback runnables are done.
1462 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463
Joe Onorato36115782010-06-17 13:28:48 -04001464 synchronized (mLock) {
1465 // If we are still the last one to be scheduled, remove ourselves.
1466 if (mLoaderTask == this) {
1467 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 }
1472
1473 public void stopLocked() {
1474 synchronized (LoaderTask.this) {
1475 mStopped = true;
1476 this.notify();
1477 }
1478 }
1479
1480 /**
1481 * Gets the callbacks object. If we've been stopped, or if the launcher object
1482 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1483 * object that was around when the deferred message was scheduled, and if there's
1484 * a new Callbacks object around then also return null. This will save us from
1485 * calling onto it with data that will be ignored.
1486 */
1487 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1488 synchronized (mLock) {
1489 if (mStopped) {
1490 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492
1493 if (mCallbacks == null) {
1494 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001495 }
Joe Onorato36115782010-06-17 13:28:48 -04001496
1497 final Callbacks callbacks = mCallbacks.get();
1498 if (callbacks != oldCallbacks) {
1499 return null;
1500 }
1501 if (callbacks == null) {
1502 Log.w(TAG, "no mCallbacks");
1503 return null;
1504 }
1505
1506 return callbacks;
1507 }
1508 }
1509
Winson Chungc763c4e2013-07-19 13:49:06 -07001510 private void verifyApplications() {
1511 final Context context = mApp.getContext();
1512
1513 // Cross reference all the applications in our apps list with items in the workspace
1514 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001515 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001516 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001517 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001518 tmpInfos = getItemInfoForComponentName(app.componentName);
1519 if (tmpInfos.isEmpty()) {
1520 // We are missing an application icon, so add this to the workspace
1521 added.add(app);
1522 // This is a rare event, so lets log it
1523 Log.e(TAG, "Missing Application on load: " + app);
1524 }
1525 }
1526 }
1527 if (!added.isEmpty()) {
1528 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001529 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001530 }
1531 }
1532
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001534 LauncherAppState app = LauncherAppState.getInstance();
1535 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1536 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1537 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001538 }
1539
Joe Onorato36115782010-06-17 13:28:48 -04001540 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001541 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1542 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001543 LauncherAppState app = LauncherAppState.getInstance();
1544 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001545 final int countX = (int) grid.numColumns;
1546 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001549 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001550 // Return early if we detect that an item is under the hotseat button
1551 if (mCallbacks == null ||
1552 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1553 deleteOnItemOverlap.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001554 Log.e(TAG, "Error loading shortcut into hotseat " + item
1555 + " into position (" + item.screenId + ":" + item.cellX + ","
1556 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001557 return false;
1558 }
1559
Dan Sandler295ae182013-12-10 16:05:47 -05001560 final ItemInfo[][] hotseatItems =
1561 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1562
1563 if (hotseatItems != null) {
1564 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001565 Log.e(TAG, "Error loading shortcut into hotseat " + item
1566 + " into position (" + item.screenId + ":" + item.cellX + ","
1567 + item.cellY + ") occupied by "
1568 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1569 [(int) item.screenId][0]);
1570 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001571 } else {
1572 hotseatItems[(int) item.screenId][0] = item;
1573 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001575 } else {
Dan Sandler295ae182013-12-10 16:05:47 -05001576 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons + 1][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 items[(int) item.screenId][0] = item;
1578 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001579 return true;
1580 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001581 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1582 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001583 return true;
1584 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001585
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001587 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 occupied.put(item.screenId, items);
1589 }
1590
Dan Sandler295ae182013-12-10 16:05:47 -05001591 final ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001592 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001593 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1594 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001596 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001597 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001598 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001599 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001600 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001601 return false;
1602 }
1603 }
1604 }
1605 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1606 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001607 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001608 }
1609 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001610
Joe Onorato36115782010-06-17 13:28:48 -04001611 return true;
1612 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613
Winson Chungba9c37f2013-08-30 14:11:37 -07001614 /** Clears all the sBg data structures */
1615 private void clearSBgDataStructures() {
1616 synchronized (sBgLock) {
1617 sBgWorkspaceItems.clear();
1618 sBgAppWidgets.clear();
1619 sBgFolders.clear();
1620 sBgItemsIdMap.clear();
1621 sBgDbIconCache.clear();
1622 sBgWorkspaceScreens.clear();
1623 }
1624 }
1625
Winson Chungc763c4e2013-07-19 13:49:06 -07001626 /** Returns whether this is an upgradge path */
1627 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001628 // Log to disk
1629 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1630
Joe Onorato36115782010-06-17 13:28:48 -04001631 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001632
Joe Onorato36115782010-06-17 13:28:48 -04001633 final Context context = mContext;
1634 final ContentResolver contentResolver = context.getContentResolver();
1635 final PackageManager manager = context.getPackageManager();
1636 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1637 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001638
Winson Chung892c74d2013-08-22 16:15:50 -07001639 LauncherAppState app = LauncherAppState.getInstance();
1640 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1641 int countX = (int) grid.numColumns;
1642 int countY = (int) grid.numRows;
1643
Michael Jurkab85f8a42012-04-25 15:48:32 -07001644 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001645 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001646
Winson Chungc763c4e2013-07-19 13:49:06 -07001647 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001648 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001649 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001650
Winson Chung9f9f00b2013-11-15 13:27:00 -08001651 // Log to disk
1652 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001653
Winson Chung2abf94d2012-07-18 18:16:38 -07001654 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001655 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001656
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001657 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001658 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001659 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001660 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001661
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001662 // +1 for the hotseat (it can be larger than the workspace)
1663 // Load workspace in reverse order to ensure that latest items are loaded first (and
1664 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001665 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001666
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001667 try {
1668 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1669 final int intentIndex = c.getColumnIndexOrThrow
1670 (LauncherSettings.Favorites.INTENT);
1671 final int titleIndex = c.getColumnIndexOrThrow
1672 (LauncherSettings.Favorites.TITLE);
1673 final int iconTypeIndex = c.getColumnIndexOrThrow(
1674 LauncherSettings.Favorites.ICON_TYPE);
1675 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1676 final int iconPackageIndex = c.getColumnIndexOrThrow(
1677 LauncherSettings.Favorites.ICON_PACKAGE);
1678 final int iconResourceIndex = c.getColumnIndexOrThrow(
1679 LauncherSettings.Favorites.ICON_RESOURCE);
1680 final int containerIndex = c.getColumnIndexOrThrow(
1681 LauncherSettings.Favorites.CONTAINER);
1682 final int itemTypeIndex = c.getColumnIndexOrThrow(
1683 LauncherSettings.Favorites.ITEM_TYPE);
1684 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1685 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001686 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1687 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001688 final int screenIndex = c.getColumnIndexOrThrow(
1689 LauncherSettings.Favorites.SCREEN);
1690 final int cellXIndex = c.getColumnIndexOrThrow
1691 (LauncherSettings.Favorites.CELLX);
1692 final int cellYIndex = c.getColumnIndexOrThrow
1693 (LauncherSettings.Favorites.CELLY);
1694 final int spanXIndex = c.getColumnIndexOrThrow
1695 (LauncherSettings.Favorites.SPANX);
1696 final int spanYIndex = c.getColumnIndexOrThrow(
1697 LauncherSettings.Favorites.SPANY);
1698 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1699 //final int displayModeIndex = c.getColumnIndexOrThrow(
1700 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 ShortcutInfo info;
1703 String intentDescription;
1704 LauncherAppWidgetInfo appWidgetInfo;
1705 int container;
1706 long id;
1707 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001710 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001711 try {
1712 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001714 switch (itemType) {
1715 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1716 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001717 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001718 intentDescription = c.getString(intentIndex);
1719 try {
1720 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001721 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001722 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001723 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001724 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001725 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1726 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001727 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001728 // If apps can be on external storage, then we just
1729 // leave them for the user to remove (maybe add
1730 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001731 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001732 }
1733 continue;
1734 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001735 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001736 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 continue;
1738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1741 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1742 titleIndex, mLabelCache);
1743 } else {
1744 info = getShortcutInfo(c, context, iconTypeIndex,
1745 iconPackageIndex, iconResourceIndex, iconIndex,
1746 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001747
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001748 // App shortcuts that used to be automatically added to Launcher
1749 // didn't always have the correct intent flags set, so do that
1750 // here
1751 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001752 intent.getCategories() != null &&
1753 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001754 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 intent.addFlags(
1756 Intent.FLAG_ACTIVITY_NEW_TASK |
1757 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1758 }
Michael Jurka96879562012-03-22 05:54:33 -07001759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001760
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001761 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001762 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001764 container = c.getInt(containerIndex);
1765 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001766 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001767 info.cellX = c.getInt(cellXIndex);
1768 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001769 info.spanX = 1;
1770 info.spanY = 1;
1771 // Skip loading items that are out of bounds
1772 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1773 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001774 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1775 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001776 continue;
1777 }
1778 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001779 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001780 deleteOnItemOverlap.set(false);
1781 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1782 if (deleteOnItemOverlap.get()) {
1783 itemsToRemove.add(id);
1784 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001785 break;
1786 }
1787
1788 switch (container) {
1789 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1790 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1791 sBgWorkspaceItems.add(info);
1792 break;
1793 default:
1794 // Item is in a user folder
1795 FolderInfo folderInfo =
1796 findOrMakeFolder(sBgFolders, container);
1797 folderInfo.add(info);
1798 break;
1799 }
1800 sBgItemsIdMap.put(info.id, info);
1801
1802 // now that we've loaded everthing re-save it with the
1803 // icon in case it disappears somehow.
1804 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001805 } else {
1806 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001807 }
1808 break;
1809
1810 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1811 id = c.getLong(idIndex);
1812 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1813
1814 folderInfo.title = c.getString(titleIndex);
1815 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001817 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001818 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001819 folderInfo.cellX = c.getInt(cellXIndex);
1820 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001821 folderInfo.spanX = 1;
1822 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823
Winson Chung5f8afe62013-08-12 16:19:28 -07001824 // Skip loading items that are out of bounds
1825 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001826 if (checkItemDimensions(folderInfo)) {
1827 Log.d(TAG, "Skipped loading out of bounds folder");
1828 continue;
1829 }
1830 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001831 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001832 deleteOnItemOverlap.set(false);
1833 if (!checkItemPlacement(occupied, folderInfo,
1834 deleteOnItemOverlap)) {
1835 if (deleteOnItemOverlap.get()) {
1836 itemsToRemove.add(id);
1837 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001838 break;
1839 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001840
Joe Onorato9c1289c2009-08-17 11:03:03 -04001841 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001842 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1843 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1844 sBgWorkspaceItems.add(folderInfo);
1845 break;
Joe Onorato36115782010-06-17 13:28:48 -04001846 }
Joe Onorato17a89222011-02-08 17:26:11 -08001847
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001848 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1849 sBgFolders.put(folderInfo.id, folderInfo);
1850 break;
1851
1852 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1853 // Read all Launcher-specific widget details
1854 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001855 String savedProvider = c.getString(appWidgetProviderIndex);
1856
Joe Onorato36115782010-06-17 13:28:48 -04001857 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001858
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 final AppWidgetProviderInfo provider =
1860 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 if (!isSafeMode && (provider == null || provider.provider == null ||
1863 provider.provider.getPackageName() == null)) {
1864 String log = "Deleting widget that isn't installed anymore: id="
1865 + id + " appWidgetId=" + appWidgetId;
1866 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001867 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001868 itemsToRemove.add(id);
1869 } else {
1870 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1871 provider.provider);
1872 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001873 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 appWidgetInfo.cellX = c.getInt(cellXIndex);
1875 appWidgetInfo.cellY = c.getInt(cellYIndex);
1876 appWidgetInfo.spanX = c.getInt(spanXIndex);
1877 appWidgetInfo.spanY = c.getInt(spanYIndex);
1878 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1879 appWidgetInfo.minSpanX = minSpan[0];
1880 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001881
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 container = c.getInt(containerIndex);
1883 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1884 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1885 Log.e(TAG, "Widget found where container != " +
1886 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1887 continue;
1888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889
Adam Cohene25af792013-06-06 23:08:25 -07001890 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001891 // Skip loading items that are out of bounds
1892 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1893 if (checkItemDimensions(appWidgetInfo)) {
1894 Log.d(TAG, "Skipped loading out of bounds app widget");
1895 continue;
1896 }
1897 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001899 deleteOnItemOverlap.set(false);
1900 if (!checkItemPlacement(occupied, appWidgetInfo,
1901 deleteOnItemOverlap)) {
1902 if (deleteOnItemOverlap.get()) {
1903 itemsToRemove.add(id);
1904 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 break;
1906 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001907 String providerName = provider.provider.flattenToString();
1908 if (!providerName.equals(savedProvider)) {
1909 ContentValues values = new ContentValues();
1910 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1911 providerName);
1912 String where = BaseColumns._ID + "= ?";
1913 String[] args = {Integer.toString(c.getInt(idIndex))};
1914 contentResolver.update(contentUri, values, where, args);
1915 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1917 sBgAppWidgets.add(appWidgetInfo);
1918 }
Joe Onorato36115782010-06-17 13:28:48 -04001919 break;
1920 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05001922 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001923 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001924 }
1925 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001926 if (c != null) {
1927 c.close();
1928 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 }
1930
Winson Chungba9c37f2013-08-30 14:11:37 -07001931 // Break early if we've stopped loading
1932 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001933 clearSBgDataStructures();
1934 return false;
1935 }
1936
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001937 if (itemsToRemove.size() > 0) {
1938 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001939 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001940 // Remove dead items
1941 for (long id : itemsToRemove) {
1942 if (DEBUG_LOADERS) {
1943 Log.d(TAG, "Removed id = " + id);
1944 }
1945 // Don't notify content observers
1946 try {
1947 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1948 null, null);
1949 } catch (RemoteException e) {
1950 Log.w(TAG, "Could not remove id = " + id);
1951 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001952 }
1953 }
1954
Winson Chungc763c4e2013-07-19 13:49:06 -07001955 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001956 long maxScreenId = 0;
1957 // If we're importing we use the old screen order.
1958 for (ItemInfo item: sBgItemsIdMap.values()) {
1959 long screenId = item.screenId;
1960 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1961 !sBgWorkspaceScreens.contains(screenId)) {
1962 sBgWorkspaceScreens.add(screenId);
1963 if (screenId > maxScreenId) {
1964 maxScreenId = screenId;
1965 }
1966 }
1967 }
1968 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001969 // Log to disk
1970 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1971 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1972 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001973
Michael Jurka414300a2013-08-27 15:42:35 +02001974 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001975 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001976
1977 // Update the max item id after we load an old db
1978 long maxItemId = 0;
1979 // If we're importing we use the old screen order.
1980 for (ItemInfo item: sBgItemsIdMap.values()) {
1981 maxItemId = Math.max(maxItemId, item.id);
1982 }
Michael Jurka414300a2013-08-27 15:42:35 +02001983 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001985 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1986 for (Integer i : orderedScreens.keySet()) {
1987 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001989 // Log to disk
1990 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1991 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001992
1993 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001994 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 for (ItemInfo item: sBgItemsIdMap.values()) {
1996 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1998 unusedScreens.contains(screenId)) {
1999 unusedScreens.remove(screenId);
2000 }
2001 }
2002
2003 // If there are any empty screens remove them, and update.
2004 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002005 // Log to disk
2006 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2007 TextUtils.join(", ", unusedScreens), true);
2008
Winson Chung933bae62013-08-29 11:42:30 -07002009 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002010 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2011 }
2012 }
2013
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002014 if (DEBUG_LOADERS) {
2015 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2016 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002018 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002019 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002020
Daniel Sandler566da102013-06-25 23:43:45 -04002021 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002022 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002023 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002024 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002025 line += " | ";
2026 }
Winson Chung892c74d2013-08-22 16:15:50 -07002027 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 }
Joe Onorato36115782010-06-17 13:28:48 -04002030 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002031 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002032 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002033 }
Joe Onorato36115782010-06-17 13:28:48 -04002034 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002035 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002036 }
2037
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 /** Filters the set of items who are directly or indirectly (via another container) on the
2039 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002040 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002041 ArrayList<ItemInfo> allWorkspaceItems,
2042 ArrayList<ItemInfo> currentScreenItems,
2043 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002044 // Purge any null ItemInfos
2045 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2046 while (iter.hasNext()) {
2047 ItemInfo i = iter.next();
2048 if (i == null) {
2049 iter.remove();
2050 }
2051 }
2052
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002053 // Order the set of items by their containers first, this allows use to walk through the
2054 // list sequentially, build up a list of containers that are in the specified screen,
2055 // as well as all items in those containers.
2056 Set<Long> itemsOnScreen = new HashSet<Long>();
2057 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2058 @Override
2059 public int compare(ItemInfo lhs, ItemInfo rhs) {
2060 return (int) (lhs.container - rhs.container);
2061 }
2062 });
2063 for (ItemInfo info : allWorkspaceItems) {
2064 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002065 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 currentScreenItems.add(info);
2067 itemsOnScreen.add(info.id);
2068 } else {
2069 otherScreenItems.add(info);
2070 }
2071 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2072 currentScreenItems.add(info);
2073 itemsOnScreen.add(info.id);
2074 } else {
2075 if (itemsOnScreen.contains(info.container)) {
2076 currentScreenItems.add(info);
2077 itemsOnScreen.add(info.id);
2078 } else {
2079 otherScreenItems.add(info);
2080 }
2081 }
2082 }
2083 }
2084
2085 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002086 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 ArrayList<LauncherAppWidgetInfo> appWidgets,
2088 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2089 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090
2091 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002092 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002093 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002094 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 currentScreenWidgets.add(widget);
2096 } else {
2097 otherScreenWidgets.add(widget);
2098 }
2099 }
2100 }
2101
2102 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002103 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 HashMap<Long, ItemInfo> itemsIdMap,
2105 HashMap<Long, FolderInfo> folders,
2106 HashMap<Long, FolderInfo> currentScreenFolders,
2107 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002108
2109 for (long id : folders.keySet()) {
2110 ItemInfo info = itemsIdMap.get(id);
2111 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002112 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002113 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002114 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 currentScreenFolders.put(id, folder);
2116 } else {
2117 otherScreenFolders.put(id, folder);
2118 }
2119 }
2120 }
2121
2122 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2123 * right) */
2124 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002125 final LauncherAppState app = LauncherAppState.getInstance();
2126 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002127 // XXX: review this
2128 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002129 @Override
2130 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002131 int cellCountX = (int) grid.numColumns;
2132 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002133 int screenOffset = cellCountX * cellCountY;
2134 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002136 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002138 rhs.cellY * cellCountX + rhs.cellX);
2139 return (int) (lr - rr);
2140 }
2141 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002142 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002143
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2145 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002146 final Runnable r = new Runnable() {
2147 @Override
2148 public void run() {
2149 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2150 if (callbacks != null) {
2151 callbacks.bindScreens(orderedScreens);
2152 }
2153 }
2154 };
2155 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2156 }
2157
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002158 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2159 final ArrayList<ItemInfo> workspaceItems,
2160 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2161 final HashMap<Long, FolderInfo> folders,
2162 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002163
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 final boolean postOnMainThread = (deferredBindRunnables != null);
2165
2166 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002167 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002168 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002169 final int start = i;
2170 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 final Runnable r = new Runnable() {
2172 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002174 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002176 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2177 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 }
2179 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002180 };
2181 if (postOnMainThread) {
2182 deferredBindRunnables.add(r);
2183 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002184 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002185 }
Joe Onorato36115782010-06-17 13:28:48 -04002186 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187
2188 // Bind the folders
2189 if (!folders.isEmpty()) {
2190 final Runnable r = new Runnable() {
2191 public void run() {
2192 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2193 if (callbacks != null) {
2194 callbacks.bindFolders(folders);
2195 }
2196 }
2197 };
2198 if (postOnMainThread) {
2199 deferredBindRunnables.add(r);
2200 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002201 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002202 }
2203 }
2204
2205 // Bind the widgets, one at a time
2206 N = appWidgets.size();
2207 for (int i = 0; i < N; i++) {
2208 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2209 final Runnable r = new Runnable() {
2210 public void run() {
2211 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2212 if (callbacks != null) {
2213 callbacks.bindAppWidget(widget);
2214 }
2215 }
2216 };
2217 if (postOnMainThread) {
2218 deferredBindRunnables.add(r);
2219 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002220 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 }
2222 }
2223 }
2224
2225 /**
2226 * Binds all loaded data to actual views on the main thread.
2227 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002228 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002229 final long t = SystemClock.uptimeMillis();
2230 Runnable r;
2231
2232 // Don't use these two variables in any of the callback runnables.
2233 // Otherwise we hold a reference to them.
2234 final Callbacks oldCallbacks = mCallbacks.get();
2235 if (oldCallbacks == null) {
2236 // This launcher has exited and nobody bothered to tell us. Just bail.
2237 Log.w(TAG, "LoaderTask running with no launcher");
2238 return;
2239 }
2240
Winson Chung9b9fb962013-11-15 15:39:34 -08002241 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002242 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2243 ArrayList<LauncherAppWidgetInfo> appWidgets =
2244 new ArrayList<LauncherAppWidgetInfo>();
2245 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2246 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002248 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 workspaceItems.addAll(sBgWorkspaceItems);
2250 appWidgets.addAll(sBgAppWidgets);
2251 folders.putAll(sBgFolders);
2252 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002254 }
2255
Winson Chung9b9fb962013-11-15 15:39:34 -08002256 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
Adam Cohend8dbb462013-11-27 11:55:48 -08002257 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002258 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002259 if (currScreen >= orderedScreenIds.size()) {
2260 // There may be no workspace screens (just hotseat items and an empty page).
2261 currScreen = -1;
Winson Chung9b9fb962013-11-15 15:39:34 -08002262 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002263 final int currentScreen = currScreen;
2264 final long currentScreenId =
2265 currentScreen < 0 ? -1 : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002266
2267 // Load all the items that are on the current page first (and in the process, unbind
2268 // all the existing workspace items before we call startBinding() below.
2269 unbindWorkspaceItemsOnMainThread();
2270
2271 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002272 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2273 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2274 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2275 new ArrayList<LauncherAppWidgetInfo>();
2276 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2277 new ArrayList<LauncherAppWidgetInfo>();
2278 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2279 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2280
Winson Chung9b9fb962013-11-15 15:39:34 -08002281 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002282 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002283 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002284 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002285 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 otherFolders);
2287 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2288 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2289
2290 // Tell the workspace that we're about to start binding items
2291 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002292 public void run() {
2293 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2294 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002296 }
2297 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 };
Winson Chung81b52252012-08-27 15:34:29 -07002299 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300
Adam Cohendcd297f2013-06-18 13:13:40 -07002301 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2302
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303 // Load items on the current page
2304 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2305 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002306 if (isLoadingSynchronously) {
2307 r = new Runnable() {
2308 public void run() {
2309 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohenb0ee0812013-12-03 10:51:45 -08002310 if (callbacks != null && currentScreen >= 0) {
Adam Cohen1462de32012-07-24 22:34:36 -07002311 callbacks.onPageBoundSynchronously(currentScreen);
2312 }
2313 }
2314 };
Winson Chung81b52252012-08-27 15:34:29 -07002315 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002316 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002317
Winson Chung4a2afa32012-07-19 14:53:05 -07002318 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2319 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 mDeferredBindRunnables.clear();
2321 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002322 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323
2324 // Tell the workspace that we're done binding items
2325 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002326 public void run() {
2327 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2328 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002329 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002330 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331
Winson Chung98e030b2012-05-07 16:01:11 -07002332 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002333 if (DEBUG_LOADERS) {
2334 Log.d(TAG, "bound workspace in "
2335 + (SystemClock.uptimeMillis()-t) + "ms");
2336 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002337
2338 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002339 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002340 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002341 if (isLoadingSynchronously) {
2342 mDeferredBindRunnables.add(r);
2343 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002344 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002345 }
Joe Onorato36115782010-06-17 13:28:48 -04002346 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002347
Joe Onorato36115782010-06-17 13:28:48 -04002348 private void loadAndBindAllApps() {
2349 if (DEBUG_LOADERS) {
2350 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2351 }
2352 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002353 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002354 synchronized (LoaderTask.this) {
2355 if (mStopped) {
2356 return;
2357 }
2358 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002359 }
Joe Onorato36115782010-06-17 13:28:48 -04002360 } else {
2361 onlyBindAllApps();
2362 }
2363 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002364
Joe Onorato36115782010-06-17 13:28:48 -04002365 private void onlyBindAllApps() {
2366 final Callbacks oldCallbacks = mCallbacks.get();
2367 if (oldCallbacks == null) {
2368 // This launcher has exited and nobody bothered to tell us. Just bail.
2369 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2370 return;
2371 }
2372
2373 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002374 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002375 final ArrayList<AppInfo> list
2376 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002377 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002378 public void run() {
2379 final long t = SystemClock.uptimeMillis();
2380 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2381 if (callbacks != null) {
2382 callbacks.bindAllApplications(list);
2383 }
2384 if (DEBUG_LOADERS) {
2385 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2386 + (SystemClock.uptimeMillis()-t) + "ms");
2387 }
2388 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002389 };
2390 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002391 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002392 r.run();
2393 } else {
2394 mHandler.post(r);
2395 }
Joe Onorato36115782010-06-17 13:28:48 -04002396 }
2397
Winson Chung64359a52013-07-08 17:17:08 -07002398 private void loadAllApps() {
2399 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002400
Joe Onorato36115782010-06-17 13:28:48 -04002401 final Callbacks oldCallbacks = mCallbacks.get();
2402 if (oldCallbacks == null) {
2403 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002404 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002405 return;
2406 }
2407
Winson Chung64359a52013-07-08 17:17:08 -07002408 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002409 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2410 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2411
Winson Chung64359a52013-07-08 17:17:08 -07002412 // Clear the list of apps
2413 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002414
Winson Chung64359a52013-07-08 17:17:08 -07002415 // Query for the set of apps
2416 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2417 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2418 if (DEBUG_LOADERS) {
2419 Log.d(TAG, "queryIntentActivities took "
2420 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2421 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2422 }
2423 // Fail if we don't have any apps
2424 if (apps == null || apps.isEmpty()) {
2425 return;
2426 }
2427 // Sort the applications by name
2428 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2429 Collections.sort(apps,
2430 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2431 if (DEBUG_LOADERS) {
2432 Log.d(TAG, "sort took "
2433 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002434 }
2435
Winson Chung64359a52013-07-08 17:17:08 -07002436 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002437 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002438 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002439 // This builds the icon bitmaps.
2440 mBgAllAppsList.add(new AppInfo(packageManager, app,
2441 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002442 }
2443
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002444 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002445 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2446 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002447
2448 // Post callback on main thread
2449 mHandler.post(new Runnable() {
2450 public void run() {
2451 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002452 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002453 if (callbacks != null) {
2454 callbacks.bindAllApplications(added);
2455 if (DEBUG_LOADERS) {
2456 Log.d(TAG, "bound " + added.size() + " apps in "
2457 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2458 }
2459 } else {
2460 Log.i(TAG, "not binding apps: no Launcher activity");
2461 }
2462 }
2463 });
2464
Joe Onorato36115782010-06-17 13:28:48 -04002465 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002466 Log.d(TAG, "Icons processed in "
2467 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002468 }
2469 }
2470
2471 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002472 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002473 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2474 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2475 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2476 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2477 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2478 }
Joe Onorato36115782010-06-17 13:28:48 -04002479 }
2480 }
2481
2482 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002483 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002484 }
2485
2486 private class PackageUpdatedTask implements Runnable {
2487 int mOp;
2488 String[] mPackages;
2489
2490 public static final int OP_NONE = 0;
2491 public static final int OP_ADD = 1;
2492 public static final int OP_UPDATE = 2;
2493 public static final int OP_REMOVE = 3; // uninstlled
2494 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2495
2496
2497 public PackageUpdatedTask(int op, String[] packages) {
2498 mOp = op;
2499 mPackages = packages;
2500 }
2501
2502 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002503 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002504
2505 final String[] packages = mPackages;
2506 final int N = packages.length;
2507 switch (mOp) {
2508 case OP_ADD:
2509 for (int i=0; i<N; i++) {
2510 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002511 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002512 }
2513 break;
2514 case OP_UPDATE:
2515 for (int i=0; i<N; i++) {
2516 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002517 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002518 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002519 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002520 }
2521 break;
2522 case OP_REMOVE:
2523 case OP_UNAVAILABLE:
2524 for (int i=0; i<N; i++) {
2525 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002526 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002527 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002528 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002529 }
2530 break;
2531 }
2532
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002533 ArrayList<AppInfo> added = null;
2534 ArrayList<AppInfo> modified = null;
2535 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002536
Adam Cohen487f7dd2012-06-28 18:12:10 -07002537 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002538 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002539 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002540 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002541 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002542 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002543 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002544 }
Winson Chung5d55f332012-07-16 20:45:03 -07002545 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002546 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002547 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002548 }
2549
Joe Onorato36115782010-06-17 13:28:48 -04002550 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2551 if (callbacks == null) {
2552 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2553 return;
2554 }
2555
2556 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002557 // Ensure that we add all the workspace applications to the db
2558 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002559 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2560 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2561 } else {
2562 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2563 addAndBindAddedApps(context, addedInfos, cb, added);
2564 }
Joe Onorato36115782010-06-17 13:28:48 -04002565 }
2566 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002567 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002568
2569 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002570 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002571 ArrayList<ItemInfo> infos =
2572 getItemInfoForComponentName(a.componentName);
2573 for (ItemInfo i : infos) {
2574 if (isShortcutInfoUpdateable(i)) {
2575 ShortcutInfo info = (ShortcutInfo) i;
2576 info.title = a.title.toString();
2577 updateItemInDatabase(context, info);
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) {
Joe Onorato36115782010-06-17 13:28:48 -04002586 callbacks.bindAppsUpdated(modifiedFinal);
2587 }
2588 }
2589 });
2590 }
Winson Chung83892cc2013-05-01 16:53:33 -07002591
Winson Chungdf95eb12013-10-16 14:57:07 -07002592 final ArrayList<String> removedPackageNames =
2593 new ArrayList<String>();
2594 if (mOp == OP_REMOVE) {
2595 // Mark all packages in the broadcast to be removed
2596 removedPackageNames.addAll(Arrays.asList(packages));
2597 } else if (mOp == OP_UPDATE) {
2598 // Mark disabled packages in the broadcast to be removed
2599 final PackageManager pm = context.getPackageManager();
2600 for (int i=0; i<N; i++) {
2601 if (isPackageDisabled(pm, packages[i])) {
2602 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002603 }
2604 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002605 }
2606 // Remove all the components associated with this package
2607 for (String pn : removedPackageNames) {
2608 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2609 for (ItemInfo i : infos) {
2610 deleteItemFromDatabase(context, i);
2611 }
2612 }
2613 // Remove all the specific components
2614 for (AppInfo a : removedApps) {
2615 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2616 for (ItemInfo i : infos) {
2617 deleteItemFromDatabase(context, i);
2618 }
2619 }
2620 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2621 // Remove any queued items from the install queue
2622 String spKey = LauncherAppState.getSharedPreferencesKey();
2623 SharedPreferences sp =
2624 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2625 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2626 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002627 mHandler.post(new Runnable() {
2628 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002629 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2630 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002631 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002632 }
2633 }
2634 });
Joe Onoratobe386092009-11-17 17:32:16 -08002635 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002636
Michael Jurkac402cd92013-05-20 15:49:32 +02002637 final ArrayList<Object> widgetsAndShortcuts =
2638 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002639 mHandler.post(new Runnable() {
2640 @Override
2641 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002642 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2643 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002644 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002645 }
2646 }
2647 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002648
2649 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002650 mHandler.post(new Runnable() {
2651 public void run() {
2652 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2653 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002654 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002655 }
2656 }
2657 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002658 }
2659 }
2660
Michael Jurkac402cd92013-05-20 15:49:32 +02002661 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2662 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2663 PackageManager packageManager = context.getPackageManager();
2664 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2665 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2666 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2667 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2668 Collections.sort(widgetsAndShortcuts,
2669 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2670 return widgetsAndShortcuts;
2671 }
2672
Winson Chungdf95eb12013-10-16 14:57:07 -07002673 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2674 try {
2675 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2676 return !pi.applicationInfo.enabled;
2677 } catch (NameNotFoundException e) {
2678 // Fall through
2679 }
2680 return false;
2681 }
Winson Chung1323b482013-08-05 12:41:55 -07002682 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002683 if (cn == null) {
2684 return false;
2685 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002686 if (isPackageDisabled(pm, cn.getPackageName())) {
2687 return false;
2688 }
Winson Chungee055712013-07-30 14:46:24 -07002689
2690 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002691 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002692 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002693 return (pm.getActivityInfo(cn, 0) != null);
2694 } catch (NameNotFoundException e) {
2695 return false;
2696 }
2697 }
2698
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002700 * This is called from the code that adds shortcuts from the intent receiver. This
2701 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002702 */
Joe Onorato56d82912010-03-07 14:32:10 -05002703 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002704 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002706
Joe Onorato56d82912010-03-07 14:32:10 -05002707 /**
2708 * Make an ShortcutInfo object for a shortcut that is an application.
2709 *
2710 * If c is not null, then it will be used to fill in missing data like the title and icon.
2711 */
2712 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002713 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002714 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002715 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002716 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002717 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002718 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002719 } else {
2720 try {
2721 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2722 info.initFlagsAndFirstInstallTime(pi);
2723 } catch (NameNotFoundException e) {
2724 Log.d(TAG, "getPackInfo failed for package " +
2725 componentName.getPackageName());
2726 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002727 }
2728
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002729 // TODO: See if the PackageManager knows about this case. If it doesn't
2730 // then return null & delete this.
2731
Joe Onorato56d82912010-03-07 14:32:10 -05002732 // the resource -- This may implicitly give us back the fallback icon,
2733 // but don't worry about that. All we're doing with usingFallbackIcon is
2734 // to avoid saving lots of copies of that in the database, and most apps
2735 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002736
2737 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2738 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2739 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002740 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002741 ResolveInfo resolveInfo = null;
2742 ComponentName oldComponent = intent.getComponent();
2743 Intent newIntent = new Intent(intent.getAction(), null);
2744 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2745 newIntent.setPackage(oldComponent.getPackageName());
2746 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2747 for (ResolveInfo i : infos) {
2748 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2749 i.activityInfo.name);
2750 if (cn.equals(oldComponent)) {
2751 resolveInfo = i;
2752 }
2753 }
2754 if (resolveInfo == null) {
2755 resolveInfo = manager.resolveActivity(intent, 0);
2756 }
Joe Onorato56d82912010-03-07 14:32:10 -05002757 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002758 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002759 }
Joe Onorato56d82912010-03-07 14:32:10 -05002760 // the db
2761 if (icon == null) {
2762 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002763 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002764 }
2765 }
2766 // the fallback icon
2767 if (icon == null) {
2768 icon = getFallbackIcon();
2769 info.usingFallbackIcon = true;
2770 }
2771 info.setIcon(icon);
2772
2773 // from the resource
2774 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002775 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2776 if (labelCache != null && labelCache.containsKey(key)) {
2777 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002778 } else {
2779 info.title = resolveInfo.activityInfo.loadLabel(manager);
2780 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002781 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002782 }
2783 }
Joe Onorato56d82912010-03-07 14:32:10 -05002784 }
2785 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002787 if (c != null) {
2788 info.title = c.getString(titleIndex);
2789 }
2790 }
2791 // fall back to the class name of the activity
2792 if (info.title == null) {
2793 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2796 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002798
Winson Chung64359a52013-07-08 17:17:08 -07002799 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2800 ItemInfoFilter f) {
2801 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2802 for (ItemInfo i : infos) {
2803 if (i instanceof ShortcutInfo) {
2804 ShortcutInfo info = (ShortcutInfo) i;
2805 ComponentName cn = info.intent.getComponent();
2806 if (cn != null && f.filterItem(null, info, cn)) {
2807 filtered.add(info);
2808 }
2809 } else if (i instanceof FolderInfo) {
2810 FolderInfo info = (FolderInfo) i;
2811 for (ShortcutInfo s : info.contents) {
2812 ComponentName cn = s.intent.getComponent();
2813 if (cn != null && f.filterItem(info, s, cn)) {
2814 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002815 }
2816 }
Winson Chung64359a52013-07-08 17:17:08 -07002817 } else if (i instanceof LauncherAppWidgetInfo) {
2818 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2819 ComponentName cn = info.providerName;
2820 if (cn != null && f.filterItem(null, info, cn)) {
2821 filtered.add(info);
2822 }
Winson Chung8a435102012-08-30 17:16:53 -07002823 }
2824 }
Winson Chung64359a52013-07-08 17:17:08 -07002825 return new ArrayList<ItemInfo>(filtered);
2826 }
2827
2828 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002829 ItemInfoFilter filter = new ItemInfoFilter() {
2830 @Override
2831 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2832 return cn.getPackageName().equals(pn);
2833 }
2834 };
2835 return filterItemInfos(sBgItemsIdMap.values(), filter);
2836 }
2837
2838 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002839 ItemInfoFilter filter = new ItemInfoFilter() {
2840 @Override
2841 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2842 return cn.equals(cname);
2843 }
2844 };
2845 return filterItemInfos(sBgItemsIdMap.values(), filter);
2846 }
2847
2848 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2849 if (i instanceof ShortcutInfo) {
2850 ShortcutInfo info = (ShortcutInfo) i;
2851 // We need to check for ACTION_MAIN otherwise getComponent() might
2852 // return null for some shortcuts (for instance, for shortcuts to
2853 // web pages.)
2854 Intent intent = info.intent;
2855 ComponentName name = intent.getComponent();
2856 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2857 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2858 return true;
2859 }
2860 }
2861 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002862 }
2863
2864 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002865 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002867 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002868 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2869 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870
Joe Onorato56d82912010-03-07 14:32:10 -05002871 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002872 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002874
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002875 // TODO: If there's an explicit component and we can't install that, delete it.
2876
Joe Onorato56d82912010-03-07 14:32:10 -05002877 info.title = c.getString(titleIndex);
2878
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 int iconType = c.getInt(iconTypeIndex);
2880 switch (iconType) {
2881 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2882 String packageName = c.getString(iconPackageIndex);
2883 String resourceName = c.getString(iconResourceIndex);
2884 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002885 info.customIcon = false;
2886 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002889 if (resources != null) {
2890 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002891 icon = Utilities.createIconBitmap(
2892 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002895 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 }
Joe Onorato56d82912010-03-07 14:32:10 -05002897 // the db
2898 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002899 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002900 }
2901 // the fallback icon
2902 if (icon == null) {
2903 icon = getFallbackIcon();
2904 info.usingFallbackIcon = true;
2905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 break;
2907 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002908 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002909 if (icon == null) {
2910 icon = getFallbackIcon();
2911 info.customIcon = false;
2912 info.usingFallbackIcon = true;
2913 } else {
2914 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 break;
2917 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002918 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002919 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 info.customIcon = false;
2921 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002923 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 return info;
2925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926
Michael Jurka931dc972011-08-05 15:08:15 -07002927 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002928 @SuppressWarnings("all") // suppress dead code warning
2929 final boolean debug = false;
2930 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002931 Log.d(TAG, "getIconFromCursor app="
2932 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2933 }
2934 byte[] data = c.getBlob(iconIndex);
2935 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002936 return Utilities.createIconBitmap(
2937 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002938 } catch (Exception e) {
2939 return null;
2940 }
2941 }
2942
Winson Chung3d503fb2011-07-13 17:25:49 -07002943 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2944 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002945 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002946 if (info == null) {
2947 return null;
2948 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002949 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002950
2951 return info;
2952 }
2953
Winson Chunga9abd0e2010-10-27 17:18:37 -07002954 /**
Winson Chung55cef262010-10-28 14:14:18 -07002955 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2956 */
2957 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2958 ComponentName component) {
2959 List<AppWidgetProviderInfo> widgets =
2960 AppWidgetManager.getInstance(context).getInstalledProviders();
2961 for (AppWidgetProviderInfo info : widgets) {
2962 if (info.provider.equals(component)) {
2963 return info;
2964 }
2965 }
2966 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002967 }
2968
Winson Chung68846fd2010-10-29 11:00:27 -07002969 /**
2970 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2971 */
2972 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2973 final PackageManager packageManager = context.getPackageManager();
2974 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2975 new ArrayList<WidgetMimeTypeHandlerData>();
2976
2977 final Intent supportsIntent =
2978 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2979 supportsIntent.setType(mimeType);
2980
2981 // Create a set of widget configuration components that we can test against
2982 final List<AppWidgetProviderInfo> widgets =
2983 AppWidgetManager.getInstance(context).getInstalledProviders();
2984 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2985 new HashMap<ComponentName, AppWidgetProviderInfo>();
2986 for (AppWidgetProviderInfo info : widgets) {
2987 configurationComponentToWidget.put(info.configure, info);
2988 }
2989
2990 // Run through each of the intents that can handle this type of clip data, and cross
2991 // reference them with the components that are actual configuration components
2992 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2993 PackageManager.MATCH_DEFAULT_ONLY);
2994 for (ResolveInfo info : activities) {
2995 final ActivityInfo activityInfo = info.activityInfo;
2996 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2997 activityInfo.name);
2998 if (configurationComponentToWidget.containsKey(infoComponent)) {
2999 supportedConfigurationActivities.add(
3000 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3001 configurationComponentToWidget.get(infoComponent)));
3002 }
3003 }
3004 return supportedConfigurationActivities;
3005 }
3006
Winson Chunga9abd0e2010-10-27 17:18:37 -07003007 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003008 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3009 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3010 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3011
Adam Cohend9198822011-11-22 16:42:47 -08003012 if (intent == null) {
3013 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3014 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3015 return null;
3016 }
3017
Joe Onorato0589f0f2010-02-08 13:44:00 -08003018 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003019 boolean customIcon = false;
3020 ShortcutIconResource iconResource = null;
3021
3022 if (bitmap != null && bitmap instanceof Bitmap) {
3023 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003024 customIcon = true;
3025 } else {
3026 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3027 if (extra != null && extra instanceof ShortcutIconResource) {
3028 try {
3029 iconResource = (ShortcutIconResource) extra;
3030 final PackageManager packageManager = context.getPackageManager();
3031 Resources resources = packageManager.getResourcesForApplication(
3032 iconResource.packageName);
3033 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003034 icon = Utilities.createIconBitmap(
3035 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003036 } catch (Exception e) {
3037 Log.w(TAG, "Could not load shortcut icon: " + extra);
3038 }
3039 }
3040 }
3041
Michael Jurkac9d95c52011-08-29 14:03:34 -07003042 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003043
3044 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003045 if (fallbackIcon != null) {
3046 icon = fallbackIcon;
3047 } else {
3048 icon = getFallbackIcon();
3049 info.usingFallbackIcon = true;
3050 }
Joe Onorato56d82912010-03-07 14:32:10 -05003051 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003052 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003053
Joe Onorato0589f0f2010-02-08 13:44:00 -08003054 info.title = name;
3055 info.intent = intent;
3056 info.customIcon = customIcon;
3057 info.iconResource = iconResource;
3058
3059 return info;
3060 }
3061
Winson Chungaac01e12011-08-17 10:37:13 -07003062 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3063 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003064 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003065 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003066 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003067 }
Joe Onorato56d82912010-03-07 14:32:10 -05003068 // If this icon doesn't have a custom icon, check to see
3069 // what's stored in the DB, and if it doesn't match what
3070 // we're going to show, store what we are going to show back
3071 // into the DB. We do this so when we're loading, if the
3072 // package manager can't find an icon (for example because
3073 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003074 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003075 cache.put(info, c.getBlob(iconIndex));
3076 return true;
3077 }
3078 return false;
3079 }
3080 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3081 boolean needSave = false;
3082 try {
3083 if (data != null) {
3084 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3085 Bitmap loaded = info.getIcon(mIconCache);
3086 needSave = !saved.sameAs(loaded);
3087 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003088 needSave = true;
3089 }
Winson Chungaac01e12011-08-17 10:37:13 -07003090 } catch (Exception e) {
3091 needSave = true;
3092 }
3093 if (needSave) {
3094 Log.d(TAG, "going to save icon bitmap for info=" + info);
3095 // This is slower than is ideal, but this only happens once
3096 // or when the app is updated with a new icon.
3097 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003098 }
3099 }
3100
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003102 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 * or make a new one.
3104 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003105 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 // See if a placeholder was created for us already
3107 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003108 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003110 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 folders.put(id, folderInfo);
3112 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003113 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 }
3115
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003116 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003117 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003118 return new Comparator<AppInfo>() {
3119 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003120 int result = collator.compare(a.title.toString().trim(),
3121 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003122 if (result == 0) {
3123 result = a.componentName.compareTo(b.componentName);
3124 }
3125 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003126 }
Winson Chung11904872012-09-17 16:58:46 -07003127 };
3128 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003129 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3130 = new Comparator<AppInfo>() {
3131 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003132 if (a.firstInstallTime < b.firstInstallTime) return 1;
3133 if (a.firstInstallTime > b.firstInstallTime) return -1;
3134 return 0;
3135 }
3136 };
Winson Chung11904872012-09-17 16:58:46 -07003137 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3138 final Collator collator = Collator.getInstance();
3139 return new Comparator<AppWidgetProviderInfo>() {
3140 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003141 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003142 }
3143 };
3144 }
Winson Chung5308f242011-08-18 12:12:41 -07003145 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3146 if (info.activityInfo != null) {
3147 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3148 } else {
3149 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3150 }
3151 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003152 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003153 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003154 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003155 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003156 ShortcutNameComparator(PackageManager pm) {
3157 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003158 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003159 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003160 }
3161 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3162 mPackageManager = pm;
3163 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003164 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003165 }
3166 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003167 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003168 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3169 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3170 if (mLabelCache.containsKey(keyA)) {
3171 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003172 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003173 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003174
Winson Chung5308f242011-08-18 12:12:41 -07003175 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003176 }
Winson Chung5308f242011-08-18 12:12:41 -07003177 if (mLabelCache.containsKey(keyB)) {
3178 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003179 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003180 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003181
Winson Chung5308f242011-08-18 12:12:41 -07003182 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003183 }
Winson Chung11904872012-09-17 16:58:46 -07003184 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003185 }
3186 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003187 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003188 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003189 private PackageManager mPackageManager;
3190 private HashMap<Object, String> mLabelCache;
3191 WidgetAndShortcutNameComparator(PackageManager pm) {
3192 mPackageManager = pm;
3193 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003194 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003195 }
3196 public final int compare(Object a, Object b) {
3197 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003198 if (mLabelCache.containsKey(a)) {
3199 labelA = mLabelCache.get(a);
3200 } else {
3201 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003202 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003203 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003204 mLabelCache.put(a, labelA);
3205 }
3206 if (mLabelCache.containsKey(b)) {
3207 labelB = mLabelCache.get(b);
3208 } else {
3209 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003210 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003211 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003212 mLabelCache.put(b, labelB);
3213 }
Winson Chung11904872012-09-17 16:58:46 -07003214 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003215 }
3216 };
Joe Onoratobe386092009-11-17 17:32:16 -08003217
3218 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003219 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003220 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3221 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3222 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3223 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003224 if (mLoaderTask != null) {
3225 mLoaderTask.dumpState();
3226 } else {
3227 Log.d(TAG, "mLoaderTask=null");
3228 }
Joe Onoratobe386092009-11-17 17:32:16 -08003229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230}