The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 19 | import android.app.SearchManager; |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 20 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ComponentName; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 23 | import android.content.ContentProviderOperation; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 28 | import android.content.Intent.ShortcutIconResource; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 29 | import android.content.IntentFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
Jason Monk | bbe1e24 | 2014-05-16 17:37:34 -0400 | [diff] [blame] | 31 | import android.content.pm.ProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.database.Cursor; |
| 34 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 36 | import android.os.Environment; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.HandlerThread; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 39 | import android.os.Looper; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 40 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 42 | import android.os.SystemClock; |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 43 | import android.provider.BaseColumns; |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 44 | import android.text.TextUtils; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 46 | import android.util.LongSparseArray; |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 47 | import android.util.Pair; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 48 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 49 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 50 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 51 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 52 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 53 | import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 54 | import com.android.launcher3.compat.UserHandleCompat; |
| 55 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 56 | import com.android.launcher3.config.ProviderConfig; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 57 | import com.android.launcher3.folder.Folder; |
| 58 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 59 | import com.android.launcher3.model.GridSizeMigrationTask; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 60 | import com.android.launcher3.model.WidgetsModel; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 61 | import com.android.launcher3.util.ComponentKey; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 62 | import com.android.launcher3.util.CursorIconInfo; |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 63 | import com.android.launcher3.util.FlagOp; |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 64 | import com.android.launcher3.util.LongArrayMap; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 65 | import com.android.launcher3.util.ManagedProfileHeuristic; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 66 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 67 | import com.android.launcher3.util.ViewOnDrawExecutor; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 68 | |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 69 | import java.lang.ref.WeakReference; |
| 70 | import java.net.URISyntaxException; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 71 | import java.security.InvalidParameterException; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 72 | import java.util.ArrayList; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 73 | import java.util.Arrays; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 74 | import java.util.Collections; |
| 75 | import java.util.Comparator; |
| 76 | import java.util.HashMap; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 77 | import java.util.HashSet; |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 78 | import java.util.Iterator; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 79 | import java.util.List; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 80 | import java.util.Map.Entry; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 81 | import java.util.Set; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 82 | import java.util.concurrent.Executor; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 83 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 86 | * LauncherModel object held in a static. Also provide APIs for updating the database state |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 87 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 89 | public class LauncherModel extends BroadcastReceiver |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 90 | implements LauncherAppsCompat.OnAppsChangedCallbackCompat { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 91 | static final boolean DEBUG_LOADERS = false; |
Chris Wren | ee52336 | 2014-09-09 10:09:02 -0400 | [diff] [blame] | 92 | private static final boolean DEBUG_RECEIVER = false; |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 93 | private static final boolean REMOVE_UNRESTORED_ICONS = true; |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 94 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 95 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 96 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 97 | public static final int LOADER_FLAG_NONE = 0; |
| 98 | public static final int LOADER_FLAG_CLEAR_WORKSPACE = 1 << 0; |
| 99 | public static final int LOADER_FLAG_MIGRATE_SHORTCUTS = 1 << 1; |
| 100 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 101 | private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 102 | private static final long INVALID_SCREEN_ID = -1L; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 103 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 104 | @Thunk final boolean mAppsCanBeOnRemoveableStorage; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 105 | private final boolean mOldContentProviderExists; |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 106 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 107 | @Thunk final LauncherAppState mApp; |
| 108 | @Thunk final Object mLock = new Object(); |
| 109 | @Thunk DeferredHandler mHandler = new DeferredHandler(); |
| 110 | @Thunk LoaderTask mLoaderTask; |
| 111 | @Thunk boolean mIsLoaderTaskRunning; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 112 | @Thunk boolean mHasLoaderCompletedOnce; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 113 | |
Jason Monk | bbe1e24 | 2014-05-16 17:37:34 -0400 | [diff] [blame] | 114 | private static final String MIGRATE_AUTHORITY = "com.android.launcher2.settings"; |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 115 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 116 | @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 117 | static { |
| 118 | sWorkerThread.start(); |
| 119 | } |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 120 | @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 121 | |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 122 | // We start off with everything not loaded. After that, we assume that |
| 123 | // our monitoring of the package manager provides all updates and we never |
| 124 | // need to do a requery. These are only ever touched from the loader thread. |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 125 | @Thunk boolean mWorkspaceLoaded; |
| 126 | @Thunk boolean mAllAppsLoaded; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 127 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Set of runnables to be called on the background thread after the workspace binding |
| 130 | * is complete. |
| 131 | */ |
| 132 | static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>(); |
| 133 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 134 | @Thunk WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 135 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 136 | // < only access in worker thread > |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 137 | private final AllAppsList mBgAllAppsList; |
Hyunyoung Song | 9110d48 | 2015-05-22 14:49:23 -0700 | [diff] [blame] | 138 | // Entire list of widgets. |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 139 | private final WidgetsModel mBgWidgetsModel; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 140 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 141 | // The lock that must be acquired before referencing any static bg data structures. Unlike |
| 142 | // other locks, this one can generally be held long-term because we never expect any of these |
| 143 | // static data structures to be referenced outside of the worker thread except on the first |
| 144 | // load after configuration change. |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 145 | static final Object sBgLock = new Object(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 146 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 147 | // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 148 | // LauncherModel to their ids |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 149 | static final LongArrayMap<ItemInfo> sBgItemsIdMap = new LongArrayMap<>(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 150 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 151 | // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts |
| 152 | // created by LauncherModel that are directly on the home screen (however, no widgets or |
| 153 | // shortcuts within folders). |
| 154 | static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 155 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 156 | // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget() |
| 157 | static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets = |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 158 | new ArrayList<LauncherAppWidgetInfo>(); |
| 159 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 160 | // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders() |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 161 | static final LongArrayMap<FolderInfo> sBgFolders = new LongArrayMap<>(); |
Winson Chung | b1094bd | 2011-08-24 16:14:08 -0700 | [diff] [blame] | 162 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 163 | // sBgWorkspaceScreens is the ordered set of workspace screens. |
| 164 | static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>(); |
| 165 | |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 166 | // sPendingPackages is a set of packages which could be on sdcard and are not available yet |
Sameer Padala | 513edae | 2014-07-29 16:17:08 -0700 | [diff] [blame] | 167 | static final HashMap<UserHandleCompat, HashSet<String>> sPendingPackages = |
| 168 | new HashMap<UserHandleCompat, HashSet<String>>(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 169 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 170 | // </ only access in worker thread > |
| 171 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 172 | @Thunk IconCache mIconCache; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 174 | @Thunk final LauncherAppsCompat mLauncherApps; |
| 175 | @Thunk final UserManagerCompat mUserManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 176 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 177 | public interface Callbacks { |
Joe Onorato | ef2efcf | 2010-10-27 13:21:00 -0700 | [diff] [blame] | 178 | public boolean setLoadOnResume(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 179 | public int getCurrentWorkspaceScreen(); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 180 | public void clearPendingBinds(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 181 | public void startBinding(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 182 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end, |
| 183 | boolean forceAnimateIcons); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 184 | public void bindScreens(ArrayList<Long> orderedScreenIds); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 185 | public void bindAddScreens(ArrayList<Long> orderedScreenIds); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 186 | public void bindFolders(LongArrayMap<FolderInfo> folders); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 187 | public void finishBindingItems(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 188 | public void bindAppWidget(LauncherAppWidgetInfo info); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 189 | public void bindAllApplications(ArrayList<AppInfo> apps); |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 190 | public void bindAppsAdded(ArrayList<Long> newScreens, |
| 191 | ArrayList<ItemInfo> addNotAnimated, |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 192 | ArrayList<ItemInfo> addAnimated, |
| 193 | ArrayList<AppInfo> addedApps); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 194 | public void bindAppsUpdated(ArrayList<AppInfo> apps); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 195 | public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, |
| 196 | ArrayList<ShortcutInfo> removed, UserHandleCompat user); |
| 197 | public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 198 | public void bindRestoreItemsChange(HashSet<ItemInfo> updates); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 199 | public void bindWorkspaceComponentsRemoved( |
| 200 | HashSet<String> packageNames, HashSet<ComponentName> components, |
| 201 | UserHandleCompat user); |
| 202 | public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 203 | public void notifyWidgetProvidersChanged(); |
| 204 | public void bindWidgetsModel(WidgetsModel model); |
Winson Chung | 88fa741 | 2015-08-03 14:25:28 -0700 | [diff] [blame] | 205 | public void bindSearchProviderChanged(); |
Winson Chung | a0b7e86 | 2013-09-05 16:03:15 -0700 | [diff] [blame] | 206 | public boolean isAllAppsButtonRank(int rank); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 207 | public void onPageBoundSynchronously(int page); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 208 | public void executeOnNextDraw(ViewOnDrawExecutor executor); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 209 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 210 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 211 | public interface ItemInfoFilter { |
| 212 | public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn); |
| 213 | } |
| 214 | |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 215 | LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 216 | Context context = app.getContext(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 217 | |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 218 | mAppsCanBeOnRemoveableStorage = Environment.isExternalStorageRemovable(); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 219 | String oldProvider = context.getString(R.string.old_launcher_provider_uri); |
Jason Monk | bbe1e24 | 2014-05-16 17:37:34 -0400 | [diff] [blame] | 220 | // This may be the same as MIGRATE_AUTHORITY, or it may be replaced by a different |
| 221 | // resource string. |
| 222 | String redirectAuthority = Uri.parse(oldProvider).getAuthority(); |
| 223 | ProviderInfo providerInfo = |
| 224 | context.getPackageManager().resolveContentProvider(MIGRATE_AUTHORITY, 0); |
| 225 | ProviderInfo redirectProvider = |
| 226 | context.getPackageManager().resolveContentProvider(redirectAuthority, 0); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 227 | |
| 228 | Log.d(TAG, "Old launcher provider: " + oldProvider); |
Jason Monk | bbe1e24 | 2014-05-16 17:37:34 -0400 | [diff] [blame] | 229 | mOldContentProviderExists = (providerInfo != null) && (redirectProvider != null); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 230 | |
| 231 | if (mOldContentProviderExists) { |
| 232 | Log.d(TAG, "Old launcher provider exists."); |
| 233 | } else { |
| 234 | Log.d(TAG, "Old launcher provider does not exist."); |
| 235 | } |
| 236 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 237 | mApp = app; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 238 | mBgAllAppsList = new AllAppsList(iconCache, appFilter); |
Hyunyoung Song | eaf291b | 2015-06-17 21:12:44 -0700 | [diff] [blame] | 239 | mBgWidgetsModel = new WidgetsModel(context, iconCache, appFilter); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 240 | mIconCache = iconCache; |
| 241 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 242 | mLauncherApps = LauncherAppsCompat.getInstance(context); |
| 243 | mUserManager = UserManagerCompat.getInstance(context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 246 | /** Runs the specified runnable immediately if called from the main thread, otherwise it is |
| 247 | * posted on the main thread handler. */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 248 | @Thunk void runOnMainThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 249 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 250 | // If we are on the worker thread, post onto the main handler |
| 251 | mHandler.post(r); |
| 252 | } else { |
| 253 | r.run(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** Runs the specified runnable immediately if called from the worker thread, otherwise it is |
| 258 | * posted on the worker thread handler. */ |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 259 | @Thunk static void runOnWorkerThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 260 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 261 | r.run(); |
| 262 | } else { |
| 263 | // If we are not on the worker thread, then post to the worker handler |
| 264 | sWorker.post(r); |
| 265 | } |
| 266 | } |
| 267 | |
Winson Chung | e43a1e7 | 2014-01-15 10:33:02 -0800 | [diff] [blame] | 268 | boolean canMigrateFromOldLauncherDb(Launcher launcher) { |
| 269 | return mOldContentProviderExists && !launcher.isLauncherPreinstalled() ; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 272 | public void setPackageState(final PackageInstallInfo installInfo) { |
| 273 | Runnable updateRunnable = new Runnable() { |
| 274 | |
| 275 | @Override |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 276 | public void run() { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 277 | synchronized (sBgLock) { |
| 278 | final HashSet<ItemInfo> updates = new HashSet<>(); |
| 279 | |
| 280 | if (installInfo.state == PackageInstallerCompat.STATUS_INSTALLED) { |
| 281 | // Ignore install success events as they are handled by Package add events. |
| 282 | return; |
| 283 | } |
| 284 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 285 | for (ItemInfo info : sBgItemsIdMap) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 286 | if (info instanceof ShortcutInfo) { |
| 287 | ShortcutInfo si = (ShortcutInfo) info; |
| 288 | ComponentName cn = si.getTargetComponent(); |
| 289 | if (si.isPromise() && (cn != null) |
| 290 | && installInfo.packageName.equals(cn.getPackageName())) { |
| 291 | si.setInstallProgress(installInfo.progress); |
| 292 | |
| 293 | if (installInfo.state == PackageInstallerCompat.STATUS_FAILED) { |
| 294 | // Mark this info as broken. |
| 295 | si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
| 296 | } |
| 297 | updates.add(si); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | for (LauncherAppWidgetInfo widget : sBgAppWidgets) { |
| 303 | if (widget.providerName.getPackageName().equals(installInfo.packageName)) { |
| 304 | widget.installProgress = installInfo.progress; |
| 305 | updates.add(widget); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (!updates.isEmpty()) { |
| 310 | // Push changes to the callback. |
| 311 | Runnable r = new Runnable() { |
| 312 | public void run() { |
| 313 | Callbacks callbacks = getCallback(); |
| 314 | if (callbacks != null) { |
| 315 | callbacks.bindRestoreItemsChange(updates); |
| 316 | } |
| 317 | } |
| 318 | }; |
| 319 | mHandler.post(r); |
| 320 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | }; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 324 | runOnWorkerThread(updateRunnable); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 327 | /** |
| 328 | * Updates the icons and label of all pending icons for the provided package name. |
| 329 | */ |
| 330 | public void updateSessionDisplayInfo(final String packageName) { |
| 331 | Runnable updateRunnable = new Runnable() { |
| 332 | |
| 333 | @Override |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 334 | public void run() { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 335 | synchronized (sBgLock) { |
| 336 | final ArrayList<ShortcutInfo> updates = new ArrayList<>(); |
| 337 | final UserHandleCompat user = UserHandleCompat.myUserHandle(); |
| 338 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 339 | for (ItemInfo info : sBgItemsIdMap) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 340 | if (info instanceof ShortcutInfo) { |
| 341 | ShortcutInfo si = (ShortcutInfo) info; |
| 342 | ComponentName cn = si.getTargetComponent(); |
| 343 | if (si.isPromise() && (cn != null) |
| 344 | && packageName.equals(cn.getPackageName())) { |
| 345 | if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 346 | // For auto install apps update the icon as well as label. |
| 347 | mIconCache.getTitleAndIcon(si, |
| 348 | si.promisedIntent, user, |
| 349 | si.shouldUseLowResIcon()); |
| 350 | } else { |
| 351 | // Only update the icon for restored apps. |
| 352 | si.updateIcon(mIconCache); |
| 353 | } |
| 354 | updates.add(si); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (!updates.isEmpty()) { |
| 360 | // Push changes to the callback. |
| 361 | Runnable r = new Runnable() { |
| 362 | public void run() { |
| 363 | Callbacks callbacks = getCallback(); |
| 364 | if (callbacks != null) { |
| 365 | callbacks.bindShortcutsChanged(updates, |
| 366 | new ArrayList<ShortcutInfo>(), user); |
| 367 | } |
| 368 | } |
| 369 | }; |
| 370 | mHandler.post(r); |
| 371 | } |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | }; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 375 | runOnWorkerThread(updateRunnable); |
Sunny Goyal | a22666f | 2014-09-18 13:25:15 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 378 | public void addAppsToAllApps(final Context ctx, final ArrayList<AppInfo> allAppsApps) { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 379 | final Callbacks callbacks = getCallback(); |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 380 | |
| 381 | if (allAppsApps == null) { |
| 382 | throw new RuntimeException("allAppsApps must not be null"); |
| 383 | } |
| 384 | if (allAppsApps.isEmpty()) { |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | // Process the newly added applications and add them to the database first |
| 389 | Runnable r = new Runnable() { |
| 390 | public void run() { |
| 391 | runOnMainThread(new Runnable() { |
| 392 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 393 | Callbacks cb = getCallback(); |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 394 | if (callbacks == cb && cb != null) { |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 395 | callbacks.bindAppsAdded(null, null, null, allAppsApps); |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | }); |
| 399 | } |
| 400 | }; |
| 401 | runOnWorkerThread(r); |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 402 | } |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 403 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 404 | private static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> occupiedPos, |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 405 | int[] xy, int spanX, int spanY) { |
| 406 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 407 | InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
| 408 | final int xCount = (int) profile.numColumns; |
| 409 | final int yCount = (int) profile.numRows; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 410 | boolean[][] occupied = new boolean[xCount][yCount]; |
| 411 | if (occupiedPos != null) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 412 | for (ItemInfo r : occupiedPos) { |
| 413 | int right = r.cellX + r.spanX; |
| 414 | int bottom = r.cellY + r.spanY; |
| 415 | for (int x = r.cellX; 0 <= x && x < right && x < xCount; x++) { |
| 416 | for (int y = r.cellY; 0 <= y && y < bottom && y < yCount; y++) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 417 | occupied[x][y] = true; |
| 418 | } |
| 419 | } |
| 420 | } |
Winson Chung | fe9d96a | 2013-11-14 11:30:05 -0800 | [diff] [blame] | 421 | } |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 422 | return Utilities.findVacantCell(xy, spanX, spanY, xCount, yCount, occupied); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Find a position on the screen for the given size or adds a new screen. |
| 427 | * @return screenId and the coordinates for the item. |
| 428 | */ |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 429 | @Thunk Pair<Long, int[]> findSpaceForItem( |
Sunny Goyal | c3642d4 | 2015-10-30 10:27:08 -0700 | [diff] [blame] | 430 | Context context, |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 431 | ArrayList<Long> workspaceScreens, |
| 432 | ArrayList<Long> addedWorkspaceScreensFinal, |
| 433 | int spanX, int spanY) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 434 | LongSparseArray<ArrayList<ItemInfo>> screenItems = new LongSparseArray<>(); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 435 | |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 436 | // Use sBgItemsIdMap as all the items are already loaded. |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 437 | assertWorkspaceLoaded(); |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 438 | synchronized (sBgLock) { |
| 439 | for (ItemInfo info : sBgItemsIdMap) { |
| 440 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 441 | ArrayList<ItemInfo> items = screenItems.get(info.screenId); |
| 442 | if (items == null) { |
| 443 | items = new ArrayList<>(); |
| 444 | screenItems.put(info.screenId, items); |
| 445 | } |
| 446 | items.add(info); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 447 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 448 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Find appropriate space for the item. |
| 452 | long screenId = 0; |
| 453 | int[] cordinates = new int[2]; |
| 454 | boolean found = false; |
| 455 | |
| 456 | int screenCount = workspaceScreens.size(); |
| 457 | // First check the preferred screen. |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 458 | int preferredScreenIndex = workspaceScreens.isEmpty() ? 0 : 1; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 459 | if (preferredScreenIndex < screenCount) { |
| 460 | screenId = workspaceScreens.get(preferredScreenIndex); |
| 461 | found = findNextAvailableIconSpaceInScreen( |
| 462 | screenItems.get(screenId), cordinates, spanX, spanY); |
| 463 | } |
| 464 | |
| 465 | if (!found) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 466 | // Search on any of the screens starting from the first screen. |
| 467 | for (int screen = 1; screen < screenCount; screen++) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 468 | screenId = workspaceScreens.get(screen); |
| 469 | if (findNextAvailableIconSpaceInScreen( |
| 470 | screenItems.get(screenId), cordinates, spanX, spanY)) { |
| 471 | // We found a space for it |
| 472 | found = true; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (!found) { |
| 479 | // Still no position found. Add a new screen to the end. |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 480 | screenId = LauncherSettings.Settings.call(context.getContentResolver(), |
| 481 | LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) |
| 482 | .getLong(LauncherSettings.Settings.EXTRA_VALUE); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 483 | |
| 484 | // Save the screen id for binding in the workspace |
| 485 | workspaceScreens.add(screenId); |
| 486 | addedWorkspaceScreensFinal.add(screenId); |
| 487 | |
| 488 | // If we still can't find an empty space, then God help us all!!! |
| 489 | if (!findNextAvailableIconSpaceInScreen( |
| 490 | screenItems.get(screenId), cordinates, spanX, spanY)) { |
| 491 | throw new RuntimeException("Can't find space to add the item"); |
| 492 | } |
| 493 | } |
| 494 | return Pair.create(screenId, cordinates); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Adds the provided items to the workspace. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 499 | */ |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 500 | public void addAndBindAddedWorkspaceItems(final Context context, |
Sunny Goyal | a214a63 | 2015-05-06 12:23:34 -0700 | [diff] [blame] | 501 | final ArrayList<? extends ItemInfo> workspaceApps) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 502 | final Callbacks callbacks = getCallback(); |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 503 | if (workspaceApps.isEmpty()) { |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 504 | return; |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 505 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 506 | // Process the newly added applications and add them to the database first |
| 507 | Runnable r = new Runnable() { |
| 508 | public void run() { |
| 509 | final ArrayList<ItemInfo> addedShortcutsFinal = new ArrayList<ItemInfo>(); |
| 510 | final ArrayList<Long> addedWorkspaceScreensFinal = new ArrayList<Long>(); |
| 511 | |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 512 | // Get the list of workspace screens. We need to append to this list and |
| 513 | // can not use sBgWorkspaceScreens because loadWorkspace() may not have been |
| 514 | // called. |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 515 | ArrayList<Long> workspaceScreens = loadWorkspaceScreensDb(context); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 516 | synchronized(sBgLock) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 517 | for (ItemInfo item : workspaceApps) { |
Sunny Goyal | a911672 | 2015-04-29 13:55:58 -0700 | [diff] [blame] | 518 | if (item instanceof ShortcutInfo) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 519 | // Short-circuit this logic if the icon exists somewhere on the workspace |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 520 | if (shortcutExists(context, item.getIntent(), item.user)) { |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 521 | continue; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 522 | } |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 523 | } |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 524 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 525 | // Find appropriate space for the item. |
Sunny Goyal | c3642d4 | 2015-10-30 10:27:08 -0700 | [diff] [blame] | 526 | Pair<Long, int[]> coords = findSpaceForItem(context, |
Sunny Goyal | 9eba1fd | 2015-10-16 08:58:57 -0700 | [diff] [blame] | 527 | workspaceScreens, addedWorkspaceScreensFinal, 1, 1); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 528 | long screenId = coords.first; |
| 529 | int[] cordinates = coords.second; |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 530 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 531 | ItemInfo itemInfo; |
| 532 | if (item instanceof ShortcutInfo || item instanceof FolderInfo) { |
| 533 | itemInfo = item; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 534 | } else if (item instanceof AppInfo) { |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 535 | itemInfo = ((AppInfo) item).makeShortcut(); |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 536 | } else { |
| 537 | throw new RuntimeException("Unexpected info type"); |
| 538 | } |
Winson Chung | 94d6768 | 2013-09-25 16:29:40 -0700 | [diff] [blame] | 539 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 540 | // Add the shortcut to the db |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 541 | addItemToDatabase(context, itemInfo, |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 542 | LauncherSettings.Favorites.CONTAINER_DESKTOP, |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 543 | screenId, cordinates[0], cordinates[1]); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 544 | // Save the ShortcutInfo for binding in the workspace |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 545 | addedShortcutsFinal.add(itemInfo); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 549 | // Update the workspace screens |
| 550 | updateWorkspaceScreenOrder(context, workspaceScreens); |
| 551 | |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 552 | if (!addedShortcutsFinal.isEmpty()) { |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 553 | runOnMainThread(new Runnable() { |
| 554 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 555 | Callbacks cb = getCallback(); |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 556 | if (callbacks == cb && cb != null) { |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 557 | final ArrayList<ItemInfo> addAnimated = new ArrayList<ItemInfo>(); |
| 558 | final ArrayList<ItemInfo> addNotAnimated = new ArrayList<ItemInfo>(); |
Winson Chung | 94d6768 | 2013-09-25 16:29:40 -0700 | [diff] [blame] | 559 | if (!addedShortcutsFinal.isEmpty()) { |
| 560 | ItemInfo info = addedShortcutsFinal.get(addedShortcutsFinal.size() - 1); |
| 561 | long lastScreenId = info.screenId; |
| 562 | for (ItemInfo i : addedShortcutsFinal) { |
| 563 | if (i.screenId == lastScreenId) { |
| 564 | addAnimated.add(i); |
| 565 | } else { |
| 566 | addNotAnimated.add(i); |
| 567 | } |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 570 | callbacks.bindAppsAdded(addedWorkspaceScreensFinal, |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 571 | addNotAnimated, addAnimated, null); |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 572 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 573 | } |
Winson Chung | 997a923 | 2013-07-24 15:33:46 -0700 | [diff] [blame] | 574 | }); |
| 575 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 576 | } |
| 577 | }; |
| 578 | runOnWorkerThread(r); |
| 579 | } |
| 580 | |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 581 | private void unbindItemInfosAndClearQueuedBindRunnables() { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 582 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 583 | throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " + |
| 584 | "main thread"); |
| 585 | } |
| 586 | |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 587 | // Remove any queued UI runnables |
| 588 | mHandler.cancelAll(); |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 589 | // Unbind all the workspace items |
| 590 | unbindWorkspaceItemsOnMainThread(); |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 593 | /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */ |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 594 | void unbindWorkspaceItemsOnMainThread() { |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 595 | // Ensure that we don't use the same workspace items data structure on the main thread |
| 596 | // by making a copy of workspace items first. |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 597 | final ArrayList<ItemInfo> tmpItems = new ArrayList<ItemInfo>(); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 598 | synchronized (sBgLock) { |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 599 | tmpItems.addAll(sBgWorkspaceItems); |
| 600 | tmpItems.addAll(sBgAppWidgets); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 601 | } |
| 602 | Runnable r = new Runnable() { |
| 603 | @Override |
| 604 | public void run() { |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 605 | for (ItemInfo item : tmpItems) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 606 | item.unbind(); |
| 607 | } |
| 608 | } |
| 609 | }; |
| 610 | runOnMainThread(r); |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 613 | /** |
| 614 | * Adds an item to the DB if it was not created previously, or move it to a new |
| 615 | * <container, screen, cellX, cellY> |
| 616 | */ |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 617 | public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 618 | long screenId, int cellX, int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 619 | if (item.container == ItemInfo.NO_ID) { |
| 620 | // From all apps |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 621 | addItemToDatabase(context, item, container, screenId, cellX, cellY); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 622 | } else { |
| 623 | // From somewhere else |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 624 | moveItemInDatabase(context, item, container, screenId, cellX, cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 628 | static void checkItemInfoLocked( |
| 629 | final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { |
| 630 | ItemInfo modelItem = sBgItemsIdMap.get(itemId); |
| 631 | if (modelItem != null && item != modelItem) { |
| 632 | // check all the data is consistent |
| 633 | if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { |
| 634 | ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; |
| 635 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 636 | if (modelShortcut.title.toString().equals(shortcut.title.toString()) && |
| 637 | modelShortcut.intent.filterEquals(shortcut.intent) && |
| 638 | modelShortcut.id == shortcut.id && |
| 639 | modelShortcut.itemType == shortcut.itemType && |
| 640 | modelShortcut.container == shortcut.container && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 641 | modelShortcut.screenId == shortcut.screenId && |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 642 | modelShortcut.cellX == shortcut.cellX && |
| 643 | modelShortcut.cellY == shortcut.cellY && |
| 644 | modelShortcut.spanX == shortcut.spanX && |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 645 | modelShortcut.spanY == shortcut.spanY) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 646 | // For all intents and purposes, this is the same object |
| 647 | return; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // the modelItem needs to match up perfectly with item if our model is |
| 652 | // to be consistent with the database-- for now, just require |
| 653 | // modelItem == item or the equality check above |
| 654 | String msg = "item: " + ((item != null) ? item.toString() : "null") + |
| 655 | "modelItem: " + |
| 656 | ((modelItem != null) ? modelItem.toString() : "null") + |
| 657 | "Error: ItemInfo passed to checkItemInfo doesn't match original"; |
| 658 | RuntimeException e = new RuntimeException(msg); |
| 659 | if (stackTrace != null) { |
| 660 | e.setStackTrace(stackTrace); |
| 661 | } |
Adam Cohen | b9ada65 | 2013-11-08 08:25:08 -0800 | [diff] [blame] | 662 | throw e; |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 666 | static void checkItemInfo(final ItemInfo item) { |
| 667 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 668 | final long itemId = item.id; |
| 669 | Runnable r = new Runnable() { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 670 | public void run() { |
| 671 | synchronized (sBgLock) { |
| 672 | checkItemInfoLocked(itemId, item, stackTrace); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 673 | } |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 674 | } |
| 675 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 676 | runOnWorkerThread(r); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 679 | static void updateItemInDatabaseHelper(Context context, final ContentValues values, |
| 680 | final ItemInfo item, final String callingFunction) { |
| 681 | final long itemId = item.id; |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 682 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 683 | final ContentResolver cr = context.getContentResolver(); |
| 684 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 685 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 686 | Runnable r = new Runnable() { |
| 687 | public void run() { |
| 688 | cr.update(uri, values, null, null); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 689 | updateItemArrays(item, itemId, stackTrace); |
| 690 | } |
| 691 | }; |
| 692 | runOnWorkerThread(r); |
| 693 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 694 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 695 | static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList, |
| 696 | final ArrayList<ItemInfo> items, final String callingFunction) { |
| 697 | final ContentResolver cr = context.getContentResolver(); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 698 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 699 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 700 | Runnable r = new Runnable() { |
| 701 | public void run() { |
| 702 | ArrayList<ContentProviderOperation> ops = |
| 703 | new ArrayList<ContentProviderOperation>(); |
| 704 | int count = items.size(); |
| 705 | for (int i = 0; i < count; i++) { |
| 706 | ItemInfo item = items.get(i); |
| 707 | final long itemId = item.id; |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 708 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 709 | ContentValues values = valuesList.get(i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 710 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 711 | ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); |
| 712 | updateItemArrays(item, itemId, stackTrace); |
| 713 | |
| 714 | } |
| 715 | try { |
| 716 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 717 | } catch (Exception e) { |
| 718 | e.printStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 722 | runOnWorkerThread(r); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 723 | } |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 724 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 725 | static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) { |
| 726 | // Lock on mBgLock *after* the db operation |
| 727 | synchronized (sBgLock) { |
| 728 | checkItemInfoLocked(itemId, item, stackTrace); |
| 729 | |
| 730 | if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 731 | item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 732 | // Item is in a folder, make sure this folder exists |
| 733 | if (!sBgFolders.containsKey(item.container)) { |
| 734 | // An items container is being set to a that of an item which is not in |
| 735 | // the list of Folders. |
| 736 | String msg = "item: " + item + " container being set to: " + |
| 737 | item.container + ", not in the list of folders"; |
| 738 | Log.e(TAG, msg); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | // Items are added/removed from the corresponding FolderInfo elsewhere, such |
| 743 | // as in Workspace.onDrop. Here, we just add/remove them from the list of items |
| 744 | // that are on the desktop, as appropriate |
| 745 | ItemInfo modelItem = sBgItemsIdMap.get(itemId); |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 746 | if (modelItem != null && |
| 747 | (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || |
| 748 | modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) { |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 749 | switch (modelItem.itemType) { |
| 750 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 751 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 752 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 753 | if (!sBgWorkspaceItems.contains(modelItem)) { |
| 754 | sBgWorkspaceItems.add(modelItem); |
| 755 | } |
| 756 | break; |
| 757 | default: |
| 758 | break; |
| 759 | } |
| 760 | } else { |
| 761 | sBgWorkspaceItems.remove(modelItem); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 766 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 767 | * Move an item in the DB to a new <container, screen, cellX, cellY> |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 768 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 769 | public static void moveItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 770 | final long screenId, final int cellX, final int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 771 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 772 | item.cellX = cellX; |
| 773 | item.cellY = cellY; |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 774 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 775 | // We store hotseat items in canonical form which is this orientation invariant position |
| 776 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 777 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 778 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 779 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 780 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 781 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 782 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 783 | |
| 784 | final ContentValues values = new ContentValues(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 785 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 786 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 787 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 788 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 789 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 790 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 791 | updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase"); |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /** |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 795 | * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the |
| 796 | * cellX, cellY have already been updated on the ItemInfos. |
| 797 | */ |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 798 | public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items, |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 799 | final long container, final int screen) { |
| 800 | |
| 801 | ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>(); |
| 802 | int count = items.size(); |
| 803 | |
| 804 | for (int i = 0; i < count; i++) { |
| 805 | ItemInfo item = items.get(i); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 806 | item.container = container; |
| 807 | |
| 808 | // We store hotseat items in canonical form which is this orientation invariant position |
| 809 | // in the hotseat |
| 810 | if (context instanceof Launcher && screen < 0 && |
| 811 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 812 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX, |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 813 | item.cellY); |
| 814 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 815 | item.screenId = screen; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | final ContentValues values = new ContentValues(); |
| 819 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
| 820 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 821 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 822 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 823 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 824 | |
| 825 | contentValues.add(values); |
| 826 | } |
| 827 | updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase"); |
| 828 | } |
| 829 | |
| 830 | /** |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 831 | * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY> |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 832 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 833 | static void modifyItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 834 | final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) { |
Winson Chung | 0f84a60 | 2013-09-30 14:30:58 -0700 | [diff] [blame] | 835 | item.container = container; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 836 | item.cellX = cellX; |
| 837 | item.cellY = cellY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 838 | item.spanX = spanX; |
| 839 | item.spanY = spanY; |
| 840 | |
| 841 | // We store hotseat items in canonical form which is this orientation invariant position |
| 842 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 843 | if (context instanceof Launcher && screenId < 0 && |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 844 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 845 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 846 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 847 | item.screenId = screenId; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 848 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 849 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 850 | final ContentValues values = new ContentValues(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 851 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 852 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 853 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 854 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 855 | values.put(LauncherSettings.Favorites.SPANX, item.spanX); |
| 856 | values.put(LauncherSettings.Favorites.SPANY, item.spanY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 857 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 858 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 859 | updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase"); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 860 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 861 | |
| 862 | /** |
| 863 | * Update an item to the database in a specified container. |
| 864 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 865 | public static void updateItemInDatabase(Context context, final ItemInfo item) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 866 | final ContentValues values = new ContentValues(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 867 | item.onAddToDatabase(context, values); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 868 | updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase"); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 871 | private void assertWorkspaceLoaded() { |
Sunny Goyal | 8f531dd | 2015-08-25 17:08:57 -0700 | [diff] [blame] | 872 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 873 | synchronized (mLock) { |
| 874 | if (!mHasLoaderCompletedOnce || |
| 875 | (mLoaderTask != null && mLoaderTask.mIsLoadingAndBindingWorkspace)) { |
| 876 | throw new RuntimeException("Trying to add shortcut while loader is running"); |
| 877 | } |
| 878 | } |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 882 | /** |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 883 | * Returns true if the shortcuts already exists on the workspace. This must be called after |
| 884 | * the workspace has been loaded. We identify a shortcut by its intent. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 885 | */ |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 886 | @Thunk boolean shortcutExists(Context context, Intent intent, UserHandleCompat user) { |
| 887 | assertWorkspaceLoaded(); |
Sunny Goyal | dfde999 | 2015-05-01 12:31:32 -0700 | [diff] [blame] | 888 | final String intentWithPkg, intentWithoutPkg; |
Sunny Goyal | 2a6cf09 | 2014-06-26 15:27:14 -0700 | [diff] [blame] | 889 | if (intent.getComponent() != null) { |
| 890 | // If component is not null, an intent with null package will produce |
| 891 | // the same result and should also be a match. |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 892 | String packageName = intent.getComponent().getPackageName(); |
Sunny Goyal | 2a6cf09 | 2014-06-26 15:27:14 -0700 | [diff] [blame] | 893 | if (intent.getPackage() != null) { |
Sunny Goyal | dfde999 | 2015-05-01 12:31:32 -0700 | [diff] [blame] | 894 | intentWithPkg = intent.toUri(0); |
| 895 | intentWithoutPkg = new Intent(intent).setPackage(null).toUri(0); |
Sunny Goyal | 2a6cf09 | 2014-06-26 15:27:14 -0700 | [diff] [blame] | 896 | } else { |
Sunny Goyal | dfde999 | 2015-05-01 12:31:32 -0700 | [diff] [blame] | 897 | intentWithPkg = new Intent(intent).setPackage(packageName).toUri(0); |
| 898 | intentWithoutPkg = intent.toUri(0); |
Sunny Goyal | 2a6cf09 | 2014-06-26 15:27:14 -0700 | [diff] [blame] | 899 | } |
| 900 | } else { |
Sunny Goyal | dfde999 | 2015-05-01 12:31:32 -0700 | [diff] [blame] | 901 | intentWithPkg = intent.toUri(0); |
| 902 | intentWithoutPkg = intent.toUri(0); |
Sunny Goyal | 2a6cf09 | 2014-06-26 15:27:14 -0700 | [diff] [blame] | 903 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 904 | |
| 905 | synchronized (sBgLock) { |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 906 | for (ItemInfo item : sBgItemsIdMap) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 907 | if (item instanceof ShortcutInfo) { |
| 908 | ShortcutInfo info = (ShortcutInfo) item; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 909 | Intent targetIntent = info.promisedIntent == null |
| 910 | ? info.intent : info.promisedIntent; |
| 911 | if (targetIntent != null && info.user.equals(user)) { |
| 912 | String s = targetIntent.toUri(0); |
Sunny Goyal | dfde999 | 2015-05-01 12:31:32 -0700 | [diff] [blame] | 913 | if (intentWithPkg.equals(s) || intentWithoutPkg.equals(s)) { |
| 914 | return true; |
| 915 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 919 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 920 | return false; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 923 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 924 | * Add an item to the database in a specified container. Sets the container, screen, cellX and |
| 925 | * cellY fields of the item. Also assigns an ID to the item. |
| 926 | */ |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 927 | public static void addItemToDatabase(Context context, final ItemInfo item, final long container, |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 928 | final long screenId, final int cellX, final int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 929 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 930 | item.cellX = cellX; |
| 931 | item.cellY = cellY; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 932 | // We store hotseat items in canonical form which is this orientation invariant position |
| 933 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 934 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 935 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 936 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 937 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 938 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 939 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 940 | |
| 941 | final ContentValues values = new ContentValues(); |
| 942 | final ContentResolver cr = context.getContentResolver(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 943 | item.onAddToDatabase(context, values); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 944 | |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 945 | item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID) |
| 946 | .getLong(LauncherSettings.Settings.EXTRA_VALUE); |
| 947 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 948 | values.put(LauncherSettings.Favorites._ID, item.id); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 949 | |
Jason Monk | 8e19cf2 | 2014-03-20 15:06:57 -0400 | [diff] [blame] | 950 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 951 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 952 | public void run() { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 953 | cr.insert(LauncherSettings.Favorites.CONTENT_URI, values); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 954 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 955 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 956 | synchronized (sBgLock) { |
Jason Monk | 8e19cf2 | 2014-03-20 15:06:57 -0400 | [diff] [blame] | 957 | checkItemInfoLocked(item.id, item, stackTrace); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 958 | sBgItemsIdMap.put(item.id, item); |
| 959 | switch (item.itemType) { |
| 960 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 961 | sBgFolders.put(item.id, (FolderInfo) item); |
| 962 | // Fall through |
| 963 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 964 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 965 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || |
| 966 | item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 967 | sBgWorkspaceItems.add(item); |
| 968 | } else { |
| 969 | if (!sBgFolders.containsKey(item.container)) { |
| 970 | // Adding an item to a folder that doesn't exist. |
| 971 | String msg = "adding item: " + item + " to a folder that " + |
| 972 | " doesn't exist"; |
Adam Cohen | 28b3e10 | 2012-10-04 17:21:33 -0700 | [diff] [blame] | 973 | Log.e(TAG, msg); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 974 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 975 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 976 | break; |
| 977 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 978 | sBgAppWidgets.add((LauncherAppWidgetInfo) item); |
| 979 | break; |
| 980 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 983 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 984 | runOnWorkerThread(r); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 987 | private static ArrayList<ItemInfo> getItemsByPackageName( |
| 988 | final String pn, final UserHandleCompat user) { |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 989 | ItemInfoFilter filter = new ItemInfoFilter() { |
| 990 | @Override |
| 991 | public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) { |
| 992 | return cn.getPackageName().equals(pn) && info.user.equals(user); |
| 993 | } |
| 994 | }; |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 995 | return filterItemInfos(sBgItemsIdMap, filter); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Removes all the items from the database corresponding to the specified package. |
| 1000 | */ |
| 1001 | static void deletePackageFromDatabase(Context context, final String pn, |
| 1002 | final UserHandleCompat user) { |
| 1003 | deleteItemsFromDatabase(context, getItemsByPackageName(pn, user)); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 1007 | * Removes the specified item from the database |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1008 | */ |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 1009 | public static void deleteItemFromDatabase(Context context, final ItemInfo item) { |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1010 | ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); |
| 1011 | items.add(item); |
| 1012 | deleteItemsFromDatabase(context, items); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Removes the specified items from the database |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1017 | */ |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 1018 | static void deleteItemsFromDatabase(Context context, final ArrayList<? extends ItemInfo> items) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1019 | final ContentResolver cr = context.getContentResolver(); |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 1020 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1021 | public void run() { |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1022 | for (ItemInfo item : items) { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1023 | final Uri uri = LauncherSettings.Favorites.getContentUri(item.id); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1024 | cr.delete(uri, null, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1025 | |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1026 | // Lock on mBgLock *after* the db operation |
| 1027 | synchronized (sBgLock) { |
| 1028 | switch (item.itemType) { |
| 1029 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 1030 | sBgFolders.remove(item.id); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1031 | for (ItemInfo info: sBgItemsIdMap) { |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1032 | if (info.container == item.id) { |
| 1033 | // We are deleting a folder which still contains items that |
| 1034 | // think they are contained by that folder. |
| 1035 | String msg = "deleting a folder (" + item + ") which still " + |
| 1036 | "contains items (" + info + ")"; |
| 1037 | Log.e(TAG, msg); |
| 1038 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1039 | } |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 1040 | sBgWorkspaceItems.remove(item); |
| 1041 | break; |
| 1042 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1043 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 1044 | sBgWorkspaceItems.remove(item); |
| 1045 | break; |
| 1046 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 1047 | sBgAppWidgets.remove((LauncherAppWidgetInfo) item); |
| 1048 | break; |
| 1049 | } |
| 1050 | sBgItemsIdMap.remove(item.id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1051 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 1054 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1055 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /** |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1059 | * Update the order of the workspace screens in the database. The array list contains |
| 1060 | * a list of screen ids in the order that they should appear. |
| 1061 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1062 | public void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1063 | final ArrayList<Long> screensCopy = new ArrayList<Long>(screens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1064 | final ContentResolver cr = context.getContentResolver(); |
| 1065 | final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 1066 | |
| 1067 | // Remove any negative screen ids -- these aren't persisted |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1068 | Iterator<Long> iter = screensCopy.iterator(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1069 | while (iter.hasNext()) { |
| 1070 | long id = iter.next(); |
| 1071 | if (id < 0) { |
| 1072 | iter.remove(); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | Runnable r = new Runnable() { |
| 1077 | @Override |
| 1078 | public void run() { |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 1079 | ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1080 | // Clear the table |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 1081 | ops.add(ContentProviderOperation.newDelete(uri).build()); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1082 | int count = screensCopy.size(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1083 | for (int i = 0; i < count; i++) { |
| 1084 | ContentValues v = new ContentValues(); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1085 | long screenId = screensCopy.get(i); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1086 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 1087 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 1088 | ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build()); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1089 | } |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 1090 | |
| 1091 | try { |
| 1092 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 1093 | } catch (Exception ex) { |
| 1094 | throw new RuntimeException(ex); |
| 1095 | } |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 1096 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1097 | synchronized (sBgLock) { |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1098 | sBgWorkspaceScreens.clear(); |
| 1099 | sBgWorkspaceScreens.addAll(screensCopy); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 1100 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1101 | } |
| 1102 | }; |
| 1103 | runOnWorkerThread(r); |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 1107 | * Remove the specified folder and all its contents from the database. |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1108 | */ |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 1109 | public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1110 | final ContentResolver cr = context.getContentResolver(); |
| 1111 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 1112 | Runnable r = new Runnable() { |
| 1113 | public void run() { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1114 | cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1115 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1116 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1117 | sBgItemsIdMap.remove(info.id); |
| 1118 | sBgFolders.remove(info.id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1119 | sBgWorkspaceItems.remove(info); |
| 1120 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1121 | |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1122 | cr.delete(LauncherSettings.Favorites.CONTENT_URI, |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 1123 | LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1124 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1125 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1126 | for (ItemInfo childInfo : info.contents) { |
| 1127 | sBgItemsIdMap.remove(childInfo.id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1128 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1129 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 1130 | } |
| 1131 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1132 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * Set this as the current Launcher activity object for the loader. |
| 1137 | */ |
| 1138 | public void initialize(Callbacks callbacks) { |
| 1139 | synchronized (mLock) { |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1140 | // Disconnect any of the callbacks and drawables associated with ItemInfos on the |
| 1141 | // workspace to prevent leaking Launcher activities on orientation change. |
| 1142 | unbindItemInfosAndClearQueuedBindRunnables(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1143 | mCallbacks = new WeakReference<Callbacks>(callbacks); |
| 1144 | } |
| 1145 | } |
| 1146 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1147 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 1148 | public void onPackageChanged(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1149 | int op = PackageUpdatedTask.OP_UPDATE; |
| 1150 | enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, |
| 1151 | user)); |
| 1152 | } |
| 1153 | |
| 1154 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 1155 | public void onPackageRemoved(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1156 | int op = PackageUpdatedTask.OP_REMOVE; |
| 1157 | enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, |
| 1158 | user)); |
| 1159 | } |
| 1160 | |
| 1161 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 1162 | public void onPackageAdded(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1163 | int op = PackageUpdatedTask.OP_ADD; |
| 1164 | enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, |
| 1165 | user)); |
| 1166 | } |
| 1167 | |
| 1168 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 1169 | public void onPackagesAvailable(String[] packageNames, UserHandleCompat user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1170 | boolean replacing) { |
| 1171 | if (!replacing) { |
| 1172 | enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames, |
| 1173 | user)); |
| 1174 | if (mAppsCanBeOnRemoveableStorage) { |
| 1175 | // Only rebind if we support removable storage. It catches the |
| 1176 | // case where |
| 1177 | // apps on the external sd card need to be reloaded |
| 1178 | startLoaderFromBackground(); |
| 1179 | } |
| 1180 | } else { |
| 1181 | // If we are replacing then just update the packages in the list |
| 1182 | enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, |
| 1183 | packageNames, user)); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 1188 | public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1189 | boolean replacing) { |
| 1190 | if (!replacing) { |
| 1191 | enqueuePackageUpdated(new PackageUpdatedTask( |
| 1192 | PackageUpdatedTask.OP_UNAVAILABLE, packageNames, |
| 1193 | user)); |
| 1194 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 1197 | @Override |
| 1198 | public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) { |
| 1199 | enqueuePackageUpdated(new PackageUpdatedTask( |
| 1200 | PackageUpdatedTask.OP_SUSPEND, packageNames, |
| 1201 | user)); |
| 1202 | } |
| 1203 | |
| 1204 | @Override |
| 1205 | public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) { |
| 1206 | enqueuePackageUpdated(new PackageUpdatedTask( |
| 1207 | PackageUpdatedTask.OP_UNSUSPEND, packageNames, |
| 1208 | user)); |
| 1209 | } |
| 1210 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 1211 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1212 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 1213 | * ACTION_PACKAGE_CHANGED. |
| 1214 | */ |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 1215 | @Override |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 1216 | public void onReceive(Context context, Intent intent) { |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 1217 | if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1218 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1219 | final String action = intent.getAction(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1220 | if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1221 | // If we have changed locale we need to clear out the labels in all apps/workspace. |
| 1222 | forceReload(); |
Winson Chung | 88fa741 | 2015-08-03 14:25:28 -0700 | [diff] [blame] | 1223 | } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action)) { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1224 | Callbacks callbacks = getCallback(); |
| 1225 | if (callbacks != null) { |
Winson Chung | 88fa741 | 2015-08-03 14:25:28 -0700 | [diff] [blame] | 1226 | callbacks.bindSearchProviderChanged(); |
Winson Chung | cfdf7ee | 2011-08-25 11:38:34 -0700 | [diff] [blame] | 1227 | } |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 1228 | } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action) |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1229 | || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action) |
| 1230 | || LauncherAppsCompat.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED.equals(action)) { |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 1231 | UserManagerCompat.getInstance(context).enableAndResetCache(); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 1232 | forceReload(); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
Amith Yamasani | 6cc806d | 2014-05-02 13:47:11 -0700 | [diff] [blame] | 1236 | void forceReload() { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1237 | resetLoadedState(true, true); |
| 1238 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1239 | // Do this here because if the launcher activity is running it will be restarted. |
| 1240 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 1241 | // to reload. |
| 1242 | startLoaderFromBackground(); |
| 1243 | } |
| 1244 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1245 | public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) { |
| 1246 | synchronized (mLock) { |
| 1247 | // Stop any existing loaders first, so they don't set mAllAppsLoaded or |
| 1248 | // mWorkspaceLoaded to true later |
| 1249 | stopLoaderLocked(); |
| 1250 | if (resetAllAppsLoaded) mAllAppsLoaded = false; |
| 1251 | if (resetWorkspaceLoaded) mWorkspaceLoaded = false; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1255 | /** |
| 1256 | * When the launcher is in the background, it's possible for it to miss paired |
| 1257 | * configuration changes. So whenever we trigger the loader from the background |
| 1258 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 1259 | * of doing it now. |
| 1260 | */ |
| 1261 | public void startLoaderFromBackground() { |
| 1262 | boolean runLoader = false; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1263 | Callbacks callbacks = getCallback(); |
| 1264 | if (callbacks != null) { |
| 1265 | // Only actually run the loader if they're not paused. |
| 1266 | if (!callbacks.setLoadOnResume()) { |
| 1267 | runLoader = true; |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | if (runLoader) { |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1271 | startLoader(PagedView.INVALID_RESTORE_PAGE); |
Joe Onorato | 790c2d9 | 2010-06-11 00:14:11 -0700 | [diff] [blame] | 1272 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1273 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 1274 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1275 | /** |
| 1276 | * If there is already a loader task running, tell it to stop. |
| 1277 | */ |
| 1278 | private void stopLoaderLocked() { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1279 | LoaderTask oldTask = mLoaderTask; |
| 1280 | if (oldTask != null) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1281 | oldTask.stopLocked(); |
| 1282 | } |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Adam Cohen | 1a85c58 | 2014-09-30 09:48:49 -0700 | [diff] [blame] | 1285 | public boolean isCurrentCallbacks(Callbacks callbacks) { |
| 1286 | return (mCallbacks != null && mCallbacks.get() == callbacks); |
| 1287 | } |
| 1288 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1289 | public void startLoader(int synchronousBindPage) { |
| 1290 | startLoader(synchronousBindPage, LOADER_FLAG_NONE); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1291 | } |
| 1292 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1293 | public void startLoader(int synchronousBindPage, int loadFlags) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1294 | // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems |
| 1295 | InstallShortcutReceiver.enableInstallQueue(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1296 | synchronized (mLock) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1297 | // Don't bother to start the thread if we know it's not going to do anything |
| 1298 | if (mCallbacks != null && mCallbacks.get() != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1299 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1300 | // Clear any pending bind-runnables from the synchronized load process. |
| 1301 | runOnMainThread(new Runnable() { |
| 1302 | public void run() { |
| 1303 | oldCallbacks.clearPendingBinds(); |
| 1304 | } |
| 1305 | }); |
| 1306 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1307 | // If there is already one running, tell it to stop. |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1308 | stopLoaderLocked(); |
| 1309 | mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags); |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 1310 | if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 1311 | && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1312 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
| 1313 | } else { |
| 1314 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 1315 | sWorker.post(mLoaderTask); |
| 1316 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1317 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1321 | public void stopLoader() { |
| 1322 | synchronized (mLock) { |
| 1323 | if (mLoaderTask != null) { |
| 1324 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1325 | } |
| 1326 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1327 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1328 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 1329 | /** |
| 1330 | * Loads the workspace screen ids in an ordered list. |
| 1331 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1332 | public static ArrayList<Long> loadWorkspaceScreensDb(Context context) { |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1333 | final ContentResolver contentResolver = context.getContentResolver(); |
| 1334 | final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1335 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 1336 | // Get screens ordered by rank. |
| 1337 | final Cursor sc = contentResolver.query(screensUri, null, null, null, |
| 1338 | LauncherSettings.WorkspaceScreens.SCREEN_RANK); |
| 1339 | ArrayList<Long> screenIds = new ArrayList<Long>(); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1340 | try { |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 1341 | final int idIndex = sc.getColumnIndexOrThrow(LauncherSettings.WorkspaceScreens._ID); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1342 | while (sc.moveToNext()) { |
| 1343 | try { |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 1344 | screenIds.add(sc.getLong(idIndex)); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1345 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1346 | addDumpLog("Invalid screen id: " + e); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | } finally { |
| 1350 | sc.close(); |
| 1351 | } |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 1352 | return screenIds; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1355 | /** |
| 1356 | * Runnable for the thread that loads the contents of the launcher: |
| 1357 | * - workspace icons |
| 1358 | * - widgets |
| 1359 | * - all apps icons |
| 1360 | */ |
| 1361 | private class LoaderTask implements Runnable { |
| 1362 | private Context mContext; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1363 | @Thunk boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1364 | private boolean mStopped; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1365 | @Thunk boolean mLoadAndBindStepFinished; |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1366 | private int mFlags; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1367 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 1368 | LoaderTask(Context context, int flags) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1369 | mContext = context; |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1370 | mFlags = flags; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1373 | private void loadAndBindWorkspace() { |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1374 | mIsLoadingAndBindingWorkspace = true; |
| 1375 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1376 | // Load the workspace |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1377 | if (DEBUG_LOADERS) { |
| 1378 | Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1379 | } |
Michael Jurka | 288a36b | 2011-07-12 16:53:48 -0700 | [diff] [blame] | 1380 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1381 | if (!mWorkspaceLoaded) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1382 | loadWorkspace(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1383 | synchronized (LoaderTask.this) { |
| 1384 | if (mStopped) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1385 | return; |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1386 | } |
| 1387 | mWorkspaceLoaded = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1391 | // Bind the workspace |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1392 | bindWorkspace(-1); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1393 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1394 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1395 | private void waitForIdle() { |
| 1396 | // Wait until the either we're stopped or the other threads are done. |
| 1397 | // This way we don't start loading all apps until the workspace has settled |
| 1398 | // down. |
| 1399 | synchronized (LoaderTask.this) { |
| 1400 | final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1401 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1402 | mHandler.postIdle(new Runnable() { |
| 1403 | public void run() { |
| 1404 | synchronized (LoaderTask.this) { |
| 1405 | mLoadAndBindStepFinished = true; |
| 1406 | if (DEBUG_LOADERS) { |
| 1407 | Log.d(TAG, "done with previous binding step"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1408 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1409 | LoaderTask.this.notify(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1410 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1411 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1412 | }); |
| 1413 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 1414 | while (!mStopped && !mLoadAndBindStepFinished) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1415 | try { |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 1416 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 1417 | // wait no longer than 1sec at a time |
| 1418 | this.wait(1000); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1419 | } catch (InterruptedException ex) { |
| 1420 | // Ignore |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1421 | } |
| 1422 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1423 | if (DEBUG_LOADERS) { |
| 1424 | Log.d(TAG, "waited " |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1425 | + (SystemClock.uptimeMillis()-workspaceWaitTime) |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1426 | + "ms for previous step to finish binding"); |
| 1427 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1428 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1429 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1430 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1431 | void runBindSynchronousPage(int synchronousBindPage) { |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 1432 | if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1433 | // Ensure that we have a valid page index to load synchronously |
| 1434 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 1435 | "valid page index"); |
| 1436 | } |
| 1437 | if (!mAllAppsLoaded || !mWorkspaceLoaded) { |
| 1438 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 1439 | // loaded already (we should load everything asynchronously in that case) |
| 1440 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 1441 | } |
| 1442 | synchronized (mLock) { |
| 1443 | if (mIsLoaderTaskRunning) { |
| 1444 | // Ensure that we are never running the background loading at this point since |
| 1445 | // we also touch the background collections |
| 1446 | throw new RuntimeException("Error! Background loading is already running"); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | // XXX: Throw an exception if we are already loading (since we touch the worker thread |
| 1451 | // data structures, we can't allow any other thread to touch that data, but because |
| 1452 | // this call is synchronous, we can get away with not locking). |
| 1453 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1454 | // The LauncherModel is static in the LauncherAppState and mHandler may have queued |
Adam Cohen | a13a2f2 | 2012-07-23 14:29:15 -0700 | [diff] [blame] | 1455 | // operations from the previous activity. We need to ensure that all queued operations |
| 1456 | // are executed before any synchronous binding work is done. |
| 1457 | mHandler.flush(); |
| 1458 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1459 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 1460 | // everything else that is to be bound normally (asynchronously). |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1461 | bindWorkspace(synchronousBindPage); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1462 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 1463 | // arise from that. |
| 1464 | onlyBindAllApps(); |
| 1465 | } |
| 1466 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1467 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1468 | synchronized (mLock) { |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 1469 | if (mStopped) { |
| 1470 | return; |
| 1471 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1472 | mIsLoaderTaskRunning = true; |
| 1473 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1474 | // Optimize for end-user experience: if the Launcher is up and // running with the |
| 1475 | // All Apps interface in the foreground, load All Apps first. Otherwise, load the |
| 1476 | // workspace first (default). |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1477 | keep_running: { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1478 | if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace"); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1479 | loadAndBindWorkspace(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1480 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1481 | if (mStopped) { |
| 1482 | break keep_running; |
| 1483 | } |
| 1484 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1485 | waitForIdle(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1486 | |
| 1487 | // second step |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1488 | if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps"); |
| 1489 | loadAndBindAllApps(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1490 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1491 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1492 | // Clear out this reference, otherwise we end up holding it until all of the |
| 1493 | // callback runnables are done. |
| 1494 | mContext = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1495 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1496 | synchronized (mLock) { |
| 1497 | // If we are still the last one to be scheduled, remove ourselves. |
| 1498 | if (mLoaderTask == this) { |
| 1499 | mLoaderTask = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1500 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1501 | mIsLoaderTaskRunning = false; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1502 | mHasLoaderCompletedOnce = true; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1503 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | public void stopLocked() { |
| 1507 | synchronized (LoaderTask.this) { |
| 1508 | mStopped = true; |
| 1509 | this.notify(); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 1515 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 1516 | * object that was around when the deferred message was scheduled, and if there's |
| 1517 | * a new Callbacks object around then also return null. This will save us from |
| 1518 | * calling onto it with data that will be ignored. |
| 1519 | */ |
| 1520 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 1521 | synchronized (mLock) { |
| 1522 | if (mStopped) { |
| 1523 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1524 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1525 | |
| 1526 | if (mCallbacks == null) { |
| 1527 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1528 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1529 | |
| 1530 | final Callbacks callbacks = mCallbacks.get(); |
| 1531 | if (callbacks != oldCallbacks) { |
| 1532 | return null; |
| 1533 | } |
| 1534 | if (callbacks == null) { |
| 1535 | Log.w(TAG, "no mCallbacks"); |
| 1536 | return null; |
| 1537 | } |
| 1538 | |
| 1539 | return callbacks; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | // check & update map of what's occupied; used to discard overlapping/invalid items |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1544 | private boolean checkItemPlacement(LongArrayMap<ItemInfo[][]> occupied, ItemInfo item, |
| 1545 | ArrayList<Long> workspaceScreens) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1546 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1547 | InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1548 | final int countX = profile.numColumns; |
| 1549 | final int countY = profile.numRows; |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1550 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1551 | long containerIndex = item.screenId; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1552 | if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Winson Chung | a0b7e86 | 2013-09-05 16:03:15 -0700 | [diff] [blame] | 1553 | // Return early if we detect that an item is under the hotseat button |
| 1554 | if (mCallbacks == null || |
| 1555 | mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) { |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1556 | Log.e(TAG, "Error loading shortcut into hotseat " + item |
| 1557 | + " into position (" + item.screenId + ":" + item.cellX + "," |
| 1558 | + item.cellY + ") occupied by all apps"); |
Winson Chung | a0b7e86 | 2013-09-05 16:03:15 -0700 | [diff] [blame] | 1559 | return false; |
| 1560 | } |
| 1561 | |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1562 | final ItemInfo[][] hotseatItems = |
| 1563 | occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT); |
| 1564 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1565 | if (item.screenId >= profile.numHotseatIcons) { |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1566 | Log.e(TAG, "Error loading shortcut " + item |
| 1567 | + " into hotseat position " + item.screenId |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1568 | + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1) |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1569 | + ")"); |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1573 | if (hotseatItems != null) { |
| 1574 | if (hotseatItems[(int) item.screenId][0] != null) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1575 | Log.e(TAG, "Error loading shortcut into hotseat " + item |
| 1576 | + " into position (" + item.screenId + ":" + item.cellX + "," |
| 1577 | + item.cellY + ") occupied by " |
| 1578 | + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT) |
| 1579 | [(int) item.screenId][0]); |
| 1580 | return false; |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1581 | } else { |
| 1582 | hotseatItems[(int) item.screenId][0] = item; |
| 1583 | return true; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1584 | } |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1585 | } else { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1586 | final ItemInfo[][] items = new ItemInfo[(int) profile.numHotseatIcons][1]; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1587 | items[(int) item.screenId][0] = item; |
| 1588 | occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items); |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1589 | return true; |
| 1590 | } |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1591 | } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1592 | if (!workspaceScreens.contains((Long) item.screenId)) { |
| 1593 | // The item has an invalid screen id. |
| 1594 | return false; |
| 1595 | } |
| 1596 | } else { |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1597 | // Skip further checking if it is not the hotseat or workspace container |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1598 | return true; |
| 1599 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1600 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1601 | if (!occupied.containsKey(item.screenId)) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1602 | ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1]; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1603 | occupied.put(item.screenId, items); |
| 1604 | } |
| 1605 | |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1606 | final ItemInfo[][] screens = occupied.get(item.screenId); |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1607 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1608 | item.cellX < 0 || item.cellY < 0 || |
| 1609 | item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) { |
| 1610 | Log.e(TAG, "Error loading shortcut " + item |
| 1611 | + " into cell (" + containerIndex + "-" + item.screenId + ":" |
| 1612 | + item.cellX + "," + item.cellY |
| 1613 | + ") out of screen bounds ( " + countX + "x" + countY + ")"); |
| 1614 | return false; |
| 1615 | } |
| 1616 | |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1617 | // Check if any workspace icons overlap with each other |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1618 | for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { |
| 1619 | for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1620 | if (screens[x][y] != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1621 | Log.e(TAG, "Error loading shortcut " + item |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1622 | + " into cell (" + containerIndex + "-" + item.screenId + ":" |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1623 | + x + "," + y |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1624 | + ") occupied by " |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1625 | + screens[x][y]); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1626 | return false; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { |
| 1631 | for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1632 | screens[x][y] = item; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1633 | } |
| 1634 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1635 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1636 | return true; |
| 1637 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1638 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1639 | /** Clears all the sBg data structures */ |
| 1640 | private void clearSBgDataStructures() { |
| 1641 | synchronized (sBgLock) { |
| 1642 | sBgWorkspaceItems.clear(); |
| 1643 | sBgAppWidgets.clear(); |
| 1644 | sBgFolders.clear(); |
| 1645 | sBgItemsIdMap.clear(); |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1646 | sBgWorkspaceScreens.clear(); |
| 1647 | } |
| 1648 | } |
| 1649 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1650 | private void loadWorkspace() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1651 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1652 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1653 | final Context context = mContext; |
| 1654 | final ContentResolver contentResolver = context.getContentResolver(); |
| 1655 | final PackageManager manager = context.getPackageManager(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1656 | final boolean isSafeMode = manager.isSafeMode(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1657 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1658 | final boolean isSdCardReady = Utilities.isBootCompleted(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 1659 | |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1660 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1661 | InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1662 | int countX = profile.numColumns; |
| 1663 | int countY = profile.numRows; |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1664 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 1665 | if (GridSizeMigrationTask.ENABLED && |
| 1666 | !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) { |
| 1667 | // Migration failed. Clear workspace. |
| 1668 | mFlags = mFlags | LOADER_FLAG_CLEAR_WORKSPACE; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1671 | if ((mFlags & LOADER_FLAG_CLEAR_WORKSPACE) != 0) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1672 | Log.d(TAG, "loadWorkspace: resetting launcher database"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1673 | LauncherSettings.Settings.call(contentResolver, |
| 1674 | LauncherSettings.Settings.METHOD_DELETE_DB); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | if ((mFlags & LOADER_FLAG_MIGRATE_SHORTCUTS) != 0) { |
| 1678 | // append the user's Launcher2 shortcuts |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1679 | Log.d(TAG, "loadWorkspace: migrating from launcher2"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1680 | LauncherSettings.Settings.call(contentResolver, |
| 1681 | LauncherSettings.Settings.METHOD_MIGRATE_LAUNCHER2_SHORTCUTS); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1682 | } else { |
| 1683 | // Make sure the default workspace is loaded |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1684 | Log.d(TAG, "loadWorkspace: loading default favorites"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1685 | LauncherSettings.Settings.call(contentResolver, |
| 1686 | LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1687 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1688 | |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1689 | synchronized (sBgLock) { |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1690 | clearSBgDataStructures(); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1691 | final HashMap<String, Integer> installingPkgs = PackageInstallerCompat |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1692 | .getInstance(mContext).updateAndGetActiveSessionCache(); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1693 | sBgWorkspaceScreens.addAll(loadWorkspaceScreensDb(mContext)); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1694 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1695 | final ArrayList<Long> itemsToRemove = new ArrayList<>(); |
| 1696 | final ArrayList<Long> restoredRows = new ArrayList<>(); |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1697 | final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI; |
Chris Wren | e523e70 | 2013-10-09 10:36:55 -0400 | [diff] [blame] | 1698 | if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1699 | final Cursor c = contentResolver.query(contentUri, null, null, null, null); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1700 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1701 | // +1 for the hotseat (it can be larger than the workspace) |
| 1702 | // Load workspace in reverse order to ensure that latest items are loaded first (and |
| 1703 | // before any earlier duplicates) |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1704 | final LongArrayMap<ItemInfo[][]> occupied = new LongArrayMap<>(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1705 | HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1706 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1707 | try { |
| 1708 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
| 1709 | final int intentIndex = c.getColumnIndexOrThrow |
| 1710 | (LauncherSettings.Favorites.INTENT); |
| 1711 | final int titleIndex = c.getColumnIndexOrThrow |
| 1712 | (LauncherSettings.Favorites.TITLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1713 | final int containerIndex = c.getColumnIndexOrThrow( |
| 1714 | LauncherSettings.Favorites.CONTAINER); |
| 1715 | final int itemTypeIndex = c.getColumnIndexOrThrow( |
| 1716 | LauncherSettings.Favorites.ITEM_TYPE); |
| 1717 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 1718 | LauncherSettings.Favorites.APPWIDGET_ID); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1719 | final int appWidgetProviderIndex = c.getColumnIndexOrThrow( |
| 1720 | LauncherSettings.Favorites.APPWIDGET_PROVIDER); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1721 | final int screenIndex = c.getColumnIndexOrThrow( |
| 1722 | LauncherSettings.Favorites.SCREEN); |
| 1723 | final int cellXIndex = c.getColumnIndexOrThrow |
| 1724 | (LauncherSettings.Favorites.CELLX); |
| 1725 | final int cellYIndex = c.getColumnIndexOrThrow |
| 1726 | (LauncherSettings.Favorites.CELLY); |
| 1727 | final int spanXIndex = c.getColumnIndexOrThrow |
| 1728 | (LauncherSettings.Favorites.SPANX); |
| 1729 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 1730 | LauncherSettings.Favorites.SPANY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1731 | final int rankIndex = c.getColumnIndexOrThrow( |
| 1732 | LauncherSettings.Favorites.RANK); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1733 | final int restoredIndex = c.getColumnIndexOrThrow( |
| 1734 | LauncherSettings.Favorites.RESTORED); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1735 | final int profileIdIndex = c.getColumnIndexOrThrow( |
| 1736 | LauncherSettings.Favorites.PROFILE_ID); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1737 | final int optionsIndex = c.getColumnIndexOrThrow( |
| 1738 | LauncherSettings.Favorites.OPTIONS); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1739 | final CursorIconInfo cursorIconInfo = new CursorIconInfo(c); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1740 | |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1741 | final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>(); |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1742 | final LongSparseArray<Boolean> quietMode = new LongSparseArray<>(); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1743 | for (UserHandleCompat user : mUserManager.getUserProfiles()) { |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1744 | long serialNo = mUserManager.getSerialNumberForUser(user); |
| 1745 | allUsers.put(serialNo, user); |
| 1746 | quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user)); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1749 | ShortcutInfo info; |
| 1750 | String intentDescription; |
| 1751 | LauncherAppWidgetInfo appWidgetInfo; |
| 1752 | int container; |
| 1753 | long id; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1754 | long serialNumber; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1755 | Intent intent; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1756 | UserHandleCompat user; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1757 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1758 | while (!mStopped && c.moveToNext()) { |
| 1759 | try { |
| 1760 | int itemType = c.getInt(itemTypeIndex); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1761 | boolean restored = 0 != c.getInt(restoredIndex); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1762 | boolean allowMissingTarget = false; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1763 | container = c.getInt(containerIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1764 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1765 | switch (itemType) { |
| 1766 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1767 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1768 | id = c.getLong(idIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1769 | intentDescription = c.getString(intentIndex); |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1770 | serialNumber = c.getInt(profileIdIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1771 | user = allUsers.get(serialNumber); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1772 | int promiseType = c.getInt(restoredIndex); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1773 | int disabledState = 0; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1774 | boolean itemReplaced = false; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1775 | if (user == null) { |
| 1776 | // User has been deleted remove the item. |
| 1777 | itemsToRemove.add(id); |
| 1778 | continue; |
| 1779 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1780 | try { |
| 1781 | intent = Intent.parseUri(intentDescription, 0); |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1782 | ComponentName cn = intent.getComponent(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1783 | if (cn != null && cn.getPackageName() != null) { |
| 1784 | boolean validPkg = launcherApps.isPackageEnabledForProfile( |
| 1785 | cn.getPackageName(), user); |
| 1786 | boolean validComponent = validPkg && |
| 1787 | launcherApps.isActivityEnabledForProfile(cn, user); |
| 1788 | |
| 1789 | if (validComponent) { |
| 1790 | if (restored) { |
| 1791 | // no special handling necessary for this item |
| 1792 | restoredRows.add(id); |
| 1793 | restored = false; |
| 1794 | } |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 1795 | boolean isSuspended = launcherApps.isPackageSuspendedForProfile( |
| 1796 | cn.getPackageName(), user); |
| 1797 | if (isSuspended) { |
| 1798 | disabledState = ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1799 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1800 | if (quietMode.get(serialNumber)) { |
| 1801 | disabledState |= ShortcutInfo.FLAG_DISABLED_QUIET_USER; |
| 1802 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1803 | } else if (validPkg) { |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1804 | intent = null; |
| 1805 | if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) { |
| 1806 | // We allow auto install apps to have their intent |
| 1807 | // updated after an install. |
| 1808 | intent = manager.getLaunchIntentForPackage( |
| 1809 | cn.getPackageName()); |
| 1810 | if (intent != null) { |
| 1811 | ContentValues values = new ContentValues(); |
| 1812 | values.put(LauncherSettings.Favorites.INTENT, |
| 1813 | intent.toUri(0)); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1814 | updateItem(id, values); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | if (intent == null) { |
| 1819 | // The app is installed but the component is no |
| 1820 | // longer available. |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1821 | addDumpLog("Invalid component removed: " + cn); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1822 | itemsToRemove.add(id); |
| 1823 | continue; |
| 1824 | } else { |
| 1825 | // no special handling necessary for this item |
| 1826 | restoredRows.add(id); |
| 1827 | restored = false; |
| 1828 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1829 | } else if (restored) { |
| 1830 | // Package is not yet available but might be |
| 1831 | // installed later. |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1832 | addDumpLog("package not yet restored: " + cn); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1833 | |
| 1834 | if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) { |
| 1835 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1836 | } else if (installingPkgs.containsKey(cn.getPackageName())) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1837 | // App restore has started. Update the flag |
| 1838 | promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED; |
| 1839 | ContentValues values = new ContentValues(); |
| 1840 | values.put(LauncherSettings.Favorites.RESTORED, |
| 1841 | promiseType); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1842 | updateItem(id, values); |
| 1843 | } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) { |
| 1844 | // This is a common app. Try to replace this. |
| 1845 | int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType); |
| 1846 | CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context); |
| 1847 | if (parser.findDefaultApp()) { |
| 1848 | // Default app found. Replace it. |
| 1849 | intent = parser.parsedIntent; |
| 1850 | cn = intent.getComponent(); |
| 1851 | ContentValues values = parser.parsedValues; |
| 1852 | values.put(LauncherSettings.Favorites.RESTORED, 0); |
| 1853 | updateItem(id, values); |
| 1854 | restored = false; |
| 1855 | itemReplaced = true; |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1856 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1857 | } else if (REMOVE_UNRESTORED_ICONS) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1858 | addDumpLog("Unrestored package removed: " + cn); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1859 | itemsToRemove.add(id); |
| 1860 | continue; |
| 1861 | } |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1862 | } else if (REMOVE_UNRESTORED_ICONS) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1863 | addDumpLog("Unrestored package removed: " + cn); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1864 | itemsToRemove.add(id); |
| 1865 | continue; |
| 1866 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1867 | } else if (launcherApps.isAppEnabled( |
| 1868 | manager, cn.getPackageName(), |
| 1869 | PackageManager.GET_UNINSTALLED_PACKAGES)) { |
| 1870 | // Package is present but not available. |
| 1871 | allowMissingTarget = true; |
| 1872 | disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; |
| 1873 | } else if (!isSdCardReady) { |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1874 | // SdCard is not ready yet. Package might get available, |
| 1875 | // once it is ready. |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1876 | Log.d(TAG, "Invalid package: " + cn + " (check again later)"); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1877 | HashSet<String> pkgs = sPendingPackages.get(user); |
| 1878 | if (pkgs == null) { |
Sameer Padala | 513edae | 2014-07-29 16:17:08 -0700 | [diff] [blame] | 1879 | pkgs = new HashSet<String>(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1880 | sPendingPackages.put(user, pkgs); |
| 1881 | } |
| 1882 | pkgs.add(cn.getPackageName()); |
| 1883 | allowMissingTarget = true; |
| 1884 | // Add the icon on the workspace anyway. |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1885 | |
| 1886 | } else { |
| 1887 | // Do not wait for external media load anymore. |
| 1888 | // Log the invalid package, and remove it |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1889 | addDumpLog("Invalid package removed: " + cn); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1890 | itemsToRemove.add(id); |
| 1891 | continue; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1892 | } |
Sunny Goyal | 938a53d | 2014-09-05 03:17:45 -0700 | [diff] [blame] | 1893 | } else if (cn == null) { |
| 1894 | // For shortcuts with no component, keep them as they are |
| 1895 | restoredRows.add(id); |
| 1896 | restored = false; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1897 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1898 | } catch (URISyntaxException e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1899 | addDumpLog("Invalid uri: " + intentDescription); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1900 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1901 | continue; |
| 1902 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1903 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1904 | boolean useLowResIcon = container >= 0 && |
| 1905 | c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW; |
| 1906 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1907 | if (itemReplaced) { |
| 1908 | if (user.equals(UserHandleCompat.myUserHandle())) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 1909 | info = getAppShortcutInfo(manager, intent, user, context, null, |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1910 | cursorIconInfo.iconIndex, titleIndex, |
| 1911 | false, useLowResIcon); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1912 | } else { |
| 1913 | // Don't replace items for other profiles. |
| 1914 | itemsToRemove.add(id); |
| 1915 | continue; |
| 1916 | } |
| 1917 | } else if (restored) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1918 | if (user.equals(UserHandleCompat.myUserHandle())) { |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1919 | info = getRestoredItemInfo(c, titleIndex, intent, |
Sunny Goyal | c22841b | 2015-07-13 19:59:50 -0700 | [diff] [blame] | 1920 | promiseType, itemType, cursorIconInfo, context); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1921 | intent = getRestoredItemIntent(c, context, intent); |
| 1922 | } else { |
| 1923 | // Don't restore items for other profiles. |
| 1924 | itemsToRemove.add(id); |
| 1925 | continue; |
| 1926 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1927 | } else if (itemType == |
| 1928 | LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 1929 | info = getAppShortcutInfo(manager, intent, user, context, c, |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1930 | cursorIconInfo.iconIndex, titleIndex, |
| 1931 | allowMissingTarget, useLowResIcon); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1932 | } else { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1933 | info = getShortcutInfo(c, context, titleIndex, cursorIconInfo); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1934 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1935 | // App shortcuts that used to be automatically added to Launcher |
| 1936 | // didn't always have the correct intent flags set, so do that |
| 1937 | // here |
| 1938 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1939 | intent.getCategories() != null && |
| 1940 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1941 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1942 | intent.addFlags( |
| 1943 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1944 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1945 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1946 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1947 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1948 | if (info != null) { |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1949 | info.id = id; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1950 | info.intent = intent; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1951 | info.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1952 | info.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1953 | info.cellX = c.getInt(cellXIndex); |
| 1954 | info.cellY = c.getInt(cellYIndex); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1955 | info.rank = c.getInt(rankIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1956 | info.spanX = 1; |
| 1957 | info.spanY = 1; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1958 | info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1959 | if (info.promisedIntent != null) { |
| 1960 | info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber); |
| 1961 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1962 | info.isDisabled = disabledState; |
| 1963 | if (isSafeMode && !Utilities.isSystemApp(context, intent)) { |
| 1964 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; |
| 1965 | } |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1966 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1967 | // check & update map of what's occupied |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1968 | if (!checkItemPlacement(occupied, info, sBgWorkspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 1969 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1970 | break; |
| 1971 | } |
| 1972 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1973 | if (restored) { |
| 1974 | ComponentName cn = info.getTargetComponent(); |
| 1975 | if (cn != null) { |
| 1976 | Integer progress = installingPkgs.get(cn.getPackageName()); |
| 1977 | if (progress != null) { |
| 1978 | info.setInstallProgress(progress); |
| 1979 | } else { |
| 1980 | info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1985 | switch (container) { |
| 1986 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 1987 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: |
| 1988 | sBgWorkspaceItems.add(info); |
| 1989 | break; |
| 1990 | default: |
| 1991 | // Item is in a user folder |
| 1992 | FolderInfo folderInfo = |
| 1993 | findOrMakeFolder(sBgFolders, container); |
| 1994 | folderInfo.add(info); |
| 1995 | break; |
| 1996 | } |
| 1997 | sBgItemsIdMap.put(info.id, info); |
Winson Chung | 1323b48 | 2013-08-05 12:41:55 -0700 | [diff] [blame] | 1998 | } else { |
| 1999 | throw new RuntimeException("Unexpected null ShortcutInfo"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2000 | } |
| 2001 | break; |
| 2002 | |
| 2003 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 2004 | id = c.getLong(idIndex); |
| 2005 | FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id); |
| 2006 | |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 2007 | // Do not trim the folder label, as is was set by the user. |
| 2008 | folderInfo.title = c.getString(titleIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2009 | folderInfo.id = id; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2010 | folderInfo.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2011 | folderInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2012 | folderInfo.cellX = c.getInt(cellXIndex); |
| 2013 | folderInfo.cellY = c.getInt(cellYIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2014 | folderInfo.spanX = 1; |
| 2015 | folderInfo.spanY = 1; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 2016 | folderInfo.options = c.getInt(optionsIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2017 | |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 2018 | // check & update map of what's occupied |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 2019 | if (!checkItemPlacement(occupied, folderInfo, sBgWorkspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 2020 | itemsToRemove.add(id); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 2021 | break; |
| 2022 | } |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 2023 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2024 | switch (container) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2025 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 2026 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: |
| 2027 | sBgWorkspaceItems.add(folderInfo); |
| 2028 | break; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2029 | } |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 2030 | |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2031 | if (restored) { |
| 2032 | // no special handling required for restored folders |
| 2033 | restoredRows.add(id); |
| 2034 | } |
| 2035 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2036 | sBgItemsIdMap.put(folderInfo.id, folderInfo); |
| 2037 | sBgFolders.put(folderInfo.id, folderInfo); |
| 2038 | break; |
| 2039 | |
| 2040 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2041 | case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2042 | // Read all Launcher-specific widget details |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2043 | boolean customWidget = itemType == |
| 2044 | LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 2045 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2046 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 2047 | serialNumber = c.getLong(profileIdIndex); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 2048 | String savedProvider = c.getString(appWidgetProviderIndex); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2049 | id = c.getLong(idIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 2050 | user = allUsers.get(serialNumber); |
| 2051 | if (user == null) { |
| 2052 | itemsToRemove.add(id); |
| 2053 | continue; |
| 2054 | } |
| 2055 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2056 | final ComponentName component = |
| 2057 | ComponentName.unflattenFromString(savedProvider); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2058 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2059 | final int restoreStatus = c.getInt(restoredIndex); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2060 | final boolean isIdValid = (restoreStatus & |
| 2061 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2062 | final boolean wasProviderReady = (restoreStatus & |
| 2063 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2064 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2065 | if (widgetProvidersMap == null) { |
| 2066 | widgetProvidersMap = AppWidgetManagerCompat |
| 2067 | .getInstance(mContext).getAllProvidersMap(); |
| 2068 | } |
| 2069 | final AppWidgetProviderInfo provider = widgetProvidersMap.get( |
| 2070 | new ComponentKey( |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 2071 | ComponentName.unflattenFromString(savedProvider), |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2072 | user)); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2073 | |
| 2074 | final boolean isProviderReady = isValidProvider(provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2075 | if (!isSafeMode && !customWidget && |
| 2076 | wasProviderReady && !isProviderReady) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 2077 | addDumpLog("Deleting widget that isn't installed anymore: " |
| 2078 | + provider); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2079 | itemsToRemove.add(id); |
| 2080 | } else { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2081 | if (isProviderReady) { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2082 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 2083 | provider.provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2084 | |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 2085 | // The provider is available. So the widget is either |
| 2086 | // available or not available. We do not need to track |
| 2087 | // any future restore updates. |
| 2088 | int status = restoreStatus & |
| 2089 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2090 | if (!wasProviderReady) { |
| 2091 | // If provider was not previously ready, update the |
| 2092 | // status and UI flag. |
| 2093 | |
| 2094 | // Id would be valid only if the widget restore broadcast was received. |
| 2095 | if (isIdValid) { |
Sunny Goyal | 84b4adc | 2015-08-12 15:12:16 -0700 | [diff] [blame] | 2096 | status = LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2097 | } else { |
| 2098 | status &= ~LauncherAppWidgetInfo |
| 2099 | .FLAG_PROVIDER_NOT_READY; |
| 2100 | } |
| 2101 | } |
| 2102 | appWidgetInfo.restoreStatus = status; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2103 | } else { |
| 2104 | Log.v(TAG, "Widget restore pending id=" + id |
| 2105 | + " appWidgetId=" + appWidgetId |
| 2106 | + " status =" + restoreStatus); |
| 2107 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2108 | component); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2109 | appWidgetInfo.restoreStatus = restoreStatus; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2110 | Integer installProgress = installingPkgs.get(component.getPackageName()); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 2111 | |
| 2112 | if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) { |
| 2113 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2114 | } else if (installProgress != null) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 2115 | // App restore has started. Update the flag |
| 2116 | appWidgetInfo.restoreStatus |= |
| 2117 | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | 9b4b081 | 2014-10-08 10:47:28 -0700 | [diff] [blame] | 2118 | } else if (REMOVE_UNRESTORED_ICONS && !isSafeMode) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 2119 | addDumpLog("Unrestored widget removed: " + component); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 2120 | itemsToRemove.add(id); |
| 2121 | continue; |
| 2122 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2123 | |
| 2124 | appWidgetInfo.installProgress = |
| 2125 | installProgress == null ? 0 : installProgress; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2126 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 2127 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2128 | appWidgetInfo.id = id; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2129 | appWidgetInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2130 | appWidgetInfo.cellX = c.getInt(cellXIndex); |
| 2131 | appWidgetInfo.cellY = c.getInt(cellYIndex); |
| 2132 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 2133 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
Sunny Goyal | ab7a4fe | 2015-07-15 17:20:54 -0700 | [diff] [blame] | 2134 | appWidgetInfo.user = user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2135 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2136 | if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 2137 | container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 2138 | Log.e(TAG, "Widget found where container != " + |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 2139 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
| 2140 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2141 | continue; |
| 2142 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2143 | |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 2144 | appWidgetInfo.container = container; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2145 | // check & update map of what's occupied |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 2146 | if (!checkItemPlacement(occupied, appWidgetInfo, sBgWorkspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 2147 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2148 | break; |
| 2149 | } |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2150 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2151 | if (!customWidget) { |
| 2152 | String providerName = |
| 2153 | appWidgetInfo.providerName.flattenToString(); |
| 2154 | if (!providerName.equals(savedProvider) || |
| 2155 | (appWidgetInfo.restoreStatus != restoreStatus)) { |
| 2156 | ContentValues values = new ContentValues(); |
| 2157 | values.put( |
| 2158 | LauncherSettings.Favorites.APPWIDGET_PROVIDER, |
| 2159 | providerName); |
| 2160 | values.put(LauncherSettings.Favorites.RESTORED, |
| 2161 | appWidgetInfo.restoreStatus); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 2162 | updateItem(id, values); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 2163 | } |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 2164 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2165 | sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo); |
| 2166 | sBgAppWidgets.add(appWidgetInfo); |
| 2167 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2168 | break; |
| 2169 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2170 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 2171 | Log.e(TAG, "Desktop items loading interrupted", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 2172 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2173 | } |
| 2174 | } finally { |
Daniel Sandler | 47b5031 | 2013-07-25 13:16:14 -0400 | [diff] [blame] | 2175 | if (c != null) { |
| 2176 | c.close(); |
| 2177 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 2180 | // Break early if we've stopped loading |
| 2181 | if (mStopped) { |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 2182 | clearSBgDataStructures(); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2183 | return; |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2186 | if (itemsToRemove.size() > 0) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2187 | // Remove dead items |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 2188 | contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI, |
| 2189 | Utilities.createDbSelectionQuery( |
| 2190 | LauncherSettings.Favorites._ID, itemsToRemove), null); |
| 2191 | if (DEBUG_LOADERS) { |
| 2192 | Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery( |
| 2193 | LauncherSettings.Favorites._ID, itemsToRemove)); |
| 2194 | } |
| 2195 | |
| 2196 | // Remove any empty folder |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 2197 | ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings |
| 2198 | .call(contentResolver, |
| 2199 | LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS) |
| 2200 | .getSerializable(LauncherSettings.Settings.EXTRA_VALUE); |
| 2201 | for (long folderId : deletedFolderIds) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 2202 | sBgWorkspaceItems.remove(sBgFolders.get(folderId)); |
| 2203 | sBgFolders.remove(folderId); |
| 2204 | sBgItemsIdMap.remove(folderId); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 2208 | // Sort all the folder items and make sure the first 3 items are high resolution. |
| 2209 | for (FolderInfo folder : sBgFolders) { |
| 2210 | Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR); |
| 2211 | int pos = 0; |
| 2212 | for (ShortcutInfo info : folder.contents) { |
| 2213 | if (info.usingLowResIcon) { |
| 2214 | info.updateIcon(mIconCache, false); |
| 2215 | } |
| 2216 | pos ++; |
| 2217 | if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) { |
| 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2223 | if (restoredRows.size() > 0) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2224 | // Update restored items that no longer require special handling |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 2225 | ContentValues values = new ContentValues(); |
| 2226 | values.put(LauncherSettings.Favorites.RESTORED, 0); |
| 2227 | contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values, |
| 2228 | Utilities.createDbSelectionQuery( |
| 2229 | LauncherSettings.Favorites._ID, restoredRows), null); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2230 | } |
| 2231 | |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2232 | if (!isSdCardReady && !sPendingPackages.isEmpty()) { |
| 2233 | context.registerReceiver(new AppsAvailabilityCheck(), |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 2234 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2235 | null, sWorker); |
| 2236 | } |
| 2237 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2238 | // Remove any empty screens |
| 2239 | ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgWorkspaceScreens); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2240 | for (ItemInfo item: sBgItemsIdMap) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2241 | long screenId = item.screenId; |
| 2242 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 2243 | unusedScreens.contains(screenId)) { |
| 2244 | unusedScreens.remove(screenId); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | // If there are any empty screens remove them, and update. |
| 2249 | if (unusedScreens.size() != 0) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2250 | sBgWorkspaceScreens.removeAll(unusedScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2251 | updateWorkspaceScreenOrder(context, sBgWorkspaceScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2254 | if (DEBUG_LOADERS) { |
| 2255 | Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms"); |
| 2256 | Log.d(TAG, "workspace layout: "); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2257 | int nScreens = occupied.size(); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 2258 | for (int y = 0; y < countY; y++) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2259 | String line = ""; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2260 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2261 | for (int i = 0; i < nScreens; i++) { |
| 2262 | long screenId = occupied.keyAt(i); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 2263 | if (screenId > 0) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2264 | line += " | "; |
| 2265 | } |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2266 | ItemInfo[][] screen = occupied.valueAt(i); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 2267 | for (int x = 0; x < countX; x++) { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 2268 | if (x < screen.length && y < screen[x].length) { |
| 2269 | line += (screen[x][y] != null) ? "#" : "."; |
| 2270 | } else { |
| 2271 | line += "!"; |
| 2272 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2273 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2274 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2275 | Log.d(TAG, "[ " + line + " ]"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2276 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2277 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2278 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 2281 | /** |
| 2282 | * Partially updates the item without any notification. Must be called on the worker thread. |
| 2283 | */ |
| 2284 | private void updateItem(long itemId, ContentValues update) { |
| 2285 | mContext.getContentResolver().update( |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 2286 | LauncherSettings.Favorites.CONTENT_URI, |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 2287 | update, |
| 2288 | BaseColumns._ID + "= ?", |
| 2289 | new String[]{Long.toString(itemId)}); |
| 2290 | } |
| 2291 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2292 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 2293 | * specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2294 | private void filterCurrentWorkspaceItems(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2295 | ArrayList<ItemInfo> allWorkspaceItems, |
| 2296 | ArrayList<ItemInfo> currentScreenItems, |
| 2297 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2298 | // Purge any null ItemInfos |
| 2299 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 2300 | while (iter.hasNext()) { |
| 2301 | ItemInfo i = iter.next(); |
| 2302 | if (i == null) { |
| 2303 | iter.remove(); |
| 2304 | } |
| 2305 | } |
| 2306 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2307 | // Order the set of items by their containers first, this allows use to walk through the |
| 2308 | // list sequentially, build up a list of containers that are in the specified screen, |
| 2309 | // as well as all items in those containers. |
| 2310 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 2311 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 2312 | @Override |
| 2313 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 2314 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2315 | } |
| 2316 | }); |
| 2317 | for (ItemInfo info : allWorkspaceItems) { |
| 2318 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2319 | if (info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2320 | currentScreenItems.add(info); |
| 2321 | itemsOnScreen.add(info.id); |
| 2322 | } else { |
| 2323 | otherScreenItems.add(info); |
| 2324 | } |
| 2325 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 2326 | currentScreenItems.add(info); |
| 2327 | itemsOnScreen.add(info.id); |
| 2328 | } else { |
| 2329 | if (itemsOnScreen.contains(info.container)) { |
| 2330 | currentScreenItems.add(info); |
| 2331 | itemsOnScreen.add(info.id); |
| 2332 | } else { |
| 2333 | otherScreenItems.add(info); |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | /** Filters the set of widgets which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2340 | private void filterCurrentAppWidgets(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2341 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 2342 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 2343 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2344 | |
| 2345 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2346 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2347 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2348 | widget.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2349 | currentScreenWidgets.add(widget); |
| 2350 | } else { |
| 2351 | otherScreenWidgets.add(widget); |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | /** Filters the set of folders which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2357 | private void filterCurrentFolders(long currentScreenId, |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2358 | LongArrayMap<ItemInfo> itemsIdMap, |
| 2359 | LongArrayMap<FolderInfo> folders, |
| 2360 | LongArrayMap<FolderInfo> currentScreenFolders, |
| 2361 | LongArrayMap<FolderInfo> otherScreenFolders) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2362 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2363 | int total = folders.size(); |
| 2364 | for (int i = 0; i < total; i++) { |
| 2365 | long id = folders.keyAt(i); |
| 2366 | FolderInfo folder = folders.valueAt(i); |
| 2367 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2368 | ItemInfo info = itemsIdMap.get(id); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2369 | if (info == null || folder == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2370 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2371 | info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2372 | currentScreenFolders.put(id, folder); |
| 2373 | } else { |
| 2374 | otherScreenFolders.put(id, folder); |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 2380 | * right) */ |
| 2381 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 2382 | final LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 2383 | final InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2384 | final int screenCols = profile.numColumns; |
| 2385 | final int screenCellCount = profile.numColumns * profile.numRows; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2386 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 2387 | @Override |
| 2388 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2389 | if (lhs.container == rhs.container) { |
| 2390 | // Within containers, order by their spatial position in that container |
| 2391 | switch ((int) lhs.container) { |
| 2392 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: { |
| 2393 | long lr = (lhs.screenId * screenCellCount + |
| 2394 | lhs.cellY * screenCols + lhs.cellX); |
| 2395 | long rr = (rhs.screenId * screenCellCount + |
| 2396 | rhs.cellY * screenCols + rhs.cellX); |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 2397 | return Utilities.longCompare(lr, rr); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2398 | } |
| 2399 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: { |
| 2400 | // We currently use the screen id as the rank |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 2401 | return Utilities.longCompare(lhs.screenId, rhs.screenId); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2402 | } |
| 2403 | default: |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 2404 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2405 | throw new RuntimeException("Unexpected container type when " + |
| 2406 | "sorting workspace items."); |
| 2407 | } |
| 2408 | return 0; |
| 2409 | } |
| 2410 | } else { |
| 2411 | // Between containers, order by hotseat, desktop |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 2412 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 2413 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 2414 | } |
| 2415 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2416 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 2417 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2418 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 2419 | final ArrayList<Long> orderedScreens) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2420 | final Runnable r = new Runnable() { |
| 2421 | @Override |
| 2422 | public void run() { |
| 2423 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2424 | if (callbacks != null) { |
| 2425 | callbacks.bindScreens(orderedScreens); |
| 2426 | } |
| 2427 | } |
| 2428 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 2429 | runOnMainThread(r); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2432 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 2433 | final ArrayList<ItemInfo> workspaceItems, |
| 2434 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2435 | final LongArrayMap<FolderInfo> folders, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2436 | final Executor executor) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2437 | |
| 2438 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 2439 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2440 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2441 | final int start = i; |
| 2442 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2443 | final Runnable r = new Runnable() { |
| 2444 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2445 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 2446 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2447 | if (callbacks != null) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2448 | callbacks.bindItems(workspaceItems, start, start+chunkSize, |
| 2449 | false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2450 | } |
| 2451 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2452 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2453 | executor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2454 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2455 | |
| 2456 | // Bind the folders |
| 2457 | if (!folders.isEmpty()) { |
| 2458 | final Runnable r = new Runnable() { |
| 2459 | public void run() { |
| 2460 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2461 | if (callbacks != null) { |
| 2462 | callbacks.bindFolders(folders); |
| 2463 | } |
| 2464 | } |
| 2465 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2466 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | // Bind the widgets, one at a time |
| 2470 | N = appWidgets.size(); |
| 2471 | for (int i = 0; i < N; i++) { |
| 2472 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 2473 | final Runnable r = new Runnable() { |
| 2474 | public void run() { |
| 2475 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2476 | if (callbacks != null) { |
| 2477 | callbacks.bindAppWidget(widget); |
| 2478 | } |
| 2479 | } |
| 2480 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2481 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | /** |
| 2486 | * Binds all loaded data to actual views on the main thread. |
| 2487 | */ |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2488 | private void bindWorkspace(int synchronizeBindPage) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2489 | final long t = SystemClock.uptimeMillis(); |
| 2490 | Runnable r; |
| 2491 | |
| 2492 | // Don't use these two variables in any of the callback runnables. |
| 2493 | // Otherwise we hold a reference to them. |
| 2494 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2495 | if (oldCallbacks == null) { |
| 2496 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2497 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 2498 | return; |
| 2499 | } |
| 2500 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2501 | // Save a copy of all the bg-thread collections |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2502 | ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(); |
| 2503 | ArrayList<LauncherAppWidgetInfo> appWidgets = |
| 2504 | new ArrayList<LauncherAppWidgetInfo>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2505 | ArrayList<Long> orderedScreenIds = new ArrayList<Long>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2506 | |
| 2507 | final LongArrayMap<FolderInfo> folders; |
| 2508 | final LongArrayMap<ItemInfo> itemsIdMap; |
| 2509 | |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2510 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2511 | workspaceItems.addAll(sBgWorkspaceItems); |
| 2512 | appWidgets.addAll(sBgAppWidgets); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2513 | orderedScreenIds.addAll(sBgWorkspaceScreens); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2514 | |
| 2515 | folders = sBgFolders.clone(); |
| 2516 | itemsIdMap = sBgItemsIdMap.clone(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 2519 | final boolean isLoadingSynchronously = |
| 2520 | synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE; |
Adam Cohen | d8dbb46 | 2013-11-27 11:55:48 -0800 | [diff] [blame] | 2521 | int currScreen = isLoadingSynchronously ? synchronizeBindPage : |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2522 | oldCallbacks.getCurrentWorkspaceScreen(); |
Adam Cohen | d8dbb46 | 2013-11-27 11:55:48 -0800 | [diff] [blame] | 2523 | if (currScreen >= orderedScreenIds.size()) { |
| 2524 | // There may be no workspace screens (just hotseat items and an empty page). |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 2525 | currScreen = PagedView.INVALID_RESTORE_PAGE; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2526 | } |
Adam Cohen | d8dbb46 | 2013-11-27 11:55:48 -0800 | [diff] [blame] | 2527 | final int currentScreen = currScreen; |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 2528 | final long currentScreenId = currentScreen < 0 |
| 2529 | ? INVALID_SCREEN_ID : orderedScreenIds.get(currentScreen); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2530 | |
| 2531 | // Load all the items that are on the current page first (and in the process, unbind |
| 2532 | // all the existing workspace items before we call startBinding() below. |
| 2533 | unbindWorkspaceItemsOnMainThread(); |
| 2534 | |
| 2535 | // Separate the items that are on the current screen, and all the other remaining items |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2536 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>(); |
| 2537 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>(); |
| 2538 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = |
| 2539 | new ArrayList<LauncherAppWidgetInfo>(); |
| 2540 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = |
| 2541 | new ArrayList<LauncherAppWidgetInfo>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2542 | LongArrayMap<FolderInfo> currentFolders = new LongArrayMap<>(); |
| 2543 | LongArrayMap<FolderInfo> otherFolders = new LongArrayMap<>(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2544 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2545 | filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2546 | otherWorkspaceItems); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2547 | filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2548 | otherAppWidgets); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2549 | filterCurrentFolders(currentScreenId, itemsIdMap, folders, currentFolders, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2550 | otherFolders); |
| 2551 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 2552 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 2553 | |
| 2554 | // Tell the workspace that we're about to start binding items |
| 2555 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2556 | public void run() { |
| 2557 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2558 | if (callbacks != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2559 | callbacks.clearPendingBinds(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2560 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2561 | } |
| 2562 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2563 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 2564 | runOnMainThread(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2565 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2566 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 2567 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2568 | Executor mainExecutor = new DeferredMainThreadExecutor(); |
| 2569 | // Load items on the current page. |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2570 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2571 | currentFolders, mainExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2572 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2573 | // In case of isLoadingSynchronously, only bind the first screen, and defer binding the |
| 2574 | // remaining screens after first onDraw is called. This ensures that the first screen |
| 2575 | // is immediately visible (eg. during rotation) |
| 2576 | // In case of !isLoadingSynchronously, bind all pages one after other. |
| 2577 | final Executor deferredExecutor = isLoadingSynchronously ? |
| 2578 | new ViewOnDrawExecutor(mHandler) : mainExecutor; |
| 2579 | |
| 2580 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, |
| 2581 | otherFolders, deferredExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2582 | |
| 2583 | // Tell the workspace that we're done binding items |
| 2584 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2585 | public void run() { |
| 2586 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2587 | if (callbacks != null) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2588 | callbacks.finishBindingItems(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2589 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2590 | |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2591 | mIsLoadingAndBindingWorkspace = false; |
| 2592 | |
| 2593 | // Run all the bind complete runnables after workspace is bound. |
| 2594 | if (!mBindCompleteRunnables.isEmpty()) { |
| 2595 | synchronized (mBindCompleteRunnables) { |
| 2596 | for (final Runnable r : mBindCompleteRunnables) { |
| 2597 | runOnWorkerThread(r); |
| 2598 | } |
| 2599 | mBindCompleteRunnables.clear(); |
| 2600 | } |
| 2601 | } |
| 2602 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 2603 | // If we're profiling, ensure this is the last thing in the queue. |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2604 | if (DEBUG_LOADERS) { |
| 2605 | Log.d(TAG, "bound workspace in " |
| 2606 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 2607 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 2608 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2609 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2610 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2611 | deferredExecutor.execute(r); |
| 2612 | |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2613 | if (isLoadingSynchronously) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2614 | r = new Runnable() { |
| 2615 | public void run() { |
| 2616 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2617 | if (callbacks != null) { |
| 2618 | // We are loading synchronously, which means, some of the pages will be |
| 2619 | // bound after first draw. Inform the callbacks that page binding is |
| 2620 | // not complete, and schedule the remaining pages. |
| 2621 | if (currentScreen != PagedView.INVALID_RESTORE_PAGE) { |
| 2622 | callbacks.onPageBoundSynchronously(currentScreen); |
| 2623 | } |
| 2624 | callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor); |
| 2625 | } |
| 2626 | } |
| 2627 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 2628 | runOnMainThread(r); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2629 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2630 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2631 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2632 | private void loadAndBindAllApps() { |
| 2633 | if (DEBUG_LOADERS) { |
| 2634 | Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded); |
| 2635 | } |
| 2636 | if (!mAllAppsLoaded) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2637 | loadAllApps(); |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 2638 | synchronized (LoaderTask.this) { |
| 2639 | if (mStopped) { |
| 2640 | return; |
| 2641 | } |
| 2642 | } |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2643 | updateIconCache(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 2644 | synchronized (LoaderTask.this) { |
| 2645 | if (mStopped) { |
| 2646 | return; |
| 2647 | } |
| 2648 | mAllAppsLoaded = true; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2649 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2650 | } else { |
| 2651 | onlyBindAllApps(); |
| 2652 | } |
| 2653 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2654 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2655 | private void updateIconCache() { |
| 2656 | // Ignore packages which have a promise icon. |
| 2657 | HashSet<String> packagesToIgnore = new HashSet<>(); |
| 2658 | synchronized (sBgLock) { |
| 2659 | for (ItemInfo info : sBgItemsIdMap) { |
| 2660 | if (info instanceof ShortcutInfo) { |
| 2661 | ShortcutInfo si = (ShortcutInfo) info; |
| 2662 | if (si.isPromise() && si.getTargetComponent() != null) { |
| 2663 | packagesToIgnore.add(si.getTargetComponent().getPackageName()); |
| 2664 | } |
| 2665 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 2666 | LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info; |
| 2667 | if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) { |
| 2668 | packagesToIgnore.add(lawi.providerName.getPackageName()); |
| 2669 | } |
| 2670 | } |
| 2671 | } |
| 2672 | } |
| 2673 | mIconCache.updateDbIcons(packagesToIgnore); |
| 2674 | } |
| 2675 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2676 | private void onlyBindAllApps() { |
| 2677 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2678 | if (oldCallbacks == null) { |
| 2679 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2680 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 2681 | return; |
| 2682 | } |
| 2683 | |
| 2684 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 2685 | @SuppressWarnings("unchecked") |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2686 | final ArrayList<AppInfo> list |
| 2687 | = (ArrayList<AppInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2688 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2689 | public void run() { |
| 2690 | final long t = SystemClock.uptimeMillis(); |
| 2691 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2692 | if (callbacks != null) { |
| 2693 | callbacks.bindAllApplications(list); |
| 2694 | } |
| 2695 | if (DEBUG_LOADERS) { |
| 2696 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
Hyunyoung Song | 747a5bc | 2016-02-08 11:31:33 -0800 | [diff] [blame] | 2697 | + (SystemClock.uptimeMillis() - t) + "ms"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2698 | } |
| 2699 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2700 | }; |
| 2701 | boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid()); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2702 | if (isRunningOnMainThread) { |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2703 | r.run(); |
| 2704 | } else { |
| 2705 | mHandler.post(r); |
| 2706 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2707 | } |
| 2708 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2709 | private void loadAllApps() { |
| 2710 | final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2711 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2712 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2713 | if (oldCallbacks == null) { |
| 2714 | // This launcher has exited and nobody bothered to tell us. Just bail. |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2715 | Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2716 | return; |
| 2717 | } |
| 2718 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2719 | final List<UserHandleCompat> profiles = mUserManager.getUserProfiles(); |
| 2720 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2721 | // Clear the list of apps |
| 2722 | mBgAllAppsList.clear(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2723 | for (UserHandleCompat user : profiles) { |
| 2724 | // Query for the set of apps |
| 2725 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2726 | final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2727 | if (DEBUG_LOADERS) { |
| 2728 | Log.d(TAG, "getActivityList took " |
| 2729 | + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user); |
| 2730 | Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user); |
| 2731 | } |
| 2732 | // Fail if we don't have any apps |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2733 | // TODO: Fix this. Only fail for the current user. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2734 | if (apps == null || apps.isEmpty()) { |
| 2735 | return; |
| 2736 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 2737 | boolean quietMode = mUserManager.isQuietModeEnabled(user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2738 | // Create the ApplicationInfos |
| 2739 | for (int i = 0; i < apps.size(); i++) { |
| 2740 | LauncherActivityInfoCompat app = apps.get(i); |
| 2741 | // This builds the icon bitmaps. |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 2742 | mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2743 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2744 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2745 | final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user); |
| 2746 | if (heuristic != null) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2747 | final Runnable r = new Runnable() { |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2748 | |
| 2749 | @Override |
| 2750 | public void run() { |
| 2751 | heuristic.processUserApps(apps); |
| 2752 | } |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2753 | }; |
| 2754 | runOnMainThread(new Runnable() { |
| 2755 | |
| 2756 | @Override |
| 2757 | public void run() { |
| 2758 | // Check isLoadingWorkspace on the UI thread, as it is updated on |
| 2759 | // the UI thread. |
| 2760 | if (mIsLoadingAndBindingWorkspace) { |
| 2761 | synchronized (mBindCompleteRunnables) { |
| 2762 | mBindCompleteRunnables.add(r); |
| 2763 | } |
| 2764 | } else { |
| 2765 | runOnWorkerThread(r); |
| 2766 | } |
| 2767 | } |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2768 | }); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2769 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2770 | } |
Bjorn Bringert | 85f418d | 2013-09-06 12:50:05 +0100 | [diff] [blame] | 2771 | // Huh? Shouldn't this be inside the Runnable below? |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2772 | final ArrayList<AppInfo> added = mBgAllAppsList.added; |
| 2773 | mBgAllAppsList.added = new ArrayList<AppInfo>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2774 | |
| 2775 | // Post callback on main thread |
| 2776 | mHandler.post(new Runnable() { |
| 2777 | public void run() { |
Hyunyoung Song | 9892e58 | 2015-05-05 10:07:23 -0700 | [diff] [blame] | 2778 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2779 | final long bindTime = SystemClock.uptimeMillis(); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 2780 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2781 | if (callbacks != null) { |
| 2782 | callbacks.bindAllApplications(added); |
| 2783 | if (DEBUG_LOADERS) { |
| 2784 | Log.d(TAG, "bound " + added.size() + " apps in " |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2785 | + (SystemClock.uptimeMillis() - bindTime) + "ms"); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2786 | } |
| 2787 | } else { |
| 2788 | Log.i(TAG, "not binding apps: no Launcher activity"); |
| 2789 | } |
| 2790 | } |
| 2791 | }); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2792 | // Cleanup any data stored for a deleted user. |
| 2793 | ManagedProfileHeuristic.processAllUsers(profiles, mContext); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2794 | if (DEBUG_LOADERS) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2795 | Log.d(TAG, "Icons processed in " |
| 2796 | + (SystemClock.uptimeMillis() - loadTime) + "ms"); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | public void dumpState() { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2801 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2802 | Log.d(TAG, "mLoaderTask.mContext=" + mContext); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2803 | Log.d(TAG, "mLoaderTask.mStopped=" + mStopped); |
| 2804 | Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished); |
| 2805 | Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size()); |
| 2806 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 2810 | /** |
| 2811 | * Called when the icons for packages have been updated in the icon cache. |
| 2812 | */ |
| 2813 | public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) { |
| 2814 | final Callbacks callbacks = getCallback(); |
| 2815 | final ArrayList<AppInfo> updatedApps = new ArrayList<>(); |
| 2816 | final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<>(); |
| 2817 | |
| 2818 | // If any package icon has changed (app was updated while launcher was dead), |
| 2819 | // update the corresponding shortcuts. |
| 2820 | synchronized (sBgLock) { |
| 2821 | for (ItemInfo info : sBgItemsIdMap) { |
| 2822 | if (info instanceof ShortcutInfo && user.equals(info.user) |
| 2823 | && info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
| 2824 | ShortcutInfo si = (ShortcutInfo) info; |
| 2825 | ComponentName cn = si.getTargetComponent(); |
| 2826 | if (cn != null && updatedPackages.contains(cn.getPackageName())) { |
| 2827 | si.updateIcon(mIconCache); |
| 2828 | updatedShortcuts.add(si); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | mBgAllAppsList.updateIconsAndLabels(updatedPackages, user, updatedApps); |
| 2833 | } |
| 2834 | |
| 2835 | if (!updatedShortcuts.isEmpty()) { |
| 2836 | final UserHandleCompat userFinal = user; |
| 2837 | mHandler.post(new Runnable() { |
| 2838 | |
| 2839 | public void run() { |
| 2840 | Callbacks cb = getCallback(); |
| 2841 | if (cb != null && callbacks == cb) { |
| 2842 | cb.bindShortcutsChanged(updatedShortcuts, |
| 2843 | new ArrayList<ShortcutInfo>(), userFinal); |
| 2844 | } |
| 2845 | } |
| 2846 | }); |
| 2847 | } |
| 2848 | |
| 2849 | if (!updatedApps.isEmpty()) { |
| 2850 | mHandler.post(new Runnable() { |
| 2851 | |
| 2852 | public void run() { |
| 2853 | Callbacks cb = getCallback(); |
| 2854 | if (cb != null && callbacks == cb) { |
| 2855 | cb.bindAppsUpdated(updatedApps); |
| 2856 | } |
| 2857 | } |
| 2858 | }); |
| 2859 | } |
| 2860 | } |
| 2861 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2862 | void enqueuePackageUpdated(PackageUpdatedTask task) { |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 2863 | sWorker.post(task); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2864 | } |
| 2865 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2866 | @Thunk class AppsAvailabilityCheck extends BroadcastReceiver { |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2867 | |
| 2868 | @Override |
| 2869 | public void onReceive(Context context, Intent intent) { |
| 2870 | synchronized (sBgLock) { |
| 2871 | final LauncherAppsCompat launcherApps = LauncherAppsCompat |
| 2872 | .getInstance(mApp.getContext()); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2873 | final PackageManager manager = context.getPackageManager(); |
| 2874 | final ArrayList<String> packagesRemoved = new ArrayList<String>(); |
| 2875 | final ArrayList<String> packagesUnavailable = new ArrayList<String>(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2876 | for (Entry<UserHandleCompat, HashSet<String>> entry : sPendingPackages.entrySet()) { |
| 2877 | UserHandleCompat user = entry.getKey(); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2878 | packagesRemoved.clear(); |
| 2879 | packagesUnavailable.clear(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2880 | for (String pkg : entry.getValue()) { |
| 2881 | if (!launcherApps.isPackageEnabledForProfile(pkg, user)) { |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2882 | boolean packageOnSdcard = launcherApps.isAppEnabled( |
| 2883 | manager, pkg, PackageManager.GET_UNINSTALLED_PACKAGES); |
| 2884 | if (packageOnSdcard) { |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2885 | packagesUnavailable.add(pkg); |
| 2886 | } else { |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2887 | packagesRemoved.add(pkg); |
| 2888 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | if (!packagesRemoved.isEmpty()) { |
| 2892 | enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_REMOVE, |
| 2893 | packagesRemoved.toArray(new String[packagesRemoved.size()]), user)); |
| 2894 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2895 | if (!packagesUnavailable.isEmpty()) { |
| 2896 | enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_UNAVAILABLE, |
| 2897 | packagesUnavailable.toArray(new String[packagesUnavailable.size()]), user)); |
| 2898 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2899 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2900 | sPendingPackages.clear(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2905 | private class PackageUpdatedTask implements Runnable { |
| 2906 | int mOp; |
| 2907 | String[] mPackages; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2908 | UserHandleCompat mUser; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2909 | |
| 2910 | public static final int OP_NONE = 0; |
| 2911 | public static final int OP_ADD = 1; |
| 2912 | public static final int OP_UPDATE = 2; |
| 2913 | public static final int OP_REMOVE = 3; // uninstlled |
| 2914 | public static final int OP_UNAVAILABLE = 4; // external media unmounted |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 2915 | public static final int OP_SUSPEND = 5; // package suspended |
| 2916 | public static final int OP_UNSUSPEND = 6; // package unsuspended |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2917 | |
| 2918 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2919 | public PackageUpdatedTask(int op, String[] packages, UserHandleCompat user) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2920 | mOp = op; |
| 2921 | mPackages = packages; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2922 | mUser = user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | public void run() { |
Sunny Goyal | c905efc | 2015-05-06 09:54:53 -0700 | [diff] [blame] | 2926 | if (!mHasLoaderCompletedOnce) { |
| 2927 | // Loader has not yet run. |
| 2928 | return; |
| 2929 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 2930 | final Context context = mApp.getContext(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2931 | |
| 2932 | final String[] packages = mPackages; |
| 2933 | final int N = packages.length; |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2934 | FlagOp flagOp = FlagOp.NO_OP; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2935 | switch (mOp) { |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2936 | case OP_ADD: { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2937 | for (int i=0; i<N; i++) { |
| 2938 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2939 | mIconCache.updateIconsForPkg(packages[i], mUser); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2940 | mBgAllAppsList.addPackage(context, packages[i], mUser); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2941 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2942 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2943 | ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser); |
| 2944 | if (heuristic != null) { |
| 2945 | heuristic.processPackageAdd(mPackages); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2946 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2947 | break; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2948 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2949 | case OP_UPDATE: |
| 2950 | for (int i=0; i<N; i++) { |
| 2951 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2952 | mIconCache.updateIconsForPkg(packages[i], mUser); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2953 | mBgAllAppsList.updatePackage(context, packages[i], mUser); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 2954 | mApp.getWidgetCache().removePackage(packages[i], mUser); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2955 | } |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2956 | // Since package was just updated, the target must be available now. |
| 2957 | flagOp = FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2958 | break; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2959 | case OP_REMOVE: { |
| 2960 | ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(context, mUser); |
| 2961 | if (heuristic != null) { |
| 2962 | heuristic.processPackageRemoved(mPackages); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2963 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2964 | for (int i=0; i<N; i++) { |
| 2965 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2966 | mIconCache.removeIconsForPkg(packages[i], mUser); |
| 2967 | } |
| 2968 | // Fall through |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2969 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2970 | case OP_UNAVAILABLE: |
| 2971 | for (int i=0; i<N; i++) { |
| 2972 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]); |
| 2973 | mBgAllAppsList.removePackage(packages[i], mUser); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 2974 | mApp.getWidgetCache().removePackage(packages[i], mUser); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2975 | } |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2976 | flagOp = FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2977 | break; |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 2978 | case OP_SUSPEND: |
| 2979 | case OP_UNSUSPEND: |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2980 | flagOp = mOp == OP_SUSPEND ? |
| 2981 | FlagOp.addFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED) : |
| 2982 | FlagOp.removeFlag(ShortcutInfo.FLAG_DISABLED_SUSPENDED); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 2983 | for (int i=0; i<N; i++) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2984 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.(un)suspend " + packages[i]); |
| 2985 | mBgAllAppsList.updatePackageFlags(packages[i], mUser, flagOp); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 2986 | } |
| 2987 | break; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2988 | } |
| 2989 | |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2990 | ArrayList<AppInfo> added = null; |
| 2991 | ArrayList<AppInfo> modified = null; |
| 2992 | final ArrayList<AppInfo> removedApps = new ArrayList<AppInfo>(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2993 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2994 | if (mBgAllAppsList.added.size() > 0) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2995 | added = new ArrayList<>(mBgAllAppsList.added); |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 2996 | mBgAllAppsList.added.clear(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2997 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2998 | if (mBgAllAppsList.modified.size() > 0) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 2999 | modified = new ArrayList<>(mBgAllAppsList.modified); |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 3000 | mBgAllAppsList.modified.clear(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3001 | } |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 3002 | if (mBgAllAppsList.removed.size() > 0) { |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 3003 | removedApps.addAll(mBgAllAppsList.removed); |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 3004 | mBgAllAppsList.removed.clear(); |
Winson Chung | cd81073 | 2012-06-18 16:45:43 -0700 | [diff] [blame] | 3005 | } |
| 3006 | |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3007 | final HashMap<ComponentName, AppInfo> addedOrUpdatedApps = new HashMap<>(); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3008 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3009 | if (added != null) { |
Sunny Goyal | c9acdd5 | 2015-02-26 12:34:42 -0800 | [diff] [blame] | 3010 | addAppsToAllApps(context, added); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3011 | for (AppInfo ai : added) { |
| 3012 | addedOrUpdatedApps.put(ai.componentName, ai); |
| 3013 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3014 | } |
Adam Cohen | 76a47a1 | 2014-02-05 11:47:43 -0800 | [diff] [blame] | 3015 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3016 | if (modified != null) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3017 | final Callbacks callbacks = getCallback(); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 3018 | final ArrayList<AppInfo> modifiedFinal = modified; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3019 | for (AppInfo ai : modified) { |
| 3020 | addedOrUpdatedApps.put(ai.componentName, ai); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3023 | mHandler.post(new Runnable() { |
| 3024 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 3025 | Callbacks cb = getCallback(); |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 3026 | if (callbacks == cb && cb != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3027 | callbacks.bindAppsUpdated(modifiedFinal); |
| 3028 | } |
| 3029 | } |
| 3030 | }); |
| 3031 | } |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 3032 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3033 | // Update shortcut infos |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3034 | if (mOp == OP_ADD || flagOp != FlagOp.NO_OP) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3035 | final ArrayList<ShortcutInfo> updatedShortcuts = new ArrayList<ShortcutInfo>(); |
| 3036 | final ArrayList<ShortcutInfo> removedShortcuts = new ArrayList<ShortcutInfo>(); |
| 3037 | final ArrayList<LauncherAppWidgetInfo> widgets = new ArrayList<LauncherAppWidgetInfo>(); |
| 3038 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3039 | HashSet<String> packageSet = new HashSet<String>(Arrays.asList(packages)); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3040 | synchronized (sBgLock) { |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 3041 | for (ItemInfo info : sBgItemsIdMap) { |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3042 | if (info instanceof ShortcutInfo && mUser.equals(info.user)) { |
| 3043 | ShortcutInfo si = (ShortcutInfo) info; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3044 | boolean infoUpdated = false; |
| 3045 | boolean shortcutUpdated = false; |
| 3046 | |
| 3047 | // Update shortcuts which use iconResource. |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3048 | if ((si.iconResource != null) |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3049 | && packageSet.contains(si.iconResource.packageName)) { |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 3050 | Bitmap icon = Utilities.createIconBitmap( |
| 3051 | si.iconResource.packageName, |
| 3052 | si.iconResource.resourceName, context); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3053 | if (icon != null) { |
| 3054 | si.setIcon(icon); |
| 3055 | si.usingFallbackIcon = false; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3056 | infoUpdated = true; |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3059 | |
| 3060 | ComponentName cn = si.getTargetComponent(); |
| 3061 | if (cn != null && packageSet.contains(cn.getPackageName())) { |
| 3062 | AppInfo appInfo = addedOrUpdatedApps.get(cn); |
| 3063 | |
| 3064 | if (si.isPromise()) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3065 | if (si.hasStatusFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 3066 | // Auto install icon |
| 3067 | PackageManager pm = context.getPackageManager(); |
| 3068 | ResolveInfo matched = pm.resolveActivity( |
| 3069 | new Intent(Intent.ACTION_MAIN) |
| 3070 | .setComponent(cn).addCategory(Intent.CATEGORY_LAUNCHER), |
| 3071 | PackageManager.MATCH_DEFAULT_ONLY); |
| 3072 | if (matched == null) { |
| 3073 | // Try to find the best match activity. |
| 3074 | Intent intent = pm.getLaunchIntentForPackage( |
| 3075 | cn.getPackageName()); |
| 3076 | if (intent != null) { |
| 3077 | cn = intent.getComponent(); |
| 3078 | appInfo = addedOrUpdatedApps.get(cn); |
| 3079 | } |
| 3080 | |
| 3081 | if ((intent == null) || (appInfo == null)) { |
| 3082 | removedShortcuts.add(si); |
| 3083 | continue; |
| 3084 | } |
| 3085 | si.promisedIntent = intent; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | // Restore the shortcut. |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 3090 | if (appInfo != null) { |
| 3091 | si.flags = appInfo.flags; |
| 3092 | } |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3093 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3094 | si.intent = si.promisedIntent; |
| 3095 | si.promisedIntent = null; |
| 3096 | si.status = ShortcutInfo.DEFAULT; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3097 | infoUpdated = true; |
| 3098 | si.updateIcon(mIconCache); |
| 3099 | } |
| 3100 | |
| 3101 | if (appInfo != null && Intent.ACTION_MAIN.equals(si.intent.getAction()) |
| 3102 | && si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
| 3103 | si.updateIcon(mIconCache); |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3104 | si.title = Utilities.trim(appInfo.title); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3105 | si.contentDescription = appInfo.contentDescription; |
| 3106 | infoUpdated = true; |
| 3107 | } |
| 3108 | |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3109 | int oldDisabledFlags = si.isDisabled; |
| 3110 | si.isDisabled = flagOp.apply(si.isDisabled); |
| 3111 | if (si.isDisabled != oldDisabledFlags) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3112 | shortcutUpdated = true; |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | if (infoUpdated || shortcutUpdated) { |
| 3117 | updatedShortcuts.add(si); |
| 3118 | } |
| 3119 | if (infoUpdated) { |
| 3120 | updateItemInDatabase(context, si); |
| 3121 | } |
| 3122 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 3123 | LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) info; |
| 3124 | if (mUser.equals(widgetInfo.user) |
| 3125 | && widgetInfo.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) |
| 3126 | && packageSet.contains(widgetInfo.providerName.getPackageName())) { |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 3127 | widgetInfo.restoreStatus &= |
| 3128 | ~LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY & |
| 3129 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | 84b4adc | 2015-08-12 15:12:16 -0700 | [diff] [blame] | 3130 | |
| 3131 | // adding this flag ensures that launcher shows 'click to setup' |
| 3132 | // if the widget has a config activity. In case there is no config |
| 3133 | // activity, it will be marked as 'restored' during bind. |
| 3134 | widgetInfo.restoreStatus |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
| 3135 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3136 | widgets.add(widgetInfo); |
| 3137 | updateItemInDatabase(context, widgetInfo); |
| 3138 | } |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | } |
| 3142 | |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3143 | if (!updatedShortcuts.isEmpty() || !removedShortcuts.isEmpty()) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3144 | final Callbacks callbacks = getCallback(); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3145 | mHandler.post(new Runnable() { |
| 3146 | |
| 3147 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 3148 | Callbacks cb = getCallback(); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3149 | if (callbacks == cb && cb != null) { |
| 3150 | callbacks.bindShortcutsChanged( |
| 3151 | updatedShortcuts, removedShortcuts, mUser); |
| 3152 | } |
| 3153 | } |
| 3154 | }); |
| 3155 | if (!removedShortcuts.isEmpty()) { |
| 3156 | deleteItemsFromDatabase(context, removedShortcuts); |
| 3157 | } |
| 3158 | } |
| 3159 | if (!widgets.isEmpty()) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3160 | final Callbacks callbacks = getCallback(); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3161 | mHandler.post(new Runnable() { |
| 3162 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 3163 | Callbacks cb = getCallback(); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3164 | if (callbacks == cb && cb != null) { |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 3165 | callbacks.bindWidgetsRestored(widgets); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | }); |
| 3169 | } |
| 3170 | } |
| 3171 | |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3172 | final HashSet<String> removedPackages = new HashSet<>(); |
| 3173 | final HashSet<ComponentName> removedComponents = new HashSet<>(); |
| 3174 | if (mOp == OP_REMOVE) { |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3175 | // Mark all packages in the broadcast to be removed |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3176 | Collections.addAll(removedPackages, packages); |
| 3177 | |
| 3178 | // No need to update the removedComponents as |
| 3179 | // removedPackages is a super-set of removedComponents |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3180 | } else if (mOp == OP_UPDATE) { |
| 3181 | // Mark disabled packages in the broadcast to be removed |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3182 | for (int i=0; i<N; i++) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3183 | if (isPackageDisabled(context, packages[i], mUser)) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3184 | removedPackages.add(packages[i]); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3185 | } |
| 3186 | } |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3187 | |
| 3188 | // Update removedComponents as some components can get removed during package update |
| 3189 | for (AppInfo info : removedApps) { |
| 3190 | removedComponents.add(info.componentName); |
| 3191 | } |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3192 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 3193 | |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3194 | if (!removedPackages.isEmpty() || !removedComponents.isEmpty()) { |
| 3195 | for (String pn : removedPackages) { |
| 3196 | deletePackageFromDatabase(context, pn, mUser); |
| 3197 | } |
| 3198 | for (ComponentName cn : removedComponents) { |
| 3199 | deleteItemsFromDatabase(context, getItemInfoForComponentName(cn, mUser)); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3202 | // Remove any queued items from the install queue |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3203 | InstallShortcutReceiver.removeFromInstallQueue(context, removedPackages, mUser); |
| 3204 | |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3205 | // Call the components-removed callback |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3206 | final Callbacks callbacks = getCallback(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3207 | mHandler.post(new Runnable() { |
| 3208 | public void run() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 3209 | Callbacks cb = getCallback(); |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 3210 | if (callbacks == cb && cb != null) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3211 | callbacks.bindWorkspaceComponentsRemoved( |
| 3212 | removedPackages, removedComponents, mUser); |
| 3213 | } |
| 3214 | } |
| 3215 | }); |
| 3216 | } |
| 3217 | |
| 3218 | if (!removedApps.isEmpty()) { |
| 3219 | // Remove corresponding apps from All-Apps |
| 3220 | final Callbacks callbacks = getCallback(); |
| 3221 | mHandler.post(new Runnable() { |
| 3222 | public void run() { |
| 3223 | Callbacks cb = getCallback(); |
| 3224 | if (callbacks == cb && cb != null) { |
| 3225 | callbacks.bindAppInfosRemoved(removedApps); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3226 | } |
| 3227 | } |
| 3228 | }); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 3229 | } |
Hyunyoung Song | 227239e | 2015-05-04 18:17:35 -0700 | [diff] [blame] | 3230 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3231 | // Notify launcher of widget update. From marshmallow onwards we use AppWidgetHost to |
| 3232 | // get widget update signals. |
| 3233 | if (!Utilities.ATLEAST_MARSHMALLOW && |
| 3234 | (mOp == OP_ADD || mOp == OP_REMOVE || mOp == OP_UPDATE)) { |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 3235 | final Callbacks callbacks = getCallback(); |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 3236 | mHandler.post(new Runnable() { |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 3237 | public void run() { |
| 3238 | Callbacks cb = getCallback(); |
| 3239 | if (callbacks == cb && cb != null) { |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3240 | callbacks.notifyWidgetProvidersChanged(); |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | }); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | private void bindWidgetsModel(final Callbacks callbacks, final WidgetsModel model) { |
| 3249 | mHandler.post(new Runnable() { |
| 3250 | @Override |
| 3251 | public void run() { |
| 3252 | Callbacks cb = getCallback(); |
| 3253 | if (callbacks == cb && cb != null) { |
| 3254 | callbacks.bindWidgetsModel(model); |
| 3255 | } |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 3256 | } |
| 3257 | }); |
| 3258 | } |
| 3259 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3260 | public void refreshAndBindWidgetsAndShortcuts( |
| 3261 | final Callbacks callbacks, final boolean bindFirst) { |
| 3262 | runOnWorkerThread(new Runnable() { |
| 3263 | @Override |
| 3264 | public void run() { |
| 3265 | if (bindFirst && !mBgWidgetsModel.isEmpty()) { |
| 3266 | bindWidgetsModel(callbacks, mBgWidgetsModel.clone()); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 3267 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3268 | final WidgetsModel model = mBgWidgetsModel.updateAndClone(mApp.getContext()); |
| 3269 | bindWidgetsModel(callbacks, model); |
| 3270 | // update the Widget entries inside DB on the worker thread. |
| 3271 | LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews( |
| 3272 | model.getRawList()); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 3273 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 3274 | }); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 3275 | } |
| 3276 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 3277 | @Thunk static boolean isPackageDisabled(Context context, String packageName, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3278 | UserHandleCompat user) { |
| 3279 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 3280 | return !launcherApps.isPackageEnabledForProfile(packageName, user); |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3281 | } |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 3282 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3283 | public static boolean isValidPackageActivity(Context context, ComponentName cn, |
| 3284 | UserHandleCompat user) { |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 3285 | if (cn == null) { |
| 3286 | return false; |
| 3287 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3288 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 3289 | if (!launcherApps.isPackageEnabledForProfile(cn.getPackageName(), user)) { |
Winson Chung | df95eb1 | 2013-10-16 14:57:07 -0700 | [diff] [blame] | 3290 | return false; |
| 3291 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3292 | return launcherApps.isActivityEnabledForProfile(cn, user); |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Adam Cohen | a28b78e | 2014-05-20 17:03:04 -0700 | [diff] [blame] | 3295 | public static boolean isValidPackage(Context context, String packageName, |
| 3296 | UserHandleCompat user) { |
| 3297 | if (packageName == null) { |
| 3298 | return false; |
| 3299 | } |
| 3300 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
| 3301 | return launcherApps.isPackageEnabledForProfile(packageName, user); |
| 3302 | } |
| 3303 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3304 | /** |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3305 | * Make an ShortcutInfo object for a restored application or shortcut item that points |
| 3306 | * to a package that is not yet installed on the system. |
| 3307 | */ |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3308 | public ShortcutInfo getRestoredItemInfo(Cursor c, int titleIndex, Intent intent, |
Sunny Goyal | c22841b | 2015-07-13 19:59:50 -0700 | [diff] [blame] | 3309 | int promiseType, int itemType, CursorIconInfo iconInfo, Context context) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3310 | final ShortcutInfo info = new ShortcutInfo(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3311 | info.user = UserHandleCompat.myUserHandle(); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3312 | |
| 3313 | Bitmap icon = iconInfo.loadIcon(c, info, context); |
| 3314 | // the fallback icon |
| 3315 | if (icon == null) { |
| 3316 | mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */); |
| 3317 | } else { |
| 3318 | info.setIcon(icon); |
| 3319 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3320 | |
| 3321 | if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3322 | String title = (c != null) ? c.getString(titleIndex) : null; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3323 | if (!TextUtils.isEmpty(title)) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3324 | info.title = Utilities.trim(title); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3325 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3326 | } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) { |
| 3327 | if (TextUtils.isEmpty(info.title)) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3328 | info.title = (c != null) ? Utilities.trim(c.getString(titleIndex)) : ""; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3329 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3330 | } else { |
| 3331 | throw new InvalidParameterException("Invalid restoreType " + promiseType); |
| 3332 | } |
| 3333 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3334 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Sunny Goyal | c22841b | 2015-07-13 19:59:50 -0700 | [diff] [blame] | 3335 | info.itemType = itemType; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3336 | info.promisedIntent = intent; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3337 | info.status = promiseType; |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3338 | return info; |
| 3339 | } |
| 3340 | |
| 3341 | /** |
| 3342 | * Make an Intent object for a restored application or shortcut item that points |
| 3343 | * to the market page for the item. |
| 3344 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 3345 | @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3346 | ComponentName componentName = intent.getComponent(); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 3347 | return getMarketIntent(componentName.getPackageName()); |
| 3348 | } |
| 3349 | |
| 3350 | static Intent getMarketIntent(String packageName) { |
| 3351 | return new Intent(Intent.ACTION_VIEW) |
| 3352 | .setData(new Uri.Builder() |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3353 | .scheme("market") |
| 3354 | .authority("details") |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 3355 | .appendQueryParameter("id", packageName) |
| 3356 | .build()); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3360 | * Make an ShortcutInfo object for a shortcut that is an application. |
| 3361 | * |
| 3362 | * If c is not null, then it will be used to fill in missing data like the title and icon. |
| 3363 | */ |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 3364 | public ShortcutInfo getAppShortcutInfo(PackageManager manager, Intent intent, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3365 | UserHandleCompat user, Context context, Cursor c, int iconIndex, int titleIndex, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 3366 | boolean allowMissingTarget, boolean useLowResIcon) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3367 | if (user == null) { |
| 3368 | Log.d(TAG, "Null user found in getShortcutInfo"); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3369 | return null; |
| 3370 | } |
| 3371 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3372 | ComponentName componentName = intent.getComponent(); |
| 3373 | if (componentName == null) { |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 3374 | Log.d(TAG, "Missing component found in getShortcutInfo"); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3375 | return null; |
| 3376 | } |
| 3377 | |
| 3378 | Intent newIntent = new Intent(intent.getAction(), null); |
| 3379 | newIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 3380 | newIntent.setComponent(componentName); |
| 3381 | LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 3382 | if ((lai == null) && !allowMissingTarget) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3383 | Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName); |
| 3384 | return null; |
| 3385 | } |
| 3386 | |
| 3387 | final ShortcutInfo info = new ShortcutInfo(); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 3388 | mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 3389 | if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) { |
| 3390 | Bitmap icon = Utilities.createIconBitmap(c, iconIndex, context); |
| 3391 | info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3392 | } |
| 3393 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3394 | // from the db |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 3395 | if (TextUtils.isEmpty(info.title) && c != null) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3396 | info.title = Utilities.trim(c.getString(titleIndex)); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3397 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 3398 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3399 | // fall back to the class name of the activity |
| 3400 | if (info.title == null) { |
| 3401 | info.title = componentName.getClassName(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 3402 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 3403 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3404 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3405 | info.user = user; |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3406 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 3407 | if (lai != null) { |
| 3408 | info.flags = AppInfo.initFlags(lai); |
| 3409 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3410 | return info; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3411 | } |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 3412 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 3413 | static ArrayList<ItemInfo> filterItemInfos(Iterable<ItemInfo> infos, |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3414 | ItemInfoFilter f) { |
| 3415 | HashSet<ItemInfo> filtered = new HashSet<ItemInfo>(); |
| 3416 | for (ItemInfo i : infos) { |
| 3417 | if (i instanceof ShortcutInfo) { |
| 3418 | ShortcutInfo info = (ShortcutInfo) i; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3419 | ComponentName cn = info.getTargetComponent(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3420 | if (cn != null && f.filterItem(null, info, cn)) { |
| 3421 | filtered.add(info); |
| 3422 | } |
| 3423 | } else if (i instanceof FolderInfo) { |
| 3424 | FolderInfo info = (FolderInfo) i; |
| 3425 | for (ShortcutInfo s : info.contents) { |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 3426 | ComponentName cn = s.getTargetComponent(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3427 | if (cn != null && f.filterItem(info, s, cn)) { |
| 3428 | filtered.add(s); |
Winson Chung | 8a43510 | 2012-08-30 17:16:53 -0700 | [diff] [blame] | 3429 | } |
| 3430 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3431 | } else if (i instanceof LauncherAppWidgetInfo) { |
| 3432 | LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) i; |
| 3433 | ComponentName cn = info.providerName; |
| 3434 | if (cn != null && f.filterItem(null, info, cn)) { |
| 3435 | filtered.add(info); |
| 3436 | } |
Winson Chung | 8a43510 | 2012-08-30 17:16:53 -0700 | [diff] [blame] | 3437 | } |
| 3438 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3439 | return new ArrayList<ItemInfo>(filtered); |
| 3440 | } |
| 3441 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 3442 | @Thunk ArrayList<ItemInfo> getItemInfoForComponentName(final ComponentName cname, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3443 | final UserHandleCompat user) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3444 | ItemInfoFilter filter = new ItemInfoFilter() { |
| 3445 | @Override |
| 3446 | public boolean filterItem(ItemInfo parent, ItemInfo info, ComponentName cn) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3447 | if (info.user == null) { |
| 3448 | return cn.equals(cname); |
| 3449 | } else { |
| 3450 | return cn.equals(cname) && info.user.equals(user); |
| 3451 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3452 | } |
| 3453 | }; |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 3454 | return filterItemInfos(sBgItemsIdMap, filter); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 3457 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3458 | * Make an ShortcutInfo object for a shortcut that isn't an application. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3459 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 3460 | @Thunk ShortcutInfo getShortcutInfo(Cursor c, Context context, |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3461 | int titleIndex, CursorIconInfo iconInfo) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 3462 | final ShortcutInfo info = new ShortcutInfo(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3463 | // Non-app shortcuts are only supported for current user. |
| 3464 | info.user = UserHandleCompat.myUserHandle(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3465 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3466 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 3467 | // TODO: If there's an explicit component and we can't install that, delete it. |
| 3468 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3469 | info.title = Utilities.trim(c.getString(titleIndex)); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3470 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 3471 | Bitmap icon = iconInfo.loadIcon(c, info, context); |
| 3472 | // the fallback icon |
| 3473 | if (icon == null) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3474 | icon = mIconCache.getDefaultIcon(info.user); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3475 | info.usingFallbackIcon = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3476 | } |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 3477 | info.setIcon(icon); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3478 | return info; |
| 3479 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3480 | |
Sunny Goyal | 2350bc9 | 2014-10-14 16:42:54 -0700 | [diff] [blame] | 3481 | ShortcutInfo infoFromShortcutIntent(Context context, Intent data) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3482 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 3483 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 3484 | Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 3485 | |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 3486 | if (intent == null) { |
| 3487 | // If the intent is null, we can't construct a valid ShortcutInfo, so we return null |
| 3488 | Log.e(TAG, "Can't construct ShorcutInfo with null intent"); |
| 3489 | return null; |
| 3490 | } |
| 3491 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3492 | Bitmap icon = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3493 | boolean customIcon = false; |
| 3494 | ShortcutIconResource iconResource = null; |
| 3495 | |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 3496 | if (bitmap instanceof Bitmap) { |
| 3497 | icon = Utilities.createIconBitmap((Bitmap) bitmap, context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3498 | customIcon = true; |
| 3499 | } else { |
| 3500 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 3501 | if (extra instanceof ShortcutIconResource) { |
| 3502 | iconResource = (ShortcutIconResource) extra; |
| 3503 | icon = Utilities.createIconBitmap(iconResource.packageName, |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 3504 | iconResource.resourceName, context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 3508 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3509 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 3510 | // Only support intents for current user for now. Intents sent from other |
| 3511 | // users wouldn't get here without intent forwarding anyway. |
| 3512 | info.user = UserHandleCompat.myUserHandle(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3513 | if (icon == null) { |
Sunny Goyal | 2350bc9 | 2014-10-14 16:42:54 -0700 | [diff] [blame] | 3514 | icon = mIconCache.getDefaultIcon(info.user); |
| 3515 | info.usingFallbackIcon = true; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3516 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3517 | info.setIcon(icon); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 3518 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 3519 | info.title = Utilities.trim(name); |
| 3520 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 3521 | info.intent = intent; |
| 3522 | info.customIcon = customIcon; |
| 3523 | info.iconResource = iconResource; |
| 3524 | |
| 3525 | return info; |
| 3526 | } |
| 3527 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3528 | /** |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3529 | * Return an existing FolderInfo object if we have encountered this ID previously, |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3530 | * or make a new one. |
| 3531 | */ |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 3532 | @Thunk static FolderInfo findOrMakeFolder(LongArrayMap<FolderInfo> folders, long id) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3533 | // See if a placeholder was created for us already |
| 3534 | FolderInfo folderInfo = folders.get(id); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3535 | if (folderInfo == null) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3536 | // No placeholder -- create a new instance |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 3537 | folderInfo = new FolderInfo(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 3538 | folders.put(id, folderInfo); |
| 3539 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 3540 | return folderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3541 | } |
| 3542 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 3543 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 3544 | static boolean isValidProvider(AppWidgetProviderInfo provider) { |
| 3545 | return (provider != null) && (provider.provider != null) |
| 3546 | && (provider.provider.getPackageName() != null); |
| 3547 | } |
| 3548 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 3549 | public void dumpState() { |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 3550 | Log.d(TAG, "mCallbacks=" + mCallbacks); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 3551 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data); |
| 3552 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added); |
| 3553 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed); |
| 3554 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 3555 | if (mLoaderTask != null) { |
| 3556 | mLoaderTask.dumpState(); |
| 3557 | } else { |
| 3558 | Log.d(TAG, "mLoaderTask=null"); |
| 3559 | } |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 3560 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 3561 | |
| 3562 | public Callbacks getCallback() { |
| 3563 | return mCallbacks != null ? mCallbacks.get() : null; |
| 3564 | } |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 3565 | |
| 3566 | /** |
| 3567 | * @return {@link FolderInfo} if its already loaded. |
| 3568 | */ |
| 3569 | public FolderInfo findFolderById(Long folderId) { |
| 3570 | synchronized (sBgLock) { |
| 3571 | return sBgFolders.get(folderId); |
| 3572 | } |
| 3573 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3574 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 3575 | @Thunk class DeferredMainThreadExecutor implements Executor { |
| 3576 | |
| 3577 | @Override |
| 3578 | public void execute(Runnable command) { |
| 3579 | runOnMainThread(command); |
| 3580 | } |
| 3581 | } |
| 3582 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 3583 | /** |
| 3584 | * @return the looper for the worker thread which can be used to start background tasks. |
| 3585 | */ |
| 3586 | public static Looper getWorkerLooper() { |
| 3587 | return sWorkerThread.getLooper(); |
| 3588 | } |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 3589 | |
| 3590 | @Thunk static final void addDumpLog(String log) { |
| 3591 | Launcher.addDumpLog(TAG, log); |
| 3592 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 3593 | } |