diff options
author | euxane | 2024-09-18 23:41:13 +0200 |
---|---|---|
committer | euxane | 2024-09-18 23:41:13 +0200 |
commit | 8cb2bff8042f9647b159c9121721f0bdae587692 (patch) | |
tree | 93a0230907f7561a58a4adc7155a97b30ca4bae3 | |
parent | 8e284ccc94387c6724a238dcb9353ea496c79753 (diff) | |
download | tincapp-8cb2bff8042f9647b159c9121721f0bdae587692.tar.gz |
utils/Files: extract isParentOf and pathUnder to utils
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt | 18 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/utils/Files.kt | 20 |
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 | |||
31 | import androidx.annotation.RequiresApi | 31 | import androidx.annotation.RequiresApi |
32 | import org.pacien.tincapp.R | 32 | import org.pacien.tincapp.R |
33 | import org.pacien.tincapp.context.AppPaths | 33 | import org.pacien.tincapp.context.AppPaths |
34 | import org.pacien.tincapp.utils.isParentOf | ||
35 | import org.pacien.tincapp.utils.pathUnder | ||
34 | import java.io.File | 36 | import java.io.File |
35 | import java.io.FileNotFoundException | 37 | import java.io.FileNotFoundException |
36 | import kotlin.io.path.Path | 38 | import 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 | |||
49 | fun 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 | |||
62 | fun 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 | } | ||