fix: use commons extension for size calculation (#392)

* docs(changelog): add entry regarding file size calculation change

* refactor: remove empty line

* refactor: reformat code
This commit is contained in:
Naveen Singh
2026-02-08 17:47:20 +05:30
committed by GitHub
parent be63034b1c
commit a1f06bfcbc
3 changed files with 22 additions and 24 deletions

View File

@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Fixed file size calculations to use SI decimal units (divide by 1000)
## [1.6.0] - 2026-01-30
### Added

View File

@@ -1,14 +0,0 @@
package org.fossify.filemanager.extensions
import java.text.DecimalFormat
// use 1000 instead of 1024 at dividing
fun Long.formatSizeThousand(): String {
if (this <= 0) {
return "0 B"
}
val units = arrayOf("B", "kB", "MB", "GB", "TB")
val digitGroups = (Math.log10(toDouble()) / Math.log10(1000.0)).toInt()
return "${DecimalFormat("#,##0.#").format(this / Math.pow(1000.0, digitGroups.toDouble()))} ${units[digitGroups]}"
}

View File

@@ -46,7 +46,6 @@ import org.fossify.filemanager.adapters.ItemsAdapter
import org.fossify.filemanager.databinding.ItemStorageVolumeBinding
import org.fossify.filemanager.databinding.StorageFragmentBinding
import org.fossify.filemanager.extensions.config
import org.fossify.filemanager.extensions.formatSizeThousand
import org.fossify.filemanager.extensions.getAllVolumeNames
import org.fossify.filemanager.helpers.ARCHIVES
import org.fossify.filemanager.helpers.AUDIO
@@ -65,8 +64,10 @@ import org.fossify.filemanager.interfaces.ItemOperationsListener
import org.fossify.filemanager.models.ListItem
import java.util.Locale
class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.StorageInnerBinding>(context, attributeSet),
ItemOperationsListener {
class StorageFragment(
context: Context,
attributeSet: AttributeSet
) : MyViewPagerFragment<MyViewPagerFragment.StorageInnerBinding>(context, attributeSet), ItemOperationsListener {
private val SIZE_DIVIDER = 100000
private var allDeviceListItems = ArrayList<ListItem>()
private var lastSearchedText = ""
@@ -252,7 +253,8 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
try {
context.queryCursor(uri, projection) { cursor ->
try {
val mimeType = cursor.getStringValue(MediaStore.Files.FileColumns.MIME_TYPE)?.lowercase(Locale.getDefault())
val mimeType =
cursor.getStringValue(MediaStore.Files.FileColumns.MIME_TYPE)?.lowercase(Locale.getDefault())
val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE)
if (mimeType == null) {
if (size > 0 && size != 4096L) {
@@ -309,7 +311,8 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
if (storageVolume.isPrimary) {
// internal storage
volumeName = PRIMARY_VOLUME_NAME
val storageStatsManager = context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
val storageStatsManager =
context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
val uuid = StorageManager.UUID_DEFAULT
totalStorageSpace = storageStatsManager.getTotalBytes(uuid)
freeStorageSpace = storageStatsManager.getFreeBytes(uuid)
@@ -322,17 +325,24 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
post {
volumes[volumeName]?.apply {
arrayOf(
mainStorageUsageProgressbar, imagesProgressbar, videosProgressbar, audioProgressbar, documentsProgressbar,
archivesProgressbar, othersProgressbar
mainStorageUsageProgressbar,
imagesProgressbar,
videosProgressbar,
audioProgressbar,
documentsProgressbar,
archivesProgressbar,
othersProgressbar
).forEach {
it.max = (totalStorageSpace / SIZE_DIVIDER).toInt()
}
mainStorageUsageProgressbar.progress = ((totalStorageSpace - freeStorageSpace) / SIZE_DIVIDER).toInt()
mainStorageUsageProgressbar.progress =
((totalStorageSpace - freeStorageSpace) / SIZE_DIVIDER).toInt()
mainStorageUsageProgressbar.beVisible()
freeSpaceValue.text = freeStorageSpace.formatSizeThousand()
totalSpace.text = String.format(context.getString(R.string.total_storage), totalStorageSpace.formatSizeThousand())
freeSpaceValue.text = freeStorageSpace.formatSize()
totalSpace.text =
String.format(context.getString(R.string.total_storage), totalStorageSpace.formatSize())
freeSpaceLabel.beVisible()
getSizes(volumeName)
}