Use ListFormatter to join strings

Currently in Settings we are using R.string.join_many_items_first, R.string.join_many_items_middle and R.string.join_many_items_last to manually join strings. The join code is messy and the joined string is incorrect in some languages, so we migrate all string join to just use ListFormatter.getInstance().format().

Bug: b/78248791
Test: robotests
Change-Id: I898339978e6e2027587e28994b0280fa46821fd6
This commit is contained in:
jyhshiangwang
2018-04-24 16:05:57 +08:00
parent 7c45f59478
commit 4d015b17b3
12 changed files with 210 additions and 212 deletions

View File

@@ -17,6 +17,7 @@ package com.android.settings.applications;
import android.app.Activity;
import android.content.Context;
import android.icu.text.ListFormatter;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
@@ -138,25 +139,22 @@ public class DefaultAppSettings extends DashboardFragment {
if (!listening) {
return;
}
CharSequence summary = concatSummaryText(
mDefaultBrowserPreferenceController.getDefaultAppLabel(),
mDefaultPhonePreferenceController.getDefaultAppLabel());
summary = concatSummaryText(summary,
mDefaultSmsPreferenceController.getDefaultAppLabel());
final List<CharSequence> summaries = new ArrayList<>();
if(!TextUtils.isEmpty(mDefaultBrowserPreferenceController.getDefaultAppLabel())) {
summaries.add(mDefaultBrowserPreferenceController.getDefaultAppLabel());
}
if(!TextUtils.isEmpty(mDefaultPhonePreferenceController.getDefaultAppLabel())) {
summaries.add(mDefaultPhonePreferenceController.getDefaultAppLabel());
}
if(!TextUtils.isEmpty(mDefaultSmsPreferenceController.getDefaultAppLabel())) {
summaries.add(mDefaultSmsPreferenceController.getDefaultAppLabel());
}
CharSequence summary = ListFormatter.getInstance().format(summaries);
if (!TextUtils.isEmpty(summary)) {
mSummaryLoader.setSummary(this, summary);
}
}
private CharSequence concatSummaryText(CharSequence summary1, CharSequence summary2) {
if (TextUtils.isEmpty(summary1)) {
return summary2;
}
if (TextUtils.isEmpty(summary2)) {
return summary1;
}
return mContext.getString(R.string.join_many_items_middle, summary1, summary2);
}
}
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =