diff --git a/build.gradle b/build.gradle index 975defc..e078c4c 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { dependencies { classpath "com.android.tools.build:gradle:9.0.0" - classpath "org.jetbrains.dokka:dokka-gradle-plugin:2.1.0" + classpath "org.jetbrains.dokka:dokka-gradle-plugin:2.2.0" classpath "com.vanniktech:gradle-maven-publish-plugin:0.36.0" } } @@ -23,7 +23,7 @@ allprojects { } plugins.withId("com.vanniktech.maven.publish.base") { - version = "1.5" + version = "1.6" group = "com.lazygeniouz" mavenPublishing { diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt index 1d279df..3537242 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/file/DocumentFileCompat.kt @@ -105,10 +105,9 @@ abstract class DocumentFileCompat( /** * Rename a Document File / Folder. * - * Will throw [UnsupportedOperationException] when called on a [SingleDocumentFileCompat]. + * Succeeds when the underlying Uri carries `FLAG_SUPPORTS_RENAME`. * * @return True if the rename was successful, False otherwise. - * @throws UnsupportedOperationException */ abstract fun renameTo(name: String): Boolean diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt index 69fa5d5..c2552c7 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/file/internals/SingleDocumentFileCompat.kt @@ -78,12 +78,16 @@ internal class SingleDocumentFileCompat( } /** - * [SingleDocumentFileCompat] has limited access and permissions to the [uri]. + * Rename succeeds when the underlying Uri carries `FLAG_SUPPORTS_RENAME`; + * ideally true for documents obtained from a tree grant via [DocumentFileCompat.listFiles]. * - * @throws UnsupportedOperationException + * @return True if the rename was successful, False otherwise. */ override fun renameTo(name: String): Boolean { - throw UnsupportedOperationException() + val newUri = fileController.renameTo(name) + val success = newUri != null + if (success) uri = newUri + return success } // Copies current file to the destination uri. diff --git a/dfc/src/main/java/com/lazygeniouz/dfc/resolver/ResolverCompat.kt b/dfc/src/main/java/com/lazygeniouz/dfc/resolver/ResolverCompat.kt index 74c1afe..def6858 100644 --- a/dfc/src/main/java/com/lazygeniouz/dfc/resolver/ResolverCompat.kt +++ b/dfc/src/main/java/com/lazygeniouz/dfc/resolver/ResolverCompat.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.provider.DocumentsContract import android.provider.DocumentsContract.Document import com.lazygeniouz.dfc.file.DocumentFileCompat +import com.lazygeniouz.dfc.file.internals.SingleDocumentFileCompat import com.lazygeniouz.dfc.file.internals.TreeDocumentFileCompat import com.lazygeniouz.dfc.logger.ErrorLogger @@ -125,10 +126,11 @@ internal object ResolverCompat { val childrenUri = createChildrenUri(uri) val listOfDocuments = arrayListOf() - // ensure `Document.COLUMN_DOCUMENT_ID` is always included - val finalProjection = if (Document.COLUMN_DOCUMENT_ID !in projection) { - arrayOf(Document.COLUMN_DOCUMENT_ID, *projection) - } else projection + val finalProjection = arrayOf( + Document.COLUMN_DOCUMENT_ID, /* identifier */ + Document.COLUMN_MIME_TYPE, /* for supporting rename via `isDirectory` check */ + *projection + ).distinct().toTypedArray() val cursor = getCursor(context, childrenUri, finalProjection) ?: return emptyList() @@ -168,14 +170,23 @@ internal object ResolverCompat { */ val documentFlags = getLongOrDefault(cursor, flagsIndex, 0L).toInt() - TreeDocumentFileCompat( - context, documentUri, documentName, - documentSize, lastModifiedTime, - documentMimeType, documentFlags - ).also { childFile -> - childFile.parentFile = file - listOfDocuments.add(childFile) - } + /* return correct document type */ + val childFile: DocumentFileCompat = + if (documentMimeType == Document.MIME_TYPE_DIR) { + TreeDocumentFileCompat( + context, documentUri, documentName, + documentSize, lastModifiedTime, + documentMimeType, documentFlags + ) + } else { + SingleDocumentFileCompat( + context, documentUri, documentName, + documentSize, lastModifiedTime, + documentMimeType, documentFlags + ) + } + childFile.parentFile = file + listOfDocuments.add(childFile) } }