diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java
index dc965edd7fada..34e6f04f04044 100644
--- a/core/java/android/util/SparseArray.java
+++ b/core/java/android/util/SparseArray.java
@@ -160,6 +160,9 @@ public class SparseArray implements Cloneable {
/**
* Removes the mapping at the specified index.
+ *
+ * For indices outside of the range 0...size()-1,
+ * the behavior is undefined.
*/
public void removeAt(int index) {
if (mValues[index] != DELETED) {
@@ -173,6 +176,9 @@ public class SparseArray implements Cloneable {
*
* @param index Index to begin at
* @param size Number of mappings to remove
+ *
+ * For indices outside of the range 0...size()-1,
+ * the behavior is undefined.
*/
public void removeAtRange(int index, int size) {
final int end = Math.min(mSize, index + size);
@@ -262,6 +268,9 @@ public class SparseArray implements Cloneable {
* be in ascending order, e.g., keyAt(0) will return the
* smallest key and keyAt(size()-1) will return the largest
* key.
+ *
+ * For indices outside of the range 0...size()-1,
+ * the behavior is undefined.
*/
public int keyAt(int index) {
if (mGarbage) {
@@ -281,6 +290,9 @@ public class SparseArray implements Cloneable {
* valueAt(0) will return the value associated with the
* smallest key and valueAt(size()-1) will return the value
* associated with the largest key.
+ *
+ * For indices outside of the range 0...size()-1,
+ * the behavior is undefined.
*/
@SuppressWarnings("unchecked")
public E valueAt(int index) {
@@ -295,6 +307,8 @@ public class SparseArray implements Cloneable {
* Given an index in the range 0...size()-1, sets a new
* value for the indexth key-value mapping that this
* SparseArray stores.
+ *
+ * For indices outside of the range 0...size()-1, the behavior is undefined.
*/
public void setValueAt(int index, E value) {
if (mGarbage) {
diff --git a/core/java/android/view/accessibility/AccessibilityCache.java b/core/java/android/view/accessibility/AccessibilityCache.java
index 1da305f09f541..28e31c4c7aa83 100644
--- a/core/java/android/view/accessibility/AccessibilityCache.java
+++ b/core/java/android/view/accessibility/AccessibilityCache.java
@@ -221,8 +221,11 @@ final class AccessibilityCache {
sortedWindows.put(window.getLayer(), window);
}
- List windows = new ArrayList<>(windowCount);
- for (int i = windowCount - 1; i >= 0; i--) {
+ // It's possible in transient conditions for two windows to share the same
+ // layer, which results in sortedWindows being smaller than mWindowCache
+ final int sortedWindowCount = sortedWindows.size();
+ List windows = new ArrayList<>(sortedWindowCount);
+ for (int i = sortedWindowCount - 1; i >= 0; i--) {
AccessibilityWindowInfo window = sortedWindows.valueAt(i);
windows.add(AccessibilityWindowInfo.obtain(window));
sortedWindows.removeAt(i);