APP启动流程(一)Launcher到ActivityTaskManagerService

前置知识

  • 当Android系统启动时,首先会创建Zygote进程,Zygote进程启动后会fork若干其他的进程,例如SystemServer进程,Launcher进程。

  • 创建SystemServer进程后会创建SystemServiceManager,启动很多系统服务,例ActivityTaskManagerService,ActivityManagerService,PackageManagerService等。

  • 其中Launcher桌面实际上就是一个Activity,当我们点击桌面icon,也是通过startActivity来启动App的。

在桌面点击一个APP图标启动APP的过程中,涉及到了跨进程通信,APP进程创建,Application和Activity的创建和启动等内容。

本篇文章主要分析点击桌面App图标到ActivityTaskManagerService的过程。

涉及到的几个类

  • packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
  • frameworks/base/core/java/android/app/Instrumentation.java
  • frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

具体过程:

1.找到Launcher桌面的APP图标入口位置

  1. Launcher是一个Activity,加载的布局为R.layout.launcher
  2. launcher.xml中的layout="@layout/all_apps"引入了LauncherAllAppsContainerView,其中layout="@layout/all_apps_rv_layout"引入了AllAppsRecyclerView
  3. 在AllAppsContainerView中为其设置的adapter是AllAppsGridAdapter,点击事件由launcher.getItemOnClickListener()获取,通过ItemClickHandler.INSTANCE单例方法获取点击事件
  4. 最终调用ItemClickHandler的onClick->startAppShortcutOrInfoActivity->launcher.startActivitySafely

packages/apps/Launcher3/src/com/android/launcher3/touch/ItemClickHandler.java

private static void onClick(View v, String sourceContainer) {
    if (v.getWindowToken() == null) return;
    Launcher launcher = Launcher.getLauncher(v.getContext());
    if (!launcher.getWorkspace().isFinishedSwitchingState()) return;
    Object tag = v.getTag();
    if (tag instanceof WorkspaceItemInfo) {
        onClickAppShortcut(v, (WorkspaceItemInfo) tag, launcher, sourceContainer);
    } else if (tag instanceof FolderInfo) {
        if (v instanceof FolderIcon) {
            onClickFolderIcon(v);
        }
    } else if (tag instanceof AppInfo) {
        //点击App图标
        startAppShortcutOrInfoActivity(v, (AppInfo) tag, launcher,
                sourceContainer == null ? CONTAINER_ALL_APPS: sourceContainer);
    } else if (tag instanceof LauncherAppWidgetInfo) {
        if (v instanceof PendingAppWidgetHostView) {
            onClickPendingWidget((PendingAppWidgetHostView) v, launcher);
        }
    }
}

private static void startAppShortcutOrInfoActivity(View v, ItemInfo item, Launcher launcher,
            @Nullable String sourceContainer) {
    Intent intent;
    if (item instanceof PromiseAppInfo) {
        PromiseAppInfo promiseAppInfo = (PromiseAppInfo) item;
        intent = promiseAppInfo.getMarketIntent(launcher);
    } else {
        intent = item.getIntent();
    }
    if (intent == null) {
        throw new IllegalArgumentException("Input must have a valid intent");
    }
    if (item instanceof WorkspaceItemInfo) {
        WorkspaceItemInfo si = (WorkspaceItemInfo) item;
        if (si.hasStatusFlag(WorkspaceItemInfo.FLAG_SUPPORTS_WEB_UI)
                && Intent.ACTION_VIEW.equals(intent.getAction())) {
            intent = new Intent(intent);
            intent.setPackage(null);
        }
    }
    if (v != null && launcher.getAppTransitionManager().supportsAdaptiveIconAnimation()) {
        FloatingIconView.fetchIcon(launcher, v, item, true /* isOpening */);
    }
    //调用
    launcher.startActivitySafely(v, intent, item, sourceContainer);
}

2.startActivitySafely->startActivityForResult

packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item, @Nullable String sourceContainer) {
    //...
    //添加FLAG_ACTIVITY_NEW_TASK
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        boolean isShortcut = (item instanceof WorkspaceItemInfo)
                && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
                || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
                && !((WorkspaceItemInfo) item).isPromise();
        if (isShortcut) {
            //点击Shortcut启动activity
            // Shortcuts need some special checks due to legacy reasons.
            startShortcutIntentSafely(intent, optsBundle, item, sourceContainer);
        } else if (user == null || user.equals(Process.myUserHandle())) {
            //点击app图标启动app
            // Could be launching some bookkeeping activity
            startActivity(intent, optsBundle);
            AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(),
                    Process.myUserHandle(), sourceContainer);
        } else {
            getSystemService(LauncherApps.class).startMainActivity(
                    intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
            AppLaunchTracker.INSTANCE.get(this).onStartApp(intent.getComponent(), user,
                    sourceContainer);
        }
        return true;
    } catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
    ...
    }
    return false;
}


public void startActivity(Intent intent, @Nullable Bundle options) {
    //本质上还是通过startActivityForResult启动activity
    if (options != null) {
        startActivityForResult(intent, -1, options);
    } else {
        startActivityForResult(intent, -1);
    }
}
    
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
    //此时需要启动的Activity的parent还是null
    if (mParent == null) {
        options = transferSpringboardActivityOptions(options);
        //Instrumentation执行了execStartActivity方法
        //mMainThread:Launcher所在的进程
        //mMainThread.getApplicationThread():获取mMainThread.getApplicationThread(),ApplicationThread 继承自 Stub,也就是 Binder,用于进程间通信
        Instrumentation.ActivityResult ar =
            mInstrumentation.execStartActivity(
                this, mMainThread.getApplicationThread(), mToken, this,
                intent, requestCode, options);
        if (ar != null) {
            mMainThread.sendActivityResult(
                mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                ar.getResultData());
        }
        if (requestCode >= 0) {
            mStartedActivity = true;
        }

        cancelInputsAndStartExitTransition(options);
    } else {
        if (options != null) {
            mParent.startActivityFromChild(this, intent, requestCode, options);
        } else {
            mParent.startActivityFromChild(this, intent, requestCode);
        }
    }
}

3.Instrumentation.execStartActivity->ActivityTaskManagerService.startActivity

frameworks/base/core/java/android/app/Instrumentation.java

Instrumentation是系统进程和App进程交互的监测类,执行execStartActivity

public ActivityResult execStartActivity(
        Context who, IBinder contextThread, IBinder token, Activity target,
        Intent intent, int requestCode, Bundle options) {
    IApplicationThread whoThread = (IApplicationThread) contextThread;
    Uri referrer = target != null ? target.onProvideReferrer() : null;
    try {
        intent.migrateExtraStreamToClipData(who);
        intent.prepareToLeaveProcess(who);
        //通过binder机制,ActivityTaskManager.getService()获取到了ActivityTaskManagerservice
        int result = ActivityTaskManager.getService().startActivity(whoThread,
                who.getBasePackageName(), who.getAttributionTag(), intent,
                intent.resolveTypeIfNeeded(who.getContentResolver()), token,
                target != null ? target.mEmbeddedID : null, requestCode, 0, null, options);
        checkStartActivityResult(result, intent);
    } catch (RemoteException e) {
        throw new RuntimeException("Failure from system", e);
    }
    return null;
}

frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java

ActivityTaskManagerService继承了IActivityTaskManager.Stub,这里采用了AIDL的形式获取到了ActivityTaskManagerService的代理类
,然后跨进程通信ActivityTaskManagerService调用startActivity方法启动activity

public static IActivityTaskManager getService() {
    return IActivityTaskManagerSingleton.get();
}

@UnsupportedAppUsage(trackingBug = 129726065)
private static final Singleton IActivityTaskManagerSingleton =
        new Singleton() {
            @Override
            protected IActivityTaskManager create() {
                //通过 ServiceManager 获取对应的系统服务,也就是 IBinder 类型的 ATMS 引用。
                final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
                return IActivityTaskManager.Stub.asInterface(b);
            }
        };

ActivityTaskManagerService extends IActivityTaskManager.Stub

总结:startActivitySafely(Launcher)->startActivityForResult(Activity)->execStartActivity(Instrumentation)->startActivity(ATMS)

参考:Android Code Search

你可能感兴趣的:(APP启动流程(一)Launcher到ActivityTaskManagerService)