mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Rename BinaryFile by BinaryData
This commit is contained in:
@@ -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)))
|
||||
@@ -64,10 +64,10 @@ class ImageViewerActivity : LockingActivity() {
|
||||
intent.getParcelableExtra<Attachment>(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 ->
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<BinaryFile> = object : Parcelable.Creator<BinaryFile> {
|
||||
override fun createFromParcel(parcel: Parcel): BinaryFile {
|
||||
return BinaryFile(parcel)
|
||||
val CREATOR: Parcelable.Creator<BinaryData> = object : Parcelable.Creator<BinaryData> {
|
||||
override fun createFromParcel(parcel: Parcel): BinaryData {
|
||||
return BinaryData(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<BinaryFile?> {
|
||||
override fun newArray(size: Int): Array<BinaryData?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import kotlin.math.abs
|
||||
|
||||
abstract class BinaryPool<T> {
|
||||
|
||||
protected val pool = LinkedHashMap<T, BinaryFile>()
|
||||
protected val pool = LinkedHashMap<T, BinaryData>()
|
||||
|
||||
// To build unique file id
|
||||
private var creationId: String = System.currentTimeMillis().toString()
|
||||
@@ -36,7 +36,7 @@ abstract class BinaryPool<T> {
|
||||
/**
|
||||
* 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<T> {
|
||||
protection: Boolean = false): KeyBinary<T> {
|
||||
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<T> {
|
||||
/**
|
||||
* 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<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<T> {
|
||||
* 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<T> {
|
||||
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<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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<T> {
|
||||
/**
|
||||
* 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<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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<KeyBinary<T>> {
|
||||
val keyBinaryList = ArrayList<KeyBinary<T>>()
|
||||
for ((key, binary) in pool) {
|
||||
@@ -199,7 +199,7 @@ abstract class BinaryPool<T> {
|
||||
* Different from doForEach, provide an ordered index to each binary
|
||||
*/
|
||||
private fun doForEachBinaryWithoutDuplication(action: (keyBinary: KeyBinary<T>) -> Unit,
|
||||
conditionToAdd: (binary: BinaryFile) -> Boolean) {
|
||||
conditionToAdd: (binary: BinaryData) -> Boolean) {
|
||||
orderedBinariesWithoutDuplication(conditionToAdd).forEach { keyBinary ->
|
||||
action.invoke(keyBinary)
|
||||
}
|
||||
@@ -212,14 +212,14 @@ abstract class BinaryPool<T> {
|
||||
/**
|
||||
* 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<T> {
|
||||
/**
|
||||
* Utility class to order binaries
|
||||
*/
|
||||
class KeyBinary<T>(val binary: BinaryFile, key: T) {
|
||||
class KeyBinary<T>(val binary: BinaryData, key: T) {
|
||||
val keys = HashSet<T>()
|
||||
init {
|
||||
addKey(key)
|
||||
|
||||
@@ -274,11 +274,11 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
|
||||
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 {
|
||||
|
||||
@@ -312,11 +312,11 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
|
||||
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<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
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<BinaryFile>()
|
||||
fun removeUnlinkedAttachment(binary: BinaryData, clear: Boolean) {
|
||||
val listBinaries = ArrayList<BinaryData>()
|
||||
listBinaries.add(binary)
|
||||
removeUnlinkedAttachments(listBinaries, clear)
|
||||
}
|
||||
@@ -651,9 +651,9 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
removeUnlinkedAttachments(emptyList(), clear)
|
||||
}
|
||||
|
||||
private fun removeUnlinkedAttachments(binaries: List<BinaryFile>, clear: Boolean) {
|
||||
private fun removeUnlinkedAttachments(binaries: List<BinaryData>, clear: Boolean) {
|
||||
// Build binaries to remove with all binaries known
|
||||
val binariesToRemove = ArrayList<BinaryFile>()
|
||||
val binariesToRemove = ArrayList<BinaryData>()
|
||||
if (binaries.isEmpty()) {
|
||||
binaryPool.doForEachBinary { _, binary ->
|
||||
binariesToRemove.add(binary)
|
||||
@@ -665,7 +665,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
rootGroup?.doForEachChild(object : NodeHandler<EntryKDBX>() {
|
||||
override fun operate(node: EntryKDBX): Boolean {
|
||||
node.getAttachments(binaryPool, true).forEach {
|
||||
binariesToRemove.remove(it.binaryFile)
|
||||
binariesToRemove.remove(it.binaryData)
|
||||
}
|
||||
return binariesToRemove.isNotEmpty()
|
||||
}
|
||||
|
||||
@@ -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<Int, UUID, GroupKDB, EntryKDB>, 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<Int, UUID, GroupKDB, EntryKDB>, 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<Int, UUID, GroupKDB, EntryKDB>, NodeKDBInterface
|
||||
|
||||
fun putAttachment(attachment: Attachment) {
|
||||
this.binaryDescription = attachment.name
|
||||
this.binaryData = attachment.binaryFile
|
||||
this.binaryData = attachment.binaryData
|
||||
}
|
||||
|
||||
fun removeAttachment(attachment: Attachment? = null) {
|
||||
|
||||
@@ -282,7 +282,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, 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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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>() ?: StreamDirection.DOWNLOAD,
|
||||
parcel.readEnum<AttachmentState>() ?: AttachmentState.NULL,
|
||||
parcel.readInt(),
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<Bitmap?> = 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) {
|
||||
|
||||
Reference in New Issue
Block a user