Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Add context extensions for getBoolean, getInteger, getDimension. #434

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<? super android.content.res.TypedArray,kotlin.Unit> block);
method public static void withStyledAttributes(android.content.Context, @StyleRes int resourceId, int[] attrs, kotlin.jvm.functions.Function1<? super android.content.res.TypedArray,kotlin.Unit> block);
}
Expand Down
20 changes: 20 additions & 0 deletions src/androidTest/java/androidx/core/content/ContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}
}
21 changes: 21 additions & 0 deletions src/androidTest/res/values/booleans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2018 The Android Open Source Project
~
~ 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.
-->

<resources>
<bool name="bool_true">true</bool>
<bool name="bool_false">false</bool>
</resources>
20 changes: 20 additions & 0 deletions src/androidTest/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2018 The Android Open Source Project
~
~ 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.
-->

<resources>
<dimen name="dimen_25">25dp</dimen>
</resources>
20 changes: 20 additions & 0 deletions src/androidTest/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2018 The Android Open Source Project
~
~ 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.
-->

<resources>
<integer name="integer_25">25</integer>
</resources>
56 changes: 56 additions & 0 deletions src/main/java/androidx/core/content/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)