| OLD | NEW |
| 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.contextualsearch; | 5 package org.chromium.chrome.browser.contextualsearch; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.text.TextUtils; |
| 8 | 9 |
| 9 import org.chromium.base.VisibleForTesting; | 10 import org.chromium.base.VisibleForTesting; |
| 10 import org.chromium.chrome.browser.ChromeVersionInfo; | 11 import org.chromium.chrome.browser.ChromeVersionInfo; |
| 11 import org.chromium.chrome.browser.contextualsearch.ContextualSearchSelectionCon
troller.SelectionType; | 12 import org.chromium.chrome.browser.contextualsearch.ContextualSearchSelectionCon
troller.SelectionType; |
| 12 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; | 13 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; |
| 13 import org.chromium.chrome.browser.preferences.NetworkPredictionOptions; | 14 import org.chromium.chrome.browser.preferences.NetworkPredictionOptions; |
| 14 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 15 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 15 import org.chromium.content.browser.ContentViewCore; | 16 import org.chromium.content.browser.ContentViewCore; |
| 16 | 17 |
| 17 import java.net.URL; | 18 import java.net.URL; |
| 19 import java.util.List; |
| 20 import java.util.Locale; |
| 18 import java.util.regex.Pattern; | 21 import java.util.regex.Pattern; |
| 19 | 22 |
| 20 import javax.annotation.Nullable; | 23 import javax.annotation.Nullable; |
| 21 | 24 |
| 22 | 25 |
| 23 /** | 26 /** |
| 24 * Handles policy decisions for the {@code ContextualSearchManager}. | 27 * Handles policy decisions for the {@code ContextualSearchManager}. |
| 25 */ | 28 */ |
| 26 class ContextualSearchPolicy { | 29 class ContextualSearchPolicy { |
| 27 private static final Pattern CONTAINS_WHITESPACE_PATTERN = Pattern.compile("
\\s"); | 30 private static final Pattern CONTAINS_WHITESPACE_PATTERN = Pattern.compile("
\\s"); |
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 } | 383 } |
| 381 | 384 |
| 382 /** | 385 /** |
| 383 * @return The number of times the user has tapped since the last panel open
. | 386 * @return The number of times the user has tapped since the last panel open
. |
| 384 */ | 387 */ |
| 385 @VisibleForTesting | 388 @VisibleForTesting |
| 386 int getTapCount() { | 389 int getTapCount() { |
| 387 return mPreferenceManager.getContextualSearchTapCount(); | 390 return mPreferenceManager.getContextualSearchTapCount(); |
| 388 } | 391 } |
| 389 | 392 |
| 393 /** |
| 394 * Determines whether translation is needed between the given languages. |
| 395 * @param sourceLanguage The source language code; language we're translatin
g from. |
| 396 * @param targetLanguages A list of target language codes; languages we migh
t translate to. |
| 397 * @return Whether translation is needed or not. |
| 398 */ |
| 399 boolean needsTranslation(String sourceLanguage, List<String> targetLanguages
) { |
| 400 // For now, we just look for a language match. |
| 401 for (String targetLanguage : targetLanguages) { |
| 402 if (TextUtils.equals(sourceLanguage, targetLanguage)) { |
| 403 return false; |
| 404 } |
| 405 } |
| 406 return true; |
| 407 } |
| 408 |
| 409 /** |
| 410 * Determines the best target language. |
| 411 */ |
| 412 String bestTargetLanguage(List<String> targetLanguages) { |
| 413 // For now, we just return the first language, unless it's English (due
to over-usage). |
| 414 // TODO(donnd): Improve this logic. Determining the right language seems
non-trivial. |
| 415 // E.g. If this language doesn't match the user's server preferences, th
ey might see a page |
| 416 // in one language and the one box translation in another, which might b
e confusing. |
| 417 if (TextUtils.equals(targetLanguages.get(0), Locale.ENGLISH.getLanguage(
)) |
| 418 && targetLanguages.size() > 1) { |
| 419 return targetLanguages.get(1); |
| 420 } else { |
| 421 return targetLanguages.get(0); |
| 422 } |
| 423 } |
| 424 |
| 425 /** |
| 426 * @return Whether translation should be enabled or not. |
| 427 */ |
| 428 boolean isTranslationEnabled() { |
| 429 return ContextualSearchFieldTrial.isTranslationOneboxEnabled(); |
| 430 } |
| 431 |
| 390 // -------------------------------------------------------------------------
------------------- | 432 // -------------------------------------------------------------------------
------------------- |
| 391 // Private helpers. | 433 // Private helpers. |
| 392 // -------------------------------------------------------------------------
------------------- | 434 // -------------------------------------------------------------------------
------------------- |
| 393 | 435 |
| 394 /** | 436 /** |
| 395 * @return Whether a promo is needed because the user is still undecided | 437 * @return Whether a promo is needed because the user is still undecided |
| 396 * on enabling or disabling the feature. | 438 * on enabling or disabling the feature. |
| 397 */ | 439 */ |
| 398 private boolean isUserUndecided() { | 440 private boolean isUserUndecided() { |
| 399 // TODO(donnd) use dependency injection for the PrefServiceBridge instea
d! | 441 // TODO(donnd) use dependency injection for the PrefServiceBridge instea
d! |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 495 |
| 454 /** | 496 /** |
| 455 * @return The limit of the number of taps to resolve using search term reso
lution. | 497 * @return The limit of the number of taps to resolve using search term reso
lution. |
| 456 */ | 498 */ |
| 457 private int getTapResolveLimit() { | 499 private int getTapResolveLimit() { |
| 458 return isUserUndecided() | 500 return isUserUndecided() |
| 459 ? ContextualSearchFieldTrial.getTapResolveLimitForUndecided() | 501 ? ContextualSearchFieldTrial.getTapResolveLimitForUndecided() |
| 460 : ContextualSearchFieldTrial.getTapResolveLimitForDecided(); | 502 : ContextualSearchFieldTrial.getTapResolveLimitForDecided(); |
| 461 } | 503 } |
| 462 } | 504 } |
| OLD | NEW |