File tree Expand file tree Collapse file tree
dfc/src/main/java/com/lazygeniouz/dfc/file Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package com.lazygeniouz.dfc.file.internals
22
33import android.content.Context
44import android.net.Uri
5+ import android.provider.DocumentsContract
56import com.lazygeniouz.dfc.file.DocumentFileCompat
67import 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()) {
Original file line number Diff line number Diff 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 ->
You can’t perform that action at this time.
0 commit comments