Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1026)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java

Issue 2559243003: Extend with a language code in http accept languages
Patch Set: Rebased, fixed nits and handed over this CL :( Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.physicalweb; 5 package org.chromium.chrome.browser.physicalweb;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.graphics.Bitmap; 8 import android.graphics.Bitmap;
9 import android.os.AsyncTask; 9 import android.os.AsyncTask;
10 import android.os.Build; 10 import android.os.Build;
11 import android.text.TextUtils; 11 import android.text.TextUtils;
12 12
13 import org.json.JSONArray; 13 import org.json.JSONArray;
14 import org.json.JSONException; 14 import org.json.JSONException;
15 import org.json.JSONObject; 15 import org.json.JSONObject;
16 16
17 import org.chromium.base.LocaleUtils; 17 import org.chromium.base.LocaleUtils;
18 import org.chromium.base.Log; 18 import org.chromium.base.Log;
19 import org.chromium.base.ThreadUtils; 19 import org.chromium.base.ThreadUtils;
20 import org.chromium.base.VisibleForTesting; 20 import org.chromium.base.VisibleForTesting;
21 import org.chromium.chrome.GoogleAPIKeys; 21 import org.chromium.chrome.GoogleAPIKeys;
22 import org.chromium.chrome.R; 22 import org.chromium.chrome.R;
23 import org.chromium.chrome.browser.ChromeVersionInfo; 23 import org.chromium.chrome.browser.ChromeVersionInfo;
24 import org.chromium.chrome.browser.physicalweb.PwsClient.FetchIconCallback; 24 import org.chromium.chrome.browser.physicalweb.PwsClient.FetchIconCallback;
25 import org.chromium.chrome.browser.physicalweb.PwsClient.ResolveScanCallback; 25 import org.chromium.chrome.browser.physicalweb.PwsClient.ResolveScanCallback;
26 26
27 import java.net.MalformedURLException; 27 import java.net.MalformedURLException;
28 import java.util.ArrayList; 28 import java.util.ArrayList;
29 import java.util.Collection; 29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Formatter; 30 import java.util.Formatter;
32 import java.util.HashSet; 31 import java.util.HashSet;
33 import java.util.Locale; 32 import java.util.Locale;
34 33
35 /** 34 /**
36 * This class sends requests to the Physical Web Service. 35 * This class sends requests to the Physical Web Service.
37 */ 36 */
38 class PwsClientImpl implements PwsClient { 37 class PwsClientImpl implements PwsClient {
39 private static final String TAG = "PhysicalWeb"; 38 private static final String TAG = "PhysicalWeb";
40 private static final String ENDPOINT_URL = 39 private static final String ENDPOINT_URL =
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 * chrome/browser/android/preferences/pref_service_bridge.cc 244 * chrome/browser/android/preferences/pref_service_bridge.cc
246 * @param locales A comma separated string that represents a list of default locales. 245 * @param locales A comma separated string that represents a list of default locales.
247 * @param acceptLanguages The default language list for the language of the user's locales. 246 * @param acceptLanguages The default language list for the language of the user's locales.
248 * @return An updated language list. 247 * @return An updated language list.
249 */ 248 */
250 @VisibleForTesting 249 @VisibleForTesting
251 static String prependToAcceptLanguagesIfNecessary(String locales, String acc eptLanguages) { 250 static String prependToAcceptLanguagesIfNecessary(String locales, String acc eptLanguages) {
252 String localeStrings = locales + "," + acceptLanguages; 251 String localeStrings = locales + "," + acceptLanguages;
253 String[] localeList = localeStrings.split(","); 252 String[] localeList = localeStrings.split(",");
254 253
255 ArrayList<Locale> uniqueList = new ArrayList<>(); 254 HashSet<Locale> seenLocales = new HashSet<>();
255 ArrayList<String> uniqueList = new ArrayList<>();
256 for (String localeString : localeList) { 256 for (String localeString : localeList) {
257 Locale locale = LocaleUtils.forLanguageTag(localeString); 257 Locale locale = LocaleUtils.forLanguageTag(localeString);
258 if (uniqueList.contains(locale) || locale.getLanguage().isEmpty()) { 258 if (seenLocales.contains(locale) || locale.getLanguage().isEmpty()) {
259 continue; 259 continue;
260 } 260 }
261 uniqueList.add(locale); 261 seenLocales.add(locale);
262 uniqueList.add(LocaleUtils.toLanguageTag(locale));
262 } 263 }
263 264 return TextUtils.join(",", uniqueList);
264 // If language is not in the accept languages list, also add language co de.
265 // A language code should only be inserted after the last languageTag th at
266 // contains that language.
267 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
268 // with Chrome but may fail on arbitrary lists of language tags due to
269 // differences in case and whitespace.
270 HashSet<String> seenLanguages = new HashSet<>();
271 ArrayList<String> outputList = new ArrayList<>();
272 for (int i = uniqueList.size() - 1; i >= 0; i--) {
273 Locale localeAdd = uniqueList.get(i);
274 String languageAdd = localeAdd.getLanguage();
275 String countryAdd = localeAdd.getCountry();
276
277 if (!seenLanguages.contains(languageAdd)) {
278 seenLanguages.add(languageAdd);
279 outputList.add(languageAdd);
280 }
281 if (!countryAdd.isEmpty()) {
282 outputList.add(LocaleUtils.toLanguageTag(localeAdd));
283 }
284 }
285 Collections.reverse(outputList);
286 return TextUtils.join(",", outputList);
287 } 265 }
288 266
289 /** 267 /**
290 * Given a list of comma-delimited language codes in decreasing order of pre ference, insert 268 * Given a list of comma-delimited language codes in decreasing order of pre ference, insert
291 * q-values to represent the relative quality/precedence of each language. T he logic should 269 * q-values to represent the relative quality/precedence of each language. T he logic should
292 * match GenerateAcceptLanguageHeader in net/http/http_util.cc. 270 * match GenerateAcceptLanguageHeader in net/http/http_util.cc.
293 * @param languageList A comma-delimited list of language codes containing n o whitespace. 271 * @param languageList A comma-delimited list of language codes containing n o whitespace.
294 * @return An Accept-Language header with q-values. 272 * @return An Accept-Language header with q-values.
295 */ 273 */
296 @VisibleForTesting 274 @VisibleForTesting
(...skipping 12 matching lines...) Expand all
309 langListWithQ.format(",%s;q=0.%d", language, qvalue10); 287 langListWithQ.format(",%s;q=0.%d", language, qvalue10);
310 } 288 }
311 // It does not make sense to have 'q=0'. 289 // It does not make sense to have 'q=0'.
312 if (qvalue10 > kQvalueDecrement10) { 290 if (qvalue10 > kQvalueDecrement10) {
313 qvalue10 -= kQvalueDecrement10; 291 qvalue10 -= kQvalueDecrement10;
314 } 292 }
315 } 293 }
316 return langListWithQ.toString(); 294 return langListWithQ.toString();
317 } 295 }
318 } 296 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698