相關(guān)文章
Android系統(tǒng)架構(gòu)與系統(tǒng)源碼目錄
Android系統(tǒng)啟動流程(一)解析init進(jìn)程啟動過程
Android系統(tǒng)啟動流程(二)解析Zygote進(jìn)程啟動過程
Android系統(tǒng)啟動流程(三)解析SyetemServer進(jìn)程啟動過程
前言
此前的文章我們學(xué)習(xí)了init進(jìn)程余黎、Zygote進(jìn)程和SyetemServer進(jìn)程的啟動過程,這一篇文章我們就來學(xué)習(xí)Android系統(tǒng)啟動流程的最后一步:Launcher的啟動流程巡扇,并結(jié)合本系列的前三篇文章的內(nèi)容來講解Android系統(tǒng)啟動流程厅翔。建議讀這篇文章前要通讀本系列的前三篇文章搀突,否則你可能不會理解我在講什么描姚。
1.Launcher概述
Android系統(tǒng)啟動的最后一步是啟動一個Home應(yīng)用程序,這個應(yīng)用程序用來顯示系統(tǒng)中已經(jīng)安裝的應(yīng)用程序筒扒,這個Home應(yīng)用程序就叫做Launcher花墩。應(yīng)用程序Launcher在啟動過程中會請求PackageManagerService返回系統(tǒng)中已經(jīng)安裝的應(yīng)用程序的信息澄步,并將這些信息封裝成一個快捷圖標(biāo)列表顯示在系統(tǒng)屏幕上村缸,這樣用戶可以通過點(diǎn)擊這些快捷圖標(biāo)來啟動相應(yīng)的應(yīng)用程序。
2.Launcher啟動流程
SyetemServer進(jìn)程在啟動的過程中會啟動PackageManagerService仇箱,PackageManagerService啟動后會將系統(tǒng)中的應(yīng)用程序安裝完成剂桥。在此前已經(jīng)啟動的ActivityManagerService會將Launcher啟動起來。
啟動Launcher的入口為ActivityManagerService的systemReady函數(shù)美尸,如下所示斟薇。
frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
...
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
mSystemServiceManager.startBootPhase(
SystemService.PHASE_ACTIVITY_MANAGER_READY);
...
}
...
}
在startOtherServices函數(shù)中奔垦,會調(diào)用ActivityManagerService的systemReady函數(shù):
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void systemReady(final Runnable goingCallback) {
...
synchronized (this) {
...
mStackSupervisor.resumeFocusedStackTopActivityLocked();
mUserController.sendUserSwitchBroadcastsLocked(-1, currentUserId);
}
}
systemReady函數(shù)中調(diào)用了ActivityStackSupervisor的resumeFocusedStackTopActivityLocked函數(shù):
frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
if (targetStack != null && isFocusedStack(targetStack)) {
return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);//1
}
final ActivityRecord r = mFocusedStack.topRunningActivityLocked();
if (r == null || r.state != RESUMED) {
mFocusedStack.resumeTopActivityUncheckedLocked(null, null);
}
return false;
}
在注釋1處會調(diào)用ActivityStack的resumeTopActivityUncheckedLocked函數(shù)椿猎,ActivityStack對象是用來描述Activity堆棧的犯眠,resumeTopActivityUncheckedLocked函數(shù)如下所示症革。
frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
if (mStackSupervisor.inResumeTopActivity) {
// Don't even start recursing.
return false;
}
boolean result = false;
try {
// Protect against recursion.
mStackSupervisor.inResumeTopActivity = true;
if (mService.mLockScreenShown == ActivityManagerService.LOCK_SCREEN_LEAVING) {
mService.mLockScreenShown = ActivityManagerService.LOCK_SCREEN_HIDDEN;
mService.updateSleepIfNeededLocked();
}
result = resumeTopActivityInnerLocked(prev, options);//1
} finally {
mStackSupervisor.inResumeTopActivity = false;
}
return result;
}
注釋1調(diào)用了resumeTopActivityInnerLocked函數(shù):
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
...
return isOnHomeDisplay() &&
mStackSupervisor.resumeHomeStackTask(returnTaskType, prev, "prevFinished");
...
}
resumeTopActivityInnerLocked函數(shù)的代碼很長量蕊,我們截取我們要分析的關(guān)鍵的一句:調(diào)用ActivityStackSupervisor的resumeHomeStackTask函數(shù)艇挨,代碼如下所示缩滨。
frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
boolean resumeHomeStackTask(int homeStackTaskType, ActivityRecord prev, String reason) {
...
if (r != null && !r.finishing) {
mService.setFocusedActivityLocked(r, myReason);
return resumeFocusedStackTopActivityLocked(mHomeStack, prev, null);
}
return mService.startHomeActivityLocked(mCurrentUser, myReason);//1
}
在注釋1處調(diào)用了ActivityManagerService的startHomeActivityLocked函數(shù)脉漏,如下所示。
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
boolean startHomeActivityLocked(int userId, String reason) {
if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {//1
return false;
}
Intent intent = getHomeIntent();//2
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {//3
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);//4
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
注釋1處的mFactoryTest代表系統(tǒng)的運(yùn)行模式舅锄,系統(tǒng)的運(yùn)行模式分為三種皇忿,分別是非工廠模式、低級工廠模式和高級工廠模式撮胧,mTopAction則用來描述第一個被啟動Activity組件的Action芹啥,它的值為Intent.ACTION_MAIN铺峭。因此注釋1的代碼意思就是mFactoryTest為FactoryTest.FACTORY_TEST_LOW_LEVEL(低級工廠模式)并且mTopAction=null時卫键,直接返回false。注釋2處的getHomeIntent函數(shù)如下所示钓账。
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
getHomeIntent函數(shù)中創(chuàng)建了Intent梆暮,并將mTopAction和mTopData傳入绍昂。mTopAction的值為Intent.ACTION_MAIN窘游,并且如果系統(tǒng)運(yùn)行模式不是低級工廠模式則將intent的Category設(shè)置為Intent.CATEGORY_HOME。我們再回到ActivityManagerService的startHomeActivityLocked函數(shù)贪嫂,假設(shè)系統(tǒng)的運(yùn)行模式不是低級工廠模式撩荣,在注釋3處判斷符合Action為Intent.ACTION_MAIN饶深,Category為Intent.CATEGORY_HOME的應(yīng)用程序是否已經(jīng)啟動敌厘,如果沒啟動則調(diào)用注釋4的方法啟動該應(yīng)用程序。
這個被啟動的應(yīng)用程序就是Launcher饱狂,因?yàn)長auncher的Manifest文件中的intent-filter標(biāo)簽匹配了Action為Intent.ACTION_MAIN,Category為Intent.CATEGORY_HOME讲婚。Launcher的Manifest文件如下所示筹麸。
packages/apps/Launcher3/AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.launcher3">
<uses-sdk android:targetSdkVersion="23" android:minSdkVersion="16"/>
...
<application
...
<activity
android:name="com.android.launcher3.Launcher"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/Theme"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor"
android:configChanges="keyboard|keyboardHidden|navigation"
android:resumeWhilePausing="true"
android:taskAffinity=""
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity>
...
</application>
</manifest>
這樣物赶,應(yīng)用程序Launcher就會被啟動起來酵紫,并執(zhí)行它的onCreate函數(shù)错维。
3.Launcher中應(yīng)用圖標(biāo)顯示流程
Launcher的onCreate函數(shù)如下所示。
packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
@Override
protected void onCreate(Bundle savedInstanceState) {
...
LauncherAppState app = LauncherAppState.getInstance();//1
mDeviceProfile = getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE ?
app.getInvariantDeviceProfile().landscapeProfile
: app.getInvariantDeviceProfile().portraitProfile;
mSharedPrefs = Utilities.getPrefs(this);
mIsSafeModeEnabled = getPackageManager().isSafeMode();
mModel = app.setLauncher(this);//2
....
if (!mRestoring) {
if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);//2
} else {
mModel.startLoader(mWorkspace.getRestorePage());
}
}
...
}
注釋1處獲取LauncherAppState的實(shí)例并在注釋2處調(diào)用它的setLauncher函數(shù)并將Launcher對象傳入轧坎,LauncherAppState的setLauncher函數(shù)如下所示。
packages/apps/Launcher3/src/com/android/launcher3/LauncherAppState.java
LauncherModel setLauncher(Launcher launcher) {
getLauncherProvider().setLauncherProviderChangeListener(launcher);
mModel.initialize(launcher);//1
mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
new LauncherAccessibilityDelegate(launcher) : null;
return mModel;
}
注釋1處會調(diào)用LauncherModel的initialize函數(shù):
public void initialize(Callbacks callbacks) {
synchronized (mLock) {
unbindItemInfosAndClearQueuedBindRunnables();
mCallbacks = new WeakReference<Callbacks>(callbacks);
}
}
在initialize函數(shù)中會將Callbacks,也就是傳入的Launcher 封裝成一個弱引用對象捎泻。因此我們得知mCallbacks變量指的就是封裝成弱引用對象的Launcher笆豁,這個mCallbacks后文會用到它。
再回到Launcher的onCreate函數(shù)煞赢,在注釋2處調(diào)用了LauncherModel的startLoader函數(shù):
packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java
...
@Thunk static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");//1
static {
sWorkerThread.start();
}
@Thunk static final Handler sWorker = new Handler(sWorkerThread.getLooper());//2
...
public void startLoader(int synchronousBindPage, int loadFlags) {s
InstallShortcutReceiver.enableInstallQueue();
synchronized (mLock) {
synchronized (mDeferredBindRunnables) {
mDeferredBindRunnables.clear();
}
if (mCallbacks != null && mCallbacks.get() != null) {
stopLoaderLocked();
mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);//3
if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
&& mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
mLoaderTask.runBindSynchronousPage(synchronousBindPage);
} else {
sWorkerThread.setPriority(Thread.NORM_PRIORITY);
sWorker.post(mLoaderTask);//4
}
}
}
}
注釋1處創(chuàng)建了具有消息循環(huán)的線程HandlerThread對象。注釋2處創(chuàng)建了Handler,并且傳入HandlerThread的Looper波俄。Hander的作用就是向HandlerThread發(fā)送消息懦铺。在注釋3處創(chuàng)建LoaderTask支鸡,在注釋4處將LoaderTask作為消息發(fā)送給HandlerThread 苍匆。
LoaderTask類實(shí)現(xiàn)了Runnable接口,當(dāng)LoaderTask所描述的消息被處理時則會調(diào)用它的run函數(shù)叔汁,代碼如下所示
private class LoaderTask implements Runnable {
...
public void run() {
synchronized (mLock) {
if (mStopped) {
return;
}
mIsLoaderTaskRunning = true;
}
keep_running: {
if (DEBUG_LOADERS) Log.d(TAG, "step 1: loading workspace");
loadAndBindWorkspace();//1
if (mStopped) {
break keep_running;
}
waitForIdle();
if (DEBUG_LOADERS) Log.d(TAG, "step 2: loading all apps");
loadAndBindAllApps();//2
}
mContext = null;
synchronized (mLock) {
if (mLoaderTask == this) {
mLoaderTask = null;
}
mIsLoaderTaskRunning = false;
mHasLoaderCompletedOnce = true;
}
}
...
}
Launcher是用工作區(qū)的形式來顯示系統(tǒng)安裝的應(yīng)用程序的快捷圖標(biāo)据块,每一個工作區(qū)都是來描述一個抽象桌面的折剃,它由n個屏幕組成怕犁,每個屏幕又分n個單元格,每個單元格用來顯示一個應(yīng)用程序的快捷圖標(biāo)戈轿。注釋1處調(diào)用loadAndBindWorkspace函數(shù)用來加載工作區(qū)信息思杯,注釋2處的loadAndBindAllApps函數(shù)是用來加載系統(tǒng)已經(jīng)安裝的應(yīng)用程序信息挠进,loadAndBindAllApps函數(shù)代碼如下所示领突。
private void loadAndBindAllApps() {
if (DEBUG_LOADERS) {
Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
}
if (!mAllAppsLoaded) {
loadAllApps();//1
synchronized (LoaderTask.this) {
if (mStopped) {
return;
}
}
updateIconCache();
synchronized (LoaderTask.this) {
if (mStopped) {
return;
}
mAllAppsLoaded = true;
}
} else {
onlyBindAllApps();
}
}
如果系統(tǒng)沒有加載已經(jīng)安裝的應(yīng)用程序信息攘须,則會調(diào)用注釋1處的loadAllApps函數(shù):
private void loadAllApps() {
...
mHandler.post(new Runnable() {
public void run() {
final long bindTime = SystemClock.uptimeMillis();
final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
if (callbacks != null) {
callbacks.bindAllApplications(added);//1
if (DEBUG_LOADERS) {
Log.d(TAG, "bound " + added.size() + " apps in "
+ (SystemClock.uptimeMillis() - bindTime) + "ms");
}
} else {
Log.i(TAG, "not binding apps: no Launcher activity");
}
}
});
...
}
在注釋1處會調(diào)用callbacks的bindAllApplications函數(shù),在前面我們得知這個callbacks實(shí)際是指向Launcher的浮驳,因此我們來查看Launcher的bindAllApplications函數(shù)至会,代碼如下所示。
packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
public void bindAllApplications(final ArrayList<AppInfo> apps) {
if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
mTmpAppsList = apps;
return;
}
if (mAppsView != null) {
mAppsView.setApps(apps);//1
}
if (mLauncherCallbacks != null) {
mLauncherCallbacks.bindAllApplications(apps);
}
}
在注釋1處會調(diào)用AllAppsContainerView的setApps函數(shù)宵蛀,并將包含應(yīng)用信息的列表apps傳進(jìn)去术陶,AllAppsContainerView的setApps函數(shù)如下所示梧宫。
packages/apps/Launcher3/src/com/android/launcher3/allapps/AllAppsContainerView.java
public void setApps(List<AppInfo> apps) {
mApps.setApps(apps);
}
包含應(yīng)用信息的列表apps已經(jīng)傳給了AllAppsContainerView摆碉,查看AllAppsContainerView的onFinishInflate函數(shù):
@Override
protected void onFinishInflate() {
super.onFinishInflate();
...
// Load the all apps recycler view
mAppsRecyclerView = (AllAppsRecyclerView) findViewById(R.id.apps_list_view);//1
mAppsRecyclerView.setApps(mApps);//2
mAppsRecyclerView.setLayoutManager(mLayoutManager);
mAppsRecyclerView.setAdapter(mAdapter);//3
mAppsRecyclerView.setHasFixedSize(true);
mAppsRecyclerView.addOnScrollListener(mElevationController);
mAppsRecyclerView.setElevationController(mElevationController);
...
}
onFinishInflate函數(shù)在加載完xml文件時就會調(diào)用巷帝,在注釋1處得到AllAppsRecyclerView用來顯示App列表,并在注釋2處將apps的信息列表傳進(jìn)去驰徊,并在注釋3處為AllAppsRecyclerView設(shè)置Adapter辣垒。這樣應(yīng)用程序快捷圖標(biāo)的列表就會顯示在屏幕上印蔬。
到這里L(fēng)auncher啟動流程就講到這侥猬,接下來講Android系統(tǒng)啟動流程捐韩。
4.Android系統(tǒng)啟動流程
那么結(jié)合本篇以及本系列的前三篇文章荤胁,我們就可以得出Android系統(tǒng)啟動流程,如下所示垢油。
1.啟動電源以及系統(tǒng)啟動
當(dāng)電源按下時引導(dǎo)芯片代碼開始從預(yù)定義的地方(固化在ROM)開始執(zhí)行滩愁。加載引導(dǎo)程序Bootloader到RAM,然后執(zhí)行廉丽。
2.引導(dǎo)程序BootLoader
引導(dǎo)程序BootLoader是在Android操作系統(tǒng)開始運(yùn)行前的一個小程序妻味,它的主要作用是把系統(tǒng)OS拉起來并運(yùn)行责球。
3.Linux內(nèi)核啟動
內(nèi)核啟動時,設(shè)置緩存裁良、被保護(hù)存儲器价脾、計劃列表笛匙、加載驅(qū)動妹孙。當(dāng)內(nèi)核完成系統(tǒng)設(shè)置,它首先在系統(tǒng)文件中尋找init.rc文件骇笔,并啟動init進(jìn)程嚣崭。
4.init進(jìn)程啟動
初始化和啟動屬性服務(wù)雹舀,并且啟動Zygote進(jìn)程说榆。
5.Zygote進(jìn)程啟動
創(chuàng)建JavaVM并為JavaVM注冊JNI寸认,創(chuàng)建服務(wù)端Socket串慰,啟動SystemServer進(jìn)程模庐。
6.SystemServer進(jìn)程啟動
啟動Binder線程池和SystemServiceManager,并且啟動各種系統(tǒng)服務(wù)怜姿。
7.Launcher啟動
被SystemServer進(jìn)程啟動的ActivityManagerService會啟動Launcher沧卢,Launcher啟動后會將已安裝應(yīng)用的快捷圖標(biāo)顯示到界面上醉者。
結(jié)合上面的流程撬即,給出Android系統(tǒng)啟動流程圖:
歡迎關(guān)注我的微信公眾號剥槐,第一時間獲得博客更新提醒,以及更多成體系的Android相關(guān)原創(chuàng)技術(shù)干貨颅崩。
掃一掃下方二維碼即可關(guān)注: