Merge "Make strings for data usage easier to localize"

This commit is contained in:
Roozbeh Pournader
2016-06-14 00:38:57 +00:00
committed by Android (Google) Code Review
2 changed files with 31 additions and 7 deletions

View File

@@ -7248,13 +7248,13 @@
<string name="ethernet">Ethernet</string> <string name="ethernet">Ethernet</string>
<!-- Format string for amount of cellular data used [CHAR LIMIT=30] --> <!-- Format string for amount of cellular data used [CHAR LIMIT=30] -->
<string name="cell_data_template"><xliff:g id="number" example="128">^1</xliff:g><small><small> <xliff:g id="unit" example="KB">^2</xliff:g></small></small><small><small> cellular data</small></small></string> <string name="cell_data_template"><xliff:g id="amount" example="1 GB">^1</xliff:g> cellular data</string>
<!-- Format string for amount of wifi data used [CHAR LIMIT=30] --> <!-- Format string for amount of wifi data used [CHAR LIMIT=30] -->
<string name="wifi_data_template"><xliff:g id="number" example="128">^1</xliff:g><small><small> <xliff:g id="unit" example="KB">^2</xliff:g></small></small><small><small> Wi-Fi data</small></small></string> <string name="wifi_data_template"><xliff:g id="amount" example="1 GB">^1</xliff:g> Wi-Fi data</string>
<!-- Format string for amount of ethernet data used [CHAR LIMIT=30] --> <!-- Format string for amount of ethernet data used [CHAR LIMIT=30] -->
<string name="ethernet_data_template"><xliff:g id="number" example="128">^1</xliff:g><small><small> <xliff:g id="unit" example="KB">^2</xliff:g></small></small><small><small> ethernet data</small></small></string> <string name="ethernet_data_template"><xliff:g id="amount" example="1 GB">^1</xliff:g> ethernet data</string>
<!-- Format for a summary describing the amount of data before the user is warned [CHAR LIMIT=NONE] --> <!-- Format for a summary describing the amount of data before the user is warned [CHAR LIMIT=NONE] -->
<string name="cell_warning_only"><xliff:g name="amount" example="1 GB">%1$s</xliff:g> Data warning</string> <string name="cell_warning_only"><xliff:g name="amount" example="1 GB">%1$s</xliff:g> Data warning</string>

View File

@@ -32,8 +32,12 @@ import android.support.v7.preference.PreferenceScreen;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.BidiFormatter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.text.style.RelativeSizeSpan;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
@@ -183,15 +187,35 @@ public class DataUsageSummary extends DataUsageBase implements Indexable {
updateState(); 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() { private void updateState() {
DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo( DataUsageController.DataUsageInfo info = mDataUsageController.getDataUsageInfo(
mDefaultTemplate); mDefaultTemplate);
Context context = getContext(); Context context = getContext();
if (mSummaryPreference != null) { if (mSummaryPreference != null) {
Formatter.BytesResult usedResult = Formatter.formatBytes(context.getResources(), mSummaryPreference.setTitle(
info.usageLevel, Formatter.FLAG_SHORTER); formatTitle(context, getString(mDataUsageTemplate), info.usageLevel));
mSummaryPreference.setTitle(TextUtils.expandTemplate(getText(mDataUsageTemplate),
usedResult.value, usedResult.units));
long limit = info.limitLevel; long limit = info.limitLevel;
if (limit <= 0) { if (limit <= 0) {
limit = info.warningLevel; limit = info.warningLevel;