本来只打算写一篇,后来发现这一块实在太大。关系也比较复杂。只好分成一系列,内容各有侧重,主要是研究ActivityThread, Activity,Window,WindowManager,ViewRoot之间初始化各是怎么衔接起来的。还有一些内容特别是Native的部分还没弄清楚,所以是从ActivityThread类开始的。
之前在跟事件的分发机制时,是从WindowManagerImpl的addView开始的。跟完了以后,忽然又发现有个地方连接不上,那就是addView是谁调用的?因此本篇博文的主旨,是想弄清楚这个问题,把这个流程讲述清楚。
OK,下面直奔主题:
Activity的启动是由ActivityThread类直接控制的
源码路径:framework/base/core/java/android/app/ActivityThread.java
源码中国链接:http://www.oschina.net/code/explore/android-2.2-froyo/android/app/ActivityThread.java
ActivityThread类里有一个main函数,是Java进程的启动函数
public static void main(String[] args)里面调用了同样在ActivityThread类里的attach方法
private void attach(boolean system)
这里都不贴内容了,大家可以自己看源码。整个详细的过程可以参看老罗的博客Android应用程序启动过程源代码分析,另外他的博客也有很多详尽的分析源码的文章。
继续正题。我们来看看下面这个类H,,它是ActivityThread的内部类:
private class H extends Handler { ... public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what); switch (msg.what) { case LAUNCH_ACTIVITY: { ActivityClientRecord r = (ActivityClientRecord)msg.obj; r.packageInfo = getPackageInfoNoCheck( r.activityInfo.applicationInfo, r.compatInfo); handleLaunchActivity(r, null); } break; } } }H就是一个Handler,里面定义了一堆消息ID的定义,以及对各种消息的处理。我们就从这个LAUNCH_ACTIVITY消息的处理开始。
至于在这之前android所做的工作,请参考上面那篇博文有详细的说明。
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); if (r.profileFd != null) { mProfiler.setProfiler(r.profileFile, r.profileFd); mProfiler.startProfiling(); mProfiler.autoStopProfiler = r.autoStopProfiler; } // Make sure we are running with the most recent config. handleConfigurationChanged(null, null); if (localLOGV) Slog.v( TAG, "Handling launch of " + r); Activity a = performLaunchActivity(r, customIntent); if (a != null) { r.createdConfig = new Configuration(mConfiguration); Bundle oldState = r.state; handleResumeActivity(r.token, false, r.isForward); ... }
可以看到里面先后有调用performLaunchActivity和handleResumeActivity函数。这里先忽略performLaunchActivity函数,我只是想知道WindowManagerImpl的addView是哪个地方调用的?来看看handleResumeActivity
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); ActivityClientRecord r = performResumeActivity(token, clearHide); if (r != null) { final Activity a = r.activity; ... if (r.window == null && !a.mFinished && willBeVisible) { r.window = r.activity.getWindow(); View decor = r.window.getDecorView(); decor.setVisibility(View.INVISIBLE); ViewManager wm = a.getWindowManager(); WindowManager.LayoutParams l = r.window.getAttributes(); a.mDecor = decor; l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; wm.addView(decor, l); } // If the window has already been added, but during resume // we started another activity, then don't yet make the // window visible. } ... } }可以看到里面有获取WindowManager并且调用了WindowManager对象的addView函数。终于和WindowManager这条线连接起来了。从addView函数开始,又是另外一条线,将与ViewRoot联系起来,这将在我的下一篇博文里讲述。
答案就在ActivityThread这里。继续往下走,看看Activity的getWindowManager()的内容:
/** Retrieve the window manager for showing custom windows. */ public WindowManager getWindowManager() { return mWindowManager; }简单的返回了一个mWindowManager对象。那就再看看mWindowManager在哪里初始化的?
final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, NonConfigurationInstances lastNonConfigurationInstances, Configuration config) { ... mWindowManager = mWindow.getWindowManager(); ... }我们看到是attach函数这里赋值的。mWindowManager实际上是从mWindow对象的getWindowManager()方法获取来的。这个mWindow对象我们知道实际上就是一个PhoneWindow对象。看过之后发现是定义在Window类中的,与Actitvy的差不多,都是直接返回一个对象,就不贴了。实际初始化是在Window的setWindowManager函数中:
源码路径:frameworks\base\core\java\android\view\Window.java
源码中国链接:http://www.oschina.net/code/explore/android-2.2-froyo/android/view/Window.java
public void setWindowManager(WindowManager wm, IBinder appToken, String appName, boolean hardwareAccelerated) { mAppToken = appToken; mAppName = appName; if (wm == null) { wm = WindowManagerImpl.getDefault(); } mWindowManager = new LocalWindowManager(wm, hardwareAccelerated); }再回到ActivityThread类的performResumeActivity函数
if (a.mVisibleFromClient) { a.mWindowAdded = true; wm.addView(decor, l); }就是在这里显式的调用了WindowManager的addView函数。至此,整个过程就串起来了。