From d4d3b821be940868bf1292c87397431f118743f8 Mon Sep 17 00:00:00 2001 From: Jeff DeCew Date: Wed, 24 Mar 2021 15:42:17 -0400 Subject: [PATCH] Make TypedArray implement AutoCloseable Fixes: 170316676 Test: atest TypedArrayTest Change-Id: If3af1c1402156e43f29b166bfcaabc16ee0c0330 --- core/api/current.txt | 3 ++- core/java/android/content/res/TypedArray.java | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index 3b26ce6fcd5d6..b1142361de952 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -13328,7 +13328,8 @@ package android.content.res { method public void setTo(android.content.res.Resources.Theme); } - public class TypedArray { + public class TypedArray implements java.lang.AutoCloseable { + method public void close(); method public boolean getBoolean(@StyleableRes int, boolean); method public int getChangingConfigurations(); method @ColorInt public int getColor(@StyleableRes int, @ColorInt int); diff --git a/core/java/android/content/res/TypedArray.java b/core/java/android/content/res/TypedArray.java index 5eaa7662b250b..2a349e9a4600d 100644 --- a/core/java/android/content/res/TypedArray.java +++ b/core/java/android/content/res/TypedArray.java @@ -46,7 +46,7 @@ import java.util.Arrays; * The indices used to retrieve values from this structure correspond to * the positions of the attributes given to obtainStyledAttributes. */ -public class TypedArray { +public class TypedArray implements AutoCloseable { static TypedArray obtain(Resources res, int len) { TypedArray attrs = res.mTypedArrayPool.acquire(); @@ -1252,6 +1252,17 @@ public class TypedArray { mResources.mTypedArrayPool.release(this); } + /** + * Recycles the TypedArray, to be re-used by a later caller. After calling + * this function you must not ever touch the typed array again. + * + * @see #recycle() + * @throws RuntimeException if the TypedArray has already been recycled. + */ + public void close() { + recycle(); + } + /** * Extracts theme attributes from a typed array for later resolution using * {@link android.content.res.Resources.Theme#resolveAttributes(int[], int[])}.