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