If you think the Android project roboject listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
/*www.java2s.com*/
This file is part of Roboject
Copyright (c) 2010-2011 akquinet A.G.
Contact: http://www.akquinet.de/en
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/package de.akquinet.android.roboject.util;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Movie;
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.lang.reflect.Field;
publicclass AndroidUtil {
/**
* Check application's R.id class and retrieve the value of the static
* member with the given name.
*/publicstaticint getIdentifierFromR(Context context, String type, String name) {
Class<?> rClass = null;
try {
rClass = Class.forName(context.getPackageName() + ".R$" + type);
} catch (ClassNotFoundException e) {
// No R.id class? This should never happen.
thrownew RuntimeException(e);
}
try {
Field rField = rClass.getField(name);
Object intValue;
try {
intValue = rField.get(null);
} catch (IllegalAccessException e) {
thrownew RuntimeException(e);
}
if (!(intValue instanceofInteger)) {
thrownew RuntimeException("Not an int: "
+ rClass.getCanonicalName() + "." + rField.getName());
}
return ((Integer) intValue).intValue();
} catch (NoSuchFieldException e) {
thrownew RuntimeException("There is no such id in the R class: "
+ context.getPackageName() + ".R." + type + "." + name + ")");
} catch (SecurityException e) {
thrownew RuntimeException(e);
}
}
publicstatic String getTypeForField(Field field) {
Class type = field.getType();
// Animation
if (type.isAssignableFrom(Animation.class))
return"anim";
// Color State List
elseif (type.isAssignableFrom(ColorStateList.class))
return"color";
// Drawable
elseif (type.isAssignableFrom(Drawable.class))
return"drawable";
// String
elseif (type.isAssignableFrom(String.class))
return"string";
// String Array
elseif (type.isArray())
return"array";
// TODO: Recognize plural strings if possible
// else if (type.isAssignableFrom(String.class))
// return "plural";
// Boolean
elseif (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
return"bool";
// Integer
elseif (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class))
return"integer";
// Dimen
elseif (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
return"dimen";
thrownew RuntimeException("No suitable type found for " + field.getName() + "of class " + type.getName());
}
publicstatic Object getResource(Context context, Field field, int value) {
Resources resources = context.getResources();
Class type = field.getType();
if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
return resources.getBoolean(value);
elseif (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
return resources.getInteger(value);
} elseif (type.isAssignableFrom(ColorStateList.class))
return resources.getColorStateList(value);
elseif (type.isAssignableFrom(XmlResourceParser.class))
return resources.getXml(value);
elseif (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
return resources.getDimension(value);
elseif (type.isAssignableFrom(Drawable.class))
return resources.getDrawable(value);
elseif (type.isAssignableFrom(Animation.class))
return AnimationUtils.loadAnimation(context, value);
elseif (type.isAssignableFrom(Movie.class))
return resources.getMovie(value);
elseif (type.isAssignableFrom(String.class))
return resources.getString(value);
elseif (type.isArray()) {
if (type.getName().equals("[I")) {
return resources.getIntArray(value);
} elseif (type.isAssignableFrom(String[].class)) {
return resources.getStringArray(value);
}
}
return null;
}
}