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; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 27 | import android.content.Intent.ShortcutIconResource; |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 28 | import android.content.IntentFilter; |
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.database.Cursor; |
| 31 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.HandlerThread; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 35 | import android.os.Looper; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 36 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.os.Process; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 38 | import android.os.SystemClock; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 39 | import android.os.Trace; |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 40 | import android.provider.BaseColumns; |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 42 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 43 | import android.util.LongSparseArray; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 44 | import android.util.MutableInt; |
Michael Jurka | 34c2e6c | 2013-12-13 16:07:45 +0100 | [diff] [blame] | 45 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 46 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 47 | import com.android.launcher3.compat.LauncherActivityInfoCompat; |
| 48 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 49 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 50 | import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 51 | import com.android.launcher3.compat.UserHandleCompat; |
| 52 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 53 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 54 | import com.android.launcher3.config.ProviderConfig; |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 55 | import com.android.launcher3.dynamicui.ExtractionUtils; |
Sunny Goyal | 2611943 | 2016-02-18 22:09:23 +0000 | [diff] [blame] | 56 | import com.android.launcher3.folder.Folder; |
| 57 | import com.android.launcher3.folder.FolderIcon; |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 58 | import com.android.launcher3.graphics.LauncherIcons; |
Sunny Goyal | 1acb9e9 | 2016-05-16 12:41:09 -0700 | [diff] [blame] | 59 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 60 | import com.android.launcher3.model.AddWorkspaceItemsTask; |
| 61 | import com.android.launcher3.model.ExtendedModelTask; |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 62 | import com.android.launcher3.model.BgDataModel; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 63 | import com.android.launcher3.model.CacheDataUpdatedTask; |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 64 | import com.android.launcher3.model.GridSizeMigrationTask; |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 65 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 66 | import com.android.launcher3.model.SdCardAvailableReceiver; |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 67 | import com.android.launcher3.model.WidgetItem; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 68 | import com.android.launcher3.model.PackageInstallStateChangedTask; |
| 69 | import com.android.launcher3.model.PackageUpdatedTask; |
| 70 | import com.android.launcher3.model.ShortcutsChangedTask; |
| 71 | import com.android.launcher3.model.UserLockStateChangedTask; |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 72 | import com.android.launcher3.model.WidgetsModel; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 73 | import com.android.launcher3.provider.ImportDataTask; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 74 | import com.android.launcher3.provider.LauncherDbUtils; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 75 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
| 76 | import com.android.launcher3.shortcuts.ShortcutInfoCompat; |
| 77 | import com.android.launcher3.shortcuts.ShortcutKey; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 78 | import com.android.launcher3.util.ComponentKey; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 79 | import com.android.launcher3.util.CursorIconInfo; |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 80 | import com.android.launcher3.util.GridOccupancy; |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 81 | import com.android.launcher3.util.ItemInfoMatcher; |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 82 | import com.android.launcher3.util.LongArrayMap; |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 83 | import com.android.launcher3.util.ManagedProfileHeuristic; |
Tony Wickham | d82a39d | 2016-07-01 15:44:13 -0700 | [diff] [blame] | 84 | import com.android.launcher3.util.MultiHashMap; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 85 | import com.android.launcher3.util.PackageManagerHelper; |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 86 | import com.android.launcher3.util.Preconditions; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 87 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 88 | import com.android.launcher3.util.ViewOnDrawExecutor; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 89 | |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 90 | import java.lang.ref.WeakReference; |
| 91 | import java.net.URISyntaxException; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 92 | import java.security.InvalidParameterException; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 93 | import java.util.ArrayList; |
| 94 | import java.util.Collections; |
| 95 | import java.util.Comparator; |
| 96 | import java.util.HashMap; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 97 | import java.util.HashSet; |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 98 | import java.util.Iterator; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 99 | import java.util.List; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 100 | import java.util.Map; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 101 | import java.util.Set; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 102 | import java.util.concurrent.Executor; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 103 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 106 | * 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] | 107 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | */ |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 109 | public class LauncherModel extends BroadcastReceiver |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 110 | implements LauncherAppsCompat.OnAppsChangedCallbackCompat { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 111 | static final boolean DEBUG_LOADERS = false; |
Chris Wren | ee52336 | 2014-09-09 10:09:02 -0400 | [diff] [blame] | 112 | private static final boolean DEBUG_RECEIVER = false; |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 113 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 114 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 115 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 116 | 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] | 117 | private static final long INVALID_SCREEN_ID = -1L; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 118 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 119 | @Thunk final LauncherAppState mApp; |
| 120 | @Thunk final Object mLock = new Object(); |
| 121 | @Thunk DeferredHandler mHandler = new DeferredHandler(); |
| 122 | @Thunk LoaderTask mLoaderTask; |
| 123 | @Thunk boolean mIsLoaderTaskRunning; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 124 | @Thunk boolean mHasLoaderCompletedOnce; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 125 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 126 | @Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 127 | static { |
| 128 | sWorkerThread.start(); |
| 129 | } |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 130 | @Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 131 | |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 132 | // We start off with everything not loaded. After that, we assume that |
| 133 | // our monitoring of the package manager provides all updates and we never |
| 134 | // 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] | 135 | private boolean mWorkspaceLoaded; |
| 136 | private boolean mAllAppsLoaded; |
| 137 | private boolean mDeepShortcutsLoaded; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 138 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Set of runnables to be called on the background thread after the workspace binding |
| 141 | * is complete. |
| 142 | */ |
| 143 | static final ArrayList<Runnable> mBindCompleteRunnables = new ArrayList<Runnable>(); |
| 144 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 145 | @Thunk WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 147 | // < only access in worker thread > |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 148 | private final AllAppsList mBgAllAppsList; |
Hyunyoung Song | 9110d48 | 2015-05-22 14:49:23 -0700 | [diff] [blame] | 149 | // Entire list of widgets. |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 150 | private final WidgetsModel mBgWidgetsModel; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 151 | |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 152 | private boolean mHasShortcutHostPermission; |
| 153 | // Runnable to check if the shortcuts permission has changed. |
| 154 | private final Runnable mShortcutPermissionCheckRunnable = new Runnable() { |
| 155 | @Override |
| 156 | public void run() { |
| 157 | if (mDeepShortcutsLoaded) { |
| 158 | boolean hasShortcutHostPermission = mDeepShortcutManager.hasHostPermission(); |
| 159 | if (hasShortcutHostPermission != mHasShortcutHostPermission) { |
| 160 | mApp.reloadWorkspace(); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 166 | /** |
| 167 | * All the static data should be accessed on the background thread, A lock should be acquired |
| 168 | * on this object when accessing any data from this model. |
| 169 | */ |
| 170 | static final BgDataModel sBgDataModel = new BgDataModel(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 171 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 172 | // </ only access in worker thread > |
| 173 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 174 | private final IconCache mIconCache; |
| 175 | private final DeepShortcutManager mDeepShortcutManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 176 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 177 | private final LauncherAppsCompat mLauncherApps; |
| 178 | private final UserManagerCompat mUserManager; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 179 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 180 | public interface Callbacks { |
Joe Onorato | ef2efcf | 2010-10-27 13:21:00 -0700 | [diff] [blame] | 181 | public boolean setLoadOnResume(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 182 | public int getCurrentWorkspaceScreen(); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 183 | public void clearPendingBinds(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 184 | public void startBinding(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 185 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end, |
| 186 | boolean forceAnimateIcons); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 187 | public void bindScreens(ArrayList<Long> orderedScreenIds); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 188 | public void finishFirstPageBind(ViewOnDrawExecutor executor); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 189 | public void finishBindingItems(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 190 | public void bindAppWidget(LauncherAppWidgetInfo info); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 191 | public void bindAllApplications(ArrayList<AppInfo> apps); |
Winson Chung | d64d176 | 2013-08-20 14:37:16 -0700 | [diff] [blame] | 192 | public void bindAppsAdded(ArrayList<Long> newScreens, |
| 193 | ArrayList<ItemInfo> addNotAnimated, |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 194 | ArrayList<ItemInfo> addAnimated, |
| 195 | ArrayList<AppInfo> addedApps); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 196 | public void bindAppsUpdated(ArrayList<AppInfo> apps); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 197 | public void bindShortcutsChanged(ArrayList<ShortcutInfo> updated, |
| 198 | ArrayList<ShortcutInfo> removed, UserHandleCompat user); |
| 199 | public void bindWidgetsRestored(ArrayList<LauncherAppWidgetInfo> widgets); |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 200 | public void bindRestoreItemsChange(HashSet<ItemInfo> updates); |
Sunny Goyal | 3bbbabc | 2016-03-15 09:16:30 -0700 | [diff] [blame] | 201 | public void bindWorkspaceComponentsRemoved( |
| 202 | HashSet<String> packageNames, HashSet<ComponentName> components, |
| 203 | UserHandleCompat user); |
| 204 | public void bindAppInfosRemoved(ArrayList<AppInfo> appInfos); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 205 | public void notifyWidgetProvidersChanged(); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 206 | public void bindAllWidgets(MultiHashMap<PackageItemInfo, WidgetItem> widgets); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 207 | public void onPageBoundSynchronously(int page); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 208 | public void executeOnNextDraw(ViewOnDrawExecutor executor); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 209 | public void bindDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMap); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 210 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 211 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 212 | LauncherModel(LauncherAppState app, IconCache iconCache, AppFilter appFilter, |
| 213 | DeepShortcutManager deepShortcutManager) { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 214 | Context context = app.getContext(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 215 | mApp = app; |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 216 | mBgAllAppsList = new AllAppsList(iconCache, appFilter); |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 217 | mBgWidgetsModel = new WidgetsModel(iconCache, appFilter); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 218 | mIconCache = iconCache; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 219 | mDeepShortcutManager = deepShortcutManager; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 220 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 221 | mLauncherApps = LauncherAppsCompat.getInstance(context); |
| 222 | mUserManager = UserManagerCompat.getInstance(context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 225 | /** Runs the specified runnable immediately if called from the main thread, otherwise it is |
| 226 | * posted on the main thread handler. */ |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 227 | private void runOnMainThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 228 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 229 | // If we are on the worker thread, post onto the main handler |
| 230 | mHandler.post(r); |
| 231 | } else { |
| 232 | r.run(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** Runs the specified runnable immediately if called from the worker thread, otherwise it is |
| 237 | * posted on the worker thread handler. */ |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 238 | private static void runOnWorkerThread(Runnable r) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 239 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 240 | r.run(); |
| 241 | } else { |
| 242 | // If we are not on the worker thread, then post to the worker handler |
| 243 | sWorker.post(r); |
| 244 | } |
| 245 | } |
| 246 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 247 | public void setPackageState(PackageInstallInfo installInfo) { |
| 248 | enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo)); |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 251 | /** |
| 252 | * Updates the icons and label of all pending icons for the provided package name. |
| 253 | */ |
| 254 | public void updateSessionDisplayInfo(final String packageName) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 255 | HashSet<String> packages = new HashSet<>(); |
| 256 | packages.add(packageName); |
| 257 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
| 258 | CacheDataUpdatedTask.OP_SESSION_UPDATE, UserHandleCompat.myUserHandle(), packages)); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Adds the provided items to the workspace. |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 263 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 264 | public void addAndBindAddedWorkspaceItems( |
Sunny Goyal | a214a63 | 2015-05-06 12:23:34 -0700 | [diff] [blame] | 265 | final ArrayList<? extends ItemInfo> workspaceApps) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 266 | enqueueModelUpdateTask(new AddWorkspaceItemsTask(workspaceApps)); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 269 | /** |
| 270 | * Adds an item to the DB if it was not created previously, or move it to a new |
| 271 | * <container, screen, cellX, cellY> |
| 272 | */ |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 273 | public static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 274 | long screenId, int cellX, int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 275 | if (item.container == ItemInfo.NO_ID) { |
| 276 | // From all apps |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 277 | addItemToDatabase(context, item, container, screenId, cellX, cellY); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 278 | } else { |
| 279 | // From somewhere else |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 280 | moveItemInDatabase(context, item, container, screenId, cellX, cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 284 | static void checkItemInfoLocked( |
| 285 | final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 286 | ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId); |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 287 | if (modelItem != null && item != modelItem) { |
| 288 | // check all the data is consistent |
| 289 | if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { |
| 290 | ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; |
| 291 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 292 | if (modelShortcut.title.toString().equals(shortcut.title.toString()) && |
| 293 | modelShortcut.intent.filterEquals(shortcut.intent) && |
| 294 | modelShortcut.id == shortcut.id && |
| 295 | modelShortcut.itemType == shortcut.itemType && |
| 296 | modelShortcut.container == shortcut.container && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 297 | modelShortcut.screenId == shortcut.screenId && |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 298 | modelShortcut.cellX == shortcut.cellX && |
| 299 | modelShortcut.cellY == shortcut.cellY && |
| 300 | modelShortcut.spanX == shortcut.spanX && |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 301 | modelShortcut.spanY == shortcut.spanY) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 302 | // For all intents and purposes, this is the same object |
| 303 | return; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // the modelItem needs to match up perfectly with item if our model is |
| 308 | // to be consistent with the database-- for now, just require |
| 309 | // modelItem == item or the equality check above |
| 310 | String msg = "item: " + ((item != null) ? item.toString() : "null") + |
| 311 | "modelItem: " + |
| 312 | ((modelItem != null) ? modelItem.toString() : "null") + |
| 313 | "Error: ItemInfo passed to checkItemInfo doesn't match original"; |
| 314 | RuntimeException e = new RuntimeException(msg); |
| 315 | if (stackTrace != null) { |
| 316 | e.setStackTrace(stackTrace); |
| 317 | } |
Adam Cohen | b9ada65 | 2013-11-08 08:25:08 -0800 | [diff] [blame] | 318 | throw e; |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 322 | static void checkItemInfo(final ItemInfo item) { |
| 323 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 324 | final long itemId = item.id; |
| 325 | Runnable r = new Runnable() { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 326 | public void run() { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 327 | synchronized (sBgDataModel) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 328 | checkItemInfoLocked(itemId, item, stackTrace); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 329 | } |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 330 | } |
| 331 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 332 | runOnWorkerThread(r); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 335 | static void updateItemInDatabaseHelper(Context context, final ContentValues values, |
| 336 | final ItemInfo item, final String callingFunction) { |
| 337 | final long itemId = item.id; |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 338 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 339 | final ContentResolver cr = context.getContentResolver(); |
| 340 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 341 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 342 | Runnable r = new Runnable() { |
| 343 | public void run() { |
| 344 | cr.update(uri, values, null, null); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 345 | updateItemArrays(item, itemId, stackTrace); |
| 346 | } |
| 347 | }; |
| 348 | runOnWorkerThread(r); |
| 349 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 350 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 351 | static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList, |
| 352 | final ArrayList<ItemInfo> items, final String callingFunction) { |
| 353 | final ContentResolver cr = context.getContentResolver(); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 354 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 355 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 356 | Runnable r = new Runnable() { |
| 357 | public void run() { |
| 358 | ArrayList<ContentProviderOperation> ops = |
| 359 | new ArrayList<ContentProviderOperation>(); |
| 360 | int count = items.size(); |
| 361 | for (int i = 0; i < count; i++) { |
| 362 | ItemInfo item = items.get(i); |
| 363 | final long itemId = item.id; |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 364 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 365 | ContentValues values = valuesList.get(i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 366 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 367 | ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); |
| 368 | updateItemArrays(item, itemId, stackTrace); |
| 369 | |
| 370 | } |
| 371 | try { |
| 372 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 373 | } catch (Exception e) { |
| 374 | e.printStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 378 | runOnWorkerThread(r); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 379 | } |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 380 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 381 | static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) { |
| 382 | // Lock on mBgLock *after* the db operation |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 383 | synchronized (sBgDataModel) { |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 384 | checkItemInfoLocked(itemId, item, stackTrace); |
| 385 | |
| 386 | if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 387 | item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 388 | // Item is in a folder, make sure this folder exists |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 389 | if (!sBgDataModel.folders.containsKey(item.container)) { |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 390 | // An items container is being set to a that of an item which is not in |
| 391 | // the list of Folders. |
| 392 | String msg = "item: " + item + " container being set to: " + |
| 393 | item.container + ", not in the list of folders"; |
| 394 | Log.e(TAG, msg); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | // Items are added/removed from the corresponding FolderInfo elsewhere, such |
| 399 | // as in Workspace.onDrop. Here, we just add/remove them from the list of items |
| 400 | // that are on the desktop, as appropriate |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 401 | ItemInfo modelItem = sBgDataModel.itemsIdMap.get(itemId); |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 402 | if (modelItem != null && |
| 403 | (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || |
| 404 | modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT)) { |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 405 | switch (modelItem.itemType) { |
| 406 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 407 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 408 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 409 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 410 | if (!sBgDataModel.workspaceItems.contains(modelItem)) { |
| 411 | sBgDataModel.workspaceItems.add(modelItem); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 412 | } |
| 413 | break; |
| 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | } else { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 418 | sBgDataModel.workspaceItems.remove(modelItem); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 424 | * Move an item in the DB to a new <container, screen, cellX, cellY> |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 425 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 426 | public static void moveItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 427 | final long screenId, final int cellX, final int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 428 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 429 | item.cellX = cellX; |
| 430 | item.cellY = cellY; |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 431 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 432 | // We store hotseat items in canonical form which is this orientation invariant position |
| 433 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 434 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 435 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 436 | item.screenId = Launcher.getLauncher(context).getHotseat() |
| 437 | .getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 438 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 439 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 440 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 441 | |
| 442 | final ContentValues values = new ContentValues(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 443 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 444 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 445 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 446 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 447 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 448 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 449 | updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase"); |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 453 | * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the |
| 454 | * cellX, cellY have already been updated on the ItemInfos. |
| 455 | */ |
Adam Cohen | f9c184a | 2016-01-15 16:47:43 -0800 | [diff] [blame] | 456 | public static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items, |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 457 | final long container, final int screen) { |
| 458 | |
| 459 | ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>(); |
| 460 | int count = items.size(); |
| 461 | |
| 462 | for (int i = 0; i < count; i++) { |
| 463 | ItemInfo item = items.get(i); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 464 | item.container = container; |
| 465 | |
| 466 | // We store hotseat items in canonical form which is this orientation invariant position |
| 467 | // in the hotseat |
| 468 | if (context instanceof Launcher && screen < 0 && |
| 469 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 470 | item.screenId = Launcher.getLauncher(context).getHotseat().getOrderInHotseat(item.cellX, |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 471 | item.cellY); |
| 472 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 473 | item.screenId = screen; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | final ContentValues values = new ContentValues(); |
| 477 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
| 478 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 479 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 480 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 481 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 482 | |
| 483 | contentValues.add(values); |
| 484 | } |
| 485 | updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase"); |
| 486 | } |
| 487 | |
| 488 | /** |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 489 | * Move and/or resize item in the DB to a new <container, screen, cellX, cellY, spanX, spanY> |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 490 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 491 | static void modifyItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 492 | final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) { |
Winson Chung | 0f84a60 | 2013-09-30 14:30:58 -0700 | [diff] [blame] | 493 | item.container = container; |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 494 | item.cellX = cellX; |
| 495 | item.cellY = cellY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 496 | item.spanX = spanX; |
| 497 | item.spanY = spanY; |
| 498 | |
| 499 | // We store hotseat items in canonical form which is this orientation invariant position |
| 500 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 501 | if (context instanceof Launcher && screenId < 0 && |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 502 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 503 | item.screenId = Launcher.getLauncher(context).getHotseat() |
| 504 | .getOrderInHotseat(cellX, cellY); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 505 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 506 | item.screenId = screenId; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 507 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 508 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 509 | final ContentValues values = new ContentValues(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 510 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 511 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 512 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 513 | values.put(LauncherSettings.Favorites.RANK, item.rank); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 514 | values.put(LauncherSettings.Favorites.SPANX, item.spanX); |
| 515 | values.put(LauncherSettings.Favorites.SPANY, item.spanY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 516 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 517 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 518 | updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase"); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 519 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 520 | |
| 521 | /** |
| 522 | * Update an item to the database in a specified container. |
| 523 | */ |
Sunny Goyal | 83a8f04 | 2015-05-19 12:52:12 -0700 | [diff] [blame] | 524 | public static void updateItemInDatabase(Context context, final ItemInfo item) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 525 | final ContentValues values = new ContentValues(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 526 | item.onAddToDatabase(context, values); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 527 | updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase"); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 530 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 531 | * Add an item to the database in a specified container. Sets the container, screen, cellX and |
| 532 | * cellY fields of the item. Also assigns an ID to the item. |
| 533 | */ |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 534 | public static void addItemToDatabase(Context context, final ItemInfo item, final long container, |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 535 | final long screenId, final int cellX, final int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 536 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 537 | item.cellX = cellX; |
| 538 | item.cellY = cellY; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 539 | // We store hotseat items in canonical form which is this orientation invariant position |
| 540 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 541 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 542 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 543 | item.screenId = Launcher.getLauncher(context).getHotseat() |
| 544 | .getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 545 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 546 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 547 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 548 | |
| 549 | final ContentValues values = new ContentValues(); |
| 550 | final ContentResolver cr = context.getContentResolver(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 551 | item.onAddToDatabase(context, values); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 552 | |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 553 | item.id = LauncherSettings.Settings.call(cr, LauncherSettings.Settings.METHOD_NEW_ITEM_ID) |
| 554 | .getLong(LauncherSettings.Settings.EXTRA_VALUE); |
| 555 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 556 | values.put(LauncherSettings.Favorites._ID, item.id); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 557 | |
Jason Monk | 8e19cf2 | 2014-03-20 15:06:57 -0400 | [diff] [blame] | 558 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 559 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 560 | public void run() { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 561 | cr.insert(LauncherSettings.Favorites.CONTENT_URI, values); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 562 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 563 | synchronized (sBgDataModel) { |
Jason Monk | 8e19cf2 | 2014-03-20 15:06:57 -0400 | [diff] [blame] | 564 | checkItemInfoLocked(item.id, item, stackTrace); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 565 | sBgDataModel.addItem(item, true); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 568 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 569 | runOnWorkerThread(r); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 572 | /** |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 573 | * Removes the specified item from the database |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 574 | */ |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 575 | public static void deleteItemFromDatabase(Context context, final ItemInfo item) { |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 576 | ArrayList<ItemInfo> items = new ArrayList<>(); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 577 | items.add(item); |
| 578 | deleteItemsFromDatabase(context, items); |
| 579 | } |
| 580 | |
| 581 | /** |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 582 | * Removes all the items from the database matching {@param matcher}. |
| 583 | */ |
| 584 | public static void deleteItemsFromDatabase(Context context, ItemInfoMatcher matcher) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 585 | deleteItemsFromDatabase(context, matcher.filterItemInfos(sBgDataModel.itemsIdMap)); |
Sunny Goyal | 40452cf | 2016-09-01 15:17:46 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /** |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 589 | * Removes the specified items from the database |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 590 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 591 | public static void deleteItemsFromDatabase(Context context, |
| 592 | final Iterable<? extends ItemInfo> items) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 593 | final ContentResolver cr = context.getContentResolver(); |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 594 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 595 | public void run() { |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 596 | for (ItemInfo item : items) { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 597 | final Uri uri = LauncherSettings.Favorites.getContentUri(item.id); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 598 | cr.delete(uri, null, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 599 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 600 | sBgDataModel.removeItem(item); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 603 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 604 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 608 | * Update the order of the workspace screens in the database. The array list contains |
| 609 | * a list of screen ids in the order that they should appear. |
| 610 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 611 | public static void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 612 | final ArrayList<Long> screensCopy = new ArrayList<Long>(screens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 613 | final ContentResolver cr = context.getContentResolver(); |
| 614 | final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 615 | |
| 616 | // Remove any negative screen ids -- these aren't persisted |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 617 | Iterator<Long> iter = screensCopy.iterator(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 618 | while (iter.hasNext()) { |
| 619 | long id = iter.next(); |
| 620 | if (id < 0) { |
| 621 | iter.remove(); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | Runnable r = new Runnable() { |
| 626 | @Override |
| 627 | public void run() { |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 628 | ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 629 | // Clear the table |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 630 | ops.add(ContentProviderOperation.newDelete(uri).build()); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 631 | int count = screensCopy.size(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 632 | for (int i = 0; i < count; i++) { |
| 633 | ContentValues v = new ContentValues(); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 634 | long screenId = screensCopy.get(i); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 635 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 636 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 637 | ops.add(ContentProviderOperation.newInsert(uri).withValues(v).build()); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 638 | } |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 639 | |
| 640 | try { |
| 641 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 642 | } catch (Exception ex) { |
| 643 | throw new RuntimeException(ex); |
| 644 | } |
Winson Chung | 9e6a0a2 | 2013-08-27 11:58:12 -0700 | [diff] [blame] | 645 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 646 | synchronized (sBgDataModel) { |
| 647 | sBgDataModel.workspaceScreens.clear(); |
| 648 | sBgDataModel.workspaceScreens.addAll(screensCopy); |
Adam Cohen | 4caf298 | 2013-08-20 18:54:31 -0700 | [diff] [blame] | 649 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 650 | } |
| 651 | }; |
| 652 | runOnWorkerThread(r); |
| 653 | } |
| 654 | |
| 655 | /** |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 656 | * Remove the specified folder and all its contents from the database. |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 657 | */ |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 658 | public static void deleteFolderAndContentsFromDatabase(Context context, final FolderInfo info) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 659 | final ContentResolver cr = context.getContentResolver(); |
| 660 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 661 | Runnable r = new Runnable() { |
| 662 | public void run() { |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 663 | cr.delete(LauncherSettings.Favorites.CONTENT_URI, |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 664 | LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 665 | sBgDataModel.removeItem(info.contents); |
| 666 | info.contents.clear(); |
| 667 | |
| 668 | cr.delete(LauncherSettings.Favorites.getContentUri(info.id), null, null); |
| 669 | sBgDataModel.removeItem(info); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 670 | } |
| 671 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 672 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Set this as the current Launcher activity object for the loader. |
| 677 | */ |
| 678 | public void initialize(Callbacks callbacks) { |
| 679 | synchronized (mLock) { |
Sunny Goyal | aaf7d1d | 2016-05-17 13:38:54 -0700 | [diff] [blame] | 680 | Preconditions.assertUIThread(); |
| 681 | // Remove any queued UI runnables |
| 682 | mHandler.cancelAll(); |
| 683 | mCallbacks = new WeakReference<>(callbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 687 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 688 | public void onPackageChanged(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 689 | int op = PackageUpdatedTask.OP_UPDATE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 690 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 694 | public void onPackageRemoved(String packageName, UserHandleCompat user) { |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 695 | onPackagesRemoved(user, packageName); |
| 696 | } |
| 697 | |
| 698 | public void onPackagesRemoved(UserHandleCompat user, String... packages) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 699 | int op = PackageUpdatedTask.OP_REMOVE; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 700 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 704 | public void onPackageAdded(String packageName, UserHandleCompat user) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 705 | int op = PackageUpdatedTask.OP_ADD; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 706 | enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 710 | public void onPackagesAvailable(String[] packageNames, UserHandleCompat user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 711 | boolean replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 712 | enqueueModelUpdateTask( |
| 713 | new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | @Override |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 717 | public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user, |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 718 | boolean replacing) { |
| 719 | if (!replacing) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 720 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 721 | PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 722 | } |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 725 | @Override |
| 726 | public void onPackagesSuspended(String[] packageNames, UserHandleCompat user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 727 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 728 | PackageUpdatedTask.OP_SUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | @Override |
| 732 | public void onPackagesUnsuspended(String[] packageNames, UserHandleCompat user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 733 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 734 | PackageUpdatedTask.OP_UNSUSPEND, user, packageNames)); |
Kenny Guy | 44cba69 | 2016-01-21 19:50:02 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 737 | @Override |
| 738 | public void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts, |
| 739 | UserHandleCompat user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 740 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true)); |
Sunny Goyal | 50941fb | 2016-08-04 12:03:52 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | public void updatePinnedShortcuts(String packageName, List<ShortcutInfoCompat> shortcuts, |
| 744 | UserHandleCompat user) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 745 | enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, false)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 748 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 749 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 750 | * ACTION_PACKAGE_CHANGED. |
| 751 | */ |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 752 | @Override |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 753 | public void onReceive(Context context, Intent intent) { |
Chris Wren | b358f81 | 2014-04-16 13:37:00 -0400 | [diff] [blame] | 754 | if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 755 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 756 | final String action = intent.getAction(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 757 | if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 758 | // If we have changed locale we need to clear out the labels in all apps/workspace. |
| 759 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 760 | } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action) |
| 761 | || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) { |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 762 | UserManagerCompat.getInstance(context).enableAndResetCache(); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 763 | forceReload(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 764 | } else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 765 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 766 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 767 | UserHandleCompat user = UserHandleCompat.fromIntent(intent); |
| 768 | if (user != null) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 769 | if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) || |
| 770 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 771 | enqueueModelUpdateTask(new PackageUpdatedTask( |
| 772 | PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so |
| 776 | // we need to run the state change task again. |
| 777 | if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) || |
| 778 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 779 | enqueueModelUpdateTask(new UserLockStateChangedTask(user)); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 780 | } |
Sunny Goyal | da891c1 | 2016-03-18 18:29:24 -0700 | [diff] [blame] | 781 | } |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 782 | } else if (Intent.ACTION_WALLPAPER_CHANGED.equals(action)) { |
| 783 | ExtractionUtils.startColorExtractionServiceIfNecessary(context); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Amith Yamasani | 6cc806d | 2014-05-02 13:47:11 -0700 | [diff] [blame] | 787 | void forceReload() { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 788 | resetLoadedState(true, true); |
| 789 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 790 | // Do this here because if the launcher activity is running it will be restarted. |
| 791 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 792 | // to reload. |
| 793 | startLoaderFromBackground(); |
| 794 | } |
| 795 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 796 | public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) { |
| 797 | synchronized (mLock) { |
| 798 | // Stop any existing loaders first, so they don't set mAllAppsLoaded or |
| 799 | // mWorkspaceLoaded to true later |
| 800 | stopLoaderLocked(); |
| 801 | if (resetAllAppsLoaded) mAllAppsLoaded = false; |
| 802 | if (resetWorkspaceLoaded) mWorkspaceLoaded = false; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 803 | // Always reset deep shortcuts loaded. |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 804 | // TODO: why? |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 805 | mDeepShortcutsLoaded = false; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 809 | /** |
| 810 | * When the launcher is in the background, it's possible for it to miss paired |
| 811 | * configuration changes. So whenever we trigger the loader from the background |
| 812 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 813 | * of doing it now. |
| 814 | */ |
| 815 | public void startLoaderFromBackground() { |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 816 | Callbacks callbacks = getCallback(); |
| 817 | if (callbacks != null) { |
| 818 | // Only actually run the loader if they're not paused. |
| 819 | if (!callbacks.setLoadOnResume()) { |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 820 | startLoader(callbacks.getCurrentWorkspaceScreen()); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 823 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 824 | |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 825 | /** |
| 826 | * If there is already a loader task running, tell it to stop. |
| 827 | */ |
| 828 | private void stopLoaderLocked() { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 829 | LoaderTask oldTask = mLoaderTask; |
| 830 | if (oldTask != null) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 831 | oldTask.stopLocked(); |
| 832 | } |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Adam Cohen | 1a85c58 | 2014-09-30 09:48:49 -0700 | [diff] [blame] | 835 | public boolean isCurrentCallbacks(Callbacks callbacks) { |
| 836 | return (mCallbacks != null && mCallbacks.get() == callbacks); |
| 837 | } |
| 838 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 839 | /** |
| 840 | * Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible. |
| 841 | * @return true if the page could be bound synchronously. |
| 842 | */ |
| 843 | public boolean startLoader(int synchronousBindPage) { |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 844 | // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems |
| 845 | InstallShortcutReceiver.enableInstallQueue(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 846 | synchronized (mLock) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 847 | // Don't bother to start the thread if we know it's not going to do anything |
| 848 | if (mCallbacks != null && mCallbacks.get() != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 849 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 850 | // Clear any pending bind-runnables from the synchronized load process. |
| 851 | runOnMainThread(new Runnable() { |
| 852 | public void run() { |
| 853 | oldCallbacks.clearPendingBinds(); |
| 854 | } |
| 855 | }); |
| 856 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 857 | // If there is already one running, tell it to stop. |
Sunny Goyal | 2bba4c3 | 2015-05-18 15:42:48 -0700 | [diff] [blame] | 858 | stopLoaderLocked(); |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 859 | mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 860 | // TODO: mDeepShortcutsLoaded does not need to be true for synchronous bind. |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 861 | if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded |
| 862 | && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 863 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 864 | return true; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 865 | } else { |
| 866 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 867 | sWorker.post(mLoaderTask); |
| 868 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 869 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 870 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 871 | return false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 872 | } |
| 873 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 874 | public void stopLoader() { |
| 875 | synchronized (mLock) { |
| 876 | if (mLoaderTask != null) { |
| 877 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 878 | } |
| 879 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 880 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 881 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 882 | /** |
| 883 | * Loads the workspace screen ids in an ordered list. |
| 884 | */ |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 885 | public static ArrayList<Long> loadWorkspaceScreensDb(Context context) { |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 886 | final ContentResolver contentResolver = context.getContentResolver(); |
| 887 | final Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 888 | |
Sunny Goyal | c1b7c2e | 2015-01-16 16:19:04 -0800 | [diff] [blame] | 889 | // Get screens ordered by rank. |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 890 | return LauncherDbUtils.getScreenIdsFromCursor(contentResolver.query( |
| 891 | screensUri, null, null, null, LauncherSettings.WorkspaceScreens.SCREEN_RANK)); |
Winson Chung | 76828c8 | 2013-08-19 15:43:29 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 894 | /** |
| 895 | * Runnable for the thread that loads the contents of the launcher: |
| 896 | * - workspace icons |
| 897 | * - widgets |
| 898 | * - all apps icons |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 899 | * - deep shortcuts within apps |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 900 | */ |
| 901 | private class LoaderTask implements Runnable { |
| 902 | private Context mContext; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 903 | private int mPageToBindFirst; |
| 904 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 905 | @Thunk boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 906 | private boolean mStopped; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 907 | @Thunk boolean mLoadAndBindStepFinished; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 908 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 909 | LoaderTask(Context context, int pageToBindFirst) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 910 | mContext = context; |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 911 | mPageToBindFirst = pageToBindFirst; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 912 | } |
| 913 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 914 | private void loadAndBindWorkspace() { |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 915 | mIsLoadingAndBindingWorkspace = true; |
| 916 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 917 | // Load the workspace |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 918 | if (DEBUG_LOADERS) { |
| 919 | Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 920 | } |
Michael Jurka | 288a36b | 2011-07-12 16:53:48 -0700 | [diff] [blame] | 921 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 922 | if (!mWorkspaceLoaded) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 923 | loadWorkspace(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 924 | synchronized (LoaderTask.this) { |
| 925 | if (mStopped) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 926 | return; |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 927 | } |
| 928 | mWorkspaceLoaded = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 932 | // Bind the workspace |
Sunny Goyal | 93f878c | 2016-03-30 17:31:24 -0700 | [diff] [blame] | 933 | bindWorkspace(mPageToBindFirst); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 934 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 935 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 936 | private void waitForIdle() { |
| 937 | // Wait until the either we're stopped or the other threads are done. |
| 938 | // This way we don't start loading all apps until the workspace has settled |
| 939 | // down. |
| 940 | synchronized (LoaderTask.this) { |
| 941 | final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 942 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 943 | mHandler.postIdle(new Runnable() { |
| 944 | public void run() { |
| 945 | synchronized (LoaderTask.this) { |
| 946 | mLoadAndBindStepFinished = true; |
| 947 | if (DEBUG_LOADERS) { |
| 948 | Log.d(TAG, "done with previous binding step"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 949 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 950 | LoaderTask.this.notify(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 951 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 952 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 953 | }); |
| 954 | |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 955 | while (!mStopped && !mLoadAndBindStepFinished) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 956 | try { |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 957 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 958 | // wait no longer than 1sec at a time |
| 959 | this.wait(1000); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 960 | } catch (InterruptedException ex) { |
| 961 | // Ignore |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 962 | } |
| 963 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 964 | if (DEBUG_LOADERS) { |
| 965 | Log.d(TAG, "waited " |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 966 | + (SystemClock.uptimeMillis()-workspaceWaitTime) |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 967 | + "ms for previous step to finish binding"); |
| 968 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 969 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 970 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 971 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 972 | void runBindSynchronousPage(int synchronousBindPage) { |
Derek Prothro | 7aff399 | 2013-12-10 14:00:37 -0500 | [diff] [blame] | 973 | if (synchronousBindPage == PagedView.INVALID_RESTORE_PAGE) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 974 | // Ensure that we have a valid page index to load synchronously |
| 975 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 976 | "valid page index"); |
| 977 | } |
| 978 | if (!mAllAppsLoaded || !mWorkspaceLoaded) { |
| 979 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 980 | // loaded already (we should load everything asynchronously in that case) |
| 981 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 982 | } |
| 983 | synchronized (mLock) { |
| 984 | if (mIsLoaderTaskRunning) { |
| 985 | // Ensure that we are never running the background loading at this point since |
| 986 | // we also touch the background collections |
| 987 | throw new RuntimeException("Error! Background loading is already running"); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // XXX: Throw an exception if we are already loading (since we touch the worker thread |
| 992 | // data structures, we can't allow any other thread to touch that data, but because |
| 993 | // this call is synchronous, we can get away with not locking). |
| 994 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 995 | // The LauncherModel is static in the LauncherAppState and mHandler may have queued |
Adam Cohen | a13a2f2 | 2012-07-23 14:29:15 -0700 | [diff] [blame] | 996 | // operations from the previous activity. We need to ensure that all queued operations |
| 997 | // are executed before any synchronous binding work is done. |
| 998 | mHandler.flush(); |
| 999 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1000 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 1001 | // everything else that is to be bound normally (asynchronously). |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1002 | bindWorkspace(synchronousBindPage); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1003 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 1004 | // arise from that. |
| 1005 | onlyBindAllApps(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 1006 | |
| 1007 | bindDeepShortcuts(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1010 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1011 | synchronized (mLock) { |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 1012 | if (mStopped) { |
| 1013 | return; |
| 1014 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1015 | mIsLoaderTaskRunning = true; |
| 1016 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1017 | // Optimize for end-user experience: if the Launcher is up and // running with the |
| 1018 | // All Apps interface in the foreground, load All Apps first. Otherwise, load the |
| 1019 | // workspace first (default). |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1020 | keep_running: { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1021 | if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace"); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1022 | loadAndBindWorkspace(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1023 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1024 | if (mStopped) { |
| 1025 | break keep_running; |
| 1026 | } |
| 1027 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1028 | waitForIdle(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1029 | |
| 1030 | // second step |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1031 | if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps"); |
| 1032 | loadAndBindAllApps(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1033 | |
| 1034 | waitForIdle(); |
| 1035 | |
| 1036 | // third step |
| 1037 | if (DEBUG_LOADERS) Log.d(TAG, "step 3: loading deep shortcuts"); |
| 1038 | loadAndBindDeepShortcuts(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1039 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1040 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1041 | // Clear out this reference, otherwise we end up holding it until all of the |
| 1042 | // callback runnables are done. |
| 1043 | mContext = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1044 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1045 | synchronized (mLock) { |
| 1046 | // If we are still the last one to be scheduled, remove ourselves. |
| 1047 | if (mLoaderTask == this) { |
| 1048 | mLoaderTask = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1049 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1050 | mIsLoaderTaskRunning = false; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 1051 | mHasLoaderCompletedOnce = true; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1052 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | public void stopLocked() { |
| 1056 | synchronized (LoaderTask.this) { |
| 1057 | mStopped = true; |
| 1058 | this.notify(); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 1064 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 1065 | * object that was around when the deferred message was scheduled, and if there's |
| 1066 | * a new Callbacks object around then also return null. This will save us from |
| 1067 | * calling onto it with data that will be ignored. |
| 1068 | */ |
| 1069 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 1070 | synchronized (mLock) { |
| 1071 | if (mStopped) { |
| 1072 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1073 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1074 | |
| 1075 | if (mCallbacks == null) { |
| 1076 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1077 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1078 | |
| 1079 | final Callbacks callbacks = mCallbacks.get(); |
| 1080 | if (callbacks != oldCallbacks) { |
| 1081 | return null; |
| 1082 | } |
| 1083 | if (callbacks == null) { |
| 1084 | Log.w(TAG, "no mCallbacks"); |
| 1085 | return null; |
| 1086 | } |
| 1087 | |
| 1088 | return callbacks; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // check & update map of what's occupied; used to discard overlapping/invalid items |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1093 | private boolean checkItemPlacement(LongArrayMap<GridOccupancy> occupied, ItemInfo item, |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1094 | ArrayList<Long> workspaceScreens) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1095 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1096 | InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1097 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1098 | long containerIndex = item.screenId; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1099 | if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Winson Chung | a0b7e86 | 2013-09-05 16:03:15 -0700 | [diff] [blame] | 1100 | // Return early if we detect that an item is under the hotseat button |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 1101 | if (!FeatureFlags.NO_ALL_APPS_ICON && |
| 1102 | profile.isAllAppsButtonRank((int) item.screenId)) { |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1103 | Log.e(TAG, "Error loading shortcut into hotseat " + item |
| 1104 | + " into position (" + item.screenId + ":" + item.cellX + "," |
| 1105 | + item.cellY + ") occupied by all apps"); |
Winson Chung | a0b7e86 | 2013-09-05 16:03:15 -0700 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1109 | final GridOccupancy hotseatOccupancy = |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1110 | occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT); |
| 1111 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1112 | if (item.screenId >= profile.numHotseatIcons) { |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1113 | Log.e(TAG, "Error loading shortcut " + item |
| 1114 | + " into hotseat position " + item.screenId |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1115 | + ", position out of bounds: (0 to " + (profile.numHotseatIcons - 1) |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1116 | + ")"); |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1120 | if (hotseatOccupancy != null) { |
| 1121 | if (hotseatOccupancy.cells[(int) item.screenId][0]) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1122 | Log.e(TAG, "Error loading shortcut into hotseat " + item |
| 1123 | + " into position (" + item.screenId + ":" + item.cellX + "," |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1124 | + item.cellY + ") already occupied"); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1125 | return false; |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1126 | } else { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1127 | hotseatOccupancy.cells[(int) item.screenId][0] = true; |
Dan Sandler | 295ae18 | 2013-12-10 16:05:47 -0500 | [diff] [blame] | 1128 | return true; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1129 | } |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1130 | } else { |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1131 | final GridOccupancy occupancy = new GridOccupancy(profile.numHotseatIcons, 1); |
| 1132 | occupancy.cells[(int) item.screenId][0] = true; |
| 1133 | occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy); |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1134 | return true; |
| 1135 | } |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1136 | } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1137 | if (!workspaceScreens.contains((Long) item.screenId)) { |
| 1138 | // The item has an invalid screen id. |
| 1139 | return false; |
| 1140 | } |
| 1141 | } else { |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1142 | // Skip further checking if it is not the hotseat or workspace container |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1143 | return true; |
| 1144 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1145 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1146 | final int countX = profile.numColumns; |
| 1147 | final int countY = profile.numRows; |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1148 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1149 | item.cellX < 0 || item.cellY < 0 || |
| 1150 | item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) { |
| 1151 | Log.e(TAG, "Error loading shortcut " + item |
| 1152 | + " into cell (" + containerIndex + "-" + item.screenId + ":" |
| 1153 | + item.cellX + "," + item.cellY |
| 1154 | + ") out of screen bounds ( " + countX + "x" + countY + ")"); |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1158 | if (!occupied.containsKey(item.screenId)) { |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1159 | GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1); |
| 1160 | if (item.screenId == Workspace.FIRST_SCREEN_ID) { |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 1161 | // Mark the first row as occupied (if the feature is enabled) |
| 1162 | // in order to account for the QSB. |
| 1163 | screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN); |
Sunny Goyal | da4fe1a | 2016-05-26 16:05:17 -0700 | [diff] [blame] | 1164 | } |
| 1165 | occupied.put(item.screenId, screen); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1166 | } |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1167 | final GridOccupancy occupancy = occupied.get(item.screenId); |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1168 | |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1169 | // Check if any workspace icons overlap with each other |
| 1170 | if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) { |
| 1171 | occupancy.markCells(item, true); |
| 1172 | return true; |
| 1173 | } else { |
| 1174 | Log.e(TAG, "Error loading shortcut " + item |
| 1175 | + " into cell (" + containerIndex + "-" + item.screenId + ":" |
| 1176 | + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY |
| 1177 | + ") already occupied"); |
| 1178 | return false; |
| 1179 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1180 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1181 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1182 | private void loadWorkspace() { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 1183 | if (LauncherAppState.PROFILE_STARTUP) { |
| 1184 | Trace.beginSection("Loading Workspace"); |
| 1185 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1186 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1187 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1188 | final Context context = mContext; |
| 1189 | final ContentResolver contentResolver = context.getContentResolver(); |
| 1190 | final PackageManager manager = context.getPackageManager(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1191 | final boolean isSafeMode = manager.isSafeMode(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1192 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(context); |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1193 | final boolean isSdCardReady = Utilities.isBootCompleted(); |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1194 | final MultiHashMap<UserHandleCompat, String> pendingPackages = new MultiHashMap<>(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 1195 | |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1196 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1197 | InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1198 | int countX = profile.numColumns; |
| 1199 | int countY = profile.numRows; |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1200 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 1201 | boolean clearDb = false; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 1202 | try { |
| 1203 | ImportDataTask.performImportIfPossible(context); |
| 1204 | } catch (Exception e) { |
| 1205 | // Migration failed. Clear workspace. |
| 1206 | clearDb = true; |
| 1207 | } |
| 1208 | |
| 1209 | if (!clearDb && GridSizeMigrationTask.ENABLED && |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 1210 | !GridSizeMigrationTask.migrateGridIfNeeded(mContext)) { |
| 1211 | // Migration failed. Clear workspace. |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 1212 | clearDb = true; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 1215 | if (clearDb) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1216 | Log.d(TAG, "loadWorkspace: resetting launcher database"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1217 | LauncherSettings.Settings.call(contentResolver, |
| 1218 | LauncherSettings.Settings.METHOD_DELETE_DB); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1219 | } |
| 1220 | |
Sunny Goyal | ded0fdb | 2016-05-23 15:55:41 -0700 | [diff] [blame] | 1221 | Log.d(TAG, "loadWorkspace: loading default favorites"); |
| 1222 | LauncherSettings.Settings.call(contentResolver, |
| 1223 | LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1224 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1225 | synchronized (sBgDataModel) { |
| 1226 | sBgDataModel.clear(); |
| 1227 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1228 | final HashMap<String, Integer> installingPkgs = PackageInstallerCompat |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1229 | .getInstance(mContext).updateAndGetActiveSessionCache(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1230 | sBgDataModel.workspaceScreens.addAll(loadWorkspaceScreensDb(mContext)); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1231 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1232 | final ArrayList<Long> itemsToRemove = new ArrayList<>(); |
| 1233 | final ArrayList<Long> restoredRows = new ArrayList<>(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1234 | Map<ShortcutKey, ShortcutInfoCompat> shortcutKeyToPinnedShortcuts = new HashMap<>(); |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1235 | final Uri contentUri = LauncherSettings.Favorites.CONTENT_URI; |
Chris Wren | e523e70 | 2013-10-09 10:36:55 -0400 | [diff] [blame] | 1236 | if (DEBUG_LOADERS) Log.d(TAG, "loading model from " + contentUri); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1237 | final Cursor c = contentResolver.query(contentUri, null, null, null, null); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1238 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1239 | // +1 for the hotseat (it can be larger than the workspace) |
| 1240 | // Load workspace in reverse order to ensure that latest items are loaded first (and |
| 1241 | // before any earlier duplicates) |
Sunny Goyal | ff4ba2d | 2016-04-02 14:12:34 -0700 | [diff] [blame] | 1242 | final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1243 | HashMap<ComponentKey, AppWidgetProviderInfo> widgetProvidersMap = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1244 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1245 | try { |
| 1246 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
| 1247 | final int intentIndex = c.getColumnIndexOrThrow |
| 1248 | (LauncherSettings.Favorites.INTENT); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1249 | final int containerIndex = c.getColumnIndexOrThrow( |
| 1250 | LauncherSettings.Favorites.CONTAINER); |
| 1251 | final int itemTypeIndex = c.getColumnIndexOrThrow( |
| 1252 | LauncherSettings.Favorites.ITEM_TYPE); |
| 1253 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 1254 | LauncherSettings.Favorites.APPWIDGET_ID); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1255 | final int appWidgetProviderIndex = c.getColumnIndexOrThrow( |
| 1256 | LauncherSettings.Favorites.APPWIDGET_PROVIDER); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1257 | final int screenIndex = c.getColumnIndexOrThrow( |
| 1258 | LauncherSettings.Favorites.SCREEN); |
| 1259 | final int cellXIndex = c.getColumnIndexOrThrow |
| 1260 | (LauncherSettings.Favorites.CELLX); |
| 1261 | final int cellYIndex = c.getColumnIndexOrThrow |
| 1262 | (LauncherSettings.Favorites.CELLY); |
| 1263 | final int spanXIndex = c.getColumnIndexOrThrow |
| 1264 | (LauncherSettings.Favorites.SPANX); |
| 1265 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 1266 | LauncherSettings.Favorites.SPANY); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1267 | final int rankIndex = c.getColumnIndexOrThrow( |
| 1268 | LauncherSettings.Favorites.RANK); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1269 | final int restoredIndex = c.getColumnIndexOrThrow( |
| 1270 | LauncherSettings.Favorites.RESTORED); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1271 | final int profileIdIndex = c.getColumnIndexOrThrow( |
| 1272 | LauncherSettings.Favorites.PROFILE_ID); |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1273 | final int optionsIndex = c.getColumnIndexOrThrow( |
| 1274 | LauncherSettings.Favorites.OPTIONS); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1275 | final CursorIconInfo cursorIconInfo = new CursorIconInfo(mContext, c); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1276 | |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1277 | final LongSparseArray<UserHandleCompat> allUsers = new LongSparseArray<>(); |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1278 | final LongSparseArray<Boolean> quietMode = new LongSparseArray<>(); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1279 | final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>(); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1280 | for (UserHandleCompat user : mUserManager.getUserProfiles()) { |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1281 | long serialNo = mUserManager.getSerialNumberForUser(user); |
| 1282 | allUsers.put(serialNo, user); |
| 1283 | quietMode.put(serialNo, mUserManager.isQuietModeEnabled(user)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1284 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1285 | boolean userUnlocked = mUserManager.isUserUnlocked(user); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1286 | |
| 1287 | // We can only query for shortcuts when the user is unlocked. |
| 1288 | if (userUnlocked) { |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 1289 | List<ShortcutInfoCompat> pinnedShortcuts = |
| 1290 | mDeepShortcutManager.queryForPinnedShortcuts(null, user); |
| 1291 | if (mDeepShortcutManager.wasLastCallSuccess()) { |
| 1292 | for (ShortcutInfoCompat shortcut : pinnedShortcuts) { |
| 1293 | shortcutKeyToPinnedShortcuts.put(ShortcutKey.fromInfo(shortcut), |
| 1294 | shortcut); |
| 1295 | } |
| 1296 | } else { |
| 1297 | // Shortcut manager can fail due to some race condition when the |
| 1298 | // lock state changes too frequently. For the purpose of the loading |
| 1299 | // shortcuts, consider the user is still locked. |
| 1300 | userUnlocked = false; |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1301 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1302 | } |
Sunny Goyal | 49f4f03 | 2016-08-01 15:45:49 -0700 | [diff] [blame] | 1303 | unlockedUsers.put(serialNo, userUnlocked); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1306 | ShortcutInfo info; |
| 1307 | String intentDescription; |
| 1308 | LauncherAppWidgetInfo appWidgetInfo; |
| 1309 | int container; |
| 1310 | long id; |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1311 | long serialNumber; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1312 | Intent intent; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1313 | UserHandleCompat user; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1314 | String targetPackage; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1315 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1316 | while (!mStopped && c.moveToNext()) { |
| 1317 | try { |
| 1318 | int itemType = c.getInt(itemTypeIndex); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1319 | boolean restored = 0 != c.getInt(restoredIndex); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1320 | boolean allowMissingTarget = false; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1321 | container = c.getInt(containerIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1322 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1323 | switch (itemType) { |
| 1324 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1325 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1326 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1327 | id = c.getLong(idIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1328 | intentDescription = c.getString(intentIndex); |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1329 | serialNumber = c.getInt(profileIdIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1330 | user = allUsers.get(serialNumber); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1331 | int promiseType = c.getInt(restoredIndex); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1332 | int disabledState = 0; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1333 | boolean itemReplaced = false; |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1334 | targetPackage = null; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1335 | if (user == null) { |
| 1336 | // User has been deleted remove the item. |
| 1337 | itemsToRemove.add(id); |
| 1338 | continue; |
| 1339 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1340 | try { |
| 1341 | intent = Intent.parseUri(intentDescription, 0); |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1342 | ComponentName cn = intent.getComponent(); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1343 | if (cn != null && cn.getPackageName() != null) { |
| 1344 | boolean validPkg = launcherApps.isPackageEnabledForProfile( |
| 1345 | cn.getPackageName(), user); |
| 1346 | boolean validComponent = validPkg && |
| 1347 | launcherApps.isActivityEnabledForProfile(cn, user); |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1348 | if (validPkg) { |
| 1349 | targetPackage = cn.getPackageName(); |
| 1350 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (validComponent) { |
| 1353 | if (restored) { |
| 1354 | // no special handling necessary for this item |
| 1355 | restoredRows.add(id); |
| 1356 | restored = false; |
| 1357 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1358 | if (quietMode.get(serialNumber)) { |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1359 | disabledState = ShortcutInfo.FLAG_DISABLED_QUIET_USER; |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 1360 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1361 | } else if (validPkg) { |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1362 | intent = null; |
| 1363 | if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) { |
| 1364 | // We allow auto install apps to have their intent |
| 1365 | // updated after an install. |
| 1366 | intent = manager.getLaunchIntentForPackage( |
| 1367 | cn.getPackageName()); |
| 1368 | if (intent != null) { |
| 1369 | ContentValues values = new ContentValues(); |
| 1370 | values.put(LauncherSettings.Favorites.INTENT, |
| 1371 | intent.toUri(0)); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1372 | updateItem(id, values); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | if (intent == null) { |
| 1377 | // The app is installed but the component is no |
| 1378 | // longer available. |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1379 | FileLog.d(TAG, "Invalid component removed: " + cn); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 1380 | itemsToRemove.add(id); |
| 1381 | continue; |
| 1382 | } else { |
| 1383 | // no special handling necessary for this item |
| 1384 | restoredRows.add(id); |
| 1385 | restored = false; |
| 1386 | } |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1387 | } else if (restored) { |
| 1388 | // Package is not yet available but might be |
| 1389 | // installed later. |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1390 | FileLog.d(TAG, "package not yet restored: " + cn); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1391 | |
| 1392 | if ((promiseType & ShortcutInfo.FLAG_RESTORE_STARTED) != 0) { |
| 1393 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1394 | } else if (installingPkgs.containsKey(cn.getPackageName())) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1395 | // App restore has started. Update the flag |
| 1396 | promiseType |= ShortcutInfo.FLAG_RESTORE_STARTED; |
| 1397 | ContentValues values = new ContentValues(); |
| 1398 | values.put(LauncherSettings.Favorites.RESTORED, |
| 1399 | promiseType); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1400 | updateItem(id, values); |
| 1401 | } else if ((promiseType & ShortcutInfo.FLAG_RESTORED_APP_TYPE) != 0) { |
| 1402 | // This is a common app. Try to replace this. |
| 1403 | int appType = CommonAppTypeParser.decodeItemTypeFromFlag(promiseType); |
| 1404 | CommonAppTypeParser parser = new CommonAppTypeParser(id, appType, context); |
| 1405 | if (parser.findDefaultApp()) { |
| 1406 | // Default app found. Replace it. |
| 1407 | intent = parser.parsedIntent; |
| 1408 | cn = intent.getComponent(); |
| 1409 | ContentValues values = parser.parsedValues; |
| 1410 | values.put(LauncherSettings.Favorites.RESTORED, 0); |
| 1411 | updateItem(id, values); |
| 1412 | restored = false; |
| 1413 | itemReplaced = true; |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1414 | |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1415 | } else { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1416 | FileLog.d(TAG, "Unrestored package removed: " + cn); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1417 | itemsToRemove.add(id); |
| 1418 | continue; |
| 1419 | } |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1420 | } else { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1421 | FileLog.d(TAG, "Unrestored package removed: " + cn); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1422 | itemsToRemove.add(id); |
| 1423 | continue; |
| 1424 | } |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1425 | } else if (PackageManagerHelper.isAppOnSdcard( |
| 1426 | manager, cn.getPackageName())) { |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1427 | // Package is present but not available. |
| 1428 | allowMissingTarget = true; |
| 1429 | disabledState = ShortcutInfo.FLAG_DISABLED_NOT_AVAILABLE; |
| 1430 | } else if (!isSdCardReady) { |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1431 | // SdCard is not ready yet. Package might get available, |
| 1432 | // once it is ready. |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1433 | Log.d(TAG, "Invalid package: " + cn + " (check again later)"); |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1434 | pendingPackages.addToList(user, cn.getPackageName()); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1435 | allowMissingTarget = true; |
| 1436 | // Add the icon on the workspace anyway. |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1437 | |
| 1438 | } else { |
| 1439 | // Do not wait for external media load anymore. |
| 1440 | // Log the invalid package, and remove it |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1441 | FileLog.d(TAG, "Invalid package removed: " + cn); |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1442 | itemsToRemove.add(id); |
| 1443 | continue; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1444 | } |
Sunny Goyal | 938a53d | 2014-09-05 03:17:45 -0700 | [diff] [blame] | 1445 | } else if (cn == null) { |
| 1446 | // For shortcuts with no component, keep them as they are |
| 1447 | restoredRows.add(id); |
| 1448 | restored = false; |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1449 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1450 | } catch (URISyntaxException e) { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1451 | FileLog.d(TAG, "Invalid uri: " + intentDescription); |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1452 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1453 | continue; |
| 1454 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1455 | |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 1456 | boolean useLowResIcon = container >= 0 && |
| 1457 | c.getInt(rankIndex) >= FolderIcon.NUM_ITEMS_IN_PREVIEW; |
| 1458 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1459 | if (itemReplaced) { |
| 1460 | if (user.equals(UserHandleCompat.myUserHandle())) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1461 | info = getAppShortcutInfo(intent, user, null, |
| 1462 | cursorIconInfo, false, useLowResIcon); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1463 | } else { |
| 1464 | // Don't replace items for other profiles. |
| 1465 | itemsToRemove.add(id); |
| 1466 | continue; |
| 1467 | } |
| 1468 | } else if (restored) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1469 | if (user.equals(UserHandleCompat.myUserHandle())) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1470 | info = getRestoredItemInfo(c, intent, |
| 1471 | promiseType, itemType, cursorIconInfo); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1472 | intent = getRestoredItemIntent(c, context, intent); |
| 1473 | } else { |
| 1474 | // Don't restore items for other profiles. |
| 1475 | itemsToRemove.add(id); |
| 1476 | continue; |
| 1477 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1478 | } else if (itemType == |
| 1479 | LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1480 | info = getAppShortcutInfo(intent, user, c, |
| 1481 | cursorIconInfo, allowMissingTarget, useLowResIcon); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1482 | } else if (itemType == |
| 1483 | LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1484 | |
| 1485 | ShortcutKey key = ShortcutKey.fromIntent(intent, user); |
| 1486 | if (unlockedUsers.get(serialNumber)) { |
| 1487 | ShortcutInfoCompat pinnedShortcut = |
| 1488 | shortcutKeyToPinnedShortcuts.get(key); |
| 1489 | if (pinnedShortcut == null) { |
| 1490 | // The shortcut is no longer valid. |
| 1491 | itemsToRemove.add(id); |
| 1492 | continue; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1493 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1494 | info = new ShortcutInfo(pinnedShortcut, context); |
| 1495 | intent = info.intent; |
| 1496 | } else { |
| 1497 | // Create a shortcut info in disabled mode for now. |
| 1498 | info = new ShortcutInfo(); |
| 1499 | info.user = user; |
| 1500 | info.itemType = itemType; |
| 1501 | loadInfoFromCursor(info, c, cursorIconInfo); |
| 1502 | |
| 1503 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_LOCKED_USER; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1504 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1505 | } else { // item type == ITEM_TYPE_SHORTCUT |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1506 | info = getShortcutInfo(c, cursorIconInfo); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1507 | |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1508 | // Shortcuts are only available on the primary profile |
| 1509 | if (PackageManagerHelper.isAppSuspended(manager, targetPackage)) { |
| 1510 | disabledState |= ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 1511 | } |
| 1512 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1513 | // App shortcuts that used to be automatically added to Launcher |
| 1514 | // didn't always have the correct intent flags set, so do that |
| 1515 | // here |
| 1516 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1517 | intent.getCategories() != null && |
| 1518 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1519 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1520 | intent.addFlags( |
| 1521 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1522 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1523 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1524 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1525 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1526 | if (info != null) { |
Winson Chung | ee05571 | 2013-07-30 14:46:24 -0700 | [diff] [blame] | 1527 | info.id = id; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1528 | info.intent = intent; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1529 | info.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1530 | info.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1531 | info.cellX = c.getInt(cellXIndex); |
| 1532 | info.cellY = c.getInt(cellYIndex); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1533 | info.rank = c.getInt(rankIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1534 | info.spanX = 1; |
| 1535 | info.spanY = 1; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1536 | info.intent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 1537 | if (info.promisedIntent != null) { |
| 1538 | info.promisedIntent.putExtra(ItemInfo.EXTRA_PROFILE, serialNumber); |
| 1539 | } |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 1540 | info.isDisabled |= disabledState; |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 1541 | if (isSafeMode && !Utilities.isSystemApp(context, intent)) { |
| 1542 | info.isDisabled |= ShortcutInfo.FLAG_DISABLED_SAFEMODE; |
| 1543 | } |
Adam Cohen | ae4409d | 2013-11-26 10:34:59 -0800 | [diff] [blame] | 1544 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1545 | // check & update map of what's occupied |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1546 | if (!checkItemPlacement(occupied, info, sBgDataModel.workspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 1547 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1548 | break; |
| 1549 | } |
| 1550 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1551 | if (restored) { |
| 1552 | ComponentName cn = info.getTargetComponent(); |
| 1553 | if (cn != null) { |
| 1554 | Integer progress = installingPkgs.get(cn.getPackageName()); |
| 1555 | if (progress != null) { |
| 1556 | info.setInstallProgress(progress); |
| 1557 | } else { |
| 1558 | info.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1563 | sBgDataModel.addItem(info, false); |
Winson Chung | 1323b48 | 2013-08-05 12:41:55 -0700 | [diff] [blame] | 1564 | } else { |
| 1565 | throw new RuntimeException("Unexpected null ShortcutInfo"); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1566 | } |
| 1567 | break; |
| 1568 | |
| 1569 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 1570 | id = c.getLong(idIndex); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1571 | FolderInfo folderInfo = sBgDataModel.findOrMakeFolder(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1572 | |
Sunny Goyal | a508e4f | 2015-05-21 09:33:57 -0700 | [diff] [blame] | 1573 | // Do not trim the folder label, as is was set by the user. |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 1574 | folderInfo.title = c.getString(cursorIconInfo.titleIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1575 | folderInfo.id = id; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1576 | folderInfo.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1577 | folderInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1578 | folderInfo.cellX = c.getInt(cellXIndex); |
| 1579 | folderInfo.cellY = c.getInt(cellYIndex); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 1580 | folderInfo.spanX = 1; |
| 1581 | folderInfo.spanY = 1; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1582 | folderInfo.options = c.getInt(optionsIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1583 | |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1584 | // check & update map of what's occupied |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1585 | if (!checkItemPlacement(occupied, folderInfo, sBgDataModel.workspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 1586 | itemsToRemove.add(id); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1587 | break; |
| 1588 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1589 | if (restored) { |
| 1590 | // no special handling required for restored folders |
| 1591 | restoredRows.add(id); |
| 1592 | } |
| 1593 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1594 | sBgDataModel.addItem(folderInfo, false); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1595 | break; |
| 1596 | |
| 1597 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1598 | case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET: |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1599 | // Read all Launcher-specific widget details |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1600 | boolean customWidget = itemType == |
| 1601 | LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 1602 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1603 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1604 | serialNumber = c.getLong(profileIdIndex); |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1605 | String savedProvider = c.getString(appWidgetProviderIndex); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1606 | id = c.getLong(idIndex); |
Sunny Goyal | 7f834d2 | 2015-04-21 10:10:23 -0700 | [diff] [blame] | 1607 | user = allUsers.get(serialNumber); |
| 1608 | if (user == null) { |
| 1609 | itemsToRemove.add(id); |
| 1610 | continue; |
| 1611 | } |
| 1612 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1613 | final ComponentName component = |
| 1614 | ComponentName.unflattenFromString(savedProvider); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1615 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1616 | final int restoreStatus = c.getInt(restoredIndex); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1617 | final boolean isIdValid = (restoreStatus & |
| 1618 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) == 0; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1619 | final boolean wasProviderReady = (restoreStatus & |
| 1620 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1621 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1622 | if (widgetProvidersMap == null) { |
| 1623 | widgetProvidersMap = AppWidgetManagerCompat |
| 1624 | .getInstance(mContext).getAllProvidersMap(); |
| 1625 | } |
| 1626 | final AppWidgetProviderInfo provider = widgetProvidersMap.get( |
| 1627 | new ComponentKey( |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 1628 | ComponentName.unflattenFromString(savedProvider), |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1629 | user)); |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1630 | |
| 1631 | final boolean isProviderReady = isValidProvider(provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1632 | if (!isSafeMode && !customWidget && |
| 1633 | wasProviderReady && !isProviderReady) { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1634 | FileLog.d(TAG, "Deleting widget that isn't installed anymore: " |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1635 | + provider); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1636 | itemsToRemove.add(id); |
| 1637 | } else { |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1638 | if (isProviderReady) { |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1639 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 1640 | provider.provider); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1641 | |
Sunny Goyal | 53f9672 | 2015-07-13 19:54:53 -0700 | [diff] [blame] | 1642 | // The provider is available. So the widget is either |
| 1643 | // available or not available. We do not need to track |
| 1644 | // any future restore updates. |
| 1645 | int status = restoreStatus & |
| 1646 | ~LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1647 | if (!wasProviderReady) { |
| 1648 | // If provider was not previously ready, update the |
| 1649 | // status and UI flag. |
| 1650 | |
| 1651 | // Id would be valid only if the widget restore broadcast was received. |
| 1652 | if (isIdValid) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1653 | status |= LauncherAppWidgetInfo.FLAG_UI_NOT_READY; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1654 | } else { |
| 1655 | status &= ~LauncherAppWidgetInfo |
| 1656 | .FLAG_PROVIDER_NOT_READY; |
| 1657 | } |
| 1658 | } |
| 1659 | appWidgetInfo.restoreStatus = status; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1660 | } else { |
| 1661 | Log.v(TAG, "Widget restore pending id=" + id |
| 1662 | + " appWidgetId=" + appWidgetId |
| 1663 | + " status =" + restoreStatus); |
| 1664 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1665 | component); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1666 | appWidgetInfo.restoreStatus = restoreStatus; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1667 | Integer installProgress = installingPkgs.get(component.getPackageName()); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1668 | |
| 1669 | if ((restoreStatus & LauncherAppWidgetInfo.FLAG_RESTORE_STARTED) != 0) { |
| 1670 | // Restore has started once. |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1671 | } else if (installProgress != null) { |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1672 | // App restore has started. Update the flag |
| 1673 | appWidgetInfo.restoreStatus |= |
| 1674 | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED; |
Sunny Goyal | b05a00a | 2016-08-29 10:06:57 -0700 | [diff] [blame] | 1675 | } else if (!isSafeMode) { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1676 | FileLog.d(TAG, "Unrestored widget removed: " + component); |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 1677 | itemsToRemove.add(id); |
| 1678 | continue; |
| 1679 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 1680 | |
| 1681 | appWidgetInfo.installProgress = |
| 1682 | installProgress == null ? 0 : installProgress; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1683 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 1684 | if (appWidgetInfo.hasRestoreFlag( |
| 1685 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG)) { |
| 1686 | intentDescription = c.getString(intentIndex); |
| 1687 | if (!TextUtils.isEmpty(intentDescription)) { |
| 1688 | appWidgetInfo.bindOptions = |
| 1689 | Intent.parseUri(intentDescription, 0); |
| 1690 | } |
| 1691 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 1692 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1693 | appWidgetInfo.id = id; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1694 | appWidgetInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1695 | appWidgetInfo.cellX = c.getInt(cellXIndex); |
| 1696 | appWidgetInfo.cellY = c.getInt(cellYIndex); |
| 1697 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 1698 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
Sunny Goyal | ab7a4fe | 2015-07-15 17:20:54 -0700 | [diff] [blame] | 1699 | appWidgetInfo.user = user; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1700 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1701 | if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1702 | container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1703 | Log.e(TAG, "Widget found where container != " + |
Sunny Goyal | 41cdc8d | 2015-09-04 12:53:04 -0700 | [diff] [blame] | 1704 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
| 1705 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1706 | continue; |
| 1707 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1708 | |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1709 | appWidgetInfo.container = container; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1710 | // check & update map of what's occupied |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1711 | if (!checkItemPlacement(occupied, appWidgetInfo, sBgDataModel.workspaceScreens)) { |
Sunny Goyal | fc0fe6b | 2014-10-16 12:18:37 -0700 | [diff] [blame] | 1712 | itemsToRemove.add(id); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | } |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 1715 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1716 | if (!customWidget) { |
| 1717 | String providerName = |
| 1718 | appWidgetInfo.providerName.flattenToString(); |
| 1719 | if (!providerName.equals(savedProvider) || |
| 1720 | (appWidgetInfo.restoreStatus != restoreStatus)) { |
| 1721 | ContentValues values = new ContentValues(); |
| 1722 | values.put( |
| 1723 | LauncherSettings.Favorites.APPWIDGET_PROVIDER, |
| 1724 | providerName); |
| 1725 | values.put(LauncherSettings.Favorites.RESTORED, |
| 1726 | appWidgetInfo.restoreStatus); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1727 | updateItem(id, values); |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 1728 | } |
Chris Wren | c3919c0 | 2013-09-18 09:48:33 -0400 | [diff] [blame] | 1729 | } |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1730 | sBgDataModel.addItem(appWidgetInfo, false); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1731 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1732 | break; |
| 1733 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1734 | } catch (Exception e) { |
Sunny Goyal | a136545 | 2015-10-01 15:46:24 -0700 | [diff] [blame] | 1735 | Log.e(TAG, "Desktop items loading interrupted", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1736 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1737 | } |
| 1738 | } finally { |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 1739 | Utilities.closeSilently(c); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1742 | // Break early if we've stopped loading |
| 1743 | if (mStopped) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1744 | sBgDataModel.clear(); |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1745 | return; |
Winson Chung | ba9c37f | 2013-08-30 14:11:37 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1748 | if (itemsToRemove.size() > 0) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1749 | // Remove dead items |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1750 | contentResolver.delete(LauncherSettings.Favorites.CONTENT_URI, |
| 1751 | Utilities.createDbSelectionQuery( |
| 1752 | LauncherSettings.Favorites._ID, itemsToRemove), null); |
| 1753 | if (DEBUG_LOADERS) { |
| 1754 | Log.d(TAG, "Removed = " + Utilities.createDbSelectionQuery( |
| 1755 | LauncherSettings.Favorites._ID, itemsToRemove)); |
| 1756 | } |
| 1757 | |
| 1758 | // Remove any empty folder |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 1759 | ArrayList<Long> deletedFolderIds = (ArrayList<Long>) LauncherSettings.Settings |
| 1760 | .call(contentResolver, |
| 1761 | LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS) |
| 1762 | .getSerializable(LauncherSettings.Settings.EXTRA_VALUE); |
| 1763 | for (long folderId : deletedFolderIds) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1764 | sBgDataModel.workspaceItems.remove(sBgDataModel.folders.get(folderId)); |
| 1765 | sBgDataModel.folders.remove(folderId); |
| 1766 | sBgDataModel.itemsIdMap.remove(folderId); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1770 | // Unpin shortcuts that don't exist on the workspace. |
| 1771 | for (ShortcutKey key : shortcutKeyToPinnedShortcuts.keySet()) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1772 | MutableInt numTimesPinned = sBgDataModel.pinnedShortcutCounts.get(key); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 1773 | if (numTimesPinned == null || numTimesPinned.value == 0) { |
| 1774 | // Shortcut is pinned but doesn't exist on the workspace; unpin it. |
| 1775 | mDeepShortcutManager.unpinShortcut(key); |
| 1776 | } |
| 1777 | } |
| 1778 | |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1779 | // 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] | 1780 | for (FolderInfo folder : sBgDataModel.folders) { |
Sunny Goyal | 317698b | 2015-07-29 11:45:41 -0700 | [diff] [blame] | 1781 | Collections.sort(folder.contents, Folder.ITEM_POS_COMPARATOR); |
| 1782 | int pos = 0; |
| 1783 | for (ShortcutInfo info : folder.contents) { |
| 1784 | if (info.usingLowResIcon) { |
| 1785 | info.updateIcon(mIconCache, false); |
| 1786 | } |
| 1787 | pos ++; |
| 1788 | if (pos >= FolderIcon.NUM_ITEMS_IN_PREVIEW) { |
| 1789 | break; |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 | |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1794 | if (restoredRows.size() > 0) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1795 | // Update restored items that no longer require special handling |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 1796 | ContentValues values = new ContentValues(); |
| 1797 | values.put(LauncherSettings.Favorites.RESTORED, 0); |
| 1798 | contentResolver.update(LauncherSettings.Favorites.CONTENT_URI, values, |
| 1799 | Utilities.createDbSelectionQuery( |
| 1800 | LauncherSettings.Favorites._ID, restoredRows), null); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 1801 | } |
| 1802 | |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1803 | if (!isSdCardReady && !pendingPackages.isEmpty()) { |
| 1804 | context.registerReceiver( |
| 1805 | new SdCardAvailableReceiver( |
| 1806 | LauncherModel.this, mContext, pendingPackages), |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 1807 | new IntentFilter(Intent.ACTION_BOOT_COMPLETED), |
Sunny Goyal | c2936bc | 2016-09-01 15:50:36 -0700 | [diff] [blame] | 1808 | null, |
| 1809 | sWorker); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1812 | // Remove any empty screens |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1813 | ArrayList<Long> unusedScreens = new ArrayList<Long>(sBgDataModel.workspaceScreens); |
| 1814 | for (ItemInfo item: sBgDataModel.itemsIdMap) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 1815 | long screenId = item.screenId; |
| 1816 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1817 | unusedScreens.contains(screenId)) { |
| 1818 | unusedScreens.remove(screenId); |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | // If there are any empty screens remove them, and update. |
| 1823 | if (unusedScreens.size() != 0) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 1824 | sBgDataModel.workspaceScreens.removeAll(unusedScreens); |
| 1825 | updateWorkspaceScreenOrder(context, sBgDataModel.workspaceScreens); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1828 | if (DEBUG_LOADERS) { |
| 1829 | Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1830 | Log.d(TAG, "workspace layout: "); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1831 | int nScreens = occupied.size(); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1832 | for (int y = 0; y < countY; y++) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1833 | String line = ""; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1834 | |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 1835 | for (int i = 0; i < nScreens; i++) { |
| 1836 | long screenId = occupied.keyAt(i); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 1837 | if (screenId > 0) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1838 | line += " | "; |
| 1839 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1840 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1841 | Log.d(TAG, "[ " + line + " ]"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1842 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1843 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1844 | } |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 1845 | if (LauncherAppState.PROFILE_STARTUP) { |
| 1846 | Trace.endSection(); |
| 1847 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1850 | /** |
| 1851 | * Partially updates the item without any notification. Must be called on the worker thread. |
| 1852 | */ |
| 1853 | private void updateItem(long itemId, ContentValues update) { |
| 1854 | mContext.getContentResolver().update( |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 1855 | LauncherSettings.Favorites.CONTENT_URI, |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1856 | update, |
| 1857 | BaseColumns._ID + "= ?", |
| 1858 | new String[]{Long.toString(itemId)}); |
| 1859 | } |
| 1860 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1861 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 1862 | * specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1863 | private void filterCurrentWorkspaceItems(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1864 | ArrayList<ItemInfo> allWorkspaceItems, |
| 1865 | ArrayList<ItemInfo> currentScreenItems, |
| 1866 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1867 | // Purge any null ItemInfos |
| 1868 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 1869 | while (iter.hasNext()) { |
| 1870 | ItemInfo i = iter.next(); |
| 1871 | if (i == null) { |
| 1872 | iter.remove(); |
| 1873 | } |
| 1874 | } |
| 1875 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1876 | // Order the set of items by their containers first, this allows use to walk through the |
| 1877 | // list sequentially, build up a list of containers that are in the specified screen, |
| 1878 | // as well as all items in those containers. |
| 1879 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 1880 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 1881 | @Override |
| 1882 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 1883 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1884 | } |
| 1885 | }); |
| 1886 | for (ItemInfo info : allWorkspaceItems) { |
| 1887 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1888 | if (info.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1889 | currentScreenItems.add(info); |
| 1890 | itemsOnScreen.add(info.id); |
| 1891 | } else { |
| 1892 | otherScreenItems.add(info); |
| 1893 | } |
| 1894 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1895 | currentScreenItems.add(info); |
| 1896 | itemsOnScreen.add(info.id); |
| 1897 | } else { |
| 1898 | if (itemsOnScreen.contains(info.container)) { |
| 1899 | currentScreenItems.add(info); |
| 1900 | itemsOnScreen.add(info.id); |
| 1901 | } else { |
| 1902 | otherScreenItems.add(info); |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /** Filters the set of widgets which are on the specified screen. */ |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1909 | private void filterCurrentAppWidgets(long currentScreenId, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1910 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1911 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 1912 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1913 | |
| 1914 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1915 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1916 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 1917 | widget.screenId == currentScreenId) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1918 | currentScreenWidgets.add(widget); |
| 1919 | } else { |
| 1920 | otherScreenWidgets.add(widget); |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1925 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 1926 | * right) */ |
| 1927 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 1928 | final LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1929 | final InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1930 | final int screenCols = profile.numColumns; |
| 1931 | final int screenCellCount = profile.numColumns * profile.numRows; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1932 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1933 | @Override |
| 1934 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1935 | if (lhs.container == rhs.container) { |
| 1936 | // Within containers, order by their spatial position in that container |
| 1937 | switch ((int) lhs.container) { |
| 1938 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: { |
| 1939 | long lr = (lhs.screenId * screenCellCount + |
| 1940 | lhs.cellY * screenCols + lhs.cellX); |
| 1941 | long rr = (rhs.screenId * screenCellCount + |
| 1942 | rhs.cellY * screenCols + rhs.cellX); |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1943 | return Utilities.longCompare(lr, rr); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1944 | } |
| 1945 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: { |
| 1946 | // We currently use the screen id as the rank |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1947 | return Utilities.longCompare(lhs.screenId, rhs.screenId); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1948 | } |
| 1949 | default: |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 1950 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1951 | throw new RuntimeException("Unexpected container type when " + |
| 1952 | "sorting workspace items."); |
| 1953 | } |
| 1954 | return 0; |
| 1955 | } |
| 1956 | } else { |
| 1957 | // Between containers, order by hotseat, desktop |
Winson | 722e856 | 2015-10-07 13:04:30 -0700 | [diff] [blame] | 1958 | return Utilities.longCompare(lhs.container, rhs.container); |
Winson Chung | 882a52e | 2015-07-08 14:32:26 -0700 | [diff] [blame] | 1959 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1960 | } |
| 1961 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1962 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1963 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1964 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 1965 | final ArrayList<Long> orderedScreens) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1966 | final Runnable r = new Runnable() { |
| 1967 | @Override |
| 1968 | public void run() { |
| 1969 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1970 | if (callbacks != null) { |
| 1971 | callbacks.bindScreens(orderedScreens); |
| 1972 | } |
| 1973 | } |
| 1974 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 1975 | runOnMainThread(r); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1978 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 1979 | final ArrayList<ItemInfo> workspaceItems, |
| 1980 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1981 | final Executor executor) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1982 | |
| 1983 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1984 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1985 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1986 | final int start = i; |
| 1987 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1988 | final Runnable r = new Runnable() { |
| 1989 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1990 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1991 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1992 | if (callbacks != null) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 1993 | callbacks.bindItems(workspaceItems, start, start+chunkSize, |
| 1994 | false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1995 | } |
| 1996 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1997 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 1998 | executor.execute(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1999 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2000 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2001 | // Bind the widgets, one at a time |
| 2002 | N = appWidgets.size(); |
| 2003 | for (int i = 0; i < N; i++) { |
| 2004 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 2005 | final Runnable r = new Runnable() { |
| 2006 | public void run() { |
| 2007 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2008 | if (callbacks != null) { |
| 2009 | callbacks.bindAppWidget(widget); |
| 2010 | } |
| 2011 | } |
| 2012 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2013 | executor.execute(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | /** |
| 2018 | * Binds all loaded data to actual views on the main thread. |
| 2019 | */ |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2020 | private void bindWorkspace(int synchronizeBindPage) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2021 | final long t = SystemClock.uptimeMillis(); |
| 2022 | Runnable r; |
| 2023 | |
| 2024 | // Don't use these two variables in any of the callback runnables. |
| 2025 | // Otherwise we hold a reference to them. |
| 2026 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2027 | if (oldCallbacks == null) { |
| 2028 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2029 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 2030 | return; |
| 2031 | } |
| 2032 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2033 | // Save a copy of all the bg-thread collections |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 2034 | ArrayList<ItemInfo> workspaceItems = new ArrayList<>(); |
| 2035 | ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>(); |
| 2036 | ArrayList<Long> orderedScreenIds = new ArrayList<>(); |
Sunny Goyal | e2df062 | 2015-04-24 11:27:00 -0700 | [diff] [blame] | 2037 | |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 2038 | synchronized (sBgDataModel) { |
| 2039 | workspaceItems.addAll(sBgDataModel.workspaceItems); |
| 2040 | appWidgets.addAll(sBgDataModel.appWidgets); |
| 2041 | orderedScreenIds.addAll(sBgDataModel.workspaceScreens); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 2044 | final int currentScreen; |
| 2045 | { |
| 2046 | int currScreen = synchronizeBindPage != PagedView.INVALID_RESTORE_PAGE |
| 2047 | ? synchronizeBindPage : oldCallbacks.getCurrentWorkspaceScreen(); |
| 2048 | if (currScreen >= orderedScreenIds.size()) { |
| 2049 | // There may be no workspace screens (just hotseat items and an empty page). |
| 2050 | currScreen = PagedView.INVALID_RESTORE_PAGE; |
| 2051 | } |
| 2052 | currentScreen = currScreen; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2053 | } |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 2054 | final boolean validFirstPage = currentScreen >= 0; |
| 2055 | final long currentScreenId = |
| 2056 | validFirstPage ? orderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID; |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2057 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2058 | // 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] | 2059 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>(); |
| 2060 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>(); |
| 2061 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>(); |
| 2062 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2063 | |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2064 | filterCurrentWorkspaceItems(currentScreenId, workspaceItems, currentWorkspaceItems, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2065 | otherWorkspaceItems); |
Winson Chung | 9b9fb96 | 2013-11-15 15:39:34 -0800 | [diff] [blame] | 2066 | filterCurrentAppWidgets(currentScreenId, appWidgets, currentAppWidgets, |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2067 | otherAppWidgets); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2068 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 2069 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 2070 | |
| 2071 | // Tell the workspace that we're about to start binding items |
| 2072 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2073 | public void run() { |
| 2074 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2075 | if (callbacks != null) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2076 | callbacks.clearPendingBinds(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2077 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2078 | } |
| 2079 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2080 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 2081 | runOnMainThread(r); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2082 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2083 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 2084 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2085 | Executor mainExecutor = new DeferredMainThreadExecutor(); |
| 2086 | // Load items on the current page. |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 2087 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, mainExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2088 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 2089 | // In case of validFirstPage, only bind the first screen, and defer binding the |
| 2090 | // remaining screens after first onDraw (and an optional the fade animation whichever |
| 2091 | // happens later). |
| 2092 | // This ensures that the first screen is immediately visible (eg. during rotation) |
| 2093 | // In case of !validFirstPage, bind all pages one after other. |
| 2094 | final Executor deferredExecutor = |
| 2095 | validFirstPage ? new ViewOnDrawExecutor(mHandler) : mainExecutor; |
| 2096 | |
| 2097 | mainExecutor.execute(new Runnable() { |
| 2098 | @Override |
| 2099 | public void run() { |
| 2100 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2101 | if (callbacks != null) { |
| 2102 | callbacks.finishFirstPageBind( |
| 2103 | validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null); |
| 2104 | } |
| 2105 | } |
| 2106 | }); |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2107 | |
Sunny Goyal | 44c0643 | 2016-04-02 10:56:02 -0700 | [diff] [blame] | 2108 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, deferredExecutor); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2109 | |
| 2110 | // Tell the workspace that we're done binding items |
| 2111 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2112 | public void run() { |
| 2113 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2114 | if (callbacks != null) { |
Sunny Goyal | 66cfdc2 | 2015-02-02 13:01:51 -0800 | [diff] [blame] | 2115 | callbacks.finishBindingItems(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2116 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2117 | |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2118 | mIsLoadingAndBindingWorkspace = false; |
| 2119 | |
| 2120 | // Run all the bind complete runnables after workspace is bound. |
| 2121 | if (!mBindCompleteRunnables.isEmpty()) { |
| 2122 | synchronized (mBindCompleteRunnables) { |
| 2123 | for (final Runnable r : mBindCompleteRunnables) { |
| 2124 | runOnWorkerThread(r); |
| 2125 | } |
| 2126 | mBindCompleteRunnables.clear(); |
| 2127 | } |
| 2128 | } |
| 2129 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 2130 | // 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] | 2131 | if (DEBUG_LOADERS) { |
| 2132 | Log.d(TAG, "bound workspace in " |
| 2133 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 2134 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 2135 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2136 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2137 | }; |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2138 | deferredExecutor.execute(r); |
| 2139 | |
Sunny Goyal | b5b9ad6 | 2016-04-02 11:23:39 -0700 | [diff] [blame] | 2140 | if (validFirstPage) { |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2141 | r = new Runnable() { |
| 2142 | public void run() { |
| 2143 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2144 | if (callbacks != null) { |
| 2145 | // We are loading synchronously, which means, some of the pages will be |
| 2146 | // bound after first draw. Inform the callbacks that page binding is |
| 2147 | // not complete, and schedule the remaining pages. |
| 2148 | if (currentScreen != PagedView.INVALID_RESTORE_PAGE) { |
| 2149 | callbacks.onPageBoundSynchronously(currentScreen); |
| 2150 | } |
| 2151 | callbacks.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor); |
| 2152 | } |
| 2153 | } |
| 2154 | }; |
Sunny Goyal | d33860f | 2015-04-23 16:02:20 -0700 | [diff] [blame] | 2155 | runOnMainThread(r); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2156 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2157 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2158 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2159 | private void loadAndBindAllApps() { |
| 2160 | if (DEBUG_LOADERS) { |
| 2161 | Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded); |
| 2162 | } |
| 2163 | if (!mAllAppsLoaded) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2164 | loadAllApps(); |
Sunny Goyal | f5cd998 | 2015-05-18 15:19:29 -0700 | [diff] [blame] | 2165 | synchronized (LoaderTask.this) { |
| 2166 | if (mStopped) { |
| 2167 | return; |
| 2168 | } |
| 2169 | } |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2170 | updateIconCache(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 2171 | synchronized (LoaderTask.this) { |
| 2172 | if (mStopped) { |
| 2173 | return; |
| 2174 | } |
| 2175 | mAllAppsLoaded = true; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2176 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2177 | } else { |
| 2178 | onlyBindAllApps(); |
| 2179 | } |
| 2180 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2181 | |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2182 | private void updateIconCache() { |
| 2183 | // Ignore packages which have a promise icon. |
| 2184 | HashSet<String> packagesToIgnore = new HashSet<>(); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 2185 | synchronized (sBgDataModel) { |
| 2186 | for (ItemInfo info : sBgDataModel.itemsIdMap) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2187 | if (info instanceof ShortcutInfo) { |
| 2188 | ShortcutInfo si = (ShortcutInfo) info; |
| 2189 | if (si.isPromise() && si.getTargetComponent() != null) { |
| 2190 | packagesToIgnore.add(si.getTargetComponent().getPackageName()); |
| 2191 | } |
| 2192 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 2193 | LauncherAppWidgetInfo lawi = (LauncherAppWidgetInfo) info; |
| 2194 | if (lawi.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY)) { |
| 2195 | packagesToIgnore.add(lawi.providerName.getPackageName()); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | mIconCache.updateDbIcons(packagesToIgnore); |
| 2201 | } |
| 2202 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2203 | private void onlyBindAllApps() { |
| 2204 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2205 | if (oldCallbacks == null) { |
| 2206 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2207 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 2208 | return; |
| 2209 | } |
| 2210 | |
| 2211 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 2212 | @SuppressWarnings("unchecked") |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2213 | final ArrayList<AppInfo> list |
| 2214 | = (ArrayList<AppInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2215 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2216 | public void run() { |
| 2217 | final long t = SystemClock.uptimeMillis(); |
| 2218 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2219 | if (callbacks != null) { |
| 2220 | callbacks.bindAllApplications(list); |
| 2221 | } |
| 2222 | if (DEBUG_LOADERS) { |
| 2223 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
Hyunyoung Song | 747a5bc | 2016-02-08 11:31:33 -0800 | [diff] [blame] | 2224 | + (SystemClock.uptimeMillis() - t) + "ms"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2225 | } |
| 2226 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2227 | }; |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 2228 | runOnMainThread(r); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2229 | } |
| 2230 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2231 | private void loadAllApps() { |
| 2232 | final long loadTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2233 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2234 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2235 | if (oldCallbacks == null) { |
| 2236 | // This launcher has exited and nobody bothered to tell us. Just bail. |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2237 | Log.w(TAG, "LoaderTask running with no launcher (loadAllApps)"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2238 | return; |
| 2239 | } |
| 2240 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2241 | final List<UserHandleCompat> profiles = mUserManager.getUserProfiles(); |
| 2242 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2243 | // Clear the list of apps |
| 2244 | mBgAllAppsList.clear(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2245 | for (UserHandleCompat user : profiles) { |
| 2246 | // Query for the set of apps |
| 2247 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2248 | final List<LauncherActivityInfoCompat> apps = mLauncherApps.getActivityList(null, user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2249 | if (DEBUG_LOADERS) { |
| 2250 | Log.d(TAG, "getActivityList took " |
| 2251 | + (SystemClock.uptimeMillis()-qiaTime) + "ms for user " + user); |
| 2252 | Log.d(TAG, "getActivityList got " + apps.size() + " apps for user " + user); |
| 2253 | } |
| 2254 | // Fail if we don't have any apps |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2255 | // TODO: Fix this. Only fail for the current user. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2256 | if (apps == null || apps.isEmpty()) { |
| 2257 | return; |
| 2258 | } |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 2259 | boolean quietMode = mUserManager.isQuietModeEnabled(user); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2260 | // Create the ApplicationInfos |
| 2261 | for (int i = 0; i < apps.size(); i++) { |
| 2262 | LauncherActivityInfoCompat app = apps.get(i); |
| 2263 | // This builds the icon bitmaps. |
Kenny Guy | ff05f43 | 2016-01-22 17:48:29 +0000 | [diff] [blame] | 2264 | mBgAllAppsList.add(new AppInfo(mContext, app, user, mIconCache, quietMode)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2265 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2266 | |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2267 | final ManagedProfileHeuristic heuristic = ManagedProfileHeuristic.get(mContext, user); |
| 2268 | if (heuristic != null) { |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2269 | final Runnable r = new Runnable() { |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2270 | |
| 2271 | @Override |
| 2272 | public void run() { |
| 2273 | heuristic.processUserApps(apps); |
| 2274 | } |
Sunny Goyal | 639e906 | 2015-08-19 19:17:06 -0700 | [diff] [blame] | 2275 | }; |
| 2276 | runOnMainThread(new Runnable() { |
| 2277 | |
| 2278 | @Override |
| 2279 | public void run() { |
| 2280 | // Check isLoadingWorkspace on the UI thread, as it is updated on |
| 2281 | // the UI thread. |
| 2282 | if (mIsLoadingAndBindingWorkspace) { |
| 2283 | synchronized (mBindCompleteRunnables) { |
| 2284 | mBindCompleteRunnables.add(r); |
| 2285 | } |
| 2286 | } else { |
| 2287 | runOnWorkerThread(r); |
| 2288 | } |
| 2289 | } |
Sunny Goyal | 756a28a | 2015-04-23 17:07:55 -0700 | [diff] [blame] | 2290 | }); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2291 | } |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2292 | } |
Bjorn Bringert | 85f418d | 2013-09-06 12:50:05 +0100 | [diff] [blame] | 2293 | // Huh? Shouldn't this be inside the Runnable below? |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2294 | final ArrayList<AppInfo> added = mBgAllAppsList.added; |
| 2295 | mBgAllAppsList.added = new ArrayList<AppInfo>(); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2296 | |
| 2297 | // Post callback on main thread |
| 2298 | mHandler.post(new Runnable() { |
| 2299 | public void run() { |
Hyunyoung Song | 9892e58 | 2015-05-05 10:07:23 -0700 | [diff] [blame] | 2300 | |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2301 | final long bindTime = SystemClock.uptimeMillis(); |
Winson Chung | 11a1a53 | 2013-09-13 11:14:45 -0700 | [diff] [blame] | 2302 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2303 | if (callbacks != null) { |
| 2304 | callbacks.bindAllApplications(added); |
| 2305 | if (DEBUG_LOADERS) { |
| 2306 | Log.d(TAG, "bound " + added.size() + " apps in " |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2307 | + (SystemClock.uptimeMillis() - bindTime) + "ms"); |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2308 | } |
| 2309 | } else { |
| 2310 | Log.i(TAG, "not binding apps: no Launcher activity"); |
| 2311 | } |
| 2312 | } |
| 2313 | }); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2314 | // Cleanup any data stored for a deleted user. |
| 2315 | ManagedProfileHeuristic.processAllUsers(profiles, mContext); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2316 | if (DEBUG_LOADERS) { |
Winson Chung | 64359a5 | 2013-07-08 17:17:08 -0700 | [diff] [blame] | 2317 | Log.d(TAG, "Icons processed in " |
| 2318 | + (SystemClock.uptimeMillis() - loadTime) + "ms"); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2322 | private void loadAndBindDeepShortcuts() { |
| 2323 | if (DEBUG_LOADERS) { |
| 2324 | Log.d(TAG, "loadAndBindDeepShortcuts mDeepShortcutsLoaded=" + mDeepShortcutsLoaded); |
| 2325 | } |
| 2326 | if (!mDeepShortcutsLoaded) { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 2327 | sBgDataModel.deepShortcutMap.clear(); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 2328 | mHasShortcutHostPermission = mDeepShortcutManager.hasHostPermission(); |
| 2329 | if (mHasShortcutHostPermission) { |
| 2330 | for (UserHandleCompat user : mUserManager.getUserProfiles()) { |
| 2331 | if (mUserManager.isUserUnlocked(user)) { |
| 2332 | List<ShortcutInfoCompat> shortcuts = mDeepShortcutManager |
| 2333 | .queryForAllShortcuts(user); |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 2334 | sBgDataModel.updateDeepShortcutMap(null, user, shortcuts); |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 2335 | } |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2336 | } |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2337 | } |
| 2338 | synchronized (LoaderTask.this) { |
| 2339 | if (mStopped) { |
| 2340 | return; |
| 2341 | } |
| 2342 | mDeepShortcutsLoaded = true; |
| 2343 | } |
| 2344 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 2345 | bindDeepShortcuts(); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2348 | public void dumpState() { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 2349 | synchronized (sBgDataModel) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2350 | Log.d(TAG, "mLoaderTask.mContext=" + mContext); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2351 | Log.d(TAG, "mLoaderTask.mStopped=" + mStopped); |
| 2352 | Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished); |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 2353 | Log.d(TAG, "mItems size=" + sBgDataModel.workspaceItems.size()); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2354 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 2358 | public void bindDeepShortcuts() { |
Sunny Goyal | 8e0e1d7 | 2016-10-10 10:41:41 -0700 | [diff] [blame] | 2359 | final MultiHashMap<ComponentKey, String> shortcutMapCopy = |
| 2360 | sBgDataModel.deepShortcutMap.clone(); |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 2361 | Runnable r = new Runnable() { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2362 | @Override |
| 2363 | public void run() { |
| 2364 | Callbacks callbacks = getCallback(); |
| 2365 | if (callbacks != null) { |
| 2366 | callbacks.bindDeepShortcutMap(shortcutMapCopy); |
| 2367 | } |
| 2368 | } |
Tony Wickham | 80f5787 | 2016-06-29 18:12:15 -0700 | [diff] [blame] | 2369 | }; |
| 2370 | runOnMainThread(r); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 2373 | /** |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 2374 | * Refreshes the cached shortcuts if the shortcut permission has changed. |
| 2375 | * Current implementation simply reloads the workspace, but it can be optimized to |
| 2376 | * use partial updates similar to {@link UserManagerCompat} |
| 2377 | */ |
| 2378 | public void refreshShortcutsIfRequired() { |
Sunny Goyal | f5e3744 | 2016-11-02 10:31:24 -0700 | [diff] [blame] | 2379 | if (Utilities.ATLEAST_NOUGAT_MR1) { |
Sunny Goyal | 95f3d6b | 2016-08-10 16:09:29 -0700 | [diff] [blame] | 2380 | sWorker.removeCallbacks(mShortcutPermissionCheckRunnable); |
| 2381 | sWorker.post(mShortcutPermissionCheckRunnable); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | /** |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 2386 | * Called when the icons for packages have been updated in the icon cache. |
| 2387 | */ |
| 2388 | public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandleCompat user) { |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 2389 | // If any package icon has changed (app was updated while launcher was dead), |
| 2390 | // update the corresponding shortcuts. |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2391 | enqueueModelUpdateTask(new CacheDataUpdatedTask( |
| 2392 | CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages)); |
Sunny Goyal | 75b0f55 | 2015-05-20 21:57:06 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2395 | void enqueueModelUpdateTask(BaseModelUpdateTask task) { |
| 2396 | task.init(this); |
| 2397 | runOnWorkerThread(task); |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2400 | /** |
| 2401 | * A task to be executed on the current callbacks on the UI thread. |
| 2402 | * If there is no current callbacks, the task is ignored. |
| 2403 | */ |
| 2404 | public interface CallbackTask { |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2405 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2406 | void execute(Callbacks callbacks); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2409 | /** |
| 2410 | * A runnable which changes/updates the data model of the launcher based on certain events. |
| 2411 | */ |
| 2412 | public static abstract class BaseModelUpdateTask implements Runnable { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2413 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2414 | private LauncherModel mModel; |
| 2415 | private DeferredHandler mUiHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2416 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2417 | /* package private */ |
| 2418 | void init(LauncherModel model) { |
| 2419 | mModel = model; |
| 2420 | mUiHandler = mModel.mHandler; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2421 | } |
| 2422 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2423 | @Override |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2424 | public void run() { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2425 | if (!mModel.mHasLoaderCompletedOnce) { |
Sunny Goyal | c905efc | 2015-05-06 09:54:53 -0700 | [diff] [blame] | 2426 | // Loader has not yet run. |
| 2427 | return; |
| 2428 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2429 | execute(mModel.mApp, sBgDataModel, mModel.mBgAllAppsList); |
| 2430 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2431 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2432 | /** |
| 2433 | * Execute the actual task. Called on the worker thread. |
| 2434 | */ |
| 2435 | public abstract void execute( |
| 2436 | LauncherAppState app, BgDataModel dataModel, AllAppsList apps); |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2437 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2438 | /** |
| 2439 | * Schedules a {@param task} to be executed on the current callbacks. |
| 2440 | */ |
| 2441 | public final void scheduleCallbackTask(final CallbackTask task) { |
| 2442 | final Callbacks callbacks = mModel.getCallback(); |
| 2443 | mUiHandler.post(new Runnable() { |
| 2444 | public void run() { |
| 2445 | Callbacks cb = mModel.getCallback(); |
| 2446 | if (callbacks == cb && cb != null) { |
| 2447 | task.execute(callbacks); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2450 | }); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 2454 | /** |
| 2455 | * Repopulates the shortcut info, possibly updating any icon already on the workspace. |
| 2456 | */ |
| 2457 | public void updateShortcutInfo(final ShortcutInfoCompat fullDetail, final ShortcutInfo info) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2458 | enqueueModelUpdateTask(new ExtendedModelTask() { |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 2459 | @Override |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame^] | 2460 | public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) { |
| 2461 | info.updateFromDeepShortcutInfo(fullDetail, app.getContext()); |
| 2462 | |
| 2463 | ArrayList<ShortcutInfo> update = new ArrayList<>(); |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 2464 | update.add(info); |
| 2465 | bindUpdatedShortcuts(update, fullDetail.getUserHandle()); |
| 2466 | } |
| 2467 | }); |
| 2468 | } |
| 2469 | |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 2470 | private void bindWidgetsModel(final Callbacks callbacks) { |
| 2471 | final MultiHashMap<PackageItemInfo, WidgetItem> widgets |
| 2472 | = mBgWidgetsModel.getWidgetsMap().clone(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2473 | mHandler.post(new Runnable() { |
| 2474 | @Override |
| 2475 | public void run() { |
| 2476 | Callbacks cb = getCallback(); |
| 2477 | if (callbacks == cb && cb != null) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 2478 | callbacks.bindAllWidgets(widgets); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2479 | } |
Hyunyoung Song | d4af148 | 2015-04-20 20:40:03 -0700 | [diff] [blame] | 2480 | } |
| 2481 | }); |
| 2482 | } |
| 2483 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2484 | public void refreshAndBindWidgetsAndShortcuts( |
| 2485 | final Callbacks callbacks, final boolean bindFirst) { |
| 2486 | runOnWorkerThread(new Runnable() { |
| 2487 | @Override |
| 2488 | public void run() { |
| 2489 | if (bindFirst && !mBgWidgetsModel.isEmpty()) { |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 2490 | bindWidgetsModel(callbacks); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 2491 | } |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 2492 | ArrayList<WidgetItem> allWidgets = mBgWidgetsModel.update(mApp.getContext()); |
| 2493 | bindWidgetsModel(callbacks); |
| 2494 | |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2495 | // update the Widget entries inside DB on the worker thread. |
Sunny Goyal | d164b7f | 2016-10-12 20:49:31 -0700 | [diff] [blame] | 2496 | LauncherAppState.getInstance().getWidgetCache().removeObsoletePreviews(allWidgets); |
Sunny Goyal | 3186058 | 2015-09-18 08:38:57 -0700 | [diff] [blame] | 2497 | } |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 2498 | }); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 2499 | } |
| 2500 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2501 | /** |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2502 | * Make an ShortcutInfo object for a restored application or shortcut item that points |
| 2503 | * to a package that is not yet installed on the system. |
| 2504 | */ |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2505 | public ShortcutInfo getRestoredItemInfo(Cursor c, Intent intent, |
| 2506 | int promiseType, int itemType, CursorIconInfo iconInfo) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2507 | final ShortcutInfo info = new ShortcutInfo(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2508 | info.user = UserHandleCompat.myUserHandle(); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2509 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2510 | Bitmap icon = iconInfo.loadIcon(c, info); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2511 | // the fallback icon |
| 2512 | if (icon == null) { |
| 2513 | mIconCache.getTitleAndIcon(info, intent, info.user, false /* useLowResIcon */); |
| 2514 | } else { |
| 2515 | info.setIcon(icon); |
| 2516 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2517 | |
| 2518 | if ((promiseType & ShortcutInfo.FLAG_RESTORED_ICON) != 0) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2519 | String title = iconInfo.getTitle(c); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2520 | if (!TextUtils.isEmpty(title)) { |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 2521 | info.title = Utilities.trim(title); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2522 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2523 | } else if ((promiseType & ShortcutInfo.FLAG_AUTOINTALL_ICON) != 0) { |
| 2524 | if (TextUtils.isEmpty(info.title)) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2525 | info.title = iconInfo.getTitle(c); |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2526 | } |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2527 | } else { |
| 2528 | throw new InvalidParameterException("Invalid restoreType " + promiseType); |
| 2529 | } |
| 2530 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 2531 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Sunny Goyal | c22841b | 2015-07-13 19:59:50 -0700 | [diff] [blame] | 2532 | info.itemType = itemType; |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 2533 | info.promisedIntent = intent; |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2534 | info.status = promiseType; |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2535 | return info; |
| 2536 | } |
| 2537 | |
| 2538 | /** |
| 2539 | * Make an Intent object for a restored application or shortcut item that points |
| 2540 | * to the market page for the item. |
| 2541 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 2542 | @Thunk Intent getRestoredItemIntent(Cursor c, Context context, Intent intent) { |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2543 | ComponentName componentName = intent.getComponent(); |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 2544 | return getMarketIntent(componentName.getPackageName()); |
| 2545 | } |
| 2546 | |
| 2547 | static Intent getMarketIntent(String packageName) { |
| 2548 | return new Intent(Intent.ACTION_VIEW) |
| 2549 | .setData(new Uri.Builder() |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2550 | .scheme("market") |
| 2551 | .authority("details") |
Sunny Goyal | e7b8cd9 | 2014-08-27 14:04:33 -0700 | [diff] [blame] | 2552 | .appendQueryParameter("id", packageName) |
| 2553 | .build()); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2557 | * Make an ShortcutInfo object for a shortcut that is an application. |
| 2558 | * |
| 2559 | * If c is not null, then it will be used to fill in missing data like the title and icon. |
| 2560 | */ |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 2561 | public ShortcutInfo getAppShortcutInfo(Intent intent, |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2562 | UserHandleCompat user, Cursor c, CursorIconInfo iconInfo, |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 2563 | boolean allowMissingTarget, boolean useLowResIcon) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2564 | if (user == null) { |
| 2565 | Log.d(TAG, "Null user found in getShortcutInfo"); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2566 | return null; |
| 2567 | } |
| 2568 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2569 | ComponentName componentName = intent.getComponent(); |
| 2570 | if (componentName == null) { |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 2571 | Log.d(TAG, "Missing component found in getShortcutInfo"); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2572 | return null; |
| 2573 | } |
| 2574 | |
| 2575 | Intent newIntent = new Intent(intent.getAction(), null); |
| 2576 | newIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 2577 | newIntent.setComponent(componentName); |
| 2578 | LauncherActivityInfoCompat lai = mLauncherApps.resolveActivity(newIntent, user); |
Sunny Goyal | f599ccf | 2014-07-08 13:01:29 -0700 | [diff] [blame] | 2579 | if ((lai == null) && !allowMissingTarget) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2580 | Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName); |
| 2581 | return null; |
| 2582 | } |
| 2583 | |
| 2584 | final ShortcutInfo info = new ShortcutInfo(); |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 2585 | mIconCache.getTitleAndIcon(info, componentName, lai, user, false, useLowResIcon); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2586 | if (mIconCache.isDefaultIcon(info.getIcon(mIconCache), user) && c != null) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2587 | Bitmap icon = iconInfo.loadIcon(c); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2588 | info.setIcon(icon == null ? mIconCache.getDefaultIcon(user) : icon); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Sunny Goyal | d09c370 | 2016-04-06 16:18:20 -0700 | [diff] [blame] | 2591 | if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) { |
| 2592 | info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED; |
| 2593 | } |
| 2594 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2595 | // from the db |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2596 | if (TextUtils.isEmpty(info.title) && c != null) { |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2597 | info.title = iconInfo.getTitle(c); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2598 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2599 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2600 | // fall back to the class name of the activity |
| 2601 | if (info.title == null) { |
| 2602 | info.title = componentName.getClassName(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2603 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 2604 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2605 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2606 | info.user = user; |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 2607 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2608 | return info; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2609 | } |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 2610 | |
Sunny Goyal | 1a745e8 | 2014-10-02 15:58:31 -0700 | [diff] [blame] | 2611 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2612 | * Make an ShortcutInfo object for a shortcut that isn't an application. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2613 | */ |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2614 | @Thunk ShortcutInfo getShortcutInfo(Cursor c, CursorIconInfo iconInfo) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2615 | final ShortcutInfo info = new ShortcutInfo(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2616 | // Non-app shortcuts are only supported for current user. |
| 2617 | info.user = UserHandleCompat.myUserHandle(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2618 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2619 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 2620 | // TODO: If there's an explicit component and we can't install that, delete it. |
| 2621 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2622 | loadInfoFromCursor(info, c, iconInfo); |
| 2623 | return info; |
| 2624 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2625 | |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 2626 | /** |
| 2627 | * Make an ShortcutInfo object for a shortcut that isn't an application. |
| 2628 | */ |
| 2629 | public void loadInfoFromCursor(ShortcutInfo info, Cursor c, CursorIconInfo iconInfo) { |
| 2630 | info.title = iconInfo.getTitle(c); |
| 2631 | Bitmap icon = iconInfo.loadIcon(c, info); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 2632 | // the fallback icon |
| 2633 | if (icon == null) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2634 | icon = mIconCache.getDefaultIcon(info.user); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2635 | info.usingFallbackIcon = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2636 | } |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 2637 | info.setIcon(icon); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2638 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2639 | |
Sunny Goyal | 2350bc9 | 2014-10-14 16:42:54 -0700 | [diff] [blame] | 2640 | ShortcutInfo infoFromShortcutIntent(Context context, Intent data) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2641 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 2642 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 2643 | Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 2644 | |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 2645 | if (intent == null) { |
| 2646 | // If the intent is null, we can't construct a valid ShortcutInfo, so we return null |
| 2647 | Log.e(TAG, "Can't construct ShorcutInfo with null intent"); |
| 2648 | return null; |
| 2649 | } |
| 2650 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2651 | Bitmap icon = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2652 | ShortcutIconResource iconResource = null; |
| 2653 | |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 2654 | if (bitmap instanceof Bitmap) { |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 2655 | icon = LauncherIcons.createIconBitmap((Bitmap) bitmap, context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2656 | } else { |
| 2657 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 2658 | if (extra instanceof ShortcutIconResource) { |
| 2659 | iconResource = (ShortcutIconResource) extra; |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 2660 | icon = LauncherIcons.createIconBitmap(iconResource.packageName, |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 2661 | iconResource.resourceName, context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2665 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2666 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 2667 | // Only support intents for current user for now. Intents sent from other |
| 2668 | // users wouldn't get here without intent forwarding anyway. |
| 2669 | info.user = UserHandleCompat.myUserHandle(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2670 | if (icon == null) { |
Sunny Goyal | 2350bc9 | 2014-10-14 16:42:54 -0700 | [diff] [blame] | 2671 | icon = mIconCache.getDefaultIcon(info.user); |
| 2672 | info.usingFallbackIcon = true; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2673 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2674 | info.setIcon(icon); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2675 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 2676 | info.title = Utilities.trim(name); |
| 2677 | info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2678 | info.intent = intent; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2679 | info.iconResource = iconResource; |
| 2680 | |
| 2681 | return info; |
| 2682 | } |
| 2683 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 2684 | static boolean isValidProvider(AppWidgetProviderInfo provider) { |
| 2685 | return (provider != null) && (provider.provider != null) |
| 2686 | && (provider.provider.getPackageName() != null); |
| 2687 | } |
| 2688 | |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2689 | public void dumpState() { |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2690 | Log.d(TAG, "mCallbacks=" + mCallbacks); |
Michael Jurka | eadbfc5 | 2013-09-04 00:45:37 +0200 | [diff] [blame] | 2691 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data); |
| 2692 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added); |
| 2693 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed); |
| 2694 | AppInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2695 | if (mLoaderTask != null) { |
| 2696 | mLoaderTask.dumpState(); |
| 2697 | } else { |
| 2698 | Log.d(TAG, "mLoaderTask=null"); |
| 2699 | } |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2700 | } |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 2701 | |
| 2702 | public Callbacks getCallback() { |
| 2703 | return mCallbacks != null ? mCallbacks.get() : null; |
| 2704 | } |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2705 | |
| 2706 | /** |
| 2707 | * @return {@link FolderInfo} if its already loaded. |
| 2708 | */ |
| 2709 | public FolderInfo findFolderById(Long folderId) { |
Sunny Goyal | e9956a7 | 2016-09-01 17:24:47 -0700 | [diff] [blame] | 2710 | synchronized (sBgDataModel) { |
| 2711 | return sBgDataModel.folders.get(folderId); |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 2712 | } |
| 2713 | } |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2714 | |
Sunny Goyal | 527c7d3 | 2015-08-28 15:19:36 -0700 | [diff] [blame] | 2715 | @Thunk class DeferredMainThreadExecutor implements Executor { |
| 2716 | |
| 2717 | @Override |
| 2718 | public void execute(Runnable command) { |
| 2719 | runOnMainThread(command); |
| 2720 | } |
| 2721 | } |
| 2722 | |
Sunny Goyal | 756adbc | 2015-04-16 15:20:51 -0700 | [diff] [blame] | 2723 | /** |
| 2724 | * @return the looper for the worker thread which can be used to start background tasks. |
| 2725 | */ |
| 2726 | public static Looper getWorkerLooper() { |
| 2727 | return sWorkerThread.getLooper(); |
| 2728 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2729 | } |