Search in sources :

Example 36 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.

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

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.

the class BridgeInflater method inflate.

@Override
public View inflate(int resource, ViewGroup root) {
    Context context = getContext();
    context = getBaseContext(context);
    if (context instanceof BridgeContext) {
        BridgeContext bridgeContext = (BridgeContext) context;
        ResourceValue value = null;
        @SuppressWarnings("deprecation") Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
        if (layoutInfo != null) {
            value = bridgeContext.getRenderResources().getFrameworkResource(ResourceType.LAYOUT, layoutInfo.getSecond());
        } else {
            layoutInfo = mLayoutlibCallback.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, true);
                    BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser(parser, bridgeContext, value.isFramework());
                    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) BridgeContext.getBaseContext(com.android.layoutlib.bridge.android.BridgeContext.getBaseContext) 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)

Example 38 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.

the class StatusBar method loadIcon.

@Override
protected void loadIcon(int index, String iconName, Density density) {
    if (!iconName.endsWith(".xml")) {
        super.loadIcon(index, iconName, density);
        return;
    }
    View child = getChildAt(index);
    if (child instanceof ImageView) {
        ImageView imageView = (ImageView) child;
        // The xml is stored only in xhdpi.
        IconLoader iconLoader = new IconLoader(iconName, Density.XHIGH, mSimulatedPlatformVersion, null);
        InputStream stream = iconLoader.getIcon();
        if (stream != null) {
            try {
                BridgeXmlBlockParser parser = new BridgeXmlBlockParser(ParserFactory.create(stream, null), (BridgeContext) mContext, true);
                imageView.setImageDrawable(Drawable.createFromXml(mContext.getResources(), parser));
            } catch (XmlPullParserException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
            } catch (IOException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to draw wifi icon", e, null);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ImageView(android.widget.ImageView) IOException(java.io.IOException) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser)

Example 39 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.

the class RenderSessionImpl method insertChild.

/**
     * Insert a new child into an existing parent.
     * <p>
     * {@link #acquire(long)} must have been called before this.
     *
     * @throws IllegalStateException if the current context is different than the one owned by
     *      the scene, or if {@link #acquire(long)} was not called.
     *
     * @see RenderSession#insertChild(Object, ILayoutPullParser, int, IAnimationListener)
     */
public Result insertChild(final ViewGroup parentView, ILayoutPullParser childXml, final int index, IAnimationListener listener) {
    checkLock();
    BridgeContext context = getContext();
    // create a block parser for the XML
    BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(childXml, context, false);
    // inflate the child without adding it to the root since we want to control where it'll
    // get added. We do pass the parentView however to ensure that the layoutParams will
    // be created correctly.
    final View child = mInflater.inflate(blockParser, parentView, false);
    blockParser.ensurePopped();
    invalidateRenderingSize();
    if (listener != null) {
        new AnimationThread(this, "insertChild", listener) {

            @Override
            public Result preAnimation() {
                parentView.setLayoutTransition(new LayoutTransition());
                return addView(parentView, child, index);
            }

            @Override
            public void postAnimation() {
                parentView.setLayoutTransition(null);
            }
        }.start();
        // always return success since the real status will come through the listener.
        return SUCCESS.createResult(child);
    }
    // add it to the parentView in the correct location
    Result result = addView(parentView, child, index);
    if (!result.isSuccess()) {
        return result;
    }
    result = render(false);
    if (result.isSuccess()) {
        result = result.getCopyWithData(child);
    }
    return result;
}
Also used : AnimationThread(android.animation.AnimationThread) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) LayoutTransition(android.animation.LayoutTransition) MenuView(com.android.internal.view.menu.MenuView) View(android.view.View) AdapterView(android.widget.AdapterView) ActionMenuItemView(com.android.internal.view.menu.ActionMenuItemView) IconMenuItemView(com.android.internal.view.menu.IconMenuItemView) ListView(android.widget.ListView) ListMenuItemView(com.android.internal.view.menu.ListMenuItemView) AbsListView(android.widget.AbsListView) ActionMenuView(android.widget.ActionMenuView) ExpandableListView(android.widget.ExpandableListView) BridgeXmlBlockParser(com.android.layoutlib.bridge.android.BridgeXmlBlockParser) Result(com.android.ide.common.rendering.api.Result)

Example 40 with BridgeXmlBlockParser

use of com.android.layoutlib.bridge.android.BridgeXmlBlockParser in project platform_frameworks_base by android.

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