Skip to content

Commit 503247f

Browse files
Xavier DucrohetAndroid (Google) Code Review
authored andcommitted
Merge "Make sure resource references are resolved." into ics-mr1
2 parents 942b823 + c3f979f commit 503247f

File tree

2 files changed

+176
-19
lines changed

2 files changed

+176
-19
lines changed

tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java

Lines changed: 173 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
import com.android.ide.common.rendering.api.RenderResources;
2020
import com.android.ide.common.rendering.api.ResourceValue;
21+
import com.android.internal.util.XmlUtils;
2122
import com.android.layoutlib.bridge.Bridge;
2223
import com.android.layoutlib.bridge.BridgeConstants;
2324
import com.android.layoutlib.bridge.android.BridgeContext;
2425
import com.android.resources.ResourceType;
2526

2627
import org.xmlpull.v1.XmlPullParser;
2728

28-
import android.util.AttributeSet;
29-
import android.util.XmlPullAttributes;
30-
3129
/**
3230
* A correct implementation of the {@link AttributeSet} interface on top of a XmlPullParser
3331
*/
@@ -80,34 +78,192 @@ public int getAttributeNameResource(int index) {
8078
return 0;
8179
}
8280

83-
/*
84-
* (non-Javadoc)
85-
* @see android.util.XmlPullAttributes#getAttributeResourceValue(int, int)
86-
*/
8781
@Override
88-
public int getAttributeResourceValue(int index, int defaultValue) {
89-
String value = getAttributeValue(index);
82+
public int getAttributeListValue(String namespace, String attribute,
83+
String[] options, int defaultValue) {
84+
String value = getAttributeValue(namespace, attribute);
85+
if (value != null) {
86+
ResourceValue r = getResourceValue(value);
9087

91-
return resolveResourceValue(value, defaultValue);
88+
if (r != null) {
89+
value = r.getValue();
90+
}
91+
92+
return XmlUtils.convertValueToList(value, options, defaultValue);
93+
}
94+
95+
return defaultValue;
96+
}
97+
98+
@Override
99+
public boolean getAttributeBooleanValue(String namespace, String attribute,
100+
boolean defaultValue) {
101+
String value = getAttributeValue(namespace, attribute);
102+
if (value != null) {
103+
ResourceValue r = getResourceValue(value);
104+
105+
if (r != null) {
106+
value = r.getValue();
107+
}
108+
109+
return XmlUtils.convertValueToBoolean(value, defaultValue);
110+
}
111+
112+
return defaultValue;
92113
}
93114

94-
/*
95-
* (non-Javadoc)
96-
* @see android.util.XmlPullAttributes#getAttributeResourceValue(java.lang.String, java.lang.String, int)
97-
*/
98115
@Override
99116
public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
100117
String value = getAttributeValue(namespace, attribute);
101118

102119
return resolveResourceValue(value, defaultValue);
103120
}
104121

105-
private int resolveResourceValue(String value, int defaultValue) {
122+
@Override
123+
public int getAttributeIntValue(String namespace, String attribute,
124+
int defaultValue) {
125+
String value = getAttributeValue(namespace, attribute);
126+
if (value != null) {
127+
ResourceValue r = getResourceValue(value);
128+
129+
if (r != null) {
130+
value = r.getValue();
131+
}
132+
133+
return XmlUtils.convertValueToInt(value, defaultValue);
134+
}
135+
136+
return defaultValue;
137+
}
138+
139+
@Override
140+
public int getAttributeUnsignedIntValue(String namespace, String attribute,
141+
int defaultValue) {
142+
String value = getAttributeValue(namespace, attribute);
143+
if (value != null) {
144+
ResourceValue r = getResourceValue(value);
145+
146+
if (r != null) {
147+
value = r.getValue();
148+
}
149+
150+
return XmlUtils.convertValueToUnsignedInt(value, defaultValue);
151+
}
152+
153+
return defaultValue;
154+
}
155+
156+
@Override
157+
public float getAttributeFloatValue(String namespace, String attribute,
158+
float defaultValue) {
159+
String s = getAttributeValue(namespace, attribute);
160+
if (s != null) {
161+
ResourceValue r = getResourceValue(s);
162+
163+
if (r != null) {
164+
s = r.getValue();
165+
}
166+
167+
return Float.parseFloat(s);
168+
}
169+
170+
return defaultValue;
171+
}
172+
173+
@Override
174+
public int getAttributeListValue(int index,
175+
String[] options, int defaultValue) {
176+
return XmlUtils.convertValueToList(
177+
getAttributeValue(index), options, defaultValue);
178+
}
179+
180+
@Override
181+
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
182+
String value = getAttributeValue(index);
183+
if (value != null) {
184+
ResourceValue r = getResourceValue(value);
185+
186+
if (r != null) {
187+
value = r.getValue();
188+
}
189+
190+
return XmlUtils.convertValueToBoolean(value, defaultValue);
191+
}
192+
193+
return defaultValue;
194+
}
195+
196+
@Override
197+
public int getAttributeResourceValue(int index, int defaultValue) {
198+
String value = getAttributeValue(index);
199+
200+
return resolveResourceValue(value, defaultValue);
201+
}
202+
203+
@Override
204+
public int getAttributeIntValue(int index, int defaultValue) {
205+
String value = getAttributeValue(index);
206+
if (value != null) {
207+
ResourceValue r = getResourceValue(value);
208+
209+
if (r != null) {
210+
value = r.getValue();
211+
}
212+
213+
return XmlUtils.convertValueToInt(value, defaultValue);
214+
}
215+
216+
return defaultValue;
217+
}
218+
219+
@Override
220+
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
221+
String value = getAttributeValue(index);
222+
if (value != null) {
223+
ResourceValue r = getResourceValue(value);
224+
225+
if (r != null) {
226+
value = r.getValue();
227+
}
228+
229+
return XmlUtils.convertValueToUnsignedInt(value, defaultValue);
230+
}
231+
232+
return defaultValue;
233+
}
234+
235+
@Override
236+
public float getAttributeFloatValue(int index, float defaultValue) {
237+
String s = getAttributeValue(index);
238+
if (s != null) {
239+
ResourceValue r = getResourceValue(s);
240+
241+
if (r != null) {
242+
s = r.getValue();
243+
}
244+
245+
return Float.parseFloat(s);
246+
}
247+
248+
return defaultValue;
249+
}
250+
251+
// -- private helper methods
252+
253+
/**
254+
* Returns a resolved {@link ResourceValue} from a given value.
255+
*/
256+
private ResourceValue getResourceValue(String value) {
106257
// now look for this particular value
107258
RenderResources resources = mContext.getRenderResources();
108-
ResourceValue resource = resources.resolveResValue(
109-
resources.findResValue(value, mPlatformFile));
259+
return resources.resolveResValue(resources.findResValue(value, mPlatformFile));
260+
}
110261

262+
/**
263+
* Resolves and return a value to its associated integer.
264+
*/
265+
private int resolveResourceValue(String value, int defaultValue) {
266+
ResourceValue resource = getResourceValue(value);
111267
if (resource != null) {
112268
Integer id = null;
113269
if (mPlatformFile || resource.isFramework()) {
@@ -124,5 +280,4 @@ private int resolveResourceValue(String value, int defaultValue) {
124280

125281
return defaultValue;
126282
}
127-
128283
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import com.android.ide.common.rendering.api.LayoutLog;
2525
import com.android.ide.common.rendering.api.RenderSession;
2626
import com.android.ide.common.rendering.api.Result;
27-
import com.android.ide.common.rendering.api.SessionParams;
2827
import com.android.ide.common.rendering.api.Result.Status;
28+
import com.android.ide.common.rendering.api.SessionParams;
2929
import com.android.layoutlib.bridge.impl.FontLoader;
3030
import com.android.layoutlib.bridge.impl.RenderDrawable;
3131
import com.android.layoutlib.bridge.impl.RenderSessionImpl;
@@ -242,6 +242,8 @@ public void onInvokeV(String signature, boolean isNative, Object caller) {
242242
if (fontLoader != null) {
243243
Typeface_Delegate.init(fontLoader);
244244
} else {
245+
log.error(LayoutLog.TAG_BROKEN,
246+
"Failed create FontLoader in layout lib.", null);
245247
return false;
246248
}
247249

0 commit comments

Comments
 (0)