Android应用程序启动过程源代码分析(2)
-
public class ActivityStack {
-
-
......
-
-
final int startActivityLocked(IApplicationThread caller,
-
Intent intent, String resolvedType,
-
Uri[] grantedUriPermissions,
-
int grantedMode, ActivityInfo aInfo, IBinder resultTo,
-
String resultWho, int requestCode,
-
int callingPid, int callingUid, boolean onlyIfNeeded,
-
boolean componentSpecified) {
-
int err = START_SUCCESS;
-
-
ProcessRecord callerApp = null;
-
if (caller != null) {
-
callerApp = mService.getRecordForAppLocked(caller);
-
if (callerApp != null) {
-
callingPid = callerApp.pid;
-
callingUid = callerApp.info.uid;
-
} else {
-
......
-
}
-
}
-
-
......
-
-
ActivityRecord sourceRecord = null;
-
ActivityRecord resultRecord = null;
-
if (resultTo != null) {
-
int index = indexOfTokenLocked(resultTo);
-
-
......
-
-
if (index >= 0) {
-
sourceRecord = (ActivityRecord)mHistory.get(index);
-
if (requestCode >= 0 && !sourceRecord.finishing) {
-
......
-
}
-
}
-
}
-
-
int launchFlags = intent.getFlags();
-
-
if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
-
&& sourceRecord != null) {
-
......
-
}
-
-
if (err == START_SUCCESS && intent.getComponent() == null) {
-
......
-
}
-
-
if (err == START_SUCCESS && aInfo == null) {
-
......
-
}
-
-
if (err != START_SUCCESS) {
-
......
-
}
-
-
......
-
-
ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
-
intent, resolvedType, aInfo, mService.mConfiguration,
-
resultRecord, resultWho, requestCode, componentSpecified);
-
-
......
-
-
return startActivityUncheckedLocked(r, sourceRecord,
-
grantedUriPermissions, grantedMode, onlyIfNeeded, true);
-
}
-
-
-
......
-
-
}
再接下来,创建即将要启动的Activity的相关信息,并保存在r变量中:
-
ActivityRecord r = new ActivityRecord(mService, this, callerApp, callingUid,
-
intent, resolvedType, aInfo, mService.mConfiguration,
-
resultRecord, resultWho, requestCode, componentSpecified);
-
public class ActivityStack {
-
-
......
-
-
final int startActivityUncheckedLocked(ActivityRecord r,
-
ActivityRecord sourceRecord, Uri[] grantedUriPermissions,
-
int grantedMode, boolean onlyIfNeeded, boolean doResume) {
-
final Intent intent = r.intent;
-
final int callingUid = r.launchedFromUid;
-
-
int launchFlags = intent.getFlags();
-
-
// We'll invoke onUserLeaving before onPause only if the launching
-
// activity did not explicitly state that this is an automated launch.
-
mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
-
-
......
-
-
ActivityRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
-
!= 0 ? r : null;
-
-
// If the onlyIfNeeded flag is set, then we can do this if the activity
-
// being launched is the same as the one making the call... or, as
-
// a special case, if we do not know the caller then we count the
-
// current top activity as the caller.
-
if (onlyIfNeeded) {
-
......
-
}
-
-
if (sourceRecord == null) {
-
......
-
} else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
-
......
-
} else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
-
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
-
......
-
}
-
-
if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-
......
-
}
-
-
boolean addingToTask = false;
-
if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
-
(launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
-
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
-
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
-
// If bring to front is requested, and no result is requested, and
-
// we can find a task that was started with this same
-
// component, then instead of launching bring that one to the front.
-
if (r.resultTo == null) {
-
// See if there is a task to bring to the front. If this is
-
// a SINGLE_INSTANCE activity, there can be one and only one
-
// instance of it in the history, and it is always in its own
-
// unique task, so we do a special search.
-
ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
-
? findTaskLocked(intent, r.info)
-
: findActivityLocked(intent, r.info);
-
if (taskTop != null) {
-
......
-
}
-
}
-
}
-
-
......
-
-
if (r.packageName != null) {
-
// If the activity being launched is the same as the one currently
-
// at the top, then we need to check if it should only be launched
-
// once.
-
ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
-
if (top != null && r.resultTo == null) {
-
if (top.realActivity.equals(r.realActivity)) {
-
......
-
}
-
}
-
-
} else {
-
......
-
}
-
-
boolean newTask = false;
-
-
// Should this be considered a new task?
-
if (r.resultTo == null && !addingToTask
-
&& (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-
// todo: should do better management of integers.
-
mService.mCurTask++;
-
if (mService.mCurTask <= 0) {
-
mService.mCurTask = 1;
-
}
-
r.task = new TaskRecord(mService.mCurTask, r.info, intent,
-
(r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
-
......
-
newTask = true;
-
if (mMainStack) {
-
mService.addRecentTaskLocked(r.task);
-
}
-
-
} else if (sourceRecord != null) {
-
......
-
} else {
-
......
-
}
-
-
......
-
-
startActivityLocked(r, newTask, doResume);
-
return START_SUCCESS;
-
}
-
-
......
-
-
}
-
if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
-
(launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
-
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
-
|| r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
-
// If bring to front is requested, and no result is requested, and
-
// we can find a task that was started with this same
-
// component, then instead of launching bring that one to the front.
-
if (r.resultTo == null) {
-
// See if there is a task to bring to the front. If this is
-
// a SINGLE_INSTANCE activity, there can be one and only one
-
// instance of it in the history, and it is always in its own
-
// unique task, so we do a special search.
-
ActivityRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
-
? findTaskLocked(intent, r.info)
-
: findActivityLocked(intent, r.info);
-
if (taskTop != null) {
-
......
-
}
-
}
-
}
-
if (r.packageName != null) {
-
// If the activity being launched is the same as the one currently
-
// at the top, then we need to check if it should only be launched
-
// once.
-
ActivityRecord top = topRunningNonDelayedActivityLocked(notTop);
-
if (top != null && r.resultTo == null) {
-
if (top.realActivity.equals(r.realActivity)) {
-
......
-
}
-
}
-
-
}
-
if (r.resultTo == null && !addingToTask
-
&& (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-
// todo: should do better management of integers.
-
mService.mCurTask++;
-
if (mService.mCurTask <= 0) {
-
mService.mCurTask = 1;
-
}
-
r.task = new TaskRecord(mService.mCurTask, r.info, intent,
-
(r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
-
......
-
newTask = true;
-
if (mMainStack) {
-
mService.addRecentTaskLocked(r.task);
-
}
-
-
}
-
public class ActivityStack {
-
-
......
-
-
private final void startActivityLocked(ActivityRecord r, boolean newTask,
-
boolean doResume) {
-
final int NH = mHistory.size();
-
-
int addPos = - 1;
-
-
if (!newTask) {
-
......
-
}
-
-
// Place a new activity at top of stack, so it is next to interact
-
// with the user.
-
if (addPos < 0) {
-
addPos = NH;
-
}
-
-
// If we are not placing the new activity frontmost, we do not want
-
// to deliver the onUserLeaving callback to the actual frontmost
-
// activity
-
if (addPos < NH) {
-
......
-
}
-
-
// Slot the activity into the history stack and proceed
-
mHistory.add(addPos, r);
-
r.inHistory = true;
-
r.frontOfTask = newTask;
-
r.task.numActivities++;
-
if (NH > 0) {
-
// We want to show the starting preview window if we are
-
// switching to a new task, or the next activity's process is
-
// not currently running.
-
......
-
} else {
-
// If this is the first activity, don't do any fancy animations,
-
// because there is nothing for it to animate on top of.
-
......
-
}
-
-
......
-
-
if (doResume) {
-
resumeTopActivityLocked( null);
-
}
-
}
-
-
......
-
-
}
-
public class ActivityStack {
-
-
......
-
-
/**
-
* Ensure that the top activity in the stack is resumed.
-
*
-
* @param prev The previously resumed activity, for when in the process
-
* of pausing; can be null to call from elsewhere.
-
*
-
* @return Returns true if something is being resumed, or false if
-
* nothing happened.
-
*/
-
final boolean resumeTopActivityLocked(ActivityRecord prev) {
-
// Find the first activity that is not finishing.
-
ActivityRecord next = topRunningActivityLocked( null);
-
-
// Remember how we'll process this pause/resume situation, and ensure
-
// that the state is reset however we wind up proceeding.
-
final boolean userLeaving = mUserLeaving;
-
mUserLeaving = false;
-
-
if (next == null) {
-
......
-
}
-
-
next.delayedResume = false;
-
-
// If the top activity is the resumed one, nothing to do.
-
if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
-
......
-
}
-
-
// If we are sleeping, and there is no resumed activity, and the top
-
// activity is paused, well that is the state we want.
-
if ((mService.mSleeping || mService.mShuttingDown)
-
&& mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
-
......
-
}
-
-
......
-
-
// If we are currently pausing an activity, then don't do anything
-
// until that is done.
-
if (mPausingActivity != null) {
-
......
-
}
-
-
......
-
-
// We need to start pausing the current activity so the top one
-
// can be resumed...
-
if (mResumedActivity != null) {
-
......
-
startPausingLocked(userLeaving, false);
-
return true;
-
}
-
-
......
-
}
-
-
......
-
-
}
-
// If the top activity is the resumed one, nothing to do.
-
if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
-
......
-
}
-
-
// If we are sleeping, and there is no resumed activity, and the top
-
// activity is paused, well that is the state we want.
-
if ((mService.mSleeping || mService.mShuttingDown)
-
&& mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
-
......
-
}
-
public class ActivityStack {
-
-
......
-
-
private final void startPausingLocked( boolean userLeaving, boolean uiSleeping) {
-
if (mPausingActivity != null) {
-
......
-
}
-
ActivityRecord prev = mResumedActivity;
-
if (prev == null) {
-
......
-
}
-
......
-
mResumedActivity = null;
-
mPausingActivity = prev;
-
mLastPausedActivity = prev;
-
prev.state = ActivityState.PAUSING;
-
......
-
-
if (prev.app != null && prev.app.thread != null) {
-
......
-
try {
-
......
-
prev.app.thread.schedulePauseActivity(prev, prev.finishing, userLeaving,
-
prev.configChangeFlags);
-
......
-
} catch (Exception e) {
-
......
-
}
-
} else {
-
......
-
}
-
-
......
-
-
}
-
-
......
-
-
}
-
class ApplicationThreadProxy implements IApplicationThread {
-
-
......
-
-
public final void schedulePauseActivity(IBinder token, boolean finished,
-
boolean userLeaving, int configChanges) throws RemoteException {
-
Parcel data = Parcel.obtain();
-
data.writeInterfaceToken(IApplicationThread.descriptor);
-
data.writeStrongBinder(token);
-
data.writeInt(finished ? 1 : 0);
-
data.writeInt(userLeaving ? 1 : 0);
-
data.writeInt(configChanges);
-
mRemote.transact(SCHEDULE_PAUSE_ACTIVITY_TRANSACTION, data, null,
-
IBinder.FLAG_ONEWAY);
-
data.recycle();
-
}
-
-
......
-
-
}
-
public final class ActivityThread {
-
-
......
-
-
private final class ApplicationThread extends ApplicationThreadNative {
-
-
......
-
-
public final void schedulePauseActivity(IBinder token, boolean finished,
-
boolean userLeaving, int configChanges) {
-
queueOrSendMessage(
-
finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
-
token,
-
(userLeaving ? 1 : 0),
-
configChanges);
-
}
-
-
......
-
-
}
-
-
......
-
-
}
-
public final class ActivityThread {
-
-
......
-
-
private final void queueOrSendMessage( int what, Object obj, int arg1) {
-
queueOrSendMessage(what, obj, arg1, 0);
-
}
-
-
private final void queueOrSendMessage( int what, Object obj, int arg1, int arg2) {
-
synchronized ( this) {
-
......
-
Message msg = Message.obtain();
-
msg.what = what;
-
msg.obj = obj;
-
msg.arg1 = arg1;
-
msg.arg2 = arg2;
-
mH.sendMessage(msg);
-
}
-
}
-
-
......
-
-
}
-
public final class ActivityThread {
-
-
......
-
-
private final class H extends Handler {
-
-
......
-
-
public void handleMessage(Message msg) {
-
......
-
switch (msg.what) {
-
-
......
-
-
case PAUSE_ACTIVITY:
-
handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
-
maybeSnapshot();
-
break;
-
-
......
-
-
}
-
......
-
-
}
-
-
......
-
-
}