Search in sources :

Example 96 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android by JetBrains.

the class NlDefaultRenderer method getColorIcon.

@Nullable
private static Icon getColorIcon(@NotNull ResourceResolver resolver, @NotNull NlProperty property, @NotNull String value) {
    boolean isFrameworkRes = value.startsWith(SdkConstants.ANDROID_COLOR_RESOURCE_PREFIX);
    ResourceValue resourceValue = resolver.resolveValue(ResourceType.COLOR, property.getName(), value, isFrameworkRes);
    if (resourceValue == null) {
        return null;
    }
    String resolvedValue = resourceValue.getValue();
    if (isColorValue(resolvedValue)) {
        return getColorIcon(resolvedValue);
    }
    return null;
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Nullable(org.jetbrains.annotations.Nullable)

Example 97 with ResourceValue

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

the class BridgeContext method obtainStyledAttributes.

@Override
public BridgeTypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) {
    PropertiesMap defaultPropMap = null;
    boolean isPlatformFile = true;
    // Hint: for XmlPullParser, attach source //DEVICE_SRC/dalvik/libcore/xml/src/java
    if (set instanceof BridgeXmlBlockParser) {
        BridgeXmlBlockParser parser;
        parser = (BridgeXmlBlockParser) set;
        isPlatformFile = parser.isPlatformFile();
        Object key = parser.getViewCookie();
        if (key != null) {
            defaultPropMap = mDefaultPropMaps.get(key);
            if (defaultPropMap == null) {
                defaultPropMap = new PropertiesMap();
                mDefaultPropMaps.put(key, defaultPropMap);
            }
        }
    } else if (set instanceof BridgeLayoutParamsMapAttributes) {
        // this is only for temp layout params generated dynamically, so this is never
        // platform content.
        isPlatformFile = false;
    } else if (set != null) {
        // null parser is ok
        // really this should not be happening since its instantiated in Bridge
        Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Parser is not a BridgeXmlBlockParser!", null);
        return null;
    }
    List<Pair<String, Boolean>> attributeList = searchAttrs(attrs);
    BridgeTypedArray ta = Resources_Delegate.newTypeArray(mSystemResources, attrs.length, isPlatformFile);
    // look for a custom style.
    String customStyle = null;
    if (set != null) {
        customStyle = set.getAttributeValue(null, "style");
    }
    StyleResourceValue customStyleValues = null;
    if (customStyle != null) {
        ResourceValue item = mRenderResources.findResValue(customStyle, isPlatformFile);
        // resolve it in case it links to something else
        item = mRenderResources.resolveResValue(item);
        if (item instanceof StyleResourceValue) {
            customStyleValues = (StyleResourceValue) item;
        }
    }
    // resolve the defStyleAttr value into a IStyleResourceValue
    StyleResourceValue defStyleValues = null;
    if (defStyleAttr != 0) {
        // get the name from the int.
        Pair<String, Boolean> defStyleAttribute = searchAttr(defStyleAttr);
        if (defStyleAttribute == null) {
            // This should be rare. Happens trying to map R.style.foo to @style/foo fails.
            // This will happen if the user explicitly used a non existing int value for
            // defStyleAttr or there's something wrong with the project structure/build.
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_RESOLVE, "Failed to find the style corresponding to the id " + defStyleAttr, null);
        } else {
            String defStyleName = defStyleAttribute.getFirst();
            // look for the style in the current theme, and its parent:
            ResourceValue item = mRenderResources.findItemInTheme(defStyleName, defStyleAttribute.getSecond());
            if (item != null) {
                // item is a reference to a style entry. Search for it.
                item = mRenderResources.findResValue(item.getValue(), item.isFramework());
                item = mRenderResources.resolveResValue(item);
                if (item instanceof StyleResourceValue) {
                    defStyleValues = (StyleResourceValue) item;
                }
                if (defaultPropMap != null) {
                    if (defStyleAttribute.getSecond()) {
                        defStyleName = "android:" + defStyleName;
                    }
                    defaultPropMap.put("style", new Property(defStyleName, item.getValue()));
                }
            } else {
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_RESOLVE_THEME_ATTR, String.format("Failed to find style '%s' in current theme", defStyleAttribute.getFirst()), null);
            }
        }
    } else if (defStyleRes != 0) {
        StyleResourceValue item = getStyleByDynamicId(defStyleRes);
        if (item != null) {
            defStyleValues = item;
        } else {
            boolean isFrameworkRes = true;
            Pair<ResourceType, String> value = Bridge.resolveResourceId(defStyleRes);
            if (value == null) {
                value = mLayoutlibCallback.resolveResourceId(defStyleRes);
                isFrameworkRes = false;
            }
            if (value != null) {
                if ((value.getFirst() == ResourceType.STYLE)) {
                    // look for the style in all resources:
                    item = mRenderResources.getStyle(value.getSecond(), isFrameworkRes);
                    if (item != null) {
                        if (defaultPropMap != null) {
                            String name = item.getName();
                            defaultPropMap.put("style", new Property(name, name));
                        }
                        defStyleValues = item;
                    } else {
                        Bridge.getLog().error(null, String.format("Style with id 0x%x (resolved to '%s') does not exist.", defStyleRes, value.getSecond()), null);
                    }
                } else {
                    Bridge.getLog().error(null, String.format("Resource id 0x%x is not of type STYLE (instead %s)", defStyleRes, value.getFirst().toString()), null);
                }
            } else {
                Bridge.getLog().error(null, String.format("Failed to find style with id 0x%x in current theme", defStyleRes), null);
            }
        }
    }
    String appNamespace = mLayoutlibCallback.getNamespace();
    if (attributeList != null) {
        for (int index = 0; index < attributeList.size(); index++) {
            Pair<String, Boolean> attribute = attributeList.get(index);
            if (attribute == null) {
                continue;
            }
            String attrName = attribute.getFirst();
            boolean frameworkAttr = attribute.getSecond();
            String value = null;
            if (set != null) {
                value = set.getAttributeValue(frameworkAttr ? BridgeConstants.NS_RESOURCES : appNamespace, attrName);
                // new res-auto namespace as well
                if (!frameworkAttr && value == null) {
                    value = set.getAttributeValue(BridgeConstants.NS_APP_RES_AUTO, attrName);
                }
            }
            // values in the widget defStyle, and then in the theme.
            if (value == null) {
                ResourceValue resValue = null;
                // look for the value in the custom style first (and its parent if needed)
                if (customStyleValues != null) {
                    resValue = mRenderResources.findItemInStyle(customStyleValues, attrName, frameworkAttr);
                }
                // then look for the value in the default Style (and its parent if needed)
                if (resValue == null && defStyleValues != null) {
                    resValue = mRenderResources.findItemInStyle(defStyleValues, attrName, frameworkAttr);
                }
                // its parent themes)
                if (resValue == null) {
                    resValue = mRenderResources.findItemInTheme(attrName, frameworkAttr);
                }
                // So we resolve it.
                if (resValue != null) {
                    String preResolve = resValue.getValue();
                    resValue = mRenderResources.resolveResValue(resValue);
                    if (defaultPropMap != null) {
                        defaultPropMap.put(frameworkAttr ? SdkConstants.PREFIX_ANDROID + attrName : attrName, new Property(preResolve, resValue.getValue()));
                    }
                    // If the value is a reference to another theme attribute that doesn't
                    // exist, we should log a warning and omit it.
                    String val = resValue.getValue();
                    if (val != null && val.startsWith(SdkConstants.PREFIX_THEME_REF)) {
                        if (!attrName.equals(RTL_ATTRS.get(val)) || getApplicationInfo().targetSdkVersion < VERSION_CODES.JELLY_BEAN_MR1) {
                            // Only log a warning if the referenced value isn't one of the RTL
                            // attributes, or the app targets old API.
                            Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_RESOLVE_THEME_ATTR, String.format("Failed to find '%s' in current theme.", val), val);
                        }
                        resValue = null;
                    }
                }
                ta.bridgeSetValue(index, attrName, frameworkAttr, resValue);
            } else {
                // there is a value in the XML, but we need to resolve it in case it's
                // referencing another resource or a theme value.
                ta.bridgeSetValue(index, attrName, frameworkAttr, mRenderResources.resolveValue(null, attrName, value, isPlatformFile));
            }
        }
    }
    ta.sealArray();
    return ta;
}
Also used : PropertiesMap(com.android.util.PropertiesMap) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeTypedArray(android.content.res.BridgeTypedArray) Property(com.android.util.PropertiesMap.Property) Pair(com.android.util.Pair)

Example 98 with ResourceValue

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

the class BridgeContext method inflateView.

public Pair<View, Boolean> inflateView(ResourceReference resource, ViewGroup parent, boolean attachToRoot, boolean skipCallbackParser) {
    boolean isPlatformLayout = resource.isFramework();
    if (!isPlatformLayout && !skipCallbackParser) {
        // check if the project callback can provide us with a custom parser.
        ILayoutPullParser parser = getParser(resource);
        if (parser != null) {
            BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, this, resource.isFramework());
            try {
                pushParser(blockParser);
                return Pair.of(mBridgeInflater.inflate(blockParser, parent, attachToRoot), Boolean.TRUE);
            } finally {
                popParser();
            }
        }
    }
    ResourceValue resValue;
    if (resource instanceof ResourceValue) {
        resValue = (ResourceValue) resource;
    } else {
        if (isPlatformLayout) {
            resValue = mRenderResources.getFrameworkResource(ResourceType.LAYOUT, resource.getName());
        } else {
            resValue = mRenderResources.getProjectResource(ResourceType.LAYOUT, resource.getName());
        }
    }
    if (resValue != null) {
        File xml = new File(resValue.getValue());
        if (xml.isFile()) {
            // give that to our XmlBlockParser
            try {
                XmlPullParser parser = ParserFactory.create(xml, true);
                // set the resource ref to have correct view cookies
                mBridgeInflater.setResourceReference(resource);
                BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(parser, this, resource.isFramework());
                try {
                    pushParser(blockParser);
                    return Pair.of(mBridgeInflater.inflate(blockParser, parent, attachToRoot), Boolean.FALSE);
                } finally {
                    popParser();
                }
            } catch (XmlPullParserException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to configure parser for " + xml, e, null);
            // we'll return null below.
            } catch (FileNotFoundException e) {
            // this shouldn't happen since we check above.
            } finally {
                mBridgeInflater.setResourceReference(null);
            }
        } else {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("File %s is missing!", xml), null);
        }
    } else {
        Bridge.getLog().error(LayoutLog.TAG_BROKEN, String.format("Layout %s%s does not exist.", isPlatformLayout ? "android:" : "", resource.getName()), null);
    }
    return Pair.of(null, Boolean.FALSE);
}
Also used : ILayoutPullParser(com.android.ide.common.rendering.api.ILayoutPullParser) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) 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)

Example 99 with ResourceValue

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

the class CustomBar method getColor.

private static int getColor(RenderResources renderResources, String attr) {
    // From ?attr/foo to @color/bar. This is most likely an ItemResourceValue.
    ResourceValue resource = renderResources.findItemInTheme(attr, true);
    // Form @color/bar to the #AARRGGBB
    resource = renderResources.resolveResValue(resource);
    if (resource != null) {
        ResourceType type = resource.getResourceType();
        if (type == null || type == ResourceType.COLOR) {
            // file, rather than referencing a color resource value.
            try {
                return ResourceHelper.getColor(resource.getValue());
            } catch (NumberFormatException e) {
                // Conversion failed.
                Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, "Theme attribute @android:" + attr + " does not reference a color, instead is '" + resource.getValue() + "'.", resource);
            }
        }
    }
    return 0;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 100 with ResourceValue

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

the class RenderSessionImpl method animate.

/**
     * Animate an object
     * <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#animate(Object, String, boolean, IAnimationListener)
     */
public Result animate(Object targetObject, String animationName, boolean isFrameworkAnimation, IAnimationListener listener) {
    checkLock();
    BridgeContext context = getContext();
    // find the animation file.
    ResourceValue animationResource;
    int animationId = 0;
    if (isFrameworkAnimation) {
        animationResource = context.getRenderResources().getFrameworkResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = Bridge.getResourceId(ResourceType.ANIMATOR, animationName);
        }
    } else {
        animationResource = context.getRenderResources().getProjectResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = context.getLayoutlibCallback().getResourceId(ResourceType.ANIMATOR, animationName);
        }
    }
    if (animationResource != null) {
        try {
            Animator anim = AnimatorInflater.loadAnimator(context, animationId);
            if (anim != null) {
                anim.setTarget(targetObject);
                new PlayAnimationThread(anim, this, animationName, listener).start();
                return SUCCESS.createResult();
            }
        } catch (Exception e) {
            // get the real cause of the exception.
            Throwable t = e;
            while (t.getCause() != null) {
                t = t.getCause();
            }
            return ERROR_UNKNOWN.createResult(t.getMessage(), t);
        }
    }
    return ERROR_ANIM_NOT_FOUND.createResult();
}
Also used : Animator(android.animation.Animator) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext)

Aggregations

ResourceValue (com.android.ide.common.rendering.api.ResourceValue)243 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)82 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)80 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)69 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)56 RenderResources (com.android.ide.common.rendering.api.RenderResources)48 File (java.io.File)44 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)40 ResourceType (com.android.resources.ResourceType)31 XmlPullParser (org.xmlpull.v1.XmlPullParser)28 TypedValue (android.util.TypedValue)26 FileNotFoundException (java.io.FileNotFoundException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)23 AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)22 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)22 Context (android.content.Context)16 NotNull (org.jetbrains.annotations.NotNull)14 ResourceResolver (com.android.ide.common.resources.ResourceResolver)13 BridgeTypedArray (android.content.res.BridgeTypedArray)12 NotFoundException (android.content.res.Resources.NotFoundException)12