Search in sources :

Example 11 with ResourceValue

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

the class BridgeContext method obtainStyledAttributes.

@Override
public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) {
    Map<String, String> defaultPropMap = null;
    boolean isPlatformFile = true;
    // Hint: for XmlPullParser, attach source //DEVICE_SRC/dalvik/libcore/xml/src/java
    if (set instanceof BridgeXmlBlockParser) {
        BridgeXmlBlockParser parser = null;
        parser = (BridgeXmlBlockParser) set;
        isPlatformFile = parser.isPlatformFile();
        Object key = parser.getViewCookie();
        if (key != null) {
            defaultPropMap = mDefaultPropMaps.get(key);
            if (defaultPropMap == null) {
                defaultPropMap = new HashMap<String, String>();
                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 = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, isPlatformFile);
    // look for a custom style.
    String customStyle = null;
    if (set != null) {
        customStyle = set.getAttributeValue(null, /* namespace*/
        "style");
    }
    StyleResourceValue customStyleValues = null;
    if (customStyle != null) {
        ResourceValue item = mRenderResources.findResValue(customStyle, false);
        // 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 (defaultPropMap != null) {
            String defStyleName = defStyleAttribute.getFirst();
            if (defStyleAttribute.getSecond()) {
                defStyleName = "android:" + defStyleName;
            }
            defaultPropMap.put("style", defStyleName);
        }
        // look for the style in the current theme, and its parent:
        ResourceValue item = mRenderResources.findItemInTheme(defStyleAttribute.getFirst(), defStyleAttribute.getSecond());
        if (item != null) {
            // item is a reference to a style entry. Search for it.
            item = mRenderResources.findResValue(item.getValue(), false);
            if (item instanceof StyleResourceValue) {
                defStyleValues = (StyleResourceValue) item;
            }
        } 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) {
        boolean isFrameworkRes = true;
        Pair<ResourceType, String> value = Bridge.resolveResourceId(defStyleRes);
        if (value == null) {
            value = mProjectCallback.resolveResourceId(defStyleRes);
            isFrameworkRes = false;
        }
        if (value != null) {
            if (value.getFirst() == ResourceType.STYLE) {
                // look for the style in the current theme, and its parent:
                ResourceValue item = mRenderResources.findItemInTheme(value.getSecond(), isFrameworkRes);
                if (item != null) {
                    if (item instanceof StyleResourceValue) {
                        if (defaultPropMap != null) {
                            defaultPropMap.put("style", item.getName());
                        }
                        defStyleValues = (StyleResourceValue) 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("Resouce 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 = mProjectCallback.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().booleanValue();
            String value = null;
            if (set != null) {
                value = set.getAttributeValue(frameworkAttr ? BridgeConstants.NS_RESOURCES : appNamespace, attrName);
                // new res-auto namespace as well
                if (frameworkAttr == false && 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) {
                    // put the first default value, before the resolution.
                    if (defaultPropMap != null) {
                        defaultPropMap.put(attrName, resValue.getValue());
                    }
                    resValue = mRenderResources.resolveResValue(resValue);
                }
                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 : BridgeResources(android.content.res.BridgeResources) 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) Pair(com.android.util.Pair)

Example 12 with ResourceValue

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

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 = mProjectCallback.resolveResourceId(resid);
        isFrameworkRes = false;
    }
    if (resourceInfo == null) {
        return false;
    }
    ResourceValue value = mRenderResources.findItemInTheme(resourceInfo.getSecond(), isFrameworkRes);
    if (resolveRefs) {
        value = mRenderResources.resolveResValue(value);
    }
    // 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 13 with ResourceValue

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

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 14 with ResourceValue

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

the class RenderSessionImpl method setActiveToolbar.

/**
     * If the root layout is a CoordinatorLayout with an AppBar:
     * Set the title of the AppBar to the title of the activity context.
     */
private void setActiveToolbar(View view, BridgeContext context, SessionParams params) {
    View coordinatorLayout = findChildView(view, DesignLibUtil.CN_COORDINATOR_LAYOUT);
    if (coordinatorLayout == null) {
        return;
    }
    View appBar = findChildView(coordinatorLayout, DesignLibUtil.CN_APPBAR_LAYOUT);
    if (appBar == null) {
        return;
    }
    ViewGroup collapsingToolbar = (ViewGroup) findChildView(appBar, DesignLibUtil.CN_COLLAPSING_TOOLBAR_LAYOUT);
    if (collapsingToolbar == null) {
        return;
    }
    if (!hasToolbar(collapsingToolbar)) {
        return;
    }
    RenderResources res = context.getRenderResources();
    String title = params.getAppLabel();
    ResourceValue titleValue = res.findResValue(title, false);
    if (titleValue != null && titleValue.getValue() != null) {
        title = titleValue.getValue();
    }
    DesignLibUtil.setTitle(collapsingToolbar, title);
}
Also used : ViewGroup(android.view.ViewGroup) RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) 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)

Example 15 with ResourceValue

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

the class BridgeTypedArray method sealArray.

/**
     * Seals the array after all calls to
     * {@link #bridgeSetValue(int, String, boolean, ResourceValue)} have been done.
     * <p/>This allows to compute the list of non default values, permitting
     * {@link #getIndexCount()} to return the proper value.
     */
public void sealArray() {
    // fills TypedArray.mIndices which is used to implement getIndexCount/getIndexAt
    // first count the array size
    int count = 0;
    ArrayList<Integer> emptyIds = null;
    for (int i = 0; i < mResourceData.length; i++) {
        ResourceValue data = mResourceData[i];
        if (data != null) {
            String dataValue = data.getValue();
            if (REFERENCE_NULL.equals(dataValue) || REFERENCE_UNDEFINED.equals(dataValue)) {
                mResourceData[i] = null;
            } else if (REFERENCE_EMPTY.equals(dataValue)) {
                mResourceData[i] = null;
                if (emptyIds == null) {
                    emptyIds = new ArrayList<Integer>(4);
                }
                emptyIds.add(i);
            } else {
                count++;
            }
        }
    }
    if (emptyIds != null) {
        mEmptyIds = new int[emptyIds.size()];
        for (int i = 0; i < emptyIds.size(); i++) {
            mEmptyIds[i] = emptyIds.get(i);
        }
    }
    // allocate the table with an extra to store the size
    mIndices = new int[count + 1];
    mIndices[0] = count;
    // fill the array with the indices.
    int index = 1;
    for (int i = 0; i < mResourceData.length; i++) {
        if (mResourceData[i] != null) {
            mIndices[index++] = i;
        }
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue) ArrayList(java.util.ArrayList)

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