Search in sources :

Example 11 with ArrayResourceValue

use of com.android.ide.common.rendering.api.ArrayResourceValue in project android_frameworks_base by crdroidandroid.

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)

Example 12 with ArrayResourceValue

use of com.android.ide.common.rendering.api.ArrayResourceValue in project platform_frameworks_base by android.

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)

Example 13 with ArrayResourceValue

use of com.android.ide.common.rendering.api.ArrayResourceValue in project platform_frameworks_base by android.

the class Resources_Delegate method getTextArray.

@LayoutlibDelegate
static CharSequence[] getTextArray(Resources resources, int id) throws NotFoundException {
    ResourceValue resValue = getArrayResourceValue(resources, id);
    if (resValue == null) {
        // Error already logged by getArrayResourceValue.
        return new CharSequence[0];
    } else if (!(resValue instanceof ArrayResourceValue)) {
        return new CharSequence[] { resolveReference(resources, resValue.getValue(), resValue.isFramework()) };
    }
    ArrayResourceValue arv = ((ArrayResourceValue) resValue);
    return fillValues(resources, arv, new CharSequence[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 14 with ArrayResourceValue

use of com.android.ide.common.rendering.api.ArrayResourceValue in project android_frameworks_base by DirtyUnicorns.

the class BridgeTypedArray method getTextArray.

/**
     * Retrieve the CharSequence[] for the attribute at <var>index</var>.
     * This gets the resource ID of the selected attribute, and uses
     * {@link Resources#getTextArray Resources.getTextArray} of the owning
     * Resources object to retrieve its String[].
     *
     * @param index Index of attribute to retrieve.
     *
     * @return CharSequence[] for the attribute, or null if not defined.
     */
@Override
public CharSequence[] getTextArray(int index) {
    if (!hasValue(index)) {
        return null;
    }
    ResourceValue resVal = mResourceData[index];
    if (resVal instanceof ArrayResourceValue) {
        ArrayResourceValue array = (ArrayResourceValue) resVal;
        int count = array.getElementCount();
        return count >= 0 ? Resources_Delegate.fillValues(mBridgeResources, array, new CharSequence[count]) : null;
    }
    int id = getResourceId(index, 0);
    String resIdMessage = id > 0 ? " (resource id 0x" + Integer.toHexString(id) + ')' : "";
    throw new NotFoundException(String.format("%1$s in %2$s%3$s is not a valid array resource.", resVal.getValue(), mNames[index], resIdMessage));
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) NotFoundException(android.content.res.Resources.NotFoundException) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue)

Example 15 with ArrayResourceValue

use of com.android.ide.common.rendering.api.ArrayResourceValue in project android_frameworks_base by DirtyUnicorns.

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

ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)20 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)20 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)16 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)12 Nullable (android.annotation.Nullable)4 NotFoundException (android.content.res.Resources.NotFoundException)4 AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)4 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)4 ResourceType (com.android.resources.ResourceType)4