com.android.ide.common.rendering.api.ResourceValue Java Examples
The following examples show how to use
com.android.ide.common.rendering.api.ResourceValue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Search for the style in the given map and log an error if the obtained resource is not * {@link StyleResourceValue}. * * @return The {@link ResourceValue} found in the map. */ @Nullable private ResourceValue getStyleFromMap(@NonNull Map<String, ResourceValue> styleMap, @NonNull String styleName) { ResourceValue res; res = styleMap.get(styleName); if (res != null) { if (!(res instanceof StyleResourceValue) && mLogger != null) { mLogger.error(null, String.format( "Style %1$s is not of type STYLE (instead %2$s)", styleName, res.getResourceType().toString()), null); } } return res; }
Example #2
Source File: ResourceItem.java From javaide with GNU General Public License v3.0 | 6 votes |
private ResourceValue parsePluralsValue(PluralsResourceValue value) { NodeList children = mValue.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { NamedNodeMap attributes = child.getAttributes(); String quantity = getAttributeValue(attributes, ATTR_QUANTITY); if (quantity != null) { String text = getTextNode(child.getChildNodes()); text = ValueXmlHelper.unescapeResourceString(text, false, true); value.addPlural(quantity, text); } } } return value; }
Example #3
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) { if (!mLookupChain.isEmpty() && reference.startsWith(PREFIX_RESOURCE_REF)) { ResourceValue prev = mLookupChain.get(mLookupChain.size() - 1); if (!reference.equals(prev.getValue())) { ResourceValue next = new ResourceValue(prev.getResourceType(), prev.getName(), prev.isFramework()); next.setValue(reference); mLookupChain.add(next); } } ResourceValue resValue = super.findResValue(reference, forceFrameworkOnly); if (resValue != null) { mLookupChain.add(resValue); } return resValue; }
Example #4
Source File: AbstractResourceRepository.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
@Nullable public ResourceValue getConfiguredValue( @NonNull ResourceType type, @NonNull String name, @NonNull FolderConfiguration referenceConfig) { // get the resource item for the given type ListMultimap<String, ResourceItem> items = getMap(type, false); if (items == null) { return null; } List<ResourceItem> keyItems = items.get(name); if (keyItems == null) { return null; } // look for the best match for the given configuration // the match has to be of type ResourceFile since that's what the input list contains ResourceItem match = (ResourceItem) referenceConfig.findMatchingConfigurable(keyItems); return match != null ? match.getResourceValue(mFramework) : null; }
Example #5
Source File: SingleResourceFile.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
public SingleResourceFile(IAbstractFile file, ResourceFolder folder) { super(file, folder); // we need to infer the type of the resource from the folder type. // This is easy since this is a single Resource file. List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType()); mType = types.get(0); // compute the resource name mResourceName = getResourceName(mType); // test if there's a density qualifier associated with the resource DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier(); if (qualifier == null) { mValue = new ResourceValue(mType, getResourceName(mType), file.getOsLocation(), isFramework()); } else { mValue = new DensityBasedResourceValue( mType, getResourceName(mType), file.getOsLocation(), qualifier.getValue(), isFramework()); } }
Example #6
Source File: AppIndexingApiDetector.java From javaide with GNU General Public License v3.0 | 6 votes |
private static String replaceUrlWithValue(@NonNull XmlContext context, @NonNull String str) { Project project = context.getProject(); LintClient client = context.getClient(); if (!client.supportsProjectResources()) { return str; } ResourceUrl style = ResourceUrl.parse(str); if (style == null || style.type != ResourceType.STRING || style.framework) { return str; } AbstractResourceRepository resources = client.getProjectResources(project, true); if (resources == null) { return str; } List<ResourceItem> items = resources.getResourceItem(ResourceType.STRING, style.name); if (items == null || items.isEmpty()) { return str; } ResourceValue resourceValue = items.get(0).getResourceValue(false); if (resourceValue == null) { return str; } return resourceValue.getValue() == null ? str : resourceValue.getValue(); }
Example #7
Source File: RenderServiceFactory.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Returns a {@link ResourceResolver} for a given config and project resource. * * @param config * @param projectResources * @param themeName * @param isProjectTheme * @return */ public ResourceResolver createResourceResolver( FolderConfiguration config, ResourceRepository projectResources, String themeName, boolean isProjectTheme) { Map<ResourceType, Map<String, ResourceValue>> configedProjectRes = projectResources.getConfiguredResources(config); Map<ResourceType, Map<String, ResourceValue>> configedFrameworkRes = mResources.getConfiguredResources(config); return ResourceResolver.create(configedProjectRes, configedFrameworkRes, themeName, isProjectTheme); }
Example #8
Source File: RenderService.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Renders the given resource value (which should refer to a drawable) and returns it * as an image * * @param drawableResourceValue the drawable resource value to be rendered, or null * @return the image, or null if something went wrong */ public BufferedImage renderDrawable(ResourceValue drawableResourceValue) { if (drawableResourceValue == null) { return null; } finishConfiguration(); figureSomeValuesOut(); DrawableParams params = new DrawableParams(drawableResourceValue, this, mWidth, mHeight, mConfig.getDensityQualifier().getValue(), mXdpi, mYdpi, mResourceResolver, mProjectCallback, mMinSdkVersion, mTargetSdkVersion, mLogger); params.setForceNoDecor(); Result result = mLayoutLib.renderDrawable(params); if (result != null && result.isSuccess()) { Object data = result.getData(); if (data instanceof BufferedImage) { return (BufferedImage) data; } } return null; }
Example #9
Source File: ResourceRepository.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Returns the resources values matching a given {@link FolderConfiguration} for the current * project. * * @param referenceConfig the configuration that each value must match. * @return a map with guaranteed to contain an entry for each {@link ResourceType} */ @NonNull protected final Map<ResourceType, Map<String, ResourceValue>> doGetConfiguredResources( @NonNull FolderConfiguration referenceConfig) { ensureInitialized(); Map<ResourceType, Map<String, ResourceValue>> map = new EnumMap<ResourceType, Map<String, ResourceValue>>(ResourceType.class); for (ResourceType key : ResourceType.values()) { // get the local results and put them in the map map.put(key, getConfiguredResource(key, referenceConfig)); } return map; }
Example #10
Source File: MultiResourceFile.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Adds a resource item to the list * @param value The value of the resource. */ @Override public void addResourceValue(ResourceValue value) { ResourceType resType = value.getResourceType(); Map<String, ResourceValue> list = mResourceItems.get(resType); // if the list does not exist, create it. if (list == null) { list = new HashMap<String, ResourceValue>(); mResourceItems.put(resType, list); } else { // look for a possible value already existing. ResourceValue oldValue = list.get(value.getName()); if (oldValue != null) { oldValue.replaceWith(value); return; } } // empty list or no match found? add the given resource list.put(value.getName(), value); }
Example #11
Source File: AbstractResourceRepository.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Returns the resources values matching a given {@link FolderConfiguration}. * * @param referenceConfig the configuration that each value must match. * @return a map with guaranteed to contain an entry for each {@link ResourceType} */ @NonNull public Map<ResourceType, Map<String, ResourceValue>> getConfiguredResources( @NonNull FolderConfiguration referenceConfig) { Map<ResourceType, Map<String, ResourceValue>> map = Maps.newEnumMap(ResourceType.class); synchronized (ITEM_MAP_LOCK) { Map<ResourceType, ListMultimap<String, ResourceItem>> itemMap = getMap(); for (ResourceType key : ResourceType.values()) { // get the local results and put them in the map map.put(key, getConfiguredResources(itemMap, key, referenceConfig)); } } return map; }
Example #12
Source File: IdGeneratingResourceFile.java From NBANDROID-V2 with Apache License 2.0 | 6 votes |
/** * Returns the resource value associated with this whole file as a layout resource * @param file the file handler that represents this file * @param folder the folder this file is under * @return a resource value associated with this layout */ private ResourceValue getFileValue(IAbstractFile file, ResourceFolder folder) { // test if there's a density qualifier associated with the resource DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier(); ResourceValue value; if (!ResourceQualifier.isValid(qualifier)) { value = new ResourceValueImpl( new ResourceReference(mFileType, mFileName, isFramework()), file.getOsLocation()); } else { value = new DensityBasedResourceValueImpl( new ResourceReference(mFileType, mFileName, isFramework()), file.getOsLocation(), qualifier.getValue()); } return value; }
Example #13
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public StyleResourceValue getTheme(String name, boolean frameworkTheme) { ResourceValue theme; if (frameworkTheme) { Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get( ResourceType.STYLE); theme = frameworkStyleMap.get(name); } else { Map<String, ResourceValue> projectStyleMap = mProjectResources.get(ResourceType.STYLE); theme = projectStyleMap.get(name); } if (theme instanceof StyleResourceValue) { return (StyleResourceValue) theme; } return null; }
Example #14
Source File: ResourceResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
@Override public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) { if (!mLookupChain.isEmpty() && reference.startsWith(PREFIX_RESOURCE_REF)) { ResourceValue prev = mLookupChain.get(mLookupChain.size() - 1); if (!reference.equals(prev.getValue())) { ResourceValue next = new ResourceValue(prev.getResourceType(), prev.getName(), prev.isFramework()); next.setValue(reference); mLookupChain.add(next); } } ResourceValue resValue = super.findResValue(reference, forceFrameworkOnly); if (resValue != null) { mLookupChain.add(resValue); } return resValue; }
Example #15
Source File: IdGeneratingResourceFile.java From NBANDROID-V2 with Apache License 2.0 | 6 votes |
@Override public ResourceValue getValue(ResourceType type, String name) { // Check to see if they're asking for one of the right types: if (type != mFileType && type != ResourceType.ID) { return null; } // If they're looking for a resource of this type with this name give them the whole file if (type == mFileType && name.equals(mFileName)) { return mFileValue; } else { // Otherwise try to return them an ID // the map will return null if it's not found return mIdResources.get(name); } }
Example #16
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * Search for the style in the given map and log an error if the obtained resource is not * {@link StyleResourceValue}. * * @return The {@link ResourceValue} found in the map. */ @Nullable private ResourceValue getStyleFromMap(@NonNull Map<String, ResourceValue> styleMap, @NonNull String styleName) { ResourceValue res; res = styleMap.get(styleName); if (res != null) { if (!(res instanceof StyleResourceValue) && mLogger != null) { mLogger.error(null, String.format( "Style %1$s is not of type STYLE (instead %2$s)", styleName, res.getResourceType().toString()), null); } } return res; }
Example #17
Source File: SingleResourceFile.java From javaide with GNU General Public License v3.0 | 6 votes |
public SingleResourceFile(IAbstractFile file, ResourceFolder folder) { super(file, folder); // we need to infer the type of the resource from the folder type. // This is easy since this is a single Resource file. List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType()); mType = types.get(0); // compute the resource name mResourceName = getResourceName(mType); // test if there's a density qualifier associated with the resource DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier(); if (qualifier == null) { mValue = new ResourceValue(mType, getResourceName(mType), file.getOsLocation(), isFramework()); } else { mValue = new DensityBasedResourceValue( mType, getResourceName(mType), file.getOsLocation(), qualifier.getValue(), isFramework()); } }
Example #18
Source File: ResourceRepository.java From NBANDROID-V2 with Apache License 2.0 | 6 votes |
/** * Returns a map of (resource name, resource value) for the given {@link ResourceType}. * <p>The values returned are taken from the resource files best matching a given * {@link FolderConfiguration}. * * @param type the type of the resources. * @param referenceConfig the configuration to best match. */ @NonNull private ResourceValueMap getConfiguredResource(@NonNull ResourceType type, @NonNull FolderConfiguration referenceConfig) { // get the resource item for the given type Map<String, ResourceItem> items = mResourceMap.get(type); if (items == null) { return ResourceValueMap.create(); } // create the map ResourceValueMap map = ResourceValueMap.createWithExpectedSize(items.size()); for (ResourceItem item : items.values()) { ResourceValue value = item.getResourceValue(type, referenceConfig, isFrameworkRepository()); if (value != null) { map.put(item.getName(), value); } } return map; }
Example #19
Source File: ResourceResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Returns true if the given {@code themeStyle} extends the theme given by * {@code parentStyle} */ public boolean themeExtends(@NonNull String parentStyle, @NonNull String themeStyle) { ResourceValue parentValue = findResValue(parentStyle, parentStyle.startsWith(ANDROID_STYLE_RESOURCE_PREFIX)); if (parentValue instanceof StyleResourceValue) { ResourceValue themeValue = findResValue(themeStyle, themeStyle.startsWith(ANDROID_STYLE_RESOURCE_PREFIX)); if (themeValue == parentValue) { return true; } if (themeValue instanceof StyleResourceValue) { return themeIsParentOf((StyleResourceValue) parentValue, (StyleResourceValue) themeValue); } } return false; }
Example #20
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 6 votes |
@Override public ResourceValue resolveValue(ResourceType type, String name, String value, boolean isFrameworkValue) { if (value == null) { return null; } // get the ResourceValue referenced by this value ResourceValue resValue = findResValue(value, isFrameworkValue); // if resValue is null, but value is not null, this means it was not a reference. // we return the name/value wrapper in a ResourceValue. the isFramework flag doesn't // matter. if (resValue == null) { return new ResourceValue(type, name, value, isFrameworkValue); } // we resolved a first reference, but we need to make sure this isn't a reference also. return resolveResValue(resValue); }
Example #21
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override @Nullable public StyleResourceValue getStyle(@NonNull String styleName, boolean isFramework) { ResourceValue res; Map<String, ResourceValue> styleMap; // First check if we can find the style directly. if (isFramework) { styleMap = mFrameworkResources.get(ResourceType.STYLE); } else { styleMap = mProjectResources.get(ResourceType.STYLE); } res = getStyleFromMap(styleMap, styleName); if (res != null) { // If the obtained resource is not StyleResourceValue, return null. return res instanceof StyleResourceValue ? (StyleResourceValue) res : null; } // We cannot find the style directly. The style name may have been flattened by AAPT for use // in the R class. Try and obtain the original name. String xmlStyleName = getReverseStyleMap(isFramework) .get(getNormalizedStyleName(styleName)); if (!styleName.equals(xmlStyleName)) { res = getStyleFromMap(styleMap, xmlStyleName); } return res instanceof StyleResourceValue ? (StyleResourceValue) res : null; }
Example #22
Source File: AbstractResourceRepository.java From javaide with GNU General Public License v3.0 | 5 votes |
@NonNull public Map<String, ResourceValue> getConfiguredResources( @NonNull Map<ResourceType, ListMultimap<String, ResourceItem>> itemMap, @NonNull ResourceType type, @NonNull FolderConfiguration referenceConfig) { // get the resource item for the given type ListMultimap<String, ResourceItem> items = itemMap.get(type); if (items == null) { return Maps.newHashMap(); } Set<String> keys = items.keySet(); // create the map Map<String, ResourceValue> map = Maps.newHashMapWithExpectedSize(keys.size()); for (String key : keys) { List<ResourceItem> keyItems = items.get(key); // look for the best match for the given configuration // the match has to be of type ResourceFile since that's what the input list contains ResourceItem match = (ResourceItem) referenceConfig.findMatchingConfigurable(keyItems); if (match != null) { ResourceValue value = match.getResourceValue(mFramework); if (value != null) { map.put(match.getName(), value); } } } return map; }
Example #23
Source File: ResourceItemResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
@Nullable private ResourceValue resolveResValue(@Nullable ResourceValue resValue, int depth) { if (resValue == null) { return null; } // if the resource value is null, we simply return it. String value = resValue.getValue(); if (value == null) { return resValue; } // else attempt to find another ResourceValue referenced by this one. ResourceValue resolvedResValue = findResValue(value, resValue.isFramework()); // if the value did not reference anything, then we simply return the input value if (resolvedResValue == null) { return resValue; } // detect potential loop due to mishandled namespace in attributes if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) { if (mLogger != null) { mLogger.error(LayoutLog.TAG_BROKEN, String.format( "Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.", value), null); } return resValue; } // otherwise, we attempt to resolve this new value as well return resolveResValue(resolvedResValue, depth + 1); }
Example #24
Source File: ResourceItemResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
/** * Returns a display string for a resource lookup * @param url the resource url, such as {@code @string/foo} * @param lookupChain the list of resolved items to display * @return the display string */ @NonNull public static String getDisplayString( @NonNull String url, @NonNull List<ResourceValue> lookupChain) { StringBuilder sb = new StringBuilder(); sb.append(url); String prev = url; for (ResourceValue element : lookupChain) { if (element == null) { continue; } String value = element.getValue(); if (value == null) { continue; } String text = value; if (text.equals(prev)) { continue; } sb.append(" => "); // Strip paths if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) { int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\')); if (end != -1) { text = text.substring(end + 1); } } sb.append(text); prev = value; } return sb.toString(); }
Example #25
Source File: ResourceItem.java From javaide with GNU General Public License v3.0 | 5 votes |
/** * Returns a {@link ResourceValue} for this item based on the given configuration. * If the ResourceItem has several source files, one will be selected based on the config. * @param type the type of the resource. This is necessary because ResourceItem doesn't embed * its type, but ResourceValue does. * @param referenceConfig the config of the resource item. * @param isFramework whether the resource is a framework value. Same as the type. * @return a ResourceValue or null if none match the config. */ public ResourceValue getResourceValue(ResourceType type, FolderConfiguration referenceConfig, boolean isFramework) { // look for the best match for the given configuration // the match has to be of type ResourceFile since that's what the input list contains ResourceFile match = (ResourceFile) referenceConfig.findMatchingConfigurable(mFiles); if (match != null) { // get the value of this configured resource. return match.getValue(type, mName); } return null; }
Example #26
Source File: LayoutLibCallback.java From NBANDROID-V2 with Apache License 2.0 | 5 votes |
@Override public ILayoutPullParser getParser(ResourceValue layoutResource) { if (layoutResource.getValue() != null) { return new LayoutPullParser(new File(layoutResource.getValue()), ResourceNamespace.RES_AUTO); } return null; }
Example #27
Source File: ResourceRepository.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
/** * Returns the resources values matching a given {@link FolderConfiguration}. * * @param referenceConfig the configuration that each value must match. * @return a map with guaranteed to contain an entry for each {@link ResourceType} */ @NonNull public Map<ResourceType, Map<String, ResourceValue>> getConfiguredResources( @NonNull FolderConfiguration referenceConfig) { ensureInitialized(); return doGetConfiguredResources(referenceConfig); }
Example #28
Source File: ResourceResolver.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public ResourceValue resolveResValue(ResourceValue resValue) { if (resValue != null) { mLookupChain.add(resValue); } return super.resolveResValue(resValue); }
Example #29
Source File: ResourceResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
/** * Creates a new {@link ResourceResolver} which records all resource resolution * lookups into the given list. Note that it is the responsibility of the caller * to clear/reset the list between subsequent lookup operations. * * @param lookupChain the list to write resource lookups into * @return a new {@link ResourceResolver} */ public ResourceResolver createRecorder(List<ResourceValue> lookupChain) { ResourceResolver resolver = new RecordingResourceResolver( lookupChain, mProjectResources, mFrameworkResources, mThemeName, mIsProjectTheme); resolver.mFrameworkProvider = mFrameworkProvider; resolver.mLogger = mLogger; resolver.mDefaultTheme = mDefaultTheme; resolver.mStyleInheritanceMap.putAll(mStyleInheritanceMap); resolver.mThemes.addAll(mThemes); return resolver; }
Example #30
Source File: ResourceResolver.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
@Override public ResourceValue findItemInStyle(StyleResourceValue style, String itemName, boolean isFrameworkAttr) { ResourceValue value = super.findItemInStyle(style, itemName, isFrameworkAttr); if (value != null) { mLookupChain.add(value); } return value; }