Files
packages_apps_Settings/src/com/android/settings/datausage/AppDataUsageSummaryController.kt
Chaohui Wang 611c62294a Use BytesFormatter for data usage
Change "480 B" to "480 byte".

And no longer use FormattedDataUsage.

Bug: 321861088
Flag: EXEMPT bug fix
Test: manual - App data usage
Test: unit test
Change-Id: I9ed220e2d5b8fc512d7d28f6fa7faebb37beab83
2025-01-02 18:14:10 -08:00

81 lines
3.2 KiB
Kotlin

/*
* Copyright (C) 2024 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.datausage
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settings.R
import com.android.settings.datausage.lib.DataUsageFormatter
import com.android.settings.datausage.lib.NetworkUsageDetailsData
import com.android.settings.spa.preference.ComposePreferenceController
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spa.widget.ui.Category
import com.android.settingslib.spaprivileged.framework.compose.getPlaceholder
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
class AppDataUsageSummaryController(context: Context, preferenceKey: String) :
ComposePreferenceController(context, preferenceKey) {
private val dataFlow = MutableStateFlow(NetworkUsageDetailsData.AllZero)
private val dataUsageFormatter = DataUsageFormatter(context)
private val emptyDataUsage = context.getPlaceholder()
private val totalUsageFlow = dataFlow.map {
dataUsageFormatter.formatDataUsage(it.totalUsage)
}
private val foregroundUsageFlow = dataFlow.map {
dataUsageFormatter.formatDataUsage(it.foregroundUsage)
}
private val backgroundUsageFlow = dataFlow.map {
dataUsageFormatter.formatDataUsage(it.backgroundUsage)
}
override fun getAvailabilityStatus() = AVAILABLE
fun update(data: NetworkUsageDetailsData) {
dataFlow.value = data
}
@Composable
override fun Content() {
Category {
val totalUsage by totalUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
val foregroundUsage by foregroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
val backgroundUsage by backgroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
Preference(object : PreferenceModel {
override val title = stringResource(R.string.total_size_label)
override val summary = { totalUsage }
})
Preference(object : PreferenceModel {
override val title = stringResource(R.string.data_usage_label_foreground)
override val summary = { foregroundUsage }
})
Preference(object : PreferenceModel {
override val title = stringResource(R.string.data_usage_label_background)
override val summary = { backgroundUsage }
})
}
}
}