Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5e6a84b4 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Fix NameNotFoundException of getStorageSize" into udc-dev

parents e85e2477 642b7d89
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settingslib.spaprivileged.template.app
import android.content.Context
import android.content.pm.ApplicationInfo
import android.text.format.Formatter
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.produceState
@@ -30,18 +31,26 @@ import com.android.settingslib.spaprivileged.model.app.userHandle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

private const val TAG = "AppStorageSize"

@Composable
fun ApplicationInfo.getStorageSize(): State<String> {
    val context = LocalContext.current
    return produceState(initialValue = stringResource(R.string.summary_placeholder)) {
        withContext(Dispatchers.IO) {
            value = Formatter.formatFileSize(context, calculateSizeBytes(context))
            val sizeBytes = calculateSizeBytes(context)
            value = if (sizeBytes != null) Formatter.formatFileSize(context, sizeBytes) else ""
        }
    }
}

private fun ApplicationInfo.calculateSizeBytes(context: Context): Long {
private fun ApplicationInfo.calculateSizeBytes(context: Context): Long? {
    val storageStatsManager = context.storageStatsManager
    return try {
        val stats = storageStatsManager.queryStatsForPackage(storageUuid, packageName, userHandle)
    return stats.codeBytes + stats.dataBytes
        stats.codeBytes + stats.dataBytes
    } catch (e: Exception) {
        Log.w(TAG, "Failed to query stats: $e")
        null
    }
}
+24 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.usage.StorageStats
import android.app.usage.StorageStatsManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager.NameNotFoundException
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule
@@ -60,9 +61,11 @@ class AppStorageSizeTest {
    @Before
    fun setUp() {
        whenever(context.storageStatsManager).thenReturn(storageStatsManager)
        whenever(storageStatsManager.queryStatsForPackage(
        whenever(
            storageStatsManager.queryStatsForPackage(
                app.storageUuid, app.packageName, app.userHandle
        )).thenReturn(STATS)
            )
        ).thenReturn(STATS)
    }

    @Test
@@ -78,6 +81,24 @@ class AppStorageSizeTest {
        composeTestRule.waitUntil { storageSize.value == "120 B" }
    }

    @Test
    fun getStorageSize_throwException() {
        var storageSize = stateOf("Computing")
        whenever(
            storageStatsManager.queryStatsForPackage(
                app.storageUuid, app.packageName, app.userHandle
            )
        ).thenThrow(NameNotFoundException())

        composeTestRule.setContent {
            CompositionLocalProvider(LocalContext provides context) {
                storageSize = app.getStorageSize()
            }
        }

        composeTestRule.waitUntil { storageSize.value == "" }
    }

    companion object {
        private val STATS = StorageStats().apply {
            codeBytes = 100