This paper mainly introduces the start-up of Activitymanagerservice and the management of activitymanagerservice activity stack in android4.4.
First, the start of Activitymanagerservice
Activitymanagerservice was also created at the time of Systemserver startup,
<span style= "FONT-SIZE:18PX;" >class serverthread { ... public void Initandloop () { ... Context = Activitymanagerservice.main (factorytest); Start AMS .... Activitymanagerservice.setsystemprocess (); Register with Service Manager ... } </span>
A singleton mode is required to initialize Activitymanagerservice,activitymanagerservice in a new thread.
<span style= "FONT-SIZE:18PX;" > public static final Context main (int factorytest) {athread thr = new Athread (); Thr.start (); Starts a thread to start synchronized (THR) {while (Thr.mservice = = null) {try { Thr.wait ();//thread Waits Activitymanagerservice initialization complete} catch (Interruptedexception e) {} }} activitymanagerservice m = Thr.mservice; Mself = m; Activitythread at = Activitythread.systemmain (); Start a main thread msystemthread = at; Context context = At.getsystemcontext (); Context.settheme (Android. R.style.theme_holo); ... m.mstacksupervisor = new Activitystacksupervisor (m, context, thr.mlooper); Create a new activity Stack management helper class instance ... synchronized (THR) {Thr.mready = true; Thr.notifyall (); Activitymanagerservice Boot Complete} m.startrunning (null, NULL, NULL, NULL); return context; }&lT;/span>
ii. activitystacksupervisor and activitystack managing the status of Activity
1.ActivityState
Describes all the states that an activity may experience, defined in the Activitystack.java file:
Enum Activitystate { INITIALIZING,//initializing resumed,//restored pausing,//pausing PAUSED,//Paused Stopping,//is stopping STOPPED,//has stopped finishing,//is completing destroying,//is destroying destroyed//has been destroyed }
State transition Graph:
/** * The back history of all previous (and possibly still * running) activities. It contains #TaskRecord objects. */private arraylist<taskrecord> mtaskhistory = new arraylist<taskrecord> (); /** * used for validating apps tokens with window manager. */FINAL arraylist<taskgroup> mvalidateapptokens = new arraylist<taskgroup> (); /** * List of running activities, sorted by recent usage. * The first entry in the list is the least recently used. * It contains Historyrecord objects. */FINAL arraylist<activityrecord> mlruactivities = new arraylist<activityrecord> (); /** * Animations, the current transition has requested not to * is considered for the transition animatio N. */FINAL arraylist<activityrecord> mnoanimactivities = new arraylist<activityrecord> (); /** * When we were in the process of pausing a activity, before starting the * next one, this VAriable holds the activity is currently being paused. */Activityrecord mpausingactivity = null; /** * This was the last activity, that we put to the paused state. This was * used to determine if we need to do a activity transition while sleeping and * when we normally hold the P activity paused. */Activityrecord mlastpausedactivity = null; /** * Activities that specify No must is removed once the user navigates away from them. * If the device goes to sleep with such a activity in the paused state then we save it here * and finish it later If Another activity replaces it on wakeup. */Activityrecord mlastnohistoryactivity = null; /** * Current Activity This is resumed, or null if there is none. */Activityrecord mresumedactivity = null; The last activity of/** * is been started. It was only used to * identify when multiple activities was started at once so, the user * can be WArned They may is in the activity they think they is. */Activityrecord mlaststartedactivity = null;
Activityrecord the specific meaning of the variable, you can view the comment.
2. Record the ArrayList and activitystack of activity
/** the stack containing the Launcher app */ private activitystack mhomestack; /** the Non-home stack currently receiving input or launching the next activity. IF Home is * in front then Mhomestack overrides Mfocusedstack. * Do not ACCESS directly-it is null, use Getfocusedstack () */ private activitystack mfocusedstack; /** all the non-launcher stacks * * private arraylist<activitystack> mstacks = new arraylist<activitystack& gt; ();
Mhomestack: Records only tasks that hold launcher and systemui (recently applied)
Mfocusedstack: Log All tasks for non-launcher apps
Mstacks: This ArrayList only holds Mhomestack and mfocusedstack,mhomestack is placed in the No. 0 position.
This result I was using the ADB shell dumpsys activity command to see:
Activity MANAGER Activities (Dumpsys activity Activities) Stack #0: Task ID #1 taskrecord{b22161f0 #1 a=com.andro Id.launcher u=0 Sz=1} Intent {act=android.intent.action.main cat=[android.intent.category.home] flg=0x10200000 cmp=c Om.android.launcher/com.android.launcher2.launcher} Hist #0: activityrecord{b2214570 u0 COM.ANDROID.LAUNCHER/COM.A Ndroid.launcher2.Launcher T1} Intent {Act=android.intent.action.main cat=[android.intent.category.home] flg=0x10 000000 Cmp=com.android.launcher/com.android.launcher2.launcher} processrecord{b21df5c8 1402:com.android.launcher /U0A8} Task ID #3 taskrecord{b2288fd0 #3 a=com.android.systemui u=0 sz=1} Intent {ACT=COM.ANDROID.SYSTEMUI.R Ecent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent. Recentsactivity bnds=[132,331][296,476] (has extras)} Hist #0: activityrecord{b22d5aa0 u0 com.android.systemui/.re cent. recentsactivity T3} Intent {Act=com.android.systemui.reCent.action.TOGGLE_RECENTS flg=0x10800000 cmp=com.android.systemui/.recent. Recentsactivity bnds=[132,331][296,476] (has extras)} Processrecord{b2169ac8 1313:com.android.systemui/u0a7} Running activities (most recent first): taskrecord{b22161f0 #1 a=com.android.launcher u=0 sz=1} Run #1: Activi tyrecord{b2214570 u0 com.android.launcher/com.android.launcher2.launcher T1} taskrecord{b2288fd0 #3 A=com.android.sy Stemui u=0 Sz=1} Run #0: activityrecord{b22d5aa0 u0 com.android.systemui/.recent. recentsactivity T3} Stack #1: Task ID #4 taskrecord{b21e57d0 #4 a=android.task.contacts u=0 sz=1} Intent {A Ct=android.intent.action.main Cat=[android.intent.category.launcher] flg=0x10200000 cmp=com.android.contacts/. Activities. Peopleactivity} Hist #0: activityrecord{b2281e68 u0 com.android.contacts/.activities. peopleactivity T4} Intent {Act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x10200000 Cmp=com.androiD.contacts/.activities. Peopleactivity bnds=[64,417][128,481]} processrecord{b22f66c0 1820:COM.ANDROID.CONTACTS/U0A2} Task ID #2 taskrecord{b2229370 #2 a=com.android.dialer u=0 sz=1} Intent {Act=android.intent.action.main cat=[android.intent.ca Tegory. LAUNCHER] flg=0x10200000 cmp=com.android.dialer/. Dialtactsactivity} Hist #0: Activityrecord{b223d3c8 u0 com.android.dialer/. Dialtactsactivity T2} Intent {Act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x102000 XX cmp=com.android.dialer/. Dialtactsactivity bnds=[0,417][64,481]} processrecord{b2215d48 1569:com.android.dialer/u0a4} Running Activiti ES (most recent first): Taskrecord{b21e57d0 #4 a=android.task.contacts u=0 sz=1} Run #1: activityrecord{b2281e U0 com.android.contacts/.activities. peopleactivity T4} taskrecord{b2229370 #2 a=com.android.dialer u=0 sz=1} Run #0: Activityrecord{b223d3c8 u0 Co m.android.dialer/. Dialtactsactivity T2} Mresumedactivity:activityrecord{b2281e68 u0 com.android.contacts/.activities. peopleactivity T4} mfocusedactivity:activityrecord{b2281e68 u0 com.android.contacts/.activities. peopleactivity T4} mdismisskeyguardonnextactivity:false mstackstate=stack_state_home_in_back Msleeptimeout:false Mcurtaskid:4 Muserstackinfront: {} Recent tasks: * Recent #0: taskrecord{b21e57d0 #4 a=android.task.contacts U=0 Sz=1} * Recent #1: taskrecord{b22161f0 #1 a=com.android.launcher u=0 sz=1} * Recent #2: taskrecord{b2229370 #2 A=com.android. Dialer U=0 sz=1} * Recent #3: taskrecord{b2288fd0 #3 a=com.android.systemui u=0 Sz=1}
Stack #0 isMhomestack, with launcher and Systemui's task on it.
The Stack #1 is mfocusedstack, which puts the task of all the apps that have been launched.
These two are stored inside the mstacks.
3. Now look at when Mhomestack and Mfocusedstack were put into the mstacks? Where did the elements inside the Mhomestack and mfocusedstack join?
Mhomestack is initialized when the Windowmanagerservice is created, Activitymanagerservice when the Windowmanagerservice is set,
Activitymanagerservice.self (). Setwindowmanager (WM); In Systemserver.java
public void Setwindowmanager (Windowmanagerservice wm) {//<font size= "4" ><font size= "4" ><font size= "4" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >ActivityManagerService</span></span></span></font></font>.java</font> in Mwindowmanager = WM; Mstacksupervisor.setwindowmanager (WM); Wm.createstack (home_stack_id,-1, Stackbox.task_stack_goes_over, 1.0f); }
void Setwindowmanager (Windowmanagerservice wm) {//activitystacksupervisor.java in Mwindowmanager = WM; Mhomestack = new Activitystack (Mservice, Mcontext, Mlooper, home_stack_id); Mstacks.add (Mhomestack); Put it in the No. 0 position }
The process of starting an application (startactivity) in the Android4.4 framework analysis--launcher is mfocusedstack the application's task, so it is created when the activity starts, The activation process of the activity can be referred to in step16,startactivityuncheckedlocked (),
if (R.resultto = = null &&!addingtotask && (launchflags&intent.flag_activity_new_task)! = 0) { C3/>targetstack = Adjuststackfocus (R); Targetstack is the target stack,r stack to join
Activitystack Adjuststackfocus (Activityrecord R) {Final Taskrecord task = R.task; R is an activity type for an application, or R belongs to a task is an application task//android definition distinguishes between three different activity types //static final int app Lication_activity_type = 0; Application type //static final int home_activity_type = 1; Launcher type //static final int recents_activity_type = 2; Recent application of Systemui if (r.isapplicationactivity () | | (Task! = null && task.isapplicationtask ())) {if (task! = null) {if (Mfocusedstack! = task.stack) {if (Debug_focus | | Debug_stack) slog.d (TAG, "adjuststackfocus:setting focused STACK to r=" + R + "task=" + task ); Mfocusedstack = Task.stack; } else {if (Debug_focus | | Debug_stack) slog.d (TAG, "adjuststackfocus:focused STACK already=" + mfocusedstack); } Return Mfocusedstack;//app type of activity is added Mfocusedstack} if (mfocusedstack! = null) each time {if (Debug_focus | | Debug_stack) slog.d (TAG, "Adjuststackfocus:have a focused stack=" + mfocusedstack); Return Mfocusedstack;//app type activity is added to Mfocusedstack} for (int stackndx = Mstacks.size ()-1 each time; Stackndx > 0; --STACKNDX) {Activitystack stack = mstacks.get (STACKNDX); if (!stack.ishomestack ()) {if (Debug_focus | | Debug_stack) slog.d (TAG, "adjuststackfocus:setting focused stack=" + STACK); Mfocusedstack = stack; Return Mfocusedstack;//app type of activity is added mfocusedstack}}//Time to create the FIR St App stack for this user. int stackid = Mservice.createstack ( -1, home_stack_id,//Start the first app, need to create a STACK, when other apps are created, the front criteria will intercept Stackbox.task_stack_goes_over, 1.0f); if (Debug_focus | | Debug_stack) slog.d (TAG, "Adjuststackfocus:new STACK r=" + R + "stackid=" + stackid); Mfocusedstack = Getstack (stackid); return mfocusedstack; App-type activity adds Mfocusedstack} return mhomestack each time; }
As seen from the above method, the launcher type and the recently applied type of task are added to the mhomestack, while the other application type of task joins the Mfocusedstack,
public int Createstack (int taskId, int relativestackboxid, int position, float weight) { Enforcecallingpermission ( Android. Manifest.permission.MANAGE_ACTIVITY_STACKS, "Createstack ()"); Synchronized (this) { long ident = Binder.clearcallingidentity (); try { int stackid = Mstacksupervisor.createstack ();//See here mwindowmanager.createstack (Stackid, Relativestackboxid, position, weight); if (TaskId > 0) { movetasktostack (taskId, Stackid, True); } return stackid; } finally { binder.restorecallingidentity (ident);}}}
int Createstack () { while (true) { if (++mlaststackid <= home_stack_id) { Mlaststackid = home_stack_id + 1; } if (Getstack (mlaststackid) = = null) {break ; } } Mstacks.add (New Activitystack (Mservice, Mcontext, Mlooper, Mlaststackid)); A new stack was added to Mstacks return mlaststackid; }
The Createstack () method has only one chance to execute, that is, to run the app for the first time.
Mstacks in what case will appear the third or fourth element, this does not know, currently found no such situation ...
Please correct me if there is any wrong place to be continued.
Android4.4 Framework Analysis--activitymanagerservice startup and management of activity