mirror of
https://github.com/android-password-store/Android-Password-Store
synced 2025-08-31 14:25:28 +00:00
Replace FileUtils with Kotlin stdlib calls (#843)
This commit is contained in:
@@ -55,7 +55,6 @@ import com.zeapo.pwdstore.git.GitOperationActivity
|
|||||||
import com.zeapo.pwdstore.git.GitServerConfigActivity
|
import com.zeapo.pwdstore.git.GitServerConfigActivity
|
||||||
import com.zeapo.pwdstore.git.config.ConnectionMode
|
import com.zeapo.pwdstore.git.config.ConnectionMode
|
||||||
import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment
|
import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment
|
||||||
import com.zeapo.pwdstore.utils.FileUtils
|
|
||||||
import com.zeapo.pwdstore.utils.PasswordItem
|
import com.zeapo.pwdstore.utils.PasswordItem
|
||||||
import com.zeapo.pwdstore.utils.PasswordRepository
|
import com.zeapo.pwdstore.utils.PasswordRepository
|
||||||
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.closeRepository
|
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.initialize
|
||||||
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized
|
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized
|
||||||
import com.zeapo.pwdstore.utils.PasswordRepository.PasswordSortOrder.Companion.getSortOrder
|
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.Git
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException
|
import org.eclipse.jgit.api.errors.GitAPIException
|
||||||
import org.eclipse.jgit.revwalk.RevCommit
|
import org.eclipse.jgit.revwalk.RevCommit
|
||||||
@@ -576,7 +576,7 @@ class PasswordStore : AppCompatActivity() {
|
|||||||
.setMessage(resources.getString(R.string.delete_dialog_text, item.longName))
|
.setMessage(resources.getString(R.string.delete_dialog_text, item.longName))
|
||||||
.setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ ->
|
.setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ ->
|
||||||
val filesToDelete = if (item.file.isDirectory) {
|
val filesToDelete = if (item.file.isDirectory) {
|
||||||
FileUtils.listFiles(item.file, true)
|
item.file.listFilesRecursively()
|
||||||
} else {
|
} else {
|
||||||
listOf(item.file)
|
listOf(item.file)
|
||||||
}
|
}
|
||||||
@@ -667,7 +667,7 @@ class PasswordStore : AppCompatActivity() {
|
|||||||
if (dir != null &&
|
if (dir != null &&
|
||||||
dir.exists() &&
|
dir.exists() &&
|
||||||
dir.isDirectory &&
|
dir.isDirectory &&
|
||||||
!FileUtils.listFiles(dir, true).isEmpty() &&
|
dir.listFilesRecursively().isNotEmpty() &&
|
||||||
getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) {
|
getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) {
|
||||||
closeRepository()
|
closeRepository()
|
||||||
checkLocalRepository()
|
checkLocalRepository()
|
||||||
@@ -702,7 +702,7 @@ class PasswordStore : AppCompatActivity() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val destinationFile = File(target.absolutePath + "/" + source.name)
|
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 sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename)
|
||||||
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
|
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
|
||||||
if (destinationFile.exists()) {
|
if (destinationFile.exists()) {
|
||||||
@@ -738,7 +738,7 @@ class PasswordStore : AppCompatActivity() {
|
|||||||
// Recursively list all files (not directories) below `source`, then
|
// Recursively list all files (not directories) below `source`, then
|
||||||
// obtain the corresponding target file by resolving the relative path
|
// obtain the corresponding target file by resolving the relative path
|
||||||
// starting at the destination folder.
|
// starting at the destination folder.
|
||||||
val sourceFiles = FileUtils.listFiles(source, true)
|
val sourceFiles = source.listFilesRecursively()
|
||||||
sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) }
|
sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) }
|
||||||
} else {
|
} else {
|
||||||
mapOf(source to destinationFile)
|
mapOf(source to destinationFile)
|
||||||
|
@@ -43,7 +43,6 @@ import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences
|
|||||||
import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure
|
import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure
|
||||||
import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment
|
import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment
|
||||||
import com.zeapo.pwdstore.ui.dialogs.XkPasswordGeneratorDialogFragment
|
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_category_decrypt
|
||||||
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_file
|
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_file
|
||||||
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_last_changed
|
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 repoPath: String by lazy { intent.getStringExtra("REPO_PATH") }
|
||||||
|
|
||||||
private val fullPath: String by lazy { intent.getStringExtra("FILE_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 {
|
private val lastChangedString: CharSequence by lazy {
|
||||||
getLastChangedString(
|
getLastChangedString(
|
||||||
intent.getLongExtra(
|
intent.getLongExtra(
|
||||||
@@ -767,13 +766,6 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
|
|||||||
return "/${relativePath.substring(startIndex = 0, endIndex = index + 1)}/".replace("/+".toRegex(), "/")
|
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
|
* /path/to/store/social/facebook.gpg -> social/facebook
|
||||||
*/
|
*/
|
||||||
|
@@ -16,9 +16,7 @@ import androidx.core.content.getSystemService
|
|||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.zeapo.pwdstore.R
|
import com.zeapo.pwdstore.R
|
||||||
import com.zeapo.pwdstore.utils.FileUtils
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.charset.StandardCharsets
|
|
||||||
|
|
||||||
class ShowSshKeyFragment : DialogFragment() {
|
class ShowSshKeyFragment : DialogFragment() {
|
||||||
|
|
||||||
|
@@ -17,6 +17,7 @@ import androidx.appcompat.app.AlertDialog
|
|||||||
import androidx.core.content.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import androidx.security.crypto.EncryptedSharedPreferences
|
import androidx.security.crypto.EncryptedSharedPreferences
|
||||||
import androidx.security.crypto.MasterKeys
|
import androidx.security.crypto.MasterKeys
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
infix fun Int.hasFlag(flag: Int): Boolean {
|
infix fun Int.hasFlag(flag: Int): Boolean {
|
||||||
return this and flag == flag
|
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 {
|
fun Context.resolveAttribute(attr: Int): Int {
|
||||||
val typedValue = TypedValue()
|
val typedValue = TypedValue()
|
||||||
this.theme.resolveAttribute(attr, typedValue, true)
|
this.theme.resolveAttribute(attr, typedValue, true)
|
||||||
|
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user