blob: 3983835dc13a667298b4d5483b355135a9be90eb [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
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08001647 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07001648 // Ensure that all the applications that are in the system are
1649 // represented on the home screen.
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001650 verifyApplications();
Winson Chungc763c4e2013-07-19 13:49:06 -07001651 }
1652
Joe Onorato36115782010-06-17 13:28:48 -04001653 // Clear out this reference, otherwise we end up holding it until all of the
1654 // callback runnables are done.
1655 mContext = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001656
Joe Onorato36115782010-06-17 13:28:48 -04001657 synchronized (mLock) {
1658 // If we are still the last one to be scheduled, remove ourselves.
1659 if (mLoaderTask == this) {
1660 mLoaderTask = null;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001662 mIsLoaderTaskRunning = false;
Joe Onorato36115782010-06-17 13:28:48 -04001663 }
Joe Onorato36115782010-06-17 13:28:48 -04001664 }
1665
1666 public void stopLocked() {
1667 synchronized (LoaderTask.this) {
1668 mStopped = true;
1669 this.notify();
1670 }
1671 }
1672
1673 /**
1674 * Gets the callbacks object. If we've been stopped, or if the launcher object
1675 * has somehow been garbage collected, return null instead. Pass in the Callbacks
1676 * object that was around when the deferred message was scheduled, and if there's
1677 * a new Callbacks object around then also return null. This will save us from
1678 * calling onto it with data that will be ignored.
1679 */
1680 Callbacks tryGetCallbacks(Callbacks oldCallbacks) {
1681 synchronized (mLock) {
1682 if (mStopped) {
1683 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001684 }
Joe Onorato36115782010-06-17 13:28:48 -04001685
1686 if (mCallbacks == null) {
1687 return null;
Daniel Sandler8802e962010-05-26 16:28:16 -04001688 }
Joe Onorato36115782010-06-17 13:28:48 -04001689
1690 final Callbacks callbacks = mCallbacks.get();
1691 if (callbacks != oldCallbacks) {
1692 return null;
1693 }
1694 if (callbacks == null) {
1695 Log.w(TAG, "no mCallbacks");
1696 return null;
1697 }
1698
1699 return callbacks;
1700 }
1701 }
1702
Winson Chungc763c4e2013-07-19 13:49:06 -07001703 private void verifyApplications() {
1704 final Context context = mApp.getContext();
1705
1706 // Cross reference all the applications in our apps list with items in the workspace
1707 ArrayList<ItemInfo> tmpInfos;
Michael Jurka695ff6b2013-08-05 12:06:48 +02001708 ArrayList<ItemInfo> added = new ArrayList<ItemInfo>();
Winson Chungc763c4e2013-07-19 13:49:06 -07001709 synchronized (sBgLock) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02001710 for (AppInfo app : mBgAllAppsList.data) {
Kenny Guyed131872014-04-30 03:02:21 +01001711 tmpInfos = getItemInfoForComponentName(app.componentName, app.user);
Winson Chungc763c4e2013-07-19 13:49:06 -07001712 if (tmpInfos.isEmpty()) {
1713 // We are missing an application icon, so add this to the workspace
1714 added.add(app);
1715 // This is a rare event, so lets log it
1716 Log.e(TAG, "Missing Application on load: " + app);
1717 }
1718 }
1719 }
1720 if (!added.isEmpty()) {
Adam Cohen76a47a12014-02-05 11:47:43 -08001721 addAndBindAddedWorkspaceApps(context, added);
Winson Chungc763c4e2013-07-19 13:49:06 -07001722 }
1723 }
1724
Joe Onorato36115782010-06-17 13:28:48 -04001725 // check & update map of what's occupied; used to discard overlapping/invalid items
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07001726 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) {
Winson Chung892c74d2013-08-22 16:15:50 -07001727 LauncherAppState app = LauncherAppState.getInstance();
1728 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Dan Sandler295ae182013-12-10 16:05:47 -05001729 final int countX = (int) grid.numColumns;
1730 final int countY = (int) grid.numRows;
Winson Chung892c74d2013-08-22 16:15:50 -07001731
Adam Cohendcd297f2013-06-18 13:13:40 -07001732 long containerIndex = item.screenId;
Winson Chungf30ad5f2011-08-08 10:55:42 -07001733 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
Winson Chunga0b7e862013-09-05 16:03:15 -07001734 // Return early if we detect that an item is under the hotseat button
1735 if (mCallbacks == null ||
1736 mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
Dan Sandler295ae182013-12-10 16:05:47 -05001737 Log.e(TAG, "Error loading shortcut into hotseat " + item
1738 + " into position (" + item.screenId + ":" + item.cellX + ","
1739 + item.cellY + ") occupied by all apps");
Winson Chunga0b7e862013-09-05 16:03:15 -07001740 return false;
1741 }
1742
Dan Sandler295ae182013-12-10 16:05:47 -05001743 final ItemInfo[][] hotseatItems =
1744 occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
1745
Adam Cohenae4409d2013-11-26 10:34:59 -08001746 if (item.screenId >= grid.numHotseatIcons) {
1747 Log.e(TAG, "Error loading shortcut " + item
1748 + " into hotseat position " + item.screenId
1749 + ", position out of bounds: (0 to " + (grid.numHotseatIcons - 1)
1750 + ")");
1751 return false;
1752 }
1753
Dan Sandler295ae182013-12-10 16:05:47 -05001754 if (hotseatItems != null) {
1755 if (hotseatItems[(int) item.screenId][0] != null) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001756 Log.e(TAG, "Error loading shortcut into hotseat " + item
1757 + " into position (" + item.screenId + ":" + item.cellX + ","
1758 + item.cellY + ") occupied by "
1759 + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
1760 [(int) item.screenId][0]);
1761 return false;
Dan Sandler295ae182013-12-10 16:05:47 -05001762 } else {
1763 hotseatItems[(int) item.screenId][0] = item;
1764 return true;
Adam Cohendcd297f2013-06-18 13:13:40 -07001765 }
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001766 } else {
Adam Cohenae4409d2013-11-26 10:34:59 -08001767 final ItemInfo[][] items = new ItemInfo[(int) grid.numHotseatIcons][1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001768 items[(int) item.screenId][0] = item;
1769 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001770 return true;
1771 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001772 } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1773 // Skip further checking if it is not the hotseat or workspace container
Daniel Sandler8802e962010-05-26 16:28:16 -04001774 return true;
1775 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001776
Adam Cohendcd297f2013-06-18 13:13:40 -07001777 if (!occupied.containsKey(item.screenId)) {
Winson Chung892c74d2013-08-22 16:15:50 -07001778 ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
Adam Cohendcd297f2013-06-18 13:13:40 -07001779 occupied.put(item.screenId, items);
1780 }
1781
Dan Sandler295ae182013-12-10 16:05:47 -05001782 final ItemInfo[][] screens = occupied.get(item.screenId);
Adam Cohenae4409d2013-11-26 10:34:59 -08001783 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
1784 item.cellX < 0 || item.cellY < 0 ||
1785 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
1786 Log.e(TAG, "Error loading shortcut " + item
1787 + " into cell (" + containerIndex + "-" + item.screenId + ":"
1788 + item.cellX + "," + item.cellY
1789 + ") out of screen bounds ( " + countX + "x" + countY + ")");
1790 return false;
1791 }
1792
Winson Chung6ba2a1b2011-09-02 16:22:11 -07001793 // Check if any workspace icons overlap with each other
Joe Onorato36115782010-06-17 13:28:48 -04001794 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1795 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 if (screens[x][y] != null) {
Joe Onorato36115782010-06-17 13:28:48 -04001797 Log.e(TAG, "Error loading shortcut " + item
Adam Cohendcd297f2013-06-18 13:13:40 -07001798 + " into cell (" + containerIndex + "-" + item.screenId + ":"
Joe Onorato36115782010-06-17 13:28:48 -04001799 + x + "," + y
Winson Chungaafa03c2010-06-11 17:34:16 -07001800 + ") occupied by "
Adam Cohendcd297f2013-06-18 13:13:40 -07001801 + screens[x][y]);
Joe Onorato36115782010-06-17 13:28:48 -04001802 return false;
1803 }
1804 }
1805 }
1806 for (int x = item.cellX; x < (item.cellX+item.spanX); x++) {
1807 for (int y = item.cellY; y < (item.cellY+item.spanY); y++) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001808 screens[x][y] = item;
Joe Onorato36115782010-06-17 13:28:48 -04001809 }
1810 }
Winson Chungf30ad5f2011-08-08 10:55:42 -07001811
Joe Onorato36115782010-06-17 13:28:48 -04001812 return true;
1813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814
Winson Chungba9c37f2013-08-30 14:11:37 -07001815 /** Clears all the sBg data structures */
1816 private void clearSBgDataStructures() {
1817 synchronized (sBgLock) {
1818 sBgWorkspaceItems.clear();
1819 sBgAppWidgets.clear();
1820 sBgFolders.clear();
1821 sBgItemsIdMap.clear();
1822 sBgDbIconCache.clear();
1823 sBgWorkspaceScreens.clear();
1824 }
1825 }
1826
Sunny Goyal66cfdc22015-02-02 13:01:51 -08001827 private void loadWorkspace() {
Winson Chung9f9f00b2013-11-15 13:27:00 -08001828 // Log to disk
1829 Launcher.addDumpLog(TAG, "11683562 - loadWorkspace()", true);
1830
Joe Onorato36115782010-06-17 13:28:48 -04001831 final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832
Joe Onorato36115782010-06-17 13:28:48 -04001833 final Context context = mContext;
1834 final ContentResolver contentResolver = context.getContentResolver();
1835 final PackageManager manager = context.getPackageManager();
Joe Onorato36115782010-06-17 13:28:48 -04001836 final boolean isSafeMode = manager.isSafeMode();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001837 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
1838 final boolean isSdCardReady = context.registerReceiver(null,
Sunny Goyal05e318d2014-07-29 11:49:35 -07001839 new IntentFilter(StartupReceiver.SYSTEM_READY)) != null;
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001840
Winson Chung892c74d2013-08-22 16:15:50 -07001841 LauncherAppState app = LauncherAppState.getInstance();
1842 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1843 int countX = (int) grid.numColumns;
1844 int countY = (int) grid.numRows;
1845
Dan Sandlerd5024042014-01-09 15:01:33 -05001846 if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) {
1847 Launcher.addDumpLog(TAG, "loadWorkspace: resetting launcher database", true);
1848 LauncherAppState.getLauncherProvider().deleteDatabase();
1849 }
1850
1851 if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) {
1852 // append the user's Launcher2 shortcuts
1853 Launcher.addDumpLog(TAG, "loadWorkspace: migrating from launcher2", true);
1854 LauncherAppState.getLauncherProvider().migrateLauncher2Shortcuts();
1855 } else {
1856 // Make sure the default workspace is loaded
1857 Launcher.addDumpLog(TAG, "loadWorkspace: loading default favorites", false);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001858 LauncherAppState.getLauncherProvider().loadDefaultFavoritesIfNecessary();
Dan Sandlerd5024042014-01-09 15:01:33 -05001859 }
Adam Cohene25af792013-06-06 23:08:25 -07001860
Winson Chung2abf94d2012-07-18 18:16:38 -07001861 synchronized (sBgLock) {
Winson Chungba9c37f2013-08-30 14:11:37 -07001862 clearSBgDataStructures();
Sunny Goyal94485362014-09-18 16:13:58 -07001863 final HashSet<String> installingPkgs = PackageInstallerCompat
1864 .getInstance(mContext).updateAndGetActiveSessionCache();
Romain Guy5c16f3e2010-01-12 17:24:58 -08001865
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001866 final ArrayList<Long> itemsToRemove = new ArrayList<Long>();
Chris Wrenf4d08112014-01-16 18:13:56 -05001867 final ArrayList<Long> restoredRows = new ArrayList<Long>();
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07001868 final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION;
Chris Wrene523e702013-10-09 10:36:55 -04001869 if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri);
Adam Cohene25af792013-06-06 23:08:25 -07001870 final Cursor c = contentResolver.query(contentUri, null, null, null, null);
Daniel Sandler8802e962010-05-26 16:28:16 -04001871
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001872 // +1 for the hotseat (it can be larger than the workspace)
1873 // Load workspace in reverse order to ensure that latest items are loaded first (and
1874 // before any earlier duplicates)
Adam Cohendcd297f2013-06-18 13:13:40 -07001875 final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001877 try {
1878 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1879 final int intentIndex = c.getColumnIndexOrThrow
1880 (LauncherSettings.Favorites.INTENT);
1881 final int titleIndex = c.getColumnIndexOrThrow
1882 (LauncherSettings.Favorites.TITLE);
1883 final int iconTypeIndex = c.getColumnIndexOrThrow(
1884 LauncherSettings.Favorites.ICON_TYPE);
1885 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1886 final int iconPackageIndex = c.getColumnIndexOrThrow(
1887 LauncherSettings.Favorites.ICON_PACKAGE);
1888 final int iconResourceIndex = c.getColumnIndexOrThrow(
1889 LauncherSettings.Favorites.ICON_RESOURCE);
1890 final int containerIndex = c.getColumnIndexOrThrow(
1891 LauncherSettings.Favorites.CONTAINER);
1892 final int itemTypeIndex = c.getColumnIndexOrThrow(
1893 LauncherSettings.Favorites.ITEM_TYPE);
1894 final int appWidgetIdIndex = c.getColumnIndexOrThrow(
1895 LauncherSettings.Favorites.APPWIDGET_ID);
Chris Wrenc3919c02013-09-18 09:48:33 -04001896 final int appWidgetProviderIndex = c.getColumnIndexOrThrow(
1897 LauncherSettings.Favorites.APPWIDGET_PROVIDER);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001898 final int screenIndex = c.getColumnIndexOrThrow(
1899 LauncherSettings.Favorites.SCREEN);
1900 final int cellXIndex = c.getColumnIndexOrThrow
1901 (LauncherSettings.Favorites.CELLX);
1902 final int cellYIndex = c.getColumnIndexOrThrow
1903 (LauncherSettings.Favorites.CELLY);
1904 final int spanXIndex = c.getColumnIndexOrThrow
1905 (LauncherSettings.Favorites.SPANX);
1906 final int spanYIndex = c.getColumnIndexOrThrow(
1907 LauncherSettings.Favorites.SPANY);
Sunny Goyal08f72612015-01-05 13:41:43 -08001908 final int rankIndex = c.getColumnIndexOrThrow(
1909 LauncherSettings.Favorites.RANK);
Chris Wrenf4d08112014-01-16 18:13:56 -05001910 final int restoredIndex = c.getColumnIndexOrThrow(
1911 LauncherSettings.Favorites.RESTORED);
Kenny Guyed131872014-04-30 03:02:21 +01001912 final int profileIdIndex = c.getColumnIndexOrThrow(
1913 LauncherSettings.Favorites.PROFILE_ID);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001914 //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1915 //final int displayModeIndex = c.getColumnIndexOrThrow(
1916 // LauncherSettings.Favorites.DISPLAY_MODE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001918 ShortcutInfo info;
1919 String intentDescription;
1920 LauncherAppWidgetInfo appWidgetInfo;
1921 int container;
1922 long id;
1923 Intent intent;
Kenny Guyed131872014-04-30 03:02:21 +01001924 UserHandleCompat user;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001926 while (!mStopped && c.moveToNext()) {
1927 try {
1928 int itemType = c.getInt(itemTypeIndex);
Chris Wrenf4d08112014-01-16 18:13:56 -05001929 boolean restored = 0 != c.getInt(restoredIndex);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001930 boolean allowMissingTarget = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -04001931
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001932 switch (itemType) {
1933 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1934 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungee055712013-07-30 14:46:24 -07001935 id = c.getLong(idIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001936 intentDescription = c.getString(intentIndex);
Kenny Guy1317e2d2014-05-08 18:52:50 +01001937 long serialNumber = c.getInt(profileIdIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001938 user = mUserManager.getUserForSerialNumber(serialNumber);
Sunny Goyal34942622014-08-29 17:20:55 -07001939 int promiseType = c.getInt(restoredIndex);
Sunny Goyal1a745e82014-10-02 15:58:31 -07001940 int disabledState = 0;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001941 boolean itemReplaced = false;
Kenny Guyed131872014-04-30 03:02:21 +01001942 if (user == null) {
1943 // User has been deleted remove the item.
1944 itemsToRemove.add(id);
1945 continue;
1946 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07001947 try {
1948 intent = Intent.parseUri(intentDescription, 0);
Winson Chungee055712013-07-30 14:46:24 -07001949 ComponentName cn = intent.getComponent();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001950 if (cn != null && cn.getPackageName() != null) {
1951 boolean validPkg = launcherApps.isPackageEnabledForProfile(
1952 cn.getPackageName(), user);
1953 boolean validComponent = validPkg &&
1954 launcherApps.isActivityEnabledForProfile(cn, user);
1955
1956 if (validComponent) {
1957 if (restored) {
1958 // no special handling necessary for this item
1959 restoredRows.add(id);
1960 restored = false;
1961 }
1962 } else if (validPkg) {
Sunny Goyal34942622014-08-29 17:20:55 -07001963 intent = null;
1964 if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
1965 // We allow auto install apps to have their intent
1966 // updated after an install.
1967 intent = manager.getLaunchIntentForPackage(
1968 cn.getPackageName());
1969 if (intent != null) {
1970 ContentValues values = new ContentValues();
1971 values.put(LauncherSettings.Favorites.INTENT,
1972 intent.toUri(0));
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001973 updateItem(id, values);
Sunny Goyal34942622014-08-29 17:20:55 -07001974 }
1975 }
1976
1977 if (intent == null) {
1978 // The app is installed but the component is no
1979 // longer available.
1980 Launcher.addDumpLog(TAG,
1981 "Invalid component removed: " + cn, true);
1982 itemsToRemove.add(id);
1983 continue;
1984 } else {
1985 // no special handling necessary for this item
1986 restoredRows.add(id);
1987 restored = false;
1988 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07001989 } else if (restored) {
1990 // Package is not yet available but might be
1991 // installed later.
Chris Wrenf4d08112014-01-16 18:13:56 -05001992 Launcher.addDumpLog(TAG,
1993 "package not yet restored: " + cn, true);
Sunny Goyal94485362014-09-18 16:13:58 -07001994
1995 if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) {
1996 // Restore has started once.
1997 } else if (installingPkgs.contains(cn.getPackageName())) {
1998 // App restore has started. Update the flag
1999 promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED;
2000 ContentValues values = new ContentValues();
2001 values.put(LauncherSettings.Favorites.RESTORED,
2002 promiseType);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002003 updateItem(id, values);
2004 } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) {
2005 // This is a common app. Try to replace this.
2006 int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType);
2007 CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context);
2008 if (parser.findDefaultApp()) {
2009 // Default app found. Replace it.
2010 intent = parser.parsedIntent;
2011 cn = intent.getComponent();
2012 ContentValues values = parser.parsedValues;
2013 values.put(LauncherSettings.Favorites.RESTORED, 0);
2014 updateItem(id, values);
2015 restored = false;
2016 itemReplaced = true;
Sunny Goyal94485362014-09-18 16:13:58 -07002017
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002018 } else if (REMOVE_UNRESTORED_ICONS) {
2019 Launcher.addDumpLog(TAG,
2020 "Unrestored package removed: " + cn, true);
2021 itemsToRemove.add(id);
2022 continue;
2023 }
Sunny Goyal94485362014-09-18 16:13:58 -07002024 } else if (REMOVE_UNRESTORED_ICONS) {
2025 Launcher.addDumpLog(TAG,
2026 "Unrestored package removed: " + cn, true);
2027 itemsToRemove.add(id);
2028 continue;
2029 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002030 } else if (launcherApps.isAppEnabled(
2031 manager, cn.getPackageName(),
2032 PackageManager.GET_UNINSTALLED_PACKAGES)) {
2033 // Package is present but not available.
2034 allowMissingTarget = true;
2035 disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
2036 } else if (!isSdCardReady) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002037 // SdCard is not ready yet. Package might get available,
2038 // once it is ready.
2039 Launcher.addDumpLog(TAG, "Invalid package: " + cn
2040 + " (check again later)", true);
2041 HashSet<String> pkgs = sPendingPackages.get(user);
2042 if (pkgs == null) {
Sameer Padala513edae2014-07-29 16:17:08 -07002043 pkgs = new HashSet<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002044 sPendingPackages.put(user, pkgs);
2045 }
2046 pkgs.add(cn.getPackageName());
2047 allowMissingTarget = true;
2048 // Add the icon on the workspace anyway.
Sunny Goyal1a745e82014-10-02 15:58:31 -07002049
2050 } else {
2051 // Do not wait for external media load anymore.
2052 // Log the invalid package, and remove it
2053 Launcher.addDumpLog(TAG,
2054 "Invalid package removed: " + cn, true);
2055 itemsToRemove.add(id);
2056 continue;
Winson Chungee055712013-07-30 14:46:24 -07002057 }
Sunny Goyal938a53d2014-09-05 03:17:45 -07002058 } else if (cn == null) {
2059 // For shortcuts with no component, keep them as they are
2060 restoredRows.add(id);
2061 restored = false;
Winson Chungee055712013-07-30 14:46:24 -07002062 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002063 } catch (URISyntaxException e) {
Chris Wrenf4d08112014-01-16 18:13:56 -05002064 Launcher.addDumpLog(TAG,
2065 "Invalid uri: " + intentDescription, true);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002066 continue;
2067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002068
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002069 if (itemReplaced) {
2070 if (user.equals(UserHandleCompat.myUserHandle())) {
2071 info = getShortcutInfo(manager, intent, user, context, null,
2072 iconIndex, titleIndex, mLabelCache, false);
2073 } else {
2074 // Don't replace items for other profiles.
2075 itemsToRemove.add(id);
2076 continue;
2077 }
2078 } else if (restored) {
Kenny Guyed131872014-04-30 03:02:21 +01002079 if (user.equals(UserHandleCompat.myUserHandle())) {
2080 Launcher.addDumpLog(TAG,
2081 "constructing info for partially restored package",
2082 true);
Sunny Goyal34942622014-08-29 17:20:55 -07002083 info = getRestoredItemInfo(c, titleIndex, intent, promiseType);
Kenny Guyed131872014-04-30 03:02:21 +01002084 intent = getRestoredItemIntent(c, context, intent);
2085 } else {
2086 // Don't restore items for other profiles.
2087 itemsToRemove.add(id);
2088 continue;
2089 }
Chris Wrenf4d08112014-01-16 18:13:56 -05002090 } else if (itemType ==
2091 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002092 info = getShortcutInfo(manager, intent, user, context, c,
2093 iconIndex, titleIndex, mLabelCache, allowMissingTarget);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002094 } else {
2095 info = getShortcutInfo(c, context, iconTypeIndex,
2096 iconPackageIndex, iconResourceIndex, iconIndex,
2097 titleIndex);
Michael Jurka96879562012-03-22 05:54:33 -07002098
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002099 // App shortcuts that used to be automatically added to Launcher
2100 // didn't always have the correct intent flags set, so do that
2101 // here
2102 if (intent.getAction() != null &&
Michael Jurka9ad00562012-05-14 12:24:22 -07002103 intent.getCategories() != null &&
2104 intent.getAction().equals(Intent.ACTION_MAIN) &&
Michael Jurka96879562012-03-22 05:54:33 -07002105 intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002106 intent.addFlags(
2107 Intent.FLAG_ACTIVITY_NEW_TASK |
2108 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
2109 }
Michael Jurka96879562012-03-22 05:54:33 -07002110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002111
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002112 if (info != null) {
Winson Chungee055712013-07-30 14:46:24 -07002113 info.id = id;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002114 info.intent = intent;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002115 container = c.getInt(containerIndex);
2116 info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 info.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002118 info.cellX = c.getInt(cellXIndex);
2119 info.cellY = c.getInt(cellYIndex);
Sunny Goyal08f72612015-01-05 13:41:43 -08002120 info.rank = c.getInt(rankIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002121 info.spanX = 1;
2122 info.spanY = 1;
Kenny Guyed131872014-04-30 03:02:21 +01002123 info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber);
Sunny Goyal1a745e82014-10-02 15:58:31 -07002124 info.isDisabled = disabledState;
2125 if (isSafeMode && !Utilities.isSystemApp(context, intent)) {
2126 info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE;
2127 }
Adam Cohenae4409d2013-11-26 10:34:59 -08002128
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002129 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002130 if (!checkItemPlacement(occupied, info)) {
2131 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002132 break;
2133 }
2134
2135 switch (container) {
2136 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2137 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2138 sBgWorkspaceItems.add(info);
2139 break;
2140 default:
2141 // Item is in a user folder
2142 FolderInfo folderInfo =
2143 findOrMakeFolder(sBgFolders, container);
2144 folderInfo.add(info);
2145 break;
2146 }
2147 sBgItemsIdMap.put(info.id, info);
2148
2149 // now that we've loaded everthing re-save it with the
2150 // icon in case it disappears somehow.
2151 queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex);
Winson Chung1323b482013-08-05 12:41:55 -07002152 } else {
2153 throw new RuntimeException("Unexpected null ShortcutInfo");
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002154 }
2155 break;
2156
2157 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
2158 id = c.getLong(idIndex);
2159 FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id);
2160
2161 folderInfo.title = c.getString(titleIndex);
2162 folderInfo.id = id;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002164 folderInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 folderInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002166 folderInfo.cellX = c.getInt(cellXIndex);
2167 folderInfo.cellY = c.getInt(cellYIndex);
Winson Chung5f8afe62013-08-12 16:19:28 -07002168 folderInfo.spanX = 1;
2169 folderInfo.spanY = 1;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170
Daniel Sandler8802e962010-05-26 16:28:16 -04002171 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002172 if (!checkItemPlacement(occupied, folderInfo)) {
2173 itemsToRemove.add(id);
Daniel Sandler8802e962010-05-26 16:28:16 -04002174 break;
2175 }
Winson Chung5f8afe62013-08-12 16:19:28 -07002176
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 switch (container) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002178 case LauncherSettings.Favorites.CONTAINER_DESKTOP:
2179 case LauncherSettings.Favorites.CONTAINER_HOTSEAT:
2180 sBgWorkspaceItems.add(folderInfo);
2181 break;
Joe Onorato36115782010-06-17 13:28:48 -04002182 }
Joe Onorato17a89222011-02-08 17:26:11 -08002183
Chris Wrenf4d08112014-01-16 18:13:56 -05002184 if (restored) {
2185 // no special handling required for restored folders
2186 restoredRows.add(id);
2187 }
2188
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002189 sBgItemsIdMap.put(folderInfo.id, folderInfo);
2190 sBgFolders.put(folderInfo.id, folderInfo);
2191 break;
2192
2193 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohen59400422014-03-05 18:07:04 -08002194 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002195 // Read all Launcher-specific widget details
Adam Cohen59400422014-03-05 18:07:04 -08002196 boolean customWidget = itemType ==
2197 LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2198
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002199 int appWidgetId = c.getInt(appWidgetIdIndex);
Chris Wrenc3919c02013-09-18 09:48:33 -04002200 String savedProvider = c.getString(appWidgetProviderIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002201 id = c.getLong(idIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002202 final ComponentName component =
2203 ComponentName.unflattenFromString(savedProvider);
Joe Onorato36115782010-06-17 13:28:48 -04002204
Sunny Goyal651077b2014-06-30 14:15:31 -07002205 final int restoreStatus = c.getInt(restoredIndex);
Sunny Goyalff572272014-07-23 13:58:07 -07002206 final boolean isIdValid = (restoreStatus &
2207 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0;
Joe Onorato36115782010-06-17 13:28:48 -04002208
Sunny Goyalff572272014-07-23 13:58:07 -07002209 final boolean wasProviderReady = (restoreStatus &
2210 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0;
Sunny Goyal651077b2014-06-30 14:15:31 -07002211
Adam Cohen59400422014-03-05 18:07:04 -08002212 final LauncherAppWidgetProviderInfo provider =
2213 LauncherModel.getProviderInfo(context,
2214 ComponentName.unflattenFromString(savedProvider));
Sunny Goyalff572272014-07-23 13:58:07 -07002215
2216 final boolean isProviderReady = isValidProvider(provider);
Adam Cohen59400422014-03-05 18:07:04 -08002217 if (!isSafeMode && !customWidget &&
2218 wasProviderReady && !isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002219 String log = "Deleting widget that isn't installed anymore: "
Sunny Goyalff572272014-07-23 13:58:07 -07002220 + "id=" + id + " appWidgetId=" + appWidgetId;
Adam Cohen59400422014-03-05 18:07:04 -08002221
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002222 Log.e(TAG, log);
Adam Cohen4caf2982013-08-20 18:54:31 -07002223 Launcher.addDumpLog(TAG, log, false);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002224 itemsToRemove.add(id);
2225 } else {
Sunny Goyalff572272014-07-23 13:58:07 -07002226 if (isProviderReady) {
Sunny Goyal651077b2014-06-30 14:15:31 -07002227 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
2228 provider.provider);
Adam Cohen59400422014-03-05 18:07:04 -08002229
2230 if (!customWidget) {
2231 int[] minSpan =
2232 Launcher.getMinSpanForWidget(context, provider);
2233 appWidgetInfo.minSpanX = minSpan[0];
2234 appWidgetInfo.minSpanY = minSpan[1];
2235 }
Sunny Goyalff572272014-07-23 13:58:07 -07002236
2237 int status = restoreStatus;
2238 if (!wasProviderReady) {
2239 // If provider was not previously ready, update the
2240 // status and UI flag.
2241
2242 // Id would be valid only if the widget restore broadcast was received.
2243 if (isIdValid) {
2244 status = LauncherAppWidgetInfo.RESTORE_COMPLETED;
2245 } else {
2246 status &= ~LauncherAppWidgetInfo
2247 .FLAG_PROVIDER_NOT_READY;
2248 }
2249 }
2250 appWidgetInfo.restoreStatus = status;
Sunny Goyal651077b2014-06-30 14:15:31 -07002251 } else {
2252 Log.v(TAG, "Widget restore pending id=" + id
2253 + " appWidgetId=" + appWidgetId
2254 + " status =" + restoreStatus);
2255 appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId,
Sunny Goyalff572272014-07-23 13:58:07 -07002256 component);
Sunny Goyal651077b2014-06-30 14:15:31 -07002257 appWidgetInfo.restoreStatus = restoreStatus;
Sunny Goyal94485362014-09-18 16:13:58 -07002258
2259 if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) {
2260 // Restore has started once.
2261 } else if (installingPkgs.contains(component.getPackageName())) {
2262 // App restore has started. Update the flag
2263 appWidgetInfo.restoreStatus |=
2264 LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002265 } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) {
Sunny Goyal94485362014-09-18 16:13:58 -07002266 Launcher.addDumpLog(TAG,
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002267 "Unrestored widget removed: " + component, true);
Sunny Goyal94485362014-09-18 16:13:58 -07002268 itemsToRemove.add(id);
2269 continue;
2270 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002271 }
Sunny Goyalff572272014-07-23 13:58:07 -07002272
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002273 appWidgetInfo.id = id;
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 appWidgetInfo.screenId = c.getInt(screenIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002275 appWidgetInfo.cellX = c.getInt(cellXIndex);
2276 appWidgetInfo.cellY = c.getInt(cellYIndex);
2277 appWidgetInfo.spanX = c.getInt(spanXIndex);
2278 appWidgetInfo.spanY = c.getInt(spanYIndex);
Joe Onorato36115782010-06-17 13:28:48 -04002279
Adam Cohen59400422014-03-05 18:07:04 -08002280 if (!customWidget) {
2281 int[] minSpan = Launcher.getMinSpanForWidget(context, provider);
2282 appWidgetInfo.minSpanX = minSpan[0];
2283 appWidgetInfo.minSpanY = minSpan[1];
2284 }
2285
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002286 container = c.getInt(containerIndex);
2287 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2288 container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2289 Log.e(TAG, "Widget found where container != " +
2290 "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!");
2291 continue;
2292 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002293
Adam Cohene25af792013-06-06 23:08:25 -07002294 appWidgetInfo.container = c.getInt(containerIndex);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002295 // check & update map of what's occupied
Sunny Goyalfc0fe6b2014-10-16 12:18:37 -07002296 if (!checkItemPlacement(occupied, appWidgetInfo)) {
2297 itemsToRemove.add(id);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002298 break;
2299 }
Sunny Goyal651077b2014-06-30 14:15:31 -07002300
Adam Cohen59400422014-03-05 18:07:04 -08002301 if (!customWidget) {
2302 String providerName =
2303 appWidgetInfo.providerName.flattenToString();
2304 if (!providerName.equals(savedProvider) ||
2305 (appWidgetInfo.restoreStatus != restoreStatus)) {
2306 ContentValues values = new ContentValues();
2307 values.put(
2308 LauncherSettings.Favorites.APPWIDGET_PROVIDER,
2309 providerName);
2310 values.put(LauncherSettings.Favorites.RESTORED,
2311 appWidgetInfo.restoreStatus);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002312 updateItem(id, values);
Adam Cohen59400422014-03-05 18:07:04 -08002313 }
Chris Wrenc3919c02013-09-18 09:48:33 -04002314 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002315 sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo);
2316 sBgAppWidgets.add(appWidgetInfo);
2317 }
Joe Onorato36115782010-06-17 13:28:48 -04002318 break;
2319 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002320 } catch (Exception e) {
Dan Sandler295ae182013-12-10 16:05:47 -05002321 Launcher.addDumpLog(TAG, "Desktop items loading interrupted", e, true);
Romain Guy5c16f3e2010-01-12 17:24:58 -08002322 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002323 }
2324 } finally {
Daniel Sandler47b50312013-07-25 13:16:14 -04002325 if (c != null) {
2326 c.close();
2327 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002328 }
2329
Winson Chungba9c37f2013-08-30 14:11:37 -07002330 // Break early if we've stopped loading
2331 if (mStopped) {
Winson Chungba9c37f2013-08-30 14:11:37 -07002332 clearSBgDataStructures();
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002333 return;
Winson Chungba9c37f2013-08-30 14:11:37 -07002334 }
2335
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002336 if (itemsToRemove.size() > 0) {
2337 ContentProviderClient client = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002338 contentUri);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002339 // Remove dead items
2340 for (long id : itemsToRemove) {
2341 if (DEBUG_LOADERS) {
2342 Log.d(TAG, "Removed id = " + id);
2343 }
2344 // Don't notify content observers
2345 try {
2346 client.delete(LauncherSettings.Favorites.getContentUri(id, false),
2347 null, null);
2348 } catch (RemoteException e) {
2349 Log.w(TAG, "Could not remove id = " + id);
2350 }
Romain Guy5c16f3e2010-01-12 17:24:58 -08002351 }
2352 }
2353
Chris Wrenf4d08112014-01-16 18:13:56 -05002354 if (restoredRows.size() > 0) {
2355 ContentProviderClient updater = contentResolver.acquireContentProviderClient(
Sunny Goyalc5fb59f2014-09-25 16:20:38 -07002356 contentUri);
Chris Wrenf4d08112014-01-16 18:13:56 -05002357 // Update restored items that no longer require special handling
2358 try {
2359 StringBuilder selectionBuilder = new StringBuilder();
2360 selectionBuilder.append(LauncherSettings.Favorites._ID);
2361 selectionBuilder.append(" IN (");
2362 selectionBuilder.append(TextUtils.join(", ", restoredRows));
2363 selectionBuilder.append(")");
2364 ContentValues values = new ContentValues();
2365 values.put(LauncherSettings.Favorites.RESTORED, 0);
Sunny Goyal34942622014-08-29 17:20:55 -07002366 updater.update(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
Chris Wrenf4d08112014-01-16 18:13:56 -05002367 values, selectionBuilder.toString(), null);
2368 } catch (RemoteException e) {
2369 Log.w(TAG, "Could not update restored rows");
2370 }
2371 }
2372
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002373 if (!isSdCardReady && !sPendingPackages.isEmpty()) {
2374 context.registerReceiver(new AppsAvailabilityCheck(),
Sunny Goyal05e318d2014-07-29 11:49:35 -07002375 new IntentFilter(StartupReceiver.SYSTEM_READY),
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002376 null, sWorker);
2377 }
2378
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002379 sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext));
2380 // Log to disk
2381 Launcher.addDumpLog(TAG, "11683562 - sBgWorkspaceScreens: " +
2382 TextUtils.join(", ", sBgWorkspaceScreens), true);
Winson Chung9e6a0a22013-08-27 11:58:12 -07002383
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002384 // Remove any empty screens
2385 ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens);
2386 for (ItemInfo item: sBgItemsIdMap.values()) {
2387 long screenId = item.screenId;
2388 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
2389 unusedScreens.contains(screenId)) {
2390 unusedScreens.remove(screenId);
2391 }
2392 }
2393
2394 // If there are any empty screens remove them, and update.
2395 if (unusedScreens.size() != 0) {
2396 // Log to disk
2397 Launcher.addDumpLog(TAG, "11683562 - unusedScreens (to be removed): " +
2398 TextUtils.join(", ", unusedScreens), true);
2399
2400 sBgWorkspaceScreens.removeAll(unusedScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002401 updateWorkspaceScreenOrder(context, sBgWorkspaceScreens);
Adam Cohendcd297f2013-06-18 13:13:40 -07002402 }
2403
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002404 if (DEBUG_LOADERS) {
2405 Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms");
2406 Log.d(TAG, "workspace layout: ");
Adam Cohendcd297f2013-06-18 13:13:40 -07002407 int nScreens = occupied.size();
Winson Chung892c74d2013-08-22 16:15:50 -07002408 for (int y = 0; y < countY; y++) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002409 String line = "";
Adam Cohendcd297f2013-06-18 13:13:40 -07002410
Daniel Sandler566da102013-06-25 23:43:45 -04002411 Iterator<Long> iter = occupied.keySet().iterator();
Winson Chungc9168342013-06-26 14:54:55 -07002412 while (iter.hasNext()) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 long screenId = iter.next();
Winson Chungc9168342013-06-26 14:54:55 -07002414 if (screenId > 0) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002415 line += " | ";
2416 }
Winson Chung892c74d2013-08-22 16:15:50 -07002417 for (int x = 0; x < countX; x++) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05002418 ItemInfo[][] screen = occupied.get(screenId);
2419 if (x < screen.length && y < screen[x].length) {
2420 line += (screen[x][y] != null) ? "#" : ".";
2421 } else {
2422 line += "!";
2423 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002424 }
Joe Onorato36115782010-06-17 13:28:48 -04002425 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002426 Log.d(TAG, "[ " + line + " ]");
Joe Onorato36115782010-06-17 13:28:48 -04002427 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 }
Joe Onorato36115782010-06-17 13:28:48 -04002429 }
Adam Cohene25af792013-06-06 23:08:25 -07002430 }
2431
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08002432 /**
2433 * Partially updates the item without any notification. Must be called on the worker thread.
2434 */
2435 private void updateItem(long itemId, ContentValues update) {
2436 mContext.getContentResolver().update(
2437 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION,
2438 update,
2439 BaseColumns._ID + "= ?",
2440 new String[]{Long.toString(itemId)});
2441 }
2442
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002443 /** Filters the set of items who are directly or indirectly (via another container) on the
2444 * specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002445 private void filterCurrentWorkspaceItems(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002446 ArrayList<ItemInfo> allWorkspaceItems,
2447 ArrayList<ItemInfo> currentScreenItems,
2448 ArrayList<ItemInfo> otherScreenItems) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002449 // Purge any null ItemInfos
2450 Iterator<ItemInfo> iter = allWorkspaceItems.iterator();
2451 while (iter.hasNext()) {
2452 ItemInfo i = iter.next();
2453 if (i == null) {
2454 iter.remove();
2455 }
2456 }
2457
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002458 // Order the set of items by their containers first, this allows use to walk through the
2459 // list sequentially, build up a list of containers that are in the specified screen,
2460 // as well as all items in those containers.
2461 Set<Long> itemsOnScreen = new HashSet<Long>();
2462 Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() {
2463 @Override
2464 public int compare(ItemInfo lhs, ItemInfo rhs) {
2465 return (int) (lhs.container - rhs.container);
2466 }
2467 });
2468 for (ItemInfo info : allWorkspaceItems) {
2469 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002470 if (info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002471 currentScreenItems.add(info);
2472 itemsOnScreen.add(info.id);
2473 } else {
2474 otherScreenItems.add(info);
2475 }
2476 } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2477 currentScreenItems.add(info);
2478 itemsOnScreen.add(info.id);
2479 } else {
2480 if (itemsOnScreen.contains(info.container)) {
2481 currentScreenItems.add(info);
2482 itemsOnScreen.add(info.id);
2483 } else {
2484 otherScreenItems.add(info);
2485 }
2486 }
2487 }
2488 }
2489
2490 /** Filters the set of widgets which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002491 private void filterCurrentAppWidgets(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002492 ArrayList<LauncherAppWidgetInfo> appWidgets,
2493 ArrayList<LauncherAppWidgetInfo> currentScreenWidgets,
2494 ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002495
2496 for (LauncherAppWidgetInfo widget : appWidgets) {
Winson Chung2abf94d2012-07-18 18:16:38 -07002497 if (widget == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002498 if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002499 widget.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002500 currentScreenWidgets.add(widget);
2501 } else {
2502 otherScreenWidgets.add(widget);
2503 }
2504 }
2505 }
2506
2507 /** Filters the set of folders which are on the specified screen. */
Winson Chung9b9fb962013-11-15 15:39:34 -08002508 private void filterCurrentFolders(long currentScreenId,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002509 HashMap<Long, ItemInfo> itemsIdMap,
2510 HashMap<Long, FolderInfo> folders,
2511 HashMap<Long, FolderInfo> currentScreenFolders,
2512 HashMap<Long, FolderInfo> otherScreenFolders) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002513
2514 for (long id : folders.keySet()) {
2515 ItemInfo info = itemsIdMap.get(id);
2516 FolderInfo folder = folders.get(id);
Winson Chung2abf94d2012-07-18 18:16:38 -07002517 if (info == null || folder == null) continue;
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002518 if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
Winson Chung9b9fb962013-11-15 15:39:34 -08002519 info.screenId == currentScreenId) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002520 currentScreenFolders.put(id, folder);
2521 } else {
2522 otherScreenFolders.put(id, folder);
2523 }
2524 }
2525 }
2526
2527 /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to
2528 * right) */
2529 private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) {
Winson Chung892c74d2013-08-22 16:15:50 -07002530 final LauncherAppState app = LauncherAppState.getInstance();
2531 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002532 // XXX: review this
2533 Collections.sort(workspaceItems, new Comparator<ItemInfo>() {
Winson Chungdb8a8942012-04-03 14:08:41 -07002534 @Override
2535 public int compare(ItemInfo lhs, ItemInfo rhs) {
Winson Chung892c74d2013-08-22 16:15:50 -07002536 int cellCountX = (int) grid.numColumns;
2537 int cellCountY = (int) grid.numRows;
Winson Chungdb8a8942012-04-03 14:08:41 -07002538 int screenOffset = cellCountX * cellCountY;
2539 int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat
Adam Cohendcd297f2013-06-18 13:13:40 -07002540 long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002541 lhs.cellY * cellCountX + lhs.cellX);
Adam Cohendcd297f2013-06-18 13:13:40 -07002542 long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset +
Winson Chungdb8a8942012-04-03 14:08:41 -07002543 rhs.cellY * cellCountX + rhs.cellX);
2544 return (int) (lr - rr);
2545 }
2546 });
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002547 }
Winson Chungdb8a8942012-04-03 14:08:41 -07002548
Adam Cohendcd297f2013-06-18 13:13:40 -07002549 private void bindWorkspaceScreens(final Callbacks oldCallbacks,
2550 final ArrayList<Long> orderedScreens) {
Adam Cohendcd297f2013-06-18 13:13:40 -07002551 final Runnable r = new Runnable() {
2552 @Override
2553 public void run() {
2554 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2555 if (callbacks != null) {
2556 callbacks.bindScreens(orderedScreens);
2557 }
2558 }
2559 };
2560 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
2561 }
2562
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002563 private void bindWorkspaceItems(final Callbacks oldCallbacks,
2564 final ArrayList<ItemInfo> workspaceItems,
2565 final ArrayList<LauncherAppWidgetInfo> appWidgets,
2566 final HashMap<Long, FolderInfo> folders,
2567 ArrayList<Runnable> deferredBindRunnables) {
Winson Chung603bcb92011-09-02 11:45:39 -07002568
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002569 final boolean postOnMainThread = (deferredBindRunnables != null);
2570
2571 // Bind the workspace items
Winson Chungdb8a8942012-04-03 14:08:41 -07002572 int N = workspaceItems.size();
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002573 for (int i = 0; i < N; i += ITEMS_CHUNK) {
Joe Onorato36115782010-06-17 13:28:48 -04002574 final int start = i;
2575 final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002576 final Runnable r = new Runnable() {
2577 @Override
Joe Onorato9c1289c2009-08-17 11:03:03 -04002578 public void run() {
Joe Onoratoc131b742010-03-11 15:45:05 -08002579 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002580 if (callbacks != null) {
Winson Chung64359a52013-07-08 17:17:08 -07002581 callbacks.bindItems(workspaceItems, start, start+chunkSize,
2582 false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002583 }
2584 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002585 };
2586 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002587 synchronized (deferredBindRunnables) {
2588 deferredBindRunnables.add(r);
2589 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002590 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002591 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002592 }
Joe Onorato36115782010-06-17 13:28:48 -04002593 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002594
2595 // Bind the folders
2596 if (!folders.isEmpty()) {
2597 final Runnable r = new Runnable() {
2598 public void run() {
2599 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2600 if (callbacks != null) {
2601 callbacks.bindFolders(folders);
2602 }
2603 }
2604 };
2605 if (postOnMainThread) {
Jason Monka0a7a742014-04-22 09:23:19 -04002606 synchronized (deferredBindRunnables) {
2607 deferredBindRunnables.add(r);
2608 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002609 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002610 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002611 }
2612 }
2613
2614 // Bind the widgets, one at a time
2615 N = appWidgets.size();
2616 for (int i = 0; i < N; i++) {
2617 final LauncherAppWidgetInfo widget = appWidgets.get(i);
2618 final Runnable r = new Runnable() {
2619 public void run() {
2620 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2621 if (callbacks != null) {
2622 callbacks.bindAppWidget(widget);
2623 }
2624 }
2625 };
2626 if (postOnMainThread) {
2627 deferredBindRunnables.add(r);
2628 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002629 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002630 }
2631 }
2632 }
2633
2634 /**
2635 * Binds all loaded data to actual views on the main thread.
2636 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002637 private void bindWorkspace(int synchronizeBindPage) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002638 final long t = SystemClock.uptimeMillis();
2639 Runnable r;
2640
2641 // Don't use these two variables in any of the callback runnables.
2642 // Otherwise we hold a reference to them.
2643 final Callbacks oldCallbacks = mCallbacks.get();
2644 if (oldCallbacks == null) {
2645 // This launcher has exited and nobody bothered to tell us. Just bail.
2646 Log.w(TAG, "LoaderTask running with no launcher");
2647 return;
2648 }
2649
Winson Chung9b9fb962013-11-15 15:39:34 -08002650 // Save a copy of all the bg-thread collections
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002651 ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>();
2652 ArrayList<LauncherAppWidgetInfo> appWidgets =
2653 new ArrayList<LauncherAppWidgetInfo>();
2654 HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
2655 HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>();
Adam Cohendcd297f2013-06-18 13:13:40 -07002656 ArrayList<Long> orderedScreenIds = new ArrayList<Long>();
Winson Chung2abf94d2012-07-18 18:16:38 -07002657 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002658 workspaceItems.addAll(sBgWorkspaceItems);
2659 appWidgets.addAll(sBgAppWidgets);
2660 folders.putAll(sBgFolders);
2661 itemsIdMap.putAll(sBgItemsIdMap);
Adam Cohendcd297f2013-06-18 13:13:40 -07002662 orderedScreenIds.addAll(sBgWorkspaceScreens);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002663 }
2664
Derek Prothro7aff3992013-12-10 14:00:37 -05002665 final boolean isLoadingSynchronously =
2666 synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE;
Adam Cohend8dbb462013-11-27 11:55:48 -08002667 int currScreen = isLoadingSynchronously ? synchronizeBindPage :
Winson Chung9b9fb962013-11-15 15:39:34 -08002668 oldCallbacks.getCurrentWorkspaceScreen();
Adam Cohend8dbb462013-11-27 11:55:48 -08002669 if (currScreen >= orderedScreenIds.size()) {
2670 // There may be no workspace screens (just hotseat items and an empty page).
Derek Prothro7aff3992013-12-10 14:00:37 -05002671 currScreen = PagedView.INVALID_RESTORE_PAGE;
Winson Chung9b9fb962013-11-15 15:39:34 -08002672 }
Adam Cohend8dbb462013-11-27 11:55:48 -08002673 final int currentScreen = currScreen;
Derek Prothro7aff3992013-12-10 14:00:37 -05002674 final long currentScreenId = currentScreen < 0
2675 ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen);
Winson Chung9b9fb962013-11-15 15:39:34 -08002676
2677 // Load all the items that are on the current page first (and in the process, unbind
2678 // all the existing workspace items before we call startBinding() below.
2679 unbindWorkspaceItemsOnMainThread();
2680
2681 // Separate the items that are on the current screen, and all the other remaining items
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002682 ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>();
2683 ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>();
2684 ArrayList<LauncherAppWidgetInfo> currentAppWidgets =
2685 new ArrayList<LauncherAppWidgetInfo>();
2686 ArrayList<LauncherAppWidgetInfo> otherAppWidgets =
2687 new ArrayList<LauncherAppWidgetInfo>();
2688 HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>();
2689 HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>();
2690
Winson Chung9b9fb962013-11-15 15:39:34 -08002691 filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002692 otherWorkspaceItems);
Winson Chung9b9fb962013-11-15 15:39:34 -08002693 filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002694 otherAppWidgets);
Winson Chung9b9fb962013-11-15 15:39:34 -08002695 filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders,
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002696 otherFolders);
2697 sortWorkspaceItemsSpatially(currentWorkspaceItems);
2698 sortWorkspaceItemsSpatially(otherWorkspaceItems);
2699
2700 // Tell the workspace that we're about to start binding items
2701 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002702 public void run() {
2703 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2704 if (callbacks != null) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002705 callbacks.startBinding();
Joe Onorato36115782010-06-17 13:28:48 -04002706 }
2707 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002708 };
Winson Chung81b52252012-08-27 15:34:29 -07002709 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002710
Adam Cohendcd297f2013-06-18 13:13:40 -07002711 bindWorkspaceScreens(oldCallbacks, orderedScreenIds);
2712
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002713 // Load items on the current page
2714 bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets,
2715 currentFolders, null);
Adam Cohen1462de32012-07-24 22:34:36 -07002716 if (isLoadingSynchronously) {
2717 r = new Runnable() {
2718 public void run() {
2719 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Derek Prothro7aff3992013-12-10 14:00:37 -05002720 if (callbacks != null && currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Adam Cohen1462de32012-07-24 22:34:36 -07002721 callbacks.onPageBoundSynchronously(currentScreen);
2722 }
2723 }
2724 };
Winson Chung81b52252012-08-27 15:34:29 -07002725 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Adam Cohen1462de32012-07-24 22:34:36 -07002726 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002727
Winson Chung4a2afa32012-07-19 14:53:05 -07002728 // Load all the remaining pages (if we are loading synchronously, we want to defer this
2729 // work until after the first render)
Jason Monka0a7a742014-04-22 09:23:19 -04002730 synchronized (mDeferredBindRunnables) {
2731 mDeferredBindRunnables.clear();
2732 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002733 bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders,
Winson Chung4a2afa32012-07-19 14:53:05 -07002734 (isLoadingSynchronously ? mDeferredBindRunnables : null));
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002735
2736 // Tell the workspace that we're done binding items
2737 r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002738 public void run() {
2739 Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2740 if (callbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08002741 callbacks.finishBindingItems();
Joe Onorato36115782010-06-17 13:28:48 -04002742 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002743
Winson Chung98e030b2012-05-07 16:01:11 -07002744 // If we're profiling, ensure this is the last thing in the queue.
Joe Onorato36115782010-06-17 13:28:48 -04002745 if (DEBUG_LOADERS) {
2746 Log.d(TAG, "bound workspace in "
2747 + (SystemClock.uptimeMillis()-t) + "ms");
2748 }
Winson Chung36a62fe2012-05-06 18:04:42 -07002749
2750 mIsLoadingAndBindingWorkspace = false;
Joe Onorato36115782010-06-17 13:28:48 -04002751 }
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002752 };
Winson Chung4a2afa32012-07-19 14:53:05 -07002753 if (isLoadingSynchronously) {
Jason Monka0a7a742014-04-22 09:23:19 -04002754 synchronized (mDeferredBindRunnables) {
2755 mDeferredBindRunnables.add(r);
2756 }
Winson Chung4a2afa32012-07-19 14:53:05 -07002757 } else {
Winson Chung81b52252012-08-27 15:34:29 -07002758 runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE);
Winson Chung4a2afa32012-07-19 14:53:05 -07002759 }
Joe Onorato36115782010-06-17 13:28:48 -04002760 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002761
Joe Onorato36115782010-06-17 13:28:48 -04002762 private void loadAndBindAllApps() {
2763 if (DEBUG_LOADERS) {
2764 Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
2765 }
2766 if (!mAllAppsLoaded) {
Winson Chung64359a52013-07-08 17:17:08 -07002767 loadAllApps();
Reena Lee93f824a2011-09-23 17:20:28 -07002768 synchronized (LoaderTask.this) {
2769 if (mStopped) {
2770 return;
2771 }
2772 mAllAppsLoaded = true;
Joe Onoratocc67f472010-06-08 10:54:30 -07002773 }
Joe Onorato36115782010-06-17 13:28:48 -04002774 } else {
2775 onlyBindAllApps();
2776 }
2777 }
Joe Onoratocc67f472010-06-08 10:54:30 -07002778
Joe Onorato36115782010-06-17 13:28:48 -04002779 private void onlyBindAllApps() {
2780 final Callbacks oldCallbacks = mCallbacks.get();
2781 if (oldCallbacks == null) {
2782 // This launcher has exited and nobody bothered to tell us. Just bail.
2783 Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)");
2784 return;
2785 }
2786
2787 // shallow copy
Winson Chungc208ff92012-03-29 17:37:41 -07002788 @SuppressWarnings("unchecked")
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002789 final ArrayList<AppInfo> list
2790 = (ArrayList<AppInfo>) mBgAllAppsList.data.clone();
Winson Chungc93e5ae2012-07-23 20:48:26 -07002791 Runnable r = new Runnable() {
Joe Onorato36115782010-06-17 13:28:48 -04002792 public void run() {
2793 final long t = SystemClock.uptimeMillis();
2794 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
2795 if (callbacks != null) {
2796 callbacks.bindAllApplications(list);
2797 }
2798 if (DEBUG_LOADERS) {
2799 Log.d(TAG, "bound all " + list.size() + " apps from cache in "
2800 + (SystemClock.uptimeMillis()-t) + "ms");
2801 }
2802 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07002803 };
2804 boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
Winson Chung64359a52013-07-08 17:17:08 -07002805 if (isRunningOnMainThread) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002806 r.run();
2807 } else {
2808 mHandler.post(r);
2809 }
Joe Onorato36115782010-06-17 13:28:48 -04002810 }
2811
Winson Chung64359a52013-07-08 17:17:08 -07002812 private void loadAllApps() {
2813 final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
Joe Onorato36115782010-06-17 13:28:48 -04002814
Joe Onorato36115782010-06-17 13:28:48 -04002815 final Callbacks oldCallbacks = mCallbacks.get();
2816 if (oldCallbacks == null) {
2817 // This launcher has exited and nobody bothered to tell us. Just bail.
Winson Chung64359a52013-07-08 17:17:08 -07002818 Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)");
Joe Onorato36115782010-06-17 13:28:48 -04002819 return;
2820 }
2821
2822 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2823 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
2824
Kenny Guyed131872014-04-30 03:02:21 +01002825 final List<UserHandleCompat> profiles = mUserManager.getUserProfiles();
2826
Winson Chung64359a52013-07-08 17:17:08 -07002827 // Clear the list of apps
2828 mBgAllAppsList.clear();
Sunny Goyale0f58d72014-11-10 18:05:31 -08002829 SharedPreferences prefs = mContext.getSharedPreferences(
2830 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
Kenny Guyed131872014-04-30 03:02:21 +01002831 for (UserHandleCompat user : profiles) {
2832 // Query for the set of apps
2833 final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2834 List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user);
2835 if (DEBUG_LOADERS) {
2836 Log.d(TAG, "getActivityList took "
2837 + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user);
2838 Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user);
2839 }
2840 // Fail if we don't have any apps
Sunny Goyale0f58d72014-11-10 18:05:31 -08002841 // TODO: Fix this. Only fail for the current user.
Kenny Guyed131872014-04-30 03:02:21 +01002842 if (apps == null || apps.isEmpty()) {
2843 return;
2844 }
2845 // Sort the applications by name
2846 final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
2847 Collections.sort(apps,
2848 new LauncherModel.ShortcutNameComparator(mLabelCache));
2849 if (DEBUG_LOADERS) {
2850 Log.d(TAG, "sort took "
2851 + (SystemClock.uptimeMillis()-sortTime) + "ms");
2852 }
Joe Onorato36115782010-06-17 13:28:48 -04002853
Kenny Guyed131872014-04-30 03:02:21 +01002854 // Create the ApplicationInfos
2855 for (int i = 0; i < apps.size(); i++) {
2856 LauncherActivityInfoCompat app = apps.get(i);
2857 // This builds the icon bitmaps.
2858 mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, mLabelCache));
2859 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002860
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08002861 if (ADD_MANAGED_PROFILE_SHORTCUTS && !user.equals(UserHandleCompat.myUserHandle())) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002862 // Add shortcuts for packages which were installed while launcher was dead.
2863 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
2864 + mUserManager.getSerialNumberForUser(user);
2865 Set<String> packagesAdded = prefs.getStringSet(shortcutsSetKey, Collections.EMPTY_SET);
2866 HashSet<String> newPackageSet = new HashSet<String>();
2867
2868 for (LauncherActivityInfoCompat info : apps) {
2869 String packageName = info.getComponentName().getPackageName();
2870 if (!packagesAdded.contains(packageName)
2871 && !newPackageSet.contains(packageName)) {
2872 InstallShortcutReceiver.queueInstallShortcut(info, mContext);
2873 }
2874 newPackageSet.add(packageName);
2875 }
2876
2877 prefs.edit().putStringSet(shortcutsSetKey, newPackageSet).commit();
2878 }
Winson Chung64359a52013-07-08 17:17:08 -07002879 }
Bjorn Bringert85f418d2013-09-06 12:50:05 +01002880 // Huh? Shouldn't this be inside the Runnable below?
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002881 final ArrayList<AppInfo> added = mBgAllAppsList.added;
2882 mBgAllAppsList.added = new ArrayList<AppInfo>();
Winson Chung64359a52013-07-08 17:17:08 -07002883
2884 // Post callback on main thread
2885 mHandler.post(new Runnable() {
2886 public void run() {
2887 final long bindTime = SystemClock.uptimeMillis();
Winson Chung11a1a532013-09-13 11:14:45 -07002888 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
Winson Chung64359a52013-07-08 17:17:08 -07002889 if (callbacks != null) {
2890 callbacks.bindAllApplications(added);
2891 if (DEBUG_LOADERS) {
2892 Log.d(TAG, "bound " + added.size() + " apps in "
2893 + (SystemClock.uptimeMillis() - bindTime) + "ms");
2894 }
2895 } else {
2896 Log.i(TAG, "not binding apps: no Launcher activity");
2897 }
2898 }
2899 });
2900
Joe Onorato36115782010-06-17 13:28:48 -04002901 if (DEBUG_LOADERS) {
Winson Chung64359a52013-07-08 17:17:08 -07002902 Log.d(TAG, "Icons processed in "
2903 + (SystemClock.uptimeMillis() - loadTime) + "ms");
Joe Onoratobe386092009-11-17 17:32:16 -08002904 }
2905 }
2906
2907 public void dumpState() {
Winson Chung2abf94d2012-07-18 18:16:38 -07002908 synchronized (sBgLock) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -07002909 Log.d(TAG, "mLoaderTask.mContext=" + mContext);
2910 Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching);
2911 Log.d(TAG, "mLoaderTask.mStopped=" + mStopped);
2912 Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished);
2913 Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size());
2914 }
Joe Onorato36115782010-06-17 13:28:48 -04002915 }
2916 }
2917
2918 void enqueuePackageUpdated(PackageUpdatedTask task) {
Brad Fitzpatrick700889f2010-10-11 09:40:44 -07002919 sWorker.post(task);
Joe Onorato36115782010-06-17 13:28:48 -04002920 }
2921
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002922 private class AppsAvailabilityCheck extends BroadcastReceiver {
2923
2924 @Override
2925 public void onReceive(Context context, Intent intent) {
2926 synchronized (sBgLock) {
2927 final LauncherAppsCompat launcherApps = LauncherAppsCompat
2928 .getInstance(mApp.getContext());
Sunny Goyal1a745e82014-10-02 15:58:31 -07002929 final PackageManager manager = context.getPackageManager();
2930 final ArrayList<String> packagesRemoved = new ArrayList<String>();
2931 final ArrayList<String> packagesUnavailable = new ArrayList<String>();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002932 for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) {
2933 UserHandleCompat user = entry.getKey();
Sunny Goyal1a745e82014-10-02 15:58:31 -07002934 packagesRemoved.clear();
2935 packagesUnavailable.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002936 for (String pkg : entry.getValue()) {
2937 if (!launcherApps.isPackageEnabledForProfile(pkg, user)) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07002938 boolean packageOnSdcard = launcherApps.isAppEnabled(
2939 manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
2940 if (packageOnSdcard) {
2941 Launcher.addDumpLog(TAG, "Package found on sd-card: " + pkg, true);
2942 packagesUnavailable.add(pkg);
2943 } else {
2944 Launcher.addDumpLog(TAG, "Package not found: " + pkg, true);
2945 packagesRemoved.add(pkg);
2946 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002947 }
2948 }
2949 if (!packagesRemoved.isEmpty()) {
2950 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE,
2951 packagesRemoved.toArray(new String[packagesRemoved.size()]), user));
2952 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07002953 if (!packagesUnavailable.isEmpty()) {
2954 enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE,
2955 packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user));
2956 }
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002957 }
Sunny Goyal34942622014-08-29 17:20:55 -07002958 sPendingPackages.clear();
Sunny Goyalf599ccf2014-07-08 13:01:29 -07002959 }
2960 }
2961 }
2962
Joe Onorato36115782010-06-17 13:28:48 -04002963 private class PackageUpdatedTask implements Runnable {
2964 int mOp;
2965 String[] mPackages;
Kenny Guyed131872014-04-30 03:02:21 +01002966 UserHandleCompat mUser;
Joe Onorato36115782010-06-17 13:28:48 -04002967
2968 public static final int OP_NONE = 0;
2969 public static final int OP_ADD = 1;
2970 public static final int OP_UPDATE = 2;
2971 public static final int OP_REMOVE = 3; // uninstlled
2972 public static final int OP_UNAVAILABLE = 4; // external media unmounted
2973
2974
Kenny Guyed131872014-04-30 03:02:21 +01002975 public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) {
Joe Onorato36115782010-06-17 13:28:48 -04002976 mOp = op;
2977 mPackages = packages;
Kenny Guyed131872014-04-30 03:02:21 +01002978 mUser = user;
Joe Onorato36115782010-06-17 13:28:48 -04002979 }
2980
2981 public void run() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002982 final Context context = mApp.getContext();
Joe Onorato36115782010-06-17 13:28:48 -04002983
2984 final String[] packages = mPackages;
2985 final int N = packages.length;
2986 switch (mOp) {
2987 case OP_ADD:
2988 for (int i=0; i<N; i++) {
2989 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01002990 mIconCache.remove(packages[i], mUser);
2991 mBgAllAppsList.addPackage(context, packages[i], mUser);
Joe Onorato36115782010-06-17 13:28:48 -04002992 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08002993
2994 // Auto add shortcuts for added packages.
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08002995 if (ADD_MANAGED_PROFILE_SHORTCUTS
2996 && !UserHandleCompat.myUserHandle().equals(mUser)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08002997 SharedPreferences prefs = context.getSharedPreferences(
2998 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
2999 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
3000 + mUserManager.getSerialNumberForUser(mUser);
3001 Set<String> shortcutSet = new HashSet<String>(
3002 prefs.getStringSet(shortcutsSetKey,Collections.EMPTY_SET));
3003
3004 for (int i=0; i<N; i++) {
3005 if (!shortcutSet.contains(packages[i])) {
3006 shortcutSet.add(packages[i]);
3007 List<LauncherActivityInfoCompat> activities =
3008 mLauncherApps.getActivityList(packages[i], mUser);
3009 if (activities != null && !activities.isEmpty()) {
3010 InstallShortcutReceiver.queueInstallShortcut(
3011 activities.get(0), context);
3012 }
3013 }
3014 }
3015
3016 prefs.edit().putStringSet(shortcutsSetKey, shortcutSet).commit();
3017 }
Joe Onorato36115782010-06-17 13:28:48 -04003018 break;
3019 case OP_UPDATE:
3020 for (int i=0; i<N; i++) {
3021 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]);
Kenny Guyed131872014-04-30 03:02:21 +01003022 mBgAllAppsList.updatePackage(context, packages[i], mUser);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07003023 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04003024 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04003025 }
3026 break;
3027 case OP_REMOVE:
Sunny Goyale0f58d72014-11-10 18:05:31 -08003028 // Remove the packageName for the set of auto-installed shortcuts. This
3029 // will ensure that the shortcut when the app is installed again.
Sunny Goyal2a66bbf2014-11-20 17:01:00 -08003030 if (ADD_MANAGED_PROFILE_SHORTCUTS
3031 && !UserHandleCompat.myUserHandle().equals(mUser)) {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003032 SharedPreferences prefs = context.getSharedPreferences(
3033 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
3034 String shortcutsSetKey = INSTALLED_SHORTCUTS_SET_PREFIX
3035 + mUserManager.getSerialNumberForUser(mUser);
3036 HashSet<String> shortcutSet = new HashSet<String>(
3037 prefs.getStringSet(shortcutsSetKey, Collections.EMPTY_SET));
3038 shortcutSet.removeAll(Arrays.asList(mPackages));
3039 prefs.edit().putStringSet(shortcutsSetKey, shortcutSet).commit();
3040 }
3041 // Fall through
Joe Onorato36115782010-06-17 13:28:48 -04003042 case OP_UNAVAILABLE:
Sunny Goyal1a745e82014-10-02 15:58:31 -07003043 boolean clearCache = mOp == OP_REMOVE;
Joe Onorato36115782010-06-17 13:28:48 -04003044 for (int i=0; i<N; i++) {
3045 if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]);
Sunny Goyal1a745e82014-10-02 15:58:31 -07003046 mBgAllAppsList.removePackage(packages[i], mUser, clearCache);
Michael Jurkaeb1bb922013-09-26 11:29:01 -07003047 WidgetPreviewLoader.removePackageFromDb(
Daniel Sandlere4f98912013-06-25 15:13:26 -04003048 mApp.getWidgetPreviewCacheDb(), packages[i]);
Joe Onorato36115782010-06-17 13:28:48 -04003049 }
3050 break;
3051 }
3052
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003053 ArrayList<AppInfo> added = null;
3054 ArrayList<AppInfo> modified = null;
3055 final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>();
Joe Onorato36115782010-06-17 13:28:48 -04003056
Adam Cohen487f7dd2012-06-28 18:12:10 -07003057 if (mBgAllAppsList.added.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003058 added = new ArrayList<AppInfo>(mBgAllAppsList.added);
Winson Chung5d55f332012-07-16 20:45:03 -07003059 mBgAllAppsList.added.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003060 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003061 if (mBgAllAppsList.modified.size() > 0) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003062 modified = new ArrayList<AppInfo>(mBgAllAppsList.modified);
Winson Chung5d55f332012-07-16 20:45:03 -07003063 mBgAllAppsList.modified.clear();
Joe Onorato36115782010-06-17 13:28:48 -04003064 }
Winson Chung5d55f332012-07-16 20:45:03 -07003065 if (mBgAllAppsList.removed.size() > 0) {
Winson Chung83892cc2013-05-01 16:53:33 -07003066 removedApps.addAll(mBgAllAppsList.removed);
Winson Chung5d55f332012-07-16 20:45:03 -07003067 mBgAllAppsList.removed.clear();
Winson Chungcd810732012-06-18 16:45:43 -07003068 }
3069
Sunny Goyale0f58d72014-11-10 18:05:31 -08003070 final Callbacks callbacks = getCallback();
Joe Onorato36115782010-06-17 13:28:48 -04003071 if (callbacks == null) {
3072 Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading.");
3073 return;
3074 }
3075
Sunny Goyal4390ace2014-10-13 11:33:11 -07003076 final HashMap<ComponentName, AppInfo> addedOrUpdatedApps =
3077 new HashMap<ComponentName, AppInfo>();
3078
Joe Onorato36115782010-06-17 13:28:48 -04003079 if (added != null) {
Winson Chung64359a52013-07-08 17:17:08 -07003080 // Ensure that we add all the workspace applications to the db
Adam Cohen76a47a12014-02-05 11:47:43 -08003081 if (LauncherAppState.isDisableAllApps()) {
Winson Chung94d67682013-09-25 16:29:40 -07003082 final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>(added);
Adam Cohen76a47a12014-02-05 11:47:43 -08003083 addAndBindAddedWorkspaceApps(context, addedInfos);
3084 } else {
3085 addAppsToAllApps(context, added);
Winson Chung94d67682013-09-25 16:29:40 -07003086 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003087 for (AppInfo ai : added) {
3088 addedOrUpdatedApps.put(ai.componentName, ai);
3089 }
Joe Onorato36115782010-06-17 13:28:48 -04003090 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003091
Joe Onorato36115782010-06-17 13:28:48 -04003092 if (modified != null) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003093 final ArrayList<AppInfo> modifiedFinal = modified;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003094 for (AppInfo ai : modified) {
3095 addedOrUpdatedApps.put(ai.componentName, ai);
Winson Chung64359a52013-07-08 17:17:08 -07003096 }
3097
Joe Onorato36115782010-06-17 13:28:48 -04003098 mHandler.post(new Runnable() {
3099 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003100 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003101 if (callbacks == cb && cb != null) {
Joe Onorato36115782010-06-17 13:28:48 -04003102 callbacks.bindAppsUpdated(modifiedFinal);
3103 }
3104 }
3105 });
3106 }
Winson Chung83892cc2013-05-01 16:53:33 -07003107
Sunny Goyal4390ace2014-10-13 11:33:11 -07003108 // Update shortcut infos
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003109 if (mOp == OP_ADD || mOp == OP_UPDATE) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003110 final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>();
3111 final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>();
3112 final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>();
3113
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003114 HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages));
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003115 synchronized (sBgLock) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003116 for (ItemInfo info : sBgItemsIdMap.values()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003117 if (info instanceof ShortcutInfo && mUser.equals(info.user)) {
3118 ShortcutInfo si = (ShortcutInfo) info;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003119 boolean infoUpdated = false;
3120 boolean shortcutUpdated = false;
3121
3122 // Update shortcuts which use iconResource.
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003123 if ((si.iconResource != null)
Sunny Goyal4390ace2014-10-13 11:33:11 -07003124 && packageSet.contains(si.iconResource.packageName)) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003125 Bitmap icon = Utilities.createIconBitmap(si.iconResource.packageName,
3126 si.iconResource.resourceName, mIconCache, context);
3127 if (icon != null) {
3128 si.setIcon(icon);
3129 si.usingFallbackIcon = false;
Sunny Goyal4390ace2014-10-13 11:33:11 -07003130 infoUpdated = true;
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003131 }
3132 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07003133
3134 ComponentName cn = si.getTargetComponent();
3135 if (cn != null && packageSet.contains(cn.getPackageName())) {
3136 AppInfo appInfo = addedOrUpdatedApps.get(cn);
3137
3138 if (si.isPromise()) {
3139 mIconCache.deletePreloadedIcon(cn, mUser);
3140 if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
3141 // Auto install icon
3142 PackageManager pm = context.getPackageManager();
3143 ResolveInfo matched = pm.resolveActivity(
3144 new Intent(Intent.ACTION_MAIN)
3145 .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER),
3146 PackageManager.MATCH_DEFAULT_ONLY);
3147 if (matched == null) {
3148 // Try to find the best match activity.
3149 Intent intent = pm.getLaunchIntentForPackage(
3150 cn.getPackageName());
3151 if (intent != null) {
3152 cn = intent.getComponent();
3153 appInfo = addedOrUpdatedApps.get(cn);
3154 }
3155
3156 if ((intent == null) || (appInfo == null)) {
3157 removedShortcuts.add(si);
3158 continue;
3159 }
3160 si.promisedIntent = intent;
3161 }
3162 }
3163
3164 // Restore the shortcut.
3165 si.intent = si.promisedIntent;
3166 si.promisedIntent = null;
3167 si.status &= ~ShortcutInfo.FLAG_RESTORED_ICON
3168 & ~ShortcutInfo.FLAG_AUTOINTALL_ICON
3169 & ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
3170
3171 infoUpdated = true;
3172 si.updateIcon(mIconCache);
3173 }
3174
3175 if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction())
3176 && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
3177 si.updateIcon(mIconCache);
3178 si.title = appInfo.title.toString();
3179 si.contentDescription = appInfo.contentDescription;
3180 infoUpdated = true;
3181 }
3182
3183 if ((si.isDisabled & ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE) != 0) {
3184 // Since package was just updated, the target must be available now.
3185 si.isDisabled &= ~ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3186 shortcutUpdated = true;
3187 }
3188 }
3189
3190 if (infoUpdated || shortcutUpdated) {
3191 updatedShortcuts.add(si);
3192 }
3193 if (infoUpdated) {
3194 updateItemInDatabase(context, si);
3195 }
3196 } else if (info instanceof LauncherAppWidgetInfo) {
3197 LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info;
3198 if (mUser.equals(widgetInfo.user)
3199 && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)
3200 && packageSet.contains(widgetInfo.providerName.getPackageName())) {
3201 widgetInfo.restoreStatus &= ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
3202 widgets.add(widgetInfo);
3203 updateItemInDatabase(context, widgetInfo);
3204 }
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003205 }
3206 }
3207 }
3208
Sunny Goyal4390ace2014-10-13 11:33:11 -07003209 if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) {
3210 mHandler.post(new Runnable() {
3211
3212 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003213 Callbacks cb = getCallback();
Sunny Goyal4390ace2014-10-13 11:33:11 -07003214 if (callbacks == cb && cb != null) {
3215 callbacks.bindShortcutsChanged(
3216 updatedShortcuts, removedShortcuts, mUser);
3217 }
3218 }
3219 });
3220 if (!removedShortcuts.isEmpty()) {
3221 deleteItemsFromDatabase(context, removedShortcuts);
3222 }
3223 }
3224 if (!widgets.isEmpty()) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003225 mHandler.post(new Runnable() {
3226 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003227 Callbacks cb = getCallback();
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003228 if (callbacks == cb && cb != null) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07003229 callbacks.bindWidgetsRestored(widgets);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003230 }
3231 }
3232 });
3233 }
3234 }
3235
Winson Chungdf95eb12013-10-16 14:57:07 -07003236 final ArrayList<String> removedPackageNames =
3237 new ArrayList<String>();
Sunny Goyal1a745e82014-10-02 15:58:31 -07003238 if (mOp == OP_REMOVE || mOp == OP_UNAVAILABLE) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003239 // Mark all packages in the broadcast to be removed
3240 removedPackageNames.addAll(Arrays.asList(packages));
3241 } else if (mOp == OP_UPDATE) {
3242 // Mark disabled packages in the broadcast to be removed
Winson Chungdf95eb12013-10-16 14:57:07 -07003243 for (int i=0; i<N; i++) {
Kenny Guyed131872014-04-30 03:02:21 +01003244 if (isPackageDisabled(context, packages[i], mUser)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003245 removedPackageNames.add(packages[i]);
Winson Chung64359a52013-07-08 17:17:08 -07003246 }
3247 }
Winson Chungdf95eb12013-10-16 14:57:07 -07003248 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07003249
Winson Chungdf95eb12013-10-16 14:57:07 -07003250 if (!removedPackageNames.isEmpty() || !removedApps.isEmpty()) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003251 final int removeReason;
3252 if (mOp == OP_UNAVAILABLE) {
3253 removeReason = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE;
3254 } else {
3255 // Remove all the components associated with this package
3256 for (String pn : removedPackageNames) {
3257 deletePackageFromDatabase(context, pn, mUser);
3258 }
3259 // Remove all the specific components
3260 for (AppInfo a : removedApps) {
3261 ArrayList<ItemInfo> infos = getItemInfoForComponentName(a.componentName, mUser);
3262 deleteItemsFromDatabase(context, infos);
3263 }
3264 removeReason = 0;
3265 }
3266
Winson Chungdf95eb12013-10-16 14:57:07 -07003267 // Remove any queued items from the install queue
Sunny Goyale0f58d72014-11-10 18:05:31 -08003268 InstallShortcutReceiver.removeFromInstallQueue(context, removedPackageNames, mUser);
Winson Chungdf95eb12013-10-16 14:57:07 -07003269 // Call the components-removed callback
Joe Onorato36115782010-06-17 13:28:48 -04003270 mHandler.post(new Runnable() {
3271 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003272 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003273 if (callbacks == cb && cb != null) {
Sunny Goyal1a745e82014-10-02 15:58:31 -07003274 callbacks.bindComponentsRemoved(
3275 removedPackageNames, removedApps, mUser, removeReason);
Joe Onorato36115782010-06-17 13:28:48 -04003276 }
3277 }
3278 });
Joe Onoratobe386092009-11-17 17:32:16 -08003279 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003280
Michael Jurkac402cd92013-05-20 15:49:32 +02003281 final ArrayList<Object> widgetsAndShortcuts =
Kenny Guyed131872014-04-30 03:02:21 +01003282 getSortedWidgetsAndShortcuts(context);
Winson Chung80baf5a2010-08-09 16:03:15 -07003283 mHandler.post(new Runnable() {
3284 @Override
3285 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003286 Callbacks cb = getCallback();
Winson Chungcd2b0142011-06-08 16:02:26 -07003287 if (callbacks == cb && cb != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003288 callbacks.bindPackagesUpdated(widgetsAndShortcuts);
Winson Chung80baf5a2010-08-09 16:03:15 -07003289 }
3290 }
3291 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003292
3293 // Write all the logs to disk
Adam Cohen4caf2982013-08-20 18:54:31 -07003294 mHandler.post(new Runnable() {
3295 public void run() {
Sunny Goyale0f58d72014-11-10 18:05:31 -08003296 Callbacks cb = getCallback();
Adam Cohen4caf2982013-08-20 18:54:31 -07003297 if (callbacks == cb && cb != null) {
Winson Chungede41292013-09-19 16:27:36 -07003298 callbacks.dumpLogsToLocalData();
Adam Cohen4caf2982013-08-20 18:54:31 -07003299 }
3300 }
3301 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 }
3303 }
3304
Adam Cohen59400422014-03-05 18:07:04 -08003305 public static List<LauncherAppWidgetProviderInfo> getWidgetProviders(Context context) {
3306 synchronized (sBgLock) {
3307 if (sBgWidgetProviders != null && !sWidgetProvidersDirty) {
3308 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3309 }
3310 sBgWidgetProviders = new HashMap<ComponentName, LauncherAppWidgetProviderInfo>();
3311 List<AppWidgetProviderInfo> widgets =
3312 AppWidgetManagerCompat.getInstance(context).getAllProviders();
3313 LauncherAppWidgetProviderInfo info;
3314 for (AppWidgetProviderInfo pInfo : widgets) {
3315 info = LauncherAppWidgetProviderInfo.fromProviderInfo(context, pInfo);
3316 sBgWidgetProviders.put(info.provider, info);
3317 }
3318
3319 Collection<CustomAppWidget> customWidgets = Launcher.getCustomAppWidgets().values();
3320 for (CustomAppWidget widget : customWidgets) {
3321 info = new LauncherAppWidgetProviderInfo(context, widget);
3322 sBgWidgetProviders.put(info.provider, info);
3323 }
3324 sWidgetProvidersDirty = false;
3325 return new ArrayList<LauncherAppWidgetProviderInfo>(sBgWidgetProviders.values());
3326 }
3327 }
3328
3329 public static LauncherAppWidgetProviderInfo getProviderInfo(Context ctx, ComponentName name) {
3330 synchronized (sBgLock) {
3331 if (sBgWidgetProviders == null) {
3332 getWidgetProviders(ctx);
3333 }
3334 return sBgWidgetProviders.get(name);
3335 }
3336 }
3337
Michael Jurkac402cd92013-05-20 15:49:32 +02003338 // Returns a list of ResolveInfos/AppWindowInfos in sorted order
3339 public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) {
3340 PackageManager packageManager = context.getPackageManager();
3341 final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>();
Adam Cohen59400422014-03-05 18:07:04 -08003342 widgetsAndShortcuts.addAll(getWidgetProviders(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003343 Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
3344 widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0));
Sunny Goyalffe83f12014-08-14 17:39:34 -07003345 Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context));
Michael Jurkac402cd92013-05-20 15:49:32 +02003346 return widgetsAndShortcuts;
3347 }
3348
Kenny Guyed131872014-04-30 03:02:21 +01003349 private static boolean isPackageDisabled(Context context, String packageName,
3350 UserHandleCompat user) {
3351 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3352 return !launcherApps.isPackageEnabledForProfile(packageName, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07003353 }
Adam Cohen556f6132014-01-15 15:18:08 -08003354
Kenny Guyed131872014-04-30 03:02:21 +01003355 public static boolean isValidPackageActivity(Context context, ComponentName cn,
3356 UserHandleCompat user) {
Winson Chungee055712013-07-30 14:46:24 -07003357 if (cn == null) {
3358 return false;
3359 }
Kenny Guyed131872014-04-30 03:02:21 +01003360 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3361 if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) {
Winson Chungdf95eb12013-10-16 14:57:07 -07003362 return false;
3363 }
Kenny Guyed131872014-04-30 03:02:21 +01003364 return launcherApps.isActivityEnabledForProfile(cn, user);
Winson Chungee055712013-07-30 14:46:24 -07003365 }
3366
Adam Cohena28b78e2014-05-20 17:03:04 -07003367 public static boolean isValidPackage(Context context, String packageName,
3368 UserHandleCompat user) {
3369 if (packageName == null) {
3370 return false;
3371 }
3372 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context);
3373 return launcherApps.isPackageEnabledForProfile(packageName, user);
3374 }
3375
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 /**
Chris Wrenf4d08112014-01-16 18:13:56 -05003377 * Make an ShortcutInfo object for a restored application or shortcut item that points
3378 * to a package that is not yet installed on the system.
3379 */
Sunny Goyal34942622014-08-29 17:20:55 -07003380 public ShortcutInfo getRestoredItemInfo(Cursor cursor, int titleIndex, Intent intent,
3381 int promiseType) {
Chris Wrenf4d08112014-01-16 18:13:56 -05003382 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003383 info.user = UserHandleCompat.myUserHandle();
Sunny Goyal34942622014-08-29 17:20:55 -07003384 mIconCache.getTitleAndIcon(info, intent, info.user, true);
3385
3386 if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) {
3387 String title = (cursor != null) ? cursor.getString(titleIndex) : null;
3388 if (!TextUtils.isEmpty(title)) {
3389 info.title = title;
3390 }
3391 info.status = ShortcutInfo.FLAG_RESTORED_ICON;
3392 } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) {
3393 if (TextUtils.isEmpty(info.title)) {
3394 info.title = (cursor != null) ? cursor.getString(titleIndex) : "";
3395 }
3396 info.status = ShortcutInfo.FLAG_AUTOINTALL_ICON;
3397 } else {
3398 throw new InvalidParameterException("Invalid restoreType " + promiseType);
3399 }
3400
Kenny Guyc2bd8102014-06-30 12:30:31 +01003401 info.contentDescription = mUserManager.getBadgedLabelForUser(
3402 info.title.toString(), info.user);
Chris Wrenf4d08112014-01-16 18:13:56 -05003403 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
Sunny Goyal34942622014-08-29 17:20:55 -07003404 info.promisedIntent = intent;
Chris Wrenf4d08112014-01-16 18:13:56 -05003405 return info;
3406 }
3407
3408 /**
3409 * Make an Intent object for a restored application or shortcut item that points
3410 * to the market page for the item.
3411 */
3412 private Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) {
3413 ComponentName componentName = intent.getComponent();
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003414 return getMarketIntent(componentName.getPackageName());
3415 }
3416
3417 static Intent getMarketIntent(String packageName) {
3418 return new Intent(Intent.ACTION_VIEW)
3419 .setData(new Uri.Builder()
Chris Wrenf4d08112014-01-16 18:13:56 -05003420 .scheme("market")
3421 .authority("details")
Sunny Goyale7b8cd92014-08-27 14:04:33 -07003422 .appendQueryParameter("id", packageName)
3423 .build());
Chris Wrenf4d08112014-01-16 18:13:56 -05003424 }
3425
3426 /**
Joe Onorato56d82912010-03-07 14:32:10 -05003427 * This is called from the code that adds shortcuts from the intent receiver. This
3428 * doesn't have a Cursor, but
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 */
Kenny Guyed131872014-04-30 03:02:21 +01003430 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3431 UserHandleCompat user, Context context) {
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003432 return getShortcutInfo(manager, intent, user, context, null, -1, -1, null, false);
Joe Onorato56d82912010-03-07 14:32:10 -05003433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434
Joe Onorato56d82912010-03-07 14:32:10 -05003435 /**
3436 * Make an ShortcutInfo object for a shortcut that is an application.
3437 *
3438 * If c is not null, then it will be used to fill in missing data like the title and icon.
3439 */
Kenny Guyed131872014-04-30 03:02:21 +01003440 public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent,
3441 UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex,
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003442 HashMap<Object, CharSequence> labelCache, boolean allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003443 if (user == null) {
3444 Log.d(TAG, "Null user found in getShortcutInfo");
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003445 return null;
3446 }
3447
Kenny Guyed131872014-04-30 03:02:21 +01003448 ComponentName componentName = intent.getComponent();
3449 if (componentName == null) {
3450 Log.d(TAG, "Missing component found in getShortcutInfo: " + componentName);
3451 return null;
3452 }
3453
3454 Intent newIntent = new Intent(intent.getAction(), null);
3455 newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3456 newIntent.setComponent(componentName);
3457 LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user);
Sunny Goyalf599ccf2014-07-08 13:01:29 -07003458 if ((lai == null) && !allowMissingTarget) {
Kenny Guyed131872014-04-30 03:02:21 +01003459 Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
3460 return null;
3461 }
3462
3463 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003464
Joe Onorato56d82912010-03-07 14:32:10 -05003465 // the resource -- This may implicitly give us back the fallback icon,
3466 // but don't worry about that. All we're doing with usingFallbackIcon is
3467 // to avoid saving lots of copies of that in the database, and most apps
3468 // have icons anyway.
Kenny Guyed131872014-04-30 03:02:21 +01003469 Bitmap icon = mIconCache.getIcon(componentName, lai, labelCache);
Winson Chungc208ff92012-03-29 17:37:41 -07003470
Joe Onorato56d82912010-03-07 14:32:10 -05003471 // the db
3472 if (icon == null) {
3473 if (c != null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003474 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003475 }
3476 }
3477 // the fallback icon
3478 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003479 icon = mIconCache.getDefaultIcon(user);
Joe Onorato56d82912010-03-07 14:32:10 -05003480 info.usingFallbackIcon = true;
3481 }
3482 info.setIcon(icon);
3483
Kenny Guyed131872014-04-30 03:02:21 +01003484 // From the cache.
3485 if (labelCache != null) {
3486 info.title = labelCache.get(componentName);
3487 }
3488
Joe Onorato56d82912010-03-07 14:32:10 -05003489 // from the resource
Kenny Guyed131872014-04-30 03:02:21 +01003490 if (info.title == null && lai != null) {
3491 info.title = lai.getLabel();
3492 if (labelCache != null) {
3493 labelCache.put(componentName, info.title);
Winson Chungc3eecff2011-07-11 17:44:15 -07003494 }
Joe Onorato56d82912010-03-07 14:32:10 -05003495 }
3496 // from the db
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 if (info.title == null) {
Joe Onorato56d82912010-03-07 14:32:10 -05003498 if (c != null) {
3499 info.title = c.getString(titleIndex);
3500 }
3501 }
3502 // fall back to the class name of the activity
3503 if (info.title == null) {
3504 info.title = componentName.getClassName();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Kenny Guyed131872014-04-30 03:02:21 +01003507 info.user = user;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003508 info.contentDescription = mUserManager.getBadgedLabelForUser(
3509 info.title.toString(), info.user);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 return info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003511 }
The Android Open Source Projectbc219c32009-03-09 11:52:14 -07003512
Winson Chung64359a52013-07-08 17:17:08 -07003513 static ArrayList<ItemInfo> filterItemInfos(Collection<ItemInfo> infos,
3514 ItemInfoFilter f) {
3515 HashSet<ItemInfo> filtered = new HashSet<ItemInfo>();
3516 for (ItemInfo i : infos) {
3517 if (i instanceof ShortcutInfo) {
3518 ShortcutInfo info = (ShortcutInfo) i;
Sunny Goyal34942622014-08-29 17:20:55 -07003519 ComponentName cn = info.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003520 if (cn != null && f.filterItem(null, info, cn)) {
3521 filtered.add(info);
3522 }
3523 } else if (i instanceof FolderInfo) {
3524 FolderInfo info = (FolderInfo) i;
3525 for (ShortcutInfo s : info.contents) {
Sunny Goyal34942622014-08-29 17:20:55 -07003526 ComponentName cn = s.getTargetComponent();
Winson Chung64359a52013-07-08 17:17:08 -07003527 if (cn != null && f.filterItem(info, s, cn)) {
3528 filtered.add(s);
Winson Chung8a435102012-08-30 17:16:53 -07003529 }
3530 }
Winson Chung64359a52013-07-08 17:17:08 -07003531 } else if (i instanceof LauncherAppWidgetInfo) {
3532 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i;
3533 ComponentName cn = info.providerName;
3534 if (cn != null && f.filterItem(null, info, cn)) {
3535 filtered.add(info);
3536 }
Winson Chung8a435102012-08-30 17:16:53 -07003537 }
3538 }
Winson Chung64359a52013-07-08 17:17:08 -07003539 return new ArrayList<ItemInfo>(filtered);
3540 }
3541
Kenny Guyed131872014-04-30 03:02:21 +01003542 private ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname,
3543 final UserHandleCompat user) {
Winson Chung64359a52013-07-08 17:17:08 -07003544 ItemInfoFilter filter = new ItemInfoFilter() {
3545 @Override
3546 public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) {
Kenny Guyed131872014-04-30 03:02:21 +01003547 if (info.user == null) {
3548 return cn.equals(cname);
3549 } else {
3550 return cn.equals(cname) && info.user.equals(user);
3551 }
Winson Chung64359a52013-07-08 17:17:08 -07003552 }
3553 };
3554 return filterItemInfos(sBgItemsIdMap.values(), filter);
3555 }
3556
Sunny Goyal1a745e82014-10-02 15:58:31 -07003557 /**
Joe Onorato0589f0f2010-02-08 13:44:00 -08003558 * Make an ShortcutInfo object for a shortcut that isn't an application.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003559 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08003560 private ShortcutInfo getShortcutInfo(Cursor c, Context context,
Joe Onorato56d82912010-03-07 14:32:10 -05003561 int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex,
3562 int titleIndex) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003563
Joe Onorato56d82912010-03-07 14:32:10 -05003564 Bitmap icon = null;
Michael Jurkac9d95c52011-08-29 14:03:34 -07003565 final ShortcutInfo info = new ShortcutInfo();
Kenny Guyed131872014-04-30 03:02:21 +01003566 // Non-app shortcuts are only supported for current user.
3567 info.user = UserHandleCompat.myUserHandle();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003569
Joe Onorato8ddc4fd2010-03-17 09:14:50 -07003570 // TODO: If there's an explicit component and we can't install that, delete it.
3571
Joe Onorato56d82912010-03-07 14:32:10 -05003572 info.title = c.getString(titleIndex);
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 int iconType = c.getInt(iconTypeIndex);
3575 switch (iconType) {
3576 case LauncherSettings.Favorites.ICON_TYPE_RESOURCE:
3577 String packageName = c.getString(iconPackageIndex);
3578 String resourceName = c.getString(iconResourceIndex);
Joe Onorato56d82912010-03-07 14:32:10 -05003579 info.customIcon = false;
3580 // the resource
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003581 icon = Utilities.createIconBitmap(packageName, resourceName, mIconCache, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003582 // the db
3583 if (icon == null) {
Michael Jurka931dc972011-08-05 15:08:15 -07003584 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003585 }
3586 // the fallback icon
3587 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003588 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003589 info.usingFallbackIcon = true;
3590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 break;
3592 case LauncherSettings.Favorites.ICON_TYPE_BITMAP:
Michael Jurka931dc972011-08-05 15:08:15 -07003593 icon = getIconFromCursor(c, iconIndex, context);
Joe Onorato56d82912010-03-07 14:32:10 -05003594 if (icon == null) {
Kenny Guyed131872014-04-30 03:02:21 +01003595 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003596 info.customIcon = false;
3597 info.usingFallbackIcon = true;
3598 } else {
3599 info.customIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 break;
3602 default:
Kenny Guyed131872014-04-30 03:02:21 +01003603 icon = mIconCache.getDefaultIcon(info.user);
Joe Onorato56d82912010-03-07 14:32:10 -05003604 info.usingFallbackIcon = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 info.customIcon = false;
3606 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003607 }
Joe Onoratod8d22da2010-03-11 17:59:11 -08003608 info.setIcon(icon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 return info;
3610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003611
Michael Jurka931dc972011-08-05 15:08:15 -07003612 Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) {
Michael Jurka3a9fced2012-04-13 14:44:29 -07003613 @SuppressWarnings("all") // suppress dead code warning
3614 final boolean debug = false;
3615 if (debug) {
Joe Onorato56d82912010-03-07 14:32:10 -05003616 Log.d(TAG, "getIconFromCursor app="
3617 + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE)));
3618 }
3619 byte[] data = c.getBlob(iconIndex);
3620 try {
Michael Jurka931dc972011-08-05 15:08:15 -07003621 return Utilities.createIconBitmap(
3622 BitmapFactory.decodeByteArray(data, 0, data.length), context);
Joe Onorato56d82912010-03-07 14:32:10 -05003623 } catch (Exception e) {
3624 return null;
3625 }
3626 }
3627
Sunny Goyal2350bc92014-10-14 16:42:54 -07003628 ShortcutInfo infoFromShortcutIntent(Context context, Intent data) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003629 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
3630 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
3631 Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
3632
Adam Cohend9198822011-11-22 16:42:47 -08003633 if (intent == null) {
3634 // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
3635 Log.e(TAG, "Can't construct ShorcutInfo with null intent");
3636 return null;
3637 }
3638
Joe Onorato0589f0f2010-02-08 13:44:00 -08003639 Bitmap icon = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003640 boolean customIcon = false;
3641 ShortcutIconResource iconResource = null;
3642
Sunny Goyal2fce90c2014-10-07 12:01:58 -07003643 if (bitmap instanceof Bitmap) {
3644 icon = Utilities.createIconBitmap((Bitmap) bitmap, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003645 customIcon = true;
3646 } else {
3647 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07003648 if (extra instanceof ShortcutIconResource) {
3649 iconResource = (ShortcutIconResource) extra;
3650 icon = Utilities.createIconBitmap(iconResource.packageName,
3651 iconResource.resourceName, mIconCache, context);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003652 }
3653 }
3654
Michael Jurkac9d95c52011-08-29 14:03:34 -07003655 final ShortcutInfo info = new ShortcutInfo();
Joe Onorato56d82912010-03-07 14:32:10 -05003656
Kenny Guyed131872014-04-30 03:02:21 +01003657 // Only support intents for current user for now. Intents sent from other
3658 // users wouldn't get here without intent forwarding anyway.
3659 info.user = UserHandleCompat.myUserHandle();
Joe Onorato56d82912010-03-07 14:32:10 -05003660 if (icon == null) {
Sunny Goyal2350bc92014-10-14 16:42:54 -07003661 icon = mIconCache.getDefaultIcon(info.user);
3662 info.usingFallbackIcon = true;
Joe Onorato56d82912010-03-07 14:32:10 -05003663 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08003664 info.setIcon(icon);
Joe Onorato56d82912010-03-07 14:32:10 -05003665
Joe Onorato0589f0f2010-02-08 13:44:00 -08003666 info.title = name;
Kenny Guyc2bd8102014-06-30 12:30:31 +01003667 info.contentDescription = mUserManager.getBadgedLabelForUser(
3668 info.title.toString(), info.user);
Joe Onorato0589f0f2010-02-08 13:44:00 -08003669 info.intent = intent;
3670 info.customIcon = customIcon;
3671 info.iconResource = iconResource;
3672
3673 return info;
3674 }
3675
Winson Chungaac01e12011-08-17 10:37:13 -07003676 boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c,
3677 int iconIndex) {
Joe Onorato17a89222011-02-08 17:26:11 -08003678 // If apps can't be on SD, don't even bother.
Winson Chungee055712013-07-30 14:46:24 -07003679 if (!mAppsCanBeOnRemoveableStorage) {
Winson Chungaac01e12011-08-17 10:37:13 -07003680 return false;
Joe Onorato17a89222011-02-08 17:26:11 -08003681 }
Joe Onorato56d82912010-03-07 14:32:10 -05003682 // If this icon doesn't have a custom icon, check to see
3683 // what's stored in the DB, and if it doesn't match what
3684 // we're going to show, store what we are going to show back
3685 // into the DB. We do this so when we're loading, if the
3686 // package manager can't find an icon (for example because
3687 // the app is on SD) then we can use that instead.
Joe Onoratoddc9c1f2010-08-30 18:30:15 -07003688 if (!info.customIcon && !info.usingFallbackIcon) {
Winson Chungaac01e12011-08-17 10:37:13 -07003689 cache.put(info, c.getBlob(iconIndex));
3690 return true;
3691 }
3692 return false;
3693 }
3694 void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
3695 boolean needSave = false;
3696 try {
3697 if (data != null) {
3698 Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
3699 Bitmap loaded = info.getIcon(mIconCache);
3700 needSave = !saved.sameAs(loaded);
3701 } else {
Joe Onorato56d82912010-03-07 14:32:10 -05003702 needSave = true;
3703 }
Winson Chungaac01e12011-08-17 10:37:13 -07003704 } catch (Exception e) {
3705 needSave = true;
3706 }
3707 if (needSave) {
3708 Log.d(TAG, "going to save icon bitmap for info=" + info);
3709 // This is slower than is ideal, but this only happens once
3710 // or when the app is updated with a new icon.
3711 updateItemInDatabase(context, info);
Joe Onorato56d82912010-03-07 14:32:10 -05003712 }
3713 }
3714
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 /**
Adam Cohendf2cc412011-04-27 16:56:57 -07003716 * Return an existing FolderInfo object if we have encountered this ID previously,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003717 * or make a new one.
3718 */
Adam Cohendf2cc412011-04-27 16:56:57 -07003719 private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 // See if a placeholder was created for us already
3721 FolderInfo folderInfo = folders.get(id);
Adam Cohendf2cc412011-04-27 16:56:57 -07003722 if (folderInfo == null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 // No placeholder -- create a new instance
Michael Jurkac9d95c52011-08-29 14:03:34 -07003724 folderInfo = new FolderInfo();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 folders.put(id, folderInfo);
3726 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003727 return folderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 }
3729
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003730 public static final Comparator<AppInfo> getAppNameComparator() {
Winson Chung11904872012-09-17 16:58:46 -07003731 final Collator collator = Collator.getInstance();
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003732 return new Comparator<AppInfo>() {
3733 public final int compare(AppInfo a, AppInfo b) {
Kenny Guyed131872014-04-30 03:02:21 +01003734 if (a.user.equals(b.user)) {
3735 int result = collator.compare(a.title.toString().trim(),
3736 b.title.toString().trim());
3737 if (result == 0) {
3738 result = a.componentName.compareTo(b.componentName);
3739 }
3740 return result;
3741 } else {
3742 // TODO Need to figure out rules for sorting
3743 // profiles, this puts work second.
3744 return a.user.toString().compareTo(b.user.toString());
Winson Chung11904872012-09-17 16:58:46 -07003745 }
Michael Jurka5b1808d2011-07-11 19:59:46 -07003746 }
Winson Chung11904872012-09-17 16:58:46 -07003747 };
3748 }
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003749 public static final Comparator<AppInfo> APP_INSTALL_TIME_COMPARATOR
3750 = new Comparator<AppInfo>() {
3751 public final int compare(AppInfo a, AppInfo b) {
Winson Chung78403fe2011-01-21 15:38:02 -08003752 if (a.firstInstallTime < b.firstInstallTime) return 1;
3753 if (a.firstInstallTime > b.firstInstallTime) return -1;
3754 return 0;
3755 }
3756 };
Winson Chung5308f242011-08-18 12:12:41 -07003757 static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) {
3758 if (info.activityInfo != null) {
3759 return new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
3760 } else {
3761 return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
3762 }
3763 }
Kenny Guyed131872014-04-30 03:02:21 +01003764 public static class ShortcutNameComparator implements Comparator<LauncherActivityInfoCompat> {
Winson Chung11904872012-09-17 16:58:46 -07003765 private Collator mCollator;
Winson Chungc3eecff2011-07-11 17:44:15 -07003766 private HashMap<Object, CharSequence> mLabelCache;
Winson Chung785d2eb2011-04-14 16:08:02 -07003767 ShortcutNameComparator(PackageManager pm) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003768 mLabelCache = new HashMap<Object, CharSequence>();
Winson Chung11904872012-09-17 16:58:46 -07003769 mCollator = Collator.getInstance();
Winson Chungc3eecff2011-07-11 17:44:15 -07003770 }
Kenny Guyed131872014-04-30 03:02:21 +01003771 ShortcutNameComparator(HashMap<Object, CharSequence> labelCache) {
Winson Chungc3eecff2011-07-11 17:44:15 -07003772 mLabelCache = labelCache;
Winson Chung11904872012-09-17 16:58:46 -07003773 mCollator = Collator.getInstance();
Winson Chung785d2eb2011-04-14 16:08:02 -07003774 }
Kenny Guyed131872014-04-30 03:02:21 +01003775 public final int compare(LauncherActivityInfoCompat a, LauncherActivityInfoCompat b) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003776 String labelA, labelB;
Kenny Guyed131872014-04-30 03:02:21 +01003777 ComponentName keyA = a.getComponentName();
3778 ComponentName keyB = b.getComponentName();
Winson Chung5308f242011-08-18 12:12:41 -07003779 if (mLabelCache.containsKey(keyA)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003780 labelA = mLabelCache.get(keyA).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003781 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003782 labelA = a.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003783
Winson Chung5308f242011-08-18 12:12:41 -07003784 mLabelCache.put(keyA, labelA);
Winson Chungc3eecff2011-07-11 17:44:15 -07003785 }
Winson Chung5308f242011-08-18 12:12:41 -07003786 if (mLabelCache.containsKey(keyB)) {
Sunny Goyal0c4a6442014-07-22 12:27:04 -07003787 labelB = mLabelCache.get(keyB).toString();
Winson Chungc3eecff2011-07-11 17:44:15 -07003788 } else {
Kenny Guyed131872014-04-30 03:02:21 +01003789 labelB = b.getLabel().toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003790
Winson Chung5308f242011-08-18 12:12:41 -07003791 mLabelCache.put(keyB, labelB);
Winson Chungc3eecff2011-07-11 17:44:15 -07003792 }
Winson Chung11904872012-09-17 16:58:46 -07003793 return mCollator.compare(labelA, labelB);
Winson Chung785d2eb2011-04-14 16:08:02 -07003794 }
3795 };
Winson Chung1ed747a2011-05-03 16:18:34 -07003796 public static class WidgetAndShortcutNameComparator implements Comparator<Object> {
Sunny Goyalffe83f12014-08-14 17:39:34 -07003797 private final AppWidgetManagerCompat mManager;
3798 private final PackageManager mPackageManager;
3799 private final HashMap<Object, String> mLabelCache;
3800 private final Collator mCollator;
3801
3802 WidgetAndShortcutNameComparator(Context context) {
3803 mManager = AppWidgetManagerCompat.getInstance(context);
3804 mPackageManager = context.getPackageManager();
Winson Chung1ed747a2011-05-03 16:18:34 -07003805 mLabelCache = new HashMap<Object, String>();
Winson Chung11904872012-09-17 16:58:46 -07003806 mCollator = Collator.getInstance();
Winson Chung1ed747a2011-05-03 16:18:34 -07003807 }
3808 public final int compare(Object a, Object b) {
3809 String labelA, labelB;
Winson Chungc3eecff2011-07-11 17:44:15 -07003810 if (mLabelCache.containsKey(a)) {
3811 labelA = mLabelCache.get(a);
3812 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003813 labelA = (a instanceof LauncherAppWidgetProviderInfo)
3814 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) a)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003815 : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003816 mLabelCache.put(a, labelA);
3817 }
3818 if (mLabelCache.containsKey(b)) {
3819 labelB = mLabelCache.get(b);
3820 } else {
Adam Cohen59400422014-03-05 18:07:04 -08003821 labelB = (b instanceof LauncherAppWidgetProviderInfo)
Adam Cohenb76c1652015-01-30 10:28:23 -08003822 ? mManager.loadLabel((LauncherAppWidgetProviderInfo) b)
Sunny Goyalffe83f12014-08-14 17:39:34 -07003823 : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim();
Winson Chungc3eecff2011-07-11 17:44:15 -07003824 mLabelCache.put(b, labelB);
3825 }
Winson Chung11904872012-09-17 16:58:46 -07003826 return mCollator.compare(labelA, labelB);
Winson Chung1ed747a2011-05-03 16:18:34 -07003827 }
3828 };
Joe Onoratobe386092009-11-17 17:32:16 -08003829
Sunny Goyal651077b2014-06-30 14:15:31 -07003830 static boolean isValidProvider(AppWidgetProviderInfo provider) {
3831 return (provider != null) && (provider.provider != null)
3832 && (provider.provider.getPackageName() != null);
3833 }
3834
Joe Onoratobe386092009-11-17 17:32:16 -08003835 public void dumpState() {
Joe Onoratobe386092009-11-17 17:32:16 -08003836 Log.d(TAG, "mCallbacks=" + mCallbacks);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003837 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data);
3838 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added);
3839 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed);
3840 AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified);
Joe Onorato36115782010-06-17 13:28:48 -04003841 if (mLoaderTask != null) {
3842 mLoaderTask.dumpState();
3843 } else {
3844 Log.d(TAG, "mLoaderTask=null");
3845 }
Joe Onoratobe386092009-11-17 17:32:16 -08003846 }
Sunny Goyale0f58d72014-11-10 18:05:31 -08003847
3848 public Callbacks getCallback() {
3849 return mCallbacks != null ? mCallbacks.get() : null;
3850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851}