以下文章基于 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 --