Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,13 @@ abstract class DocumentFileCompat constructor(
fun isDocument(context: Context, uri: Uri): Boolean {
return isDocumentUri(context, uri)
}

/**
* Return whether the given [uri] is a tree uri.
*/
internal fun isTreeUri(uri: Uri): Boolean {
val paths = uri.pathSegments
return paths.size >= 2 && "tree" == paths[0]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lazygeniouz.dfc.file.internals

import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
import com.lazygeniouz.dfc.file.DocumentFileCompat
import com.lazygeniouz.dfc.resolver.ResolverCompat

Expand Down Expand Up @@ -96,6 +97,9 @@ internal class SingleDocumentFileCompat(
* Build a [SingleDocumentFileCompat] from a given [uri].
*/
internal fun make(context: Context, self: Uri): SingleDocumentFileCompat? {
if (isTreeUri(self)) return null
if (!DocumentsContract.isDocumentUri(context, self)) return null

ResolverCompat.getCursor(context, self, ResolverCompat.fullProjection)
?.use { cursor ->
if (cursor.moveToFirst()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ internal class TreeDocumentFileCompat constructor(

internal companion object {

/**
* Return whether the given [uri] is a tree uri.
*/
private fun isTreeUri(uri: Uri): Boolean {
val paths = uri.pathSegments
return paths.size >= 2 && "tree" == paths[0]
}

/**
* Build the initial [TreeDocumentFileCompat] from a given [uri].
*/
Expand All @@ -120,9 +112,12 @@ internal class TreeDocumentFileCompat constructor(
}

// build a new tree uri if this is a first tree doc creation...
val treeUri = if (isInitial) DocumentsContract.buildDocumentUriUsingTree(
uri, DocumentsContract.getTreeDocumentId(uri)
) else uri
// but only if the uri is not already a document uri to preserve subdir info.
val treeUri = if (isInitial && !DocumentsContract.isDocumentUri(context, uri)) {
DocumentsContract.buildDocumentUriUsingTree(
uri, DocumentsContract.getTreeDocumentId(uri)
)
} else uri

ResolverCompat.getCursor(context, treeUri, ResolverCompat.fullProjection)
?.use { cursor ->
Expand Down