Search in sources :

Example 1 with BridgeTypedArray

use of android.content.res.BridgeTypedArray in project android_frameworks_base by ParanoidAndroid.

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.
     * @see #obtainStyledAttributes(int, int[])
     */
private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) throws Resources.NotFoundException {
    List<Pair<String, Boolean>> attributes = searchAttrs(attrs);
    BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, false);
    // 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 = mRenderResources.findItemInStyle(style, attribute.getFirst(), attribute.getSecond());
            if (resValue != null) {
                // resolve it to make sure there are no references left.
                ta.bridgeSetValue(i, attribute.getFirst(), attribute.getSecond(), mRenderResources.resolveResValue(resValue));
            }
        }
    }
    ta.sealArray();
    return ta;
}
Also used : 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) BridgeResources(android.content.res.BridgeResources)

Example 2 with BridgeTypedArray

use of android.content.res.BridgeTypedArray in project android_frameworks_base by ParanoidAndroid.

the class BridgeContext method obtainStyledAttributes.

@Override
public final TypedArray obtainStyledAttributes(int resid, int[] attrs) throws Resources.NotFoundException {
    // get the StyleResourceValue based on the resId;
    StyleResourceValue style = getStyleByDynamicId(resid);
    if (style == null) {
        throw new Resources.NotFoundException();
    }
    if (mTypedArrayCache == null) {
        mTypedArrayCache = new HashMap<int[], Map<Integer, TypedArray>>();
        Map<Integer, TypedArray> map = new HashMap<Integer, TypedArray>();
        mTypedArrayCache.put(attrs, map);
        BridgeTypedArray ta = createStyleBasedTypedArray(style, attrs);
        map.put(resid, ta);
        return ta;
    }
    // get the 2nd map
    Map<Integer, TypedArray> map = mTypedArrayCache.get(attrs);
    if (map == null) {
        map = new HashMap<Integer, TypedArray>();
        mTypedArrayCache.put(attrs, map);
    }
    // get the array from the 2nd map
    TypedArray ta = map.get(resid);
    if (ta == null) {
        ta = createStyleBasedTypedArray(style, attrs);
        map.put(resid, ta);
    }
    return ta;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) TypedArray(android.content.res.TypedArray) BridgeTypedArray(android.content.res.BridgeTypedArray) FileNotFoundException(java.io.FileNotFoundException) BridgeTypedArray(android.content.res.BridgeTypedArray) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 3 with BridgeTypedArray

use of android.content.res.BridgeTypedArray 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 4 with BridgeTypedArray

use of android.content.res.BridgeTypedArray in project android_frameworks_base by DirtyUnicorns.

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 5 with BridgeTypedArray

use of android.content.res.BridgeTypedArray in project android_frameworks_base by DirtyUnicorns.

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)

Aggregations

BridgeTypedArray (android.content.res.BridgeTypedArray)18 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)18 PropertiesMap (com.android.util.PropertiesMap)15 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)12 Pair (com.android.util.Pair)12 Property (com.android.util.PropertiesMap.Property)10 FileNotFoundException (java.io.FileNotFoundException)6 ResourceReference (com.android.ide.common.rendering.api.ResourceReference)5 BridgeResources (android.content.res.BridgeResources)2 TypedArray (android.content.res.TypedArray)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1