Loading packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/AppStorageSize.kt +13 −4 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 } } packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/template/app/AppStorageSizeTest.kt +24 −3 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 Loading @@ -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 Loading Loading
packages/SettingsLib/SpaPrivileged/src/com/android/settingslib/spaprivileged/template/app/AppStorageSize.kt +13 −4 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 } }
packages/SettingsLib/SpaPrivileged/tests/src/com/android/settingslib/spaprivileged/template/app/AppStorageSizeTest.kt +24 −3 Original line number Diff line number Diff line Loading @@ -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 Loading Loading @@ -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 Loading @@ -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 Loading