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
3 changes: 3 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Expand Down
16 changes: 16 additions & 0 deletions src/androidTest/java/androidx/core/net/UriTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
33 changes: 33 additions & 0 deletions src/main/java/androidx/core/net/Uri.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package androidx.core.net

import android.content.ContentUris
import android.net.Uri
import java.io.File

Expand All @@ -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.
*
* <p>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)