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