Skip to content

Commit ea5f0d0

Browse files
author
Xavier Ducrohet
committed
Bring in more layout lib changes from hc-mr1.
fe051bb2 : Change the way the layoutlib instantiate its XmlPullParser. A lot of the init code was duplicated so I made a ParserFactory class. Also created an extension of the KXmlPullParser to override toString(). This allows easier debugging when dealing with multiple parsers (which is always the case). Also added some (disabled) debugging printf to deal with parser stack as it can be tricky figuring out which parsers are in the stack at which point. 8969147c : Fix case where the int[] attrs doesn't directly match a styleable. In the case of the FastScroller the int[] is a custom mix of attr instead of a int[] that exists as R.styleable.foo. This makes our reflection based mechanism used to find the styleable fail, so instead we search for each attribute separately (like we probably should have done from the beginning). 0c264b35: Fix various cases of getDimension to report error if unit is missing. if getDimention###() is called for a string that has no unit, then an error is output through LayoutLog, but the rendering keeps going by using dp as a default. 0beb7eea: Make (Bridge)TypedArray.getInteger() call out to getInt() Only getInt() resolved attribute flags/enum and I'm not sure why there's two to begin with. Change-Id: I015111263d2a2bee76834978ae71eef79defdae3
1 parent d6465e1 commit ea5f0d0

12 files changed

Lines changed: 322 additions & 192 deletions

File tree

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import com.android.ide.common.rendering.api.StyleResourceValue;
2626
import com.android.layoutlib.bridge.Bridge;
2727
import com.android.layoutlib.bridge.BridgeConstants;
28+
import com.android.layoutlib.bridge.impl.ParserFactory;
2829
import com.android.layoutlib.bridge.impl.Stack;
2930
import com.android.resources.ResourceType;
3031
import com.android.util.Pair;
3132

32-
import org.kxml2.io.KXmlParser;
3333
import org.xmlpull.v1.XmlPullParser;
3434
import org.xmlpull.v1.XmlPullParserException;
3535

@@ -201,14 +201,20 @@ public Map<String, String> getDefaultPropMap(Object key) {
201201
* @param parser the parser to add.
202202
*/
203203
public void pushParser(BridgeXmlBlockParser parser) {
204+
if (ParserFactory.LOG_PARSER) {
205+
System.out.println("PUSH " + parser.getParser().toString());
206+
}
204207
mParserStack.push(parser);
205208
}
206209

207210
/**
208211
* Removes the parser at the top of the stack
209212
*/
210213
public void popParser() {
211-
mParserStack.pop();
214+
BridgeXmlBlockParser parser = mParserStack.pop();
215+
if (ParserFactory.LOG_PARSER) {
216+
System.out.println("POPD " + parser.getParser().toString());
217+
}
212218
}
213219

214220
/**
@@ -341,9 +347,7 @@ public Pair<View, Boolean> inflateView(ResourceReference resource, ViewGroup par
341347
// we need to create a pull parser around the layout XML file, and then
342348
// give that to our XmlBlockParser
343349
try {
344-
KXmlParser parser = new KXmlParser();
345-
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
346-
parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$);
350+
XmlPullParser parser = ParserFactory.create(xml);
347351

348352
// set the resource ref to have correct view cookies
349353
mBridgeInflater.setResourceReference(resource);
@@ -682,25 +686,25 @@ public Looper getMainLooper() {
682686
*/
683687
private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs)
684688
throws Resources.NotFoundException {
685-
AtomicBoolean frameworkAttributes = new AtomicBoolean();
686-
AtomicReference<String> attrName = new AtomicReference<String>();
687-
TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes, attrName);
688689

689690
BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length,
690-
style.isFramework(), frameworkAttributes.get(), attrName.get());
691-
692-
// loop through all the values in the style map, and init the TypedArray with
693-
// the style we got from the dynamic id
694-
for (Entry<Integer, String> styleAttribute : styleNameMap.entrySet()) {
695-
int index = styleAttribute.getKey().intValue();
691+
false, true, null);
696692

697-
String name = styleAttribute.getValue();
693+
// for each attribute, get its name so that we can search it in the style
694+
for (int i = 0 ; i < attrs.length ; i++) {
695+
Pair<ResourceType, String> resolvedResource = Bridge.resolveResourceId(attrs[i]);
696+
if (resolvedResource != null) {
697+
String attrName = resolvedResource.getSecond();
698+
// look for the value in the given style
699+
ResourceValue resValue = mRenderResources.findItemInStyle(style, attrName);
698700

699-
// get the value from the style, or its parent styles.
700-
ResourceValue resValue = mRenderResources.findItemInStyle(style, name);
701+
if (resValue != null) {
702+
// resolve it to make sure there are no references left.
703+
ta.bridgeSetValue(i, attrName, mRenderResources.resolveResValue(resValue));
701704

702-
// resolve it to make sure there are no references left.
703-
ta.bridgeSetValue(index, name, mRenderResources.resolveResValue(resValue));
705+
resValue = mRenderResources.resolveResValue(resValue);
706+
}
707+
}
704708
}
705709

706710
ta.sealArray();

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import com.android.ide.common.rendering.api.ResourceReference;
2323
import com.android.ide.common.rendering.api.ResourceValue;
2424
import com.android.layoutlib.bridge.Bridge;
25+
import com.android.layoutlib.bridge.impl.ParserFactory;
2526
import com.android.resources.ResourceType;
2627
import com.android.util.Pair;
2728

28-
import org.kxml2.io.KXmlParser;
2929
import org.xmlpull.v1.XmlPullParser;
3030

3131
import android.content.Context;
@@ -36,7 +36,6 @@
3636
import android.view.ViewGroup;
3737

3838
import java.io.File;
39-
import java.io.FileInputStream;
4039

4140
/**
4241
* Custom implementation of {@link LayoutInflater} to handle custom views.
@@ -175,9 +174,7 @@ public View inflate(int resource, ViewGroup root) {
175174
File f = new File(value.getValue());
176175
if (f.isFile()) {
177176
try {
178-
KXmlParser parser = new KXmlParser();
179-
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
180-
parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$
177+
XmlPullParser parser = ParserFactory.create(f);
181178

182179
BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(
183180
parser, bridgeContext, false);

0 commit comments

Comments
 (0)