Skip to content

Commit

Permalink
Bring in more layout lib changes from hc-mr1. do not merge.
Browse files Browse the repository at this point in the history
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: I8344a820e270b4bdb7e045906d35440274ae200a
  • Loading branch information
Xavier Ducrohet committed Jun 17, 2011
1 parent fffa6ad commit 4db1f43
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 192 deletions.
Expand Up @@ -25,11 +25,11 @@
import com.android.ide.common.rendering.api.StyleResourceValue;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.BridgeConstants;
import com.android.layoutlib.bridge.impl.ParserFactory;
import com.android.layoutlib.bridge.impl.Stack;
import com.android.resources.ResourceType;
import com.android.util.Pair;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

Expand Down Expand Up @@ -201,14 +201,20 @@ public Map<String, String> getDefaultPropMap(Object key) {
* @param parser the parser to add.
*/
public void pushParser(BridgeXmlBlockParser parser) {
if (ParserFactory.LOG_PARSER) {
System.out.println("PUSH " + parser.getParser().toString());
}
mParserStack.push(parser);
}

/**
* Removes the parser at the top of the stack
*/
public void popParser() {
mParserStack.pop();
BridgeXmlBlockParser parser = mParserStack.pop();
if (ParserFactory.LOG_PARSER) {
System.out.println("POPD " + parser.getParser().toString());
}
}

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

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

BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length,
style.isFramework(), frameworkAttributes.get(), attrName.get());

// loop through all the values in the style map, and init the TypedArray with
// the style we got from the dynamic id
for (Entry<Integer, String> styleAttribute : styleNameMap.entrySet()) {
int index = styleAttribute.getKey().intValue();
false, true, null);

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

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

// resolve it to make sure there are no references left.
ta.bridgeSetValue(index, name, mRenderResources.resolveResValue(resValue));
resValue = mRenderResources.resolveResValue(resValue);
}
}
}

ta.sealArray();
Expand Down
Expand Up @@ -22,10 +22,10 @@
import com.android.ide.common.rendering.api.ResourceReference;
import com.android.ide.common.rendering.api.ResourceValue;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.impl.ParserFactory;
import com.android.resources.ResourceType;
import com.android.util.Pair;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
Expand All @@ -36,7 +36,6 @@
import android.view.ViewGroup;

import java.io.File;
import java.io.FileInputStream;

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

BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(
parser, bridgeContext, false);
Expand Down

0 comments on commit 4db1f43

Please sign in to comment.