aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreuxane2024-09-18 23:41:13 +0200
committereuxane2024-09-18 23:41:13 +0200
commit8cb2bff8042f9647b159c9121721f0bdae587692 (patch)
tree93a0230907f7561a58a4adc7155a97b30ca4bae3
parent8e284ccc94387c6724a238dcb9353ea496c79753 (diff)
downloadtincapp-8cb2bff8042f9647b159c9121721f0bdae587692.tar.gz
utils/Files: extract isParentOf and pathUnder to utils
-rw-r--r--app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt18
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/Files.kt20
2 files changed, 22 insertions, 16 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt
index 688ae27..48d0319 100644
--- a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt
+++ b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt
@@ -31,6 +31,8 @@ import android.provider.DocumentsProvider
31import androidx.annotation.RequiresApi 31import androidx.annotation.RequiresApi
32import org.pacien.tincapp.R 32import org.pacien.tincapp.R
33import org.pacien.tincapp.context.AppPaths 33import org.pacien.tincapp.context.AppPaths
34import org.pacien.tincapp.utils.isParentOf
35import org.pacien.tincapp.utils.pathUnder
34import java.io.File 36import java.io.File
35import java.io.FileNotFoundException 37import java.io.FileNotFoundException
36import kotlin.io.path.Path 38import kotlin.io.path.Path
@@ -178,22 +180,6 @@ class FilesDocumentsProvider : DocumentsProvider() {
178 throw IllegalArgumentException() 180 throw IllegalArgumentException()
179 } 181 }
180 182
181 private fun File.pathUnder(parent: File): String =
182 canonicalPath.removePrefix(parent.canonicalPath)
183
184 private fun File.isParentOf(childCandidate: File, strict: Boolean = true): Boolean {
185 var parentOfChild = childCandidate.canonicalFile
186
187 if (strict)
188 parentOfChild = parentOfChild.parentFile
189
190 while (parentOfChild != null) {
191 if (parentOfChild.equals(canonicalFile)) return true
192 parentOfChild = parentOfChild.parentFile
193 }
194 return false
195 }
196
197 private fun File.documentMimeType() = 183 private fun File.documentMimeType() =
198 if (isDirectory) Document.MIME_TYPE_DIR 184 if (isDirectory) Document.MIME_TYPE_DIR
199 else "text/plain" 185 else "text/plain"
diff --git a/app/src/main/java/org/pacien/tincapp/utils/Files.kt b/app/src/main/java/org/pacien/tincapp/utils/Files.kt
index cfa16b8..8eda328 100644
--- a/app/src/main/java/org/pacien/tincapp/utils/Files.kt
+++ b/app/src/main/java/org/pacien/tincapp/utils/Files.kt
@@ -45,3 +45,23 @@ fun File.makePublic() {
45 for (file in this.listFiles()!!) 45 for (file in this.listFiles()!!)
46 file.makePublic() 46 file.makePublic()
47} 47}
48
49fun File.isParentOf(childCandidate: File, strict: Boolean = true): Boolean {
50 var parentOfChild = childCandidate.canonicalFile
51
52 if (strict)
53 parentOfChild = parentOfChild.parentFile
54
55 while (parentOfChild != null) {
56 if (parentOfChild.equals(canonicalFile)) return true
57 parentOfChild = parentOfChild.parentFile
58 }
59 return false
60}
61
62fun File.pathUnder(parent: File): String {
63 if (!parent.isParentOf(this, false))
64 throw IllegalArgumentException("File is not under the given parent.")
65
66 return canonicalPath.removePrefix(parent.canonicalPath)
67}