Remove Apache Commons dependencies (#840)

Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Diogenes Molinares
2020-06-10 10:35:03 +02:00
committed by GitHub
parent 7569d40270
commit 0050deb501
9 changed files with 70 additions and 29 deletions

View File

@@ -94,8 +94,6 @@ dependencies {
implementation deps.kotlin.coroutines.android implementation deps.kotlin.coroutines.android
implementation deps.kotlin.coroutines.core implementation deps.kotlin.coroutines.core
implementation deps.third_party.commons_io
implementation deps.third_party.commons_codec
implementation deps.third_party.fastscroll implementation deps.third_party.fastscroll
implementation(deps.third_party.jgit) { implementation(deps.third_party.jgit) {
exclude group: 'org.apache.httpcomponents', module: 'httpclient' exclude group: 'org.apache.httpcomponents', module: 'httpclient'

View File

@@ -55,6 +55,7 @@ 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
@@ -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.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 org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
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
@@ -577,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, null, true) FileUtils.listFiles(item.file, true)
} else { } else {
listOf(item.file) listOf(item.file)
} }
@@ -668,7 +667,7 @@ class PasswordStore : AppCompatActivity() {
if (dir != null && if (dir != null &&
dir.exists() && dir.exists() &&
dir.isDirectory && dir.isDirectory &&
!FileUtils.listFiles(dir, null, true).isEmpty() && !FileUtils.listFiles(dir, true).isEmpty() &&
getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) { getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) {
closeRepository() closeRepository()
checkLocalRepository() checkLocalRepository()
@@ -703,7 +702,7 @@ class PasswordStore : AppCompatActivity() {
continue continue
} }
val destinationFile = File(target.absolutePath + "/" + source.name) 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 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()) {
@@ -739,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, null, true) val sourceFiles = FileUtils.listFiles(source, true)
sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) } sourceFiles.associateWith { destinationFile.resolve(it.relativeTo(source)) }
} else { } else {
mapOf(source to destinationFile) mapOf(source to destinationFile)

View File

@@ -36,6 +36,7 @@ import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreferenceCompat import androidx.preference.SwitchPreferenceCompat
import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.d import com.github.ajalt.timberkt.d
import com.github.ajalt.timberkt.w
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity 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.autofillManager
import com.zeapo.pwdstore.utils.getEncryptedPrefs import com.zeapo.pwdstore.utils.getEncryptedPrefs
import me.msfjarvis.openpgpktx.util.OpenPgpUtils import me.msfjarvis.openpgpktx.util.OpenPgpUtils
import org.apache.commons.io.FileUtils
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.time.LocalDateTime import java.time.LocalDateTime
@@ -227,7 +227,7 @@ class UserPreference : AppCompatActivity() {
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.dialog_delete) { dialogInterface, _ -> .setPositiveButton(R.string.dialog_delete) { dialogInterface, _ ->
try { try {
FileUtils.cleanDirectory(PasswordRepository.getRepositoryDirectory(callingActivity.applicationContext)) PasswordRepository.getRepositoryDirectory(callingActivity.applicationContext).deleteRecursively()
PasswordRepository.closeRepository() PasswordRepository.closeRepository()
} catch (ignored: Exception) { } catch (ignored: Exception) {
// TODO Handle the different cases of exceptions // TODO Handle the different cases of exceptions
@@ -357,8 +357,8 @@ class UserPreference : AppCompatActivity() {
prefIsCustomDict?.onPreferenceChangeListener = ChangeListener { _, newValue -> prefIsCustomDict?.onPreferenceChangeListener = ChangeListener { _, newValue ->
if (!(newValue as Boolean)) { if (!(newValue as Boolean)) {
val customDictFile = File(context.filesDir, XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) val customDictFile = File(context.filesDir, XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE)
if (customDictFile.exists()) { if (customDictFile.exists() && !customDictFile.delete()) {
FileUtils.deleteQuietly(customDictFile) w { "Failed to delete custom XkPassword dictionary: $customDictFile" }
} }
prefCustomDictPicker?.setSummary(R.string.xkpwgen_pref_custom_dict_picker_summary) 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") val customDictPref = prefsFragment.findPreference<Preference>("pref_key_custom_dict")
setCustomDictSummary(customDictPref, uri) setCustomDictSummary(customDictPref, uri)
// copy user selected file to internal storage // copy user selected file to internal storage
val inputStream = this.contentResolver.openInputStream(uri) val inputStream = contentResolver.openInputStream(uri)
val customDictFile = File(this.filesDir.toString(), XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE) val customDictFile = File(filesDir.toString(), XkpwdDictionary.XKPWD_CUSTOM_DICT_FILE).outputStream()
FileUtils.copyInputStreamToFile(inputStream, customDictFile) inputStream?.copyTo(customDictFile, 1024)
inputStream?.close()
customDictFile.close()
setResult(Activity.RESULT_OK) setResult(Activity.RESULT_OK)
} }

View File

@@ -39,7 +39,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import me.msfjarvis.openpgpktx.util.OpenPgpApi import me.msfjarvis.openpgpktx.util.OpenPgpApi
import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection
import org.apache.commons.io.FileUtils
import org.openintents.openpgp.IOpenPgpService2 import org.openintents.openpgp.IOpenPgpService2
import org.openintents.openpgp.OpenPgpError import org.openintents.openpgp.OpenPgpError
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@@ -497,7 +496,7 @@ class AutofillService : AccessibilityService(), CoroutineScope by CoroutineScope
var inputStream: InputStream? = null var inputStream: InputStream? = null
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
try { try {
inputStream = FileUtils.openInputStream(items[lastWhichItem]) inputStream = items[lastWhichItem].inputStream()
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
cancel("", e) cancel("", e)

View File

@@ -43,6 +43,7 @@ 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
@@ -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_ERROR
import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.RESULT_INTENT import me.msfjarvis.openpgpktx.util.OpenPgpApi.Companion.RESULT_INTENT
import me.msfjarvis.openpgpktx.util.OpenPgpServiceConnection 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.IOpenPgpService2
import org.openintents.openpgp.OpenPgpError import org.openintents.openpgp.OpenPgpError
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
@@ -352,7 +351,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
val data = receivedIntent ?: Intent() val data = receivedIntent ?: Intent()
data.action = ACTION_DECRYPT_VERIFY data.action = ACTION_DECRYPT_VERIFY
val iStream = FileUtils.openInputStream(File(fullPath)) val iStream = File(fullPath).inputStream()
val oStream = ByteArrayOutputStream() val oStream = ByteArrayOutputStream()
lifecycleScope.launch(IO) { lifecycleScope.launch(IO) {
@@ -478,7 +477,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
try { try {
// TODO This might fail, we should check that the write is successful // TODO This might fail, we should check that the write is successful
val file = File(path) val file = File(path)
val outputStream = FileUtils.openOutputStream(file) val outputStream = file.outputStream()
outputStream.write(oStream.toByteArray()) outputStream.write(oStream.toByteArray())
outputStream.close() outputStream.close()
@@ -772,7 +771,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound {
* Gets the name of the password (excluding .gpg) * Gets the name of the password (excluding .gpg)
*/ */
fun getName(fullPath: String): String { fun getName(fullPath: String): String {
return FilenameUtils.getBaseName(fullPath) return FileUtils.getBaseName(fullPath)
} }
/** /**

View File

@@ -16,7 +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 org.apache.commons.io.FileUtils import com.zeapo.pwdstore.utils.FileUtils
import java.io.File import java.io.File
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@@ -61,7 +61,7 @@ class ShowSshKeyFragment : DialogFragment() {
private fun readKeyFromFile() { private fun readKeyFromFile() {
val file = File(requireActivity().filesDir.toString() + "/.ssh_key.pub") val file = File(requireActivity().filesDir.toString() + "/.ssh_key.pub")
try { try {
publicKey.text = FileUtils.readFileToString(file, StandardCharsets.UTF_8) publicKey.text = file.readText()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }

View File

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

View File

@@ -8,7 +8,6 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import org.apache.commons.io.filefilter.FileFilterUtils
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.eclipse.jgit.storage.file.FileRepositoryBuilder
@@ -195,9 +194,9 @@ open class PasswordRepository protected constructor() {
fun getFilesList(path: File?): ArrayList<File> { fun getFilesList(path: File?): ArrayList<File> {
if (path == null || !path.exists()) return 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() ?: emptyArray()).toList()
val files = (path.listFiles(FileFilterUtils.suffixFileFilter(".gpg") as FileFilter) val files = (path.listFiles(FileFilter { pathname -> pathname.extension == "gpg" })
?: emptyArray()).toList() ?: emptyArray()).toList()
val items = ArrayList<File>() val items = ArrayList<File>()

View File

@@ -47,8 +47,6 @@ ext.deps = [
third_party: [ third_party: [
bouncycastle: 'org.bouncycastle:bcprov-jdk15on:1.65', bouncycastle: 'org.bouncycastle:bcprov-jdk15on:1.65',
commons_io: 'commons-io:commons-io:2.5',
commons_codec: 'commons-codec:commons-codec:1.13',
fastscroll: 'me.zhanghai.android.fastscroll:library:1.1.4', fastscroll: 'me.zhanghai.android.fastscroll:library:1.1.4',
jsch: 'com.jcraft:jsch:0.1.55', jsch: 'com.jcraft:jsch:0.1.55',
jgit: 'org.eclipse.jgit:org.eclipse.jgit:3.7.1.201504261725-r', jgit: 'org.eclipse.jgit:org.eclipse.jgit:3.7.1.201504261725-r',