Add kibibyte and gibibyte #851

This commit is contained in:
J-Jamet
2021-01-22 16:07:50 +01:00
parent 3b6ad080b4
commit f34e007ecd
2 changed files with 44 additions and 6 deletions

View File

@@ -1,3 +1,22 @@
/*
* Copyright 2021 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.utils
import android.content.Context
@@ -9,13 +28,24 @@ class DataByte(var number: Long, var format: ByteFormat) {
fun toBetterByteFormat(): DataByte {
return when (this.format) {
ByteFormat.BYTE -> {
if (this.number % MEBIBYTES == 0L) {
when {
this.number % GIBIBYTES == 0L -> {
DataByte((this.number / GIBIBYTES), ByteFormat.GIBIBYTE)
}
this.number % MEBIBYTES == 0L -> {
DataByte((this.number / MEBIBYTES), ByteFormat.MEBIBYTE)
} else {
}
this.number % KIBIBYTES == 0L -> {
DataByte((this.number / KIBIBYTES), ByteFormat.KIBIBYTE)
}
else -> {
DataByte(this.number, ByteFormat.BYTE)
}
}
ByteFormat.MEBIBYTE -> this
}
else -> {
DataByte(toBytes(), ByteFormat.BYTE).toBetterByteFormat()
}
}
}
@@ -25,7 +55,9 @@ class DataByte(var number: Long, var format: ByteFormat) {
fun toBytes(): Long {
return when (this.format) {
ByteFormat.BYTE -> this.number
ByteFormat.KIBIBYTE -> this.number * KIBIBYTES
ByteFormat.MEBIBYTE -> this.number * MEBIBYTES
ByteFormat.GIBIBYTE -> this.number * GIBIBYTES
}
}
@@ -39,10 +71,14 @@ class DataByte(var number: Long, var format: ByteFormat) {
enum class ByteFormat(@StringRes var stringId: Int) {
BYTE(R.string.unit_byte),
MEBIBYTE(R.string.unit_mebibyte)
KIBIBYTE(R.string.unit_kibibyte),
MEBIBYTE(R.string.unit_mebibyte),
GIBIBYTE(R.string.unit_gibibyte)
}
companion object {
const val KIBIBYTES = 1024L
const val MEBIBYTES = 1048576L
const val GIBIBYTES = 1073741824L
}
}

View File

@@ -521,7 +521,9 @@
<string name="kdf_Argon2d">Argon2d</string>
<string name="kdf_Argon2id">Argon2id</string>
<string name="unit_byte">B</string>
<string name="unit_kibibyte">KiB</string>
<string name="unit_mebibyte">MiB</string>
<string name="unit_gibibyte">GiB</string>
<string-array name="timeout_options">
<item>5 seconds</item>
<item>10 seconds</item>