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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchManager.java

Issue 1463583004: [Contextual Search] Trigger translation on long-press. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tiny tweak. Created 5 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.contextualsearch; 5 package org.chromium.chrome.browser.contextualsearch;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.content.Context; 8 import android.content.Context;
9 import android.text.TextUtils; 9 import android.text.TextUtils;
10 import android.view.View; 10 import android.view.View;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 mNetworkCommunicator.getBasePageUrl())) { 485 mNetworkCommunicator.getBasePageUrl())) {
486 mNetworkCommunicator.startSearchTermResolutionRequest( 486 mNetworkCommunicator.startSearchTermResolutionRequest(
487 mSelectionController.getSelectedText()); 487 mSelectionController.getSelectedText());
488 didRequestSurroundings = true; 488 didRequestSurroundings = true;
489 // Cache the target languages in case they are needed for translatio n. 489 // Cache the target languages in case they are needed for translatio n.
490 if (!mPolicy.disableForceTranslationOnebox()) getReadableLanguages() ; 490 if (!mPolicy.disableForceTranslationOnebox()) getReadableLanguages() ;
491 } else { 491 } else {
492 boolean shouldPrefetch = mPolicy.shouldPrefetchSearchResult(isTap); 492 boolean shouldPrefetch = mPolicy.shouldPrefetchSearchResult(isTap);
493 mSearchRequest = new ContextualSearchRequest(mSelectionController.ge tSelectedText(), 493 mSearchRequest = new ContextualSearchRequest(mSelectionController.ge tSelectedText(),
494 null, shouldPrefetch); 494 null, shouldPrefetch);
495 // TODO(donnd): figure out a way to do translation on long-press sel ections. 495 forceAutoDetectTranslateUnlessDisabled(mSearchRequest);
496 mDidStartLoadingResolvedSearchRequest = false; 496 mDidStartLoadingResolvedSearchRequest = false;
497 mSearchPanel.displaySearchTerm(mSelectionController.getSelectedText( )); 497 mSearchPanel.displaySearchTerm(mSelectionController.getSelectedText( ));
498 if (shouldPrefetch) loadSearchUrl(); 498 if (shouldPrefetch) loadSearchUrl();
499 } 499 }
500 500
501 if (!didRequestSurroundings) { 501 if (!didRequestSurroundings) {
502 // Gather surrounding text for Icing integration, which will make th e selection and 502 // Gather surrounding text for Icing integration, which will make th e selection and
503 // a shorter version of the surroundings available for Conversationa l Search. 503 // a shorter version of the surroundings available for Conversationa l Search.
504 // Although the surroundings are extracted, they will not be sent to the server as 504 // Although the surroundings are extracted, they will not be sent to the server as
505 // part of search term resolution, just sent to Icing which keeps th em local until 505 // part of search term resolution, just sent to Icing which keeps th em local until
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 searchTerm = mSelectionController.getSelectedText(); 731 searchTerm = mSelectionController.getSelectedText();
732 alternateTerm = null; 732 alternateTerm = null;
733 doPreventPreload = true; 733 doPreventPreload = true;
734 } 734 }
735 if (!TextUtils.isEmpty(searchTerm)) { 735 if (!TextUtils.isEmpty(searchTerm)) {
736 // TODO(donnd): Instead of preloading, we should prefetch (ie the UR L should not 736 // TODO(donnd): Instead of preloading, we should prefetch (ie the UR L should not
737 // appear in the user's history until the user views it). See crbug .com/406446. 737 // appear in the user's history until the user views it). See crbug .com/406446.
738 boolean shouldPreload = !doPreventPreload && mPolicy.shouldPrefetchS earchResult(true); 738 boolean shouldPreload = !doPreventPreload && mPolicy.shouldPrefetchS earchResult(true);
739 mSearchRequest = new ContextualSearchRequest(searchTerm, alternateTe rm, shouldPreload); 739 mSearchRequest = new ContextualSearchRequest(searchTerm, alternateTe rm, shouldPreload);
740 // Trigger translation, if enabled. 740 // Trigger translation, if enabled.
741 if (!contextLanguage.isEmpty()) { 741 forceTranslateIfNeeded(mSearchRequest, contextLanguage);
742 if (mPolicy.needsTranslation(contextLanguage, getReadableLanguag es())) {
743 boolean doForceTranslate = !mPolicy.disableForceTranslationO nebox();
744 if (doForceTranslate) {
745 mSearchRequest.forceTranslation(contextLanguage,
746 mPolicy.bestTargetLanguage(getWritableLanguages( )));
747 }
748 ContextualSearchUma.logTranslateOnebox(doForceTranslate);
749 }
750 }
751 mDidStartLoadingResolvedSearchRequest = false; 742 mDidStartLoadingResolvedSearchRequest = false;
752 if (mSearchPanel.isContentShowing()) { 743 if (mSearchPanel.isContentShowing()) {
753 mSearchRequest.setNormalPriority(); 744 mSearchRequest.setNormalPriority();
754 } 745 }
755 if (mSearchPanel.isContentShowing() || shouldPreload) { 746 if (mSearchPanel.isContentShowing() || shouldPreload) {
756 loadSearchUrl(); 747 loadSearchUrl();
757 } 748 }
758 mPolicy.logSearchTermResolutionDetails(searchTerm, 749 mPolicy.logSearchTermResolutionDetails(searchTerm,
759 mNetworkCommunicator.getBasePageUrl()); 750 mNetworkCommunicator.getBasePageUrl());
760 } 751 }
(...skipping 29 matching lines...) Expand all
790 */ 781 */
791 private boolean isTapSupported() { 782 private boolean isTapSupported() {
792 // Base page just started navigating away, so taps should be ignored. 783 // Base page just started navigating away, so taps should be ignored.
793 if (mDidBasePageLoadJustStart) return false; 784 if (mDidBasePageLoadJustStart) return false;
794 785
795 return mPolicy.isTapSupported(); 786 return mPolicy.isTapSupported();
796 } 787 }
797 788
798 // ========================================================================= =================== 789 // ========================================================================= ===================
799 // Translation support 790 // Translation support
791 // TODO(donnd): move to a separate file.
800 // ========================================================================= =================== 792 // ========================================================================= ===================
801 793
802 /** 794 /**
803 * Gets the list of readable languages for the current user, with the first 795 * Gets the list of readable languages for the current user, with the first
804 * item in the list being the user's primary language. 796 * item in the list being the user's primary language.
805 * We assume that the user can read all languages that they can write. 797 * We assume that the user can read all languages that they can write.
806 * @return The {@link List} of languages the user understands with their pri mary language first. 798 * @return The {@link List} of languages the user understands with their pri mary language first.
807 */ 799 */
808 private List<String> getReadableLanguages() { 800 private List<String> getReadableLanguages() {
809 // May be cached. 801 // May be cached.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
864 * @return The given locale as a language code. 856 * @return The given locale as a language code.
865 */ 857 */
866 private String trimLocaleToLanguage(String locale) { 858 private String trimLocaleToLanguage(String locale) {
867 // TODO(donnd): use getScript or getLanguageTag (both API 21), or some o ther standard way to 859 // TODO(donnd): use getScript or getLanguageTag (both API 21), or some o ther standard way to
868 // strip the country, instead of hard-coding the two character language code. 860 // strip the country, instead of hard-coding the two character language code.
869 // TODO(donnd): Shouldn't getLanguage() do this? 861 // TODO(donnd): Shouldn't getLanguage() do this?
870 String trimmedLocale = locale.substring(0, 2); 862 String trimmedLocale = locale.substring(0, 2);
871 return new Locale(trimmedLocale).getLanguage(); 863 return new Locale(trimmedLocale).getLanguage();
872 } 864 }
873 865
866 /**
867 * Force translation from the given language for the current search request,
868 * unless disabled by experiment. Also log whenever conditions are right to translate.
869 * @param searchRequest The search request to force translation upon.
870 * @param sourceLanguage The language to translate from, or an empty string if not known.
871 */
872 private void forceTranslateIfNeeded(ContextualSearchRequest searchRequest,
873 String sourceLanguage) {
874 if (!TextUtils.isEmpty(sourceLanguage)) {
875 if (mPolicy.needsTranslation(sourceLanguage, getReadableLanguages()) ) {
876 boolean doForceTranslate = !mPolicy.disableForceTranslationOnebo x();
877 if (doForceTranslate && searchRequest != null) {
878 searchRequest.forceTranslation(sourceLanguage,
879 mPolicy.bestTargetLanguage(getWritableLanguages()));
880 }
881 // Log that conditions were right for translation, even though i t may be disabled
882 // for an experiment so we can compare with the counter factual data.
883 ContextualSearchUma.logTranslateOnebox(doForceTranslate);
884 }
885 }
886 }
887
888 /**
889 * Force auto-detect translation for the current search request unless disab led by experiment.
890 * Also log that conditions are right to translate.
891 * @param searchRequest The search request to force translation upon.
892 */
893 private void forceAutoDetectTranslateUnlessDisabled(ContextualSearchRequest searchRequest) {
894 // Always trigger translation using auto-detect when we're not resolving ,
895 // unless disabled by policy.
896 boolean shouldAutoDetectTranslate = !mPolicy.disableAutoDetectTranslatio nOnebox();
897 if (shouldAutoDetectTranslate && searchRequest != null) {
898 // The translation one-box won't actually show when the source text ends up being
899 // the same as the target text, so we err on over-triggering.
900 searchRequest.forceAutoDetectTranslation(
901 mPolicy.bestTargetLanguage(getWritableLanguages()));
902 }
903 // Log that conditions were right for translation, even though it may be disabled
904 // for an experiment so we can compare with the counter factual data.
905 ContextualSearchUma.logTranslateOnebox(shouldAutoDetectTranslate);
906 }
907
874 // ========================================================================= =================== 908 // ========================================================================= ===================
875 // OverlayContentDelegate 909 // OverlayContentDelegate
876 // ========================================================================= =================== 910 // ========================================================================= ===================
877 911
878 @Override 912 @Override
879 public OverlayContentDelegate getOverlayContentDelegate() { 913 public OverlayContentDelegate getOverlayContentDelegate() {
880 return new SearchOverlayContentDelegate(); 914 return new SearchOverlayContentDelegate();
881 } 915 }
882 916
883 /** 917 /**
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
1293 private native void nativeDestroy(long nativeContextualSearchManager); 1327 private native void nativeDestroy(long nativeContextualSearchManager);
1294 private native void nativeStartSearchTermResolutionRequest(long nativeContex tualSearchManager, 1328 private native void nativeStartSearchTermResolutionRequest(long nativeContex tualSearchManager,
1295 String selection, boolean useResolvedSearchTerm, ContentViewCore bas eContentViewCore, 1329 String selection, boolean useResolvedSearchTerm, ContentViewCore bas eContentViewCore,
1296 boolean maySendBasePageUrl); 1330 boolean maySendBasePageUrl);
1297 private native void nativeGatherSurroundingText(long nativeContextualSearchM anager, 1331 private native void nativeGatherSurroundingText(long nativeContextualSearchM anager,
1298 String selection, boolean useResolvedSearchTerm, ContentViewCore bas eContentViewCore, 1332 String selection, boolean useResolvedSearchTerm, ContentViewCore bas eContentViewCore,
1299 boolean maySendBasePageUrl); 1333 boolean maySendBasePageUrl);
1300 private native String nativeGetTargetLanguage(long nativeContextualSearchMan ager); 1334 private native String nativeGetTargetLanguage(long nativeContextualSearchMan ager);
1301 private native String nativeGetAcceptLanguages(long nativeContextualSearchMa nager); 1335 private native String nativeGetAcceptLanguages(long nativeContextualSearchMa nager);
1302 } 1336 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698