美文网首页
Launcher启动过程分析

Launcher启动过程分析

作者: JeromeWang | 来源:发表于2020-10-30 10:46 被阅读0次

    launcher其实也是一个app,主要功能是对手机上安装的所有APP以及小部件,文件夹,快捷方式等等一个管理,在AndroidManifest文件配置上比其他app多了一个“<category android:name="android.intent.category.HOME" />”属性,在启动系统或者按Home键时会过滤这个属性,一般手机系统中只有一个应用具有这个属性,所以启动后会直接跳转到这个界面,也就是这个launcher。如果开发launcher应用,运行代码后手机系统会让用户做出选择。下面主要分析一下launcher启动过程中涉及到的主要操作。过程分析是基于Android 28 API的。我们知道再Android系统服务初始化过程中会调用ActivityManagerService.systemReady方法

    public class ActivityManagerService extends IActivityManager.Stub
            implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
    
     public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {   
          .....
             synchronized (this) {
               ....
                startHomeActivityLocked(currentUserId, "systemReady");
                ....
            }
        }
    
      boolean startHomeActivityLocked(int userId, String reason) {
            if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                    && mTopAction == null) {
                // We are running in factory test mode, but unable to find
                // the factory test app, so just sit around displaying the
                // error message and don't try to start anything.
                return false;
            }
            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);
                }
            } else {
                Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
            }
    
            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;
        }
    }
    

    systemReady方法里面会调用startHomeActivityLocked方法,startHomeActivityLocked首先创建一个CATEGORY_HOME类型的Intent,然后通过Intent.resolveActivityInfo函数向PackageManagerService查询Category类型为HOME的Activity,如果不为空,则startHomeActivity,调用这个函数的最后结果就是把com.android.launcher3.Launcher启动起来.

    public class Launcher extends BaseDraggingActivity implements LauncherExterns,
            Callbacks, LauncherProviderChangeListener, UserEventDelegate,
            InvariantDeviceProfile.OnIDPChangeListener {
    
         @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);
            }
             .....
        }
    
    }
    

    这里的mModel是一个LauncherModel类型的成员变量,这里通过调用它的startLoader成员函数来执行加载应用数据的操作。

    public class LauncherModel extends BroadcastReceiver
            implements LauncherAppsCompat.OnAppsChangedCallbackCompat {
    
      public boolean startLoader(int synchronousBindPage) {
            // Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
            InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_LOADER_RUNNING);
            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) {
                    final Callbacks oldCallbacks = mCallbacks.get();
                    // Clear any pending bind-runnables from the synchronized load process.
                    MAIN_EXECUTOR.execute(oldCallbacks::clearPendingBinds);
    
                    // If there is already one running, tell it to stop.
                    stopLoader();
                    LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel,
                            mBgAllAppsList, synchronousBindPage, mCallbacks);
                    if (mModelLoaded && !mIsLoaderTaskRunning) {
                        // Divide the set of loaded items into those that we are binding synchronously,
                        // and everything else that is to be bound normally (asynchronously).
                        loaderResults.bindWorkspace();
                        // For now, continue posting the binding of AllApps as there are other
                        // issues that arise from that.
                        loaderResults.bindAllApps();
                        loaderResults.bindDeepShortcuts();
                        loaderResults.bindWidgets();
                        return true;
                    } else {
                        startLoaderForResults(loaderResults);
                    }
                }
            }
            return false;
        }
    
    
        public void startLoaderForResults(LoaderResults results) {
            synchronized (mLock) {
                stopLoader();
                mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, results);
    
                // Always post the loader task, instead of running directly (even on same thread) so
                // that we exit any nested synchronized blocks
                MODEL_EXECUTOR.post(mLoaderTask);
            }
        }
    }
    

    首次启动时mModelLoaded为false,所以会走startLoaderForResults,接着新建一个LoaderTask任务,LoaderTask实现了Runnable这个接口,执行run方法,我们看一下这个run方法里面做了哪些事情,

    public class LoaderTask implements Runnable {
    
        public void run() {
            synchronized (this) {
                // Skip fast if we are already stopped.
                if (mStopped) {
                    return;
                }
            }
    
            TraceHelper.beginSection(TAG);
            try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {
                TraceHelper.partitionSection(TAG, "step 1.1: loading workspace");
                loadWorkspace();
    
                verifyNotStopped();
                TraceHelper.partitionSection(TAG, "step 1.2: bind workspace workspace");
                mResults.bindWorkspace();
    
                // Notify the installer packages of packages with active installs on the first screen.
                TraceHelper.partitionSection(TAG, "step 1.3: send first screen broadcast");
                sendFirstScreenActiveInstallsBroadcast();
    
                // Take a break
                TraceHelper.partitionSection(TAG, "step 1 completed, wait for idle");
                waitForIdle();
                verifyNotStopped();
    
                // second step
                TraceHelper.partitionSection(TAG, "step 2.1: loading all apps");
                List<LauncherActivityInfo> allActivityList = loadAllApps();
    
                TraceHelper.partitionSection(TAG, "step 2.2: Binding all apps");
                verifyNotStopped();
                mResults.bindAllApps();
    
                verifyNotStopped();
                TraceHelper.partitionSection(TAG, "step 2.3: Update icon cache");
                IconCacheUpdateHandler updateHandler = mIconCache.getUpdateHandler();
                setIgnorePackages(updateHandler);
                updateHandler.updateIcons(allActivityList,
                        LauncherActivityCachingLogic.newInstance(mApp.getContext()),
                        mApp.getModel()::onPackageIconsUpdated);
    
                // Take a break
                TraceHelper.partitionSection(TAG, "step 2 completed, wait for idle");
                waitForIdle();
                verifyNotStopped();
    
                // third step
                TraceHelper.partitionSection(TAG, "step 3.1: loading deep shortcuts");
                loadDeepShortcuts();
    
                verifyNotStopped();
                TraceHelper.partitionSection(TAG, "step 3.2: bind deep shortcuts");
                mResults.bindDeepShortcuts();
    
                // Take a break
                TraceHelper.partitionSection(TAG, "step 3 completed, wait for idle");
                waitForIdle();
                verifyNotStopped();
    
                // fourth step
                TraceHelper.partitionSection(TAG, "step 4.1: loading widgets");
                List<ComponentWithLabel> allWidgetsList = mBgDataModel.widgetsModel.update(mApp, null);
    
                verifyNotStopped();
                TraceHelper.partitionSection(TAG, "step 4.2: Binding widgets");
                mResults.bindWidgets();
    
                verifyNotStopped();
    
                TraceHelper.partitionSection(TAG, "step 4.3: save widgets in icon cache");
                updateHandler.updateIcons(allWidgetsList, new ComponentCachingLogic(
                        mApp.getContext(), true), mApp.getModel()::onWidgetLabelsUpdated);
    
                verifyNotStopped();
                TraceHelper.partitionSection(TAG, "step 5: Finish icon cache update");
                updateHandler.finish();
    
                transaction.commit();
            } catch (CancellationException e) {
                // Loader stopped, ignore
                TraceHelper.partitionSection(TAG, "Cancelled");
            }
            TraceHelper.endSection(TAG);
        }
    
    }
    

    从代码中可以清晰的看到每一步操作所要做的工作。loadWorkspace和bindWorkspace,也就是加载workspace的应用并且进行绑定,waitForIdle方法主要是等待加载数据结束。sendFirstScreenActiveInstallsBroadcast发送首屏广播。loadAllApps和bindAllApps加载并绑定所有的APP信息,loadDeepShortcuts和bindDeepShortcuts加载并绑定所有的快捷方式,然后加载并绑定所有的小部件。至此launcher数据加载基本就完成了。

    相关文章

      网友评论

          本文标题:Launcher启动过程分析

          本文链接:https://www.haomeiwen.com/subject/muhlvktx.html