Merge "Safeguard against uncommon scenarios when pruning" into oc-dev
diff --git a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
index faa6182..35371a7 100644
--- a/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
+++ b/services/autofill/java/com/android/server/autofill/AutofillManagerServiceImpl.java
@@ -621,16 +621,17 @@
         @Override
         protected Void doInBackground(Void... ignored) {
             int numSessionsToRemove;
-            ArrayMap<IBinder, Integer> sessionsToRemove;
+
+            SparseArray<IBinder> sessionsToRemove;
 
             synchronized (mLock) {
                 numSessionsToRemove = mSessions.size();
-                sessionsToRemove = new ArrayMap<>(numSessionsToRemove);
+                sessionsToRemove = new SparseArray<>(numSessionsToRemove);
 
                 for (int i = 0; i < numSessionsToRemove; i++) {
                     Session session = mSessions.valueAt(i);
 
-                    sessionsToRemove.put(session.getActivityTokenLocked(), session.id);
+                    sessionsToRemove.put(session.id, session.getActivityTokenLocked());
                 }
             }
 
@@ -640,7 +641,7 @@
             for (int i = 0; i < numSessionsToRemove; i++) {
                 try {
                     // The activity manager cannot resolve activities that have been removed
-                    if (am.getActivityClassForToken(sessionsToRemove.keyAt(i)) != null) {
+                    if (am.getActivityClassForToken(sessionsToRemove.valueAt(i)) != null) {
                         sessionsToRemove.removeAt(i);
                         i--;
                         numSessionsToRemove--;
@@ -652,9 +653,10 @@
 
             synchronized (mLock) {
                 for (int i = 0; i < numSessionsToRemove; i++) {
-                    Session sessionToRemove = mSessions.get(sessionsToRemove.valueAt(i));
+                    Session sessionToRemove = mSessions.get(sessionsToRemove.keyAt(i));
 
-                    if (sessionToRemove != null) {
+                    if (sessionToRemove != null && sessionsToRemove.valueAt(i)
+                            == sessionToRemove.getActivityTokenLocked()) {
                         if (sessionToRemove.isSavingLocked()) {
                             if (sVerbose) {
                                 Slog.v(TAG, "Session " + sessionToRemove.id + " is saving");
diff --git a/services/autofill/java/com/android/server/autofill/Session.java b/services/autofill/java/com/android/server/autofill/Session.java
index 4746ee9..0d1eed4 100644
--- a/services/autofill/java/com/android/server/autofill/Session.java
+++ b/services/autofill/java/com/android/server/autofill/Session.java
@@ -1371,10 +1371,15 @@
         pw.print(prefix); pw.print("id: "); pw.println(id);
         pw.print(prefix); pw.print("uid: "); pw.println(uid);
         pw.print(prefix); pw.print("mActivityToken: "); pw.println(mActivityToken);
-        pw.print(prefix); pw.print("mResponses: "); pw.println(mResponses.size());
-        for (int i = 0; i < mResponses.size(); i++) {
-            pw.print(prefix2); pw.print('#'); pw.print(i); pw.print(' ');
-                pw.println(mResponses.valueAt(i));
+        pw.print(prefix); pw.print("mResponses: ");
+        if (mResponses == null) {
+            pw.println("null");
+        } else {
+            pw.println(mResponses.size());
+            for (int i = 0; i < mResponses.size(); i++) {
+                pw.print(prefix2); pw.print('#'); pw.print(i);
+                pw.print(' '); pw.println(mResponses.valueAt(i));
+            }
         }
         pw.print(prefix); pw.print("mCurrentViewId: "); pw.println(mCurrentViewId);
         pw.print(prefix); pw.print("mViewStates size: "); pw.println(mViewStates.size());