Mitigate Resources leak on API 21+
We don't need to use TintResources on API 21+ to
enable vectors, so we can disable it. Doing so
removes a potential memory 'leak' (not a leak,
just using more memory than normal).
BUG: 27877272
Change-Id: I5515691e073cb03fce241b116d99c2ca8259791f
diff --git a/v7/appcompat/src/android/support/v7/app/AppCompatActivity.java b/v7/appcompat/src/android/support/v7/app/AppCompatActivity.java
index 7563fe7..865f49a 100644
--- a/v7/appcompat/src/android/support/v7/app/AppCompatActivity.java
+++ b/v7/appcompat/src/android/support/v7/app/AppCompatActivity.java
@@ -536,9 +536,9 @@
@Override
public Resources getResources() {
- if (mResources == null) {
+ if (Build.VERSION.SDK_INT <= TintResources.MAX_SDK_WHERE_REQUIRED && mResources == null) {
mResources = new TintResources(this, super.getResources());
}
- return mResources;
+ return mResources != null ? mResources : super.getResources();
}
}
diff --git a/v7/appcompat/src/android/support/v7/widget/TintContextWrapper.java b/v7/appcompat/src/android/support/v7/widget/TintContextWrapper.java
index 6647f89..357e185 100644
--- a/v7/appcompat/src/android/support/v7/widget/TintContextWrapper.java
+++ b/v7/appcompat/src/android/support/v7/widget/TintContextWrapper.java
@@ -19,6 +19,7 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
+import android.os.Build;
import android.support.annotation.NonNull;
import java.lang.ref.WeakReference;
@@ -55,12 +56,12 @@
}
private static boolean shouldWrap(@NonNull final Context context) {
- if (context instanceof TintContextWrapper) {
- // If the Context is already a TintContextWrapper, no needed to wrap again
- return false;
- }
- if (context.getResources() instanceof TintResources) {
+ if (Build.VERSION.SDK_INT > TintResources.MAX_SDK_WHERE_REQUIRED
+ || context instanceof TintContextWrapper
+ || context.getResources() instanceof TintResources) {
+ // If we're running on API 21+, there's no need to wrap
// If the Context already has a TintResources impl, no needed to wrap again
+ // If the Context is already a TintContextWrapper, no needed to wrap again
return false;
}
// Else, we should wrap
diff --git a/v7/appcompat/src/android/support/v7/widget/TintResources.java b/v7/appcompat/src/android/support/v7/widget/TintResources.java
index 6739c93..dda6461 100644
--- a/v7/appcompat/src/android/support/v7/widget/TintResources.java
+++ b/v7/appcompat/src/android/support/v7/widget/TintResources.java
@@ -29,6 +29,12 @@
* @hide
*/
public class TintResources extends Resources {
+
+ /**
+ * The maximum API level where this class is needed.
+ */
+ public static final int MAX_SDK_WHERE_REQUIRED = 20;
+
private final WeakReference<Context> mContextRef;
public TintResources(@NonNull final Context context, @NonNull final Resources res) {