diff --git a/api/current.txt b/api/current.txt index e805fde9..0131cc6a 100644 --- a/api/current.txt +++ b/api/current.txt @@ -23,6 +23,9 @@ package androidx.core.content { public final class ContextKt { ctor public ContextKt(); + method public static boolean getBoolean(android.content.Context, @BoolRes int id) throws android.content.res.Resources.NotFoundException; + method public static float getDimension(android.content.Context, @DimenRes int id) throws android.content.res.Resources.NotFoundException; + method public static int getInteger(android.content.Context, @IntegerRes int id) throws android.content.res.Resources.NotFoundException; method public static void withStyledAttributes(android.content.Context, android.util.AttributeSet? set = "null", int[] attrs, @AttrRes int defStyleAttr = "0", @StyleRes int defStyleRes = "0", kotlin.jvm.functions.Function1 block); method public static void withStyledAttributes(android.content.Context, @StyleRes int resourceId, int[] attrs, kotlin.jvm.functions.Function1 block); } diff --git a/src/androidTest/java/androidx/core/content/ContextTest.kt b/src/androidTest/java/androidx/core/content/ContextTest.kt index a031b576..3f3ea872 100644 --- a/src/androidTest/java/androidx/core/content/ContextTest.kt +++ b/src/androidTest/java/androidx/core/content/ContextTest.kt @@ -16,13 +16,16 @@ package androidx.core.content +import android.content.res.Resources import android.support.test.InstrumentationRegistry import android.support.test.filters.SdkSuppress import android.test.mock.MockContext import androidx.core.kotlin.test.R +import androidx.testutils.assertThrows import androidx.testutils.getAttributeSet import org.junit.Assert.assertEquals import org.junit.Assert.assertSame +import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test @@ -69,5 +72,22 @@ class ContextTest { context.withStyledAttributes(attrs, R.styleable.SampleAttrs, 0, 0) { assertTrue(getInt(R.styleable.SampleAttrs_sample, -1) != -1) } + + assertEquals(context.getInteger(R.integer.integer_25), 25) + assertThrows< Resources.NotFoundException> { + context.getInteger(-1) + } + + assertTrue(context.getBoolean(R.bool.bool_true)) + assertFalse(context.getBoolean(R.bool.bool_false)) + assertThrows< Resources.NotFoundException> { + context.getBoolean(-1) + } + + assertEquals(context.getDimension(R.dimen.dimen_25), + 25f * context.resources.displayMetrics.density) + assertThrows< Resources.NotFoundException> { + context.getDimension(-1) + } } } diff --git a/src/androidTest/res/values/booleans.xml b/src/androidTest/res/values/booleans.xml new file mode 100644 index 00000000..04b33f5d --- /dev/null +++ b/src/androidTest/res/values/booleans.xml @@ -0,0 +1,21 @@ + + + + + true + false + diff --git a/src/androidTest/res/values/dimens.xml b/src/androidTest/res/values/dimens.xml new file mode 100644 index 00000000..ce82fae9 --- /dev/null +++ b/src/androidTest/res/values/dimens.xml @@ -0,0 +1,20 @@ + + + + + 25dp + diff --git a/src/androidTest/res/values/integers.xml b/src/androidTest/res/values/integers.xml new file mode 100644 index 00000000..cea013b4 --- /dev/null +++ b/src/androidTest/res/values/integers.xml @@ -0,0 +1,20 @@ + + + + + 25 + diff --git a/src/main/java/androidx/core/content/Context.kt b/src/main/java/androidx/core/content/Context.kt index 488d5652..4a1867fb 100644 --- a/src/main/java/androidx/core/content/Context.kt +++ b/src/main/java/androidx/core/content/Context.kt @@ -14,11 +14,17 @@ * limitations under the License. */ +@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. + package androidx.core.content import android.content.Context +import android.content.res.Resources import android.content.res.TypedArray import android.support.annotation.AttrRes +import android.support.annotation.BoolRes +import android.support.annotation.DimenRes +import android.support.annotation.IntegerRes import android.support.annotation.RequiresApi import android.support.annotation.StyleRes import android.util.AttributeSet @@ -91,3 +97,53 @@ inline fun Context.withStyledAttributes( typedArray.recycle() } } + +/** + * Return a boolean associated with a particular resource ID. This can be + * used with any integral resource value, and will return true if it is + * non-zero. + * + * @param id The desired resource identifier, as generated by the aapt + * tool. This integer encodes the package, type, and resource + * entry. The value 0 is an invalid identifier. + * + * @throws android.content.res.Resources.NotFoundException Throws NotFoundException if the given ID does not exist. + * + * @return Returns the boolean value contained in the resource. + */ +@Throws(Resources.NotFoundException::class) +inline fun Context.getBoolean(@BoolRes id: Int) = resources.getBoolean(id) + +/** + * Return an integer associated with a particular resource ID. + * + * @param id The desired resource identifier, as generated by the aapt + * tool. This integer encodes the package, type, and resource + * entry. The value 0 is an invalid identifier. + * + * @throws android.content.res.Resources.NotFoundException Throws NotFoundException if the given ID does not exist. + * + * @return Returns the integer value contained in the resource. + */ +@Throws(Resources.NotFoundException::class) +inline fun Context.getInteger(@IntegerRes id: Int) = resources.getInteger(id) + +/** + * Retrieve a dimensional for a particular resource ID. Unit + * conversions are based on the current {@link DisplayMetrics} associated + * with the resources. + * + * @param id The desired resource identifier, as generated by the aapt + * tool. This integer encodes the package, type, and resource + * entry. The value 0 is an invalid identifier. + * + * @return Resource dimension value multiplied by the appropriate + * metric. + * + * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. + * + * @see android.content.res.Resources.getDimensionPixelOffset + * @see android.content.res.Resources.getDimensionPixelSize + */ +@Throws(Resources.NotFoundException::class) +inline fun Context.getDimension(@DimenRes id: Int) = resources.getDimension(id) \ No newline at end of file