Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 Framework 解析:Launcher3 的啟動與資料載入流程

編輯:關於 Android 程式設計

本文以 Android 4.4 為對象,梳理預設 Home 應用 Launcher3 的兩件事:它是怎麼被拉起來的,以及它的資料是怎麼載入的。Launcher 的啟動發生在開機時 ActivityManagerService 準備好之後,整體序列如下圖。

一、AMS 準備完成,找出 Home

SystemServer 中,ActivityManagerService 初始化完成後便進入 resume 流程。此時 mStacks 裡已經放進 mHomeStack,實際上也就只有它一個 ActivityStack。

boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target,
        Bundle targetOptions) {
    if (targetStack == null) {
        targetStack = getFocusedStack();   // 取得 mHomeStack
    }
    boolean result = false;
    for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
        final ActivityStack stack = mStacks.get(stackNdx);
        if (isFrontStack(stack)) {
            if (stack == targetStack) {
                result = stack.resumeTopActivityLocked(target, targetOptions);
            } else {
                stack.resumeTopActivityLocked(null);
            }
        }
    }
    return result;
}

接著透過 getHomeIntent() 加上 CATEGORY_HOME 類別去解析出 Home 應用,再交給 mStackSupervisor 啟動:

boolean startHomeActivityLocked(int userId) {
    if (mHeadless) {
        ensureBootCompleted();
        return false;
    }
    Intent intent = getHomeIntent();          // 帶有 CATEGORY_HOME 的 Intent
    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);

        // 若 Home 應用尚未啟動,getProcessRecordLocked 會回傳 null
        ProcessRecord app = getProcessRecordLocked(
                aInfo.processName, aInfo.applicationInfo.uid, true);
        if (app == null || app.instrumentationClass == null) {
            intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
            mStackSupervisor.startHomeActivity(intent, aInfo);
        }
    }
    return true;
}

Intent getHomeIntent() {
    Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
    intent.setComponent(mTopComponent);
    if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
        intent.addCategory(Intent.CATEGORY_HOME);   // Home 應用的標記
    }
    return intent;
}

之後的 Activity 啟動細節,與一般 startActivity 的流程相同,此處不再重複。

二、Launcher3 的資料載入

Launcher 起來之後,圖標的載入是由工作執行緒 sWorkerThread 異步進行的,順序大致如下:

  1. 開始異步載入應用圖標。
  2. 載入 workspace 上的圖標:先讀取 LauncherProvider 的 Favorites 表。這張表的內容是資料庫首次建立時,從 default_workspace.xml 解析寫入的。接著把圖標資訊綁定上去。
  3. 睡眠等待空閒進程,之後再載入主選單(All Apps)的所有應用圖標,包含 widget 等。

主選單的資料來源在 loadAllApps() 中:

private void loadAllApps() {
    final PackageManager packageManager = mContext.getPackageManager();
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    // 向 PackageManagerService 查詢所有要顯示在桌面上的 app
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    mBgAllAppsList.clear();

    List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
    if (apps == null || apps.isEmpty()) {
        return;
    }

    // 依應用名稱排序
    Collections.sort(apps,
            new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));

    // 建立 AppInfo,這個建構子會順便把圖標 bitmap 載入
    for (int i = 0; i < apps.size(); i++) {
        ResolveInfo app = apps.get(i);
        mBgAllAppsList.add(new AppInfo(packageManager, app, mIconCache, mLabelCache));
    }

    final ArrayList added = mBgAllAppsList.added;
    mBgAllAppsList.added = new ArrayList<>();

    // 回到主執行緒綁定
    mHandler.post(new Runnable() {
        public void run() {
            final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
            if (callbacks != null) {
                callbacks.bindAllApplications(added);
            } else {
                Log.i(TAG, "not binding apps: no Launcher activity");
            }
        }
    });
}

小結

整條路徑可以濃縮成:AMS 準備完成 → 找出 CATEGORY_HOME 的 Home 應用 → 啟動 Launcher3 → 背景執行緒讀取 LauncherProvider 的 Favorites 還原桌面 → 透過 PackageManager 查詢 CATEGORY_LAUNCHER 取得全部應用 → 排序、載入圖標 → 回到主執行緒 bindAllApplications 完成畫面。理解這個順序,後續要改桌面佈局、替換預設 Launcher 或排查圖標顯示異常時,就有明確的定位依據了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved