Last active
January 12, 2024 17:55
-
-
Save cyrilmottier/8179328 to your computer and use it in GitHub Desktop.
Lightweight key-value pairs resources for Android applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Great work !