From 8cb2bff8042f9647b159c9121721f0bdae587692 Mon Sep 17 00:00:00 2001 From: euxane Date: Wed, 18 Sep 2024 23:41:13 +0200 Subject: utils/Files: extract isParentOf and pathUnder to utils --- .../storageprovider/FilesDocumentsProvider.kt | 18 ++---------------- app/src/main/java/org/pacien/tincapp/utils/Files.kt | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) (limited to 'app') 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 import androidx.annotation.RequiresApi import org.pacien.tincapp.R import org.pacien.tincapp.context.AppPaths +import org.pacien.tincapp.utils.isParentOf +import org.pacien.tincapp.utils.pathUnder import java.io.File import java.io.FileNotFoundException import kotlin.io.path.Path @@ -178,22 +180,6 @@ class FilesDocumentsProvider : DocumentsProvider() { throw IllegalArgumentException() } - private fun File.pathUnder(parent: File): String = - canonicalPath.removePrefix(parent.canonicalPath) - - private fun File.isParentOf(childCandidate: File, strict: Boolean = true): Boolean { - var parentOfChild = childCandidate.canonicalFile - - if (strict) - parentOfChild = parentOfChild.parentFile - - while (parentOfChild != null) { - if (parentOfChild.equals(canonicalFile)) return true - parentOfChild = parentOfChild.parentFile - } - return false - } - private fun File.documentMimeType() = if (isDirectory) Document.MIME_TYPE_DIR 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() { for (file in this.listFiles()!!) file.makePublic() } + +fun File.isParentOf(childCandidate: File, strict: Boolean = true): Boolean { + var parentOfChild = childCandidate.canonicalFile + + if (strict) + parentOfChild = parentOfChild.parentFile + + while (parentOfChild != null) { + if (parentOfChild.equals(canonicalFile)) return true + parentOfChild = parentOfChild.parentFile + } + return false +} + +fun File.pathUnder(parent: File): String { + if (!parent.isParentOf(this, false)) + throw IllegalArgumentException("File is not under the given parent.") + + return canonicalPath.removePrefix(parent.canonicalPath) +} -- cgit v1.2.3