Skip to content

Commit d510a63

Browse files
committed
Add content file system
Signed-off-by: Kyle Corry <kylecorry31@gmail.com>
1 parent 45c5dd1 commit d510a63

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
extra.apply {
33
set("groupId", "com.kylecorry.andromeda")
4-
set("versionName", "10.5.2")
4+
set("versionName", "11.0.0")
55
set("targetVersion", 35)
66
set("compileVersion", 35)
77
}

files/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies {
5858
implementation project(':core')
5959
implementation libs.androidx.core.ktx
6060
implementation libs.kotlinx.coroutines.android
61+
implementation libs.androidx.documentfile
6162
coreLibraryDesugaring libs.desugar
6263
testImplementation libs.junit
6364
androidTestImplementation libs.androidx.junit
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.kylecorry.andromeda.files
2+
3+
import android.net.Uri
4+
import java.time.Instant
5+
6+
data class ContentDocument(
7+
val uri: Uri,
8+
val displayName: String?,
9+
val mimeType: String?,
10+
val canRead: Boolean,
11+
val canWrite: Boolean,
12+
val lastModified: Instant,
13+
val size: Long
14+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.kylecorry.andromeda.files
2+
3+
import android.content.Context
4+
import android.net.Uri
5+
import androidx.documentfile.provider.DocumentFile
6+
import java.time.Instant
7+
import kotlin.collections.toList
8+
9+
class ContentFileSystem(context: Context, treeUri: Uri) {
10+
11+
private val tree = DocumentFile.fromTreeUri(context, treeUri)
12+
13+
fun createFile(displayName: String, mimeType: String): ContentDocument? {
14+
return tree?.createFile(mimeType, displayName)?.toContentDocument()
15+
}
16+
17+
fun getFile(displayName: String): ContentDocument? {
18+
return findFile(displayName)?.toContentDocument()
19+
}
20+
21+
fun deleteFile(displayName: String): Boolean {
22+
return findFile(displayName)?.delete() == true
23+
}
24+
25+
fun listFiles(): List<DocumentFile> {
26+
return tree?.listFiles()?.toList() ?: emptyList()
27+
}
28+
29+
fun canWrite(displayName: String? = null): Boolean {
30+
return if (displayName == null) {
31+
tree?.canWrite() == true
32+
} else {
33+
findFile(displayName)?.canWrite() == true
34+
}
35+
}
36+
37+
fun canRead(displayName: String? = null): Boolean {
38+
return if (displayName == null) {
39+
tree?.canRead() == true
40+
} else {
41+
findFile(displayName)?.canRead() == true
42+
}
43+
}
44+
45+
private fun findFile(displayName: String): DocumentFile? {
46+
return tree?.findFile(displayName)
47+
}
48+
49+
companion object {
50+
fun getDocumentFromUri(context: Context, uri: Uri): ContentDocument? {
51+
return DocumentFile.fromSingleUri(context, uri)?.toContentDocument()
52+
}
53+
54+
private fun DocumentFile.toContentDocument(): ContentDocument {
55+
return ContentDocument(
56+
uri,
57+
name,
58+
type,
59+
canRead(),
60+
canWrite(),
61+
Instant.ofEpochMilli(lastModified()),
62+
length()
63+
)
64+
}
65+
}
66+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[versions]
22
androidLibraryVersion = "8.2.2"
3+
androidXDocumentFileVersion = "1.0.1"
34
appCompatVersion = "1.7.0"
45
cameraCamera2Version = "1.3.4"
56
constraintlayoutVersion = "2.1.4"
@@ -37,6 +38,7 @@ androidx-camera-lifecycle = { module = "androidx.camera:camera-lifecycle", versi
3738
androidx-camera-view = { module = "androidx.camera:camera-view", version.ref = "cameraCamera2Version" }
3839
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayoutVersion" }
3940
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtxVersion" }
41+
androidx-documentfile = { module = "androidx.documentfile:documentfile", version.ref = "androidXDocumentFileVersion" }
4042
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferencesVersion" }
4143
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCoreVersion" }
4244
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junit" }

0 commit comments

Comments
 (0)