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