From 6d06825ad8809353785eec2f337a6da65ec187fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Engstr=C3=B6m?= Date: Tue, 24 Apr 2012 15:30:19 +0200 Subject: [PATCH] Fix for infinite loop in RemoteViewsAdapter This patch fixes an error in RemoteViewsAdapter when there is only one view in the cache, and it is bigger than the cache size threshold. This would cause the cleanup of the cache to get stuck in an infinite loop while holding the mCache lock that is also needed by for example getView which is called on the UI thread, leading to ANRs. This patch breaks the loop when it sees that it can not remove the next view up for removal. Change-Id: I331259bb10eae9fe91e5112102e08f49cc078a1b --- core/java/android/widget/RemoteViewsAdapter.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/java/android/widget/RemoteViewsAdapter.java b/core/java/android/widget/RemoteViewsAdapter.java index 56bdb9ba4b0ef..e631650dec933 100644 --- a/core/java/android/widget/RemoteViewsAdapter.java +++ b/core/java/android/widget/RemoteViewsAdapter.java @@ -620,7 +620,15 @@ public class RemoteViewsAdapter extends BaseAdapter implements Handler.Callback // remove based on both its position as well as it's current memory usage, as well // as whether it was directly requested vs. whether it was preloaded by our caching // mechanism. - mIndexRemoteViews.remove(getFarthestPositionFrom(pruneFromPosition, visibleWindow)); + int trimIndex = getFarthestPositionFrom(pruneFromPosition, visibleWindow); + + // Need to check that this is a valid index, to cover the case where you have only + // a single view in the cache, but it's larger than the max memory limit + if (trimIndex < 0) { + break; + } + + mIndexRemoteViews.remove(trimIndex); } // Update the metadata cache