android framework——Android系统应用La
2021-04-26 本文已影响0人
Peakmain
一、入口分析
public void systemReady(final Runnable goingCallback) {
synchronized (this) {
// 启动 Launcher
startHomeActivityLocked(mCurrentUserId, "systemReady");
}
}
boolean startHomeActivityLocked(int userId, String reason) {
// 获取 Launcher 的启动意图
Intent intent = getHomeIntent();
// 通过意图解析到 ActivityInfo
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);
// 通过进程名和uid 查询进程信息
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
// 这里进程还没启动 app 为 null
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mStackSupervisor.startHomeActivity(intent, aInfo, reason);
}
}
return true;
}
Intent getHomeIntent() {
Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
//用 CATEGORY_HOME 去查询
intent.addCategory(Intent.CATEGORY_HOME);
}
return intent;
}
- mTopAction的值为Intent.ACTION_MAIN
- 如果系统运行模式不是低级工厂模式,则设置Category为CATEGORY_HOME
我们看下Launch的Manifest.xml文件
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.launcher3">
<uses-sdk android:targetSdkVersion="23" android:minSdkVersion="16"/>
<application
android:allowBackup="@bool/enable_backup"
android:backupAgent="com.android.launcher3.LauncherBackupAgentHelper"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher_home"
android:label="@string/application_name"
android:largeHeap="@bool/config_largeHeap"
android:restoreAnyVersion="true"
android:supportsRtl="true" >
<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: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>
Launcher启动是在AMS的SystemReady中,首先通过意图向PMS发送解析请求,PMS查询返回的ActivityInfo对象,最后会通过startHomeActivity方法去启动和创建Launcher,并走到Launcher的onCreate方法
填充App信息
@Override
protected void onCreate(Bundle savedInstanceState) {
...
//获取LauncherAppState的示例
LauncherAppState app = LauncherAppState.getInstance();
mDeviceProfile = getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE ?
app.getInvariantDeviceProfile().landscapeProfile
: app.getInvariantDeviceProfile().portraitProfile;
mSharedPrefs = Utilities.getPrefs(this);
mIsSafeModeEnabled = getPackageManager().isSafeMode();
mModel = app.setLauncher(this);
....
if (!mRestoring) {
if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
} else {
mModel.startLoader(mWorkspace.getRestorePage());
}
}
...
}
LauncherModel setLauncher(Launcher launcher) {
getLauncherProvider().setLauncherProviderChangeListener(launcher);
mModel.initialize(launcher);
mAccessibilityDelegate = ((launcher != null) && Utilities.ATLEAST_LOLLIPOP) ?
new LauncherAccessibilityDelegate(launcher) : null;
return mModel;
}
public void initialize(Callbacks callbacks) {
synchronized (mLock) {
mCallbacks = new WeakReference<Callbacks>(callbacks);
}
}
callbacks实际是Launcher,并将其封装成弱引用对象
回到 mModel.startLoader方法
static final HandlerThread sWorkerThread = new HandlerThread("launcher-loader");
//创建一个handler
static final Handler sWorker = new Handler(sWorkerThread.getLooper());
public void startLoader(int synchronousBindPage, int loadFlags) {
synchronized (mLock) {
if (mCallbacks != null && mCallbacks.get() != null) {
mLoaderTask = new LoaderTask(mApp.getContext(), loadFlags);
if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
&& mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
mLoaderTask.runBindSynchronousPage(synchronousBindPage);
} else {
//将mLoaderTask作为消息发送给HandlerThread
sWorkerThread.setPriority(Thread.NORM_PRIORITY);
sWorker.post(mLoaderTask);
}
}
}
}
private class LoaderTask implements Runnable {
public void run() {
//加载工作区信息
loadAndBindWorkspace();
//加载系统已经安装的应用程序信息
loadAndBindAllApps();
}
}
private void loadAndBindAllApps() {
if (DEBUG_LOADERS) {
Log.d(TAG, "loadAndBindAllApps mAllAppsLoaded=" + mAllAppsLoaded);
}
if (!mAllAppsLoaded) {
loadAllApps();
....
} else {
onlyBindAllApps();
}
}
private void loadAllApps() {
........
mHandler.post(new Runnable() {
public void run() {
final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
if (callbacks != null) {
callbacks.bindAllApplications(added);
} else {
...
}
}});
}
callbacks上面我们分析知道它实际是Launcher,所以我们看Launcher的bindAllApplications方法
public void bindAllApplications(final ArrayList<AppInfo> apps) {
if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
mTmpAppsList = apps;
return;
}
if (mAppsView != null) {
mAppsView.setApps(apps);
}
if (mLauncherCallbacks != null) {
mLauncherCallbacks.bindAllApplications(apps);
}
}
public void setApps(List<AppInfo> apps) {
//将所有的app信息传给了AllAppsContainerView
mApps.setApps(apps);
}
查看AllAppsContainerView的onFinishInflate函数
@Override
protected void onFinishInflate() {
super.onFinishInflate();
boolean isRtl = Utilities.isRtl(getResources());
mAdapter.setRtl(isRtl);
mContent = findViewById(R.id.content);
View.OnFocusChangeListener focusProxyListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mAppsRecyclerView.requestFocus();
}
}
};
mAppsRecyclerView = (AllAppsRecyclerView) findViewById(R.id.apps_list_view);
mAppsRecyclerView.setApps(mApps);
mAppsRecyclerView.setLayoutManager(mLayoutManager);
mAppsRecyclerView.setAdapter(mAdapter);
mAppsRecyclerView.setHasFixedSize(true);
if (mItemDecoration != null) {
mAppsRecyclerView.addItemDecoration(mItemDecoration);
}
updateBackgroundAndPaddings();
}
onFinishInflate在布局加载完之后就会调用,通过mAppsRecyclerView 用来显示App列表
总结
被SystemServer启动的ActivityManagerService会启动Launcher,Launcher启动后会将已安装的应用的快捷图标显示到界面,用网络上的一张图总结,如下:
image.png