use of org.robolectric.res.ResName in project robolectric by robolectric.
the class ShadowAssetManager method openNonAsset.
@HiddenApi
@Implementation
public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException {
final ResName resName = qualifyFromNonAssetFileName(fileName);
final FileTypedResource typedResource = (FileTypedResource) resourceTable.getValue(resName, RuntimeEnvironment.getQualifiers());
if (typedResource == null) {
throw new IOException("Unable to find resource for " + fileName);
}
if (accessMode == AssetManager.ACCESS_STREAMING) {
return typedResource.getFsFile().getInputStream();
} else {
return new ByteArrayInputStream(typedResource.getFsFile().getBytes());
}
}
use of org.robolectric.res.ResName in project robolectric by robolectric.
the class ResourceLoaderTest method shouldMakeInternalResourcesAvailable.
@Test
public void shouldMakeInternalResourcesAvailable() throws Exception {
ResourceTable resourceProvider = RuntimeEnvironment.getSystemResourceTable();
ResName internalResource = new ResName("android", "string", "badPin");
Integer resId = resourceProvider.getResourceId(internalResource);
assertThat(resId).isNotNull();
assertThat(resourceProvider.getResName(resId)).isEqualTo(internalResource);
Class<?> internalRIdClass = Robolectric.class.getClassLoader().loadClass("com.android.internal.R$" + internalResource.type);
int internalResourceId;
internalResourceId = (Integer) internalRIdClass.getDeclaredField(internalResource.name).get(null);
assertThat(resId).isEqualTo(internalResourceId);
assertThat(RuntimeEnvironment.application.getResources().getString(resId)).isEqualTo("The old PIN you typed isn't correct.");
}
use of org.robolectric.res.ResName in project robolectric by robolectric.
the class XmlResourceParserImpl method getResourceId.
private int getResourceId(String possiblyQualifiedResourceName, String defaultPackageName, String defaultType) {
if (AttributeResource.isNull(possiblyQualifiedResourceName))
return 0;
if (AttributeResource.isStyleReference(possiblyQualifiedResourceName)) {
ResName styleReference = AttributeResource.getStyleReference(possiblyQualifiedResourceName, defaultPackageName, "attr");
Integer resourceId = resourceTable.getResourceId(styleReference);
if (resourceId == null) {
throw new Resources.NotFoundException(styleReference.getFullyQualifiedName());
}
return resourceId;
}
if (AttributeResource.isResourceReference(possiblyQualifiedResourceName)) {
ResName resourceReference = AttributeResource.getResourceReference(possiblyQualifiedResourceName, defaultPackageName, defaultType);
Integer resourceId = resourceTable.getResourceId(resourceReference);
if (resourceId == null) {
throw new Resources.NotFoundException(resourceReference.getFullyQualifiedName());
}
return resourceId;
}
possiblyQualifiedResourceName = removeLeadingSpecialCharsIfAny(possiblyQualifiedResourceName);
ResName resName = ResName.qualifyResName(possiblyQualifiedResourceName, defaultPackageName, defaultType);
Integer resourceId = resourceTable.getResourceId(resName);
return resourceId == null ? 0 : resourceId;
}
use of org.robolectric.res.ResName in project robolectric by robolectric.
the class ShadowAssetManager method getThemeValue.
@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, RuntimeEnvironment.getQualifiers(), resolveRefs);
return true;
}
return false;
}
use of org.robolectric.res.ResName in project robolectric by robolectric.
the class ShadowAssetManager method convertAndFill.
private void convertAndFill(AttributeResource attribute, TypedValue outValue, String qualifiers, boolean resolveRefs) {
if (attribute.isNull()) {
outValue.type = TypedValue.TYPE_NULL;
outValue.data = TypedValue.DATA_NULL_UNDEFINED;
return;
} else if (attribute.isEmpty()) {
outValue.type = TypedValue.TYPE_NULL;
outValue.data = TypedValue.DATA_NULL_EMPTY;
return;
}
// short-circuit Android caching of loaded resources cuz our string positions don't remain stable...
outValue.assetCookie = Converter.getNextStringCookie();
// TODO: Handle resource and style references
if (attribute.isStyleReference()) {
return;
}
while (attribute.isResourceReference()) {
Integer resourceId;
ResName resName = attribute.getResourceReference();
if (attribute.getReferenceResId() != null) {
resourceId = attribute.getReferenceResId();
} else {
resourceId = resourceTable.getResourceId(resName);
}
if (resourceId == null) {
throw new Resources.NotFoundException("unknown resource " + resName);
}
outValue.type = TypedValue.TYPE_REFERENCE;
if (!resolveRefs) {
// Just return the resourceId if resolveRefs is false.
outValue.data = resourceId;
return;
}
outValue.resourceId = resourceId;
TypedResource dereferencedRef = resourceTable.getValue(resName, qualifiers);
if (dereferencedRef == null) {
Logger.strict("couldn't resolve %s from %s", resName.getFullyQualifiedName(), attribute);
if (resName.type.equals("id")) {
return;
} else if (resName.type.equals("layout")) {
// resourceId is good enough, right?
return;
} else if (resName.type.equals("dimen")) {
return;
} else if (resName.type.equals("transition")) {
return;
} else if (resName.type.equals("interpolator")) {
return;
} else if (resName.type.equals("menu")) {
return;
} else if (resName.type.equals("raw")) {
return;
} else if (DrawableResourceLoader.isStillHandledHere(resName.type)) {
// wtf. color and drawable references reference are all kinds of stupid.
TypedResource drawableResource = resourceTable.getValue(resName, qualifiers);
if (drawableResource == null) {
throw new Resources.NotFoundException("can't find file for " + resName);
} else {
outValue.type = TypedValue.TYPE_STRING;
outValue.data = 0;
outValue.assetCookie = Converter.getNextStringCookie();
outValue.string = (CharSequence) drawableResource.getData();
return;
}
} else {
throw new RuntimeException("huh? " + resName);
}
} else {
if (dereferencedRef.isFile()) {
outValue.type = TypedValue.TYPE_STRING;
outValue.data = 0;
outValue.assetCookie = Converter.getNextStringCookie();
outValue.string = dereferencedRef.asString();
return;
} else if (dereferencedRef.getData() instanceof String) {
attribute = new AttributeResource(attribute.resName, dereferencedRef.asString(), resName.packageName);
if (attribute.isResourceReference()) {
continue;
}
if (resolveRefs) {
Converter.getConverter(dereferencedRef.getResType()).fillTypedValue(attribute.value, outValue);
return;
}
}
}
break;
}
if (attribute.isNull()) {
outValue.type = TypedValue.TYPE_NULL;
return;
}
TypedResource attrTypeData = resourceTable.getValue(attribute.resName, qualifiers);
if (attrTypeData != null) {
AttrData attrData = (AttrData) attrTypeData.getData();
String format = attrData.getFormat();
String[] types = format.split("\\|");
for (String type : types) {
// already handled above
if ("reference".equals(type))
continue;
Converter converter = Converter.getConverterFor(attrData, type);
if (converter != null) {
if (converter.fillTypedValue(attribute.value, outValue)) {
return;
}
}
}
} else {
/**
* In cases where the runtime framework doesn't know this attribute, e.g: viewportHeight (added in 21) on a
* KitKat runtine, then infer the attribute type from the value.
*
* TODO: When we are able to pass the SDK resources from the build environment then we can remove this
* and replace the NullResourceLoader with simple ResourceProvider that only parses attribute type information.
*/
ResType resType = ResType.inferFromValue(attribute.value);
Converter.getConverter(resType).fillTypedValue(attribute.value, outValue);
}
}
Aggregations