Search in sources :

Example 21 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method getBoolean.

@LayoutlibDelegate
static boolean getBoolean(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
    if (value != null) {
        ResourceValue resValue = value.getSecond();
        if (resValue != null) {
            String v = resValue.getValue();
            if (v != null) {
                return Boolean.parseBoolean(v);
            }
        }
    }
    // id was not found or not resolved. Throw a NotFoundException.
    throwException(resources, id);
    // this is not used since the method above always throws
    return false;
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 22 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method getLayout.

@LayoutlibDelegate
static XmlResourceParser getLayout(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> v = getResourceValue(resources, id, mPlatformResourceFlag);
    if (v != null) {
        ResourceValue value = v.getSecond();
        XmlPullParser parser = null;
        try {
            // check if the current parser can provide us with a custom parser.
            if (!mPlatformResourceFlag[0]) {
                parser = resources.mLayoutlibCallback.getParser(value);
            }
            // create a new one manually if needed.
            if (parser == null) {
                File xml = new File(value.getValue());
                if (xml.isFile()) {
                    // we need to create a pull parser around the layout XML file, and then
                    // give that to our XmlBlockParser
                    parser = ParserFactory.create(xml, true);
                }
            }
            if (parser != null) {
                return new BridgeXmlBlockParser(parser, resources.mContext, mPlatformResourceFlag[0]);
            }
        } catch (XmlPullParserException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + value.getValue(), e, null);
        // we'll return null below.
        } catch (FileNotFoundException e) {
        // this shouldn't happen since we check above.
        }
    }
    // id was not found or not resolved. Throw a NotFoundException.
    throwException(resources, id);
    // this is not used since the method above always throws
    return null;
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 23 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method getStringArray.

@LayoutlibDelegate
static String[] getStringArray(Resources resources, int id) throws NotFoundException {
    ResourceValue resValue = getArrayResourceValue(resources, id);
    if (resValue == null) {
        // Error already logged by getArrayResourceValue.
        return new String[0];
    } else if (!(resValue instanceof ArrayResourceValue)) {
        return new String[] { resolveReference(resources, resValue.getValue(), resValue.isFramework()) };
    }
    ArrayResourceValue arv = ((ArrayResourceValue) resValue);
    return fillValues(resources, arv, new String[arv.getElementCount()]);
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 24 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method getDimension.

@LayoutlibDelegate
static float getDimension(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
    if (value != null) {
        ResourceValue resValue = value.getSecond();
        assert resValue != null;
        if (resValue != null) {
            String v = resValue.getValue();
            if (v != null) {
                if (v.equals(BridgeConstants.MATCH_PARENT) || v.equals(BridgeConstants.FILL_PARENT)) {
                    return LayoutParams.MATCH_PARENT;
                } else if (v.equals(BridgeConstants.WRAP_CONTENT)) {
                    return LayoutParams.WRAP_CONTENT;
                }
                TypedValue tmpValue = new TypedValue();
                if (ResourceHelper.parseFloatAttribute(value.getFirst(), v, tmpValue, true) && tmpValue.type == TypedValue.TYPE_DIMENSION) {
                    return tmpValue.getDimension(resources.getDisplayMetrics());
                }
            }
        }
    }
    // id was not found or not resolved. Throw a NotFoundException.
    throwException(resources, id);
    // this is not used since the method above always throws
    return 0;
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) TypedValue(android.util.TypedValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 25 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ResurrectionRemix.

the class Resources_Delegate method getIntArray.

@LayoutlibDelegate
static int[] getIntArray(Resources resources, int id) throws NotFoundException {
    ResourceValue rv = getArrayResourceValue(resources, id);
    if (rv == null) {
        // Error already logged by getArrayResourceValue.
        return new int[0];
    } else if (!(rv instanceof ArrayResourceValue)) {
        // This is an older IDE that can only give us the first element of the array.
        String firstValue = resolveReference(resources, rv.getValue(), rv.isFramework());
        try {
            return new int[] { getInt(firstValue) };
        } catch (NumberFormatException e) {
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Integer resource array contains non-integer value: " + firstValue, null);
            return new int[1];
        }
    }
    ArrayResourceValue resValue = ((ArrayResourceValue) rv);
    int[] values = new int[resValue.getElementCount()];
    int i = 0;
    for (Iterator<String> iterator = resValue.iterator(); iterator.hasNext(); i++) {
        String element = resolveReference(resources, iterator.next(), resValue.isFramework());
        try {
            values[i] = getInt(element);
        } catch (NumberFormatException e) {
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Integer resource array contains non-integer value: " + element, null);
        }
    }
    return values;
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

ResourceValue (com.android.ide.common.rendering.api.ResourceValue)243 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)82 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)80 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)69 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)56 RenderResources (com.android.ide.common.rendering.api.RenderResources)48 File (java.io.File)44 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)40 ResourceType (com.android.resources.ResourceType)31 XmlPullParser (org.xmlpull.v1.XmlPullParser)28 TypedValue (android.util.TypedValue)26 FileNotFoundException (java.io.FileNotFoundException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)23 AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)22 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)22 Context (android.content.Context)16 NotNull (org.jetbrains.annotations.NotNull)14 ResourceResolver (com.android.ide.common.resources.ResourceResolver)13 BridgeTypedArray (android.content.res.BridgeTypedArray)12 NotFoundException (android.content.res.Resources.NotFoundException)12