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

Commit c3f979f6 authored by Xavier Ducrohet's avatar Xavier Ducrohet
Browse files

Make sure resource references are resolved.

XmlPullAttribute can query for attributes and return them in a given
format. We need to make sure they are first resolved before
trying to convert them to int/float/boolean/...

Change-Id: I2aaced022a0382e501978c396e49d6191d53bdc8
parent 2d559b53
Loading
Loading
Loading
Loading
+173 −18
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.util;

import com.android.ide.common.rendering.api.RenderResources;
import com.android.ide.common.rendering.api.ResourceValue;
import com.android.internal.util.XmlUtils;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.BridgeConstants;
import com.android.layoutlib.bridge.android.BridgeContext;
@@ -25,9 +26,6 @@ import com.android.resources.ResourceType;

import org.xmlpull.v1.XmlPullParser;

import android.util.AttributeSet;
import android.util.XmlPullAttributes;

/**
 * A correct implementation of the {@link AttributeSet} interface on top of a XmlPullParser
 */
@@ -80,21 +78,40 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see android.util.XmlPullAttributes#getAttributeResourceValue(int, int)
     */
    @Override
    public int getAttributeResourceValue(int index, int defaultValue) {
        String value = getAttributeValue(index);
    public int getAttributeListValue(String namespace, String attribute,
            String[] options, int defaultValue) {
        String value = getAttributeValue(namespace, attribute);
        if (value != null) {
            ResourceValue r = getResourceValue(value);

        return resolveResourceValue(value, defaultValue);
            if (r != null) {
                value = r.getValue();
            }

            return XmlUtils.convertValueToList(value, options, defaultValue);
        }

        return defaultValue;
    }

    @Override
    public boolean getAttributeBooleanValue(String namespace, String attribute,
            boolean defaultValue) {
        String value = getAttributeValue(namespace, attribute);
        if (value != null) {
            ResourceValue r = getResourceValue(value);

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

            return XmlUtils.convertValueToBoolean(value, defaultValue);
        }

        return defaultValue;
    }

    /*
     * (non-Javadoc)
     * @see android.util.XmlPullAttributes#getAttributeResourceValue(java.lang.String, java.lang.String, int)
     */
    @Override
    public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
        String value = getAttributeValue(namespace, attribute);
@@ -102,12 +119,151 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
        return resolveResourceValue(value, defaultValue);
    }

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

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

            return XmlUtils.convertValueToInt(value, defaultValue);
        }

        return defaultValue;
    }

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

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

            return XmlUtils.convertValueToUnsignedInt(value, defaultValue);
        }

        return defaultValue;
    }

    @Override
    public float getAttributeFloatValue(String namespace, String attribute,
            float defaultValue) {
        String s = getAttributeValue(namespace, attribute);
        if (s != null) {
            ResourceValue r = getResourceValue(s);

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

            return Float.parseFloat(s);
        }

        return defaultValue;
    }

    @Override
    public int getAttributeListValue(int index,
            String[] options, int defaultValue) {
        return XmlUtils.convertValueToList(
            getAttributeValue(index), options, defaultValue);
    }

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

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

            return XmlUtils.convertValueToBoolean(value, defaultValue);
        }

        return defaultValue;
    }

    @Override
    public int getAttributeResourceValue(int index, int defaultValue) {
        String value = getAttributeValue(index);

        return resolveResourceValue(value, defaultValue);
    }

    @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();
            }

            return XmlUtils.convertValueToInt(value, defaultValue);
        }

        return defaultValue;
    }

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

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

            return XmlUtils.convertValueToUnsignedInt(value, defaultValue);
        }

        return defaultValue;
    }

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

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

            return Float.parseFloat(s);
        }

        return defaultValue;
    }

    // -- private helper methods

    /**
     * Returns a resolved {@link ResourceValue} from a given value.
     */
    private ResourceValue getResourceValue(String value) {
        // now look for this particular value
        RenderResources resources = mContext.getRenderResources();
        ResourceValue resource = resources.resolveResValue(
                resources.findResValue(value, mPlatformFile));
        return resources.resolveResValue(resources.findResValue(value, mPlatformFile));
    }

    /**
     * Resolves and return a value to its associated integer.
     */
    private int resolveResourceValue(String value, int defaultValue) {
        ResourceValue resource = getResourceValue(value);
        if (resource != null) {
            Integer id = null;
            if (mPlatformFile || resource.isFramework()) {
@@ -124,5 +280,4 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {

        return defaultValue;
    }

}
+3 −1
Original line number Diff line number Diff line
@@ -24,8 +24,8 @@ import com.android.ide.common.rendering.api.DrawableParams;
import com.android.ide.common.rendering.api.LayoutLog;
import com.android.ide.common.rendering.api.RenderSession;
import com.android.ide.common.rendering.api.Result;
import com.android.ide.common.rendering.api.SessionParams;
import com.android.ide.common.rendering.api.Result.Status;
import com.android.ide.common.rendering.api.SessionParams;
import com.android.layoutlib.bridge.impl.FontLoader;
import com.android.layoutlib.bridge.impl.RenderDrawable;
import com.android.layoutlib.bridge.impl.RenderSessionImpl;
@@ -242,6 +242,8 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
        if (fontLoader != null) {
            Typeface_Delegate.init(fontLoader);
        } else {
            log.error(LayoutLog.TAG_BROKEN,
                    "Failed create FontLoader in layout lib.", null);
            return false;
        }