ActivityManagerService-A 常见数据结构

版权说明:本文为 开开向前冲 原创文章,转载请注明出处;
注:限于作者水平有限,文中有不对的地方还请指教

提到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官网

image.png

这里逐一介绍一下上面几个数据结构:

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

参考资料:
Android Framework之AMS篇
Taskaffinity属性使用小结

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