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