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