Search in sources :

Example 56 with StyleResourceValue

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

the class StyleEnumSupport method convertStylesToDisplayValues.

@NotNull
protected List<ValueWithDisplayString> convertStylesToDisplayValues(@NotNull List<StyleResourceValue> styles) {
    List<ValueWithDisplayString> values = new ArrayList<>();
    StyleResourceValue previousStyle = null;
    for (StyleResourceValue style : styles) {
        if (previousStyle != null && (previousStyle.isFramework() != style.isFramework() || previousStyle.isUserDefined() != style.isUserDefined())) {
            values.add(ValueWithDisplayString.SEPARATOR);
        }
        previousStyle = style;
        String prefix = style.isFramework() ? ANDROID_STYLE_RESOURCE_PREFIX : STYLE_RESOURCE_PREFIX;
        values.add(createFromResolvedValue(style.getName(), prefix + style.getName(), null));
    }
    return values;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with StyleResourceValue

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

the class LintUtils method getInheritedStyles.

@Nullable
public static List<StyleResourceValue> getInheritedStyles(@NonNull Project project, @NonNull LintClient client, @NonNull String styleUrl) {
    if (!client.supportsProjectResources()) {
        return null;
    }
    AbstractResourceRepository resources = client.getProjectResources(project, true);
    if (resources == null) {
        return null;
    }
    ResourceUrl style = ResourceUrl.parse(styleUrl);
    if (style == null || style.framework) {
        return null;
    }
    List<StyleResourceValue> result = null;
    Queue<ResourceValue> queue = new ArrayDeque<ResourceValue>();
    queue.add(new ResourceValue(style.type, style.name, false));
    Set<String> seen = Sets.newHashSet();
    int count = 0;
    while (count < 30 && !queue.isEmpty()) {
        ResourceValue front = queue.remove();
        String name = front.getName();
        seen.add(name);
        List<ResourceItem> items = resources.getResourceItem(front.getResourceType(), name);
        if (items != null) {
            for (ResourceItem item : items) {
                ResourceValue rv = item.getResourceValue(false);
                if (rv instanceof StyleResourceValue) {
                    StyleResourceValue srv = (StyleResourceValue) rv;
                    if (result == null) {
                        result = Lists.newArrayList();
                    }
                    result.add(srv);
                    String parent = srv.getParentStyle();
                    if (parent != null && !parent.startsWith(ANDROID_PREFIX)) {
                        ResourceUrl p = ResourceUrl.parse(parent);
                        if (p != null && !p.framework && !seen.contains(p.name)) {
                            seen.add(p.name);
                            queue.add(new ResourceValue(ResourceType.STYLE, p.name, false));
                        }
                    }
                    int index = name.lastIndexOf('.');
                    if (index > 0) {
                        String parentName = name.substring(0, index);
                        if (!seen.contains(parentName)) {
                            seen.add(parentName);
                            queue.add(new ResourceValue(ResourceType.STYLE, parentName, false));
                        }
                    }
                }
            }
        }
        count++;
    }
    return result;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) AbstractResourceRepository(com.android.ide.common.res2.AbstractResourceRepository) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceItem(com.android.ide.common.res2.ResourceItem) ResourceUrl(com.android.ide.common.resources.ResourceUrl) Nullable(com.android.annotations.Nullable)

Example 58 with StyleResourceValue

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

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 59 with StyleResourceValue

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

the class BridgeContext method obtainStyledAttributes.

@Override
public final BridgeTypedArray obtainStyledAttributes(int resId, int[] attrs) throws Resources.NotFoundException {
    StyleResourceValue style = null;
    // get the StyleResourceValue based on the resId;
    if (resId != 0) {
        style = getStyleByDynamicId(resId);
        if (style == null) {
            // In some cases, style may not be a dynamic id, so we do a full search.
            ResourceReference ref = resolveId(resId);
            if (ref != null) {
                style = mRenderResources.getStyle(ref.getName(), ref.isFramework());
            }
        }
        if (style == null) {
            throw new Resources.NotFoundException();
        }
    }
    if (mTypedArrayCache == null) {
        mTypedArrayCache = new TypedArrayCache();
    }
    List<StyleResourceValue> currentThemes = mRenderResources.getAllThemes();
    Pair<BridgeTypedArray, PropertiesMap> typeArrayAndPropertiesPair = mTypedArrayCache.get(attrs, currentThemes, resId);
    if (typeArrayAndPropertiesPair == null) {
        typeArrayAndPropertiesPair = createStyleBasedTypedArray(style, attrs);
        mTypedArrayCache.put(attrs, currentThemes, resId, typeArrayAndPropertiesPair);
    }
    // Add value to defaultPropsMap if needed
    if (typeArrayAndPropertiesPair.getSecond() != null) {
        BridgeXmlBlockParser parser = getCurrentParser();
        Object key = parser != null ? parser.getViewCookie() : null;
        if (key != null) {
            PropertiesMap defaultPropMap = mDefaultPropMaps.get(key);
            if (defaultPropMap == null) {
                defaultPropMap = typeArrayAndPropertiesPair.getSecond();
                mDefaultPropMaps.put(key, defaultPropMap);
            } else {
                defaultPropMap.putAll(typeArrayAndPropertiesPair.getSecond());
            }
        }
    }
    return typeArrayAndPropertiesPair.getFirst();
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) FileNotFoundException(java.io.FileNotFoundException) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) BridgeTypedArray(android.content.res.BridgeTypedArray) PropertiesMap(com.android.util.PropertiesMap)

Example 60 with StyleResourceValue

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

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)

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