use of org.robolectric.res.TypedResource in project robolectric by robolectric.
the class ShadowAssetManager method resolveStyle.
private Style resolveStyle(@NotNull ResName themeStyleName, Style themeStyleSet) {
TypedResource themeStyleResource = resourceTable.getValue(themeStyleName, RuntimeEnvironment.getQualifiers());
if (themeStyleResource == null)
return null;
StyleData themeStyleData = (StyleData) themeStyleResource.getData();
if (themeStyleSet == null) {
themeStyleSet = new ThemeStyleSet();
}
return new StyleResolver(resourceTable, shadowOf(AssetManager.getSystem()).getResourceTable(), themeStyleData, themeStyleSet, themeStyleName, RuntimeEnvironment.getQualifiers());
}
use of org.robolectric.res.TypedResource in project robolectric by robolectric.
the class ShadowAssetManager method getTypedArray.
private TypedArray getTypedArray(Resources resources, TypedResource[] typedResources, int resId) {
final CharSequence[] stringData = new CharSequence[typedResources.length];
final int totalLen = typedResources.length * ShadowAssetManager.STYLE_NUM_ENTRIES;
final int[] data = new int[totalLen];
for (int i = 0; i < typedResources.length; i++) {
final int offset = i * ShadowAssetManager.STYLE_NUM_ENTRIES;
TypedResource typedResource = typedResources[i];
// Classify the item.
int type = getResourceType(typedResource);
if (type == -1) {
// This type is unsupported; leave empty.
continue;
}
final TypedValue typedValue = new TypedValue();
if (type == TypedValue.TYPE_REFERENCE) {
final String reference = typedResource.asString();
ResName refResName = AttributeResource.getResourceReference(reference, typedResource.getXmlContext().getPackageName(), null);
typedValue.resourceId = resourceTable.getResourceId(refResName);
typedValue.data = typedValue.resourceId;
typedResource = resolve(typedResource, RuntimeEnvironment.getQualifiers(), typedValue.resourceId);
if (typedResource != null) {
// Reclassify to a non-reference type.
type = getResourceType(typedResource);
if (type == TypedValue.TYPE_ATTRIBUTE) {
type = TypedValue.TYPE_REFERENCE;
} else if (type == -1) {
// This type is unsupported; leave empty.
continue;
}
}
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
final String reference = typedResource.asString();
final ResName attrResName = AttributeResource.getStyleReference(reference, typedResource.getXmlContext().getPackageName(), "attr");
typedValue.data = resourceTable.getResourceId(attrResName);
}
if (typedResource != null && type != TypedValue.TYPE_NULL && type != TypedValue.TYPE_ATTRIBUTE) {
getConverter(typedResource).fillTypedValue(typedResource.getData(), typedValue);
}
data[offset + ShadowAssetManager.STYLE_TYPE] = type;
data[offset + ShadowAssetManager.STYLE_RESOURCE_ID] = typedValue.resourceId;
data[offset + ShadowAssetManager.STYLE_DATA] = typedValue.data;
data[offset + ShadowAssetManager.STYLE_ASSET_COOKIE] = typedValue.assetCookie;
data[offset + ShadowAssetManager.STYLE_CHANGING_CONFIGURATIONS] = typedValue.changingConfigurations;
data[offset + ShadowAssetManager.STYLE_DENSITY] = typedValue.density;
stringData[i] = typedResource == null ? null : typedResource.asString();
}
int[] indices = new int[typedResources.length + 1];
/* keep zeroed out */
return ShadowTypedArray.create(resources, null, data, indices, typedResources.length, stringData);
}
use of org.robolectric.res.TypedResource in project robolectric by robolectric.
the class ShadowAssetManager method getResourceValue.
@HiddenApi
@Implementation
public boolean getResourceValue(int ident, int density, TypedValue outValue, boolean resolveRefs) {
TypedResource value = getAndResolve(ident, RuntimeEnvironment.getQualifiers(), resolveRefs);
if (value == null)
return false;
getConverter(value).fillTypedValue(value.getData(), outValue);
return true;
}
use of org.robolectric.res.TypedResource in project robolectric by robolectric.
the class ShadowAssetManager method getTypedArrayResource.
protected TypedArray getTypedArrayResource(Resources resources, int resId) {
TypedResource value = getAndResolve(resId, RuntimeEnvironment.getQualifiers(), true);
if (value == null) {
return null;
}
TypedResource[] items = getConverter(value).getItems(value);
return getTypedArray(resources, items, resId);
}
use of org.robolectric.res.TypedResource in project robolectric by robolectric.
the class ShadowResourcesImpl method getQuantityString.
@Implementation
public String getQuantityString(int resId, int quantity) throws Resources.NotFoundException {
ShadowAssetManager shadowAssetManager = shadowOf(realResourcesImpl.getAssets());
TypedResource typedResource = shadowAssetManager.getResourceTable().getValue(resId, RuntimeEnvironment.getQualifiers());
if (typedResource != null && typedResource instanceof PluralResourceLoader.PluralRules) {
PluralResourceLoader.PluralRules pluralRules = (PluralResourceLoader.PluralRules) typedResource;
Plural plural = pluralRules.find(quantity);
if (plural == null) {
return null;
}
TypedResource<?> resolvedTypedResource = shadowAssetManager.resolve(new TypedResource<>(plural.getString(), ResType.CHAR_SEQUENCE, pluralRules.getXmlContext()), RuntimeEnvironment.getQualifiers(), resId);
return resolvedTypedResource == null ? null : resolvedTypedResource.asString();
} else {
return null;
}
}
Aggregations