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