From 0050deb5012d158f83f04bc6be61a52596fa99c2 Mon Sep 17 00:00:00 2001 From: Diogenes Molinares Date: Wed, 10 Jun 2020 10:35:03 +0200 Subject: Remove Apache Commons dependencies (#840) Co-authored-by: Harsh Shandilya --- .../java/com/zeapo/pwdstore/utils/FileUtils.kt | 47 ++++++++++++++++++++++ .../com/zeapo/pwdstore/utils/PasswordRepository.kt | 5 +-- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt (limited to 'app/src/main/java/com/zeapo/pwdstore/utils') diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt b/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt new file mode 100644 index 00000000..867ec0aa --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt @@ -0,0 +1,47 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package com.zeapo.pwdstore.utils + +import java.io.File + +object FileUtils { + @JvmStatic + fun listFiles(dir: File, recursive: Boolean): Collection { + val res = ArrayList() + val files = dir.listFiles() + + if (files != null && files.isNotEmpty()) { + + files.forEach { file -> + // Check if the file is a directory and recursive add + if (file.isDirectory && recursive) { + res.addAll(listFiles(file, recursive)) + } else if (!file.isDirectory) { + res.add(file) + } + } + } + return res + } + + @JvmStatic + fun getBaseName(filename: String): String { + // Take the file name along with its extension + val indexName = filename.lastIndexOf('/') + val nameWithExtension = filename.substring(indexName + 1) + + // Find the final '.' character in the previously calculated nameWithExtension + val indexExt = nameWithExtension.lastIndexOf('.') + + // If no '.' is found in the name, we assume this is a directory and return the previously + // derived nameWithExtensions as-is, otherwise we slice out a substring from the first character + // to the last occurrence of '.' which we found earlier. + return if (indexExt == -1) + nameWithExtension + else + nameWithExtension.substring(0, indexExt) + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt index ccbf179e..794630be 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -8,7 +8,6 @@ import android.content.Context import android.content.SharedPreferences import androidx.core.content.edit import androidx.preference.PreferenceManager -import org.apache.commons.io.filefilter.FileFilterUtils import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.storage.file.FileRepositoryBuilder @@ -195,9 +194,9 @@ open class PasswordRepository protected constructor() { fun getFilesList(path: File?): ArrayList { if (path == null || !path.exists()) return ArrayList() - val directories = (path.listFiles(FileFilterUtils.directoryFileFilter() as FileFilter) + val directories = (path.listFiles(FileFilter { pathname -> pathname.isDirectory }) ?: emptyArray()).toList() - val files = (path.listFiles(FileFilterUtils.suffixFileFilter(".gpg") as FileFilter) + val files = (path.listFiles(FileFilter { pathname -> pathname.extension == "gpg" }) ?: emptyArray()).toList() val items = ArrayList() -- cgit v1.2.3