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