Skip to content

Commit ab0f485

Browse files
author
Dianne Hackborn
committed
Fix problems where we were allocating TypedArray when not needed.
Fixes up some recycling of TypedArray objects to reduce the number we need to allocate during inflation etc. Change-Id: I948dccc052997779001eaa99db2a710b04be01ae
1 parent bd5c976 commit ab0f485

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

core/java/android/app/ActionBar.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ public LayoutParams(Context c, AttributeSet attrs) {
915915
com.android.internal.R.styleable.ActionBar_LayoutParams);
916916
gravity = a.getInt(
917917
com.android.internal.R.styleable.ActionBar_LayoutParams_layout_gravity, -1);
918+
a.recycle();
918919
}
919920

920921
public LayoutParams(int width, int height) {

core/java/android/content/res/Resources.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class Resources {
7272
static final String TAG = "Resources";
7373
private static final boolean DEBUG_LOAD = false;
7474
private static final boolean DEBUG_CONFIG = false;
75+
private static final boolean DEBUG_ATTRIBUTES_CACHE = false;
7576
private static final boolean TRACE_FOR_PRELOAD = false;
7677
private static final boolean TRACE_FOR_MISS_PRELOAD = false;
7778

@@ -104,6 +105,7 @@ public class Resources {
104105
private boolean mPreloading;
105106

106107
/*package*/ TypedArray mCachedStyledAttributes = null;
108+
RuntimeException mLastRetrievedAttrs = null;
107109

108110
private int mLastCachedXmlBlockIndex = -1;
109111
private final int[] mCachedXmlBlockIds = { 0, 0, 0, 0 };
@@ -2167,6 +2169,10 @@ private TypedArray getCachedStyledAttributes(int len) {
21672169
TypedArray attrs = mCachedStyledAttributes;
21682170
if (attrs != null) {
21692171
mCachedStyledAttributes = null;
2172+
if (DEBUG_ATTRIBUTES_CACHE) {
2173+
mLastRetrievedAttrs = new RuntimeException("here");
2174+
mLastRetrievedAttrs.fillInStackTrace();
2175+
}
21702176

21712177
attrs.mLength = len;
21722178
int fullLen = len * AssetManager.STYLE_NUM_ENTRIES;
@@ -2177,6 +2183,15 @@ private TypedArray getCachedStyledAttributes(int len) {
21772183
attrs.mIndices = new int[1+len];
21782184
return attrs;
21792185
}
2186+
if (DEBUG_ATTRIBUTES_CACHE) {
2187+
RuntimeException here = new RuntimeException("here");
2188+
here.fillInStackTrace();
2189+
if (mLastRetrievedAttrs != null) {
2190+
Log.i(TAG, "Allocated new TypedArray of " + len + " in " + this, here);
2191+
Log.i(TAG, "Last retrieved attributes here", mLastRetrievedAttrs);
2192+
}
2193+
mLastRetrievedAttrs = here;
2194+
}
21802195
return new TypedArray(this,
21812196
new int[len*AssetManager.STYLE_NUM_ENTRIES],
21822197
new int[1+len], len);

core/java/android/view/View.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import android.graphics.Interpolator;
2828
import android.graphics.LinearGradient;
2929
import android.graphics.Matrix;
30-
import android.graphics.Matrix.ScaleToFit;
3130
import android.graphics.Paint;
3231
import android.graphics.PixelFormat;
3332
import android.graphics.Point;
@@ -2272,8 +2271,6 @@ static class TransformationInfo {
22722271
*/
22732272
int mOldHeightMeasureSpec = Integer.MIN_VALUE;
22742273

2275-
private Resources mResources = null;
2276-
22772274
private Drawable mBGDrawable;
22782275

22792276
private int mBackgroundResource;
@@ -2336,6 +2333,8 @@ static class TransformationInfo {
23362333
*/
23372334
protected Context mContext;
23382335

2336+
private final Resources mResources;
2337+
23392338
private ScrollabilityCache mScrollCache;
23402339

23412340
private int[] mDrawableState = null;
@@ -3017,6 +3016,8 @@ public void onClick(View v) {
30173016
}
30183017
}
30193018

3019+
a.recycle();
3020+
30203021
setOverScrollMode(overScrollMode);
30213022

30223023
if (background != null) {
@@ -3074,14 +3075,13 @@ public void onClick(View v) {
30743075
}
30753076

30763077
computeOpaqueFlags();
3077-
3078-
a.recycle();
30793078
}
30803079

30813080
/**
30823081
* Non-public constructor for use in testing
30833082
*/
30843083
View() {
3084+
mResources = null;
30853085
}
30863086

30873087
/**

core/java/android/view/animation/Animation.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,6 @@ public Animation(Context context, AttributeSet attrs) {
232232
setFillBefore(a.getBoolean(com.android.internal.R.styleable.Animation_fillBefore, mFillBefore));
233233
setFillAfter(a.getBoolean(com.android.internal.R.styleable.Animation_fillAfter, mFillAfter));
234234

235-
final int resID = a.getResourceId(com.android.internal.R.styleable.Animation_interpolator, 0);
236-
if (resID > 0) {
237-
setInterpolator(context, resID);
238-
}
239-
240235
setRepeatCount(a.getInt(com.android.internal.R.styleable.Animation_repeatCount, mRepeatCount));
241236
setRepeatMode(a.getInt(com.android.internal.R.styleable.Animation_repeatMode, RESTART));
242237

@@ -245,10 +240,16 @@ public Animation(Context context, AttributeSet attrs) {
245240
setBackgroundColor(a.getInt(com.android.internal.R.styleable.Animation_background, 0));
246241

247242
setDetachWallpaper(a.getBoolean(com.android.internal.R.styleable.Animation_detachWallpaper, false));
248-
249-
ensureInterpolator();
243+
244+
final int resID = a.getResourceId(com.android.internal.R.styleable.Animation_interpolator, 0);
250245

251246
a.recycle();
247+
248+
if (resID > 0) {
249+
setInterpolator(context, resID);
250+
}
251+
252+
ensureInterpolator();
252253
}
253254

254255
@Override

core/java/android/widget/TextView.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,6 @@ public TextView(Context context,
461461
mMovement = getDefaultMovementMethod();
462462
mTransformation = null;
463463

464-
TypedArray a =
465-
context.obtainStyledAttributes(
466-
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
467-
468464
int textColorHighlight = 0;
469465
ColorStateList textColor = null;
470466
ColorStateList textColorHint = null;
@@ -474,18 +470,23 @@ public TextView(Context context,
474470
int styleIndex = -1;
475471
boolean allCaps = false;
476472

473+
final Resources.Theme theme = context.getTheme();
474+
477475
/*
478476
* Look the appearance up without checking first if it exists because
479477
* almost every TextView has one and it greatly simplifies the logic
480478
* to be able to parse the appearance first and then let specific tags
481479
* for this View override it.
482480
*/
481+
TypedArray a = theme.obtainStyledAttributes(
482+
attrs, com.android.internal.R.styleable.TextViewAppearance, defStyle, 0);
483483
TypedArray appearance = null;
484-
int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
484+
int ap = a.getResourceId(
485+
com.android.internal.R.styleable.TextViewAppearance_textAppearance, -1);
486+
a.recycle();
485487
if (ap != -1) {
486-
appearance = context.obtainStyledAttributes(ap,
487-
com.android.internal.R.styleable.
488-
TextAppearance);
488+
appearance = theme.obtainStyledAttributes(
489+
ap, com.android.internal.R.styleable.TextAppearance);
489490
}
490491
if (appearance != null) {
491492
int n = appearance.getIndexCount();
@@ -552,6 +553,9 @@ public TextView(Context context,
552553
boolean password = false;
553554
int inputType = EditorInfo.TYPE_NULL;
554555

556+
a = theme.obtainStyledAttributes(
557+
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
558+
555559
int n = a.getIndexCount();
556560
for (int i = 0; i < n; i++) {
557561
int attr = a.getIndex(i);

core/res/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,10 @@
31773177
<!-- Present the text in ALL CAPS. This may use a small-caps form when available. -->
31783178
<attr name="textAllCaps" />
31793179
</declare-styleable>
3180+
<declare-styleable name="TextViewAppearance">
3181+
<!-- Base text color, typeface, size, and style. -->
3182+
<attr name="textAppearance" />
3183+
</declare-styleable>
31803184
<declare-styleable name="SuggestionSpan">
31813185
<attr name="textUnderlineColor" />
31823186
<attr name="textUnderlineThickness" />

0 commit comments

Comments
 (0)