Skip to content

Commit 9918ffb

Browse files
Xavier DucrohetAndroid (Google) Code Review
authored andcommitted
Merge "CherryPick 988eeeb5 from hc-mr1. do not merge." into gingerbread
2 parents 54146a6 + 6b62c82 commit 9918ffb

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import java.util.Map;
7777
import java.util.TreeMap;
7878
import java.util.Map.Entry;
79+
import java.util.concurrent.atomic.AtomicBoolean;
80+
import java.util.concurrent.atomic.AtomicReference;
7981

8082
/**
8183
* Custom implementation of Context/Activity to handle non compiled resources.
@@ -502,11 +504,12 @@ public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs,
502504
return null;
503505
}
504506

505-
boolean[] frameworkAttributes = new boolean[1];
506-
TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes);
507+
AtomicBoolean frameworkAttributes = new AtomicBoolean();
508+
AtomicReference<String> attrName = new AtomicReference<String>();
509+
TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes, attrName);
507510

508511
BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length,
509-
isPlatformFile);
512+
isPlatformFile, frameworkAttributes.get(), attrName.get());
510513

511514
// look for a custom style.
512515
String customStyle = null;
@@ -597,7 +600,7 @@ public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs,
597600
}
598601

599602
String namespace = BridgeConstants.NS_RESOURCES;
600-
if (frameworkAttributes[0] == false) {
603+
if (frameworkAttributes.get() == false) {
601604
// need to use the application namespace
602605
namespace = mProjectCallback.getNamespace();
603606
}
@@ -674,10 +677,12 @@ public Looper getMainLooper() {
674677
*/
675678
private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs)
676679
throws Resources.NotFoundException {
677-
TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, null);
680+
AtomicBoolean frameworkAttributes = new AtomicBoolean();
681+
AtomicReference<String> attrName = new AtomicReference<String>();
682+
TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes, attrName);
678683

679684
BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length,
680-
false /* platformResourceFlag */);
685+
style.isFramework(), frameworkAttributes.get(), attrName.get());
681686

682687
// loop through all the values in the style map, and init the TypedArray with
683688
// the style we got from the dynamic id
@@ -709,10 +714,13 @@ private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, in
709714
* that is used to reference the attribute later in the TypedArray.
710715
*
711716
* @param attrs An attribute array reference given to obtainStyledAttributes.
717+
* @param outFrameworkFlag out value indicating if the attr array is a framework value
718+
* @param outAttrName out value for the resolved attr name.
712719
* @return A sorted map Attribute-Value to Attribute-Name for all attributes declared by the
713720
* attribute array. Returns null if nothing is found.
714721
*/
715-
private TreeMap<Integer,String> searchAttrs(int[] attrs, boolean[] outFrameworkFlag) {
722+
private TreeMap<Integer,String> searchAttrs(int[] attrs, AtomicBoolean outFrameworkFlag,
723+
AtomicReference<String> outAttrName) {
716724
// get the name of the array from the framework resources
717725
String arrayName = Bridge.resolveResourceId(attrs);
718726
if (arrayName != null) {
@@ -729,7 +737,10 @@ private TreeMap<Integer,String> searchAttrs(int[] attrs, boolean[] outFrameworkF
729737
}
730738

731739
if (outFrameworkFlag != null) {
732-
outFrameworkFlag[0] = true;
740+
outFrameworkFlag.set(true);
741+
}
742+
if (outAttrName != null) {
743+
outAttrName.set(arrayName);
733744
}
734745

735746
return attributes;
@@ -751,7 +762,10 @@ private TreeMap<Integer,String> searchAttrs(int[] attrs, boolean[] outFrameworkF
751762
}
752763

753764
if (outFrameworkFlag != null) {
754-
outFrameworkFlag[0] = false;
765+
outFrameworkFlag.set(false);
766+
}
767+
if (outAttrName != null) {
768+
outAttrName.set(arrayName);
755769
}
756770

757771
return attributes;

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ private BridgeResources(BridgeContext context, AssetManager assets, DisplayMetri
125125
mProjectCallback = projectCallback;
126126
}
127127

128-
public BridgeTypedArray newTypeArray(int numEntries, boolean platformFile) {
129-
return new BridgeTypedArray(this, mContext, numEntries, platformFile);
128+
public BridgeTypedArray newTypeArray(int numEntries, boolean platformFile,
129+
boolean platformStyleable, String styleableName) {
130+
return new BridgeTypedArray(this, mContext, numEntries, platformFile,
131+
platformStyleable, styleableName);
130132
}
131133

132134
private ResourceValue getResourceValue(int id, boolean[] platformResFlag_out) {

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.android.layoutlib.bridge.android;
1818

19+
import com.android.ide.common.rendering.api.DeclareStyleableResourceValue;
1920
import com.android.ide.common.rendering.api.LayoutLog;
2021
import com.android.ide.common.rendering.api.RenderResources;
2122
import com.android.ide.common.rendering.api.ResourceValue;
@@ -49,18 +50,23 @@
4950
*/
5051
public final class BridgeTypedArray extends TypedArray {
5152

52-
private BridgeResources mBridgeResources;
53-
private BridgeContext mContext;
53+
private final BridgeResources mBridgeResources;
54+
private final BridgeContext mContext;
55+
private final boolean mPlatformFile;
56+
private final boolean mPlatformStyleable;
57+
private final String mStyleableName;
58+
5459
private ResourceValue[] mResourceData;
5560
private String[] mNames;
56-
private final boolean mPlatformFile;
5761

5862
public BridgeTypedArray(BridgeResources resources, BridgeContext context, int len,
59-
boolean platformFile) {
63+
boolean platformFile, boolean platformStyleable, String styleableName) {
6064
super(null, null, null, 0);
6165
mBridgeResources = resources;
6266
mContext = context;
6367
mPlatformFile = platformFile;
68+
mPlatformStyleable = platformStyleable;
69+
mStyleableName = styleableName;
6470
mResourceData = new ResourceValue[len];
6571
mNames = new String[len];
6672
}
@@ -202,7 +208,18 @@ public int getInt(int index, int defValue) {
202208
// Field is not null and is not an integer.
203209
// Check for possible constants and try to find them.
204210
// Get the map of attribute-constant -> IntegerValue
205-
Map<String, Integer> map = Bridge.getEnumValues(mNames[index]);
211+
Map<String, Integer> map = null;
212+
if (mPlatformStyleable) {
213+
map = Bridge.getEnumValues(mNames[index]);
214+
} else {
215+
// get the styleable matching the resolved name
216+
RenderResources res = mContext.getRenderResources();
217+
ResourceValue styleable = res.getProjectResource(ResourceType.DECLARE_STYLEABLE,
218+
mStyleableName);
219+
if (styleable instanceof DeclareStyleableResourceValue) {
220+
map = ((DeclareStyleableResourceValue) styleable).getAttributeValues(mNames[index]);
221+
}
222+
}
206223

207224
if (map != null) {
208225
// accumulator to store the value of the 1+ constants.

0 commit comments

Comments
 (0)