cesards
2/22/2015 - 2:17 AM

ResourcesAdditions.java

<?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>
<?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>
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"));
    }
}
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;
        }
    }

}