diff --git a/api/current.txt b/api/current.txt index 4c56ac98..e37144c1 100644 --- a/api/current.txt +++ b/api/current.txt @@ -312,9 +312,12 @@ package androidx.core.net { public final class UriKt { ctor public UriKt(); + method public static android.net.Uri.Builder appendId(android.net.Uri.Builder, long id); + method public static long parseId(android.net.Uri); method public static java.io.File toFile(android.net.Uri); method public static android.net.Uri toUri(String); method public static android.net.Uri toUri(java.io.File); + method public static android.net.Uri withAppendedId(android.net.Uri, long id); } } diff --git a/src/androidTest/java/androidx/core/net/UriTest.kt b/src/androidTest/java/androidx/core/net/UriTest.kt index d25de356..f416a69f 100644 --- a/src/androidTest/java/androidx/core/net/UriTest.kt +++ b/src/androidTest/java/androidx/core/net/UriTest.kt @@ -16,6 +16,7 @@ package androidx.core.net +import android.content.ContentUris import android.net.Uri import org.junit.Assert.assertEquals import org.junit.Test @@ -36,4 +37,19 @@ class UriTest { val uri = Uri.parse("path/to/my/file") assertEquals(File(uri.path), uri.toFile()) } + + @Test fun appendId() { + val uriBuilder = Uri.parse("content://authority/path").buildUpon() + assertEquals(uriBuilder.appendId(4).build(), ContentUris.appendId(uriBuilder, 4).build()) + } + + @Test fun withAppendedId() { + val uri = Uri.parse("content://authority/path") + assertEquals(uri.withAppendedId(4), ContentUris.withAppendedId(uri, 4)) + } + + @Test fun parseId() { + val uri = Uri.parse("content://authority/path/4") + assertEquals(uri.parseId(), ContentUris.parseId(uri)) + } } diff --git a/src/main/java/androidx/core/net/Uri.kt b/src/main/java/androidx/core/net/Uri.kt index a5d9f1dd..b4e56415 100644 --- a/src/main/java/androidx/core/net/Uri.kt +++ b/src/main/java/androidx/core/net/Uri.kt @@ -18,6 +18,7 @@ package androidx.core.net +import android.content.ContentUris import android.net.Uri import java.io.File @@ -37,3 +38,35 @@ inline fun File.toUri(): Uri = Uri.fromFile(this) /** Creates a [File] from the given [Uri]. */ inline fun Uri.toFile(): File = File(path) + +/** + * Appends the given ID to the end of the path. + * + * @param id to append + * + * @return the builder + */ +inline fun Uri.Builder.appendId(id: Long): Uri.Builder = ContentUris.appendId(this, id) + +/** + * Appends the given ID to the end of the path. + * + * @param id to append + * + * @return a new URI with the given ID appended to the end of the path + */ +inline fun Uri.withAppendedId(id: Long): Uri = ContentUris.withAppendedId(this, id) + +/** + * Converts the last path segment to a long. + * + *
This supports a common convention for content URIs where an ID is + * stored in the last segment. + * + * @throws UnsupportedOperationException if this isn't a hierarchical URI + * @throws NumberFormatException if the last segment isn't a number + * + * @return the long conversion of the last segment or -1 if the path is + * empty + */ +inline fun Uri.parseId(): Long = ContentUris.parseId(this)