Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java |
| index 2bd35ad4d8e3bba2dd24ddf461955c3a54641e7d..784b2c869c63db551b1cdc9a33cb0fe1aa644fa4 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java |
| @@ -8,6 +8,7 @@ import android.content.Context; |
| import android.graphics.Bitmap; |
| import android.os.AsyncTask; |
| import android.os.Build; |
| +import android.text.TextUtils; |
| import org.chromium.base.Log; |
| import org.chromium.base.ThreadUtils; |
| @@ -25,7 +26,9 @@ import org.json.JSONObject; |
| import java.net.MalformedURLException; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| +import java.util.Collections; |
| import java.util.Formatter; |
| +import java.util.HashSet; |
| import java.util.Locale; |
| /** |
| @@ -263,35 +266,55 @@ class PwsClientImpl implements PwsClient { |
| * Get the language code for the default locale and prepend it to the Accept-Language string if |
| * it isn't already present. The logic should match PrependToAcceptLanguagesIfNecessary in |
| * chrome/browser/android/preferences/pref_service_bridge.cc |
| - * @param locale A string representing the default locale. |
| + * @param locales A string representing a default locale or a list of default locales. |
| * @param acceptLanguages The default language list for the language of the user's locale. |
| * @return An updated language list. |
| */ |
| @VisibleForTesting |
| - static String prependToAcceptLanguagesIfNecessary(String locale, String acceptLanguages) |
| - { |
| - if (locale.length() != 5 || locale.charAt(2) != '_') { |
| - return acceptLanguages; |
| - } |
| + static String prependToAcceptLanguagesIfNecessary(String locales, String acceptLanguages) { |
| + String locale_string = locales + "," + acceptLanguages; |
|
Maria
2016/10/07 19:14:29
style: use camelCase for variable names in Java he
Yirui Huang
2016/10/09 05:11:37
Done.
|
| + String[] locale_list = locale_string.split(","); |
| + |
| + HashSet<String> seen_locales = new HashSet<String>(); |
|
Maria
2016/10/07 19:14:29
nit: can use HashSet<>() and ArrayList<>() etc her
Yirui Huang
2016/10/09 05:11:37
Done.
|
| + ArrayList<String> unique_list = new ArrayList<String>(); |
| + for (String locale : locale_list) { |
| + // TODO(yirui): Support BCP47 compliant format including 3-letter country code, |
| + // '-' separator and missing country case. |
| + if (locale.length() != 5 || (locale.charAt(2) != '_' && locale.charAt(2) != '-')) { |
| + // Skip checking not well formed locales. |
| + continue; |
| + } |
| - String language = locale.substring(0, 2); |
| - String region = locale.substring(3); |
| - String languageTag = makeLanguageTag(language, region); |
| + String language = locale.substring(0, 2); |
| + String country = locale.substring(3); |
| + String languageTag = makeLanguageTag(language, country); |
| - if (acceptLanguages.contains(languageTag)) { |
| - return acceptLanguages; |
| + if (seen_locales.contains(languageTag)) { |
| + continue; |
| + } |
| + unique_list.add(languageTag); |
| + seen_locales.add(languageTag); |
| } |
| - Formatter parts = new Formatter(); |
| - parts.format("%s,", languageTag); |
| // If language is not in the accept languages list, also add language code. |
| - // This will work with the IDS_ACCEPT_LANGUAGES localized strings bundled with Chrome but |
| - // may fail on arbitrary lists of language tags due to differences in case and whitespace. |
| - if (!acceptLanguages.contains(language + ",") && !acceptLanguages.endsWith(language)) { |
| - parts.format("%s,", language); |
| + // A language code should only be inserted after the last languageTag that |
| + // contains that language. |
| + // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled |
| + // with Chrome but may fail on arbitrary lists of language tags due to |
| + // differences in case and whitespace. |
| + HashSet<String> seen_languages = new HashSet<String>(); |
| + ArrayList<String> output_list = new ArrayList<String>(); |
| + for (int i = unique_list.size() - 1; i >= 0; i--) { |
| + String locale_add = unique_list.get(i); |
| + String language_add = locale_add.substring(0, 2); |
| + if (!seen_languages.contains(language_add)) { |
| + seen_languages.add(language_add); |
| + output_list.add(language_add); |
| + } |
| + output_list.add(locale_add); |
| } |
| - parts.format("%s", acceptLanguages); |
| - return parts.toString(); |
| + Collections.reverse(output_list); |
| + return TextUtils.join(",", output_list); |
| } |
| /** |