/** * Returns the {@link ResourceFile} matching the given name, {@link ResourceType} and * configuration. * * <p>This only works with files generating one resource named after the file (for instance, * layouts, bitmap based drawable, xml, anims). * * @param name the resource name or file name * @param type the folder type search for * @param config the folder configuration to match for * @return the matching file or <code>null</code> if no match was found. */ @Nullable public ResourceFile getMatchingFile( @NonNull String name, @NonNull ResourceType type, @NonNull FolderConfiguration config) { ensureInitialized(); String resourceName = name; int dot = resourceName.indexOf('.'); if (dot != -1) { resourceName = resourceName.substring(0, dot); } Map<String, ResourceItem> items = mResourceMap.get(type); if (items != null) { ResourceItem item = items.get(resourceName); if (item != null) { List<ResourceFile> files = item.getSourceFileList(); if (files != null) { if (files.size() > 1) { ResourceValue value = item.getResourceValue(type, config, isFrameworkRepository()); if (value != null) { String v = value.getValue(); if (v != null) { ResourceUrl url = ResourceUrl.parse(v); if (url != null) { return getMatchingFile(url.name, url.type, config); } else { // Looks like the resource value is pointing to a file // It's most likely one of the source files for this // resource item, so check those first for (ResourceFile f : files) { if (v.equals(f.getFile().getOsLocation())) { // Found the file return f; } } // No; look up the resource file from the full path File file = new File(v); if (file.exists()) { ResourceFile f = findResourceFile(file); if (f != null) { return f; } } } } } } else if (files.size() == 1) { // Single file: see if it matches ResourceFile matchingFile = files.get(0); if (matchingFile.getFolder().getConfiguration().isMatchFor(config)) { return matchingFile; } } } } } return null; }
/** * 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 Map<String, ResourceValue> 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 new HashMap<String, ResourceValue>(); } // create the map HashMap<String, ResourceValue> map = new HashMap<String, ResourceValue>(items.size()); for (ResourceItem item : items.values()) { ResourceValue value = item.getResourceValue(type, referenceConfig, isFrameworkRepository()); if (value != null) { map.put(item.getName(), value); } } return map; }