Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
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
1 change: 1 addition & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package androidx.core.content {

public final class ContextKt {
ctor public ContextKt();
method public static android.content.ComponentName componentName(android.content.Context, String className);
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
16 changes: 16 additions & 0 deletions src/androidTest/java/androidx/core/content/ContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package androidx.core.content

import android.content.ComponentName
import android.content.ContextWrapper
import android.support.test.InstrumentationRegistry
import android.support.test.filters.SdkSuppress
import androidx.core.TestActivity
import androidx.core.ktx.test.R
import androidx.core.getAttributeSet
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -70,4 +72,18 @@ class ContextTest {
assertTrue(getInt(R.styleable.SampleAttrs_sample, -1) != -1)
}
}

@Test fun componentName() {
val componentName = ComponentName(context, TestActivity::class.java)
context.componentName<TestActivity>().also {
assertEquals(componentName, it)
}
}

@Test fun componentNameViaClassName() {
val componentName = ComponentName(context, TestActivity::class.java)
context.componentName("androidx.core.TestActivity").also {
assertEquals(componentName, it)
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/androidx/core/content/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package androidx.core.content

import android.content.ComponentName
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
Expand Down Expand Up @@ -91,3 +92,9 @@ inline fun Context.withStyledAttributes(
typedArray.recycle()
}
}

/** Creates a [ComponentName] from the given [T]. */
inline fun <reified T> Context.componentName() = ComponentName(this, T::class.java)

/** Creates a [ComponentName] from the given [String]. */
inline fun Context.componentName(className: String) = ComponentName(this, className)