blob: b2cfb2456a978727292b9fec4a1ae141bb6f83f3 [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;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010046
Daniel Sandler325dc232013-06-05 22:57:57 -040047import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080048
Michael Jurkac2f801e2011-07-12 14:19:46 -070049import java.lang.ref.WeakReference;
50import java.net.URISyntaxException;
51import java.text.Collator;
52import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070053import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070054import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070055import java.util.Collections;
56import java.util.Comparator;
57import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070058import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070059import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070060import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070061import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070062import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070063import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065/**
66 * Maintains in-memory state of the Launcher. It is expected that there should be only one
67 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070068 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040070public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080071 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070073
Daniel Sandler8707e0f2013-08-15 15:54:18 -070074 // true = use a "More Apps" folder for non-workspace apps on upgrade
75 // false = strew non-workspace apps across the workspace on upgrade
76 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
77
Joe Onorato36115782010-06-17 13:28:48 -040078 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050079 private static final long INVALID_SCREEN_ID = -1L;
Winson Chungee055712013-07-30 14:46:24 -070080 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040081
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 private final Object mLock = new Object();
84 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040085 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070086 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020087 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Winson Chung81b52252012-08-27 15:34:29 -070089 // Specific runnable types that are run on the main thread deferred handler, this allows us to
90 // clear all queued binding runnables when the Launcher activity is destroyed.
91 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
92 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
93
94
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070095 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
96 static {
97 sWorkerThread.start();
98 }
99 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
100
Joe Onoratocc67f472010-06-08 10:54:30 -0700101 // We start off with everything not loaded. After that, we assume that
102 // our monitoring of the package manager provides all updates and we never
103 // need to do a requery. These are only ever touched from the loader thread.
104 private boolean mWorkspaceLoaded;
105 private boolean mAllAppsLoaded;
106
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700107 // When we are loading pages synchronously, we can't just post the binding of items on the side
108 // pages as this delays the rotation process. Instead, we wait for a callback from the first
109 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
110 // a normal load, we also clear this set of Runnables.
111 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
112
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700115 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700116 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800117
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700118 // The lock that must be acquired before referencing any static bg data structures. Unlike
119 // other locks, this one can generally be held long-term because we never expect any of these
120 // static data structures to be referenced outside of the worker thread except on the first
121 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700122 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700123
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700127
Adam Cohen487f7dd2012-06-28 18:12:10 -0700128 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
129 // created by LauncherModel that are directly on the home screen (however, no widgets or
130 // shortcuts within folders).
131 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700132
Adam Cohen487f7dd2012-06-28 18:12:10 -0700133 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
134 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700135 new ArrayList<LauncherAppWidgetInfo>();
136
Adam Cohen487f7dd2012-06-28 18:12:10 -0700137 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
138 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700139
Adam Cohen487f7dd2012-06-28 18:12:10 -0700140 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
141 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700142
143 // sBgWorkspaceScreens is the ordered set of workspace screens.
144 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
145
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700146 // </ only access in worker thread >
147
148 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800149 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Reena Lee99a73f32011-10-24 17:27:37 -0700151 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700152
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700154 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 public int getCurrentWorkspaceScreen();
156 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700157 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
158 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700159 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700160 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500161 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700162 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200164 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700165 public void bindAppsAdded(ArrayList<Long> newScreens,
166 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700167 ArrayList<ItemInfo> addAnimated,
168 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200169 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700170 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700171 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200172 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100173 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700174 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700175 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700176 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung64359a52013-07-08 17:17:08 -0700179 public interface ItemInfoFilter {
180 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
181 }
182
Bjorn Bringert1307f632013-10-03 22:31:03 +0100183 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 final Context context = app.getContext();
185
Winson Chungee055712013-07-30 14:46:24 -0700186 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400187 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100188 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 mIconCache = iconCache;
190
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700192 Configuration config = res.getConfiguration();
193 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 }
195
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700196 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
197 * posted on the main thread handler. */
198 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700199 runOnMainThread(r, 0);
200 }
201 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700202 if (sWorkerThread.getThreadId() == Process.myTid()) {
203 // If we are on the worker thread, post onto the main handler
204 mHandler.post(r);
205 } else {
206 r.run();
207 }
208 }
209
210 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
211 * posted on the worker thread handler. */
212 private static void runOnWorkerThread(Runnable r) {
213 if (sWorkerThread.getThreadId() == Process.myTid()) {
214 r.run();
215 } else {
216 // If we are not on the worker thread, then post to the worker handler
217 sWorker.post(r);
218 }
219 }
220
Winson Chungc9168342013-06-26 14:54:55 -0700221 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
222 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700223 LauncherAppState app = LauncherAppState.getInstance();
224 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
225 final int xCount = (int) grid.numColumns;
226 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700227 boolean[][] occupied = new boolean[xCount][yCount];
228
229 int cellX, cellY, spanX, spanY;
230 for (int i = 0; i < items.size(); ++i) {
231 final ItemInfo item = items.get(i);
232 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
233 if (item.screenId == screen) {
234 cellX = item.cellX;
235 cellY = item.cellY;
236 spanX = item.spanX;
237 spanY = item.spanY;
238 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
239 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
240 occupied[x][y] = true;
241 }
242 }
243 }
244 }
245 }
246
247 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
248 }
249 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700250 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700251 int firstScreenIndex,
252 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700253 // Lock on the app so that we don't try and get the items while apps are being added
254 LauncherAppState app = LauncherAppState.getInstance();
255 LauncherModel model = app.getModel();
256 boolean found = false;
257 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700258 if (sWorkerThread.getThreadId() != Process.myTid()) {
259 // Flush the LauncherModel worker thread, so that if we just did another
260 // processInstallShortcut, we give it time for its shortcut to get added to the
261 // database (getItemsInLocalCoordinates reads the database)
262 model.flushWorkerThread();
263 }
Winson Chungc9168342013-06-26 14:54:55 -0700264 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700265
266 // Try adding to the workspace screens incrementally, starting at the default or center
267 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700268 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
269 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700270 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700271 int[] tmpCoordinates = new int[2];
272 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700273 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700274 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700275 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700276 }
277 }
278 }
Winson Chungc9168342013-06-26 14:54:55 -0700279 return null;
280 }
281
Winson Chung94d67682013-09-25 16:29:40 -0700282 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
283 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700284 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700285 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700286 }
Winson Chung94d67682013-09-25 16:29:40 -0700287 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800288 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
289 if (workspaceApps == null || allAppsApps == null) {
290 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
291 }
Winson Chung94d67682013-09-25 16:29:40 -0700292 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700293 return;
Winson Chung997a9232013-07-24 15:33:46 -0700294 }
Winson Chung64359a52013-07-08 17:17:08 -0700295 // Process the newly added applications and add them to the database first
296 Runnable r = new Runnable() {
297 public void run() {
298 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
299 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
300
Winson Chung76828c82013-08-19 15:43:29 -0700301 // Get the list of workspace screens. We need to append to this list and
302 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
303 // called.
304 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
305 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
306 for (Integer i : orderedScreens.keySet()) {
307 long screenId = orderedScreens.get(i);
308 workspaceScreens.add(screenId);
309 }
310
Winson Chung64359a52013-07-08 17:17:08 -0700311 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700312 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700313 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700314 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700315 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700316 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700317
318 // Short-circuit this logic if the icon exists somewhere on the workspace
319 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
320 continue;
321 }
322
Winson Chung87412982013-10-03 18:34:14 -0700323 // Add this icon to the db, creating a new page if necessary. If there
324 // is only the empty page then we just add items to the first page.
325 // Otherwise, we add them to the next pages.
326 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700327 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700328 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700329 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200330 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700331
332 // If we can't find a valid position, then just add a new screen.
333 // This takes time so we need to re-queue the add until the new
334 // page is added. Create as many screens as necessary to satisfy
335 // the startSearchPageIndex.
336 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700337 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 while (numPagesToAdd > 0) {
339 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700341 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700342 addedWorkspaceScreensFinal.add(screenId);
343 numPagesToAdd--;
344 }
Winson Chung76828c82013-08-19 15:43:29 -0700345
Winson Chung64359a52013-07-08 17:17:08 -0700346 // Find the coordinate again
347 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700348 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700349 }
350 if (coords == null) {
351 throw new RuntimeException("Coordinates should not be null");
352 }
353
Winson Chung997a9232013-07-24 15:33:46 -0700354 ShortcutInfo shortcutInfo;
355 if (a instanceof ShortcutInfo) {
356 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200357 } else if (a instanceof AppInfo) {
358 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700359 } else {
360 throw new RuntimeException("Unexpected info type");
361 }
Winson Chung94d67682013-09-25 16:29:40 -0700362
Winson Chung64359a52013-07-08 17:17:08 -0700363 // Add the shortcut to the db
364 addItemToDatabase(context, shortcutInfo,
365 LauncherSettings.Favorites.CONTAINER_DESKTOP,
366 coords.first, coords.second[0], coords.second[1], false);
367 // Save the ShortcutInfo for binding in the workspace
368 addedShortcutsFinal.add(shortcutInfo);
369 }
370 }
371
Winson Chung76828c82013-08-19 15:43:29 -0700372 // Update the workspace screens
373 updateWorkspaceScreenOrder(context, workspaceScreens);
374
Winson Chung94d67682013-09-25 16:29:40 -0700375 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700376 runOnMainThread(new Runnable() {
377 public void run() {
378 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
379 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700380 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
381 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700382 if (!addedShortcutsFinal.isEmpty()) {
383 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
384 long lastScreenId = info.screenId;
385 for (ItemInfo i : addedShortcutsFinal) {
386 if (i.screenId == lastScreenId) {
387 addAnimated.add(i);
388 } else {
389 addNotAnimated.add(i);
390 }
Winson Chung997a9232013-07-24 15:33:46 -0700391 }
392 }
Winson Chungd64d1762013-08-20 14:37:16 -0700393 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700394 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700395 }
Winson Chung64359a52013-07-08 17:17:08 -0700396 }
Winson Chung997a9232013-07-24 15:33:46 -0700397 });
398 }
Winson Chung64359a52013-07-08 17:17:08 -0700399 }
400 };
401 runOnWorkerThread(r);
402 }
403
Joe Onorato56d82912010-03-07 14:32:10 -0500404 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700405 if (mDefaultIcon == null) {
406 final Context context = LauncherAppState.getInstance().getContext();
407 mDefaultIcon = Utilities.createIconBitmap(
408 mIconCache.getFullResDefaultActivityIcon(), context);
409 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800410 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Winson Chung81b52252012-08-27 15:34:29 -0700413 public void unbindItemInfosAndClearQueuedBindRunnables() {
414 if (sWorkerThread.getThreadId() == Process.myTid()) {
415 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
416 "main thread");
417 }
418
419 // Clear any deferred bind runnables
420 mDeferredBindRunnables.clear();
421 // Remove any queued bind runnables
422 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
423 // Unbind all the workspace items
424 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700425 }
426
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700427 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700428 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700429 // Ensure that we don't use the same workspace items data structure on the main thread
430 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
432 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700433 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700434 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
435 tmpAppWidgets.addAll(sBgAppWidgets);
436 }
437 Runnable r = new Runnable() {
438 @Override
439 public void run() {
440 for (ItemInfo item : tmpWorkspaceItems) {
441 item.unbind();
442 }
443 for (ItemInfo item : tmpAppWidgets) {
444 item.unbind();
445 }
446 }
447 };
448 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700449 }
450
Joe Onorato9c1289c2009-08-17 11:03:03 -0400451 /**
452 * Adds an item to the DB if it was not created previously, or move it to a new
453 * <container, screen, cellX, cellY>
454 */
455 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700456 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400457 if (item.container == ItemInfo.NO_ID) {
458 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700459 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400460 } else {
461 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700462 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464 }
465
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700466 static void checkItemInfoLocked(
467 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
468 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
469 if (modelItem != null && item != modelItem) {
470 // check all the data is consistent
471 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
472 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
473 ShortcutInfo shortcut = (ShortcutInfo) item;
474 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
475 modelShortcut.intent.filterEquals(shortcut.intent) &&
476 modelShortcut.id == shortcut.id &&
477 modelShortcut.itemType == shortcut.itemType &&
478 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700479 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700480 modelShortcut.cellX == shortcut.cellX &&
481 modelShortcut.cellY == shortcut.cellY &&
482 modelShortcut.spanX == shortcut.spanX &&
483 modelShortcut.spanY == shortcut.spanY &&
484 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
485 (modelShortcut.dropPos != null &&
486 shortcut.dropPos != null &&
487 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
488 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
489 // For all intents and purposes, this is the same object
490 return;
491 }
492 }
493
494 // the modelItem needs to match up perfectly with item if our model is
495 // to be consistent with the database-- for now, just require
496 // modelItem == item or the equality check above
497 String msg = "item: " + ((item != null) ? item.toString() : "null") +
498 "modelItem: " +
499 ((modelItem != null) ? modelItem.toString() : "null") +
500 "Error: ItemInfo passed to checkItemInfo doesn't match original";
501 RuntimeException e = new RuntimeException(msg);
502 if (stackTrace != null) {
503 e.setStackTrace(stackTrace);
504 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800505 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700506 }
507 }
508
Michael Jurka816474f2012-06-25 14:49:02 -0700509 static void checkItemInfo(final ItemInfo item) {
510 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
511 final long itemId = item.id;
512 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700513 public void run() {
514 synchronized (sBgLock) {
515 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700516 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700517 }
518 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700519 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700520 }
521
Michael Jurkac9d95c52011-08-29 14:03:34 -0700522 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
523 final ItemInfo item, final String callingFunction) {
524 final long itemId = item.id;
525 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
526 final ContentResolver cr = context.getContentResolver();
527
Adam Cohen487f7dd2012-06-28 18:12:10 -0700528 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700529 Runnable r = new Runnable() {
530 public void run() {
531 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700532 updateItemArrays(item, itemId, stackTrace);
533 }
534 };
535 runOnWorkerThread(r);
536 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700537
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700538 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
539 final ArrayList<ItemInfo> items, final String callingFunction) {
540 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700541
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700542 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
543 Runnable r = new Runnable() {
544 public void run() {
545 ArrayList<ContentProviderOperation> ops =
546 new ArrayList<ContentProviderOperation>();
547 int count = items.size();
548 for (int i = 0; i < count; i++) {
549 ItemInfo item = items.get(i);
550 final long itemId = item.id;
551 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
552 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700553
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700554 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
555 updateItemArrays(item, itemId, stackTrace);
556
557 }
558 try {
559 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
560 } catch (Exception e) {
561 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700562 }
563 }
564 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700565 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700566 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700567
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700568 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
569 // Lock on mBgLock *after* the db operation
570 synchronized (sBgLock) {
571 checkItemInfoLocked(itemId, item, stackTrace);
572
573 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
574 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
575 // Item is in a folder, make sure this folder exists
576 if (!sBgFolders.containsKey(item.container)) {
577 // An items container is being set to a that of an item which is not in
578 // the list of Folders.
579 String msg = "item: " + item + " container being set to: " +
580 item.container + ", not in the list of folders";
581 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700582 }
583 }
584
585 // Items are added/removed from the corresponding FolderInfo elsewhere, such
586 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
587 // that are on the desktop, as appropriate
588 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800589 if (modelItem != null &&
590 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
591 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700592 switch (modelItem.itemType) {
593 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
594 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
595 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
596 if (!sBgWorkspaceItems.contains(modelItem)) {
597 sBgWorkspaceItems.add(modelItem);
598 }
599 break;
600 default:
601 break;
602 }
603 } else {
604 sBgWorkspaceItems.remove(modelItem);
605 }
606 }
607 }
608
Michael Jurkac7700af2013-05-14 20:17:58 +0200609 public void flushWorkerThread() {
610 mFlushingWorkerThread = true;
611 Runnable waiter = new Runnable() {
612 public void run() {
613 synchronized (this) {
614 notifyAll();
615 mFlushingWorkerThread = false;
616 }
617 }
618 };
619
620 synchronized(waiter) {
621 runOnWorkerThread(waiter);
622 if (mLoaderTask != null) {
623 synchronized(mLoaderTask) {
624 mLoaderTask.notify();
625 }
626 }
627 boolean success = false;
628 while (!success) {
629 try {
630 waiter.wait();
631 success = true;
632 } catch (InterruptedException e) {
633 }
634 }
635 }
636 }
637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400639 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700640 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700641 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 item.cellX = cellX;
645 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700646
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 // We store hotseat items in canonical form which is this orientation invariant position
648 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700653 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655
656 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700658 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
659 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700660 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661
Michael Jurkac9d95c52011-08-29 14:03:34 -0700662 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700663 }
664
665 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700666 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
667 * cellX, cellY have already been updated on the ItemInfos.
668 */
669 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
670 final long container, final int screen) {
671
672 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
673 int count = items.size();
674
675 for (int i = 0; i < count; i++) {
676 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700677 item.container = container;
678
679 // We store hotseat items in canonical form which is this orientation invariant position
680 // in the hotseat
681 if (context instanceof Launcher && screen < 0 &&
682 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 item.cellY);
685 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700687 }
688
689 final ContentValues values = new ContentValues();
690 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
691 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
692 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700693 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700694
695 contentValues.add(values);
696 }
697 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
698 }
699
700 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800702 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700703 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700704 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700705 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800706 item.cellX = cellX;
707 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700708 item.spanX = spanX;
709 item.spanY = spanY;
710
711 // We store hotseat items in canonical form which is this orientation invariant position
712 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700718 }
Adam Cohend4844c32011-02-18 19:25:06 -0800719
Adam Cohend4844c32011-02-18 19:25:06 -0800720 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800721 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700722 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
723 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
724 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
725 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700726 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800727
Michael Jurka816474f2012-06-25 14:49:02 -0700728 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700729 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700730
731 /**
732 * Update an item to the database in a specified container.
733 */
734 static void updateItemInDatabase(Context context, final ItemInfo item) {
735 final ContentValues values = new ContentValues();
736 item.onAddToDatabase(values);
737 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
738 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800739 }
740
741 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 * Returns true if the shortcuts already exists in the database.
743 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 static boolean shortcutExists(Context context, String title, Intent intent) {
746 final ContentResolver cr = context.getContentResolver();
747 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
748 new String[] { "title", "intent" }, "title=? and intent=?",
749 new String[] { title, intent.toUri(0) }, null);
750 boolean result = false;
751 try {
752 result = c.moveToFirst();
753 } finally {
754 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700757 }
758
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700760 * Returns an ItemInfo array containing all the items in the LauncherModel.
761 * The ItemInfo.id is not set through this function.
762 */
763 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
764 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
765 final ContentResolver cr = context.getContentResolver();
766 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
767 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
768 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
769 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
770
771 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
772 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
773 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
774 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
775 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
776 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
777 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
778
779 try {
780 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700781 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700782 item.cellX = c.getInt(cellXIndex);
783 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700784 item.spanX = Math.max(1, c.getInt(spanXIndex));
785 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 item.container = c.getInt(containerIndex);
787 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700788 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700789
790 items.add(item);
791 }
792 } catch (Exception e) {
793 items.clear();
794 } finally {
795 c.close();
796 }
797
798 return items;
799 }
800
801 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
803 */
804 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
805 final ContentResolver cr = context.getContentResolver();
806 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
807 "_id=? and (itemType=? or itemType=?)",
808 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700809 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700810
Joe Onorato9c1289c2009-08-17 11:03:03 -0400811 try {
812 if (c.moveToFirst()) {
813 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
814 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
815 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
816 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
817 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
818 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819
Joe Onorato9c1289c2009-08-17 11:03:03 -0400820 FolderInfo folderInfo = null;
821 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700822 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
823 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700825 }
826
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 folderInfo.title = c.getString(titleIndex);
828 folderInfo.id = id;
829 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700830 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700831 folderInfo.cellX = c.getInt(cellXIndex);
832 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833
834 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700835 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 } finally {
837 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700838 }
839
840 return null;
841 }
842
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 /**
844 * Add an item to the database in a specified container. Sets the container, screen, cellX and
845 * cellY fields of the item. Also assigns an ID to the item.
846 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700848 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 item.cellX = cellX;
851 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 // We store hotseat items in canonical form which is this orientation invariant position
853 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700858 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860
861 final ContentValues values = new ContentValues();
862 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 item.onAddToDatabase(values);
864
Michael Jurka414300a2013-08-27 15:42:35 +0200865 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700866 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700868
Michael Jurkac9d95c52011-08-29 14:03:34 -0700869 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 public void run() {
871 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
872 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700874 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700875 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700876 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700877 sBgItemsIdMap.put(item.id, item);
878 switch (item.itemType) {
879 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
880 sBgFolders.put(item.id, (FolderInfo) item);
881 // Fall through
882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
884 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
885 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
886 sBgWorkspaceItems.add(item);
887 } else {
888 if (!sBgFolders.containsKey(item.container)) {
889 // Adding an item to a folder that doesn't exist.
890 String msg = "adding item: " + item + " to a folder that " +
891 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700892 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700894 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700895 break;
896 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
897 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
898 break;
899 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700900 }
901 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700902 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700903 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700904 }
905
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700907 * Creates a new unique child id, for a given cell span across all layouts.
908 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700909 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700913 }
914
Winson Chungaafa03c2010-06-11 17:34:16 -0700915 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700916 * Removes the specified item from the database
917 * @param context
918 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700922 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700923
Michael Jurka83df1882011-08-31 20:59:26 -0700924 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700926 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700927
928 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700929 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700930 switch (item.itemType) {
931 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
932 sBgFolders.remove(item.id);
933 for (ItemInfo info: sBgItemsIdMap.values()) {
934 if (info.container == item.id) {
935 // We are deleting a folder which still contains items that
936 // think they are contained by that folder.
937 String msg = "deleting a folder (" + item + ") which still " +
938 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700939 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700941 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700942 sBgWorkspaceItems.remove(item);
943 break;
944 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
945 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
946 sBgWorkspaceItems.remove(item);
947 break;
948 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
949 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
950 break;
951 }
952 sBgItemsIdMap.remove(item.id);
953 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700954 }
955 }
Michael Jurka83df1882011-08-31 20:59:26 -0700956 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700957 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400958 }
959
960 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 * Update the order of the workspace screens in the database. The array list contains
962 * a list of screen ids in the order that they should appear.
963 */
Winson Chungc9168342013-06-26 14:54:55 -0700964 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800965 // Log to disk
966 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
967 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
968
Winson Chung64359a52013-07-08 17:17:08 -0700969 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700970 final ContentResolver cr = context.getContentResolver();
971 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
972
973 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700974 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 while (iter.hasNext()) {
976 long id = iter.next();
977 if (id < 0) {
978 iter.remove();
979 }
980 }
981
982 Runnable r = new Runnable() {
983 @Override
984 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 // Clear the table
986 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700987 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700988 ContentValues[] values = new ContentValues[count];
989 for (int i = 0; i < count; i++) {
990 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700991 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
993 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 values[i] = v;
995 }
996 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700997
Winson Chungba9c37f2013-08-30 14:11:37 -0700998 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700999 sBgWorkspaceScreens.clear();
1000 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001001 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001002 }
1003 };
1004 runOnWorkerThread(r);
1005 }
1006
1007 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 * Remove the contents of the specified folder from the database
1009 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 final ContentResolver cr = context.getContentResolver();
1012
Michael Jurkac9d95c52011-08-29 14:03:34 -07001013 Runnable r = new Runnable() {
1014 public void run() {
1015 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001017 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001018 sBgItemsIdMap.remove(info.id);
1019 sBgFolders.remove(info.id);
1020 sBgDbIconCache.remove(info);
1021 sBgWorkspaceItems.remove(info);
1022 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001023
Michael Jurkac9d95c52011-08-29 14:03:34 -07001024 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1025 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001027 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001028 for (ItemInfo childInfo : info.contents) {
1029 sBgItemsIdMap.remove(childInfo.id);
1030 sBgDbIconCache.remove(childInfo);
1031 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001032 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001033 }
1034 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001035 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 }
1037
1038 /**
1039 * Set this as the current Launcher activity object for the loader.
1040 */
1041 public void initialize(Callbacks callbacks) {
1042 synchronized (mLock) {
1043 mCallbacks = new WeakReference<Callbacks>(callbacks);
1044 }
1045 }
1046
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001047 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1049 * ACTION_PACKAGE_CHANGED.
1050 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001051 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001052 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001053 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001056
Joe Onorato36115782010-06-17 13:28:48 -04001057 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1058 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1059 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1060 final String packageName = intent.getData().getSchemeSpecificPart();
1061 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001062
Joe Onorato36115782010-06-17 13:28:48 -04001063 int op = PackageUpdatedTask.OP_NONE;
1064
1065 if (packageName == null || packageName.length() == 0) {
1066 // they sent us a bad intent
1067 return;
1068 }
1069
1070 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1071 op = PackageUpdatedTask.OP_UPDATE;
1072 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1073 if (!replacing) {
1074 op = PackageUpdatedTask.OP_REMOVE;
1075 }
1076 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1077 // later, we will update the package at this time
1078 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1079 if (!replacing) {
1080 op = PackageUpdatedTask.OP_ADD;
1081 } else {
1082 op = PackageUpdatedTask.OP_UPDATE;
1083 }
1084 }
1085
1086 if (op != PackageUpdatedTask.OP_NONE) {
1087 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1088 }
1089
1090 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001091 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onoratocec58332010-10-07 14:37:40 -04001092 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Winson Chung3ee4a472014-01-06 15:53:37 -08001093 if (!replacing) {
1094 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1095 if (mAppsCanBeOnRemoveableStorage) {
1096 // Only rebind if we support removable storage. It catches the case where
1097 // apps on the external sd card need to be reloaded
1098 startLoaderFromBackground();
1099 }
1100 } else {
1101 // If we are replacing then just update the packages in the list
1102 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1103 packages));
1104 }
Joe Onorato36115782010-06-17 13:28:48 -04001105 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
Winson Chung3ee4a472014-01-06 15:53:37 -08001106 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1107 if (!replacing) {
1108 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1109 enqueuePackageUpdated(new PackageUpdatedTask(
1110 PackageUpdatedTask.OP_UNAVAILABLE, packages));
1111 }
1112 // else, we are replacing the packages, so ignore this event and wait for
1113 // EXTERNAL_APPLICATIONS_AVAILABLE to update the packages at that time
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001114 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001115 // If we have changed locale we need to clear out the labels in all apps/workspace.
1116 forceReload();
1117 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1118 // Check if configuration change was an mcc/mnc change which would affect app resources
1119 // and we would need to clear out the labels in all apps/workspace. Same handling as
1120 // above for ACTION_LOCALE_CHANGED
1121 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001122 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001123 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001124 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001125 forceReload();
1126 }
1127 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001128 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001129 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1130 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001131 if (mCallbacks != null) {
1132 Callbacks callbacks = mCallbacks.get();
1133 if (callbacks != null) {
1134 callbacks.bindSearchablesChanged();
1135 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001136 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001137 }
1138 }
1139
Reena Lee93f824a2011-09-23 17:20:28 -07001140 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001141 resetLoadedState(true, true);
1142
Reena Lee93f824a2011-09-23 17:20:28 -07001143 // Do this here because if the launcher activity is running it will be restarted.
1144 // If it's not running startLoaderFromBackground will merely tell it that it needs
1145 // to reload.
1146 startLoaderFromBackground();
1147 }
1148
Winson Chungf0c6ae02012-03-21 16:10:31 -07001149 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1150 synchronized (mLock) {
1151 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1152 // mWorkspaceLoaded to true later
1153 stopLoaderLocked();
1154 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1155 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1156 }
1157 }
1158
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001159 /**
1160 * When the launcher is in the background, it's possible for it to miss paired
1161 * configuration changes. So whenever we trigger the loader from the background
1162 * tell the launcher that it needs to re-run the loader when it comes back instead
1163 * of doing it now.
1164 */
1165 public void startLoaderFromBackground() {
1166 boolean runLoader = false;
1167 if (mCallbacks != null) {
1168 Callbacks callbacks = mCallbacks.get();
1169 if (callbacks != null) {
1170 // Only actually run the loader if they're not paused.
1171 if (!callbacks.setLoadOnResume()) {
1172 runLoader = true;
1173 }
1174 }
1175 }
1176 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001177 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001178 }
Joe Onorato36115782010-06-17 13:28:48 -04001179 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001180
Reena Lee93f824a2011-09-23 17:20:28 -07001181 // If there is already a loader task running, tell it to stop.
1182 // returns true if isLaunching() was true on the old task
1183 private boolean stopLoaderLocked() {
1184 boolean isLaunching = false;
1185 LoaderTask oldTask = mLoaderTask;
1186 if (oldTask != null) {
1187 if (oldTask.isLaunching()) {
1188 isLaunching = true;
1189 }
1190 oldTask.stopLocked();
1191 }
1192 return isLaunching;
1193 }
1194
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001196 synchronized (mLock) {
1197 if (DEBUG_LOADERS) {
1198 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001201 // Clear any deferred bind-runnables from the synchronized load process
1202 // We must do this before any loading/binding is scheduled below.
1203 mDeferredBindRunnables.clear();
1204
Joe Onorato36115782010-06-17 13:28:48 -04001205 // Don't bother to start the thread if we know it's not going to do anything
1206 if (mCallbacks != null && mCallbacks.get() != null) {
1207 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001208 // also, don't downgrade isLaunching if we're already running
1209 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001210 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Derek Prothro7aff3992013-12-10 14:00:37 -05001211 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1212 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1214 } else {
1215 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1216 sWorker.post(mLoaderTask);
1217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 }
1220 }
1221
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 void bindRemainingSynchronousPages() {
1223 // Post the remaining side pages to be loaded
1224 if (!mDeferredBindRunnables.isEmpty()) {
1225 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001226 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001227 }
1228 mDeferredBindRunnables.clear();
1229 }
1230 }
1231
Joe Onorato36115782010-06-17 13:28:48 -04001232 public void stopLoader() {
1233 synchronized (mLock) {
1234 if (mLoaderTask != null) {
1235 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 }
1237 }
Joe Onorato36115782010-06-17 13:28:48 -04001238 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239
Winson Chung76828c82013-08-19 15:43:29 -07001240 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1241 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1242 final ContentResolver contentResolver = context.getContentResolver();
1243 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1244 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1245 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1246
1247 try {
1248 final int idIndex = sc.getColumnIndexOrThrow(
1249 LauncherSettings.WorkspaceScreens._ID);
1250 final int rankIndex = sc.getColumnIndexOrThrow(
1251 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1252 while (sc.moveToNext()) {
1253 try {
1254 long screenId = sc.getLong(idIndex);
1255 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001256 orderedScreens.put(rank, screenId);
1257 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001258 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001259 }
1260 }
1261 } finally {
1262 sc.close();
1263 }
Winson Chunga90303b2013-11-15 13:05:06 -08001264
1265 // Log to disk
1266 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1267 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1268 for (Integer i : orderedScreens.keySet()) {
1269 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1270 }
1271 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1272 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001273 return orderedScreens;
1274 }
1275
Michael Jurkac57b7a82011-08-09 22:02:20 -07001276 public boolean isAllAppsLoaded() {
1277 return mAllAppsLoaded;
1278 }
1279
Winson Chung36a62fe2012-05-06 18:04:42 -07001280 boolean isLoadingWorkspace() {
1281 synchronized (mLock) {
1282 if (mLoaderTask != null) {
1283 return mLoaderTask.isLoadingWorkspace();
1284 }
1285 }
1286 return false;
1287 }
1288
Joe Onorato36115782010-06-17 13:28:48 -04001289 /**
1290 * Runnable for the thread that loads the contents of the launcher:
1291 * - workspace icons
1292 * - widgets
1293 * - all apps icons
1294 */
1295 private class LoaderTask implements Runnable {
1296 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001297 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001298 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001299 private boolean mStopped;
1300 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001301
Winson Chungc3eecff2011-07-11 17:44:15 -07001302 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001303
1304 LoaderTask(Context context, boolean isLaunching) {
1305 mContext = context;
1306 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001307 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
1309
Joe Onorato36115782010-06-17 13:28:48 -04001310 boolean isLaunching() {
1311 return mIsLaunching;
1312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313
Winson Chung36a62fe2012-05-06 18:04:42 -07001314 boolean isLoadingWorkspace() {
1315 return mIsLoadingAndBindingWorkspace;
1316 }
1317
Winson Chungc763c4e2013-07-19 13:49:06 -07001318 /** Returns whether this is an upgrade path */
1319 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001320 mIsLoadingAndBindingWorkspace = true;
1321
Joe Onorato36115782010-06-17 13:28:48 -04001322 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001323 if (DEBUG_LOADERS) {
1324 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001326
Winson Chungc763c4e2013-07-19 13:49:06 -07001327 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001328 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001329 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001330 synchronized (LoaderTask.this) {
1331 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001332 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001333 }
1334 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 }
1336 }
1337
Joe Onorato36115782010-06-17 13:28:48 -04001338 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001339 bindWorkspace(-1, isUpgradePath);
1340 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001341 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001342
Joe Onorato36115782010-06-17 13:28:48 -04001343 private void waitForIdle() {
1344 // Wait until the either we're stopped or the other threads are done.
1345 // This way we don't start loading all apps until the workspace has settled
1346 // down.
1347 synchronized (LoaderTask.this) {
1348 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001349
Joe Onorato36115782010-06-17 13:28:48 -04001350 mHandler.postIdle(new Runnable() {
1351 public void run() {
1352 synchronized (LoaderTask.this) {
1353 mLoadAndBindStepFinished = true;
1354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001356 }
Joe Onorato36115782010-06-17 13:28:48 -04001357 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001358 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001359 }
Joe Onorato36115782010-06-17 13:28:48 -04001360 });
1361
Michael Jurkac7700af2013-05-14 20:17:58 +02001362 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001363 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001364 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1365 // wait no longer than 1sec at a time
1366 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001367 } catch (InterruptedException ex) {
1368 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001369 }
1370 }
Joe Onorato36115782010-06-17 13:28:48 -04001371 if (DEBUG_LOADERS) {
1372 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001373 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001374 + "ms for previous step to finish binding");
1375 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001376 }
Joe Onorato36115782010-06-17 13:28:48 -04001377 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001378
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001379 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001380 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001381 // Ensure that we have a valid page index to load synchronously
1382 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1383 "valid page index");
1384 }
1385 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1386 // Ensure that we don't try and bind a specified page when the pages have not been
1387 // loaded already (we should load everything asynchronously in that case)
1388 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1389 }
1390 synchronized (mLock) {
1391 if (mIsLoaderTaskRunning) {
1392 // Ensure that we are never running the background loading at this point since
1393 // we also touch the background collections
1394 throw new RuntimeException("Error! Background loading is already running");
1395 }
1396 }
1397
1398 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1399 // data structures, we can't allow any other thread to touch that data, but because
1400 // this call is synchronous, we can get away with not locking).
1401
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001402 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001403 // operations from the previous activity. We need to ensure that all queued operations
1404 // are executed before any synchronous binding work is done.
1405 mHandler.flush();
1406
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 // Divide the set of loaded items into those that we are binding synchronously, and
1408 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001409 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001410 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1411 // arise from that.
1412 onlyBindAllApps();
1413 }
1414
Joe Onorato36115782010-06-17 13:28:48 -04001415 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001416 boolean isUpgrade = false;
1417
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 synchronized (mLock) {
1419 mIsLoaderTaskRunning = true;
1420 }
Joe Onorato36115782010-06-17 13:28:48 -04001421 // Optimize for end-user experience: if the Launcher is up and // running with the
1422 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1423 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001424 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001425 // Elevate priority when Home launches for the first time to avoid
1426 // starving at boot time. Staring at a blank home is not cool.
1427 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001428 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1429 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001430 android.os.Process.setThreadPriority(mIsLaunching
1431 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1432 }
Winson Chung64359a52013-07-08 17:17:08 -07001433 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001434 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001435
Joe Onorato36115782010-06-17 13:28:48 -04001436 if (mStopped) {
1437 break keep_running;
1438 }
1439
1440 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1441 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001442 synchronized (mLock) {
1443 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001444 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001445 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1446 }
1447 }
Joe Onorato36115782010-06-17 13:28:48 -04001448 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001449
1450 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001451 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1452 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001453
1454 // Restore the default thread priority after we are done loading items
1455 synchronized (mLock) {
1456 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1457 }
Joe Onorato36115782010-06-17 13:28:48 -04001458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001459
Winson Chungaac01e12011-08-17 10:37:13 -07001460 // Update the saved icons if necessary
1461 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001462 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001463 for (Object key : sBgDbIconCache.keySet()) {
1464 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1465 }
1466 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001467 }
Winson Chungaac01e12011-08-17 10:37:13 -07001468
Winson Chungc58497e2013-09-03 17:48:37 -07001469 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1470 // Ensure that all the applications that are in the system are
1471 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001472 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001473 verifyApplications();
1474 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001475 }
1476
Joe Onorato36115782010-06-17 13:28:48 -04001477 // Clear out this reference, otherwise we end up holding it until all of the
1478 // callback runnables are done.
1479 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480
Joe Onorato36115782010-06-17 13:28:48 -04001481 synchronized (mLock) {
1482 // If we are still the last one to be scheduled, remove ourselves.
1483 if (mLoaderTask == this) {
1484 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001486 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001487 }
Joe Onorato36115782010-06-17 13:28:48 -04001488 }
1489
1490 public void stopLocked() {
1491 synchronized (LoaderTask.this) {
1492 mStopped = true;
1493 this.notify();
1494 }
1495 }
1496
1497 /**
1498 * Gets the callbacks object. If we've been stopped, or if the launcher object
1499 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1500 * object that was around when the deferred message was scheduled, and if there's
1501 * a new Callbacks object around then also return null. This will save us from
1502 * calling onto it with data that will be ignored.
1503 */
1504 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1505 synchronized (mLock) {
1506 if (mStopped) {
1507 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001508 }
Joe Onorato36115782010-06-17 13:28:48 -04001509
1510 if (mCallbacks == null) {
1511 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001512 }
Joe Onorato36115782010-06-17 13:28:48 -04001513
1514 final Callbacks callbacks = mCallbacks.get();
1515 if (callbacks != oldCallbacks) {
1516 return null;
1517 }
1518 if (callbacks == null) {
1519 Log.w(TAG, "no mCallbacks");
1520 return null;
1521 }
1522
1523 return callbacks;
1524 }
1525 }
1526
Winson Chungc763c4e2013-07-19 13:49:06 -07001527 private void verifyApplications() {
1528 final Context context = mApp.getContext();
1529
1530 // Cross reference all the applications in our apps list with items in the workspace
1531 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001532 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001533 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001534 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001535 tmpInfos = getItemInfoForComponentName(app.componentName);
1536 if (tmpInfos.isEmpty()) {
1537 // We are missing an application icon, so add this to the workspace
1538 added.add(app);
1539 // This is a rare event, so lets log it
1540 Log.e(TAG, "Missing Application on load: " + app);
1541 }
1542 }
1543 }
1544 if (!added.isEmpty()) {
1545 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001546 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001547 }
1548 }
1549
Joe Onorato36115782010-06-17 13:28:48 -04001550 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001551 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
Adam Cohenae4409d2013-11-26 10:34:59 -08001552 AtomicBoolean deleteOnInvalidPlacement) {
Winson Chung892c74d2013-08-22 16:15:50 -07001553 LauncherAppState app = LauncherAppState.getInstance();
1554 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001555 final int countX = (int) grid.numColumns;
1556 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001559 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001560 // Return early if we detect that an item is under the hotseat button
1561 if (mCallbacks == null ||
1562 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001563 deleteOnInvalidPlacement.set(true);
Dan Sandler295ae182013-12-10 16:05:47 -05001564 Log.e(TAG, "Error loading shortcut into hotseat " + item
1565 + " into position (" + item.screenId + ":" + item.cellX + ","
1566 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001567 return false;
1568 }
1569
Dan Sandler295ae182013-12-10 16:05:47 -05001570 final ItemInfo[][] hotseatItems =
1571 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1572
Adam Cohenae4409d2013-11-26 10:34:59 -08001573 if (item.screenId >= grid.numHotseatIcons) {
1574 Log.e(TAG, "Error loading shortcut " + item
1575 + " into hotseat position " + item.screenId
1576 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1577 + ")");
1578 return false;
1579 }
1580
Dan Sandler295ae182013-12-10 16:05:47 -05001581 if (hotseatItems != null) {
1582 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 Log.e(TAG, "Error loading shortcut into hotseat " + item
1584 + " into position (" + item.screenId + ":" + item.cellX + ","
1585 + item.cellY + ") occupied by "
1586 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1587 [(int) item.screenId][0]);
1588 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001589 } else {
1590 hotseatItems[(int) item.screenId][0] = item;
1591 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001593 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001594 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 items[(int) item.screenId][0] = item;
1596 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001597 return true;
1598 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001599 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1600 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001601 return true;
1602 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001603
Adam Cohendcd297f2013-06-18 13:13:40 -07001604 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001605 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001606 occupied.put(item.screenId, items);
1607 }
1608
Dan Sandler295ae182013-12-10 16:05:47 -05001609 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001610 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1611 item.cellX < 0 || item.cellY < 0 ||
1612 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1613 Log.e(TAG, "Error loading shortcut " + item
1614 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1615 + item.cellX + "," + item.cellY
1616 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1617 return false;
1618 }
1619
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001620 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001621 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1622 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001623 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001624 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001626 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001627 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001628 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001629 return false;
1630 }
1631 }
1632 }
1633 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1634 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001635 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001636 }
1637 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001638
Joe Onorato36115782010-06-17 13:28:48 -04001639 return true;
1640 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641
Winson Chungba9c37f2013-08-30 14:11:37 -07001642 /** Clears all the sBg data structures */
1643 private void clearSBgDataStructures() {
1644 synchronized (sBgLock) {
1645 sBgWorkspaceItems.clear();
1646 sBgAppWidgets.clear();
1647 sBgFolders.clear();
1648 sBgItemsIdMap.clear();
1649 sBgDbIconCache.clear();
1650 sBgWorkspaceScreens.clear();
1651 }
1652 }
1653
Winson Chungc763c4e2013-07-19 13:49:06 -07001654 /** Returns whether this is an upgradge path */
1655 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001656 // Log to disk
1657 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1658
Joe Onorato36115782010-06-17 13:28:48 -04001659 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660
Joe Onorato36115782010-06-17 13:28:48 -04001661 final Context context = mContext;
1662 final ContentResolver contentResolver = context.getContentResolver();
1663 final PackageManager manager = context.getPackageManager();
1664 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1665 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001666
Winson Chung892c74d2013-08-22 16:15:50 -07001667 LauncherAppState app = LauncherAppState.getInstance();
1668 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1669 int countX = (int) grid.numColumns;
1670 int countY = (int) grid.numRows;
1671
Michael Jurkab85f8a42012-04-25 15:48:32 -07001672 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001673 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001674
Winson Chungc763c4e2013-07-19 13:49:06 -07001675 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001676 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001677 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001678
Winson Chung9f9f00b2013-11-15 13:27:00 -08001679 // Log to disk
1680 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001681
Winson Chung2abf94d2012-07-18 18:16:38 -07001682 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001683 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001684
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001685 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001686 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001687 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001688 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001689
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001690 // +1 for the hotseat (it can be larger than the workspace)
1691 // Load workspace in reverse order to ensure that latest items are loaded first (and
1692 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001693 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001695 try {
1696 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1697 final int intentIndex = c.getColumnIndexOrThrow
1698 (LauncherSettings.Favorites.INTENT);
1699 final int titleIndex = c.getColumnIndexOrThrow
1700 (LauncherSettings.Favorites.TITLE);
1701 final int iconTypeIndex = c.getColumnIndexOrThrow(
1702 LauncherSettings.Favorites.ICON_TYPE);
1703 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1704 final int iconPackageIndex = c.getColumnIndexOrThrow(
1705 LauncherSettings.Favorites.ICON_PACKAGE);
1706 final int iconResourceIndex = c.getColumnIndexOrThrow(
1707 LauncherSettings.Favorites.ICON_RESOURCE);
1708 final int containerIndex = c.getColumnIndexOrThrow(
1709 LauncherSettings.Favorites.CONTAINER);
1710 final int itemTypeIndex = c.getColumnIndexOrThrow(
1711 LauncherSettings.Favorites.ITEM_TYPE);
1712 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1713 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001714 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1715 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001716 final int screenIndex = c.getColumnIndexOrThrow(
1717 LauncherSettings.Favorites.SCREEN);
1718 final int cellXIndex = c.getColumnIndexOrThrow
1719 (LauncherSettings.Favorites.CELLX);
1720 final int cellYIndex = c.getColumnIndexOrThrow
1721 (LauncherSettings.Favorites.CELLY);
1722 final int spanXIndex = c.getColumnIndexOrThrow
1723 (LauncherSettings.Favorites.SPANX);
1724 final int spanYIndex = c.getColumnIndexOrThrow(
1725 LauncherSettings.Favorites.SPANY);
1726 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1727 //final int displayModeIndex = c.getColumnIndexOrThrow(
1728 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001730 ShortcutInfo info;
1731 String intentDescription;
1732 LauncherAppWidgetInfo appWidgetInfo;
1733 int container;
1734 long id;
1735 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001737 while (!mStopped && c.moveToNext()) {
Adam Cohenae4409d2013-11-26 10:34:59 -08001738 AtomicBoolean deleteOnInvalidPlacement = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 try {
1740 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001742 switch (itemType) {
1743 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1744 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001745 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 intentDescription = c.getString(intentIndex);
1747 try {
1748 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001749 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001750 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001751 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001752 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001753 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1754 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001755 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001756 // If apps can be on external storage, then we just
1757 // leave them for the user to remove (maybe add
1758 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001759 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001760 }
1761 continue;
1762 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001763 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001764 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001765 continue;
1766 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001768 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1769 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1770 titleIndex, mLabelCache);
1771 } else {
1772 info = getShortcutInfo(c, context, iconTypeIndex,
1773 iconPackageIndex, iconResourceIndex, iconIndex,
1774 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001775
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 // App shortcuts that used to be automatically added to Launcher
1777 // didn't always have the correct intent flags set, so do that
1778 // here
1779 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001780 intent.getCategories() != null &&
1781 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001782 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001783 intent.addFlags(
1784 Intent.FLAG_ACTIVITY_NEW_TASK |
1785 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1786 }
Michael Jurka96879562012-03-22 05:54:33 -07001787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001789 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001790 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001791 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001792 container = c.getInt(containerIndex);
1793 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001795 info.cellX = c.getInt(cellXIndex);
1796 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001797 info.spanX = 1;
1798 info.spanY = 1;
Adam Cohenae4409d2013-11-26 10:34:59 -08001799
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001800 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001801 deleteOnInvalidPlacement.set(false);
1802 if (!checkItemPlacement(occupied, info, deleteOnInvalidPlacement)) {
1803 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001804 itemsToRemove.add(id);
1805 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001806 break;
1807 }
1808
1809 switch (container) {
1810 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1811 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1812 sBgWorkspaceItems.add(info);
1813 break;
1814 default:
1815 // Item is in a user folder
1816 FolderInfo folderInfo =
1817 findOrMakeFolder(sBgFolders, container);
1818 folderInfo.add(info);
1819 break;
1820 }
1821 sBgItemsIdMap.put(info.id, info);
1822
1823 // now that we've loaded everthing re-save it with the
1824 // icon in case it disappears somehow.
1825 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001826 } else {
1827 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001828 }
1829 break;
1830
1831 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1832 id = c.getLong(idIndex);
1833 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1834
1835 folderInfo.title = c.getString(titleIndex);
1836 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001839 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 folderInfo.cellX = c.getInt(cellXIndex);
1841 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001842 folderInfo.spanX = 1;
1843 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844
Daniel Sandler8802e962010-05-26 16:28:16 -04001845 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001846 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001847 if (!checkItemPlacement(occupied, folderInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001848 deleteOnInvalidPlacement)) {
1849 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001850 itemsToRemove.add(id);
1851 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001852 break;
1853 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001854
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001856 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1857 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1858 sBgWorkspaceItems.add(folderInfo);
1859 break;
Joe Onorato36115782010-06-17 13:28:48 -04001860 }
Joe Onorato17a89222011-02-08 17:26:11 -08001861
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001862 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1863 sBgFolders.put(folderInfo.id, folderInfo);
1864 break;
1865
1866 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1867 // Read all Launcher-specific widget details
1868 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001869 String savedProvider = c.getString(appWidgetProviderIndex);
1870
Joe Onorato36115782010-06-17 13:28:48 -04001871 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001872
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 final AppWidgetProviderInfo provider =
1874 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001875
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001876 if (!isSafeMode && (provider == null || provider.provider == null ||
1877 provider.provider.getPackageName() == null)) {
1878 String log = "Deleting widget that isn't installed anymore: id="
1879 + id + " appWidgetId=" + appWidgetId;
1880 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001881 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001882 itemsToRemove.add(id);
1883 } else {
1884 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1885 provider.provider);
1886 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001887 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001888 appWidgetInfo.cellX = c.getInt(cellXIndex);
1889 appWidgetInfo.cellY = c.getInt(cellYIndex);
1890 appWidgetInfo.spanX = c.getInt(spanXIndex);
1891 appWidgetInfo.spanY = c.getInt(spanYIndex);
1892 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1893 appWidgetInfo.minSpanX = minSpan[0];
1894 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001895
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896 container = c.getInt(containerIndex);
1897 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1898 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1899 Log.e(TAG, "Widget found where container != " +
1900 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1901 continue;
1902 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001903
Adam Cohene25af792013-06-06 23:08:25 -07001904 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001905 // check & update map of what's occupied
Adam Cohenae4409d2013-11-26 10:34:59 -08001906 deleteOnInvalidPlacement.set(false);
Winson Chunga0b7e862013-09-05 16:03:15 -07001907 if (!checkItemPlacement(occupied, appWidgetInfo,
Adam Cohenae4409d2013-11-26 10:34:59 -08001908 deleteOnInvalidPlacement)) {
1909 if (deleteOnInvalidPlacement.get()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001910 itemsToRemove.add(id);
1911 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001912 break;
1913 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001914 String providerName = provider.provider.flattenToString();
1915 if (!providerName.equals(savedProvider)) {
1916 ContentValues values = new ContentValues();
1917 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1918 providerName);
1919 String where = BaseColumns._ID + "= ?";
1920 String[] args = {Integer.toString(c.getInt(idIndex))};
1921 contentResolver.update(contentUri, values, where, args);
1922 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001923 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1924 sBgAppWidgets.add(appWidgetInfo);
1925 }
Joe Onorato36115782010-06-17 13:28:48 -04001926 break;
1927 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05001929 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001930 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 }
1932 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001933 if (c != null) {
1934 c.close();
1935 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 }
1937
Winson Chungba9c37f2013-08-30 14:11:37 -07001938 // Break early if we've stopped loading
1939 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001940 clearSBgDataStructures();
1941 return false;
1942 }
1943
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001944 if (itemsToRemove.size() > 0) {
1945 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001946 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 // Remove dead items
1948 for (long id : itemsToRemove) {
1949 if (DEBUG_LOADERS) {
1950 Log.d(TAG, "Removed id = " + id);
1951 }
1952 // Don't notify content observers
1953 try {
1954 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1955 null, null);
1956 } catch (RemoteException e) {
1957 Log.w(TAG, "Could not remove id = " + id);
1958 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001959 }
1960 }
1961
Winson Chungc763c4e2013-07-19 13:49:06 -07001962 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 long maxScreenId = 0;
1964 // If we're importing we use the old screen order.
1965 for (ItemInfo item: sBgItemsIdMap.values()) {
1966 long screenId = item.screenId;
1967 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1968 !sBgWorkspaceScreens.contains(screenId)) {
1969 sBgWorkspaceScreens.add(screenId);
1970 if (screenId > maxScreenId) {
1971 maxScreenId = screenId;
1972 }
1973 }
1974 }
1975 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001976 // Log to disk
1977 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1978 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1979 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001980
Michael Jurka414300a2013-08-27 15:42:35 +02001981 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001982 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001983
1984 // Update the max item id after we load an old db
1985 long maxItemId = 0;
1986 // If we're importing we use the old screen order.
1987 for (ItemInfo item: sBgItemsIdMap.values()) {
1988 maxItemId = Math.max(maxItemId, item.id);
1989 }
Michael Jurka414300a2013-08-27 15:42:35 +02001990 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001991 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001992 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1993 for (Integer i : orderedScreens.keySet()) {
1994 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001995 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001996 // Log to disk
1997 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1998 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001999
2000 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07002001 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 for (ItemInfo item: sBgItemsIdMap.values()) {
2003 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2005 unusedScreens.contains(screenId)) {
2006 unusedScreens.remove(screenId);
2007 }
2008 }
2009
2010 // If there are any empty screens remove them, and update.
2011 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08002012 // Log to disk
2013 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2014 TextUtils.join(", ", unusedScreens), true);
2015
Winson Chung933bae62013-08-29 11:42:30 -07002016 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2018 }
2019 }
2020
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 if (DEBUG_LOADERS) {
2022 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2023 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002025 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002026 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002027
Daniel Sandler566da102013-06-25 23:43:45 -04002028 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002029 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002030 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002031 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 line += " | ";
2033 }
Winson Chung892c74d2013-08-22 16:15:50 -07002034 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002035 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002036 }
Joe Onorato36115782010-06-17 13:28:48 -04002037 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 }
Joe Onorato36115782010-06-17 13:28:48 -04002041 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002042 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002043 }
2044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 /** Filters the set of items who are directly or indirectly (via another container) on the
2046 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002047 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002048 ArrayList<ItemInfo> allWorkspaceItems,
2049 ArrayList<ItemInfo> currentScreenItems,
2050 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002051 // Purge any null ItemInfos
2052 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2053 while (iter.hasNext()) {
2054 ItemInfo i = iter.next();
2055 if (i == null) {
2056 iter.remove();
2057 }
2058 }
2059
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002060 // Order the set of items by their containers first, this allows use to walk through the
2061 // list sequentially, build up a list of containers that are in the specified screen,
2062 // as well as all items in those containers.
2063 Set<Long> itemsOnScreen = new HashSet<Long>();
2064 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2065 @Override
2066 public int compare(ItemInfo lhs, ItemInfo rhs) {
2067 return (int) (lhs.container - rhs.container);
2068 }
2069 });
2070 for (ItemInfo info : allWorkspaceItems) {
2071 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002072 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002073 currentScreenItems.add(info);
2074 itemsOnScreen.add(info.id);
2075 } else {
2076 otherScreenItems.add(info);
2077 }
2078 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2079 currentScreenItems.add(info);
2080 itemsOnScreen.add(info.id);
2081 } else {
2082 if (itemsOnScreen.contains(info.container)) {
2083 currentScreenItems.add(info);
2084 itemsOnScreen.add(info.id);
2085 } else {
2086 otherScreenItems.add(info);
2087 }
2088 }
2089 }
2090 }
2091
2092 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002093 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 ArrayList<LauncherAppWidgetInfo> appWidgets,
2095 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2096 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002097
2098 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002099 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002101 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002102 currentScreenWidgets.add(widget);
2103 } else {
2104 otherScreenWidgets.add(widget);
2105 }
2106 }
2107 }
2108
2109 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002110 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002111 HashMap<Long, ItemInfo> itemsIdMap,
2112 HashMap<Long, FolderInfo> folders,
2113 HashMap<Long, FolderInfo> currentScreenFolders,
2114 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115
2116 for (long id : folders.keySet()) {
2117 ItemInfo info = itemsIdMap.get(id);
2118 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002119 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002120 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002121 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002122 currentScreenFolders.put(id, folder);
2123 } else {
2124 otherScreenFolders.put(id, folder);
2125 }
2126 }
2127 }
2128
2129 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2130 * right) */
2131 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002132 final LauncherAppState app = LauncherAppState.getInstance();
2133 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 // XXX: review this
2135 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002136 @Override
2137 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002138 int cellCountX = (int) grid.numColumns;
2139 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002140 int screenOffset = cellCountX * cellCountY;
2141 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002143 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002145 rhs.cellY * cellCountX + rhs.cellX);
2146 return (int) (lr - rr);
2147 }
2148 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002150
Adam Cohendcd297f2013-06-18 13:13:40 -07002151 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2152 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002153 final Runnable r = new Runnable() {
2154 @Override
2155 public void run() {
2156 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2157 if (callbacks != null) {
2158 callbacks.bindScreens(orderedScreens);
2159 }
2160 }
2161 };
2162 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2163 }
2164
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002165 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2166 final ArrayList<ItemInfo> workspaceItems,
2167 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2168 final HashMap<Long, FolderInfo> folders,
2169 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 final boolean postOnMainThread = (deferredBindRunnables != null);
2172
2173 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002174 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002175 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002176 final int start = i;
2177 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 final Runnable r = new Runnable() {
2179 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002181 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002183 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2184 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002185 }
2186 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002187 };
2188 if (postOnMainThread) {
2189 deferredBindRunnables.add(r);
2190 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002191 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002192 }
Joe Onorato36115782010-06-17 13:28:48 -04002193 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194
2195 // Bind the folders
2196 if (!folders.isEmpty()) {
2197 final Runnable r = new Runnable() {
2198 public void run() {
2199 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2200 if (callbacks != null) {
2201 callbacks.bindFolders(folders);
2202 }
2203 }
2204 };
2205 if (postOnMainThread) {
2206 deferredBindRunnables.add(r);
2207 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002208 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002209 }
2210 }
2211
2212 // Bind the widgets, one at a time
2213 N = appWidgets.size();
2214 for (int i = 0; i < N; i++) {
2215 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2216 final Runnable r = new Runnable() {
2217 public void run() {
2218 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2219 if (callbacks != null) {
2220 callbacks.bindAppWidget(widget);
2221 }
2222 }
2223 };
2224 if (postOnMainThread) {
2225 deferredBindRunnables.add(r);
2226 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002227 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002228 }
2229 }
2230 }
2231
2232 /**
2233 * Binds all loaded data to actual views on the main thread.
2234 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002235 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002236 final long t = SystemClock.uptimeMillis();
2237 Runnable r;
2238
2239 // Don't use these two variables in any of the callback runnables.
2240 // Otherwise we hold a reference to them.
2241 final Callbacks oldCallbacks = mCallbacks.get();
2242 if (oldCallbacks == null) {
2243 // This launcher has exited and nobody bothered to tell us. Just bail.
2244 Log.w(TAG, "LoaderTask running with no launcher");
2245 return;
2246 }
2247
Winson Chung9b9fb962013-11-15 15:39:34 -08002248 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002249 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2250 ArrayList<LauncherAppWidgetInfo> appWidgets =
2251 new ArrayList<LauncherAppWidgetInfo>();
2252 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2253 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002254 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002255 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002256 workspaceItems.addAll(sBgWorkspaceItems);
2257 appWidgets.addAll(sBgAppWidgets);
2258 folders.putAll(sBgFolders);
2259 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002260 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002261 }
2262
Derek Prothro7aff3992013-12-10 14:00:37 -05002263 final boolean isLoadingSynchronously =
2264 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002265 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002266 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002267 if (currScreen >= orderedScreenIds.size()) {
2268 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002269 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002270 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002271 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002272 final long currentScreenId = currentScreen < 0
2273 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002274
2275 // Load all the items that are on the current page first (and in the process, unbind
2276 // all the existing workspace items before we call startBinding() below.
2277 unbindWorkspaceItemsOnMainThread();
2278
2279 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002280 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2281 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2282 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2283 new ArrayList<LauncherAppWidgetInfo>();
2284 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2285 new ArrayList<LauncherAppWidgetInfo>();
2286 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2287 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2288
Winson Chung9b9fb962013-11-15 15:39:34 -08002289 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002291 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002293 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 otherFolders);
2295 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2296 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2297
2298 // Tell the workspace that we're about to start binding items
2299 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002300 public void run() {
2301 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2302 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002303 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002304 }
2305 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002306 };
Winson Chung81b52252012-08-27 15:34:29 -07002307 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2310
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 // Load items on the current page
2312 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2313 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002314 if (isLoadingSynchronously) {
2315 r = new Runnable() {
2316 public void run() {
2317 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002318 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002319 callbacks.onPageBoundSynchronously(currentScreen);
2320 }
2321 }
2322 };
Winson Chung81b52252012-08-27 15:34:29 -07002323 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002324 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002325
Winson Chung4a2afa32012-07-19 14:53:05 -07002326 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2327 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 mDeferredBindRunnables.clear();
2329 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002330 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331
2332 // Tell the workspace that we're done binding items
2333 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002334 public void run() {
2335 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2336 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002337 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339
Winson Chung98e030b2012-05-07 16:01:11 -07002340 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002341 if (DEBUG_LOADERS) {
2342 Log.d(TAG, "bound workspace in "
2343 + (SystemClock.uptimeMillis()-t) + "ms");
2344 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002345
2346 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002347 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002348 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002349 if (isLoadingSynchronously) {
2350 mDeferredBindRunnables.add(r);
2351 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002352 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002353 }
Joe Onorato36115782010-06-17 13:28:48 -04002354 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002355
Joe Onorato36115782010-06-17 13:28:48 -04002356 private void loadAndBindAllApps() {
2357 if (DEBUG_LOADERS) {
2358 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2359 }
2360 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002361 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002362 synchronized (LoaderTask.this) {
2363 if (mStopped) {
2364 return;
2365 }
2366 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002367 }
Joe Onorato36115782010-06-17 13:28:48 -04002368 } else {
2369 onlyBindAllApps();
2370 }
2371 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002372
Joe Onorato36115782010-06-17 13:28:48 -04002373 private void onlyBindAllApps() {
2374 final Callbacks oldCallbacks = mCallbacks.get();
2375 if (oldCallbacks == null) {
2376 // This launcher has exited and nobody bothered to tell us. Just bail.
2377 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2378 return;
2379 }
2380
2381 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002382 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002383 final ArrayList<AppInfo> list
2384 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002385 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002386 public void run() {
2387 final long t = SystemClock.uptimeMillis();
2388 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2389 if (callbacks != null) {
2390 callbacks.bindAllApplications(list);
2391 }
2392 if (DEBUG_LOADERS) {
2393 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2394 + (SystemClock.uptimeMillis()-t) + "ms");
2395 }
2396 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002397 };
2398 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002399 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002400 r.run();
2401 } else {
2402 mHandler.post(r);
2403 }
Joe Onorato36115782010-06-17 13:28:48 -04002404 }
2405
Winson Chung64359a52013-07-08 17:17:08 -07002406 private void loadAllApps() {
2407 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002408
Joe Onorato36115782010-06-17 13:28:48 -04002409 final Callbacks oldCallbacks = mCallbacks.get();
2410 if (oldCallbacks == null) {
2411 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002412 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002413 return;
2414 }
2415
Winson Chung64359a52013-07-08 17:17:08 -07002416 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002417 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2418 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2419
Winson Chung64359a52013-07-08 17:17:08 -07002420 // Clear the list of apps
2421 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002422
Winson Chung64359a52013-07-08 17:17:08 -07002423 // Query for the set of apps
2424 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2425 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2426 if (DEBUG_LOADERS) {
2427 Log.d(TAG, "queryIntentActivities took "
2428 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2429 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2430 }
2431 // Fail if we don't have any apps
2432 if (apps == null || apps.isEmpty()) {
2433 return;
2434 }
2435 // Sort the applications by name
2436 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2437 Collections.sort(apps,
2438 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2439 if (DEBUG_LOADERS) {
2440 Log.d(TAG, "sort took "
2441 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002442 }
2443
Winson Chung64359a52013-07-08 17:17:08 -07002444 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002445 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002446 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002447 // This builds the icon bitmaps.
2448 mBgAllAppsList.add(new AppInfo(packageManager, app,
2449 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002450 }
2451
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002452 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002453 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2454 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002455
2456 // Post callback on main thread
2457 mHandler.post(new Runnable() {
2458 public void run() {
2459 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002460 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002461 if (callbacks != null) {
2462 callbacks.bindAllApplications(added);
2463 if (DEBUG_LOADERS) {
2464 Log.d(TAG, "bound " + added.size() + " apps in "
2465 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2466 }
2467 } else {
2468 Log.i(TAG, "not binding apps: no Launcher activity");
2469 }
2470 }
2471 });
2472
Joe Onorato36115782010-06-17 13:28:48 -04002473 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002474 Log.d(TAG, "Icons processed in "
2475 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002476 }
2477 }
2478
2479 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002480 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2482 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2483 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2484 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2485 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2486 }
Joe Onorato36115782010-06-17 13:28:48 -04002487 }
2488 }
2489
2490 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002491 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002492 }
2493
2494 private class PackageUpdatedTask implements Runnable {
2495 int mOp;
2496 String[] mPackages;
2497
2498 public static final int OP_NONE = 0;
2499 public static final int OP_ADD = 1;
2500 public static final int OP_UPDATE = 2;
2501 public static final int OP_REMOVE = 3; // uninstlled
2502 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2503
2504
2505 public PackageUpdatedTask(int op, String[] packages) {
2506 mOp = op;
2507 mPackages = packages;
2508 }
2509
2510 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002511 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002512
2513 final String[] packages = mPackages;
2514 final int N = packages.length;
2515 switch (mOp) {
2516 case OP_ADD:
2517 for (int i=0; i<N; i++) {
2518 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002519 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002520 }
2521 break;
2522 case OP_UPDATE:
2523 for (int i=0; i<N; i++) {
2524 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002525 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002526 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002527 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002528 }
2529 break;
2530 case OP_REMOVE:
2531 case OP_UNAVAILABLE:
2532 for (int i=0; i<N; i++) {
2533 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002534 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002535 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002536 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002537 }
2538 break;
2539 }
2540
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002541 ArrayList<AppInfo> added = null;
2542 ArrayList<AppInfo> modified = null;
2543 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002544
Adam Cohen487f7dd2012-06-28 18:12:10 -07002545 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002546 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002547 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002548 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002549 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002550 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002551 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002552 }
Winson Chung5d55f332012-07-16 20:45:03 -07002553 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002554 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002555 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002556 }
2557
Joe Onorato36115782010-06-17 13:28:48 -04002558 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2559 if (callbacks == null) {
2560 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2561 return;
2562 }
2563
2564 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002565 // Ensure that we add all the workspace applications to the db
2566 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002567 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2568 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2569 } else {
2570 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2571 addAndBindAddedApps(context, addedInfos, cb, added);
2572 }
Joe Onorato36115782010-06-17 13:28:48 -04002573 }
2574 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002575 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002576
2577 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002578 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002579 ArrayList<ItemInfo> infos =
2580 getItemInfoForComponentName(a.componentName);
2581 for (ItemInfo i : infos) {
2582 if (isShortcutInfoUpdateable(i)) {
2583 ShortcutInfo info = (ShortcutInfo) i;
2584 info.title = a.title.toString();
2585 updateItemInDatabase(context, info);
2586 }
2587 }
2588 }
2589
Joe Onorato36115782010-06-17 13:28:48 -04002590 mHandler.post(new Runnable() {
2591 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002592 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2593 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002594 callbacks.bindAppsUpdated(modifiedFinal);
2595 }
2596 }
2597 });
2598 }
Winson Chung83892cc2013-05-01 16:53:33 -07002599
Winson Chungdf95eb12013-10-16 14:57:07 -07002600 final ArrayList<String> removedPackageNames =
2601 new ArrayList<String>();
2602 if (mOp == OP_REMOVE) {
2603 // Mark all packages in the broadcast to be removed
2604 removedPackageNames.addAll(Arrays.asList(packages));
2605 } else if (mOp == OP_UPDATE) {
2606 // Mark disabled packages in the broadcast to be removed
2607 final PackageManager pm = context.getPackageManager();
2608 for (int i=0; i<N; i++) {
2609 if (isPackageDisabled(pm, packages[i])) {
2610 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002611 }
2612 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002613 }
2614 // Remove all the components associated with this package
2615 for (String pn : removedPackageNames) {
2616 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2617 for (ItemInfo i : infos) {
2618 deleteItemFromDatabase(context, i);
2619 }
2620 }
2621 // Remove all the specific components
2622 for (AppInfo a : removedApps) {
2623 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2624 for (ItemInfo i : infos) {
2625 deleteItemFromDatabase(context, i);
2626 }
2627 }
2628 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2629 // Remove any queued items from the install queue
2630 String spKey = LauncherAppState.getSharedPreferencesKey();
2631 SharedPreferences sp =
2632 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2633 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2634 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002635 mHandler.post(new Runnable() {
2636 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002637 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2638 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002639 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002640 }
2641 }
2642 });
Joe Onoratobe386092009-11-17 17:32:16 -08002643 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002644
Michael Jurkac402cd92013-05-20 15:49:32 +02002645 final ArrayList<Object> widgetsAndShortcuts =
2646 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002647 mHandler.post(new Runnable() {
2648 @Override
2649 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002650 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2651 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002652 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002653 }
2654 }
2655 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002656
2657 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002658 mHandler.post(new Runnable() {
2659 public void run() {
2660 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2661 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002662 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002663 }
2664 }
2665 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002666 }
2667 }
2668
Michael Jurkac402cd92013-05-20 15:49:32 +02002669 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2670 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2671 PackageManager packageManager = context.getPackageManager();
2672 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2673 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2674 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2675 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2676 Collections.sort(widgetsAndShortcuts,
2677 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2678 return widgetsAndShortcuts;
2679 }
2680
Winson Chungdf95eb12013-10-16 14:57:07 -07002681 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2682 try {
2683 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2684 return !pi.applicationInfo.enabled;
2685 } catch (NameNotFoundException e) {
2686 // Fall through
2687 }
2688 return false;
2689 }
Winson Chung1323b482013-08-05 12:41:55 -07002690 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002691 if (cn == null) {
2692 return false;
2693 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002694 if (isPackageDisabled(pm, cn.getPackageName())) {
2695 return false;
2696 }
Winson Chungee055712013-07-30 14:46:24 -07002697
2698 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002699 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002700 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002701 return (pm.getActivityInfo(cn, 0) != null);
2702 } catch (NameNotFoundException e) {
2703 return false;
2704 }
2705 }
2706
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002708 * This is called from the code that adds shortcuts from the intent receiver. This
2709 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002710 */
Joe Onorato56d82912010-03-07 14:32:10 -05002711 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002712 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002713 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002714
Joe Onorato56d82912010-03-07 14:32:10 -05002715 /**
2716 * Make an ShortcutInfo object for a shortcut that is an application.
2717 *
2718 * If c is not null, then it will be used to fill in missing data like the title and icon.
2719 */
2720 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002721 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002722 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002723 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002724 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002725 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002726 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002727 } else {
2728 try {
2729 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2730 info.initFlagsAndFirstInstallTime(pi);
2731 } catch (NameNotFoundException e) {
2732 Log.d(TAG, "getPackInfo failed for package " +
2733 componentName.getPackageName());
2734 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002735 }
2736
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002737 // TODO: See if the PackageManager knows about this case. If it doesn't
2738 // then return null & delete this.
2739
Joe Onorato56d82912010-03-07 14:32:10 -05002740 // the resource -- This may implicitly give us back the fallback icon,
2741 // but don't worry about that. All we're doing with usingFallbackIcon is
2742 // to avoid saving lots of copies of that in the database, and most apps
2743 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002744
2745 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2746 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2747 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002748 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002749 ResolveInfo resolveInfo = null;
2750 ComponentName oldComponent = intent.getComponent();
2751 Intent newIntent = new Intent(intent.getAction(), null);
2752 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2753 newIntent.setPackage(oldComponent.getPackageName());
2754 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2755 for (ResolveInfo i : infos) {
2756 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2757 i.activityInfo.name);
2758 if (cn.equals(oldComponent)) {
2759 resolveInfo = i;
2760 }
2761 }
2762 if (resolveInfo == null) {
2763 resolveInfo = manager.resolveActivity(intent, 0);
2764 }
Joe Onorato56d82912010-03-07 14:32:10 -05002765 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002766 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002767 }
Joe Onorato56d82912010-03-07 14:32:10 -05002768 // the db
2769 if (icon == null) {
2770 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002771 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002772 }
2773 }
2774 // the fallback icon
2775 if (icon == null) {
2776 icon = getFallbackIcon();
2777 info.usingFallbackIcon = true;
2778 }
2779 info.setIcon(icon);
2780
2781 // from the resource
2782 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002783 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2784 if (labelCache != null && labelCache.containsKey(key)) {
2785 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002786 } else {
2787 info.title = resolveInfo.activityInfo.loadLabel(manager);
2788 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002789 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002790 }
2791 }
Joe Onorato56d82912010-03-07 14:32:10 -05002792 }
2793 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002795 if (c != null) {
2796 info.title = c.getString(titleIndex);
2797 }
2798 }
2799 // fall back to the class name of the activity
2800 if (info.title == null) {
2801 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002803 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2804 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002806
Winson Chung64359a52013-07-08 17:17:08 -07002807 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2808 ItemInfoFilter f) {
2809 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2810 for (ItemInfo i : infos) {
2811 if (i instanceof ShortcutInfo) {
2812 ShortcutInfo info = (ShortcutInfo) i;
2813 ComponentName cn = info.intent.getComponent();
2814 if (cn != null && f.filterItem(null, info, cn)) {
2815 filtered.add(info);
2816 }
2817 } else if (i instanceof FolderInfo) {
2818 FolderInfo info = (FolderInfo) i;
2819 for (ShortcutInfo s : info.contents) {
2820 ComponentName cn = s.intent.getComponent();
2821 if (cn != null && f.filterItem(info, s, cn)) {
2822 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002823 }
2824 }
Winson Chung64359a52013-07-08 17:17:08 -07002825 } else if (i instanceof LauncherAppWidgetInfo) {
2826 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2827 ComponentName cn = info.providerName;
2828 if (cn != null && f.filterItem(null, info, cn)) {
2829 filtered.add(info);
2830 }
Winson Chung8a435102012-08-30 17:16:53 -07002831 }
2832 }
Winson Chung64359a52013-07-08 17:17:08 -07002833 return new ArrayList<ItemInfo>(filtered);
2834 }
2835
2836 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002837 ItemInfoFilter filter = new ItemInfoFilter() {
2838 @Override
2839 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2840 return cn.getPackageName().equals(pn);
2841 }
2842 };
2843 return filterItemInfos(sBgItemsIdMap.values(), filter);
2844 }
2845
2846 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002847 ItemInfoFilter filter = new ItemInfoFilter() {
2848 @Override
2849 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2850 return cn.equals(cname);
2851 }
2852 };
2853 return filterItemInfos(sBgItemsIdMap.values(), filter);
2854 }
2855
2856 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2857 if (i instanceof ShortcutInfo) {
2858 ShortcutInfo info = (ShortcutInfo) i;
2859 // We need to check for ACTION_MAIN otherwise getComponent() might
2860 // return null for some shortcuts (for instance, for shortcuts to
2861 // web pages.)
2862 Intent intent = info.intent;
2863 ComponentName name = intent.getComponent();
2864 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2865 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2866 return true;
2867 }
2868 }
2869 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002870 }
2871
2872 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002873 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002874 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002875 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002876 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2877 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878
Joe Onorato56d82912010-03-07 14:32:10 -05002879 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002880 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002883 // TODO: If there's an explicit component and we can't install that, delete it.
2884
Joe Onorato56d82912010-03-07 14:32:10 -05002885 info.title = c.getString(titleIndex);
2886
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 int iconType = c.getInt(iconTypeIndex);
2888 switch (iconType) {
2889 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2890 String packageName = c.getString(iconPackageIndex);
2891 String resourceName = c.getString(iconResourceIndex);
2892 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002893 info.customIcon = false;
2894 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002897 if (resources != null) {
2898 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002899 icon = Utilities.createIconBitmap(
2900 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002901 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002902 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002903 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 }
Joe Onorato56d82912010-03-07 14:32:10 -05002905 // the db
2906 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002907 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002908 }
2909 // the fallback icon
2910 if (icon == null) {
2911 icon = getFallbackIcon();
2912 info.usingFallbackIcon = true;
2913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 break;
2915 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002916 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002917 if (icon == null) {
2918 icon = getFallbackIcon();
2919 info.customIcon = false;
2920 info.usingFallbackIcon = true;
2921 } else {
2922 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 break;
2925 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002926 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002927 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 info.customIcon = false;
2929 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002931 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 return info;
2933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934
Michael Jurka931dc972011-08-05 15:08:15 -07002935 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002936 @SuppressWarnings("all") // suppress dead code warning
2937 final boolean debug = false;
2938 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002939 Log.d(TAG, "getIconFromCursor app="
2940 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2941 }
2942 byte[] data = c.getBlob(iconIndex);
2943 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002944 return Utilities.createIconBitmap(
2945 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002946 } catch (Exception e) {
2947 return null;
2948 }
2949 }
2950
Winson Chung3d503fb2011-07-13 17:25:49 -07002951 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2952 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002953 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002954 if (info == null) {
2955 return null;
2956 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002957 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002958
2959 return info;
2960 }
2961
Winson Chunga9abd0e2010-10-27 17:18:37 -07002962 /**
Winson Chung55cef262010-10-28 14:14:18 -07002963 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2964 */
2965 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2966 ComponentName component) {
2967 List<AppWidgetProviderInfo> widgets =
2968 AppWidgetManager.getInstance(context).getInstalledProviders();
2969 for (AppWidgetProviderInfo info : widgets) {
2970 if (info.provider.equals(component)) {
2971 return info;
2972 }
2973 }
2974 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002975 }
2976
Winson Chung68846fd2010-10-29 11:00:27 -07002977 /**
2978 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2979 */
2980 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2981 final PackageManager packageManager = context.getPackageManager();
2982 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2983 new ArrayList<WidgetMimeTypeHandlerData>();
2984
2985 final Intent supportsIntent =
2986 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2987 supportsIntent.setType(mimeType);
2988
2989 // Create a set of widget configuration components that we can test against
2990 final List<AppWidgetProviderInfo> widgets =
2991 AppWidgetManager.getInstance(context).getInstalledProviders();
2992 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2993 new HashMap<ComponentName, AppWidgetProviderInfo>();
2994 for (AppWidgetProviderInfo info : widgets) {
2995 configurationComponentToWidget.put(info.configure, info);
2996 }
2997
2998 // Run through each of the intents that can handle this type of clip data, and cross
2999 // reference them with the components that are actual configuration components
3000 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
3001 PackageManager.MATCH_DEFAULT_ONLY);
3002 for (ResolveInfo info : activities) {
3003 final ActivityInfo activityInfo = info.activityInfo;
3004 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
3005 activityInfo.name);
3006 if (configurationComponentToWidget.containsKey(infoComponent)) {
3007 supportedConfigurationActivities.add(
3008 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
3009 configurationComponentToWidget.get(infoComponent)));
3010 }
3011 }
3012 return supportedConfigurationActivities;
3013 }
3014
Winson Chunga9abd0e2010-10-27 17:18:37 -07003015 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003016 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3017 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3018 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3019
Adam Cohend9198822011-11-22 16:42:47 -08003020 if (intent == null) {
3021 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3022 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3023 return null;
3024 }
3025
Joe Onorato0589f0f2010-02-08 13:44:00 -08003026 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003027 boolean customIcon = false;
3028 ShortcutIconResource iconResource = null;
3029
3030 if (bitmap != null && bitmap instanceof Bitmap) {
3031 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003032 customIcon = true;
3033 } else {
3034 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3035 if (extra != null && extra instanceof ShortcutIconResource) {
3036 try {
3037 iconResource = (ShortcutIconResource) extra;
3038 final PackageManager packageManager = context.getPackageManager();
3039 Resources resources = packageManager.getResourcesForApplication(
3040 iconResource.packageName);
3041 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003042 icon = Utilities.createIconBitmap(
3043 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003044 } catch (Exception e) {
3045 Log.w(TAG, "Could not load shortcut icon: " + extra);
3046 }
3047 }
3048 }
3049
Michael Jurkac9d95c52011-08-29 14:03:34 -07003050 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003051
3052 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003053 if (fallbackIcon != null) {
3054 icon = fallbackIcon;
3055 } else {
3056 icon = getFallbackIcon();
3057 info.usingFallbackIcon = true;
3058 }
Joe Onorato56d82912010-03-07 14:32:10 -05003059 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003060 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003061
Joe Onorato0589f0f2010-02-08 13:44:00 -08003062 info.title = name;
3063 info.intent = intent;
3064 info.customIcon = customIcon;
3065 info.iconResource = iconResource;
3066
3067 return info;
3068 }
3069
Winson Chungaac01e12011-08-17 10:37:13 -07003070 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3071 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003072 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003073 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003074 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003075 }
Joe Onorato56d82912010-03-07 14:32:10 -05003076 // If this icon doesn't have a custom icon, check to see
3077 // what's stored in the DB, and if it doesn't match what
3078 // we're going to show, store what we are going to show back
3079 // into the DB. We do this so when we're loading, if the
3080 // package manager can't find an icon (for example because
3081 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003082 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003083 cache.put(info, c.getBlob(iconIndex));
3084 return true;
3085 }
3086 return false;
3087 }
3088 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3089 boolean needSave = false;
3090 try {
3091 if (data != null) {
3092 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3093 Bitmap loaded = info.getIcon(mIconCache);
3094 needSave = !saved.sameAs(loaded);
3095 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003096 needSave = true;
3097 }
Winson Chungaac01e12011-08-17 10:37:13 -07003098 } catch (Exception e) {
3099 needSave = true;
3100 }
3101 if (needSave) {
3102 Log.d(TAG, "going to save icon bitmap for info=" + info);
3103 // This is slower than is ideal, but this only happens once
3104 // or when the app is updated with a new icon.
3105 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003106 }
3107 }
3108
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003110 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 * or make a new one.
3112 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003113 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 // See if a placeholder was created for us already
3115 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003116 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003118 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 folders.put(id, folderInfo);
3120 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003121 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
3123
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003124 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003125 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003126 return new Comparator<AppInfo>() {
3127 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003128 int result = collator.compare(a.title.toString().trim(),
3129 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003130 if (result == 0) {
3131 result = a.componentName.compareTo(b.componentName);
3132 }
3133 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003134 }
Winson Chung11904872012-09-17 16:58:46 -07003135 };
3136 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003137 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3138 = new Comparator<AppInfo>() {
3139 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003140 if (a.firstInstallTime < b.firstInstallTime) return 1;
3141 if (a.firstInstallTime > b.firstInstallTime) return -1;
3142 return 0;
3143 }
3144 };
Winson Chung11904872012-09-17 16:58:46 -07003145 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3146 final Collator collator = Collator.getInstance();
3147 return new Comparator<AppWidgetProviderInfo>() {
3148 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003149 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003150 }
3151 };
3152 }
Winson Chung5308f242011-08-18 12:12:41 -07003153 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3154 if (info.activityInfo != null) {
3155 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3156 } else {
3157 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3158 }
3159 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003160 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003161 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003162 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003163 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003164 ShortcutNameComparator(PackageManager pm) {
3165 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003166 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003167 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003168 }
3169 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3170 mPackageManager = pm;
3171 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003172 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003173 }
3174 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003176 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3177 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3178 if (mLabelCache.containsKey(keyA)) {
3179 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003180 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003181 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003182
Winson Chung5308f242011-08-18 12:12:41 -07003183 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003184 }
Winson Chung5308f242011-08-18 12:12:41 -07003185 if (mLabelCache.containsKey(keyB)) {
3186 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003187 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003188 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003189
Winson Chung5308f242011-08-18 12:12:41 -07003190 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003191 }
Winson Chung11904872012-09-17 16:58:46 -07003192 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003193 }
3194 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003195 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003196 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003197 private PackageManager mPackageManager;
3198 private HashMap<Object, String> mLabelCache;
3199 WidgetAndShortcutNameComparator(PackageManager pm) {
3200 mPackageManager = pm;
3201 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003202 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003203 }
3204 public final int compare(Object a, Object b) {
3205 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003206 if (mLabelCache.containsKey(a)) {
3207 labelA = mLabelCache.get(a);
3208 } else {
3209 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003210 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003211 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003212 mLabelCache.put(a, labelA);
3213 }
3214 if (mLabelCache.containsKey(b)) {
3215 labelB = mLabelCache.get(b);
3216 } else {
3217 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003218 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003219 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003220 mLabelCache.put(b, labelB);
3221 }
Winson Chung11904872012-09-17 16:58:46 -07003222 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003223 }
3224 };
Joe Onoratobe386092009-11-17 17:32:16 -08003225
3226 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003227 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003228 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3229 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3230 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3231 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003232 if (mLoaderTask != null) {
3233 mLoaderTask.dumpState();
3234 } else {
3235 Log.d(TAG, "mLoaderTask=null");
3236 }
Joe Onoratobe386092009-11-17 17:32:16 -08003237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238}