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
This commit is contained in:
Chaohui Wang
2025-01-02 10:34:30 +08:00
parent 2d2770b835
commit 611c62294a
9 changed files with 30 additions and 66 deletions

View File

@@ -37,8 +37,7 @@ class AppDataUsageSummaryController(context: Context, preferenceKey: String) :
private val dataFlow = MutableStateFlow(NetworkUsageDetailsData.AllZero)
private val dataUsageFormatter = DataUsageFormatter(context)
private val emptyDataUsage =
DataUsageFormatter.FormattedDataUsage(context.getPlaceholder(), context.getPlaceholder())
private val emptyDataUsage = context.getPlaceholder()
private val totalUsageFlow = dataFlow.map {
dataUsageFormatter.formatDataUsage(it.totalUsage)
@@ -66,18 +65,15 @@ class AppDataUsageSummaryController(context: Context, preferenceKey: String) :
val backgroundUsage by backgroundUsageFlow.collectAsStateWithLifecycle(emptyDataUsage)
Preference(object : PreferenceModel {
override val title = stringResource(R.string.total_size_label)
override val summary = { totalUsage.displayText }
override val summaryContentDescription = { totalUsage.contentDescription }
override val summary = { totalUsage }
})
Preference(object : PreferenceModel {
override val title = stringResource(R.string.data_usage_label_foreground)
override val summary = { foregroundUsage.displayText }
override val summaryContentDescription = { foregroundUsage.contentDescription }
override val summary = { foregroundUsage }
})
Preference(object : PreferenceModel {
override val title = stringResource(R.string.data_usage_label_background)
override val summary = { backgroundUsage.displayText }
override val summaryContentDescription = { backgroundUsage.contentDescription }
override val summary = { backgroundUsage }
})
}
}

View File

@@ -172,7 +172,7 @@ open class DataUsageList : DashboardFragment() {
private fun updateSelectedCycle(usageData: NetworkUsageData) {
Log.d(TAG, "showing cycle $usageData")
usageAmount?.title = usageData.getDataUsedString(requireContext()).displayText
usageAmount?.title = usageData.getDataUsedString(requireContext())
viewModel.selectedCycleFlow.value = usageData
updateApps(usageData)

View File

@@ -16,40 +16,16 @@
package com.android.settings.datausage.lib
import android.annotation.StringRes
import android.content.Context
import android.content.res.Resources
import android.icu.text.UnicodeSet
import android.icu.text.UnicodeSetSpanner
import android.text.BidiFormatter
import android.text.format.Formatter
import com.android.internal.R
import com.android.settingslib.spaprivileged.framework.common.BytesFormatter
class DataUsageFormatter(private val context: Context) {
data class FormattedDataUsage(
val displayText: String,
val contentDescription: String,
) {
fun format(context: Context, @StringRes resId: Int, vararg formatArgs: Any?) =
FormattedDataUsage(
displayText = context.getString(resId, displayText, *formatArgs),
contentDescription = context.getString(resId, contentDescription, *formatArgs),
)
}
/** Formats the data usage. */
fun formatDataUsage(sizeBytes: Long): FormattedDataUsage {
val result = Formatter.formatBytes(context.resources, sizeBytes, Formatter.FLAG_IEC_UNITS)
return FormattedDataUsage(
displayText = BidiFormatter.getInstance().unicodeWrap(
context.getString(R.string.fileSizeSuffix, result.value, result.units)
),
contentDescription = context.getString(
R.string.fileSizeSuffix, result.value, result.unitsContentDescription
),
)
}
fun formatDataUsage(sizeBytes: Long): String =
BytesFormatter(context).format(sizeBytes, BytesFormatter.UseCase.DataUsage)
companion object {
/**

View File

@@ -20,7 +20,6 @@ import android.content.Context
import android.text.format.DateUtils
import android.util.Range
import com.android.settings.R
import com.android.settings.datausage.lib.DataUsageFormatter.FormattedDataUsage
/**
* Base data structure representing usage data in a period.
@@ -38,11 +37,11 @@ data class NetworkUsageData(
fun formatDateRange(context: Context): String =
DateUtils.formatDateRange(context, startTime, endTime, DATE_FORMAT)
fun formatUsage(context: Context): FormattedDataUsage =
fun formatUsage(context: Context): String =
DataUsageFormatter(context).formatDataUsage(usage)
fun getDataUsedString(context: Context): FormattedDataUsage =
formatUsage(context).format(context, R.string.data_used_template)
fun getDataUsedString(context: Context): String =
context.getString(R.string.data_used_template, formatUsage(context))
companion object {
val AllZero = NetworkUsageData(