diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt b/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt similarity index 86% rename from app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt rename to app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt index c854a51bc..d47b16928 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryFileTest.kt +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/stream/BinaryDataTest.kt @@ -3,7 +3,7 @@ package com.kunzisoft.keepass.tests.stream import android.content.Context import androidx.test.platform.app.InstrumentationRegistry import com.kunzisoft.keepass.database.element.Database -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.stream.readAllBytes import com.kunzisoft.keepass.utils.UriUtil import junit.framework.TestCase.assertEquals @@ -12,7 +12,7 @@ import java.io.DataInputStream import java.io.File import java.io.InputStream -class BinaryFileTest { +class BinaryDataTest { private val context: Context by lazy { InstrumentationRegistry.getInstrumentation().context @@ -25,9 +25,9 @@ class BinaryFileTest { private val loadedKey = Database.LoadedKey.generateNewCipherKey() - private fun saveBinary(asset: String, binaryFile: BinaryFile) { + private fun saveBinary(asset: String, binaryData: BinaryData) { context.assets.open(asset).use { assetInputStream -> - binaryFile.getOutputDataStream(loadedKey).use { binaryOutputStream -> + binaryData.getOutputDataStream(loadedKey).use { binaryOutputStream -> assetInputStream.readAllBytes(DEFAULT_BUFFER_SIZE) { buffer -> binaryOutputStream.write(buffer) } @@ -37,8 +37,8 @@ class BinaryFileTest { @Test fun testSaveTextInCache() { - val binaryA = BinaryFile(fileA) - val binaryB = BinaryFile(fileB) + val binaryA = BinaryData(fileA) + val binaryB = BinaryData(fileB) saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryB) assertEquals("Save text binary length failed.", binaryA.getSize(), binaryB.getSize()) @@ -47,8 +47,8 @@ class BinaryFileTest { @Test fun testSaveImageInCache() { - val binaryA = BinaryFile(fileA) - val binaryB = BinaryFile(fileB) + val binaryA = BinaryData(fileA) + val binaryB = BinaryData(fileB) saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryB) assertEquals("Save image binary length failed.", binaryA.getSize(), binaryB.getSize()) @@ -57,9 +57,9 @@ class BinaryFileTest { @Test fun testCompressText() { - val binaryA = BinaryFile(fileA) - val binaryB = BinaryFile(fileB) - val binaryC = BinaryFile(fileC) + val binaryA = BinaryData(fileA) + val binaryB = BinaryData(fileB) + val binaryC = BinaryData(fileC) saveBinary(TEST_TEXT_ASSET, binaryA) saveBinary(TEST_TEXT_ASSET, binaryB) saveBinary(TEST_TEXT_ASSET, binaryC) @@ -74,9 +74,9 @@ class BinaryFileTest { @Test fun testCompressImage() { - val binaryA = BinaryFile(fileA) - var binaryB = BinaryFile(fileB) - val binaryC = BinaryFile(fileC) + val binaryA = BinaryData(fileA) + var binaryB = BinaryData(fileB) + val binaryC = BinaryData(fileC) saveBinary(TEST_IMAGE_ASSET, binaryA) saveBinary(TEST_IMAGE_ASSET, binaryB) saveBinary(TEST_IMAGE_ASSET, binaryC) @@ -84,7 +84,7 @@ class BinaryFileTest { binaryB.compress(loadedKey) assertEquals("Compress image length failed.", binaryA.getSize(), binaryA.getSize()) assertEquals("Compress image failed.", binaryA.binaryHash(), binaryA.binaryHash()) - binaryB = BinaryFile(fileB, true) + binaryB = BinaryData(fileB, true) binaryB.decompress(loadedKey) assertEquals("Decompress image length failed.", binaryB.getSize(), binaryC.getSize()) assertEquals("Decompress image failed.", binaryB.binaryHash(), binaryC.binaryHash()) @@ -92,7 +92,7 @@ class BinaryFileTest { @Test fun testReadText() { - val binaryA = BinaryFile(fileA) + val binaryA = BinaryData(fileA) saveBinary(TEST_TEXT_ASSET, binaryA) assert(streamAreEquals(context.assets.open(TEST_TEXT_ASSET), binaryA.getInputDataStream(loadedKey))) @@ -100,7 +100,7 @@ class BinaryFileTest { @Test fun testReadImage() { - val binaryA = BinaryFile(fileA) + val binaryA = BinaryData(fileA) saveBinary(TEST_IMAGE_ASSET, binaryA) assert(streamAreEquals(context.assets.open(TEST_IMAGE_ASSET), binaryA.getInputDataStream(loadedKey))) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt index ec4906b71..143fd5763 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/ImageViewerActivity.kt @@ -64,10 +64,10 @@ class ImageViewerActivity : LockingActivity() { intent.getParcelableExtra(IMAGE_ATTACHMENT_TAG)?.let { attachment -> supportActionBar?.title = attachment.name - supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryFile.getSize()) + supportActionBar?.subtitle = Formatter.formatFileSize(this, attachment.binaryData.getSize()) BinaryDatabaseManager.loadBitmap( - attachment.binaryFile, + attachment.binaryData, Database.getInstance().loadedCipherKey, mImagePreviewMaxWidth ) { bitmapLoaded -> diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt index a9fbb4d71..31d03ec02 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsItemsAdapter.kt @@ -83,7 +83,7 @@ class EntryAttachmentsItemsAdapter(context: Context) entryAttachmentState.previewState = AttachmentState.IN_PROGRESS // Load the bitmap image BinaryDatabaseManager.loadBitmap( - entryAttachmentState.attachment.binaryFile, + entryAttachmentState.attachment.binaryData, binaryCipherKey, mImagePreviewMaxWidth ) { imageLoaded -> @@ -111,22 +111,22 @@ class EntryAttachmentsItemsAdapter(context: Context) } holder.binaryFileBroken.apply { setColorFilter(Color.RED) - visibility = if (entryAttachmentState.attachment.binaryFile.isCorrupted) { + visibility = if (entryAttachmentState.attachment.binaryData.isCorrupted) { View.VISIBLE } else { View.GONE } } holder.binaryFileTitle.text = entryAttachmentState.attachment.name - if (entryAttachmentState.attachment.binaryFile.isCorrupted) { + if (entryAttachmentState.attachment.binaryData.isCorrupted) { holder.binaryFileTitle.setTextColor(Color.RED) } else { holder.binaryFileTitle.setTextColor(mTitleColor) } holder.binaryFileSize.text = Formatter.formatFileSize(context, - entryAttachmentState.attachment.binaryFile.getSize()) + entryAttachmentState.attachment.binaryData.getSize()) holder.binaryFileCompression.apply { - if (entryAttachmentState.attachment.binaryFile.isCompressed) { + if (entryAttachmentState.attachment.binaryData.isCompressed) { text = CompressionAlgorithm.GZip.getName(context.resources) visibility = View.VISIBLE } else { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt index be71e580e..565702de7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/node/UpdateEntryRunnable.kt @@ -49,7 +49,7 @@ class UpdateEntryRunnable constructor( newEntryAttachments.forEach { newAttachment -> oldEntryAttachments.forEach { oldAttachment -> if (oldAttachment.name == newAttachment.name - && oldAttachment.binaryFile == newAttachment.binaryFile) + && oldAttachment.binaryData == newAttachment.binaryData) attachmentsToRemove.remove(oldAttachment) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Attachment.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Attachment.kt index 0c0ad7bd4..bd5c988de 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Attachment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Attachment.kt @@ -21,20 +21,20 @@ package com.kunzisoft.keepass.database.element import android.os.Parcel import android.os.Parcelable -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData data class Attachment(var name: String, - var binaryFile: BinaryFile) : Parcelable { + var binaryData: BinaryData) : Parcelable { constructor(parcel: Parcel) : this( parcel.readString() ?: "", - parcel.readParcelable(BinaryFile::class.java.classLoader) ?: BinaryFile() + parcel.readParcelable(BinaryData::class.java.classLoader) ?: BinaryData() ) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(name) - parcel.writeParcelable(binaryFile, flags) + parcel.writeParcelable(binaryData, flags) } override fun describeContents(): Int { @@ -42,7 +42,7 @@ data class Attachment(var name: String, } override fun toString(): String { - return "$name at $binaryFile" + return "$name at $binaryData" } override fun equals(other: Any?): Boolean { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index e26d81a5b..d09e7f966 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -117,7 +117,7 @@ class Database { val allowCustomIcons: Boolean get() = mDatabaseKDBX != null - fun doForEachCustomIcons(action: (IconImageCustom, BinaryFile) -> Unit) { + fun doForEachCustomIcons(action: (IconImageCustom, BinaryData) -> Unit) { return iconsManager.doForEachCustomIcon(action) } @@ -126,12 +126,12 @@ class Database { } fun buildNewCustomIcon(cacheDirectory: File, - result: (IconImageCustom?, BinaryFile?) -> Unit) { + result: (IconImageCustom?, BinaryData?) -> Unit) { mDatabaseKDBX?.buildNewCustomIcon(cacheDirectory, null, result) } - fun isCustomIconBinaryDuplicate(binaryFile: BinaryFile): Boolean { - return mDatabaseKDBX?.isCustomIconBinaryDuplicate(binaryFile) ?: false + fun isCustomIconBinaryDuplicate(binaryData: BinaryData): Boolean { + return mDatabaseKDBX?.isCustomIconBinaryDuplicate(binaryData) ?: false } fun removeCustomIcon(customIcon: IconImageCustom) { @@ -590,7 +590,7 @@ class Database { fun buildNewBinaryAttachment(cacheDirectory: File, compressed: Boolean = false, - protected: Boolean = false): BinaryFile? { + protected: Boolean = false): BinaryData? { return mDatabaseKDB?.buildNewAttachment(cacheDirectory) ?: mDatabaseKDBX?.buildNewAttachment(cacheDirectory, compressed, protected) } @@ -598,7 +598,7 @@ class Database { fun removeAttachmentIfNotUsed(attachment: Attachment) { // No need in KDB database because unique attachment by entry // Don't clear to fix upload multiple times - mDatabaseKDBX?.removeUnlinkedAttachment(attachment.binaryFile, false) + mDatabaseKDBX?.removeUnlinkedAttachment(attachment.binaryData, false) } fun removeUnlinkedAttachments() { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryData.kt similarity index 96% rename from app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt rename to app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryData.kt index c83a78acd..0f727ef0f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryFile.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryData.kt @@ -37,7 +37,7 @@ import javax.crypto.CipherInputStream import javax.crypto.CipherOutputStream import javax.crypto.spec.IvParameterSpec -class BinaryFile : Parcelable { +class BinaryData : Parcelable { private var mDataFile: File? = null private var mLength: Long = 0 @@ -199,7 +199,7 @@ class BinaryFile : Parcelable { return true if (other == null || javaClass != other.javaClass) return false - if (other !is BinaryFile) + if (other !is BinaryData) return false var sameData = false @@ -281,15 +281,15 @@ class BinaryFile : Parcelable { companion object { - private val TAG = BinaryFile::class.java.name + private val TAG = BinaryData::class.java.name @JvmField - val CREATOR: Parcelable.Creator = object : Parcelable.Creator { - override fun createFromParcel(parcel: Parcel): BinaryFile { - return BinaryFile(parcel) + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): BinaryData { + return BinaryData(parcel) } - override fun newArray(size: Int): Array { + override fun newArray(size: Int): Array { return arrayOfNulls(size) } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt index 1ce76e3df..827bd260e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/BinaryPool.kt @@ -26,7 +26,7 @@ import kotlin.math.abs abstract class BinaryPool { - protected val pool = LinkedHashMap() + protected val pool = LinkedHashMap() // To build unique file id private var creationId: String = System.currentTimeMillis().toString() @@ -36,7 +36,7 @@ abstract class BinaryPool { /** * To get a binary by the pool key (ref attribute in entry) */ - operator fun get(key: T): BinaryFile? { + operator fun get(key: T): BinaryData? { return pool[key] } @@ -49,7 +49,7 @@ abstract class BinaryPool { protection: Boolean = false): KeyBinary { val fileInCache = File(cacheDirectory, "$poolId$creationId$binaryFileIncrement") binaryFileIncrement++ - val newBinaryFile = BinaryFile(fileInCache, compression, protection) + val newBinaryFile = BinaryData(fileInCache, compression, protection) val newKey = put(key, newBinaryFile) return KeyBinary(newBinaryFile, newKey) } @@ -57,7 +57,7 @@ abstract class BinaryPool { /** * To linked a binary with a pool key, if the pool key doesn't exists, create an unused one */ - fun put(key: T?, value: BinaryFile): T { + fun put(key: T?, value: BinaryData): T { if (key == null) return put(value) else @@ -66,16 +66,16 @@ abstract class BinaryPool { } /** - * To put a [binaryFile] in the pool, + * To put a [binaryData] in the pool, * if already exists, replace the current one, * else add it with a new key */ - fun put(binaryFile: BinaryFile): T { - var key: T? = findKey(binaryFile) + fun put(binaryData: BinaryData): T { + var key: T? = findKey(binaryData) if (key == null) { key = findUnusedKey() } - pool[key!!] = binaryFile + pool[key!!] = binaryData return key } @@ -92,8 +92,8 @@ abstract class BinaryPool { * Remove a binary from the pool, the file is not deleted */ @Throws(IOException::class) - fun remove(binaryFile: BinaryFile) { - findKey(binaryFile)?.let { + fun remove(binaryData: BinaryData) { + findKey(binaryData)?.let { pool.remove(it) } // Don't clear attachment here because a file can be used in many BinaryAttachment @@ -105,15 +105,15 @@ abstract class BinaryPool { abstract fun findUnusedKey(): T /** - * Return key of [binaryFileToRetrieve] or null if not found + * Return key of [binaryDataToRetrieve] or null if not found */ - private fun findKey(binaryFileToRetrieve: BinaryFile): T? { - val contains = pool.containsValue(binaryFileToRetrieve) + private fun findKey(binaryDataToRetrieve: BinaryData): T? { + val contains = pool.containsValue(binaryDataToRetrieve) return if (!contains) null else { for ((key, binary) in pool) { - if (binary == binaryFileToRetrieve) { + if (binary == binaryDataToRetrieve) { return key } } @@ -121,9 +121,9 @@ abstract class BinaryPool { } } - fun isBinaryDuplicate(binaryFile: BinaryFile?): Boolean { + fun isBinaryDuplicate(binaryData: BinaryData?): Boolean { try { - binaryFile?.let { + binaryData?.let { if (it.getSize() > 0) { val searchBinaryMD5 = it.binaryHash() var i = 0 @@ -145,8 +145,8 @@ abstract class BinaryPool { /** * To do an action on each binary in the pool (order is not important) */ - private fun doForEachBinary(action: (key: T, binary: BinaryFile) -> Unit, - condition: (key: T, binary: BinaryFile) -> Boolean) { + private fun doForEachBinary(action: (key: T, binary: BinaryData) -> Unit, + condition: (key: T, binary: BinaryData) -> Boolean) { for ((key, value) in pool) { if (condition.invoke(key, value)) { action.invoke(key, value) @@ -154,14 +154,14 @@ abstract class BinaryPool { } } - fun doForEachBinary(action: (key: T, binary: BinaryFile) -> Unit) { + fun doForEachBinary(action: (key: T, binary: BinaryData) -> Unit) { doForEachBinary(action) { _, _ -> true } } /** * Utility method to order binaries and solve index problem in database v4 */ - protected fun orderedBinariesWithoutDuplication(condition: ((binary: BinaryFile) -> Boolean) = { true }) + protected fun orderedBinariesWithoutDuplication(condition: ((binary: BinaryData) -> Boolean) = { true }) : List> { val keyBinaryList = ArrayList>() for ((key, binary) in pool) { @@ -199,7 +199,7 @@ abstract class BinaryPool { * Different from doForEach, provide an ordered index to each binary */ private fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary) -> Unit, - conditionToAdd: (binary: BinaryFile) -> Boolean) { + conditionToAdd: (binary: BinaryData) -> Boolean) { orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary -> action.invoke(keyBinary) } @@ -212,14 +212,14 @@ abstract class BinaryPool { /** * Different from doForEach, provide an ordered index to each binary */ - private fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit, - conditionToAdd: (binary: BinaryFile) -> Boolean) { + private fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryData) -> Unit, + conditionToAdd: (binary: BinaryData) -> Boolean) { orderedBinariesWithoutDuplication(conditionToAdd).forEachIndexed { index, keyBinary -> action.invoke(index, keyBinary.binary) } } - fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryFile) -> Unit) { + fun doForEachOrderedBinaryWithoutDuplication(action: (index: Int, binary: BinaryData) -> Unit) { doForEachOrderedBinaryWithoutDuplication(action, { true }) } @@ -249,7 +249,7 @@ abstract class BinaryPool { /** * Utility class to order binaries */ - class KeyBinary(val binary: BinaryFile, key: T) { + class KeyBinary(val binary: BinaryData, key: T) { val keys = HashSet() init { addKey(key) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDB.kt index 6592325bb..c804904fc 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDB.kt @@ -274,11 +274,11 @@ class DatabaseKDB : DatabaseVersioned() { addEntryTo(entry, origParent) } - fun buildNewAttachment(cacheDirectory: File): BinaryFile { + fun buildNewAttachment(cacheDirectory: File): BinaryData { // Generate an unique new file val fileInCache = File(cacheDirectory, binaryIncrement.toString()) binaryIncrement++ - return BinaryFile(fileInCache) + return BinaryData(fileInCache) } companion object { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index 9c3266d1e..f69d5c00a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -312,11 +312,11 @@ class DatabaseKDBX : DatabaseVersioned { fun buildNewCustomIcon(cacheDirectory: File, customIconId: UUID? = null, - result: (IconImageCustom, BinaryFile?) -> Unit) { + result: (IconImageCustom, BinaryData?) -> Unit) { iconsManager.buildNewCustomIcon(cacheDirectory, customIconId, result) } - fun isCustomIconBinaryDuplicate(binary: BinaryFile): Boolean { + fun isCustomIconBinaryDuplicate(binary: BinaryData): Boolean { return iconsManager.isCustomIconBinaryDuplicate(binary) } @@ -637,12 +637,12 @@ class DatabaseKDBX : DatabaseVersioned { fun buildNewAttachment(cacheDirectory: File, compression: Boolean, protection: Boolean, - binaryPoolId: Int? = null): BinaryFile { + binaryPoolId: Int? = null): BinaryData { return binaryPool.put(cacheDirectory, binaryPoolId, compression, protection).binary } - fun removeUnlinkedAttachment(binary: BinaryFile, clear: Boolean) { - val listBinaries = ArrayList() + fun removeUnlinkedAttachment(binary: BinaryData, clear: Boolean) { + val listBinaries = ArrayList() listBinaries.add(binary) removeUnlinkedAttachments(listBinaries, clear) } @@ -651,9 +651,9 @@ class DatabaseKDBX : DatabaseVersioned { removeUnlinkedAttachments(emptyList(), clear) } - private fun removeUnlinkedAttachments(binaries: List, clear: Boolean) { + private fun removeUnlinkedAttachments(binaries: List, clear: Boolean) { // Build binaries to remove with all binaries known - val binariesToRemove = ArrayList() + val binariesToRemove = ArrayList() if (binaries.isEmpty()) { binaryPool.doForEachBinary { _, binary -> binariesToRemove.add(binary) @@ -665,7 +665,7 @@ class DatabaseKDBX : DatabaseVersioned { rootGroup?.doForEachChild(object : NodeHandler() { override fun operate(node: EntryKDBX): Boolean { node.getAttachments(binaryPool, true).forEach { - binariesToRemove.remove(it.binaryFile) + binariesToRemove.remove(it.binaryData) } return binariesToRemove.isNotEmpty() } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDB.kt index 3e642b896..1b2acdee3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDB.kt @@ -22,7 +22,7 @@ package com.kunzisoft.keepass.database.element.entry import android.os.Parcel import android.os.Parcelable import com.kunzisoft.keepass.database.element.Attachment -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.database.element.group.GroupKDB import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID import com.kunzisoft.keepass.database.element.node.NodeId @@ -56,7 +56,7 @@ class EntryKDB : EntryVersioned, NodeKDBInterface /** A string describing what is in binaryData */ var binaryDescription = "" - var binaryData: BinaryFile? = null + var binaryData: BinaryData? = null // Determine if this is a MetaStream entry val isMetaStream: Boolean @@ -89,7 +89,7 @@ class EntryKDB : EntryVersioned, NodeKDBInterface url = parcel.readString() ?: url notes = parcel.readString() ?: notes binaryDescription = parcel.readString() ?: binaryDescription - binaryData = parcel.readParcelable(BinaryFile::class.java.classLoader) + binaryData = parcel.readParcelable(BinaryData::class.java.classLoader) } override fun readParentParcelable(parcel: Parcel): GroupKDB? { @@ -151,7 +151,7 @@ class EntryKDB : EntryVersioned, NodeKDBInterface fun putAttachment(attachment: Attachment) { this.binaryDescription = attachment.name - this.binaryData = attachment.binaryFile + this.binaryData = attachment.binaryData } fun removeAttachment(attachment: Attachment? = null) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt index 4d981380e..c1d0ea8cb 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/entry/EntryKDBX.kt @@ -282,7 +282,7 @@ class EntryKDBX : EntryVersioned, NodeKDBXInte } fun putAttachment(attachment: Attachment, attachmentPool: AttachmentPool) { - binaries[attachment.name] = attachmentPool.put(attachment.binaryFile) + binaries[attachment.name] = attachmentPool.put(attachment.binaryData) } fun removeAttachment(attachment: Attachment) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt index 6d33290de..a1d7d8d96 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/icon/IconsManager.kt @@ -20,13 +20,12 @@ package com.kunzisoft.keepass.database.element.icon import android.util.Log -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.database.element.database.CustomIconPool import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.KEY_ID import com.kunzisoft.keepass.icons.IconPack.Companion.NB_ICONS import java.io.File import java.util.* -import kotlin.collections.ArrayList class IconsManager { @@ -52,7 +51,7 @@ class IconsManager { fun buildNewCustomIcon(cacheDirectory: File, key: UUID? = null, - result: (IconImageCustom, BinaryFile?) -> Unit) { + result: (IconImageCustom, BinaryData?) -> Unit) { val keyBinary = customCache.put(cacheDirectory, key) result.invoke(IconImageCustom(keyBinary.keys.first()), keyBinary.binary) } @@ -61,8 +60,8 @@ class IconsManager { return IconImageCustom(iconUuid) } - fun isCustomIconBinaryDuplicate(binaryFile: BinaryFile): Boolean { - return customCache.isBinaryDuplicate(binaryFile) + fun isCustomIconBinaryDuplicate(binaryData: BinaryData): Boolean { + return customCache.isBinaryDuplicate(binaryData) } fun removeCustomIcon(iconUuid: UUID) { @@ -75,11 +74,11 @@ class IconsManager { } } - fun getBinaryForCustomIcon(iconUuid: UUID): BinaryFile? { + fun getBinaryForCustomIcon(iconUuid: UUID): BinaryData? { return customCache[iconUuid] } - fun doForEachCustomIcon(action: (IconImageCustom, BinaryFile) -> Unit) { + fun doForEachCustomIcon(action: (IconImageCustom, BinaryData) -> Unit) { customCache.doForEachBinary { key, binary -> action.invoke(IconImageCustom(key), binary) } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt index b525aecfb..05cfc2c4d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt @@ -29,7 +29,7 @@ import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.DeletedObject -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG @@ -78,7 +78,7 @@ class DatabaseInputKDBX(cacheDirectory: File) private var ctxStringName: String? = null private var ctxStringValue: ProtectedString? = null private var ctxBinaryName: String? = null - private var ctxBinaryValue: BinaryFile? = null + private var ctxBinaryValue: BinaryData? = null private var ctxATName: String? = null private var ctxATSeq: String? = null private var entryInHistory = false @@ -966,7 +966,7 @@ class DatabaseInputKDBX(cacheDirectory: File) } @Throws(XmlPullParserException::class, IOException::class) - private fun readBinary(xpp: XmlPullParser): BinaryFile? { + private fun readBinary(xpp: XmlPullParser): BinaryData? { // Reference Id to a binary already present in binary pool val ref = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrRef) @@ -997,7 +997,7 @@ class DatabaseInputKDBX(cacheDirectory: File) } @Throws(IOException::class, XmlPullParserException::class) - private fun createBinary(binaryId: Int?, xpp: XmlPullParser): BinaryFile? { + private fun createBinary(binaryId: Int?, xpp: XmlPullParser): BinaryData? { var compressed = false var protected = true diff --git a/app/src/main/java/com/kunzisoft/keepass/icons/IconDrawableFactory.kt b/app/src/main/java/com/kunzisoft/keepass/icons/IconDrawableFactory.kt index 1cb7c0c00..038bdfe5b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/icons/IconDrawableFactory.kt +++ b/app/src/main/java/com/kunzisoft/keepass/icons/IconDrawableFactory.kt @@ -34,7 +34,7 @@ import androidx.core.graphics.drawable.toBitmap import androidx.core.widget.ImageViewCompat import com.kunzisoft.keepass.R import com.kunzisoft.keepass.database.element.Database -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.database.element.icon.IconImageCustom import com.kunzisoft.keepass.database.element.icon.IconImageDraw import kotlinx.coroutines.CoroutineScope @@ -49,7 +49,7 @@ import kotlin.collections.HashMap * Factory class who build database icons dynamically, can assign an icon of IconPack, or a custom icon to an ImageView with a tint */ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedKey?, - private val retrieveCustomIconBinary : (iconId: UUID) -> BinaryFile?) { + private val retrieveCustomIconBinary : (iconId: UUID) -> BinaryData?) { /** customIconMap * Cache for icon drawable. @@ -85,7 +85,7 @@ class IconDrawableFactory(private val retrieveCipherKey : () -> Database.LoadedK /** * Build a custom [Drawable] from custom [icon] */ - private fun getIconDrawable(resources: Resources, icon: IconImageCustom, iconCustomBinary: BinaryFile?): Drawable? { + private fun getIconDrawable(resources: Resources, icon: IconImageCustom, iconCustomBinary: BinaryData?): Drawable? { val patternIcon = PatternIcon(resources) val cipherKey = retrieveCipherKey() if (cipherKey != null) { diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt index c297edada..2f0863fae 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryAttachmentState.kt @@ -22,7 +22,7 @@ package com.kunzisoft.keepass.model import android.os.Parcel import android.os.Parcelable import com.kunzisoft.keepass.database.element.Attachment -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.utils.readEnum import com.kunzisoft.keepass.utils.writeEnum @@ -33,7 +33,7 @@ data class EntryAttachmentState(var attachment: Attachment, var previewState: AttachmentState = AttachmentState.NULL) : Parcelable { constructor(parcel: Parcel) : this( - parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryFile()), + parcel.readParcelable(Attachment::class.java.classLoader) ?: Attachment("", BinaryData()), parcel.readEnum() ?: StreamDirection.DOWNLOAD, parcel.readEnum() ?: AttachmentState.NULL, parcel.readInt(), diff --git a/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt index fffdb3666..3f9ac7802 100644 --- a/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt @@ -346,7 +346,7 @@ class AttachmentFileNotificationService: LockNotificationService() { StreamDirection.UPLOAD -> { BinaryDatabaseManager.uploadToDatabase( attachmentNotification.uri, - attachment.binaryFile, + attachment.binaryData, contentResolver, { percent -> publishProgress(percent) @@ -359,7 +359,7 @@ class AttachmentFileNotificationService: LockNotificationService() { StreamDirection.DOWNLOAD -> { BinaryDatabaseManager.downloadFromDatabase( attachmentNotification.uri, - attachment.binaryFile, + attachment.binaryData, contentResolver, { percent -> publishProgress(percent) diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt index 3395ddf57..8a6387e12 100644 --- a/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt +++ b/app/src/main/java/com/kunzisoft/keepass/tasks/BinaryDatabaseManager.kt @@ -6,7 +6,7 @@ import android.graphics.BitmapFactory import android.net.Uri import android.util.Log import com.kunzisoft.keepass.database.element.Database -import com.kunzisoft.keepass.database.element.database.BinaryFile +import com.kunzisoft.keepass.database.element.database.BinaryData import com.kunzisoft.keepass.stream.readAllBytes import com.kunzisoft.keepass.utils.UriUtil import kotlinx.coroutines.* @@ -22,25 +22,25 @@ import kotlin.math.pow object BinaryDatabaseManager { fun downloadFromDatabase(attachmentToUploadUri: Uri, - binaryFile: BinaryFile, + binaryData: BinaryData, contentResolver: ContentResolver, update: ((percent: Int)->Unit)? = null, canceled: ()-> Boolean = { false }, bufferSize: Int = DEFAULT_BUFFER_SIZE) { UriUtil.getUriOutputStream(contentResolver, attachmentToUploadUri)?.use { outputStream -> - downloadFromDatabase(outputStream, binaryFile, update, canceled, bufferSize) + downloadFromDatabase(outputStream, binaryData, update, canceled, bufferSize) } } private fun downloadFromDatabase(outputStream: OutputStream, - binaryFile: BinaryFile, - update: ((percent: Int)->Unit)? = null, - canceled: ()-> Boolean = { false }, - bufferSize: Int = DEFAULT_BUFFER_SIZE) { - val fileSize = binaryFile.getSize() + binaryData: BinaryData, + update: ((percent: Int)->Unit)? = null, + canceled: ()-> Boolean = { false }, + bufferSize: Int = DEFAULT_BUFFER_SIZE) { + val fileSize = binaryData.getSize() var dataDownloaded = 0L Database.getInstance().loadedCipherKey?.let { binaryCipherKey -> - binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { inputStream -> + binaryData.getUnGzipInputDataStream(binaryCipherKey).use { inputStream -> inputStream.readAllBytes(bufferSize, canceled) { buffer -> outputStream.write(buffer) dataDownloaded += buffer.size @@ -56,26 +56,26 @@ object BinaryDatabaseManager { } fun uploadToDatabase(attachmentFromDownloadUri: Uri, - binaryFile: BinaryFile, + binaryData: BinaryData, contentResolver: ContentResolver, update: ((percent: Int)->Unit)? = null, canceled: ()-> Boolean = { false }, bufferSize: Int = DEFAULT_BUFFER_SIZE) { val fileSize = contentResolver.openFileDescriptor(attachmentFromDownloadUri, "r")?.statSize ?: 0 UriUtil.getUriInputStream(contentResolver, attachmentFromDownloadUri)?.use { inputStream -> - uploadToDatabase(inputStream, fileSize, binaryFile, update, canceled, bufferSize) + uploadToDatabase(inputStream, fileSize, binaryData, update, canceled, bufferSize) } } private fun uploadToDatabase(inputStream: InputStream, - fileSize: Long, - binaryFile: BinaryFile, - update: ((percent: Int)->Unit)? = null, - canceled: ()-> Boolean = { false }, - bufferSize: Int = DEFAULT_BUFFER_SIZE) { + fileSize: Long, + binaryData: BinaryData, + update: ((percent: Int)->Unit)? = null, + canceled: ()-> Boolean = { false }, + bufferSize: Int = DEFAULT_BUFFER_SIZE) { var dataUploaded = 0L Database.getInstance().loadedCipherKey?.let { binaryCipherKey -> - binaryFile.getGzipOutputDataStream(binaryCipherKey).use { outputStream -> + binaryData.getGzipOutputDataStream(binaryCipherKey).use { outputStream -> inputStream.readAllBytes(bufferSize, canceled) { buffer -> outputStream.write(buffer) dataUploaded += buffer.size @@ -92,9 +92,9 @@ object BinaryDatabaseManager { fun resizeBitmapAndStoreDataInBinaryFile(contentResolver: ContentResolver, bitmapUri: Uri?, - binaryFile: BinaryFile?) { + binaryData: BinaryData?) { try { - binaryFile?.let { + binaryData?.let { UriUtil.getUriInputStream(contentResolver, bitmapUri)?.use { inputStream -> BitmapFactory.decodeStream(inputStream)?.let { bitmap -> val bitmapResized = bitmap.resize(DEFAULT_ICON_WIDTH) @@ -105,7 +105,7 @@ object BinaryDatabaseManager { uploadToDatabase( byteArrayInputStream, bitmapData.size.toLong(), - binaryFile + binaryData ) } } @@ -136,7 +136,7 @@ object BinaryDatabaseManager { return Bitmap.createScaledBitmap(this, width, height, true) } - fun loadBitmap(binaryFile: BinaryFile, + fun loadBitmap(binaryData: BinaryData, binaryCipherKey: Database.LoadedKey?, maxWidth: Int, actionOnFinish: (Bitmap?) -> Unit) { @@ -145,7 +145,7 @@ object BinaryDatabaseManager { val asyncResult: Deferred = async { runCatching { binaryCipherKey?.let { binaryKey -> - val bitmap: Bitmap? = decodeSampledBitmap(binaryFile, + val bitmap: Bitmap? = decodeSampledBitmap(binaryData, binaryKey, maxWidth) bitmap @@ -159,14 +159,14 @@ object BinaryDatabaseManager { } } - private fun decodeSampledBitmap(binaryFile: BinaryFile, + private fun decodeSampledBitmap(binaryData: BinaryData, binaryCipherKey: Database.LoadedKey, maxWidth: Int): Bitmap? { // First decode with inJustDecodeBounds=true to check dimensions return BitmapFactory.Options().run { try { inJustDecodeBounds = true - binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { + binaryData.getUnGzipInputDataStream(binaryCipherKey).use { BitmapFactory.decodeStream(it, null, this) } // Calculate inSampleSize @@ -178,7 +178,7 @@ object BinaryDatabaseManager { // Decode bitmap with inSampleSize set inJustDecodeBounds = false - binaryFile.getUnGzipInputDataStream(binaryCipherKey).use { + binaryData.getUnGzipInputDataStream(binaryCipherKey).use { BitmapFactory.decodeStream(it, null, this) } } catch (e: Exception) {