Search in sources :

Example 66 with ResourceValue

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

the class BridgeContext method resolveThemeAttribute.

public boolean resolveThemeAttribute(int resId, TypedValue outValue, boolean resolveRefs) {
    Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(resId);
    boolean isFrameworkRes = true;
    if (resourceInfo == null) {
        resourceInfo = mLayoutlibCallback.resolveResourceId(resId);
        isFrameworkRes = false;
    }
    if (resourceInfo == null) {
        return false;
    }
    ResourceValue value = mRenderResources.findItemInTheme(resourceInfo.getSecond(), isFrameworkRes);
    if (resolveRefs) {
        value = mRenderResources.resolveResValue(value);
    }
    if (value == null) {
        // unable to find the attribute.
        return false;
    }
    // check if this is a style resource
    if (value instanceof StyleResourceValue) {
        // get the id that will represent this style.
        outValue.resourceId = getDynamicIdByStyle((StyleResourceValue) value);
        return true;
    }
    int a;
    // if this is a framework value.
    if (value.isFramework()) {
        // look for idName in the android R classes.
        // use 0 a default res value as it's not a valid id value.
        a = getFrameworkResourceValue(value.getResourceType(), value.getName(), 0);
    } else {
        // look for idName in the project R class.
        // use 0 a default res value as it's not a valid id value.
        a = getProjectResourceValue(value.getResourceType(), value.getName(), 0);
    }
    if (a != 0) {
        outValue.resourceId = a;
        return true;
    }
    return false;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 67 with ResourceValue

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

the class BridgeContext method createStyleBasedTypedArray.

// ------------- private new methods
/**
     * Creates a {@link BridgeTypedArray} by filling the values defined by the int[] with the
     * values found in the given style. If no style is specified, the default theme, along with the
     * styles applied to it are used.
     *
     * @see #obtainStyledAttributes(int, int[])
     */
private Pair<BridgeTypedArray, PropertiesMap> createStyleBasedTypedArray(@Nullable StyleResourceValue style, int[] attrs) throws Resources.NotFoundException {
    List<Pair<String, Boolean>> attributes = searchAttrs(attrs);
    BridgeTypedArray ta = Resources_Delegate.newTypeArray(mSystemResources, attrs.length, false);
    PropertiesMap defaultPropMap = new PropertiesMap();
    // for each attribute, get its name so that we can search it in the style
    for (int i = 0; i < attrs.length; i++) {
        Pair<String, Boolean> attribute = attributes.get(i);
        if (attribute != null) {
            // look for the value in the given style
            ResourceValue resValue;
            String attrName = attribute.getFirst();
            boolean frameworkAttr = attribute.getSecond();
            if (style != null) {
                resValue = mRenderResources.findItemInStyle(style, attrName, frameworkAttr);
            } else {
                resValue = mRenderResources.findItemInTheme(attrName, frameworkAttr);
            }
            if (resValue != null) {
                // Add it to defaultPropMap before resolving
                String preResolve = resValue.getValue();
                // resolve it to make sure there are no references left.
                resValue = mRenderResources.resolveResValue(resValue);
                ta.bridgeSetValue(i, attrName, frameworkAttr, resValue);
                defaultPropMap.put(frameworkAttr ? SdkConstants.ANDROID_PREFIX + attrName : attrName, new Property(preResolve, resValue.getValue()));
            }
        }
    }
    ta.sealArray();
    return Pair.of(ta, defaultPropMap);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeTypedArray(android.content.res.BridgeTypedArray) PropertiesMap(com.android.util.PropertiesMap) Property(com.android.util.PropertiesMap.Property) Pair(com.android.util.Pair)

Example 68 with ResourceValue

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

the class AppCompatActionBar method getDrawable.

// TODO: this is duplicated from FrameworkActionBarWrapper$WindowActionBarWrapper
@Nullable
private Drawable getDrawable(@NonNull String name, boolean isFramework) {
    RenderResources res = mBridgeContext.getRenderResources();
    ResourceValue value = res.findResValue(name, isFramework);
    value = res.resolveResValue(value);
    if (value != null) {
        return ResourceHelper.getDrawable(value, mBridgeContext);
    }
    return null;
}
Also used : RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) Nullable(android.annotation.Nullable)

Example 69 with ResourceValue

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

the class FrameworkActionBar method getActionBarHeight.

// TODO: This is duplicated from RenderSessionImpl.
private int getActionBarHeight() {
    RenderResources resources = mBridgeContext.getRenderResources();
    DisplayMetrics metrics = mBridgeContext.getMetrics();
    ResourceValue value = resources.findItemInTheme("actionBarSize", true);
    // resolve it
    value = resources.resolveResValue(value);
    if (value != null) {
        // get the numerical value, if available
        TypedValue typedValue = ResourceHelper.getValue("actionBarSize", value.getValue(), true);
        if (typedValue != null) {
            // compute the pixel value based on the display metrics
            return (int) typedValue.getDimension(metrics);
        }
    }
    return 0;
}
Also used : RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) DisplayMetrics(android.util.DisplayMetrics) TypedValue(android.util.TypedValue)

Example 70 with ResourceValue

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

the class CustomBar method setStyle.

protected void setStyle(String themeEntryName) {
    BridgeContext bridgeContext = getContext();
    RenderResources res = bridgeContext.getRenderResources();
    ResourceValue value = res.findItemInTheme(themeEntryName, true);
    value = res.resolveResValue(value);
    if (!(value instanceof StyleResourceValue)) {
        return;
    }
    StyleResourceValue style = (StyleResourceValue) value;
    // get the background
    ResourceValue backgroundValue = res.findItemInStyle(style, "background", true);
    backgroundValue = res.resolveResValue(backgroundValue);
    if (backgroundValue != null) {
        Drawable d = ResourceHelper.getDrawable(backgroundValue, bridgeContext);
        if (d != null) {
            setBackground(d);
        }
    }
    TextView textView = getStyleableTextView();
    if (textView != null) {
        // get the text style
        ResourceValue textStyleValue = res.findItemInStyle(style, "titleTextStyle", true);
        textStyleValue = res.resolveResValue(textStyleValue);
        if (textStyleValue instanceof StyleResourceValue) {
            StyleResourceValue textStyle = (StyleResourceValue) textStyleValue;
            ResourceValue textSize = res.findItemInStyle(textStyle, "textSize", true);
            textSize = res.resolveResValue(textSize);
            if (textSize != null) {
                TypedValue out = new TypedValue();
                if (ResourceHelper.parseFloatAttribute("textSize", textSize.getValue(), out, true)) {
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, out.getDimension(bridgeContext.getResources().getDisplayMetrics()));
                }
            }
            ResourceValue textColor = res.findItemInStyle(textStyle, "textColor", true);
            textColor = res.resolveResValue(textColor);
            if (textColor != null) {
                ColorStateList stateList = ResourceHelper.getColorStateList(textColor, bridgeContext);
                if (stateList != null) {
                    textView.setTextColor(stateList);
                }
            }
        }
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) RenderResources(com.android.ide.common.rendering.api.RenderResources) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ColorStateList(android.content.res.ColorStateList) TextView(android.widget.TextView) TypedValue(android.util.TypedValue)

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