Skip to content

Commit 631da73

Browse files
committed
Refactor String path to Uri, doc fixes, make renameTo abstract, add getType.
1 parent 489489d commit 631da73

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.lazygeniouz.dfc.file
33
import android.content.ContentResolver
44
import android.content.Context
55
import android.net.Uri
6+
import android.provider.DocumentsContract.Document.COLUMN_MIME_TYPE
7+
import android.provider.DocumentsContract.Document.MIME_TYPE_DIR
68
import android.provider.DocumentsContract.isDocumentUri
79
import com.lazygeniouz.dfc.controller.DocumentController
810
import com.lazygeniouz.dfc.file.internals.RawDocumentFileCompat
@@ -17,8 +19,7 @@ import java.io.File
1719
*/
1820
abstract class DocumentFileCompat constructor(
1921
internal val context: Context,
20-
internal val path: String,
21-
val name: String = "",
22+
uri: Uri, val name: String = "",
2223
open val length: Long = 0,
2324
val lastModified: Long = -1L,
2425
internal val documentFlags: Int = -1,
@@ -27,7 +28,7 @@ abstract class DocumentFileCompat constructor(
2728

2829
// Secondary constructor for java.io.File type
2930
constructor(context: Context, file: File) : this(
30-
context, file.absolutePath, file.name, file.length(),
31+
context, Uri.fromFile(file), file.name, file.length(),
3132
file.lastModified(), -1, RawDocumentFileCompat.getMimeType(file)
3233
)
3334

@@ -70,7 +71,7 @@ abstract class DocumentFileCompat constructor(
7071
*
7172
* Returns **0** if the uri is not a directory or if the object at uri is null.
7273
*
73-
* **Note: You should use [listFiles] if you are gonna need them later.**
74+
* **Note: You should use [listFiles] if you are going to need the elements later.**
7475
*/
7576
abstract fun count(): Int
7677

@@ -95,8 +96,18 @@ abstract class DocumentFileCompat constructor(
9596
*/
9697
abstract fun copyFrom(source: Uri)
9798

98-
open val uri: Uri
99-
get() = Uri.parse(path)
99+
/**
100+
* Rename a Document File / Folder.
101+
*
102+
* Will throw [UnsupportedOperationException] when called on a [SingleDocumentFileCompat].
103+
*
104+
* @return True if the rename was successful, False otherwise.
105+
* @throws UnsupportedOperationException
106+
*/
107+
abstract fun renameTo(name: String): Boolean
108+
109+
var uri: Uri = uri
110+
internal set
100111

101112
/**
102113
* Get the extension of the Document **File**.
@@ -105,6 +116,16 @@ abstract class DocumentFileCompat constructor(
105116
// taken from Kotlin extension
106117
get() = name.substringAfterLast('.', "")
107118

119+
/**
120+
* Return the MIME type of this document.
121+
*
122+
* @return A concrete mime type from [COLUMN_MIME_TYPE] column.
123+
*/
124+
@Suppress("unused")
125+
fun getType(): String? {
126+
return if (documentMimeType == MIME_TYPE_DIR) null else documentMimeType
127+
}
128+
108129
/**
109130
* Delete the file.
110131
*
@@ -159,19 +180,10 @@ abstract class DocumentFileCompat constructor(
159180
return fileController.isDirectory()
160181
}
161182

162-
/**
163-
* Rename a Document File / Folder.
164-
*
165-
* Returns True if the rename was successful, False otherwise.
166-
*/
167-
open fun renameTo(name: String): Boolean {
168-
return fileController.renameTo(name)
169-
}
170-
171183
/**
172184
* Converts a non serializable [DocumentFileCompat] to a serializable [SerializedFile].
173185
*/
174-
@Suppress("unused")
186+
@Suppress("MemberVisibilityCanBePrivate")
175187
fun serialize(): SerializedFile {
176188
return SerializedFile.from(this)
177189
}

0 commit comments

Comments
 (0)