Android Activity的启动过程-5 之 Task(2) TaskInfo

以下文章基于 Android 13/14 源码. (20230801 codesearch main 分支)

TaskInfo 可以看做是对 Task 内容的封装, 并且它实现了 序列化可进程间传输.
可以提供给 App 使用.(在 android/app包下)

在 system_server (wm) 启动 Activity 并且 创建 或 更新 Task 时,
创建
TaskInfo 对象,并且调用 Task.fillTaskInfo 将对 传递过来的 TaskInfo 进行填充内容.

1. fillTaskInfo 的调用栈

08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.fillTaskInfo(Task.java:4673)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.fillTaskInfo(Task.java:4497)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.fillTaskInfo(Task.java:4493)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.getTaskInfo(Task.java:4769)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.updateTaskDescription(Task.java:2423)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.updateEffectiveIntent(Task.java:2492)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.onDescendantActivityAdded(Task.java:1972)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.TaskFragment.addChild(TaskFragment.java:2291)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.Task.addChild(Task.java:1924)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.addOrReparentStartingActivity(ActivityStarter.java:4617)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.setNewTask(ActivityStarter.java:4552)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.startActivityInner(ActivityStarter.java:2997)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.startActivityUnchecked(ActivityStarter.java:2523)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1861)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:1049)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1955)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1827)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at android.app.IActivityTaskManager$Stub.onTransact(IActivityTaskManager.java:1364)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at com.android.server.wm.ActivityTaskManagerService.onTransact(ActivityTaskManagerService.java:6659)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at android.os.Binder.execTransactInternal(Binder.java:1316)
08-01 16:56:25.825 E 2464 3221 DebugOnly: at android.os.Binder.execTransact(Binder.java:1280)

1.1 fillTaskInfo 源码

frameworks/base/services/core/java/com/android/server/wm/Task.java
会根据当前 Task 的属性值,对传入的 TaskInfo 对象info 进行填充.

class Task extends TaskFragment {
    /**
     * Fills in a {@link TaskInfo} with information from this task. Note that the base intent in the
     * task info will not include any extras or clip data.
     */
    void fillTaskInfo(TaskInfo info) {
        fillTaskInfo(info, true /* stripExtras */);
    }

    void fillTaskInfo(TaskInfo info, boolean stripExtras) {
        fillTaskInfo(info, stripExtras, getDisplayArea());
    }

    /**
     * Fills in a {@link TaskInfo} with information from this task.
     *
     * @param tda consider whether this Task can be put in multi window as it will be attached to
     *            the give {@link TaskDisplayArea}.
     */
    void fillTaskInfo(TaskInfo info, boolean stripExtras, @Nullable TaskDisplayArea tda) {
        info.launchCookies.clear();
        info.addLaunchCookie(mLaunchCookie);
        final ActivityRecord top = mTaskSupervisor.mTaskInfoHelper.fillAndReturnTop(this, info);

        info.userId = isLeafTask() ? mUserId : mCurrentUser;
        info.taskId = mTaskId;
        info.displayId = getDisplayId();
        info.displayAreaFeatureId = tda != null ? tda.mFeatureId : FEATURE_UNDEFINED;
        final Intent baseIntent = getBaseIntent();
        // Make a copy of base intent because this is like a snapshot info.
        // Besides, {@link RecentTasks#getRecentTasksImpl} may modify it.
        final int baseIntentFlags = baseIntent == null ? 0 : baseIntent.getFlags();
        info.baseIntent = baseIntent == null
                ? new Intent()
                : stripExtras ? baseIntent.cloneFilter() : new Intent(baseIntent);
        info.baseIntent.setFlags(baseIntentFlags);

        info.isRunning = top != null;
        info.topActivity = top != null ? top.mActivityComponent : null;
        info.origActivity = origActivity;
        info.realActivity = realActivity;
        info.lastActiveTime = lastActiveTime;
        info.taskDescription = new ActivityManager.TaskDescription(getTaskDescription());
        info.supportsMultiWindow = supportsMultiWindowInDisplayArea(tda);
        info.configuration.setTo(getConfiguration());
        // Update to the task's current activity type and windowing mode which may differ from the
        // window configuration
        info.configuration.windowConfiguration.setActivityType(getActivityType());
        info.configuration.windowConfiguration.setWindowingMode(getWindowingMode());
        info.token = mRemoteToken.toWindowContainerToken();

        //TODO (AM refactor): Just use local once updateEffectiveIntent is run during all child
        //                    order changes.
        final Task topTask = top != null ? top.getTask() : this;
        info.resizeMode = topTask.mResizeMode;
        info.topActivityType = topTask.getActivityType();
        info.displayCutoutInsets = topTask.getDisplayCutoutInsets();
        info.isResizeable = isResizeable();
        info.minWidth = mMinWidth;
        info.minHeight = mMinHeight;
        info.defaultMinSize = mDisplayContent == null
                ? DEFAULT_MIN_TASK_SIZE_DP : mDisplayContent.mMinSizeOfResizeableTaskDp;
        info.positionInParent = getRelativePosition();

        info.topActivityInfo = top != null ? top.info : null;
        info.pictureInPictureParams = getPictureInPictureParams(top);
        info.launchIntoPipHostTaskId = (info.pictureInPictureParams != null
                && info.pictureInPictureParams.isLaunchIntoPip()
                && top.getLastParentBeforePip() != null)
                        ? top.getLastParentBeforePip().mTaskId : INVALID_TASK_ID;
        info.lastParentTaskIdBeforePip = top != null && top.getLastParentBeforePip() != null
                ? top.getLastParentBeforePip().mTaskId : INVALID_TASK_ID;
        info.shouldDockBigOverlays = top != null && top.shouldDockBigOverlays;
        info.mTopActivityLocusId = top != null ? top.getLocusId() : null;

        final boolean isTopActivityResumed = top != null
                && top.getOrganizedTask() == this && top.isState(RESUMED);
        final boolean isTopActivityVisible = top != null
                && top.getOrganizedTask() == this && top.isVisible();
        // Whether the direct top activity is in size compat mode
        info.topActivityInSizeCompat = isTopActivityVisible && top.inSizeCompatMode();
        if (info.topActivityInSizeCompat
                && mWmService.mLetterboxConfiguration.isTranslucentLetterboxingEnabled()) {
            // We hide the restart button in case of transparent activities.
            info.topActivityInSizeCompat = top.fillsParent();
        }
        // Whether the direct top activity is eligible for letterbox education.
        info.topActivityEligibleForLetterboxEducation = isTopActivityResumed
                && top.isEligibleForLetterboxEducation();
        // Whether the direct top activity requested showing camera compat control.
        info.cameraCompatControlState = isTopActivityResumed
                ? top.getCameraCompatControlState()
                : TaskInfo.CAMERA_COMPAT_CONTROL_HIDDEN;

        final Task parentTask = getParent() != null ? getParent().asTask() : null;
        info.parentTaskId = parentTask != null && parentTask.mCreatedByOrganizer
                ? parentTask.mTaskId
                : INVALID_TASK_ID;
        info.isFocused = isFocused();
        info.isVisible = hasVisibleChildren();
        info.isSleeping = shouldSleepActivities();
        info.isLetterboxDoubleTapEnabled = top != null
                && top.mLetterboxUiController.isLetterboxDoubleTapEducationEnabled();
        info.topActivityLetterboxVerticalPosition = TaskInfo.PROPERTY_VALUE_UNSET;
        info.topActivityLetterboxHorizontalPosition = TaskInfo.PROPERTY_VALUE_UNSET;
        info.topActivityLetterboxWidth = TaskInfo.PROPERTY_VALUE_UNSET;
        info.topActivityLetterboxHeight = TaskInfo.PROPERTY_VALUE_UNSET;
        info.isFromLetterboxDoubleTap = top != null && top.mLetterboxUiController.isFromDoubleTap();
        if (info.isLetterboxDoubleTapEnabled) {
            info.topActivityLetterboxWidth = top.getBounds().width();
            info.topActivityLetterboxHeight = top.getBounds().height();
            if (info.topActivityLetterboxWidth < info.topActivityLetterboxHeight) {
                // Pillarboxed
                info.topActivityLetterboxHorizontalPosition =
                        top.mLetterboxUiController.getLetterboxPositionForHorizontalReachability();
            } else {
                // Letterboxed
                info.topActivityLetterboxVerticalPosition =
                        top.mLetterboxUiController.getLetterboxPositionForVerticalReachability();
            }
        }
    }
}

可见,
获取当前 任务栈顶的 activity 是通过 ActivityTaskSupervisor 对象去获取的:

final ActivityRecord top = mTaskSupervisor.mTaskInfoHelper.fillAndReturnTop(this, info);

mTaskSupervisor在 Task 的父类 TaskFragment中声明 并赋值:
frameworks/base/services/core/java/com/android/server/wm/TaskFragment.java

mTaskSupervisor = mAtmService.mTaskSupervisor;

由此可见, ActivityTaskSupervisor 也可以看做是ATMS 的一个工具类.

1.2 getTaskInfo 源码

从上面的调用栈看, 会通过 getTaskInfo 创建 RunningTaskInfo (TaskInfo的子类),
正是这里面调用了 fillTaskInfo方法,填充了刚才创建的 RunningTaskInfo 对象.
frameworks/base/services/core/java/com/android/server/wm/Task.java

    /**
     * Returns a {@link TaskInfo} with information from this task.
     */
    ActivityManager.RunningTaskInfo getTaskInfo() {
        ActivityManager.RunningTaskInfo info = new ActivityManager.RunningTaskInfo();
        fillTaskInfo(info);
        return info;
    }

2. RunningTaskInfo 源码

上面分析可知, 会创建 RunningTaskInfo 对象并根据当前 Task对象,封装RunningTaskInfo 并返回给调用者。
这就相当于 调用者 不直接访问 Task, 而是访问它的 代理对象 即 RunningTaskInfo。

RunningTaskInfo 的源码是在 android/app 包里的.
作为 ActivityManager内部静态类
继承自 TaskInfo并实现 Parcelable接口(表示可以跨进程传递)
frameworks/base/core/java/android/app/ActivityManager.java

    public static class RunningTaskInfo extends TaskInfo implements Parcelable {

        /**
         * A unique identifier for this task.
         *
         * @deprecated As of {@link android.os.Build.VERSION_CODES#Q}, use
         * {@link RunningTaskInfo#taskId}.
         */
        @Deprecated
        public int id;
        public RunningTaskInfo() {
        }

        private RunningTaskInfo(Parcel source) {
            readFromParcel(source);
        }
        public void readFromParcel(Parcel source) {
            id = source.readInt();
            super.readFromParcel(source);
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            super.writeToParcel(dest, flags);
        }

        public static final @android.annotation.NonNull Creator<RunningTaskInfo> CREATOR = new Creator<RunningTaskInfo>() {
            public RunningTaskInfo createFromParcel(Parcel source) {
                return new RunningTaskInfo(source);
            }
            public RunningTaskInfo[] newArray(int size) {
                return new RunningTaskInfo[size];
            }
        };

从这里的代码可以更加明确的是, RunningTaskInfo 实际上就是 TaskInfo 的代理,
仅是多实现了 Parcelable 接口,可以跨进程传输,仅此而已.

3. TaskInfo 源码

TaskInfo 也是 android/app 目录下的类. 即表示可以给 App 使用.

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

package android.app;
/**
 * Stores information about a particular Task.
 */
public class TaskInfo {
    private static final String TAG = "TaskInfo";
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    public int userId;

    /**
     * The identifier for this task.
     */
    public int taskId;

    /**
     * Whether or not this task has any running activities.
     */
    public boolean isRunning;
    /**
     * The base intent of the task (generally the intent that launched the task). This intent can
     * be used to relaunch the task (if it is no longer running) or brought to the front if it is.
     */
    @NonNull
    public Intent baseIntent;
    /**
     * The component of the top activity in the task, currently showing to the user.
     */
    @Nullable
    public ComponentName topActivity;

    /**
     * The component of the target activity if this task was started from an activity alias.
     * Otherwise, this is null.
     */
    @Nullable
    public ComponentName origActivity;

    /**
     * The component of the activity that started this task (may be the component of the activity
     * alias).
     * @hide
     */
    @Nullable
    public ComponentName realActivity;  
    
    /**
     * The {@link ActivityInfo} of the top activity in this task.
     * @hide
     */
    @Nullable
    public ActivityInfo topActivityInfo;

    /**
     * The identifier of the parent task that is created by organizer, otherwise
     * {@link ActivityTaskManager#INVALID_TASK_ID}.
     * @hide
     */
    public int parentTaskId;

    /**
     * Whether this task is focused.
     * @hide
     */
    public boolean isFocused;

    /**
     * Whether this task is visible.
     * @hide
     */
    public boolean isVisible;
    ... 

可以看到,大部分是 Task 里的属性.
它的方法主要 也是 序列化 的操作, 即从序列化里读取信息 或写入信息,用于进程间的 传输.

    private TaskInfo(Parcel source) {
        readFromParcel(source);
    }
    /**
     * Reads the TaskInfo from a parcel.
     */
    void readFromParcel(Parcel source) {
        userId = source.readInt();
        taskId = source.readInt();
        displayId = source.readInt();
        isRunning = source.readBoolean();
         ....
    }
    /**
     * Writes the TaskInfo to a parcel.
     */
    void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(userId);
        dest.writeInt(taskId);
        dest.writeInt(displayId);
        ...
    }       

-- End Now --

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,817评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,329评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,354评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,498评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,600评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,829评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,979评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,722评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,189评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,519评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,654评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,329评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,940评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,762评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,993评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,382评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,543评论 2 349

推荐阅读更多精彩内容