small FileUtils kotlinezation

pull/594/head
olegivo 5 years ago
parent 7eafb5eff5
commit 6d34b7ce0b

@ -49,22 +49,14 @@ object FileUtils {
} }
@JvmStatic @JvmStatic
fun getDir(potentialParentDirs: Array<File?>, fun getDir(potentialParentDirs: Array<File?>, relativePath: String?): File? {
relativePath: String?): File? { val chosenDir: File? = potentialParentDirs.firstOrNull { dir -> dir?.canWrite() == true }
var relativePath = relativePath
if (relativePath == null) relativePath = ""
var chosenDir: File? = null
for (dir in potentialParentDirs) {
if (dir == null || !dir.canWrite()) continue
chosenDir = dir
break
}
if (chosenDir == null) { if (chosenDir == null) {
Log.e("FileUtils", Log.e("FileUtils",
"getDir: all potential parents are null or non-writable") "getDir: all potential parents are null or non-writable")
return null return null
} }
val dir = File(String.format("%s/%s/", chosenDir.absolutePath, relativePath)) val dir = File("${chosenDir.absolutePath}/${relativePath ?: ""}/")
if (!dir.exists() && !dir.mkdirs()) { if (!dir.exists() && !dir.mkdirs()) {
Log.e("FileUtils", Log.e("FileUtils",
"getDir: chosen dir does not exist and cannot be created") "getDir: chosen dir does not exist and cannot be created")

Loading…
Cancel
Save