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