Skip to content

Commit

Permalink
Analyzer: Fix invalid storage size being displayed for large SD-cards
Browse files Browse the repository at this point in the history
Fixes #1575
  • Loading branch information
d4rken committed Feb 4, 2025
1 parent 3c27d58 commit a490370
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.darken.sdmse.analyzer.core.device

import android.app.usage.StorageStatsManager
import android.os.storage.StorageManager
import eu.darken.sdmse.R
import eu.darken.sdmse.common.ca.toCaString
Expand All @@ -16,6 +15,7 @@ import eu.darken.sdmse.common.progress.updateProgressSecondary
import eu.darken.sdmse.common.storage.StorageEnvironment
import eu.darken.sdmse.common.storage.StorageId
import eu.darken.sdmse.common.storage.StorageManager2
import eu.darken.sdmse.common.storage.StorageStatsManager2
import eu.darken.sdmse.setup.isComplete
import eu.darken.sdmse.setup.storage.StorageSetupModule
import kotlinx.coroutines.flow.Flow
Expand All @@ -27,7 +27,7 @@ class DeviceStorageScanner @Inject constructor(
private val storageSetupModule: StorageSetupModule,
private val environment: StorageEnvironment,
private val storageManager2: StorageManager2,
private val storageStatsmanager: StorageStatsManager,
private val storageStatsmanager: StorageStatsManager2,
) : Progress.Host, Progress.Client {

private val progressPub = MutableStateFlow<Progress.Data?>(
Expand Down Expand Up @@ -55,17 +55,13 @@ class DeviceStorageScanner @Inject constructor(
)

val totalBytes = try {
storageStatsmanager.getTotalBytes(id.externalId).also {
if (it == 0L) throw IllegalStateException("Total bytes is 0")
}
storageStatsmanager.getTotalBytes(id)
} catch (e: Exception) {
log(TAG, WARN) { "Failed to get total bytes for $id" }
environment.dataDir.asFile().totalSpace
}
val freeBytes = try {
storageStatsmanager.getFreeBytes(id.externalId).also {
if (it == 0L) throw IllegalStateException("Free bytes is 0")
}
storageStatsmanager.getFreeBytes(id)
} catch (e: Exception) {
log(TAG, WARN) { "Failed to get free bytes for $id" }
environment.dataDir.asFile().freeSpace
Expand Down Expand Up @@ -117,18 +113,13 @@ class DeviceStorageScanner @Inject constructor(

val totalBytes = try {
// Secondary storage isn't available in on all APIs, (e.g. not on a Redmi 7A @ Android 9)
storageStatsmanager.getTotalBytes(id.externalId).also {
if (it == 0L) throw IllegalStateException("Total bytes is 0")
}
storageStatsmanager.getTotalBytes(id)
} catch (e: Exception) {
log(TAG, WARN) { "Failed to get total bytes for $id" }
volume.path?.totalSpace ?: 0L
}
val freeBytes = try {
storageStatsmanager.getFreeBytes(id.externalId).also {
// Was 0 for Xiaomi/pine_eea/pine:9/PKQ1.190319.001/V11.0.18.0.PCMEUXM:user/release-keys
if (it == 0L) throw IllegalStateException("Free bytes is 0")
}
storageStatsmanager.getFreeBytes(id)
} catch (e: Exception) {
log(TAG, WARN) { "Failed to get free bytes for $id" }
volume.path?.freeSpace ?: 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,25 @@ class StorageStatsManager2 @Inject constructor(
osStatManager.queryStatsForPackage(storageId.externalId, pkg.packageName, pkg.userHandle.asUserHandle())
}

@Throws(IllegalStateException::class)
suspend fun getTotalBytes(id: StorageId): Long {
val value = osStatManager.getTotalBytes(id.externalId)
if (value == ERRROR_MIN || value == ERROR_MAX) throw IllegalStateException("Total bytes is $value")
return value
}

@Throws(IllegalStateException::class)
suspend fun getFreeBytes(id: StorageId): Long {
val value = osStatManager.getFreeBytes(id.externalId)
if (value == ERRROR_MIN || value == ERROR_MAX) throw IllegalStateException("Total bytes is $value")
return value
}

companion object {
// using get*Bytes on a 512GB sdcard leads to invalid values being returned
// See https://github.com/d4rken-org/sdmaid-se/issues/1575
private const val ERROR_MAX = 1000000000000L
private const val ERRROR_MIN = 0L
val TAG: String = logTag("StorageStatsManager2")
}
}

0 comments on commit a490370

Please sign in to comment.