blob: a69617ac77795e2dc5ec892f5a5e1a36003497fe [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Narayan Kamathcb1a4772011-06-28 13:46:59 +010019import android.app.SearchManager;
Romain Guy629de3e2010-01-13 12:20:59 -080020import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProviderInfo;
Winson Chungc9168342013-06-26 14:54:55 -070022import android.content.*;
Joe Onorato0589f0f2010-02-08 13:44:00 -080023import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.pm.ActivityInfo;
Adam Cohen00fcb492011-11-02 21:53:47 -070025import android.content.pm.PackageInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.pm.PackageManager;
Adam Cohen00fcb492011-11-02 21:53:47 -070027import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070029import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.res.Resources;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080035import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040036import android.os.Handler;
37import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080038import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040041import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040042import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080043import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070045import android.util.Pair;
Daniel Sandler325dc232013-06-05 22:57:57 -040046import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080047
Michael Jurkac2f801e2011-07-12 14:19:46 -070048import java.lang.ref.WeakReference;
49import java.net.URISyntaxException;
50import java.text.Collator;
51import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070052import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070053import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070054import java.util.Collections;
55import java.util.Comparator;
56import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070057import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070058import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070059import java.util.List;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070060import java.util.Set;
Adam Cohendcd297f2013-06-18 13:13:40 -070061import java.util.TreeMap;
Winson Chunga0b7e862013-09-05 16:03:15 -070062import java.util.concurrent.atomic.AtomicBoolean;
Michael Jurkac2f801e2011-07-12 14:19:46 -070063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064/**
65 * Maintains in-memory state of the Launcher. It is expected that there should be only one
66 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070067 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 */
Joe Onoratof99f8c12009-10-31 17:27:36 -040069public class LauncherModel extends BroadcastReceiver {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 static final boolean DEBUG_LOADERS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070072
Daniel Sandler8707e0f2013-08-15 15:54:18 -070073 // true = use a "More Apps" folder for non-workspace apps on upgrade
74 // false = strew non-workspace apps across the workspace on upgrade
75 public static final boolean UPGRADE_USE_MORE_APPS_FOLDER = false;
76
Joe Onorato36115782010-06-17 13:28:48 -040077 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Winson Chungee055712013-07-30 14:46:24 -070078 private final boolean mAppsCanBeOnRemoveableStorage;
Daniel Sandlerdca66122010-04-13 16:23:58 -040079
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private final Object mLock = new Object();
82 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -040083 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070084 private boolean mIsLoaderTaskRunning;
Michael Jurkac7700af2013-05-14 20:17:58 +020085 private volatile boolean mFlushingWorkerThread;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chung81b52252012-08-27 15:34:29 -070087 // Specific runnable types that are run on the main thread deferred handler, this allows us to
88 // clear all queued binding runnables when the Launcher activity is destroyed.
89 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
90 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
91
92
Brad Fitzpatrick700889f2010-10-11 09:40:44 -070093 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
94 static {
95 sWorkerThread.start();
96 }
97 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
98
Joe Onoratocc67f472010-06-08 10:54:30 -070099 // We start off with everything not loaded. After that, we assume that
100 // our monitoring of the package manager provides all updates and we never
101 // need to do a requery. These are only ever touched from the loader thread.
102 private boolean mWorkspaceLoaded;
103 private boolean mAllAppsLoaded;
104
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700105 // When we are loading pages synchronously, we can't just post the binding of items on the side
106 // pages as this delays the rotation process. Instead, we wait for a callback from the first
107 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
108 // a normal load, we also clear this set of Runnables.
109 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700113 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700114 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800115
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700116 // The lock that must be acquired before referencing any static bg data structures. Unlike
117 // other locks, this one can generally be held long-term because we never expect any of these
118 // static data structures to be referenced outside of the worker thread except on the first
119 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700120 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700121
Adam Cohen487f7dd2012-06-28 18:12:10 -0700122 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700123 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700124 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700125
Adam Cohen487f7dd2012-06-28 18:12:10 -0700126 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
127 // created by LauncherModel that are directly on the home screen (however, no widgets or
128 // shortcuts within folders).
129 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700130
Adam Cohen487f7dd2012-06-28 18:12:10 -0700131 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
132 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700133 new ArrayList<LauncherAppWidgetInfo>();
134
Adam Cohen487f7dd2012-06-28 18:12:10 -0700135 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
136 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700137
Adam Cohen487f7dd2012-06-28 18:12:10 -0700138 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
139 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700140
141 // sBgWorkspaceScreens is the ordered set of workspace screens.
142 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 // </ only access in worker thread >
145
146 private IconCache mIconCache;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800147 private Bitmap mDefaultIcon;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Reena Lee99a73f32011-10-24 17:27:37 -0700149 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700150
Joe Onorato9c1289c2009-08-17 11:03:03 -0400151 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700152 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 public int getCurrentWorkspaceScreen();
154 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700155 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
156 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700157 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700158 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500159 public void bindFolders(HashMap<Long,FolderInfo> folders);
Adam Cohene25af792013-06-06 23:08:25 -0700160 public void finishBindingItems(boolean upgradePath);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200162 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700163 public void bindAppsAdded(ArrayList<Long> newScreens,
164 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700165 ArrayList<ItemInfo> addAnimated,
166 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200167 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Winson Chung83892cc2013-05-01 16:53:33 -0700168 public void bindComponentsRemoved(ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -0700169 ArrayList<AppInfo> appInfos);
Michael Jurkac402cd92013-05-20 15:49:32 +0200170 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100171 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700172 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700173 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700174 public void dumpLogsToLocalData();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chung64359a52013-07-08 17:17:08 -0700177 public interface ItemInfoFilter {
178 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
179 }
180
Bjorn Bringert1307f632013-10-03 22:31:03 +0100181 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 final Context context = app.getContext();
183
Winson Chungee055712013-07-30 14:46:24 -0700184 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100186 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800187 mIconCache = iconCache;
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700190 Configuration config = res.getConfiguration();
191 mPreviousConfigMcc = config.mcc;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800192 }
193
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700194 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
195 * posted on the main thread handler. */
196 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700197 runOnMainThread(r, 0);
198 }
199 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700200 if (sWorkerThread.getThreadId() == Process.myTid()) {
201 // If we are on the worker thread, post onto the main handler
202 mHandler.post(r);
203 } else {
204 r.run();
205 }
206 }
207
208 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
209 * posted on the worker thread handler. */
210 private static void runOnWorkerThread(Runnable r) {
211 if (sWorkerThread.getThreadId() == Process.myTid()) {
212 r.run();
213 } else {
214 // If we are not on the worker thread, then post to the worker handler
215 sWorker.post(r);
216 }
217 }
218
Winson Chungc9168342013-06-26 14:54:55 -0700219 static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy,
220 long screen) {
Winson Chung892c74d2013-08-22 16:15:50 -0700221 LauncherAppState app = LauncherAppState.getInstance();
222 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
223 final int xCount = (int) grid.numColumns;
224 final int yCount = (int) grid.numRows;
Winson Chungc9168342013-06-26 14:54:55 -0700225 boolean[][] occupied = new boolean[xCount][yCount];
226
227 int cellX, cellY, spanX, spanY;
228 for (int i = 0; i < items.size(); ++i) {
229 final ItemInfo item = items.get(i);
230 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
231 if (item.screenId == screen) {
232 cellX = item.cellX;
233 cellY = item.cellY;
234 spanX = item.spanX;
235 spanY = item.spanY;
236 for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) {
237 for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) {
238 occupied[x][y] = true;
239 }
240 }
241 }
242 }
243 }
244
245 return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied);
246 }
247 static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
Winson Chung156ab5b2013-07-12 14:14:16 -0700248 Intent launchIntent,
Winson Chung76828c82013-08-19 15:43:29 -0700249 int firstScreenIndex,
250 ArrayList<Long> workspaceScreens) {
Winson Chungc9168342013-06-26 14:54:55 -0700251 // Lock on the app so that we don't try and get the items while apps are being added
252 LauncherAppState app = LauncherAppState.getInstance();
253 LauncherModel model = app.getModel();
254 boolean found = false;
255 synchronized (app) {
Winson Chung64359a52013-07-08 17:17:08 -0700256 if (sWorkerThread.getThreadId() != Process.myTid()) {
257 // Flush the LauncherModel worker thread, so that if we just did another
258 // processInstallShortcut, we give it time for its shortcut to get added to the
259 // database (getItemsInLocalCoordinates reads the database)
260 model.flushWorkerThread();
261 }
Winson Chungc9168342013-06-26 14:54:55 -0700262 final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context);
Winson Chungc9168342013-06-26 14:54:55 -0700263
264 // Try adding to the workspace screens incrementally, starting at the default or center
265 // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))
Winson Chung76828c82013-08-19 15:43:29 -0700266 firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());
267 int count = workspaceScreens.size();
Winson Chung156ab5b2013-07-12 14:14:16 -0700268 for (int screen = firstScreenIndex; screen < count && !found; screen++) {
Winson Chungc9168342013-06-26 14:54:55 -0700269 int[] tmpCoordinates = new int[2];
270 if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,
Winson Chung76828c82013-08-19 15:43:29 -0700271 workspaceScreens.get(screen))) {
Winson Chungc9168342013-06-26 14:54:55 -0700272 // Update the Launcher db
Winson Chung76828c82013-08-19 15:43:29 -0700273 return new Pair<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
Winson Chungc9168342013-06-26 14:54:55 -0700274 }
275 }
276 }
Winson Chungc9168342013-06-26 14:54:55 -0700277 return null;
278 }
279
Winson Chung94d67682013-09-25 16:29:40 -0700280 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
281 final ArrayList<AppInfo> allAppsApps) {
Winson Chung997a9232013-07-24 15:33:46 -0700282 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -0700283 addAndBindAddedApps(context, workspaceApps, cb, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700284 }
Winson Chung94d67682013-09-25 16:29:40 -0700285 public void addAndBindAddedApps(final Context context, final ArrayList<ItemInfo> workspaceApps,
Winson Chungfe9d96a2013-11-14 11:30:05 -0800286 final Callbacks callbacks, final ArrayList<AppInfo> allAppsApps) {
287 if (workspaceApps == null || allAppsApps == null) {
288 throw new RuntimeException("workspaceApps and allAppsApps must not be null");
289 }
Winson Chung94d67682013-09-25 16:29:40 -0700290 if (workspaceApps.isEmpty() && allAppsApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700291 return;
Winson Chung997a9232013-07-24 15:33:46 -0700292 }
Winson Chung64359a52013-07-08 17:17:08 -0700293 // Process the newly added applications and add them to the database first
294 Runnable r = new Runnable() {
295 public void run() {
296 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
297 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
298
Winson Chung76828c82013-08-19 15:43:29 -0700299 // Get the list of workspace screens. We need to append to this list and
300 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
301 // called.
302 ArrayList<Long> workspaceScreens = new ArrayList<Long>();
303 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(context);
304 for (Integer i : orderedScreens.keySet()) {
305 long screenId = orderedScreens.get(i);
306 workspaceScreens.add(screenId);
307 }
308
Winson Chung64359a52013-07-08 17:17:08 -0700309 synchronized(sBgLock) {
Winson Chung94d67682013-09-25 16:29:40 -0700310 Iterator<ItemInfo> iter = workspaceApps.iterator();
Winson Chung64359a52013-07-08 17:17:08 -0700311 while (iter.hasNext()) {
Winson Chung997a9232013-07-24 15:33:46 -0700312 ItemInfo a = iter.next();
Winson Chung64359a52013-07-08 17:17:08 -0700313 final String name = a.title.toString();
Winson Chung997a9232013-07-24 15:33:46 -0700314 final Intent launchIntent = a.getIntent();
Winson Chung64359a52013-07-08 17:17:08 -0700315
316 // Short-circuit this logic if the icon exists somewhere on the workspace
317 if (LauncherModel.shortcutExists(context, name, launchIntent)) {
318 continue;
319 }
320
Winson Chung87412982013-10-03 18:34:14 -0700321 // Add this icon to the db, creating a new page if necessary. If there
322 // is only the empty page then we just add items to the first page.
323 // Otherwise, we add them to the next pages.
324 int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;
Winson Chung64359a52013-07-08 17:17:08 -0700325 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700326 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700327 if (coords == null) {
Michael Jurka414300a2013-08-27 15:42:35 +0200328 LauncherProvider lp = LauncherAppState.getLauncherProvider();
Winson Chungc763c4e2013-07-19 13:49:06 -0700329
330 // If we can't find a valid position, then just add a new screen.
331 // This takes time so we need to re-queue the add until the new
332 // page is added. Create as many screens as necessary to satisfy
333 // the startSearchPageIndex.
334 int numPagesToAdd = Math.max(1, startSearchPageIndex + 1 -
Winson Chung76828c82013-08-19 15:43:29 -0700335 workspaceScreens.size());
Winson Chungc763c4e2013-07-19 13:49:06 -0700336 while (numPagesToAdd > 0) {
337 long screenId = lp.generateNewScreenId();
Winson Chungc763c4e2013-07-19 13:49:06 -0700338 // Save the screen id for binding in the workspace
Winson Chung76828c82013-08-19 15:43:29 -0700339 workspaceScreens.add(screenId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700340 addedWorkspaceScreensFinal.add(screenId);
341 numPagesToAdd--;
342 }
Winson Chung76828c82013-08-19 15:43:29 -0700343
Winson Chung64359a52013-07-08 17:17:08 -0700344 // Find the coordinate again
345 coords = LauncherModel.findNextAvailableIconSpace(context,
Winson Chung76828c82013-08-19 15:43:29 -0700346 name, launchIntent, startSearchPageIndex, workspaceScreens);
Winson Chung64359a52013-07-08 17:17:08 -0700347 }
348 if (coords == null) {
349 throw new RuntimeException("Coordinates should not be null");
350 }
351
Winson Chung997a9232013-07-24 15:33:46 -0700352 ShortcutInfo shortcutInfo;
353 if (a instanceof ShortcutInfo) {
354 shortcutInfo = (ShortcutInfo) a;
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200355 } else if (a instanceof AppInfo) {
356 shortcutInfo = ((AppInfo) a).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700357 } else {
358 throw new RuntimeException("Unexpected info type");
359 }
Winson Chung94d67682013-09-25 16:29:40 -0700360
Winson Chung64359a52013-07-08 17:17:08 -0700361 // Add the shortcut to the db
362 addItemToDatabase(context, shortcutInfo,
363 LauncherSettings.Favorites.CONTAINER_DESKTOP,
364 coords.first, coords.second[0], coords.second[1], false);
365 // Save the ShortcutInfo for binding in the workspace
366 addedShortcutsFinal.add(shortcutInfo);
367 }
368 }
369
Winson Chung76828c82013-08-19 15:43:29 -0700370 // Update the workspace screens
371 updateWorkspaceScreenOrder(context, workspaceScreens);
372
Winson Chung94d67682013-09-25 16:29:40 -0700373 if (!addedShortcutsFinal.isEmpty() || !allAppsApps.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700374 runOnMainThread(new Runnable() {
375 public void run() {
376 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
377 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700378 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
379 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700380 if (!addedShortcutsFinal.isEmpty()) {
381 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
382 long lastScreenId = info.screenId;
383 for (ItemInfo i : addedShortcutsFinal) {
384 if (i.screenId == lastScreenId) {
385 addAnimated.add(i);
386 } else {
387 addNotAnimated.add(i);
388 }
Winson Chung997a9232013-07-24 15:33:46 -0700389 }
390 }
Winson Chungd64d1762013-08-20 14:37:16 -0700391 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Winson Chung94d67682013-09-25 16:29:40 -0700392 addNotAnimated, addAnimated, allAppsApps);
Winson Chung997a9232013-07-24 15:33:46 -0700393 }
Winson Chung64359a52013-07-08 17:17:08 -0700394 }
Winson Chung997a9232013-07-24 15:33:46 -0700395 });
396 }
Winson Chung64359a52013-07-08 17:17:08 -0700397 }
398 };
399 runOnWorkerThread(r);
400 }
401
Joe Onorato56d82912010-03-07 14:32:10 -0500402 public Bitmap getFallbackIcon() {
Winson Chung5801ef02013-10-16 13:46:28 -0700403 if (mDefaultIcon == null) {
404 final Context context = LauncherAppState.getInstance().getContext();
405 mDefaultIcon = Utilities.createIconBitmap(
406 mIconCache.getFullResDefaultActivityIcon(), context);
407 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800408 return Bitmap.createBitmap(mDefaultIcon);
Joe Onoratof99f8c12009-10-31 17:27:36 -0400409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Winson Chung81b52252012-08-27 15:34:29 -0700411 public void unbindItemInfosAndClearQueuedBindRunnables() {
412 if (sWorkerThread.getThreadId() == Process.myTid()) {
413 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
414 "main thread");
415 }
416
417 // Clear any deferred bind runnables
418 mDeferredBindRunnables.clear();
419 // Remove any queued bind runnables
420 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
421 // Unbind all the workspace items
422 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700423 }
424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700426 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700427 // Ensure that we don't use the same workspace items data structure on the main thread
428 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
430 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700431 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
433 tmpAppWidgets.addAll(sBgAppWidgets);
434 }
435 Runnable r = new Runnable() {
436 @Override
437 public void run() {
438 for (ItemInfo item : tmpWorkspaceItems) {
439 item.unbind();
440 }
441 for (ItemInfo item : tmpAppWidgets) {
442 item.unbind();
443 }
444 }
445 };
446 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700447 }
448
Joe Onorato9c1289c2009-08-17 11:03:03 -0400449 /**
450 * Adds an item to the DB if it was not created previously, or move it to a new
451 * <container, screen, cellX, cellY>
452 */
453 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700454 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400455 if (item.container == ItemInfo.NO_ID) {
456 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700457 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400458 } else {
459 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700460 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700464 static void checkItemInfoLocked(
465 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
466 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
467 if (modelItem != null && item != modelItem) {
468 // check all the data is consistent
469 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
470 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
471 ShortcutInfo shortcut = (ShortcutInfo) item;
472 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
473 modelShortcut.intent.filterEquals(shortcut.intent) &&
474 modelShortcut.id == shortcut.id &&
475 modelShortcut.itemType == shortcut.itemType &&
476 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700477 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700478 modelShortcut.cellX == shortcut.cellX &&
479 modelShortcut.cellY == shortcut.cellY &&
480 modelShortcut.spanX == shortcut.spanX &&
481 modelShortcut.spanY == shortcut.spanY &&
482 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
483 (modelShortcut.dropPos != null &&
484 shortcut.dropPos != null &&
485 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
486 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
487 // For all intents and purposes, this is the same object
488 return;
489 }
490 }
491
492 // the modelItem needs to match up perfectly with item if our model is
493 // to be consistent with the database-- for now, just require
494 // modelItem == item or the equality check above
495 String msg = "item: " + ((item != null) ? item.toString() : "null") +
496 "modelItem: " +
497 ((modelItem != null) ? modelItem.toString() : "null") +
498 "Error: ItemInfo passed to checkItemInfo doesn't match original";
499 RuntimeException e = new RuntimeException(msg);
500 if (stackTrace != null) {
501 e.setStackTrace(stackTrace);
502 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800503 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700504 }
505 }
506
Michael Jurka816474f2012-06-25 14:49:02 -0700507 static void checkItemInfo(final ItemInfo item) {
508 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
509 final long itemId = item.id;
510 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700511 public void run() {
512 synchronized (sBgLock) {
513 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700514 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700515 }
516 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700517 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700518 }
519
Michael Jurkac9d95c52011-08-29 14:03:34 -0700520 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
521 final ItemInfo item, final String callingFunction) {
522 final long itemId = item.id;
523 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
524 final ContentResolver cr = context.getContentResolver();
525
Adam Cohen487f7dd2012-06-28 18:12:10 -0700526 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700527 Runnable r = new Runnable() {
528 public void run() {
529 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700530 updateItemArrays(item, itemId, stackTrace);
531 }
532 };
533 runOnWorkerThread(r);
534 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700535
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700536 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
537 final ArrayList<ItemInfo> items, final String callingFunction) {
538 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700539
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700540 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
541 Runnable r = new Runnable() {
542 public void run() {
543 ArrayList<ContentProviderOperation> ops =
544 new ArrayList<ContentProviderOperation>();
545 int count = items.size();
546 for (int i = 0; i < count; i++) {
547 ItemInfo item = items.get(i);
548 final long itemId = item.id;
549 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
550 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700551
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700552 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
553 updateItemArrays(item, itemId, stackTrace);
554
555 }
556 try {
557 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
558 } catch (Exception e) {
559 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700560 }
561 }
562 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700563 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700564 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700565
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700566 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
567 // Lock on mBgLock *after* the db operation
568 synchronized (sBgLock) {
569 checkItemInfoLocked(itemId, item, stackTrace);
570
571 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
572 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
573 // Item is in a folder, make sure this folder exists
574 if (!sBgFolders.containsKey(item.container)) {
575 // An items container is being set to a that of an item which is not in
576 // the list of Folders.
577 String msg = "item: " + item + " container being set to: " +
578 item.container + ", not in the list of folders";
579 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700580 }
581 }
582
583 // Items are added/removed from the corresponding FolderInfo elsewhere, such
584 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
585 // that are on the desktop, as appropriate
586 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800587 if (modelItem != null &&
588 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
589 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700590 switch (modelItem.itemType) {
591 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
592 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
593 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
594 if (!sBgWorkspaceItems.contains(modelItem)) {
595 sBgWorkspaceItems.add(modelItem);
596 }
597 break;
598 default:
599 break;
600 }
601 } else {
602 sBgWorkspaceItems.remove(modelItem);
603 }
604 }
605 }
606
Michael Jurkac7700af2013-05-14 20:17:58 +0200607 public void flushWorkerThread() {
608 mFlushingWorkerThread = true;
609 Runnable waiter = new Runnable() {
610 public void run() {
611 synchronized (this) {
612 notifyAll();
613 mFlushingWorkerThread = false;
614 }
615 }
616 };
617
618 synchronized(waiter) {
619 runOnWorkerThread(waiter);
620 if (mLoaderTask != null) {
621 synchronized(mLoaderTask) {
622 mLoaderTask.notify();
623 }
624 }
625 boolean success = false;
626 while (!success) {
627 try {
628 waiter.wait();
629 success = true;
630 } catch (InterruptedException e) {
631 }
632 }
633 }
634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700638 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700639 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700640 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 item.cellX = cellX;
643 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700644
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 // We store hotseat items in canonical form which is this orientation invariant position
646 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700649 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700651 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653
654 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
657 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700658 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659
Michael Jurkac9d95c52011-08-29 14:03:34 -0700660 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700661 }
662
663 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700664 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
665 * cellX, cellY have already been updated on the ItemInfos.
666 */
667 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
668 final long container, final int screen) {
669
670 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
671 int count = items.size();
672
673 for (int i = 0; i < count; i++) {
674 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700675 item.container = container;
676
677 // We store hotseat items in canonical form which is this orientation invariant position
678 // in the hotseat
679 if (context instanceof Launcher && screen < 0 &&
680 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700682 item.cellY);
683 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700684 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700685 }
686
687 final ContentValues values = new ContentValues();
688 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
689 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
690 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700692
693 contentValues.add(values);
694 }
695 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
696 }
697
698 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700699 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800700 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700701 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700702 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700703 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800704 item.cellX = cellX;
705 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700706 item.spanX = spanX;
707 item.spanY = spanY;
708
709 // We store hotseat items in canonical form which is this orientation invariant position
710 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700711 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700712 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700713 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700714 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700715 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700716 }
Adam Cohend4844c32011-02-18 19:25:06 -0800717
Adam Cohend4844c32011-02-18 19:25:06 -0800718 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800719 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700720 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
721 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
722 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
723 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700724 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800725
Michael Jurka816474f2012-06-25 14:49:02 -0700726 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700727 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728
729 /**
730 * Update an item to the database in a specified container.
731 */
732 static void updateItemInDatabase(Context context, final ItemInfo item) {
733 final ContentValues values = new ContentValues();
734 item.onAddToDatabase(values);
735 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
736 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800737 }
738
739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 * Returns true if the shortcuts already exists in the database.
741 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 static boolean shortcutExists(Context context, String title, Intent intent) {
744 final ContentResolver cr = context.getContentResolver();
745 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
746 new String[] { "title", "intent" }, "title=? and intent=?",
747 new String[] { title, intent.toUri(0) }, null);
748 boolean result = false;
749 try {
750 result = c.moveToFirst();
751 } finally {
752 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 return result;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700755 }
756
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700758 * Returns an ItemInfo array containing all the items in the LauncherModel.
759 * The ItemInfo.id is not set through this function.
760 */
761 static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) {
762 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
763 final ContentResolver cr = context.getContentResolver();
764 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] {
765 LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER,
766 LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY,
767 LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null);
768
769 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
770 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
771 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
772 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
773 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
774 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
775 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
776
777 try {
778 while (c.moveToNext()) {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700779 ItemInfo item = new ItemInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -0700780 item.cellX = c.getInt(cellXIndex);
781 item.cellY = c.getInt(cellYIndex);
Winson Chung61c69862013-08-21 19:10:29 -0700782 item.spanX = Math.max(1, c.getInt(spanXIndex));
783 item.spanY = Math.max(1, c.getInt(spanYIndex));
Winson Chungaafa03c2010-06-11 17:34:16 -0700784 item.container = c.getInt(containerIndex);
785 item.itemType = c.getInt(itemTypeIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 item.screenId = c.getInt(screenIndex);
Winson Chungaafa03c2010-06-11 17:34:16 -0700787
788 items.add(item);
789 }
790 } catch (Exception e) {
791 items.clear();
792 } finally {
793 c.close();
794 }
795
796 return items;
797 }
798
799 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
801 */
802 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
803 final ContentResolver cr = context.getContentResolver();
804 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
805 "_id=? and (itemType=? or itemType=?)",
806 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700807 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700808
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 try {
810 if (c.moveToFirst()) {
811 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
812 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
813 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
814 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
815 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
816 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 FolderInfo folderInfo = null;
819 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700820 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
821 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700823 }
824
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825 folderInfo.title = c.getString(titleIndex);
826 folderInfo.id = id;
827 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700829 folderInfo.cellX = c.getInt(cellXIndex);
830 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831
832 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 } finally {
835 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700836 }
837
838 return null;
839 }
840
Joe Onorato9c1289c2009-08-17 11:03:03 -0400841 /**
842 * Add an item to the database in a specified container. Sets the container, screen, cellX and
843 * cellY fields of the item. Also assigns an ID to the item.
844 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400847 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 item.cellX = cellX;
849 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 // We store hotseat items in canonical form which is this orientation invariant position
851 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400858
859 final ContentValues values = new ContentValues();
860 final ContentResolver cr = context.getContentResolver();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 item.onAddToDatabase(values);
862
Michael Jurka414300a2013-08-27 15:42:35 +0200863 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700864 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 item.updateValuesWithCoordinates(values, item.cellX, item.cellY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700866
Michael Jurkac9d95c52011-08-29 14:03:34 -0700867 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700868 public void run() {
869 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
870 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700872 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700873 synchronized (sBgLock) {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700874 checkItemInfoLocked(item.id, item, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700875 sBgItemsIdMap.put(item.id, item);
876 switch (item.itemType) {
877 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
878 sBgFolders.put(item.id, (FolderInfo) item);
879 // Fall through
880 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
881 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
882 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
883 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
884 sBgWorkspaceItems.add(item);
885 } else {
886 if (!sBgFolders.containsKey(item.container)) {
887 // Adding an item to a folder that doesn't exist.
888 String msg = "adding item: " + item + " to a folder that " +
889 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -0700890 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700891 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700892 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700893 break;
894 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
895 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
896 break;
897 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700898 }
899 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700900 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700901 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700902 }
903
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700905 * Creates a new unique child id, for a given cell span across all layouts.
906 */
Michael Jurka845ba3b2010-09-28 17:09:46 -0700907 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -0700908 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700909 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -0700911 }
912
Winson Chungaafa03c2010-06-11 17:34:16 -0700913 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -0700914 * Removes the specified item from the database
915 * @param context
916 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 */
Michael Jurkac9d95c52011-08-29 14:03:34 -0700918 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 final ContentResolver cr = context.getContentResolver();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700920 final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false);
Adam Cohen487f7dd2012-06-28 18:12:10 -0700921
Michael Jurka83df1882011-08-31 20:59:26 -0700922 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700923 public void run() {
Michael Jurkac9d95c52011-08-29 14:03:34 -0700924 cr.delete(uriToDelete, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700925
926 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -0700927 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700928 switch (item.itemType) {
929 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
930 sBgFolders.remove(item.id);
931 for (ItemInfo info: sBgItemsIdMap.values()) {
932 if (info.container == item.id) {
933 // We are deleting a folder which still contains items that
934 // think they are contained by that folder.
935 String msg = "deleting a folder (" + item + ") which still " +
936 "contains items (" + info + ")";
Adam Cohen28b3e102012-10-04 17:21:33 -0700937 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700938 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700939 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700940 sBgWorkspaceItems.remove(item);
941 break;
942 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
943 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
944 sBgWorkspaceItems.remove(item);
945 break;
946 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
947 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
948 break;
949 }
950 sBgItemsIdMap.remove(item.id);
951 sBgDbIconCache.remove(item);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 }
953 }
Michael Jurka83df1882011-08-31 20:59:26 -0700954 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700955 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400956 }
957
958 /**
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 * Update the order of the workspace screens in the database. The array list contains
960 * a list of screen ids in the order that they should appear.
961 */
Winson Chungc9168342013-06-26 14:54:55 -0700962 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -0800963 // Log to disk
964 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
965 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
966
Winson Chung64359a52013-07-08 17:17:08 -0700967 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -0700968 final ContentResolver cr = context.getContentResolver();
969 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
970
971 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -0700972 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 while (iter.hasNext()) {
974 long id = iter.next();
975 if (id < 0) {
976 iter.remove();
977 }
978 }
979
980 Runnable r = new Runnable() {
981 @Override
982 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700983 // Clear the table
984 cr.delete(uri, null, null);
Winson Chung76828c82013-08-19 15:43:29 -0700985 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 ContentValues[] values = new ContentValues[count];
987 for (int i = 0; i < count; i++) {
988 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -0700989 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700990 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
991 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 values[i] = v;
993 }
994 cr.bulkInsert(uri, values);
Winson Chung9e6a0a22013-08-27 11:58:12 -0700995
Winson Chungba9c37f2013-08-30 14:11:37 -0700996 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -0700997 sBgWorkspaceScreens.clear();
998 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -0700999 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 }
1001 };
1002 runOnWorkerThread(r);
1003 }
1004
1005 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001006 * Remove the contents of the specified folder from the database
1007 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001008 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 final ContentResolver cr = context.getContentResolver();
1010
Michael Jurkac9d95c52011-08-29 14:03:34 -07001011 Runnable r = new Runnable() {
1012 public void run() {
1013 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001014 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001015 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001016 sBgItemsIdMap.remove(info.id);
1017 sBgFolders.remove(info.id);
1018 sBgDbIconCache.remove(info);
1019 sBgWorkspaceItems.remove(info);
1020 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001021
Michael Jurkac9d95c52011-08-29 14:03:34 -07001022 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1023 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001024 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001025 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001026 for (ItemInfo childInfo : info.contents) {
1027 sBgItemsIdMap.remove(childInfo.id);
1028 sBgDbIconCache.remove(childInfo);
1029 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001030 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001031 }
1032 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001033 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 }
1035
1036 /**
1037 * Set this as the current Launcher activity object for the loader.
1038 */
1039 public void initialize(Callbacks callbacks) {
1040 synchronized (mLock) {
1041 mCallbacks = new WeakReference<Callbacks>(callbacks);
1042 }
1043 }
1044
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001045 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1047 * ACTION_PACKAGE_CHANGED.
1048 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001049 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001050 public void onReceive(Context context, Intent intent) {
Joe Onorato36115782010-06-17 13:28:48 -04001051 if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001052
Joe Onorato36115782010-06-17 13:28:48 -04001053 final String action = intent.getAction();
Joe Onoratof99f8c12009-10-31 17:27:36 -04001054
Joe Onorato36115782010-06-17 13:28:48 -04001055 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
1056 || Intent.ACTION_PACKAGE_REMOVED.equals(action)
1057 || Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1058 final String packageName = intent.getData().getSchemeSpecificPart();
1059 final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060
Joe Onorato36115782010-06-17 13:28:48 -04001061 int op = PackageUpdatedTask.OP_NONE;
1062
1063 if (packageName == null || packageName.length() == 0) {
1064 // they sent us a bad intent
1065 return;
1066 }
1067
1068 if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
1069 op = PackageUpdatedTask.OP_UPDATE;
1070 } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
1071 if (!replacing) {
1072 op = PackageUpdatedTask.OP_REMOVE;
1073 }
1074 // else, we are replacing the package, so a PACKAGE_ADDED will be sent
1075 // later, we will update the package at this time
1076 } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
1077 if (!replacing) {
1078 op = PackageUpdatedTask.OP_ADD;
1079 } else {
1080 op = PackageUpdatedTask.OP_UPDATE;
1081 }
1082 }
1083
1084 if (op != PackageUpdatedTask.OP_NONE) {
1085 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }));
1086 }
1087
1088 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
Joe Onoratocec58332010-10-07 14:37:40 -04001089 // First, schedule to add these apps back in.
1090 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1091 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages));
1092 // Then, rebind everything.
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001093 startLoaderFromBackground();
Joe Onorato36115782010-06-17 13:28:48 -04001094 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
1095 String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
1096 enqueuePackageUpdated(new PackageUpdatedTask(
1097 PackageUpdatedTask.OP_UNAVAILABLE, packages));
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001098 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001099 // If we have changed locale we need to clear out the labels in all apps/workspace.
1100 forceReload();
1101 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1102 // Check if configuration change was an mcc/mnc change which would affect app resources
1103 // and we would need to clear out the labels in all apps/workspace. Same handling as
1104 // above for ACTION_LOCALE_CHANGED
1105 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001106 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001107 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001108 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001109 forceReload();
1110 }
1111 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001112 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001113 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1114 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Michael Jurkaec9788e2011-08-29 11:24:45 -07001115 if (mCallbacks != null) {
1116 Callbacks callbacks = mCallbacks.get();
1117 if (callbacks != null) {
1118 callbacks.bindSearchablesChanged();
1119 }
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001120 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001121 }
1122 }
1123
Reena Lee93f824a2011-09-23 17:20:28 -07001124 private void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001125 resetLoadedState(true, true);
1126
Reena Lee93f824a2011-09-23 17:20:28 -07001127 // Do this here because if the launcher activity is running it will be restarted.
1128 // If it's not running startLoaderFromBackground will merely tell it that it needs
1129 // to reload.
1130 startLoaderFromBackground();
1131 }
1132
Winson Chungf0c6ae02012-03-21 16:10:31 -07001133 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1134 synchronized (mLock) {
1135 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1136 // mWorkspaceLoaded to true later
1137 stopLoaderLocked();
1138 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1139 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1140 }
1141 }
1142
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001143 /**
1144 * When the launcher is in the background, it's possible for it to miss paired
1145 * configuration changes. So whenever we trigger the loader from the background
1146 * tell the launcher that it needs to re-run the loader when it comes back instead
1147 * of doing it now.
1148 */
1149 public void startLoaderFromBackground() {
1150 boolean runLoader = false;
1151 if (mCallbacks != null) {
1152 Callbacks callbacks = mCallbacks.get();
1153 if (callbacks != null) {
1154 // Only actually run the loader if they're not paused.
1155 if (!callbacks.setLoadOnResume()) {
1156 runLoader = true;
1157 }
1158 }
1159 }
1160 if (runLoader) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001161 startLoader(false, -1);
Joe Onorato790c2d92010-06-11 00:14:11 -07001162 }
Joe Onorato36115782010-06-17 13:28:48 -04001163 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001164
Reena Lee93f824a2011-09-23 17:20:28 -07001165 // If there is already a loader task running, tell it to stop.
1166 // returns true if isLaunching() was true on the old task
1167 private boolean stopLoaderLocked() {
1168 boolean isLaunching = false;
1169 LoaderTask oldTask = mLoaderTask;
1170 if (oldTask != null) {
1171 if (oldTask.isLaunching()) {
1172 isLaunching = true;
1173 }
1174 oldTask.stopLocked();
1175 }
1176 return isLaunching;
1177 }
1178
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001179 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Joe Onorato36115782010-06-17 13:28:48 -04001180 synchronized (mLock) {
1181 if (DEBUG_LOADERS) {
1182 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001184
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001185 // Clear any deferred bind-runnables from the synchronized load process
1186 // We must do this before any loading/binding is scheduled below.
1187 mDeferredBindRunnables.clear();
1188
Joe Onorato36115782010-06-17 13:28:48 -04001189 // Don't bother to start the thread if we know it's not going to do anything
1190 if (mCallbacks != null && mCallbacks.get() != null) {
1191 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001192 // also, don't downgrade isLaunching if we're already running
1193 isLaunching = isLaunching || stopLoaderLocked();
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001194 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001195 if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) {
1196 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1197 } else {
1198 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1199 sWorker.post(mLoaderTask);
1200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203 }
1204
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 void bindRemainingSynchronousPages() {
1206 // Post the remaining side pages to be loaded
1207 if (!mDeferredBindRunnables.isEmpty()) {
1208 for (final Runnable r : mDeferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001209 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001210 }
1211 mDeferredBindRunnables.clear();
1212 }
1213 }
1214
Joe Onorato36115782010-06-17 13:28:48 -04001215 public void stopLoader() {
1216 synchronized (mLock) {
1217 if (mLoaderTask != null) {
1218 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 }
1220 }
Joe Onorato36115782010-06-17 13:28:48 -04001221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222
Winson Chung76828c82013-08-19 15:43:29 -07001223 /** Loads the workspace screens db into a map of Rank -> ScreenId */
1224 private static TreeMap<Integer, Long> loadWorkspaceScreensDb(Context context) {
1225 final ContentResolver contentResolver = context.getContentResolver();
1226 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1227 final Cursor sc = contentResolver.query(screensUri, null, null, null, null);
1228 TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>();
1229
1230 try {
1231 final int idIndex = sc.getColumnIndexOrThrow(
1232 LauncherSettings.WorkspaceScreens._ID);
1233 final int rankIndex = sc.getColumnIndexOrThrow(
1234 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1235 while (sc.moveToNext()) {
1236 try {
1237 long screenId = sc.getLong(idIndex);
1238 int rank = sc.getInt(rankIndex);
Winson Chung76828c82013-08-19 15:43:29 -07001239 orderedScreens.put(rank, screenId);
1240 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001241 Launcher.addDumpLog(TAG, "Desktop items loading interrupted - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001242 }
1243 }
1244 } finally {
1245 sc.close();
1246 }
Winson Chunga90303b2013-11-15 13:05:06 -08001247
1248 // Log to disk
1249 Launcher.addDumpLog(TAG, "11683562 - loadWorkspaceScreensDb()", true);
1250 ArrayList<String> orderedScreensPairs= new ArrayList<String>();
1251 for (Integer i : orderedScreens.keySet()) {
1252 orderedScreensPairs.add("{ " + i + ": " + orderedScreens.get(i) + " }");
1253 }
1254 Launcher.addDumpLog(TAG, "11683562 - screens: " +
1255 TextUtils.join(", ", orderedScreensPairs), true);
Winson Chung76828c82013-08-19 15:43:29 -07001256 return orderedScreens;
1257 }
1258
Michael Jurkac57b7a82011-08-09 22:02:20 -07001259 public boolean isAllAppsLoaded() {
1260 return mAllAppsLoaded;
1261 }
1262
Winson Chung36a62fe2012-05-06 18:04:42 -07001263 boolean isLoadingWorkspace() {
1264 synchronized (mLock) {
1265 if (mLoaderTask != null) {
1266 return mLoaderTask.isLoadingWorkspace();
1267 }
1268 }
1269 return false;
1270 }
1271
Joe Onorato36115782010-06-17 13:28:48 -04001272 /**
1273 * Runnable for the thread that loads the contents of the launcher:
1274 * - workspace icons
1275 * - widgets
1276 * - all apps icons
1277 */
1278 private class LoaderTask implements Runnable {
1279 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001280 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001281 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001282 private boolean mStopped;
1283 private boolean mLoadAndBindStepFinished;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001284
Winson Chungc3eecff2011-07-11 17:44:15 -07001285 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001286
1287 LoaderTask(Context context, boolean isLaunching) {
1288 mContext = context;
1289 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001290 mLabelCache = new HashMap<Object, CharSequence>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001291 }
1292
Joe Onorato36115782010-06-17 13:28:48 -04001293 boolean isLaunching() {
1294 return mIsLaunching;
1295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001296
Winson Chung36a62fe2012-05-06 18:04:42 -07001297 boolean isLoadingWorkspace() {
1298 return mIsLoadingAndBindingWorkspace;
1299 }
1300
Winson Chungc763c4e2013-07-19 13:49:06 -07001301 /** Returns whether this is an upgrade path */
1302 private boolean loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001303 mIsLoadingAndBindingWorkspace = true;
1304
Joe Onorato36115782010-06-17 13:28:48 -04001305 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001306 if (DEBUG_LOADERS) {
1307 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001309
Winson Chungc763c4e2013-07-19 13:49:06 -07001310 boolean isUpgradePath = false;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001311 if (!mWorkspaceLoaded) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001312 isUpgradePath = loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001313 synchronized (LoaderTask.this) {
1314 if (mStopped) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001315 return isUpgradePath;
Reena Lee93f824a2011-09-23 17:20:28 -07001316 }
1317 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 }
1319 }
1320
Joe Onorato36115782010-06-17 13:28:48 -04001321 // Bind the workspace
Winson Chungc763c4e2013-07-19 13:49:06 -07001322 bindWorkspace(-1, isUpgradePath);
1323 return isUpgradePath;
Joe Onorato36115782010-06-17 13:28:48 -04001324 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001325
Joe Onorato36115782010-06-17 13:28:48 -04001326 private void waitForIdle() {
1327 // Wait until the either we're stopped or the other threads are done.
1328 // This way we don't start loading all apps until the workspace has settled
1329 // down.
1330 synchronized (LoaderTask.this) {
1331 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001332
Joe Onorato36115782010-06-17 13:28:48 -04001333 mHandler.postIdle(new Runnable() {
1334 public void run() {
1335 synchronized (LoaderTask.this) {
1336 mLoadAndBindStepFinished = true;
1337 if (DEBUG_LOADERS) {
1338 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001339 }
Joe Onorato36115782010-06-17 13:28:48 -04001340 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001341 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001342 }
Joe Onorato36115782010-06-17 13:28:48 -04001343 });
1344
Michael Jurkac7700af2013-05-14 20:17:58 +02001345 while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) {
Joe Onorato36115782010-06-17 13:28:48 -04001346 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001347 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1348 // wait no longer than 1sec at a time
1349 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001350 } catch (InterruptedException ex) {
1351 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001352 }
1353 }
Joe Onorato36115782010-06-17 13:28:48 -04001354 if (DEBUG_LOADERS) {
1355 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001356 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001357 + "ms for previous step to finish binding");
1358 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001359 }
Joe Onorato36115782010-06-17 13:28:48 -04001360 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001361
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001362 void runBindSynchronousPage(int synchronousBindPage) {
1363 if (synchronousBindPage < 0) {
1364 // Ensure that we have a valid page index to load synchronously
1365 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1366 "valid page index");
1367 }
1368 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1369 // Ensure that we don't try and bind a specified page when the pages have not been
1370 // loaded already (we should load everything asynchronously in that case)
1371 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1372 }
1373 synchronized (mLock) {
1374 if (mIsLoaderTaskRunning) {
1375 // Ensure that we are never running the background loading at this point since
1376 // we also touch the background collections
1377 throw new RuntimeException("Error! Background loading is already running");
1378 }
1379 }
1380
1381 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1382 // data structures, we can't allow any other thread to touch that data, but because
1383 // this call is synchronous, we can get away with not locking).
1384
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001385 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001386 // operations from the previous activity. We need to ensure that all queued operations
1387 // are executed before any synchronous binding work is done.
1388 mHandler.flush();
1389
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001390 // Divide the set of loaded items into those that we are binding synchronously, and
1391 // everything else that is to be bound normally (asynchronously).
Winson Chungc763c4e2013-07-19 13:49:06 -07001392 bindWorkspace(synchronousBindPage, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001393 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1394 // arise from that.
1395 onlyBindAllApps();
1396 }
1397
Joe Onorato36115782010-06-17 13:28:48 -04001398 public void run() {
Winson Chungc763c4e2013-07-19 13:49:06 -07001399 boolean isUpgrade = false;
1400
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001401 synchronized (mLock) {
1402 mIsLoaderTaskRunning = true;
1403 }
Joe Onorato36115782010-06-17 13:28:48 -04001404 // Optimize for end-user experience: if the Launcher is up and // running with the
1405 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1406 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001407 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001408 // Elevate priority when Home launches for the first time to avoid
1409 // starving at boot time. Staring at a blank home is not cool.
1410 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001411 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1412 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001413 android.os.Process.setThreadPriority(mIsLaunching
1414 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1415 }
Winson Chung64359a52013-07-08 17:17:08 -07001416 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Winson Chungc763c4e2013-07-19 13:49:06 -07001417 isUpgrade = loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001418
Joe Onorato36115782010-06-17 13:28:48 -04001419 if (mStopped) {
1420 break keep_running;
1421 }
1422
1423 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1424 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001425 synchronized (mLock) {
1426 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001427 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001428 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1429 }
1430 }
Joe Onorato36115782010-06-17 13:28:48 -04001431 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001432
1433 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001434 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1435 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001436
1437 // Restore the default thread priority after we are done loading items
1438 synchronized (mLock) {
1439 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1440 }
Joe Onorato36115782010-06-17 13:28:48 -04001441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001442
Winson Chungaac01e12011-08-17 10:37:13 -07001443 // Update the saved icons if necessary
1444 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001445 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001446 for (Object key : sBgDbIconCache.keySet()) {
1447 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1448 }
1449 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001450 }
Winson Chungaac01e12011-08-17 10:37:13 -07001451
Winson Chungc58497e2013-09-03 17:48:37 -07001452 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
1453 // Ensure that all the applications that are in the system are
1454 // represented on the home screen.
Winson Chungc58497e2013-09-03 17:48:37 -07001455 if (!UPGRADE_USE_MORE_APPS_FOLDER || !isUpgrade) {
Winson Chungc58497e2013-09-03 17:48:37 -07001456 verifyApplications();
1457 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001458 }
1459
Joe Onorato36115782010-06-17 13:28:48 -04001460 // Clear out this reference, otherwise we end up holding it until all of the
1461 // callback runnables are done.
1462 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463
Joe Onorato36115782010-06-17 13:28:48 -04001464 synchronized (mLock) {
1465 // If we are still the last one to be scheduled, remove ourselves.
1466 if (mLoaderTask == this) {
1467 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001469 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001470 }
Joe Onorato36115782010-06-17 13:28:48 -04001471 }
1472
1473 public void stopLocked() {
1474 synchronized (LoaderTask.this) {
1475 mStopped = true;
1476 this.notify();
1477 }
1478 }
1479
1480 /**
1481 * Gets the callbacks object. If we've been stopped, or if the launcher object
1482 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1483 * object that was around when the deferred message was scheduled, and if there's
1484 * a new Callbacks object around then also return null. This will save us from
1485 * calling onto it with data that will be ignored.
1486 */
1487 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1488 synchronized (mLock) {
1489 if (mStopped) {
1490 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001491 }
Joe Onorato36115782010-06-17 13:28:48 -04001492
1493 if (mCallbacks == null) {
1494 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001495 }
Joe Onorato36115782010-06-17 13:28:48 -04001496
1497 final Callbacks callbacks = mCallbacks.get();
1498 if (callbacks != oldCallbacks) {
1499 return null;
1500 }
1501 if (callbacks == null) {
1502 Log.w(TAG, "no mCallbacks");
1503 return null;
1504 }
1505
1506 return callbacks;
1507 }
1508 }
1509
Winson Chungc763c4e2013-07-19 13:49:06 -07001510 private void verifyApplications() {
1511 final Context context = mApp.getContext();
1512
1513 // Cross reference all the applications in our apps list with items in the workspace
1514 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001515 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001516 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001517 for (AppInfo app : mBgAllAppsList.data) {
Winson Chungc763c4e2013-07-19 13:49:06 -07001518 tmpInfos = getItemInfoForComponentName(app.componentName);
1519 if (tmpInfos.isEmpty()) {
1520 // We are missing an application icon, so add this to the workspace
1521 added.add(app);
1522 // This is a rare event, so lets log it
1523 Log.e(TAG, "Missing Application on load: " + app);
1524 }
1525 }
1526 }
1527 if (!added.isEmpty()) {
1528 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chungfe9d96a2013-11-14 11:30:05 -08001529 addAndBindAddedApps(context, added, cb, new ArrayList<AppInfo>());
Winson Chungc763c4e2013-07-19 13:49:06 -07001530 }
1531 }
1532
Winson Chung5f8afe62013-08-12 16:19:28 -07001533 private boolean checkItemDimensions(ItemInfo info) {
Winson Chung892c74d2013-08-22 16:15:50 -07001534 LauncherAppState app = LauncherAppState.getInstance();
1535 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1536 return (info.cellX + info.spanX) > (int) grid.numColumns ||
1537 (info.cellY + info.spanY) > (int) grid.numRows;
Winson Chung5f8afe62013-08-12 16:19:28 -07001538 }
1539
Joe Onorato36115782010-06-17 13:28:48 -04001540 // check & update map of what's occupied; used to discard overlapping/invalid items
Winson Chunga0b7e862013-09-05 16:03:15 -07001541 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
1542 AtomicBoolean deleteOnItemOverlap) {
Winson Chung892c74d2013-08-22 16:15:50 -07001543 LauncherAppState app = LauncherAppState.getInstance();
1544 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1545 int countX = (int) grid.numColumns;
1546 int countY = (int) grid.numRows;
1547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001549 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001550 // Return early if we detect that an item is under the hotseat button
1551 if (mCallbacks == null ||
1552 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
1553 deleteOnItemOverlap.set(true);
1554 return false;
1555 }
1556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
1558 if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1559 [(int) item.screenId][0] != null) {
1560 Log.e(TAG, "Error loading shortcut into hotseat " + item
1561 + " into position (" + item.screenId + ":" + item.cellX + ","
1562 + item.cellY + ") occupied by "
1563 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1564 [(int) item.screenId][0]);
1565 return false;
1566 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001567 } else {
Winson Chung892c74d2013-08-22 16:15:50 -07001568 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 items[(int) item.screenId][0] = item;
1570 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001571 return true;
1572 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001573 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1574 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001575 return true;
1576 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001577
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001579 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 occupied.put(item.screenId, items);
1581 }
1582
1583 ItemInfo[][] screens = occupied.get(item.screenId);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001584 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001585 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1586 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001588 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001590 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001591 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001593 return false;
1594 }
1595 }
1596 }
1597 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1598 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001600 }
1601 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001602
Joe Onorato36115782010-06-17 13:28:48 -04001603 return true;
1604 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001605
Winson Chungba9c37f2013-08-30 14:11:37 -07001606 /** Clears all the sBg data structures */
1607 private void clearSBgDataStructures() {
1608 synchronized (sBgLock) {
1609 sBgWorkspaceItems.clear();
1610 sBgAppWidgets.clear();
1611 sBgFolders.clear();
1612 sBgItemsIdMap.clear();
1613 sBgDbIconCache.clear();
1614 sBgWorkspaceScreens.clear();
1615 }
1616 }
1617
Winson Chungc763c4e2013-07-19 13:49:06 -07001618 /** Returns whether this is an upgradge path */
1619 private boolean loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001620 // Log to disk
1621 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1622
Joe Onorato36115782010-06-17 13:28:48 -04001623 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624
Joe Onorato36115782010-06-17 13:28:48 -04001625 final Context context = mContext;
1626 final ContentResolver contentResolver = context.getContentResolver();
1627 final PackageManager manager = context.getPackageManager();
1628 final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
1629 final boolean isSafeMode = manager.isSafeMode();
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001630
Winson Chung892c74d2013-08-22 16:15:50 -07001631 LauncherAppState app = LauncherAppState.getInstance();
1632 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1633 int countX = (int) grid.numColumns;
1634 int countY = (int) grid.numRows;
1635
Michael Jurkab85f8a42012-04-25 15:48:32 -07001636 // Make sure the default workspace is loaded, if needed
Michael Jurka414300a2013-08-27 15:42:35 +02001637 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary(0);
Adam Cohene25af792013-06-06 23:08:25 -07001638
Winson Chungc763c4e2013-07-19 13:49:06 -07001639 // Check if we need to do any upgrade-path logic
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001640 // (Includes having just imported default favorites)
Michael Jurka414300a2013-08-27 15:42:35 +02001641 boolean loadedOldDb = LauncherAppState.getLauncherProvider().justLoadedOldDb();
Dan Sandlerf0b8dac2013-11-19 12:21:25 -05001642
Winson Chung9f9f00b2013-11-15 13:27:00 -08001643 // Log to disk
1644 Launcher.addDumpLog(TAG, "11683562 - loadedOldDb: " + loadedOldDb, true);
Michael Jurkab85f8a42012-04-25 15:48:32 -07001645
Winson Chung2abf94d2012-07-18 18:16:38 -07001646 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001647 clearSBgDataStructures();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001648
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001649 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001650 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI;
Chris Wrene523e702013-10-09 10:36:55 -04001651 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001652 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001653
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001654 // +1 for the hotseat (it can be larger than the workspace)
1655 // Load workspace in reverse order to ensure that latest items are loaded first (and
1656 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001657 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001659 try {
1660 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1661 final int intentIndex = c.getColumnIndexOrThrow
1662 (LauncherSettings.Favorites.INTENT);
1663 final int titleIndex = c.getColumnIndexOrThrow
1664 (LauncherSettings.Favorites.TITLE);
1665 final int iconTypeIndex = c.getColumnIndexOrThrow(
1666 LauncherSettings.Favorites.ICON_TYPE);
1667 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1668 final int iconPackageIndex = c.getColumnIndexOrThrow(
1669 LauncherSettings.Favorites.ICON_PACKAGE);
1670 final int iconResourceIndex = c.getColumnIndexOrThrow(
1671 LauncherSettings.Favorites.ICON_RESOURCE);
1672 final int containerIndex = c.getColumnIndexOrThrow(
1673 LauncherSettings.Favorites.CONTAINER);
1674 final int itemTypeIndex = c.getColumnIndexOrThrow(
1675 LauncherSettings.Favorites.ITEM_TYPE);
1676 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1677 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001678 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1679 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001680 final int screenIndex = c.getColumnIndexOrThrow(
1681 LauncherSettings.Favorites.SCREEN);
1682 final int cellXIndex = c.getColumnIndexOrThrow
1683 (LauncherSettings.Favorites.CELLX);
1684 final int cellYIndex = c.getColumnIndexOrThrow
1685 (LauncherSettings.Favorites.CELLY);
1686 final int spanXIndex = c.getColumnIndexOrThrow
1687 (LauncherSettings.Favorites.SPANX);
1688 final int spanYIndex = c.getColumnIndexOrThrow(
1689 LauncherSettings.Favorites.SPANY);
1690 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1691 //final int displayModeIndex = c.getColumnIndexOrThrow(
1692 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001694 ShortcutInfo info;
1695 String intentDescription;
1696 LauncherAppWidgetInfo appWidgetInfo;
1697 int container;
1698 long id;
1699 Intent intent;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001701 while (!mStopped && c.moveToNext()) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001702 AtomicBoolean deleteOnItemOverlap = new AtomicBoolean(false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001703 try {
1704 int itemType = c.getInt(itemTypeIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001705
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001706 switch (itemType) {
1707 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1708 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001709 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001710 intentDescription = c.getString(intentIndex);
1711 try {
1712 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001713 ComponentName cn = intent.getComponent();
Winson Chung68fd3c32013-08-30 16:38:00 -07001714 if (cn != null && !isValidPackageComponent(manager, cn)) {
Winson Chungee055712013-07-30 14:46:24 -07001715 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chung1323b482013-08-05 12:41:55 -07001716 // Log the invalid package, and remove it from the db
Winson Chunga0b7e862013-09-05 16:03:15 -07001717 Launcher.addDumpLog(TAG, "Invalid package removed: " + cn, true);
1718 itemsToRemove.add(id);
Winson Chungee055712013-07-30 14:46:24 -07001719 } else {
Winson Chung1323b482013-08-05 12:41:55 -07001720 // If apps can be on external storage, then we just
1721 // leave them for the user to remove (maybe add
1722 // visual treatment to it)
Winson Chung933bae62013-08-29 11:42:30 -07001723 Launcher.addDumpLog(TAG, "Invalid package found: " + cn, true);
Winson Chungee055712013-07-30 14:46:24 -07001724 }
1725 continue;
1726 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001727 } catch (URISyntaxException e) {
Winson Chung933bae62013-08-29 11:42:30 -07001728 Launcher.addDumpLog(TAG, "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001729 continue;
1730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001731
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001732 if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
1733 info = getShortcutInfo(manager, intent, context, c, iconIndex,
1734 titleIndex, mLabelCache);
1735 } else {
1736 info = getShortcutInfo(c, context, iconTypeIndex,
1737 iconPackageIndex, iconResourceIndex, iconIndex,
1738 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07001739
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001740 // App shortcuts that used to be automatically added to Launcher
1741 // didn't always have the correct intent flags set, so do that
1742 // here
1743 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07001744 intent.getCategories() != null &&
1745 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07001746 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001747 intent.addFlags(
1748 Intent.FLAG_ACTIVITY_NEW_TASK |
1749 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1750 }
Michael Jurka96879562012-03-22 05:54:33 -07001751 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001752
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001753 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07001754 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001755 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001756 container = c.getInt(containerIndex);
1757 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001758 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001759 info.cellX = c.getInt(cellXIndex);
1760 info.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001761 info.spanX = 1;
1762 info.spanY = 1;
1763 // Skip loading items that are out of bounds
1764 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1765 if (checkItemDimensions(info)) {
Winson Chung933bae62013-08-29 11:42:30 -07001766 Launcher.addDumpLog(TAG, "Skipped loading out of bounds shortcut: "
1767 + info + ", " + grid.numColumns + "x" + grid.numRows, true);
Winson Chung5f8afe62013-08-12 16:19:28 -07001768 continue;
1769 }
1770 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001771 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001772 deleteOnItemOverlap.set(false);
1773 if (!checkItemPlacement(occupied, info, deleteOnItemOverlap)) {
1774 if (deleteOnItemOverlap.get()) {
1775 itemsToRemove.add(id);
1776 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001777 break;
1778 }
1779
1780 switch (container) {
1781 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1782 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1783 sBgWorkspaceItems.add(info);
1784 break;
1785 default:
1786 // Item is in a user folder
1787 FolderInfo folderInfo =
1788 findOrMakeFolder(sBgFolders, container);
1789 folderInfo.add(info);
1790 break;
1791 }
1792 sBgItemsIdMap.put(info.id, info);
1793
1794 // now that we've loaded everthing re-save it with the
1795 // icon in case it disappears somehow.
1796 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07001797 } else {
1798 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001799 }
1800 break;
1801
1802 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1803 id = c.getLong(idIndex);
1804 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
1805
1806 folderInfo.title = c.getString(titleIndex);
1807 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001809 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001810 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001811 folderInfo.cellX = c.getInt(cellXIndex);
1812 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001813 folderInfo.spanX = 1;
1814 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815
Winson Chung5f8afe62013-08-12 16:19:28 -07001816 // Skip loading items that are out of bounds
1817 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung5f8afe62013-08-12 16:19:28 -07001818 if (checkItemDimensions(folderInfo)) {
1819 Log.d(TAG, "Skipped loading out of bounds folder");
1820 continue;
1821 }
1822 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001823 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001824 deleteOnItemOverlap.set(false);
1825 if (!checkItemPlacement(occupied, folderInfo,
1826 deleteOnItemOverlap)) {
1827 if (deleteOnItemOverlap.get()) {
1828 itemsToRemove.add(id);
1829 }
Daniel Sandler8802e962010-05-26 16:28:16 -04001830 break;
1831 }
Winson Chung5f8afe62013-08-12 16:19:28 -07001832
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001834 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
1835 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
1836 sBgWorkspaceItems.add(folderInfo);
1837 break;
Joe Onorato36115782010-06-17 13:28:48 -04001838 }
Joe Onorato17a89222011-02-08 17:26:11 -08001839
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001840 sBgItemsIdMap.put(folderInfo.id, folderInfo);
1841 sBgFolders.put(folderInfo.id, folderInfo);
1842 break;
1843
1844 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1845 // Read all Launcher-specific widget details
1846 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04001847 String savedProvider = c.getString(appWidgetProviderIndex);
1848
Joe Onorato36115782010-06-17 13:28:48 -04001849 id = c.getLong(idIndex);
Joe Onorato36115782010-06-17 13:28:48 -04001850
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001851 final AppWidgetProviderInfo provider =
1852 widgets.getAppWidgetInfo(appWidgetId);
Joe Onorato36115782010-06-17 13:28:48 -04001853
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001854 if (!isSafeMode && (provider == null || provider.provider == null ||
1855 provider.provider.getPackageName() == null)) {
1856 String log = "Deleting widget that isn't installed anymore: id="
1857 + id + " appWidgetId=" + appWidgetId;
1858 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07001859 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001860 itemsToRemove.add(id);
1861 } else {
1862 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
1863 provider.provider);
1864 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07001865 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 appWidgetInfo.cellX = c.getInt(cellXIndex);
1867 appWidgetInfo.cellY = c.getInt(cellYIndex);
1868 appWidgetInfo.spanX = c.getInt(spanXIndex);
1869 appWidgetInfo.spanY = c.getInt(spanYIndex);
1870 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
1871 appWidgetInfo.minSpanX = minSpan[0];
1872 appWidgetInfo.minSpanY = minSpan[1];
Joe Onorato36115782010-06-17 13:28:48 -04001873
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001874 container = c.getInt(containerIndex);
1875 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1876 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1877 Log.e(TAG, "Widget found where container != " +
1878 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
1879 continue;
1880 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001881
Adam Cohene25af792013-06-06 23:08:25 -07001882 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07001883 // Skip loading items that are out of bounds
1884 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1885 if (checkItemDimensions(appWidgetInfo)) {
1886 Log.d(TAG, "Skipped loading out of bounds app widget");
1887 continue;
1888 }
1889 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 // check & update map of what's occupied
Winson Chunga0b7e862013-09-05 16:03:15 -07001891 deleteOnItemOverlap.set(false);
1892 if (!checkItemPlacement(occupied, appWidgetInfo,
1893 deleteOnItemOverlap)) {
1894 if (deleteOnItemOverlap.get()) {
1895 itemsToRemove.add(id);
1896 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001897 break;
1898 }
Chris Wrenc3919c02013-09-18 09:48:33 -04001899 String providerName = provider.provider.flattenToString();
1900 if (!providerName.equals(savedProvider)) {
1901 ContentValues values = new ContentValues();
1902 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER,
1903 providerName);
1904 String where = BaseColumns._ID + "= ?";
1905 String[] args = {Integer.toString(c.getInt(idIndex))};
1906 contentResolver.update(contentUri, values, where, args);
1907 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
1909 sBgAppWidgets.add(appWidgetInfo);
1910 }
Joe Onorato36115782010-06-17 13:28:48 -04001911 break;
1912 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001913 } catch (Exception e) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001914 Launcher.addDumpLog(TAG, "Desktop items loading interrupted: " + e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08001915 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001916 }
1917 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04001918 if (c != null) {
1919 c.close();
1920 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001921 }
1922
Winson Chungba9c37f2013-08-30 14:11:37 -07001923 // Break early if we've stopped loading
1924 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001925 clearSBgDataStructures();
1926 return false;
1927 }
1928
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001929 if (itemsToRemove.size() > 0) {
1930 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Adam Cohen4caf2982013-08-20 18:54:31 -07001931 LauncherSettings.Favorites.CONTENT_URI);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 // Remove dead items
1933 for (long id : itemsToRemove) {
1934 if (DEBUG_LOADERS) {
1935 Log.d(TAG, "Removed id = " + id);
1936 }
1937 // Don't notify content observers
1938 try {
1939 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
1940 null, null);
1941 } catch (RemoteException e) {
1942 Log.w(TAG, "Could not remove id = " + id);
1943 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08001944 }
1945 }
1946
Winson Chungc763c4e2013-07-19 13:49:06 -07001947 if (loadedOldDb) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 long maxScreenId = 0;
1949 // If we're importing we use the old screen order.
1950 for (ItemInfo item: sBgItemsIdMap.values()) {
1951 long screenId = item.screenId;
1952 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1953 !sBgWorkspaceScreens.contains(screenId)) {
1954 sBgWorkspaceScreens.add(screenId);
1955 if (screenId > maxScreenId) {
1956 maxScreenId = screenId;
1957 }
1958 }
1959 }
1960 Collections.sort(sBgWorkspaceScreens);
Winson Chung9f9f00b2013-11-15 13:27:00 -08001961 // Log to disk
1962 Launcher.addDumpLog(TAG, "11683562 - maxScreenId: " + maxScreenId, true);
1963 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1964 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07001965
Michael Jurka414300a2013-08-27 15:42:35 +02001966 LauncherAppState.getLauncherProvider().updateMaxScreenId(maxScreenId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Winson Chungc763c4e2013-07-19 13:49:06 -07001968
1969 // Update the max item id after we load an old db
1970 long maxItemId = 0;
1971 // If we're importing we use the old screen order.
1972 for (ItemInfo item: sBgItemsIdMap.values()) {
1973 maxItemId = Math.max(maxItemId, item.id);
1974 }
Michael Jurka414300a2013-08-27 15:42:35 +02001975 LauncherAppState.getLauncherProvider().updateMaxItemId(maxItemId);
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 } else {
Winson Chung76828c82013-08-19 15:43:29 -07001977 TreeMap<Integer, Long> orderedScreens = loadWorkspaceScreensDb(mContext);
1978 for (Integer i : orderedScreens.keySet()) {
1979 sBgWorkspaceScreens.add(orderedScreens.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 }
Winson Chung9f9f00b2013-11-15 13:27:00 -08001981 // Log to disk
1982 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
1983 TextUtils.join(", ", sBgWorkspaceScreens), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001984
1985 // Remove any empty screens
Winson Chung933bae62013-08-29 11:42:30 -07001986 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 for (ItemInfo item: sBgItemsIdMap.values()) {
1988 long screenId = item.screenId;
Adam Cohendcd297f2013-06-18 13:13:40 -07001989 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1990 unusedScreens.contains(screenId)) {
1991 unusedScreens.remove(screenId);
1992 }
1993 }
1994
1995 // If there are any empty screens remove them, and update.
1996 if (unusedScreens.size() != 0) {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001997 // Log to disk
1998 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
1999 TextUtils.join(", ", unusedScreens), true);
2000
Winson Chung933bae62013-08-29 11:42:30 -07002001 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
2003 }
2004 }
2005
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002006 if (DEBUG_LOADERS) {
2007 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2008 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002009 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002010 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002011 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002012
Daniel Sandler566da102013-06-25 23:43:45 -04002013 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002014 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002016 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002017 line += " | ";
2018 }
Winson Chung892c74d2013-08-22 16:15:50 -07002019 for (int x = 0; x < countX; x++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002020 line += ((occupied.get(screenId)[x][y] != null) ? "#" : ".");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002021 }
Joe Onorato36115782010-06-17 13:28:48 -04002022 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002023 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002024 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025 }
Joe Onorato36115782010-06-17 13:28:48 -04002026 }
Winson Chungc763c4e2013-07-19 13:49:06 -07002027 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -07002028 }
2029
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002030 /** Filters the set of items who are directly or indirectly (via another container) on the
2031 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002032 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002033 ArrayList<ItemInfo> allWorkspaceItems,
2034 ArrayList<ItemInfo> currentScreenItems,
2035 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002036 // Purge any null ItemInfos
2037 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2038 while (iter.hasNext()) {
2039 ItemInfo i = iter.next();
2040 if (i == null) {
2041 iter.remove();
2042 }
2043 }
2044
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002045 // Order the set of items by their containers first, this allows use to walk through the
2046 // list sequentially, build up a list of containers that are in the specified screen,
2047 // as well as all items in those containers.
2048 Set<Long> itemsOnScreen = new HashSet<Long>();
2049 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2050 @Override
2051 public int compare(ItemInfo lhs, ItemInfo rhs) {
2052 return (int) (lhs.container - rhs.container);
2053 }
2054 });
2055 for (ItemInfo info : allWorkspaceItems) {
2056 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002057 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002058 currentScreenItems.add(info);
2059 itemsOnScreen.add(info.id);
2060 } else {
2061 otherScreenItems.add(info);
2062 }
2063 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2064 currentScreenItems.add(info);
2065 itemsOnScreen.add(info.id);
2066 } else {
2067 if (itemsOnScreen.contains(info.container)) {
2068 currentScreenItems.add(info);
2069 itemsOnScreen.add(info.id);
2070 } else {
2071 otherScreenItems.add(info);
2072 }
2073 }
2074 }
2075 }
2076
2077 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002078 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002079 ArrayList<LauncherAppWidgetInfo> appWidgets,
2080 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2081 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002082
2083 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002084 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002085 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002086 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 currentScreenWidgets.add(widget);
2088 } else {
2089 otherScreenWidgets.add(widget);
2090 }
2091 }
2092 }
2093
2094 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002095 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002096 HashMap<Long, ItemInfo> itemsIdMap,
2097 HashMap<Long, FolderInfo> folders,
2098 HashMap<Long, FolderInfo> currentScreenFolders,
2099 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002100
2101 for (long id : folders.keySet()) {
2102 ItemInfo info = itemsIdMap.get(id);
2103 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002104 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002105 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002106 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002107 currentScreenFolders.put(id, folder);
2108 } else {
2109 otherScreenFolders.put(id, folder);
2110 }
2111 }
2112 }
2113
2114 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2115 * right) */
2116 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002117 final LauncherAppState app = LauncherAppState.getInstance();
2118 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002119 // XXX: review this
2120 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002121 @Override
2122 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002123 int cellCountX = (int) grid.numColumns;
2124 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002125 int screenOffset = cellCountX * cellCountY;
2126 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002127 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002128 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002129 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002130 rhs.cellY * cellCountX + rhs.cellX);
2131 return (int) (lr - rr);
2132 }
2133 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002134 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002135
Adam Cohendcd297f2013-06-18 13:13:40 -07002136 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2137 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002138 final Runnable r = new Runnable() {
2139 @Override
2140 public void run() {
2141 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2142 if (callbacks != null) {
2143 callbacks.bindScreens(orderedScreens);
2144 }
2145 }
2146 };
2147 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2148 }
2149
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2151 final ArrayList<ItemInfo> workspaceItems,
2152 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2153 final HashMap<Long, FolderInfo> folders,
2154 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002155
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002156 final boolean postOnMainThread = (deferredBindRunnables != null);
2157
2158 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002159 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002160 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002161 final int start = i;
2162 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002163 final Runnable r = new Runnable() {
2164 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002166 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002168 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2169 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 }
2171 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002172 };
2173 if (postOnMainThread) {
2174 deferredBindRunnables.add(r);
2175 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002176 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002177 }
Joe Onorato36115782010-06-17 13:28:48 -04002178 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002179
2180 // Bind the folders
2181 if (!folders.isEmpty()) {
2182 final Runnable r = new Runnable() {
2183 public void run() {
2184 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2185 if (callbacks != null) {
2186 callbacks.bindFolders(folders);
2187 }
2188 }
2189 };
2190 if (postOnMainThread) {
2191 deferredBindRunnables.add(r);
2192 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002193 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 }
2195 }
2196
2197 // Bind the widgets, one at a time
2198 N = appWidgets.size();
2199 for (int i = 0; i < N; i++) {
2200 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2201 final Runnable r = new Runnable() {
2202 public void run() {
2203 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2204 if (callbacks != null) {
2205 callbacks.bindAppWidget(widget);
2206 }
2207 }
2208 };
2209 if (postOnMainThread) {
2210 deferredBindRunnables.add(r);
2211 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002212 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002213 }
2214 }
2215 }
2216
2217 /**
2218 * Binds all loaded data to actual views on the main thread.
2219 */
Winson Chungc763c4e2013-07-19 13:49:06 -07002220 private void bindWorkspace(int synchronizeBindPage, final boolean isUpgradePath) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002221 final long t = SystemClock.uptimeMillis();
2222 Runnable r;
2223
2224 // Don't use these two variables in any of the callback runnables.
2225 // Otherwise we hold a reference to them.
2226 final Callbacks oldCallbacks = mCallbacks.get();
2227 if (oldCallbacks == null) {
2228 // This launcher has exited and nobody bothered to tell us. Just bail.
2229 Log.w(TAG, "LoaderTask running with no launcher");
2230 return;
2231 }
2232
Winson Chung9b9fb962013-11-15 15:39:34 -08002233 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002234 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2235 ArrayList<LauncherAppWidgetInfo> appWidgets =
2236 new ArrayList<LauncherAppWidgetInfo>();
2237 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2238 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002240 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002241 workspaceItems.addAll(sBgWorkspaceItems);
2242 appWidgets.addAll(sBgAppWidgets);
2243 folders.putAll(sBgFolders);
2244 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002246 }
2247
Winson Chung9b9fb962013-11-15 15:39:34 -08002248 final boolean isLoadingSynchronously = (synchronizeBindPage > -1);
Adam Cohend8dbb462013-11-27 11:55:48 -08002249 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002250 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002251 if (currScreen >= orderedScreenIds.size()) {
2252 // There may be no workspace screens (just hotseat items and an empty page).
2253 currScreen = -1;
Winson Chung9b9fb962013-11-15 15:39:34 -08002254 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002255 final int currentScreen = currScreen;
2256 final long currentScreenId =
2257 currentScreen < 0 ? -1 : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002258
2259 // Load all the items that are on the current page first (and in the process, unbind
2260 // all the existing workspace items before we call startBinding() below.
2261 unbindWorkspaceItemsOnMainThread();
2262
2263 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002264 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2265 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2266 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2267 new ArrayList<LauncherAppWidgetInfo>();
2268 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2269 new ArrayList<LauncherAppWidgetInfo>();
2270 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2271 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2272
Winson Chung9b9fb962013-11-15 15:39:34 -08002273 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002274 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002275 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002276 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002277 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002278 otherFolders);
2279 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2280 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2281
2282 // Tell the workspace that we're about to start binding items
2283 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002284 public void run() {
2285 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2286 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002288 }
2289 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002290 };
Winson Chung81b52252012-08-27 15:34:29 -07002291 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292
Adam Cohendcd297f2013-06-18 13:13:40 -07002293 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2294
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 // Load items on the current page
2296 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2297 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002298 if (isLoadingSynchronously) {
2299 r = new Runnable() {
2300 public void run() {
2301 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Adam Cohenb0ee0812013-12-03 10:51:45 -08002302 if (callbacks != null && currentScreen >= 0) {
Adam Cohen1462de32012-07-24 22:34:36 -07002303 callbacks.onPageBoundSynchronously(currentScreen);
2304 }
2305 }
2306 };
Winson Chung81b52252012-08-27 15:34:29 -07002307 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002308 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002309
Winson Chung4a2afa32012-07-19 14:53:05 -07002310 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2311 // work until after the first render)
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002312 mDeferredBindRunnables.clear();
2313 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002314 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315
2316 // Tell the workspace that we're done binding items
2317 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002318 public void run() {
2319 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2320 if (callbacks != null) {
Winson Chungc763c4e2013-07-19 13:49:06 -07002321 callbacks.finishBindingItems(isUpgradePath);
Joe Onorato36115782010-06-17 13:28:48 -04002322 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323
Winson Chung98e030b2012-05-07 16:01:11 -07002324 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002325 if (DEBUG_LOADERS) {
2326 Log.d(TAG, "bound workspace in "
2327 + (SystemClock.uptimeMillis()-t) + "ms");
2328 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002329
2330 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002331 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002332 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002333 if (isLoadingSynchronously) {
2334 mDeferredBindRunnables.add(r);
2335 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002336 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002337 }
Joe Onorato36115782010-06-17 13:28:48 -04002338 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002339
Joe Onorato36115782010-06-17 13:28:48 -04002340 private void loadAndBindAllApps() {
2341 if (DEBUG_LOADERS) {
2342 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2343 }
2344 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002345 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002346 synchronized (LoaderTask.this) {
2347 if (mStopped) {
2348 return;
2349 }
2350 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002351 }
Joe Onorato36115782010-06-17 13:28:48 -04002352 } else {
2353 onlyBindAllApps();
2354 }
2355 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002356
Joe Onorato36115782010-06-17 13:28:48 -04002357 private void onlyBindAllApps() {
2358 final Callbacks oldCallbacks = mCallbacks.get();
2359 if (oldCallbacks == null) {
2360 // This launcher has exited and nobody bothered to tell us. Just bail.
2361 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2362 return;
2363 }
2364
2365 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002366 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002367 final ArrayList<AppInfo> list
2368 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002369 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002370 public void run() {
2371 final long t = SystemClock.uptimeMillis();
2372 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2373 if (callbacks != null) {
2374 callbacks.bindAllApplications(list);
2375 }
2376 if (DEBUG_LOADERS) {
2377 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2378 + (SystemClock.uptimeMillis()-t) + "ms");
2379 }
2380 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002381 };
2382 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002383 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002384 r.run();
2385 } else {
2386 mHandler.post(r);
2387 }
Joe Onorato36115782010-06-17 13:28:48 -04002388 }
2389
Winson Chung64359a52013-07-08 17:17:08 -07002390 private void loadAllApps() {
2391 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002392
Joe Onorato36115782010-06-17 13:28:48 -04002393 final Callbacks oldCallbacks = mCallbacks.get();
2394 if (oldCallbacks == null) {
2395 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002396 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002397 return;
2398 }
2399
Winson Chung64359a52013-07-08 17:17:08 -07002400 final PackageManager packageManager = mContext.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04002401 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2402 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2403
Winson Chung64359a52013-07-08 17:17:08 -07002404 // Clear the list of apps
2405 mBgAllAppsList.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002406
Winson Chung64359a52013-07-08 17:17:08 -07002407 // Query for the set of apps
2408 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2409 List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
2410 if (DEBUG_LOADERS) {
2411 Log.d(TAG, "queryIntentActivities took "
2412 + (SystemClock.uptimeMillis()-qiaTime) + "ms");
2413 Log.d(TAG, "queryIntentActivities got " + apps.size() + " apps");
2414 }
2415 // Fail if we don't have any apps
2416 if (apps == null || apps.isEmpty()) {
2417 return;
2418 }
2419 // Sort the applications by name
2420 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2421 Collections.sort(apps,
2422 new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
2423 if (DEBUG_LOADERS) {
2424 Log.d(TAG, "sort took "
2425 + (SystemClock.uptimeMillis()-sortTime) + "ms");
Joe Onorato9c1289c2009-08-17 11:03:03 -04002426 }
2427
Winson Chung64359a52013-07-08 17:17:08 -07002428 // Create the ApplicationInfos
Winson Chung64359a52013-07-08 17:17:08 -07002429 for (int i = 0; i < apps.size(); i++) {
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002430 ResolveInfo app = apps.get(i);
Bjorn Bringert1307f632013-10-03 22:31:03 +01002431 // This builds the icon bitmaps.
2432 mBgAllAppsList.add(new AppInfo(packageManager, app,
2433 mIconCache, mLabelCache));
Winson Chung64359a52013-07-08 17:17:08 -07002434 }
2435
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002436 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002437 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2438 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002439
2440 // Post callback on main thread
2441 mHandler.post(new Runnable() {
2442 public void run() {
2443 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002444 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002445 if (callbacks != null) {
2446 callbacks.bindAllApplications(added);
2447 if (DEBUG_LOADERS) {
2448 Log.d(TAG, "bound " + added.size() + " apps in "
2449 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2450 }
2451 } else {
2452 Log.i(TAG, "not binding apps: no Launcher activity");
2453 }
2454 }
2455 });
2456
Joe Onorato36115782010-06-17 13:28:48 -04002457 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002458 Log.d(TAG, "Icons processed in "
2459 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002460 }
2461 }
2462
2463 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002464 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002465 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2466 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2467 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2468 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2469 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2470 }
Joe Onorato36115782010-06-17 13:28:48 -04002471 }
2472 }
2473
2474 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002475 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002476 }
2477
2478 private class PackageUpdatedTask implements Runnable {
2479 int mOp;
2480 String[] mPackages;
2481
2482 public static final int OP_NONE = 0;
2483 public static final int OP_ADD = 1;
2484 public static final int OP_UPDATE = 2;
2485 public static final int OP_REMOVE = 3; // uninstlled
2486 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2487
2488
2489 public PackageUpdatedTask(int op, String[] packages) {
2490 mOp = op;
2491 mPackages = packages;
2492 }
2493
2494 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002495 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002496
2497 final String[] packages = mPackages;
2498 final int N = packages.length;
2499 switch (mOp) {
2500 case OP_ADD:
2501 for (int i=0; i<N; i++) {
2502 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002503 mBgAllAppsList.addPackage(context, packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002504 }
2505 break;
2506 case OP_UPDATE:
2507 for (int i=0; i<N; i++) {
2508 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002509 mBgAllAppsList.updatePackage(context, packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002510 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002511 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002512 }
2513 break;
2514 case OP_REMOVE:
2515 case OP_UNAVAILABLE:
2516 for (int i=0; i<N; i++) {
2517 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Adam Cohen487f7dd2012-06-28 18:12:10 -07002518 mBgAllAppsList.removePackage(packages[i]);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002519 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002520 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002521 }
2522 break;
2523 }
2524
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002525 ArrayList<AppInfo> added = null;
2526 ArrayList<AppInfo> modified = null;
2527 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04002528
Adam Cohen487f7dd2012-06-28 18:12:10 -07002529 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002530 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07002531 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002532 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07002533 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002534 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07002535 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04002536 }
Winson Chung5d55f332012-07-16 20:45:03 -07002537 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07002538 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07002539 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07002540 }
2541
Joe Onorato36115782010-06-17 13:28:48 -04002542 final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null;
2543 if (callbacks == null) {
2544 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
2545 return;
2546 }
2547
2548 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002549 // Ensure that we add all the workspace applications to the db
2550 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
Winson Chung94d67682013-09-25 16:29:40 -07002551 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2552 addAndBindAddedApps(context, new ArrayList<ItemInfo>(), cb, added);
2553 } else {
2554 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
2555 addAndBindAddedApps(context, addedInfos, cb, added);
2556 }
Joe Onorato36115782010-06-17 13:28:48 -04002557 }
2558 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002559 final ArrayList<AppInfo> modifiedFinal = modified;
Winson Chung64359a52013-07-08 17:17:08 -07002560
2561 // Update the launcher db to reflect the changes
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002562 for (AppInfo a : modifiedFinal) {
Winson Chung64359a52013-07-08 17:17:08 -07002563 ArrayList<ItemInfo> infos =
2564 getItemInfoForComponentName(a.componentName);
2565 for (ItemInfo i : infos) {
2566 if (isShortcutInfoUpdateable(i)) {
2567 ShortcutInfo info = (ShortcutInfo) i;
2568 info.title = a.title.toString();
2569 updateItemInDatabase(context, info);
2570 }
2571 }
2572 }
2573
Joe Onorato36115782010-06-17 13:28:48 -04002574 mHandler.post(new Runnable() {
2575 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002576 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2577 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04002578 callbacks.bindAppsUpdated(modifiedFinal);
2579 }
2580 }
2581 });
2582 }
Winson Chung83892cc2013-05-01 16:53:33 -07002583
Winson Chungdf95eb12013-10-16 14:57:07 -07002584 final ArrayList<String> removedPackageNames =
2585 new ArrayList<String>();
2586 if (mOp == OP_REMOVE) {
2587 // Mark all packages in the broadcast to be removed
2588 removedPackageNames.addAll(Arrays.asList(packages));
2589 } else if (mOp == OP_UPDATE) {
2590 // Mark disabled packages in the broadcast to be removed
2591 final PackageManager pm = context.getPackageManager();
2592 for (int i=0; i<N; i++) {
2593 if (isPackageDisabled(pm, packages[i])) {
2594 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07002595 }
2596 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002597 }
2598 // Remove all the components associated with this package
2599 for (String pn : removedPackageNames) {
2600 ArrayList<ItemInfo> infos = getItemInfoForPackageName(pn);
2601 for (ItemInfo i : infos) {
2602 deleteItemFromDatabase(context, i);
2603 }
2604 }
2605 // Remove all the specific components
2606 for (AppInfo a : removedApps) {
2607 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName);
2608 for (ItemInfo i : infos) {
2609 deleteItemFromDatabase(context, i);
2610 }
2611 }
2612 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
2613 // Remove any queued items from the install queue
2614 String spKey = LauncherAppState.getSharedPreferencesKey();
2615 SharedPreferences sp =
2616 context.getSharedPreferences(spKey, Context.MODE_PRIVATE);
2617 InstallShortcutReceiver.removeFromInstallQueue(sp, removedPackageNames);
2618 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04002619 mHandler.post(new Runnable() {
2620 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002621 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2622 if (callbacks == cb && cb != null) {
Winson Chungdf95eb12013-10-16 14:57:07 -07002623 callbacks.bindComponentsRemoved(removedPackageNames, removedApps);
Joe Onorato36115782010-06-17 13:28:48 -04002624 }
2625 }
2626 });
Joe Onoratobe386092009-11-17 17:32:16 -08002627 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002628
Michael Jurkac402cd92013-05-20 15:49:32 +02002629 final ArrayList<Object> widgetsAndShortcuts =
2630 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07002631 mHandler.post(new Runnable() {
2632 @Override
2633 public void run() {
Winson Chungcd2b0142011-06-08 16:02:26 -07002634 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2635 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02002636 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07002637 }
2638 }
2639 });
Adam Cohen4caf2982013-08-20 18:54:31 -07002640
2641 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07002642 mHandler.post(new Runnable() {
2643 public void run() {
2644 Callbacks cb = mCallbacks != null ? mCallbacks.get() : null;
2645 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07002646 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07002647 }
2648 }
2649 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 }
2651 }
2652
Michael Jurkac402cd92013-05-20 15:49:32 +02002653 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
2654 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
2655 PackageManager packageManager = context.getPackageManager();
2656 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
2657 widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders());
2658 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2659 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
2660 Collections.sort(widgetsAndShortcuts,
2661 new LauncherModel.WidgetAndShortcutNameComparator(packageManager));
2662 return widgetsAndShortcuts;
2663 }
2664
Winson Chungdf95eb12013-10-16 14:57:07 -07002665 private boolean isPackageDisabled(PackageManager pm, String packageName) {
2666 try {
2667 PackageInfo pi = pm.getPackageInfo(packageName, 0);
2668 return !pi.applicationInfo.enabled;
2669 } catch (NameNotFoundException e) {
2670 // Fall through
2671 }
2672 return false;
2673 }
Winson Chung1323b482013-08-05 12:41:55 -07002674 private boolean isValidPackageComponent(PackageManager pm, ComponentName cn) {
Winson Chungee055712013-07-30 14:46:24 -07002675 if (cn == null) {
2676 return false;
2677 }
Winson Chungdf95eb12013-10-16 14:57:07 -07002678 if (isPackageDisabled(pm, cn.getPackageName())) {
2679 return false;
2680 }
Winson Chungee055712013-07-30 14:46:24 -07002681
2682 try {
Winson Chungba9c37f2013-08-30 14:11:37 -07002683 // Check the activity
Winson Chungdf95eb12013-10-16 14:57:07 -07002684 PackageInfo pi = pm.getPackageInfo(cn.getPackageName(), 0);
Winson Chungee055712013-07-30 14:46:24 -07002685 return (pm.getActivityInfo(cn, 0) != null);
2686 } catch (NameNotFoundException e) {
2687 return false;
2688 }
2689 }
2690
Joe Onorato9c1289c2009-08-17 11:03:03 -04002691 /**
Joe Onorato56d82912010-03-07 14:32:10 -05002692 * This is called from the code that adds shortcuts from the intent receiver. This
2693 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04002694 */
Joe Onorato56d82912010-03-07 14:32:10 -05002695 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) {
Winson Chungc3eecff2011-07-11 17:44:15 -07002696 return getShortcutInfo(manager, intent, context, null, -1, -1, null);
Joe Onorato56d82912010-03-07 14:32:10 -05002697 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002698
Joe Onorato56d82912010-03-07 14:32:10 -05002699 /**
2700 * Make an ShortcutInfo object for a shortcut that is an application.
2701 *
2702 * If c is not null, then it will be used to fill in missing data like the title and icon.
2703 */
2704 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context,
Winson Chungc3eecff2011-07-11 17:44:15 -07002705 Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) {
Joe Onorato56d82912010-03-07 14:32:10 -05002706 ComponentName componentName = intent.getComponent();
Winson Chung1323b482013-08-05 12:41:55 -07002707 final ShortcutInfo info = new ShortcutInfo();
Winson Chung68fd3c32013-08-30 16:38:00 -07002708 if (componentName != null && !isValidPackageComponent(manager, componentName)) {
Winson Chungee055712013-07-30 14:46:24 -07002709 Log.d(TAG, "Invalid package found in getShortcutInfo: " + componentName);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002710 return null;
Winson Chung1323b482013-08-05 12:41:55 -07002711 } else {
2712 try {
2713 PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0);
2714 info.initFlagsAndFirstInstallTime(pi);
2715 } catch (NameNotFoundException e) {
2716 Log.d(TAG, "getPackInfo failed for package " +
2717 componentName.getPackageName());
2718 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002719 }
2720
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002721 // TODO: See if the PackageManager knows about this case. If it doesn't
2722 // then return null & delete this.
2723
Joe Onorato56d82912010-03-07 14:32:10 -05002724 // the resource -- This may implicitly give us back the fallback icon,
2725 // but don't worry about that. All we're doing with usingFallbackIcon is
2726 // to avoid saving lots of copies of that in the database, and most apps
2727 // have icons anyway.
Winson Chungc208ff92012-03-29 17:37:41 -07002728
2729 // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and
2730 // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info
2731 // via resolveActivity().
Winson Chungee055712013-07-30 14:46:24 -07002732 Bitmap icon = null;
Winson Chungc208ff92012-03-29 17:37:41 -07002733 ResolveInfo resolveInfo = null;
2734 ComponentName oldComponent = intent.getComponent();
2735 Intent newIntent = new Intent(intent.getAction(), null);
2736 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2737 newIntent.setPackage(oldComponent.getPackageName());
2738 List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0);
2739 for (ResolveInfo i : infos) {
2740 ComponentName cn = new ComponentName(i.activityInfo.packageName,
2741 i.activityInfo.name);
2742 if (cn.equals(oldComponent)) {
2743 resolveInfo = i;
2744 }
2745 }
2746 if (resolveInfo == null) {
2747 resolveInfo = manager.resolveActivity(intent, 0);
2748 }
Joe Onorato56d82912010-03-07 14:32:10 -05002749 if (resolveInfo != null) {
Winson Chungaac01e12011-08-17 10:37:13 -07002750 icon = mIconCache.getIcon(componentName, resolveInfo, labelCache);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002751 }
Joe Onorato56d82912010-03-07 14:32:10 -05002752 // the db
2753 if (icon == null) {
2754 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002755 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002756 }
2757 }
2758 // the fallback icon
2759 if (icon == null) {
2760 icon = getFallbackIcon();
2761 info.usingFallbackIcon = true;
2762 }
2763 info.setIcon(icon);
2764
2765 // from the resource
2766 if (resolveInfo != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002767 ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo);
2768 if (labelCache != null && labelCache.containsKey(key)) {
2769 info.title = labelCache.get(key);
Winson Chungc3eecff2011-07-11 17:44:15 -07002770 } else {
2771 info.title = resolveInfo.activityInfo.loadLabel(manager);
2772 if (labelCache != null) {
Winson Chung5308f242011-08-18 12:12:41 -07002773 labelCache.put(key, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07002774 }
2775 }
Joe Onorato56d82912010-03-07 14:32:10 -05002776 }
2777 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04002778 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05002779 if (c != null) {
2780 info.title = c.getString(titleIndex);
2781 }
2782 }
2783 // fall back to the class name of the activity
2784 if (info.title == null) {
2785 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002786 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
2788 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07002790
Winson Chung64359a52013-07-08 17:17:08 -07002791 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
2792 ItemInfoFilter f) {
2793 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
2794 for (ItemInfo i : infos) {
2795 if (i instanceof ShortcutInfo) {
2796 ShortcutInfo info = (ShortcutInfo) i;
2797 ComponentName cn = info.intent.getComponent();
2798 if (cn != null && f.filterItem(null, info, cn)) {
2799 filtered.add(info);
2800 }
2801 } else if (i instanceof FolderInfo) {
2802 FolderInfo info = (FolderInfo) i;
2803 for (ShortcutInfo s : info.contents) {
2804 ComponentName cn = s.intent.getComponent();
2805 if (cn != null && f.filterItem(info, s, cn)) {
2806 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07002807 }
2808 }
Winson Chung64359a52013-07-08 17:17:08 -07002809 } else if (i instanceof LauncherAppWidgetInfo) {
2810 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
2811 ComponentName cn = info.providerName;
2812 if (cn != null && f.filterItem(null, info, cn)) {
2813 filtered.add(info);
2814 }
Winson Chung8a435102012-08-30 17:16:53 -07002815 }
2816 }
Winson Chung64359a52013-07-08 17:17:08 -07002817 return new ArrayList<ItemInfo>(filtered);
2818 }
2819
2820 private ArrayList<ItemInfo> getItemInfoForPackageName(final String pn) {
Winson Chung64359a52013-07-08 17:17:08 -07002821 ItemInfoFilter filter = new ItemInfoFilter() {
2822 @Override
2823 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2824 return cn.getPackageName().equals(pn);
2825 }
2826 };
2827 return filterItemInfos(sBgItemsIdMap.values(), filter);
2828 }
2829
2830 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname) {
Winson Chung64359a52013-07-08 17:17:08 -07002831 ItemInfoFilter filter = new ItemInfoFilter() {
2832 @Override
2833 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
2834 return cn.equals(cname);
2835 }
2836 };
2837 return filterItemInfos(sBgItemsIdMap.values(), filter);
2838 }
2839
2840 public static boolean isShortcutInfoUpdateable(ItemInfo i) {
2841 if (i instanceof ShortcutInfo) {
2842 ShortcutInfo info = (ShortcutInfo) i;
2843 // We need to check for ACTION_MAIN otherwise getComponent() might
2844 // return null for some shortcuts (for instance, for shortcuts to
2845 // web pages.)
2846 Intent intent = info.intent;
2847 ComponentName name = intent.getComponent();
2848 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
2849 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
2850 return true;
2851 }
2852 }
2853 return false;
Winson Chung8a435102012-08-30 17:16:53 -07002854 }
2855
2856 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08002857 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08002859 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05002860 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
2861 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862
Joe Onorato56d82912010-03-07 14:32:10 -05002863 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07002864 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07002867 // TODO: If there's an explicit component and we can't install that, delete it.
2868
Joe Onorato56d82912010-03-07 14:32:10 -05002869 info.title = c.getString(titleIndex);
2870
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 int iconType = c.getInt(iconTypeIndex);
2872 switch (iconType) {
2873 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
2874 String packageName = c.getString(iconPackageIndex);
2875 String resourceName = c.getString(iconResourceIndex);
2876 PackageManager packageManager = context.getPackageManager();
Joe Onorato56d82912010-03-07 14:32:10 -05002877 info.customIcon = false;
2878 // the resource
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 try {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 Resources resources = packageManager.getResourcesForApplication(packageName);
Joe Onorato56d82912010-03-07 14:32:10 -05002881 if (resources != null) {
2882 final int id = resources.getIdentifier(resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07002883 icon = Utilities.createIconBitmap(
2884 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 } catch (Exception e) {
Joe Onorato56d82912010-03-07 14:32:10 -05002887 // drop this. we have other places to look for icons
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 }
Joe Onorato56d82912010-03-07 14:32:10 -05002889 // the db
2890 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07002891 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002892 }
2893 // the fallback icon
2894 if (icon == null) {
2895 icon = getFallbackIcon();
2896 info.usingFallbackIcon = true;
2897 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898 break;
2899 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07002900 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05002901 if (icon == null) {
2902 icon = getFallbackIcon();
2903 info.customIcon = false;
2904 info.usingFallbackIcon = true;
2905 } else {
2906 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 break;
2909 default:
Joe Onoratod8d22da2010-03-11 17:59:11 -08002910 icon = getFallbackIcon();
Joe Onorato56d82912010-03-07 14:32:10 -05002911 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912 info.customIcon = false;
2913 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08002915 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 return info;
2917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918
Michael Jurka931dc972011-08-05 15:08:15 -07002919 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07002920 @SuppressWarnings("all") // suppress dead code warning
2921 final boolean debug = false;
2922 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05002923 Log.d(TAG, "getIconFromCursor app="
2924 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
2925 }
2926 byte[] data = c.getBlob(iconIndex);
2927 try {
Michael Jurka931dc972011-08-05 15:08:15 -07002928 return Utilities.createIconBitmap(
2929 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05002930 } catch (Exception e) {
2931 return null;
2932 }
2933 }
2934
Winson Chung3d503fb2011-07-13 17:25:49 -07002935 ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
2936 int cellX, int cellY, boolean notify) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002937 final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08002938 if (info == null) {
2939 return null;
2940 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002941 addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
Joe Onorato0589f0f2010-02-08 13:44:00 -08002942
2943 return info;
2944 }
2945
Winson Chunga9abd0e2010-10-27 17:18:37 -07002946 /**
Winson Chung55cef262010-10-28 14:14:18 -07002947 * Attempts to find an AppWidgetProviderInfo that matches the given component.
2948 */
2949 AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context,
2950 ComponentName component) {
2951 List<AppWidgetProviderInfo> widgets =
2952 AppWidgetManager.getInstance(context).getInstalledProviders();
2953 for (AppWidgetProviderInfo info : widgets) {
2954 if (info.provider.equals(component)) {
2955 return info;
2956 }
2957 }
2958 return null;
Winson Chunga9abd0e2010-10-27 17:18:37 -07002959 }
2960
Winson Chung68846fd2010-10-29 11:00:27 -07002961 /**
2962 * Returns a list of all the widgets that can handle configuration with a particular mimeType.
2963 */
2964 List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) {
2965 final PackageManager packageManager = context.getPackageManager();
2966 final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities =
2967 new ArrayList<WidgetMimeTypeHandlerData>();
2968
2969 final Intent supportsIntent =
2970 new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE);
2971 supportsIntent.setType(mimeType);
2972
2973 // Create a set of widget configuration components that we can test against
2974 final List<AppWidgetProviderInfo> widgets =
2975 AppWidgetManager.getInstance(context).getInstalledProviders();
2976 final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget =
2977 new HashMap<ComponentName, AppWidgetProviderInfo>();
2978 for (AppWidgetProviderInfo info : widgets) {
2979 configurationComponentToWidget.put(info.configure, info);
2980 }
2981
2982 // Run through each of the intents that can handle this type of clip data, and cross
2983 // reference them with the components that are actual configuration components
2984 final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent,
2985 PackageManager.MATCH_DEFAULT_ONLY);
2986 for (ResolveInfo info : activities) {
2987 final ActivityInfo activityInfo = info.activityInfo;
2988 final ComponentName infoComponent = new ComponentName(activityInfo.packageName,
2989 activityInfo.name);
2990 if (configurationComponentToWidget.containsKey(infoComponent)) {
2991 supportedConfigurationActivities.add(
2992 new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info,
2993 configurationComponentToWidget.get(infoComponent)));
2994 }
2995 }
2996 return supportedConfigurationActivities;
2997 }
2998
Winson Chunga9abd0e2010-10-27 17:18:37 -07002999 ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003000 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3001 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3002 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3003
Adam Cohend9198822011-11-22 16:42:47 -08003004 if (intent == null) {
3005 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3006 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3007 return null;
3008 }
3009
Joe Onorato0589f0f2010-02-08 13:44:00 -08003010 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003011 boolean customIcon = false;
3012 ShortcutIconResource iconResource = null;
3013
3014 if (bitmap != null && bitmap instanceof Bitmap) {
3015 icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003016 customIcon = true;
3017 } else {
3018 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
3019 if (extra != null && extra instanceof ShortcutIconResource) {
3020 try {
3021 iconResource = (ShortcutIconResource) extra;
3022 final PackageManager packageManager = context.getPackageManager();
3023 Resources resources = packageManager.getResourcesForApplication(
3024 iconResource.packageName);
3025 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
Michael Jurkac9a96192010-11-01 11:52:08 -07003026 icon = Utilities.createIconBitmap(
3027 mIconCache.getFullResIcon(resources, id), context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003028 } catch (Exception e) {
3029 Log.w(TAG, "Could not load shortcut icon: " + extra);
3030 }
3031 }
3032 }
3033
Michael Jurkac9d95c52011-08-29 14:03:34 -07003034 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003035
3036 if (icon == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07003037 if (fallbackIcon != null) {
3038 icon = fallbackIcon;
3039 } else {
3040 icon = getFallbackIcon();
3041 info.usingFallbackIcon = true;
3042 }
Joe Onorato56d82912010-03-07 14:32:10 -05003043 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003044 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003045
Joe Onorato0589f0f2010-02-08 13:44:00 -08003046 info.title = name;
3047 info.intent = intent;
3048 info.customIcon = customIcon;
3049 info.iconResource = iconResource;
3050
3051 return info;
3052 }
3053
Winson Chungaac01e12011-08-17 10:37:13 -07003054 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3055 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003056 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003057 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003058 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003059 }
Joe Onorato56d82912010-03-07 14:32:10 -05003060 // If this icon doesn't have a custom icon, check to see
3061 // what's stored in the DB, and if it doesn't match what
3062 // we're going to show, store what we are going to show back
3063 // into the DB. We do this so when we're loading, if the
3064 // package manager can't find an icon (for example because
3065 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003066 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003067 cache.put(info, c.getBlob(iconIndex));
3068 return true;
3069 }
3070 return false;
3071 }
3072 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3073 boolean needSave = false;
3074 try {
3075 if (data != null) {
3076 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3077 Bitmap loaded = info.getIcon(mIconCache);
3078 needSave = !saved.sameAs(loaded);
3079 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003080 needSave = true;
3081 }
Winson Chungaac01e12011-08-17 10:37:13 -07003082 } catch (Exception e) {
3083 needSave = true;
3084 }
3085 if (needSave) {
3086 Log.d(TAG, "going to save icon bitmap for info=" + info);
3087 // This is slower than is ideal, but this only happens once
3088 // or when the app is updated with a new icon.
3089 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003090 }
3091 }
3092
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003094 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 * or make a new one.
3096 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003097 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 // See if a placeholder was created for us already
3099 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003100 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003102 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 folders.put(id, folderInfo);
3104 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003105 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
3107
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003108 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003109 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003110 return new Comparator<AppInfo>() {
3111 public final int compare(AppInfo a, AppInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003112 int result = collator.compare(a.title.toString().trim(),
3113 b.title.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003114 if (result == 0) {
3115 result = a.componentName.compareTo(b.componentName);
3116 }
3117 return result;
Michael Jurka5b1808d2011-07-11 19:59:46 -07003118 }
Winson Chung11904872012-09-17 16:58:46 -07003119 };
3120 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003121 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3122 = new Comparator<AppInfo>() {
3123 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003124 if (a.firstInstallTime < b.firstInstallTime) return 1;
3125 if (a.firstInstallTime > b.firstInstallTime) return -1;
3126 return 0;
3127 }
3128 };
Winson Chung11904872012-09-17 16:58:46 -07003129 public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() {
3130 final Collator collator = Collator.getInstance();
3131 return new Comparator<AppWidgetProviderInfo>() {
3132 public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) {
Winson Chung780fe592013-09-26 14:48:44 -07003133 return collator.compare(a.label.toString().trim(), b.label.toString().trim());
Winson Chung11904872012-09-17 16:58:46 -07003134 }
3135 };
3136 }
Winson Chung5308f242011-08-18 12:12:41 -07003137 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3138 if (info.activityInfo != null) {
3139 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3140 } else {
3141 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3142 }
3143 }
Winson Chung785d2eb2011-04-14 16:08:02 -07003144 public static class ShortcutNameComparator implements Comparator<ResolveInfo> {
Winson Chung11904872012-09-17 16:58:46 -07003145 private Collator mCollator;
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 private PackageManager mPackageManager;
Winson Chungc3eecff2011-07-11 17:44:15 -07003147 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 ShortcutNameComparator(PackageManager pm) {
3149 mPackageManager = pm;
Winson Chungc3eecff2011-07-11 17:44:15 -07003150 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003151 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003152 }
3153 ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) {
3154 mPackageManager = pm;
3155 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003156 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003157 }
3158 public final int compare(ResolveInfo a, ResolveInfo b) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003159 CharSequence labelA, labelB;
Winson Chung5308f242011-08-18 12:12:41 -07003160 ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a);
3161 ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b);
3162 if (mLabelCache.containsKey(keyA)) {
3163 labelA = mLabelCache.get(keyA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003164 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003165 labelA = a.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003166
Winson Chung5308f242011-08-18 12:12:41 -07003167 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003168 }
Winson Chung5308f242011-08-18 12:12:41 -07003169 if (mLabelCache.containsKey(keyB)) {
3170 labelB = mLabelCache.get(keyB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003171 } else {
Winson Chung780fe592013-09-26 14:48:44 -07003172 labelB = b.loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003173
Winson Chung5308f242011-08-18 12:12:41 -07003174 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003175 }
Winson Chung11904872012-09-17 16:58:46 -07003176 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003177 }
3178 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003179 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Winson Chung11904872012-09-17 16:58:46 -07003180 private Collator mCollator;
Winson Chung1ed747a2011-05-03 16:18:34 -07003181 private PackageManager mPackageManager;
3182 private HashMap<Object, String> mLabelCache;
3183 WidgetAndShortcutNameComparator(PackageManager pm) {
3184 mPackageManager = pm;
3185 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003186 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003187 }
3188 public final int compare(Object a, Object b) {
3189 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003190 if (mLabelCache.containsKey(a)) {
3191 labelA = mLabelCache.get(a);
3192 } else {
3193 labelA = (a instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003194 ((AppWidgetProviderInfo) a).label :
Winson Chung780fe592013-09-26 14:48:44 -07003195 ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003196 mLabelCache.put(a, labelA);
3197 }
3198 if (mLabelCache.containsKey(b)) {
3199 labelB = mLabelCache.get(b);
3200 } else {
3201 labelB = (b instanceof AppWidgetProviderInfo) ?
Winson Chung1ed747a2011-05-03 16:18:34 -07003202 ((AppWidgetProviderInfo) b).label :
Winson Chung780fe592013-09-26 14:48:44 -07003203 ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003204 mLabelCache.put(b, labelB);
3205 }
Winson Chung11904872012-09-17 16:58:46 -07003206 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003207 }
3208 };
Joe Onoratobe386092009-11-17 17:32:16 -08003209
3210 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003211 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003212 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3213 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3214 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3215 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003216 if (mLoaderTask != null) {
3217 mLoaderTask.dumpState();
3218 } else {
3219 Log.d(TAG, "mLoaderTask=null");
3220 }
Joe Onoratobe386092009-11-17 17:32:16 -08003221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222}