版权说明:本文为 开开向前冲 原创文章,转载请注明出处;
注:限于作者水平有限,文中有不对的地方还请指教
提到ActivityManagerService,大家都知道是Android 用于管理四大组件的系统服务,AMS具体是怎么管理四大组件的呢?
ActivityManagerService要管理四大组件,那四大组件就必须在AMS中有存在的形式,这里先从AMS 如何管理Activity 谈起;
Activity在AMS 中存在的形式为ActivityRecord;
AMS以Task的方式管理Activity,Task在AMS存在的形式为TaskRecord;TaskRecord中的mActivities用栈的方式管理ActivityRecord;
TaskRecord在AMS中依靠ActivityStack去管理,从命名来看,ActivityStack更像Activity栈,但是ActivityStack并不是Activity 栈,而是负责管理TaskRecord的类,android系统中有三种ActivityStack(mHomeStack,mFocusedStack,mLastFocusedStack);
ActivityStack同样有管理者,ActivityStackSupervisor负责管理ActivityStack;
每一个Activity和TaskRecord 都是属于某个进程,所以进程还需要在AMS有存在形式,进程在AMS中存在的形式就是ProcessRecord;
TaskRecord官网
这里逐一介绍一下上面几个数据结构:
ActivityRecord
ActivityRecord是Activity在AMS中的存在形式,ActivityRecord保存了Activity的信息;
------> frameworks/base/services/core/java/com/android/server/am/ActivityRecord.java
//ams
final ActivityManagerService service; // owner
//token用来和wms交互
final IApplicationToken.Stub appToken; // window manager token
final ActivityInfo info; // all about ActivityInfo
final ApplicationInfo appInfo; // information about activity's app
int mActivityType; //Activity Type
......
//Activity资源信息,我们在AndroidManifest.xml中配置的内容
CharSequence nonLocalizedLabel; // the label information from the package mgr.
int labelRes; // the label information from the package mgr.
int icon; // resource identifier of activity's icon.
int logo; // resource identifier of activity's logo.
int theme; // resource identifier of activity's theme.
int realTheme; // actual theme resource we will use, never 0.
int windowFlags; // custom window flags for preview window.
//ActivityRecord所在的TaskRecord
TaskRecord task; // the task this is in.
......
//ActivityRecord所在进程
ProcessRecord app; // if non-null, hosting application
ActivityState state; // current state we are in
......
TaskRecord task变量表示该ActivityRecord所在的TaskRecord;
ActivityRecord 还规定了Activity的类型如下:
static final int APPLICATION_ACTIVITY_TYPE = 0;
static final int HOME_ACTIVITY_TYPE = 1;
static final int RECENTS_ACTIVITY_TYPE = 2;
TaskRecord
我们都知道AMS以Task的方式在管理Activity,TaskRecord中的mActivities是一个栈,它的作用是以栈的方式组织管理Activity。Android把用户一次操作相关的Activity按照先后顺序保存在一个Task中,这个Task在AMS中的存在形式就是TaskRecord;
------> /frameworks/base/services/core/java/com/android/server/am/TaskRecord.java
final class TaskRecord {
...........
final int taskId; // Unique identifier for this task.
int mAffiliatedTaskId; // taskId of parent affiliation or self if no parent.
// 是指root activity的affinity,即该Task中第一个Activity; 可以理解为当前task的name;
String affinity; // The affinity name for this task, or null; may change identity.
// 启动这个task的intent
Intent intent; // The original intent that started the task.
long firstActiveTime; // First time this task was active.
long lastActiveTime; // Last time this task was active, including sleep.
boolean inRecents; // Actually in the recents list?
boolean isAvailable; // Is the activity available to be launched?
// task模式
int mLockTaskMode; // Which tasklock mode to launch this task in. One of
// ActivityManager.LOCK_TASK_LAUNCH_MODE_*
/** List of all activities in the task arranged in history order */
// 该Task中所有的Activity
final ArrayList<ActivityRecord> mActivities;
/** Current stack */
// 管理该Task的ActivityStack
ActivityStack stack;
// 最近列表中,可以看到当前Task的缩略图
private Bitmap mLastThumbnail; // Last thumbnail captured for this item.
private final File mLastThumbnailFile; // File containing last thumbnail.
final ActivityManagerService mService;
..........
}
TaskRecord 的affinity只有在其被创建的时候才有用,以后加入这个Task的Activity,即使他们通过taskAffinity指定了一个不同的字符串,也不会更改Task的名称;Activity所在的Task通过AndroidManifest.xml中<Activity>标签中的android:taskAffinity="xxx"来指定,通常不去主动设置一个Activity的taskAffinity属性,那么taskAffinity的值缺省使用包名。正因为如此,应用中所有的Activity的taskAffinity属性值默认都是相同的,都是包名,所以在应用中使用FLAG_ACTIVITY_NEW_TASK标志去启动一个本应用中的一个Activity,也不会创建一个新的task,除非这个Activity 额外指定了不同的taskAffinity属性值;
TaskAffinity属性使用小结
ActivityStack
ActivityStack充当TaskRecord的Manager角色;
------> /frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
/**
* The back history of all previous (and possibly still
* running) activities. It contains #TaskRecord objects.
*/
private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>(); //ActivityStack中所有的TaskRecord
private final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>();//最近活动过的ActivityRecord
......
final int mStackId; //ActivityStack的唯一标识
final ActivityContainer mActivityContainer;
/** The other stacks, in order, on the attached display. Updated at attach/detach time. */
ArrayList<ActivityStack> mStacks; //绑定的ActivityDisplay中的所有ActivityStack
/** The attached Display's unique identifier, or -1 if detached */
int mDisplayId;//绑定的ActivityDisplay的id,默认为Display.DEFAULT_DISPLAY = 0;
......
/** Run all ActivityStacks through this */
final ActivityStackSupervisor mStackSupervisor; //ActivityStack的管理者ActivityStackSupervisor
mTaskHistory:是一个列表,存储的是ActivityStack中的所有TaskRecord对象,TaskRecord 通过mActivities变量存储Task中所有的Activity,所以mTaskHistory间接管理了ActivityStack中的所有activity;
mLRUActivities:一个列表,存储的是ActivityStack中按照最近活动情况运行的所有Activity;
ActivityStackSupervisor
ActivityStackSupervisor 用于管理ActivityStack;ActivityStackSupervisor为AMS提供管理方法;管理着系统中的三个ActivityStack;
------>/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
final ActivityManagerService mService; //
final ActivityStackSupervisorHandler mHandler;
......
/** The stack containing the launcher app. Assumed to always be attached to
* Display.DEFAULT_DISPLAY. */
private ActivityStack mHomeStack;
/** The stack currently receiving input or launching the next activity. */
private ActivityStack mFocusedStack;
/** If this is the same as mFocusedStack then the activity on the top of the focused stack has
* been resumed. If stacks are changing position this will hold the old stack until the new
* stack becomes resumed after which it will be set to mFocusedStack. */
private ActivityStack mLastFocusedStack;
/** List of activities that are waiting for a new activity to become visible before completing
* whatever operation they are supposed to do. */
final ArrayList<ActivityRecord> mWaitingVisibleActivities = new ArrayList<ActivityRecord>();
/** List of processes waiting to find out about the next visible activity. */
final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible =
new ArrayList<IActivityManager.WaitResult>();
/** List of processes waiting to find out about the next launched activity. */
final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched =
new ArrayList<IActivityManager.WaitResult>();
/** List of activities that are ready to be stopped, but waiting for the next activity to
* settle down before doing so. */
final ArrayList<ActivityRecord> mStoppingActivities = new ArrayList<ActivityRecord>();
/** List of activities that are ready to be finished, but waiting for the previous activity to
* settle down before doing so. It contains ActivityRecord objects. */
final ArrayList<ActivityRecord> mFinishingActivities = new ArrayList<ActivityRecord>();
/** List of activities that are in the process of going to sleep. */
final ArrayList<ActivityRecord> mGoingToSleepActivities = new ArrayList<ActivityRecord>();
......
/** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();//以mStackId为key
/** Mapping from displayId to display current state */
private final SparseArray<ActivityDisplay> mActivityDisplays =
new SparseArray<ActivityDisplay>();//以displayId为key
AMS使用mHomeStack,mFocusedStack来完成系统全部的Activity的管理和调度。其中mHomeStack管理的是Launcher相关的任务,包括Launcher、RecentTask,Keyguad,除了上述以外的任务都归mFocusedStack管理。
AMS 通过操作ActivityStackSupervisor来管理Activity;具体是如何操作的呢?ActivityStackSupervisor通过ActivityContainer来管理ActivityStack(ActivityStack的构造方法只有在ActivityContainer的构造方法中被调用),ActivityContainer关联ActivityDisplay,ActivityDisplay将自己的mStacks赋值给ActivityContainer的mStack;mStack.mStacks = activityDisplay.mStacks;
------>/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
class ActivityContainer extends android.app.IActivityContainer.Stub {
final static int FORCE_NEW_TASK_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION;
final int mStackId;
IActivityContainerCallback mCallback = null;
final ActivityStack mStack; //ActivityContainer用于维护ActivityStack的
ActivityRecord mParentActivity = null;
String mIdString;
boolean mVisible = true;
/** Display this ActivityStack is currently on. Null if not attached to a Display. */
ActivityDisplay mActivityDisplay;
final static int CONTAINER_STATE_HAS_SURFACE = 0;
final static int CONTAINER_STATE_NO_SURFACE = 1;
final static int CONTAINER_STATE_FINISHING = 2;
int mContainerState = CONTAINER_STATE_HAS_SURFACE;
ActivityContainer(int stackId) {
synchronized (mService) {
mStackId = stackId;
mStack = new ActivityStack(this);
mIdString = "ActivtyContainer{" + mStackId + "}";
if (DEBUG_STACK) Slog.d(TAG, "Creating " + this);
}
}
void attachToDisplayLocked(ActivityDisplay activityDisplay) {
//ActivityContainer关联ActivityDisplay,用
if (DEBUG_STACK) Slog.d(TAG, "attachToDisplayLocked: " + this
+ " to display=" + activityDisplay);
mActivityDisplay = activityDisplay;
mStack.mDisplayId = activityDisplay.mDisplayId;
mStack.mStacks = activityDisplay.mStacks;
activityDisplay.attachActivities(mStack);
mWindowManager.attachStack(mStackId, activityDisplay.mDisplayId);
}
......
------>/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
/** Exactly one of these classes per Display in the system. Capable of holding zero or more
* attached {@link ActivityStack}s */
class ActivityDisplay {
/** Actual Display this object tracks. */
int mDisplayId;
Display mDisplay;
DisplayInfo mDisplayInfo = new DisplayInfo();
/** All of the stacks on this display. Order matters, topmost stack is in front of all other
* stacks, bottommost behind. Accessed directly by ActivityManager package classes */
final ArrayList<ActivityStack> mStacks = new ArrayList<ActivityStack>();
ActivityRecord mVisibleBehindActivity;
ActivityDisplay() {
}
// After instantiation, check that mDisplay is not null before using this. The alternative
// is for this to throw an exception if mDisplayManager.getDisplay() returns null.
ActivityDisplay(int displayId) {
final Display display = mDisplayManager.getDisplay(displayId);
if (display == null) {
return;
}
init(display);
}
.......
void attachActivities(ActivityStack stack) {
if (DEBUG_STACK) Slog.v(TAG, "attachActivities: attaching " + stack + " to displayId="
+ mDisplayId);
mStacks.add(stack);
}
......
追踪代码,发现ActivityContainer在ActivitySupervisor的createStackOnDisplay中被初始化,createStackOnDisplay在setWindowManager或adjustStackFocus或restoreRecentTaskLocked中被调用;这里说说从setWindowManager开始的调用流程,setWindowManager在SystemServer启动AMS时调用,然后调用ActivityContainer.attachToDisplayLocked方法;流程的大概就是根据mDisplayId获取ActivityDisplay,将ActivityDisplay的mStacks(代表该Display上的所有activitystack) 赋值给ActivityContainer的mStack的mStacks;同时将调用activityDisplay.attachActivities(mStack)将ActivityContainer的mStack(ActivityContainer维护的ActivityStack)添加到ActivityDisplay的mStacks 中;
ActivityStackSupervisor对ActivityRecord的管理过程如下:
ActivityStackSupervisor.mActivityDisplays
-> ActivityDisplay.mStacks
-> ActivityStack.mTaskHistory
-> TaskRecord.mActivities
-> ActivityRecord