public ResName withPackageName(String packageName) { if (packageName.equals(this.packageName)) return this; return new ResName(packageName, type, name); }
/** * @return null if the resource could not be qualified. */ public static String qualifyResourceName(@Nonnull String possiblyQualifiedResourceName, String defaultPackageName, String defaultType) { ResName resName = qualifyResName(possiblyQualifiedResourceName, defaultPackageName, defaultType); return resName != null ? resName.getFullyQualifiedName() : null; }
void addResource(int resId, String type, String name) { if (ResourceIds.isFrameworkResource(resId)) { androidResourceIdGenerator.record(resId, type, name); } ResName resName = new ResName(packageName, type, name); int resIdPackageIdentifier = ResourceIds.getPackageIdentifier(resId); if (getPackageIdentifier() == 0) { this.packageIdentifier = resIdPackageIdentifier; } else if (getPackageIdentifier() != resIdPackageIdentifier) { throw new IllegalArgumentException("Incompatible package for " + packageName + ":" + type + "/" + name + " with resId " + resIdPackageIdentifier + " to ResourceIndex with packageIdentifier " + getPackageIdentifier()); } ResName existingEntry = resourceTable.put(resId, resName); if (existingEntry != null && !existingEntry.equals(resName)) { throw new IllegalArgumentException("ResId " + Integer.toHexString(resId) + " mapped to both " + resName + " and " + existingEntry); } }
private StyleData getParent(StyleData style) { String parent = getParentStyleName(style); if (parent == null) return null; if (parent.startsWith("@")) parent = parent.substring(1); ResName styleRef = ResName.qualifyResName(parent, style.getPackageName(), "style"); styleRef = dereferenceResName(styleRef); // TODO: Refactor this to a ResourceLoaderChooser ResourceTable resourceProvider = "android".equals(styleRef.packageName) ? systemResourceTable : appResourceTable; TypedResource typedResource = resourceProvider.getValue(styleRef, config); if (typedResource == null) { StringBuilder builder = new StringBuilder("Could not find any resource") .append(" from reference ").append(styleRef) .append(" from ").append(style) .append(" with ").append(theme); throw new RuntimeException(builder.toString()); } Object data = typedResource.getData(); if (data instanceof StyleData) { return (StyleData) data; } else { StringBuilder builder = new StringBuilder(styleRef.toString()) .append(" does not resolve to a Style.") .append(" got ").append(data).append(" instead. ") .append(" from ").append(style) .append(" with ").append(theme); throw new RuntimeException(builder.toString()); } }
@Test public void shouldQualifyResNameFromString() throws Exception { assertThat(ResName.qualifyResName("some.package:type/name", "default_package", "default_type")) .isEqualTo(new ResName("some.package", "type", "name")); assertThat(ResName.qualifyResName("some.package:name", "default_package", "default_type")) .isEqualTo(new ResName("some.package", "default_type", "name")); assertThat(ResName.qualifyResName("type/name", "default_package", "default_type")) .isEqualTo(new ResName("default_package", "type", "name")); assertThat(ResName.qualifyResName("name", "default_package", "default_type")) .isEqualTo(new ResName("default_package", "default_type", "name")); assertThat(ResName.qualifyResName("type/package:name", "default_package", "default_type")) .isEqualTo(new ResName("package", "type", "name")); }
@HiddenApi @Implementation(minSdk = LOLLIPOP) public boolean getThemeValue(long themePtr, int ident, TypedValue outValue, boolean resolveRefs) { ResName resName = resourceTable.getResName(ident); ThemeStyleSet themeStyleSet = getNativeTheme(themePtr).themeStyleSet; AttributeResource attrValue = themeStyleSet.getAttrValue(resName); while(attrValue != null && attrValue.isStyleReference()) { ResName attrResName = attrValue.getStyleReference(); if (attrValue.resName.equals(attrResName)) { Logger.info("huh... circular reference for %s?", attrResName.getFullyQualifiedName()); return false; } attrValue = themeStyleSet.getAttrValue(attrResName); } if (attrValue != null) { convertAndFill(attrValue, outValue, config, resolveRefs); return true; } return false; }
@Override public String toString() { return "ResName{" + getFullyQualifiedName() + "}"; }
public static ResName qualifyResName(@Nonnull String possiblyQualifiedResourceName, ResName defaults) { return qualifyResName(possiblyQualifiedResourceName, defaults.packageName, defaults.type); }
@Test public void qualifyFromFilePathShouldExtractResourceTypeAndNameFromQualifiedPath() { final ResName actual = ResName.qualifyFromFilePath("some.package", "./res/drawable-hdpi/icon.png"); assertThat(actual.getFullyQualifiedName()).isEqualTo("some.package:drawable/icon"); }
public static String qualifyResName(String possiblyQualifiedResourceName, String contextPackageName) { if (possiblyQualifiedResourceName == null) { return null; } if (AttributeResource.isNull(possiblyQualifiedResourceName)) { return null; } // Was not able to fully qualify the resource name String fullyQualifiedResourceName = qualifyResourceName(possiblyQualifiedResourceName, contextPackageName, null); if (fullyQualifiedResourceName == null) { return null; } return fullyQualifiedResourceName.replaceAll("[@+]", ""); }
@Override public boolean equals(Object obj) { if (!(obj instanceof StyleResolver)) { return false; } StyleResolver other = (StyleResolver) obj; return ((theme == null && other.theme == null) || (theme != null && theme.equals(other.theme))) && ((myResName == null && other.myResName == null) || (myResName != null && myResName.equals(other.myResName))) && Objects.equals(config, other.config); }
private ResName qualifyFromNonAssetFileName(String fileName) { // Resources from a jar belong to the "android" namespace, except when they come from "resource_files.zip" // when they are application resources produced by Bazel. if (fileName.startsWith("jar:") && !fileName.contains("resource_files.zip")) { // Must remove "jar:" prefix, or else qualifyFromFilePath fails on Windows return ResName.qualifyFromFilePath("android", fileName.replaceFirst("jar:", "")); } else { return ResName.qualifyFromFilePath(RuntimeEnvironment.application.getPackageName(), fileName); } }
public ResName(@Nonnull String packageName, @Nonnull String type, @Nonnull String name) { this.packageName = packageName; this.type = type.trim(); this.name = name.trim(); hashCode = computeHashCode(); }
if (attrValue == null) { throw new RuntimeException( "no value for " + styleAttributeResName.getFullyQualifiedName() + " in " + themeStyleSet); if (resName.type.equals("attr")) { System.out.println("WARN: " + resName.getFullyQualifiedName() + " should be a style resId"); while (attribute != null && attribute.isStyleReference()) { ResName otherAttrName = attribute.getStyleReference(); if (attribute.resName.equals(otherAttrName)) { Logger.info("huh... circular reference for %s?", attribute.resName.getFullyQualifiedName()); return null; strictError("no such attr %s in %s while resolving value for %s", attribute.value, themeStyleSet, resName.getFullyQualifiedName()); attribute = null; } else {
public void mustBe(String expectedType) { if (!type.equals(expectedType)) { throw new RuntimeException("expected " + getFullyQualifiedName() + " to be a " + expectedType + ", is a " + type); } }
public static ResName getStyleReference(String value, String defPackage, String defType) { if (!isStyleReference(value)) throw new IllegalArgumentException("not a style reference: " + value); return ResName.qualifyResName(value.substring(1), defPackage, defType); }
RuntimeEnvironment.getAppResourceTable() .getResourceId( ResName.qualifyResName( androidManifest.getThemeRef().replace("@", ""), pkg.packageName, "style")); if (androidManifest.getLabelRef() != null) { String fullyQualifiedName = ResName.qualifyResName(androidManifest.getLabelRef(), androidManifest.getPackageName()); Integer id = fullyQualifiedName == null ? null : RuntimeEnvironment.getAppResourceTable() .getResourceId(new ResName(fullyQualifiedName)); labelRes = id != null ? id : 0; RuntimeEnvironment.getAppResourceTable() .getResourceId( ResName.qualifyResName(themeRef.replace("@", ""), pkg.packageName, "style")); RuntimeEnvironment.getAppResourceTable() .getResourceId( ResName.qualifyResName( data.getLabel().replace("@", ""), pkg.packageName, "string")); if (activityInfo.labelRes == 0) {
@Test public void qualifyFromFilePathShouldExtractResourceTypeAndNameFromUnqualifiedPath() { final ResName actual = ResName.qualifyFromFilePath("some.package", "./res/drawable/icon.png"); assertThat(actual.getFullyQualifiedName()).isEqualTo("some.package:drawable/icon"); }
public String qualify(String value) { if (value == null) return null; if (AttributeResource.isResourceReference(value)) { return "@" + ResName.qualifyResourceName(value.trim().substring(1).replace("+", ""), packageName, "attr"); } else if (AttributeResource.isStyleReference(value)) { return "?" + ResName.qualifyResourceName(value.trim().substring(1), packageName, "attr"); } else { return StringResources.processStringResources(value); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof StyleResolver)) { return false; } StyleResolver other = (StyleResolver) obj; return ((theme == null && other.theme == null) || (theme != null && theme.equals(other.theme))) && ((myResName == null && other.myResName == null) || (myResName != null && myResName.equals(other.myResName))) && Objects.equals(config, other.config); }