Search in sources :

Example 31 with StyleResourceValue

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

the class StyleFilter method isDerived.

@VisibleForTesting
boolean isDerived(@NotNull StyleResourceValue style) {
    String styleName = getStyleName(style);
    if (myDerivedStyles.contains(styleName)) {
        return true;
    }
    if (myOtherStyles.contains(styleName)) {
        return false;
    }
    StyleResourceValue parentStyle = myResolver.getParent(style);
    if (parentStyle != null && !myCurrentInheritanceChain.contains(styleName)) {
        myCurrentInheritanceChain.add(styleName);
        return found(styleName, isDerived(parentStyle));
    }
    return found(styleName, false);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 32 with StyleResourceValue

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

the class EnumSupportFactoryTest method testStyle.

public void testStyle() {
    StyleResourceValue checkboxStyle = new StyleResourceValue(ResourceType.STYLE, "Widget.CompoundButton.CheckBox", true);
    ResourceValueMap frameworkResources = ResourceValueMap.create();
    frameworkResources.put(checkboxStyle.getName(), checkboxStyle);
    ResourceValueMap projectResources = ResourceValueMap.create();
    when(myProperty.getTagName()).thenReturn(CHECK_BOX);
    when(myResolver.getFrameworkResources()).thenReturn(Collections.singletonMap(ResourceType.STYLE, frameworkResources));
    when(myResolver.getProjectResources()).thenReturn(Collections.singletonMap(ResourceType.STYLE, projectResources));
    checkSupported(ATTR_STYLE, StyleEnumSupport.class);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap)

Example 33 with StyleResourceValue

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

the class StyleFilterTest method assertStyle.

private void assertStyle(@NotNull String name, boolean isFramework, boolean userDefined, boolean isDerived, boolean filteredOut) {
    StyleResourceValue style;
    if (isFramework) {
        style = (StyleResourceValue) (myResolver.getFrameworkResources().get(ResourceType.STYLE).get(name));
    } else {
        style = (StyleResourceValue) (myResolver.getProjectResources().get(ResourceType.STYLE).get(name));
    }
    assertNotNull(style);
    assertStyle(style, name, isFramework, userDefined, isDerived, filteredOut);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Example 34 with StyleResourceValue

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

the class StyleFilterTest method dumpStyles.

private static void dumpStyles(@NotNull List<StyleResourceValue> styles) {
    StringBuilder builder = new StringBuilder();
    String divider = StringUtil.repeat("=", 80) + "\n";
    builder.append(divider);
    builder.append("Origin    ");
    builder.append("Name\n");
    builder.append(divider);
    for (StyleResourceValue style : styles) {
        if (style.isUserDefined()) {
            builder.append("User      ");
        } else if (!style.isFramework()) {
            builder.append("AppCompat ");
        } else {
            builder.append("Framework ");
        }
        builder.append(style.getName());
        builder.append("\n");
    }
    builder.append(divider);
    System.err.println(builder);
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Example 35 with StyleResourceValue

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

the class BridgeTypedArray method getResourceId.

/**
     * Retrieve the resource identifier for the attribute at
     * <var>index</var>.  Note that attribute resource as resolved when
     * the overall {@link TypedArray} object is retrieved.  As a
     * result, this function will return the resource identifier of the
     * final resource value that was found, <em>not</em> necessarily the
     * original resource that was specified by the attribute.
     *
     * @param index Index of attribute to retrieve.
     * @param defValue Value to return if the attribute is not defined or
     *                 not a resource.
     *
     * @return Attribute resource identifier, or defValue if not defined.
     */
@Override
public int getResourceId(int index, int defValue) {
    if (index < 0 || index >= mResourceData.length) {
        return defValue;
    }
    // get the Resource for this index
    ResourceValue resValue = mResourceData[index];
    // no data, return the default value.
    if (resValue == null) {
        return defValue;
    }
    // check if this is a style resource
    if (resValue instanceof StyleResourceValue) {
        // get the id that will represent this style.
        return mContext.getDynamicIdByStyle((StyleResourceValue) resValue);
    }
    // (and getValue() returning null!). We need to handle this!
    if (resValue.getResourceType() != null) {
        // if this is a framework id
        if (mPlatformFile || resValue.isFramework()) {
            // look for idName in the android R classes
            return mContext.getFrameworkResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
        }
        // look for idName in the project R class.
        return mContext.getProjectResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
    }
    // else, try to get the value, and resolve it somehow.
    String value = resValue.getValue();
    if (value == null) {
        return defValue;
    }
    // if the value is just an integer, return it.
    try {
        int i = Integer.parseInt(value);
        if (Integer.toString(i).equals(value)) {
            return i;
        }
    } catch (NumberFormatException e) {
    // pass
    }
    // if this is a reference to an id, find it.
    if (value.startsWith("@id/") || value.startsWith("@+") || value.startsWith("@android:id/")) {
        int pos = value.indexOf('/');
        String idName = value.substring(pos + 1);
        boolean create = value.startsWith("@+");
        boolean isFrameworkId = mPlatformFile || value.startsWith("@android") || value.startsWith("@+android");
        // Look for the idName in project or android R class depending on isPlatform.
        if (create) {
            Integer idValue;
            if (isFrameworkId) {
                idValue = Bridge.getResourceId(ResourceType.ID, idName);
            } else {
                idValue = mContext.getLayoutlibCallback().getResourceId(ResourceType.ID, idName);
            }
            return idValue == null ? defValue : idValue;
        }
        // one is not found.
        if (isFrameworkId) {
            return mContext.getFrameworkResourceValue(ResourceType.ID, idName, defValue);
        } else {
            return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue);
        }
    }
    // not a direct id valid reference? resolve it
    Integer idValue;
    if (resValue.isFramework()) {
        idValue = Bridge.getResourceId(resValue.getResourceType(), resValue.getName());
    } else {
        idValue = mContext.getLayoutlibCallback().getResourceId(resValue.getResourceType(), resValue.getName());
    }
    if (idValue != null) {
        return idValue;
    }
    Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_RESOLVE, String.format("Unable to resolve id \"%1$s\" for attribute \"%2$s\"", value, mNames[index]), resValue);
    return defValue;
}
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) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue)

Aggregations

StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)69 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)41 BridgeTypedArray (android.content.res.BridgeTypedArray)18 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)15 PropertiesMap (com.android.util.PropertiesMap)15 Pair (com.android.util.Pair)12 RenderResources (com.android.ide.common.rendering.api.RenderResources)11 Property (com.android.util.PropertiesMap.Property)10 ResourceReference (com.android.ide.common.rendering.api.ResourceReference)9 ResourceItem (com.android.ide.common.res2.ResourceItem)7 ColorStateList (android.content.res.ColorStateList)6 BitmapDrawable (android.graphics.drawable.BitmapDrawable)6 Drawable (android.graphics.drawable.Drawable)6 TypedValue (android.util.TypedValue)6 TextView (android.widget.TextView)6 ResourceType (com.android.resources.ResourceType)6 FileNotFoundException (java.io.FileNotFoundException)6 NotNull (org.jetbrains.annotations.NotNull)6 Context (android.content.Context)5 ContextThemeWrapper (android.view.ContextThemeWrapper)5