最近由于工作需要,需要重新定制Recents部分的UI。recents就是长按home键后弹出的最近应用列表。为了能更好的理解这部分代码,可以使用Monitor工具看一下这个应用的view结构图。可以在android studio的tools下的Android下,打开Android device Monitor工具。然后连接上设备,USB或者网线都可以,网线的话要使用adb connect命令连接上设备。连接上后长按Home键打开最近应用,在Monitor中选择hierarchy view,然后可以在左边选择要查看的应用,选择我们的systemui.recents.RecentsActivity,view结构图就出来了。如下图所示:
下面是对对于源码的分析,源码在framework/base/packages/Systemui,recents的入口是RecentsActivity,所以从RecentsActivity的onCreate开始分析。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// For the non-primary user, ensure that the SystemServicesProxy and configuration is
// initialized
RecentsTaskLoader.initialize(this);
SystemServicesProxy ssp = RecentsTaskLoader.getInstance().getSystemServicesProxy();
RecentsActivity的OnCreate一开始就调用RecentsTaskLoader.initialize(this);这部分整个后台应用显示的入口了。这里就出现了两个很重要的核心类:RecentsTaskLoader和SystemServicesProxy。RecentsTaskLoader是全局的最近任务的装载器类,它内部会创建SystemServicesProxy,并且在需要的时候返回给类的实例。RecentsActivity里面有这样几个重要的的函数:
getAndUpdateActivityIcon
getAndUpdateThumbnail等
这几个函数从名字上看可以知道是得到最近应用的Icon,thumbnail等的,那是怎么获得的呢?
以这个函数为例:
/** Returns the bitmap using as many cached values as we can. */
public Bitmap getAndUpdateThumbnail(Task.TaskKey taskKey, SystemServicesProxy ssp,
boolean loadIfNotCached) {
// Return the cached thumbnail if it exists
Bitmap thumbnail = mThumbnailCache.getAndInvalidateIfModified(taskKey);
if (thumbnail != null) {
return