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