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..133d68641df50e02d3957086a259917623292b4e 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,54 @@ 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; |
+ String[] locale_list = locale_string.split(","); |
+ |
+ HashSet<String> seen_locales = new HashSet<String>(); |
+ ArrayList<String> unique_list = new ArrayList<String>(); |
+ for (String locale : locale_list) { |
+ // TODO(yirui): Support BCP47 compliant format including 3-letter |
Seigo Nonaka
2016/10/07 10:06:40
nit: you can continue line until 100 chars.
Yirui Huang
2016/10/07 10:34:15
Done.
|
+ // country code, '-' separator and missing region 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 region = locale.substring(3); |
+ String languageTag = makeLanguageTag(language, region); |
- 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); |
+ // If language is not in the accept languages list, also add language |
Seigo Nonaka
2016/10/07 10:06:40
nit: you can continue line until 100 chars.
Yirui Huang
2016/10/07 10:34:14
Done.
|
+ // code. 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 language_add = unique_list.get(i).substring(0, 2); |
ksk1
2016/10/07 09:16:23
How about:
String locale = unique_list.get(i);
Str
Yirui Huang
2016/10/07 10:34:15
Done.
|
+ if (!seen_languages.contains(language_add)) { |
+ seen_languages.add(language_add); |
+ output_list.add(language_add); |
+ } |
+ output_list.add(unique_list.get(i)); |
} |
- parts.format("%s", acceptLanguages); |
- return parts.toString(); |
+ Collections.reverse(output_list); |
+ return TextUtils.join(",", output_list); |
} |
/** |