Search in sources :

Example 46 with BridgeXmlBlockParser

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

the class BridgeInflater method setupViewInContext.

private void setupViewInContext(View view, AttributeSet attrs) {
    if (getContext() instanceof BridgeContext) {
        BridgeContext bc = (BridgeContext) getContext();
        if (attrs instanceof BridgeXmlBlockParser) {
            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 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 = mIsInMerge ? 2 : 1;
                    if (currentDepth == testDepth) {
                        viewKey = previousParser.getViewCookie();
                        // if we are in a merge, wrap the cookie in a MergeCookie.
                        if (viewKey != null && mIsInMerge) {
                            viewKey = new MergeCookie(viewKey);
                        }
                    }
                } else if (mResourceReference != 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 = mResourceReference;
                }
            }
            if (viewKey != null) {
                bc.addViewKey(view, viewKey);
            }
        }
    }
}
Also used : BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) MergeCookie(com.android.ide.common.rendering.api.MergeCookie) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 47 with BridgeXmlBlockParser

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

the class BridgeInflater method inflate.

@Override
public View inflate(int resource, ViewGroup root) {
    Context context = getContext();
    if (context instanceof BridgeContext) {
        BridgeContext bridgeContext = (BridgeContext) context;
        ResourceValue value = null;
        Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
        if (layoutInfo != null) {
            value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
        } else {
            layoutInfo = mProjectCallback.resolveResourceId(resource);
            if (layoutInfo != null) {
                value = bridgeContext.getRenderResources().getProjectResource(ResourceType.LAYOUT, layoutInfo.getSecond());
            }
        }
        if (value != null) {
            File f = new File(value.getValue());
            if (f.isFile()) {
                try {
                    XmlPullParser parser = ParserFactory.create(f);
                    BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, false);
                    return inflate(bridgeParser, root);
                } catch (Exception e) {
                    Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, "Failed to parse file " + f.getAbsolutePath(), e, null);
                    return null;
                }
            }
        }
    }
    return null;
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) XmlPullParser(org.xmlpull.v1.XmlPullParser) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) File(java.io.File) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) InflateException(android.view.InflateException)

Example 48 with BridgeXmlBlockParser

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

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 49 with BridgeXmlBlockParser

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

the class Resources_Delegate method loadXmlResourceParser.

@LayoutlibDelegate
static XmlResourceParser loadXmlResourceParser(Resources resources, String file, int id, int assetCookie, String type) throws NotFoundException {
    // even though we know the XML file to load directly, we still need to resolve the
    // id so that we can know if it's a platform or project resource.
    // (mPlatformResouceFlag will get the result and will be used later).
    getResourceValue(resources, id, mPlatformResourceFlag);
    File f = new File(file);
    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;
    }
}
Also used : 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 50 with BridgeXmlBlockParser

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

the class Resources_Delegate method getAnimation.

@LayoutlibDelegate
static XmlResourceParser getAnimation(Resources resources, int id) throws NotFoundException {
    Pair<String, ResourceValue> v = getResourceValue(resources, id, mPlatformResourceFlag);
    if (v != null) {
        ResourceValue value = v.getSecond();
        XmlPullParser parser;
        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, 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)

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