org.robolectric.shadows.Converter Maven / Gradle / Ivy
package org.robolectric.shadows;
import android.content.res.Resources;
import android.util.TypedValue;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.robolectric.res.AttrData;
import org.robolectric.res.ResType;
import org.robolectric.res.TypedResource;
import org.robolectric.util.Util;
@SuppressWarnings("NewApi")
public class Converter {
private static int nextStringCookie = 0xbaaa5;
synchronized static int getNextStringCookie() {
return nextStringCookie++;
}
static Converter getConverterFor(AttrData attrData, String type) {
switch (type) {
case "enum":
return new EnumConverter(attrData);
case "flag":
case "flags": // because {@link ResourceTable#gFormatFlags} uses "flags"
return new FlagConverter(attrData);
case "boolean":
return new FromBoolean();
case "color":
return new FromColor();
case "dimension":
return new FromDimen();
case "float":
return new FromFloat();
case "integer":
return new FromInt();
case "string":
return new FromCharSequence();
case "fraction":
return new FromFraction();
default:
throw new UnsupportedOperationException("Type not supported: " + type);
}
}
// TODO: Handle 'anim' resources
public static Converter getConverter(ResType resType) {
switch (resType) {
case ATTR_DATA:
return new FromAttrData();
case BOOLEAN:
return new FromBoolean();
case CHAR_SEQUENCE:
return new FromCharSequence();
case COLOR:
case DRAWABLE:
return new FromColor();
case COLOR_STATE_LIST:
case LAYOUT:
return new FromFilePath();
case DIMEN:
return new FromDimen();
case FILE:
return new FromFile();
case FLOAT:
return new FromFloat();
case INTEGER:
return new FromInt();
case FRACTION:
return new FromFraction();
case CHAR_SEQUENCE_ARRAY:
case INTEGER_ARRAY:
case TYPED_ARRAY:
return new FromArray();
case STYLE:
return new Converter();
default:
throw new UnsupportedOperationException("can't convert from " + resType.name());
}
}
public CharSequence asCharSequence(TypedResource typedResource) {
return typedResource.asString();
}
public int asInt(TypedResource typedResource) {
throw cantDo("asInt");
}
public List getItems(TypedResource typedResource) {
return new ArrayList<>();
}
public boolean fillTypedValue(T data, TypedValue typedValue) {
return false;
}
private UnsupportedOperationException cantDo(String operation) {
return new UnsupportedOperationException(getClass().getName() + " doesn't support " + operation);
}
public static class FromAttrData extends Converter {
@Override
public CharSequence asCharSequence(TypedResource typedResource) {
return typedResource.asString();
}
@Override
public boolean fillTypedValue(AttrData data, TypedValue typedValue) {
typedValue.type = TypedValue.TYPE_STRING;
return false;
}
}
public static class FromCharSequence extends Converter {
@Override
public CharSequence asCharSequence(TypedResource typedResource) {
return typedResource.asString().trim();
}
@Override
public int asInt(TypedResource typedResource) {
return convertInt(typedResource.asString().trim());
}
@Override
public boolean fillTypedValue(String data, TypedValue typedValue) {
typedValue.type = TypedValue.TYPE_STRING;
typedValue.data = 0;
typedValue.assetCookie = getNextStringCookie();
typedValue.string = data;
return true;
}
}
public static class FromColor extends Converter {
@Override
public boolean fillTypedValue(String data, TypedValue typedValue) {
try {
typedValue.type = ResourceHelper.getColorType(data);
typedValue.data = ResourceHelper.getColor(data);
typedValue.assetCookie = 0;
typedValue.string = null;
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public int asInt(TypedResource typedResource) {
return ResourceHelper.getColor(typedResource.asString().trim());
}
}
public static class FromFilePath extends Converter {
@Override
public boolean fillTypedValue(String data, TypedValue typedValue) {
typedValue.type = TypedValue.TYPE_STRING;
typedValue.data = 0;
typedValue.string = data;
typedValue.assetCookie = getNextStringCookie();
return true;
}
}
public static class FromArray extends Converter {
@Override
public List getItems(TypedResource typedResource) {
return (List) typedResource.getData();
}
}
private static class FromInt extends Converter {
@Override
public boolean fillTypedValue(String data, TypedValue typedValue) {
try {
if (data.startsWith("0x")) {
typedValue.type = data.startsWith("0x") ? TypedValue.TYPE_INT_HEX : TypedValue.TYPE_INT_DEC;
} else {
typedValue.type = TypedValue.TYPE_INT_DEC;
}
typedValue.data = convertInt(data);
typedValue.assetCookie = 0;
typedValue.string = null;
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public int asInt(TypedResource typedResource) {
return convertInt(typedResource.asString().trim());
}
}
private static class FromFraction extends Converter {
@Override
public boolean fillTypedValue(String data, TypedValue typedValue) {
return ResourceHelper.parseFloatAttribute(null, data, typedValue, false);
}
}
private static class FromFile extends Converter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy