Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit abb080d7 authored by Diego Perez's avatar Diego Perez
Browse files

Fix support for enum attributes in BridgeContext

Test: Updated BridgeXmlPullAttributesTest
Bug: http://b.android.com/231347
Change-Id: I5fd8b46155ccea6c9b65787f311a0887fa4b4806
parent 6c149326
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
<component name="libraryTable">
  <library name="mockito">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/../../../../out/host/common/obj/JAVA_LIBRARIES/mockito-host_intermediates/javalib.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
<component name="libraryTable">
  <library name="objenesis">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/../../../../out/host/common/obj/JAVA_LIBRARIES/objenesis-host_intermediates/javalib.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -86,5 +86,7 @@
      </library>
    </orderEntry>
    <orderEntry type="library" scope="TEST" name="junit" level="project" />
    <orderEntry type="library" scope="TEST" name="mockito" level="project" />
    <orderEntry type="library" scope="TEST" name="objenesis" level="project" />
  </component>
</module>
 No newline at end of file
+59 −33
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.util;

import com.android.ide.common.rendering.api.AttrResourceValue;
import com.android.ide.common.rendering.api.RenderResources;
import com.android.ide.common.rendering.api.ResourceValue;
import com.android.internal.util.XmlUtils;
@@ -27,6 +28,11 @@ import com.android.resources.ResourceType;

import org.xmlpull.v1.XmlPullParser;

import android.annotation.NonNull;

import java.util.Map;
import java.util.function.Function;

/**
 * A correct implementation of the {@link AttributeSet} interface on top of a XmlPullParser
 */
@@ -34,12 +40,30 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {

    private final BridgeContext mContext;
    private final boolean mPlatformFile;

    public BridgeXmlPullAttributes(XmlPullParser parser, BridgeContext context,
            boolean platformFile) {
    private final Function<String, Map<String, Integer>> mFrameworkEnumValueSupplier;
    private final Function<String, Map<String, Integer>> mProjectEnumValueSupplier;

    // VisibleForTesting
    BridgeXmlPullAttributes(@NonNull XmlPullParser parser, @NonNull BridgeContext context,
            boolean platformFile,
            @NonNull Function<String, Map<String, Integer>> frameworkEnumValueSupplier,
            @NonNull Function<String, Map<String, Integer>> projectEnumValueSupplier) {
        super(parser);
        mContext = context;
        mPlatformFile = platformFile;
        mFrameworkEnumValueSupplier = frameworkEnumValueSupplier;
        mProjectEnumValueSupplier = projectEnumValueSupplier;
    }

    public BridgeXmlPullAttributes(@NonNull XmlPullParser parser, @NonNull BridgeContext context,
            boolean platformFile) {
        this(parser, context, platformFile, Bridge::getEnumValues, attrName -> {
            // get the styleable matching the resolved name
            RenderResources res = context.getRenderResources();
            ResourceValue attr = res.getProjectResource(ResourceType.ATTR, attrName);
            return attr instanceof AttrResourceValue ?
                    ((AttrResourceValue) attr).getAttributeValues() : null;
        });
    }

    /*
@@ -59,12 +83,8 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
        String ns = mParser.getAttributeNamespace(index);

        if (BridgeConstants.NS_RESOURCES.equals(ns)) {
            Integer v = Bridge.getResourceId(ResourceType.ATTR, name);
            if (v != null) {
                return v.intValue();
            }
            return Bridge.getResourceId(ResourceType.ATTR, name);

            return 0;
        }

        // this is not an attribute in the android namespace, we query the customviewloader, if
@@ -72,7 +92,7 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
        if (mContext.getLayoutlibCallback().getNamespace().equals(ns)) {
            Integer v = mContext.getLayoutlibCallback().getResourceId(ResourceType.ATTR, name);
            if (v != null) {
                return v.intValue();
                return v;
            }
        }

@@ -121,20 +141,38 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
    }

    @Override
    public int getAttributeIntValue(String namespace, String attribute,
            int defaultValue) {
    public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
        String value = getAttributeValue(namespace, attribute);
        if (value != null) {
        if (value == null) {
            return defaultValue;
        }

        ResourceValue r = getResourceValue(value);

        if (r != null) {
            value = r.getValue();
        }

        if (value.charAt(0) == '#') {
            return ResourceHelper.getColor(value);
        }

        try {
            return XmlUtils.convertValueToInt(value, defaultValue);
        } catch (NumberFormatException e) {
            // This is probably an enum
            Map<String, Integer> enumValues = BridgeConstants.NS_RESOURCES.equals(namespace) ?
                    mFrameworkEnumValueSupplier.apply(attribute) :
                    mProjectEnumValueSupplier.apply(attribute);

            Integer enumValue = enumValues != null ? enumValues.get(value) : null;
            if (enumValue != null) {
                return enumValue;
            }

        return defaultValue;
            // We weren't able to find the enum int value
            throw e;
        }
    }

    @Override
@@ -203,21 +241,9 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {

    @Override
    public int getAttributeIntValue(int index, int defaultValue) {
        String value = getAttributeValue(index);
        if (value != null) {
            ResourceValue r = getResourceValue(value);

            if (r != null) {
                value = r.getValue();
            }

            if (value.charAt(0) == '#') {
                return ResourceHelper.getColor(value);
            }
            return XmlUtils.convertValueToInt(value, defaultValue);
        }

        return defaultValue;
        return getAttributeIntValue(mParser.getAttributeNamespace(index),
                getAttributeName(index)
                , defaultValue);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ import static com.android.layoutlib.bridge.android.RenderParamsFlags.FLAG_KEY_AP
 * Custom implementation of Context/Activity to handle non compiled resources.
 */
@SuppressWarnings("deprecation")  // For use of Pair.
public final class BridgeContext extends Context {
public class BridgeContext extends Context {
    private static final String PREFIX_THEME_APPCOMPAT = "Theme.AppCompat";

    private static final Map<String, ResourceValue> FRAMEWORK_PATCHED_VALUES = new HashMap<>(2);
Loading