Improve FileSizeFormatter.format API.

This commit is contained in:
Benoit Marty
2023-06-20 14:55:26 +02:00
committed by Benoit Marty
parent ac60ef0cbe
commit 2809d6fc2f
3 changed files with 23 additions and 4 deletions

View File

@@ -20,5 +20,5 @@ interface FileSizeFormatter {
/**
* Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
*/
fun format(fileSize: Long): String
fun format(fileSize: Long, useShortFormat: Boolean = true): String
}

View File

@@ -17,6 +17,7 @@
package io.element.android.libraries.androidtools.impl
import android.content.Context
import android.os.Build
import android.text.format.Formatter
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.androidtools.api.FileSizeFormatter
@@ -26,7 +27,25 @@ import javax.inject.Inject
@ContributesBinding(AppScope::class)
class AndroidFileSizeFormatter @Inject constructor(@ApplicationContext private val context: Context) : FileSizeFormatter {
override fun format(fileSize: Long): String {
return Formatter.formatShortFileSize(context, fileSize)
override fun format(fileSize: Long, useShortFormat: Boolean): String {
// Since Android O, the system considers that 1ko = 1000 bytes instead of 1024 bytes.
// We want to avoid that.
val normalizedSize = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
fileSize
} else {
// First convert the size
when {
fileSize < 1024 -> fileSize
fileSize < 1024 * 1024 -> fileSize * 1000 / 1024
fileSize < 1024 * 1024 * 1024 -> fileSize * 1000 / 1024 * 1000 / 1024
else -> fileSize * 1000 / 1024 * 1000 / 1024 * 1000 / 1024
}
}
return if (useShortFormat) {
Formatter.formatShortFileSize(context, normalizedSize)
} else {
Formatter.formatFileSize(context, normalizedSize)
}
}
}

View File

@@ -19,7 +19,7 @@ package io.element.android.libraries.androidtools.test
import io.element.android.libraries.androidtools.api.FileSizeFormatter
class FakeFileSizeFormatter : FileSizeFormatter {
override fun format(fileSize: Long): String {
override fun format(fileSize: Long, useShortFormat: Boolean): String {
return "$fileSize Bytes"
}
}