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