From 50685606cfa045213bcc1bf29ce1c00c256b055c Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Thu, 1 Dec 2011 12:23:37 -0800 Subject: [PATCH] Improve how we manage the previous app. Setting it when a new activity is being resumed is too soon, because things like an activity launching an exiting (without being seen by the user) can knock out the real previous app that we want. So now we set it when an activity is stopped. At this point it is going to move from the preceptible to background oom adj, so it is a good point to determine whether it should be a previous app to instead put it to that oom adj. This also avoids things like activities that start and immediately finish from impacting the previous app. Further, we keep track of the time each activity was last shown, and use this to further filter what is set as the previous app. Change-Id: I72d1cac4de0cc2d4598170296028f11b06918d4f --- .../server/am/ActivityManagerService.java | 13 +++++++++- .../com/android/server/am/ActivityRecord.java | 6 +++++ .../com/android/server/am/ActivityStack.java | 24 ++++++++++++------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java index b36ba3a3cc92..a4d321dadb1e 100644 --- a/services/java/com/android/server/am/ActivityManagerService.java +++ b/services/java/com/android/server/am/ActivityManagerService.java @@ -414,7 +414,12 @@ public final class ActivityManagerService extends ActivityManagerNative * is in a different process from the one they are currently in. */ ProcessRecord mPreviousProcess; - + + /** + * The time at which the previous process was last visible. + */ + long mPreviousProcessVisibleTime; + /** * Packages that the user has asked to have run in screen size * compatibility mode instead of filling the screen. @@ -8361,6 +8366,12 @@ public final class ActivityManagerService extends ActivityManagerNative pw.println(); pw.println(" mHomeProcess: " + mHomeProcess); pw.println(" mPreviousProcess: " + mPreviousProcess); + if (dumpAll) { + StringBuilder sb = new StringBuilder(128); + sb.append(" mPreviousProcessVisibleTime: "); + TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb); + pw.println(sb); + } if (mHeavyWeightProcess != null) { pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess); } diff --git a/services/java/com/android/server/am/ActivityRecord.java b/services/java/com/android/server/am/ActivityRecord.java index 951a946e06a0..c819114f3866 100644 --- a/services/java/com/android/server/am/ActivityRecord.java +++ b/services/java/com/android/server/am/ActivityRecord.java @@ -80,6 +80,7 @@ final class ActivityRecord { ThumbnailHolder thumbHolder; // where our thumbnails should go. long launchTime; // when we starting launching this activity long startTime; // last time this activity was started + long lastVisibleTime; // last time this activity became visible long cpuTimeAtResume; // the cpu time of host process at the time of resuming activity Configuration configuration; // configuration activity was last running in CompatibilityInfo compat;// last used compatibility mode @@ -188,6 +189,10 @@ final class ActivityRecord { TimeUtils.formatDuration(launchTime, pw); pw.print(" startTime="); TimeUtils.formatDuration(startTime, pw); pw.println(""); } + if (lastVisibleTime != 0) { + pw.print(prefix); pw.print("lastVisibleTime="); + TimeUtils.formatDuration(lastVisibleTime, pw); pw.println(""); + } if (waitingVisible || nowVisible) { pw.print(prefix); pw.print("waitingVisible="); pw.print(waitingVisible); pw.print(" nowVisible="); pw.println(nowVisible); @@ -632,6 +637,7 @@ final class ActivityRecord { ActivityManagerService.TAG, "windowsVisible(): " + this); if (!nowVisible) { nowVisible = true; + lastVisibleTime = SystemClock.uptimeMillis(); if (!idle) { // Instead of doing the full stop routine here, let's just // hide any activities we now can, and let them stop when diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java index c7ce3c3eed8d..b5edc0a656cb 100644 --- a/services/java/com/android/server/am/ActivityStack.java +++ b/services/java/com/android/server/am/ActivityStack.java @@ -949,6 +949,22 @@ final class ActivityStack { if (r.configDestroy) { destroyActivityLocked(r, true, false, "stop-config"); resumeTopActivityLocked(null); + } else { + // Now that this process has stopped, we may want to consider + // it to be the previous app to try to keep around in case + // the user wants to return to it. + ProcessRecord fgApp = null; + if (mResumedActivity != null) { + fgApp = mResumedActivity.app; + } else if (mPausingActivity != null) { + fgApp = mPausingActivity.app; + } + if (r.app != null && fgApp != null && r.app != fgApp + && r.lastVisibleTime > mService.mPreviousProcessVisibleTime + && r.app != mService.mHomeProcess) { + mService.mPreviousProcess = r.app; + mService.mPreviousProcessVisibleTime = r.lastVisibleTime; + } } } } @@ -1363,14 +1379,6 @@ final class ActivityStack { + ", nowVisible=" + next.nowVisible); } } - - if (!prev.finishing && prev.app != null && prev.app != next.app - && prev.app != mService.mHomeProcess) { - // We are switching to a new activity that is in a different - // process than the previous one. Note the previous process, - // so we can try to keep it around. - mService.mPreviousProcess = prev.app; - } } // Launching this app's activity, make sure the app is no longer