Skip to content

Commit 79f0c7e

Browse files
authored
Merge pull request #22 from ItzNotABug/fix-issues
Fix: issues
2 parents cf59032 + f0c0cd9 commit 79f0c7e

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,13 @@ abstract class DocumentFileCompat constructor(
240240
fun isDocument(context: Context, uri: Uri): Boolean {
241241
return isDocumentUri(context, uri)
242242
}
243+
244+
/**
245+
* Return whether the given [uri] is a tree uri.
246+
*/
247+
internal fun isTreeUri(uri: Uri): Boolean {
248+
val paths = uri.pathSegments
249+
return paths.size >= 2 && "tree" == paths[0]
250+
}
243251
}
244252
}

dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lazygeniouz.dfc.file.internals
22

33
import android.content.Context
44
import android.net.Uri
5+
import android.provider.DocumentsContract
56
import com.lazygeniouz.dfc.file.DocumentFileCompat
67
import com.lazygeniouz.dfc.resolver.ResolverCompat
78

@@ -96,6 +97,9 @@ internal class SingleDocumentFileCompat(
9697
* Build a [SingleDocumentFileCompat] from a given [uri].
9798
*/
9899
internal fun make(context: Context, self: Uri): SingleDocumentFileCompat? {
100+
if (isTreeUri(self)) return null
101+
if (!DocumentsContract.isDocumentUri(context, self)) return null
102+
99103
ResolverCompat.getCursor(context, self, ResolverCompat.fullProjection)
100104
?.use { cursor ->
101105
if (cursor.moveToFirst()) {

dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocumentFileCompat.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ internal class TreeDocumentFileCompat constructor(
103103

104104
internal companion object {
105105

106-
/**
107-
* Return whether the given [uri] is a tree uri.
108-
*/
109-
private fun isTreeUri(uri: Uri): Boolean {
110-
val paths = uri.pathSegments
111-
return paths.size >= 2 && "tree" == paths[0]
112-
}
113-
114106
/**
115107
* Build the initial [TreeDocumentFileCompat] from a given [uri].
116108
*/
@@ -120,9 +112,12 @@ internal class TreeDocumentFileCompat constructor(
120112
}
121113

122114
// build a new tree uri if this is a first tree doc creation...
123-
val treeUri = if (isInitial) DocumentsContract.buildDocumentUriUsingTree(
124-
uri, DocumentsContract.getTreeDocumentId(uri)
125-
) else uri
115+
// but only if the uri is not already a document uri to preserve subdir info.
116+
val treeUri = if (isInitial && !DocumentsContract.isDocumentUri(context, uri)) {
117+
DocumentsContract.buildDocumentUriUsingTree(
118+
uri, DocumentsContract.getTreeDocumentId(uri)
119+
)
120+
} else uri
126121

127122
ResolverCompat.getCursor(context, treeUri, ResolverCompat.fullProjection)
128123
?.use { cursor ->

0 commit comments

Comments
 (0)