共计 13674 个字符,预计需要花费 35 分钟才能阅读完成。
前言
因为源码剖析的代码量比拟大,大部分博客网站的内容显示页面都比拟窄,显示进去的成果都异样俊俏,所以您也能够间接查看《Thinking in Android》来浏览这边文章,心愿这篇文章能帮你梳理分明 “Launcher 启动流程”。
外围源码
要害类 | 门路 |
---|---|
SystemServer.java | frameworks/base/services/java/com/android/server/SystemServer.java |
ActivityManagerService.java | frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java |
ActivityStack.java | frameworks/base/services/core/java/com/android/server/am/ActivityStack.java |
ActivityTaskManagerService.java | frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java |
RootActivityContainer.java | frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java |
Launcher.java | packages/apps/Launcher3/src/com/android/launcher3/Launcher.java |
LauncherModel.java | packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java |
LauncherAppState.java | packages/apps/Launcher3/src/com/android/launcher3/LauncherAppState.java |
LoaderTask.java | packages/apps/Launcher3/src/com/android/launcher3/model/LoaderTask.java |
一、Launcher 启动流程
Android 系统启动的最初一步是启动一个 Home 应用程序
,这个应用程序用来显示零碎中曾经装置的应用程序,咱们称说这个应用程序为 Launcher
。
应用程序 Launcher 在启动过程中会申请 PackageManagerService 返回零碎中曾经装置的应用程序的信息
,并将这些信息封装成一个 快捷图标
列表显示在零碎屏幕上,这样用户能够通过点击这些快捷图标来启动相应的应用程序了。
1.1 SystemServer.startOtherServices()
// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
private ActivityManagerService mActivityManagerService;
private void startOtherServices() {
... ...
mActivityManagerService.systemReady(() -> { // 调用 ActivityManagerService 的 systemReady() 办法
Slog.i(TAG, "Making services ready");
... ...
}
... ...
}
}
在 startOtherServices()
办法中,会调用 ActivityManagerService
的 systemReady()
办法。
1.2 ActivityManagerService.systemReady()
// frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public class ActivityManagerService extends IActivityManager.Stub
implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
@VisibleForTesting
public ActivityTaskManagerInternal mAtmInternal;
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
... ...
synchronized (this) {
... ...
// 调用 resumeTopActivities() 办法,这是一个形象办法
mAtmInternal.resumeTopActivities(false /* scheduleIdle */);
mUserController.sendUserSwitchBroadcasts(-1, currentUserId);
... ...
}
... ...
}
}
1.3 ActivityTaskManagerService.resumeTopActivities()
resumeTopActivities()
办法由 ActivityTaskManagerService
的外部类 LocalService
实现。
// frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
RootActivityContainer mRootActivityContainer;
final class LocalService extends ActivityTaskManagerInternal {
... ...
@Override
public void resumeTopActivities(boolean scheduleIdle) {synchronized (mGlobalLock) {// 调用 RootActivityContainer 的 resumeFocusedStacksTopActivities() 办法
mRootActivityContainer.resumeFocusedStacksTopActivities();
if (scheduleIdle) {mStackSupervisor.scheduleIdleLocked();
}
}
}
... ...
}
}
1.4 RootActivityContainer.resumeFocusedStacksTopActivities()
// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
class RootActivityContainer extends ConfigurationContainer
implements DisplayManager.DisplayListener {boolean resumeFocusedStacksTopActivities() {return resumeFocusedStacksTopActivities(null, null, null);
}
boolean resumeFocusedStacksTopActivities(ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
... ...
for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
boolean resumedOnDisplay = false;
final ActivityDisplay display = mActivityDisplays.get(displayNdx);
... ...
if (!resumedOnDisplay) {final ActivityStack focusedStack = display.getFocusedStack();
if (focusedStack != null) {// 调用 resumeTopActivityUncheckedLocked() 办法
focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions);
}
}
}
return result;
}
}
1.5 ActivityStack.resumeTopActivityUncheckedLocked()
// frameworks/base/services/core/java/com/android/server/wm/ActivityStack.java
class ActivityStack extends ConfigurationContainer {boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {if (mInResumeTopActivity) {
// Don't even start recursing.
return false;
}
boolean result = false;
try {
// Protect against recursion.
mInResumeTopActivity = true;
result = resumeTopActivityInnerLocked(prev, options); // 调用 resumeTopActivityInnerLocked() 办法
final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
if (next == null || !next.canTurnScreenOn()) {checkReadyForSleep();
}
} finally {mInResumeTopActivity = false;}
return result;
}
}
1.6 ActivityStack.resumeTopActivityInnerLocked()
// frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
class ActivityStack extends ConfigurationContainer {private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
... ...
if (!hasRunningActivity) {
// There are no activities left in the stack, let's look somewhere else.
return resumeNextFocusableActivityWhenStackIsEmpty(prev, options);
}
... ...
}
}
1.7 ActivityStack.resumeNextFocusableActivityWhenStackIsEmpty()
// frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
class ActivityStack extends ConfigurationContainer {
protected final RootActivityContainer mRootActivityContainer;
private boolean resumeNextFocusableActivityWhenStackIsEmpty(ActivityRecord prev,
ActivityOptions options) {
final String reason = "noMoreActivities";
if (!isActivityTypeHome()) {final ActivityStack nextFocusedStack = adjustFocusToNextFocusableStack(reason);
if (nextFocusedStack != null) {
return mRootActivityContainer.resumeFocusedStacksTopActivities(nextFocusedStack,
prev, null /* targetOptions */);
}
}
// If the current stack is a home stack, or if focus didn't switch to a different stack -
// just start up the Launcher...
ActivityOptions.abort(options);
if (DEBUG_STATES) Slog.d(TAG_STATES,
"resumeNextFocusableActivityWhenStackIsEmpty:" + reason + ", go home");
// 调用 resumeHomeActivity() 办法
return mRootActivityContainer.resumeHomeActivity(prev, reason, mDisplayId);
}
}
1.8 RootActivityContainer.resumeHomeActivity()
// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
class RootActivityContainer extends ConfigurationContainer
implements DisplayManager.DisplayListener {boolean resumeHomeActivity(ActivityRecord prev, String reason, int displayId) {if (!mService.isBooting() && !mService.isBooted()) {
// Not ready yet!
return false;
}
if (displayId == INVALID_DISPLAY) {displayId = DEFAULT_DISPLAY;}
final ActivityRecord r = getActivityDisplay(displayId).getHomeActivity();
final String myReason = reason + "resumeHomeActivity";
// Only resume home activity if isn't finishing.
if (r != null && !r.finishing) {r.moveFocusableActivityToTop(myReason);
return resumeFocusedStacksTopActivities(r.getActivityStack(), prev, null);
}
// 调用 startHomeOnDisplay() 办法
return startHomeOnDisplay(mCurrentUser, myReason, displayId);
}
}
1.9 RootActivityContainer.startHomeOnDisplay()
// frameworks/base/services/core/java/com/android/server/wm/RootActivityContainer.java
class RootActivityContainer extends ConfigurationContainer
implements DisplayManager.DisplayListener {
ActivityTaskManagerService mService;
boolean startHomeOnDisplay(int userId, String reason, int displayId) {
return startHomeOnDisplay(userId, reason, displayId, false /* allowInstrumenting */,
false /* fromHomeKey */);
}
boolean startHomeOnDisplay(int userId, String reason, int displayId, boolean allowInstrumenting,
boolean fromHomeKey) {
... ...
// Updates the home component of the intent.
homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
... ...
// 启动 Launcher
mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,
displayId);
return true;
}
}
二、利用图标显示流程
接下来咱们看看 Launcher
是如何加载并显示所有已装置 Apk
的:
2.1 Launcher.onCreate()
// packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
/**
* Default launcher application.
*/
public class Launcher extends BaseDraggingActivity implements LauncherExterns,
LauncherModel.Callbacks, LauncherProviderChangeListener, UserEventDelegate,
InvariantDeviceProfile.OnIDPChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
... ...
LauncherAppState app = LauncherAppState.getInstance(this);
... ...
mModel = app.setLauncher(this); // 调用 setLauncher() 办法
... ...
if (!mModel.startLoader(currentScreen)) { // 见 2.4
if (!internalStateHandled) {
// If we are not binding synchronously, show a fade in animation when
// the first page bind completes.
mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
}
} else {
// Pages bound synchronously.
mWorkspace.setCurrentPage(currentScreen);
setWorkspaceLoading(true);
}
... ...
}
}
2.2 LauncherAppState.setLauncher()
// packages/apps/Launcher3/src/com/android/launcher3/LauncherAppState.java
public class LauncherAppState {LauncherModel setLauncher(Launcher launcher) {getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
mModel.initialize(launcher); // 调用 initialize() 办法
return mModel;
}
}
2.3 LauncherModel.initialize()
// packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java
public class LauncherModel extends BroadcastReceiver
implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
/**
* Set this as the current Launcher activity object for the loader.
*/
public void initialize(Callbacks callbacks) {synchronized (mLock) {Preconditions.assertUIThread();
mCallbacks = new WeakReference<>(callbacks);
}
}
}
在 initialize
办法中会将 Callbacks
(也就是传入的 Launcher)封装成一个 弱援用对象
,因而咱们得悉 mCallbacks
变量指的就是封装成弱援用对象的 Launcher
。
从新回到 oncreate() 函数,有个 startLoader()
办法!
2.4 LauncherModel.startLoader()
// packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java
public class LauncherModel extends BroadcastReceiver
implements LauncherAppsCompat.OnAppsChangedCallbackCompat {public boolean startLoader(int synchronousBindPage) {
// Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_LOADER_RUNNING);
synchronized (mLock) {
// Don't bother to start the thread if we know it's not going to do anything
if (mCallbacks != null && mCallbacks.get() != null) {final Callbacks oldCallbacks = mCallbacks.get();
// Clear any pending bind-runnables from the synchronized load process.
mUiExecutor.execute(oldCallbacks::clearPendingBinds);
// If there is already one running, tell it to stop.
stopLoader();
LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel,
mBgAllAppsList, synchronousBindPage, mCallbacks);
if (mModelLoaded && !mIsLoaderTaskRunning) {
// Divide the set of loaded items into those that we are binding synchronously,
// and everything else that is to be bound normally (asynchronously).
loaderResults.bindWorkspace();
// For now, continue posting the binding of AllApps as there are other
// issues that arise from that.
loaderResults.bindAllApps();
loaderResults.bindDeepShortcuts();
loaderResults.bindWidgets();
return true;
} else {startLoaderForResults(loaderResults); // 调用 startLoaderForResults() 办法}
}
}
return false;
}
}
2.5 LauncherModel.startLoaderForResults()
// packages/apps/Launcher3/src/com/android/launcher3/LauncherModel.java
public class LauncherModel extends BroadcastReceiver
implements LauncherAppsCompat.OnAppsChangedCallbackCompat {@Thunk static final Handler sWorker = new Handler(mWorkerLooper);
public void startLoaderForResults(LoaderResults results) {synchronized (mLock) {stopLoader();
mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, results);
// Always post the loader task, instead of running directly (even on same thread) so
// that we exit any nested synchronized blocks
sWorker.post(mLoaderTask);
}
}
}
2.6 LoaderTask.run()
当 LoaderTask
所形容的音讯被解决时则会调用它的 run()
办法:
// packages/apps/Launcher3/src/com/android/launcher3/model/LoaderTask.java
public class LoaderTask implements Runnable {public void run() {synchronized (this) {
// Skip fast if we are already stopped.
if (mStopped) {return;}
}
try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {TraceHelper.partitionSection(TAG, "step 1.1: loading workspace");
loadWorkspace(); // 加载工作区信息
verifyNotStopped();
TraceHelper.partitionSection(TAG, "step 1.2: bind workspace workspace");
mResults.bindWorkspace();
... ...
// second step
TraceHelper.partitionSection(TAG, "step 2.1: loading all apps");
List<LauncherActivityInfo> allActivityList = loadAllApps(); // 加载零碎曾经装置的应用程序信息
TraceHelper.partitionSection(TAG, "step 2.2: Binding all apps");
verifyNotStopped();
mResults.bindAllApps();
... ...
// third step
TraceHelper.partitionSection(TAG, "step 3.1: loading deep shortcuts");
loadDeepShortcuts();
verifyNotStopped();
TraceHelper.partitionSection(TAG, "step 3.2: bind deep shortcuts");
mResults.bindDeepShortcuts();
... ...
// fourth step
TraceHelper.partitionSection(TAG, "step 4.1: loading widgets");
List<ComponentWithLabel> allWidgetsList = mBgDataModel.widgetsModel.update(mApp, null);
verifyNotStopped();
TraceHelper.partitionSection(TAG, "step 4.2: Binding widgets");
mResults.bindWidgets();
... ...
transaction.commit();} catch (CancellationException e) {
// Loader stopped, ignore
TraceHelper.partitionSection(TAG, "Cancelled");
}
TraceHelper.endSection(TAG);
}
}
2.7 LoaderTask.loadAllApps()
// packages/apps/Launcher3/src/com/android/launcher3/model/LoaderTask.java
public class LoaderTask implements Runnable {private List<LauncherActivityInfo> loadAllApps() {final List<UserHandle> profiles = mUserManager.getUserProfiles();
List<LauncherActivityInfo> allActivityList = new ArrayList<>();
// Clear the list of apps
mBgAllAppsList.clear();
for (UserHandle user : profiles) {
// Query for the set of apps
final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
// Fail if we don't have any apps
// TODO: Fix this. Only fail for the current user.
if (apps == null || apps.isEmpty()) {return allActivityList;}
boolean quietMode = mUserManager.isQuietModeEnabled(user);
// Create the ApplicationInfos
for (int i = 0; i < apps.size(); i++) {LauncherActivityInfo app = apps.get(i);
// This builds the icon bitmaps.
mBgAllAppsList.add(new AppInfo(app, user, quietMode), app);
}
allActivityList.addAll(apps);
}
if (FeatureFlags.LAUNCHER3_PROMISE_APPS_IN_ALL_APPS) {
// get all active sessions and add them to the all apps list
for (PackageInstaller.SessionInfo info :
mPackageInstaller.getAllVerifiedSessions()) {mBgAllAppsList.addPromiseApp(mApp.getContext(),
PackageInstallerCompat.PackageInstallInfo.fromInstallingState(info));
}
}
mBgAllAppsList.added = new ArrayList<>();
return allActivityList;
}
}
自此,Launcher 的启动和利用加载的流程源码剖析结束。
正文完