From 0761a4bf064239eb0c7d4c21e8d1d8c00eb3be93 Mon Sep 17 00:00:00 2001 From: Roozbeh Pournader Date: Thu, 9 Jun 2016 16:54:43 -0700 Subject: [PATCH] Make strings for data usage easier to localize The strings were complex to localize, especially since they used formatting that needed to be carefully reconstructed for cases where the amount of data appears in the middle of a string. Also, we already had a localization for file sizes, which we should avoid redoing, to minimize discrepancies. Finally, we are now bidi wrapping the usage amount, as it may break in the RTL locales otherwise, since the RTL locales may be using Latin abbreviations for gigabyte etc that are LTR. Since formatting needs to be applied to everything except the number of units in the amount of data used, and markup should be avoided since it will confuse bidi wrapping, the code carefully constructs CharSequence templates that are then decorated with spans after getting filled in. Bug: 28747101 Change-Id: I18756b4de99eb551a51fe5e74d1c9cf505551f08 --- res/values/strings.xml | 6 ++-- .../settings/datausage/DataUsageSummary.java | 32 ++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index aba7b66add6..e0caa955e1c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7248,13 +7248,13 @@ Ethernet - ^1 ^2 cellular data + ^1 cellular data - ^1 ^2 Wi-Fi data + ^1 Wi-Fi data - ^1 ^2 ethernet data + ^1 ethernet data %1$s Data warning diff --git a/src/com/android/settings/datausage/DataUsageSummary.java b/src/com/android/settings/datausage/DataUsageSummary.java index 51804c132de..8de6a844537 100644 --- a/src/com/android/settings/datausage/DataUsageSummary.java +++ b/src/com/android/settings/datausage/DataUsageSummary.java @@ -32,8 +32,12 @@ import android.support.v7.preference.PreferenceScreen; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; +import android.text.BidiFormatter; +import android.text.Spannable; +import android.text.SpannableString; import android.text.TextUtils; import android.text.format.Formatter; +import android.text.style.RelativeSizeSpan; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -183,15 +187,35 @@ public class DataUsageSummary extends DataUsageBase implements Indexable { updateState(); } + private static CharSequence formatTitle(Context context, String template, long usageLevel) { + final float LARGER_SIZE = 1.25f * 1.25f; // (1/0.8)^2 + final float SMALLER_SIZE = 1.0f / LARGER_SIZE; // 0.8^2 + final int FLAGS = Spannable.SPAN_INCLUSIVE_INCLUSIVE; + + final Formatter.BytesResult usedResult = Formatter.formatBytes(context.getResources(), + usageLevel, Formatter.FLAG_SHORTER); + final SpannableString enlargedValue = new SpannableString(usedResult.value); + enlargedValue.setSpan(new RelativeSizeSpan(LARGER_SIZE), 0, enlargedValue.length(), FLAGS); + + final SpannableString amountTemplate = new SpannableString( + context.getString(com.android.internal.R.string.fileSizeSuffix) + .replace("%1$s", "^1").replace("%2$s", "^2")); + final CharSequence formattedUsage = TextUtils.expandTemplate(amountTemplate, + enlargedValue, usedResult.units); + + final SpannableString fullTemplate = new SpannableString(template); + fullTemplate.setSpan(new RelativeSizeSpan(SMALLER_SIZE), 0, fullTemplate.length(), FLAGS); + return TextUtils.expandTemplate(fullTemplate, + BidiFormatter.getInstance().unicodeWrap(formattedUsage)); + } + private void updateState() { DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo( mDefaultTemplate); Context context = getContext(); if (mSummaryPreference != null) { - Formatter.BytesResult usedResult = Formatter.formatBytes(context.getResources(), - info.usageLevel, Formatter.FLAG_SHORTER); - mSummaryPreference.setTitle(TextUtils.expandTemplate(getText(mDataUsageTemplate), - usedResult.value, usedResult.units)); + mSummaryPreference.setTitle( + formatTitle(context, getString(mDataUsageTemplate), info.usageLevel)); long limit = info.limitLevel; if (limit <= 0) { limit = info.warningLevel;