-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kyle Corry <[email protected]>
- Loading branch information
1 parent
45c5dd1
commit d510a63
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
files/src/main/java/com/kylecorry/andromeda/files/ContentDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.kylecorry.andromeda.files | ||
|
||
import android.net.Uri | ||
import java.time.Instant | ||
|
||
data class ContentDocument( | ||
val uri: Uri, | ||
val displayName: String?, | ||
val mimeType: String?, | ||
val canRead: Boolean, | ||
val canWrite: Boolean, | ||
val lastModified: Instant, | ||
val size: Long | ||
) |
66 changes: 66 additions & 0 deletions
66
files/src/main/java/com/kylecorry/andromeda/files/ContentFileSystem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.kylecorry.andromeda.files | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import androidx.documentfile.provider.DocumentFile | ||
import java.time.Instant | ||
import kotlin.collections.toList | ||
|
||
class ContentFileSystem(context: Context, treeUri: Uri) { | ||
|
||
private val tree = DocumentFile.fromTreeUri(context, treeUri) | ||
|
||
fun createFile(displayName: String, mimeType: String): ContentDocument? { | ||
return tree?.createFile(mimeType, displayName)?.toContentDocument() | ||
} | ||
|
||
fun getFile(displayName: String): ContentDocument? { | ||
return findFile(displayName)?.toContentDocument() | ||
} | ||
|
||
fun deleteFile(displayName: String): Boolean { | ||
return findFile(displayName)?.delete() == true | ||
} | ||
|
||
fun listFiles(): List<DocumentFile> { | ||
return tree?.listFiles()?.toList() ?: emptyList() | ||
} | ||
|
||
fun canWrite(displayName: String? = null): Boolean { | ||
return if (displayName == null) { | ||
tree?.canWrite() == true | ||
} else { | ||
findFile(displayName)?.canWrite() == true | ||
} | ||
} | ||
|
||
fun canRead(displayName: String? = null): Boolean { | ||
return if (displayName == null) { | ||
tree?.canRead() == true | ||
} else { | ||
findFile(displayName)?.canRead() == true | ||
} | ||
} | ||
|
||
private fun findFile(displayName: String): DocumentFile? { | ||
return tree?.findFile(displayName) | ||
} | ||
|
||
companion object { | ||
fun getDocumentFromUri(context: Context, uri: Uri): ContentDocument? { | ||
return DocumentFile.fromSingleUri(context, uri)?.toContentDocument() | ||
} | ||
|
||
private fun DocumentFile.toContentDocument(): ContentDocument { | ||
return ContentDocument( | ||
uri, | ||
name, | ||
type, | ||
canRead(), | ||
canWrite(), | ||
Instant.ofEpochMilli(lastModified()), | ||
length() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters