diff options
author | Diogenes Molinares <amolinares19@gmail.com> | 2020-06-10 10:35:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-10 14:05:03 +0530 |
commit | 0050deb5012d158f83f04bc6be61a52596fa99c2 (patch) | |
tree | 51417971d7b7dbcbeb3d34d688c9a8d45a9c24ca /app/src/main | |
parent | 7569d4027081d27a6e3d975c14cfc9bdcd879257 (diff) |
Remove Apache Commons dependencies (#840)
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
7 files changed, 70 insertions, 25 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 8c5aa3ae..525ad1e2 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -55,6 +55,7 @@ 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 @@ -65,8 +66,6 @@ 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 org.apache.commons.io.FileUtils -import org.apache.commons.io.FilenameUtils import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.errors.GitAPIException import org.eclipse.jgit.revwalk.RevCommit @@ -577,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, null, true) + FileUtils.listFiles(item.file, true) } else { listOf(item.file) } @@ -668,7 +667,7 @@ class PasswordStore : AppCompatActivity() { if (dir != null && dir.exists() && dir.isDirectory && - !FileUtils.listFiles(dir, null, true).isEmpty() && + !FileUtils.listFiles(dir, true).isEmpty() && getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) { closeRepository() checkLocalRepository() @@ -703,7 +702,7 @@ class PasswordStore : AppCompatActivity() { continue } val destinationFile = File(target.absolutePath + "/" + source.name) - val basename = FilenameUtils.getBaseName(source.absolutePath) + val basename = FileUtils.getBaseName(source.absolutePath) val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename) val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename) if (destinationFile.exists()) { @@ -739,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, null, true) + val sourceFiles = FileUtils.listFiles(source, true) sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) } } else { mapOf(source to destinationFile) diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 63c9ce7c..96f23724 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -36,6 +36,7 @@ import androidx.preference.PreferenceManager import androidx.preference.SwitchPreferenceCompat import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.d +import com.github.ajalt.timberkt.w import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity @@ -52,7 +53,6 @@ import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.autofillManager import com.zeapo.pwdstore.utils.getEncryptedPrefs import me.msfjarvis.openpgpktx.util.OpenPgpUtils -import org.apache.commons.io.FileUtils import java.io.File import java.io.IOException import java.time.LocalDateTime @@ -227,7 +227,7 @@ class UserPreference : AppCompatActivity() { .setCancelable(false) .setPositiveButton(R.string.dialog_delete) { dialogInterface, _ -> try { - FileUtils.cleanDirectory(PasswordRepository.getRepositoryDirectory(callingActivity.applicationContext)) + PasswordRepository.getRepositoryDirectory(callingActivity.applicationContext).deleteRecursively() PasswordRepository.closeRepository() } catch (ignored: Exception) { // TODO Handle the different cases of exceptions @@ -357,8 +357,8 @@ class UserPreference : AppCompatActivity() { prefIsCustomDict?.onPreferenceChangeListener = ChangeListener { _, newValue -> if (!(newValue as Boolean)) { val customDictFile = File(context.filesDir, XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) - if (customDictFile.exists()) { - FileUtils.deleteQuietly(customDictFile) + if (customDictFile.exists() && !customDictFile.delete()) { + w { "Failed to delete custom XkPassword dictionary: $customDictFile" } } prefCustomDictPicker?.setSummary(R.string.xkpwgen_pref_custom_dict_picker_summary) } @@ -706,9 +706,11 @@ class UserPreference : AppCompatActivity() { val customDictPref = prefsFragment.findPreference<Preference>("pref_key_custom_dict") setCustomDictSummary(customDictPref, uri) // copy user selected file to internal storage - val inputStream = this.contentResolver.openInputStream(uri) - val customDictFile = File(this.filesDir.toString(), XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) - FileUtils.copyInputStreamToFile(inputStream, customDictFile) + val inputStream = contentResolver.openInputStream(uri) + val customDictFile = File(filesDir.toString(), XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE).outputStream() + inputStream?.copyTo(customDictFile, 1024) + inputStream?.close() + customDictFile.close() setResult(Activity.RESULT_OK) } diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt index 0e8809e7..26c86fc8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.kt @@ -39,7 +39,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection -import org.apache.commons.io.FileUtils import org.openintents.openpgp.IOpenPgpService2 import org.openintents.openpgp.OpenPgpError import java.io.ByteArrayOutputStream @@ -497,7 +496,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope var inputStream: InputStream? = null withContext(Dispatchers.IO) { try { - inputStream = FileUtils.openInputStream(items[lastWhichItem]) + inputStream = items[lastWhichItem].inputStream() } catch (e: IOException) { e.printStackTrace() cancel("", e) 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 35550d8d..c62b6293 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -43,6 +43,7 @@ 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 @@ -69,8 +70,6 @@ import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.RESULT_CODE_USER_INTERA import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.RESULT_ERROR import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.RESULT_INTENT import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection -import org.apache.commons.io.FileUtils -import org.apache.commons.io.FilenameUtils import org.openintents.openpgp.IOpenPgpService2 import org.openintents.openpgp.OpenPgpError import java.io.ByteArrayInputStream @@ -352,7 +351,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { val data = receivedIntent ?: Intent() data.action = ACTION_DECRYPT_VERIFY - val iStream = FileUtils.openInputStream(File(fullPath)) + val iStream = File(fullPath).inputStream() val oStream = ByteArrayOutputStream() lifecycleScope.launch(IO) { @@ -478,7 +477,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { try { // TODO This might fail, we should check that the write is successful val file = File(path) - val outputStream = FileUtils.openOutputStream(file) + val outputStream = file.outputStream() outputStream.write(oStream.toByteArray()) outputStream.close() @@ -772,7 +771,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { * Gets the name of the password (excluding .gpg) */ fun getName(fullPath: String): String { - return FilenameUtils.getBaseName(fullPath) + return FileUtils.getBaseName(fullPath) } /** 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 0a70d74a..950968ff 100644 --- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/ShowSshKeyFragment.kt @@ -16,7 +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 org.apache.commons.io.FileUtils +import com.zeapo.pwdstore.utils.FileUtils import java.io.File import java.nio.charset.StandardCharsets @@ -61,7 +61,7 @@ class ShowSshKeyFragment : DialogFragment() { private fun readKeyFromFile() { val file = File(requireActivity().filesDir.toString() + "/.ssh_key.pub") try { - publicKey.text = FileUtils.readFileToString(file, StandardCharsets.UTF_8) + publicKey.text = file.readText() } catch (e: Exception) { e.printStackTrace() } 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<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) + } +} 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<File> { 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<File>() |