blob: 4d9dff93a4933305797b4aaa93e8cfb2be43b2d9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080047
Michael Jurkac2f801e2011-07-12 14:19:46 -070048import java.lang.ref.WeakReference;
49import java.net.URISyntaxException;
50import java.text.Collator;
51import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070052import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070053import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070058import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070061import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070062import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Maintains in-memory state of the Launcher. It is expected that there should be only one
66 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070067 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040069public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070072
Daniel Sandler8707e0f2013-08-15 15:54:18 -070073 // true = use a "More Apps" folder for non-workspace apps on upgrade
74 // false = strew non-workspace apps across the workspace on upgrade
75 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
76
Joe Onorato36115782010-06-17 13:28:48 -040077 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070078 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020085 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung81b52252012-08-27 15:34:29 -070087 // Specific runnable types that are run on the main thread deferred handler, this allows us to
88 // clear all queued binding runnables when the Launcher activity is destroyed.
89 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
90 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
91
92
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070093 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
94 static {
95 sWorkerThread.start();
96 }
97 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
98
Joe Onoratocc67f472010-06-08 10:54:30 -070099 // We start off with everything not loaded. After that, we assume that
100 // our monitoring of the package manager provides all updates and we never
101 // need to do a requery. These are only ever touched from the loader thread.
102 private boolean mWorkspaceLoaded;
103 private boolean mAllAppsLoaded;
104
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700105 // When we are loading pages synchronously, we can't just post the binding of items on the side
106 // pages as this delays the rotation process. Instead, we wait for a callback from the first
107 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
108 // a normal load, we also clear this set of Runnables.
109 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700114 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800115
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700116 // The lock that must be acquired before referencing any static bg data structures. Unlike
117 // other locks, this one can generally be held long-term because we never expect any of these
118 // static data structures to be referenced outside of the worker thread except on the first
119 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700120 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
127 // created by LauncherModel that are directly on the home screen (however, no widgets or
128 // shortcuts within folders).
129 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
132 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 new ArrayList<LauncherAppWidgetInfo>();
134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
136 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
139 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700140
141 // sBgWorkspaceScreens is the ordered set of workspace screens.
142 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // </ only access in worker thread >
145
146 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Reena Lee99a73f32011-10-24 17:27:37 -0700149 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700152 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public int getCurrentWorkspaceScreen();
154 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
156 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700157 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700158 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500159 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700160 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200162 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700163 public void bindAppsAdded(ArrayList<Long> newScreens,
164 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700165 ArrayList<ItemInfo> addAnimated,
166 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700168 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700169 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700172 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700173 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700174 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung64359a52013-07-08 17:17:08 -0700177 public interface ItemInfoFilter {
178 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
179 }
180
Bjorn Bringert1307f632013-10-03 22:31:03 +0100181 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 final Context context = app.getContext();
183
Winson Chungee055712013-07-30 14:46:24 -0700184 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100186 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 mIconCache = iconCache;
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700190 Configuration config = res.getConfiguration();
191 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 }
193
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
195 * posted on the main thread handler. */
196 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700197 runOnMainThread(r, 0);
198 }
199 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 // If we are on the worker thread, post onto the main handler
202 mHandler.post(r);
203 } else {
204 r.run();
205 }
206 }
207
208 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
209 * posted on the worker thread handler. */
210 private static void runOnWorkerThread(Runnable r) {
211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 r.run();
213 } else {
214 // If we are not on the worker thread, then post to the worker handler
215 sWorker.post(r);
216 }
217 }
218
Winson Chungc9168342013-06-26 14:54:55 -0700219 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
220 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700221 LauncherAppState app = LauncherAppState.getInstance();
222 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
223 final int xCount = (int) grid.numColumns;
224 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700225 boolean[][] occupied = new boolean[xCount][yCount];
226
227 int cellX, cellY, spanX, spanY;
228 for (int i = 0; i < items.size(); ++i) {
229 final ItemInfo item = items.get(i);
230 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
231 if (item.screenId == screen) {
232 cellX = item.cellX;
233 cellY = item.cellY;
234 spanX = item.spanX;
235 spanY = item.spanY;
236 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
237 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
238 occupied[x][y] = true;
239 }
240 }
241 }
242 }
243 }
244
245 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
246 }
247 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700248 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700249 int firstScreenIndex,
250 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700251 // Lock on the app so that we don't try and get the items while apps are being added
252 LauncherAppState app = LauncherAppState.getInstance();
253 LauncherModel model = app.getModel();
254 boolean found = false;
255 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700256 if (sWorkerThread.getThreadId() != Process.myTid()) {
257 // Flush the LauncherModel worker thread, so that if we just did another
258 // processInstallShortcut, we give it time for its shortcut to get added to the
259 // database (getItemsInLocalCoordinates reads the database)
260 model.flushWorkerThread();
261 }
Winson Chungc9168342013-06-26 14:54:55 -0700262 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700263
264 // Try adding to the workspace screens incrementally, starting at the default or center
265 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700266 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
267 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700269 int[] tmpCoordinates = new int[2];
270 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700271 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700273 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700274 }
275 }
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 return null;
278 }
279
Winson Chung94d67682013-09-25 16:29:40 -0700280 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
281 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700282 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700283 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700284 }
Winson Chung94d67682013-09-25 16:29:40 -0700285 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800286 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
287 if (workspaceApps == null || allAppsApps == null) {
288 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
289 }
Winson Chung94d67682013-09-25 16:29:40 -0700290 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700291 return;
Winson Chung997a9232013-07-24 15:33:46 -0700292 }
Winson Chung64359a52013-07-08 17:17:08 -0700293 // Process the newly added applications and add them to the database first
294 Runnable r = new Runnable() {
295 public void run() {
296 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
297 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
298
Winson Chung76828c82013-08-19 15:43:29 -0700299 // Get the list of workspace screens. We need to append to this list and
300 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
301 // called.
302 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
303 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
304 for (Integer i : orderedScreens.keySet()) {
305 long screenId = orderedScreens.get(i);
306 workspaceScreens.add(screenId);
307 }
308
Winson Chung64359a52013-07-08 17:17:08 -0700309 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700310 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700311 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700312 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700313 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700314 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700315
316 // Short-circuit this logic if the icon exists somewhere on the workspace
317 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
318 continue;
319 }
320
Winson Chung87412982013-10-03 18:34:14 -0700321 // Add this icon to the db, creating a new page if necessary. If there
322 // is only the empty page then we just add items to the first page.
323 // Otherwise, we add them to the next pages.
324 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700325 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700326 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700327 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200328 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700329
330 // If we can't find a valid position, then just add a new screen.
331 // This takes time so we need to re-queue the add until the new
332 // page is added. Create as many screens as necessary to satisfy
333 // the startSearchPageIndex.
334 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700335 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 while (numPagesToAdd > 0) {
337 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700339 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 addedWorkspaceScreensFinal.add(screenId);
341 numPagesToAdd--;
342 }
Winson Chung76828c82013-08-19 15:43:29 -0700343
Winson Chung64359a52013-07-08 17:17:08 -0700344 // Find the coordinate again
345 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700346 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700347 }
348 if (coords == null) {
349 throw new RuntimeException("Coordinates should not be null");
350 }
351
Winson Chung997a9232013-07-24 15:33:46 -0700352 ShortcutInfo shortcutInfo;
353 if (a instanceof ShortcutInfo) {
354 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200355 } else if (a instanceof AppInfo) {
356 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700357 } else {
358 throw new RuntimeException("Unexpected info type");
359 }
Winson Chung94d67682013-09-25 16:29:40 -0700360
Winson Chung64359a52013-07-08 17:17:08 -0700361 // Add the shortcut to the db
362 addItemToDatabase(context, shortcutInfo,
363 LauncherSettings.Favorites.CONTAINER_DESKTOP,
364 coords.first, coords.second[0], coords.second[1], false);
365 // Save the ShortcutInfo for binding in the workspace
366 addedShortcutsFinal.add(shortcutInfo);
367 }
368 }
369
Winson Chung76828c82013-08-19 15:43:29 -0700370 // Update the workspace screens
371 updateWorkspaceScreenOrder(context, workspaceScreens);
372
Winson Chung94d67682013-09-25 16:29:40 -0700373 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 runOnMainThread(new Runnable() {
375 public void run() {
376 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
377 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700380 if (!addedShortcutsFinal.isEmpty()) {
381 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
382 long lastScreenId = info.screenId;
383 for (ItemInfo i : addedShortcutsFinal) {
384 if (i.screenId == lastScreenId) {
385 addAnimated.add(i);
386 } else {
387 addNotAnimated.add(i);
388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
390 }
Winson Chungd64d1762013-08-20 14:37:16 -0700391 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700392 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
Winson Chung997a9232013-07-24 15:33:46 -0700395 });
396 }
Winson Chung64359a52013-07-08 17:17:08 -0700397 }
398 };
399 runOnWorkerThread(r);
400 }
401
Joe Onorato56d82912010-03-07 14:32:10 -0500402 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700403 if (mDefaultIcon == null) {
404 final Context context = LauncherAppState.getInstance().getContext();
405 mDefaultIcon = Utilities.createIconBitmap(
406 mIconCache.getFullResDefaultActivityIcon(), context);
407 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Winson Chung81b52252012-08-27 15:34:29 -0700411 public void unbindItemInfosAndClearQueuedBindRunnables() {
412 if (sWorkerThread.getThreadId() == Process.myTid()) {
413 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
414 "main thread");
415 }
416
417 // Clear any deferred bind runnables
418 mDeferredBindRunnables.clear();
419 // Remove any queued bind runnables
420 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
421 // Unbind all the workspace items
422 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700423 }
424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700426 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700427 // Ensure that we don't use the same workspace items data structure on the main thread
428 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
430 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700431 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
433 tmpAppWidgets.addAll(sBgAppWidgets);
434 }
435 Runnable r = new Runnable() {
436 @Override
437 public void run() {
438 for (ItemInfo item : tmpWorkspaceItems) {
439 item.unbind();
440 }
441 for (ItemInfo item : tmpAppWidgets) {
442 item.unbind();
443 }
444 }
445 };
446 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700447 }
448
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 /**
450 * Adds an item to the DB if it was not created previously, or move it to a new
451 * <container, screen, cellX, cellY>
452 */
453 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 if (item.container == ItemInfo.NO_ID) {
456 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700457 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 } else {
459 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700460 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700464 static void checkItemInfoLocked(
465 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
466 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
467 if (modelItem != null && item != modelItem) {
468 // check all the data is consistent
469 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
470 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
471 ShortcutInfo shortcut = (ShortcutInfo) item;
472 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
473 modelShortcut.intent.filterEquals(shortcut.intent) &&
474 modelShortcut.id == shortcut.id &&
475 modelShortcut.itemType == shortcut.itemType &&
476 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700477 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700478 modelShortcut.cellX == shortcut.cellX &&
479 modelShortcut.cellY == shortcut.cellY &&
480 modelShortcut.spanX == shortcut.spanX &&
481 modelShortcut.spanY == shortcut.spanY &&
482 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
483 (modelShortcut.dropPos != null &&
484 shortcut.dropPos != null &&
485 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
486 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
487 // For all intents and purposes, this is the same object
488 return;
489 }
490 }
491
492 // the modelItem needs to match up perfectly with item if our model is
493 // to be consistent with the database-- for now, just require
494 // modelItem == item or the equality check above
495 String msg = "item: " + ((item != null) ? item.toString() : "null") +
496 "modelItem: " +
497 ((modelItem != null) ? modelItem.toString() : "null") +
498 "Error: ItemInfo passed to checkItemInfo doesn't match original";
499 RuntimeException e = new RuntimeException(msg);
500 if (stackTrace != null) {
501 e.setStackTrace(stackTrace);
502 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800503 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700504 }
505 }
506
Michael Jurka816474f2012-06-25 14:49:02 -0700507 static void checkItemInfo(final ItemInfo item) {
508 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
509 final long itemId = item.id;
510 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700511 public void run() {
512 synchronized (sBgLock) {
513 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700514 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700515 }
516 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700517 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700518 }
519
Michael Jurkac9d95c52011-08-29 14:03:34 -0700520 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
521 final ItemInfo item, final String callingFunction) {
522 final long itemId = item.id;
523 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
524 final ContentResolver cr = context.getContentResolver();
525
Adam Cohen487f7dd2012-06-28 18:12:10 -0700526 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527 Runnable r = new Runnable() {
528 public void run() {
529 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 updateItemArrays(item, itemId, stackTrace);
531 }
532 };
533 runOnWorkerThread(r);
534 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700535
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700536 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
537 final ArrayList<ItemInfo> items, final String callingFunction) {
538 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700539
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700540 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
541 Runnable r = new Runnable() {
542 public void run() {
543 ArrayList<ContentProviderOperation> ops =
544 new ArrayList<ContentProviderOperation>();
545 int count = items.size();
546 for (int i = 0; i < count; i++) {
547 ItemInfo item = items.get(i);
548 final long itemId = item.id;
549 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
550 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700552 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
553 updateItemArrays(item, itemId, stackTrace);
554
555 }
556 try {
557 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
558 } catch (Exception e) {
559 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 }
561 }
562 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700563 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700564 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700565
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700566 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
567 // Lock on mBgLock *after* the db operation
568 synchronized (sBgLock) {
569 checkItemInfoLocked(itemId, item, stackTrace);
570
571 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
572 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
573 // Item is in a folder, make sure this folder exists
574 if (!sBgFolders.containsKey(item.container)) {
575 // An items container is being set to a that of an item which is not in
576 // the list of Folders.
577 String msg = "item: " + item + " container being set to: " +
578 item.container + ", not in the list of folders";
579 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700580 }
581 }
582
583 // Items are added/removed from the corresponding FolderInfo elsewhere, such
584 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
585 // that are on the desktop, as appropriate
586 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
587 if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
588 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
589 switch (modelItem.itemType) {
590 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
591 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
592 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
593 if (!sBgWorkspaceItems.contains(modelItem)) {
594 sBgWorkspaceItems.add(modelItem);
595 }
596 break;
597 default:
598 break;
599 }
600 } else {
601 sBgWorkspaceItems.remove(modelItem);
602 }
603 }
604 }
605
Michael Jurkac7700af2013-05-14 20:17:58 +0200606 public void flushWorkerThread() {
607 mFlushingWorkerThread = true;
608 Runnable waiter = new Runnable() {
609 public void run() {
610 synchronized (this) {
611 notifyAll();
612 mFlushingWorkerThread = false;
613 }
614 }
615 };
616
617 synchronized(waiter) {
618 runOnWorkerThread(waiter);
619 if (mLoaderTask != null) {
620 synchronized(mLoaderTask) {
621 mLoaderTask.notify();
622 }
623 }
624 boolean success = false;
625 while (!success) {
626 try {
627 waiter.wait();
628 success = true;
629 } catch (InterruptedException e) {
630 }
631 }
632 }
633 }
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700637 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700638 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700639 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400640 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 item.cellX = cellX;
642 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700643
Winson Chung3d503fb2011-07-13 17:25:49 -0700644 // We store hotseat items in canonical form which is this orientation invariant position
645 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700646 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700650 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400652
653 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
656 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700657 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
Michael Jurkac9d95c52011-08-29 14:03:34 -0700659 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700660 }
661
662 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700663 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
664 * cellX, cellY have already been updated on the ItemInfos.
665 */
666 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
667 final long container, final int screen) {
668
669 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
670 int count = items.size();
671
672 for (int i = 0; i < count; i++) {
673 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700674 item.container = container;
675
676 // We store hotseat items in canonical form which is this orientation invariant position
677 // in the hotseat
678 if (context instanceof Launcher && screen < 0 &&
679 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700681 item.cellY);
682 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700684 }
685
686 final ContentValues values = new ContentValues();
687 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
688 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
689 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700690 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700691
692 contentValues.add(values);
693 }
694 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
695 }
696
697 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700698 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800699 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700700 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700701 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700702 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800703 item.cellX = cellX;
704 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700705 item.spanX = spanX;
706 item.spanY = spanY;
707
708 // We store hotseat items in canonical form which is this orientation invariant position
709 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700710 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700711 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700712 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700713 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700715 }
Adam Cohend4844c32011-02-18 19:25:06 -0800716
Adam Cohend4844c32011-02-18 19:25:06 -0800717 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800718 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700719 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
720 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
721 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
722 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800724
Michael Jurka816474f2012-06-25 14:49:02 -0700725 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700726 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700727
728 /**
729 * Update an item to the database in a specified container.
730 */
731 static void updateItemInDatabase(Context context, final ItemInfo item) {
732 final ContentValues values = new ContentValues();
733 item.onAddToDatabase(values);
734 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
735 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800736 }
737
738 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 * Returns true if the shortcuts already exists in the database.
740 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 static boolean shortcutExists(Context context, String title, Intent intent) {
743 final ContentResolver cr = context.getContentResolver();
744 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
745 new String[] { "title", "intent" }, "title=? and intent=?",
746 new String[] { title, intent.toUri(0) }, null);
747 boolean result = false;
748 try {
749 result = c.moveToFirst();
750 } finally {
751 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700754 }
755
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700757 * Returns an ItemInfo array containing all the items in the LauncherModel.
758 * The ItemInfo.id is not set through this function.
759 */
760 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
761 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
762 final ContentResolver cr = context.getContentResolver();
763 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
764 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
765 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
766 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
767
768 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
769 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
770 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
771 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
772 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
773 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
774 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
775
776 try {
777 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700778 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700779 item.cellX = c.getInt(cellXIndex);
780 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700781 item.spanX = Math.max(1, c.getInt(spanXIndex));
782 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700783 item.container = c.getInt(containerIndex);
784 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700786
787 items.add(item);
788 }
789 } catch (Exception e) {
790 items.clear();
791 } finally {
792 c.close();
793 }
794
795 return items;
796 }
797
798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
800 */
801 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
802 final ContentResolver cr = context.getContentResolver();
803 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
804 "_id=? and (itemType=? or itemType=?)",
805 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700806 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700807
Joe Onorato9c1289c2009-08-17 11:03:03 -0400808 try {
809 if (c.moveToFirst()) {
810 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
811 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
812 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
813 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
814 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
815 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 FolderInfo folderInfo = null;
818 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700819 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
820 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400821 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700822 }
823
Joe Onorato9c1289c2009-08-17 11:03:03 -0400824 folderInfo.title = c.getString(titleIndex);
825 folderInfo.id = id;
826 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700827 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700828 folderInfo.cellX = c.getInt(cellXIndex);
829 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400830
831 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833 } finally {
834 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700835 }
836
837 return null;
838 }
839
Joe Onorato9c1289c2009-08-17 11:03:03 -0400840 /**
841 * Add an item to the database in a specified container. Sets the container, screen, cellX and
842 * cellY fields of the item. Also assigns an ID to the item.
843 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700844 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700845 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400846 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 item.cellX = cellX;
848 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 // We store hotseat items in canonical form which is this orientation invariant position
850 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857
858 final ContentValues values = new ContentValues();
859 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400860 item.onAddToDatabase(values);
861
Michael Jurka414300a2013-08-27 15:42:35 +0200862 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700863 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700865
Michael Jurkac9d95c52011-08-29 14:03:34 -0700866 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700867 public void run() {
868 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
869 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700871 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700872 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700873 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700874 sBgItemsIdMap.put(item.id, item);
875 switch (item.itemType) {
876 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
877 sBgFolders.put(item.id, (FolderInfo) item);
878 // Fall through
879 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
880 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
881 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
882 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
883 sBgWorkspaceItems.add(item);
884 } else {
885 if (!sBgFolders.containsKey(item.container)) {
886 // Adding an item to a folder that doesn't exist.
887 String msg = "adding item: " + item + " to a folder that " +
888 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700889 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700890 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700891 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700892 break;
893 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
894 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
895 break;
896 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700897 }
898 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700899 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700900 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700901 }
902
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 * Creates a new unique child id, for a given cell span across all layouts.
905 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700906 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700907 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700908 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700909 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700910 }
911
Winson Chungaafa03c2010-06-11 17:34:16 -0700912 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700913 * Removes the specified item from the database
914 * @param context
915 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700917 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700919 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700920
Michael Jurka83df1882011-08-31 20:59:26 -0700921 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700922 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700923 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700924
925 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700926 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700927 switch (item.itemType) {
928 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
929 sBgFolders.remove(item.id);
930 for (ItemInfo info: sBgItemsIdMap.values()) {
931 if (info.container == item.id) {
932 // We are deleting a folder which still contains items that
933 // think they are contained by that folder.
934 String msg = "deleting a folder (" + item + ") which still " +
935 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700936 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700937 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700938 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700939 sBgWorkspaceItems.remove(item);
940 break;
941 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
942 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
943 sBgWorkspaceItems.remove(item);
944 break;
945 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
946 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
947 break;
948 }
949 sBgItemsIdMap.remove(item.id);
950 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700951 }
952 }
Michael Jurka83df1882011-08-31 20:59:26 -0700953 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700954 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 }
956
957 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 * Update the order of the workspace screens in the database. The array list contains
959 * a list of screen ids in the order that they should appear.
960 */
Winson Chungc9168342013-06-26 14:54:55 -0700961 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800962 // Log to disk
963 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
964 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
965
Winson Chung64359a52013-07-08 17:17:08 -0700966 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 final ContentResolver cr = context.getContentResolver();
968 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
969
970 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700971 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700972 while (iter.hasNext()) {
973 long id = iter.next();
974 if (id < 0) {
975 iter.remove();
976 }
977 }
978
979 Runnable r = new Runnable() {
980 @Override
981 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700982 // Clear the table
983 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700984 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 ContentValues[] values = new ContentValues[count];
986 for (int i = 0; i < count; i++) {
987 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700988 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700989 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
990 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 values[i] = v;
992 }
993 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700994
Winson Chungba9c37f2013-08-30 14:11:37 -0700995 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700996 sBgWorkspaceScreens.clear();
997 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700998 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700999 }
1000 };
1001 runOnWorkerThread(r);
1002 }
1003
1004 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 * Remove the contents of the specified folder from the database
1006 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001007 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 final ContentResolver cr = context.getContentResolver();
1009
Michael Jurkac9d95c52011-08-29 14:03:34 -07001010 Runnable r = new Runnable() {
1011 public void run() {
1012 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001013 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001014 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001015 sBgItemsIdMap.remove(info.id);
1016 sBgFolders.remove(info.id);
1017 sBgDbIconCache.remove(info);
1018 sBgWorkspaceItems.remove(info);
1019 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020
Michael Jurkac9d95c52011-08-29 14:03:34 -07001021 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1022 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001023 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001024 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001025 for (ItemInfo childInfo : info.contents) {
1026 sBgItemsIdMap.remove(childInfo.id);
1027 sBgDbIconCache.remove(childInfo);
1028 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001029 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001030 }
1031 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001032 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 }
1034
1035 /**
1036 * Set this as the current Launcher activity object for the loader.
1037 */
1038 public void initialize(Callbacks callbacks) {
1039 synchronized (mLock) {
1040 mCallbacks = new WeakReference<Callbacks>(callbacks);
1041 }
1042 }
1043
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001044 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1046 * ACTION_PACKAGE_CHANGED.
1047 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001048 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001049 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001050 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001051
Joe Onorato36115782010-06-17 13:28:48 -04001052 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001053
Joe Onorato36115782010-06-17 13:28:48 -04001054 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1055 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1056 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1057 final String packageName = intent.getData().getSchemeSpecificPart();
1058 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059
Joe Onorato36115782010-06-17 13:28:48 -04001060 int op = PackageUpdatedTask.OP_NONE;
1061
1062 if (packageName == null || packageName.length() == 0) {
1063 // they sent us a bad intent
1064 return;
1065 }
1066
1067 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1068 op = PackageUpdatedTask.OP_UPDATE;
1069 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1070 if (!replacing) {
1071 op = PackageUpdatedTask.OP_REMOVE;
1072 }
1073 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1074 // later, we will update the package at this time
1075 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1076 if (!replacing) {
1077 op = PackageUpdatedTask.OP_ADD;
1078 } else {
1079 op = PackageUpdatedTask.OP_UPDATE;
1080 }
1081 }
1082
1083 if (op != PackageUpdatedTask.OP_NONE) {
1084 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1085 }
1086
1087 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001088 // First, schedule to add these apps back in.
1089 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1090 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1091 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001092 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001093 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1094 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1095 enqueuePackageUpdated(new PackageUpdatedTask(
1096 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001097 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001098 // If we have changed locale we need to clear out the labels in all apps/workspace.
1099 forceReload();
1100 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1101 // Check if configuration change was an mcc/mnc change which would affect app resources
1102 // and we would need to clear out the labels in all apps/workspace. Same handling as
1103 // above for ACTION_LOCALE_CHANGED
1104 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001105 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001106 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001107 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001108 forceReload();
1109 }
1110 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001111 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001112 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1113 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001114 if (mCallbacks != null) {
1115 Callbacks callbacks = mCallbacks.get();
1116 if (callbacks != null) {
1117 callbacks.bindSearchablesChanged();
1118 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001119 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001120 }
1121 }
1122
Reena Lee93f824a2011-09-23 17:20:28 -07001123 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001124 resetLoadedState(true, true);
1125
Reena Lee93f824a2011-09-23 17:20:28 -07001126 // Do this here because if the launcher activity is running it will be restarted.
1127 // If it's not running startLoaderFromBackground will merely tell it that it needs
1128 // to reload.
1129 startLoaderFromBackground();
1130 }
1131
Winson Chungf0c6ae02012-03-21 16:10:31 -07001132 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1133 synchronized (mLock) {
1134 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1135 // mWorkspaceLoaded to true later
1136 stopLoaderLocked();
1137 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1138 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1139 }
1140 }
1141
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001142 /**
1143 * When the launcher is in the background, it's possible for it to miss paired
1144 * configuration changes. So whenever we trigger the loader from the background
1145 * tell the launcher that it needs to re-run the loader when it comes back instead
1146 * of doing it now.
1147 */
1148 public void startLoaderFromBackground() {
1149 boolean runLoader = false;
1150 if (mCallbacks != null) {
1151 Callbacks callbacks = mCallbacks.get();
1152 if (callbacks != null) {
1153 // Only actually run the loader if they're not paused.
1154 if (!callbacks.setLoadOnResume()) {
1155 runLoader = true;
1156 }
1157 }
1158 }
1159 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001160 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001161 }
Joe Onorato36115782010-06-17 13:28:48 -04001162 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001163
Reena Lee93f824a2011-09-23 17:20:28 -07001164 // If there is already a loader task running, tell it to stop.
1165 // returns true if isLaunching() was true on the old task
1166 private boolean stopLoaderLocked() {
1167 boolean isLaunching = false;
1168 LoaderTask oldTask = mLoaderTask;
1169 if (oldTask != null) {
1170 if (oldTask.isLaunching()) {
1171 isLaunching = true;
1172 }
1173 oldTask.stopLocked();
1174 }
1175 return isLaunching;
1176 }
1177
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001178 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001179 synchronized (mLock) {
1180 if (DEBUG_LOADERS) {
1181 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001184 // Clear any deferred bind-runnables from the synchronized load process
1185 // We must do this before any loading/binding is scheduled below.
1186 mDeferredBindRunnables.clear();
1187
Joe Onorato36115782010-06-17 13:28:48 -04001188 // Don't bother to start the thread if we know it's not going to do anything
1189 if (mCallbacks != null && mCallbacks.get() != null) {
1190 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001191 // also, don't downgrade isLaunching if we're already running
1192 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001193 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001194 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1195 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1196 } else {
1197 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1198 sWorker.post(mLoaderTask);
1199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202 }
1203
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001204 void bindRemainingSynchronousPages() {
1205 // Post the remaining side pages to be loaded
1206 if (!mDeferredBindRunnables.isEmpty()) {
1207 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001208 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001209 }
1210 mDeferredBindRunnables.clear();
1211 }
1212 }
1213
Joe Onorato36115782010-06-17 13:28:48 -04001214 public void stopLoader() {
1215 synchronized (mLock) {
1216 if (mLoaderTask != null) {
1217 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 }
1219 }
Joe Onorato36115782010-06-17 13:28:48 -04001220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221
Winson Chung76828c82013-08-19 15:43:29 -07001222 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1223 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1224 final ContentResolver contentResolver = context.getContentResolver();
1225 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1226 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1227 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1228
1229 try {
1230 final int idIndex = sc.getColumnIndexOrThrow(
1231 LauncherSettings.WorkspaceScreens._ID);
1232 final int rankIndex = sc.getColumnIndexOrThrow(
1233 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1234 while (sc.moveToNext()) {
1235 try {
1236 long screenId = sc.getLong(idIndex);
1237 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001238 orderedScreens.put(rank, screenId);
1239 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001240 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001241 }
1242 }
1243 } finally {
1244 sc.close();
1245 }
Winson Chunga90303b2013-11-15 13:05:06 -08001246
1247 // Log to disk
1248 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1249 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1250 for (Integer i : orderedScreens.keySet()) {
1251 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1252 }
1253 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1254 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001255 return orderedScreens;
1256 }
1257
Michael Jurkac57b7a82011-08-09 22:02:20 -07001258 public boolean isAllAppsLoaded() {
1259 return mAllAppsLoaded;
1260 }
1261
Winson Chung36a62fe2012-05-06 18:04:42 -07001262 boolean isLoadingWorkspace() {
1263 synchronized (mLock) {
1264 if (mLoaderTask != null) {
1265 return mLoaderTask.isLoadingWorkspace();
1266 }
1267 }
1268 return false;
1269 }
1270
Joe Onorato36115782010-06-17 13:28:48 -04001271 /**
1272 * Runnable for the thread that loads the contents of the launcher:
1273 * - workspace icons
1274 * - widgets
1275 * - all apps icons
1276 */
1277 private class LoaderTask implements Runnable {
1278 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001279 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001280 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001281 private boolean mStopped;
1282 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001283
Winson Chungc3eecff2011-07-11 17:44:15 -07001284 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001285
1286 LoaderTask(Context context, boolean isLaunching) {
1287 mContext = context;
1288 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001289 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001290 }
1291
Joe Onorato36115782010-06-17 13:28:48 -04001292 boolean isLaunching() {
1293 return mIsLaunching;
1294 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001295
Winson Chung36a62fe2012-05-06 18:04:42 -07001296 boolean isLoadingWorkspace() {
1297 return mIsLoadingAndBindingWorkspace;
1298 }
1299
Winson Chungc763c4e2013-07-19 13:49:06 -07001300 /** Returns whether this is an upgrade path */
1301 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001302 mIsLoadingAndBindingWorkspace = true;
1303
Joe Onorato36115782010-06-17 13:28:48 -04001304 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001305 if (DEBUG_LOADERS) {
1306 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001307 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001308
Winson Chungc763c4e2013-07-19 13:49:06 -07001309 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001310 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001311 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001312 synchronized (LoaderTask.this) {
1313 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001314 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001315 }
1316 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 }
1318 }
1319
Joe Onorato36115782010-06-17 13:28:48 -04001320 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001321 bindWorkspace(-1, isUpgradePath);
1322 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001323 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001324
Joe Onorato36115782010-06-17 13:28:48 -04001325 private void waitForIdle() {
1326 // Wait until the either we're stopped or the other threads are done.
1327 // This way we don't start loading all apps until the workspace has settled
1328 // down.
1329 synchronized (LoaderTask.this) {
1330 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001331
Joe Onorato36115782010-06-17 13:28:48 -04001332 mHandler.postIdle(new Runnable() {
1333 public void run() {
1334 synchronized (LoaderTask.this) {
1335 mLoadAndBindStepFinished = true;
1336 if (DEBUG_LOADERS) {
1337 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001338 }
Joe Onorato36115782010-06-17 13:28:48 -04001339 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001340 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Joe Onorato36115782010-06-17 13:28:48 -04001342 });
1343
Michael Jurkac7700af2013-05-14 20:17:58 +02001344 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001345 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001346 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1347 // wait no longer than 1sec at a time
1348 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001349 } catch (InterruptedException ex) {
1350 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001351 }
1352 }
Joe Onorato36115782010-06-17 13:28:48 -04001353 if (DEBUG_LOADERS) {
1354 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001355 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001356 + "ms for previous step to finish binding");
1357 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001358 }
Joe Onorato36115782010-06-17 13:28:48 -04001359 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001360
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001361 void runBindSynchronousPage(int synchronousBindPage) {
1362 if (synchronousBindPage < 0) {
1363 // Ensure that we have a valid page index to load synchronously
1364 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1365 "valid page index");
1366 }
1367 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1368 // Ensure that we don't try and bind a specified page when the pages have not been
1369 // loaded already (we should load everything asynchronously in that case)
1370 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1371 }
1372 synchronized (mLock) {
1373 if (mIsLoaderTaskRunning) {
1374 // Ensure that we are never running the background loading at this point since
1375 // we also touch the background collections
1376 throw new RuntimeException("Error! Background loading is already running");
1377 }
1378 }
1379
1380 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1381 // data structures, we can't allow any other thread to touch that data, but because
1382 // this call is synchronous, we can get away with not locking).
1383
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001384 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001385 // operations from the previous activity. We need to ensure that all queued operations
1386 // are executed before any synchronous binding work is done.
1387 mHandler.flush();
1388
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389 // Divide the set of loaded items into those that we are binding synchronously, and
1390 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001391 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001392 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1393 // arise from that.
1394 onlyBindAllApps();
1395 }
1396
Joe Onorato36115782010-06-17 13:28:48 -04001397 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001398 boolean isUpgrade = false;
1399
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001400 synchronized (mLock) {
1401 mIsLoaderTaskRunning = true;
1402 }
Joe Onorato36115782010-06-17 13:28:48 -04001403 // Optimize for end-user experience: if the Launcher is up and // running with the
1404 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1405 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001406 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001407 // Elevate priority when Home launches for the first time to avoid
1408 // starving at boot time. Staring at a blank home is not cool.
1409 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001410 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1411 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001412 android.os.Process.setThreadPriority(mIsLaunching
1413 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1414 }
Winson Chung64359a52013-07-08 17:17:08 -07001415 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001416 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001417
Joe Onorato36115782010-06-17 13:28:48 -04001418 if (mStopped) {
1419 break keep_running;
1420 }
1421
1422 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1423 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001424 synchronized (mLock) {
1425 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001426 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001427 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1428 }
1429 }
Joe Onorato36115782010-06-17 13:28:48 -04001430 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001431
1432 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001433 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1434 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001435
1436 // Restore the default thread priority after we are done loading items
1437 synchronized (mLock) {
1438 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1439 }
Joe Onorato36115782010-06-17 13:28:48 -04001440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441
Winson Chungaac01e12011-08-17 10:37:13 -07001442 // Update the saved icons if necessary
1443 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001444 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001445 for (Object key : sBgDbIconCache.keySet()) {
1446 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1447 }
1448 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001449 }
Winson Chungaac01e12011-08-17 10:37:13 -07001450
Winson Chungc58497e2013-09-03 17:48:37 -07001451 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1452 // Ensure that all the applications that are in the system are
1453 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001454 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001455 verifyApplications();
1456 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001457 }
1458
Joe Onorato36115782010-06-17 13:28:48 -04001459 // Clear out this reference, otherwise we end up holding it until all of the
1460 // callback runnables are done.
1461 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462
Joe Onorato36115782010-06-17 13:28:48 -04001463 synchronized (mLock) {
1464 // If we are still the last one to be scheduled, remove ourselves.
1465 if (mLoaderTask == this) {
1466 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001468 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001469 }
Joe Onorato36115782010-06-17 13:28:48 -04001470 }
1471
1472 public void stopLocked() {
1473 synchronized (LoaderTask.this) {
1474 mStopped = true;
1475 this.notify();
1476 }
1477 }
1478
1479 /**
1480 * Gets the callbacks object. If we've been stopped, or if the launcher object
1481 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1482 * object that was around when the deferred message was scheduled, and if there's
1483 * a new Callbacks object around then also return null. This will save us from
1484 * calling onto it with data that will be ignored.
1485 */
1486 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1487 synchronized (mLock) {
1488 if (mStopped) {
1489 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001490 }
Joe Onorato36115782010-06-17 13:28:48 -04001491
1492 if (mCallbacks == null) {
1493 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001494 }
Joe Onorato36115782010-06-17 13:28:48 -04001495
1496 final Callbacks callbacks = mCallbacks.get();
1497 if (callbacks != oldCallbacks) {
1498 return null;
1499 }
1500 if (callbacks == null) {
1501 Log.w(TAG, "no mCallbacks");
1502 return null;
1503 }
1504
1505 return callbacks;
1506 }
1507 }
1508
Winson Chungc763c4e2013-07-19 13:49:06 -07001509 private void verifyApplications() {
1510 final Context context = mApp.getContext();
1511
1512 // Cross reference all the applications in our apps list with items in the workspace
1513 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001514 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001515 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001516 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001517 tmpInfos = getItemInfoForComponentName(app.componentName);
1518 if (tmpInfos.isEmpty()) {
1519 // We are missing an application icon, so add this to the workspace
1520 added.add(app);
1521 // This is a rare event, so lets log it
1522 Log.e(TAG, "Missing Application on load: " + app);
1523 }
1524 }
1525 }
1526 if (!added.isEmpty()) {
1527 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001528 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001529 }
1530 }
1531
Winson Chung5f8afe62013-08-12 16:19:28 -07001532 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001533 LauncherAppState app = LauncherAppState.getInstance();
1534 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1535 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1536 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001537 }
1538
Joe Onorato36115782010-06-17 13:28:48 -04001539 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001540 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1541 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001542 LauncherAppState app = LauncherAppState.getInstance();
1543 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1544 int countX = (int) grid.numColumns;
1545 int countY = (int) grid.numRows;
1546
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001548 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001549 // Return early if we detect that an item is under the hotseat button
1550 if (mCallbacks == null ||
1551 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1552 deleteOnItemOverlap.set(true);
1553 return false;
1554 }
1555
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1557 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1558 [(int) item.screenId][0] != null) {
1559 Log.e(TAG, "Error loading shortcut into hotseat " + item
1560 + " into position (" + item.screenId + ":" + item.cellX + ","
1561 + item.cellY + ") occupied by "
1562 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1563 [(int) item.screenId][0]);
1564 return false;
1565 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001566 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001567 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 items[(int) item.screenId][0] = item;
1569 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001570 return true;
1571 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001572 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1573 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001574 return true;
1575 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001576
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001578 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 occupied.put(item.screenId, items);
1580 }
1581
1582 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001583 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001584 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1585 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001587 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001589 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001590 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001592 return false;
1593 }
1594 }
1595 }
1596 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1597 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001598 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001599 }
1600 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001601
Joe Onorato36115782010-06-17 13:28:48 -04001602 return true;
1603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604
Winson Chungba9c37f2013-08-30 14:11:37 -07001605 /** Clears all the sBg data structures */
1606 private void clearSBgDataStructures() {
1607 synchronized (sBgLock) {
1608 sBgWorkspaceItems.clear();
1609 sBgAppWidgets.clear();
1610 sBgFolders.clear();
1611 sBgItemsIdMap.clear();
1612 sBgDbIconCache.clear();
1613 sBgWorkspaceScreens.clear();
1614 }
1615 }
1616
Winson Chungc763c4e2013-07-19 13:49:06 -07001617 /** Returns whether this is an upgradge path */
1618 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001619 // Log to disk
1620 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1621
Joe Onorato36115782010-06-17 13:28:48 -04001622 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623
Joe Onorato36115782010-06-17 13:28:48 -04001624 final Context context = mContext;
1625 final ContentResolver contentResolver = context.getContentResolver();
1626 final PackageManager manager = context.getPackageManager();
1627 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1628 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001629
Winson Chung892c74d2013-08-22 16:15:50 -07001630 LauncherAppState app = LauncherAppState.getInstance();
1631 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1632 int countX = (int) grid.numColumns;
1633 int countY = (int) grid.numRows;
1634
Michael Jurkab85f8a42012-04-25 15:48:32 -07001635 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001636 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001637
Winson Chungc763c4e2013-07-19 13:49:06 -07001638 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001639 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001640 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001641
Winson Chung9f9f00b2013-11-15 13:27:00 -08001642 // Log to disk
1643 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001644
Winson Chung2abf94d2012-07-18 18:16:38 -07001645 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001646 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001647
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001648 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001649 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001650 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001651 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001652
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001653 // +1 for the hotseat (it can be larger than the workspace)
1654 // Load workspace in reverse order to ensure that latest items are loaded first (and
1655 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001656 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001658 try {
1659 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1660 final int intentIndex = c.getColumnIndexOrThrow
1661 (LauncherSettings.Favorites.INTENT);
1662 final int titleIndex = c.getColumnIndexOrThrow
1663 (LauncherSettings.Favorites.TITLE);
1664 final int iconTypeIndex = c.getColumnIndexOrThrow(
1665 LauncherSettings.Favorites.ICON_TYPE);
1666 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1667 final int iconPackageIndex = c.getColumnIndexOrThrow(
1668 LauncherSettings.Favorites.ICON_PACKAGE);
1669 final int iconResourceIndex = c.getColumnIndexOrThrow(
1670 LauncherSettings.Favorites.ICON_RESOURCE);
1671 final int containerIndex = c.getColumnIndexOrThrow(
1672 LauncherSettings.Favorites.CONTAINER);
1673 final int itemTypeIndex = c.getColumnIndexOrThrow(
1674 LauncherSettings.Favorites.ITEM_TYPE);
1675 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1676 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001677 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1678 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001679 final int screenIndex = c.getColumnIndexOrThrow(
1680 LauncherSettings.Favorites.SCREEN);
1681 final int cellXIndex = c.getColumnIndexOrThrow
1682 (LauncherSettings.Favorites.CELLX);
1683 final int cellYIndex = c.getColumnIndexOrThrow
1684 (LauncherSettings.Favorites.CELLY);
1685 final int spanXIndex = c.getColumnIndexOrThrow
1686 (LauncherSettings.Favorites.SPANX);
1687 final int spanYIndex = c.getColumnIndexOrThrow(
1688 LauncherSettings.Favorites.SPANY);
1689 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1690 //final int displayModeIndex = c.getColumnIndexOrThrow(
1691 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001693 ShortcutInfo info;
1694 String intentDescription;
1695 LauncherAppWidgetInfo appWidgetInfo;
1696 int container;
1697 long id;
1698 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001700 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001701 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001702 try {
1703 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001705 switch (itemType) {
1706 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1707 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001708 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001709 intentDescription = c.getString(intentIndex);
1710 try {
1711 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001712 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001713 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001714 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001715 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001716 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1717 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001718 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001719 // If apps can be on external storage, then we just
1720 // leave them for the user to remove (maybe add
1721 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001722 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001723 }
1724 continue;
1725 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001726 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001727 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001728 continue;
1729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001731 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1732 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1733 titleIndex, mLabelCache);
1734 } else {
1735 info = getShortcutInfo(c, context, iconTypeIndex,
1736 iconPackageIndex, iconResourceIndex, iconIndex,
1737 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001738
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001739 // App shortcuts that used to be automatically added to Launcher
1740 // didn't always have the correct intent flags set, so do that
1741 // here
1742 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001743 intent.getCategories() != null &&
1744 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001745 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001746 intent.addFlags(
1747 Intent.FLAG_ACTIVITY_NEW_TASK |
1748 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1749 }
Michael Jurka96879562012-03-22 05:54:33 -07001750 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001752 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001753 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001754 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 container = c.getInt(containerIndex);
1756 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001757 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001758 info.cellX = c.getInt(cellXIndex);
1759 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001760 info.spanX = 1;
1761 info.spanY = 1;
1762 // Skip loading items that are out of bounds
1763 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1764 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001765 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1766 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001767 continue;
1768 }
1769 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001770 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001771 deleteOnItemOverlap.set(false);
1772 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1773 if (deleteOnItemOverlap.get()) {
1774 itemsToRemove.add(id);
1775 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001776 break;
1777 }
1778
1779 switch (container) {
1780 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1781 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1782 sBgWorkspaceItems.add(info);
1783 break;
1784 default:
1785 // Item is in a user folder
1786 FolderInfo folderInfo =
1787 findOrMakeFolder(sBgFolders, container);
1788 folderInfo.add(info);
1789 break;
1790 }
1791 sBgItemsIdMap.put(info.id, info);
1792
1793 // now that we've loaded everthing re-save it with the
1794 // icon in case it disappears somehow.
1795 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001796 } else {
1797 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001798 }
1799 break;
1800
1801 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1802 id = c.getLong(idIndex);
1803 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1804
1805 folderInfo.title = c.getString(titleIndex);
1806 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001808 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001810 folderInfo.cellX = c.getInt(cellXIndex);
1811 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001812 folderInfo.spanX = 1;
1813 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814
Winson Chung5f8afe62013-08-12 16:19:28 -07001815 // Skip loading items that are out of bounds
1816 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001817 if (checkItemDimensions(folderInfo)) {
1818 Log.d(TAG, "Skipped loading out of bounds folder");
1819 continue;
1820 }
1821 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001822 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001823 deleteOnItemOverlap.set(false);
1824 if (!checkItemPlacement(occupied, folderInfo,
1825 deleteOnItemOverlap)) {
1826 if (deleteOnItemOverlap.get()) {
1827 itemsToRemove.add(id);
1828 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001829 break;
1830 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001831
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001833 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1834 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1835 sBgWorkspaceItems.add(folderInfo);
1836 break;
Joe Onorato36115782010-06-17 13:28:48 -04001837 }
Joe Onorato17a89222011-02-08 17:26:11 -08001838
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001839 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1840 sBgFolders.put(folderInfo.id, folderInfo);
1841 break;
1842
1843 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1844 // Read all Launcher-specific widget details
1845 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001846 String savedProvider = c.getString(appWidgetProviderIndex);
1847
Joe Onorato36115782010-06-17 13:28:48 -04001848 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001849
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001850 final AppWidgetProviderInfo provider =
1851 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001852
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001853 if (!isSafeMode && (provider == null || provider.provider == null ||
1854 provider.provider.getPackageName() == null)) {
1855 String log = "Deleting widget that isn't installed anymore: id="
1856 + id + " appWidgetId=" + appWidgetId;
1857 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001858 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001859 itemsToRemove.add(id);
1860 } else {
1861 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1862 provider.provider);
1863 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001864 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001865 appWidgetInfo.cellX = c.getInt(cellXIndex);
1866 appWidgetInfo.cellY = c.getInt(cellYIndex);
1867 appWidgetInfo.spanX = c.getInt(spanXIndex);
1868 appWidgetInfo.spanY = c.getInt(spanYIndex);
1869 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1870 appWidgetInfo.minSpanX = minSpan[0];
1871 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001872
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001873 container = c.getInt(containerIndex);
1874 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1875 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1876 Log.e(TAG, "Widget found where container != " +
1877 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1878 continue;
1879 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001880
Adam Cohene25af792013-06-06 23:08:25 -07001881 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001882 // Skip loading items that are out of bounds
1883 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1884 if (checkItemDimensions(appWidgetInfo)) {
1885 Log.d(TAG, "Skipped loading out of bounds app widget");
1886 continue;
1887 }
1888 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001889 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001890 deleteOnItemOverlap.set(false);
1891 if (!checkItemPlacement(occupied, appWidgetInfo,
1892 deleteOnItemOverlap)) {
1893 if (deleteOnItemOverlap.get()) {
1894 itemsToRemove.add(id);
1895 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001896 break;
1897 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001898 String providerName = provider.provider.flattenToString();
1899 if (!providerName.equals(savedProvider)) {
1900 ContentValues values = new ContentValues();
1901 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1902 providerName);
1903 String where = BaseColumns._ID + "= ?";
1904 String[] args = {Integer.toString(c.getInt(idIndex))};
1905 contentResolver.update(contentUri, values, where, args);
1906 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001907 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1908 sBgAppWidgets.add(appWidgetInfo);
1909 }
Joe Onorato36115782010-06-17 13:28:48 -04001910 break;
1911 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001912 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001913 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001914 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001915 }
1916 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001917 if (c != null) {
1918 c.close();
1919 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001920 }
1921
Winson Chungba9c37f2013-08-30 14:11:37 -07001922 // Break early if we've stopped loading
1923 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001924 clearSBgDataStructures();
1925 return false;
1926 }
1927
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001928 if (itemsToRemove.size() > 0) {
1929 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001930 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001931 // Remove dead items
1932 for (long id : itemsToRemove) {
1933 if (DEBUG_LOADERS) {
1934 Log.d(TAG, "Removed id = " + id);
1935 }
1936 // Don't notify content observers
1937 try {
1938 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1939 null, null);
1940 } catch (RemoteException e) {
1941 Log.w(TAG, "Could not remove id = " + id);
1942 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001943 }
1944 }
1945
Winson Chungc763c4e2013-07-19 13:49:06 -07001946 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 long maxScreenId = 0;
1948 // If we're importing we use the old screen order.
1949 for (ItemInfo item: sBgItemsIdMap.values()) {
1950 long screenId = item.screenId;
1951 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1952 !sBgWorkspaceScreens.contains(screenId)) {
1953 sBgWorkspaceScreens.add(screenId);
1954 if (screenId > maxScreenId) {
1955 maxScreenId = screenId;
1956 }
1957 }
1958 }
1959 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001960 // Log to disk
1961 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1962 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1963 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001964
Michael Jurka414300a2013-08-27 15:42:35 +02001965 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001967
1968 // Update the max item id after we load an old db
1969 long maxItemId = 0;
1970 // If we're importing we use the old screen order.
1971 for (ItemInfo item: sBgItemsIdMap.values()) {
1972 maxItemId = Math.max(maxItemId, item.id);
1973 }
Michael Jurka414300a2013-08-27 15:42:35 +02001974 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001975 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001976 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1977 for (Integer i : orderedScreens.keySet()) {
1978 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001979 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001980 // Log to disk
1981 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1982 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001983
1984 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001985 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 for (ItemInfo item: sBgItemsIdMap.values()) {
1987 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1989 unusedScreens.contains(screenId)) {
1990 unusedScreens.remove(screenId);
1991 }
1992 }
1993
1994 // If there are any empty screens remove them, and update.
1995 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001996 // Log to disk
1997 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
1998 TextUtils.join(", ", unusedScreens), true);
1999
Winson Chung933bae62013-08-29 11:42:30 -07002000 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2002 }
2003 }
2004
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002005 if (DEBUG_LOADERS) {
2006 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2007 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002009 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002010 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002011
Daniel Sandler566da102013-06-25 23:43:45 -04002012 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002013 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002014 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002015 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002016 line += " | ";
2017 }
Winson Chung892c74d2013-08-22 16:15:50 -07002018 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002020 }
Joe Onorato36115782010-06-17 13:28:48 -04002021 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002022 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 }
Joe Onorato36115782010-06-17 13:28:48 -04002025 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002026 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002027 }
2028
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002029 /** Filters the set of items who are directly or indirectly (via another container) on the
2030 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002031 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002032 ArrayList<ItemInfo> allWorkspaceItems,
2033 ArrayList<ItemInfo> currentScreenItems,
2034 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002035 // Purge any null ItemInfos
2036 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2037 while (iter.hasNext()) {
2038 ItemInfo i = iter.next();
2039 if (i == null) {
2040 iter.remove();
2041 }
2042 }
2043
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002044 // Order the set of items by their containers first, this allows use to walk through the
2045 // list sequentially, build up a list of containers that are in the specified screen,
2046 // as well as all items in those containers.
2047 Set<Long> itemsOnScreen = new HashSet<Long>();
2048 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2049 @Override
2050 public int compare(ItemInfo lhs, ItemInfo rhs) {
2051 return (int) (lhs.container - rhs.container);
2052 }
2053 });
2054 for (ItemInfo info : allWorkspaceItems) {
2055 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002056 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002057 currentScreenItems.add(info);
2058 itemsOnScreen.add(info.id);
2059 } else {
2060 otherScreenItems.add(info);
2061 }
2062 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2063 currentScreenItems.add(info);
2064 itemsOnScreen.add(info.id);
2065 } else {
2066 if (itemsOnScreen.contains(info.container)) {
2067 currentScreenItems.add(info);
2068 itemsOnScreen.add(info.id);
2069 } else {
2070 otherScreenItems.add(info);
2071 }
2072 }
2073 }
2074 }
2075
2076 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002077 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 ArrayList<LauncherAppWidgetInfo> appWidgets,
2079 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2080 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002081
2082 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002083 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002085 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 currentScreenWidgets.add(widget);
2087 } else {
2088 otherScreenWidgets.add(widget);
2089 }
2090 }
2091 }
2092
2093 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002094 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002095 HashMap<Long, ItemInfo> itemsIdMap,
2096 HashMap<Long, FolderInfo> folders,
2097 HashMap<Long, FolderInfo> currentScreenFolders,
2098 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099
2100 for (long id : folders.keySet()) {
2101 ItemInfo info = itemsIdMap.get(id);
2102 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002103 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002105 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 currentScreenFolders.put(id, folder);
2107 } else {
2108 otherScreenFolders.put(id, folder);
2109 }
2110 }
2111 }
2112
2113 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2114 * right) */
2115 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002116 final LauncherAppState app = LauncherAppState.getInstance();
2117 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 // XXX: review this
2119 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002120 @Override
2121 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002122 int cellCountX = (int) grid.numColumns;
2123 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002124 int screenOffset = cellCountX * cellCountY;
2125 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002126 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002127 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002128 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002129 rhs.cellY * cellCountX + rhs.cellX);
2130 return (int) (lr - rr);
2131 }
2132 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002133 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002134
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2136 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 final Runnable r = new Runnable() {
2138 @Override
2139 public void run() {
2140 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2141 if (callbacks != null) {
2142 callbacks.bindScreens(orderedScreens);
2143 }
2144 }
2145 };
2146 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2147 }
2148
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002149 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2150 final ArrayList<ItemInfo> workspaceItems,
2151 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2152 final HashMap<Long, FolderInfo> folders,
2153 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002154
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002155 final boolean postOnMainThread = (deferredBindRunnables != null);
2156
2157 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002158 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002159 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002160 final int start = i;
2161 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002162 final Runnable r = new Runnable() {
2163 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002165 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002167 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2168 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002169 }
2170 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 };
2172 if (postOnMainThread) {
2173 deferredBindRunnables.add(r);
2174 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002175 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002176 }
Joe Onorato36115782010-06-17 13:28:48 -04002177 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178
2179 // Bind the folders
2180 if (!folders.isEmpty()) {
2181 final Runnable r = new Runnable() {
2182 public void run() {
2183 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2184 if (callbacks != null) {
2185 callbacks.bindFolders(folders);
2186 }
2187 }
2188 };
2189 if (postOnMainThread) {
2190 deferredBindRunnables.add(r);
2191 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002192 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002193 }
2194 }
2195
2196 // Bind the widgets, one at a time
2197 N = appWidgets.size();
2198 for (int i = 0; i < N; i++) {
2199 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2200 final Runnable r = new Runnable() {
2201 public void run() {
2202 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2203 if (callbacks != null) {
2204 callbacks.bindAppWidget(widget);
2205 }
2206 }
2207 };
2208 if (postOnMainThread) {
2209 deferredBindRunnables.add(r);
2210 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002211 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002212 }
2213 }
2214 }
2215
2216 /**
2217 * Binds all loaded data to actual views on the main thread.
2218 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002219 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002220 final long t = SystemClock.uptimeMillis();
2221 Runnable r;
2222
2223 // Don't use these two variables in any of the callback runnables.
2224 // Otherwise we hold a reference to them.
2225 final Callbacks oldCallbacks = mCallbacks.get();
2226 if (oldCallbacks == null) {
2227 // This launcher has exited and nobody bothered to tell us. Just bail.
2228 Log.w(TAG, "LoaderTask running with no launcher");
2229 return;
2230 }
2231
Winson Chung9b9fb962013-11-15 15:39:34 -08002232 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002233 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2234 ArrayList<LauncherAppWidgetInfo> appWidgets =
2235 new ArrayList<LauncherAppWidgetInfo>();
2236 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2237 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002238 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002239 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002240 workspaceItems.addAll(sBgWorkspaceItems);
2241 appWidgets.addAll(sBgAppWidgets);
2242 folders.putAll(sBgFolders);
2243 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 }
2246
Winson Chung9b9fb962013-11-15 15:39:34 -08002247 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
Adam Cohend8dbb462013-11-27 11:55:48 -08002248 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002249 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002250 if (currScreen >= orderedScreenIds.size()) {
2251 // There may be no workspace screens (just hotseat items and an empty page).
2252 currScreen = -1;
Winson Chung9b9fb962013-11-15 15:39:34 -08002253 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002254 final int currentScreen = currScreen;
2255 final long currentScreenId =
2256 currentScreen < 0 ? -1 : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002257
2258 // Load all the items that are on the current page first (and in the process, unbind
2259 // all the existing workspace items before we call startBinding() below.
2260 unbindWorkspaceItemsOnMainThread();
2261
2262 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002263 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2264 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2265 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2266 new ArrayList<LauncherAppWidgetInfo>();
2267 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2268 new ArrayList<LauncherAppWidgetInfo>();
2269 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2270 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2271
Winson Chung9b9fb962013-11-15 15:39:34 -08002272 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002274 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002276 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002277 otherFolders);
2278 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2279 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2280
2281 // Tell the workspace that we're about to start binding items
2282 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002283 public void run() {
2284 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2285 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002287 }
2288 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002289 };
Winson Chung81b52252012-08-27 15:34:29 -07002290 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002291
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2293
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002294 // Load items on the current page
2295 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2296 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002297 if (isLoadingSynchronously) {
2298 r = new Runnable() {
2299 public void run() {
2300 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2301 if (callbacks != null) {
2302 callbacks.onPageBoundSynchronously(currentScreen);
2303 }
2304 }
2305 };
Winson Chung81b52252012-08-27 15:34:29 -07002306 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002307 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308
Winson Chung4a2afa32012-07-19 14:53:05 -07002309 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2310 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 mDeferredBindRunnables.clear();
2312 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002313 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002314
2315 // Tell the workspace that we're done binding items
2316 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002317 public void run() {
2318 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2319 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002320 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002321 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002322
Winson Chung98e030b2012-05-07 16:01:11 -07002323 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002324 if (DEBUG_LOADERS) {
2325 Log.d(TAG, "bound workspace in "
2326 + (SystemClock.uptimeMillis()-t) + "ms");
2327 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002328
2329 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002330 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002331 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002332 if (isLoadingSynchronously) {
2333 mDeferredBindRunnables.add(r);
2334 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002335 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002336 }
Joe Onorato36115782010-06-17 13:28:48 -04002337 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002338
Joe Onorato36115782010-06-17 13:28:48 -04002339 private void loadAndBindAllApps() {
2340 if (DEBUG_LOADERS) {
2341 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2342 }
2343 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002344 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002345 synchronized (LoaderTask.this) {
2346 if (mStopped) {
2347 return;
2348 }
2349 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002350 }
Joe Onorato36115782010-06-17 13:28:48 -04002351 } else {
2352 onlyBindAllApps();
2353 }
2354 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002355
Joe Onorato36115782010-06-17 13:28:48 -04002356 private void onlyBindAllApps() {
2357 final Callbacks oldCallbacks = mCallbacks.get();
2358 if (oldCallbacks == null) {
2359 // This launcher has exited and nobody bothered to tell us. Just bail.
2360 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2361 return;
2362 }
2363
2364 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002365 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002366 final ArrayList<AppInfo> list
2367 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002368 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002369 public void run() {
2370 final long t = SystemClock.uptimeMillis();
2371 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2372 if (callbacks != null) {
2373 callbacks.bindAllApplications(list);
2374 }
2375 if (DEBUG_LOADERS) {
2376 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2377 + (SystemClock.uptimeMillis()-t) + "ms");
2378 }
2379 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002380 };
2381 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002382 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002383 r.run();
2384 } else {
2385 mHandler.post(r);
2386 }
Joe Onorato36115782010-06-17 13:28:48 -04002387 }
2388
Winson Chung64359a52013-07-08 17:17:08 -07002389 private void loadAllApps() {
2390 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002391
Joe Onorato36115782010-06-17 13:28:48 -04002392 final Callbacks oldCallbacks = mCallbacks.get();
2393 if (oldCallbacks == null) {
2394 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002395 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002396 return;
2397 }
2398
Winson Chung64359a52013-07-08 17:17:08 -07002399 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002400 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2401 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2402
Winson Chung64359a52013-07-08 17:17:08 -07002403 // Clear the list of apps
2404 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002405
Winson Chung64359a52013-07-08 17:17:08 -07002406 // Query for the set of apps
2407 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2408 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2409 if (DEBUG_LOADERS) {
2410 Log.d(TAG, "queryIntentActivities took "
2411 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2412 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2413 }
2414 // Fail if we don't have any apps
2415 if (apps == null || apps.isEmpty()) {
2416 return;
2417 }
2418 // Sort the applications by name
2419 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2420 Collections.sort(apps,
2421 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2422 if (DEBUG_LOADERS) {
2423 Log.d(TAG, "sort took "
2424 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002425 }
2426
Winson Chung64359a52013-07-08 17:17:08 -07002427 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002428 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002429 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002430 // This builds the icon bitmaps.
2431 mBgAllAppsList.add(new AppInfo(packageManager, app,
2432 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002433 }
2434
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002435 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002436 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2437 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002438
2439 // Post callback on main thread
2440 mHandler.post(new Runnable() {
2441 public void run() {
2442 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002443 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002444 if (callbacks != null) {
2445 callbacks.bindAllApplications(added);
2446 if (DEBUG_LOADERS) {
2447 Log.d(TAG, "bound " + added.size() + " apps in "
2448 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2449 }
2450 } else {
2451 Log.i(TAG, "not binding apps: no Launcher activity");
2452 }
2453 }
2454 });
2455
Joe Onorato36115782010-06-17 13:28:48 -04002456 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002457 Log.d(TAG, "Icons processed in "
2458 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002459 }
2460 }
2461
2462 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002463 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2465 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2466 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2467 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2468 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2469 }
Joe Onorato36115782010-06-17 13:28:48 -04002470 }
2471 }
2472
2473 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002474 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002475 }
2476
2477 private class PackageUpdatedTask implements Runnable {
2478 int mOp;
2479 String[] mPackages;
2480
2481 public static final int OP_NONE = 0;
2482 public static final int OP_ADD = 1;
2483 public static final int OP_UPDATE = 2;
2484 public static final int OP_REMOVE = 3; // uninstlled
2485 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2486
2487
2488 public PackageUpdatedTask(int op, String[] packages) {
2489 mOp = op;
2490 mPackages = packages;
2491 }
2492
2493 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002494 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002495
2496 final String[] packages = mPackages;
2497 final int N = packages.length;
2498 switch (mOp) {
2499 case OP_ADD:
2500 for (int i=0; i<N; i++) {
2501 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002502 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002503 }
2504 break;
2505 case OP_UPDATE:
2506 for (int i=0; i<N; i++) {
2507 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002508 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002509 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002510 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002511 }
2512 break;
2513 case OP_REMOVE:
2514 case OP_UNAVAILABLE:
2515 for (int i=0; i<N; i++) {
2516 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002517 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002518 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002519 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002520 }
2521 break;
2522 }
2523
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002524 ArrayList<AppInfo> added = null;
2525 ArrayList<AppInfo> modified = null;
2526 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002527
Adam Cohen487f7dd2012-06-28 18:12:10 -07002528 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002529 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002530 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002531 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002532 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002533 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002534 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002535 }
Winson Chung5d55f332012-07-16 20:45:03 -07002536 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002537 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002538 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002539 }
2540
Joe Onorato36115782010-06-17 13:28:48 -04002541 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2542 if (callbacks == null) {
2543 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2544 return;
2545 }
2546
2547 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002548 // Ensure that we add all the workspace applications to the db
2549 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002550 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2551 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2552 } else {
2553 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2554 addAndBindAddedApps(context, addedInfos, cb, added);
2555 }
Joe Onorato36115782010-06-17 13:28:48 -04002556 }
2557 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002558 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002559
2560 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002561 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002562 ArrayList<ItemInfo> infos =
2563 getItemInfoForComponentName(a.componentName);
2564 for (ItemInfo i : infos) {
2565 if (isShortcutInfoUpdateable(i)) {
2566 ShortcutInfo info = (ShortcutInfo) i;
2567 info.title = a.title.toString();
2568 updateItemInDatabase(context, info);
2569 }
2570 }
2571 }
2572
Joe Onorato36115782010-06-17 13:28:48 -04002573 mHandler.post(new Runnable() {
2574 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002575 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2576 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002577 callbacks.bindAppsUpdated(modifiedFinal);
2578 }
2579 }
2580 });
2581 }
Winson Chung83892cc2013-05-01 16:53:33 -07002582
Winson Chungdf95eb12013-10-16 14:57:07 -07002583 final ArrayList<String> removedPackageNames =
2584 new ArrayList<String>();
2585 if (mOp == OP_REMOVE) {
2586 // Mark all packages in the broadcast to be removed
2587 removedPackageNames.addAll(Arrays.asList(packages));
2588 } else if (mOp == OP_UPDATE) {
2589 // Mark disabled packages in the broadcast to be removed
2590 final PackageManager pm = context.getPackageManager();
2591 for (int i=0; i<N; i++) {
2592 if (isPackageDisabled(pm, packages[i])) {
2593 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002594 }
2595 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002596 }
2597 // Remove all the components associated with this package
2598 for (String pn : removedPackageNames) {
2599 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2600 for (ItemInfo i : infos) {
2601 deleteItemFromDatabase(context, i);
2602 }
2603 }
2604 // Remove all the specific components
2605 for (AppInfo a : removedApps) {
2606 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2607 for (ItemInfo i : infos) {
2608 deleteItemFromDatabase(context, i);
2609 }
2610 }
2611 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2612 // Remove any queued items from the install queue
2613 String spKey = LauncherAppState.getSharedPreferencesKey();
2614 SharedPreferences sp =
2615 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2616 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2617 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002618 mHandler.post(new Runnable() {
2619 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002620 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2621 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002622 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002623 }
2624 }
2625 });
Joe Onoratobe386092009-11-17 17:32:16 -08002626 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002627
Michael Jurkac402cd92013-05-20 15:49:32 +02002628 final ArrayList<Object> widgetsAndShortcuts =
2629 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002630 mHandler.post(new Runnable() {
2631 @Override
2632 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002633 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2634 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002635 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002636 }
2637 }
2638 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002639
2640 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002641 mHandler.post(new Runnable() {
2642 public void run() {
2643 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2644 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002645 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002646 }
2647 }
2648 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002649 }
2650 }
2651
Michael Jurkac402cd92013-05-20 15:49:32 +02002652 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2653 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2654 PackageManager packageManager = context.getPackageManager();
2655 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2656 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2657 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2658 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2659 Collections.sort(widgetsAndShortcuts,
2660 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2661 return widgetsAndShortcuts;
2662 }
2663
Winson Chungdf95eb12013-10-16 14:57:07 -07002664 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2665 try {
2666 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2667 return !pi.applicationInfo.enabled;
2668 } catch (NameNotFoundException e) {
2669 // Fall through
2670 }
2671 return false;
2672 }
Winson Chung1323b482013-08-05 12:41:55 -07002673 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002674 if (cn == null) {
2675 return false;
2676 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002677 if (isPackageDisabled(pm, cn.getPackageName())) {
2678 return false;
2679 }
Winson Chungee055712013-07-30 14:46:24 -07002680
2681 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002682 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002683 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002684 return (pm.getActivityInfo(cn, 0) != null);
2685 } catch (NameNotFoundException e) {
2686 return false;
2687 }
2688 }
2689
Joe Onorato9c1289c2009-08-17 11:03:03 -04002690 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002691 * This is called from the code that adds shortcuts from the intent receiver. This
2692 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002693 */
Joe Onorato56d82912010-03-07 14:32:10 -05002694 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002695 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002696 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002697
Joe Onorato56d82912010-03-07 14:32:10 -05002698 /**
2699 * Make an ShortcutInfo object for a shortcut that is an application.
2700 *
2701 * If c is not null, then it will be used to fill in missing data like the title and icon.
2702 */
2703 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002704 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002705 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002706 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002707 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002708 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002709 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002710 } else {
2711 try {
2712 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2713 info.initFlagsAndFirstInstallTime(pi);
2714 } catch (NameNotFoundException e) {
2715 Log.d(TAG, "getPackInfo failed for package " +
2716 componentName.getPackageName());
2717 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002718 }
2719
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002720 // TODO: See if the PackageManager knows about this case. If it doesn't
2721 // then return null & delete this.
2722
Joe Onorato56d82912010-03-07 14:32:10 -05002723 // the resource -- This may implicitly give us back the fallback icon,
2724 // but don't worry about that. All we're doing with usingFallbackIcon is
2725 // to avoid saving lots of copies of that in the database, and most apps
2726 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002727
2728 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2729 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2730 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002731 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002732 ResolveInfo resolveInfo = null;
2733 ComponentName oldComponent = intent.getComponent();
2734 Intent newIntent = new Intent(intent.getAction(), null);
2735 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2736 newIntent.setPackage(oldComponent.getPackageName());
2737 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2738 for (ResolveInfo i : infos) {
2739 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2740 i.activityInfo.name);
2741 if (cn.equals(oldComponent)) {
2742 resolveInfo = i;
2743 }
2744 }
2745 if (resolveInfo == null) {
2746 resolveInfo = manager.resolveActivity(intent, 0);
2747 }
Joe Onorato56d82912010-03-07 14:32:10 -05002748 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002749 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002750 }
Joe Onorato56d82912010-03-07 14:32:10 -05002751 // the db
2752 if (icon == null) {
2753 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002754 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002755 }
2756 }
2757 // the fallback icon
2758 if (icon == null) {
2759 icon = getFallbackIcon();
2760 info.usingFallbackIcon = true;
2761 }
2762 info.setIcon(icon);
2763
2764 // from the resource
2765 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002766 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2767 if (labelCache != null && labelCache.containsKey(key)) {
2768 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002769 } else {
2770 info.title = resolveInfo.activityInfo.loadLabel(manager);
2771 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002772 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002773 }
2774 }
Joe Onorato56d82912010-03-07 14:32:10 -05002775 }
2776 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002777 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002778 if (c != null) {
2779 info.title = c.getString(titleIndex);
2780 }
2781 }
2782 // fall back to the class name of the activity
2783 if (info.title == null) {
2784 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2787 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002789
Winson Chung64359a52013-07-08 17:17:08 -07002790 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2791 ItemInfoFilter f) {
2792 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2793 for (ItemInfo i : infos) {
2794 if (i instanceof ShortcutInfo) {
2795 ShortcutInfo info = (ShortcutInfo) i;
2796 ComponentName cn = info.intent.getComponent();
2797 if (cn != null && f.filterItem(null, info, cn)) {
2798 filtered.add(info);
2799 }
2800 } else if (i instanceof FolderInfo) {
2801 FolderInfo info = (FolderInfo) i;
2802 for (ShortcutInfo s : info.contents) {
2803 ComponentName cn = s.intent.getComponent();
2804 if (cn != null && f.filterItem(info, s, cn)) {
2805 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002806 }
2807 }
Winson Chung64359a52013-07-08 17:17:08 -07002808 } else if (i instanceof LauncherAppWidgetInfo) {
2809 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2810 ComponentName cn = info.providerName;
2811 if (cn != null && f.filterItem(null, info, cn)) {
2812 filtered.add(info);
2813 }
Winson Chung8a435102012-08-30 17:16:53 -07002814 }
2815 }
Winson Chung64359a52013-07-08 17:17:08 -07002816 return new ArrayList<ItemInfo>(filtered);
2817 }
2818
2819 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002820 ItemInfoFilter filter = new ItemInfoFilter() {
2821 @Override
2822 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2823 return cn.getPackageName().equals(pn);
2824 }
2825 };
2826 return filterItemInfos(sBgItemsIdMap.values(), filter);
2827 }
2828
2829 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002830 ItemInfoFilter filter = new ItemInfoFilter() {
2831 @Override
2832 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2833 return cn.equals(cname);
2834 }
2835 };
2836 return filterItemInfos(sBgItemsIdMap.values(), filter);
2837 }
2838
2839 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2840 if (i instanceof ShortcutInfo) {
2841 ShortcutInfo info = (ShortcutInfo) i;
2842 // We need to check for ACTION_MAIN otherwise getComponent() might
2843 // return null for some shortcuts (for instance, for shortcuts to
2844 // web pages.)
2845 Intent intent = info.intent;
2846 ComponentName name = intent.getComponent();
2847 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2848 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2849 return true;
2850 }
2851 }
2852 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002853 }
2854
2855 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002856 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002858 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002859 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2860 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861
Joe Onorato56d82912010-03-07 14:32:10 -05002862 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002863 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002866 // TODO: If there's an explicit component and we can't install that, delete it.
2867
Joe Onorato56d82912010-03-07 14:32:10 -05002868 info.title = c.getString(titleIndex);
2869
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 int iconType = c.getInt(iconTypeIndex);
2871 switch (iconType) {
2872 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2873 String packageName = c.getString(iconPackageIndex);
2874 String resourceName = c.getString(iconResourceIndex);
2875 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002876 info.customIcon = false;
2877 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002880 if (resources != null) {
2881 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002882 icon = Utilities.createIconBitmap(
2883 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002886 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 }
Joe Onorato56d82912010-03-07 14:32:10 -05002888 // the db
2889 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002890 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002891 }
2892 // the fallback icon
2893 if (icon == null) {
2894 icon = getFallbackIcon();
2895 info.usingFallbackIcon = true;
2896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 break;
2898 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002899 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002900 if (icon == null) {
2901 icon = getFallbackIcon();
2902 info.customIcon = false;
2903 info.usingFallbackIcon = true;
2904 } else {
2905 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 break;
2908 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002909 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002910 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 info.customIcon = false;
2912 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002914 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 return info;
2916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917
Michael Jurka931dc972011-08-05 15:08:15 -07002918 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002919 @SuppressWarnings("all") // suppress dead code warning
2920 final boolean debug = false;
2921 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002922 Log.d(TAG, "getIconFromCursor app="
2923 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2924 }
2925 byte[] data = c.getBlob(iconIndex);
2926 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002927 return Utilities.createIconBitmap(
2928 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002929 } catch (Exception e) {
2930 return null;
2931 }
2932 }
2933
Winson Chung3d503fb2011-07-13 17:25:49 -07002934 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2935 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002936 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002937 if (info == null) {
2938 return null;
2939 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002940 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002941
2942 return info;
2943 }
2944
Winson Chunga9abd0e2010-10-27 17:18:37 -07002945 /**
Winson Chung55cef262010-10-28 14:14:18 -07002946 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2947 */
2948 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2949 ComponentName component) {
2950 List<AppWidgetProviderInfo> widgets =
2951 AppWidgetManager.getInstance(context).getInstalledProviders();
2952 for (AppWidgetProviderInfo info : widgets) {
2953 if (info.provider.equals(component)) {
2954 return info;
2955 }
2956 }
2957 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002958 }
2959
Winson Chung68846fd2010-10-29 11:00:27 -07002960 /**
2961 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2962 */
2963 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2964 final PackageManager packageManager = context.getPackageManager();
2965 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2966 new ArrayList<WidgetMimeTypeHandlerData>();
2967
2968 final Intent supportsIntent =
2969 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2970 supportsIntent.setType(mimeType);
2971
2972 // Create a set of widget configuration components that we can test against
2973 final List<AppWidgetProviderInfo> widgets =
2974 AppWidgetManager.getInstance(context).getInstalledProviders();
2975 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2976 new HashMap<ComponentName, AppWidgetProviderInfo>();
2977 for (AppWidgetProviderInfo info : widgets) {
2978 configurationComponentToWidget.put(info.configure, info);
2979 }
2980
2981 // Run through each of the intents that can handle this type of clip data, and cross
2982 // reference them with the components that are actual configuration components
2983 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2984 PackageManager.MATCH_DEFAULT_ONLY);
2985 for (ResolveInfo info : activities) {
2986 final ActivityInfo activityInfo = info.activityInfo;
2987 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2988 activityInfo.name);
2989 if (configurationComponentToWidget.containsKey(infoComponent)) {
2990 supportedConfigurationActivities.add(
2991 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2992 configurationComponentToWidget.get(infoComponent)));
2993 }
2994 }
2995 return supportedConfigurationActivities;
2996 }
2997
Winson Chunga9abd0e2010-10-27 17:18:37 -07002998 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08002999 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3000 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3001 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3002
Adam Cohend9198822011-11-22 16:42:47 -08003003 if (intent == null) {
3004 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3005 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3006 return null;
3007 }
3008
Joe Onorato0589f0f2010-02-08 13:44:00 -08003009 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003010 boolean customIcon = false;
3011 ShortcutIconResource iconResource = null;
3012
3013 if (bitmap != null && bitmap instanceof Bitmap) {
3014 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003015 customIcon = true;
3016 } else {
3017 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3018 if (extra != null && extra instanceof ShortcutIconResource) {
3019 try {
3020 iconResource = (ShortcutIconResource) extra;
3021 final PackageManager packageManager = context.getPackageManager();
3022 Resources resources = packageManager.getResourcesForApplication(
3023 iconResource.packageName);
3024 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003025 icon = Utilities.createIconBitmap(
3026 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003027 } catch (Exception e) {
3028 Log.w(TAG, "Could not load shortcut icon: " + extra);
3029 }
3030 }
3031 }
3032
Michael Jurkac9d95c52011-08-29 14:03:34 -07003033 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003034
3035 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003036 if (fallbackIcon != null) {
3037 icon = fallbackIcon;
3038 } else {
3039 icon = getFallbackIcon();
3040 info.usingFallbackIcon = true;
3041 }
Joe Onorato56d82912010-03-07 14:32:10 -05003042 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003043 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003044
Joe Onorato0589f0f2010-02-08 13:44:00 -08003045 info.title = name;
3046 info.intent = intent;
3047 info.customIcon = customIcon;
3048 info.iconResource = iconResource;
3049
3050 return info;
3051 }
3052
Winson Chungaac01e12011-08-17 10:37:13 -07003053 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3054 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003055 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003056 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003057 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003058 }
Joe Onorato56d82912010-03-07 14:32:10 -05003059 // If this icon doesn't have a custom icon, check to see
3060 // what's stored in the DB, and if it doesn't match what
3061 // we're going to show, store what we are going to show back
3062 // into the DB. We do this so when we're loading, if the
3063 // package manager can't find an icon (for example because
3064 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003065 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003066 cache.put(info, c.getBlob(iconIndex));
3067 return true;
3068 }
3069 return false;
3070 }
3071 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3072 boolean needSave = false;
3073 try {
3074 if (data != null) {
3075 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3076 Bitmap loaded = info.getIcon(mIconCache);
3077 needSave = !saved.sameAs(loaded);
3078 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003079 needSave = true;
3080 }
Winson Chungaac01e12011-08-17 10:37:13 -07003081 } catch (Exception e) {
3082 needSave = true;
3083 }
3084 if (needSave) {
3085 Log.d(TAG, "going to save icon bitmap for info=" + info);
3086 // This is slower than is ideal, but this only happens once
3087 // or when the app is updated with a new icon.
3088 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003089 }
3090 }
3091
Joe Onorato9c1289c2009-08-17 11:03:03 -04003092 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003093 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 * or make a new one.
3095 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003096 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 // See if a placeholder was created for us already
3098 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003099 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003101 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 folders.put(id, folderInfo);
3103 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003104 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 }
3106
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003107 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003108 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003109 return new Comparator<AppInfo>() {
3110 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003111 int result = collator.compare(a.title.toString().trim(),
3112 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003113 if (result == 0) {
3114 result = a.componentName.compareTo(b.componentName);
3115 }
3116 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003117 }
Winson Chung11904872012-09-17 16:58:46 -07003118 };
3119 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003120 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3121 = new Comparator<AppInfo>() {
3122 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003123 if (a.firstInstallTime < b.firstInstallTime) return 1;
3124 if (a.firstInstallTime > b.firstInstallTime) return -1;
3125 return 0;
3126 }
3127 };
Winson Chung11904872012-09-17 16:58:46 -07003128 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3129 final Collator collator = Collator.getInstance();
3130 return new Comparator<AppWidgetProviderInfo>() {
3131 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003132 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003133 }
3134 };
3135 }
Winson Chung5308f242011-08-18 12:12:41 -07003136 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3137 if (info.activityInfo != null) {
3138 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3139 } else {
3140 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3141 }
3142 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003143 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003144 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003145 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003146 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003147 ShortcutNameComparator(PackageManager pm) {
3148 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003149 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003150 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003151 }
3152 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3153 mPackageManager = pm;
3154 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003155 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003156 }
3157 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003158 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003159 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3160 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3161 if (mLabelCache.containsKey(keyA)) {
3162 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003163 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003164 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003165
Winson Chung5308f242011-08-18 12:12:41 -07003166 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003167 }
Winson Chung5308f242011-08-18 12:12:41 -07003168 if (mLabelCache.containsKey(keyB)) {
3169 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003170 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003171 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003172
Winson Chung5308f242011-08-18 12:12:41 -07003173 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003174 }
Winson Chung11904872012-09-17 16:58:46 -07003175 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003176 }
3177 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003178 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003179 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003180 private PackageManager mPackageManager;
3181 private HashMap<Object, String> mLabelCache;
3182 WidgetAndShortcutNameComparator(PackageManager pm) {
3183 mPackageManager = pm;
3184 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003185 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003186 }
3187 public final int compare(Object a, Object b) {
3188 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003189 if (mLabelCache.containsKey(a)) {
3190 labelA = mLabelCache.get(a);
3191 } else {
3192 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003193 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003194 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003195 mLabelCache.put(a, labelA);
3196 }
3197 if (mLabelCache.containsKey(b)) {
3198 labelB = mLabelCache.get(b);
3199 } else {
3200 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003201 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003202 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003203 mLabelCache.put(b, labelB);
3204 }
Winson Chung11904872012-09-17 16:58:46 -07003205 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003206 }
3207 };
Joe Onoratobe386092009-11-17 17:32:16 -08003208
3209 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003210 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003211 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3212 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3213 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3214 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003215 if (mLoaderTask != null) {
3216 mLoaderTask.dumpState();
3217 } else {
3218 Log.d(TAG, "mLoaderTask=null");
3219 }
Joe Onoratobe386092009-11-17 17:32:16 -08003220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221}