ActivityManagerService数据结构Activity栈管理(二)

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
他们的关系应该是这样的:

 

 

一、主要涉及4个类

1) ActivityRecord
源码注释:An entry in the history stack, representing an activity.
翻译一下:存在历史栈的一个实例,代表一个Activity。
2) TaskRecord
Activity栈,内部维护一个ArrayList<ActivityRecord>
3) ActivityStack
并不是一个Activity栈,真正意义上的Activity栈是TaskRecord,这个类是负责管理各个Activity栈,内部维护一个ArrayList<TaskRecord>
4) ActivityStackSupervisor
内部持有一个ActivityStack,而ActivityStack内部也持有ActivityStackSupervisor,相当于ActivityStack的辅助管理类

ActivityRecord

ActivityRecord是Activity在AMS中的存在形式,ActivityRecord保存了Activity的信息。

final class ActivityRecord {
    TaskRecord task;  // the task this is in.
    final IApplicationToken.Stub appToken;
    final int userId;
    int theme;
    int launchMode;
    ...
}

成员变量task表示自己所在的TaskRecord,这样要找到自己所在的TaskRecord就不必遍历查找了。

TaskRecord
我们都知道AMS以Task的方式在管理Activity,TaskRecord中的mActivities是一个栈,它的作用是以栈的方式组织管理Activity。Android把用户一次操作相关的Activity按照先后顺序保存在一个Task中,这个Task在AMS中的存在形式就是TaskRecord;
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属性值;
ActivityStack

ActivityStack充当TaskRecord的Manager角色;

final class ActivityStack {
   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

    ActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer, RecentTasks recentTasks) {
        mStackSupervisor = activityContainer.getOuter();
        ...
    }
}
mTaskHistory:是一个列表,存储的是ActivityStack中的所有TaskRecord对象,TaskRecord 通过mActivities变量存储Task中所有的Activity,所以mTaskHistory间接管理了ActivityStack中的所有activity;
mLRUActivities:一个列表,存储的是ActivityStack中按照最近活动情况运行的所有Activity;
ActivityStackSupervisor

ActivityStackSupervisor 用于管理ActivityStack;ActivityStackSupervisor为AMS提供管理方法;管理着系统中的三个ActivityStack;

public final class ActivityStackSupervisor {/** 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;
 
ActivityContainer
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

 


二、场景解析
1、从桌面第一次启动App

startActivityLocked里构造一个ActivityRecord
新建一个TaskRecord,并存入mTaskHistory
ActivityRecord存入mActivities

final int startActivityUncheckedLocked(...) {
    final int startActivityUncheckedLocked(...) {
    if (reuseTask == null) {
        r.setTask(targetStack.createTaskRecord(...);
        ...
        targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options);
        ...
    }
}

1.TaskRecord存入mTaskHistory

 TaskRecord createTaskRecord(int taskId, ActivityInfo info, Intent intent,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            boolean toTop) {
        TaskRecord task = new TaskRecord(mService, taskId, info, intent, voiceSession,
                voiceInteractor);
        addTask(task, toTop, false);
        return task;
    }

    void addTask(final TaskRecord task, final boolean toTop, boolean moving) {
        task.stack = this;
        if (toTop) {
            insertTaskAtTop(task, null);
        } else {
            mTaskHistory.add(0, task);
            updateTaskMovement(task, false);
        }
        ...
    }

    private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
        ...
        mTaskHistory.add(taskNdx, task);
        updateTaskMovement(task, true);
    }

2.ActivityRecord存入mActivities

final void startActivityLocked(ActivityRecord r, boolean newTask, ...) {
    ...
    task = mTaskHistory.get(taskNdx);
    ...
    task.addActivityToTop(r);
}
void addActivityToTop(ActivityRecord r) {
    addActivityAtIndex(mActivities.size(), r);
}
void addActivityAtIndex(int index, ActivityRecord r) {
    ...
    mActivities.add(index, r);
    ...
}

 

posted on 2019-03-21 20:46  mingfeng002  阅读(2570)  评论(1编辑  收藏  举报