mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-26 20:06:37 -08:00
Fix many upload issues
This commit is contained in:
1 parent
df722925fa
commit
65d318ed88
22 files changed
+422
-219
No files matched your search
@@ -45,8 +45,8 @@ import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.education.EntryActivityEducation
|
||||
import com.kunzisoft.keepass.icons.assignDatabaseIcon
|
||||
import com.kunzisoft.keepass.magikeyboard.MagikIME
|
||||
import com.kunzisoft.keepass.model.AttachmentState
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
import com.kunzisoft.keepass.notifications.AttachmentFileNotificationService
|
||||
import com.kunzisoft.keepass.notifications.ClipboardEntryNotificationService
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_DELETE_ENTRY_HISTORY
|
||||
@@ -212,8 +212,8 @@ class EntryActivity : LockingActivity() {
|
||||
mAttachmentFileBinderManager?.apply {
|
||||
registerProgressTask()
|
||||
onActionTaskListener = object : AttachmentFileNotificationService.ActionTaskListener {
|
||||
override fun onAttachmentAction(fileUri: Uri, attachment: EntryAttachment) {
|
||||
entryContentsView?.updateAttachmentDownloadProgress(attachment)
|
||||
override fun onAttachmentAction(fileUri: Uri, entryAttachmentState: EntryAttachmentState) {
|
||||
entryContentsView?.updateAttachmentDownloadProgress(entryAttachmentState)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,15 +333,8 @@ class EntryActivity : LockingActivity() {
|
||||
|
||||
// Manage attachments
|
||||
entryContentsView?.assignAttachments(entry.getAttachments()) { attachmentItem ->
|
||||
when (attachmentItem.downloadState) {
|
||||
AttachmentState.NULL, AttachmentState.ERROR, AttachmentState.COMPLETE -> {
|
||||
createDocument(this, attachmentItem.name)?.let { requestCode ->
|
||||
mAttachmentsToDownload[requestCode] = attachmentItem
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// TODO Stop download
|
||||
}
|
||||
createDocument(this, attachmentItem.name)?.let { requestCode ->
|
||||
mAttachmentsToDownload[requestCode] = attachmentItem
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,10 +48,7 @@ import com.kunzisoft.keepass.database.element.icon.IconImageStandard
|
||||
import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.education.EntryEditActivityEducation
|
||||
import com.kunzisoft.keepass.model.AttachmentState
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.model.FocusedEditField
|
||||
import com.kunzisoft.keepass.model.*
|
||||
import com.kunzisoft.keepass.notifications.AttachmentFileNotificationService
|
||||
import com.kunzisoft.keepass.notifications.ClipboardEntryNotificationService
|
||||
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK
|
||||
@@ -296,14 +293,16 @@ class EntryEditActivity : LockingActivity(),
|
||||
mAttachmentFileBinderManager?.apply {
|
||||
registerProgressTask()
|
||||
onActionTaskListener = object : AttachmentFileNotificationService.ActionTaskListener {
|
||||
override fun onAttachmentAction(fileUri: Uri, attachment: EntryAttachment) {
|
||||
when (attachment.downloadState) {
|
||||
override fun onAttachmentAction(fileUri: Uri, entryAttachmentState: EntryAttachmentState) {
|
||||
when (entryAttachmentState.downloadState) {
|
||||
AttachmentState.COMPLETE -> {
|
||||
entryEditContentsView?.putAttachment(attachment)
|
||||
entryEditContentsView?.putAttachment(entryAttachmentState)
|
||||
mAttachmentFileBinderManager?.removeAttachmentAction(entryAttachmentState)
|
||||
}
|
||||
AttachmentState.ERROR -> {
|
||||
// TODO error
|
||||
mDatabase?.destroyAttachment(attachment.binaryAttachment)
|
||||
mDatabase?.removeAttachmentIfNotUsed(entryAttachmentState.entryAttachment)
|
||||
mAttachmentFileBinderManager?.removeAttachmentAction(entryAttachmentState)
|
||||
}
|
||||
else -> {
|
||||
// TODO progress
|
||||
@@ -420,8 +419,7 @@ class EntryEditActivity : LockingActivity(),
|
||||
uri?.let { attachmentToUploadUri ->
|
||||
// TODO Async to get the name
|
||||
UriUtil.getFileData(this, attachmentToUploadUri)?.name?.let { fileName ->
|
||||
mDatabase?.buildNewAttachment(applicationContext.filesDir)?.let { binaryAttachment ->
|
||||
val entryAttachment = EntryAttachment(fileName, binaryAttachment)
|
||||
mDatabase?.buildNewAttachment(applicationContext.filesDir, fileName)?.let { entryAttachment ->
|
||||
mAttachmentFileBinderManager?.startUploadAttachment(attachmentToUploadUri, entryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,12 @@ import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.Database
|
||||
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
|
||||
import com.kunzisoft.keepass.model.AttachmentState
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
|
||||
class EntryAttachmentsItemsAdapter(context: Context, private val editable: Boolean)
|
||||
: AnimatedItemsAdapter<EntryAttachment, EntryAttachmentsItemsAdapter.EntryBinariesViewHolder>(context) {
|
||||
: AnimatedItemsAdapter<EntryAttachmentState, EntryAttachmentsItemsAdapter.EntryBinariesViewHolder>(context) {
|
||||
|
||||
var onItemClickListener: ((item: EntryAttachment)->Unit)? = null
|
||||
var onItemClickListener: ((item: EntryAttachmentState)->Unit)? = null
|
||||
|
||||
private val mDatabase = Database.getInstance()
|
||||
|
||||
@@ -44,15 +44,16 @@ class EntryAttachmentsItemsAdapter(context: Context, private val editable: Boole
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: EntryBinariesViewHolder, position: Int) {
|
||||
val entryAttachment = itemsList[position]
|
||||
val entryAttachmentState = itemsList[position]
|
||||
|
||||
holder.itemView.visibility = View.VISIBLE
|
||||
holder.binaryFileTitle.text = entryAttachment.name
|
||||
holder.binaryFileTitle.text = entryAttachmentState.entryAttachment.name
|
||||
holder.binaryFilePath.text = entryAttachmentState.entryAttachment.binaryAttachment.toString()
|
||||
holder.binaryFileSize.text = Formatter.formatFileSize(context,
|
||||
entryAttachment.binaryAttachment.length())
|
||||
entryAttachmentState.entryAttachment.binaryAttachment.length())
|
||||
holder.binaryFileCompression.apply {
|
||||
if (mDatabase.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
|| entryAttachment.binaryAttachment.isCompressed == true) {
|
||||
|| entryAttachmentState.entryAttachment.binaryAttachment.isCompressed == true) {
|
||||
text = CompressionAlgorithm.GZip.getName(context.resources)
|
||||
visibility = View.VISIBLE
|
||||
} else {
|
||||
@@ -64,35 +65,38 @@ class EntryAttachmentsItemsAdapter(context: Context, private val editable: Boole
|
||||
holder.binaryFileProgressContainer.visibility = View.GONE
|
||||
holder.binaryFileDeleteButton.apply {
|
||||
visibility = View.VISIBLE
|
||||
onBindDeleteButton(holder, this, entryAttachment, position)
|
||||
onBindDeleteButton(holder, this, entryAttachmentState, position)
|
||||
}
|
||||
} else {
|
||||
holder.binaryFileProgressContainer.visibility = View.VISIBLE
|
||||
holder.binaryFileDeleteButton.visibility = View.GONE
|
||||
holder.binaryFileProgress.apply {
|
||||
visibility = when (entryAttachment.downloadState) {
|
||||
visibility = when (entryAttachmentState.downloadState) {
|
||||
AttachmentState.NULL, AttachmentState.COMPLETE, AttachmentState.ERROR -> View.GONE
|
||||
AttachmentState.START, AttachmentState.IN_PROGRESS -> View.VISIBLE
|
||||
}
|
||||
progress = entryAttachment.downloadProgression
|
||||
progress = entryAttachmentState.downloadProgression
|
||||
}
|
||||
holder.itemView.setOnClickListener {
|
||||
onItemClickListener?.invoke(entryAttachment)
|
||||
onItemClickListener?.invoke(entryAttachmentState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateProgress(entryAttachment: EntryAttachment) {
|
||||
val indexEntryAttachment = itemsList.indexOfLast { current -> current.name == entryAttachment.name }
|
||||
fun updateProgress(entryAttachment: EntryAttachmentState) {
|
||||
val indexEntryAttachment = itemsList.indexOfLast { current ->
|
||||
current.entryAttachment == entryAttachment.entryAttachment
|
||||
}
|
||||
if (indexEntryAttachment != -1) {
|
||||
itemsList[indexEntryAttachment] = entryAttachment
|
||||
notifyItemChanged(indexEntryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
inner class EntryBinariesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
class EntryBinariesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
|
||||
var binaryFileTitle: TextView = itemView.findViewById(R.id.item_attachment_title)
|
||||
var binaryFilePath: TextView = itemView.findViewById(R.id.item_attachment_path)
|
||||
var binaryFileSize: TextView = itemView.findViewById(R.id.item_attachment_size)
|
||||
var binaryFileCompression: TextView = itemView.findViewById(R.id.item_attachment_compression)
|
||||
var binaryFileProgressContainer: View = itemView.findViewById(R.id.item_attachment_progress_container)
|
||||
|
||||
@@ -34,6 +34,7 @@ open class SaveDatabaseRunnable(protected var context: Context,
|
||||
override fun onStartRun() {}
|
||||
|
||||
override fun onActionRun() {
|
||||
database.removeUnlinkedAttachment()
|
||||
if (saveDatabase && result.isSuccess) {
|
||||
try {
|
||||
database.saveData(context.contentResolver)
|
||||
|
||||
@@ -32,7 +32,6 @@ import com.kunzisoft.keepass.database.element.icon.IconImageFactory
|
||||
import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseOutputException
|
||||
import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException
|
||||
@@ -53,6 +52,7 @@ import com.kunzisoft.keepass.utils.SingletonHolder
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
|
||||
class Database {
|
||||
@@ -429,26 +429,19 @@ class Database {
|
||||
}, omitBackup, max)
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File): BinaryAttachment? {
|
||||
|
||||
val unusedCacheFileName = mDatabaseKDB?.getUnusedCacheFileName()
|
||||
?: mDatabaseKDBX?.getUnusedCacheFileName()
|
||||
|
||||
unusedCacheFileName?.let { cacheFileName ->
|
||||
val fileInCache = File(cacheDirectory, cacheFileName)
|
||||
// TODO protection?
|
||||
val compression = mDatabaseKDBX?.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
val binaryAttachment = BinaryAttachment(fileInCache, false, compression)
|
||||
// add attachment to pool
|
||||
mDatabaseKDBX?.binaryPool?.add(binaryAttachment)
|
||||
return binaryAttachment
|
||||
}
|
||||
return null
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment? {
|
||||
return mDatabaseKDB?.buildNewAttachment(cacheDirectory, fileName)
|
||||
?: mDatabaseKDBX?.buildNewAttachment(cacheDirectory, fileName)
|
||||
}
|
||||
|
||||
fun destroyAttachment(attachment: BinaryAttachment) {
|
||||
// Remove attachment from pool
|
||||
mDatabaseKDBX?.binaryPool?.remove(attachment)
|
||||
fun removeAttachmentIfNotUsed(attachment: EntryAttachment) {
|
||||
// No need in KDB database because unique attachment by entry
|
||||
mDatabaseKDBX?.removeAttachmentIfNotUsed(attachment)
|
||||
}
|
||||
|
||||
fun removeUnlinkedAttachment() {
|
||||
// No check in database KDB because unique attachment by entry
|
||||
mDatabaseKDBX?.removeUnlinkedAttachment()
|
||||
}
|
||||
|
||||
@Throws(DatabaseOutputException::class)
|
||||
|
||||
@@ -33,7 +33,6 @@ import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.node.Type
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryInfo
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.otp.OtpElement
|
||||
@@ -325,41 +324,30 @@ class Entry : Node, EntryVersionedInterface<Group> {
|
||||
entryKDBX?.stopToManageFieldReferences()
|
||||
}
|
||||
|
||||
fun getAttachments(): ArrayList<EntryAttachment> {
|
||||
override fun getAttachments(): ArrayList<EntryAttachment> {
|
||||
val attachments = ArrayList<EntryAttachment>()
|
||||
|
||||
entryKDB?.binaryData?.let { binaryKDB ->
|
||||
attachments.add(EntryAttachment(entryKDB?.binaryDescription ?: "", binaryKDB))
|
||||
entryKDB?.getAttachments()?.let {
|
||||
attachments.addAll(it)
|
||||
}
|
||||
|
||||
entryKDBX?.binaries?.let { binariesKDBX ->
|
||||
for ((key, value) in binariesKDBX) {
|
||||
attachments.add(EntryAttachment(key, value))
|
||||
}
|
||||
entryKDBX?.getAttachments()?.let {
|
||||
attachments.addAll(it)
|
||||
}
|
||||
|
||||
return attachments
|
||||
}
|
||||
|
||||
fun putAttachment(attachment: EntryAttachment) {
|
||||
entryKDB?.apply {
|
||||
binaryDescription = attachment.name
|
||||
binaryData = attachment.binaryAttachment
|
||||
}
|
||||
|
||||
entryKDBX?.putProtectedBinary(attachment.name, attachment.binaryAttachment)
|
||||
override fun containsAttachment(attachment: EntryAttachment): Boolean {
|
||||
return entryKDB?.containsAttachment(attachment) == true
|
||||
|| entryKDBX?.containsAttachment(attachment) == true
|
||||
}
|
||||
|
||||
fun removeAttachment(attachment: EntryAttachment) {
|
||||
entryKDB?.apply {
|
||||
if (binaryDescription == attachment.name
|
||||
&& binaryData == attachment.binaryAttachment) {
|
||||
binaryDescription = ""
|
||||
binaryData = null
|
||||
}
|
||||
}
|
||||
override fun putAttachment(attachment: EntryAttachment) {
|
||||
entryKDB?.putAttachment(attachment)
|
||||
entryKDBX?.putAttachment(attachment)
|
||||
}
|
||||
|
||||
entryKDBX?.removeProtectedBinary(attachment.name)
|
||||
override fun removeAttachment(attachment: EntryAttachment) {
|
||||
entryKDB?.removeAttachment(attachment)
|
||||
entryKDBX?.removeAttachment(attachment)
|
||||
}
|
||||
|
||||
fun getHistory(): ArrayList<Entry> {
|
||||
|
||||
+8
-15
@@ -17,30 +17,23 @@
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.model
|
||||
package com.kunzisoft.keepass.database.element
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.utils.readEnum
|
||||
import com.kunzisoft.keepass.utils.writeEnum
|
||||
|
||||
data class EntryAttachment(var name: String,
|
||||
var binaryAttachment: BinaryAttachment,
|
||||
var downloadState: AttachmentState = AttachmentState.NULL,
|
||||
var downloadProgression: Int = 0) : Parcelable {
|
||||
var binaryAttachment: BinaryAttachment) : Parcelable {
|
||||
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.readString() ?: "",
|
||||
parcel.readParcelable(BinaryAttachment::class.java.classLoader) ?: BinaryAttachment(),
|
||||
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL,
|
||||
parcel.readInt())
|
||||
parcel.readParcelable(BinaryAttachment::class.java.classLoader) ?: BinaryAttachment()
|
||||
)
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(name)
|
||||
parcel.writeParcelable(binaryAttachment, flags)
|
||||
parcel.writeEnum(downloadState)
|
||||
parcel.writeInt(downloadProgression)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
@@ -63,6 +56,10 @@ data class EntryAttachment(var name: String,
|
||||
return result
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "$name at $binaryAttachment"
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<EntryAttachment> {
|
||||
override fun createFromParcel(parcel: Parcel): EntryAttachment {
|
||||
return EntryAttachment(parcel)
|
||||
@@ -72,8 +69,4 @@ data class EntryAttachment(var name: String,
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class AttachmentState {
|
||||
NULL, START, IN_PROGRESS, COMPLETE, ERROR
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
package com.kunzisoft.keepass.database.element.database
|
||||
|
||||
import android.util.SparseArray
|
||||
import androidx.core.util.forEach
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import java.io.IOException
|
||||
|
||||
@@ -34,20 +35,22 @@ class BinaryPool {
|
||||
pool.put(key, value)
|
||||
}
|
||||
|
||||
fun add(fileBinary: BinaryAttachment) {
|
||||
if (findKey(fileBinary) == null) {
|
||||
pool.put(findUnusedKey(), fileBinary)
|
||||
fun add(binaryAttachment: BinaryAttachment) {
|
||||
if (findKey(binaryAttachment) == null) {
|
||||
pool.put(findUnusedKey(), binaryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
fun remove(fileBinary: BinaryAttachment) {
|
||||
findKey(fileBinary)?.let {
|
||||
@Throws(IOException::class)
|
||||
fun remove(binaryAttachment: BinaryAttachment) {
|
||||
findKey(binaryAttachment)?.let {
|
||||
pool.remove(it)
|
||||
}
|
||||
binaryAttachment.clear()
|
||||
}
|
||||
|
||||
fun findUnusedKey(): Int {
|
||||
var unusedKey = pool.size()
|
||||
var unusedKey = 0
|
||||
while (pool[unusedKey] != null)
|
||||
unusedKey++
|
||||
return unusedKey
|
||||
@@ -57,21 +60,22 @@ class BinaryPool {
|
||||
* Return position of [binaryAttachmentToRetrieve] or null if not found
|
||||
*/
|
||||
fun findKey(binaryAttachmentToRetrieve: BinaryAttachment): Int? {
|
||||
for (i in 0 until pool.size()) {
|
||||
if (pool.get(pool.keyAt(i)) == binaryAttachmentToRetrieve) return i
|
||||
}
|
||||
return null
|
||||
val index = pool.indexOfValue(binaryAttachmentToRetrieve)
|
||||
return if (index < 0)
|
||||
null
|
||||
else
|
||||
pool.keyAt(index)
|
||||
}
|
||||
|
||||
fun doForEachBinary(action: (key: Int, binary: BinaryAttachment) -> Unit) {
|
||||
for (i in 0 until pool.size()) {
|
||||
action.invoke(i, pool.get(pool.keyAt(i)))
|
||||
pool.forEach { key, binaryAttachment ->
|
||||
action.invoke(key, binaryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun clear() {
|
||||
doForEachBinary { _, binary ->
|
||||
pool.forEach { _, binary ->
|
||||
binary.clear()
|
||||
}
|
||||
pool.clear()
|
||||
|
||||
@@ -26,8 +26,11 @@ import com.kunzisoft.keepass.database.element.entry.EntryKDB
|
||||
import com.kunzisoft.keepass.database.element.group.GroupKDB
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.stream.NullOutputStream
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.security.DigestOutputStream
|
||||
@@ -254,6 +257,12 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
|
||||
return System.currentTimeMillis().toString()
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment? {
|
||||
val fileInCache = File(cacheDirectory, getUnusedCacheFileName())
|
||||
val binaryAttachment = BinaryAttachment(fileInCache)
|
||||
return EntryAttachment(fileName, binaryAttachment)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TYPE = DatabaseKDB::class.java
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.kunzisoft.keepass.crypto.engine.CipherEngine
|
||||
import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine
|
||||
import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory
|
||||
import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters
|
||||
import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDB.Companion.BACKUP_FOLDER_TITLE
|
||||
@@ -37,15 +38,18 @@ import com.kunzisoft.keepass.database.element.group.GroupKDBX
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImageCustom
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.node.NodeVersioned
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_3
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_4
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.VariantDictionary
|
||||
import org.w3c.dom.Node
|
||||
import org.w3c.dom.Text
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
@@ -536,10 +540,56 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
return publicCustomData.size() > 0
|
||||
}
|
||||
|
||||
// TODO encapsulate
|
||||
fun getUnusedCacheFileName(): String {
|
||||
return binaryPool.findUnusedKey().toString()
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment {
|
||||
val cacheId = getUnusedCacheFileName()
|
||||
val fileInCache = File(cacheDirectory, cacheId)
|
||||
// TODO protection?
|
||||
val compression = compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
val binaryAttachment = BinaryAttachment(fileInCache, false, compression)
|
||||
// add attachment to pool
|
||||
binaryPool.put(cacheId.toInt(), binaryAttachment)
|
||||
return EntryAttachment(fileName, binaryAttachment)
|
||||
}
|
||||
|
||||
fun removeAttachmentIfNotUsed(attachment: EntryAttachment) {
|
||||
// Remove attachment from pool
|
||||
removeUnlinkedAttachment(attachment.binaryAttachment)
|
||||
}
|
||||
|
||||
fun removeUnlinkedAttachment(vararg binaries: BinaryAttachment) {
|
||||
// Build binaries to remove with all binaries known
|
||||
val binariesToRemove = ArrayList<BinaryAttachment>()
|
||||
if (binaries.isEmpty()) {
|
||||
binaryPool.doForEachBinary { _, binary ->
|
||||
binariesToRemove.add(binary)
|
||||
}
|
||||
} else {
|
||||
binariesToRemove.addAll(binaries)
|
||||
}
|
||||
// Remove binaries from the list
|
||||
rootGroup?.doForEachChild(object : NodeHandler<EntryKDBX>() {
|
||||
override fun operate(node: EntryKDBX): Boolean {
|
||||
node.getAttachments().forEach {
|
||||
binariesToRemove.remove(it.binaryAttachment)
|
||||
}
|
||||
return binariesToRemove.isNotEmpty()
|
||||
}
|
||||
}, null)
|
||||
// Effective removing
|
||||
binariesToRemove.forEach {
|
||||
try {
|
||||
binaryPool.remove(it)
|
||||
} catch (e: java.lang.Exception) {
|
||||
Log.w(TAG, "Unable to clean binaries", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun validatePasswordEncoding(password: String?, containsKeyFile: Boolean): Boolean {
|
||||
if (password == null)
|
||||
return true
|
||||
|
||||
@@ -27,7 +27,9 @@ import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.node.NodeKDBInterface
|
||||
import com.kunzisoft.keepass.database.element.node.Type
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
/**
|
||||
* Structure containing information about one entry.
|
||||
@@ -135,6 +137,30 @@ class EntryKDB : EntryVersioned<Int, UUID, GroupKDB, EntryKDB>, NodeKDBInterface
|
||||
override val type: Type
|
||||
get() = Type.ENTRY
|
||||
|
||||
override fun getAttachments(): ArrayList<EntryAttachment> {
|
||||
return ArrayList<EntryAttachment>().apply {
|
||||
val binary = binaryData
|
||||
if (binary != null)
|
||||
add(EntryAttachment(binaryDescription, binary))
|
||||
}
|
||||
}
|
||||
|
||||
override fun containsAttachment(attachment: EntryAttachment): Boolean {
|
||||
return binaryData != null
|
||||
}
|
||||
|
||||
override fun putAttachment(attachment: EntryAttachment) {
|
||||
this.binaryDescription = attachment.name
|
||||
this.binaryData = attachment.binaryAttachment
|
||||
}
|
||||
|
||||
override fun removeAttachment(attachment: EntryAttachment) {
|
||||
if (this.binaryDescription == attachment.name) {
|
||||
this.binaryDescription = ""
|
||||
this.binaryData = null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
/** Size of byte buffer needed to hold this struct. */
|
||||
|
||||
@@ -33,9 +33,11 @@ import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface
|
||||
import com.kunzisoft.keepass.database.element.node.Type
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.utils.ParcelableUtil
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.collections.LinkedHashMap
|
||||
|
||||
class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInterface {
|
||||
@@ -60,8 +62,9 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
}
|
||||
var iconCustom = IconImageCustom.UNKNOWN_ICON
|
||||
private var customData = LinkedHashMap<String, String>()
|
||||
// TODO Private
|
||||
var fields = LinkedHashMap<String, ProtectedString>()
|
||||
var binaries = LinkedHashMap<String, BinaryAttachment>()
|
||||
private var binaries = LinkedHashMap<String, BinaryAttachment>()
|
||||
var foregroundColor = ""
|
||||
var backgroundColor = ""
|
||||
var overrideURL = ""
|
||||
@@ -284,14 +287,31 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
fields[label] = value
|
||||
}
|
||||
|
||||
fun putProtectedBinary(key: String, value: BinaryAttachment) {
|
||||
binaries[key] = value
|
||||
override fun getAttachments(): ArrayList<EntryAttachment> {
|
||||
val entryAttachmentList = ArrayList<EntryAttachment>()
|
||||
for ((key, value) in binaries) {
|
||||
entryAttachmentList.add(EntryAttachment(key, value))
|
||||
}
|
||||
return entryAttachmentList
|
||||
}
|
||||
|
||||
fun removeProtectedBinary(name: String) {
|
||||
binaries.remove(name)
|
||||
override fun containsAttachment(attachment: EntryAttachment): Boolean {
|
||||
for ((_, binary) in binaries) {
|
||||
if (binary == attachment.binaryAttachment)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun putAttachment(attachment: EntryAttachment) {
|
||||
binaries[attachment.name] = attachment.binaryAttachment
|
||||
}
|
||||
|
||||
override fun removeAttachment(attachment: EntryAttachment) {
|
||||
binaries.remove(attachment.name)
|
||||
}
|
||||
|
||||
// TODO Remove ?
|
||||
fun sizeOfHistory(): Int {
|
||||
return history.size
|
||||
}
|
||||
|
||||
+9
@@ -20,6 +20,7 @@
|
||||
package com.kunzisoft.keepass.database.element.entry
|
||||
|
||||
import com.kunzisoft.keepass.database.element.node.NodeVersionedInterface
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
|
||||
interface EntryVersionedInterface<ParentGroup> : NodeVersionedInterface<ParentGroup> {
|
||||
|
||||
@@ -30,4 +31,12 @@ interface EntryVersionedInterface<ParentGroup> : NodeVersionedInterface<ParentGr
|
||||
var url: String
|
||||
|
||||
var notes: String
|
||||
|
||||
fun getAttachments(): ArrayList<EntryAttachment>
|
||||
|
||||
fun containsAttachment(attachment: EntryAttachment): Boolean
|
||||
|
||||
fun putAttachment(attachment: EntryAttachment)
|
||||
|
||||
fun removeAttachment(attachment: EntryAttachment)
|
||||
}
|
||||
@@ -171,6 +171,10 @@ class BinaryAttachment : Parcelable {
|
||||
return result
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return dataFile.toString()
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ import com.kunzisoft.keepass.database.exception.*
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
|
||||
import com.kunzisoft.keepass.database.file.DatabaseKDBXXML
|
||||
import com.kunzisoft.keepass.database.file.DateKDBXUtil
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.stream.*
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
@@ -249,14 +250,15 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
val protectedFlag = dataInputStream.readBytes(1)[0].toInt() != 0
|
||||
val byteLength = size - 1
|
||||
// Read in a file
|
||||
val fileInCache = File(cacheDirectory, mDatabase.getUnusedCacheFileName())
|
||||
val cacheId = mDatabase.getUnusedCacheFileName()
|
||||
val fileInCache = File(cacheDirectory, cacheId)
|
||||
FileOutputStream(fileInCache).use { outputStream ->
|
||||
dataInputStream.readBytes(byteLength, DatabaseKDBX.BUFFER_SIZE_BYTES) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
}
|
||||
}
|
||||
val protectedBinary = BinaryAttachment(fileInCache, protectedFlag)
|
||||
mDatabase.binaryPool.add(protectedBinary)
|
||||
mDatabase.binaryPool.put(cacheId.toInt(), protectedBinary)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,14 +441,7 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
}
|
||||
|
||||
KdbContext.Binaries -> if (name.equals(DatabaseKDBXXML.ElemBinary, ignoreCase = true)) {
|
||||
val key = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrId)
|
||||
if (key != null) {
|
||||
val pbData = readBinary(xpp)
|
||||
val id = Integer.parseInt(key)
|
||||
mDatabase.binaryPool.put(id, pbData!!)
|
||||
} else {
|
||||
readUnknown(xpp)
|
||||
}
|
||||
readBinary(xpp)
|
||||
} else {
|
||||
readUnknown(xpp)
|
||||
}
|
||||
@@ -763,7 +758,7 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
return KdbContext.Entry
|
||||
} else if (ctx == KdbContext.EntryBinary && name.equals(DatabaseKDBXXML.ElemBinary, ignoreCase = true)) {
|
||||
if (ctxBinaryName != null && ctxBinaryValue != null)
|
||||
ctxEntry?.putProtectedBinary(ctxBinaryName!!, ctxBinaryValue!!)
|
||||
ctxEntry?.putAttachment(EntryAttachment(ctxBinaryName!!, ctxBinaryValue!!))
|
||||
ctxBinaryName = null
|
||||
ctxBinaryValue = null
|
||||
|
||||
@@ -950,39 +945,59 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
return mDatabase.binaryPool[id]
|
||||
}
|
||||
|
||||
val key = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrId)
|
||||
if (key != null) {
|
||||
val binaryId = Integer.parseInt(key)
|
||||
createBinary(binaryId.toString(), xpp)?.let {
|
||||
mDatabase.binaryPool.put(binaryId, it)
|
||||
return it
|
||||
}
|
||||
}
|
||||
|
||||
// New binary to retrieve
|
||||
else {
|
||||
var compressed = false
|
||||
var protected = false
|
||||
val binaryId = mDatabase.getUnusedCacheFileName()
|
||||
createBinary(binaryId, xpp)?.let {
|
||||
mDatabase.binaryPool.put(binaryId.toInt(), it)
|
||||
return it
|
||||
}
|
||||
}
|
||||
|
||||
if (xpp.attributeCount > 0) {
|
||||
val compress = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrCompressed)
|
||||
if (compress != null) {
|
||||
compressed = compress.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected)
|
||||
if (protect != null) {
|
||||
protected = protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)
|
||||
}
|
||||
@Throws(IOException::class, XmlPullParserException::class)
|
||||
private fun createBinary(binaryId: String, xpp: XmlPullParser): BinaryAttachment? {
|
||||
var compressed = false
|
||||
var protected = false
|
||||
|
||||
if (xpp.attributeCount > 0) {
|
||||
val compress = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrCompressed)
|
||||
if (compress != null) {
|
||||
compressed = compress.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)
|
||||
}
|
||||
|
||||
val base64 = readString(xpp)
|
||||
if (base64.isEmpty())
|
||||
return BinaryAttachment()
|
||||
val data = Base64.decode(base64, BASE_64_FLAG)
|
||||
val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected)
|
||||
if (protect != null) {
|
||||
protected = protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)
|
||||
}
|
||||
}
|
||||
|
||||
val file = File(cacheDirectory, mDatabase.getUnusedCacheFileName())
|
||||
return FileOutputStream(file).use { outputStream ->
|
||||
// Force compression in this specific case
|
||||
if (mDatabase.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
&& !compressed) {
|
||||
GZIPOutputStream(outputStream).write(data)
|
||||
BinaryAttachment(file, protected, true)
|
||||
} else {
|
||||
outputStream.write(data)
|
||||
BinaryAttachment(file, protected, compressed)
|
||||
}
|
||||
val base64 = readString(xpp)
|
||||
if (base64.isEmpty())
|
||||
return null
|
||||
val data = Base64.decode(base64, BASE_64_FLAG)
|
||||
|
||||
val file = File(cacheDirectory, binaryId)
|
||||
return FileOutputStream(file).use { outputStream ->
|
||||
// Force compression in this specific case
|
||||
if (mDatabase.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
&& !compressed) {
|
||||
GZIPOutputStream(outputStream).write(data)
|
||||
BinaryAttachment(file, protected, true)
|
||||
} else {
|
||||
outputStream.write(data)
|
||||
BinaryAttachment(file, protected, compressed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import com.kunzisoft.keepass.database.exception.UnknownKDF
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
|
||||
import com.kunzisoft.keepass.database.file.DatabaseKDBXXML
|
||||
import com.kunzisoft.keepass.database.file.DateKDBXUtil
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.stream.*
|
||||
import org.bouncycastle.crypto.StreamCipher
|
||||
import org.joda.time.DateTime
|
||||
@@ -359,7 +360,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
|
||||
writeTimes(entry)
|
||||
|
||||
writeFields(entry.fields)
|
||||
writeEntryBinaries(entry.binaries)
|
||||
writeEntryBinaries(entry.getAttachments())
|
||||
writeAutoType(entry.autoType)
|
||||
|
||||
if (!isHistory) {
|
||||
@@ -559,19 +560,19 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
|
||||
}
|
||||
|
||||
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class)
|
||||
private fun writeEntryBinaries(binaries: Map<String, BinaryAttachment>) {
|
||||
for ((key, binary) in binaries) {
|
||||
private fun writeEntryBinaries(entryAttachments: List<EntryAttachment>) {
|
||||
entryAttachments.forEach {
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemBinary)
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemKey)
|
||||
xml.text(safeXmlString(key))
|
||||
xml.text(safeXmlString(it.name))
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemKey)
|
||||
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemValue)
|
||||
val ref = mDatabaseKDBX.binaryPool.findKey(binary)
|
||||
val ref = mDatabaseKDBX.binaryPool.findKey(it.binaryAttachment)
|
||||
if (ref != null) {
|
||||
xml.attribute(null, DatabaseKDBXXML.AttrRef, ref.toString())
|
||||
} else {
|
||||
writeBinary(binary)
|
||||
writeBinary(it.binaryAttachment)
|
||||
}
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemValue)
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.model
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.utils.readEnum
|
||||
import com.kunzisoft.keepass.utils.writeEnum
|
||||
|
||||
data class EntryAttachmentState(var entryAttachment: EntryAttachment,
|
||||
var downloadState: AttachmentState = AttachmentState.NULL,
|
||||
var downloadProgression: Int = 0) : Parcelable {
|
||||
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.readParcelable(EntryAttachment::class.java.classLoader) ?: EntryAttachment("", BinaryAttachment()),
|
||||
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL,
|
||||
parcel.readInt())
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeParcelable(entryAttachment, flags)
|
||||
parcel.writeEnum(downloadState)
|
||||
parcel.writeInt(downloadProgression)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is EntryAttachmentState) return false
|
||||
|
||||
if (entryAttachment != other.entryAttachment) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return entryAttachment.hashCode()
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<EntryAttachmentState> {
|
||||
override fun createFromParcel(parcel: Parcel): EntryAttachmentState {
|
||||
return EntryAttachmentState(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<EntryAttachmentState?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class AttachmentState {
|
||||
NULL, START, IN_PROGRESS, COMPLETE, ERROR
|
||||
}
|
||||
+47
-40
@@ -30,7 +30,8 @@ import androidx.documentfile.provider.DocumentFile
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.model.AttachmentState
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
import com.kunzisoft.keepass.stream.readBytes
|
||||
import com.kunzisoft.keepass.timeout.TimeoutHelper
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
@@ -45,6 +46,7 @@ import kotlin.collections.ArrayList
|
||||
class AttachmentFileNotificationService: LockNotificationService() {
|
||||
|
||||
override val notificationId: Int = 10000
|
||||
private val attachmentNotificationList = ArrayList<AttachmentNotification>()
|
||||
|
||||
private var mActionTaskBinder = ActionTaskBinder()
|
||||
private var mActionTaskListeners = LinkedList<ActionTaskListener>()
|
||||
@@ -71,16 +73,19 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
}
|
||||
|
||||
private val attachmentFileActionListener = object: AttachmentFileAction.AttachmentFileActionListener {
|
||||
override fun onUpdate(uri: Uri, entryAttachment: EntryAttachment, notificationId: Int) {
|
||||
newNotification(uri, entryAttachment, notificationId)
|
||||
override fun onUpdate(attachmentNotification: AttachmentNotification) {
|
||||
newNotification(attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachmentState,
|
||||
attachmentNotification.notificationId)
|
||||
mActionTaskListeners.forEach { actionListener ->
|
||||
actionListener.onAttachmentAction(uri, entryAttachment)
|
||||
actionListener.onAttachmentAction(attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachmentState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ActionTaskListener {
|
||||
fun onAttachmentAction(fileUri: Uri, attachment: EntryAttachment)
|
||||
fun onAttachmentAction(fileUri: Uri, entryAttachmentState: EntryAttachmentState)
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
@@ -126,14 +131,22 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
mActionTaskListeners.forEach { actionListener ->
|
||||
actionListener.onAttachmentAction(
|
||||
attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachment
|
||||
attachmentNotification.entryAttachmentState
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun removeAttachmentAction(entryAttachment: EntryAttachmentState) {
|
||||
attachmentNotificationList.firstOrNull {
|
||||
it.entryAttachmentState == entryAttachment
|
||||
}?.let {
|
||||
attachmentNotificationList.remove(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun newNotification(downloadFileUri: Uri,
|
||||
entryAttachment: EntryAttachment,
|
||||
entryAttachment: EntryAttachmentState,
|
||||
notificationIdAttachment: Int) {
|
||||
|
||||
val pendingContentIntent = PendingIntent.getActivity(this,
|
||||
@@ -179,7 +192,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
}
|
||||
}
|
||||
}
|
||||
notificationManager?.notify(notificationIdAttachment, builder.build())
|
||||
startForeground(notificationIdAttachment, builder.build())
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
@@ -187,13 +200,14 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
attachmentNotification.attachmentFileAction?.listener = null
|
||||
notificationManager?.cancel(attachmentNotification.notificationId)
|
||||
}
|
||||
attachmentNotificationList.clear()
|
||||
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private data class AttachmentNotification(var uri: Uri,
|
||||
var notificationId: Int,
|
||||
var entryAttachment: EntryAttachment,
|
||||
var entryAttachmentState: EntryAttachmentState,
|
||||
var attachmentFileAction: AttachmentFileAction? = null) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
@@ -221,7 +235,8 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
|
||||
val nextNotificationId = (attachmentNotificationList.maxByOrNull { it.notificationId }
|
||||
?.notificationId ?: notificationId) + 1
|
||||
val attachmentNotification = AttachmentNotification(downloadFileUri, nextNotificationId, entryAttachment)
|
||||
val entryAttachmentState = EntryAttachmentState(entryAttachment)
|
||||
val attachmentNotification = AttachmentNotification(downloadFileUri, nextNotificationId, entryAttachmentState)
|
||||
attachmentNotificationList.add(attachmentNotification)
|
||||
|
||||
mainScope.launch {
|
||||
@@ -252,7 +267,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
}
|
||||
|
||||
interface AttachmentFileActionListener {
|
||||
fun onUpdate(uri: Uri, entryAttachment: EntryAttachment, notificationId: Int)
|
||||
fun onUpdate(attachmentNotification: AttachmentNotification)
|
||||
}
|
||||
|
||||
suspend fun executeAction() {
|
||||
@@ -260,28 +275,25 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
|
||||
// on pre execute
|
||||
attachmentNotification.attachmentFileAction = this
|
||||
attachmentNotification.entryAttachment.apply {
|
||||
attachmentNotification.entryAttachmentState.apply {
|
||||
downloadState = AttachmentState.START
|
||||
downloadProgression = 0
|
||||
}
|
||||
listener?.onUpdate(
|
||||
attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachment,
|
||||
attachmentNotification.notificationId)
|
||||
listener?.onUpdate(attachmentNotification)
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
// on Progress with thread
|
||||
val asyncResult: Deferred<Boolean> = async {
|
||||
var progressResult = true
|
||||
try {
|
||||
attachmentNotification.entryAttachment.apply {
|
||||
attachmentNotification.entryAttachmentState.apply {
|
||||
downloadState = AttachmentState.IN_PROGRESS
|
||||
|
||||
when (streamDirection) {
|
||||
StreamDirection.UPLOAD -> {
|
||||
uploadToDatabase(
|
||||
attachmentNotification.uri,
|
||||
this.binaryAttachment,
|
||||
entryAttachment.binaryAttachment,
|
||||
contentResolver, 1024) { percent ->
|
||||
publishProgress(percent)
|
||||
}
|
||||
@@ -289,7 +301,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
StreamDirection.DOWNLOAD -> {
|
||||
downloadFromDatabase(
|
||||
attachmentNotification.uri,
|
||||
this.binaryAttachment,
|
||||
entryAttachment.binaryAttachment,
|
||||
contentResolver, 1024) { percent ->
|
||||
publishProgress(percent)
|
||||
}
|
||||
@@ -306,14 +318,11 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
withContext(Dispatchers.Main) {
|
||||
val result = asyncResult.await()
|
||||
attachmentNotification.attachmentFileAction = null
|
||||
attachmentNotification.entryAttachment.apply {
|
||||
attachmentNotification.entryAttachmentState.apply {
|
||||
downloadState = if (result) AttachmentState.COMPLETE else AttachmentState.ERROR
|
||||
downloadProgression = 100
|
||||
}
|
||||
listener?.onUpdate(
|
||||
attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachment,
|
||||
attachmentNotification.notificationId)
|
||||
listener?.onUpdate(attachmentNotification)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -324,9 +333,8 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
contentResolver: ContentResolver,
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE,
|
||||
update: ((percent: Int)->Unit)? = null) {
|
||||
|
||||
var dataDownloaded = 0
|
||||
|
||||
var dataDownloaded = 0L
|
||||
val fileSize = binaryAttachment.length()
|
||||
val attachmentOutputStream = UriUtil.getUriOutputStream(contentResolver, attachmentToUploadUri)
|
||||
attachmentOutputStream.use { outputStream ->
|
||||
outputStream?.let { fileOutputStream ->
|
||||
@@ -339,9 +347,11 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
fileOutputStream.write(buffer)
|
||||
dataDownloaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataDownloaded / binaryAttachment.length()).toInt()
|
||||
val percentDownload = (100 * dataDownloaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -353,8 +363,8 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
contentResolver: ContentResolver,
|
||||
bufferSize: Int = DEFAULT_BUFFER_SIZE,
|
||||
update: ((percent: Int)->Unit)? = null) {
|
||||
|
||||
var dataUploaded = 0
|
||||
var dataUploaded = 0L
|
||||
val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0
|
||||
val attachmentInputStream = UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)
|
||||
attachmentInputStream.use { inputStream ->
|
||||
inputStream?.let { fileInputStream ->
|
||||
@@ -368,9 +378,11 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
outputStream.write(buffer)
|
||||
dataUploaded += buffer.size
|
||||
try {
|
||||
val percentDownload = (100 * dataUploaded / binaryAttachment.length()).toInt()
|
||||
val percentDownload = (100 * dataUploaded / fileSize).toInt()
|
||||
update?.invoke(percentDownload)
|
||||
} catch (e: Exception) {}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -381,15 +393,12 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
// Publish progress
|
||||
val currentTime = System.currentTimeMillis()
|
||||
if (previousSaveTime + updateMinFrequency < currentTime) {
|
||||
attachmentNotification.entryAttachment.apply {
|
||||
attachmentNotification.entryAttachmentState.apply {
|
||||
downloadState = AttachmentState.IN_PROGRESS
|
||||
downloadProgression = percent
|
||||
}
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
listener?.onUpdate(
|
||||
attachmentNotification.uri,
|
||||
attachmentNotification.entryAttachment,
|
||||
attachmentNotification.notificationId)
|
||||
listener?.onUpdate(attachmentNotification)
|
||||
Log.d(TAG, "Download file ${attachmentNotification.uri} : $percent%")
|
||||
}
|
||||
previousSaveTime = currentTime
|
||||
@@ -409,8 +418,6 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
|
||||
const val FILE_URI_KEY = "FILE_URI_KEY"
|
||||
const val ATTACHMENT_KEY = "ATTACHMENT_KEY"
|
||||
|
||||
private val attachmentNotificationList = ArrayList<AttachmentNotification>()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,7 +28,8 @@ import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
import com.kunzisoft.keepass.notifications.AttachmentFileNotificationService
|
||||
import com.kunzisoft.keepass.notifications.AttachmentFileNotificationService.Companion.ACTION_ATTACHMENT_FILE_START_DOWNLOAD
|
||||
import com.kunzisoft.keepass.notifications.AttachmentFileNotificationService.Companion.ACTION_ATTACHMENT_FILE_START_UPLOAD
|
||||
@@ -44,8 +45,8 @@ class AttachmentFileBinderManager(private val activity: FragmentActivity) {
|
||||
private var mServiceConnection: ServiceConnection? = null
|
||||
|
||||
private val mActionTaskListener = object: AttachmentFileNotificationService.ActionTaskListener {
|
||||
override fun onAttachmentAction(fileUri: Uri, attachment: EntryAttachment) {
|
||||
onActionTaskListener?.onAttachmentAction(fileUri, attachment)
|
||||
override fun onAttachmentAction(fileUri: Uri, entryAttachmentState: EntryAttachmentState) {
|
||||
onActionTaskListener?.onAttachmentAction(fileUri, entryAttachmentState)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,15 +87,17 @@ class AttachmentFileBinderManager(private val activity: FragmentActivity) {
|
||||
mServiceConnection = null
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun removeAttachmentAction(attachment: EntryAttachmentState) {
|
||||
mBinder?.getService()?.removeAttachmentAction(attachment)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun start(bundle: Bundle? = null, actionTask: String) {
|
||||
activity.stopService(mIntentTask)
|
||||
if (bundle != null)
|
||||
mIntentTask.putExtras(bundle)
|
||||
activity.runOnUiThread {
|
||||
mIntentTask.action = actionTask
|
||||
activity.startService(mIntentTask)
|
||||
}
|
||||
mIntentTask.action = actionTask
|
||||
activity.startService(mIntentTask)
|
||||
}
|
||||
|
||||
fun startUploadAttachment(uploadFileUri: Uri,
|
||||
|
||||
@@ -37,7 +37,8 @@ import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.Entry
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.database.search.UuidUtil
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
import com.kunzisoft.keepass.otp.OtpElement
|
||||
import com.kunzisoft.keepass.otp.OtpType
|
||||
import java.util.*
|
||||
@@ -318,13 +319,13 @@ class EntryContentsView @JvmOverloads constructor(context: Context,
|
||||
fun assignAttachments(attachments: ArrayList<EntryAttachment>,
|
||||
onAttachmentClicked: (attachment: EntryAttachment)->Unit) {
|
||||
showAttachments(attachments.isNotEmpty())
|
||||
attachmentsAdapter.assignItems(attachments)
|
||||
attachmentsAdapter.assignItems(attachments.map { EntryAttachmentState(it) })
|
||||
attachmentsAdapter.onItemClickListener = { item ->
|
||||
onAttachmentClicked.invoke(item)
|
||||
onAttachmentClicked.invoke(item.entryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAttachmentDownloadProgress(attachmentToDownload: EntryAttachment) {
|
||||
fun updateAttachmentDownloadProgress(attachmentToDownload: EntryAttachmentState) {
|
||||
attachmentsAdapter.updateProgress(attachmentToDownload)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,8 @@ import com.kunzisoft.keepass.database.element.icon.IconImage
|
||||
import com.kunzisoft.keepass.icons.IconDrawableFactory
|
||||
import com.kunzisoft.keepass.icons.assignDatabaseIcon
|
||||
import com.kunzisoft.keepass.icons.assignDefaultDatabaseIcon
|
||||
import com.kunzisoft.keepass.model.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.model.EntryAttachmentState
|
||||
import com.kunzisoft.keepass.model.Field
|
||||
import com.kunzisoft.keepass.model.FocusedEditField
|
||||
import org.joda.time.Duration
|
||||
@@ -242,7 +243,7 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
* -------------
|
||||
*/
|
||||
|
||||
fun getExtraFields(): MutableList<Field> {
|
||||
fun getExtraFields(): List<Field> {
|
||||
return extraFieldsAdapter.itemsList
|
||||
}
|
||||
|
||||
@@ -278,20 +279,20 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
|
||||
* -------------
|
||||
*/
|
||||
|
||||
fun getAttachments(): MutableList<EntryAttachment> {
|
||||
return attachmentsAdapter.itemsList
|
||||
fun getAttachments(): List<EntryAttachment> {
|
||||
return attachmentsAdapter.itemsList.map { it.entryAttachment }
|
||||
}
|
||||
|
||||
fun assignAttachments(attachments: ArrayList<EntryAttachment>,
|
||||
onDeleteItem: (attachment: EntryAttachment)->Unit) {
|
||||
attachmentsContainerView.visibility = if (attachments.isEmpty()) View.GONE else View.VISIBLE
|
||||
attachmentsAdapter.assignItems(attachments)
|
||||
attachmentsAdapter.assignItems(attachments.map { EntryAttachmentState(it) })
|
||||
attachmentsAdapter.onDeleteButtonClickListener = { item ->
|
||||
onDeleteItem.invoke(item)
|
||||
onDeleteItem.invoke(item.entryAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
fun putAttachment(attachment: EntryAttachment) {
|
||||
fun putAttachment(attachment: EntryAttachmentState) {
|
||||
attachmentsContainerView.visibility = View.VISIBLE
|
||||
attachmentsAdapter.putItem(attachment)
|
||||
}
|
||||
|
||||
@@ -31,11 +31,20 @@
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/item_attachment_size_container"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/KeepassDXStyle.TextAppearance.TextEntryItem"
|
||||
tools:text="BinaryFile.attach" />
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/item_attachment_path"
|
||||
app:layout_constraintTop_toBottomOf="@+id/item_attachment_title"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/item_attachment_size_container"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/KeepassDXStyle.TextAppearance.Tiny"
|
||||
tools:text="BinaryFile.attach" />
|
||||
<LinearLayout
|
||||
android:id="@+id/item_attachment_size_container"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
Reference in new issue
Block a user