Search in sources :

Example 11 with BridgeXmlBlockParser

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

the class BridgePreferenceInflater method onCreateItem.

@Override
protected Preference onCreateItem(String name, AttributeSet attrs) throws ClassNotFoundException {
    Object viewKey = null;
    BridgeContext bc = null;
    Context context = getContext();
    if (context instanceof BridgeContext) {
        bc = (BridgeContext) context;
    }
    if (attrs instanceof BridgeXmlBlockParser) {
        viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
    }
    Preference preference = super.onCreateItem(name, attrs);
    if (viewKey != null && bc != null) {
        bc.addCookie(preference, viewKey);
    }
    return preference;
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 12 with BridgeXmlBlockParser

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

the class BridgeInflater method getViewKeyFromParser.

/*package*/
static Object getViewKeyFromParser(AttributeSet attrs, BridgeContext bc, ResourceReference resourceReference, boolean isInMerge) {
    if (!(attrs instanceof BridgeXmlBlockParser)) {
        return null;
    }
    BridgeXmlBlockParser parser = ((BridgeXmlBlockParser) attrs);
    // get the view key
    Object viewKey = parser.getViewCookie();
    if (viewKey == null) {
        int currentDepth = parser.getDepth();
        // test whether we are in an included file or in a adapter binding view.
        BridgeXmlBlockParser previousParser = bc.getPreviousParser();
        if (previousParser != null) {
            // looks like we are inside an embedded layout.
            // only apply the cookie of the calling node (<include>) if we are at the
            // top level of the embedded layout. If there is a merge tag, then
            // skip it and look for the 2nd level
            int testDepth = isInMerge ? 2 : 1;
            if (currentDepth == testDepth) {
                viewKey = previousParser.getViewCookie();
                // if we are in a merge, wrap the cookie in a MergeCookie.
                if (viewKey != null && isInMerge) {
                    viewKey = new MergeCookie(viewKey);
                }
            }
        } else if (resourceReference != null && currentDepth == 1) {
            // else if there's a resource reference, this means we are in an adapter
            // binding case. Set the resource ref as the view cookie only for the top
            // level view.
            viewKey = resourceReference;
        }
    }
    return viewKey;
}
Also used : MergeCookie(com.android.ide.common.rendering.api.MergeCookie) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 13 with BridgeXmlBlockParser

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

the class Resources_Delegate method getXml.

@LayoutlibDelegate
static XmlResourceParser getXml(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> value = getResourceValue(resources, id, mPlatformResourceFlag);
    if (value != null) {
        String v = value.getSecond().getValue();
        if (v != null) {
            // check this is a file
            File f = new File(v);
            if (f.isFile()) {
                try {
                    XmlPullParser parser = ParserFactory.create(f);
                    return new BridgeXmlBlockParser(parser, resources.mContext, mPlatformResourceFlag[0]);
                } catch (XmlPullParserException e) {
                    NotFoundException newE = new NotFoundException();
                    newE.initCause(e);
                    throw newE;
                } catch (FileNotFoundException e) {
                    NotFoundException newE = new NotFoundException();
                    newE.initCause(e);
                    throw newE;
                }
            }
        }
    }
    // 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) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 14 with BridgeXmlBlockParser

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

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
     * @param theme the theme to be used to inflate the drawable.
     */
public static Drawable getDrawable(ResourceValue value, BridgeContext context, Theme theme) {
    if (value == null) {
        return null;
    }
    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, theme);
                } 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) FileNotFoundException(java.io.FileNotFoundException) 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 15 with BridgeXmlBlockParser

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

the class ResourceHelper method getInternalComplexColor.

/**
     * Returns a {@link ComplexColor} from the given {@link ResourceValue}
     *
     * @param resValue the value containing a color value or a file path to a complex color
     * definition
     * @param context the current context
     * @param theme the theme to use when resolving the complex color
     * @param allowGradients when false, only {@link ColorStateList} will be returned. If a {@link
     * GradientColor} is found, null will be returned.
     */
@Nullable
private static ComplexColor getInternalComplexColor(@NonNull ResourceValue resValue, @NonNull BridgeContext context, @Nullable Theme theme, boolean allowGradients) {
    String value = resValue.getValue();
    if (value == null || RenderResources.REFERENCE_NULL.equals(value)) {
        return null;
    }
    XmlPullParser parser = null;
    // first check if the value is a file (xml most likely)
    Boolean psiParserSupport = context.getLayoutlibCallback().getFlag(RenderParamsFlags.FLAG_KEY_XML_FILE_PARSER_SUPPORT);
    if (psiParserSupport != null && psiParserSupport) {
        parser = context.getLayoutlibCallback().getXmlFileParser(value);
    }
    if (parser == null) {
        File f = new File(value);
        if (f.isFile()) {
            // providing an XmlPullParser
            try {
                parser = ParserFactory.create(f);
            } catch (XmlPullParserException | FileNotFoundException e) {
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + value, e, null);
            }
        }
    }
    if (parser != null) {
        try {
            BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, context, resValue.isFramework());
            try {
                // Advance the parser to the first element so we can detect if it's a
                // color list or a gradient color
                int type;
                //noinspection StatementWithEmptyBody
                while ((type = blockParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                // Seek parser to start tag.
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }
                final String name = blockParser.getName();
                if (allowGradients && "gradient".equals(name)) {
                    return ComplexColor_Accessor.createGradientColorFromXmlInner(context.getResources(), blockParser, blockParser, theme);
                } else if ("selector".equals(name)) {
                    return ComplexColor_Accessor.createColorStateListFromXmlInner(context.getResources(), blockParser, blockParser, theme);
                }
            } 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 = 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;
}
Also used : 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) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Nullable(android.annotation.Nullable)

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