Merge "Returning the evicted element from CircularQueue. Calling destroy() on the returned element in CloudSearchPerUserService" into tm-dev
diff --git a/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchPerUserService.java b/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchPerUserService.java
index 2eae6af..222d779 100644
--- a/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchPerUserService.java
+++ b/services/cloudsearch/java/com/android/server/cloudsearch/CloudSearchPerUserService.java
@@ -147,7 +147,10 @@
                 }
             });
             if (sessionInfo.linkToDeath()) {
-                mCallbackQueue.put(requestId, sessionInfo);
+                CloudSearchCallbackInfo removedInfo = mCallbackQueue.put(requestId, sessionInfo);
+                if (removedInfo != null) {
+                    removedInfo.destroy();
+                }
             } else {
                 // destroy the session if calling process is already dead
                 onDestroyLocked(requestId);
diff --git a/services/core/java/com/android/server/CircularQueue.java b/services/core/java/com/android/server/CircularQueue.java
index aac6752..4538078 100644
--- a/services/core/java/com/android/server/CircularQueue.java
+++ b/services/core/java/com/android/server/CircularQueue.java
@@ -16,6 +16,7 @@
 
 package com.android.server;
 
+import android.annotation.Nullable;
 import android.util.ArrayMap;
 
 import java.util.Collection;
@@ -43,16 +44,19 @@
     /**
      * Put a (key|value) pair in the CircularQueue. Only the key will be added to the queue. Value
      * will be added to the ArrayMap.
-     * @return {@code true} (as specified by {@link Collection#add})
+     * @return the most recently removed value if keys were removed, or {@code null} if no keys were
+     * removed.
      */
-    public boolean put(K key, V value) {
+    @Nullable
+    public V put(K key, V value) {
         super.add(key);
         mArrayMap.put(key, value);
+        V removedValue = null;
         while (size() > mLimit) {
             K removedKey = super.remove();
-            mArrayMap.remove(removedKey);
+            removedValue = mArrayMap.remove(removedKey);
         }
-        return true;
+        return removedValue;
     }
 
     /**
diff --git a/services/tests/mockingservicestests/src/com/android/server/CircularQueueTest.java b/services/tests/mockingservicestests/src/com/android/server/CircularQueueTest.java
index fac37fd..01bafc1 100644
--- a/services/tests/mockingservicestests/src/com/android/server/CircularQueueTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/CircularQueueTest.java
@@ -43,11 +43,12 @@
         mQueue = new CircularQueue<>(LIMIT);
         mQueue.put(1, "A");
         mQueue.put(2, "B");
-        mQueue.put(3, "C");
+        String removedElement = mQueue.put(3, "C");
         assertNull(mQueue.getElement(1));
         assertEquals(mQueue.getElement(2), "B");
         assertEquals(mQueue.getElement(3), "C");
-
+        // Confirming that put is returning the deleted element
+        assertEquals(removedElement, "A");
     }
 
     @Test