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; |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 22 | import android.content.*; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 23 | import android.content.Intent.ShortcutIconResource; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.content.pm.ActivityInfo; |
Adam Cohen | 00fcb49 | 2011-11-02 21:53:47 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Adam Cohen | 00fcb49 | 2011-11-02 21:53:47 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.content.pm.ResolveInfo; |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 29 | import android.content.res.Configuration; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.content.res.Resources; |
| 31 | import android.database.Cursor; |
| 32 | import android.graphics.Bitmap; |
| 33 | import android.graphics.BitmapFactory; |
| 34 | import android.net.Uri; |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 35 | import android.os.Environment; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.HandlerThread; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 38 | import android.os.Parcelable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.os.Process; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 40 | import android.os.RemoteException; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 41 | import android.os.SystemClock; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 42 | import android.util.Log; |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 43 | import android.util.Pair; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 44 | import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 45 | |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 46 | import java.lang.ref.WeakReference; |
| 47 | import java.net.URISyntaxException; |
| 48 | import java.text.Collator; |
| 49 | import java.util.ArrayList; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 50 | import java.util.Arrays; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 51 | import java.util.Collections; |
| 52 | import java.util.Comparator; |
| 53 | import java.util.HashMap; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 54 | import java.util.HashSet; |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 55 | import java.util.Iterator; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 56 | import java.util.List; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 57 | import java.util.Set; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 58 | import java.util.TreeMap; |
Michael Jurka | c2f801e | 2011-07-12 14:19:46 -0700 | [diff] [blame] | 59 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Maintains in-memory state of the Launcher. It is expected that there should be only one |
| 62 | * 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] | 63 | * for the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 64 | */ |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 65 | public class LauncherModel extends BroadcastReceiver { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 66 | static final boolean DEBUG_LOADERS = false; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 67 | static final String TAG = "Launcher.Model"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 68 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 69 | private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 70 | private final boolean mAppsCanBeOnExternalStorage; |
Joe Onorato | d65d08e | 2010-04-20 15:43:37 -0400 | [diff] [blame] | 71 | private int mBatchSize; // 0 is all apps at once |
Daniel Sandler | 2ff10b3 | 2010-04-16 15:06:06 -0400 | [diff] [blame] | 72 | private int mAllAppsLoadDelay; // milliseconds between batches |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 73 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 74 | private final LauncherAppState mApp; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 75 | private final Object mLock = new Object(); |
| 76 | private DeferredHandler mHandler = new DeferredHandler(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 77 | private LoaderTask mLoaderTask; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 78 | private boolean mIsLoaderTaskRunning; |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 79 | private volatile boolean mFlushingWorkerThread; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 81 | // Specific runnable types that are run on the main thread deferred handler, this allows us to |
| 82 | // clear all queued binding runnables when the Launcher activity is destroyed. |
| 83 | private static final int MAIN_THREAD_NORMAL_RUNNABLE = 0; |
| 84 | private static final int MAIN_THREAD_BINDING_RUNNABLE = 1; |
| 85 | |
| 86 | |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 87 | private static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader"); |
| 88 | static { |
| 89 | sWorkerThread.start(); |
| 90 | } |
| 91 | private static final Handler sWorker = new Handler(sWorkerThread.getLooper()); |
| 92 | |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 93 | // We start off with everything not loaded. After that, we assume that |
| 94 | // our monitoring of the package manager provides all updates and we never |
| 95 | // need to do a requery. These are only ever touched from the loader thread. |
| 96 | private boolean mWorkspaceLoaded; |
| 97 | private boolean mAllAppsLoaded; |
| 98 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 99 | // When we are loading pages synchronously, we can't just post the binding of items on the side |
| 100 | // pages as this delays the rotation process. Instead, we wait for a callback from the first |
| 101 | // draw (in Workspace) to initiate the binding of the remaining side pages. Any time we start |
| 102 | // a normal load, we also clear this set of Runnables. |
| 103 | static final ArrayList<Runnable> mDeferredBindRunnables = new ArrayList<Runnable>(); |
| 104 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 105 | private WeakReference<Callbacks> mCallbacks; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 106 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 107 | // < only access in worker thread > |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 108 | private AllAppsList mBgAllAppsList; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 109 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 110 | // The lock that must be acquired before referencing any static bg data structures. Unlike |
| 111 | // other locks, this one can generally be held long-term because we never expect any of these |
| 112 | // static data structures to be referenced outside of the worker thread except on the first |
| 113 | // load after configuration change. |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 114 | static final Object sBgLock = new Object(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 115 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 116 | // sBgItemsIdMap maps *all* the ItemInfos (shortcuts, folders, and widgets) created by |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 117 | // LauncherModel to their ids |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 118 | static final HashMap<Long, ItemInfo> sBgItemsIdMap = new HashMap<Long, ItemInfo>(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 119 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 120 | // sBgWorkspaceItems is passed to bindItems, which expects a list of all folders and shortcuts |
| 121 | // created by LauncherModel that are directly on the home screen (however, no widgets or |
| 122 | // shortcuts within folders). |
| 123 | static final ArrayList<ItemInfo> sBgWorkspaceItems = new ArrayList<ItemInfo>(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 124 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 125 | // sBgAppWidgets is all LauncherAppWidgetInfo created by LauncherModel. Passed to bindAppWidget() |
| 126 | static final ArrayList<LauncherAppWidgetInfo> sBgAppWidgets = |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 127 | new ArrayList<LauncherAppWidgetInfo>(); |
| 128 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 129 | // sBgFolders is all FolderInfos created by LauncherModel. Passed to bindFolders() |
| 130 | static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>(); |
Winson Chung | b1094bd | 2011-08-24 16:14:08 -0700 | [diff] [blame] | 131 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 132 | // sBgDbIconCache is the set of ItemInfos that need to have their icons updated in the database |
| 133 | static final HashMap<Object, byte[]> sBgDbIconCache = new HashMap<Object, byte[]>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 134 | |
| 135 | // sBgWorkspaceScreens is the ordered set of workspace screens. |
| 136 | static final ArrayList<Long> sBgWorkspaceScreens = new ArrayList<Long>(); |
| 137 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 138 | // </ only access in worker thread > |
| 139 | |
| 140 | private IconCache mIconCache; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 141 | private Bitmap mDefaultIcon; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 142 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 143 | private static int mCellCountX; |
| 144 | private static int mCellCountY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 145 | |
Reena Lee | 99a73f3 | 2011-10-24 17:27:37 -0700 | [diff] [blame] | 146 | protected int mPreviousConfigMcc; |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 147 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 148 | public interface Callbacks { |
Joe Onorato | ef2efcf | 2010-10-27 13:21:00 -0700 | [diff] [blame] | 149 | public boolean setLoadOnResume(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 150 | public int getCurrentWorkspaceScreen(); |
| 151 | public void startBinding(); |
| 152 | public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 153 | public void bindScreens(ArrayList<Long> orderedScreenIds); |
Joe Onorato | ad72e17 | 2009-11-06 16:25:04 -0500 | [diff] [blame] | 154 | public void bindFolders(HashMap<Long,FolderInfo> folders); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 155 | public void finishBindingItems(boolean upgradePath); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 156 | public void bindAppWidget(LauncherAppWidgetInfo info); |
| 157 | public void bindAllApplications(ArrayList<ApplicationInfo> apps); |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 158 | public void bindAppsAdded(ArrayList<ApplicationInfo> apps); |
| 159 | public void bindAppsUpdated(ArrayList<ApplicationInfo> apps); |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 160 | public void bindComponentsRemoved(ArrayList<String> packageNames, |
| 161 | ArrayList<ApplicationInfo> appInfos, |
| 162 | boolean matchPackageNamesOnly); |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 163 | public void bindPackagesUpdated(ArrayList<Object> widgetsAndShortcuts); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 164 | public boolean isAllAppsVisible(); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 165 | public boolean isAllAppsButtonRank(int rank); |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 166 | public void bindSearchablesChanged(); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 167 | public void onPageBoundSynchronously(int page); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 168 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 169 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame^] | 170 | LauncherModel(LauncherAppState app, IconCache iconCache) { |
| 171 | final Context context = app.getContext(); |
| 172 | |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 173 | mAppsCanBeOnExternalStorage = !Environment.isExternalStorageEmulated(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame^] | 174 | mApp = app; |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 175 | mBgAllAppsList = new AllAppsList(iconCache); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 176 | mIconCache = iconCache; |
| 177 | |
| 178 | mDefaultIcon = Utilities.createIconBitmap( |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 179 | mIconCache.getFullResDefaultActivityIcon(), context); |
Daniel Sandler | 2ff10b3 | 2010-04-16 15:06:06 -0400 | [diff] [blame] | 180 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 181 | final Resources res = context.getResources(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 182 | mAllAppsLoadDelay = res.getInteger(R.integer.config_allAppsBatchLoadDelay); |
| 183 | mBatchSize = res.getInteger(R.integer.config_allAppsBatchSize); |
Reena Lee | 99a73f3 | 2011-10-24 17:27:37 -0700 | [diff] [blame] | 184 | Configuration config = res.getConfiguration(); |
| 185 | mPreviousConfigMcc = config.mcc; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 188 | /** Runs the specified runnable immediately if called from the main thread, otherwise it is |
| 189 | * posted on the main thread handler. */ |
| 190 | private void runOnMainThread(Runnable r) { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 191 | runOnMainThread(r, 0); |
| 192 | } |
| 193 | private void runOnMainThread(Runnable r, int type) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 194 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 195 | // If we are on the worker thread, post onto the main handler |
| 196 | mHandler.post(r); |
| 197 | } else { |
| 198 | r.run(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** Runs the specified runnable immediately if called from the worker thread, otherwise it is |
| 203 | * posted on the worker thread handler. */ |
| 204 | private static void runOnWorkerThread(Runnable r) { |
| 205 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 206 | r.run(); |
| 207 | } else { |
| 208 | // If we are not on the worker thread, then post to the worker handler |
| 209 | sWorker.post(r); |
| 210 | } |
| 211 | } |
| 212 | |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 213 | static boolean findNextAvailableIconSpaceInScreen(ArrayList<ItemInfo> items, int[] xy, |
| 214 | long screen) { |
| 215 | final int xCount = LauncherModel.getCellCountX(); |
| 216 | final int yCount = LauncherModel.getCellCountY(); |
| 217 | boolean[][] occupied = new boolean[xCount][yCount]; |
| 218 | |
| 219 | int cellX, cellY, spanX, spanY; |
| 220 | for (int i = 0; i < items.size(); ++i) { |
| 221 | final ItemInfo item = items.get(i); |
| 222 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 223 | if (item.screenId == screen) { |
| 224 | cellX = item.cellX; |
| 225 | cellY = item.cellY; |
| 226 | spanX = item.spanX; |
| 227 | spanY = item.spanY; |
| 228 | for (int x = cellX; 0 <= x && x < cellX + spanX && x < xCount; x++) { |
| 229 | for (int y = cellY; 0 <= y && y < cellY + spanY && y < yCount; y++) { |
| 230 | occupied[x][y] = true; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied); |
| 238 | } |
| 239 | static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name, |
| 240 | Intent launchIntent) { |
| 241 | // Lock on the app so that we don't try and get the items while apps are being added |
| 242 | LauncherAppState app = LauncherAppState.getInstance(); |
| 243 | LauncherModel model = app.getModel(); |
| 244 | boolean found = false; |
| 245 | synchronized (app) { |
| 246 | // Flush the LauncherModel worker thread, so that if we just did another |
| 247 | // processInstallShortcut, we give it time for its shortcut to get added to the |
| 248 | // database (getItemsInLocalCoordinates reads the database) |
| 249 | model.flushWorkerThread(); |
| 250 | final ArrayList<ItemInfo> items = LauncherModel.getItemsInLocalCoordinates(context); |
| 251 | final boolean shortcutExists = LauncherModel.shortcutExists(context, name, launchIntent); |
| 252 | |
| 253 | // Try adding to the workspace screens incrementally, starting at the default or center |
| 254 | // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1)) |
| 255 | for (int screen = 0; screen < sBgWorkspaceScreens.size() && !found; screen++) { |
| 256 | int[] tmpCoordinates = new int[2]; |
| 257 | if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates, |
| 258 | sBgWorkspaceScreens.get(screen))) { |
| 259 | // Update the Launcher db |
| 260 | return new Pair<Long, int[]>(sBgWorkspaceScreens.get(screen), tmpCoordinates); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | // XXX: Create a new page and add it to the first spot |
| 265 | return null; |
| 266 | } |
| 267 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 268 | public Bitmap getFallbackIcon() { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 269 | return Bitmap.createBitmap(mDefaultIcon); |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 270 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 271 | |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 272 | public void unbindItemInfosAndClearQueuedBindRunnables() { |
| 273 | if (sWorkerThread.getThreadId() == Process.myTid()) { |
| 274 | throw new RuntimeException("Expected unbindLauncherItemInfos() to be called from the " + |
| 275 | "main thread"); |
| 276 | } |
| 277 | |
| 278 | // Clear any deferred bind runnables |
| 279 | mDeferredBindRunnables.clear(); |
| 280 | // Remove any queued bind runnables |
| 281 | mHandler.cancelAllRunnablesOfType(MAIN_THREAD_BINDING_RUNNABLE); |
| 282 | // Unbind all the workspace items |
| 283 | unbindWorkspaceItemsOnMainThread(); |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 286 | /** Unbinds all the sBgWorkspaceItems and sBgAppWidgets on the main thread */ |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 287 | void unbindWorkspaceItemsOnMainThread() { |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 288 | // Ensure that we don't use the same workspace items data structure on the main thread |
| 289 | // by making a copy of workspace items first. |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 290 | final ArrayList<ItemInfo> tmpWorkspaceItems = new ArrayList<ItemInfo>(); |
| 291 | final ArrayList<ItemInfo> tmpAppWidgets = new ArrayList<ItemInfo>(); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 292 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 293 | tmpWorkspaceItems.addAll(sBgWorkspaceItems); |
| 294 | tmpAppWidgets.addAll(sBgAppWidgets); |
| 295 | } |
| 296 | Runnable r = new Runnable() { |
| 297 | @Override |
| 298 | public void run() { |
| 299 | for (ItemInfo item : tmpWorkspaceItems) { |
| 300 | item.unbind(); |
| 301 | } |
| 302 | for (ItemInfo item : tmpAppWidgets) { |
| 303 | item.unbind(); |
| 304 | } |
| 305 | } |
| 306 | }; |
| 307 | runOnMainThread(r); |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 310 | /** |
| 311 | * Adds an item to the DB if it was not created previously, or move it to a new |
| 312 | * <container, screen, cellX, cellY> |
| 313 | */ |
| 314 | static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 315 | long screenId, int cellX, int cellY) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 316 | if (item.container == ItemInfo.NO_ID) { |
| 317 | // From all apps |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 318 | addItemToDatabase(context, item, container, screenId, cellX, cellY, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 319 | } else { |
| 320 | // From somewhere else |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 321 | moveItemInDatabase(context, item, container, screenId, cellX, cellY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 325 | static void checkItemInfoLocked( |
| 326 | final long itemId, final ItemInfo item, StackTraceElement[] stackTrace) { |
| 327 | ItemInfo modelItem = sBgItemsIdMap.get(itemId); |
| 328 | if (modelItem != null && item != modelItem) { |
| 329 | // check all the data is consistent |
| 330 | if (modelItem instanceof ShortcutInfo && item instanceof ShortcutInfo) { |
| 331 | ShortcutInfo modelShortcut = (ShortcutInfo) modelItem; |
| 332 | ShortcutInfo shortcut = (ShortcutInfo) item; |
| 333 | if (modelShortcut.title.toString().equals(shortcut.title.toString()) && |
| 334 | modelShortcut.intent.filterEquals(shortcut.intent) && |
| 335 | modelShortcut.id == shortcut.id && |
| 336 | modelShortcut.itemType == shortcut.itemType && |
| 337 | modelShortcut.container == shortcut.container && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 338 | modelShortcut.screenId == shortcut.screenId && |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 339 | modelShortcut.cellX == shortcut.cellX && |
| 340 | modelShortcut.cellY == shortcut.cellY && |
| 341 | modelShortcut.spanX == shortcut.spanX && |
| 342 | modelShortcut.spanY == shortcut.spanY && |
| 343 | ((modelShortcut.dropPos == null && shortcut.dropPos == null) || |
| 344 | (modelShortcut.dropPos != null && |
| 345 | shortcut.dropPos != null && |
| 346 | modelShortcut.dropPos[0] == shortcut.dropPos[0] && |
| 347 | modelShortcut.dropPos[1] == shortcut.dropPos[1]))) { |
| 348 | // For all intents and purposes, this is the same object |
| 349 | return; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // the modelItem needs to match up perfectly with item if our model is |
| 354 | // to be consistent with the database-- for now, just require |
| 355 | // modelItem == item or the equality check above |
| 356 | String msg = "item: " + ((item != null) ? item.toString() : "null") + |
| 357 | "modelItem: " + |
| 358 | ((modelItem != null) ? modelItem.toString() : "null") + |
| 359 | "Error: ItemInfo passed to checkItemInfo doesn't match original"; |
| 360 | RuntimeException e = new RuntimeException(msg); |
| 361 | if (stackTrace != null) { |
| 362 | e.setStackTrace(stackTrace); |
| 363 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 364 | // TODO: something breaks this in the upgrade path |
| 365 | //throw e; |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 369 | static void checkItemInfo(final ItemInfo item) { |
| 370 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 371 | final long itemId = item.id; |
| 372 | Runnable r = new Runnable() { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 373 | public void run() { |
| 374 | synchronized (sBgLock) { |
| 375 | checkItemInfoLocked(itemId, item, stackTrace); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 376 | } |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 377 | } |
| 378 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 379 | runOnWorkerThread(r); |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 382 | static void updateItemInDatabaseHelper(Context context, final ContentValues values, |
| 383 | final ItemInfo item, final String callingFunction) { |
| 384 | final long itemId = item.id; |
| 385 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false); |
| 386 | final ContentResolver cr = context.getContentResolver(); |
| 387 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 388 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 389 | Runnable r = new Runnable() { |
| 390 | public void run() { |
| 391 | cr.update(uri, values, null, null); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 392 | updateItemArrays(item, itemId, stackTrace); |
| 393 | } |
| 394 | }; |
| 395 | runOnWorkerThread(r); |
| 396 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 397 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 398 | static void updateItemsInDatabaseHelper(Context context, final ArrayList<ContentValues> valuesList, |
| 399 | final ArrayList<ItemInfo> items, final String callingFunction) { |
| 400 | final ContentResolver cr = context.getContentResolver(); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 401 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 402 | final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); |
| 403 | Runnable r = new Runnable() { |
| 404 | public void run() { |
| 405 | ArrayList<ContentProviderOperation> ops = |
| 406 | new ArrayList<ContentProviderOperation>(); |
| 407 | int count = items.size(); |
| 408 | for (int i = 0; i < count; i++) { |
| 409 | ItemInfo item = items.get(i); |
| 410 | final long itemId = item.id; |
| 411 | final Uri uri = LauncherSettings.Favorites.getContentUri(itemId, false); |
| 412 | ContentValues values = valuesList.get(i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 413 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 414 | ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); |
| 415 | updateItemArrays(item, itemId, stackTrace); |
| 416 | |
| 417 | } |
| 418 | try { |
| 419 | cr.applyBatch(LauncherProvider.AUTHORITY, ops); |
| 420 | } catch (Exception e) { |
| 421 | e.printStackTrace(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 425 | runOnWorkerThread(r); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 426 | } |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 427 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 428 | static void updateItemArrays(ItemInfo item, long itemId, StackTraceElement[] stackTrace) { |
| 429 | // Lock on mBgLock *after* the db operation |
| 430 | synchronized (sBgLock) { |
| 431 | checkItemInfoLocked(itemId, item, stackTrace); |
| 432 | |
| 433 | if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 434 | item.container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 435 | // Item is in a folder, make sure this folder exists |
| 436 | if (!sBgFolders.containsKey(item.container)) { |
| 437 | // An items container is being set to a that of an item which is not in |
| 438 | // the list of Folders. |
| 439 | String msg = "item: " + item + " container being set to: " + |
| 440 | item.container + ", not in the list of folders"; |
| 441 | Log.e(TAG, msg); |
| 442 | Launcher.dumpDebugLogsToConsole(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Items are added/removed from the corresponding FolderInfo elsewhere, such |
| 447 | // as in Workspace.onDrop. Here, we just add/remove them from the list of items |
| 448 | // that are on the desktop, as appropriate |
| 449 | ItemInfo modelItem = sBgItemsIdMap.get(itemId); |
| 450 | if (modelItem.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || |
| 451 | modelItem.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 452 | switch (modelItem.itemType) { |
| 453 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 454 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 455 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 456 | if (!sBgWorkspaceItems.contains(modelItem)) { |
| 457 | sBgWorkspaceItems.add(modelItem); |
| 458 | } |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | } else { |
| 464 | sBgWorkspaceItems.remove(modelItem); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 469 | public void flushWorkerThread() { |
| 470 | mFlushingWorkerThread = true; |
| 471 | Runnable waiter = new Runnable() { |
| 472 | public void run() { |
| 473 | synchronized (this) { |
| 474 | notifyAll(); |
| 475 | mFlushingWorkerThread = false; |
| 476 | } |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | synchronized(waiter) { |
| 481 | runOnWorkerThread(waiter); |
| 482 | if (mLoaderTask != null) { |
| 483 | synchronized(mLoaderTask) { |
| 484 | mLoaderTask.notify(); |
| 485 | } |
| 486 | } |
| 487 | boolean success = false; |
| 488 | while (!success) { |
| 489 | try { |
| 490 | waiter.wait(); |
| 491 | success = true; |
| 492 | } catch (InterruptedException e) { |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 498 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 499 | * 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] | 500 | */ |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 501 | static void moveItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 502 | final long screenId, final int cellX, final int cellY) { |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 503 | String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id + |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 504 | " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY + |
| 505 | ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")"; |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 506 | Launcher.sDumpLogs.add(transaction); |
| 507 | Log.d(TAG, transaction); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 508 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 509 | item.cellX = cellX; |
| 510 | item.cellY = cellY; |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 511 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 512 | // We store hotseat items in canonical form which is this orientation invariant position |
| 513 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 514 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 515 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 516 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 517 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 518 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 519 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 520 | |
| 521 | final ContentValues values = new ContentValues(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 522 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 523 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 524 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 525 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 526 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 527 | updateItemInDatabaseHelper(context, values, item, "moveItemInDatabase"); |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 531 | * Move items in the DB to a new <container, screen, cellX, cellY>. We assume that the |
| 532 | * cellX, cellY have already been updated on the ItemInfos. |
| 533 | */ |
| 534 | static void moveItemsInDatabase(Context context, final ArrayList<ItemInfo> items, |
| 535 | final long container, final int screen) { |
| 536 | |
| 537 | ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>(); |
| 538 | int count = items.size(); |
| 539 | |
| 540 | for (int i = 0; i < count; i++) { |
| 541 | ItemInfo item = items.get(i); |
| 542 | String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 543 | + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 544 | + ", " + item.cellY + ") --> " + "(" + container + ", " + screen + ", " |
| 545 | + item.cellX + ", " + item.cellY + ")"; |
| 546 | Launcher.sDumpLogs.add(transaction); |
| 547 | item.container = container; |
| 548 | |
| 549 | // We store hotseat items in canonical form which is this orientation invariant position |
| 550 | // in the hotseat |
| 551 | if (context instanceof Launcher && screen < 0 && |
| 552 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 553 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(item.cellX, |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 554 | item.cellY); |
| 555 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 556 | item.screenId = screen; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | final ContentValues values = new ContentValues(); |
| 560 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
| 561 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 562 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 563 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 564 | |
| 565 | contentValues.add(values); |
| 566 | } |
| 567 | updateItemsInDatabaseHelper(context, contentValues, items, "moveItemInDatabase"); |
| 568 | } |
| 569 | |
| 570 | /** |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 571 | * 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] | 572 | */ |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 573 | static void modifyItemInDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 574 | final long screenId, final int cellX, final int cellY, final int spanX, final int spanY) { |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 575 | String transaction = "DbDebug Modify item (" + item.title + ") in db, id: " + item.id + |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 576 | " (" + item.container + ", " + item.screenId + ", " + item.cellX + ", " + item.cellY + |
| 577 | ") --> " + "(" + container + ", " + screenId + ", " + cellX + ", " + cellY + ")"; |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 578 | Launcher.sDumpLogs.add(transaction); |
| 579 | Log.d(TAG, transaction); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 580 | item.cellX = cellX; |
| 581 | item.cellY = cellY; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 582 | item.spanX = spanX; |
| 583 | item.spanY = spanY; |
| 584 | |
| 585 | // We store hotseat items in canonical form which is this orientation invariant position |
| 586 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 587 | if (context instanceof Launcher && screenId < 0 && |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 588 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 589 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 590 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 591 | item.screenId = screenId; |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 592 | } |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 593 | |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 594 | final ContentValues values = new ContentValues(); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 595 | values.put(LauncherSettings.Favorites.CONTAINER, item.container); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 596 | values.put(LauncherSettings.Favorites.CELLX, item.cellX); |
| 597 | values.put(LauncherSettings.Favorites.CELLY, item.cellY); |
| 598 | values.put(LauncherSettings.Favorites.SPANX, item.spanX); |
| 599 | values.put(LauncherSettings.Favorites.SPANY, item.spanY); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 600 | values.put(LauncherSettings.Favorites.SCREEN, item.screenId); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 601 | |
Michael Jurka | 816474f | 2012-06-25 14:49:02 -0700 | [diff] [blame] | 602 | updateItemInDatabaseHelper(context, values, item, "modifyItemInDatabase"); |
Adam Cohen | bebf042 | 2012-04-11 18:06:28 -0700 | [diff] [blame] | 603 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 604 | |
| 605 | /** |
| 606 | * Update an item to the database in a specified container. |
| 607 | */ |
| 608 | static void updateItemInDatabase(Context context, final ItemInfo item) { |
| 609 | final ContentValues values = new ContentValues(); |
| 610 | item.onAddToDatabase(values); |
| 611 | item.updateValuesWithCoordinates(values, item.cellX, item.cellY); |
| 612 | updateItemInDatabaseHelper(context, values, item, "updateItemInDatabase"); |
Adam Cohen | d4844c3 | 2011-02-18 19:25:06 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 616 | * Returns true if the shortcuts already exists in the database. |
| 617 | * we identify a shortcut by its title and intent. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 618 | */ |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 619 | static boolean shortcutExists(Context context, String title, Intent intent) { |
| 620 | final ContentResolver cr = context.getContentResolver(); |
| 621 | Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, |
| 622 | new String[] { "title", "intent" }, "title=? and intent=?", |
| 623 | new String[] { title, intent.toUri(0) }, null); |
| 624 | boolean result = false; |
| 625 | try { |
| 626 | result = c.moveToFirst(); |
| 627 | } finally { |
| 628 | c.close(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 629 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 630 | return result; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 633 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 634 | * Returns an ItemInfo array containing all the items in the LauncherModel. |
| 635 | * The ItemInfo.id is not set through this function. |
| 636 | */ |
| 637 | static ArrayList<ItemInfo> getItemsInLocalCoordinates(Context context) { |
| 638 | ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); |
| 639 | final ContentResolver cr = context.getContentResolver(); |
| 640 | Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, new String[] { |
| 641 | LauncherSettings.Favorites.ITEM_TYPE, LauncherSettings.Favorites.CONTAINER, |
| 642 | LauncherSettings.Favorites.SCREEN, LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY, |
| 643 | LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, null, null, null); |
| 644 | |
| 645 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 646 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 647 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 648 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 649 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 650 | final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX); |
| 651 | final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY); |
| 652 | |
| 653 | try { |
| 654 | while (c.moveToNext()) { |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 655 | ItemInfo item = new ItemInfo(); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 656 | item.cellX = c.getInt(cellXIndex); |
| 657 | item.cellY = c.getInt(cellYIndex); |
| 658 | item.spanX = c.getInt(spanXIndex); |
| 659 | item.spanY = c.getInt(spanYIndex); |
| 660 | item.container = c.getInt(containerIndex); |
| 661 | item.itemType = c.getInt(itemTypeIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 662 | item.screenId = c.getInt(screenIndex); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 663 | |
| 664 | items.add(item); |
| 665 | } |
| 666 | } catch (Exception e) { |
| 667 | items.clear(); |
| 668 | } finally { |
| 669 | c.close(); |
| 670 | } |
| 671 | |
| 672 | return items; |
| 673 | } |
| 674 | |
| 675 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 676 | * Find a folder in the db, creating the FolderInfo if necessary, and adding it to folderList. |
| 677 | */ |
| 678 | FolderInfo getFolderById(Context context, HashMap<Long,FolderInfo> folderList, long id) { |
| 679 | final ContentResolver cr = context.getContentResolver(); |
| 680 | Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null, |
| 681 | "_id=? and (itemType=? or itemType=?)", |
| 682 | new String[] { String.valueOf(id), |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 683 | String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}, null); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 684 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 685 | try { |
| 686 | if (c.moveToFirst()) { |
| 687 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 688 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 689 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 690 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 691 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 692 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 693 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 694 | FolderInfo folderInfo = null; |
| 695 | switch (c.getInt(itemTypeIndex)) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 696 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 697 | folderInfo = findOrMakeFolder(folderList, id); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 698 | break; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 701 | folderInfo.title = c.getString(titleIndex); |
| 702 | folderInfo.id = id; |
| 703 | folderInfo.container = c.getInt(containerIndex); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 704 | folderInfo.screenId = c.getInt(screenIndex); |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 705 | folderInfo.cellX = c.getInt(cellXIndex); |
| 706 | folderInfo.cellY = c.getInt(cellYIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 707 | |
| 708 | return folderInfo; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 709 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 710 | } finally { |
| 711 | c.close(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return null; |
| 715 | } |
| 716 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 717 | /** |
| 718 | * Add an item to the database in a specified container. Sets the container, screen, cellX and |
| 719 | * cellY fields of the item. Also assigns an ID to the item. |
| 720 | */ |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 721 | static void addItemToDatabase(Context context, final ItemInfo item, final long container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 722 | final long screenId, final int cellX, final int cellY, final boolean notify) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 723 | item.container = container; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 724 | item.cellX = cellX; |
| 725 | item.cellY = cellY; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 726 | // We store hotseat items in canonical form which is this orientation invariant position |
| 727 | // in the hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 728 | if (context instanceof Launcher && screenId < 0 && |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 729 | container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 730 | item.screenId = ((Launcher) context).getHotseat().getOrderInHotseat(cellX, cellY); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 731 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 732 | item.screenId = screenId; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 733 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 734 | |
| 735 | final ContentValues values = new ContentValues(); |
| 736 | final ContentResolver cr = context.getContentResolver(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 737 | item.onAddToDatabase(values); |
| 738 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 739 | LauncherAppState app = LauncherAppState.getInstance(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 740 | item.id = app.getLauncherProvider().generateNewItemId(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 741 | values.put(LauncherSettings.Favorites._ID, item.id); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 742 | item.updateValuesWithCoordinates(values, item.cellX, item.cellY); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 743 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 744 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 745 | public void run() { |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 746 | String transaction = "DbDebug Add item (" + item.title + ") to db, id: " |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 747 | + item.id + " (" + container + ", " + screenId + ", " + cellX + ", " |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 748 | + cellY + ")"; |
| 749 | Launcher.sDumpLogs.add(transaction); |
| 750 | Log.d(TAG, transaction); |
| 751 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 752 | cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI : |
| 753 | LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 754 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 755 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 756 | synchronized (sBgLock) { |
Michael Jurka | b2ae8ac | 2012-09-21 12:06:06 -0700 | [diff] [blame] | 757 | checkItemInfoLocked(item.id, item, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 758 | sBgItemsIdMap.put(item.id, item); |
| 759 | switch (item.itemType) { |
| 760 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 761 | sBgFolders.put(item.id, (FolderInfo) item); |
| 762 | // Fall through |
| 763 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 764 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 765 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP || |
| 766 | item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 767 | sBgWorkspaceItems.add(item); |
| 768 | } else { |
| 769 | if (!sBgFolders.containsKey(item.container)) { |
| 770 | // Adding an item to a folder that doesn't exist. |
| 771 | String msg = "adding item: " + item + " to a folder that " + |
| 772 | " doesn't exist"; |
Adam Cohen | 28b3e10 | 2012-10-04 17:21:33 -0700 | [diff] [blame] | 773 | Log.e(TAG, msg); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 774 | Launcher.dumpDebugLogsToConsole(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 775 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 776 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 777 | break; |
| 778 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 779 | sBgAppWidgets.add((LauncherAppWidgetInfo) item); |
| 780 | break; |
| 781 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 784 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 785 | runOnWorkerThread(r); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 788 | /** |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 789 | * Creates a new unique child id, for a given cell span across all layouts. |
| 790 | */ |
Michael Jurka | 845ba3b | 2010-09-28 17:09:46 -0700 | [diff] [blame] | 791 | static int getCellLayoutChildId( |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 792 | long container, long screen, int localCellX, int localCellY, int spanX, int spanY) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 793 | return (((int) container & 0xFF) << 24) |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 794 | | ((int) screen & 0xFF) << 16 | (localCellX & 0xFF) << 8 | (localCellY & 0xFF); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 797 | static int getCellCountX() { |
| 798 | return mCellCountX; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 801 | static int getCellCountY() { |
| 802 | return mCellCountY; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Updates the model orientation helper to take into account the current layout dimensions |
| 807 | * when performing local/canonical coordinate transformations. |
| 808 | */ |
| 809 | static void updateWorkspaceLayoutCells(int shortAxisCellCount, int longAxisCellCount) { |
Adam Cohen | d22015c | 2010-07-26 22:02:18 -0700 | [diff] [blame] | 810 | mCellCountX = shortAxisCellCount; |
| 811 | mCellCountY = longAxisCellCount; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 815 | * Removes the specified item from the database |
| 816 | * @param context |
| 817 | * @param item |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 818 | */ |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 819 | static void deleteItemFromDatabase(Context context, final ItemInfo item) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 820 | final ContentResolver cr = context.getContentResolver(); |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 821 | final Uri uriToDelete = LauncherSettings.Favorites.getContentUri(item.id, false); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 822 | |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 823 | Runnable r = new Runnable() { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 824 | public void run() { |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 825 | String transaction = "DbDebug Delete item (" + item.title + ") from db, id: " |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 826 | + item.id + " (" + item.container + ", " + item.screenId + ", " + item.cellX + |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 827 | ", " + item.cellY + ")"; |
| 828 | Launcher.sDumpLogs.add(transaction); |
| 829 | Log.d(TAG, transaction); |
| 830 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 831 | cr.delete(uriToDelete, null, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 832 | |
| 833 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 834 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 835 | switch (item.itemType) { |
| 836 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 837 | sBgFolders.remove(item.id); |
| 838 | for (ItemInfo info: sBgItemsIdMap.values()) { |
| 839 | if (info.container == item.id) { |
| 840 | // We are deleting a folder which still contains items that |
| 841 | // think they are contained by that folder. |
| 842 | String msg = "deleting a folder (" + item + ") which still " + |
| 843 | "contains items (" + info + ")"; |
Adam Cohen | 28b3e10 | 2012-10-04 17:21:33 -0700 | [diff] [blame] | 844 | Log.e(TAG, msg); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 845 | Launcher.dumpDebugLogsToConsole(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 846 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 847 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 848 | sBgWorkspaceItems.remove(item); |
| 849 | break; |
| 850 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 851 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 852 | sBgWorkspaceItems.remove(item); |
| 853 | break; |
| 854 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 855 | sBgAppWidgets.remove((LauncherAppWidgetInfo) item); |
| 856 | break; |
| 857 | } |
| 858 | sBgItemsIdMap.remove(item.id); |
| 859 | sBgDbIconCache.remove(item); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 860 | } |
| 861 | } |
Michael Jurka | 83df188 | 2011-08-31 20:59:26 -0700 | [diff] [blame] | 862 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 863 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 867 | * Update the order of the workspace screens in the database. The array list contains |
| 868 | * a list of screen ids in the order that they should appear. |
| 869 | */ |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 870 | void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens) { |
| 871 | updateWorkspaceScreenOrder(context, screens, null); |
| 872 | } |
| 873 | void updateWorkspaceScreenOrder(Context context, final ArrayList<Long> screens, final Runnable mainThreadCb) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 874 | final ContentResolver cr = context.getContentResolver(); |
| 875 | final Uri uri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 876 | |
| 877 | // Remove any negative screen ids -- these aren't persisted |
| 878 | Iterator<Long> iter = screens.iterator(); |
| 879 | while (iter.hasNext()) { |
| 880 | long id = iter.next(); |
| 881 | if (id < 0) { |
| 882 | iter.remove(); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | Runnable r = new Runnable() { |
| 887 | @Override |
| 888 | public void run() { |
| 889 | final ArrayList<Long> screensCopy = new ArrayList<Long>(); |
| 890 | |
| 891 | // Clear the table |
| 892 | cr.delete(uri, null, null); |
| 893 | int count = screens.size(); |
| 894 | ContentValues[] values = new ContentValues[count]; |
| 895 | for (int i = 0; i < count; i++) { |
| 896 | ContentValues v = new ContentValues(); |
| 897 | long screenId = screens.get(i); |
| 898 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 899 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
| 900 | screensCopy.add(screenId); |
| 901 | values[i] = v; |
| 902 | } |
| 903 | cr.bulkInsert(uri, values); |
| 904 | sBgWorkspaceScreens.clear(); |
| 905 | sBgWorkspaceScreens.addAll(screensCopy); |
| 906 | } |
| 907 | }; |
| 908 | runOnWorkerThread(r); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 909 | if (mainThreadCb != null) { |
| 910 | runOnWorkerThread(new Runnable() { |
| 911 | @Override |
| 912 | public void run() { |
| 913 | runOnMainThread(mainThreadCb); |
| 914 | } |
| 915 | }); |
| 916 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 920 | * Remove the contents of the specified folder from the database |
| 921 | */ |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 922 | static void deleteFolderContentsFromDatabase(Context context, final FolderInfo info) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 923 | final ContentResolver cr = context.getContentResolver(); |
| 924 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 925 | Runnable r = new Runnable() { |
| 926 | public void run() { |
| 927 | cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 928 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 929 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 930 | sBgItemsIdMap.remove(info.id); |
| 931 | sBgFolders.remove(info.id); |
| 932 | sBgDbIconCache.remove(info); |
| 933 | sBgWorkspaceItems.remove(info); |
| 934 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 935 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 936 | cr.delete(LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, |
| 937 | LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 938 | // Lock on mBgLock *after* the db operation |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 939 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 940 | for (ItemInfo childInfo : info.contents) { |
| 941 | sBgItemsIdMap.remove(childInfo.id); |
| 942 | sBgDbIconCache.remove(childInfo); |
| 943 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 944 | } |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 945 | } |
| 946 | }; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 947 | runOnWorkerThread(r); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Set this as the current Launcher activity object for the loader. |
| 952 | */ |
| 953 | public void initialize(Callbacks callbacks) { |
| 954 | synchronized (mLock) { |
| 955 | mCallbacks = new WeakReference<Callbacks>(callbacks); |
| 956 | } |
| 957 | } |
| 958 | |
Joe Onorato | 1d8e7bb | 2009-10-15 19:49:43 -0700 | [diff] [blame] | 959 | /** |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 960 | * Call from the handler for ACTION_PACKAGE_ADDED, ACTION_PACKAGE_REMOVED and |
| 961 | * ACTION_PACKAGE_CHANGED. |
| 962 | */ |
Narayan Kamath | cb1a477 | 2011-06-28 13:46:59 +0100 | [diff] [blame] | 963 | @Override |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 964 | public void onReceive(Context context, Intent intent) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 965 | if (DEBUG_LOADERS) Log.d(TAG, "onReceive intent=" + intent); |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 966 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 967 | final String action = intent.getAction(); |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 968 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 969 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action) |
| 970 | || Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 971 | || Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 972 | final String packageName = intent.getData().getSchemeSpecificPart(); |
| 973 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 974 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 975 | int op = PackageUpdatedTask.OP_NONE; |
| 976 | |
| 977 | if (packageName == null || packageName.length() == 0) { |
| 978 | // they sent us a bad intent |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
| 983 | op = PackageUpdatedTask.OP_UPDATE; |
| 984 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
| 985 | if (!replacing) { |
| 986 | op = PackageUpdatedTask.OP_REMOVE; |
| 987 | } |
| 988 | // else, we are replacing the package, so a PACKAGE_ADDED will be sent |
| 989 | // later, we will update the package at this time |
| 990 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 991 | if (!replacing) { |
| 992 | op = PackageUpdatedTask.OP_ADD; |
| 993 | } else { |
| 994 | op = PackageUpdatedTask.OP_UPDATE; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | if (op != PackageUpdatedTask.OP_NONE) { |
| 999 | enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName })); |
| 1000 | } |
| 1001 | |
| 1002 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
Joe Onorato | cec5833 | 2010-10-07 14:37:40 -0400 | [diff] [blame] | 1003 | // First, schedule to add these apps back in. |
| 1004 | String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1005 | enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packages)); |
| 1006 | // Then, rebind everything. |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1007 | startLoaderFromBackground(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1008 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 1009 | String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1010 | enqueuePackageUpdated(new PackageUpdatedTask( |
| 1011 | PackageUpdatedTask.OP_UNAVAILABLE, packages)); |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1012 | } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1013 | // If we have changed locale we need to clear out the labels in all apps/workspace. |
| 1014 | forceReload(); |
| 1015 | } else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) { |
| 1016 | // Check if configuration change was an mcc/mnc change which would affect app resources |
| 1017 | // and we would need to clear out the labels in all apps/workspace. Same handling as |
| 1018 | // above for ACTION_LOCALE_CHANGED |
| 1019 | Configuration currentConfig = context.getResources().getConfiguration(); |
Reena Lee | 99a73f3 | 2011-10-24 17:27:37 -0700 | [diff] [blame] | 1020 | if (mPreviousConfigMcc != currentConfig.mcc) { |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1021 | Log.d(TAG, "Reload apps on config change. curr_mcc:" |
Reena Lee | 99a73f3 | 2011-10-24 17:27:37 -0700 | [diff] [blame] | 1022 | + currentConfig.mcc + " prevmcc:" + mPreviousConfigMcc); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1023 | forceReload(); |
| 1024 | } |
| 1025 | // Update previousConfig |
Reena Lee | 99a73f3 | 2011-10-24 17:27:37 -0700 | [diff] [blame] | 1026 | mPreviousConfigMcc = currentConfig.mcc; |
Winson Chung | cbf7c4d | 2011-08-23 11:58:54 -0700 | [diff] [blame] | 1027 | } else if (SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED.equals(action) || |
| 1028 | SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED.equals(action)) { |
Michael Jurka | ec9788e | 2011-08-29 11:24:45 -0700 | [diff] [blame] | 1029 | if (mCallbacks != null) { |
| 1030 | Callbacks callbacks = mCallbacks.get(); |
| 1031 | if (callbacks != null) { |
| 1032 | callbacks.bindSearchablesChanged(); |
| 1033 | } |
Winson Chung | cfdf7ee | 2011-08-25 11:38:34 -0700 | [diff] [blame] | 1034 | } |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1038 | private void forceReload() { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1039 | resetLoadedState(true, true); |
| 1040 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1041 | // Do this here because if the launcher activity is running it will be restarted. |
| 1042 | // If it's not running startLoaderFromBackground will merely tell it that it needs |
| 1043 | // to reload. |
| 1044 | startLoaderFromBackground(); |
| 1045 | } |
| 1046 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 1047 | public void resetLoadedState(boolean resetAllAppsLoaded, boolean resetWorkspaceLoaded) { |
| 1048 | synchronized (mLock) { |
| 1049 | // Stop any existing loaders first, so they don't set mAllAppsLoaded or |
| 1050 | // mWorkspaceLoaded to true later |
| 1051 | stopLoaderLocked(); |
| 1052 | if (resetAllAppsLoaded) mAllAppsLoaded = false; |
| 1053 | if (resetWorkspaceLoaded) mWorkspaceLoaded = false; |
| 1054 | } |
| 1055 | } |
| 1056 | |
Joe Onorato | e9ad59e | 2010-10-29 17:35:36 -0700 | [diff] [blame] | 1057 | /** |
| 1058 | * When the launcher is in the background, it's possible for it to miss paired |
| 1059 | * configuration changes. So whenever we trigger the loader from the background |
| 1060 | * tell the launcher that it needs to re-run the loader when it comes back instead |
| 1061 | * of doing it now. |
| 1062 | */ |
| 1063 | public void startLoaderFromBackground() { |
| 1064 | boolean runLoader = false; |
| 1065 | if (mCallbacks != null) { |
| 1066 | Callbacks callbacks = mCallbacks.get(); |
| 1067 | if (callbacks != null) { |
| 1068 | // Only actually run the loader if they're not paused. |
| 1069 | if (!callbacks.setLoadOnResume()) { |
| 1070 | runLoader = true; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | if (runLoader) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1075 | startLoader(false, -1); |
Joe Onorato | 790c2d9 | 2010-06-11 00:14:11 -0700 | [diff] [blame] | 1076 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1077 | } |
Joe Onorato | f99f8c1 | 2009-10-31 17:27:36 -0400 | [diff] [blame] | 1078 | |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1079 | // If there is already a loader task running, tell it to stop. |
| 1080 | // returns true if isLaunching() was true on the old task |
| 1081 | private boolean stopLoaderLocked() { |
| 1082 | boolean isLaunching = false; |
| 1083 | LoaderTask oldTask = mLoaderTask; |
| 1084 | if (oldTask != null) { |
| 1085 | if (oldTask.isLaunching()) { |
| 1086 | isLaunching = true; |
| 1087 | } |
| 1088 | oldTask.stopLocked(); |
| 1089 | } |
| 1090 | return isLaunching; |
| 1091 | } |
| 1092 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1093 | public void startLoader(boolean isLaunching, int synchronousBindPage) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1094 | synchronized (mLock) { |
| 1095 | if (DEBUG_LOADERS) { |
| 1096 | Log.d(TAG, "startLoader isLaunching=" + isLaunching); |
| 1097 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1098 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1099 | // Clear any deferred bind-runnables from the synchronized load process |
| 1100 | // We must do this before any loading/binding is scheduled below. |
| 1101 | mDeferredBindRunnables.clear(); |
| 1102 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1103 | // Don't bother to start the thread if we know it's not going to do anything |
| 1104 | if (mCallbacks != null && mCallbacks.get() != null) { |
| 1105 | // If there is already one running, tell it to stop. |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1106 | // also, don't downgrade isLaunching if we're already running |
| 1107 | isLaunching = isLaunching || stopLoaderLocked(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1108 | mLoaderTask = new LoaderTask(mApp.getContext(), isLaunching); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1109 | if (synchronousBindPage > -1 && mAllAppsLoaded && mWorkspaceLoaded) { |
| 1110 | mLoaderTask.runBindSynchronousPage(synchronousBindPage); |
| 1111 | } else { |
| 1112 | sWorkerThread.setPriority(Thread.NORM_PRIORITY); |
| 1113 | sWorker.post(mLoaderTask); |
| 1114 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1115 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1119 | void bindRemainingSynchronousPages() { |
| 1120 | // Post the remaining side pages to be loaded |
| 1121 | if (!mDeferredBindRunnables.isEmpty()) { |
| 1122 | for (final Runnable r : mDeferredBindRunnables) { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 1123 | mHandler.post(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1124 | } |
| 1125 | mDeferredBindRunnables.clear(); |
| 1126 | } |
| 1127 | } |
| 1128 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1129 | public void stopLoader() { |
| 1130 | synchronized (mLock) { |
| 1131 | if (mLoaderTask != null) { |
| 1132 | mLoaderTask.stopLocked(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1133 | } |
| 1134 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1135 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1136 | |
Michael Jurka | c57b7a8 | 2011-08-09 22:02:20 -0700 | [diff] [blame] | 1137 | public boolean isAllAppsLoaded() { |
| 1138 | return mAllAppsLoaded; |
| 1139 | } |
| 1140 | |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1141 | boolean isLoadingWorkspace() { |
| 1142 | synchronized (mLock) { |
| 1143 | if (mLoaderTask != null) { |
| 1144 | return mLoaderTask.isLoadingWorkspace(); |
| 1145 | } |
| 1146 | } |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1150 | /** |
| 1151 | * Runnable for the thread that loads the contents of the launcher: |
| 1152 | * - workspace icons |
| 1153 | * - widgets |
| 1154 | * - all apps icons |
| 1155 | */ |
| 1156 | private class LoaderTask implements Runnable { |
| 1157 | private Context mContext; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1158 | private boolean mIsLaunching; |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1159 | private boolean mIsLoadingAndBindingWorkspace; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1160 | private boolean mStopped; |
| 1161 | private boolean mLoadAndBindStepFinished; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1162 | private boolean mIsUpgradePath; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1163 | |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 1164 | private HashMap<Object, CharSequence> mLabelCache; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1165 | |
| 1166 | LoaderTask(Context context, boolean isLaunching) { |
| 1167 | mContext = context; |
| 1168 | mIsLaunching = isLaunching; |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 1169 | mLabelCache = new HashMap<Object, CharSequence>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1172 | boolean isLaunching() { |
| 1173 | return mIsLaunching; |
| 1174 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1175 | |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1176 | boolean isLoadingWorkspace() { |
| 1177 | return mIsLoadingAndBindingWorkspace; |
| 1178 | } |
| 1179 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1180 | private void loadAndBindWorkspace() { |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 1181 | mIsLoadingAndBindingWorkspace = true; |
| 1182 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1183 | // Load the workspace |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1184 | if (DEBUG_LOADERS) { |
| 1185 | Log.d(TAG, "loadAndBindWorkspace mWorkspaceLoaded=" + mWorkspaceLoaded); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1186 | } |
Michael Jurka | 288a36b | 2011-07-12 16:53:48 -0700 | [diff] [blame] | 1187 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1188 | if (!mWorkspaceLoaded) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1189 | loadWorkspace(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 1190 | synchronized (LoaderTask.this) { |
| 1191 | if (mStopped) { |
| 1192 | return; |
| 1193 | } |
| 1194 | mWorkspaceLoaded = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1198 | // Bind the workspace |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1199 | bindWorkspace(-1); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1200 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1201 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1202 | private void waitForIdle() { |
| 1203 | // Wait until the either we're stopped or the other threads are done. |
| 1204 | // This way we don't start loading all apps until the workspace has settled |
| 1205 | // down. |
| 1206 | synchronized (LoaderTask.this) { |
| 1207 | final long workspaceWaitTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1208 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1209 | mHandler.postIdle(new Runnable() { |
| 1210 | public void run() { |
| 1211 | synchronized (LoaderTask.this) { |
| 1212 | mLoadAndBindStepFinished = true; |
| 1213 | if (DEBUG_LOADERS) { |
| 1214 | Log.d(TAG, "done with previous binding step"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1215 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1216 | LoaderTask.this.notify(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1217 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1218 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1219 | }); |
| 1220 | |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 1221 | while (!mStopped && !mLoadAndBindStepFinished && !mFlushingWorkerThread) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1222 | try { |
Michael Jurka | c7700af | 2013-05-14 20:17:58 +0200 | [diff] [blame] | 1223 | // Just in case mFlushingWorkerThread changes but we aren't woken up, |
| 1224 | // wait no longer than 1sec at a time |
| 1225 | this.wait(1000); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1226 | } catch (InterruptedException ex) { |
| 1227 | // Ignore |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1228 | } |
| 1229 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1230 | if (DEBUG_LOADERS) { |
| 1231 | Log.d(TAG, "waited " |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1232 | + (SystemClock.uptimeMillis()-workspaceWaitTime) |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1233 | + "ms for previous step to finish binding"); |
| 1234 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1235 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1236 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1237 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1238 | void runBindSynchronousPage(int synchronousBindPage) { |
| 1239 | if (synchronousBindPage < 0) { |
| 1240 | // Ensure that we have a valid page index to load synchronously |
| 1241 | throw new RuntimeException("Should not call runBindSynchronousPage() without " + |
| 1242 | "valid page index"); |
| 1243 | } |
| 1244 | if (!mAllAppsLoaded || !mWorkspaceLoaded) { |
| 1245 | // Ensure that we don't try and bind a specified page when the pages have not been |
| 1246 | // loaded already (we should load everything asynchronously in that case) |
| 1247 | throw new RuntimeException("Expecting AllApps and Workspace to be loaded"); |
| 1248 | } |
| 1249 | synchronized (mLock) { |
| 1250 | if (mIsLoaderTaskRunning) { |
| 1251 | // Ensure that we are never running the background loading at this point since |
| 1252 | // we also touch the background collections |
| 1253 | throw new RuntimeException("Error! Background loading is already running"); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // XXX: Throw an exception if we are already loading (since we touch the worker thread |
| 1258 | // data structures, we can't allow any other thread to touch that data, but because |
| 1259 | // this call is synchronous, we can get away with not locking). |
| 1260 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1261 | // The LauncherModel is static in the LauncherAppState and mHandler may have queued |
Adam Cohen | a13a2f2 | 2012-07-23 14:29:15 -0700 | [diff] [blame] | 1262 | // operations from the previous activity. We need to ensure that all queued operations |
| 1263 | // are executed before any synchronous binding work is done. |
| 1264 | mHandler.flush(); |
| 1265 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1266 | // Divide the set of loaded items into those that we are binding synchronously, and |
| 1267 | // everything else that is to be bound normally (asynchronously). |
| 1268 | bindWorkspace(synchronousBindPage); |
| 1269 | // XXX: For now, continue posting the binding of AllApps as there are other issues that |
| 1270 | // arise from that. |
| 1271 | onlyBindAllApps(); |
| 1272 | } |
| 1273 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1274 | public void run() { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1275 | synchronized (mLock) { |
| 1276 | mIsLoaderTaskRunning = true; |
| 1277 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1278 | // Optimize for end-user experience: if the Launcher is up and // running with the |
| 1279 | // All Apps interface in the foreground, load All Apps first. Otherwise, load the |
| 1280 | // workspace first (default). |
| 1281 | final Callbacks cbk = mCallbacks.get(); |
| 1282 | final boolean loadWorkspaceFirst = cbk != null ? (!cbk.isAllAppsVisible()) : true; |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1283 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1284 | keep_running: { |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1285 | // Elevate priority when Home launches for the first time to avoid |
| 1286 | // starving at boot time. Staring at a blank home is not cool. |
| 1287 | synchronized (mLock) { |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 1288 | if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to " + |
| 1289 | (mIsLaunching ? "DEFAULT" : "BACKGROUND")); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1290 | android.os.Process.setThreadPriority(mIsLaunching |
| 1291 | ? Process.THREAD_PRIORITY_DEFAULT : Process.THREAD_PRIORITY_BACKGROUND); |
| 1292 | } |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1293 | if (loadWorkspaceFirst) { |
| 1294 | if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace"); |
| 1295 | loadAndBindWorkspace(); |
| 1296 | } else { |
| 1297 | if (DEBUG_LOADERS) Log.d(TAG, "step 1: special: loading all apps"); |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1298 | loadAndBindAllApps(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1299 | } |
| 1300 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1301 | if (mStopped) { |
| 1302 | break keep_running; |
| 1303 | } |
| 1304 | |
| 1305 | // Whew! Hard work done. Slow us down, and wait until the UI thread has |
| 1306 | // settled down. |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1307 | synchronized (mLock) { |
| 1308 | if (mIsLaunching) { |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 1309 | if (DEBUG_LOADERS) Log.d(TAG, "Setting thread priority to BACKGROUND"); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1310 | android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 1311 | } |
| 1312 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1313 | waitForIdle(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1314 | |
| 1315 | // second step |
| 1316 | if (loadWorkspaceFirst) { |
| 1317 | if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps"); |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 1318 | loadAndBindAllApps(); |
Daniel Sandler | 843e860 | 2010-06-07 14:59:01 -0400 | [diff] [blame] | 1319 | } else { |
| 1320 | if (DEBUG_LOADERS) Log.d(TAG, "step 2: special: loading workspace"); |
| 1321 | loadAndBindWorkspace(); |
| 1322 | } |
Winson Chung | 7ed3774 | 2011-09-08 15:45:51 -0700 | [diff] [blame] | 1323 | |
| 1324 | // Restore the default thread priority after we are done loading items |
| 1325 | synchronized (mLock) { |
| 1326 | android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); |
| 1327 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1328 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1329 | |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 1330 | // Update the saved icons if necessary |
| 1331 | if (DEBUG_LOADERS) Log.d(TAG, "Comparing loaded icons to database icons"); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1332 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1333 | for (Object key : sBgDbIconCache.keySet()) { |
| 1334 | updateSavedIcon(mContext, (ShortcutInfo) key, sBgDbIconCache.get(key)); |
| 1335 | } |
| 1336 | sBgDbIconCache.clear(); |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 1337 | } |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 1338 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1339 | // Clear out this reference, otherwise we end up holding it until all of the |
| 1340 | // callback runnables are done. |
| 1341 | mContext = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1342 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1343 | synchronized (mLock) { |
| 1344 | // If we are still the last one to be scheduled, remove ourselves. |
| 1345 | if (mLoaderTask == this) { |
| 1346 | mLoaderTask = null; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1347 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1348 | mIsLoaderTaskRunning = false; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1349 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | public void stopLocked() { |
| 1353 | synchronized (LoaderTask.this) { |
| 1354 | mStopped = true; |
| 1355 | this.notify(); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Gets the callbacks object. If we've been stopped, or if the launcher object |
| 1361 | * has somehow been garbage collected, return null instead. Pass in the Callbacks |
| 1362 | * object that was around when the deferred message was scheduled, and if there's |
| 1363 | * a new Callbacks object around then also return null. This will save us from |
| 1364 | * calling onto it with data that will be ignored. |
| 1365 | */ |
| 1366 | Callbacks tryGetCallbacks(Callbacks oldCallbacks) { |
| 1367 | synchronized (mLock) { |
| 1368 | if (mStopped) { |
| 1369 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1370 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1371 | |
| 1372 | if (mCallbacks == null) { |
| 1373 | return null; |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1374 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1375 | |
| 1376 | final Callbacks callbacks = mCallbacks.get(); |
| 1377 | if (callbacks != oldCallbacks) { |
| 1378 | return null; |
| 1379 | } |
| 1380 | if (callbacks == null) { |
| 1381 | Log.w(TAG, "no mCallbacks"); |
| 1382 | return null; |
| 1383 | } |
| 1384 | |
| 1385 | return callbacks; |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | // check & update map of what's occupied; used to discard overlapping/invalid items |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1390 | private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item) { |
| 1391 | long containerIndex = item.screenId; |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1392 | if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1393 | if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) { |
| 1394 | if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT) |
| 1395 | [(int) item.screenId][0] != null) { |
| 1396 | Log.e(TAG, "Error loading shortcut into hotseat " + item |
| 1397 | + " into position (" + item.screenId + ":" + item.cellX + "," |
| 1398 | + item.cellY + ") occupied by " |
| 1399 | + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT) |
| 1400 | [(int) item.screenId][0]); |
| 1401 | return false; |
| 1402 | } |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1403 | } else { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1404 | ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1]; |
| 1405 | items[(int) item.screenId][0] = item; |
| 1406 | occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items); |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1409 | } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1410 | // Skip further checking if it is not the hotseat or workspace container |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1411 | return true; |
| 1412 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1413 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1414 | if (!occupied.containsKey(item.screenId)) { |
| 1415 | ItemInfo[][] items = new ItemInfo[mCellCountX + 1][mCellCountY + 1]; |
| 1416 | occupied.put(item.screenId, items); |
| 1417 | } |
| 1418 | |
| 1419 | ItemInfo[][] screens = occupied.get(item.screenId); |
Winson Chung | 6ba2a1b | 2011-09-02 16:22:11 -0700 | [diff] [blame] | 1420 | // Check if any workspace icons overlap with each other |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1421 | for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { |
| 1422 | for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1423 | if (screens[x][y] != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1424 | Log.e(TAG, "Error loading shortcut " + item |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1425 | + " into cell (" + containerIndex + "-" + item.screenId + ":" |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1426 | + x + "," + y |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 1427 | + ") occupied by " |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1428 | + screens[x][y]); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1429 | return false; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | for (int x = item.cellX; x < (item.cellX+item.spanX); x++) { |
| 1434 | for (int y = item.cellY; y < (item.cellY+item.spanY); y++) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1435 | screens[x][y] = item; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1436 | } |
| 1437 | } |
Winson Chung | f30ad5f | 2011-08-08 10:55:42 -0700 | [diff] [blame] | 1438 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1439 | return true; |
| 1440 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1441 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1442 | private void loadWorkspace() { |
| 1443 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1444 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1445 | final Context context = mContext; |
| 1446 | final ContentResolver contentResolver = context.getContentResolver(); |
| 1447 | final PackageManager manager = context.getPackageManager(); |
| 1448 | final AppWidgetManager widgets = AppWidgetManager.getInstance(context); |
| 1449 | final boolean isSafeMode = manager.isSafeMode(); |
Joe Onorato | 3c2f7e1 | 2009-10-31 19:17:31 -0400 | [diff] [blame] | 1450 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 1451 | // Make sure the default workspace is loaded, if needed |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1452 | boolean loadOldDb = mApp.getLauncherProvider().shouldLoadOldDb(); |
| 1453 | Uri contentUri = loadOldDb ? LauncherSettings.Favorites.OLD_CONTENT_URI : |
| 1454 | LauncherSettings.Favorites.CONTENT_URI; |
| 1455 | |
| 1456 | mIsUpgradePath = loadOldDb; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 1457 | |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1458 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1459 | sBgWorkspaceItems.clear(); |
| 1460 | sBgAppWidgets.clear(); |
| 1461 | sBgFolders.clear(); |
| 1462 | sBgItemsIdMap.clear(); |
| 1463 | sBgDbIconCache.clear(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1464 | sBgWorkspaceScreens.clear(); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1465 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1466 | final ArrayList<Long> itemsToRemove = new ArrayList<Long>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1467 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1468 | final Cursor c = contentResolver.query(contentUri, null, null, null, null); |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1469 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1470 | // +1 for the hotseat (it can be larger than the workspace) |
| 1471 | // Load workspace in reverse order to ensure that latest items are loaded first (and |
| 1472 | // before any earlier duplicates) |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1473 | final HashMap<Long, ItemInfo[][]> occupied = new HashMap<Long, ItemInfo[][]>(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1474 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1475 | try { |
| 1476 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
| 1477 | final int intentIndex = c.getColumnIndexOrThrow |
| 1478 | (LauncherSettings.Favorites.INTENT); |
| 1479 | final int titleIndex = c.getColumnIndexOrThrow |
| 1480 | (LauncherSettings.Favorites.TITLE); |
| 1481 | final int iconTypeIndex = c.getColumnIndexOrThrow( |
| 1482 | LauncherSettings.Favorites.ICON_TYPE); |
| 1483 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 1484 | final int iconPackageIndex = c.getColumnIndexOrThrow( |
| 1485 | LauncherSettings.Favorites.ICON_PACKAGE); |
| 1486 | final int iconResourceIndex = c.getColumnIndexOrThrow( |
| 1487 | LauncherSettings.Favorites.ICON_RESOURCE); |
| 1488 | final int containerIndex = c.getColumnIndexOrThrow( |
| 1489 | LauncherSettings.Favorites.CONTAINER); |
| 1490 | final int itemTypeIndex = c.getColumnIndexOrThrow( |
| 1491 | LauncherSettings.Favorites.ITEM_TYPE); |
| 1492 | final int appWidgetIdIndex = c.getColumnIndexOrThrow( |
| 1493 | LauncherSettings.Favorites.APPWIDGET_ID); |
| 1494 | final int screenIndex = c.getColumnIndexOrThrow( |
| 1495 | LauncherSettings.Favorites.SCREEN); |
| 1496 | final int cellXIndex = c.getColumnIndexOrThrow |
| 1497 | (LauncherSettings.Favorites.CELLX); |
| 1498 | final int cellYIndex = c.getColumnIndexOrThrow |
| 1499 | (LauncherSettings.Favorites.CELLY); |
| 1500 | final int spanXIndex = c.getColumnIndexOrThrow |
| 1501 | (LauncherSettings.Favorites.SPANX); |
| 1502 | final int spanYIndex = c.getColumnIndexOrThrow( |
| 1503 | LauncherSettings.Favorites.SPANY); |
| 1504 | //final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 1505 | //final int displayModeIndex = c.getColumnIndexOrThrow( |
| 1506 | // LauncherSettings.Favorites.DISPLAY_MODE); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1507 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1508 | ShortcutInfo info; |
| 1509 | String intentDescription; |
| 1510 | LauncherAppWidgetInfo appWidgetInfo; |
| 1511 | int container; |
| 1512 | long id; |
| 1513 | Intent intent; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1514 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1515 | while (!mStopped && c.moveToNext()) { |
| 1516 | try { |
| 1517 | int itemType = c.getInt(itemTypeIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1518 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1519 | switch (itemType) { |
| 1520 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
| 1521 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
| 1522 | intentDescription = c.getString(intentIndex); |
| 1523 | try { |
| 1524 | intent = Intent.parseUri(intentDescription, 0); |
| 1525 | } catch (URISyntaxException e) { |
| 1526 | continue; |
| 1527 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1528 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1529 | if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
| 1530 | info = getShortcutInfo(manager, intent, context, c, iconIndex, |
| 1531 | titleIndex, mLabelCache); |
| 1532 | } else { |
| 1533 | info = getShortcutInfo(c, context, iconTypeIndex, |
| 1534 | iconPackageIndex, iconResourceIndex, iconIndex, |
| 1535 | titleIndex); |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1536 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1537 | // App shortcuts that used to be automatically added to Launcher |
| 1538 | // didn't always have the correct intent flags set, so do that |
| 1539 | // here |
| 1540 | if (intent.getAction() != null && |
Michael Jurka | 9ad0056 | 2012-05-14 12:24:22 -0700 | [diff] [blame] | 1541 | intent.getCategories() != null && |
| 1542 | intent.getAction().equals(Intent.ACTION_MAIN) && |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1543 | intent.getCategories().contains(Intent.CATEGORY_LAUNCHER)) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1544 | intent.addFlags( |
| 1545 | Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1546 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1547 | } |
Michael Jurka | 9687956 | 2012-03-22 05:54:33 -0700 | [diff] [blame] | 1548 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1549 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1550 | if (info != null) { |
| 1551 | info.intent = intent; |
| 1552 | info.id = c.getLong(idIndex); |
| 1553 | container = c.getInt(containerIndex); |
| 1554 | info.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1555 | info.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1556 | info.cellX = c.getInt(cellXIndex); |
| 1557 | info.cellY = c.getInt(cellYIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1558 | // check & update map of what's occupied |
| 1559 | if (!checkItemPlacement(occupied, info)) { |
| 1560 | break; |
| 1561 | } |
| 1562 | |
| 1563 | switch (container) { |
| 1564 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 1565 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: |
| 1566 | sBgWorkspaceItems.add(info); |
| 1567 | break; |
| 1568 | default: |
| 1569 | // Item is in a user folder |
| 1570 | FolderInfo folderInfo = |
| 1571 | findOrMakeFolder(sBgFolders, container); |
| 1572 | folderInfo.add(info); |
| 1573 | break; |
| 1574 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1575 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 1576 | loadOldDb) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1577 | info.screenId = permuteScreens(info.screenId); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1578 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1579 | sBgItemsIdMap.put(info.id, info); |
| 1580 | |
| 1581 | // now that we've loaded everthing re-save it with the |
| 1582 | // icon in case it disappears somehow. |
| 1583 | queueIconToBeChecked(sBgDbIconCache, info, c, iconIndex); |
| 1584 | } else { |
| 1585 | // Failed to load the shortcut, probably because the |
| 1586 | // activity manager couldn't resolve it (maybe the app |
| 1587 | // was uninstalled), or the db row was somehow screwed up. |
| 1588 | // Delete it. |
| 1589 | id = c.getLong(idIndex); |
| 1590 | Log.e(TAG, "Error loading shortcut " + id + ", removing it"); |
| 1591 | contentResolver.delete(LauncherSettings.Favorites.getContentUri( |
| 1592 | id, false), null, null); |
| 1593 | } |
| 1594 | break; |
| 1595 | |
| 1596 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
| 1597 | id = c.getLong(idIndex); |
| 1598 | FolderInfo folderInfo = findOrMakeFolder(sBgFolders, id); |
| 1599 | |
| 1600 | folderInfo.title = c.getString(titleIndex); |
| 1601 | folderInfo.id = id; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1602 | container = c.getInt(containerIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1603 | folderInfo.container = container; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1604 | folderInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1605 | folderInfo.cellX = c.getInt(cellXIndex); |
| 1606 | folderInfo.cellY = c.getInt(cellYIndex); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1607 | |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1608 | // check & update map of what's occupied |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1609 | if (!checkItemPlacement(occupied, folderInfo)) { |
Daniel Sandler | 8802e96 | 2010-05-26 16:28:16 -0400 | [diff] [blame] | 1610 | break; |
| 1611 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1612 | switch (container) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1613 | case LauncherSettings.Favorites.CONTAINER_DESKTOP: |
| 1614 | case LauncherSettings.Favorites.CONTAINER_HOTSEAT: |
| 1615 | sBgWorkspaceItems.add(folderInfo); |
| 1616 | break; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1617 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1618 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 1619 | loadOldDb) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1620 | folderInfo.screenId = permuteScreens(folderInfo.screenId); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1621 | } |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 1622 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1623 | sBgItemsIdMap.put(folderInfo.id, folderInfo); |
| 1624 | sBgFolders.put(folderInfo.id, folderInfo); |
| 1625 | break; |
| 1626 | |
| 1627 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
| 1628 | // Read all Launcher-specific widget details |
| 1629 | int appWidgetId = c.getInt(appWidgetIdIndex); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1630 | id = c.getLong(idIndex); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1631 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1632 | final AppWidgetProviderInfo provider = |
| 1633 | widgets.getAppWidgetInfo(appWidgetId); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1634 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1635 | if (!isSafeMode && (provider == null || provider.provider == null || |
| 1636 | provider.provider.getPackageName() == null)) { |
| 1637 | String log = "Deleting widget that isn't installed anymore: id=" |
| 1638 | + id + " appWidgetId=" + appWidgetId; |
| 1639 | Log.e(TAG, log); |
| 1640 | Launcher.sDumpLogs.add(log); |
| 1641 | itemsToRemove.add(id); |
| 1642 | } else { |
| 1643 | appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId, |
| 1644 | provider.provider); |
| 1645 | appWidgetInfo.id = id; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1646 | appWidgetInfo.screenId = c.getInt(screenIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1647 | appWidgetInfo.cellX = c.getInt(cellXIndex); |
| 1648 | appWidgetInfo.cellY = c.getInt(cellYIndex); |
| 1649 | appWidgetInfo.spanX = c.getInt(spanXIndex); |
| 1650 | appWidgetInfo.spanY = c.getInt(spanYIndex); |
| 1651 | int[] minSpan = Launcher.getMinSpanForWidget(context, provider); |
| 1652 | appWidgetInfo.minSpanX = minSpan[0]; |
| 1653 | appWidgetInfo.minSpanY = minSpan[1]; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1654 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1655 | container = c.getInt(containerIndex); |
| 1656 | if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1657 | container != LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1658 | Log.e(TAG, "Widget found where container != " + |
| 1659 | "CONTAINER_DESKTOP nor CONTAINER_HOTSEAT - ignoring!"); |
| 1660 | continue; |
| 1661 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1662 | if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT && |
| 1663 | loadOldDb) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1664 | appWidgetInfo.screenId = |
| 1665 | permuteScreens(appWidgetInfo.screenId); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1666 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1667 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1668 | appWidgetInfo.container = c.getInt(containerIndex); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1669 | // check & update map of what's occupied |
| 1670 | if (!checkItemPlacement(occupied, appWidgetInfo)) { |
| 1671 | break; |
| 1672 | } |
| 1673 | sBgItemsIdMap.put(appWidgetInfo.id, appWidgetInfo); |
| 1674 | sBgAppWidgets.add(appWidgetInfo); |
| 1675 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1676 | break; |
| 1677 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1678 | } catch (Exception e) { |
| 1679 | Log.w(TAG, "Desktop items loading interrupted:", e); |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1680 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } finally { |
| 1683 | c.close(); |
| 1684 | } |
| 1685 | |
| 1686 | if (itemsToRemove.size() > 0) { |
| 1687 | ContentProviderClient client = contentResolver.acquireContentProviderClient( |
| 1688 | LauncherSettings.Favorites.CONTENT_URI); |
| 1689 | // Remove dead items |
| 1690 | for (long id : itemsToRemove) { |
| 1691 | if (DEBUG_LOADERS) { |
| 1692 | Log.d(TAG, "Removed id = " + id); |
| 1693 | } |
| 1694 | // Don't notify content observers |
| 1695 | try { |
| 1696 | client.delete(LauncherSettings.Favorites.getContentUri(id, false), |
| 1697 | null, null); |
| 1698 | } catch (RemoteException e) { |
| 1699 | Log.w(TAG, "Could not remove id = " + id); |
| 1700 | } |
Romain Guy | 5c16f3e | 2010-01-12 17:24:58 -0800 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1704 | if (loadOldDb) { |
| 1705 | long maxScreenId = 0; |
| 1706 | // If we're importing we use the old screen order. |
| 1707 | for (ItemInfo item: sBgItemsIdMap.values()) { |
| 1708 | long screenId = item.screenId; |
| 1709 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1710 | !sBgWorkspaceScreens.contains(screenId)) { |
| 1711 | sBgWorkspaceScreens.add(screenId); |
| 1712 | if (screenId > maxScreenId) { |
| 1713 | maxScreenId = screenId; |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | Collections.sort(sBgWorkspaceScreens); |
| 1718 | mApp.getLauncherProvider().updateMaxScreenId(maxScreenId); |
| 1719 | updateWorkspaceScreenOrder(context, sBgWorkspaceScreens); |
| 1720 | } else { |
| 1721 | Uri screensUri = LauncherSettings.WorkspaceScreens.CONTENT_URI; |
| 1722 | final Cursor sc = contentResolver.query(screensUri, null, null, null, null); |
| 1723 | TreeMap<Integer, Long> orderedScreens = new TreeMap<Integer, Long>(); |
| 1724 | |
| 1725 | try { |
| 1726 | final int idIndex = sc.getColumnIndexOrThrow( |
| 1727 | LauncherSettings.WorkspaceScreens._ID); |
| 1728 | final int rankIndex = sc.getColumnIndexOrThrow( |
| 1729 | LauncherSettings.WorkspaceScreens.SCREEN_RANK); |
| 1730 | while (sc.moveToNext()) { |
| 1731 | try { |
| 1732 | long screenId = sc.getLong(idIndex); |
| 1733 | int rank = sc.getInt(rankIndex); |
| 1734 | |
| 1735 | orderedScreens.put(rank, screenId); |
| 1736 | } catch (Exception e) { |
| 1737 | Log.w(TAG, "Desktop items loading interrupted:", e); |
| 1738 | } |
| 1739 | } |
| 1740 | } finally { |
| 1741 | sc.close(); |
| 1742 | } |
| 1743 | |
| 1744 | Iterator<Integer> iter = orderedScreens.keySet().iterator(); |
| 1745 | while (iter.hasNext()) { |
| 1746 | sBgWorkspaceScreens.add(orderedScreens.get(iter.next())); |
| 1747 | } |
| 1748 | |
| 1749 | // Remove any empty screens |
| 1750 | ArrayList<Long> unusedScreens = new ArrayList<Long>(); |
| 1751 | unusedScreens.addAll(sBgWorkspaceScreens); |
| 1752 | |
| 1753 | for (ItemInfo item: sBgItemsIdMap.values()) { |
| 1754 | long screenId = item.screenId; |
| 1755 | |
| 1756 | if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
| 1757 | unusedScreens.contains(screenId)) { |
| 1758 | unusedScreens.remove(screenId); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | // If there are any empty screens remove them, and update. |
| 1763 | if (unusedScreens.size() != 0) { |
| 1764 | sBgWorkspaceScreens.removeAll(unusedScreens); |
| 1765 | updateWorkspaceScreenOrder(context, sBgWorkspaceScreens); |
| 1766 | } |
| 1767 | } |
| 1768 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1769 | if (DEBUG_LOADERS) { |
| 1770 | Log.d(TAG, "loaded workspace in " + (SystemClock.uptimeMillis()-t) + "ms"); |
| 1771 | Log.d(TAG, "workspace layout: "); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1772 | int nScreens = occupied.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1773 | for (int y = 0; y < mCellCountY; y++) { |
| 1774 | String line = ""; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1775 | |
Daniel Sandler | 566da10 | 2013-06-25 23:43:45 -0400 | [diff] [blame] | 1776 | Iterator<Long> iter = occupied.keySet().iterator(); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 1777 | while (iter.hasNext()) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1778 | long screenId = iter.next(); |
Winson Chung | c916834 | 2013-06-26 14:54:55 -0700 | [diff] [blame] | 1779 | if (screenId > 0) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1780 | line += " | "; |
| 1781 | } |
| 1782 | for (int x = 0; x < mCellCountX; x++) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1783 | line += ((occupied.get(screenId)[x][y] != null) ? "#" : "."); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1784 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1785 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1786 | Log.d(TAG, "[ " + line + " ]"); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1787 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1788 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1789 | } |
| 1790 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1791 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1792 | // We rearrange the screens from the old launcher |
| 1793 | // 12345 -> 34512 |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1794 | private long permuteScreens(long screen) { |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 1795 | if (screen >= 2) { |
| 1796 | return screen - 2; |
| 1797 | } else { |
| 1798 | return screen + 3; |
| 1799 | } |
| 1800 | } |
| 1801 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1802 | /** Filters the set of items who are directly or indirectly (via another container) on the |
| 1803 | * specified screen. */ |
| 1804 | private void filterCurrentWorkspaceItems(int currentScreen, |
| 1805 | ArrayList<ItemInfo> allWorkspaceItems, |
| 1806 | ArrayList<ItemInfo> currentScreenItems, |
| 1807 | ArrayList<ItemInfo> otherScreenItems) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1808 | // Purge any null ItemInfos |
| 1809 | Iterator<ItemInfo> iter = allWorkspaceItems.iterator(); |
| 1810 | while (iter.hasNext()) { |
| 1811 | ItemInfo i = iter.next(); |
| 1812 | if (i == null) { |
| 1813 | iter.remove(); |
| 1814 | } |
| 1815 | } |
| 1816 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1817 | // If we aren't filtering on a screen, then the set of items to load is the full set of |
| 1818 | // items given. |
| 1819 | if (currentScreen < 0) { |
| 1820 | currentScreenItems.addAll(allWorkspaceItems); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1821 | } |
| 1822 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1823 | // Order the set of items by their containers first, this allows use to walk through the |
| 1824 | // list sequentially, build up a list of containers that are in the specified screen, |
| 1825 | // as well as all items in those containers. |
| 1826 | Set<Long> itemsOnScreen = new HashSet<Long>(); |
| 1827 | Collections.sort(allWorkspaceItems, new Comparator<ItemInfo>() { |
| 1828 | @Override |
| 1829 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
| 1830 | return (int) (lhs.container - rhs.container); |
| 1831 | } |
| 1832 | }); |
| 1833 | for (ItemInfo info : allWorkspaceItems) { |
| 1834 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1835 | if (info.screenId == currentScreen) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1836 | currentScreenItems.add(info); |
| 1837 | itemsOnScreen.add(info.id); |
| 1838 | } else { |
| 1839 | otherScreenItems.add(info); |
| 1840 | } |
| 1841 | } else if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1842 | currentScreenItems.add(info); |
| 1843 | itemsOnScreen.add(info.id); |
| 1844 | } else { |
| 1845 | if (itemsOnScreen.contains(info.container)) { |
| 1846 | currentScreenItems.add(info); |
| 1847 | itemsOnScreen.add(info.id); |
| 1848 | } else { |
| 1849 | otherScreenItems.add(info); |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | /** Filters the set of widgets which are on the specified screen. */ |
| 1856 | private void filterCurrentAppWidgets(int currentScreen, |
| 1857 | ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1858 | ArrayList<LauncherAppWidgetInfo> currentScreenWidgets, |
| 1859 | ArrayList<LauncherAppWidgetInfo> otherScreenWidgets) { |
| 1860 | // If we aren't filtering on a screen, then the set of items to load is the full set of |
| 1861 | // widgets given. |
| 1862 | if (currentScreen < 0) { |
| 1863 | currentScreenWidgets.addAll(appWidgets); |
| 1864 | } |
| 1865 | |
| 1866 | for (LauncherAppWidgetInfo widget : appWidgets) { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1867 | if (widget == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1868 | if (widget.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1869 | widget.screenId == currentScreen) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1870 | currentScreenWidgets.add(widget); |
| 1871 | } else { |
| 1872 | otherScreenWidgets.add(widget); |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /** Filters the set of folders which are on the specified screen. */ |
| 1878 | private void filterCurrentFolders(int currentScreen, |
| 1879 | HashMap<Long, ItemInfo> itemsIdMap, |
| 1880 | HashMap<Long, FolderInfo> folders, |
| 1881 | HashMap<Long, FolderInfo> currentScreenFolders, |
| 1882 | HashMap<Long, FolderInfo> otherScreenFolders) { |
| 1883 | // If we aren't filtering on a screen, then the set of items to load is the full set of |
| 1884 | // widgets given. |
| 1885 | if (currentScreen < 0) { |
| 1886 | currentScreenFolders.putAll(folders); |
| 1887 | } |
| 1888 | |
| 1889 | for (long id : folders.keySet()) { |
| 1890 | ItemInfo info = itemsIdMap.get(id); |
| 1891 | FolderInfo folder = folders.get(id); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 1892 | if (info == null || folder == null) continue; |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1893 | if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP && |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1894 | info.screenId == currentScreen) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1895 | currentScreenFolders.put(id, folder); |
| 1896 | } else { |
| 1897 | otherScreenFolders.put(id, folder); |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | /** Sorts the set of items by hotseat, workspace (spatially from top to bottom, left to |
| 1903 | * right) */ |
| 1904 | private void sortWorkspaceItemsSpatially(ArrayList<ItemInfo> workspaceItems) { |
| 1905 | // XXX: review this |
| 1906 | Collections.sort(workspaceItems, new Comparator<ItemInfo>() { |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1907 | @Override |
| 1908 | public int compare(ItemInfo lhs, ItemInfo rhs) { |
| 1909 | int cellCountX = LauncherModel.getCellCountX(); |
| 1910 | int cellCountY = LauncherModel.getCellCountY(); |
| 1911 | int screenOffset = cellCountX * cellCountY; |
| 1912 | int containerOffset = screenOffset * (Launcher.SCREEN_COUNT + 1); // +1 hotseat |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1913 | long lr = (lhs.container * containerOffset + lhs.screenId * screenOffset + |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1914 | lhs.cellY * cellCountX + lhs.cellX); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1915 | long rr = (rhs.container * containerOffset + rhs.screenId * screenOffset + |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1916 | rhs.cellY * cellCountX + rhs.cellX); |
| 1917 | return (int) (lr - rr); |
| 1918 | } |
| 1919 | }); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1920 | } |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1921 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1922 | private void bindWorkspaceScreens(final Callbacks oldCallbacks, |
| 1923 | final ArrayList<Long> orderedScreens) { |
| 1924 | |
| 1925 | final Runnable r = new Runnable() { |
| 1926 | @Override |
| 1927 | public void run() { |
| 1928 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1929 | if (callbacks != null) { |
| 1930 | callbacks.bindScreens(orderedScreens); |
| 1931 | } |
| 1932 | } |
| 1933 | }; |
| 1934 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
| 1935 | } |
| 1936 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1937 | private void bindWorkspaceItems(final Callbacks oldCallbacks, |
| 1938 | final ArrayList<ItemInfo> workspaceItems, |
| 1939 | final ArrayList<LauncherAppWidgetInfo> appWidgets, |
| 1940 | final HashMap<Long, FolderInfo> folders, |
| 1941 | ArrayList<Runnable> deferredBindRunnables) { |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 1942 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1943 | final boolean postOnMainThread = (deferredBindRunnables != null); |
| 1944 | |
| 1945 | // Bind the workspace items |
Winson Chung | db8a894 | 2012-04-03 14:08:41 -0700 | [diff] [blame] | 1946 | int N = workspaceItems.size(); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1947 | for (int i = 0; i < N; i += ITEMS_CHUNK) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1948 | final int start = i; |
| 1949 | final int chunkSize = (i+ITEMS_CHUNK <= N) ? ITEMS_CHUNK : (N-i); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1950 | final Runnable r = new Runnable() { |
| 1951 | @Override |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1952 | public void run() { |
Joe Onorato | c131b74 | 2010-03-11 15:45:05 -0800 | [diff] [blame] | 1953 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1954 | if (callbacks != null) { |
Winson Chung | 603bcb9 | 2011-09-02 11:45:39 -0700 | [diff] [blame] | 1955 | callbacks.bindItems(workspaceItems, start, start+chunkSize); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1956 | } |
| 1957 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1958 | }; |
| 1959 | if (postOnMainThread) { |
| 1960 | deferredBindRunnables.add(r); |
| 1961 | } else { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 1962 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1963 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 1964 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1965 | |
| 1966 | // Bind the folders |
| 1967 | if (!folders.isEmpty()) { |
| 1968 | final Runnable r = new Runnable() { |
| 1969 | public void run() { |
| 1970 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1971 | if (callbacks != null) { |
| 1972 | callbacks.bindFolders(folders); |
| 1973 | } |
| 1974 | } |
| 1975 | }; |
| 1976 | if (postOnMainThread) { |
| 1977 | deferredBindRunnables.add(r); |
| 1978 | } else { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 1979 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | // Bind the widgets, one at a time |
| 1984 | N = appWidgets.size(); |
| 1985 | for (int i = 0; i < N; i++) { |
| 1986 | final LauncherAppWidgetInfo widget = appWidgets.get(i); |
| 1987 | final Runnable r = new Runnable() { |
| 1988 | public void run() { |
| 1989 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 1990 | if (callbacks != null) { |
| 1991 | callbacks.bindAppWidget(widget); |
| 1992 | } |
| 1993 | } |
| 1994 | }; |
| 1995 | if (postOnMainThread) { |
| 1996 | deferredBindRunnables.add(r); |
| 1997 | } else { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 1998 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * Binds all loaded data to actual views on the main thread. |
| 2005 | */ |
| 2006 | private void bindWorkspace(int synchronizeBindPage) { |
| 2007 | final long t = SystemClock.uptimeMillis(); |
| 2008 | Runnable r; |
| 2009 | |
| 2010 | // Don't use these two variables in any of the callback runnables. |
| 2011 | // Otherwise we hold a reference to them. |
| 2012 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2013 | if (oldCallbacks == null) { |
| 2014 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2015 | Log.w(TAG, "LoaderTask running with no launcher"); |
| 2016 | return; |
| 2017 | } |
| 2018 | |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2019 | final boolean isLoadingSynchronously = (synchronizeBindPage > -1); |
| 2020 | final int currentScreen = isLoadingSynchronously ? synchronizeBindPage : |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2021 | oldCallbacks.getCurrentWorkspaceScreen(); |
| 2022 | |
| 2023 | // Load all the items that are on the current page first (and in the process, unbind |
| 2024 | // all the existing workspace items before we call startBinding() below. |
| 2025 | unbindWorkspaceItemsOnMainThread(); |
| 2026 | ArrayList<ItemInfo> workspaceItems = new ArrayList<ItemInfo>(); |
| 2027 | ArrayList<LauncherAppWidgetInfo> appWidgets = |
| 2028 | new ArrayList<LauncherAppWidgetInfo>(); |
| 2029 | HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>(); |
| 2030 | HashMap<Long, ItemInfo> itemsIdMap = new HashMap<Long, ItemInfo>(); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2031 | ArrayList<Long> orderedScreenIds = new ArrayList<Long>(); |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2032 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2033 | workspaceItems.addAll(sBgWorkspaceItems); |
| 2034 | appWidgets.addAll(sBgAppWidgets); |
| 2035 | folders.putAll(sBgFolders); |
| 2036 | itemsIdMap.putAll(sBgItemsIdMap); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2037 | orderedScreenIds.addAll(sBgWorkspaceScreens); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<ItemInfo>(); |
| 2041 | ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<ItemInfo>(); |
| 2042 | ArrayList<LauncherAppWidgetInfo> currentAppWidgets = |
| 2043 | new ArrayList<LauncherAppWidgetInfo>(); |
| 2044 | ArrayList<LauncherAppWidgetInfo> otherAppWidgets = |
| 2045 | new ArrayList<LauncherAppWidgetInfo>(); |
| 2046 | HashMap<Long, FolderInfo> currentFolders = new HashMap<Long, FolderInfo>(); |
| 2047 | HashMap<Long, FolderInfo> otherFolders = new HashMap<Long, FolderInfo>(); |
| 2048 | |
| 2049 | // Separate the items that are on the current screen, and all the other remaining items |
| 2050 | filterCurrentWorkspaceItems(currentScreen, workspaceItems, currentWorkspaceItems, |
| 2051 | otherWorkspaceItems); |
| 2052 | filterCurrentAppWidgets(currentScreen, appWidgets, currentAppWidgets, |
| 2053 | otherAppWidgets); |
| 2054 | filterCurrentFolders(currentScreen, itemsIdMap, folders, currentFolders, |
| 2055 | otherFolders); |
| 2056 | sortWorkspaceItemsSpatially(currentWorkspaceItems); |
| 2057 | sortWorkspaceItemsSpatially(otherWorkspaceItems); |
| 2058 | |
| 2059 | // Tell the workspace that we're about to start binding items |
| 2060 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2061 | public void run() { |
| 2062 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2063 | if (callbacks != null) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2064 | callbacks.startBinding(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2065 | } |
| 2066 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2067 | }; |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 2068 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2069 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 2070 | bindWorkspaceScreens(oldCallbacks, orderedScreenIds); |
| 2071 | |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2072 | // Load items on the current page |
| 2073 | bindWorkspaceItems(oldCallbacks, currentWorkspaceItems, currentAppWidgets, |
| 2074 | currentFolders, null); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2075 | if (isLoadingSynchronously) { |
| 2076 | r = new Runnable() { |
| 2077 | public void run() { |
| 2078 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2079 | if (callbacks != null) { |
| 2080 | callbacks.onPageBoundSynchronously(currentScreen); |
| 2081 | } |
| 2082 | } |
| 2083 | }; |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 2084 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Adam Cohen | 1462de3 | 2012-07-24 22:34:36 -0700 | [diff] [blame] | 2085 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2086 | |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2087 | // Load all the remaining pages (if we are loading synchronously, we want to defer this |
| 2088 | // work until after the first render) |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2089 | mDeferredBindRunnables.clear(); |
| 2090 | bindWorkspaceItems(oldCallbacks, otherWorkspaceItems, otherAppWidgets, otherFolders, |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2091 | (isLoadingSynchronously ? mDeferredBindRunnables : null)); |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2092 | |
| 2093 | // Tell the workspace that we're done binding items |
| 2094 | r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2095 | public void run() { |
| 2096 | Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2097 | if (callbacks != null) { |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 2098 | callbacks.finishBindingItems(mIsUpgradePath); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2099 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2100 | |
Winson Chung | 98e030b | 2012-05-07 16:01:11 -0700 | [diff] [blame] | 2101 | // 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] | 2102 | if (DEBUG_LOADERS) { |
| 2103 | Log.d(TAG, "bound workspace in " |
| 2104 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 2105 | } |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 2106 | |
| 2107 | mIsLoadingAndBindingWorkspace = false; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2108 | } |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2109 | }; |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2110 | if (isLoadingSynchronously) { |
| 2111 | mDeferredBindRunnables.add(r); |
| 2112 | } else { |
Winson Chung | 81b5225 | 2012-08-27 15:34:29 -0700 | [diff] [blame] | 2113 | runOnMainThread(r, MAIN_THREAD_BINDING_RUNNABLE); |
Winson Chung | 4a2afa3 | 2012-07-19 14:53:05 -0700 | [diff] [blame] | 2114 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2115 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2116 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2117 | private void loadAndBindAllApps() { |
| 2118 | if (DEBUG_LOADERS) { |
| 2119 | Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded); |
| 2120 | } |
| 2121 | if (!mAllAppsLoaded) { |
| 2122 | loadAllAppsByBatch(); |
Reena Lee | 93f824a | 2011-09-23 17:20:28 -0700 | [diff] [blame] | 2123 | synchronized (LoaderTask.this) { |
| 2124 | if (mStopped) { |
| 2125 | return; |
| 2126 | } |
| 2127 | mAllAppsLoaded = true; |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2128 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2129 | } else { |
| 2130 | onlyBindAllApps(); |
| 2131 | } |
| 2132 | } |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2133 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2134 | private void onlyBindAllApps() { |
| 2135 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2136 | if (oldCallbacks == null) { |
| 2137 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2138 | Log.w(TAG, "LoaderTask running with no launcher (onlyBindAllApps)"); |
| 2139 | return; |
| 2140 | } |
| 2141 | |
| 2142 | // shallow copy |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 2143 | @SuppressWarnings("unchecked") |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2144 | final ArrayList<ApplicationInfo> list |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2145 | = (ArrayList<ApplicationInfo>) mBgAllAppsList.data.clone(); |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2146 | Runnable r = new Runnable() { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2147 | public void run() { |
| 2148 | final long t = SystemClock.uptimeMillis(); |
| 2149 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
| 2150 | if (callbacks != null) { |
| 2151 | callbacks.bindAllApplications(list); |
| 2152 | } |
| 2153 | if (DEBUG_LOADERS) { |
| 2154 | Log.d(TAG, "bound all " + list.size() + " apps from cache in " |
| 2155 | + (SystemClock.uptimeMillis()-t) + "ms"); |
| 2156 | } |
| 2157 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 2158 | }; |
| 2159 | boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid()); |
| 2160 | if (oldCallbacks.isAllAppsVisible() && isRunningOnMainThread) { |
| 2161 | r.run(); |
| 2162 | } else { |
| 2163 | mHandler.post(r); |
| 2164 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | private void loadAllAppsByBatch() { |
| 2168 | final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 2169 | |
| 2170 | // Don't use these two variables in any of the callback runnables. |
| 2171 | // Otherwise we hold a reference to them. |
| 2172 | final Callbacks oldCallbacks = mCallbacks.get(); |
| 2173 | if (oldCallbacks == null) { |
| 2174 | // This launcher has exited and nobody bothered to tell us. Just bail. |
| 2175 | Log.w(TAG, "LoaderTask running with no launcher (loadAllAppsByBatch)"); |
| 2176 | return; |
| 2177 | } |
| 2178 | |
| 2179 | final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); |
| 2180 | mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 2181 | |
| 2182 | final PackageManager packageManager = mContext.getPackageManager(); |
| 2183 | List<ResolveInfo> apps = null; |
| 2184 | |
| 2185 | int N = Integer.MAX_VALUE; |
| 2186 | |
| 2187 | int startIndex; |
| 2188 | int i=0; |
| 2189 | int batchSize = -1; |
| 2190 | while (i < N && !mStopped) { |
| 2191 | if (i == 0) { |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2192 | mBgAllAppsList.clear(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2193 | final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 2194 | apps = packageManager.queryIntentActivities(mainIntent, 0); |
| 2195 | if (DEBUG_LOADERS) { |
| 2196 | Log.d(TAG, "queryIntentActivities took " |
| 2197 | + (SystemClock.uptimeMillis()-qiaTime) + "ms"); |
| 2198 | } |
| 2199 | if (apps == null) { |
| 2200 | return; |
| 2201 | } |
| 2202 | N = apps.size(); |
| 2203 | if (DEBUG_LOADERS) { |
| 2204 | Log.d(TAG, "queryIntentActivities got " + N + " apps"); |
| 2205 | } |
| 2206 | if (N == 0) { |
| 2207 | // There are no apps?!? |
| 2208 | return; |
| 2209 | } |
| 2210 | if (mBatchSize == 0) { |
| 2211 | batchSize = N; |
| 2212 | } else { |
| 2213 | batchSize = mBatchSize; |
| 2214 | } |
| 2215 | |
| 2216 | final long sortTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 2217 | Collections.sort(apps, |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2218 | new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache)); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2219 | if (DEBUG_LOADERS) { |
| 2220 | Log.d(TAG, "sort took " |
| 2221 | + (SystemClock.uptimeMillis()-sortTime) + "ms"); |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; |
| 2226 | |
| 2227 | startIndex = i; |
| 2228 | for (int j=0; i<N && j<batchSize; j++) { |
| 2229 | // This builds the icon bitmaps. |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2230 | mBgAllAppsList.add(new ApplicationInfo(packageManager, apps.get(i), |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2231 | mIconCache, mLabelCache)); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2232 | i++; |
| 2233 | } |
| 2234 | |
| 2235 | final boolean first = i <= batchSize; |
| 2236 | final Callbacks callbacks = tryGetCallbacks(oldCallbacks); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2237 | final ArrayList<ApplicationInfo> added = mBgAllAppsList.added; |
| 2238 | mBgAllAppsList.added = new ArrayList<ApplicationInfo>(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2239 | |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2240 | mHandler.post(new Runnable() { |
| 2241 | public void run() { |
| 2242 | final long t = SystemClock.uptimeMillis(); |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2243 | if (callbacks != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2244 | if (first) { |
| 2245 | callbacks.bindAllApplications(added); |
| 2246 | } else { |
| 2247 | callbacks.bindAppsAdded(added); |
| 2248 | } |
| 2249 | if (DEBUG_LOADERS) { |
| 2250 | Log.d(TAG, "bound " + added.size() + " apps in " |
| 2251 | + (SystemClock.uptimeMillis() - t) + "ms"); |
| 2252 | } |
| 2253 | } else { |
| 2254 | Log.i(TAG, "not binding apps: no Launcher activity"); |
Joe Onorato | cc67f47 | 2010-06-08 10:54:30 -0700 | [diff] [blame] | 2255 | } |
| 2256 | } |
| 2257 | }); |
| 2258 | |
Daniel Sandler | dca6612 | 2010-04-13 16:23:58 -0400 | [diff] [blame] | 2259 | if (DEBUG_LOADERS) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2260 | Log.d(TAG, "batch of " + (i-startIndex) + " icons processed in " |
| 2261 | + (SystemClock.uptimeMillis()-t2) + "ms"); |
| 2262 | } |
| 2263 | |
| 2264 | if (mAllAppsLoadDelay > 0 && i < N) { |
| 2265 | try { |
| 2266 | if (DEBUG_LOADERS) { |
| 2267 | Log.d(TAG, "sleeping for " + mAllAppsLoadDelay + "ms"); |
| 2268 | } |
| 2269 | Thread.sleep(mAllAppsLoadDelay); |
| 2270 | } catch (InterruptedException exc) { } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2274 | if (DEBUG_LOADERS) { |
| 2275 | Log.d(TAG, "cached all " + N + " apps in " |
| 2276 | + (SystemClock.uptimeMillis()-t) + "ms" |
| 2277 | + (mAllAppsLoadDelay > 0 ? " (including delay)" : "")); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | public void dumpState() { |
Winson Chung | 2abf94d | 2012-07-18 18:16:38 -0700 | [diff] [blame] | 2282 | synchronized (sBgLock) { |
Winson Chung | b8b2a5a | 2012-07-12 17:55:31 -0700 | [diff] [blame] | 2283 | Log.d(TAG, "mLoaderTask.mContext=" + mContext); |
| 2284 | Log.d(TAG, "mLoaderTask.mIsLaunching=" + mIsLaunching); |
| 2285 | Log.d(TAG, "mLoaderTask.mStopped=" + mStopped); |
| 2286 | Log.d(TAG, "mLoaderTask.mLoadAndBindStepFinished=" + mLoadAndBindStepFinished); |
| 2287 | Log.d(TAG, "mItems size=" + sBgWorkspaceItems.size()); |
| 2288 | } |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | void enqueuePackageUpdated(PackageUpdatedTask task) { |
Brad Fitzpatrick | 700889f | 2010-10-11 09:40:44 -0700 | [diff] [blame] | 2293 | sWorker.post(task); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | private class PackageUpdatedTask implements Runnable { |
| 2297 | int mOp; |
| 2298 | String[] mPackages; |
| 2299 | |
| 2300 | public static final int OP_NONE = 0; |
| 2301 | public static final int OP_ADD = 1; |
| 2302 | public static final int OP_UPDATE = 2; |
| 2303 | public static final int OP_REMOVE = 3; // uninstlled |
| 2304 | public static final int OP_UNAVAILABLE = 4; // external media unmounted |
| 2305 | |
| 2306 | |
| 2307 | public PackageUpdatedTask(int op, String[] packages) { |
| 2308 | mOp = op; |
| 2309 | mPackages = packages; |
| 2310 | } |
| 2311 | |
| 2312 | public void run() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 2313 | final Context context = mApp.getContext(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2314 | |
| 2315 | final String[] packages = mPackages; |
| 2316 | final int N = packages.length; |
| 2317 | switch (mOp) { |
| 2318 | case OP_ADD: |
| 2319 | for (int i=0; i<N; i++) { |
| 2320 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.addPackage " + packages[i]); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2321 | mBgAllAppsList.addPackage(context, packages[i]); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2322 | } |
| 2323 | break; |
| 2324 | case OP_UPDATE: |
| 2325 | for (int i=0; i<N; i++) { |
| 2326 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.updatePackage " + packages[i]); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2327 | mBgAllAppsList.updatePackage(context, packages[i]); |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 2328 | WidgetPreviewLoader.removeFromDb( |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame^] | 2329 | mApp.getWidgetPreviewCacheDb(), packages[i]); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2330 | } |
| 2331 | break; |
| 2332 | case OP_REMOVE: |
| 2333 | case OP_UNAVAILABLE: |
| 2334 | for (int i=0; i<N; i++) { |
| 2335 | if (DEBUG_LOADERS) Log.d(TAG, "mAllAppsList.removePackage " + packages[i]); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2336 | mBgAllAppsList.removePackage(packages[i]); |
Michael Jurka | d9cb4a1 | 2013-03-19 12:01:06 +0100 | [diff] [blame] | 2337 | WidgetPreviewLoader.removeFromDb( |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame^] | 2338 | mApp.getWidgetPreviewCacheDb(), packages[i]); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2339 | } |
| 2340 | break; |
| 2341 | } |
| 2342 | |
| 2343 | ArrayList<ApplicationInfo> added = null; |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2344 | ArrayList<ApplicationInfo> modified = null; |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 2345 | final ArrayList<ApplicationInfo> removedApps = new ArrayList<ApplicationInfo>(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2346 | |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2347 | if (mBgAllAppsList.added.size() > 0) { |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 2348 | added = new ArrayList<ApplicationInfo>(mBgAllAppsList.added); |
| 2349 | mBgAllAppsList.added.clear(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2350 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2351 | if (mBgAllAppsList.modified.size() > 0) { |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 2352 | modified = new ArrayList<ApplicationInfo>(mBgAllAppsList.modified); |
| 2353 | mBgAllAppsList.modified.clear(); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2354 | } |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 2355 | if (mBgAllAppsList.removed.size() > 0) { |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 2356 | removedApps.addAll(mBgAllAppsList.removed); |
Winson Chung | 5d55f33 | 2012-07-16 20:45:03 -0700 | [diff] [blame] | 2357 | mBgAllAppsList.removed.clear(); |
Winson Chung | cd81073 | 2012-06-18 16:45:43 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2360 | final Callbacks callbacks = mCallbacks != null ? mCallbacks.get() : null; |
| 2361 | if (callbacks == null) { |
| 2362 | Log.w(TAG, "Nobody to tell about the new app. Launcher is probably loading."); |
| 2363 | return; |
| 2364 | } |
| 2365 | |
| 2366 | if (added != null) { |
| 2367 | final ArrayList<ApplicationInfo> addedFinal = added; |
| 2368 | mHandler.post(new Runnable() { |
| 2369 | public void run() { |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 2370 | Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; |
| 2371 | if (callbacks == cb && cb != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2372 | callbacks.bindAppsAdded(addedFinal); |
| 2373 | } |
| 2374 | } |
| 2375 | }); |
| 2376 | } |
| 2377 | if (modified != null) { |
| 2378 | final ArrayList<ApplicationInfo> modifiedFinal = modified; |
| 2379 | mHandler.post(new Runnable() { |
| 2380 | public void run() { |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 2381 | Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; |
| 2382 | if (callbacks == cb && cb != null) { |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2383 | callbacks.bindAppsUpdated(modifiedFinal); |
| 2384 | } |
| 2385 | } |
| 2386 | }); |
| 2387 | } |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 2388 | // If a package has been removed, or an app has been removed as a result of |
| 2389 | // an update (for example), make the removed callback. |
| 2390 | if (mOp == OP_REMOVE || !removedApps.isEmpty()) { |
| 2391 | final boolean permanent = (mOp == OP_REMOVE); |
| 2392 | final ArrayList<String> removedPackageNames = |
| 2393 | new ArrayList<String>(Arrays.asList(packages)); |
| 2394 | |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2395 | mHandler.post(new Runnable() { |
| 2396 | public void run() { |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 2397 | Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; |
| 2398 | if (callbacks == cb && cb != null) { |
Winson Chung | 83892cc | 2013-05-01 16:53:33 -0700 | [diff] [blame] | 2399 | callbacks.bindComponentsRemoved(removedPackageNames, |
| 2400 | removedApps, permanent); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | }); |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2404 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2405 | |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 2406 | final ArrayList<Object> widgetsAndShortcuts = |
| 2407 | getSortedWidgetsAndShortcuts(context); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2408 | mHandler.post(new Runnable() { |
| 2409 | @Override |
| 2410 | public void run() { |
Winson Chung | cd2b014 | 2011-06-08 16:02:26 -0700 | [diff] [blame] | 2411 | Callbacks cb = mCallbacks != null ? mCallbacks.get() : null; |
| 2412 | if (callbacks == cb && cb != null) { |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 2413 | callbacks.bindPackagesUpdated(widgetsAndShortcuts); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | }); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
Michael Jurka | c402cd9 | 2013-05-20 15:49:32 +0200 | [diff] [blame] | 2420 | // Returns a list of ResolveInfos/AppWindowInfos in sorted order |
| 2421 | public static ArrayList<Object> getSortedWidgetsAndShortcuts(Context context) { |
| 2422 | PackageManager packageManager = context.getPackageManager(); |
| 2423 | final ArrayList<Object> widgetsAndShortcuts = new ArrayList<Object>(); |
| 2424 | widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders()); |
| 2425 | Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); |
| 2426 | widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0)); |
| 2427 | Collections.sort(widgetsAndShortcuts, |
| 2428 | new LauncherModel.WidgetAndShortcutNameComparator(packageManager)); |
| 2429 | return widgetsAndShortcuts; |
| 2430 | } |
| 2431 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2432 | /** |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2433 | * This is called from the code that adds shortcuts from the intent receiver. This |
| 2434 | * doesn't have a Cursor, but |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2435 | */ |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2436 | public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context) { |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2437 | return getShortcutInfo(manager, intent, context, null, -1, -1, null); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2438 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2439 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2440 | /** |
| 2441 | * Make an ShortcutInfo object for a shortcut that is an application. |
| 2442 | * |
| 2443 | * If c is not null, then it will be used to fill in missing data like the title and icon. |
| 2444 | */ |
| 2445 | public ShortcutInfo getShortcutInfo(PackageManager manager, Intent intent, Context context, |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2446 | Cursor c, int iconIndex, int titleIndex, HashMap<Object, CharSequence> labelCache) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2447 | Bitmap icon = null; |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2448 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2449 | |
| 2450 | ComponentName componentName = intent.getComponent(); |
| 2451 | if (componentName == null) { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2452 | return null; |
| 2453 | } |
| 2454 | |
Adam Cohen | 00fcb49 | 2011-11-02 21:53:47 -0700 | [diff] [blame] | 2455 | try { |
| 2456 | PackageInfo pi = manager.getPackageInfo(componentName.getPackageName(), 0); |
| 2457 | if (!pi.applicationInfo.enabled) { |
| 2458 | // If we return null here, the corresponding item will be removed from the launcher |
| 2459 | // db and will not appear in the workspace. |
| 2460 | return null; |
| 2461 | } |
| 2462 | } catch (NameNotFoundException e) { |
| 2463 | Log.d(TAG, "getPackInfo failed for package " + componentName.getPackageName()); |
| 2464 | } |
| 2465 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 2466 | // TODO: See if the PackageManager knows about this case. If it doesn't |
| 2467 | // then return null & delete this. |
| 2468 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2469 | // the resource -- This may implicitly give us back the fallback icon, |
| 2470 | // but don't worry about that. All we're doing with usingFallbackIcon is |
| 2471 | // to avoid saving lots of copies of that in the database, and most apps |
| 2472 | // have icons anyway. |
Winson Chung | c208ff9 | 2012-03-29 17:37:41 -0700 | [diff] [blame] | 2473 | |
| 2474 | // Attempt to use queryIntentActivities to get the ResolveInfo (with IntentFilter info) and |
| 2475 | // if that fails, or is ambiguious, fallback to the standard way of getting the resolve info |
| 2476 | // via resolveActivity(). |
| 2477 | ResolveInfo resolveInfo = null; |
| 2478 | ComponentName oldComponent = intent.getComponent(); |
| 2479 | Intent newIntent = new Intent(intent.getAction(), null); |
| 2480 | newIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 2481 | newIntent.setPackage(oldComponent.getPackageName()); |
| 2482 | List<ResolveInfo> infos = manager.queryIntentActivities(newIntent, 0); |
| 2483 | for (ResolveInfo i : infos) { |
| 2484 | ComponentName cn = new ComponentName(i.activityInfo.packageName, |
| 2485 | i.activityInfo.name); |
| 2486 | if (cn.equals(oldComponent)) { |
| 2487 | resolveInfo = i; |
| 2488 | } |
| 2489 | } |
| 2490 | if (resolveInfo == null) { |
| 2491 | resolveInfo = manager.resolveActivity(intent, 0); |
| 2492 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2493 | if (resolveInfo != null) { |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 2494 | icon = mIconCache.getIcon(componentName, resolveInfo, labelCache); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2495 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2496 | // the db |
| 2497 | if (icon == null) { |
| 2498 | if (c != null) { |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 2499 | icon = getIconFromCursor(c, iconIndex, context); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2500 | } |
| 2501 | } |
| 2502 | // the fallback icon |
| 2503 | if (icon == null) { |
| 2504 | icon = getFallbackIcon(); |
| 2505 | info.usingFallbackIcon = true; |
| 2506 | } |
| 2507 | info.setIcon(icon); |
| 2508 | |
| 2509 | // from the resource |
| 2510 | if (resolveInfo != null) { |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2511 | ComponentName key = LauncherModel.getComponentNameFromResolveInfo(resolveInfo); |
| 2512 | if (labelCache != null && labelCache.containsKey(key)) { |
| 2513 | info.title = labelCache.get(key); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2514 | } else { |
| 2515 | info.title = resolveInfo.activityInfo.loadLabel(manager); |
| 2516 | if (labelCache != null) { |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2517 | labelCache.put(key, info.title); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2518 | } |
| 2519 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2520 | } |
| 2521 | // from the db |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2522 | if (info.title == null) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2523 | if (c != null) { |
| 2524 | info.title = c.getString(titleIndex); |
| 2525 | } |
| 2526 | } |
| 2527 | // fall back to the class name of the activity |
| 2528 | if (info.title == null) { |
| 2529 | info.title = componentName.getClassName(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 2530 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2531 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
| 2532 | return info; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2533 | } |
The Android Open Source Project | bc219c3 | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 2534 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2535 | /** |
Winson Chung | 8a43510 | 2012-08-30 17:16:53 -0700 | [diff] [blame] | 2536 | * Returns the set of workspace ShortcutInfos with the specified intent. |
| 2537 | */ |
| 2538 | static ArrayList<ItemInfo> getWorkspaceShortcutItemInfosWithIntent(Intent intent) { |
| 2539 | ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); |
| 2540 | synchronized (sBgLock) { |
| 2541 | for (ItemInfo info : sBgWorkspaceItems) { |
| 2542 | if (info instanceof ShortcutInfo) { |
| 2543 | ShortcutInfo shortcut = (ShortcutInfo) info; |
| 2544 | if (shortcut.intent.toUri(0).equals(intent.toUri(0))) { |
| 2545 | items.add(shortcut); |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | } |
| 2550 | return items; |
| 2551 | } |
| 2552 | |
| 2553 | /** |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2554 | * 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] | 2555 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2556 | private ShortcutInfo getShortcutInfo(Cursor c, Context context, |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2557 | int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex, |
| 2558 | int titleIndex) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2559 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2560 | Bitmap icon = null; |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2561 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2562 | info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2563 | |
Joe Onorato | 8ddc4fd | 2010-03-17 09:14:50 -0700 | [diff] [blame] | 2564 | // TODO: If there's an explicit component and we can't install that, delete it. |
| 2565 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2566 | info.title = c.getString(titleIndex); |
| 2567 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2568 | int iconType = c.getInt(iconTypeIndex); |
| 2569 | switch (iconType) { |
| 2570 | case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: |
| 2571 | String packageName = c.getString(iconPackageIndex); |
| 2572 | String resourceName = c.getString(iconResourceIndex); |
| 2573 | PackageManager packageManager = context.getPackageManager(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2574 | info.customIcon = false; |
| 2575 | // the resource |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2576 | try { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2577 | Resources resources = packageManager.getResourcesForApplication(packageName); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2578 | if (resources != null) { |
| 2579 | final int id = resources.getIdentifier(resourceName, null, null); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 2580 | icon = Utilities.createIconBitmap( |
| 2581 | mIconCache.getFullResIcon(resources, id), context); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2582 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2583 | } catch (Exception e) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2584 | // drop this. we have other places to look for icons |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2585 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2586 | // the db |
| 2587 | if (icon == null) { |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 2588 | icon = getIconFromCursor(c, iconIndex, context); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2589 | } |
| 2590 | // the fallback icon |
| 2591 | if (icon == null) { |
| 2592 | icon = getFallbackIcon(); |
| 2593 | info.usingFallbackIcon = true; |
| 2594 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2595 | break; |
| 2596 | case LauncherSettings.Favorites.ICON_TYPE_BITMAP: |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 2597 | icon = getIconFromCursor(c, iconIndex, context); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2598 | if (icon == null) { |
| 2599 | icon = getFallbackIcon(); |
| 2600 | info.customIcon = false; |
| 2601 | info.usingFallbackIcon = true; |
| 2602 | } else { |
| 2603 | info.customIcon = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2604 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2605 | break; |
| 2606 | default: |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 2607 | icon = getFallbackIcon(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2608 | info.usingFallbackIcon = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2609 | info.customIcon = false; |
| 2610 | break; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2611 | } |
Joe Onorato | d8d22da | 2010-03-11 17:59:11 -0800 | [diff] [blame] | 2612 | info.setIcon(icon); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2613 | return info; |
| 2614 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2615 | |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 2616 | Bitmap getIconFromCursor(Cursor c, int iconIndex, Context context) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 2617 | @SuppressWarnings("all") // suppress dead code warning |
| 2618 | final boolean debug = false; |
| 2619 | if (debug) { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2620 | Log.d(TAG, "getIconFromCursor app=" |
| 2621 | + c.getString(c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE))); |
| 2622 | } |
| 2623 | byte[] data = c.getBlob(iconIndex); |
| 2624 | try { |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 2625 | return Utilities.createIconBitmap( |
| 2626 | BitmapFactory.decodeByteArray(data, 0, data.length), context); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2627 | } catch (Exception e) { |
| 2628 | return null; |
| 2629 | } |
| 2630 | } |
| 2631 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2632 | ShortcutInfo addShortcut(Context context, Intent data, long container, int screen, |
| 2633 | int cellX, int cellY, boolean notify) { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2634 | final ShortcutInfo info = infoFromShortcutIntent(context, data, null); |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 2635 | if (info == null) { |
| 2636 | return null; |
| 2637 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 2638 | addItemToDatabase(context, info, container, screen, cellX, cellY, notify); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2639 | |
| 2640 | return info; |
| 2641 | } |
| 2642 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2643 | /** |
Winson Chung | 55cef26 | 2010-10-28 14:14:18 -0700 | [diff] [blame] | 2644 | * Attempts to find an AppWidgetProviderInfo that matches the given component. |
| 2645 | */ |
| 2646 | AppWidgetProviderInfo findAppWidgetProviderInfoWithComponent(Context context, |
| 2647 | ComponentName component) { |
| 2648 | List<AppWidgetProviderInfo> widgets = |
| 2649 | AppWidgetManager.getInstance(context).getInstalledProviders(); |
| 2650 | for (AppWidgetProviderInfo info : widgets) { |
| 2651 | if (info.provider.equals(component)) { |
| 2652 | return info; |
| 2653 | } |
| 2654 | } |
| 2655 | return null; |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
Winson Chung | 68846fd | 2010-10-29 11:00:27 -0700 | [diff] [blame] | 2658 | /** |
| 2659 | * Returns a list of all the widgets that can handle configuration with a particular mimeType. |
| 2660 | */ |
| 2661 | List<WidgetMimeTypeHandlerData> resolveWidgetsForMimeType(Context context, String mimeType) { |
| 2662 | final PackageManager packageManager = context.getPackageManager(); |
| 2663 | final List<WidgetMimeTypeHandlerData> supportedConfigurationActivities = |
| 2664 | new ArrayList<WidgetMimeTypeHandlerData>(); |
| 2665 | |
| 2666 | final Intent supportsIntent = |
| 2667 | new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE); |
| 2668 | supportsIntent.setType(mimeType); |
| 2669 | |
| 2670 | // Create a set of widget configuration components that we can test against |
| 2671 | final List<AppWidgetProviderInfo> widgets = |
| 2672 | AppWidgetManager.getInstance(context).getInstalledProviders(); |
| 2673 | final HashMap<ComponentName, AppWidgetProviderInfo> configurationComponentToWidget = |
| 2674 | new HashMap<ComponentName, AppWidgetProviderInfo>(); |
| 2675 | for (AppWidgetProviderInfo info : widgets) { |
| 2676 | configurationComponentToWidget.put(info.configure, info); |
| 2677 | } |
| 2678 | |
| 2679 | // Run through each of the intents that can handle this type of clip data, and cross |
| 2680 | // reference them with the components that are actual configuration components |
| 2681 | final List<ResolveInfo> activities = packageManager.queryIntentActivities(supportsIntent, |
| 2682 | PackageManager.MATCH_DEFAULT_ONLY); |
| 2683 | for (ResolveInfo info : activities) { |
| 2684 | final ActivityInfo activityInfo = info.activityInfo; |
| 2685 | final ComponentName infoComponent = new ComponentName(activityInfo.packageName, |
| 2686 | activityInfo.name); |
| 2687 | if (configurationComponentToWidget.containsKey(infoComponent)) { |
| 2688 | supportedConfigurationActivities.add( |
| 2689 | new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info, |
| 2690 | configurationComponentToWidget.get(infoComponent))); |
| 2691 | } |
| 2692 | } |
| 2693 | return supportedConfigurationActivities; |
| 2694 | } |
| 2695 | |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2696 | ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2697 | Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); |
| 2698 | String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); |
| 2699 | Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); |
| 2700 | |
Adam Cohen | d919882 | 2011-11-22 16:42:47 -0800 | [diff] [blame] | 2701 | if (intent == null) { |
| 2702 | // If the intent is null, we can't construct a valid ShortcutInfo, so we return null |
| 2703 | Log.e(TAG, "Can't construct ShorcutInfo with null intent"); |
| 2704 | return null; |
| 2705 | } |
| 2706 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2707 | Bitmap icon = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2708 | boolean customIcon = false; |
| 2709 | ShortcutIconResource iconResource = null; |
| 2710 | |
| 2711 | if (bitmap != null && bitmap instanceof Bitmap) { |
| 2712 | icon = Utilities.createIconBitmap(new FastBitmapDrawable((Bitmap)bitmap), context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2713 | customIcon = true; |
| 2714 | } else { |
| 2715 | Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); |
| 2716 | if (extra != null && extra instanceof ShortcutIconResource) { |
| 2717 | try { |
| 2718 | iconResource = (ShortcutIconResource) extra; |
| 2719 | final PackageManager packageManager = context.getPackageManager(); |
| 2720 | Resources resources = packageManager.getResourcesForApplication( |
| 2721 | iconResource.packageName); |
| 2722 | final int id = resources.getIdentifier(iconResource.resourceName, null, null); |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 2723 | icon = Utilities.createIconBitmap( |
| 2724 | mIconCache.getFullResIcon(resources, id), context); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2725 | } catch (Exception e) { |
| 2726 | Log.w(TAG, "Could not load shortcut icon: " + extra); |
| 2727 | } |
| 2728 | } |
| 2729 | } |
| 2730 | |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2731 | final ShortcutInfo info = new ShortcutInfo(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2732 | |
| 2733 | if (icon == null) { |
Winson Chung | a9abd0e | 2010-10-27 17:18:37 -0700 | [diff] [blame] | 2734 | if (fallbackIcon != null) { |
| 2735 | icon = fallbackIcon; |
| 2736 | } else { |
| 2737 | icon = getFallbackIcon(); |
| 2738 | info.usingFallbackIcon = true; |
| 2739 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2740 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2741 | info.setIcon(icon); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2742 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 2743 | info.title = name; |
| 2744 | info.intent = intent; |
| 2745 | info.customIcon = customIcon; |
| 2746 | info.iconResource = iconResource; |
| 2747 | |
| 2748 | return info; |
| 2749 | } |
| 2750 | |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 2751 | boolean queueIconToBeChecked(HashMap<Object, byte[]> cache, ShortcutInfo info, Cursor c, |
| 2752 | int iconIndex) { |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 2753 | // If apps can't be on SD, don't even bother. |
| 2754 | if (!mAppsCanBeOnExternalStorage) { |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 2755 | return false; |
Joe Onorato | 17a8922 | 2011-02-08 17:26:11 -0800 | [diff] [blame] | 2756 | } |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2757 | // If this icon doesn't have a custom icon, check to see |
| 2758 | // what's stored in the DB, and if it doesn't match what |
| 2759 | // we're going to show, store what we are going to show back |
| 2760 | // into the DB. We do this so when we're loading, if the |
| 2761 | // package manager can't find an icon (for example because |
| 2762 | // the app is on SD) then we can use that instead. |
Joe Onorato | ddc9c1f | 2010-08-30 18:30:15 -0700 | [diff] [blame] | 2763 | if (!info.customIcon && !info.usingFallbackIcon) { |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 2764 | cache.put(info, c.getBlob(iconIndex)); |
| 2765 | return true; |
| 2766 | } |
| 2767 | return false; |
| 2768 | } |
| 2769 | void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) { |
| 2770 | boolean needSave = false; |
| 2771 | try { |
| 2772 | if (data != null) { |
| 2773 | Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length); |
| 2774 | Bitmap loaded = info.getIcon(mIconCache); |
| 2775 | needSave = !saved.sameAs(loaded); |
| 2776 | } else { |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2777 | needSave = true; |
| 2778 | } |
Winson Chung | aac01e1 | 2011-08-17 10:37:13 -0700 | [diff] [blame] | 2779 | } catch (Exception e) { |
| 2780 | needSave = true; |
| 2781 | } |
| 2782 | if (needSave) { |
| 2783 | Log.d(TAG, "going to save icon bitmap for info=" + info); |
| 2784 | // This is slower than is ideal, but this only happens once |
| 2785 | // or when the app is updated with a new icon. |
| 2786 | updateItemInDatabase(context, info); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2790 | /** |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 2791 | * Return an existing FolderInfo object if we have encountered this ID previously, |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2792 | * or make a new one. |
| 2793 | */ |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 2794 | private static FolderInfo findOrMakeFolder(HashMap<Long, FolderInfo> folders, long id) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2795 | // See if a placeholder was created for us already |
| 2796 | FolderInfo folderInfo = folders.get(id); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 2797 | if (folderInfo == null) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2798 | // No placeholder -- create a new instance |
Michael Jurka | c9d95c5 | 2011-08-29 14:03:34 -0700 | [diff] [blame] | 2799 | folderInfo = new FolderInfo(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 2800 | folders.put(id, folderInfo); |
| 2801 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 2802 | return folderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2803 | } |
| 2804 | |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2805 | public static final Comparator<ApplicationInfo> getAppNameComparator() { |
| 2806 | final Collator collator = Collator.getInstance(); |
| 2807 | return new Comparator<ApplicationInfo>() { |
| 2808 | public final int compare(ApplicationInfo a, ApplicationInfo b) { |
| 2809 | int result = collator.compare(a.title.toString(), b.title.toString()); |
| 2810 | if (result == 0) { |
| 2811 | result = a.componentName.compareTo(b.componentName); |
| 2812 | } |
| 2813 | return result; |
Michael Jurka | 5b1808d | 2011-07-11 19:59:46 -0700 | [diff] [blame] | 2814 | } |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2815 | }; |
| 2816 | } |
Winson Chung | 78403fe | 2011-01-21 15:38:02 -0800 | [diff] [blame] | 2817 | public static final Comparator<ApplicationInfo> APP_INSTALL_TIME_COMPARATOR |
| 2818 | = new Comparator<ApplicationInfo>() { |
| 2819 | public final int compare(ApplicationInfo a, ApplicationInfo b) { |
| 2820 | if (a.firstInstallTime < b.firstInstallTime) return 1; |
| 2821 | if (a.firstInstallTime > b.firstInstallTime) return -1; |
| 2822 | return 0; |
| 2823 | } |
| 2824 | }; |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2825 | public static final Comparator<AppWidgetProviderInfo> getWidgetNameComparator() { |
| 2826 | final Collator collator = Collator.getInstance(); |
| 2827 | return new Comparator<AppWidgetProviderInfo>() { |
| 2828 | public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) { |
| 2829 | return collator.compare(a.label.toString(), b.label.toString()); |
| 2830 | } |
| 2831 | }; |
| 2832 | } |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2833 | static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) { |
| 2834 | if (info.activityInfo != null) { |
| 2835 | return new ComponentName(info.activityInfo.packageName, info.activityInfo.name); |
| 2836 | } else { |
| 2837 | return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name); |
| 2838 | } |
| 2839 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2840 | public static class ShortcutNameComparator implements Comparator<ResolveInfo> { |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2841 | private Collator mCollator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2842 | private PackageManager mPackageManager; |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2843 | private HashMap<Object, CharSequence> mLabelCache; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2844 | ShortcutNameComparator(PackageManager pm) { |
| 2845 | mPackageManager = pm; |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2846 | mLabelCache = new HashMap<Object, CharSequence>(); |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2847 | mCollator = Collator.getInstance(); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2848 | } |
| 2849 | ShortcutNameComparator(PackageManager pm, HashMap<Object, CharSequence> labelCache) { |
| 2850 | mPackageManager = pm; |
| 2851 | mLabelCache = labelCache; |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2852 | mCollator = Collator.getInstance(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2853 | } |
| 2854 | public final int compare(ResolveInfo a, ResolveInfo b) { |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2855 | CharSequence labelA, labelB; |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2856 | ComponentName keyA = LauncherModel.getComponentNameFromResolveInfo(a); |
| 2857 | ComponentName keyB = LauncherModel.getComponentNameFromResolveInfo(b); |
| 2858 | if (mLabelCache.containsKey(keyA)) { |
| 2859 | labelA = mLabelCache.get(keyA); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2860 | } else { |
| 2861 | labelA = a.loadLabel(mPackageManager).toString(); |
| 2862 | |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2863 | mLabelCache.put(keyA, labelA); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2864 | } |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2865 | if (mLabelCache.containsKey(keyB)) { |
| 2866 | labelB = mLabelCache.get(keyB); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2867 | } else { |
| 2868 | labelB = b.loadLabel(mPackageManager).toString(); |
| 2869 | |
Winson Chung | 5308f24 | 2011-08-18 12:12:41 -0700 | [diff] [blame] | 2870 | mLabelCache.put(keyB, labelB); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2871 | } |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2872 | return mCollator.compare(labelA, labelB); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2873 | } |
| 2874 | }; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2875 | public static class WidgetAndShortcutNameComparator implements Comparator<Object> { |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2876 | private Collator mCollator; |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2877 | private PackageManager mPackageManager; |
| 2878 | private HashMap<Object, String> mLabelCache; |
| 2879 | WidgetAndShortcutNameComparator(PackageManager pm) { |
| 2880 | mPackageManager = pm; |
| 2881 | mLabelCache = new HashMap<Object, String>(); |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2882 | mCollator = Collator.getInstance(); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2883 | } |
| 2884 | public final int compare(Object a, Object b) { |
| 2885 | String labelA, labelB; |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2886 | if (mLabelCache.containsKey(a)) { |
| 2887 | labelA = mLabelCache.get(a); |
| 2888 | } else { |
| 2889 | labelA = (a instanceof AppWidgetProviderInfo) ? |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2890 | ((AppWidgetProviderInfo) a).label : |
| 2891 | ((ResolveInfo) a).loadLabel(mPackageManager).toString(); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2892 | mLabelCache.put(a, labelA); |
| 2893 | } |
| 2894 | if (mLabelCache.containsKey(b)) { |
| 2895 | labelB = mLabelCache.get(b); |
| 2896 | } else { |
| 2897 | labelB = (b instanceof AppWidgetProviderInfo) ? |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2898 | ((AppWidgetProviderInfo) b).label : |
| 2899 | ((ResolveInfo) b).loadLabel(mPackageManager).toString(); |
Winson Chung | c3eecff | 2011-07-11 17:44:15 -0700 | [diff] [blame] | 2900 | mLabelCache.put(b, labelB); |
| 2901 | } |
Winson Chung | 1190487 | 2012-09-17 16:58:46 -0700 | [diff] [blame] | 2902 | return mCollator.compare(labelA, labelB); |
Winson Chung | 1ed747a | 2011-05-03 16:18:34 -0700 | [diff] [blame] | 2903 | } |
| 2904 | }; |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2905 | |
| 2906 | public void dumpState() { |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2907 | Log.d(TAG, "mCallbacks=" + mCallbacks); |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 2908 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.data", mBgAllAppsList.data); |
| 2909 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.added", mBgAllAppsList.added); |
| 2910 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.removed", mBgAllAppsList.removed); |
| 2911 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList.modified", mBgAllAppsList.modified); |
Joe Onorato | 3611578 | 2010-06-17 13:28:48 -0400 | [diff] [blame] | 2912 | if (mLoaderTask != null) { |
| 2913 | mLoaderTask.dumpState(); |
| 2914 | } else { |
| 2915 | Log.d(TAG, "mLoaderTask=null"); |
| 2916 | } |
Joe Onorato | be38609 | 2009-11-17 17:32:16 -0800 | [diff] [blame] | 2917 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 2918 | } |