Replace FileUtils with Kotlin stdlib calls (#843)

This commit is contained in:
Fabian Henneke 2020-06-10 11:19:49 +02:00 committed by GitHub
parent 0050deb501
commit 5d6529a4d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 63 deletions

View file

@ -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)

View file

@ -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
*/

View file

@ -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() {

View file

@ -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)

View file

@ -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<File> {
val res = ArrayList<File>()
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)
}
}