Make activity manager more robust in the face of app activity leaks.

This came up from bug #5601885: Memory increase (leak?) in system_server
Stingray MR1

This isn't *really* a leak in the system process -- it is a leak in an
application process that is causing the system process to keep around
a bunch of ActivityRecord objects longer than it should, until that app
process is ultimately killed.

Unfortunately these days leaking an ActivityRecord also often means
leaking a thumbnail, which is a big slab of memory.

So make the activity manager better about this, using a weak reference
from the handle the object has so we can still clean away most of the
state associated with the ActivityRecord even if the client side leaks
its own reference.

Change-Id: Idbab45e09749cdfb54899203da7981e7b3576e25
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index 8435eaa..c892cb1 100644
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -145,7 +145,12 @@
      * running) activities.  It contains HistoryRecord objects.
      */
     final ArrayList<ActivityRecord> mHistory = new ArrayList<ActivityRecord>();
-    
+
+    /**
+     * Used for validating app tokens with window manager.
+     */
+    final ArrayList<IBinder> mValidateAppTokens = new ArrayList<IBinder>();
+
     /**
      * List of running activities, sorted by recent usage.
      * The first entry in the list is the least recently used.
@@ -294,11 +299,11 @@
                     }
                 } break;
                 case PAUSE_TIMEOUT_MSG: {
-                    IBinder token = (IBinder)msg.obj;
+                    ActivityRecord r = (ActivityRecord)msg.obj;
                     // We don't at this point know if the activity is fullscreen,
                     // so we need to be conservative and assume it isn't.
-                    Slog.w(TAG, "Activity pause timeout for " + token);
-                    activityPaused(token, true);
+                    Slog.w(TAG, "Activity pause timeout for " + r);
+                    activityPaused(r != null ? r.appToken : null, true);
                 } break;
                 case IDLE_TIMEOUT_MSG: {
                     if (mService.mDidDexOpt) {
@@ -310,20 +315,20 @@
                     }
                     // We don't at this point know if the activity is fullscreen,
                     // so we need to be conservative and assume it isn't.
-                    IBinder token = (IBinder)msg.obj;
-                    Slog.w(TAG, "Activity idle timeout for " + token);
-                    activityIdleInternal(token, true, null);
+                    ActivityRecord r = (ActivityRecord)msg.obj;
+                    Slog.w(TAG, "Activity idle timeout for " + r);
+                    activityIdleInternal(r != null ? r.appToken : null, true, null);
                 } break;
                 case DESTROY_TIMEOUT_MSG: {
-                    IBinder token = (IBinder)msg.obj;
+                    ActivityRecord r = (ActivityRecord)msg.obj;
                     // We don't at this point know if the activity is fullscreen,
                     // so we need to be conservative and assume it isn't.
-                    Slog.w(TAG, "Activity destroy timeout for " + token);
-                    activityDestroyed(token);
+                    Slog.w(TAG, "Activity destroy timeout for " + r);
+                    activityDestroyed(r != null ? r.appToken : null);
                 } break;
                 case IDLE_NOW_MSG: {
-                    IBinder token = (IBinder)msg.obj;
-                    activityIdleInternal(token, false, null);
+                    ActivityRecord r = (ActivityRecord)msg.obj;
+                    activityIdleInternal(r != null ? r.appToken : null, false, null);
                 } break;
                 case LAUNCH_TIMEOUT_MSG: {
                     if (mService.mDidDexOpt) {
@@ -397,7 +402,7 @@
         while (i >= 0) {
             ActivityRecord r = mHistory.get(i);
             // Note: the taskId check depends on real taskId fields being non-zero
-            if (!r.finishing && (token != r) && (taskId != r.task.taskId)) {
+            if (!r.finishing && (token != r.appToken) && (taskId != r.task.taskId)) {
                 return r;
             }
             i--;
@@ -406,23 +411,17 @@
     }
 
     final int indexOfTokenLocked(IBinder token) {
-        try {
-            ActivityRecord r = (ActivityRecord)token;
-            return mHistory.indexOf(r);
-        } catch (ClassCastException e) {
-            Slog.w(TAG, "Bad activity token: " + token, e);
-            return -1;
-        }
+        return mHistory.indexOf(ActivityRecord.forToken(token));
+    }
+
+    final int indexOfActivityLocked(ActivityRecord r) {
+        return mHistory.indexOf(r);
     }
 
     final ActivityRecord isInStackLocked(IBinder token) {
-        try {
-            ActivityRecord r = (ActivityRecord)token;
-            if (mHistory.contains(r)) {
-                return r;
-            }
-        } catch (ClassCastException e) {
-            Slog.w(TAG, "Bad activity token: " + token, e);
+        ActivityRecord r = ActivityRecord.forToken(token);
+        if (mHistory.contains(r)) {
+            return r;
         }
         return null;
     }
@@ -517,7 +516,7 @@
             throws RemoteException {
 
         r.startFreezingScreenLocked(app, 0);
-        mService.mWindowManager.setAppVisibility(r, true);
+        mService.mWindowManager.setAppVisibility(r.appToken, true);
 
         // Have the window manager re-evaluate the orientation of
         // the screen based on the new activity order.  Note that
@@ -528,7 +527,7 @@
         if (checkConfig) {
             Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
                     mService.mConfiguration,
-                    r.mayFreezeScreenLocked(app) ? r : null);
+                    r.mayFreezeScreenLocked(app) ? r.appToken : null);
             mService.updateConfigurationLocked(config, r, false);
         }
 
@@ -590,7 +589,7 @@
                     profileFd = null;
                 }
             }
-            app.thread.scheduleLaunchActivity(new Intent(r.intent), r,
+            app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
                     System.identityHashCode(r), r.info, mService.mConfiguration,
                     r.compat, r.icicle, results, newIntents, !andResume,
                     mService.isNextTransitionForward(), profileFile, profileFd,
@@ -624,7 +623,7 @@
                       + r.intent.getComponent().flattenToShortString()
                       + ", giving up", e);
                 mService.appDiedLocked(app, app.pid, app.thread);
-                requestFinishActivityLocked(r, Activity.RESULT_CANCELED, null,
+                requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
                         "2nd-crash");
                 return false;
             }
@@ -821,7 +820,7 @@
         }
 
         if (w > 0) {
-            return mService.mWindowManager.screenshotApplications(who, w, h);
+            return mService.mWindowManager.screenshotApplications(who.appToken, w, h);
         }
         return null;
     }
@@ -856,8 +855,8 @@
                 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
                         System.identityHashCode(prev),
                         prev.shortComponentName);
-                prev.app.thread.schedulePauseActivity(prev, prev.finishing, userLeaving,
-                        prev.configChangeFlags);
+                prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
+                        userLeaving, prev.configChangeFlags);
                 if (mMainStack) {
                     mService.updateUsageStats(prev, false);
                 }
@@ -1129,7 +1128,7 @@
                     if (!r.visible) {
                         if (DEBUG_VISBILITY) Slog.v(
                                 TAG, "Starting and making visible: " + r);
-                        mService.mWindowManager.setAppVisibility(r, true);
+                        mService.mWindowManager.setAppVisibility(r.appToken, true);
                     }
                     if (r != starting) {
                         startSpecificActivityLocked(r, false, false);
@@ -1153,10 +1152,10 @@
                     if (DEBUG_VISBILITY) Slog.v(
                             TAG, "Making visible and scheduling visibility: " + r);
                     try {
-                        mService.mWindowManager.setAppVisibility(r, true);
+                        mService.mWindowManager.setAppVisibility(r.appToken, true);
                         r.sleeping = false;
                         r.app.pendingUiClean = true;
-                        r.app.thread.scheduleWindowVisibility(r, true);
+                        r.app.thread.scheduleWindowVisibility(r.appToken, true);
                         r.stopFreezingScreenLocked(false);
                     } catch (Exception e) {
                         // Just skip on any failure; we'll make it
@@ -1195,13 +1194,13 @@
                                 TAG, "Making invisible: " + r);
                         r.visible = false;
                         try {
-                            mService.mWindowManager.setAppVisibility(r, false);
+                            mService.mWindowManager.setAppVisibility(r.appToken, false);
                             if ((r.state == ActivityState.STOPPING
                                     || r.state == ActivityState.STOPPED)
                                     && r.app != null && r.app.thread != null) {
                                 if (DEBUG_VISBILITY) Slog.v(
                                         TAG, "Scheduling invisibility: " + r);
-                                r.app.thread.scheduleWindowVisibility(r, false);
+                                r.app.thread.scheduleWindowVisibility(r.appToken, false);
                             }
                         } catch (Exception e) {
                             // Just skip on any failure; we'll make it
@@ -1351,7 +1350,7 @@
                 // previous should actually be hidden depending on whether the
                 // new one is found to be full-screen or not.
                 if (prev.finishing) {
-                    mService.mWindowManager.setAppVisibility(prev, false);
+                    mService.mWindowManager.setAppVisibility(prev.appToken, false);
                     if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
                             + prev + ", waitingVisible="
                             + (prev != null ? prev.waitingVisible : null)
@@ -1399,8 +1398,8 @@
                             ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE
                             : WindowManagerPolicy.TRANSIT_TASK_CLOSE, false);
                 }
-                mService.mWindowManager.setAppWillBeHidden(prev);
-                mService.mWindowManager.setAppVisibility(prev, false);
+                mService.mWindowManager.setAppWillBeHidden(prev.appToken);
+                mService.mWindowManager.setAppVisibility(prev.appToken, false);
             } else {
                 if (DEBUG_TRANSITION) Slog.v(TAG,
                         "Prepare open transition: prev=" + prev);
@@ -1414,8 +1413,8 @@
                 }
             }
             if (false) {
-                mService.mWindowManager.setAppWillBeHidden(prev);
-                mService.mWindowManager.setAppVisibility(prev, false);
+                mService.mWindowManager.setAppWillBeHidden(prev.appToken);
+                mService.mWindowManager.setAppVisibility(prev.appToken, false);
             }
         } else if (mHistory.size() > 1) {
             if (DEBUG_TRANSITION) Slog.v(TAG,
@@ -1433,7 +1432,7 @@
             if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
 
             // This activity is now becoming visible.
-            mService.mWindowManager.setAppVisibility(next, true);
+            mService.mWindowManager.setAppVisibility(next.appToken, true);
 
             ActivityRecord lastResumedActivity = mResumedActivity;
             ActivityState lastState = next.state;
@@ -1457,7 +1456,7 @@
                 synchronized (mService) {
                     Configuration config = mService.mWindowManager.updateOrientationFromAppTokens(
                             mService.mConfiguration,
-                            next.mayFreezeScreenLocked(next.app) ? next : null);
+                            next.mayFreezeScreenLocked(next.app) ? next.appToken : null);
                     if (config != null) {
                         next.frozenBeforeDestroy = true;
                     }
@@ -1496,12 +1495,12 @@
                         if (DEBUG_RESULTS) Slog.v(
                                 TAG, "Delivering results to " + next
                                 + ": " + a);
-                        next.app.thread.scheduleSendResult(next, a);
+                        next.app.thread.scheduleSendResult(next.appToken, a);
                     }
                 }
 
                 if (next.newIntents != null) {
-                    next.app.thread.scheduleNewIntent(next.newIntents, next);
+                    next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
                 }
 
                 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
@@ -1511,7 +1510,7 @@
                 next.sleeping = false;
                 showAskCompatModeDialogLocked(next);
                 next.app.pendingUiClean = true;
-                next.app.thread.scheduleResumeActivity(next,
+                next.app.thread.scheduleResumeActivity(next.appToken,
                         mService.isNextTransitionForward());
                 
                 checkReadyForSleepLocked();
@@ -1528,7 +1527,7 @@
                 } else {
                     if (SHOW_APP_STARTING_PREVIEW && mMainStack) {
                         mService.mWindowManager.setAppStartingWindow(
-                                next, next.packageName, next.theme,
+                                next.appToken, next.packageName, next.theme,
                                 mService.compatibilityInfoForPackageLocked(
                                         next.info.applicationInfo),
                                 next.nonLocalizedLabel,
@@ -1549,7 +1548,7 @@
                 // If any exception gets thrown, toss away this
                 // activity and try the next one.
                 Slog.w(TAG, "Exception thrown during resume of " + next, e);
-                requestFinishActivityLocked(next, Activity.RESULT_CANCELED, null,
+                requestFinishActivityLocked(next.appToken, Activity.RESULT_CANCELED, null,
                         "resume-exception");
                 return true;
             }
@@ -1567,7 +1566,7 @@
             } else {
                 if (SHOW_APP_STARTING_PREVIEW) {
                     mService.mWindowManager.setAppStartingWindow(
-                            next, next.packageName, next.theme,
+                            next.appToken, next.packageName, next.theme,
                             mService.compatibilityInfoForPackageLocked(
                                     next.info.applicationInfo),
                             next.nonLocalizedLabel,
@@ -1610,10 +1609,10 @@
                         }
                         mHistory.add(addPos, r);
                         r.putInHistory();
-                        mService.mWindowManager.addAppToken(addPos, r, r.task.taskId,
+                        mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
                                 r.info.screenOrientation, r.fullscreen);
                         if (VALIDATE_TOKENS) {
-                            mService.mWindowManager.validateAppTokens(mHistory);
+                            validateAppTokensLocked();
                         }
                         return;
                     }
@@ -1677,7 +1676,7 @@
                 mNoAnimActivities.remove(r);
             }
             mService.mWindowManager.addAppToken(
-                    addPos, r, r.task.taskId, r.info.screenOrientation, r.fullscreen);
+                    addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen);
             boolean doShow = true;
             if (newTask) {
                 // Even though this activity is starting fresh, we still need
@@ -1705,19 +1704,20 @@
                     else if (prev.nowVisible) prev = null;
                 }
                 mService.mWindowManager.setAppStartingWindow(
-                        r, r.packageName, r.theme,
+                        r.appToken, r.packageName, r.theme,
                         mService.compatibilityInfoForPackageLocked(
                                 r.info.applicationInfo), r.nonLocalizedLabel,
-                        r.labelRes, r.icon, r.windowFlags, prev, showStartingIcon);
+                        r.labelRes, r.icon, r.windowFlags,
+                        prev != null ? prev.appToken : null, showStartingIcon);
             }
         } else {
             // If this is the first activity, don't do any fancy animations,
             // because there is nothing for it to animate on top of.
-            mService.mWindowManager.addAppToken(addPos, r, r.task.taskId,
+            mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
                     r.info.screenOrientation, r.fullscreen);
         }
         if (VALIDATE_TOKENS) {
-            mService.mWindowManager.validateAppTokens(mHistory);
+            validateAppTokensLocked();
         }
 
         if (doResume) {
@@ -1725,6 +1725,15 @@
         }
     }
 
+    final void validateAppTokensLocked() {
+        mValidateAppTokens.clear();
+        mValidateAppTokens.ensureCapacity(mHistory.size());
+        for (int i=0; i<mHistory.size(); i++) {
+            mValidateAppTokens.add(mHistory.get(i).appToken);
+        }
+        mService.mWindowManager.validateAppTokens(mValidateAppTokens);
+    }
+
     /**
      * Perform a reset of the given task, if needed as part of launching it.
      * Returns the new HistoryRecord at the top of the task.
@@ -1826,7 +1835,7 @@
                             if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
                                     + " out to new task " + target.task);
                         }
-                        mService.mWindowManager.setAppGroupId(target, task.taskId);
+                        mService.mWindowManager.setAppGroupId(target.appToken, task.taskId);
                         if (replyChainEnd < 0) {
                             replyChainEnd = targetI;
                         }
@@ -1849,11 +1858,11 @@
                             }
                             mHistory.remove(srcPos);
                             mHistory.add(dstPos, p);
-                            mService.mWindowManager.moveAppToken(dstPos, p);
-                            mService.mWindowManager.setAppGroupId(p, p.task.taskId);
+                            mService.mWindowManager.moveAppToken(dstPos, p.appToken);
+                            mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
                             dstPos++;
                             if (VALIDATE_TOKENS) {
-                                mService.mWindowManager.validateAppTokens(mHistory);
+                                validateAppTokensLocked();
                             }
                             i++;
                         }
@@ -1985,10 +1994,10 @@
                         mHistory.add(lastReparentPos, p);
                         if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p
                                 + " in to resetting task " + task);
-                        mService.mWindowManager.moveAppToken(lastReparentPos, p);
-                        mService.mWindowManager.setAppGroupId(p, p.task.taskId);
+                        mService.mWindowManager.moveAppToken(lastReparentPos, p.appToken);
+                        mService.mWindowManager.setAppGroupId(p.appToken, p.task.taskId);
                         if (VALIDATE_TOKENS) {
-                            mService.mWindowManager.validateAppTokens(mHistory);
+                            validateAppTokensLocked();
                         }
                     }
                     replyChainEnd = -1;
@@ -2081,7 +2090,7 @@
                 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
                         && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
                     if (!ret.finishing) {
-                        int index = indexOfTokenLocked(ret);
+                        int index = indexOfTokenLocked(ret.appToken);
                         if (index >= 0) {
                             finishActivityLocked(ret, index, Activity.RESULT_CANCELED,
                                     null, "clear");
@@ -3007,7 +3016,7 @@
                         return res;
                     }
 
-                    resultTo = outActivity[0];
+                    resultTo = outActivity[0] != null ? outActivity[0].appToken : null;
                 }
             }
         } finally {
@@ -3065,7 +3074,7 @@
                 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
                 list.add(new ResultInfo(resultWho, requestCode,
                         resultCode, data));
-                r.app.thread.scheduleSendResult(r, list);
+                r.app.thread.scheduleSendResult(r.appToken, list);
                 return;
             } catch (Exception e) {
                 Slog.w(TAG, "Exception thrown sending result to " + r, e);
@@ -3080,7 +3089,7 @@
         if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
                 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
             if (!r.finishing) {
-                requestFinishActivityLocked(r, Activity.RESULT_CANCELED, null,
+                requestFinishActivityLocked(r.appToken, Activity.RESULT_CANCELED, null,
                         "no-history");
             }
         } else if (r.app != null && r.app.thread != null) {
@@ -3098,9 +3107,9 @@
                 if (DEBUG_VISBILITY) Slog.v(
                         TAG, "Stopping visible=" + r.visible + " for " + r);
                 if (!r.visible) {
-                    mService.mWindowManager.setAppVisibility(r, false);
+                    mService.mWindowManager.setAppVisibility(r.appToken, false);
                 }
-                r.app.thread.scheduleStopActivity(r, r.visible, r.configChangeFlags);
+                r.app.thread.scheduleStopActivity(r.appToken, r.visible, r.configChangeFlags);
                 if (mService.isSleeping()) {
                     r.setSleeping(true);
                 }
@@ -3145,7 +3154,7 @@
                     // normal flow and hide it once we determine that it is
                     // hidden by the activities in front of it.
                     if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
-                    mService.mWindowManager.setAppVisibility(s, false);
+                    mService.mWindowManager.setAppVisibility(s.appToken, false);
                 }
             }
             if ((!s.waitingVisible || mService.isSleeping()) && remove) {
@@ -3186,14 +3195,14 @@
         boolean enableScreen = false;
 
         synchronized (mService) {
-            if (token != null) {
-                mHandler.removeMessages(IDLE_TIMEOUT_MSG, token);
+            ActivityRecord r = ActivityRecord.forToken(token);
+            if (r != null) {
+                mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
             }
 
             // Get the activity record.
-            int index = indexOfTokenLocked(token);
+            int index = indexOfActivityLocked(r);
             if (index >= 0) {
-                ActivityRecord r = mHistory.get(index);
                 res = r;
 
                 if (fromTimeout) {
@@ -3413,7 +3422,7 @@
                     : WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE, false);
     
             // Tell window manager to prepare for this one to be removed.
-            mService.mWindowManager.setAppVisibility(r, false);
+            mService.mWindowManager.setAppVisibility(r.appToken, false);
                 
             if (mPausingActivity == null) {
                 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
@@ -3440,7 +3449,7 @@
 
     private final ActivityRecord finishCurrentActivityLocked(ActivityRecord r,
             int mode) {
-        final int index = indexOfTokenLocked(r);
+        final int index = indexOfActivityLocked(r);
         if (index < 0) {
             return null;
         }
@@ -3570,9 +3579,9 @@
             if (DEBUG_STATES) Slog.v(TAG, "Moving to DESTROYED: " + r
                     + " (removed from history)");
             r.state = ActivityState.DESTROYED;
-            mService.mWindowManager.removeAppToken(r);
+            mService.mWindowManager.removeAppToken(r.appToken);
             if (VALIDATE_TOKENS) {
-                mService.mWindowManager.validateAppTokens(mHistory);
+                validateAppTokensLocked();
             }
             cleanUpActivityServicesLocked(r);
             r.removeUriPermissionsLocked();
@@ -3653,7 +3662,7 @@
             
             try {
                 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
-                r.app.thread.scheduleDestroyActivity(r, r.finishing,
+                r.app.thread.scheduleDestroyActivity(r.appToken, r.finishing,
                         r.configChangeFlags);
             } catch (Exception e) {
                 // We can just ignore exceptions here...  if the process
@@ -3712,11 +3721,13 @@
 
     final void activityDestroyed(IBinder token) {
         synchronized (mService) {
-            mHandler.removeMessages(DESTROY_TIMEOUT_MSG, token);
+            ActivityRecord r = ActivityRecord.forToken(token);
+            if (r != null) {
+                mHandler.removeMessages(DESTROY_TIMEOUT_MSG, r);
+            }
             
-            int index = indexOfTokenLocked(token);
+            int index = indexOfActivityLocked(r);
             if (index >= 0) {
-                ActivityRecord r = mHistory.get(index);
                 if (r.state == ActivityState.DESTROYING) {
                     final long origId = Binder.clearCallingIdentity();
                     removeActivityFromHistoryLocked(r);
@@ -3781,7 +3792,7 @@
             return;
         }
 
-        ArrayList moved = new ArrayList();
+        ArrayList<IBinder> moved = new ArrayList<IBinder>();
 
         // Applying the affinities may have removed entries from the history,
         // so get the size again.
@@ -3803,7 +3814,7 @@
                 }
                 mHistory.remove(pos);
                 mHistory.add(top, r);
-                moved.add(0, r);
+                moved.add(0, r.appToken);
                 top--;
             }
             pos--;
@@ -3826,7 +3837,7 @@
         
         mService.mWindowManager.moveAppTokensToTop(moved);
         if (VALIDATE_TOKENS) {
-            mService.mWindowManager.validateAppTokens(mHistory);
+            validateAppTokensLocked();
         }
 
         finishTaskMoveLocked(task);
@@ -3873,7 +3884,7 @@
             }
         }
 
-        ArrayList moved = new ArrayList();
+        ArrayList<IBinder> moved = new ArrayList<IBinder>();
 
         if (DEBUG_TRANSITION) Slog.v(TAG,
                 "Prepare to back transition: task=" + task);
@@ -3898,7 +3909,7 @@
                 }
                 mHistory.remove(pos);
                 mHistory.add(bottom, r);
-                moved.add(r);
+                moved.add(r.appToken);
                 bottom++;
             }
             pos++;
@@ -3918,7 +3929,7 @@
         }
         mService.mWindowManager.moveAppTokensToBottom(moved);
         if (VALIDATE_TOKENS) {
-            mService.mWindowManager.validateAppTokens(mHistory);
+            validateAppTokensLocked();
         }
 
         finishTaskMoveLocked(task);
@@ -4148,7 +4159,7 @@
         if (r.app != null && r.app.thread != null) {
             try {
                 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
-                r.app.thread.scheduleActivityConfigurationChanged(r);
+                r.app.thread.scheduleActivityConfigurationChanged(r.appToken);
             } catch (RemoteException e) {
                 // If process died, whatever.
             }
@@ -4178,7 +4189,7 @@
         try {
             if (DEBUG_SWITCH) Slog.i(TAG, "Switch is restarting resumed " + r);
             r.forceNewConfig = false;
-            r.app.thread.scheduleRelaunchActivity(r, results, newIntents,
+            r.app.thread.scheduleRelaunchActivity(r.appToken, results, newIntents,
                     changes, !andResume, mService.mConfiguration);
             // Note: don't need to call pauseIfSleepingLocked() here, because
             // the caller will only pass in 'andResume' if this activity is