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

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

Merge "Create AppStorageSize for Spa"

parents 41dfed3e 44d9b054
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ android_library {
    ],
    kotlincflags: [
        "-Xjvm-default=all",
        "-Xopt-in=kotlin.RequiresOptIn",
        "-opt-in=kotlin.RequiresOptIn",
    ],
}

+4 −0
Original line number Diff line number Diff line
package com.android.settingslib.spaprivileged.framework.common

import android.app.admin.DevicePolicyManager
import android.app.usage.StorageStatsManager
import android.content.Context
import android.os.UserManager

@@ -9,3 +10,6 @@ val Context.userManager get() = getSystemService(UserManager::class.java)!!

/** The [DevicePolicyManager] instance. */
val Context.devicePolicyManager get() = getSystemService(DevicePolicyManager::class.java)!!

/** The [StorageStatsManager] instance. */
val Context.storageStatsManager get() = getSystemService(StorageStatsManager::class.java)!!
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.settingslib.spaprivileged.template.app

import android.content.Context
import android.content.pm.ApplicationInfo
import android.text.format.Formatter
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.produceState
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.android.settingslib.spaprivileged.R
import com.android.settingslib.spaprivileged.framework.common.storageStatsManager
import com.android.settingslib.spaprivileged.model.app.userHandle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@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))
        }
    }
}

private fun ApplicationInfo.calculateSizeBytes(context: Context): Long {
    val storageStatsManager = context.storageStatsManager
    val stats = storageStatsManager.queryStatsForPackage(storageUuid, packageName, userHandle)
    return stats.codeBytes + stats.dataBytes + stats.cacheBytes
}
+90 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.settingslib.spaprivileged.template.app

import android.app.usage.StorageStats
import android.app.usage.StorageStatsManager
import android.content.Context
import android.content.pm.ApplicationInfo
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.framework.compose.stateOf
import com.android.settingslib.spaprivileged.framework.common.storageStatsManager
import com.android.settingslib.spaprivileged.model.app.userHandle
import com.google.common.truth.Truth.assertThat
import java.util.UUID
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Spy
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.mockito.Mockito.`when` as whenever

@RunWith(AndroidJUnit4::class)
class AppStorageSizeTest {
    @JvmField
    @Rule
    val mockito: MockitoRule = MockitoJUnit.rule()

    @get:Rule
    val composeTestRule = createComposeRule()

    @Spy
    private var context: Context = ApplicationProvider.getApplicationContext()

    @Mock
    private lateinit var storageStatsManager: StorageStatsManager

    private val app = ApplicationInfo().apply {
        storageUuid = UUID.randomUUID()
    }

    @Before
    fun setUp() {
        whenever(context.storageStatsManager).thenReturn(storageStatsManager)
        whenever(storageStatsManager.queryStatsForPackage(
            app.storageUuid, app.packageName, app.userHandle
        )).thenReturn(STATS)
    }

    @Test
    fun getStorageSize() {
        var storageSize = stateOf("")

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

        assertThat(storageSize.value).isEqualTo("123 B")
    }

    companion object {
        private val STATS = StorageStats().apply {
            codeBytes = 100
            dataBytes = 20
            cacheBytes = 3
        }
    }
}