Framework - AMS

reference article

1. Concept

Starting with Android10 (API29), ActivityTaskManagerService takes over ActivityManagerService.

2. Start the ATMS process

3. Start the APP & jump to the Activity process

  1. Call Activity's startActivity method to start the target Activity
  2. Then it will call the execStartActivity method of Instrumentation, get the binder proxy object of ATMS, and then call it to the startActivity of ATMS
  3. After being called into ATMS, ActivityStarterthe execute method will be executed, and executeRequest will be executed internally, and then some verification and permission will be performed, including process check, intent check, permission check, etc., which will be created later to save the ActivityRecordActivity related information,
  4. Then it will calculate the flag according to the startup mode, and set the Task stack to start the Activity.
  5. In ActivityTaskSupervisor, check whether the Activity process to be started exists, if it exists, call back the client process ApplicationThread to start the Activity, otherwise create a process.
  6. It will be transferred to ActivityThread and start to execute the transaction returned by system_server callback in TransactionExecute, process various callbacks, and switch to the corresponding life cycle
  7. Finally, it calls back to the handleLaunchActivity of ActivityThread to start the Activity. The performLaunchActivity method is called in it.
  8. Create an Activity instance through reflection in performLaunchActivity. If there is no Application, create it first, then call the Activity's attach method to initialize, and finally call back the activity's onCreate method.

3.1 Activity → ATMS

Starting an APP or jumping to an Activity is to call startActivity(), and then call startActivityForResult().

3.1.1 Activity 

Activity{
    public void startActivity(Intent intent, @Nullable Bundle options) {
        startActivityForResult(intent, -1, options);
    }
    public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
        //委托给Instrumentation来启动
        Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options);
        mMainThread.sendActivityResult(mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData());
    }
}

3.1.2 Instrumentation

Instrumentation{
    //形参who:用来启动Activity的对象
    //形参contextThread:Binder对象,具有跨进程通信的能力,传入的是mMainThread.getApplicationThread()
    //形参token:Binder对象,指向了服务端一个 ActivityRecord 对象
    //形参target:当前Activity
    public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, String target, Intent intent, int requestCode, Bundle options) {
        //获取一个Binder对象,转为AIDL所属类型(ATMS的proxy),然后调用ATMS的startActivity()进行跨进程通信
        int result = ActivityTaskManager.getService().startActivity(whoThread,
            who.getOpPackageName(), who.getAttributionTag(), intent,
            intent.resolveTypeIfNeeded(who.getContentResolver()), token, target,
            requestCode, 0, null, options);
        //判断能否启动Activity,不能启动就会抛出异常,例如activity未在manifest中声明等。
        checkStartActivityResult(result, intent);
    }
}
ActivityTaskManager{
    public static IActivityTaskManager getService() {
        return IActivityTaskManagerSingleton.get();
    }
    //一种单例模式
    private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton = new Singleton<IActivityTaskManager>() {
        @Override
        protected IActivityTaskManager create() {
            //获取ATMS的代理对象Binder
            final IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE);
            return IActivityTaskManager.Stub.asInterface(b);
        }
    };
}

3.2 ATMS → PMS

ActivityTaskManagerService{
    public final int startActivity(IApplicationThread caller, String callingPackage, String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
        return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, UserHandle.getCallingUserId());
    }
    private int startActivityAsUser(IApplicationThread caller, String callingPackage, @Nullable String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {
        //获取ActivityStarter实例,去执行execute方法
        return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
            .setCaller(caller)
            ...
            .execute();
    }
}
ActivityStarter{
    int execute() {
        if (mRequest.activityInfo == null) {
            mRequest.resolveActivity(mSupervisor);
        }
    }
    static class Request {
        void resolveActivity(ActivityTaskSupervisor supervisor) {
            //AndroidManifest中intent标签
            resolveInfo = supervisor.resolveIntent(intent, resolvedType, userId, 0, computeResolveFilterUid(callingUid, realCallingUid, filterCallingUid));
            //转为Activity标签
            activityInfo = supervisor.resolveActivity(intent, resolveInfo, startFlags, profilerInfo);
        }
    }
}
ActivityTaskSupervisor{
    ResolveInfo resolveIntent(Intent intent, String resolvedType, int userId, int flags, int filterCallingUid) {
        //通过ATMS获取抽象类PackageManagerInternal
        //PMS内部类PackageManagerInternalImpl实现了它(内部类持有外部类,就可以调用PMS的方法了)
        //resolveIntent()会将从PMS的mPackages中查询到的ActivityInfo赋值给ResultInfo
        mService.getPackageManagerInternalLocked().resolveIntent(intent, resolvedType, modifiedFlags, privateResolveFlags, userId, true, filterCallingUid);
    }
    //将获取的ResultInfo转为ActivityInfo
    ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags, ProfilerInfo profilerInfo, int userId, int filterCallingUid) {
        final ResolveInfo rInfo = resolveIntent(intent, resolvedType, userId, 0, filterCallingUid);
        return resolveActivity(intent, rInfo, startFlags, profilerInfo);
    }
}

3.3 AMS → ApplicationThread

Guess you like

Origin blog.csdn.net/HugMua/article/details/131502514