OSDN Git Service

Improve encapsulation of Resources
authorAlan Viverette <alanv@google.com>
Fri, 31 Jan 2014 19:07:29 +0000 (11:07 -0800)
committerAlan Viverette <alanv@google.com>
Fri, 31 Jan 2014 19:18:28 +0000 (11:18 -0800)
Change-Id: Ic408915d2695ac8906231da1d2dc127f94bdd0cc

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

index d0494f7..b0b8d2d 100644 (file)
@@ -71,16 +71,19 @@ import libcore.icu.NativePluralRules;
  */
 public class Resources {
     static final String TAG = "Resources";
+
     private static final boolean DEBUG_LOAD = false;
     private static final boolean DEBUG_CONFIG = false;
     private static final boolean DEBUG_ATTRIBUTES_CACHE = false;
     private static final boolean TRACE_FOR_PRELOAD = false;
     private static final boolean TRACE_FOR_MISS_PRELOAD = false;
 
+    private static final int LAYOUT_DIR_CONFIG = ActivityInfo.activityInfoConfigToNative(
+            ActivityInfo.CONFIG_LAYOUT_DIRECTION);
+
     private static final int ID_OTHER = 0x01000004;
 
     private static final Object sSync = new Object();
-    /*package*/ static Resources mSystem = null;
 
     // Information about preloaded resources.  Note that they are not
     // protected by a lock, because while preloading in zygote we are all
@@ -91,32 +94,34 @@ public class Resources {
     private static final LongSparseArray<ColorStateList> sPreloadedColorStateLists
             = new LongSparseArray<ColorStateList>();
 
+    private static Resources mSystem = null;
     private static boolean sPreloaded;
     private static int sPreloadedDensity;
 
     // These are protected by mAccessLock.
 
-    /*package*/ final Object mAccessLock = new Object();
-    /*package*/ final Configuration mTmpConfig = new Configuration();
-    /*package*/ TypedValue mTmpValue = new TypedValue();
-    /*package*/ final LongSparseArray<WeakReference<Drawable.ConstantState> > mDrawableCache
-            = new LongSparseArray<WeakReference<Drawable.ConstantState> >(0);
-    /*package*/ final LongSparseArray<WeakReference<ColorStateList> > mColorStateListCache
-            = new LongSparseArray<WeakReference<ColorStateList> >(0);
-    /*package*/ final LongSparseArray<WeakReference<Drawable.ConstantState> > mColorDrawableCache
-            = new LongSparseArray<WeakReference<Drawable.ConstantState> >(0);
-    /*package*/ boolean mPreloading;
+    private final Object mAccessLock = new Object();
+    private final Configuration mTmpConfig = new Configuration();
+    private final LongSparseArray<WeakReference<Drawable.ConstantState>> mDrawableCache
+            = new LongSparseArray<WeakReference<Drawable.ConstantState>>(0);
+    private final LongSparseArray<WeakReference<ColorStateList>> mColorStateListCache
+            = new LongSparseArray<WeakReference<ColorStateList>>(0);
+    private final LongSparseArray<WeakReference<Drawable.ConstantState>> mColorDrawableCache
+            = new LongSparseArray<WeakReference<Drawable.ConstantState>>(0);
 
-    /*package*/ TypedArray mCachedStyledAttributes = null;
-    RuntimeException mLastRetrievedAttrs = null;
+    private TypedValue mTmpValue = new TypedValue();
+    private boolean mPreloading;
+
+    private TypedArray mCachedStyledAttributes = null;
+    private RuntimeException mLastRetrievedAttrs = null;
 
     private int mLastCachedXmlBlockIndex = -1;
     private final int[] mCachedXmlBlockIds = { 0, 0, 0, 0 };
     private final XmlBlock[] mCachedXmlBlocks = new XmlBlock[4];
 
-    /*package*/ final AssetManager mAssets;
+    private final AssetManager mAssets;
     private final Configuration mConfiguration = new Configuration();
-    /*package*/ final DisplayMetrics mMetrics = new DisplayMetrics();
+    private final DisplayMetrics mMetrics = new DisplayMetrics();
     private NativePluralRules mPluralRule;
 
     private CompatibilityInfo mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
@@ -2022,9 +2027,6 @@ public class Resources {
         return true;
     }
 
-    static private final int LAYOUT_DIR_CONFIG = ActivityInfo.activityInfoConfigToNative(
-            ActivityInfo.CONFIG_LAYOUT_DIRECTION);
-
     /*package*/ Drawable loadDrawable(TypedValue value, int id)
             throws NotFoundException {
 
@@ -2365,6 +2367,16 @@ public class Resources {
                 + Integer.toHexString(id));
     }
 
+    /*package*/ void recycleCachedStyledAttributes(TypedArray attrs) {
+        synchronized (mAccessLock) {
+            final TypedArray cached = mCachedStyledAttributes;
+            if (cached == null || cached.mData.length < attrs.mData.length) {
+                attrs.mXml = null;
+                mCachedStyledAttributes = attrs;
+            }
+        }
+    }
+
     private TypedArray getCachedStyledAttributes(int len) {
         synchronized (mAccessLock) {
             TypedArray attrs = mCachedStyledAttributes;
index 4b96800..87d65a5 100644 (file)
@@ -37,6 +37,8 @@ import java.util.Arrays;
  */
 public class TypedArray {
     private final Resources mResources;
+    private final DisplayMetrics mMetrics;
+    private final AssetManager mAssets;
     /*package*/ XmlBlock.Parser mXml;
     /*package*/ int[] mRsrcs;
     /*package*/ int[] mData;
@@ -392,7 +394,7 @@ public class TypedArray {
             return defValue;
         } else if (type == TypedValue.TYPE_DIMENSION) {
             return TypedValue.complexToDimension(
-                data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
+                data[index+AssetManager.STYLE_DATA], mMetrics);
         }
 
         throw new UnsupportedOperationException("Can't convert to dimension: type=0x"
@@ -424,7 +426,7 @@ public class TypedArray {
             return defValue;
         } else if (type == TypedValue.TYPE_DIMENSION) {
             return TypedValue.complexToDimensionPixelOffset(
-                data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
+                data[index+AssetManager.STYLE_DATA], mMetrics);
         }
 
         throw new UnsupportedOperationException("Can't convert to dimension: type=0x"
@@ -457,7 +459,7 @@ public class TypedArray {
             return defValue;
         } else if (type == TypedValue.TYPE_DIMENSION) {
             return TypedValue.complexToDimensionPixelSize(
-                data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
+                data[index+AssetManager.STYLE_DATA], mMetrics);
         }
 
         throw new UnsupportedOperationException("Can't convert to dimension: type=0x"
@@ -485,7 +487,7 @@ public class TypedArray {
             return data[index+AssetManager.STYLE_DATA];
         } else if (type == TypedValue.TYPE_DIMENSION) {
             return TypedValue.complexToDimensionPixelSize(
-                data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
+                data[index+AssetManager.STYLE_DATA], mMetrics);
         }
 
         throw new RuntimeException(getPositionDescription()
@@ -514,7 +516,7 @@ public class TypedArray {
             return data[index+AssetManager.STYLE_DATA];
         } else if (type == TypedValue.TYPE_DIMENSION) {
             return TypedValue.complexToDimensionPixelSize(
-                data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
+                data[index+AssetManager.STYLE_DATA], mMetrics);
         }
 
         return defValue;
@@ -687,13 +689,7 @@ public class TypedArray {
      * Give back a previously retrieved array, for later re-use.
      */
     public void recycle() {
-        synchronized (mResources.mAccessLock) {
-            TypedArray cached = mResources.mCachedStyledAttributes;
-            if (cached == null || cached.mData.length < mData.length) {
-                mXml = null;
-                mResources.mCachedStyledAttributes = this;
-            }
-        }
+        mResources.recycleCachedStyledAttributes(this);
     }
 
     private boolean getValueAt(int index, TypedValue outValue) {
@@ -722,18 +718,19 @@ public class TypedArray {
             }
             return null;
         }
-        //System.out.println("Getting pooled from: " + v);
-        return mResources.mAssets.getPooledString(
-            cookie, data[index+AssetManager.STYLE_DATA]);
+        return mAssets.getPooledString(cookie, data[index+AssetManager.STYLE_DATA]);
     }
 
     /*package*/ TypedArray(Resources resources, int[] data, int[] indices, int len) {
         mResources = resources;
+        mMetrics = mResources.getDisplayMetrics();
+        mAssets = mResources.getAssets();
         mData = data;
         mIndices = indices;
         mLength = len;
     }
 
+    @Override
     public String toString() {
         return Arrays.toString(mData);
     }