/* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.appfunctions.providersources import android.app.usage.StorageStatsManager import android.content.Context import com.android.settings.appfunctions.DeviceStateAppFunctionType import com.android.settingslib.spaprivileged.framework.common.BytesFormatter import com.android.settingslib.spaprivileged.model.app.userHandle import com.google.android.appfunctions.schema.common.v1.devicestate.DeviceStateItem import com.google.android.appfunctions.schema.common.v1.devicestate.PerScreenDeviceStates class AppsStorageStateSource : DeviceStateSource { override val appFunctionType: DeviceStateAppFunctionType = DeviceStateAppFunctionType.GET_STORAGE override suspend fun get( context: Context, sharedDeviceStateData: SharedDeviceStateData, ): List { val storageStatsManager = context.getSystemService(StorageStatsManager::class.java) val bytesFormatter = BytesFormatter(context) val deviceStateItems = mutableListOf() for (app in sharedDeviceStateData.installedApplications) { val appName = app.label val packageName = app.info.packageName val stats = storageStatsManager.queryStatsForPackage( app.info.storageUuid, app.info.packageName, app.info.userHandle, ) val appBytes = stats.appBytes val dataBytes = stats.dataBytes val cacheBytes = stats.cacheBytes val totalBytes = appBytes + dataBytes deviceStateItems.add( DeviceStateItem( key = "app_size_package_$packageName", purpose = "app_size_package_$packageName", jsonValue = bytesFormatter.format(appBytes, BytesFormatter.UseCase.FileSize), hintText = "App: $appName", ) ) deviceStateItems.add( DeviceStateItem( key = "data_size_package_$packageName", purpose = "data_size_package_$packageName", jsonValue = bytesFormatter.format(dataBytes, BytesFormatter.UseCase.FileSize), hintText = "App: $appName", ) ) deviceStateItems.add( DeviceStateItem( key = "cache_size_package_$packageName", purpose = "cache_size_package_$packageName", jsonValue = bytesFormatter.format(cacheBytes, BytesFormatter.UseCase.FileSize), hintText = "App: $appName", ) ) deviceStateItems.add( DeviceStateItem( key = "total_size_package_$packageName", purpose = "total_size_package_$packageName", jsonValue = bytesFormatter.format(totalBytes, BytesFormatter.UseCase.FileSize), hintText = "App: $appName", ) ) } return listOf( PerScreenDeviceStates(description = "App Storage", deviceStateItems = deviceStateItems) ) } }