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