blob: f1f7a3542946be96e39278c0f0ceaffd0a3bce84 [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.AppWidgetProviderInfo;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentProviderClient;
24import android.content.ContentProviderOperation;
25import android.content.ContentResolver;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Joe Onorato0589f0f2010-02-08 13:44:00 -080029import android.content.Intent.ShortcutIconResource;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070030import android.content.IntentFilter;
31import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.content.pm.PackageManager;
Jason Monkbbe1e242014-05-16 17:37:34 -040033import android.content.pm.ProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ResolveInfo;
Reena Lee93f824a2011-09-23 17:20:28 -070035import android.content.res.Configuration;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.Resources;
37import android.database.Cursor;
38import android.graphics.Bitmap;
39import android.graphics.BitmapFactory;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080040import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.net.Uri;
Joe Onorato17a89222011-02-08 17:26:11 -080042import android.os.Environment;
Joe Onorato36115782010-06-17 13:28:48 -040043import android.os.Handler;
44import android.os.HandlerThread;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Process;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.os.RemoteException;
Joe Onorato9c1289c2009-08-17 11:03:03 -040048import android.os.SystemClock;
Chris Wrenc3919c02013-09-18 09:48:33 -040049import android.provider.BaseColumns;
Winson Chunga90303b2013-11-15 13:05:06 -080050import android.text.TextUtils;
Winson Chungaafa03c2010-06-11 17:34:16 -070051import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080052import android.util.LongSparseArray;
Winson Chungc9168342013-06-26 14:54:55 -070053import android.util.Pair;
Michael Jurka34c2e6c2013-12-13 16:07:45 +010054
Sunny Goyalffe83f12014-08-14 17:39:34 -070055import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010056import com.android.launcher3.compat.LauncherActivityInfoCompat;
57import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal34942622014-08-29 17:20:55 -070058import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070059import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010060import com.android.launcher3.compat.UserHandleCompat;
61import com.android.launcher3.compat.UserManagerCompat;
Romain Guyedcce092010-03-04 13:03:17 -080062
Michael Jurkac2f801e2011-07-12 14:19:46 -070063import java.lang.ref.WeakReference;
64import java.net.URISyntaxException;
Sunny Goyal34942622014-08-29 17:20:55 -070065import java.security.InvalidParameterException;
Michael Jurkac2f801e2011-07-12 14:19:46 -070066import java.text.Collator;
67import java.util.ArrayList;
Adam Cohendcd297f2013-06-18 13:13:40 -070068import java.util.Arrays;
Winson Chung64359a52013-07-08 17:17:08 -070069import java.util.Collection;
Michael Jurkac2f801e2011-07-12 14:19:46 -070070import java.util.Collections;
71import java.util.Comparator;
72import java.util.HashMap;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070073import java.util.HashSet;
Winson Chung2abf94d2012-07-18 18:16:38 -070074import java.util.Iterator;
Michael Jurkac2f801e2011-07-12 14:19:46 -070075import java.util.List;
Sunny Goyalf599ccf2014-07-08 13:01:29 -070076import java.util.Map.Entry;
Winson Chungb8b2a5a2012-07-12 17:55:31 -070077import java.util.Set;
Michael Jurkac2f801e2011-07-12 14:19:46 -070078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079/**
80 * Maintains in-memory state of the Launcher. It is expected that there should be only one
81 * LauncherModel object held in a static. Also provide APIs for updating the database state
The Android Open Source Projectbc219c32009-03-09 11:52:14 -070082 * for the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 */
Kenny Guyed131872014-04-30 03:02:21 +010084public class LauncherModel extends BroadcastReceiver
Kenny Guyc2bd8102014-06-30 12:30:31 +010085 implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080086 static final boolean DEBUG_LOADERS = false;
Chris Wrenee523362014-09-09 10:09:02 -040087 private static final boolean DEBUG_RECEIVER = false;
Sunny Goyal94485362014-09-18 16:13:58 -070088 private static final boolean REMOVE_UNRESTORED_ICONS = true;
Sunny Goyal2a66bbf2014-11-20 17:01:00 -080089 private static final boolean ADD_MANAGED_PROFILE_SHORTCUTS = false;
Chris Wrenb358f812014-04-16 13:37:00 -040090
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 static final String TAG = "Launcher.Model";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070092
Dan Sandlerd5024042014-01-09 15:01:33 -050093 public static final int LOADER_FLAG_NONE = 0;
94 public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0;
95 public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1;
96
Joe Onorato36115782010-06-17 13:28:48 -040097 private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
Derek Prothro7aff3992013-12-10 14:00:37 -050098 private static final long INVALID_SCREEN_ID = -1L;
Winson Chunga6945242014-01-08 14:04:34 -080099
Winson Chungee055712013-07-30 14:46:24 -0700100 private final boolean mAppsCanBeOnRemoveableStorage;
Winson Chunga6945242014-01-08 14:04:34 -0800101 private final boolean mOldContentProviderExists;
Daniel Sandlerdca66122010-04-13 16:23:58 -0400102
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400103 private final LauncherAppState mApp;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 private final Object mLock = new Object();
105 private DeferredHandler mHandler = new DeferredHandler();
Joe Onorato36115782010-06-17 13:28:48 -0400106 private LoaderTask mLoaderTask;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700107 private boolean mIsLoaderTaskRunning;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Sunny Goyale0f58d72014-11-10 18:05:31 -0800109 /**
110 * Maintain a set of packages per user, for which we added a shortcut on the workspace.
111 */
112 private static final String INSTALLED_SHORTCUTS_SET_PREFIX = "installed_shortcuts_set_for_user_";
113
Winson Chung81b52252012-08-27 15:34:29 -0700114 // Specific runnable types that are run on the main thread deferred handler, this allows us to
115 // clear all queued binding runnables when the Launcher activity is destroyed.
116 private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0;
117 private static final int MAIN_THREAD_BINDING_RUNNABLE = 1;
118
Jason Monkbbe1e242014-05-16 17:37:34 -0400119 private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings";
Winson Chung81b52252012-08-27 15:34:29 -0700120
Brad Fitzpatrick700889f2010-10-11 09:40:44 -0700121 private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
122 static {
123 sWorkerThread.start();
124 }
125 private static final Handler sWorker = new Handler(sWorkerThread.getLooper());
126
Joe Onoratocc67f472010-06-08 10:54:30 -0700127 // We start off with everything not loaded. After that, we assume that
128 // our monitoring of the package manager provides all updates and we never
129 // need to do a requery. These are only ever touched from the loader thread.
130 private boolean mWorkspaceLoaded;
131 private boolean mAllAppsLoaded;
132
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700133 // When we are loading pages synchronously, we can't just post the binding of items on the side
134 // pages as this delays the rotation process. Instead, we wait for a callback from the first
135 // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start
136 // a normal load, we also clear this set of Runnables.
137 static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>();
138
Joe Onorato9c1289c2009-08-17 11:03:03 -0400139 private WeakReference<Callbacks> mCallbacks;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 // < only access in worker thread >
Adam Cohen4caf2982013-08-20 18:54:31 -0700142 AllAppsList mBgAllAppsList;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800143
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700144 // The lock that must be acquired before referencing any static bg data structures. Unlike
145 // other locks, this one can generally be held long-term because we never expect any of these
146 // static data structures to be referenced outside of the worker thread except on the first
147 // load after configuration change.
Winson Chung2abf94d2012-07-18 18:16:38 -0700148 static final Object sBgLock = new Object();
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700149
Adam Cohen487f7dd2012-06-28 18:12:10 -0700150 // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700151 // LauncherModel to their ids
Adam Cohen487f7dd2012-06-28 18:12:10 -0700152 static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153
Adam Cohen487f7dd2012-06-28 18:12:10 -0700154 // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts
155 // created by LauncherModel that are directly on the home screen (however, no widgets or
156 // shortcuts within folders).
157 static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>();
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700158
Adam Cohen487f7dd2012-06-28 18:12:10 -0700159 // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget()
160 static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets =
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700161 new ArrayList<LauncherAppWidgetInfo>();
162
Adam Cohen487f7dd2012-06-28 18:12:10 -0700163 // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders()
164 static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();
Winson Chungb1094bd2011-08-24 16:14:08 -0700165
Adam Cohen487f7dd2012-06-28 18:12:10 -0700166 // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database
167 static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>();
Adam Cohendcd297f2013-06-18 13:13:40 -0700168
169 // sBgWorkspaceScreens is the ordered set of workspace screens.
170 static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>();
171
Adam Cohen59400422014-03-05 18:07:04 -0800172 // sBgWidgetProviders is the set of widget providers including custom internal widgets
173 public static HashMap<ComponentName, LauncherAppWidgetProviderInfo> sBgWidgetProviders;
174 public static boolean sWidgetProvidersDirty;
175
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700176 // sPendingPackages is a set of packages which could be on sdcard and are not available yet
Sameer Padala513edae2014-07-29 16:17:08 -0700177 static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages =
178 new HashMap<UserHandleCompat, HashSet<String>>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -0700179
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700180 // </ only access in worker thread >
181
182 private IconCache mIconCache;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Reena Lee99a73f32011-10-24 17:27:37 -0700184 protected int mPreviousConfigMcc;
Reena Lee93f824a2011-09-23 17:20:28 -0700185
Kenny Guyed131872014-04-30 03:02:21 +0100186 private final LauncherAppsCompat mLauncherApps;
187 private final UserManagerCompat mUserManager;
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 public interface Callbacks {
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700190 public boolean setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 public int getCurrentWorkspaceScreen();
192 public void startBinding();
Winson Chung64359a52013-07-08 17:17:08 -0700193 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end,
194 boolean forceAnimateIcons);
Adam Cohendcd297f2013-06-18 13:13:40 -0700195 public void bindScreens(ArrayList<Long> orderedScreenIds);
Winson Chung64359a52013-07-08 17:17:08 -0700196 public void bindAddScreens(ArrayList<Long> orderedScreenIds);
Joe Onoratoad72e172009-11-06 16:25:04 -0500197 public void bindFolders(HashMap<Long,FolderInfo> folders);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800198 public void finishBindingItems();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 public void bindAppWidget(LauncherAppWidgetInfo info);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200200 public void bindAllApplications(ArrayList<AppInfo> apps);
Winson Chungd64d1762013-08-20 14:37:16 -0700201 public void bindAppsAdded(ArrayList<Long> newScreens,
202 ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -0700203 ArrayList<ItemInfo> addAnimated,
204 ArrayList<AppInfo> addedApps);
Michael Jurkaeadbfc52013-09-04 00:45:37 +0200205 public void bindAppsUpdated(ArrayList<AppInfo> apps);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700206 public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated,
207 ArrayList<ShortcutInfo> removed, UserHandleCompat user);
208 public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets);
Sunny Goyale755d462014-07-22 13:48:29 -0700209 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo);
Sunny Goyala22666f2014-09-18 13:25:15 -0700210 public void updatePackageBadge(String packageName);
Winson Chung83892cc2013-05-01 16:53:33 -0700211 public void bindComponentsRemoved(ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -0700212 ArrayList<AppInfo> appInfos, UserHandleCompat user, int reason);
Michael Jurkac402cd92013-05-20 15:49:32 +0200213 public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts);
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100214 public void bindSearchablesChanged();
Winson Chunga0b7e862013-09-05 16:03:15 -0700215 public boolean isAllAppsButtonRank(int rank);
Adam Cohen1462de32012-07-24 22:34:36 -0700216 public void onPageBoundSynchronously(int page);
Winson Chungede41292013-09-19 16:27:36 -0700217 public void dumpLogsToLocalData();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800218 public void bindAddPendingItem(PendingAddItemInfo info, long container, long screenId,
219 int[] cell, int spanX, int spanY);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
Winson Chung64359a52013-07-08 17:17:08 -0700222 public interface ItemInfoFilter {
223 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn);
224 }
225
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800226 public interface ScreenPosProvider {
227 int getScreenIndex(ArrayList<Long> screenIDs);
228 }
229
Bjorn Bringert1307f632013-10-03 22:31:03 +0100230 LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
Winson Chunga6945242014-01-08 14:04:34 -0800231 Context context = app.getContext();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400232
Winson Chungee055712013-07-30 14:46:24 -0700233 mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable();
Adam Cohen71483f42014-05-15 14:04:01 -0700234 String oldProvider = context.getString(R.string.old_launcher_provider_uri);
Jason Monkbbe1e242014-05-16 17:37:34 -0400235 // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different
236 // resource string.
237 String redirectAuthority = Uri.parse(oldProvider).getAuthority();
238 ProviderInfo providerInfo =
239 context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0);
240 ProviderInfo redirectProvider =
241 context.getPackageManager().resolveContentProvider(redirectAuthority, 0);
Adam Cohen71483f42014-05-15 14:04:01 -0700242
243 Log.d(TAG, "Old launcher provider: " + oldProvider);
Jason Monkbbe1e242014-05-16 17:37:34 -0400244 mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null);
Adam Cohen71483f42014-05-15 14:04:01 -0700245
246 if (mOldContentProviderExists) {
247 Log.d(TAG, "Old launcher provider exists.");
248 } else {
249 Log.d(TAG, "Old launcher provider does not exist.");
250 }
251
Daniel Sandlere4f98912013-06-25 15:13:26 -0400252 mApp = app;
Bjorn Bringert1307f632013-10-03 22:31:03 +0100253 mBgAllAppsList = new AllAppsList(iconCache, appFilter);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 mIconCache = iconCache;
255
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400256 final Resources res = context.getResources();
Reena Lee99a73f32011-10-24 17:27:37 -0700257 Configuration config = res.getConfiguration();
258 mPreviousConfigMcc = config.mcc;
Kenny Guyed131872014-04-30 03:02:21 +0100259 mLauncherApps = LauncherAppsCompat.getInstance(context);
260 mUserManager = UserManagerCompat.getInstance(context);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800261 }
262
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700263 /** Runs the specified runnable immediately if called from the main thread, otherwise it is
264 * posted on the main thread handler. */
265 private void runOnMainThread(Runnable r) {
Winson Chung81b52252012-08-27 15:34:29 -0700266 runOnMainThread(r, 0);
267 }
268 private void runOnMainThread(Runnable r, int type) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700269 if (sWorkerThread.getThreadId() == Process.myTid()) {
270 // If we are on the worker thread, post onto the main handler
271 mHandler.post(r);
272 } else {
273 r.run();
274 }
275 }
276
277 /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
278 * posted on the worker thread handler. */
279 private static void runOnWorkerThread(Runnable r) {
280 if (sWorkerThread.getThreadId() == Process.myTid()) {
281 r.run();
282 } else {
283 // If we are not on the worker thread, then post to the worker handler
284 sWorker.post(r);
285 }
286 }
287
Winson Chunge43a1e72014-01-15 10:33:02 -0800288 boolean canMigrateFromOldLauncherDb(Launcher launcher) {
289 return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ;
Winson Chunga6945242014-01-08 14:04:34 -0800290 }
291
Sunny Goyale755d462014-07-22 13:48:29 -0700292 public void setPackageState(final ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500293 // Process the updated package state
294 Runnable r = new Runnable() {
295 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800296 Callbacks callbacks = getCallback();
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500297 if (callbacks != null) {
Sunny Goyale755d462014-07-22 13:48:29 -0700298 callbacks.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500299 }
300 }
301 };
302 mHandler.post(r);
303 }
304
Sunny Goyala22666f2014-09-18 13:25:15 -0700305 public void updatePackageBadge(final String packageName) {
306 // Process the updated package badge
307 Runnable r = new Runnable() {
308 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800309 Callbacks callbacks = getCallback();
Sunny Goyala22666f2014-09-18 13:25:15 -0700310 if (callbacks != null) {
311 callbacks.updatePackageBadge(packageName);
312 }
313 }
314 };
315 mHandler.post(r);
316 }
317
Adam Cohen76a47a12014-02-05 11:47:43 -0800318 public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800319 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800320
321 if (allAppsApps == null) {
322 throw new RuntimeException("allAppsApps must not be null");
323 }
324 if (allAppsApps.isEmpty()) {
325 return;
326 }
327
328 // Process the newly added applications and add them to the database first
329 Runnable r = new Runnable() {
330 public void run() {
331 runOnMainThread(new Runnable() {
332 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800333 Callbacks cb = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800334 if (callbacks == cb && cb != null) {
Chris Wren6d0dde02014-02-10 12:16:54 -0500335 callbacks.bindAppsAdded(null, null, null, allAppsApps);
Adam Cohen76a47a12014-02-05 11:47:43 -0800336 }
337 }
338 });
339 }
340 };
341 runOnWorkerThread(r);
Winson Chung997a9232013-07-24 15:33:46 -0700342 }
Adam Cohen76a47a12014-02-05 11:47:43 -0800343
344 public void addAndBindAddedWorkspaceApps(final Context context,
345 final ArrayList<ItemInfo> workspaceApps) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800346 addAndBindAddedWorkspaceApps(context, workspaceApps,
347 new ScreenPosProvider() {
Adam Cohen76a47a12014-02-05 11:47:43 -0800348
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800349 @Override
350 public int getScreenIndex(ArrayList<Long> screenIDs) {
351 return screenIDs.isEmpty() ? 0 : 1;
352 }
353 }, 1, false);
354 }
355
356 private static boolean findNextAvailableIconSpaceInScreen(ArrayList<Rect> occupiedPos,
357 int[] xy, int spanX, int spanY) {
358 LauncherAppState app = LauncherAppState.getInstance();
359 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
360 final int xCount = (int) grid.numColumns;
361 final int yCount = (int) grid.numRows;
362 boolean[][] occupied = new boolean[xCount][yCount];
363 if (occupiedPos != null) {
364 for (Rect r : occupiedPos) {
365 for (int x = r.left; 0 <= x && x < r.right && x < xCount; x++) {
366 for (int y = r.top; 0 <= y && y < r.bottom && y < yCount; y++) {
367 occupied[x][y] = true;
368 }
369 }
370 }
Winson Chungfe9d96a2013-11-14 11:30:05 -0800371 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800372 return CellLayout.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied);
373 }
374
375 /**
376 * Find a position on the screen for the given size or adds a new screen.
377 * @return screenId and the coordinates for the item.
378 */
379 private static Pair<Long, int[]> findSpaceForItem(
380 Context context,
381 ScreenPosProvider preferredScreen,
382 int fallbackStartScreen,
383 ArrayList<Long> workspaceScreens,
384 ArrayList<Long> addedWorkspaceScreensFinal,
385 int spanX, int spanY) {
386 // Load position of items which are on the desktop. We can't use sBgItemsIdMap because
387 // loadWorkspace() may not have been called.
388 final ContentResolver cr = context.getContentResolver();
389 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
390 new String[] {
391 LauncherSettings.Favorites.SCREEN,
392 LauncherSettings.Favorites.CELLX,
393 LauncherSettings.Favorites.CELLY,
394 LauncherSettings.Favorites.SPANX,
395 LauncherSettings.Favorites.SPANY,
396 LauncherSettings.Favorites.CONTAINER
397 },
398 "container=?",
399 new String[] { Integer.toString(LauncherSettings.Favorites.CONTAINER_DESKTOP) },
400 null);
401
402 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
403 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
404 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
405 final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
406 final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
407 LongSparseArray<ArrayList<Rect>> screenItems = new LongSparseArray<ArrayList<Rect>>();
408 try {
409 while (c.moveToNext()) {
410 Rect rect = new Rect();
411 rect.left = c.getInt(cellXIndex);
412 rect.top = c.getInt(cellYIndex);
413 rect.right = rect.left + Math.max(1, c.getInt(spanXIndex));
414 rect.bottom = rect.top + Math.max(1, c.getInt(spanYIndex));
415
416 long screenId = c.getInt(screenIndex);
417 ArrayList<Rect> items = screenItems.get(screenId);
418 if (items == null) {
419 items = new ArrayList<Rect>();
420 screenItems.put(screenId, items);
421 }
422 items.add(rect);
423 }
424 } catch (Exception e) {
425 screenItems.clear();
426 } finally {
427 c.close();
428 }
429
430 // Find appropriate space for the item.
431 long screenId = 0;
432 int[] cordinates = new int[2];
433 boolean found = false;
434
435 int screenCount = workspaceScreens.size();
436 // First check the preferred screen.
437 int preferredScreenIndex = preferredScreen.getScreenIndex(workspaceScreens);
438 if (preferredScreenIndex < screenCount) {
439 screenId = workspaceScreens.get(preferredScreenIndex);
440 found = findNextAvailableIconSpaceInScreen(
441 screenItems.get(screenId), cordinates, spanX, spanY);
442 }
443
444 if (!found) {
445 // Search on any of the screens.
446 for (int screen = fallbackStartScreen; screen < screenCount; screen++) {
447 screenId = workspaceScreens.get(screen);
448 if (findNextAvailableIconSpaceInScreen(
449 screenItems.get(screenId), cordinates, spanX, spanY)) {
450 // We found a space for it
451 found = true;
452 break;
453 }
454 }
455 }
456
457 if (!found) {
458 // Still no position found. Add a new screen to the end.
459 screenId = LauncherAppState.getLauncherProvider().generateNewScreenId();
460
461 // Save the screen id for binding in the workspace
462 workspaceScreens.add(screenId);
463 addedWorkspaceScreensFinal.add(screenId);
464
465 // If we still can't find an empty space, then God help us all!!!
466 if (!findNextAvailableIconSpaceInScreen(
467 screenItems.get(screenId), cordinates, spanX, spanY)) {
468 throw new RuntimeException("Can't find space to add the item");
469 }
470 }
471 return Pair.create(screenId, cordinates);
472 }
473
474 /**
475 * Adds the provided items to the workspace.
476 * @param preferredScreen the screen where we should try to add the app first
477 * @param fallbackStartScreen the screen to start search for empty space if
478 * preferredScreen is not available.
479 */
480 public void addAndBindPendingItem(
481 final Context context,
482 final PendingAddItemInfo addInfo,
483 final ScreenPosProvider preferredScreen,
484 final int fallbackStartScreen) {
485 final Callbacks callbacks = getCallback();
486 // Process the newly added applications and add them to the database first
487 Runnable r = new Runnable() {
488 public void run() {
489 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800490 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800491
492 // Find appropriate space for the item.
493 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
494 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
495 addInfo.spanX,
496 addInfo.spanY);
497 final long screenId = coords.first;
498 final int[] cordinates = coords.second;
499
500 // Update the workspace screens
501 updateWorkspaceScreenOrder(context, workspaceScreens);
502 runOnMainThread(new Runnable() {
503 public void run() {
504 Callbacks cb = getCallback();
505 if (callbacks == cb && cb != null) {
506 cb.bindAddScreens(addedWorkspaceScreensFinal);
507 cb.bindAddPendingItem(addInfo,
508 LauncherSettings.Favorites.CONTAINER_DESKTOP,
509 screenId, cordinates, addInfo.spanX, addInfo.spanY);
510 }
511 }
512 });
513 }
514 };
515 runOnWorkerThread(r);
516 }
517
518 /**
519 * Adds the provided items to the workspace.
520 * @param preferredScreen the screen where we should try to add the app first
521 * @param fallbackStartScreen the screen to start search for empty space if
522 * preferredScreen is not available.
523 */
524 public void addAndBindAddedWorkspaceApps(final Context context,
525 final ArrayList<ItemInfo> workspaceApps,
526 final ScreenPosProvider preferredScreen,
527 final int fallbackStartScreen,
528 final boolean allowDuplicate) {
529 final Callbacks callbacks = getCallback();
Adam Cohen76a47a12014-02-05 11:47:43 -0800530 if (workspaceApps.isEmpty()) {
Winson Chung9e6a0a22013-08-27 11:58:12 -0700531 return;
Winson Chung997a9232013-07-24 15:33:46 -0700532 }
Winson Chung64359a52013-07-08 17:17:08 -0700533 // Process the newly added applications and add them to the database first
534 Runnable r = new Runnable() {
535 public void run() {
536 final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>();
537 final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>();
538
Winson Chung76828c82013-08-19 15:43:29 -0700539 // Get the list of workspace screens. We need to append to this list and
540 // can not use sBgWorkspaceScreens because loadWorkspace() may not have been
541 // called.
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -0800542 ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context);
Winson Chung64359a52013-07-08 17:17:08 -0700543 synchronized(sBgLock) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800544 for (ItemInfo item : workspaceApps) {
545 if (!allowDuplicate) {
546 // Short-circuit this logic if the icon exists somewhere on the workspace
547 if (shortcutExists(context, item.title.toString(),
548 item.getIntent(), item.user)) {
549 continue;
Winson Chungc763c4e2013-07-19 13:49:06 -0700550 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800551 }
Winson Chung76828c82013-08-19 15:43:29 -0700552
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800553 // Find appropriate space for the item.
554 Pair<Long, int[]> coords = findSpaceForItem(context, preferredScreen,
555 fallbackStartScreen, workspaceScreens, addedWorkspaceScreensFinal,
556 1, 1);
557 long screenId = coords.first;
558 int[] cordinates = coords.second;
Winson Chung64359a52013-07-08 17:17:08 -0700559
Winson Chung997a9232013-07-24 15:33:46 -0700560 ShortcutInfo shortcutInfo;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800561 if (item instanceof ShortcutInfo) {
562 shortcutInfo = (ShortcutInfo) item;
563 } else if (item instanceof AppInfo) {
564 shortcutInfo = ((AppInfo) item).makeShortcut();
Winson Chung997a9232013-07-24 15:33:46 -0700565 } else {
566 throw new RuntimeException("Unexpected info type");
567 }
Winson Chung94d67682013-09-25 16:29:40 -0700568
Winson Chung64359a52013-07-08 17:17:08 -0700569 // Add the shortcut to the db
570 addItemToDatabase(context, shortcutInfo,
571 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800572 screenId, cordinates[0], cordinates[1], false);
Winson Chung64359a52013-07-08 17:17:08 -0700573 // Save the ShortcutInfo for binding in the workspace
574 addedShortcutsFinal.add(shortcutInfo);
575 }
576 }
577
Winson Chung76828c82013-08-19 15:43:29 -0700578 // Update the workspace screens
579 updateWorkspaceScreenOrder(context, workspaceScreens);
580
Adam Cohen76a47a12014-02-05 11:47:43 -0800581 if (!addedShortcutsFinal.isEmpty()) {
Winson Chung997a9232013-07-24 15:33:46 -0700582 runOnMainThread(new Runnable() {
583 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800584 Callbacks cb = getCallback();
Winson Chung997a9232013-07-24 15:33:46 -0700585 if (callbacks == cb && cb != null) {
Winson Chung997a9232013-07-24 15:33:46 -0700586 final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>();
587 final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>();
Winson Chung94d67682013-09-25 16:29:40 -0700588 if (!addedShortcutsFinal.isEmpty()) {
589 ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1);
590 long lastScreenId = info.screenId;
591 for (ItemInfo i : addedShortcutsFinal) {
592 if (i.screenId == lastScreenId) {
593 addAnimated.add(i);
594 } else {
595 addNotAnimated.add(i);
596 }
Winson Chung997a9232013-07-24 15:33:46 -0700597 }
598 }
Winson Chungd64d1762013-08-20 14:37:16 -0700599 callbacks.bindAppsAdded(addedWorkspaceScreensFinal,
Adam Cohen76a47a12014-02-05 11:47:43 -0800600 addNotAnimated, addAnimated, null);
Winson Chung997a9232013-07-24 15:33:46 -0700601 }
Winson Chung64359a52013-07-08 17:17:08 -0700602 }
Winson Chung997a9232013-07-24 15:33:46 -0700603 });
604 }
Winson Chung64359a52013-07-08 17:17:08 -0700605 }
606 };
607 runOnWorkerThread(r);
608 }
609
Winson Chung81b52252012-08-27 15:34:29 -0700610 public void unbindItemInfosAndClearQueuedBindRunnables() {
611 if (sWorkerThread.getThreadId() == Process.myTid()) {
612 throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " +
613 "main thread");
614 }
615
616 // Clear any deferred bind runnables
Jason Monka0a7a742014-04-22 09:23:19 -0400617 synchronized (mDeferredBindRunnables) {
618 mDeferredBindRunnables.clear();
619 }
Winson Chung81b52252012-08-27 15:34:29 -0700620 // Remove any queued bind runnables
621 mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE);
622 // Unbind all the workspace items
623 unbindWorkspaceItemsOnMainThread();
Winson Chung603bcb92011-09-02 11:45:39 -0700624 }
625
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700626 /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */
Winson Chung81b52252012-08-27 15:34:29 -0700627 void unbindWorkspaceItemsOnMainThread() {
Winson Chung603bcb92011-09-02 11:45:39 -0700628 // Ensure that we don't use the same workspace items data structure on the main thread
629 // by making a copy of workspace items first.
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700630 final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>();
631 final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>();
Winson Chung2abf94d2012-07-18 18:16:38 -0700632 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700633 tmpWorkspaceItems.addAll(sBgWorkspaceItems);
634 tmpAppWidgets.addAll(sBgAppWidgets);
635 }
636 Runnable r = new Runnable() {
637 @Override
638 public void run() {
639 for (ItemInfo item : tmpWorkspaceItems) {
640 item.unbind();
641 }
642 for (ItemInfo item : tmpAppWidgets) {
643 item.unbind();
644 }
645 }
646 };
647 runOnMainThread(r);
Adam Cohen4eac29a2011-07-11 17:53:37 -0700648 }
649
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 /**
651 * Adds an item to the DB if it was not created previously, or move it to a new
652 * <container, screen, cellX, cellY>
653 */
654 static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700655 long screenId, int cellX, int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 if (item.container == ItemInfo.NO_ID) {
657 // From all apps
Adam Cohendcd297f2013-06-18 13:13:40 -0700658 addItemToDatabase(context, item, container, screenId, cellX, cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 } else {
660 // From somewhere else
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 moveItemInDatabase(context, item, container, screenId, cellX, cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663 }
664
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700665 static void checkItemInfoLocked(
666 final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) {
667 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
668 if (modelItem != null && item != modelItem) {
669 // check all the data is consistent
670 if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) {
671 ShortcutInfo modelShortcut = (ShortcutInfo) modelItem;
672 ShortcutInfo shortcut = (ShortcutInfo) item;
673 if (modelShortcut.title.toString().equals(shortcut.title.toString()) &&
674 modelShortcut.intent.filterEquals(shortcut.intent) &&
675 modelShortcut.id == shortcut.id &&
676 modelShortcut.itemType == shortcut.itemType &&
677 modelShortcut.container == shortcut.container &&
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 modelShortcut.screenId == shortcut.screenId &&
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700679 modelShortcut.cellX == shortcut.cellX &&
680 modelShortcut.cellY == shortcut.cellY &&
681 modelShortcut.spanX == shortcut.spanX &&
682 modelShortcut.spanY == shortcut.spanY &&
683 ((modelShortcut.dropPos == null && shortcut.dropPos == null) ||
684 (modelShortcut.dropPos != null &&
685 shortcut.dropPos != null &&
686 modelShortcut.dropPos[0] == shortcut.dropPos[0] &&
687 modelShortcut.dropPos[1] == shortcut.dropPos[1]))) {
688 // For all intents and purposes, this is the same object
689 return;
690 }
691 }
692
693 // the modelItem needs to match up perfectly with item if our model is
694 // to be consistent with the database-- for now, just require
695 // modelItem == item or the equality check above
696 String msg = "item: " + ((item != null) ? item.toString() : "null") +
697 "modelItem: " +
698 ((modelItem != null) ? modelItem.toString() : "null") +
699 "Error: ItemInfo passed to checkItemInfo doesn't match original";
700 RuntimeException e = new RuntimeException(msg);
701 if (stackTrace != null) {
702 e.setStackTrace(stackTrace);
703 }
Adam Cohenb9ada652013-11-08 08:25:08 -0800704 throw e;
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700705 }
706 }
707
Michael Jurka816474f2012-06-25 14:49:02 -0700708 static void checkItemInfo(final ItemInfo item) {
709 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
710 final long itemId = item.id;
711 Runnable r = new Runnable() {
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700712 public void run() {
713 synchronized (sBgLock) {
714 checkItemInfoLocked(itemId, item, stackTrace);
Michael Jurka816474f2012-06-25 14:49:02 -0700715 }
Michael Jurkab2ae8ac2012-09-21 12:06:06 -0700716 }
717 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700718 runOnWorkerThread(r);
Michael Jurka816474f2012-06-25 14:49:02 -0700719 }
720
Michael Jurkac9d95c52011-08-29 14:03:34 -0700721 static void updateItemInDatabaseHelper(Context context, final ContentValues values,
722 final ItemInfo item, final String callingFunction) {
723 final long itemId = item.id;
724 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
725 final ContentResolver cr = context.getContentResolver();
726
Adam Cohen487f7dd2012-06-28 18:12:10 -0700727 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700728 Runnable r = new Runnable() {
729 public void run() {
730 cr.update(uri, values, null, null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700731 updateItemArrays(item, itemId, stackTrace);
732 }
733 };
734 runOnWorkerThread(r);
735 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700736
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700737 static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList,
738 final ArrayList<ItemInfo> items, final String callingFunction) {
739 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -0700740
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700741 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
742 Runnable r = new Runnable() {
743 public void run() {
744 ArrayList<ContentProviderOperation> ops =
745 new ArrayList<ContentProviderOperation>();
746 int count = items.size();
747 for (int i = 0; i < count; i++) {
748 ItemInfo item = items.get(i);
749 final long itemId = item.id;
750 final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false);
751 ContentValues values = valuesList.get(i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700752
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700753 ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build());
754 updateItemArrays(item, itemId, stackTrace);
755
756 }
757 try {
758 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
759 } catch (Exception e) {
760 e.printStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -0700761 }
762 }
763 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700764 runOnWorkerThread(r);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700765 }
Adam Cohenbebf0422012-04-11 18:06:28 -0700766
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700767 static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) {
768 // Lock on mBgLock *after* the db operation
769 synchronized (sBgLock) {
770 checkItemInfoLocked(itemId, item, stackTrace);
771
772 if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
773 item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
774 // Item is in a folder, make sure this folder exists
775 if (!sBgFolders.containsKey(item.container)) {
776 // An items container is being set to a that of an item which is not in
777 // the list of Folders.
778 String msg = "item: " + item + " container being set to: " +
779 item.container + ", not in the list of folders";
780 Log.e(TAG, msg);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700781 }
782 }
783
784 // Items are added/removed from the corresponding FolderInfo elsewhere, such
785 // as in Workspace.onDrop. Here, we just add/remove them from the list of items
786 // that are on the desktop, as appropriate
787 ItemInfo modelItem = sBgItemsIdMap.get(itemId);
Winson Chung33231f52013-12-09 16:57:45 -0800788 if (modelItem != null &&
789 (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
790 modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700791 switch (modelItem.itemType) {
792 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
793 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
794 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
795 if (!sBgWorkspaceItems.contains(modelItem)) {
796 sBgWorkspaceItems.add(modelItem);
797 }
798 break;
799 default:
800 break;
801 }
802 } else {
803 sBgWorkspaceItems.remove(modelItem);
804 }
805 }
806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 * Move an item in the DB to a new <container, screen, cellX, cellY>
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700810 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700811 static void moveItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700812 final long screenId, final int cellX, final int cellY) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 item.cellX = cellX;
815 item.cellY = cellY;
Michael Jurkac9d95c52011-08-29 14:03:34 -0700816
Winson Chung3d503fb2011-07-13 17:25:49 -0700817 // We store hotseat items in canonical form which is this orientation invariant position
818 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700819 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -0700820 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700821 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -0700822 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700823 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400825
826 final ContentValues values = new ContentValues();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Winson Chung3d503fb2011-07-13 17:25:49 -0700828 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
829 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800830 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400832
Michael Jurkac9d95c52011-08-29 14:03:34 -0700833 updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase");
The Android Open Source Projectbc219c32009-03-09 11:52:14 -0700834 }
835
836 /**
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700837 * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the
838 * cellX, cellY have already been updated on the ItemInfos.
839 */
840 static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items,
841 final long container, final int screen) {
842
843 ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
844 int count = items.size();
845
846 for (int i = 0; i < count; i++) {
847 ItemInfo item = items.get(i);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700848 item.container = container;
849
850 // We store hotseat items in canonical form which is this orientation invariant position
851 // in the hotseat
852 if (context instanceof Launcher && screen < 0 &&
853 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700854 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX,
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700855 item.cellY);
856 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 item.screenId = screen;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700858 }
859
860 final ContentValues values = new ContentValues();
861 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
862 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
863 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800864 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohendcd297f2013-06-18 13:13:40 -0700865 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700866
867 contentValues.add(values);
868 }
869 updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase");
870 }
871
872 /**
Adam Cohenbebf0422012-04-11 18:06:28 -0700873 * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY>
Adam Cohend4844c32011-02-18 19:25:06 -0800874 */
Adam Cohenbebf0422012-04-11 18:06:28 -0700875 static void modifyItemInDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) {
Winson Chung0f84a602013-09-30 14:30:58 -0700877 item.container = container;
Adam Cohend4844c32011-02-18 19:25:06 -0800878 item.cellX = cellX;
879 item.cellY = cellY;
Adam Cohenbebf0422012-04-11 18:06:28 -0700880 item.spanX = spanX;
881 item.spanY = spanY;
882
883 // We store hotseat items in canonical form which is this orientation invariant position
884 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -0700885 if (context instanceof Launcher && screenId < 0 &&
Adam Cohenbebf0422012-04-11 18:06:28 -0700886 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700887 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Adam Cohenbebf0422012-04-11 18:06:28 -0700888 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -0700889 item.screenId = screenId;
Adam Cohenbebf0422012-04-11 18:06:28 -0700890 }
Adam Cohend4844c32011-02-18 19:25:06 -0800891
Adam Cohend4844c32011-02-18 19:25:06 -0800892 final ContentValues values = new ContentValues();
Adam Cohend4844c32011-02-18 19:25:06 -0800893 values.put(LauncherSettings.Favorites.CONTAINER, item.container);
Adam Cohenbebf0422012-04-11 18:06:28 -0700894 values.put(LauncherSettings.Favorites.CELLX, item.cellX);
895 values.put(LauncherSettings.Favorites.CELLY, item.cellY);
Sunny Goyal08f72612015-01-05 13:41:43 -0800896 values.put(LauncherSettings.Favorites.RANK, item.rank);
Adam Cohenbebf0422012-04-11 18:06:28 -0700897 values.put(LauncherSettings.Favorites.SPANX, item.spanX);
898 values.put(LauncherSettings.Favorites.SPANY, item.spanY);
Adam Cohendcd297f2013-06-18 13:13:40 -0700899 values.put(LauncherSettings.Favorites.SCREEN, item.screenId);
Adam Cohend4844c32011-02-18 19:25:06 -0800900
Michael Jurka816474f2012-06-25 14:49:02 -0700901 updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase");
Adam Cohenbebf0422012-04-11 18:06:28 -0700902 }
Michael Jurkac9d95c52011-08-29 14:03:34 -0700903
904 /**
905 * Update an item to the database in a specified container.
906 */
907 static void updateItemInDatabase(Context context, final ItemInfo item) {
908 final ContentValues values = new ContentValues();
Kenny Guyed131872014-04-30 03:02:21 +0100909 item.onAddToDatabase(context, values);
Michael Jurkac9d95c52011-08-29 14:03:34 -0700910 updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase");
Adam Cohend4844c32011-02-18 19:25:06 -0800911 }
912
913 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 * Returns true if the shortcuts already exists in the database.
915 * we identify a shortcut by its title and intent.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 */
Sunny Goyale0f58d72014-11-10 18:05:31 -0800917 static boolean shortcutExists(Context context, String title, Intent intent,
918 UserHandleCompat user) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 final ContentResolver cr = context.getContentResolver();
Sunny Goyal2a6cf092014-06-26 15:27:14 -0700920 final Intent intentWithPkg, intentWithoutPkg;
921
922 if (intent.getComponent() != null) {
923 // If component is not null, an intent with null package will produce
924 // the same result and should also be a match.
925 if (intent.getPackage() != null) {
926 intentWithPkg = intent;
927 intentWithoutPkg = new Intent(intent).setPackage(null);
928 } else {
929 intentWithPkg = new Intent(intent).setPackage(
930 intent.getComponent().getPackageName());
931 intentWithoutPkg = intent;
932 }
933 } else {
934 intentWithPkg = intent;
935 intentWithoutPkg = intent;
936 }
Sunny Goyale0f58d72014-11-10 18:05:31 -0800937 String userSerial = Long.toString(UserManagerCompat.getInstance(context)
938 .getSerialNumberForUser(user));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
Sunny Goyale0f58d72014-11-10 18:05:31 -0800940 new String[] { "title", "intent", "profileId" },
941 "title=? and (intent=? or intent=?) and profileId=?",
942 new String[] { title, intentWithPkg.toUri(0), intentWithoutPkg.toUri(0), userSerial },
943 null);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 try {
Sunny Goyale0f58d72014-11-10 18:05:31 -0800945 return c.moveToFirst();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 } finally {
947 c.close();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700949 }
950
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 /**
952 * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList.
953 */
954 FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) {
955 final ContentResolver cr = context.getContentResolver();
956 Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null,
957 "_id=? and (itemType=? or itemType=?)",
958 new String[] { String.valueOf(id),
Adam Cohendf2cc412011-04-27 16:56:57 -0700959 String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null);
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700960
Joe Onorato9c1289c2009-08-17 11:03:03 -0400961 try {
962 if (c.moveToFirst()) {
963 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
964 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
965 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
966 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
967 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
968 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969
Joe Onorato9c1289c2009-08-17 11:03:03 -0400970 FolderInfo folderInfo = null;
971 switch (c.getInt(itemTypeIndex)) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700972 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
973 folderInfo = findOrMakeFolder(folderList, id);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 break;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700975 }
976
Joe Onorato9c1289c2009-08-17 11:03:03 -0400977 folderInfo.title = c.getString(titleIndex);
978 folderInfo.id = id;
979 folderInfo.container = c.getInt(containerIndex);
Adam Cohendcd297f2013-06-18 13:13:40 -0700980 folderInfo.screenId = c.getInt(screenIndex);
Adam Cohend22015c2010-07-26 22:02:18 -0700981 folderInfo.cellX = c.getInt(cellXIndex);
982 folderInfo.cellY = c.getInt(cellYIndex);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983
984 return folderInfo;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400986 } finally {
987 c.close();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700988 }
989
990 return null;
991 }
992
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 /**
994 * Add an item to the database in a specified container. Sets the container, screen, cellX and
995 * cellY fields of the item. Also assigns an ID to the item.
996 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 static void addItemToDatabase(Context context, final ItemInfo item, final long container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700998 final long screenId, final int cellX, final int cellY, final boolean notify) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 item.container = container;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001000 item.cellX = cellX;
1001 item.cellY = cellY;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 // We store hotseat items in canonical form which is this orientation invariant position
1003 // in the hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 if (context instanceof Launcher && screenId < 0 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001006 item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 item.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
1011 final ContentValues values = new ContentValues();
1012 final ContentResolver cr = context.getContentResolver();
Kenny Guyed131872014-04-30 03:02:21 +01001013 item.onAddToDatabase(context, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014
Michael Jurka414300a2013-08-27 15:42:35 +02001015 item.id = LauncherAppState.getLauncherProvider().generateNewItemId();
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001016 values.put(LauncherSettings.Favorites._ID, item.id);
Winson Chungaafa03c2010-06-11 17:34:16 -07001017
Jason Monk8e19cf22014-03-20 15:06:57 -04001018 final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
Michael Jurkac9d95c52011-08-29 14:03:34 -07001019 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001020 public void run() {
1021 cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI :
1022 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023
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) {
Jason Monk8e19cf22014-03-20 15:06:57 -04001026 checkItemInfoLocked(item.id, item, stackTrace);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001027 sBgItemsIdMap.put(item.id, item);
1028 switch (item.itemType) {
1029 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1030 sBgFolders.put(item.id, (FolderInfo) item);
1031 // Fall through
1032 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1033 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1034 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
1035 item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1036 sBgWorkspaceItems.add(item);
1037 } else {
1038 if (!sBgFolders.containsKey(item.container)) {
1039 // Adding an item to a folder that doesn't exist.
1040 String msg = "adding item: " + item + " to a folder that " +
1041 " doesn't exist";
Adam Cohen28b3e102012-10-04 17:21:33 -07001042 Log.e(TAG, msg);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001043 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001044 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001045 break;
1046 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1047 sBgAppWidgets.add((LauncherAppWidgetInfo) item);
1048 break;
1049 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001050 }
1051 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001052 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001053 runOnWorkerThread(r);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001054 }
1055
Joe Onorato9c1289c2009-08-17 11:03:03 -04001056 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07001057 * Creates a new unique child id, for a given cell span across all layouts.
1058 */
Michael Jurka845ba3b2010-09-28 17:09:46 -07001059 static int getCellLayoutChildId(
Adam Cohendcd297f2013-06-18 13:13:40 -07001060 long container, long screen, int localCellX, int localCellY, int spanX, int spanY) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001061 return (((int) container & 0xFF) << 24)
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF);
Winson Chungaafa03c2010-06-11 17:34:16 -07001063 }
1064
Sunny Goyal34942622014-08-29 17:20:55 -07001065 private static ArrayList<ItemInfo> getItemsByPackageName(
1066 final String pn, final UserHandleCompat user) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001067 ItemInfoFilter filter = new ItemInfoFilter() {
1068 @Override
1069 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
1070 return cn.getPackageName().equals(pn) && info.user.equals(user);
1071 }
1072 };
Sunny Goyal34942622014-08-29 17:20:55 -07001073 return filterItemInfos(sBgItemsIdMap.values(), filter);
1074 }
1075
1076 /**
1077 * Removes all the items from the database corresponding to the specified package.
1078 */
1079 static void deletePackageFromDatabase(Context context, final String pn,
1080 final UserHandleCompat user) {
1081 deleteItemsFromDatabase(context, getItemsByPackageName(pn, user));
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001082 }
1083
1084 /**
Michael Jurkac9d95c52011-08-29 14:03:34 -07001085 * Removes the specified item from the database
1086 * @param context
1087 * @param item
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 */
Michael Jurkac9d95c52011-08-29 14:03:34 -07001089 static void deleteItemFromDatabase(Context context, final ItemInfo item) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001090 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1091 items.add(item);
1092 deleteItemsFromDatabase(context, items);
1093 }
1094
1095 /**
1096 * Removes the specified items from the database
1097 * @param context
1098 * @param item
1099 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07001100 static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 final ContentResolver cr = context.getContentResolver();
Adam Cohen487f7dd2012-06-28 18:12:10 -07001102
Michael Jurka83df1882011-08-31 20:59:26 -07001103 Runnable r = new Runnable() {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001104 public void run() {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001105 for (ItemInfo item : items) {
1106 final Uri uri = LauncherSettings.Favorites.getContentUri(item.id, false);
1107 cr.delete(uri, null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001108
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001109 // Lock on mBgLock *after* the db operation
1110 synchronized (sBgLock) {
1111 switch (item.itemType) {
1112 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
1113 sBgFolders.remove(item.id);
1114 for (ItemInfo info: sBgItemsIdMap.values()) {
1115 if (info.container == item.id) {
1116 // We are deleting a folder which still contains items that
1117 // think they are contained by that folder.
1118 String msg = "deleting a folder (" + item + ") which still " +
1119 "contains items (" + info + ")";
1120 Log.e(TAG, msg);
1121 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001122 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07001123 sBgWorkspaceItems.remove(item);
1124 break;
1125 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1126 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1127 sBgWorkspaceItems.remove(item);
1128 break;
1129 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
1130 sBgAppWidgets.remove((LauncherAppWidgetInfo) item);
1131 break;
1132 }
1133 sBgItemsIdMap.remove(item.id);
1134 sBgDbIconCache.remove(item);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001135 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001136 }
1137 }
Michael Jurka83df1882011-08-31 20:59:26 -07001138 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001139 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 }
1141
1142 /**
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 * Update the order of the workspace screens in the database. The array list contains
1144 * a list of screen ids in the order that they should appear.
1145 */
Winson Chungc9168342013-06-26 14:54:55 -07001146 void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) {
Winson Chunga90303b2013-11-15 13:05:06 -08001147 // Log to disk
1148 Launcher.addDumpLog(TAG, "11683562 - updateWorkspaceScreenOrder()", true);
1149 Launcher.addDumpLog(TAG, "11683562 - screens: " + TextUtils.join(", ", screens), true);
1150
Winson Chung64359a52013-07-08 17:17:08 -07001151 final ArrayList<Long> screensCopy = new ArrayList<Long>(screens);
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 final ContentResolver cr = context.getContentResolver();
1153 final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
1154
1155 // Remove any negative screen ids -- these aren't persisted
Winson Chung64359a52013-07-08 17:17:08 -07001156 Iterator<Long> iter = screensCopy.iterator();
Adam Cohendcd297f2013-06-18 13:13:40 -07001157 while (iter.hasNext()) {
1158 long id = iter.next();
1159 if (id < 0) {
1160 iter.remove();
1161 }
1162 }
1163
1164 Runnable r = new Runnable() {
1165 @Override
1166 public void run() {
Yura085c8532014-02-11 15:15:29 +00001167 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 // Clear the table
Yura085c8532014-02-11 15:15:29 +00001169 ops.add(ContentProviderOperation.newDelete(uri).build());
Winson Chung76828c82013-08-19 15:43:29 -07001170 int count = screensCopy.size();
Adam Cohendcd297f2013-06-18 13:13:40 -07001171 for (int i = 0; i < count; i++) {
1172 ContentValues v = new ContentValues();
Winson Chung76828c82013-08-19 15:43:29 -07001173 long screenId = screensCopy.get(i);
Adam Cohendcd297f2013-06-18 13:13:40 -07001174 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1175 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
Yura085c8532014-02-11 15:15:29 +00001176 ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build());
Adam Cohendcd297f2013-06-18 13:13:40 -07001177 }
Yura085c8532014-02-11 15:15:29 +00001178
1179 try {
1180 cr.applyBatch(LauncherProvider.AUTHORITY, ops);
1181 } catch (Exception ex) {
1182 throw new RuntimeException(ex);
1183 }
Winson Chung9e6a0a22013-08-27 11:58:12 -07001184
Winson Chungba9c37f2013-08-30 14:11:37 -07001185 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001186 sBgWorkspaceScreens.clear();
1187 sBgWorkspaceScreens.addAll(screensCopy);
Adam Cohen4caf2982013-08-20 18:54:31 -07001188 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 }
1190 };
1191 runOnWorkerThread(r);
1192 }
1193
1194 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 * Remove the contents of the specified folder from the database
1196 */
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001197 static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 final ContentResolver cr = context.getContentResolver();
1199
Michael Jurkac9d95c52011-08-29 14:03:34 -07001200 Runnable r = new Runnable() {
1201 public void run() {
1202 cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001203 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001204 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001205 sBgItemsIdMap.remove(info.id);
1206 sBgFolders.remove(info.id);
1207 sBgDbIconCache.remove(info);
1208 sBgWorkspaceItems.remove(info);
1209 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001210
Michael Jurkac9d95c52011-08-29 14:03:34 -07001211 cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
1212 LauncherSettings.Favorites.CONTAINER + "=" + info.id, null);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001213 // Lock on mBgLock *after* the db operation
Winson Chung2abf94d2012-07-18 18:16:38 -07001214 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001215 for (ItemInfo childInfo : info.contents) {
1216 sBgItemsIdMap.remove(childInfo.id);
1217 sBgDbIconCache.remove(childInfo);
1218 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001219 }
Michael Jurkac9d95c52011-08-29 14:03:34 -07001220 }
1221 };
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001222 runOnWorkerThread(r);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 }
1224
1225 /**
1226 * Set this as the current Launcher activity object for the loader.
1227 */
1228 public void initialize(Callbacks callbacks) {
1229 synchronized (mLock) {
1230 mCallbacks = new WeakReference<Callbacks>(callbacks);
1231 }
1232 }
1233
Kenny Guyed131872014-04-30 03:02:21 +01001234 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001235 public void onPackageChanged(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001236 int op = PackageUpdatedTask.OP_UPDATE;
1237 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1238 user));
1239 }
1240
1241 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001242 public void onPackageRemoved(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001243 int op = PackageUpdatedTask.OP_REMOVE;
1244 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1245 user));
1246 }
1247
1248 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001249 public void onPackageAdded(String packageName, UserHandleCompat user) {
Kenny Guyed131872014-04-30 03:02:21 +01001250 int op = PackageUpdatedTask.OP_ADD;
1251 enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName },
1252 user));
1253 }
1254
1255 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001256 public void onPackagesAvailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001257 boolean replacing) {
1258 if (!replacing) {
1259 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames,
1260 user));
1261 if (mAppsCanBeOnRemoveableStorage) {
1262 // Only rebind if we support removable storage. It catches the
1263 // case where
1264 // apps on the external sd card need to be reloaded
1265 startLoaderFromBackground();
1266 }
1267 } else {
1268 // If we are replacing then just update the packages in the list
1269 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE,
1270 packageNames, user));
1271 }
1272 }
1273
1274 @Override
Kenny Guyc2bd8102014-06-30 12:30:31 +01001275 public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user,
Kenny Guyed131872014-04-30 03:02:21 +01001276 boolean replacing) {
1277 if (!replacing) {
1278 enqueuePackageUpdated(new PackageUpdatedTask(
1279 PackageUpdatedTask.OP_UNAVAILABLE, packageNames,
1280 user));
1281 }
Kenny Guyed131872014-04-30 03:02:21 +01001282 }
1283
Joe Onorato1d8e7bb2009-10-15 19:49:43 -07001284 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and
1286 * ACTION_PACKAGE_CHANGED.
1287 */
Narayan Kamathcb1a4772011-06-28 13:46:59 +01001288 @Override
Joe Onoratof99f8c12009-10-31 17:27:36 -04001289 public void onReceive(Context context, Intent intent) {
Chris Wrenb358f812014-04-16 13:37:00 -04001290 if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
Winson Chungaafa03c2010-06-11 17:34:16 -07001291
Joe Onorato36115782010-06-17 13:28:48 -04001292 final String action = intent.getAction();
Kenny Guyed131872014-04-30 03:02:21 +01001293 if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Reena Lee93f824a2011-09-23 17:20:28 -07001294 // If we have changed locale we need to clear out the labels in all apps/workspace.
1295 forceReload();
1296 } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
1297 // Check if configuration change was an mcc/mnc change which would affect app resources
1298 // and we would need to clear out the labels in all apps/workspace. Same handling as
1299 // above for ACTION_LOCALE_CHANGED
1300 Configuration currentConfig = context.getResources().getConfiguration();
Reena Lee99a73f32011-10-24 17:27:37 -07001301 if (mPreviousConfigMcc != currentConfig.mcc) {
Reena Lee93f824a2011-09-23 17:20:28 -07001302 Log.d(TAG, "Reload apps on config change. curr_mcc:"
Reena Lee99a73f32011-10-24 17:27:37 -07001303 + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc);
Reena Lee93f824a2011-09-23 17:20:28 -07001304 forceReload();
1305 }
1306 // Update previousConfig
Reena Lee99a73f32011-10-24 17:27:37 -07001307 mPreviousConfigMcc = currentConfig.mcc;
Winson Chungcbf7c4d2011-08-23 11:58:54 -07001308 } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) ||
1309 SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08001310 Callbacks callbacks = getCallback();
1311 if (callbacks != null) {
1312 callbacks.bindSearchablesChanged();
Winson Chungcfdf7ee2011-08-25 11:38:34 -07001313 }
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001314 }
1315 }
1316
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001317 void forceReload() {
Winson Chungf0c6ae02012-03-21 16:10:31 -07001318 resetLoadedState(true, true);
1319
Reena Lee93f824a2011-09-23 17:20:28 -07001320 // Do this here because if the launcher activity is running it will be restarted.
1321 // If it's not running startLoaderFromBackground will merely tell it that it needs
1322 // to reload.
1323 startLoaderFromBackground();
1324 }
1325
Winson Chungf0c6ae02012-03-21 16:10:31 -07001326 public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) {
1327 synchronized (mLock) {
1328 // Stop any existing loaders first, so they don't set mAllAppsLoaded or
1329 // mWorkspaceLoaded to true later
1330 stopLoaderLocked();
1331 if (resetAllAppsLoaded) mAllAppsLoaded = false;
1332 if (resetWorkspaceLoaded) mWorkspaceLoaded = false;
1333 }
1334 }
1335
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001336 /**
1337 * When the launcher is in the background, it's possible for it to miss paired
1338 * configuration changes. So whenever we trigger the loader from the background
1339 * tell the launcher that it needs to re-run the loader when it comes back instead
1340 * of doing it now.
1341 */
1342 public void startLoaderFromBackground() {
1343 boolean runLoader = false;
Sunny Goyale0f58d72014-11-10 18:05:31 -08001344 Callbacks callbacks = getCallback();
1345 if (callbacks != null) {
1346 // Only actually run the loader if they're not paused.
1347 if (!callbacks.setLoadOnResume()) {
1348 runLoader = true;
Joe Onoratoe9ad59e2010-10-29 17:35:36 -07001349 }
1350 }
1351 if (runLoader) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001352 startLoader(false, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato790c2d92010-06-11 00:14:11 -07001353 }
Joe Onorato36115782010-06-17 13:28:48 -04001354 }
Joe Onoratof99f8c12009-10-31 17:27:36 -04001355
Reena Lee93f824a2011-09-23 17:20:28 -07001356 // If there is already a loader task running, tell it to stop.
1357 // returns true if isLaunching() was true on the old task
1358 private boolean stopLoaderLocked() {
1359 boolean isLaunching = false;
1360 LoaderTask oldTask = mLoaderTask;
1361 if (oldTask != null) {
1362 if (oldTask.isLaunching()) {
1363 isLaunching = true;
1364 }
1365 oldTask.stopLocked();
1366 }
1367 return isLaunching;
1368 }
1369
Adam Cohen1a85c582014-09-30 09:48:49 -07001370 public boolean isCurrentCallbacks(Callbacks callbacks) {
1371 return (mCallbacks != null && mCallbacks.get() == callbacks);
1372 }
1373
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001374 public void startLoader(boolean isLaunching, int synchronousBindPage) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001375 startLoader(isLaunching, synchronousBindPage, LOADER_FLAG_NONE);
1376 }
1377
1378 public void startLoader(boolean isLaunching, int synchronousBindPage, int loadFlags) {
Joe Onorato36115782010-06-17 13:28:48 -04001379 synchronized (mLock) {
1380 if (DEBUG_LOADERS) {
1381 Log.d(TAG, "startLoader isLaunching=" + isLaunching);
1382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001384 // Clear any deferred bind-runnables from the synchronized load process
1385 // We must do this before any loading/binding is scheduled below.
Jason Monka0a7a742014-04-22 09:23:19 -04001386 synchronized (mDeferredBindRunnables) {
1387 mDeferredBindRunnables.clear();
1388 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001389
Joe Onorato36115782010-06-17 13:28:48 -04001390 // Don't bother to start the thread if we know it's not going to do anything
1391 if (mCallbacks != null && mCallbacks.get() != null) {
1392 // If there is already one running, tell it to stop.
Reena Lee93f824a2011-09-23 17:20:28 -07001393 // also, don't downgrade isLaunching if we're already running
1394 isLaunching = isLaunching || stopLoaderLocked();
Dan Sandlerd5024042014-01-09 15:01:33 -05001395 mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching, loadFlags);
Derek Prothro7aff3992013-12-10 14:00:37 -05001396 if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
1397 && mAllAppsLoaded && mWorkspaceLoaded) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001398 mLoaderTask.runBindSynchronousPage(synchronousBindPage);
1399 } else {
1400 sWorkerThread.setPriority(Thread.NORM_PRIORITY);
1401 sWorker.post(mLoaderTask);
1402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 }
1405 }
1406
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001407 void bindRemainingSynchronousPages() {
1408 // Post the remaining side pages to be loaded
1409 if (!mDeferredBindRunnables.isEmpty()) {
Jason Monka0a7a742014-04-22 09:23:19 -04001410 Runnable[] deferredBindRunnables = null;
1411 synchronized (mDeferredBindRunnables) {
1412 deferredBindRunnables = mDeferredBindRunnables.toArray(
1413 new Runnable[mDeferredBindRunnables.size()]);
1414 mDeferredBindRunnables.clear();
1415 }
1416 for (final Runnable r : deferredBindRunnables) {
Winson Chung81b52252012-08-27 15:34:29 -07001417 mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001418 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001419 }
1420 }
1421
Joe Onorato36115782010-06-17 13:28:48 -04001422 public void stopLoader() {
1423 synchronized (mLock) {
1424 if (mLoaderTask != null) {
1425 mLoaderTask.stopLocked();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 }
1427 }
Joe Onorato36115782010-06-17 13:28:48 -04001428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001429
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001430 /**
1431 * Loads the workspace screen ids in an ordered list.
1432 */
1433 private static ArrayList<Long> loadWorkspaceScreensDb(Context context) {
Winson Chung76828c82013-08-19 15:43:29 -07001434 final ContentResolver contentResolver = context.getContentResolver();
1435 final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI;
Winson Chung76828c82013-08-19 15:43:29 -07001436
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001437 // Get screens ordered by rank.
1438 final Cursor sc = contentResolver.query(screensUri, null, null, null,
1439 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1440 ArrayList<Long> screenIds = new ArrayList<Long>();
Winson Chung76828c82013-08-19 15:43:29 -07001441 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001442 final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID);
Winson Chung76828c82013-08-19 15:43:29 -07001443 while (sc.moveToNext()) {
1444 try {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001445 screenIds.add(sc.getLong(idIndex));
Winson Chung76828c82013-08-19 15:43:29 -07001446 } catch (Exception e) {
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001447 Launcher.addDumpLog(TAG, "Desktop items loading interrupted"
1448 + " - invalid screens: " + e, true);
Winson Chung76828c82013-08-19 15:43:29 -07001449 }
1450 }
1451 } finally {
1452 sc.close();
1453 }
Sunny Goyalc1b7c2e2015-01-16 16:19:04 -08001454 return screenIds;
Winson Chung76828c82013-08-19 15:43:29 -07001455 }
1456
Michael Jurkac57b7a82011-08-09 22:02:20 -07001457 public boolean isAllAppsLoaded() {
1458 return mAllAppsLoaded;
1459 }
1460
Winson Chung36a62fe2012-05-06 18:04:42 -07001461 boolean isLoadingWorkspace() {
1462 synchronized (mLock) {
1463 if (mLoaderTask != null) {
1464 return mLoaderTask.isLoadingWorkspace();
1465 }
1466 }
1467 return false;
1468 }
1469
Joe Onorato36115782010-06-17 13:28:48 -04001470 /**
1471 * Runnable for the thread that loads the contents of the launcher:
1472 * - workspace icons
1473 * - widgets
1474 * - all apps icons
1475 */
1476 private class LoaderTask implements Runnable {
1477 private Context mContext;
Joe Onorato36115782010-06-17 13:28:48 -04001478 private boolean mIsLaunching;
Winson Chung36a62fe2012-05-06 18:04:42 -07001479 private boolean mIsLoadingAndBindingWorkspace;
Joe Onorato36115782010-06-17 13:28:48 -04001480 private boolean mStopped;
1481 private boolean mLoadAndBindStepFinished;
Dan Sandlerd5024042014-01-09 15:01:33 -05001482 private int mFlags;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001483
Winson Chungc3eecff2011-07-11 17:44:15 -07001484 private HashMap<Object, CharSequence> mLabelCache;
Joe Onorato36115782010-06-17 13:28:48 -04001485
Dan Sandlerd5024042014-01-09 15:01:33 -05001486 LoaderTask(Context context, boolean isLaunching, int flags) {
Joe Onorato36115782010-06-17 13:28:48 -04001487 mContext = context;
1488 mIsLaunching = isLaunching;
Winson Chungc3eecff2011-07-11 17:44:15 -07001489 mLabelCache = new HashMap<Object, CharSequence>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001490 mFlags = flags;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 }
1492
Joe Onorato36115782010-06-17 13:28:48 -04001493 boolean isLaunching() {
1494 return mIsLaunching;
1495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496
Winson Chung36a62fe2012-05-06 18:04:42 -07001497 boolean isLoadingWorkspace() {
1498 return mIsLoadingAndBindingWorkspace;
1499 }
1500
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001501 private void loadAndBindWorkspace() {
Winson Chung36a62fe2012-05-06 18:04:42 -07001502 mIsLoadingAndBindingWorkspace = true;
1503
Joe Onorato36115782010-06-17 13:28:48 -04001504 // Load the workspace
Joe Onorato36115782010-06-17 13:28:48 -04001505 if (DEBUG_LOADERS) {
1506 Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 }
Michael Jurka288a36b2011-07-12 16:53:48 -07001508
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001509 if (!mWorkspaceLoaded) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001510 loadWorkspace();
Reena Lee93f824a2011-09-23 17:20:28 -07001511 synchronized (LoaderTask.this) {
1512 if (mStopped) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001513 return;
Reena Lee93f824a2011-09-23 17:20:28 -07001514 }
1515 mWorkspaceLoaded = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001516 }
1517 }
1518
Joe Onorato36115782010-06-17 13:28:48 -04001519 // Bind the workspace
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001520 bindWorkspace(-1);
Joe Onorato36115782010-06-17 13:28:48 -04001521 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001522
Joe Onorato36115782010-06-17 13:28:48 -04001523 private void waitForIdle() {
1524 // Wait until the either we're stopped or the other threads are done.
1525 // This way we don't start loading all apps until the workspace has settled
1526 // down.
1527 synchronized (LoaderTask.this) {
1528 final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onoratocc67f472010-06-08 10:54:30 -07001529
Joe Onorato36115782010-06-17 13:28:48 -04001530 mHandler.postIdle(new Runnable() {
1531 public void run() {
1532 synchronized (LoaderTask.this) {
1533 mLoadAndBindStepFinished = true;
1534 if (DEBUG_LOADERS) {
1535 Log.d(TAG, "done with previous binding step");
Daniel Sandler843e8602010-06-07 14:59:01 -04001536 }
Joe Onorato36115782010-06-17 13:28:48 -04001537 LoaderTask.this.notify();
Daniel Sandler843e8602010-06-07 14:59:01 -04001538 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001539 }
Joe Onorato36115782010-06-17 13:28:48 -04001540 });
1541
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08001542 while (!mStopped && !mLoadAndBindStepFinished) {
Joe Onorato36115782010-06-17 13:28:48 -04001543 try {
Michael Jurkac7700af2013-05-14 20:17:58 +02001544 // Just in case mFlushingWorkerThread changes but we aren't woken up,
1545 // wait no longer than 1sec at a time
1546 this.wait(1000);
Joe Onorato36115782010-06-17 13:28:48 -04001547 } catch (InterruptedException ex) {
1548 // Ignore
Daniel Sandler843e8602010-06-07 14:59:01 -04001549 }
1550 }
Joe Onorato36115782010-06-17 13:28:48 -04001551 if (DEBUG_LOADERS) {
1552 Log.d(TAG, "waited "
Winson Chungaafa03c2010-06-11 17:34:16 -07001553 + (SystemClock.uptimeMillis()-workspaceWaitTime)
Joe Onorato36115782010-06-17 13:28:48 -04001554 + "ms for previous step to finish binding");
1555 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001556 }
Joe Onorato36115782010-06-17 13:28:48 -04001557 }
Daniel Sandler843e8602010-06-07 14:59:01 -04001558
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001559 void runBindSynchronousPage(int synchronousBindPage) {
Derek Prothro7aff3992013-12-10 14:00:37 -05001560 if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001561 // Ensure that we have a valid page index to load synchronously
1562 throw new RuntimeException("Should not call runBindSynchronousPage() without " +
1563 "valid page index");
1564 }
1565 if (!mAllAppsLoaded || !mWorkspaceLoaded) {
1566 // Ensure that we don't try and bind a specified page when the pages have not been
1567 // loaded already (we should load everything asynchronously in that case)
1568 throw new RuntimeException("Expecting AllApps and Workspace to be loaded");
1569 }
1570 synchronized (mLock) {
1571 if (mIsLoaderTaskRunning) {
1572 // Ensure that we are never running the background loading at this point since
1573 // we also touch the background collections
1574 throw new RuntimeException("Error! Background loading is already running");
1575 }
1576 }
1577
1578 // XXX: Throw an exception if we are already loading (since we touch the worker thread
1579 // data structures, we can't allow any other thread to touch that data, but because
1580 // this call is synchronous, we can get away with not locking).
1581
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001582 // The LauncherModel is static in the LauncherAppState and mHandler may have queued
Adam Cohena13a2f22012-07-23 14:29:15 -07001583 // operations from the previous activity. We need to ensure that all queued operations
1584 // are executed before any synchronous binding work is done.
1585 mHandler.flush();
1586
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001587 // Divide the set of loaded items into those that we are binding synchronously, and
1588 // everything else that is to be bound normally (asynchronously).
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001589 bindWorkspace(synchronousBindPage);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001590 // XXX: For now, continue posting the binding of AllApps as there are other issues that
1591 // arise from that.
1592 onlyBindAllApps();
1593 }
1594
Joe Onorato36115782010-06-17 13:28:48 -04001595 public void run() {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001596 synchronized (mLock) {
1597 mIsLoaderTaskRunning = true;
1598 }
Joe Onorato36115782010-06-17 13:28:48 -04001599 // Optimize for end-user experience: if the Launcher is up and // running with the
1600 // All Apps interface in the foreground, load All Apps first. Otherwise, load the
1601 // workspace first (default).
Joe Onorato36115782010-06-17 13:28:48 -04001602 keep_running: {
Daniel Sandler843e8602010-06-07 14:59:01 -04001603 // Elevate priority when Home launches for the first time to avoid
1604 // starving at boot time. Staring at a blank home is not cool.
1605 synchronized (mLock) {
Winson Chungaac01e12011-08-17 10:37:13 -07001606 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " +
1607 (mIsLaunching ? "DEFAULT" : "BACKGROUND"));
Daniel Sandler843e8602010-06-07 14:59:01 -04001608 android.os.Process.setThreadPriority(mIsLaunching
1609 ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND);
1610 }
Winson Chung64359a52013-07-08 17:17:08 -07001611 if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001612 loadAndBindWorkspace();
Daniel Sandler843e8602010-06-07 14:59:01 -04001613
Joe Onorato36115782010-06-17 13:28:48 -04001614 if (mStopped) {
1615 break keep_running;
1616 }
1617
1618 // Whew! Hard work done. Slow us down, and wait until the UI thread has
1619 // settled down.
Daniel Sandler843e8602010-06-07 14:59:01 -04001620 synchronized (mLock) {
1621 if (mIsLaunching) {
Winson Chungaac01e12011-08-17 10:37:13 -07001622 if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND");
Daniel Sandler843e8602010-06-07 14:59:01 -04001623 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1624 }
1625 }
Joe Onorato36115782010-06-17 13:28:48 -04001626 waitForIdle();
Daniel Sandler843e8602010-06-07 14:59:01 -04001627
1628 // second step
Winson Chung64359a52013-07-08 17:17:08 -07001629 if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
1630 loadAndBindAllApps();
Winson Chung7ed37742011-09-08 15:45:51 -07001631
1632 // Restore the default thread priority after we are done loading items
1633 synchronized (mLock) {
1634 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
1635 }
Joe Onorato36115782010-06-17 13:28:48 -04001636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637
Winson Chungaac01e12011-08-17 10:37:13 -07001638 // Update the saved icons if necessary
1639 if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons");
Winson Chung2abf94d2012-07-18 18:16:38 -07001640 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001641 for (Object key : sBgDbIconCache.keySet()) {
1642 updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key));
1643 }
1644 sBgDbIconCache.clear();
Winson Chungaac01e12011-08-17 10:37:13 -07001645 }
Winson Chungaac01e12011-08-17 10:37:13 -07001646
Joe Onorato36115782010-06-17 13:28:48 -04001647 // Clear out this reference, otherwise we end up holding it until all of the
1648 // callback runnables are done.
1649 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650
Joe Onorato36115782010-06-17 13:28:48 -04001651 synchronized (mLock) {
1652 // If we are still the last one to be scheduled, remove ourselves.
1653 if (mLoaderTask == this) {
1654 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001656 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001657 }
Joe Onorato36115782010-06-17 13:28:48 -04001658 }
1659
1660 public void stopLocked() {
1661 synchronized (LoaderTask.this) {
1662 mStopped = true;
1663 this.notify();
1664 }
1665 }
1666
1667 /**
1668 * Gets the callbacks object. If we've been stopped, or if the launcher object
1669 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1670 * object that was around when the deferred message was scheduled, and if there's
1671 * a new Callbacks object around then also return null. This will save us from
1672 * calling onto it with data that will be ignored.
1673 */
1674 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1675 synchronized (mLock) {
1676 if (mStopped) {
1677 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001678 }
Joe Onorato36115782010-06-17 13:28:48 -04001679
1680 if (mCallbacks == null) {
1681 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001682 }
Joe Onorato36115782010-06-17 13:28:48 -04001683
1684 final Callbacks callbacks = mCallbacks.get();
1685 if (callbacks != oldCallbacks) {
1686 return null;
1687 }
1688 if (callbacks == null) {
1689 Log.w(TAG, "no mCallbacks");
1690 return null;
1691 }
1692
1693 return callbacks;
1694 }
1695 }
1696
1697 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001698 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001699 LauncherAppState app = LauncherAppState.getInstance();
1700 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001701 final int countX = (int) grid.numColumns;
1702 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001703
Adam Cohendcd297f2013-06-18 13:13:40 -07001704 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001705 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001706 // Return early if we detect that an item is under the hotseat button
1707 if (mCallbacks == null ||
1708 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001709 Log.e(TAG, "Error loading shortcut into hotseat " + item
1710 + " into position (" + item.screenId + ":" + item.cellX + ","
1711 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001712 return false;
1713 }
1714
Dan Sandler295ae182013-12-10 16:05:47 -05001715 final ItemInfo[][] hotseatItems =
1716 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1717
Adam Cohenae4409d2013-11-26 10:34:59 -08001718 if (item.screenId >= grid.numHotseatIcons) {
1719 Log.e(TAG, "Error loading shortcut " + item
1720 + " into hotseat position " + item.screenId
1721 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1722 + ")");
1723 return false;
1724 }
1725
Dan Sandler295ae182013-12-10 16:05:47 -05001726 if (hotseatItems != null) {
1727 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001728 Log.e(TAG, "Error loading shortcut into hotseat " + item
1729 + " into position (" + item.screenId + ":" + item.cellX + ","
1730 + item.cellY + ") occupied by "
1731 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1732 [(int) item.screenId][0]);
1733 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001734 } else {
1735 hotseatItems[(int) item.screenId][0] = item;
1736 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001737 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001738 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001739 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001740 items[(int) item.screenId][0] = item;
1741 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001742 return true;
1743 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001744 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1745 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001746 return true;
1747 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001748
Adam Cohendcd297f2013-06-18 13:13:40 -07001749 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001750 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001751 occupied.put(item.screenId, items);
1752 }
1753
Dan Sandler295ae182013-12-10 16:05:47 -05001754 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001755 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1756 item.cellX < 0 || item.cellY < 0 ||
1757 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1758 Log.e(TAG, "Error loading shortcut " + item
1759 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1760 + item.cellX + "," + item.cellY
1761 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1762 return false;
1763 }
1764
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001765 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001766 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1767 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001768 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001769 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001770 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001771 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001772 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001773 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001774 return false;
1775 }
1776 }
1777 }
1778 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1779 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001780 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001781 }
1782 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001783
Joe Onorato36115782010-06-17 13:28:48 -04001784 return true;
1785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786
Winson Chungba9c37f2013-08-30 14:11:37 -07001787 /** Clears all the sBg data structures */
1788 private void clearSBgDataStructures() {
1789 synchronized (sBgLock) {
1790 sBgWorkspaceItems.clear();
1791 sBgAppWidgets.clear();
1792 sBgFolders.clear();
1793 sBgItemsIdMap.clear();
1794 sBgDbIconCache.clear();
1795 sBgWorkspaceScreens.clear();
1796 }
1797 }
1798
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001799 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001800 // Log to disk
1801 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1802
Joe Onorato36115782010-06-17 13:28:48 -04001803 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804
Joe Onorato36115782010-06-17 13:28:48 -04001805 final Context context = mContext;
1806 final ContentResolver contentResolver = context.getContentResolver();
1807 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001808 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001809 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1810 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001811 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001812
Winson Chung892c74d2013-08-22 16:15:50 -07001813 LauncherAppState app = LauncherAppState.getInstance();
1814 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1815 int countX = (int) grid.numColumns;
1816 int countY = (int) grid.numRows;
1817
Dan Sandlerd5024042014-01-09 15:01:33 -05001818 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1819 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1820 LauncherAppState.getLauncherProvider().deleteDatabase();
1821 }
1822
1823 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1824 // append the user's Launcher2 shortcuts
1825 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1826 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1827 } else {
1828 // Make sure the default workspace is loaded
1829 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001830 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001831 }
Adam Cohene25af792013-06-06 23:08:25 -07001832
Winson Chung2abf94d2012-07-18 18:16:38 -07001833 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001834 clearSBgDataStructures();
Sunny Goyal94485362014-09-18 16:13:58 -07001835 final HashSet<String> installingPkgs = PackageInstallerCompat
1836 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001837
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001838 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001839 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07001840 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION;
Chris Wrene523e702013-10-09 10:36:55 -04001841 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001842 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001843
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001844 // +1 for the hotseat (it can be larger than the workspace)
1845 // Load workspace in reverse order to ensure that latest items are loaded first (and
1846 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001847 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001849 try {
1850 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1851 final int intentIndex = c.getColumnIndexOrThrow
1852 (LauncherSettings.Favorites.INTENT);
1853 final int titleIndex = c.getColumnIndexOrThrow
1854 (LauncherSettings.Favorites.TITLE);
1855 final int iconTypeIndex = c.getColumnIndexOrThrow(
1856 LauncherSettings.Favorites.ICON_TYPE);
1857 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1858 final int iconPackageIndex = c.getColumnIndexOrThrow(
1859 LauncherSettings.Favorites.ICON_PACKAGE);
1860 final int iconResourceIndex = c.getColumnIndexOrThrow(
1861 LauncherSettings.Favorites.ICON_RESOURCE);
1862 final int containerIndex = c.getColumnIndexOrThrow(
1863 LauncherSettings.Favorites.CONTAINER);
1864 final int itemTypeIndex = c.getColumnIndexOrThrow(
1865 LauncherSettings.Favorites.ITEM_TYPE);
1866 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1867 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001868 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1869 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001870 final int screenIndex = c.getColumnIndexOrThrow(
1871 LauncherSettings.Favorites.SCREEN);
1872 final int cellXIndex = c.getColumnIndexOrThrow
1873 (LauncherSettings.Favorites.CELLX);
1874 final int cellYIndex = c.getColumnIndexOrThrow
1875 (LauncherSettings.Favorites.CELLY);
1876 final int spanXIndex = c.getColumnIndexOrThrow
1877 (LauncherSettings.Favorites.SPANX);
1878 final int spanYIndex = c.getColumnIndexOrThrow(
1879 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001880 final int rankIndex = c.getColumnIndexOrThrow(
1881 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001882 final int restoredIndex = c.getColumnIndexOrThrow(
1883 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001884 final int profileIdIndex = c.getColumnIndexOrThrow(
1885 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001886 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1887 //final int displayModeIndex = c.getColumnIndexOrThrow(
1888 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001890 ShortcutInfo info;
1891 String intentDescription;
1892 LauncherAppWidgetInfo appWidgetInfo;
1893 int container;
1894 long id;
1895 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001896 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 while (!mStopped && c.moveToNext()) {
1899 try {
1900 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001901 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001902 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001903
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001904 switch (itemType) {
1905 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1906 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001907 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001908 intentDescription = c.getString(intentIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001909 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001910 user = mUserManager.getUserForSerialNumber(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001911 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001912 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001913 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001914 if (user == null) {
1915 // User has been deleted remove the item.
1916 itemsToRemove.add(id);
1917 continue;
1918 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001919 try {
1920 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001921 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001922 if (cn != null && cn.getPackageName() != null) {
1923 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1924 cn.getPackageName(), user);
1925 boolean validComponent = validPkg &&
1926 launcherApps.isActivityEnabledForProfile(cn, user);
1927
1928 if (validComponent) {
1929 if (restored) {
1930 // no special handling necessary for this item
1931 restoredRows.add(id);
1932 restored = false;
1933 }
1934 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001935 intent = null;
1936 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1937 // We allow auto install apps to have their intent
1938 // updated after an install.
1939 intent = manager.getLaunchIntentForPackage(
1940 cn.getPackageName());
1941 if (intent != null) {
1942 ContentValues values = new ContentValues();
1943 values.put(LauncherSettings.Favorites.INTENT,
1944 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001945 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001946 }
1947 }
1948
1949 if (intent == null) {
1950 // The app is installed but the component is no
1951 // longer available.
1952 Launcher.addDumpLog(TAG,
1953 "Invalid component removed: " + cn, true);
1954 itemsToRemove.add(id);
1955 continue;
1956 } else {
1957 // no special handling necessary for this item
1958 restoredRows.add(id);
1959 restored = false;
1960 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001961 } else if (restored) {
1962 // Package is not yet available but might be
1963 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001964 Launcher.addDumpLog(TAG,
1965 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001966
1967 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1968 // Restore has started once.
1969 } else if (installingPkgs.contains(cn.getPackageName())) {
1970 // App restore has started. Update the flag
1971 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
1972 ContentValues values = new ContentValues();
1973 values.put(LauncherSettings.Favorites.RESTORED,
1974 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001975 updateItem(id, values);
1976 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
1977 // This is a common app. Try to replace this.
1978 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
1979 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
1980 if (parser.findDefaultApp()) {
1981 // Default app found. Replace it.
1982 intent = parser.parsedIntent;
1983 cn = intent.getComponent();
1984 ContentValues values = parser.parsedValues;
1985 values.put(LauncherSettings.Favorites.RESTORED, 0);
1986 updateItem(id, values);
1987 restored = false;
1988 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07001989
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001990 } else if (REMOVE_UNRESTORED_ICONS) {
1991 Launcher.addDumpLog(TAG,
1992 "Unrestored package removed: " + cn, true);
1993 itemsToRemove.add(id);
1994 continue;
1995 }
Sunny Goyal94485362014-09-18 16:13:58 -07001996 } else if (REMOVE_UNRESTORED_ICONS) {
1997 Launcher.addDumpLog(TAG,
1998 "Unrestored package removed: " + cn, true);
1999 itemsToRemove.add(id);
2000 continue;
2001 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002002 } else if (launcherApps.isAppEnabled(
2003 manager, cn.getPackageName(),
2004 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2005 // Package is present but not available.
2006 allowMissingTarget = true;
2007 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2008 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002009 // SdCard is not ready yet. Package might get available,
2010 // once it is ready.
2011 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2012 + " (check again later)", true);
2013 HashSet<String> pkgs = sPendingPackages.get(user);
2014 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002015 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002016 sPendingPackages.put(user, pkgs);
2017 }
2018 pkgs.add(cn.getPackageName());
2019 allowMissingTarget = true;
2020 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002021
2022 } else {
2023 // Do not wait for external media load anymore.
2024 // Log the invalid package, and remove it
2025 Launcher.addDumpLog(TAG,
2026 "Invalid package removed: " + cn, true);
2027 itemsToRemove.add(id);
2028 continue;
Winson Chungee055712013-07-30 14:46:24 -07002029 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002030 } else if (cn == null) {
2031 // For shortcuts with no component, keep them as they are
2032 restoredRows.add(id);
2033 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002034 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002035 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002036 Launcher.addDumpLog(TAG,
2037 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002038 continue;
2039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002041 if (itemReplaced) {
2042 if (user.equals(UserHandleCompat.myUserHandle())) {
2043 info = getShortcutInfo(manager, intent, user, context, null,
2044 iconIndex, titleIndex, mLabelCache, false);
2045 } else {
2046 // Don't replace items for other profiles.
2047 itemsToRemove.add(id);
2048 continue;
2049 }
2050 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002051 if (user.equals(UserHandleCompat.myUserHandle())) {
2052 Launcher.addDumpLog(TAG,
2053 "constructing info for partially restored package",
2054 true);
Sunny Goyal34942622014-08-29 17:20:55 -07002055 info = getRestoredItemInfo(c, titleIndex, intent, promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002056 intent = getRestoredItemIntent(c, context, intent);
2057 } else {
2058 // Don't restore items for other profiles.
2059 itemsToRemove.add(id);
2060 continue;
2061 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002062 } else if (itemType ==
2063 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002064 info = getShortcutInfo(manager, intent, user, context, c,
2065 iconIndex, titleIndex, mLabelCache, allowMissingTarget);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 } else {
2067 info = getShortcutInfo(c, context, iconTypeIndex,
2068 iconPackageIndex, iconResourceIndex, iconIndex,
2069 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002070
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002071 // App shortcuts that used to be automatically added to Launcher
2072 // didn't always have the correct intent flags set, so do that
2073 // here
2074 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002075 intent.getCategories() != null &&
2076 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002077 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002078 intent.addFlags(
2079 Intent.FLAG_ACTIVITY_NEW_TASK |
2080 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2081 }
Michael Jurka96879562012-03-22 05:54:33 -07002082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002084 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002085 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002086 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002087 container = c.getInt(containerIndex);
2088 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002090 info.cellX = c.getInt(cellXIndex);
2091 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002092 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002093 info.spanX = 1;
2094 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002095 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002096 info.isDisabled = disabledState;
2097 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2098 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2099 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002100
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002101 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002102 if (!checkItemPlacement(occupied, info)) {
2103 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002104 break;
2105 }
2106
2107 switch (container) {
2108 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2109 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2110 sBgWorkspaceItems.add(info);
2111 break;
2112 default:
2113 // Item is in a user folder
2114 FolderInfo folderInfo =
2115 findOrMakeFolder(sBgFolders, container);
2116 folderInfo.add(info);
2117 break;
2118 }
2119 sBgItemsIdMap.put(info.id, info);
2120
2121 // now that we've loaded everthing re-save it with the
2122 // icon in case it disappears somehow.
2123 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002124 } else {
2125 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002126 }
2127 break;
2128
2129 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2130 id = c.getLong(idIndex);
2131 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2132
2133 folderInfo.title = c.getString(titleIndex);
2134 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002136 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002137 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002138 folderInfo.cellX = c.getInt(cellXIndex);
2139 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002140 folderInfo.spanX = 1;
2141 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142
Daniel Sandler8802e962010-05-26 16:28:16 -04002143 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002144 if (!checkItemPlacement(occupied, folderInfo)) {
2145 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002146 break;
2147 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002150 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2151 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2152 sBgWorkspaceItems.add(folderInfo);
2153 break;
Joe Onorato36115782010-06-17 13:28:48 -04002154 }
Joe Onorato17a89222011-02-08 17:26:11 -08002155
Chris Wrenf4d08112014-01-16 18:13:56 -05002156 if (restored) {
2157 // no special handling required for restored folders
2158 restoredRows.add(id);
2159 }
2160
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002161 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2162 sBgFolders.put(folderInfo.id, folderInfo);
2163 break;
2164
2165 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002166 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002167 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002168 boolean customWidget = itemType ==
2169 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2170
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002171 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002172 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002173 id = c.getLong(idIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002174 final ComponentName component =
2175 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002176
Sunny Goyal651077b2014-06-30 14:15:31 -07002177 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002178 final boolean isIdValid = (restoreStatus &
2179 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Joe Onorato36115782010-06-17 13:28:48 -04002180
Sunny Goyalff572272014-07-23 13:58:07 -07002181 final boolean wasProviderReady = (restoreStatus &
2182 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002183
Adam Cohen59400422014-03-05 18:07:04 -08002184 final LauncherAppWidgetProviderInfo provider =
2185 LauncherModel.getProviderInfo(context,
2186 ComponentName.unflattenFromString(savedProvider));
Sunny Goyalff572272014-07-23 13:58:07 -07002187
2188 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002189 if (!isSafeMode && !customWidget &&
2190 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002191 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002192 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002193
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002194 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002195 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002196 itemsToRemove.add(id);
2197 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002198 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002199 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2200 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002201
2202 if (!customWidget) {
2203 int[] minSpan =
2204 Launcher.getMinSpanForWidget(context, provider);
2205 appWidgetInfo.minSpanX = minSpan[0];
2206 appWidgetInfo.minSpanY = minSpan[1];
2207 }
Sunny Goyalff572272014-07-23 13:58:07 -07002208
2209 int status = restoreStatus;
2210 if (!wasProviderReady) {
2211 // If provider was not previously ready, update the
2212 // status and UI flag.
2213
2214 // Id would be valid only if the widget restore broadcast was received.
2215 if (isIdValid) {
2216 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2217 } else {
2218 status &= ~LauncherAppWidgetInfo
2219 .FLAG_PROVIDER_NOT_READY;
2220 }
2221 }
2222 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002223 } else {
2224 Log.v(TAG, "Widget restore pending id=" + id
2225 + " appWidgetId=" + appWidgetId
2226 + " status =" + restoreStatus);
2227 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002228 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002229 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal94485362014-09-18 16:13:58 -07002230
2231 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2232 // Restore has started once.
2233 } else if (installingPkgs.contains(component.getPackageName())) {
2234 // App restore has started. Update the flag
2235 appWidgetInfo.restoreStatus |=
2236 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002237 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002238 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002239 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002240 itemsToRemove.add(id);
2241 continue;
2242 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002243 }
Sunny Goyalff572272014-07-23 13:58:07 -07002244
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002245 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002247 appWidgetInfo.cellX = c.getInt(cellXIndex);
2248 appWidgetInfo.cellY = c.getInt(cellYIndex);
2249 appWidgetInfo.spanX = c.getInt(spanXIndex);
2250 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002251
Adam Cohen59400422014-03-05 18:07:04 -08002252 if (!customWidget) {
2253 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2254 appWidgetInfo.minSpanX = minSpan[0];
2255 appWidgetInfo.minSpanY = minSpan[1];
2256 }
2257
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002258 container = c.getInt(containerIndex);
2259 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2260 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2261 Log.e(TAG, "Widget found where container != " +
2262 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2263 continue;
2264 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002265
Adam Cohene25af792013-06-06 23:08:25 -07002266 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002267 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002268 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2269 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002270 break;
2271 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002272
Adam Cohen59400422014-03-05 18:07:04 -08002273 if (!customWidget) {
2274 String providerName =
2275 appWidgetInfo.providerName.flattenToString();
2276 if (!providerName.equals(savedProvider) ||
2277 (appWidgetInfo.restoreStatus != restoreStatus)) {
2278 ContentValues values = new ContentValues();
2279 values.put(
2280 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2281 providerName);
2282 values.put(LauncherSettings.Favorites.RESTORED,
2283 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002284 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002285 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002286 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002287 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2288 sBgAppWidgets.add(appWidgetInfo);
2289 }
Joe Onorato36115782010-06-17 13:28:48 -04002290 break;
2291 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002292 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002293 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002294 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 }
2296 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002297 if (c != null) {
2298 c.close();
2299 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002300 }
2301
Winson Chungba9c37f2013-08-30 14:11:37 -07002302 // Break early if we've stopped loading
2303 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002304 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002305 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002306 }
2307
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002308 if (itemsToRemove.size() > 0) {
2309 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002310 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002311 // Remove dead items
2312 for (long id : itemsToRemove) {
2313 if (DEBUG_LOADERS) {
2314 Log.d(TAG, "Removed id = " + id);
2315 }
2316 // Don't notify content observers
2317 try {
2318 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2319 null, null);
2320 } catch (RemoteException e) {
2321 Log.w(TAG, "Could not remove id = " + id);
2322 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002323 }
2324 }
2325
Chris Wrenf4d08112014-01-16 18:13:56 -05002326 if (restoredRows.size() > 0) {
2327 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002328 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002329 // Update restored items that no longer require special handling
2330 try {
2331 StringBuilder selectionBuilder = new StringBuilder();
2332 selectionBuilder.append(LauncherSettings.Favorites._ID);
2333 selectionBuilder.append(" IN (");
2334 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2335 selectionBuilder.append(")");
2336 ContentValues values = new ContentValues();
2337 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal34942622014-08-29 17:20:55 -07002338 updater.update(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Chris Wrenf4d08112014-01-16 18:13:56 -05002339 values, selectionBuilder.toString(), null);
2340 } catch (RemoteException e) {
2341 Log.w(TAG, "Could not update restored rows");
2342 }
2343 }
2344
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002345 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2346 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002347 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002348 null, sWorker);
2349 }
2350
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002351 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2352 // Log to disk
2353 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2354 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002355
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002356 // Remove any empty screens
2357 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2358 for (ItemInfo item: sBgItemsIdMap.values()) {
2359 long screenId = item.screenId;
2360 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2361 unusedScreens.contains(screenId)) {
2362 unusedScreens.remove(screenId);
2363 }
2364 }
2365
2366 // If there are any empty screens remove them, and update.
2367 if (unusedScreens.size() != 0) {
2368 // Log to disk
2369 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2370 TextUtils.join(", ", unusedScreens), true);
2371
2372 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002373 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002374 }
2375
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002376 if (DEBUG_LOADERS) {
2377 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2378 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002379 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002380 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002381 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002382
Daniel Sandler566da102013-06-25 23:43:45 -04002383 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002384 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002386 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002387 line += " | ";
2388 }
Winson Chung892c74d2013-08-22 16:15:50 -07002389 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002390 ItemInfo[][] screen = occupied.get(screenId);
2391 if (x < screen.length && y < screen[x].length) {
2392 line += (screen[x][y] != null) ? "#" : ".";
2393 } else {
2394 line += "!";
2395 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002396 }
Joe Onorato36115782010-06-17 13:28:48 -04002397 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002398 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 }
Joe Onorato36115782010-06-17 13:28:48 -04002401 }
Adam Cohene25af792013-06-06 23:08:25 -07002402 }
2403
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002404 /**
2405 * Partially updates the item without any notification. Must be called on the worker thread.
2406 */
2407 private void updateItem(long itemId, ContentValues update) {
2408 mContext.getContentResolver().update(
2409 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
2410 update,
2411 BaseColumns._ID + "= ?",
2412 new String[]{Long.toString(itemId)});
2413 }
2414
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 /** Filters the set of items who are directly or indirectly (via another container) on the
2416 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002417 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002418 ArrayList<ItemInfo> allWorkspaceItems,
2419 ArrayList<ItemInfo> currentScreenItems,
2420 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002421 // Purge any null ItemInfos
2422 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2423 while (iter.hasNext()) {
2424 ItemInfo i = iter.next();
2425 if (i == null) {
2426 iter.remove();
2427 }
2428 }
2429
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002430 // Order the set of items by their containers first, this allows use to walk through the
2431 // list sequentially, build up a list of containers that are in the specified screen,
2432 // as well as all items in those containers.
2433 Set<Long> itemsOnScreen = new HashSet<Long>();
2434 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2435 @Override
2436 public int compare(ItemInfo lhs, ItemInfo rhs) {
2437 return (int) (lhs.container - rhs.container);
2438 }
2439 });
2440 for (ItemInfo info : allWorkspaceItems) {
2441 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002442 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 currentScreenItems.add(info);
2444 itemsOnScreen.add(info.id);
2445 } else {
2446 otherScreenItems.add(info);
2447 }
2448 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2449 currentScreenItems.add(info);
2450 itemsOnScreen.add(info.id);
2451 } else {
2452 if (itemsOnScreen.contains(info.container)) {
2453 currentScreenItems.add(info);
2454 itemsOnScreen.add(info.id);
2455 } else {
2456 otherScreenItems.add(info);
2457 }
2458 }
2459 }
2460 }
2461
2462 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002463 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002464 ArrayList<LauncherAppWidgetInfo> appWidgets,
2465 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2466 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002467
2468 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002469 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002470 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002471 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002472 currentScreenWidgets.add(widget);
2473 } else {
2474 otherScreenWidgets.add(widget);
2475 }
2476 }
2477 }
2478
2479 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002480 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002481 HashMap<Long, ItemInfo> itemsIdMap,
2482 HashMap<Long, FolderInfo> folders,
2483 HashMap<Long, FolderInfo> currentScreenFolders,
2484 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002485
2486 for (long id : folders.keySet()) {
2487 ItemInfo info = itemsIdMap.get(id);
2488 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002489 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002490 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002491 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 currentScreenFolders.put(id, folder);
2493 } else {
2494 otherScreenFolders.put(id, folder);
2495 }
2496 }
2497 }
2498
2499 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2500 * right) */
2501 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002502 final LauncherAppState app = LauncherAppState.getInstance();
2503 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002504 // XXX: review this
2505 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002506 @Override
2507 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002508 int cellCountX = (int) grid.numColumns;
2509 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002510 int screenOffset = cellCountX * cellCountY;
2511 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002512 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002513 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002514 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002515 rhs.cellY * cellCountX + rhs.cellX);
2516 return (int) (lr - rr);
2517 }
2518 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002519 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002520
Adam Cohendcd297f2013-06-18 13:13:40 -07002521 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2522 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002523 final Runnable r = new Runnable() {
2524 @Override
2525 public void run() {
2526 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2527 if (callbacks != null) {
2528 callbacks.bindScreens(orderedScreens);
2529 }
2530 }
2531 };
2532 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2533 }
2534
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002535 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2536 final ArrayList<ItemInfo> workspaceItems,
2537 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2538 final HashMap<Long, FolderInfo> folders,
2539 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002540
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002541 final boolean postOnMainThread = (deferredBindRunnables != null);
2542
2543 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002544 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002545 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002546 final int start = i;
2547 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002548 final Runnable r = new Runnable() {
2549 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002550 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002551 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002552 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002553 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2554 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002555 }
2556 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002557 };
2558 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002559 synchronized (deferredBindRunnables) {
2560 deferredBindRunnables.add(r);
2561 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002562 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002563 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002564 }
Joe Onorato36115782010-06-17 13:28:48 -04002565 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002566
2567 // Bind the folders
2568 if (!folders.isEmpty()) {
2569 final Runnable r = new Runnable() {
2570 public void run() {
2571 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2572 if (callbacks != null) {
2573 callbacks.bindFolders(folders);
2574 }
2575 }
2576 };
2577 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002578 synchronized (deferredBindRunnables) {
2579 deferredBindRunnables.add(r);
2580 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002581 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002582 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002583 }
2584 }
2585
2586 // Bind the widgets, one at a time
2587 N = appWidgets.size();
2588 for (int i = 0; i < N; i++) {
2589 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2590 final Runnable r = new Runnable() {
2591 public void run() {
2592 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2593 if (callbacks != null) {
2594 callbacks.bindAppWidget(widget);
2595 }
2596 }
2597 };
2598 if (postOnMainThread) {
2599 deferredBindRunnables.add(r);
2600 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002601 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002602 }
2603 }
2604 }
2605
2606 /**
2607 * Binds all loaded data to actual views on the main thread.
2608 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002609 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002610 final long t = SystemClock.uptimeMillis();
2611 Runnable r;
2612
2613 // Don't use these two variables in any of the callback runnables.
2614 // Otherwise we hold a reference to them.
2615 final Callbacks oldCallbacks = mCallbacks.get();
2616 if (oldCallbacks == null) {
2617 // This launcher has exited and nobody bothered to tell us. Just bail.
2618 Log.w(TAG, "LoaderTask running with no launcher");
2619 return;
2620 }
2621
Winson Chung9b9fb962013-11-15 15:39:34 -08002622 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002623 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2624 ArrayList<LauncherAppWidgetInfo> appWidgets =
2625 new ArrayList<LauncherAppWidgetInfo>();
2626 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2627 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002628 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002629 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 workspaceItems.addAll(sBgWorkspaceItems);
2631 appWidgets.addAll(sBgAppWidgets);
2632 folders.putAll(sBgFolders);
2633 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002634 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002635 }
2636
Derek Prothro7aff3992013-12-10 14:00:37 -05002637 final boolean isLoadingSynchronously =
2638 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002639 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002640 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002641 if (currScreen >= orderedScreenIds.size()) {
2642 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002643 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002644 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002645 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002646 final long currentScreenId = currentScreen < 0
2647 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002648
2649 // Load all the items that are on the current page first (and in the process, unbind
2650 // all the existing workspace items before we call startBinding() below.
2651 unbindWorkspaceItemsOnMainThread();
2652
2653 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002654 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2655 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2656 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2657 new ArrayList<LauncherAppWidgetInfo>();
2658 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2659 new ArrayList<LauncherAppWidgetInfo>();
2660 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2661 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2662
Winson Chung9b9fb962013-11-15 15:39:34 -08002663 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002664 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002665 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002666 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002667 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002668 otherFolders);
2669 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2670 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2671
2672 // Tell the workspace that we're about to start binding items
2673 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002674 public void run() {
2675 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2676 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002677 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002678 }
2679 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002680 };
Winson Chung81b52252012-08-27 15:34:29 -07002681 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002682
Adam Cohendcd297f2013-06-18 13:13:40 -07002683 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2684
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002685 // Load items on the current page
2686 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2687 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002688 if (isLoadingSynchronously) {
2689 r = new Runnable() {
2690 public void run() {
2691 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002692 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002693 callbacks.onPageBoundSynchronously(currentScreen);
2694 }
2695 }
2696 };
Winson Chung81b52252012-08-27 15:34:29 -07002697 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002698 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002699
Winson Chung4a2afa32012-07-19 14:53:05 -07002700 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2701 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002702 synchronized (mDeferredBindRunnables) {
2703 mDeferredBindRunnables.clear();
2704 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002705 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002706 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002707
2708 // Tell the workspace that we're done binding items
2709 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002710 public void run() {
2711 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2712 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002713 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002714 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002715
Winson Chung98e030b2012-05-07 16:01:11 -07002716 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002717 if (DEBUG_LOADERS) {
2718 Log.d(TAG, "bound workspace in "
2719 + (SystemClock.uptimeMillis()-t) + "ms");
2720 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002721
2722 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002723 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002724 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002725 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002726 synchronized (mDeferredBindRunnables) {
2727 mDeferredBindRunnables.add(r);
2728 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002729 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002730 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002731 }
Joe Onorato36115782010-06-17 13:28:48 -04002732 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002733
Joe Onorato36115782010-06-17 13:28:48 -04002734 private void loadAndBindAllApps() {
2735 if (DEBUG_LOADERS) {
2736 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2737 }
2738 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002739 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002740 synchronized (LoaderTask.this) {
2741 if (mStopped) {
2742 return;
2743 }
2744 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002745 }
Joe Onorato36115782010-06-17 13:28:48 -04002746 } else {
2747 onlyBindAllApps();
2748 }
2749 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002750
Joe Onorato36115782010-06-17 13:28:48 -04002751 private void onlyBindAllApps() {
2752 final Callbacks oldCallbacks = mCallbacks.get();
2753 if (oldCallbacks == null) {
2754 // This launcher has exited and nobody bothered to tell us. Just bail.
2755 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2756 return;
2757 }
2758
2759 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002760 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002761 final ArrayList<AppInfo> list
2762 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002763 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002764 public void run() {
2765 final long t = SystemClock.uptimeMillis();
2766 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2767 if (callbacks != null) {
2768 callbacks.bindAllApplications(list);
2769 }
2770 if (DEBUG_LOADERS) {
2771 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2772 + (SystemClock.uptimeMillis()-t) + "ms");
2773 }
2774 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002775 };
2776 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002777 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002778 r.run();
2779 } else {
2780 mHandler.post(r);
2781 }
Joe Onorato36115782010-06-17 13:28:48 -04002782 }
2783
Winson Chung64359a52013-07-08 17:17:08 -07002784 private void loadAllApps() {
2785 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002786
Joe Onorato36115782010-06-17 13:28:48 -04002787 final Callbacks oldCallbacks = mCallbacks.get();
2788 if (oldCallbacks == null) {
2789 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002790 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002791 return;
2792 }
2793
2794 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2795 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2796
Kenny Guyed131872014-04-30 03:02:21 +01002797 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2798
Winson Chung64359a52013-07-08 17:17:08 -07002799 // Clear the list of apps
2800 mBgAllAppsList.clear();
Sunny Goyale0f58d72014-11-10 18:05:31 -08002801 SharedPreferences prefs = mContext.getSharedPreferences(
2802 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
Kenny Guyed131872014-04-30 03:02:21 +01002803 for (UserHandleCompat user : profiles) {
2804 // Query for the set of apps
2805 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2806 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2807 if (DEBUG_LOADERS) {
2808 Log.d(TAG, "getActivityList took "
2809 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2810 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2811 }
2812 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002813 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002814 if (apps == null || apps.isEmpty()) {
2815 return;
2816 }
2817 // Sort the applications by name
2818 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2819 Collections.sort(apps,
2820 new LauncherModel.ShortcutNameComparator(mLabelCache));
2821 if (DEBUG_LOADERS) {
2822 Log.d(TAG, "sort took "
2823 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2824 }
Joe Onorato36115782010-06-17 13:28:48 -04002825
Kenny Guyed131872014-04-30 03:02:21 +01002826 // Create the ApplicationInfos
2827 for (int i = 0; i < apps.size(); i++) {
2828 LauncherActivityInfoCompat app = apps.get(i);
2829 // This builds the icon bitmaps.
2830 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2831 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002832
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08002833 if (ADD_MANAGED_PROFILE_SHORTCUTS && !user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002834 // Add shortcuts for packages which were installed while launcher was dead.
2835 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
2836 + mUserManager.getSerialNumberForUser(user);
2837 Set<String> packagesAdded = prefs.getStringSet(shortcutsSetKey, Collections.EMPTY_SET);
2838 HashSet<String> newPackageSet = new HashSet<String>();
2839
2840 for (LauncherActivityInfoCompat info : apps) {
2841 String packageName = info.getComponentName().getPackageName();
2842 if (!packagesAdded.contains(packageName)
2843 && !newPackageSet.contains(packageName)) {
2844 InstallShortcutReceiver.queueInstallShortcut(info, mContext);
2845 }
2846 newPackageSet.add(packageName);
2847 }
2848
2849 prefs.edit().putStringSet(shortcutsSetKey, newPackageSet).commit();
2850 }
Winson Chung64359a52013-07-08 17:17:08 -07002851 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002852 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002853 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2854 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002855
2856 // Post callback on main thread
2857 mHandler.post(new Runnable() {
2858 public void run() {
2859 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002860 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002861 if (callbacks != null) {
2862 callbacks.bindAllApplications(added);
2863 if (DEBUG_LOADERS) {
2864 Log.d(TAG, "bound " + added.size() + " apps in "
2865 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2866 }
2867 } else {
2868 Log.i(TAG, "not binding apps: no Launcher activity");
2869 }
2870 }
2871 });
2872
Joe Onorato36115782010-06-17 13:28:48 -04002873 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002874 Log.d(TAG, "Icons processed in "
2875 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002876 }
2877 }
2878
2879 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002880 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002881 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2882 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2883 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2884 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2885 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2886 }
Joe Onorato36115782010-06-17 13:28:48 -04002887 }
2888 }
2889
2890 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002891 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002892 }
2893
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002894 private class AppsAvailabilityCheck extends BroadcastReceiver {
2895
2896 @Override
2897 public void onReceive(Context context, Intent intent) {
2898 synchronized (sBgLock) {
2899 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2900 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002901 final PackageManager manager = context.getPackageManager();
2902 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2903 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002904 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2905 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002906 packagesRemoved.clear();
2907 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002908 for (String pkg : entry.getValue()) {
2909 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002910 boolean packageOnSdcard = launcherApps.isAppEnabled(
2911 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2912 if (packageOnSdcard) {
2913 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2914 packagesUnavailable.add(pkg);
2915 } else {
2916 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2917 packagesRemoved.add(pkg);
2918 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002919 }
2920 }
2921 if (!packagesRemoved.isEmpty()) {
2922 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2923 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2924 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002925 if (!packagesUnavailable.isEmpty()) {
2926 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2927 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2928 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002929 }
Sunny Goyal34942622014-08-29 17:20:55 -07002930 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002931 }
2932 }
2933 }
2934
Joe Onorato36115782010-06-17 13:28:48 -04002935 private class PackageUpdatedTask implements Runnable {
2936 int mOp;
2937 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002938 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002939
2940 public static final int OP_NONE = 0;
2941 public static final int OP_ADD = 1;
2942 public static final int OP_UPDATE = 2;
2943 public static final int OP_REMOVE = 3; // uninstlled
2944 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2945
2946
Kenny Guyed131872014-04-30 03:02:21 +01002947 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002948 mOp = op;
2949 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002950 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002951 }
2952
2953 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002954 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002955
2956 final String[] packages = mPackages;
2957 final int N = packages.length;
2958 switch (mOp) {
2959 case OP_ADD:
2960 for (int i=0; i<N; i++) {
2961 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002962 mIconCache.remove(packages[i], mUser);
2963 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002964 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002965
2966 // Auto add shortcuts for added packages.
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08002967 if (ADD_MANAGED_PROFILE_SHORTCUTS
2968 && !UserHandleCompat.myUserHandle().equals(mUser)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002969 SharedPreferences prefs = context.getSharedPreferences(
2970 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
2971 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
2972 + mUserManager.getSerialNumberForUser(mUser);
2973 Set<String> shortcutSet = new HashSet<String>(
2974 prefs.getStringSet(shortcutsSetKey,Collections.EMPTY_SET));
2975
2976 for (int i=0; i<N; i++) {
2977 if (!shortcutSet.contains(packages[i])) {
2978 shortcutSet.add(packages[i]);
2979 List<LauncherActivityInfoCompat> activities =
2980 mLauncherApps.getActivityList(packages[i], mUser);
2981 if (activities != null && !activities.isEmpty()) {
2982 InstallShortcutReceiver.queueInstallShortcut(
2983 activities.get(0), context);
2984 }
2985 }
2986 }
2987
2988 prefs.edit().putStringSet(shortcutsSetKey, shortcutSet).commit();
2989 }
Joe Onorato36115782010-06-17 13:28:48 -04002990 break;
2991 case OP_UPDATE:
2992 for (int i=0; i<N; i++) {
2993 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002994 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07002995 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04002996 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04002997 }
2998 break;
2999 case OP_REMOVE:
Sunny Goyale0f58d72014-11-10 18:05:31 -08003000 // Remove the packageName for the set of auto-installed shortcuts. This
3001 // will ensure that the shortcut when the app is installed again.
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08003002 if (ADD_MANAGED_PROFILE_SHORTCUTS
3003 && !UserHandleCompat.myUserHandle().equals(mUser)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003004 SharedPreferences prefs = context.getSharedPreferences(
3005 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
3006 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
3007 + mUserManager.getSerialNumberForUser(mUser);
3008 HashSet<String> shortcutSet = new HashSet<String>(
3009 prefs.getStringSet(shortcutsSetKey, Collections.EMPTY_SET));
3010 shortcutSet.removeAll(Arrays.asList(mPackages));
3011 prefs.edit().putStringSet(shortcutsSetKey, shortcutSet).commit();
3012 }
3013 // Fall through
Joe Onorato36115782010-06-17 13:28:48 -04003014 case OP_UNAVAILABLE:
Sunny Goyal1a745e82014-10-02 15:58:31 -07003015 boolean clearCache = mOp == OP_REMOVE;
Joe Onorato36115782010-06-17 13:28:48 -04003016 for (int i=0; i<N; i++) {
3017 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal1a745e82014-10-02 15:58:31 -07003018 mBgAllAppsList.removePackage(packages[i], mUser, clearCache);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07003019 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04003020 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04003021 }
3022 break;
3023 }
3024
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003025 ArrayList<AppInfo> added = null;
3026 ArrayList<AppInfo> modified = null;
3027 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003028
Adam Cohen487f7dd2012-06-28 18:12:10 -07003029 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003030 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003031 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003032 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003033 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003034 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003035 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003036 }
Winson Chung5d55f332012-07-16 20:45:03 -07003037 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003038 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003039 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003040 }
3041
Sunny Goyale0f58d72014-11-10 18:05:31 -08003042 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003043 if (callbacks == null) {
3044 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3045 return;
3046 }
3047
Sunny Goyal4390ace2014-10-13 11:33:11 -07003048 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3049 new HashMap<ComponentName, AppInfo>();
3050
Joe Onorato36115782010-06-17 13:28:48 -04003051 if (added != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003052 addAppsToAllApps(context, added);
Sunny Goyal4390ace2014-10-13 11:33:11 -07003053 for (AppInfo ai : added) {
3054 addedOrUpdatedApps.put(ai.componentName, ai);
3055 }
Joe Onorato36115782010-06-17 13:28:48 -04003056 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003057
Joe Onorato36115782010-06-17 13:28:48 -04003058 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003059 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003060 for (AppInfo ai : modified) {
3061 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003062 }
3063
Joe Onorato36115782010-06-17 13:28:48 -04003064 mHandler.post(new Runnable() {
3065 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003066 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003067 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003068 callbacks.bindAppsUpdated(modifiedFinal);
3069 }
3070 }
3071 });
3072 }
Winson Chung83892cc2013-05-01 16:53:33 -07003073
Sunny Goyal4390ace2014-10-13 11:33:11 -07003074 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003075 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003076 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3077 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3078 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3079
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003080 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003081 synchronized (sBgLock) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003082 for (ItemInfo info : sBgItemsIdMap.values()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003083 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3084 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003085 boolean infoUpdated = false;
3086 boolean shortcutUpdated = false;
3087
3088 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003089 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003090 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003091 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3092 si.iconResource.resourceName, mIconCache, context);
3093 if (icon != null) {
3094 si.setIcon(icon);
3095 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003096 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003097 }
3098 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003099
3100 ComponentName cn = si.getTargetComponent();
3101 if (cn != null && packageSet.contains(cn.getPackageName())) {
3102 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3103
3104 if (si.isPromise()) {
3105 mIconCache.deletePreloadedIcon(cn, mUser);
3106 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3107 // Auto install icon
3108 PackageManager pm = context.getPackageManager();
3109 ResolveInfo matched = pm.resolveActivity(
3110 new Intent(Intent.ACTION_MAIN)
3111 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3112 PackageManager.MATCH_DEFAULT_ONLY);
3113 if (matched == null) {
3114 // Try to find the best match activity.
3115 Intent intent = pm.getLaunchIntentForPackage(
3116 cn.getPackageName());
3117 if (intent != null) {
3118 cn = intent.getComponent();
3119 appInfo = addedOrUpdatedApps.get(cn);
3120 }
3121
3122 if ((intent == null) || (appInfo == null)) {
3123 removedShortcuts.add(si);
3124 continue;
3125 }
3126 si.promisedIntent = intent;
3127 }
3128 }
3129
3130 // Restore the shortcut.
3131 si.intent = si.promisedIntent;
3132 si.promisedIntent = null;
3133 si.status &= ~ShortcutInfo.FLAG_RESTORED_ICON
3134 & ~ShortcutInfo.FLAG_AUTOINTALL_ICON
3135 & ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
3136
3137 infoUpdated = true;
3138 si.updateIcon(mIconCache);
3139 }
3140
3141 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3142 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3143 si.updateIcon(mIconCache);
3144 si.title = appInfo.title.toString();
3145 si.contentDescription = appInfo.contentDescription;
3146 infoUpdated = true;
3147 }
3148
3149 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3150 // Since package was just updated, the target must be available now.
3151 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3152 shortcutUpdated = true;
3153 }
3154 }
3155
3156 if (infoUpdated || shortcutUpdated) {
3157 updatedShortcuts.add(si);
3158 }
3159 if (infoUpdated) {
3160 updateItemInDatabase(context, si);
3161 }
3162 } else if (info instanceof LauncherAppWidgetInfo) {
3163 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3164 if (mUser.equals(widgetInfo.user)
3165 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3166 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3167 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3168 widgets.add(widgetInfo);
3169 updateItemInDatabase(context, widgetInfo);
3170 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003171 }
3172 }
3173 }
3174
Sunny Goyal4390ace2014-10-13 11:33:11 -07003175 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3176 mHandler.post(new Runnable() {
3177
3178 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003179 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003180 if (callbacks == cb && cb != null) {
3181 callbacks.bindShortcutsChanged(
3182 updatedShortcuts, removedShortcuts, mUser);
3183 }
3184 }
3185 });
3186 if (!removedShortcuts.isEmpty()) {
3187 deleteItemsFromDatabase(context, removedShortcuts);
3188 }
3189 }
3190 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003191 mHandler.post(new Runnable() {
3192 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003193 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003194 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003195 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003196 }
3197 }
3198 });
3199 }
3200 }
3201
Winson Chungdf95eb12013-10-16 14:57:07 -07003202 final ArrayList<String> removedPackageNames =
3203 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003204 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003205 // Mark all packages in the broadcast to be removed
3206 removedPackageNames.addAll(Arrays.asList(packages));
3207 } else if (mOp == OP_UPDATE) {
3208 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003209 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003210 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003211 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003212 }
3213 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003214 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003215
Winson Chungdf95eb12013-10-16 14:57:07 -07003216 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003217 final int removeReason;
3218 if (mOp == OP_UNAVAILABLE) {
3219 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3220 } else {
3221 // Remove all the components associated with this package
3222 for (String pn : removedPackageNames) {
3223 deletePackageFromDatabase(context, pn, mUser);
3224 }
3225 // Remove all the specific components
3226 for (AppInfo a : removedApps) {
3227 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3228 deleteItemsFromDatabase(context, infos);
3229 }
3230 removeReason = 0;
3231 }
3232
Winson Chungdf95eb12013-10-16 14:57:07 -07003233 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003234 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003235 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003236 mHandler.post(new Runnable() {
3237 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003238 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003239 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003240 callbacks.bindComponentsRemoved(
3241 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003242 }
3243 }
3244 });
Joe Onoratobe386092009-11-17 17:32:16 -08003245 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003246
Michael Jurkac402cd92013-05-20 15:49:32 +02003247 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01003248 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07003249 mHandler.post(new Runnable() {
3250 @Override
3251 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003252 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003253 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003254 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07003255 }
3256 }
3257 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003258
3259 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003260 mHandler.post(new Runnable() {
3261 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003262 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003263 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003264 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003265 }
3266 }
3267 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 }
3269 }
3270
Adam Cohen59400422014-03-05 18:07:04 -08003271 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context) {
3272 synchronized (sBgLock) {
3273 if (sBgWidgetProviders != null && !sWidgetProvidersDirty) {
3274 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3275 }
3276 sBgWidgetProviders = new HashMap<ComponentName, LauncherAppWidgetProviderInfo>();
3277 List<AppWidgetProviderInfo> widgets =
3278 AppWidgetManagerCompat.getInstance(context).getAllProviders();
3279 LauncherAppWidgetProviderInfo info;
3280 for (AppWidgetProviderInfo pInfo : widgets) {
3281 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3282 sBgWidgetProviders.put(info.provider, info);
3283 }
3284
3285 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3286 for (CustomAppWidget widget : customWidgets) {
3287 info = new LauncherAppWidgetProviderInfo(context, widget);
3288 sBgWidgetProviders.put(info.provider, info);
3289 }
3290 sWidgetProvidersDirty = false;
3291 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3292 }
3293 }
3294
3295 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name) {
3296 synchronized (sBgLock) {
3297 if (sBgWidgetProviders == null) {
3298 getWidgetProviders(ctx);
3299 }
3300 return sBgWidgetProviders.get(name);
3301 }
3302 }
3303
Michael Jurkac402cd92013-05-20 15:49:32 +02003304 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
3305 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
3306 PackageManager packageManager = context.getPackageManager();
3307 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Adam Cohen59400422014-03-05 18:07:04 -08003308 widgetsAndShortcuts.addAll(getWidgetProviders(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003309 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3310 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003311 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003312 return widgetsAndShortcuts;
3313 }
3314
Kenny Guyed131872014-04-30 03:02:21 +01003315 private static boolean isPackageDisabled(Context context, String packageName,
3316 UserHandleCompat user) {
3317 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3318 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003319 }
Adam Cohen556f6132014-01-15 15:18:08 -08003320
Kenny Guyed131872014-04-30 03:02:21 +01003321 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3322 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003323 if (cn == null) {
3324 return false;
3325 }
Kenny Guyed131872014-04-30 03:02:21 +01003326 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3327 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003328 return false;
3329 }
Kenny Guyed131872014-04-30 03:02:21 +01003330 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003331 }
3332
Adam Cohena28b78e2014-05-20 17:03:04 -07003333 public static boolean isValidPackage(Context context, String packageName,
3334 UserHandleCompat user) {
3335 if (packageName == null) {
3336 return false;
3337 }
3338 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3339 return launcherApps.isPackageEnabledForProfile(packageName, user);
3340 }
3341
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003343 * Make an ShortcutInfo object for a restored application or shortcut item that points
3344 * to a package that is not yet installed on the system.
3345 */
Sunny Goyal34942622014-08-29 17:20:55 -07003346 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
3347 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003348 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003349 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34942622014-08-29 17:20:55 -07003350 mIconCache.getTitleAndIcon(info, intent, info.user, true);
3351
3352 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3353 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3354 if (!TextUtils.isEmpty(title)) {
3355 info.title = title;
3356 }
3357 info.status = ShortcutInfo.FLAG_RESTORED_ICON;
3358 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3359 if (TextUtils.isEmpty(info.title)) {
3360 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3361 }
3362 info.status = ShortcutInfo.FLAG_AUTOINTALL_ICON;
3363 } else {
3364 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3365 }
3366
Kenny Guyc2bd8102014-06-30 12:30:31 +01003367 info.contentDescription = mUserManager.getBadgedLabelForUser(
3368 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003369 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003370 info.promisedIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05003371 return info;
3372 }
3373
3374 /**
3375 * Make an Intent object for a restored application or shortcut item that points
3376 * to the market page for the item.
3377 */
3378 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
3379 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003380 return getMarketIntent(componentName.getPackageName());
3381 }
3382
3383 static Intent getMarketIntent(String packageName) {
3384 return new Intent(Intent.ACTION_VIEW)
3385 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003386 .scheme("market")
3387 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003388 .appendQueryParameter("id", packageName)
3389 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003390 }
3391
3392 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003393 * This is called from the code that adds shortcuts from the intent receiver. This
3394 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 */
Kenny Guyed131872014-04-30 03:02:21 +01003396 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3397 UserHandleCompat user, Context context) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003398 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null, false);
Joe Onorato56d82912010-03-07 14:32:10 -05003399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400
Joe Onorato56d82912010-03-07 14:32:10 -05003401 /**
3402 * Make an ShortcutInfo object for a shortcut that is an application.
3403 *
3404 * If c is not null, then it will be used to fill in missing data like the title and icon.
3405 */
Kenny Guyed131872014-04-30 03:02:21 +01003406 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3407 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003408 HashMap<Object, CharSequence> labelCache, boolean allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003409 if (user == null) {
3410 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003411 return null;
3412 }
3413
Kenny Guyed131872014-04-30 03:02:21 +01003414 ComponentName componentName = intent.getComponent();
3415 if (componentName == null) {
3416 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3417 return null;
3418 }
3419
3420 Intent newIntent = new Intent(intent.getAction(), null);
3421 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3422 newIntent.setComponent(componentName);
3423 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003424 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003425 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3426 return null;
3427 }
3428
3429 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003430
Joe Onorato56d82912010-03-07 14:32:10 -05003431 // the resource -- This may implicitly give us back the fallback icon,
3432 // but don't worry about that. All we're doing with usingFallbackIcon is
3433 // to avoid saving lots of copies of that in the database, and most apps
3434 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003435 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003436
Joe Onorato56d82912010-03-07 14:32:10 -05003437 // the db
3438 if (icon == null) {
3439 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003440 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003441 }
3442 }
3443 // the fallback icon
3444 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003445 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003446 info.usingFallbackIcon = true;
3447 }
3448 info.setIcon(icon);
3449
Kenny Guyed131872014-04-30 03:02:21 +01003450 // From the cache.
3451 if (labelCache != null) {
3452 info.title = labelCache.get(componentName);
3453 }
3454
Joe Onorato56d82912010-03-07 14:32:10 -05003455 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003456 if (info.title == null && lai != null) {
3457 info.title = lai.getLabel();
3458 if (labelCache != null) {
3459 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003460 }
Joe Onorato56d82912010-03-07 14:32:10 -05003461 }
3462 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003464 if (c != null) {
3465 info.title = c.getString(titleIndex);
3466 }
3467 }
3468 // fall back to the class name of the activity
3469 if (info.title == null) {
3470 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003471 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003472 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003473 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003474 info.contentDescription = mUserManager.getBadgedLabelForUser(
3475 info.title.toString(), info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003477 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003478
Winson Chung64359a52013-07-08 17:17:08 -07003479 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3480 ItemInfoFilter f) {
3481 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3482 for (ItemInfo i : infos) {
3483 if (i instanceof ShortcutInfo) {
3484 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003485 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003486 if (cn != null && f.filterItem(null, info, cn)) {
3487 filtered.add(info);
3488 }
3489 } else if (i instanceof FolderInfo) {
3490 FolderInfo info = (FolderInfo) i;
3491 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003492 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003493 if (cn != null && f.filterItem(info, s, cn)) {
3494 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003495 }
3496 }
Winson Chung64359a52013-07-08 17:17:08 -07003497 } else if (i instanceof LauncherAppWidgetInfo) {
3498 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3499 ComponentName cn = info.providerName;
3500 if (cn != null && f.filterItem(null, info, cn)) {
3501 filtered.add(info);
3502 }
Winson Chung8a435102012-08-30 17:16:53 -07003503 }
3504 }
Winson Chung64359a52013-07-08 17:17:08 -07003505 return new ArrayList<ItemInfo>(filtered);
3506 }
3507
Kenny Guyed131872014-04-30 03:02:21 +01003508 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3509 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003510 ItemInfoFilter filter = new ItemInfoFilter() {
3511 @Override
3512 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003513 if (info.user == null) {
3514 return cn.equals(cname);
3515 } else {
3516 return cn.equals(cname) && info.user.equals(user);
3517 }
Winson Chung64359a52013-07-08 17:17:08 -07003518 }
3519 };
3520 return filterItemInfos(sBgItemsIdMap.values(), filter);
3521 }
3522
Sunny Goyal1a745e82014-10-02 15:58:31 -07003523 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003524 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003525 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003526 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003527 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3528 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003529
Joe Onorato56d82912010-03-07 14:32:10 -05003530 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003531 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003532 // Non-app shortcuts are only supported for current user.
3533 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003535
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003536 // TODO: If there's an explicit component and we can't install that, delete it.
3537
Joe Onorato56d82912010-03-07 14:32:10 -05003538 info.title = c.getString(titleIndex);
3539
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 int iconType = c.getInt(iconTypeIndex);
3541 switch (iconType) {
3542 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3543 String packageName = c.getString(iconPackageIndex);
3544 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003545 info.customIcon = false;
3546 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003547 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003548 // the db
3549 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003550 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003551 }
3552 // the fallback icon
3553 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003554 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003555 info.usingFallbackIcon = true;
3556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 break;
3558 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003559 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003560 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003561 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003562 info.customIcon = false;
3563 info.usingFallbackIcon = true;
3564 } else {
3565 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 break;
3568 default:
Kenny Guyed131872014-04-30 03:02:21 +01003569 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003570 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 info.customIcon = false;
3572 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003573 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003574 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 return info;
3576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003577
Michael Jurka931dc972011-08-05 15:08:15 -07003578 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003579 @SuppressWarnings("all") // suppress dead code warning
3580 final boolean debug = false;
3581 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003582 Log.d(TAG, "getIconFromCursor app="
3583 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3584 }
3585 byte[] data = c.getBlob(iconIndex);
3586 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003587 return Utilities.createIconBitmap(
3588 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003589 } catch (Exception e) {
3590 return null;
3591 }
3592 }
3593
Sunny Goyal2350bc92014-10-14 16:42:54 -07003594 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003595 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3596 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3597 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3598
Adam Cohend9198822011-11-22 16:42:47 -08003599 if (intent == null) {
3600 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3601 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3602 return null;
3603 }
3604
Joe Onorato0589f0f2010-02-08 13:44:00 -08003605 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003606 boolean customIcon = false;
3607 ShortcutIconResource iconResource = null;
3608
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003609 if (bitmap instanceof Bitmap) {
3610 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003611 customIcon = true;
3612 } else {
3613 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003614 if (extra instanceof ShortcutIconResource) {
3615 iconResource = (ShortcutIconResource) extra;
3616 icon = Utilities.createIconBitmap(iconResource.packageName,
3617 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003618 }
3619 }
3620
Michael Jurkac9d95c52011-08-29 14:03:34 -07003621 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003622
Kenny Guyed131872014-04-30 03:02:21 +01003623 // Only support intents for current user for now. Intents sent from other
3624 // users wouldn't get here without intent forwarding anyway.
3625 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003626 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003627 icon = mIconCache.getDefaultIcon(info.user);
3628 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003629 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003630 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003631
Joe Onorato0589f0f2010-02-08 13:44:00 -08003632 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003633 info.contentDescription = mUserManager.getBadgedLabelForUser(
3634 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003635 info.intent = intent;
3636 info.customIcon = customIcon;
3637 info.iconResource = iconResource;
3638
3639 return info;
3640 }
3641
Winson Chungaac01e12011-08-17 10:37:13 -07003642 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3643 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003644 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003645 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003646 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003647 }
Joe Onorato56d82912010-03-07 14:32:10 -05003648 // If this icon doesn't have a custom icon, check to see
3649 // what's stored in the DB, and if it doesn't match what
3650 // we're going to show, store what we are going to show back
3651 // into the DB. We do this so when we're loading, if the
3652 // package manager can't find an icon (for example because
3653 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003654 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003655 cache.put(info, c.getBlob(iconIndex));
3656 return true;
3657 }
3658 return false;
3659 }
3660 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3661 boolean needSave = false;
3662 try {
3663 if (data != null) {
3664 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3665 Bitmap loaded = info.getIcon(mIconCache);
3666 needSave = !saved.sameAs(loaded);
3667 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003668 needSave = true;
3669 }
Winson Chungaac01e12011-08-17 10:37:13 -07003670 } catch (Exception e) {
3671 needSave = true;
3672 }
3673 if (needSave) {
3674 Log.d(TAG, "going to save icon bitmap for info=" + info);
3675 // This is slower than is ideal, but this only happens once
3676 // or when the app is updated with a new icon.
3677 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003678 }
3679 }
3680
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003682 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 * or make a new one.
3684 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003685 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 // See if a placeholder was created for us already
3687 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003688 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003689 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003690 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 folders.put(id, folderInfo);
3692 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003693 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
3695
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003696 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003697 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003698 return new Comparator<AppInfo>() {
3699 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003700 if (a.user.equals(b.user)) {
3701 int result = collator.compare(a.title.toString().trim(),
3702 b.title.toString().trim());
3703 if (result == 0) {
3704 result = a.componentName.compareTo(b.componentName);
3705 }
3706 return result;
3707 } else {
3708 // TODO Need to figure out rules for sorting
3709 // profiles, this puts work second.
3710 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003711 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003712 }
Winson Chung11904872012-09-17 16:58:46 -07003713 };
3714 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003715 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3716 = new Comparator<AppInfo>() {
3717 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003718 if (a.firstInstallTime < b.firstInstallTime) return 1;
3719 if (a.firstInstallTime > b.firstInstallTime) return -1;
3720 return 0;
3721 }
3722 };
Winson Chung5308f242011-08-18 12:12:41 -07003723 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3724 if (info.activityInfo != null) {
3725 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3726 } else {
3727 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3728 }
3729 }
Kenny Guyed131872014-04-30 03:02:21 +01003730 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003731 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003732 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003733 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003734 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003735 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003736 }
Kenny Guyed131872014-04-30 03:02:21 +01003737 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003738 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003739 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003740 }
Kenny Guyed131872014-04-30 03:02:21 +01003741 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003742 String labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003743 ComponentName keyA = a.getComponentName();
3744 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003745 if (mLabelCache.containsKey(keyA)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003746 labelA = mLabelCache.get(keyA).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003747 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003748 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003749
Winson Chung5308f242011-08-18 12:12:41 -07003750 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003751 }
Winson Chung5308f242011-08-18 12:12:41 -07003752 if (mLabelCache.containsKey(keyB)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003753 labelB = mLabelCache.get(keyB).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003754 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003755 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003756
Winson Chung5308f242011-08-18 12:12:41 -07003757 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003758 }
Winson Chung11904872012-09-17 16:58:46 -07003759 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003760 }
3761 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003762 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003763 private final AppWidgetManagerCompat mManager;
3764 private final PackageManager mPackageManager;
3765 private final HashMap<Object, String> mLabelCache;
3766 private final Collator mCollator;
3767
3768 WidgetAndShortcutNameComparator(Context context) {
3769 mManager = AppWidgetManagerCompat.getInstance(context);
3770 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003771 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003772 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003773 }
3774 public final int compare(Object a, Object b) {
3775 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003776 if (mLabelCache.containsKey(a)) {
3777 labelA = mLabelCache.get(a);
3778 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003779 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3780 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003781 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003782 mLabelCache.put(a, labelA);
3783 }
3784 if (mLabelCache.containsKey(b)) {
3785 labelB = mLabelCache.get(b);
3786 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003787 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c165aa2015-01-30 10:28:23 -08003788 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003789 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003790 mLabelCache.put(b, labelB);
3791 }
Winson Chung11904872012-09-17 16:58:46 -07003792 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003793 }
3794 };
Joe Onoratobe386092009-11-17 17:32:16 -08003795
Sunny Goyal651077b2014-06-30 14:15:31 -07003796 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3797 return (provider != null) && (provider.provider != null)
3798 && (provider.provider.getPackageName() != null);
3799 }
3800
Joe Onoratobe386092009-11-17 17:32:16 -08003801 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003802 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003803 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3804 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3805 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3806 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003807 if (mLoaderTask != null) {
3808 mLoaderTask.dumpState();
3809 } else {
3810 Log.d(TAG, "mLoaderTask=null");
3811 }
Joe Onoratobe386092009-11-17 17:32:16 -08003812 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003813
3814 public Callbacks getCallback() {
3815 return mCallbacks != null ? mCallbacks.get() : null;
3816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003817}