From 5d6529a4d8dc6dc938cd05903bfde76412ce17cd Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Wed, 10 Jun 2020 11:19:49 +0200 Subject: [PATCH] Replace FileUtils with Kotlin stdlib calls (#843) --- .../java/com/zeapo/pwdstore/PasswordStore.kt | 10 ++-- .../com/zeapo/pwdstore/crypto/PgpActivity.kt | 10 +--- .../pwdstore/sshkeygen/ShowSshKeyFragment.kt | 2 - .../com/zeapo/pwdstore/utils/Extensions.kt | 3 ++ .../com/zeapo/pwdstore/utils/FileUtils.kt | 47 ------------------- 5 files changed, 9 insertions(+), 63 deletions(-) delete mode 100644 app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 525ad1e2..00cfbf9a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -55,7 +55,6 @@ import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.git.GitServerConfigActivity import com.zeapo.pwdstore.git.config.ConnectionMode import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment -import com.zeapo.pwdstore.utils.FileUtils import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.PasswordRepository.Companion.closeRepository @@ -66,6 +65,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirect import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized import com.zeapo.pwdstore.utils.PasswordRepository.PasswordSortOrder.Companion.getSortOrder +import com.zeapo.pwdstore.utils.listFilesRecursively import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.errors.GitAPIException import org.eclipse.jgit.revwalk.RevCommit @@ -576,7 +576,7 @@ class PasswordStore : AppCompatActivity() { .setMessage(resources.getString(R.string.delete_dialog_text, item.longName)) .setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ -> val filesToDelete = if (item.file.isDirectory) { - FileUtils.listFiles(item.file, true) + item.file.listFilesRecursively() } else { listOf(item.file) } @@ -667,7 +667,7 @@ class PasswordStore : AppCompatActivity() { if (dir != null && dir.exists() && dir.isDirectory && - !FileUtils.listFiles(dir, true).isEmpty() && + dir.listFilesRecursively().isNotEmpty() && getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) { closeRepository() checkLocalRepository() @@ -702,7 +702,7 @@ class PasswordStore : AppCompatActivity() { continue } val destinationFile = File(target.absolutePath + "/" + source.name) - val basename = FileUtils.getBaseName(source.absolutePath) + val basename = source.nameWithoutExtension val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename) val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename) if (destinationFile.exists()) { @@ -738,7 +738,7 @@ class PasswordStore : AppCompatActivity() { // Recursively list all files (not directories) below `source`, then // obtain the corresponding target file by resolving the relative path // starting at the destination folder. - val sourceFiles = FileUtils.listFiles(source, true) + val sourceFiles = source.listFilesRecursively() sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) } } else { mapOf(source to destinationFile) diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index c62b6293..77d7e70c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -43,7 +43,6 @@ import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment import com.zeapo.pwdstore.ui.dialogs.XkPasswordGeneratorDialogFragment -import com.zeapo.pwdstore.utils.FileUtils import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_category_decrypt import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_file import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_last_changed @@ -95,7 +94,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { private val repoPath: String by lazy { intent.getStringExtra("REPO_PATH") } private val fullPath: String by lazy { intent.getStringExtra("FILE_PATH") } - private val name: String by lazy { getName(fullPath) } + private val name: String by lazy { File(fullPath).nameWithoutExtension } private val lastChangedString: CharSequence by lazy { getLastChangedString( intent.getLongExtra( @@ -767,13 +766,6 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { return "/${relativePath.substring(startIndex = 0, endIndex = index + 1)}/".replace("/+".toRegex(), "/") } - /** - * Gets the name of the password (excluding .gpg) - */ - fun getName(fullPath: String): String { - return FileUtils.getBaseName(fullPath) - } - /** * /path/to/store/social/facebook.gpg -> social/facebook */ diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt index 950968ff..5d35123d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt @@ -16,9 +16,7 @@ import androidx.core.content.getSystemService import androidx.fragment.app.DialogFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.zeapo.pwdstore.R -import com.zeapo.pwdstore.utils.FileUtils import java.io.File -import java.nio.charset.StandardCharsets class ShowSshKeyFragment : DialogFragment() { diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt index 4820d8bf..d10bdaab 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -17,6 +17,7 @@ import androidx.appcompat.app.AlertDialog import androidx.core.content.getSystemService import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys +import java.io.File infix fun Int.hasFlag(flag: Int): Boolean { return this and flag == flag @@ -32,6 +33,8 @@ fun CharArray.clear() { } } +fun File.listFilesRecursively() = walkTopDown().filter { !it.isDirectory }.toList() + fun Context.resolveAttribute(attr: Int): Int { val typedValue = TypedValue() this.theme.resolveAttribute(attr, typedValue, true) diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt b/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt deleted file mode 100644 index 867ec0aa..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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) - } -}