Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | package com.android.systemui.recent; |
| 18 | |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 20 | import android.app.AppGlobals; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.ActivityInfo; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 25 | import android.content.pm.IPackageManager; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 30 | import android.graphics.drawable.BitmapDrawable; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.os.AsyncTask; |
| 33 | import android.os.Handler; |
| 34 | import android.os.Process; |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 35 | import android.os.RemoteException; |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 36 | import android.os.UserHandle; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 38 | import android.view.MotionEvent; |
| 39 | import android.view.View; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 40 | |
| 41 | import com.android.systemui.R; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 42 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.phone.PhoneStatusBar; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 44 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | import java.util.concurrent.BlockingQueue; |
| 48 | import java.util.concurrent.LinkedBlockingQueue; |
| 49 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 50 | public class RecentTasksLoader implements View.OnTouchListener { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 51 | static final String TAG = "RecentTasksLoader"; |
John Spurlock | b0e49fc | 2013-06-12 15:50:50 -0400 | [diff] [blame] | 52 | static final boolean DEBUG = PhoneStatusBar.DEBUG || false; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 53 | |
| 54 | private static final int DISPLAY_TASKS = 20; |
| 55 | private static final int MAX_TASKS = DISPLAY_TASKS + 1; // allow extra for non-apps |
| 56 | |
| 57 | private Context mContext; |
| 58 | private RecentsPanelView mRecentsPanel; |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 59 | |
| 60 | private Object mFirstTaskLock = new Object(); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 61 | private TaskDescription mFirstTask; |
| 62 | private boolean mFirstTaskLoaded; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 63 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 64 | private AsyncTask<Void, ArrayList<TaskDescription>, Void> mTaskLoader; |
| 65 | private AsyncTask<Void, TaskDescription, Void> mThumbnailLoader; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 66 | private Handler mHandler; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 67 | |
| 68 | private int mIconDpi; |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 69 | private ColorDrawableWithDimensions mDefaultThumbnailBackground; |
| 70 | private ColorDrawableWithDimensions mDefaultIconBackground; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 71 | private int mNumTasksInFirstScreenful = Integer.MAX_VALUE; |
| 72 | |
| 73 | private boolean mFirstScreenful; |
| 74 | private ArrayList<TaskDescription> mLoadedTasks; |
| 75 | |
| 76 | private enum State { LOADING, LOADED, CANCELLED }; |
| 77 | private State mState = State.CANCELLED; |
| 78 | |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 79 | |
| 80 | private static RecentTasksLoader sInstance; |
| 81 | public static RecentTasksLoader getInstance(Context context) { |
| 82 | if (sInstance == null) { |
| 83 | sInstance = new RecentTasksLoader(context); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 84 | } |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 85 | return sInstance; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 86 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 87 | |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 88 | private RecentTasksLoader(Context context) { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 89 | mContext = context; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 90 | mHandler = new Handler(); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 91 | |
| 92 | final Resources res = context.getResources(); |
| 93 | |
| 94 | // get the icon size we want -- on tablets, we use bigger icons |
| 95 | boolean isTablet = res.getBoolean(R.bool.config_recents_interface_for_tablets); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 96 | if (isTablet) { |
Winson Chung | 43e34f6 | 2012-01-24 15:26:59 -0800 | [diff] [blame] | 97 | ActivityManager activityManager = |
| 98 | (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| 99 | mIconDpi = activityManager.getLauncherLargeIconDensity(); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 100 | } else { |
| 101 | mIconDpi = res.getDisplayMetrics().densityDpi; |
| 102 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 103 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 104 | // Render default icon (just a blank image) |
| 105 | int defaultIconSize = res.getDimensionPixelSize(com.android.internal.R.dimen.app_icon_size); |
| 106 | int iconSize = (int) (defaultIconSize * mIconDpi / res.getDisplayMetrics().densityDpi); |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 107 | mDefaultIconBackground = new ColorDrawableWithDimensions(0x00000000, iconSize, iconSize); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 108 | |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 109 | // Render the default thumbnail background |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 110 | int thumbnailWidth = |
| 111 | (int) res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width); |
| 112 | int thumbnailHeight = |
| 113 | (int) res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 114 | int color = res.getColor(R.drawable.status_bar_recents_app_thumbnail_background); |
| 115 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 116 | mDefaultThumbnailBackground = |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 117 | new ColorDrawableWithDimensions(color, thumbnailWidth, thumbnailHeight); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 120 | public void setRecentsPanel(RecentsPanelView newRecentsPanel, RecentsPanelView caller) { |
| 121 | // Only allow clearing mRecentsPanel if the caller is the current recentsPanel |
| 122 | if (newRecentsPanel != null || mRecentsPanel == caller) { |
| 123 | mRecentsPanel = newRecentsPanel; |
| 124 | if (mRecentsPanel != null) { |
| 125 | mNumTasksInFirstScreenful = mRecentsPanel.numItemsInOneScreenful(); |
| 126 | } |
| 127 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 130 | public Drawable getDefaultThumbnail() { |
Michael Jurka | 412cba8 | 2011-10-17 09:05:00 -0700 | [diff] [blame] | 131 | return mDefaultThumbnailBackground; |
| 132 | } |
| 133 | |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 134 | public Drawable getDefaultIcon() { |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 135 | return mDefaultIconBackground; |
| 136 | } |
| 137 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 138 | public ArrayList<TaskDescription> getLoadedTasks() { |
| 139 | return mLoadedTasks; |
| 140 | } |
| 141 | |
Michael Jurka | e57922c | 2012-11-26 16:05:12 -0800 | [diff] [blame] | 142 | public void remove(TaskDescription td) { |
| 143 | mLoadedTasks.remove(td); |
| 144 | } |
| 145 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 146 | public boolean isFirstScreenful() { |
| 147 | return mFirstScreenful; |
| 148 | } |
| 149 | |
| 150 | private boolean isCurrentHomeActivity(ComponentName component, ActivityInfo homeInfo) { |
| 151 | if (homeInfo == null) { |
| 152 | final PackageManager pm = mContext.getPackageManager(); |
| 153 | homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) |
| 154 | .resolveActivityInfo(pm, 0); |
| 155 | } |
| 156 | return homeInfo != null |
| 157 | && homeInfo.packageName.equals(component.getPackageName()) |
| 158 | && homeInfo.name.equals(component.getClassName()); |
| 159 | } |
| 160 | |
| 161 | // Create an TaskDescription, returning null if the title or icon is null |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 162 | TaskDescription createTaskDescription(int taskId, int persistentTaskId, Intent baseIntent, |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 163 | ComponentName origActivity, CharSequence description, int userId) { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 164 | Intent intent = new Intent(baseIntent); |
| 165 | if (origActivity != null) { |
| 166 | intent.setComponent(origActivity); |
| 167 | } |
| 168 | final PackageManager pm = mContext.getPackageManager(); |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 169 | final IPackageManager ipm = AppGlobals.getPackageManager(); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 170 | intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) |
| 171 | | Intent.FLAG_ACTIVITY_NEW_TASK); |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 172 | ResolveInfo resolveInfo = null; |
| 173 | try { |
| 174 | resolveInfo = ipm.resolveIntent(intent, null, 0, userId); |
| 175 | } catch (RemoteException re) { |
| 176 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 177 | if (resolveInfo != null) { |
| 178 | final ActivityInfo info = resolveInfo.activityInfo; |
| 179 | final String title = info.loadLabel(pm).toString(); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 180 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 181 | if (title != null && title.length() > 0) { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 182 | if (DEBUG) Log.v(TAG, "creating activity desc for id=" |
| 183 | + persistentTaskId + ", label=" + title); |
| 184 | |
| 185 | TaskDescription item = new TaskDescription(taskId, |
| 186 | persistentTaskId, resolveInfo, baseIntent, info.packageName, |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 187 | description, userId); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 188 | item.setLabel(title); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 189 | |
| 190 | return item; |
| 191 | } else { |
| 192 | if (DEBUG) Log.v(TAG, "SKIPPING item " + persistentTaskId); |
| 193 | } |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 198 | void loadThumbnailAndIcon(TaskDescription td) { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 199 | final ActivityManager am = (ActivityManager) |
| 200 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 201 | final PackageManager pm = mContext.getPackageManager(); |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 202 | final Bitmap thumbnail = SystemServicesProxy.getThumbnail(am, td.persistentTaskId); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 203 | Drawable icon = getFullResIcon(td.resolveInfo, pm); |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 204 | if (td.userId != UserHandle.myUserId()) { |
| 205 | // Need to badge the icon |
Amith Yamasani | fdf169c | 2014-09-07 15:45:57 -0700 | [diff] [blame] | 206 | icon = mContext.getPackageManager().getUserBadgedIcon(icon, new UserHandle(td.userId)); |
Amith Yamasani | 4f0a49e | 2014-04-10 13:48:15 -0700 | [diff] [blame] | 207 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 208 | if (DEBUG) Log.v(TAG, "Loaded bitmap for task " |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 209 | + td + ": " + thumbnail); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 210 | synchronized (td) { |
Dianne Hackborn | 15491c6 | 2012-09-19 10:59:14 -0700 | [diff] [blame] | 211 | if (thumbnail != null) { |
Michael Jurka | 9926272 | 2013-09-17 14:18:04 +0200 | [diff] [blame] | 212 | td.setThumbnail(new BitmapDrawable(mContext.getResources(), thumbnail)); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 213 | } else { |
| 214 | td.setThumbnail(mDefaultThumbnailBackground); |
| 215 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 216 | if (icon != null) { |
| 217 | td.setIcon(icon); |
| 218 | } |
| 219 | td.setLoaded(true); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | Drawable getFullResDefaultActivityIcon() { |
| 224 | return getFullResIcon(Resources.getSystem(), |
| 225 | com.android.internal.R.mipmap.sym_def_app_icon); |
| 226 | } |
| 227 | |
| 228 | Drawable getFullResIcon(Resources resources, int iconId) { |
| 229 | try { |
| 230 | return resources.getDrawableForDensity(iconId, mIconDpi); |
| 231 | } catch (Resources.NotFoundException e) { |
| 232 | return getFullResDefaultActivityIcon(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) { |
| 237 | Resources resources; |
| 238 | try { |
| 239 | resources = packageManager.getResourcesForApplication( |
| 240 | info.activityInfo.applicationInfo); |
| 241 | } catch (PackageManager.NameNotFoundException e) { |
| 242 | resources = null; |
| 243 | } |
| 244 | if (resources != null) { |
| 245 | int iconId = info.activityInfo.getIconResource(); |
| 246 | if (iconId != 0) { |
| 247 | return getFullResIcon(resources, iconId); |
| 248 | } |
| 249 | } |
| 250 | return getFullResDefaultActivityIcon(); |
| 251 | } |
| 252 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 253 | Runnable mPreloadTasksRunnable = new Runnable() { |
| 254 | public void run() { |
| 255 | loadTasksInBackground(); |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | // additional optimization when we have software system buttons - start loading the recent |
| 260 | // tasks on touch down |
| 261 | @Override |
| 262 | public boolean onTouch(View v, MotionEvent ev) { |
| 263 | int action = ev.getAction() & MotionEvent.ACTION_MASK; |
| 264 | if (action == MotionEvent.ACTION_DOWN) { |
Michael Jurka | d0d4bb8 | 2012-09-04 06:25:50 -0700 | [diff] [blame] | 265 | preloadRecentTasksList(); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 266 | } else if (action == MotionEvent.ACTION_CANCEL) { |
Michael Jurka | d0d4bb8 | 2012-09-04 06:25:50 -0700 | [diff] [blame] | 267 | cancelPreloadingRecentTasksList(); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 268 | } else if (action == MotionEvent.ACTION_UP) { |
| 269 | // Remove the preloader if we haven't called it yet |
| 270 | mHandler.removeCallbacks(mPreloadTasksRunnable); |
| 271 | if (!v.isPressed()) { |
| 272 | cancelLoadingThumbnailsAndIcons(); |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | |
Michael Jurka | d0d4bb8 | 2012-09-04 06:25:50 -0700 | [diff] [blame] | 279 | public void preloadRecentTasksList() { |
| 280 | mHandler.post(mPreloadTasksRunnable); |
| 281 | } |
| 282 | |
| 283 | public void cancelPreloadingRecentTasksList() { |
| 284 | cancelLoadingThumbnailsAndIcons(); |
| 285 | mHandler.removeCallbacks(mPreloadTasksRunnable); |
| 286 | } |
| 287 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 288 | public void cancelLoadingThumbnailsAndIcons(RecentsPanelView caller) { |
| 289 | // Only oblige this request if it comes from the current RecentsPanel |
| 290 | // (eg when you rotate, the old RecentsPanel request should be ignored) |
| 291 | if (mRecentsPanel == caller) { |
| 292 | cancelLoadingThumbnailsAndIcons(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | private void cancelLoadingThumbnailsAndIcons() { |
Michael Jurka | 45eed3c | 2013-05-02 14:49:45 +0200 | [diff] [blame] | 298 | if (mRecentsPanel != null && mRecentsPanel.isShowing()) { |
| 299 | return; |
| 300 | } |
| 301 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 302 | if (mTaskLoader != null) { |
| 303 | mTaskLoader.cancel(false); |
| 304 | mTaskLoader = null; |
| 305 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 306 | if (mThumbnailLoader != null) { |
| 307 | mThumbnailLoader.cancel(false); |
| 308 | mThumbnailLoader = null; |
| 309 | } |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 310 | mLoadedTasks = null; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 311 | if (mRecentsPanel != null) { |
| 312 | mRecentsPanel.onTaskLoadingCancelled(); |
| 313 | } |
| 314 | mFirstScreenful = false; |
| 315 | mState = State.CANCELLED; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 318 | private void clearFirstTask() { |
| 319 | synchronized (mFirstTaskLock) { |
| 320 | mFirstTask = null; |
| 321 | mFirstTaskLoaded = false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | public void preloadFirstTask() { |
| 326 | Thread bgLoad = new Thread() { |
| 327 | public void run() { |
| 328 | TaskDescription first = loadFirstTask(); |
| 329 | synchronized(mFirstTaskLock) { |
| 330 | if (mCancelPreloadingFirstTask) { |
| 331 | clearFirstTask(); |
| 332 | } else { |
| 333 | mFirstTask = first; |
| 334 | mFirstTaskLoaded = true; |
| 335 | } |
| 336 | mPreloadingFirstTask = false; |
| 337 | } |
| 338 | } |
| 339 | }; |
| 340 | synchronized(mFirstTaskLock) { |
| 341 | if (!mPreloadingFirstTask) { |
| 342 | clearFirstTask(); |
| 343 | mPreloadingFirstTask = true; |
| 344 | bgLoad.start(); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | public void cancelPreloadingFirstTask() { |
| 350 | synchronized(mFirstTaskLock) { |
| 351 | if (mPreloadingFirstTask) { |
| 352 | mCancelPreloadingFirstTask = true; |
| 353 | } else { |
| 354 | clearFirstTask(); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | boolean mPreloadingFirstTask; |
| 360 | boolean mCancelPreloadingFirstTask; |
| 361 | public TaskDescription getFirstTask() { |
| 362 | while(true) { |
| 363 | synchronized(mFirstTaskLock) { |
| 364 | if (mFirstTaskLoaded) { |
| 365 | return mFirstTask; |
| 366 | } else if (!mFirstTaskLoaded && !mPreloadingFirstTask) { |
| 367 | mFirstTask = loadFirstTask(); |
| 368 | mFirstTaskLoaded = true; |
| 369 | return mFirstTask; |
| 370 | } |
| 371 | } |
| 372 | try { |
| 373 | Thread.sleep(3); |
| 374 | } catch (InterruptedException e) { |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | public TaskDescription loadFirstTask() { |
| 380 | final ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 381 | |
Kenny Guy | 82326a9 | 2014-03-17 17:16:06 +0000 | [diff] [blame] | 382 | final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasksForUser(1, |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 383 | ActivityManager.RECENT_IGNORE_UNAVAILABLE | ActivityManager.RECENT_INCLUDE_PROFILES, |
Kenny Guy | 82326a9 | 2014-03-17 17:16:06 +0000 | [diff] [blame] | 384 | UserHandle.CURRENT.getIdentifier()); |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 385 | TaskDescription item = null; |
| 386 | if (recentTasks.size() > 0) { |
| 387 | ActivityManager.RecentTaskInfo recentInfo = recentTasks.get(0); |
| 388 | |
| 389 | Intent intent = new Intent(recentInfo.baseIntent); |
| 390 | if (recentInfo.origActivity != null) { |
| 391 | intent.setComponent(recentInfo.origActivity); |
| 392 | } |
| 393 | |
| 394 | // Don't load the current home activity. |
| 395 | if (isCurrentHomeActivity(intent.getComponent(), null)) { |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | // Don't load ourselves |
| 400 | if (intent.getComponent().getPackageName().equals(mContext.getPackageName())) { |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | item = createTaskDescription(recentInfo.id, |
| 405 | recentInfo.persistentId, recentInfo.baseIntent, |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 406 | recentInfo.origActivity, recentInfo.description, |
| 407 | recentInfo.userId); |
Michael Jurka | 2a430cc | 2012-10-22 10:56:18 -0700 | [diff] [blame] | 408 | if (item != null) { |
| 409 | loadThumbnailAndIcon(item); |
| 410 | } |
Michael Jurka | 80343f6 | 2012-10-18 13:13:46 +0200 | [diff] [blame] | 411 | return item; |
| 412 | } |
| 413 | return null; |
| 414 | } |
| 415 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 416 | public void loadTasksInBackground() { |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 417 | loadTasksInBackground(false); |
| 418 | } |
| 419 | public void loadTasksInBackground(final boolean zeroeth) { |
| 420 | if (mState != State.CANCELLED) { |
| 421 | return; |
| 422 | } |
| 423 | mState = State.LOADING; |
| 424 | mFirstScreenful = true; |
| 425 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 426 | final LinkedBlockingQueue<TaskDescription> tasksWaitingForThumbnails = |
| 427 | new LinkedBlockingQueue<TaskDescription>(); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 428 | mTaskLoader = new AsyncTask<Void, ArrayList<TaskDescription>, Void>() { |
| 429 | @Override |
| 430 | protected void onProgressUpdate(ArrayList<TaskDescription>... values) { |
| 431 | if (!isCancelled()) { |
| 432 | ArrayList<TaskDescription> newTasks = values[0]; |
| 433 | // do a callback to RecentsPanelView to let it know we have more values |
| 434 | // how do we let it know we're all done? just always call back twice |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 435 | if (mRecentsPanel != null) { |
| 436 | mRecentsPanel.onTasksLoaded(newTasks, mFirstScreenful); |
| 437 | } |
| 438 | if (mLoadedTasks == null) { |
| 439 | mLoadedTasks = new ArrayList<TaskDescription>(); |
| 440 | } |
| 441 | mLoadedTasks.addAll(newTasks); |
| 442 | mFirstScreenful = false; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | @Override |
| 446 | protected Void doInBackground(Void... params) { |
| 447 | // We load in two stages: first, we update progress with just the first screenful |
| 448 | // of items. Then, we update with the rest of the items |
| 449 | final int origPri = Process.getThreadPriority(Process.myTid()); |
Glenn Kasten | 252030b | 2012-03-15 16:34:01 -0700 | [diff] [blame] | 450 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 451 | final PackageManager pm = mContext.getPackageManager(); |
| 452 | final ActivityManager am = (ActivityManager) |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 453 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 454 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 455 | final List<ActivityManager.RecentTaskInfo> recentTasks = |
Kenny Guy | 82326a9 | 2014-03-17 17:16:06 +0000 | [diff] [blame] | 456 | am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 457 | | ActivityManager.RECENT_INCLUDE_PROFILES); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 458 | int numTasks = recentTasks.size(); |
| 459 | ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN) |
| 460 | .addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 461 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 462 | boolean firstScreenful = true; |
| 463 | ArrayList<TaskDescription> tasks = new ArrayList<TaskDescription>(); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 464 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 465 | // skip the first task - assume it's either the home screen or the current activity. |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 466 | final int first = 0; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 467 | for (int i = first, index = 0; i < numTasks && (index < MAX_TASKS); ++i) { |
| 468 | if (isCancelled()) { |
| 469 | break; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 470 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 471 | final ActivityManager.RecentTaskInfo recentInfo = recentTasks.get(i); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 472 | |
| 473 | Intent intent = new Intent(recentInfo.baseIntent); |
| 474 | if (recentInfo.origActivity != null) { |
| 475 | intent.setComponent(recentInfo.origActivity); |
| 476 | } |
| 477 | |
| 478 | // Don't load the current home activity. |
| 479 | if (isCurrentHomeActivity(intent.getComponent(), homeInfo)) { |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 480 | continue; |
| 481 | } |
| 482 | |
| 483 | // Don't load ourselves |
| 484 | if (intent.getComponent().getPackageName().equals(mContext.getPackageName())) { |
| 485 | continue; |
| 486 | } |
| 487 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 488 | TaskDescription item = createTaskDescription(recentInfo.id, |
| 489 | recentInfo.persistentId, recentInfo.baseIntent, |
Kenny Guy | 1f1cce1 | 2014-04-04 14:47:50 +0100 | [diff] [blame] | 490 | recentInfo.origActivity, recentInfo.description, |
| 491 | recentInfo.userId); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 492 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 493 | if (item != null) { |
| 494 | while (true) { |
| 495 | try { |
| 496 | tasksWaitingForThumbnails.put(item); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 497 | break; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 498 | } catch (InterruptedException e) { |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 501 | tasks.add(item); |
| 502 | if (firstScreenful && tasks.size() == mNumTasksInFirstScreenful) { |
| 503 | publishProgress(tasks); |
| 504 | tasks = new ArrayList<TaskDescription>(); |
| 505 | firstScreenful = false; |
| 506 | //break; |
| 507 | } |
| 508 | ++index; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 509 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | if (!isCancelled()) { |
| 513 | publishProgress(tasks); |
| 514 | if (firstScreenful) { |
| 515 | // always should publish two updates |
| 516 | publishProgress(new ArrayList<TaskDescription>()); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | while (true) { |
| 521 | try { |
| 522 | tasksWaitingForThumbnails.put(new TaskDescription()); |
| 523 | break; |
| 524 | } catch (InterruptedException e) { |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | Process.setThreadPriority(origPri); |
| 529 | return null; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 530 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 531 | }; |
| 532 | mTaskLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 533 | loadThumbnailsAndIconsInBackground(tasksWaitingForThumbnails); |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 536 | private void loadThumbnailsAndIconsInBackground( |
| 537 | final BlockingQueue<TaskDescription> tasksWaitingForThumbnails) { |
| 538 | // continually read items from tasksWaitingForThumbnails and load |
| 539 | // thumbnails and icons for them. finish thread when cancelled or there |
| 540 | // is a null item in tasksWaitingForThumbnails |
| 541 | mThumbnailLoader = new AsyncTask<Void, TaskDescription, Void>() { |
| 542 | @Override |
| 543 | protected void onProgressUpdate(TaskDescription... values) { |
| 544 | if (!isCancelled()) { |
| 545 | TaskDescription td = values[0]; |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 546 | if (td.isNull()) { // end sentinel |
| 547 | mState = State.LOADED; |
| 548 | } else { |
| 549 | if (mRecentsPanel != null) { |
| 550 | mRecentsPanel.onTaskThumbnailLoaded(td); |
| 551 | } |
| 552 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | @Override |
| 556 | protected Void doInBackground(Void... params) { |
| 557 | final int origPri = Process.getThreadPriority(Process.myTid()); |
Glenn Kasten | 252030b | 2012-03-15 16:34:01 -0700 | [diff] [blame] | 558 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 559 | |
| 560 | while (true) { |
| 561 | if (isCancelled()) { |
| 562 | break; |
| 563 | } |
| 564 | TaskDescription td = null; |
| 565 | while (td == null) { |
| 566 | try { |
| 567 | td = tasksWaitingForThumbnails.take(); |
| 568 | } catch (InterruptedException e) { |
| 569 | } |
| 570 | } |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 571 | if (td.isNull()) { // end sentinel |
| 572 | publishProgress(td); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | loadThumbnailAndIcon(td); |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 576 | |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 577 | publishProgress(td); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | Process.setThreadPriority(origPri); |
| 581 | return null; |
| 582 | } |
| 583 | }; |
| 584 | mThumbnailLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 585 | } |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 586 | } |