1、Launcher
Launcher作为Android系统的桌面,它的作用有两点:
作为Android系统的启动器,用于启动应用程序;
作为Android系统的桌面,用于显示和管理应用程序的快捷图标或者其它桌面组件;
2、Launcher进程启动流程
2.1、SystemServer调用
在SystemServer进程启动之后,执行其run()函数,在里面执行了大量的配置设置操作,并且启动了各种引导服务、核心服务以及其他服务等,包括AMS、PMS、WMS、电量管理服务等一系列服务,以及创建主线程Looper,并循环等待消息;
其中在启动引导服务方法中,启动了ActivityManagerService,并且在启动其他服务的方法中,调用AMS的systemReady()方法,Launcher进程就是从这儿开始启动的;
public final class SystemServer {
private void run() {
...
startBootstrapServices();
startOtherServices();
...
}
private void startBootstrapServices() {
...
mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
...
}
private void startOtherServices() {
...
mActivityManagerService.systemReady(() -> {
}, BOOT_TIMINGS_TRACE_LOG);
}
}
在SystemServer启动的时候,执行startOtherServices()方法中,里面调用了AMS的systemReady()方法,通过该方法来启动Launcher;
// Tag for timing measurement of main thread.
private static final String SYSTEM_SERVER_TIMING_TAG = "SystemServerTiming";
private static final TimingsTraceLog BOOT_TIMINGS_TRACE_LOG
= new TimingsTraceLog(SYSTEM_SERVER_TIMING_TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
private void startOtherServices() {
...
mActivityManagerService.systemReady(() -> {
Slog.i(TAG, "Making services ready");
traceBeginAndSlog("StartActivityManagerReadyPhase");
mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);
...
}, BOOT_TIMINGS_TRACE_LOG);
}
2.2、AMS执行
在AMS中执行systemReady()方法,在其中执行startHomeActivityLocked()方法,传入当前用户ID;
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
...
synchronized (this) {
...
startHomeActivityLocked(currentUserId, "systemReady");
...
}
...
}
2.2.1、获取Launcher的Intent
在startHomeActivityLocked()方法中,首先通过getHomeIntent()方法,获取到要启动的HomeActivity的intent对象,其中mTopAction默认为INTENT.ACTION_MAIN,并添加CATEGORY_HOME的category标志;
得到Intent对象,通过PackageManager去获取对应符合的Activity,获取对应的ActivityInfo,并获取对应的进程记录,此时对应的进程还没启动,后面继续执行,为intent添加FLAG_ACTIVITY_NEW_TASK启动参数,开启新栈,随后调用ActivityStartController类的startHomeActivity()方法去执行启动;
boolean startHomeActivityLocked(int userId, String reason) {
...
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid, true);
if (app == null || app.instr == null) {
intent.setFlags(intent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
// For ANR debugging to verify if the user activity is the one that actually launched.
final String myReason = reason + ":" + userId + ":" + resolvedUserId;
mActivityStartController.startHomeActivity(intent, aInfo, myReason);
}
}
...
return true;
}
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;
}
2.2.2、启动Launcher
在startHomeActivity()方法中,调用obtainStarter()方法获取到一个ActivityStarter对象,setCallingUid()方法设置当前调用的Uid=0,然后执行其execute()方法;
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(reason);
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
.setOutActivity(tmpOutRecord)
.setCallingUid(0)
.setActivityInfo(aInfo)
.execute();
mLastHomeActivityStartRecord = tmpOutRecord[0];
if (mSupervisor.inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
mSupervisor.scheduleResumeTopActivities();
}
}
在ActivityStarter的execute()方法中,mayWait默认为false,执行startActivity()方法;
int execute() {
try {
// TODO(b/64750076): Look into passing request directly to these methods to allow
// for transactional diffs and preprocessing.
if (mRequest.mayWait) {
return startActivityMayWait(mRequest.caller, mRequest.callingUid, ...);
} else {
return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent, ...);
}
} finally {
onExecutionComplete();
}
}
这里进入了Activity的启动流程,Launcher本身就是一个系统APP,用于显示桌面等,LauncherApp启动之后会执行其生命周期方法初始化桌面布局;
2.3、初始化桌面图标
2.3.1、执行onCreate()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
...
LauncherAppState app = LauncherAppState.getInstance(this);
...
}
获取LauncherAppState,通过LauncherAppState的getInstance()方法获取,该方法里面会判断当前线程是否为主线程,在主线程时还会直接new出对象,不在主线程时,通过MainThreadExecutor的submit()方法向主线程提交一个任务去获取该对象;
// We do not need any synchronization for this variable as its only written on UI thread.
private static LauncherAppState INSTANCE;
public static LauncherAppState getInstance(final Context context) {
if (INSTANCE == null) {
if (Looper.myLooper() == Looper.getMainLooper()) {
INSTANCE = new LauncherAppState(context.getApplicationContext());
} else {
try {
return new MainThreadExecutor().submit(new Callable() {
@Override
public LauncherAppState call() throws Exception {
return LauncherAppState.getInstance(context);
}
}).get();
} catch (InterruptedException|ExecutionException e) {
throw new RuntimeException(e);
}
}
}
return INSTANCE;
}
2.3.2、读取安装APP信息
在LauncherAppState的构造方法中,会新建InvariantDeviceProfile对象,这个类主要是存储App的基本配置信息,例如App图标的尺寸大小,文字大小,每个工作空间或文件夹能显示多少App等;
在LauncherAppState的构造方法中,会获取WindowManager,并获取屏幕的尺寸,解析桌面布局文件,获取默认尺寸信息等;
@TargetApi(23)
public InvariantDeviceProfile(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
...
ArrayList closestProfiles = findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
...
}
ArrayList getPredefinedDeviceProfiles(Context context) {
ArrayList profiles = new ArrayList<>();
try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
final int depth = parser.getDepth();
int type;
while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
TypedArray a = context.obtainStyledAttributes(Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
profiles.add(new InvariantDeviceProfile(
a.getString(R.styleable.InvariantDeviceProfile_name),
a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
numRows,
numColumns,
a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
iconSize,
a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
a.recycle();
}
}
} catch (IOException|XmlPullParserException e) {
throw new RuntimeException(e);
}
return profiles;
}
2.3.3、注册Intent广播
新建LauncherModel对象,该对象是一个BroadcastReceiver,并添加App变化的回调,以及设置Filter并注册广播,用于监听桌面App的变化;
private LauncherAppState(Context context) {
...
mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
// Register intent receivers
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
// For handling managed profiles
filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
...
mContext.registerReceiver(mModel, filter);
...
}
public class LauncherModel extends BroadcastReceiver ... {}
2.3.4、解析Launcher布局
继续回到Launcher的onCreate()方法,将Launcher添加到LauncherModel中,是以弱引用的方式添加,初始化一些其工作,解析Launcher的布局,
2.3.5、加载桌面
onCreate()方法中,通过LauncherModel的startLoader()来加载桌面App;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
if (!mModel.startLoader(currentScreen)) {
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);
}
...
}
在LauncherModel的startLoader()方法中,新建了一个LoaderResults对象,并通过startLoaderForResults()方法创建出一个LoaderTask的Runnable任务,将其在工作线程中执行起来;
public boolean startLoader(int synchronousBindPage) {
...
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) {
...
LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel, mBgAllAppsList, synchronousBindPage, mCallbacks);
if (mModelLoaded && !mIsLoaderTaskRunning) {
...
return true;
} else {
startLoaderForResults(loaderResults);
}
}
}
return false;
}
public void startLoaderForResults(LoaderResults results) {
synchronized (mLock) {
stopLoader();
mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, results);
runOnWorkerThread(mLoaderTask);
}
}
private static void runOnWorkerThread(Runnable r) {
if (sWorkerThread.getThreadId() == Process.myTid()) {
r.run();
} else {
// If we are not on the worker thread, then post to the worker handler
sWorker.post(r);
}
}
在LoaderTask的run()方法中,去加载手机已安装的App的信息,查询数据库获取已安装的App的相关信息,加载Launcher布局,并将数据转化为View,绑定到界面上,由此我们就可以看到桌面显示的宫格列表的桌面图标了;
public void run() {
...
try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {
// 查询数据库整理App信息,转化为View绑定到界面
loadWorkspace();
mResults.bindWorkspace();
loadAllApps();
mResults.bindAllApps();
loadDeepShortcuts();
mResults.bindDeepShortcuts();
mBgDataModel.widgetsModel.update(mApp, null);
mResults.bindWidgets();
transaction.commit();
} catch (CancellationException e) {
// Loader stopped, ignore
TraceHelper.partitionSection(TAG, "Cancelled");
}
TraceHelper.endSection(TAG);
}
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.shuli.cc/?p=12435,转载请注明出处。
评论0