Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Forked from cyrilmottier/ResourcesAdditions.java
Created February 13, 2014 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcxiaoke/8970677 to your computer and use it in GitHub Desktop.
Save mcxiaoke/8970677 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<units xmlns:android="http://schemas.android.com/apk/res/android">
<extra
android:name="mega"
android:value="1000000" />
<extra
android:name="kilo"
android:value="1000" />
<extra
android:name="hecto"
android:value="100" />
<extra
android:name="deca"
android:value="10" />
</units>
<?xml version="1.0" encoding="utf-8"?>
<extras xmlns:android="http://schemas.android.com/apk/res/android">
<extra
android:name="mega"
android:value="1000000" />
<extra
android:name="kilo"
android:value="1000" />
<extra
android:name="hecto"
android:value="100" />
<extra
android:name="deca"
android:value="10" />
</extras>
package com.cyrilmottier.android.resourcesadditions;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* @author Cyril Mottier
*/
public final class ResourcesAdditions {
public static final String TAG_EXTRAS = "extras";
private ResourcesAdditions() {
// No instance
}
public static Bundle getResourcesExtras(Resources res, int resId) throws Resources.NotFoundException {
return getResourcesExtras(res, TAG_EXTRAS, resId);
}
public static Bundle getResourcesExtras(Resources res, String rootTag,
int resId) throws Resources.NotFoundException {
XmlResourceParser parser = res.getXml(resId);
int type;
try {
while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
// Empty loop
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
if (!parser.getName().equals(rootTag)) {
throw new XmlPullParserException("Unknown start tag. Should be '" + rootTag + "'");
}
final Bundle extras = new Bundle();
res.parseBundleExtras(parser, extras);
return extras;
} catch (Exception e) {
final Resources.NotFoundException nfe = new Resources.NotFoundException();
nfe.initCause(e);
throw nfe;
}
}
}
package com.cyrilmottier.android.resourcesadditions;
import android.content.res.Resources;
import android.os.Bundle;
import android.test.InstrumentationTestCase;
/**
* @author Cyril Mottier
*/
public final class ResourcesAdditionsTest extends InstrumentationTestCase {
private Resources mResources;
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = getInstrumentation().getContext().getResources();
}
public void testDefaultRoot() {
Bundle bundle = ResourcesAdditions.getResourcesExtras(mResources, R.xml.extras);
assertEquals(4, bundle.size());
assertEquals(10, bundle.getInt("deca"));
assertEquals(100, bundle.getInt("hecto"));
assertEquals(1000, bundle.getInt("kilo"));
assertEquals(1000000, bundle.getInt("mega"));
}
public void testDedicatedRoot() {
Bundle bundle = ResourcesAdditions.getResourcesExtras(mResources, "units", R.xml.units);
assertEquals(4, bundle.size());
assertEquals(10, bundle.getInt("deca"));
assertEquals(100, bundle.getInt("hecto"));
assertEquals(1000, bundle.getInt("kilo"));
assertEquals(1000000, bundle.getInt("mega"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment