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