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 | |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 19 | import android.appwidget.AppWidgetProviderInfo; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.ComponentName; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 22 | import android.content.ContentProviderOperation; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 27 | import android.content.IntentFilter; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 28 | import android.content.pm.LauncherActivityInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.HandlerThread; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 32 | import android.os.Looper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 34 | import android.os.SystemClock; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 35 | import android.os.Trace; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 36 | import android.os.UserHandle; |
Tony Wickham | 86222d2 | 2017-03-29 15:30:43 -0700 | [diff] [blame^] | 37 | import android.support.annotation.Nullable; |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 38 | import android.text.TextUtils; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 40 | import android.util.LongSparseArray; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 41 | import android.util.MutableInt; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 42 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 43 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 44 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 45 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 46 | import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 47 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 48 | import com.android.launcher3.config.ProviderConfig; |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 49 | import com.android.launcher3.dynamicui.ExtractionUtils; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 50 | import com.android.launcher3.folder.Folder; |
| 51 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 52 | import com.android.launcher3.graphics.LauncherIcons; |
Sunny Goyal | 1acb9e9 | 2016-05-16 12:41:09 -0700 | [diff] [blame] | 53 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 54 | import com.android.launcher3.model.AddWorkspaceItemsTask; |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 55 | import com.android.launcher3.model.BgDataModel; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 56 | import com.android.launcher3.model.CacheDataUpdatedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 57 | import com.android.launcher3.model.ExtendedModelTask; |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 58 | import com.android.launcher3.model.GridSizeMigrationTask; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 59 | import com.android.launcher3.model.LoaderCursor; |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 60 | import com.android.launcher3.model.ModelWriter; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 61 | import com.android.launcher3.model.PackageInstallStateChangedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 62 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 63 | import com.android.launcher3.model.PackageUpdatedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 64 | import com.android.launcher3.model.SdCardAvailableReceiver; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 65 | import com.android.launcher3.model.ShortcutsChangedTask; |
| 66 | import com.android.launcher3.model.UserLockStateChangedTask; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 67 | import com.android.launcher3.model.WidgetItem; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 68 | import com.android.launcher3.model.WidgetsModel; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 69 | import com.android.launcher3.provider.ImportDataTask; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 70 | import com.android.launcher3.provider.LauncherDbUtils; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 71 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
| 72 | import com.android.launcher3.shortcuts.ShortcutInfoCompat; |
| 73 | import com.android.launcher3.shortcuts.ShortcutKey; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 74 | import com.android.launcher3.util.ComponentKey; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 75 | import com.android.launcher3.util.ManagedProfileHeuristic; |
Tony Wickham | d82a39d | 2016-07-01 15:44:13 -0700 | [diff] [blame] | 76 | import com.android.launcher3.util.MultiHashMap; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 77 | import com.android.launcher3.util.PackageManagerHelper; |
Tony Wickham | 86222d2 | 2017-03-29 15:30:43 -0700 | [diff] [blame^] | 78 | import com.android.launcher3.util.PackageUserKey; |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 80 | import com.android.launcher3.util.Provider; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 81 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 82 | import com.android.launcher3.util.ViewOnDrawExecutor; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 83 | |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 84 | import java.io.FileDescriptor; |
| 85 | import java.io.PrintWriter; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 86 | import java.lang.ref.WeakReference; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 87 | import java.util.ArrayList; |
| 88 | import java.util.Collections; |
| 89 | import java.util.Comparator; |
| 90 | import java.util.HashMap; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 91 | import java.util.HashSet; |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 92 | import java.util.Iterator; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 93 | import java.util.List; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 94 | import java.util.Map; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 95 | import java.util.Set; |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 96 | import java.util.concurrent.CancellationException; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 97 | import java.util.concurrent.Executor; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 98 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 101 | * 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] | 102 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 103 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 104 | public class LauncherModel extends BroadcastReceiver |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 105 | implements LauncherAppsCompat.OnAppsChangedCallbackCompat { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 106 | static final boolean DEBUG_LOADERS = false; |
Chris Wren | ee52336 | 2014-09-09 10:09:02 -0400 | [diff] [blame] | 107 | private static final boolean DEBUG_RECEIVER = false; |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 108 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 109 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 110 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 111 | 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] | 112 | private static final long INVALID_SCREEN_ID = -1L; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 113 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 114 | @Thunk final LauncherAppState mApp; |
| 115 | @Thunk final Object mLock = new Object(); |
| 116 | @Thunk DeferredHandler mHandler = new DeferredHandler(); |
| 117 | @Thunk LoaderTask mLoaderTask; |
| 118 | @Thunk boolean mIsLoaderTaskRunning; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 119 | @Thunk boolean mHasLoaderCompletedOnce; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 121 | @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 122 | static { |
| 123 | sWorkerThread.start(); |
| 124 | } |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 125 | @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 126 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 127 | // Indicates whether the current model data is valid or not. |
| 128 | // We start off with everything not loaded. After that, we assume that |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 129 | // our monitoring of the package manager provides all updates and we never |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 130 | // need to do a requery. This is only ever touched from the loader thread. |
| 131 | private boolean mModelLoaded; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 132 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Set of runnables to be called on the background thread after the workspace binding |
| 135 | * is complete. |
| 136 | */ |
| 137 | static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>(); |
| 138 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 139 | @Thunk WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 140 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 141 | // < only access in worker thread > |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 142 | private final AllAppsList mBgAllAppsList; |
Hyunyoung Song | 9110d48 | 2015-05-22 14:49:23 -0700 | [diff] [blame] | 143 | // Entire list of widgets. |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 144 | private final WidgetsModel mBgWidgetsModel; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 145 | |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 146 | private boolean mHasShortcutHostPermission; |
| 147 | // Runnable to check if the shortcuts permission has changed. |
| 148 | private final Runnable mShortcutPermissionCheckRunnable = new Runnable() { |
| 149 | @Override |
| 150 | public void run() { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 151 | if (mModelLoaded) { |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 152 | boolean hasShortcutHostPermission = |
| 153 | DeepShortcutManager.getInstance(mApp.getContext()).hasHostPermission(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 154 | if (hasShortcutHostPermission != mHasShortcutHostPermission) { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 155 | forceReload(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | }; |
| 160 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 161 | /** |
| 162 | * All the static data should be accessed on the background thread, A lock should be acquired |
| 163 | * on this object when accessing any data from this model. |
| 164 | */ |
| 165 | static final BgDataModel sBgDataModel = new BgDataModel(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 166 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 167 | // </ only access in worker thread > |
| 168 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 169 | private final IconCache mIconCache; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 170 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 171 | private final LauncherAppsCompat mLauncherApps; |
| 172 | private final UserManagerCompat mUserManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 173 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 174 | public interface Callbacks { |
Joe Onorato | ef2efcf | 2010-10-27 13:21:00 -0700 | [diff] [blame] | 175 | public boolean setLoadOnResume(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 176 | public int getCurrentWorkspaceScreen(); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 177 | public void clearPendingBinds(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 178 | public void startBinding(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 179 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end, |
| 180 | boolean forceAnimateIcons); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 181 | public void bindScreens(ArrayList<Long> orderedScreenIds); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 182 | public void finishFirstPageBind(ViewOnDrawExecutor executor); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 183 | public void finishBindingItems(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 184 | public void bindAppWidget(LauncherAppWidgetInfo info); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 185 | public void bindAllApplications(ArrayList<AppInfo> apps); |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 186 | public void bindAppsAdded(ArrayList<Long> newScreens, |
| 187 | ArrayList<ItemInfo> addNotAnimated, |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 188 | ArrayList<ItemInfo> addAnimated, |
| 189 | ArrayList<AppInfo> addedApps); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 190 | public void bindAppsUpdated(ArrayList<AppInfo> apps); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 191 | public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 192 | ArrayList<ShortcutInfo> removed, UserHandle user); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 193 | public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 194 | public void bindRestoreItemsChange(HashSet<ItemInfo> updates); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 195 | public void bindWorkspaceComponentsRemoved( |
| 196 | HashSet<String> packageNames, HashSet<ComponentName> components, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 197 | UserHandle user); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 198 | public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 199 | public void notifyWidgetProvidersChanged(); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 200 | public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 201 | public void onPageBoundSynchronously(int page); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 202 | public void executeOnNextDraw(ViewOnDrawExecutor executor); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 203 | public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 204 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 205 | |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 206 | LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter) { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 207 | Context context = app.getContext(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 208 | mApp = app; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 209 | mBgAllAppsList = new AllAppsList(iconCache, appFilter); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 210 | mBgWidgetsModel = new WidgetsModel(iconCache, appFilter); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 211 | mIconCache = iconCache; |
| 212 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 213 | mLauncherApps = LauncherAppsCompat.getInstance(context); |
| 214 | mUserManager = UserManagerCompat.getInstance(context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 217 | /** Runs the specified runnable immediately if called from the main thread, otherwise it is |
| 218 | * posted on the main thread handler. */ |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 219 | private void runOnMainThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 220 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 221 | // If we are on the worker thread, post onto the main handler |
| 222 | mHandler.post(r); |
| 223 | } else { |
| 224 | r.run(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** Runs the specified runnable immediately if called from the worker thread, otherwise it is |
| 229 | * posted on the worker thread handler. */ |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 230 | private static void runOnWorkerThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 231 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 232 | r.run(); |
| 233 | } else { |
| 234 | // If we are not on the worker thread, then post to the worker handler |
| 235 | sWorker.post(r); |
| 236 | } |
| 237 | } |
| 238 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 239 | public void setPackageState(PackageInstallInfo installInfo) { |
| 240 | enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo)); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 243 | /** |
| 244 | * Updates the icons and label of all pending icons for the provided package name. |
| 245 | */ |
| 246 | public void updateSessionDisplayInfo(final String packageName) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 247 | HashSet<String> packages = new HashSet<>(); |
| 248 | packages.add(packageName); |
| 249 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 250 | CacheDataUpdatedTask.OP_SESSION_UPDATE, Process.myUserHandle(), packages)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Adds the provided items to the workspace. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 255 | */ |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 256 | public void addAndBindAddedWorkspaceItems(List<ItemInfo> workspaceApps) { |
| 257 | addAndBindAddedWorkspaceItems(Provider.of(workspaceApps)); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Adds the provided items to the workspace. |
| 262 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 263 | public void addAndBindAddedWorkspaceItems( |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 264 | Provider<List<ItemInfo>> appsProvider) { |
| 265 | enqueueModelUpdateTask(new AddWorkspaceItemsTask(appsProvider)); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 268 | public ModelWriter getWriter(boolean hasVerticalHotseat) { |
| 269 | return new ModelWriter(mApp.getContext(), sBgDataModel, hasVerticalHotseat); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 272 | static void checkItemInfoLocked( |
| 273 | final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 274 | ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId); |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 275 | if (modelItem != null && item != modelItem) { |
| 276 | // check all the data is consistent |
| 277 | if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { |
| 278 | ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; |
| 279 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 280 | if (modelShortcut.title.toString().equals(shortcut.title.toString()) && |
| 281 | modelShortcut.intent.filterEquals(shortcut.intent) && |
| 282 | modelShortcut.id == shortcut.id && |
| 283 | modelShortcut.itemType == shortcut.itemType && |
| 284 | modelShortcut.container == shortcut.container && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 285 | modelShortcut.screenId == shortcut.screenId && |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 286 | modelShortcut.cellX == shortcut.cellX && |
| 287 | modelShortcut.cellY == shortcut.cellY && |
| 288 | modelShortcut.spanX == shortcut.spanX && |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 289 | modelShortcut.spanY == shortcut.spanY) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 290 | // For all intents and purposes, this is the same object |
| 291 | return; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // the modelItem needs to match up perfectly with item if our model is |
| 296 | // to be consistent with the database-- for now, just require |
| 297 | // modelItem == item or the equality check above |
| 298 | String msg = "item: " + ((item != null) ? item.toString() : "null") + |
| 299 | "modelItem: " + |
| 300 | ((modelItem != null) ? modelItem.toString() : "null") + |
| 301 | "Error: ItemInfo passed to checkItemInfo doesn't match original"; |
| 302 | RuntimeException e = new RuntimeException(msg); |
| 303 | if (stackTrace != null) { |
| 304 | e.setStackTrace(stackTrace); |
| 305 | } |
Adam Cohen | b9ada65 | 2013-11-08 08:25:08 -0800 | [diff] [blame] | 306 | throw e; |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 310 | static void checkItemInfo(final ItemInfo item) { |
| 311 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 312 | final long itemId = item.id; |
| 313 | Runnable r = new Runnable() { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 314 | public void run() { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 315 | synchronized (sBgDataModel) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 316 | checkItemInfoLocked(itemId, item, stackTrace); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 317 | } |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 318 | } |
| 319 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 320 | runOnWorkerThread(r); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 323 | /** |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 324 | * Update the order of the workspace screens in the database. The array list contains |
| 325 | * a list of screen ids in the order that they should appear. |
| 326 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 327 | public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 328 | final ArrayList<Long> screensCopy = new ArrayList<Long>(screens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 329 | final ContentResolver cr = context.getContentResolver(); |
| 330 | final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 331 | |
| 332 | // Remove any negative screen ids -- these aren't persisted |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 333 | Iterator<Long> iter = screensCopy.iterator(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 334 | while (iter.hasNext()) { |
| 335 | long id = iter.next(); |
| 336 | if (id < 0) { |
| 337 | iter.remove(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | Runnable r = new Runnable() { |
| 342 | @Override |
| 343 | public void run() { |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 344 | ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 345 | // Clear the table |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 346 | ops.add(ContentProviderOperation.newDelete(uri).build()); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 347 | int count = screensCopy.size(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 348 | for (int i = 0; i < count; i++) { |
| 349 | ContentValues v = new ContentValues(); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 350 | long screenId = screensCopy.get(i); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 351 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 352 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 353 | ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build()); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 354 | } |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 355 | |
| 356 | try { |
| 357 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 358 | } catch (Exception ex) { |
| 359 | throw new RuntimeException(ex); |
| 360 | } |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 361 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 362 | synchronized (sBgDataModel) { |
| 363 | sBgDataModel.workspaceScreens.clear(); |
| 364 | sBgDataModel.workspaceScreens.addAll(screensCopy); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 365 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 366 | } |
| 367 | }; |
| 368 | runOnWorkerThread(r); |
| 369 | } |
| 370 | |
| 371 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 372 | * Set this as the current Launcher activity object for the loader. |
| 373 | */ |
| 374 | public void initialize(Callbacks callbacks) { |
| 375 | synchronized (mLock) { |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 376 | Preconditions.assertUIThread(); |
| 377 | // Remove any queued UI runnables |
| 378 | mHandler.cancelAll(); |
| 379 | mCallbacks = new WeakReference<>(callbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 383 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 384 | public void onPackageChanged(String packageName, UserHandle user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 385 | int op = PackageUpdatedTask.OP_UPDATE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 386 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 390 | public void onPackageRemoved(String packageName, UserHandle user) { |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 391 | onPackagesRemoved(user, packageName); |
| 392 | } |
| 393 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 394 | public void onPackagesRemoved(UserHandle user, String... packages) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 395 | int op = PackageUpdatedTask.OP_REMOVE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 396 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 400 | public void onPackageAdded(String packageName, UserHandle user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 401 | int op = PackageUpdatedTask.OP_ADD; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 402 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 406 | public void onPackagesAvailable(String[] packageNames, UserHandle user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 407 | boolean replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 408 | enqueueModelUpdateTask( |
| 409 | new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 413 | public void onPackagesUnavailable(String[] packageNames, UserHandle user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 414 | boolean replacing) { |
| 415 | if (!replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 416 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 417 | PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 418 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 421 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 422 | public void onPackagesSuspended(String[] packageNames, UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 423 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 424 | PackageUpdatedTask.OP_SUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 428 | public void onPackagesUnsuspended(String[] packageNames, UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 429 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 430 | PackageUpdatedTask.OP_UNSUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 433 | @Override |
| 434 | public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 435 | UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 436 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true)); |
Sunny Goyal | 50941fb | 2016-08-04 12:03:52 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts, |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 440 | UserHandle user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 441 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 444 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 445 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 446 | * ACTION_PACKAGE_CHANGED. |
| 447 | */ |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 448 | @Override |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 449 | public void onReceive(Context context, Intent intent) { |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 450 | if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 451 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 452 | final String action = intent.getAction(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 453 | if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 454 | // If we have changed locale we need to clear out the labels in all apps/workspace. |
| 455 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 456 | } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action) |
| 457 | || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 458 | UserManagerCompat.getInstance(context).enableAndResetCache(); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 459 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 460 | } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 461 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 462 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 463 | UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER); |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 464 | if (user != null) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 465 | if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 466 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 467 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 468 | PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so |
| 472 | // we need to run the state change task again. |
| 473 | if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 474 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 475 | enqueueModelUpdateTask(new UserLockStateChangedTask(user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 476 | } |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 477 | } |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 478 | } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) { |
| 479 | ExtractionUtils.startColorExtractionServiceIfNecessary(context); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 483 | /** |
| 484 | * Reloads the workspace items from the DB and re-binds the workspace. This should generally |
| 485 | * not be called as DB updates are automatically followed by UI update |
| 486 | */ |
| 487 | public void forceReload() { |
| 488 | synchronized (mLock) { |
| 489 | // Stop any existing loaders first, so they don't set mModelLoaded to true later |
| 490 | stopLoaderLocked(); |
| 491 | mModelLoaded = false; |
| 492 | } |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 493 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 494 | // Do this here because if the launcher activity is running it will be restarted. |
| 495 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 496 | // to reload. |
| 497 | startLoaderFromBackground(); |
| 498 | } |
| 499 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 500 | /** |
| 501 | * When the launcher is in the background, it's possible for it to miss paired |
| 502 | * configuration changes. So whenever we trigger the loader from the background |
| 503 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 504 | * of doing it now. |
| 505 | */ |
| 506 | public void startLoaderFromBackground() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 507 | Callbacks callbacks = getCallback(); |
| 508 | if (callbacks != null) { |
| 509 | // Only actually run the loader if they're not paused. |
| 510 | if (!callbacks.setLoadOnResume()) { |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 511 | startLoader(callbacks.getCurrentWorkspaceScreen()); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 514 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 515 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 516 | /** |
| 517 | * If there is already a loader task running, tell it to stop. |
| 518 | */ |
| 519 | private void stopLoaderLocked() { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 520 | LoaderTask oldTask = mLoaderTask; |
| 521 | if (oldTask != null) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 522 | oldTask.stopLocked(); |
| 523 | } |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Adam Cohen | 1a85c58 | 2014-09-30 09:48:49 -0700 | [diff] [blame] | 526 | public boolean isCurrentCallbacks(Callbacks callbacks) { |
| 527 | return (mCallbacks != null && mCallbacks.get() == callbacks); |
| 528 | } |
| 529 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 530 | /** |
| 531 | * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible. |
| 532 | * @return true if the page could be bound synchronously. |
| 533 | */ |
| 534 | public boolean startLoader(int synchronousBindPage) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 535 | // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems |
| 536 | InstallShortcutReceiver.enableInstallQueue(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 537 | synchronized (mLock) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 538 | // Don't bother to start the thread if we know it's not going to do anything |
| 539 | if (mCallbacks != null && mCallbacks.get() != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 540 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 541 | // Clear any pending bind-runnables from the synchronized load process. |
| 542 | runOnMainThread(new Runnable() { |
| 543 | public void run() { |
| 544 | oldCallbacks.clearPendingBinds(); |
| 545 | } |
| 546 | }); |
| 547 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 548 | // If there is already one running, tell it to stop. |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 549 | stopLoaderLocked(); |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 550 | mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 551 | if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE |
| 552 | && mModelLoaded && !mIsLoaderTaskRunning) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 553 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 554 | return true; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 555 | } else { |
| 556 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 557 | sWorker.post(mLoaderTask); |
| 558 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 559 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 560 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 561 | return false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 564 | public void stopLoader() { |
| 565 | synchronized (mLock) { |
| 566 | if (mLoaderTask != null) { |
| 567 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 568 | } |
| 569 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 570 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 571 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 572 | /** |
| 573 | * Loads the workspace screen ids in an ordered list. |
| 574 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 575 | public static ArrayList<Long> loadWorkspaceScreensDb(Context context) { |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 576 | final ContentResolver contentResolver = context.getContentResolver(); |
| 577 | final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 578 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 579 | // Get screens ordered by rank. |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 580 | return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query( |
| 581 | screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK)); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 584 | /** |
| 585 | * Runnable for the thread that loads the contents of the launcher: |
| 586 | * - workspace icons |
| 587 | * - widgets |
| 588 | * - all apps icons |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 589 | * - deep shortcuts within apps |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 590 | */ |
| 591 | private class LoaderTask implements Runnable { |
| 592 | private Context mContext; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 593 | private int mPageToBindFirst; |
| 594 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 595 | @Thunk boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 596 | private boolean mStopped; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 597 | @Thunk boolean mLoadAndBindStepFinished; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 598 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 599 | LoaderTask(Context context, int pageToBindFirst) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 600 | mContext = context; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 601 | mPageToBindFirst = pageToBindFirst; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 602 | } |
| 603 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 604 | private void waitForIdle() { |
| 605 | // Wait until the either we're stopped or the other threads are done. |
| 606 | // This way we don't start loading all apps until the workspace has settled |
| 607 | // down. |
| 608 | synchronized (LoaderTask.this) { |
| 609 | final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 610 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 611 | mHandler.postIdle(new Runnable() { |
| 612 | public void run() { |
| 613 | synchronized (LoaderTask.this) { |
| 614 | mLoadAndBindStepFinished = true; |
| 615 | if (DEBUG_LOADERS) { |
| 616 | Log.d(TAG, "done with previous binding step"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 617 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 618 | LoaderTask.this.notify(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 619 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 620 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 621 | }); |
| 622 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 623 | while (!mStopped && !mLoadAndBindStepFinished) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 624 | try { |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 625 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 626 | // wait no longer than 1sec at a time |
| 627 | this.wait(1000); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 628 | } catch (InterruptedException ex) { |
| 629 | // Ignore |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 630 | } |
| 631 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 632 | if (DEBUG_LOADERS) { |
| 633 | Log.d(TAG, "waited " |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 634 | + (SystemClock.uptimeMillis()-workspaceWaitTime) |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 635 | + "ms for previous step to finish binding"); |
| 636 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 637 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 638 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 639 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 640 | void runBindSynchronousPage(int synchronousBindPage) { |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 641 | if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 642 | // Ensure that we have a valid page index to load synchronously |
| 643 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 644 | "valid page index"); |
| 645 | } |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 646 | if (!mModelLoaded) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 647 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 648 | // loaded already (we should load everything asynchronously in that case) |
| 649 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 650 | } |
| 651 | synchronized (mLock) { |
| 652 | if (mIsLoaderTaskRunning) { |
| 653 | // Ensure that we are never running the background loading at this point since |
| 654 | // we also touch the background collections |
| 655 | throw new RuntimeException("Error! Background loading is already running"); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // XXX: Throw an exception if we are already loading (since we touch the worker thread |
| 660 | // data structures, we can't allow any other thread to touch that data, but because |
| 661 | // this call is synchronous, we can get away with not locking). |
| 662 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 663 | // The LauncherModel is static in the LauncherAppState and mHandler may have queued |
Adam Cohen | a13a2f2 | 2012-07-23 14:29:15 -0700 | [diff] [blame] | 664 | // operations from the previous activity. We need to ensure that all queued operations |
| 665 | // are executed before any synchronous binding work is done. |
| 666 | mHandler.flush(); |
| 667 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 668 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 669 | // everything else that is to be bound normally (asynchronously). |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 670 | bindWorkspace(synchronousBindPage); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 671 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 672 | // arise from that. |
| 673 | onlyBindAllApps(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 674 | |
| 675 | bindDeepShortcuts(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 678 | private void verifyNotStopped() throws CancellationException { |
| 679 | synchronized (LoaderTask.this) { |
| 680 | if (mStopped) { |
| 681 | throw new CancellationException("Loader stopped"); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 686 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 687 | synchronized (mLock) { |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 688 | if (mStopped) { |
| 689 | return; |
| 690 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 691 | mIsLoaderTaskRunning = true; |
| 692 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 693 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 694 | try { |
| 695 | if (DEBUG_LOADERS) Log.d(TAG, "step 1.1: loading workspace"); |
| 696 | // Set to false in bindWorkspace() |
| 697 | mIsLoadingAndBindingWorkspace = true; |
| 698 | loadWorkspace(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 699 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 700 | verifyNotStopped(); |
| 701 | if (DEBUG_LOADERS) Log.d(TAG, "step 1.2: bind workspace workspace"); |
| 702 | bindWorkspace(mPageToBindFirst); |
| 703 | |
| 704 | // Take a break |
| 705 | if (DEBUG_LOADERS) Log.d(TAG, "step 1 completed, wait for idle"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 706 | waitForIdle(); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 707 | verifyNotStopped(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 708 | |
| 709 | // second step |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 710 | if (DEBUG_LOADERS) Log.d(TAG, "step 2.1: loading all apps"); |
| 711 | loadAllApps(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 712 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 713 | verifyNotStopped(); |
| 714 | if (DEBUG_LOADERS) Log.d(TAG, "step 2.2: Update icon cache"); |
| 715 | updateIconCache(); |
| 716 | |
| 717 | // Take a break |
| 718 | if (DEBUG_LOADERS) Log.d(TAG, "step 2 completed, wait for idle"); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 719 | waitForIdle(); |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 720 | verifyNotStopped(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 721 | |
| 722 | // third step |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 723 | if (DEBUG_LOADERS) Log.d(TAG, "step 3.1: loading deep shortcuts"); |
| 724 | loadDeepShortcuts(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 725 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 726 | verifyNotStopped(); |
| 727 | if (DEBUG_LOADERS) Log.d(TAG, "step 3.2: bind deep shortcuts"); |
| 728 | bindDeepShortcuts(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 729 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 730 | synchronized (mLock) { |
| 731 | // Everything loaded bind the data. |
| 732 | mModelLoaded = true; |
| 733 | mHasLoaderCompletedOnce = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 734 | } |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 735 | } catch (CancellationException e) { |
| 736 | // Loader stopped, ignore |
| 737 | } finally { |
| 738 | // Clear out this reference, otherwise we end up holding it until all of the |
| 739 | // callback runnables are done. |
| 740 | mContext = null; |
| 741 | |
| 742 | synchronized (mLock) { |
| 743 | // If we are still the last one to be scheduled, remove ourselves. |
| 744 | if (mLoaderTask == this) { |
| 745 | mLoaderTask = null; |
| 746 | } |
| 747 | mIsLoaderTaskRunning = false; |
| 748 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 749 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | public void stopLocked() { |
| 753 | synchronized (LoaderTask.this) { |
| 754 | mStopped = true; |
| 755 | this.notify(); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 761 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 762 | * object that was around when the deferred message was scheduled, and if there's |
| 763 | * a new Callbacks object around then also return null. This will save us from |
| 764 | * calling onto it with data that will be ignored. |
| 765 | */ |
| 766 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 767 | synchronized (mLock) { |
| 768 | if (mStopped) { |
| 769 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 770 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 771 | |
| 772 | if (mCallbacks == null) { |
| 773 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 774 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 775 | |
| 776 | final Callbacks callbacks = mCallbacks.get(); |
| 777 | if (callbacks != oldCallbacks) { |
| 778 | return null; |
| 779 | } |
| 780 | if (callbacks == null) { |
| 781 | Log.w(TAG, "no mCallbacks"); |
| 782 | return null; |
| 783 | } |
| 784 | |
| 785 | return callbacks; |
| 786 | } |
| 787 | } |
| 788 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 789 | private void loadWorkspace() { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 790 | if (LauncherAppState.PROFILE_STARTUP) { |
| 791 | Trace.beginSection("Loading Workspace"); |
| 792 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 793 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 794 | final Context context = mContext; |
| 795 | final ContentResolver contentResolver = context.getContentResolver(); |
Sunny Goyal | 342e466 | 2017-02-02 15:21:08 -0800 | [diff] [blame] | 796 | final PackageManagerHelper pmHelper = new PackageManagerHelper(context); |
| 797 | final boolean isSafeMode = pmHelper.isSafeMode(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 798 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 799 | final DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(context); |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 800 | final boolean isSdCardReady = Utilities.isBootCompleted(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 801 | final MultiHashMap<UserHandle, String> pendingPackages = new MultiHashMap<>(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 802 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 803 | boolean clearDb = false; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 804 | try { |
| 805 | ImportDataTask.performImportIfPossible(context); |
| 806 | } catch (Exception e) { |
| 807 | // Migration failed. Clear workspace. |
| 808 | clearDb = true; |
| 809 | } |
| 810 | |
| 811 | if (!clearDb && GridSizeMigrationTask.ENABLED && |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 812 | !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) { |
| 813 | // Migration failed. Clear workspace. |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 814 | clearDb = true; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 817 | if (clearDb) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 818 | Log.d(TAG, "loadWorkspace: resetting launcher database"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 819 | LauncherSettings.Settings.call(contentResolver, |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 820 | LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 821 | } |
| 822 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 823 | Log.d(TAG, "loadWorkspace: loading default favorites"); |
| 824 | LauncherSettings.Settings.call(contentResolver, |
| 825 | LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 826 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 827 | synchronized (sBgDataModel) { |
| 828 | sBgDataModel.clear(); |
| 829 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 830 | final HashMap<String, Integer> installingPkgs = PackageInstallerCompat |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 831 | .getInstance(mContext).updateAndGetActiveSessionCache(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 832 | sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext)); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 833 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 834 | Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>(); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 835 | final LoaderCursor c = new LoaderCursor(contentResolver.query( |
| 836 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null), mApp); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 837 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 838 | HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 839 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 840 | try { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 841 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 842 | LauncherSettings.Favorites.APPWIDGET_ID); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 843 | final int appWidgetProviderIndex = c.getColumnIndexOrThrow( |
| 844 | LauncherSettings.Favorites.APPWIDGET_PROVIDER); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 845 | final int spanXIndex = c.getColumnIndexOrThrow |
| 846 | (LauncherSettings.Favorites.SPANX); |
| 847 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 848 | LauncherSettings.Favorites.SPANY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 849 | final int rankIndex = c.getColumnIndexOrThrow( |
| 850 | LauncherSettings.Favorites.RANK); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 851 | final int optionsIndex = c.getColumnIndexOrThrow( |
| 852 | LauncherSettings.Favorites.OPTIONS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 853 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 854 | final LongSparseArray<UserHandle> allUsers = c.allUsers; |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 855 | final LongSparseArray<Boolean> quietMode = new LongSparseArray<>(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 856 | final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 857 | for (UserHandle user : mUserManager.getUserProfiles()) { |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 858 | long serialNo = mUserManager.getSerialNumberForUser(user); |
| 859 | allUsers.put(serialNo, user); |
| 860 | quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 861 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 862 | boolean userUnlocked = mUserManager.isUserUnlocked(user); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 863 | |
| 864 | // We can only query for shortcuts when the user is unlocked. |
| 865 | if (userUnlocked) { |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 866 | List<ShortcutInfoCompat> pinnedShortcuts = |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 867 | shortcutManager.queryForPinnedShortcuts(null, user); |
| 868 | if (shortcutManager.wasLastCallSuccess()) { |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 869 | for (ShortcutInfoCompat shortcut : pinnedShortcuts) { |
| 870 | shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), |
| 871 | shortcut); |
| 872 | } |
| 873 | } else { |
| 874 | // Shortcut manager can fail due to some race condition when the |
| 875 | // lock state changes too frequently. For the purpose of the loading |
| 876 | // shortcuts, consider the user is still locked. |
| 877 | userUnlocked = false; |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 878 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 879 | } |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 880 | unlockedUsers.put(serialNo, userUnlocked); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 883 | ShortcutInfo info; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 884 | LauncherAppWidgetInfo appWidgetInfo; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 885 | Intent intent; |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 886 | String targetPkg; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 887 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 888 | while (!mStopped && c.moveToNext()) { |
| 889 | try { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 890 | if (c.user == null) { |
| 891 | // User has been deleted, remove the item. |
| 892 | c.markDeleted("User has been deleted"); |
| 893 | continue; |
| 894 | } |
| 895 | |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 896 | boolean allowMissingTarget = false; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 897 | switch (c.itemType) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 898 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 899 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 900 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 901 | intent = c.parseIntent(); |
| 902 | if (intent == null) { |
| 903 | c.markDeleted("Invalid or null intent"); |
| 904 | continue; |
| 905 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 906 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 907 | int disabledState = quietMode.get(c.serialNumber) ? |
| 908 | ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0; |
| 909 | ComponentName cn = intent.getComponent(); |
| 910 | targetPkg = cn == null ? intent.getPackage() : cn.getPackageName(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 911 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 912 | if (!Process.myUserHandle().equals(c.user)) { |
| 913 | if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 914 | c.markDeleted("Legacy shortcuts are only allowed for default user"); |
| 915 | continue; |
| 916 | } else if (c.restoreFlag != 0) { |
| 917 | // Don't restore items for other profiles. |
| 918 | c.markDeleted("Restore from managed profile not supported"); |
| 919 | continue; |
| 920 | } |
| 921 | } |
| 922 | if (TextUtils.isEmpty(targetPkg) && |
| 923 | c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 924 | c.markDeleted("Only legacy shortcuts can have null package"); |
| 925 | continue; |
| 926 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 927 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 928 | // If there is no target package, its an implicit intent |
| 929 | // (legacy shortcut) which is always valid |
| 930 | boolean validTarget = TextUtils.isEmpty(targetPkg) || |
| 931 | launcherApps.isPackageEnabledForProfile(targetPkg, c.user); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 932 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 933 | if (cn != null && validTarget) { |
| 934 | // If the apk is present and the shortcut points to a specific |
| 935 | // component. |
| 936 | |
| 937 | // If the component is already present |
| 938 | if (launcherApps.isActivityEnabledForProfile(cn, c.user)) { |
| 939 | // no special handling necessary for this item |
| 940 | c.markRestored(); |
| 941 | } else { |
| 942 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 943 | // We allow auto install apps to have their intent |
| 944 | // updated after an install. |
Sunny Goyal | 342e466 | 2017-02-02 15:21:08 -0800 | [diff] [blame] | 945 | intent = pmHelper.getAppLaunchIntent(targetPkg, c.user); |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 946 | if (intent != null) { |
| 947 | c.restoreFlag = 0; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 948 | c.updater().put( |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 949 | LauncherSettings.Favorites.INTENT, |
| 950 | intent.toUri(0)).commit(); |
| 951 | cn = intent.getComponent(); |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 952 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 953 | c.markDeleted("Unable to find a launch target"); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 954 | continue; |
| 955 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 956 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 957 | // The app is installed but the component is no |
| 958 | // longer available. |
| 959 | c.markDeleted("Invalid component removed: " + cn); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 960 | continue; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 961 | } |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 962 | } |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 963 | } |
| 964 | // else if cn == null => can't infer much, leave it |
| 965 | // else if !validPkg => could be restored icon or missing sd-card |
| 966 | |
| 967 | if (!TextUtils.isEmpty(targetPkg) && !validTarget) { |
| 968 | // Points to a valid app (superset of cn != null) but the apk |
| 969 | // is not available. |
| 970 | |
| 971 | if (c.restoreFlag != 0) { |
| 972 | // Package is not yet available but might be |
| 973 | // installed later. |
| 974 | FileLog.d(TAG, "package not yet restored: " + targetPkg); |
| 975 | |
| 976 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) { |
| 977 | // Restore has started once. |
| 978 | } else if (installingPkgs.containsKey(targetPkg)) { |
| 979 | // App restore has started. Update the flag |
| 980 | c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED; |
| 981 | c.updater().commit(); |
| 982 | } else { |
| 983 | c.markDeleted("Unrestored app removed: " + targetPkg); |
| 984 | continue; |
| 985 | } |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 986 | } else if (pmHelper.isAppOnSdcard(targetPkg, c.user)) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 987 | // Package is present but not available. |
| 988 | disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; |
| 989 | // Add the icon on the workspace anyway. |
| 990 | allowMissingTarget = true; |
| 991 | } else if (!isSdCardReady) { |
| 992 | // SdCard is not ready yet. Package might get available, |
| 993 | // once it is ready. |
| 994 | Log.d(TAG, "Missing pkg, will check later: " + targetPkg); |
| 995 | pendingPackages.addToList(c.user, targetPkg); |
| 996 | // Add the icon on the workspace anyway. |
| 997 | allowMissingTarget = true; |
| 998 | } else { |
| 999 | // Do not wait for external media load anymore. |
| 1000 | c.markDeleted("Invalid package removed: " + targetPkg); |
| 1001 | continue; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | if (validTarget) { |
| 1006 | // The shortcut points to a valid target (either no target |
| 1007 | // or something which is ready to be used) |
| 1008 | c.markRestored(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1009 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1010 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1011 | boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() && |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1012 | c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW; |
| 1013 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1014 | if (c.restoreFlag != 0) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1015 | // Already verified above that user is same as default user |
| 1016 | info = c.getRestoredItemInfo(intent); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1017 | } else if (c.itemType == |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1018 | LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1019 | info = c.getAppShortcutInfo( |
| 1020 | intent, allowMissingTarget, useLowResIcon); |
| 1021 | } else if (c.itemType == |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1022 | LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1023 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1024 | ShortcutKey key = ShortcutKey.fromIntent(intent, c.user); |
| 1025 | if (unlockedUsers.get(c.serialNumber)) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1026 | ShortcutInfoCompat pinnedShortcut = |
| 1027 | shortcutKeyToPinnedShortcuts.get(key); |
| 1028 | if (pinnedShortcut == null) { |
| 1029 | // The shortcut is no longer valid. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1030 | c.markDeleted("Pinned shortcut not found"); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1031 | continue; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1032 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1033 | info = new ShortcutInfo(pinnedShortcut, context); |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1034 | info.iconBitmap = LauncherIcons |
| 1035 | .createShortcutIcon(pinnedShortcut, context); |
Charles He | 3ff9047 | 2017-02-15 12:22:56 +0000 | [diff] [blame] | 1036 | if (pmHelper.isAppSuspended( |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 1037 | pinnedShortcut.getPackage(), info.user)) { |
Charles He | 3ff9047 | 2017-02-15 12:22:56 +0000 | [diff] [blame] | 1038 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1039 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1040 | intent = info.intent; |
| 1041 | } else { |
| 1042 | // Create a shortcut info in disabled mode for now. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1043 | info = c.loadSimpleShortcut(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1044 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1045 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1046 | } else { // item type == ITEM_TYPE_SHORTCUT |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1047 | info = c.loadSimpleShortcut(); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1048 | |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1049 | // Shortcuts are only available on the primary profile |
Sunny Goyal | ad2e91a | 2017-02-28 15:14:58 -0800 | [diff] [blame] | 1050 | if (pmHelper.isAppSuspended(targetPkg, c.user)) { |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1051 | disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1052 | } |
| 1053 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1054 | // App shortcuts that used to be automatically added to Launcher |
| 1055 | // didn't always have the correct intent flags set, so do that |
| 1056 | // here |
| 1057 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1058 | intent.getCategories() != null && |
| 1059 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1060 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1061 | intent.addFlags( |
| 1062 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1063 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1064 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1065 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1066 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1067 | if (info != null) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1068 | c.applyCommonProperties(info); |
| 1069 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1070 | info.intent = intent; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1071 | info.rank = c.getInt(rankIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1072 | info.spanX = 1; |
| 1073 | info.spanY = 1; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1074 | info.isDisabled |= disabledState; |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1075 | if (isSafeMode && !Utilities.isSystemApp(context, intent)) { |
| 1076 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; |
| 1077 | } |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1078 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1079 | if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) { |
| 1080 | Integer progress = installingPkgs.get(targetPkg); |
| 1081 | if (progress != null) { |
| 1082 | info.setInstallProgress(progress); |
| 1083 | } else { |
| 1084 | info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1088 | c.checkAndAddItem(info, sBgDataModel); |
Winson Chung | 1323b48 | 2013-08-05 12:41:55 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | throw new RuntimeException("Unexpected null ShortcutInfo"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1091 | } |
| 1092 | break; |
| 1093 | |
| 1094 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1095 | FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id); |
| 1096 | c.applyCommonProperties(folderInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1097 | |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 1098 | // Do not trim the folder label, as is was set by the user. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1099 | folderInfo.title = c.getString(c.titleIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1100 | folderInfo.spanX = 1; |
| 1101 | folderInfo.spanY = 1; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1102 | folderInfo.options = c.getInt(optionsIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1103 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1104 | // no special handling required for restored folders |
| 1105 | c.markRestored(); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1106 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1107 | c.checkAndAddItem(folderInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1108 | break; |
| 1109 | |
| 1110 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1111 | case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1112 | // Read all Launcher-specific widget details |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1113 | boolean customWidget = c.itemType == |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1114 | LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 1115 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1116 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1117 | String savedProvider = c.getString(appWidgetProviderIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1118 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1119 | final ComponentName component = |
| 1120 | ComponentName.unflattenFromString(savedProvider); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1121 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1122 | final boolean isIdValid = !c.hasRestoreFlag( |
| 1123 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); |
| 1124 | final boolean wasProviderReady = !c.hasRestoreFlag( |
| 1125 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1126 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1127 | if (widgetProvidersMap == null) { |
| 1128 | widgetProvidersMap = AppWidgetManagerCompat |
| 1129 | .getInstance(mContext).getAllProvidersMap(); |
| 1130 | } |
| 1131 | final AppWidgetProviderInfo provider = widgetProvidersMap.get( |
| 1132 | new ComponentKey( |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1133 | ComponentName.unflattenFromString(savedProvider), |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1134 | c.user)); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1135 | |
| 1136 | final boolean isProviderReady = isValidProvider(provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1137 | if (!isSafeMode && !customWidget && |
| 1138 | wasProviderReady && !isProviderReady) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1139 | c.markDeleted( |
| 1140 | "Deleting widget that isn't installed anymore: " |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1141 | + provider); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1142 | } else { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1143 | if (isProviderReady) { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1144 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 1145 | provider.provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1146 | |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1147 | // The provider is available. So the widget is either |
| 1148 | // available or not available. We do not need to track |
| 1149 | // any future restore updates. |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1150 | int status = c.restoreFlag & |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1151 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1152 | if (!wasProviderReady) { |
| 1153 | // If provider was not previously ready, update the |
| 1154 | // status and UI flag. |
| 1155 | |
| 1156 | // Id would be valid only if the widget restore broadcast was received. |
| 1157 | if (isIdValid) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1158 | status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1159 | } else { |
| 1160 | status &= ~LauncherAppWidgetInfo |
| 1161 | .FLAG_PROVIDER_NOT_READY; |
| 1162 | } |
| 1163 | } |
| 1164 | appWidgetInfo.restoreStatus = status; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1165 | } else { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1166 | Log.v(TAG, "Widget restore pending id=" + c.id |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1167 | + " appWidgetId=" + appWidgetId |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1168 | + " status =" + c.restoreFlag); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1169 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1170 | component); |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1171 | appWidgetInfo.restoreStatus = c.restoreFlag; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1172 | Integer installProgress = installingPkgs.get(component.getPackageName()); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1173 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1174 | if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1175 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1176 | } else if (installProgress != null) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1177 | // App restore has started. Update the flag |
| 1178 | appWidgetInfo.restoreStatus |= |
| 1179 | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1180 | } else if (!isSafeMode) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1181 | c.markDeleted("Unrestored widget removed: " + component); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1182 | continue; |
| 1183 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1184 | |
| 1185 | appWidgetInfo.installProgress = |
| 1186 | installProgress == null ? 0 : installProgress; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1187 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1188 | if (appWidgetInfo.hasRestoreFlag( |
| 1189 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1190 | appWidgetInfo.bindOptions = c.parseIntent(); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1191 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1192 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1193 | c.applyCommonProperties(appWidgetInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1194 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 1195 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1196 | appWidgetInfo.user = c.user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1197 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1198 | if (!c.isOnWorkspaceOrHotseat()) { |
| 1199 | c.markDeleted("Widget found where container != " + |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1200 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1201 | continue; |
| 1202 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1203 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1204 | if (!customWidget) { |
| 1205 | String providerName = |
| 1206 | appWidgetInfo.providerName.flattenToString(); |
| 1207 | if (!providerName.equals(savedProvider) || |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1208 | (appWidgetInfo.restoreStatus != c.restoreFlag)) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1209 | c.updater() |
| 1210 | .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, |
| 1211 | providerName) |
| 1212 | .put(LauncherSettings.Favorites.RESTORED, |
| 1213 | appWidgetInfo.restoreStatus) |
| 1214 | .commit(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1215 | } |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1216 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1217 | c.checkAndAddItem(appWidgetInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1218 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1219 | break; |
| 1220 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1221 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1222 | Log.e(TAG, "Desktop items loading interrupted", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1223 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } finally { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1226 | Utilities.closeSilently(c); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1229 | // Break early if we've stopped loading |
| 1230 | if (mStopped) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1231 | sBgDataModel.clear(); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1232 | return; |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1235 | // Remove dead items |
| 1236 | if (c.commitDeleted()) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1237 | // Remove any empty folder |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1238 | ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings |
| 1239 | .call(contentResolver, |
| 1240 | LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS) |
| 1241 | .getSerializable(LauncherSettings.Settings.EXTRA_VALUE); |
| 1242 | for (long folderId : deletedFolderIds) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1243 | sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId)); |
| 1244 | sBgDataModel.folders.remove(folderId); |
| 1245 | sBgDataModel.itemsIdMap.remove(folderId); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1249 | // Unpin shortcuts that don't exist on the workspace. |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1250 | HashSet<ShortcutKey> pendingShortcuts = |
| 1251 | InstallShortcutReceiver.getPendingShortcuts(context); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1252 | for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1253 | MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key); |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1254 | if ((numTimesPinned == null || numTimesPinned.value == 0) |
| 1255 | && !pendingShortcuts.contains(key)) { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1256 | // Shortcut is pinned but doesn't exist on the workspace; unpin it. |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1257 | shortcutManager.unpinShortcut(key); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1261 | // Sort all the folder items and make sure the first 3 items are high resolution. |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1262 | for (FolderInfo folder : sBgDataModel.folders) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1263 | Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR); |
| 1264 | int pos = 0; |
| 1265 | for (ShortcutInfo info : folder.contents) { |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 1266 | if (info.usingLowResIcon && |
| 1267 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 3fe4a14 | 2016-12-15 17:40:07 -0800 | [diff] [blame] | 1268 | mIconCache.getTitleAndIcon(info, false); |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1269 | } |
| 1270 | pos ++; |
| 1271 | if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) { |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1277 | c.commitRestoredItems(); |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1278 | if (!isSdCardReady && !pendingPackages.isEmpty()) { |
| 1279 | context.registerReceiver( |
| 1280 | new SdCardAvailableReceiver( |
| 1281 | LauncherModel.this, mContext, pendingPackages), |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1282 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1283 | null, |
| 1284 | sWorker); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1287 | // Remove any empty screens |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1288 | ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1289 | for (ItemInfo item: sBgDataModel.itemsIdMap) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1290 | long screenId = item.screenId; |
| 1291 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1292 | unusedScreens.contains(screenId)) { |
| 1293 | unusedScreens.remove(screenId); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // If there are any empty screens remove them, and update. |
| 1298 | if (unusedScreens.size() != 0) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1299 | sBgDataModel.workspaceScreens.removeAll(unusedScreens); |
| 1300 | updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1301 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1302 | } |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 1303 | if (LauncherAppState.PROFILE_STARTUP) { |
| 1304 | Trace.endSection(); |
| 1305 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1308 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 1309 | * specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1310 | private void filterCurrentWorkspaceItems(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1311 | ArrayList<ItemInfo> allWorkspaceItems, |
| 1312 | ArrayList<ItemInfo> currentScreenItems, |
| 1313 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1314 | // Purge any null ItemInfos |
| 1315 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 1316 | while (iter.hasNext()) { |
| 1317 | ItemInfo i = iter.next(); |
| 1318 | if (i == null) { |
| 1319 | iter.remove(); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1323 | // Order the set of items by their containers first, this allows use to walk through the |
| 1324 | // list sequentially, build up a list of containers that are in the specified screen, |
| 1325 | // as well as all items in those containers. |
| 1326 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 1327 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 1328 | @Override |
| 1329 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 1330 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1331 | } |
| 1332 | }); |
| 1333 | for (ItemInfo info : allWorkspaceItems) { |
| 1334 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1335 | if (info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1336 | currentScreenItems.add(info); |
| 1337 | itemsOnScreen.add(info.id); |
| 1338 | } else { |
| 1339 | otherScreenItems.add(info); |
| 1340 | } |
| 1341 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1342 | currentScreenItems.add(info); |
| 1343 | itemsOnScreen.add(info.id); |
| 1344 | } else { |
| 1345 | if (itemsOnScreen.contains(info.container)) { |
| 1346 | currentScreenItems.add(info); |
| 1347 | itemsOnScreen.add(info.id); |
| 1348 | } else { |
| 1349 | otherScreenItems.add(info); |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /** Filters the set of widgets which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1356 | private void filterCurrentAppWidgets(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1357 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1358 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 1359 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1360 | |
| 1361 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1362 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1363 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1364 | widget.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1365 | currentScreenWidgets.add(widget); |
| 1366 | } else { |
| 1367 | otherScreenWidgets.add(widget); |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1372 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 1373 | * right) */ |
| 1374 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1375 | final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile(); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1376 | final int screenCols = profile.numColumns; |
| 1377 | final int screenCellCount = profile.numColumns * profile.numRows; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1378 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1379 | @Override |
| 1380 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1381 | if (lhs.container == rhs.container) { |
| 1382 | // Within containers, order by their spatial position in that container |
| 1383 | switch ((int) lhs.container) { |
| 1384 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: { |
| 1385 | long lr = (lhs.screenId * screenCellCount + |
| 1386 | lhs.cellY * screenCols + lhs.cellX); |
| 1387 | long rr = (rhs.screenId * screenCellCount + |
| 1388 | rhs.cellY * screenCols + rhs.cellX); |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1389 | return Utilities.longCompare(lr, rr); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1390 | } |
| 1391 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: { |
| 1392 | // We currently use the screen id as the rank |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1393 | return Utilities.longCompare(lhs.screenId, rhs.screenId); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1394 | } |
| 1395 | default: |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 1396 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1397 | throw new RuntimeException("Unexpected container type when " + |
| 1398 | "sorting workspace items."); |
| 1399 | } |
| 1400 | return 0; |
| 1401 | } |
| 1402 | } else { |
| 1403 | // Between containers, order by hotseat, desktop |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1404 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1405 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1406 | } |
| 1407 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1408 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1409 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1410 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 1411 | final ArrayList<Long> orderedScreens) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1412 | final Runnable r = new Runnable() { |
| 1413 | @Override |
| 1414 | public void run() { |
| 1415 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1416 | if (callbacks != null) { |
| 1417 | callbacks.bindScreens(orderedScreens); |
| 1418 | } |
| 1419 | } |
| 1420 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1421 | runOnMainThread(r); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1424 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 1425 | final ArrayList<ItemInfo> workspaceItems, |
| 1426 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1427 | final Executor executor) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1428 | |
| 1429 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1430 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1431 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1432 | final int start = i; |
| 1433 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1434 | final Runnable r = new Runnable() { |
| 1435 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1436 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1437 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1438 | if (callbacks != null) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1439 | callbacks.bindItems(workspaceItems, start, start+chunkSize, |
| 1440 | false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1441 | } |
| 1442 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1443 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1444 | executor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1445 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1446 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1447 | // Bind the widgets, one at a time |
| 1448 | N = appWidgets.size(); |
| 1449 | for (int i = 0; i < N; i++) { |
| 1450 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 1451 | final Runnable r = new Runnable() { |
| 1452 | public void run() { |
| 1453 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1454 | if (callbacks != null) { |
| 1455 | callbacks.bindAppWidget(widget); |
| 1456 | } |
| 1457 | } |
| 1458 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1459 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * Binds all loaded data to actual views on the main thread. |
| 1465 | */ |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1466 | private void bindWorkspace(int synchronizeBindPage) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1467 | final long t = SystemClock.uptimeMillis(); |
| 1468 | Runnable r; |
| 1469 | |
| 1470 | // Don't use these two variables in any of the callback runnables. |
| 1471 | // Otherwise we hold a reference to them. |
| 1472 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1473 | if (oldCallbacks == null) { |
| 1474 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1475 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 1476 | return; |
| 1477 | } |
| 1478 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1479 | // Save a copy of all the bg-thread collections |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1480 | ArrayList<ItemInfo> workspaceItems = new ArrayList<>(); |
| 1481 | ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>(); |
| 1482 | ArrayList<Long> orderedScreenIds = new ArrayList<>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1483 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1484 | synchronized (sBgDataModel) { |
| 1485 | workspaceItems.addAll(sBgDataModel.workspaceItems); |
| 1486 | appWidgets.addAll(sBgDataModel.appWidgets); |
| 1487 | orderedScreenIds.addAll(sBgDataModel.workspaceScreens); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1490 | final int currentScreen; |
| 1491 | { |
| 1492 | int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE |
| 1493 | ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen(); |
| 1494 | if (currScreen >= orderedScreenIds.size()) { |
| 1495 | // There may be no workspace screens (just hotseat items and an empty page). |
| 1496 | currScreen = PagedView.INVALID_RESTORE_PAGE; |
| 1497 | } |
| 1498 | currentScreen = currScreen; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1499 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1500 | final boolean validFirstPage = currentScreen >= 0; |
| 1501 | final long currentScreenId = |
| 1502 | validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1503 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1504 | // Separate the items that are on the current screen, and all the other remaining items |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1505 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>(); |
| 1506 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>(); |
| 1507 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>(); |
| 1508 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1509 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1510 | filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1511 | otherWorkspaceItems); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1512 | filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1513 | otherAppWidgets); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1514 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 1515 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 1516 | |
| 1517 | // Tell the workspace that we're about to start binding items |
| 1518 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1519 | public void run() { |
| 1520 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1521 | if (callbacks != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1522 | callbacks.clearPendingBinds(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1523 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1524 | } |
| 1525 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1526 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1527 | runOnMainThread(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1528 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1529 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 1530 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1531 | Executor mainExecutor = new DeferredMainThreadExecutor(); |
| 1532 | // Load items on the current page. |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1533 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1534 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1535 | // In case of validFirstPage, only bind the first screen, and defer binding the |
| 1536 | // remaining screens after first onDraw (and an optional the fade animation whichever |
| 1537 | // happens later). |
| 1538 | // This ensures that the first screen is immediately visible (eg. during rotation) |
| 1539 | // In case of !validFirstPage, bind all pages one after other. |
| 1540 | final Executor deferredExecutor = |
| 1541 | validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor; |
| 1542 | |
| 1543 | mainExecutor.execute(new Runnable() { |
| 1544 | @Override |
| 1545 | public void run() { |
| 1546 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1547 | if (callbacks != null) { |
| 1548 | callbacks.finishFirstPageBind( |
| 1549 | validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null); |
| 1550 | } |
| 1551 | } |
| 1552 | }); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1553 | |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1554 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1555 | |
| 1556 | // Tell the workspace that we're done binding items |
| 1557 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1558 | public void run() { |
| 1559 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1560 | if (callbacks != null) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1561 | callbacks.finishBindingItems(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1562 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1563 | |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1564 | mIsLoadingAndBindingWorkspace = false; |
| 1565 | |
| 1566 | // Run all the bind complete runnables after workspace is bound. |
| 1567 | if (!mBindCompleteRunnables.isEmpty()) { |
| 1568 | synchronized (mBindCompleteRunnables) { |
| 1569 | for (final Runnable r : mBindCompleteRunnables) { |
| 1570 | runOnWorkerThread(r); |
| 1571 | } |
| 1572 | mBindCompleteRunnables.clear(); |
| 1573 | } |
| 1574 | } |
| 1575 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 1576 | // 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] | 1577 | if (DEBUG_LOADERS) { |
| 1578 | Log.d(TAG, "bound workspace in " |
| 1579 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1580 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1581 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1582 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1583 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1584 | deferredExecutor.execute(r); |
| 1585 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1586 | if (validFirstPage) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1587 | r = new Runnable() { |
| 1588 | public void run() { |
| 1589 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1590 | if (callbacks != null) { |
| 1591 | // We are loading synchronously, which means, some of the pages will be |
| 1592 | // bound after first draw. Inform the callbacks that page binding is |
| 1593 | // not complete, and schedule the remaining pages. |
| 1594 | if (currentScreen != PagedView.INVALID_RESTORE_PAGE) { |
| 1595 | callbacks.onPageBoundSynchronously(currentScreen); |
| 1596 | } |
| 1597 | callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor); |
| 1598 | } |
| 1599 | } |
| 1600 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1601 | runOnMainThread(r); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 1602 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1603 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1604 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1605 | private void updateIconCache() { |
| 1606 | // Ignore packages which have a promise icon. |
| 1607 | HashSet<String> packagesToIgnore = new HashSet<>(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1608 | synchronized (sBgDataModel) { |
| 1609 | for (ItemInfo info : sBgDataModel.itemsIdMap) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1610 | if (info instanceof ShortcutInfo) { |
| 1611 | ShortcutInfo si = (ShortcutInfo) info; |
| 1612 | if (si.isPromise() && si.getTargetComponent() != null) { |
| 1613 | packagesToIgnore.add(si.getTargetComponent().getPackageName()); |
| 1614 | } |
| 1615 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 1616 | LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info; |
| 1617 | if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) { |
| 1618 | packagesToIgnore.add(lawi.providerName.getPackageName()); |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | mIconCache.updateDbIcons(packagesToIgnore); |
| 1624 | } |
| 1625 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1626 | private void onlyBindAllApps() { |
| 1627 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1628 | if (oldCallbacks == null) { |
| 1629 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1630 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 1631 | return; |
| 1632 | } |
| 1633 | |
| 1634 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 1635 | @SuppressWarnings("unchecked") |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1636 | final ArrayList<AppInfo> list |
| 1637 | = (ArrayList<AppInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1638 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1639 | public void run() { |
| 1640 | final long t = SystemClock.uptimeMillis(); |
| 1641 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1642 | if (callbacks != null) { |
| 1643 | callbacks.bindAllApplications(list); |
| 1644 | } |
| 1645 | if (DEBUG_LOADERS) { |
| 1646 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
Hyunyoung Song | 747a5bc | 2016-02-08 11:31:33 -0800 | [diff] [blame] | 1647 | + (SystemClock.uptimeMillis() - t) + "ms"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1648 | } |
| 1649 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1650 | }; |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1651 | runOnMainThread(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1652 | } |
| 1653 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1654 | private void loadAllApps() { |
| 1655 | final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1656 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1657 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1658 | if (oldCallbacks == null) { |
| 1659 | // This launcher has exited and nobody bothered to tell us. Just bail. |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1660 | Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1661 | return; |
| 1662 | } |
| 1663 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1664 | final List<UserHandle> profiles = mUserManager.getUserProfiles(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1665 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1666 | // Clear the list of apps |
| 1667 | mBgAllAppsList.clear(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1668 | for (UserHandle user : profiles) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1669 | // Query for the set of apps |
| 1670 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1671 | final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1672 | if (DEBUG_LOADERS) { |
| 1673 | Log.d(TAG, "getActivityList took " |
| 1674 | + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user); |
| 1675 | Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user); |
| 1676 | } |
| 1677 | // Fail if we don't have any apps |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1678 | // TODO: Fix this. Only fail for the current user. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1679 | if (apps == null || apps.isEmpty()) { |
| 1680 | return; |
| 1681 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1682 | boolean quietMode = mUserManager.isQuietModeEnabled(user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1683 | // Create the ApplicationInfos |
| 1684 | for (int i = 0; i < apps.size(); i++) { |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1685 | LauncherActivityInfo app = apps.get(i); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1686 | // This builds the icon bitmaps. |
Sunny Goyal | 24bb66a | 2017-03-21 15:12:01 -0700 | [diff] [blame] | 1687 | mBgAllAppsList.add(new AppInfo(app, user, quietMode), app); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1688 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1689 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1690 | final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user); |
| 1691 | if (heuristic != null) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1692 | final Runnable r = new Runnable() { |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1693 | |
| 1694 | @Override |
| 1695 | public void run() { |
| 1696 | heuristic.processUserApps(apps); |
| 1697 | } |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1698 | }; |
| 1699 | runOnMainThread(new Runnable() { |
| 1700 | |
| 1701 | @Override |
| 1702 | public void run() { |
| 1703 | // Check isLoadingWorkspace on the UI thread, as it is updated on |
| 1704 | // the UI thread. |
| 1705 | if (mIsLoadingAndBindingWorkspace) { |
| 1706 | synchronized (mBindCompleteRunnables) { |
| 1707 | mBindCompleteRunnables.add(r); |
| 1708 | } |
| 1709 | } else { |
| 1710 | runOnWorkerThread(r); |
| 1711 | } |
| 1712 | } |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1713 | }); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1714 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1715 | } |
Bjorn Bringert | 85f418d | 2013-09-06 12:50:05 +0100 | [diff] [blame] | 1716 | // Huh? Shouldn't this be inside the Runnable below? |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1717 | final ArrayList<AppInfo> added = mBgAllAppsList.added; |
| 1718 | mBgAllAppsList.added = new ArrayList<AppInfo>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1719 | |
| 1720 | // Post callback on main thread |
| 1721 | mHandler.post(new Runnable() { |
| 1722 | public void run() { |
Hyunyoung Song | 9892e58 | 2015-05-05 10:07:23 -0700 | [diff] [blame] | 1723 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1724 | final long bindTime = SystemClock.uptimeMillis(); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 1725 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1726 | if (callbacks != null) { |
| 1727 | callbacks.bindAllApplications(added); |
| 1728 | if (DEBUG_LOADERS) { |
| 1729 | Log.d(TAG, "bound " + added.size() + " apps in " |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1730 | + (SystemClock.uptimeMillis() - bindTime) + "ms"); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1731 | } |
| 1732 | } else { |
| 1733 | Log.i(TAG, "not binding apps: no Launcher activity"); |
| 1734 | } |
| 1735 | } |
| 1736 | }); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1737 | // Cleanup any data stored for a deleted user. |
| 1738 | ManagedProfileHeuristic.processAllUsers(profiles, mContext); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1739 | if (DEBUG_LOADERS) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1740 | Log.d(TAG, "Icons processed in " |
| 1741 | + (SystemClock.uptimeMillis() - loadTime) + "ms"); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 1745 | private void loadDeepShortcuts() { |
| 1746 | if (!mModelLoaded) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1747 | sBgDataModel.deepShortcutMap.clear(); |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1748 | DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext); |
| 1749 | mHasShortcutHostPermission = shortcutManager.hasHostPermission(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1750 | if (mHasShortcutHostPermission) { |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1751 | for (UserHandle user : mUserManager.getUserProfiles()) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1752 | if (mUserManager.isUserUnlocked(user)) { |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1753 | List<ShortcutInfoCompat> shortcuts = |
| 1754 | shortcutManager.queryForAllShortcuts(user); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1755 | sBgDataModel.updateDeepShortcutMap(null, user, shortcuts); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1756 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1757 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1758 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1759 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1760 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1761 | } |
| 1762 | |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1763 | public void bindDeepShortcuts() { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1764 | final MultiHashMap<ComponentKey, String> shortcutMapCopy = |
| 1765 | sBgDataModel.deepShortcutMap.clone(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1766 | Runnable r = new Runnable() { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1767 | @Override |
| 1768 | public void run() { |
| 1769 | Callbacks callbacks = getCallback(); |
| 1770 | if (callbacks != null) { |
| 1771 | callbacks.bindDeepShortcutMap(shortcutMapCopy); |
| 1772 | } |
| 1773 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1774 | }; |
| 1775 | runOnMainThread(r); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1778 | /** |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1779 | * Refreshes the cached shortcuts if the shortcut permission has changed. |
| 1780 | * Current implementation simply reloads the workspace, but it can be optimized to |
| 1781 | * use partial updates similar to {@link UserManagerCompat} |
| 1782 | */ |
| 1783 | public void refreshShortcutsIfRequired() { |
Sunny Goyal | f5e3744 | 2016-11-02 10:31:24 -0700 | [diff] [blame] | 1784 | if (Utilities.ATLEAST_NOUGAT_MR1) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1785 | sWorker.removeCallbacks(mShortcutPermissionCheckRunnable); |
| 1786 | sWorker.post(mShortcutPermissionCheckRunnable); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /** |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1791 | * Called when the icons for packages have been updated in the icon cache. |
| 1792 | */ |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1793 | public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) { |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1794 | // If any package icon has changed (app was updated while launcher was dead), |
| 1795 | // update the corresponding shortcuts. |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1796 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
| 1797 | CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages)); |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1800 | void enqueueModelUpdateTask(BaseModelUpdateTask task) { |
Jon Miranda | f28629a | 2017-02-23 15:12:42 -0800 | [diff] [blame] | 1801 | if (!mModelLoaded && mLoaderTask == null) { |
| 1802 | if (DEBUG_LOADERS) { |
| 1803 | Log.d(TAG, "enqueueModelUpdateTask Ignoring task since loader is pending=" + task); |
| 1804 | } |
| 1805 | return; |
| 1806 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1807 | task.init(this); |
| 1808 | runOnWorkerThread(task); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1811 | /** |
| 1812 | * A task to be executed on the current callbacks on the UI thread. |
| 1813 | * If there is no current callbacks, the task is ignored. |
| 1814 | */ |
| 1815 | public interface CallbackTask { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1816 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1817 | void execute(Callbacks callbacks); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1820 | /** |
| 1821 | * A runnable which changes/updates the data model of the launcher based on certain events. |
| 1822 | */ |
| 1823 | public static abstract class BaseModelUpdateTask implements Runnable { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1824 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1825 | private LauncherModel mModel; |
| 1826 | private DeferredHandler mUiHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1827 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1828 | /* package private */ |
| 1829 | void init(LauncherModel model) { |
| 1830 | mModel = model; |
| 1831 | mUiHandler = mModel.mHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1832 | } |
| 1833 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1834 | @Override |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1835 | public void run() { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1836 | if (!mModel.mHasLoaderCompletedOnce) { |
Sunny Goyal | c905efc | 2015-05-06 09:54:53 -0700 | [diff] [blame] | 1837 | // Loader has not yet run. |
| 1838 | return; |
| 1839 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1840 | execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList); |
| 1841 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1842 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1843 | /** |
| 1844 | * Execute the actual task. Called on the worker thread. |
| 1845 | */ |
| 1846 | public abstract void execute( |
| 1847 | LauncherAppState app, BgDataModel dataModel, AllAppsList apps); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1848 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1849 | /** |
| 1850 | * Schedules a {@param task} to be executed on the current callbacks. |
| 1851 | */ |
| 1852 | public final void scheduleCallbackTask(final CallbackTask task) { |
| 1853 | final Callbacks callbacks = mModel.getCallback(); |
| 1854 | mUiHandler.post(new Runnable() { |
| 1855 | public void run() { |
| 1856 | Callbacks cb = mModel.getCallback(); |
| 1857 | if (callbacks == cb && cb != null) { |
| 1858 | task.execute(callbacks); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 1859 | } |
| 1860 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1861 | }); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1862 | } |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame] | 1863 | |
| 1864 | public ModelWriter getModelWriter() { |
| 1865 | // Updates from model task, do not deal with icon position in hotseat. |
| 1866 | return mModel.getWriter(false /* hasVerticalHotseat */); |
| 1867 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1868 | } |
| 1869 | |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1870 | public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) { |
| 1871 | updateAndBindShortcutInfo(new Provider<ShortcutInfo>() { |
| 1872 | @Override |
| 1873 | public ShortcutInfo get() { |
| 1874 | si.updateFromDeepShortcutInfo(info, mApp.getContext()); |
| 1875 | si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext()); |
| 1876 | return si; |
| 1877 | } |
| 1878 | }); |
| 1879 | } |
| 1880 | |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1881 | /** |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1882 | * Utility method to update a shortcut on the background thread. |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1883 | */ |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1884 | public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1885 | enqueueModelUpdateTask(new ExtendedModelTask() { |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1886 | @Override |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1887 | public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1888 | ShortcutInfo info = shortcutProvider.get(); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1889 | ArrayList<ShortcutInfo> update = new ArrayList<>(); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1890 | update.add(info); |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1891 | bindUpdatedShortcuts(update, info.user); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1892 | } |
| 1893 | }); |
| 1894 | } |
| 1895 | |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1896 | private void bindWidgetsModel(final Callbacks callbacks) { |
| 1897 | final MultiHashMap<PackageItemInfo, WidgetItem> widgets |
| 1898 | = mBgWidgetsModel.getWidgetsMap().clone(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1899 | mHandler.post(new Runnable() { |
| 1900 | @Override |
| 1901 | public void run() { |
| 1902 | Callbacks cb = getCallback(); |
| 1903 | if (callbacks == cb && cb != null) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1904 | callbacks.bindAllWidgets(widgets); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1905 | } |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 1906 | } |
| 1907 | }); |
| 1908 | } |
| 1909 | |
Tony Wickham | 86222d2 | 2017-03-29 15:30:43 -0700 | [diff] [blame^] | 1910 | public void refreshAndBindWidgetsAndShortcuts(final Callbacks callbacks, |
| 1911 | final boolean bindFirst, @Nullable final PackageUserKey packageUser) { |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1912 | runOnWorkerThread(new Runnable() { |
| 1913 | @Override |
| 1914 | public void run() { |
| 1915 | if (bindFirst && !mBgWidgetsModel.isEmpty()) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1916 | bindWidgetsModel(callbacks); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1917 | } |
Tony Wickham | 86222d2 | 2017-03-29 15:30:43 -0700 | [diff] [blame^] | 1918 | ArrayList<WidgetItem> widgets = mBgWidgetsModel.update( |
| 1919 | mApp.getContext(), packageUser); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1920 | bindWidgetsModel(callbacks); |
| 1921 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1922 | // update the Widget entries inside DB on the worker thread. |
Tony Wickham | 86222d2 | 2017-03-29 15:30:43 -0700 | [diff] [blame^] | 1923 | mApp.getWidgetCache().removeObsoletePreviews(widgets, packageUser); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1924 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1925 | }); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1928 | static boolean isValidProvider(AppWidgetProviderInfo provider) { |
| 1929 | return (provider != null) && (provider.provider != null) |
| 1930 | && (provider.provider.getPackageName() != null); |
| 1931 | } |
| 1932 | |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 1933 | public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1934 | if (args.length > 0 && TextUtils.equals(args[0], "--all")) { |
| 1935 | writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size()); |
| 1936 | for (AppInfo info : mBgAllAppsList.data) { |
| 1937 | writer.println(prefix + " title=\"" + info.title + "\" iconBitmap=" + info.iconBitmap |
| 1938 | + " componentName=" + info.componentName.getPackageName()); |
| 1939 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1940 | } |
Hyunyoung Song | 3c7d9cb | 2017-01-30 15:11:27 -0800 | [diff] [blame] | 1941 | sBgDataModel.dump(prefix, fd, writer, args); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1942 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1943 | |
| 1944 | public Callbacks getCallback() { |
| 1945 | return mCallbacks != null ? mCallbacks.get() : null; |
| 1946 | } |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1947 | |
| 1948 | /** |
| 1949 | * @return {@link FolderInfo} if its already loaded. |
| 1950 | */ |
| 1951 | public FolderInfo findFolderById(Long folderId) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1952 | synchronized (sBgDataModel) { |
| 1953 | return sBgDataModel.folders.get(folderId); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1954 | } |
| 1955 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1956 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1957 | @Thunk class DeferredMainThreadExecutor implements Executor { |
| 1958 | |
| 1959 | @Override |
| 1960 | public void execute(Runnable command) { |
| 1961 | runOnMainThread(command); |
| 1962 | } |
| 1963 | } |
| 1964 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1965 | /** |
| 1966 | * @return the looper for the worker thread which can be used to start background tasks. |
| 1967 | */ |
| 1968 | public static Looper getWorkerLooper() { |
| 1969 | return sWorkerThread.getLooper(); |
| 1970 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1971 | } |