Launcher进程启动流程

程序员 2024-9-24 03:45:10 95 0 来自 中国
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()方法

@Overrideprotected 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<LauncherAppState>() {          @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<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));  ...}ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {  ArrayList<InvariantDeviceProfile> 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;
@Overrideprotected 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);}
来自:https://www.yuque.com/jesus_yangshijie/ruafsa/yl7sey
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-11-24 08:32, Processed in 0.155125 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表