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.content.pm.PackageManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.net.Uri; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.HandlerThread; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 33 | import android.os.Looper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 35 | import android.os.SystemClock; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 36 | import android.os.Trace; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 37 | import android.os.UserHandle; |
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 | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 75 | import com.android.launcher3.util.ContentWriter; |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 76 | import com.android.launcher3.util.ItemInfoMatcher; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 77 | import com.android.launcher3.util.ManagedProfileHeuristic; |
Tony Wickham | d82a39d | 2016-07-01 15:44:13 -0700 | [diff] [blame] | 78 | import com.android.launcher3.util.MultiHashMap; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.PackageManagerHelper; |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 80 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 2bcbe13 | 2016-11-16 09:23:42 -0800 | [diff] [blame] | 81 | import com.android.launcher3.util.Provider; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 82 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 83 | import com.android.launcher3.util.ViewOnDrawExecutor; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 84 | |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 85 | import java.lang.ref.WeakReference; |
| 86 | import java.net.URISyntaxException; |
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 | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 96 | import java.util.concurrent.Executor; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 97 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 100 | * 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] | 101 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 103 | public class LauncherModel extends BroadcastReceiver |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 104 | implements LauncherAppsCompat.OnAppsChangedCallbackCompat { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 105 | static final boolean DEBUG_LOADERS = false; |
Chris Wren | ee52336 | 2014-09-09 10:09:02 -0400 | [diff] [blame] | 106 | private static final boolean DEBUG_RECEIVER = false; |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 107 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 108 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 109 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 110 | 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] | 111 | private static final long INVALID_SCREEN_ID = -1L; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 112 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 113 | @Thunk final LauncherAppState mApp; |
| 114 | @Thunk final Object mLock = new Object(); |
| 115 | @Thunk DeferredHandler mHandler = new DeferredHandler(); |
| 116 | @Thunk LoaderTask mLoaderTask; |
| 117 | @Thunk boolean mIsLoaderTaskRunning; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 118 | @Thunk boolean mHasLoaderCompletedOnce; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 119 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 120 | @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 121 | static { |
| 122 | sWorkerThread.start(); |
| 123 | } |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 124 | @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 125 | |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 126 | // We start off with everything not loaded. After that, we assume that |
| 127 | // our monitoring of the package manager provides all updates and we never |
| 128 | // need to do a requery. These are only ever touched from the loader thread. |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 129 | private boolean mWorkspaceLoaded; |
| 130 | private boolean mAllAppsLoaded; |
| 131 | private boolean mDeepShortcutsLoaded; |
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() { |
| 151 | if (mDeepShortcutsLoaded) { |
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) { |
| 155 | mApp.reloadWorkspace(); |
| 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 | |
Amith Yamasani | 6cc806d | 2014-05-02 13:47:11 -0700 | [diff] [blame] | 483 | void forceReload() { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 484 | resetLoadedState(true, true); |
| 485 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 486 | // Do this here because if the launcher activity is running it will be restarted. |
| 487 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 488 | // to reload. |
| 489 | startLoaderFromBackground(); |
| 490 | } |
| 491 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 492 | public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) { |
| 493 | synchronized (mLock) { |
| 494 | // Stop any existing loaders first, so they don't set mAllAppsLoaded or |
| 495 | // mWorkspaceLoaded to true later |
| 496 | stopLoaderLocked(); |
| 497 | if (resetAllAppsLoaded) mAllAppsLoaded = false; |
| 498 | if (resetWorkspaceLoaded) mWorkspaceLoaded = false; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 499 | // Always reset deep shortcuts loaded. |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 500 | // TODO: why? |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 501 | mDeepShortcutsLoaded = false; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 505 | /** |
| 506 | * When the launcher is in the background, it's possible for it to miss paired |
| 507 | * configuration changes. So whenever we trigger the loader from the background |
| 508 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 509 | * of doing it now. |
| 510 | */ |
| 511 | public void startLoaderFromBackground() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 512 | Callbacks callbacks = getCallback(); |
| 513 | if (callbacks != null) { |
| 514 | // Only actually run the loader if they're not paused. |
| 515 | if (!callbacks.setLoadOnResume()) { |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 516 | startLoader(callbacks.getCurrentWorkspaceScreen()); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 519 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 520 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 521 | /** |
| 522 | * If there is already a loader task running, tell it to stop. |
| 523 | */ |
| 524 | private void stopLoaderLocked() { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 525 | LoaderTask oldTask = mLoaderTask; |
| 526 | if (oldTask != null) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 527 | oldTask.stopLocked(); |
| 528 | } |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Adam Cohen | 1a85c58 | 2014-09-30 09:48:49 -0700 | [diff] [blame] | 531 | public boolean isCurrentCallbacks(Callbacks callbacks) { |
| 532 | return (mCallbacks != null && mCallbacks.get() == callbacks); |
| 533 | } |
| 534 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 535 | /** |
| 536 | * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible. |
| 537 | * @return true if the page could be bound synchronously. |
| 538 | */ |
| 539 | public boolean startLoader(int synchronousBindPage) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 540 | // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems |
| 541 | InstallShortcutReceiver.enableInstallQueue(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 542 | synchronized (mLock) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 543 | // Don't bother to start the thread if we know it's not going to do anything |
| 544 | if (mCallbacks != null && mCallbacks.get() != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 545 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 546 | // Clear any pending bind-runnables from the synchronized load process. |
| 547 | runOnMainThread(new Runnable() { |
| 548 | public void run() { |
| 549 | oldCallbacks.clearPendingBinds(); |
| 550 | } |
| 551 | }); |
| 552 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 553 | // If there is already one running, tell it to stop. |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 554 | stopLoaderLocked(); |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 555 | mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 556 | // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind. |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 557 | if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded |
| 558 | && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 559 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 560 | return true; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 561 | } else { |
| 562 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 563 | sWorker.post(mLoaderTask); |
| 564 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 565 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 566 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 567 | return false; |
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 | public void stopLoader() { |
| 571 | synchronized (mLock) { |
| 572 | if (mLoaderTask != null) { |
| 573 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 574 | } |
| 575 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 576 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 577 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 578 | /** |
| 579 | * Loads the workspace screen ids in an ordered list. |
| 580 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 581 | public static ArrayList<Long> loadWorkspaceScreensDb(Context context) { |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 582 | final ContentResolver contentResolver = context.getContentResolver(); |
| 583 | final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 584 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 585 | // Get screens ordered by rank. |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 586 | return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query( |
| 587 | screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK)); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 590 | /** |
| 591 | * Runnable for the thread that loads the contents of the launcher: |
| 592 | * - workspace icons |
| 593 | * - widgets |
| 594 | * - all apps icons |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 595 | * - deep shortcuts within apps |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 596 | */ |
| 597 | private class LoaderTask implements Runnable { |
| 598 | private Context mContext; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 599 | private int mPageToBindFirst; |
| 600 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 601 | @Thunk boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 602 | private boolean mStopped; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 603 | @Thunk boolean mLoadAndBindStepFinished; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 604 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 605 | LoaderTask(Context context, int pageToBindFirst) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 606 | mContext = context; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 607 | mPageToBindFirst = pageToBindFirst; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 610 | private void loadAndBindWorkspace() { |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 611 | mIsLoadingAndBindingWorkspace = true; |
| 612 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 613 | // Load the workspace |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 614 | if (DEBUG_LOADERS) { |
| 615 | Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 616 | } |
Michael Jurka | 288a36b | 2011-07-12 16:53:48 -0700 | [diff] [blame] | 617 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 618 | if (!mWorkspaceLoaded) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 619 | loadWorkspace(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 620 | synchronized (LoaderTask.this) { |
| 621 | if (mStopped) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 622 | return; |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 623 | } |
| 624 | mWorkspaceLoaded = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 628 | // Bind the workspace |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 629 | bindWorkspace(mPageToBindFirst); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 630 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 631 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 632 | private void waitForIdle() { |
| 633 | // Wait until the either we're stopped or the other threads are done. |
| 634 | // This way we don't start loading all apps until the workspace has settled |
| 635 | // down. |
| 636 | synchronized (LoaderTask.this) { |
| 637 | final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 638 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 639 | mHandler.postIdle(new Runnable() { |
| 640 | public void run() { |
| 641 | synchronized (LoaderTask.this) { |
| 642 | mLoadAndBindStepFinished = true; |
| 643 | if (DEBUG_LOADERS) { |
| 644 | Log.d(TAG, "done with previous binding step"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 645 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 646 | LoaderTask.this.notify(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 647 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 648 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 649 | }); |
| 650 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 651 | while (!mStopped && !mLoadAndBindStepFinished) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 652 | try { |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 653 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 654 | // wait no longer than 1sec at a time |
| 655 | this.wait(1000); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 656 | } catch (InterruptedException ex) { |
| 657 | // Ignore |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 658 | } |
| 659 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 660 | if (DEBUG_LOADERS) { |
| 661 | Log.d(TAG, "waited " |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 662 | + (SystemClock.uptimeMillis()-workspaceWaitTime) |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 663 | + "ms for previous step to finish binding"); |
| 664 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 665 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 666 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 667 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 668 | void runBindSynchronousPage(int synchronousBindPage) { |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 669 | if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 670 | // Ensure that we have a valid page index to load synchronously |
| 671 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 672 | "valid page index"); |
| 673 | } |
| 674 | if (!mAllAppsLoaded || !mWorkspaceLoaded) { |
| 675 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 676 | // loaded already (we should load everything asynchronously in that case) |
| 677 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 678 | } |
| 679 | synchronized (mLock) { |
| 680 | if (mIsLoaderTaskRunning) { |
| 681 | // Ensure that we are never running the background loading at this point since |
| 682 | // we also touch the background collections |
| 683 | throw new RuntimeException("Error! Background loading is already running"); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // XXX: Throw an exception if we are already loading (since we touch the worker thread |
| 688 | // data structures, we can't allow any other thread to touch that data, but because |
| 689 | // this call is synchronous, we can get away with not locking). |
| 690 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 691 | // The LauncherModel is static in the LauncherAppState and mHandler may have queued |
Adam Cohen | a13a2f2 | 2012-07-23 14:29:15 -0700 | [diff] [blame] | 692 | // operations from the previous activity. We need to ensure that all queued operations |
| 693 | // are executed before any synchronous binding work is done. |
| 694 | mHandler.flush(); |
| 695 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 696 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 697 | // everything else that is to be bound normally (asynchronously). |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 698 | bindWorkspace(synchronousBindPage); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 699 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 700 | // arise from that. |
| 701 | onlyBindAllApps(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 702 | |
| 703 | bindDeepShortcuts(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 706 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 707 | synchronized (mLock) { |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 708 | if (mStopped) { |
| 709 | return; |
| 710 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 711 | mIsLoaderTaskRunning = true; |
| 712 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 713 | // Optimize for end-user experience: if the Launcher is up and // running with the |
| 714 | // All Apps interface in the foreground, load All Apps first. Otherwise, load the |
| 715 | // workspace first (default). |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 716 | keep_running: { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 717 | if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace"); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 718 | loadAndBindWorkspace(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 719 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 720 | if (mStopped) { |
| 721 | break keep_running; |
| 722 | } |
| 723 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 724 | waitForIdle(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 725 | |
| 726 | // second step |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 727 | if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps"); |
| 728 | loadAndBindAllApps(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 729 | |
| 730 | waitForIdle(); |
| 731 | |
| 732 | // third step |
| 733 | if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts"); |
| 734 | loadAndBindDeepShortcuts(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 735 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 736 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 737 | // Clear out this reference, otherwise we end up holding it until all of the |
| 738 | // callback runnables are done. |
| 739 | mContext = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 740 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 741 | synchronized (mLock) { |
| 742 | // If we are still the last one to be scheduled, remove ourselves. |
| 743 | if (mLoaderTask == this) { |
| 744 | mLoaderTask = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 745 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 746 | mIsLoaderTaskRunning = false; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 747 | mHasLoaderCompletedOnce = true; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 748 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | public void stopLocked() { |
| 752 | synchronized (LoaderTask.this) { |
| 753 | mStopped = true; |
| 754 | this.notify(); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 760 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 761 | * object that was around when the deferred message was scheduled, and if there's |
| 762 | * a new Callbacks object around then also return null. This will save us from |
| 763 | * calling onto it with data that will be ignored. |
| 764 | */ |
| 765 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 766 | synchronized (mLock) { |
| 767 | if (mStopped) { |
| 768 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 769 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 770 | |
| 771 | if (mCallbacks == null) { |
| 772 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 773 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 774 | |
| 775 | final Callbacks callbacks = mCallbacks.get(); |
| 776 | if (callbacks != oldCallbacks) { |
| 777 | return null; |
| 778 | } |
| 779 | if (callbacks == null) { |
| 780 | Log.w(TAG, "no mCallbacks"); |
| 781 | return null; |
| 782 | } |
| 783 | |
| 784 | return callbacks; |
| 785 | } |
| 786 | } |
| 787 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 788 | private void loadWorkspace() { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 789 | if (LauncherAppState.PROFILE_STARTUP) { |
| 790 | Trace.beginSection("Loading Workspace"); |
| 791 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 792 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
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(); |
| 796 | final PackageManager manager = context.getPackageManager(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 797 | final boolean isSafeMode = manager.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, |
| 820 | LauncherSettings.Settings.METHOD_DELETE_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; |
| 884 | String intentDescription; |
| 885 | LauncherAppWidgetInfo appWidgetInfo; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 886 | Intent intent; |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 887 | String targetPkg; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 888 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 889 | while (!mStopped && c.moveToNext()) { |
| 890 | try { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 891 | if (c.user == null) { |
| 892 | // User has been deleted, remove the item. |
| 893 | c.markDeleted("User has been deleted"); |
| 894 | continue; |
| 895 | } |
| 896 | |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 897 | boolean allowMissingTarget = false; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 898 | switch (c.itemType) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 899 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 900 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 901 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 902 | intent = c.parseIntent(); |
| 903 | if (intent == null) { |
| 904 | c.markDeleted("Invalid or null intent"); |
| 905 | continue; |
| 906 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 907 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 908 | int disabledState = quietMode.get(c.serialNumber) ? |
| 909 | ShortcutInfo.FLAG_DISABLED_QUIET_USER : 0; |
| 910 | ComponentName cn = intent.getComponent(); |
| 911 | targetPkg = cn == null ? intent.getPackage() : cn.getPackageName(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 912 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 913 | if (!Process.myUserHandle().equals(c.user)) { |
| 914 | if (c.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 915 | c.markDeleted("Legacy shortcuts are only allowed for default user"); |
| 916 | continue; |
| 917 | } else if (c.restoreFlag != 0) { |
| 918 | // Don't restore items for other profiles. |
| 919 | c.markDeleted("Restore from managed profile not supported"); |
| 920 | continue; |
| 921 | } |
| 922 | } |
| 923 | if (TextUtils.isEmpty(targetPkg) && |
| 924 | c.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) { |
| 925 | c.markDeleted("Only legacy shortcuts can have null package"); |
| 926 | continue; |
| 927 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 928 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 929 | // If there is no target package, its an implicit intent |
| 930 | // (legacy shortcut) which is always valid |
| 931 | boolean validTarget = TextUtils.isEmpty(targetPkg) || |
| 932 | launcherApps.isPackageEnabledForProfile(targetPkg, c.user); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 933 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 934 | if (cn != null && validTarget) { |
| 935 | // If the apk is present and the shortcut points to a specific |
| 936 | // component. |
| 937 | |
| 938 | // If the component is already present |
| 939 | if (launcherApps.isActivityEnabledForProfile(cn, c.user)) { |
| 940 | // no special handling necessary for this item |
| 941 | c.markRestored(); |
| 942 | } else { |
| 943 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) { |
| 944 | // We allow auto install apps to have their intent |
| 945 | // updated after an install. |
| 946 | intent = manager.getLaunchIntentForPackage(targetPkg); |
| 947 | if (intent != null) { |
| 948 | c.restoreFlag = 0; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 949 | c.updater().put( |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 950 | LauncherSettings.Favorites.INTENT, |
| 951 | intent.toUri(0)).commit(); |
| 952 | cn = intent.getComponent(); |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 953 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 954 | c.markDeleted("Unable to find a launch target"); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 955 | continue; |
| 956 | } |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 957 | } else { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 958 | // The app is installed but the component is no |
| 959 | // longer available. |
| 960 | c.markDeleted("Invalid component removed: " + cn); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 961 | continue; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 962 | } |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 963 | } |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 964 | } |
| 965 | // else if cn == null => can't infer much, leave it |
| 966 | // else if !validPkg => could be restored icon or missing sd-card |
| 967 | |
| 968 | if (!TextUtils.isEmpty(targetPkg) && !validTarget) { |
| 969 | // Points to a valid app (superset of cn != null) but the apk |
| 970 | // is not available. |
| 971 | |
| 972 | if (c.restoreFlag != 0) { |
| 973 | // Package is not yet available but might be |
| 974 | // installed later. |
| 975 | FileLog.d(TAG, "package not yet restored: " + targetPkg); |
| 976 | |
| 977 | if (c.hasRestoreFlag(ShortcutInfo.FLAG_RESTORE_STARTED)) { |
| 978 | // Restore has started once. |
| 979 | } else if (installingPkgs.containsKey(targetPkg)) { |
| 980 | // App restore has started. Update the flag |
| 981 | c.restoreFlag |= ShortcutInfo.FLAG_RESTORE_STARTED; |
| 982 | c.updater().commit(); |
| 983 | } else { |
| 984 | c.markDeleted("Unrestored app removed: " + targetPkg); |
| 985 | continue; |
| 986 | } |
| 987 | } else if (PackageManagerHelper.isAppOnSdcard( |
| 988 | manager, targetPkg)) { |
| 989 | // Package is present but not available. |
| 990 | disabledState |= ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; |
| 991 | // Add the icon on the workspace anyway. |
| 992 | allowMissingTarget = true; |
| 993 | } else if (!isSdCardReady) { |
| 994 | // SdCard is not ready yet. Package might get available, |
| 995 | // once it is ready. |
| 996 | Log.d(TAG, "Missing pkg, will check later: " + targetPkg); |
| 997 | pendingPackages.addToList(c.user, targetPkg); |
| 998 | // Add the icon on the workspace anyway. |
| 999 | allowMissingTarget = true; |
| 1000 | } else { |
| 1001 | // Do not wait for external media load anymore. |
| 1002 | c.markDeleted("Invalid package removed: " + targetPkg); |
| 1003 | continue; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if (validTarget) { |
| 1008 | // The shortcut points to a valid target (either no target |
| 1009 | // or something which is ready to be used) |
| 1010 | c.markRestored(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1011 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1012 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1013 | boolean useLowResIcon = !c.isOnWorkspaceOrHotseat() && |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1014 | c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW; |
| 1015 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1016 | if (c.restoreFlag != 0) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1017 | // Already verified above that user is same as default user |
| 1018 | info = c.getRestoredItemInfo(intent); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1019 | } else if (c.itemType == |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1020 | LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1021 | info = c.getAppShortcutInfo( |
| 1022 | intent, allowMissingTarget, useLowResIcon); |
| 1023 | } else if (c.itemType == |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1024 | LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1025 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1026 | ShortcutKey key = ShortcutKey.fromIntent(intent, c.user); |
| 1027 | if (unlockedUsers.get(c.serialNumber)) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1028 | ShortcutInfoCompat pinnedShortcut = |
| 1029 | shortcutKeyToPinnedShortcuts.get(key); |
| 1030 | if (pinnedShortcut == null) { |
| 1031 | // The shortcut is no longer valid. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1032 | c.markDeleted("Pinned shortcut not found"); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1033 | continue; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1034 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1035 | info = new ShortcutInfo(pinnedShortcut, context); |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1036 | info.iconBitmap = LauncherIcons |
| 1037 | .createShortcutIcon(pinnedShortcut, context); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1038 | intent = info.intent; |
| 1039 | } else { |
| 1040 | // Create a shortcut info in disabled mode for now. |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1041 | info = c.loadSimpleShortcut(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1042 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1043 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1044 | } else { // item type == ITEM_TYPE_SHORTCUT |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1045 | info = c.loadSimpleShortcut(); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1046 | |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1047 | // Shortcuts are only available on the primary profile |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1048 | if (PackageManagerHelper.isAppSuspended(manager, targetPkg)) { |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1049 | disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1050 | } |
| 1051 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1052 | // App shortcuts that used to be automatically added to Launcher |
| 1053 | // didn't always have the correct intent flags set, so do that |
| 1054 | // here |
| 1055 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1056 | intent.getCategories() != null && |
| 1057 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1058 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1059 | intent.addFlags( |
| 1060 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1061 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1062 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1063 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1064 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1065 | if (info != null) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1066 | c.applyCommonProperties(info); |
| 1067 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1068 | info.intent = intent; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1069 | info.rank = c.getInt(rankIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1070 | info.spanX = 1; |
| 1071 | info.spanY = 1; |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1072 | // TODO: Remove this extra. Instead we should be using |
| 1073 | // itemInfo#user. |
| 1074 | info.intent.putExtra(ItemInfo.EXTRA_PROFILE, c.serialNumber); |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1075 | info.isDisabled |= disabledState; |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1076 | if (isSafeMode && !Utilities.isSystemApp(context, intent)) { |
| 1077 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; |
| 1078 | } |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1079 | |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1080 | if (c.restoreFlag != 0 && !TextUtils.isEmpty(targetPkg)) { |
| 1081 | Integer progress = installingPkgs.get(targetPkg); |
| 1082 | if (progress != null) { |
| 1083 | info.setInstallProgress(progress); |
| 1084 | } else { |
| 1085 | info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1089 | c.checkAndAddItem(info, sBgDataModel); |
Winson Chung | 1323b48 | 2013-08-05 12:41:55 -0700 | [diff] [blame] | 1090 | } else { |
| 1091 | throw new RuntimeException("Unexpected null ShortcutInfo"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1092 | } |
| 1093 | break; |
| 1094 | |
| 1095 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1096 | FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(c.id); |
| 1097 | c.applyCommonProperties(folderInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1098 | |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 1099 | // 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] | 1100 | folderInfo.title = c.getString(c.titleIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1101 | folderInfo.spanX = 1; |
| 1102 | folderInfo.spanY = 1; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1103 | folderInfo.options = c.getInt(optionsIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1104 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1105 | // no special handling required for restored folders |
| 1106 | c.markRestored(); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1107 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1108 | c.checkAndAddItem(folderInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1109 | break; |
| 1110 | |
| 1111 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1112 | case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1113 | // Read all Launcher-specific widget details |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1114 | boolean customWidget = c.itemType == |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1115 | LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 1116 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1117 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1118 | String savedProvider = c.getString(appWidgetProviderIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1119 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1120 | final ComponentName component = |
| 1121 | ComponentName.unflattenFromString(savedProvider); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1122 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1123 | final boolean isIdValid = !c.hasRestoreFlag( |
| 1124 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); |
| 1125 | final boolean wasProviderReady = !c.hasRestoreFlag( |
| 1126 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1127 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1128 | if (widgetProvidersMap == null) { |
| 1129 | widgetProvidersMap = AppWidgetManagerCompat |
| 1130 | .getInstance(mContext).getAllProvidersMap(); |
| 1131 | } |
| 1132 | final AppWidgetProviderInfo provider = widgetProvidersMap.get( |
| 1133 | new ComponentKey( |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1134 | ComponentName.unflattenFromString(savedProvider), |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1135 | c.user)); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1136 | |
| 1137 | final boolean isProviderReady = isValidProvider(provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1138 | if (!isSafeMode && !customWidget && |
| 1139 | wasProviderReady && !isProviderReady) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1140 | c.markDeleted( |
| 1141 | "Deleting widget that isn't installed anymore: " |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1142 | + provider); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1143 | } else { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1144 | if (isProviderReady) { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1145 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 1146 | provider.provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1147 | |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1148 | // The provider is available. So the widget is either |
| 1149 | // available or not available. We do not need to track |
| 1150 | // any future restore updates. |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1151 | int status = c.restoreFlag & |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1152 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1153 | if (!wasProviderReady) { |
| 1154 | // If provider was not previously ready, update the |
| 1155 | // status and UI flag. |
| 1156 | |
| 1157 | // Id would be valid only if the widget restore broadcast was received. |
| 1158 | if (isIdValid) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1159 | status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1160 | } else { |
| 1161 | status &= ~LauncherAppWidgetInfo |
| 1162 | .FLAG_PROVIDER_NOT_READY; |
| 1163 | } |
| 1164 | } |
| 1165 | appWidgetInfo.restoreStatus = status; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1166 | } else { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1167 | Log.v(TAG, "Widget restore pending id=" + c.id |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1168 | + " appWidgetId=" + appWidgetId |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1169 | + " status =" + c.restoreFlag); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1170 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1171 | component); |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1172 | appWidgetInfo.restoreStatus = c.restoreFlag; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1173 | Integer installProgress = installingPkgs.get(component.getPackageName()); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1174 | |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1175 | if (c.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_RESTORE_STARTED)) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1176 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1177 | } else if (installProgress != null) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1178 | // App restore has started. Update the flag |
| 1179 | appWidgetInfo.restoreStatus |= |
| 1180 | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1181 | } else if (!isSafeMode) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1182 | c.markDeleted("Unrestored widget removed: " + component); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1183 | continue; |
| 1184 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1185 | |
| 1186 | appWidgetInfo.installProgress = |
| 1187 | installProgress == null ? 0 : installProgress; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1188 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1189 | if (appWidgetInfo.hasRestoreFlag( |
| 1190 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) { |
Sunny Goyal | 81e4491 | 2017-01-14 15:05:14 -0800 | [diff] [blame] | 1191 | appWidgetInfo.bindOptions = c.parseIntent(); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1192 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1193 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1194 | c.applyCommonProperties(appWidgetInfo); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1195 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 1196 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1197 | appWidgetInfo.user = c.user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1198 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1199 | if (!c.isOnWorkspaceOrHotseat()) { |
| 1200 | c.markDeleted("Widget found where container != " + |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1201 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1204 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1205 | if (!customWidget) { |
| 1206 | String providerName = |
| 1207 | appWidgetInfo.providerName.flattenToString(); |
| 1208 | if (!providerName.equals(savedProvider) || |
Sunny Goyal | c1ad0ce | 2017-01-11 14:33:38 -0800 | [diff] [blame] | 1209 | (appWidgetInfo.restoreStatus != c.restoreFlag)) { |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1210 | c.updater() |
| 1211 | .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, |
| 1212 | providerName) |
| 1213 | .put(LauncherSettings.Favorites.RESTORED, |
| 1214 | appWidgetInfo.restoreStatus) |
| 1215 | .commit(); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1216 | } |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1217 | } |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1218 | c.checkAndAddItem(appWidgetInfo, sBgDataModel); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1219 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1220 | break; |
| 1221 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1222 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1223 | Log.e(TAG, "Desktop items loading interrupted", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1224 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } finally { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1227 | Utilities.closeSilently(c); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1230 | // Break early if we've stopped loading |
| 1231 | if (mStopped) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1232 | sBgDataModel.clear(); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1233 | return; |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1236 | // Remove dead items |
| 1237 | if (c.commitDeleted()) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1238 | // Remove any empty folder |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1239 | ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings |
| 1240 | .call(contentResolver, |
| 1241 | LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS) |
| 1242 | .getSerializable(LauncherSettings.Settings.EXTRA_VALUE); |
| 1243 | for (long folderId : deletedFolderIds) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1244 | sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId)); |
| 1245 | sBgDataModel.folders.remove(folderId); |
| 1246 | sBgDataModel.itemsIdMap.remove(folderId); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1250 | // Unpin shortcuts that don't exist on the workspace. |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1251 | HashSet<ShortcutKey> pendingShortcuts = |
| 1252 | InstallShortcutReceiver.getPendingShortcuts(context); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1253 | for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1254 | MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key); |
Sunny Goyal | f75baa9 | 2016-11-22 03:23:51 +0530 | [diff] [blame] | 1255 | if ((numTimesPinned == null || numTimesPinned.value == 0) |
| 1256 | && !pendingShortcuts.contains(key)) { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1257 | // Shortcut is pinned but doesn't exist on the workspace; unpin it. |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1258 | shortcutManager.unpinShortcut(key); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1262 | // 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] | 1263 | for (FolderInfo folder : sBgDataModel.folders) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1264 | Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR); |
| 1265 | int pos = 0; |
| 1266 | for (ShortcutInfo info : folder.contents) { |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 1267 | if (info.usingLowResIcon && |
| 1268 | info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | 3fe4a14 | 2016-12-15 17:40:07 -0800 | [diff] [blame] | 1269 | mIconCache.getTitleAndIcon(info, false); |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1270 | } |
| 1271 | pos ++; |
| 1272 | if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) { |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1278 | c.commitRestoredItems(); |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1279 | if (!isSdCardReady && !pendingPackages.isEmpty()) { |
| 1280 | context.registerReceiver( |
| 1281 | new SdCardAvailableReceiver( |
| 1282 | LauncherModel.this, mContext, pendingPackages), |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1283 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1284 | null, |
| 1285 | sWorker); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1288 | // Remove any empty screens |
Sunny Goyal | aaf86fe | 2017-01-05 21:50:27 -0800 | [diff] [blame] | 1289 | ArrayList<Long> unusedScreens = new ArrayList<>(sBgDataModel.workspaceScreens); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1290 | for (ItemInfo item: sBgDataModel.itemsIdMap) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1291 | long screenId = item.screenId; |
| 1292 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1293 | unusedScreens.contains(screenId)) { |
| 1294 | unusedScreens.remove(screenId); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | // If there are any empty screens remove them, and update. |
| 1299 | if (unusedScreens.size() != 0) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1300 | sBgDataModel.workspaceScreens.removeAll(unusedScreens); |
| 1301 | updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1302 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1303 | } |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 1304 | if (LauncherAppState.PROFILE_STARTUP) { |
| 1305 | Trace.endSection(); |
| 1306 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1309 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 1310 | * specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1311 | private void filterCurrentWorkspaceItems(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1312 | ArrayList<ItemInfo> allWorkspaceItems, |
| 1313 | ArrayList<ItemInfo> currentScreenItems, |
| 1314 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1315 | // Purge any null ItemInfos |
| 1316 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 1317 | while (iter.hasNext()) { |
| 1318 | ItemInfo i = iter.next(); |
| 1319 | if (i == null) { |
| 1320 | iter.remove(); |
| 1321 | } |
| 1322 | } |
| 1323 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1324 | // Order the set of items by their containers first, this allows use to walk through the |
| 1325 | // list sequentially, build up a list of containers that are in the specified screen, |
| 1326 | // as well as all items in those containers. |
| 1327 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 1328 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 1329 | @Override |
| 1330 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 1331 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1332 | } |
| 1333 | }); |
| 1334 | for (ItemInfo info : allWorkspaceItems) { |
| 1335 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1336 | if (info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1337 | currentScreenItems.add(info); |
| 1338 | itemsOnScreen.add(info.id); |
| 1339 | } else { |
| 1340 | otherScreenItems.add(info); |
| 1341 | } |
| 1342 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1343 | currentScreenItems.add(info); |
| 1344 | itemsOnScreen.add(info.id); |
| 1345 | } else { |
| 1346 | if (itemsOnScreen.contains(info.container)) { |
| 1347 | currentScreenItems.add(info); |
| 1348 | itemsOnScreen.add(info.id); |
| 1349 | } else { |
| 1350 | otherScreenItems.add(info); |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | /** Filters the set of widgets which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1357 | private void filterCurrentAppWidgets(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1358 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1359 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 1360 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1361 | |
| 1362 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1363 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1364 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1365 | widget.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1366 | currentScreenWidgets.add(widget); |
| 1367 | } else { |
| 1368 | otherScreenWidgets.add(widget); |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1373 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 1374 | * right) */ |
| 1375 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1376 | final InvariantDeviceProfile profile = mApp.getInvariantDeviceProfile(); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1377 | final int screenCols = profile.numColumns; |
| 1378 | final int screenCellCount = profile.numColumns * profile.numRows; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1379 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1380 | @Override |
| 1381 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1382 | if (lhs.container == rhs.container) { |
| 1383 | // Within containers, order by their spatial position in that container |
| 1384 | switch ((int) lhs.container) { |
| 1385 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: { |
| 1386 | long lr = (lhs.screenId * screenCellCount + |
| 1387 | lhs.cellY * screenCols + lhs.cellX); |
| 1388 | long rr = (rhs.screenId * screenCellCount + |
| 1389 | rhs.cellY * screenCols + rhs.cellX); |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1390 | return Utilities.longCompare(lr, rr); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1391 | } |
| 1392 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: { |
| 1393 | // We currently use the screen id as the rank |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1394 | return Utilities.longCompare(lhs.screenId, rhs.screenId); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1395 | } |
| 1396 | default: |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 1397 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1398 | throw new RuntimeException("Unexpected container type when " + |
| 1399 | "sorting workspace items."); |
| 1400 | } |
| 1401 | return 0; |
| 1402 | } |
| 1403 | } else { |
| 1404 | // Between containers, order by hotseat, desktop |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1405 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1406 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1407 | } |
| 1408 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1409 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1410 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1411 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 1412 | final ArrayList<Long> orderedScreens) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1413 | final Runnable r = new Runnable() { |
| 1414 | @Override |
| 1415 | public void run() { |
| 1416 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1417 | if (callbacks != null) { |
| 1418 | callbacks.bindScreens(orderedScreens); |
| 1419 | } |
| 1420 | } |
| 1421 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1422 | runOnMainThread(r); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1425 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 1426 | final ArrayList<ItemInfo> workspaceItems, |
| 1427 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1428 | final Executor executor) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1429 | |
| 1430 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1431 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1432 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1433 | final int start = i; |
| 1434 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1435 | final Runnable r = new Runnable() { |
| 1436 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1437 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1438 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1439 | if (callbacks != null) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1440 | callbacks.bindItems(workspaceItems, start, start+chunkSize, |
| 1441 | false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1442 | } |
| 1443 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1444 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1445 | executor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1446 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1447 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1448 | // Bind the widgets, one at a time |
| 1449 | N = appWidgets.size(); |
| 1450 | for (int i = 0; i < N; i++) { |
| 1451 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 1452 | final Runnable r = new Runnable() { |
| 1453 | public void run() { |
| 1454 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1455 | if (callbacks != null) { |
| 1456 | callbacks.bindAppWidget(widget); |
| 1457 | } |
| 1458 | } |
| 1459 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1460 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * Binds all loaded data to actual views on the main thread. |
| 1466 | */ |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1467 | private void bindWorkspace(int synchronizeBindPage) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1468 | final long t = SystemClock.uptimeMillis(); |
| 1469 | Runnable r; |
| 1470 | |
| 1471 | // Don't use these two variables in any of the callback runnables. |
| 1472 | // Otherwise we hold a reference to them. |
| 1473 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1474 | if (oldCallbacks == null) { |
| 1475 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1476 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 1477 | return; |
| 1478 | } |
| 1479 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1480 | // Save a copy of all the bg-thread collections |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1481 | ArrayList<ItemInfo> workspaceItems = new ArrayList<>(); |
| 1482 | ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>(); |
| 1483 | ArrayList<Long> orderedScreenIds = new ArrayList<>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1484 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1485 | synchronized (sBgDataModel) { |
| 1486 | workspaceItems.addAll(sBgDataModel.workspaceItems); |
| 1487 | appWidgets.addAll(sBgDataModel.appWidgets); |
| 1488 | orderedScreenIds.addAll(sBgDataModel.workspaceScreens); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1491 | final int currentScreen; |
| 1492 | { |
| 1493 | int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE |
| 1494 | ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen(); |
| 1495 | if (currScreen >= orderedScreenIds.size()) { |
| 1496 | // There may be no workspace screens (just hotseat items and an empty page). |
| 1497 | currScreen = PagedView.INVALID_RESTORE_PAGE; |
| 1498 | } |
| 1499 | currentScreen = currScreen; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1500 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1501 | final boolean validFirstPage = currentScreen >= 0; |
| 1502 | final long currentScreenId = |
| 1503 | validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1504 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1505 | // 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] | 1506 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>(); |
| 1507 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>(); |
| 1508 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>(); |
| 1509 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1510 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1511 | filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1512 | otherWorkspaceItems); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1513 | filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1514 | otherAppWidgets); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1515 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 1516 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 1517 | |
| 1518 | // Tell the workspace that we're about to start binding items |
| 1519 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1520 | public void run() { |
| 1521 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1522 | if (callbacks != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1523 | callbacks.clearPendingBinds(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1524 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1525 | } |
| 1526 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1527 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1528 | runOnMainThread(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1529 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1530 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 1531 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1532 | Executor mainExecutor = new DeferredMainThreadExecutor(); |
| 1533 | // Load items on the current page. |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1534 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1535 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1536 | // In case of validFirstPage, only bind the first screen, and defer binding the |
| 1537 | // remaining screens after first onDraw (and an optional the fade animation whichever |
| 1538 | // happens later). |
| 1539 | // This ensures that the first screen is immediately visible (eg. during rotation) |
| 1540 | // In case of !validFirstPage, bind all pages one after other. |
| 1541 | final Executor deferredExecutor = |
| 1542 | validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor; |
| 1543 | |
| 1544 | mainExecutor.execute(new Runnable() { |
| 1545 | @Override |
| 1546 | public void run() { |
| 1547 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1548 | if (callbacks != null) { |
| 1549 | callbacks.finishFirstPageBind( |
| 1550 | validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null); |
| 1551 | } |
| 1552 | } |
| 1553 | }); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1554 | |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 1555 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1556 | |
| 1557 | // Tell the workspace that we're done binding items |
| 1558 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1559 | public void run() { |
| 1560 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1561 | if (callbacks != null) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1562 | callbacks.finishBindingItems(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1563 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1564 | |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1565 | mIsLoadingAndBindingWorkspace = false; |
| 1566 | |
| 1567 | // Run all the bind complete runnables after workspace is bound. |
| 1568 | if (!mBindCompleteRunnables.isEmpty()) { |
| 1569 | synchronized (mBindCompleteRunnables) { |
| 1570 | for (final Runnable r : mBindCompleteRunnables) { |
| 1571 | runOnWorkerThread(r); |
| 1572 | } |
| 1573 | mBindCompleteRunnables.clear(); |
| 1574 | } |
| 1575 | } |
| 1576 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 1577 | // 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] | 1578 | if (DEBUG_LOADERS) { |
| 1579 | Log.d(TAG, "bound workspace in " |
| 1580 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1581 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1582 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1583 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1584 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1585 | deferredExecutor.execute(r); |
| 1586 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 1587 | if (validFirstPage) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1588 | r = new Runnable() { |
| 1589 | public void run() { |
| 1590 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1591 | if (callbacks != null) { |
| 1592 | // We are loading synchronously, which means, some of the pages will be |
| 1593 | // bound after first draw. Inform the callbacks that page binding is |
| 1594 | // not complete, and schedule the remaining pages. |
| 1595 | if (currentScreen != PagedView.INVALID_RESTORE_PAGE) { |
| 1596 | callbacks.onPageBoundSynchronously(currentScreen); |
| 1597 | } |
| 1598 | callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor); |
| 1599 | } |
| 1600 | } |
| 1601 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1602 | runOnMainThread(r); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 1603 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1604 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1605 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1606 | private void loadAndBindAllApps() { |
| 1607 | if (DEBUG_LOADERS) { |
| 1608 | Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded); |
| 1609 | } |
| 1610 | if (!mAllAppsLoaded) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1611 | loadAllApps(); |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 1612 | synchronized (LoaderTask.this) { |
| 1613 | if (mStopped) { |
| 1614 | return; |
| 1615 | } |
| 1616 | } |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1617 | updateIconCache(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1618 | synchronized (LoaderTask.this) { |
| 1619 | if (mStopped) { |
| 1620 | return; |
| 1621 | } |
| 1622 | mAllAppsLoaded = true; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1623 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1624 | } else { |
| 1625 | onlyBindAllApps(); |
| 1626 | } |
| 1627 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1628 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1629 | private void updateIconCache() { |
| 1630 | // Ignore packages which have a promise icon. |
| 1631 | HashSet<String> packagesToIgnore = new HashSet<>(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1632 | synchronized (sBgDataModel) { |
| 1633 | for (ItemInfo info : sBgDataModel.itemsIdMap) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1634 | if (info instanceof ShortcutInfo) { |
| 1635 | ShortcutInfo si = (ShortcutInfo) info; |
| 1636 | if (si.isPromise() && si.getTargetComponent() != null) { |
| 1637 | packagesToIgnore.add(si.getTargetComponent().getPackageName()); |
| 1638 | } |
| 1639 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 1640 | LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info; |
| 1641 | if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) { |
| 1642 | packagesToIgnore.add(lawi.providerName.getPackageName()); |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | mIconCache.updateDbIcons(packagesToIgnore); |
| 1648 | } |
| 1649 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1650 | private void onlyBindAllApps() { |
| 1651 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1652 | if (oldCallbacks == null) { |
| 1653 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 1654 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 1659 | @SuppressWarnings("unchecked") |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1660 | final ArrayList<AppInfo> list |
| 1661 | = (ArrayList<AppInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1662 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1663 | public void run() { |
| 1664 | final long t = SystemClock.uptimeMillis(); |
| 1665 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1666 | if (callbacks != null) { |
| 1667 | callbacks.bindAllApplications(list); |
| 1668 | } |
| 1669 | if (DEBUG_LOADERS) { |
| 1670 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
Hyunyoung Song | 747a5bc | 2016-02-08 11:31:33 -0800 | [diff] [blame] | 1671 | + (SystemClock.uptimeMillis() - t) + "ms"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1672 | } |
| 1673 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 1674 | }; |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1675 | runOnMainThread(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1676 | } |
| 1677 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1678 | private void loadAllApps() { |
| 1679 | final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1680 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1681 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 1682 | if (oldCallbacks == null) { |
| 1683 | // This launcher has exited and nobody bothered to tell us. Just bail. |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1684 | Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1685 | return; |
| 1686 | } |
| 1687 | |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1688 | final List<UserHandle> profiles = mUserManager.getUserProfiles(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1689 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1690 | // Clear the list of apps |
| 1691 | mBgAllAppsList.clear(); |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1692 | for (UserHandle user : profiles) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1693 | // Query for the set of apps |
| 1694 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1695 | final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1696 | if (DEBUG_LOADERS) { |
| 1697 | Log.d(TAG, "getActivityList took " |
| 1698 | + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user); |
| 1699 | Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user); |
| 1700 | } |
| 1701 | // Fail if we don't have any apps |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1702 | // TODO: Fix this. Only fail for the current user. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1703 | if (apps == null || apps.isEmpty()) { |
| 1704 | return; |
| 1705 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1706 | boolean quietMode = mUserManager.isQuietModeEnabled(user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1707 | // Create the ApplicationInfos |
| 1708 | for (int i = 0; i < apps.size(); i++) { |
Sunny Goyal | 3e9be43 | 2017-01-05 15:22:41 -0800 | [diff] [blame] | 1709 | LauncherActivityInfo app = apps.get(i); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1710 | // This builds the icon bitmaps. |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1711 | mBgAllAppsList.add(new AppInfo(mContext, app, user, quietMode), app); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1712 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1713 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1714 | final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user); |
| 1715 | if (heuristic != null) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1716 | final Runnable r = new Runnable() { |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1717 | |
| 1718 | @Override |
| 1719 | public void run() { |
| 1720 | heuristic.processUserApps(apps); |
| 1721 | } |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 1722 | }; |
| 1723 | runOnMainThread(new Runnable() { |
| 1724 | |
| 1725 | @Override |
| 1726 | public void run() { |
| 1727 | // Check isLoadingWorkspace on the UI thread, as it is updated on |
| 1728 | // the UI thread. |
| 1729 | if (mIsLoadingAndBindingWorkspace) { |
| 1730 | synchronized (mBindCompleteRunnables) { |
| 1731 | mBindCompleteRunnables.add(r); |
| 1732 | } |
| 1733 | } else { |
| 1734 | runOnWorkerThread(r); |
| 1735 | } |
| 1736 | } |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1737 | }); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1738 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1739 | } |
Bjorn Bringert | 85f418d | 2013-09-06 12:50:05 +0100 | [diff] [blame] | 1740 | // Huh? Shouldn't this be inside the Runnable below? |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1741 | final ArrayList<AppInfo> added = mBgAllAppsList.added; |
| 1742 | mBgAllAppsList.added = new ArrayList<AppInfo>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1743 | |
| 1744 | // Post callback on main thread |
| 1745 | mHandler.post(new Runnable() { |
| 1746 | public void run() { |
Hyunyoung Song | 9892e58 | 2015-05-05 10:07:23 -0700 | [diff] [blame] | 1747 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1748 | final long bindTime = SystemClock.uptimeMillis(); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 1749 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1750 | if (callbacks != null) { |
| 1751 | callbacks.bindAllApplications(added); |
| 1752 | if (DEBUG_LOADERS) { |
| 1753 | Log.d(TAG, "bound " + added.size() + " apps in " |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1754 | + (SystemClock.uptimeMillis() - bindTime) + "ms"); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1755 | } |
| 1756 | } else { |
| 1757 | Log.i(TAG, "not binding apps: no Launcher activity"); |
| 1758 | } |
| 1759 | } |
| 1760 | }); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1761 | // Cleanup any data stored for a deleted user. |
| 1762 | ManagedProfileHeuristic.processAllUsers(profiles, mContext); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1763 | if (DEBUG_LOADERS) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1764 | Log.d(TAG, "Icons processed in " |
| 1765 | + (SystemClock.uptimeMillis() - loadTime) + "ms"); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1769 | private void loadAndBindDeepShortcuts() { |
| 1770 | if (DEBUG_LOADERS) { |
| 1771 | Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded); |
| 1772 | } |
| 1773 | if (!mDeepShortcutsLoaded) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1774 | sBgDataModel.deepShortcutMap.clear(); |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1775 | DeepShortcutManager shortcutManager = DeepShortcutManager.getInstance(mContext); |
| 1776 | mHasShortcutHostPermission = shortcutManager.hasHostPermission(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1777 | if (mHasShortcutHostPermission) { |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1778 | for (UserHandle user : mUserManager.getUserProfiles()) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1779 | if (mUserManager.isUserUnlocked(user)) { |
Sunny Goyal | dde4fd9 | 2016-11-21 16:02:39 +0530 | [diff] [blame] | 1780 | List<ShortcutInfoCompat> shortcuts = |
| 1781 | shortcutManager.queryForAllShortcuts(user); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1782 | sBgDataModel.updateDeepShortcutMap(null, user, shortcuts); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1783 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1784 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1785 | } |
| 1786 | synchronized (LoaderTask.this) { |
| 1787 | if (mStopped) { |
| 1788 | return; |
| 1789 | } |
| 1790 | mDeepShortcutsLoaded = true; |
| 1791 | } |
| 1792 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1793 | bindDeepShortcuts(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1796 | public void dumpState() { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1797 | synchronized (sBgDataModel) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1798 | Log.d(TAG, "mLoaderTask.mContext=" + mContext); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1799 | Log.d(TAG, "mLoaderTask.mStopped=" + mStopped); |
| 1800 | Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1801 | Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size()); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1802 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1806 | public void bindDeepShortcuts() { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 1807 | final MultiHashMap<ComponentKey, String> shortcutMapCopy = |
| 1808 | sBgDataModel.deepShortcutMap.clone(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1809 | Runnable r = new Runnable() { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1810 | @Override |
| 1811 | public void run() { |
| 1812 | Callbacks callbacks = getCallback(); |
| 1813 | if (callbacks != null) { |
| 1814 | callbacks.bindDeepShortcutMap(shortcutMapCopy); |
| 1815 | } |
| 1816 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1817 | }; |
| 1818 | runOnMainThread(r); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1821 | /** |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1822 | * Refreshes the cached shortcuts if the shortcut permission has changed. |
| 1823 | * Current implementation simply reloads the workspace, but it can be optimized to |
| 1824 | * use partial updates similar to {@link UserManagerCompat} |
| 1825 | */ |
| 1826 | public void refreshShortcutsIfRequired() { |
Sunny Goyal | f5e3744 | 2016-11-02 10:31:24 -0700 | [diff] [blame] | 1827 | if (Utilities.ATLEAST_NOUGAT_MR1) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 1828 | sWorker.removeCallbacks(mShortcutPermissionCheckRunnable); |
| 1829 | sWorker.post(mShortcutPermissionCheckRunnable); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | /** |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1834 | * Called when the icons for packages have been updated in the icon cache. |
| 1835 | */ |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 1836 | public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) { |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1837 | // If any package icon has changed (app was updated while launcher was dead), |
| 1838 | // update the corresponding shortcuts. |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1839 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
| 1840 | CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages)); |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1843 | void enqueueModelUpdateTask(BaseModelUpdateTask task) { |
| 1844 | task.init(this); |
| 1845 | runOnWorkerThread(task); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1848 | /** |
| 1849 | * A task to be executed on the current callbacks on the UI thread. |
| 1850 | * If there is no current callbacks, the task is ignored. |
| 1851 | */ |
| 1852 | public interface CallbackTask { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1853 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1854 | void execute(Callbacks callbacks); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1857 | /** |
| 1858 | * A runnable which changes/updates the data model of the launcher based on certain events. |
| 1859 | */ |
| 1860 | public static abstract class BaseModelUpdateTask implements Runnable { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1861 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1862 | private LauncherModel mModel; |
| 1863 | private DeferredHandler mUiHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1864 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1865 | /* package private */ |
| 1866 | void init(LauncherModel model) { |
| 1867 | mModel = model; |
| 1868 | mUiHandler = mModel.mHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1869 | } |
| 1870 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1871 | @Override |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1872 | public void run() { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1873 | if (!mModel.mHasLoaderCompletedOnce) { |
Sunny Goyal | c905efc | 2015-05-06 09:54:53 -0700 | [diff] [blame] | 1874 | // Loader has not yet run. |
| 1875 | return; |
| 1876 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1877 | execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList); |
| 1878 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1879 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1880 | /** |
| 1881 | * Execute the actual task. Called on the worker thread. |
| 1882 | */ |
| 1883 | public abstract void execute( |
| 1884 | LauncherAppState app, BgDataModel dataModel, AllAppsList apps); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1885 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1886 | /** |
| 1887 | * Schedules a {@param task} to be executed on the current callbacks. |
| 1888 | */ |
| 1889 | public final void scheduleCallbackTask(final CallbackTask task) { |
| 1890 | final Callbacks callbacks = mModel.getCallback(); |
| 1891 | mUiHandler.post(new Runnable() { |
| 1892 | public void run() { |
| 1893 | Callbacks cb = mModel.getCallback(); |
| 1894 | if (callbacks == cb && cb != null) { |
| 1895 | task.execute(callbacks); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 1896 | } |
| 1897 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1898 | }); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1899 | } |
Sunny Goyal | 43bf11d | 2017-02-02 13:52:53 -0800 | [diff] [blame^] | 1900 | |
| 1901 | public ModelWriter getModelWriter() { |
| 1902 | // Updates from model task, do not deal with icon position in hotseat. |
| 1903 | return mModel.getWriter(false /* hasVerticalHotseat */); |
| 1904 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Sunny Goyal | 1b07263 | 2017-01-18 11:30:23 -0800 | [diff] [blame] | 1907 | public void updateAndBindShortcutInfo(final ShortcutInfo si, final ShortcutInfoCompat info) { |
| 1908 | updateAndBindShortcutInfo(new Provider<ShortcutInfo>() { |
| 1909 | @Override |
| 1910 | public ShortcutInfo get() { |
| 1911 | si.updateFromDeepShortcutInfo(info, mApp.getContext()); |
| 1912 | si.iconBitmap = LauncherIcons.createShortcutIcon(info, mApp.getContext()); |
| 1913 | return si; |
| 1914 | } |
| 1915 | }); |
| 1916 | } |
| 1917 | |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1918 | /** |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1919 | * Utility method to update a shortcut on the background thread. |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1920 | */ |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1921 | public void updateAndBindShortcutInfo(final Provider<ShortcutInfo> shortcutProvider) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1922 | enqueueModelUpdateTask(new ExtendedModelTask() { |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1923 | @Override |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1924 | public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1925 | ShortcutInfo info = shortcutProvider.get(); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 1926 | ArrayList<ShortcutInfo> update = new ArrayList<>(); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1927 | update.add(info); |
Sunny Goyal | 1cc1c9a | 2017-01-06 16:32:57 -0800 | [diff] [blame] | 1928 | bindUpdatedShortcuts(update, info.user); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 1929 | } |
| 1930 | }); |
| 1931 | } |
| 1932 | |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1933 | private void bindWidgetsModel(final Callbacks callbacks) { |
| 1934 | final MultiHashMap<PackageItemInfo, WidgetItem> widgets |
| 1935 | = mBgWidgetsModel.getWidgetsMap().clone(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1936 | mHandler.post(new Runnable() { |
| 1937 | @Override |
| 1938 | public void run() { |
| 1939 | Callbacks cb = getCallback(); |
| 1940 | if (callbacks == cb && cb != null) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1941 | callbacks.bindAllWidgets(widgets); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1942 | } |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 1943 | } |
| 1944 | }); |
| 1945 | } |
| 1946 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1947 | public void refreshAndBindWidgetsAndShortcuts( |
| 1948 | final Callbacks callbacks, final boolean bindFirst) { |
| 1949 | runOnWorkerThread(new Runnable() { |
| 1950 | @Override |
| 1951 | public void run() { |
| 1952 | if (bindFirst && !mBgWidgetsModel.isEmpty()) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1953 | bindWidgetsModel(callbacks); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1954 | } |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 1955 | ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext()); |
| 1956 | bindWidgetsModel(callbacks); |
| 1957 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1958 | // update the Widget entries inside DB on the worker thread. |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 1959 | mApp.getWidgetCache().removeObsoletePreviews(allWidgets); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 1960 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1961 | }); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 1962 | } |
| 1963 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1964 | static boolean isValidProvider(AppWidgetProviderInfo provider) { |
| 1965 | return (provider != null) && (provider.provider != null) |
| 1966 | && (provider.provider.getPackageName() != null); |
| 1967 | } |
| 1968 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1969 | public void dumpState() { |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1970 | Log.d(TAG, "mCallbacks=" + mCallbacks); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 1971 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data); |
| 1972 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added); |
| 1973 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed); |
| 1974 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1975 | if (mLoaderTask != null) { |
| 1976 | mLoaderTask.dumpState(); |
| 1977 | } else { |
| 1978 | Log.d(TAG, "mLoaderTask=null"); |
| 1979 | } |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 1980 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 1981 | |
| 1982 | public Callbacks getCallback() { |
| 1983 | return mCallbacks != null ? mCallbacks.get() : null; |
| 1984 | } |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1985 | |
| 1986 | /** |
| 1987 | * @return {@link FolderInfo} if its already loaded. |
| 1988 | */ |
| 1989 | public FolderInfo findFolderById(Long folderId) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1990 | synchronized (sBgDataModel) { |
| 1991 | return sBgDataModel.folders.get(folderId); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 1992 | } |
| 1993 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1994 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1995 | @Thunk class DeferredMainThreadExecutor implements Executor { |
| 1996 | |
| 1997 | @Override |
| 1998 | public void execute(Runnable command) { |
| 1999 | runOnMainThread(command); |
| 2000 | } |
| 2001 | } |
| 2002 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2003 | /** |
| 2004 | * @return the looper for the worker thread which can be used to start background tasks. |
| 2005 | */ |
| 2006 | public static Looper getWorkerLooper() { |
| 2007 | return sWorkerThread.getLooper(); |
| 2008 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2009 | } |