Launcherプロセス起動

63008 ワード

記事はGithubブログを同期しました:Launcherプロセスが開始しました
使用する関連ソース:https://github.com/JesusYoung/AndroidResourceCode9.0/tree/master
Android 9.0ベース

1、Launcher


LauncherはAndroidシステムのデスクトップとして、2つの役割を果たしています.
  • は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()メソッドを実行し、startHomeActivity Locked()メソッドを実行し、現在のユーザIDを入力する.
    public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
      ...
      synchronized (this) {
        ...
        startHomeActivityLocked(currentUserId, "systemReady");
        ...
      }
      ...
    }
    

    2.2.1、LauncherのIntentを取得する


    startHomeActivity Locked()メソッドでは、まずgetHomeIntent()メソッドにより、起動するHomeActivityのintentオブジェクトが取得され、mTopActionのデフォルトはINTENTである.ACTION_MAIN、CATEGORYを追加HOMEのcategoryマーク
    Intentオブジェクトを取得し、PackageManagerで該当するActivityを取得し、対応するActivity Infoを取得し、対応するプロセスレコードを取得します.この場合、対応するプロセスはまだ起動していません.その後、実行を続け、intentにFLAG_を追加します.ACTIVITY_NEW_TASKはパラメータを起動し、新しいスタックを開き、その後Activity StartControllerクラスの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()メソッドを呼び出してActivity Starterオブジェクトを取得し、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();
      }
    }
    

    Activity Starterの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<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の構造方法では、Appアイコンのサイズ、文字サイズ、ワークスペースやフォルダごとにどのくらいのAppが表示されるかなど、Appの基本的な構成情報を格納するInvariantDeviceProfileオブジェクトが新設されます.
    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放送の登録


    BroadcastReceiverであるLauncherModelオブジェクトを新規作成し、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のレイアウトを解析します.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      ...
      mModel = app.setLauncher(this);
      ...
      mLauncherView = LayoutInflater.from(this).inflate(R.layout.launcher, null);
      ...
    }
    
    # LauncherModel
    public void initialize(Callbacks callbacks) {
      synchronized (mLock) {
        Preconditions.assertUIThread();
        mCallbacks = new WeakReference<>(callbacks);
      }
    }
    

    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);
    }