Search in sources :

Example 41 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by ParanoidAndroid.

the class BridgeResources method getLayout.

@Override
public XmlResourceParser getLayout(int id) throws NotFoundException {
    Pair<String, ResourceValue> v = getResourceValue(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] == false) {
                parser = mProjectCallback.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);
                }
            }
            if (parser != null) {
                return new BridgeXmlBlockParser(parser, 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(id);
    // this is not used since the method above always throws
    return null;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) 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)

Example 42 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by ParanoidAndroid.

the class BridgeResources method getAnimation.

@Override
public XmlResourceParser getAnimation(int id) throws NotFoundException {
    Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag);
    if (v != null) {
        ResourceValue value = v.getSecond();
        XmlPullParser parser = null;
        try {
            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);
                return new BridgeXmlBlockParser(parser, 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(id);
    // this is not used since the method above always throws
    return null;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) 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)

Example 43 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by ParanoidAndroid.

the class BridgeTypedArray method getColorStateList.

/**
     * Retrieve the ColorStateList for the attribute at <var>index</var>.
     * The value may be either a single solid color or a reference to
     * a color or complex {@link android.content.res.ColorStateList} description.
     *
     * @param index Index of attribute to retrieve.
     *
     * @return ColorStateList for the attribute, or null if not defined.
     */
@Override
public ColorStateList getColorStateList(int index) {
    if (index < 0 || index >= mResourceData.length) {
        return null;
    }
    if (mResourceData[index] == null) {
        return null;
    }
    ResourceValue resValue = mResourceData[index];
    String value = resValue.getValue();
    if (value == null) {
        return null;
    }
    if (RenderResources.REFERENCE_NULL.equals(value)) {
        return null;
    }
    // let the framework inflate the ColorStateList from the XML file.
    File f = new File(value);
    if (f.isFile()) {
        try {
            XmlPullParser parser = ParserFactory.create(f);
            BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, mContext, resValue.isFramework());
            try {
                return ColorStateList.createFromXml(mContext.getResources(), blockParser);
            } finally {
                blockParser.ensurePopped();
            }
        } catch (XmlPullParserException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + value, e, null);
            return null;
        } catch (Exception e) {
            // this is an error and not warning since the file existence is checked before
            // attempting to parse it.
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + value, e, null);
            return null;
        }
    }
    try {
        int color = ResourceHelper.getColor(value);
        return ColorStateList.valueOf(color);
    } catch (NumberFormatException e) {
        Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, null);
    }
    return null;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 44 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by ParanoidAndroid.

the class ResourceHelper method getDrawable.

/**
     * Returns a drawable from the given value.
     * @param value The value that contains a path to a 9 patch, a bitmap or a xml based drawable,
     * or an hexadecimal color
     * @param context the current context
     */
public static Drawable getDrawable(ResourceValue value, BridgeContext context) {
    String stringValue = value.getValue();
    if (RenderResources.REFERENCE_NULL.equals(stringValue)) {
        return null;
    }
    String lowerCaseValue = stringValue.toLowerCase();
    Density density = Density.MEDIUM;
    if (value instanceof DensityBasedResourceValue) {
        density = ((DensityBasedResourceValue) value).getResourceDensity();
    }
    if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) {
        File file = new File(stringValue);
        if (file.isFile()) {
            try {
                return getNinePatchDrawable(new FileInputStream(file), density, value.isFramework(), stringValue, context);
            } catch (IOException e) {
                // failed to read the file, we'll return null below.
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed lot load " + file.getAbsolutePath(), e, null);
            }
        }
        return null;
    } else if (lowerCaseValue.endsWith(".xml")) {
        // create a block parser for the file
        File f = new File(stringValue);
        if (f.isFile()) {
            try {
                // let the framework inflate the Drawable from the XML file.
                XmlPullParser parser = ParserFactory.create(f);
                BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, context, value.isFramework());
                try {
                    return Drawable.createFromXml(context.getResources(), blockParser);
                } finally {
                    blockParser.ensurePopped();
                }
            } catch (Exception e) {
                // this is an error and not warning since the file existence is checked before
                // attempting to parse it.
                Bridge.getLog().error(null, "Failed to parse file " + stringValue, e, null);
            }
        } else {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("File %s does not exist (or is not a file)", stringValue), null);
        }
        return null;
    } else {
        File bmpFile = new File(stringValue);
        if (bmpFile.isFile()) {
            try {
                Bitmap bitmap = Bridge.getCachedBitmap(stringValue, value.isFramework() ? null : context.getProjectKey());
                if (bitmap == null) {
                    bitmap = Bitmap_Delegate.createBitmap(bmpFile, false, /*isMutable*/
                    density);
                    Bridge.setCachedBitmap(stringValue, bitmap, value.isFramework() ? null : context.getProjectKey());
                }
                return new BitmapDrawable(context.getResources(), bitmap);
            } catch (IOException e) {
                // we'll return null below
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed lot load " + bmpFile.getAbsolutePath(), e, null);
            }
        } else {
            // attempt to get a color from the value
            try {
                int color = getColor(stringValue);
                return new ColorDrawable(color);
            } catch (NumberFormatException e) {
                // we'll return null below.
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Failed to convert " + stringValue + " into a drawable", e, null);
            }
        }
    }
    return null;
}
Also used : DensityBasedResourceValue(com.android.ide.common.rendering.api.DensityBasedResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Density(com.android.resources.Density) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 45 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project android_frameworks_base by ParanoidAndroid.

the class ResourceHelper method getColorStateList.

public static ColorStateList getColorStateList(ResourceValue resValue, BridgeContext context) {
    String value = resValue.getValue();
    if (value != null && RenderResources.REFERENCE_NULL.equals(value) == false) {
        // first check if the value is a file (xml most likely)
        File f = new File(value);
        if (f.isFile()) {
            try {
                // let the framework inflate the ColorStateList from the XML file, by
                // providing an XmlPullParser
                XmlPullParser parser = ParserFactory.create(f);
                BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, context, resValue.isFramework());
                try {
                    return ColorStateList.createFromXml(context.getResources(), blockParser);
                } finally {
                    blockParser.ensurePopped();
                }
            } catch (XmlPullParserException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + value, e, null);
            // we'll return null below.
            } catch (Exception e) {
                // this is an error and not warning since the file existence is
                // checked before attempting to parse it.
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + value, e, null);
                return null;
            }
        } else {
            // try to load the color state list from an int
            try {
                int color = ResourceHelper.getColor(value);
                return ColorStateList.valueOf(color);
            } catch (NumberFormatException e) {
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, "Failed to convert " + value + " into a ColorStateList", e, null);
                return null;
            }
        }
    }
    return null;
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)67 File (java.io.File)39 XmlPullParser (org.xmlpull.v1.XmlPullParser)39 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)38 FileNotFoundException (java.io.FileNotFoundException)30 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)24 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)22 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)18 IOException (java.io.IOException)17 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)16 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)12 Result (com.android.ide.common.rendering.api.Result)12 MalformedURLException (java.net.MalformedURLException)12 Context (android.content.Context)11 View (android.view.View)11 NotFoundException (android.content.res.Resources.NotFoundException)8 AnimationThread (android.animation.AnimationThread)6 LayoutTransition (android.animation.LayoutTransition)6 Bitmap (android.graphics.Bitmap)6 BitmapDrawable (android.graphics.drawable.BitmapDrawable)6