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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/omnibox/LocationBarLayout.java

Issue 2738583002: Add a way to cache and show zero suggest results before native (Closed)
Patch Set: Created 3 years, 9 months 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.omnibox; 5 package org.chromium.chrome.browser.omnibox;
6 6
7 import static org.chromium.chrome.browser.toolbar.ToolbarPhone.URL_FOCUS_CHANGE_ ANIMATION_DURATION_MS; 7 import static org.chromium.chrome.browser.toolbar.ToolbarPhone.URL_FOCUS_CHANGE_ ANIMATION_DURATION_MS;
8 8
9 import android.Manifest; 9 import android.Manifest;
10 import android.animation.Animator; 10 import android.animation.Animator;
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 221
222 // True if the user has just selected a suggestion from the suggestion list. This suppresses 222 // True if the user has just selected a suggestion from the suggestion list. This suppresses
223 // the recording of the dismissal of the suggestion list. (The list is only considered to have 223 // the recording of the dismissal of the suggestion list. (The list is only considered to have
224 // been dismissed if the user didn't choose one of the suggestions shown.) T his signal is used 224 // been dismissed if the user didn't choose one of the suggestions shown.) T his signal is used
225 // instead of a parameter to hideSuggestions because that method is often ca lled from multiple 225 // instead of a parameter to hideSuggestions because that method is often ca lled from multiple
226 // code paths in a not necessarily obvious or even deterministic order. 226 // code paths in a not necessarily obvious or even deterministic order.
227 private boolean mSuggestionSelectionInProgress; 227 private boolean mSuggestionSelectionInProgress;
228 228
229 private Runnable mShowSuggestions; 229 private Runnable mShowSuggestions;
230 230
231 private boolean mShowCachedZeroSuggestResults;
232
233 private DeferredOnSelectionRunnable mDeferredOnSelection;
234
235 private abstract class DeferredOnSelectionRunnable implements Runnable {
236 protected final OmniboxSuggestion mSuggestion;
237 protected final int mPosition;
238 protected boolean mShouldLog;
239
240 public DeferredOnSelectionRunnable(OmniboxSuggestion suggestion, int pos ition) {
241 this.mSuggestion = suggestion;
242 this.mPosition = position;
243 }
244
245 /**
246 * Set whether the selection matches with native results for logging to make sense.
247 * @param log Whether the selection should be logged in native code.
248 */
249 public void setShouldLog(boolean log) {
250 mShouldLog = log;
251 }
252
253 /**
254 * @return Whether the selection should be logged in native code.
255 */
256 public boolean shouldLog() {
257 return mShouldLog;
258 }
259 }
260
231 /** 261 /**
232 * Listener for receiving the messages related with interacting with the omn ibox during startup. 262 * Listener for receiving the messages related with interacting with the omn ibox during startup.
233 */ 263 */
234 public interface OmniboxLivenessListener { 264 public interface OmniboxLivenessListener {
235 /** 265 /**
236 * Called after the first draw when the omnibox can receive touch events . 266 * Called after the first draw when the omnibox can receive touch events .
237 */ 267 */
238 void onOmniboxInteractive(); 268 void onOmniboxInteractive();
239 269
240 /** 270 /**
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 786
757 @Override 787 @Override
758 public void initializeControls(WindowDelegate windowDelegate, 788 public void initializeControls(WindowDelegate windowDelegate,
759 WindowAndroid windowAndroid) { 789 WindowAndroid windowAndroid) {
760 mWindowDelegate = windowDelegate; 790 mWindowDelegate = windowDelegate;
761 mWindowAndroid = windowAndroid; 791 mWindowAndroid = windowAndroid;
762 792
763 // If the user focused the omnibox prior to the native libraries being i nitialized, 793 // If the user focused the omnibox prior to the native libraries being i nitialized,
764 // autocomplete will not always be enabled, so we force it enabled in th at case. 794 // autocomplete will not always be enabled, so we force it enabled in th at case.
765 mUrlBar.setIgnoreTextChangesForAutocomplete(false); 795 mUrlBar.setIgnoreTextChangesForAutocomplete(false);
796 mAutocomplete = new AutocompleteController(this);
766 } 797 }
767 798
768 /** 799 /**
800 * Sets to show cached zero suggest results. This will start both caching ze ro suggest results
801 * in shared preferences and also attempt to show them when appropriate with out needing native
802 * initialization. See {@link LocationBarLayout#showCachedZeroSuggestResults IfAvailable()} for
803 * showing the loaded results before native initialization.
804 * @param showCachedZeroSuggestResults Whether cached zero suggest should be shown.
805 */
806 public void setShowCachedZeroSuggestResults(boolean showCachedZeroSuggestRes ults) {
807 mShowCachedZeroSuggestResults = showCachedZeroSuggestResults;
808 if (mShowCachedZeroSuggestResults) mAutocomplete.setUseCachedZeroSuggest Results();
809 }
810
811 /**
812 * Signals the omnibox to shows the cached zero suggest results if they have been loaded from
813 * cache successfully.
814 */
815 public void showCachedZeroSuggestResultsIfAvailable() {
816 if (!mShowCachedZeroSuggestResults || mSuggestionList == null) return;
817 setSuggestionsListVisibility(true);
818 mSuggestionList.updateLayoutParams();
819 }
820
821 /**
769 * @param focusable Whether the url bar should be focusable. 822 * @param focusable Whether the url bar should be focusable.
770 */ 823 */
771 public void setUrlBarFocusable(boolean focusable) { 824 public void setUrlBarFocusable(boolean focusable) {
772 if (mUrlBar == null) return; 825 if (mUrlBar == null) return;
773 mUrlBar.setFocusable(focusable); 826 mUrlBar.setFocusable(focusable);
774 mUrlBar.setFocusableInTouchMode(focusable); 827 mUrlBar.setFocusableInTouchMode(focusable);
775 } 828 }
776 829
777 /** 830 /**
778 * @return The WindowDelegate for the LocationBar. This should be used for a ll Window related 831 * @return The WindowDelegate for the LocationBar. This should be used for a ll Window related
(...skipping 10 matching lines...) Expand all
789 public void onNativeLibraryReady() { 842 public void onNativeLibraryReady() {
790 mNativeInitialized = true; 843 mNativeInitialized = true;
791 844
792 mSecurityButton.setOnClickListener(this); 845 mSecurityButton.setOnClickListener(this);
793 mNavigationButton.setOnClickListener(this); 846 mNavigationButton.setOnClickListener(this);
794 mVerboseStatusTextView.setOnClickListener(this); 847 mVerboseStatusTextView.setOnClickListener(this);
795 updateMicButtonState(); 848 updateMicButtonState();
796 mDeleteButton.setOnClickListener(this); 849 mDeleteButton.setOnClickListener(this);
797 mMicButton.setOnClickListener(this); 850 mMicButton.setOnClickListener(this);
798 851
799 mAutocomplete = new AutocompleteController(this);
800
801 mOmniboxPrerender = new OmniboxPrerender(); 852 mOmniboxPrerender = new OmniboxPrerender();
802 853
803 for (Runnable deferredRunnable : mDeferredNativeRunnables) { 854 for (Runnable deferredRunnable : mDeferredNativeRunnables) {
804 post(deferredRunnable); 855 post(deferredRunnable);
805 } 856 }
806 mDeferredNativeRunnables.clear(); 857 mDeferredNativeRunnables.clear();
807 858
808 mUrlBar.onOmniboxFullyFunctional(); 859 mUrlBar.onOmniboxFullyFunctional();
809 updateVisualsForState(); 860 updateVisualsForState();
810 } 861 }
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after
1543 mOmniboxResultsContainer.addView(mSuggestionList); 1594 mOmniboxResultsContainer.addView(mSuggestionList);
1544 1595
1545 // Start with visibility GONE to ensure that show() is called. http://cr bug.com/517438 1596 // Start with visibility GONE to ensure that show() is called. http://cr bug.com/517438
1546 mSuggestionList.setVisibility(GONE); 1597 mSuggestionList.setVisibility(GONE);
1547 mSuggestionList.setAdapter(mSuggestionListAdapter); 1598 mSuggestionList.setAdapter(mSuggestionListAdapter);
1548 mSuggestionList.setClipToPadding(false); 1599 mSuggestionList.setClipToPadding(false);
1549 mSuggestionListAdapter.setSuggestionDelegate(new OmniboxSuggestionDelega te() { 1600 mSuggestionListAdapter.setSuggestionDelegate(new OmniboxSuggestionDelega te() {
1550 @Override 1601 @Override
1551 public void onSelection(OmniboxSuggestion suggestion, int position) { 1602 public void onSelection(OmniboxSuggestion suggestion, int position) {
1552 mSuggestionSelectionInProgress = true; 1603 mSuggestionSelectionInProgress = true;
1604 if (mShowCachedZeroSuggestResults && !mNativeInitialized) {
1605 mDeferredOnSelection = new DeferredOnSelectionRunnable(sugge stion, position) {
1606 @Override
1607 public void run() {
1608 onSelection(this.mSuggestion, this.mPosition);
1609 }
1610 };
1611 return;
1612 }
1553 String suggestionMatchUrl = updateSuggestionUrlIfNeeded( 1613 String suggestionMatchUrl = updateSuggestionUrlIfNeeded(
1554 suggestion, position, false); 1614 suggestion, position, false);
1555 loadUrlFromOmniboxMatch(suggestionMatchUrl, suggestion.getTransi tion(), position, 1615 loadUrlFromOmniboxMatch(suggestionMatchUrl, suggestion.getTransi tion(), position,
1556 suggestion.getType()); 1616 suggestion.getType());
1557 hideSuggestions(); 1617 hideSuggestions();
1558 UiUtils.hideKeyboard(mUrlBar); 1618 UiUtils.hideKeyboard(mUrlBar);
1559 } 1619 }
1560 1620
1561 @Override 1621 @Override
1562 public void onRefineSuggestion(OmniboxSuggestion suggestion) { 1622 public void onRefineSuggestion(OmniboxSuggestion suggestion) {
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1715 /** 1775 /**
1716 * Hides the omnibox suggestion popup. 1776 * Hides the omnibox suggestion popup.
1717 * 1777 *
1718 * <p> 1778 * <p>
1719 * Signals the autocomplete controller to stop generating omnibox suggestion s. 1779 * Signals the autocomplete controller to stop generating omnibox suggestion s.
1720 * 1780 *
1721 * @see AutocompleteController#stop(boolean) 1781 * @see AutocompleteController#stop(boolean)
1722 */ 1782 */
1723 @Override 1783 @Override
1724 public void hideSuggestions() { 1784 public void hideSuggestions() {
1725 if (mAutocomplete == null) return; 1785 if (mAutocomplete == null || !mNativeInitialized) return;
1726 1786
1727 if (mShowSuggestions != null) removeCallbacks(mShowSuggestions); 1787 if (mShowSuggestions != null) removeCallbacks(mShowSuggestions);
1728 1788
1729 recordSuggestionsDismissed(); 1789 recordSuggestionsDismissed();
1730 1790
1731 stopAutocomplete(true); 1791 stopAutocomplete(true);
1732 1792
1733 setSuggestionsListVisibility(false); 1793 setSuggestionsListVisibility(false);
1734 clearSuggestions(true); 1794 clearSuggestions(true);
1735 updateNavigationButton(); 1795 updateNavigationButton();
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
1888 } 1948 }
1889 } 1949 }
1890 1950
1891 @Override 1951 @Override
1892 public void onSuggestionsReceived(List<OmniboxSuggestion> newSuggestions, 1952 public void onSuggestionsReceived(List<OmniboxSuggestion> newSuggestions,
1893 String inlineAutocompleteText) { 1953 String inlineAutocompleteText) {
1894 // This is a callback from a listener that is set up by onNativeLibraryR eady, 1954 // This is a callback from a listener that is set up by onNativeLibraryR eady,
1895 // so can only be called once the native side is set up. 1955 // so can only be called once the native side is set up.
1896 assert mNativeInitialized : "Suggestions received before native side int ialialized"; 1956 assert mNativeInitialized : "Suggestions received before native side int ialialized";
1897 1957
1898 if (getCurrentTab() == null) { 1958 if (mDeferredOnSelection != null) {
1899 // If the current tab is not available, drop the suggestions and hid e the autocomplete. 1959 mDeferredOnSelection.setShouldLog(newSuggestions.size() > mDeferredO nSelection.mPosition
1900 hideSuggestions(); 1960 && mDeferredOnSelection.mSuggestion.equals(
1901 return; 1961 newSuggestions.get(mDeferredOnSelection.mPosition))) ;
1962 mDeferredOnSelection.run();
1963 mDeferredOnSelection = null;
1902 } 1964 }
1903
1904 String userText = mUrlBar.getTextWithoutAutocomplete(); 1965 String userText = mUrlBar.getTextWithoutAutocomplete();
1905 mUrlTextAfterSuggestionsReceived = userText + inlineAutocompleteText; 1966 mUrlTextAfterSuggestionsReceived = userText + inlineAutocompleteText;
1906 1967
1907 boolean itemsChanged = false; 1968 boolean itemsChanged = false;
1908 boolean itemCountChanged = false; 1969 boolean itemCountChanged = false;
1909 // If the length of the incoming suggestions matches that of those curre ntly being shown, 1970 // If the length of the incoming suggestions matches that of those curre ntly being shown,
1910 // replace them inline to allow transient entries to retain their proper highlighting. 1971 // replace them inline to allow transient entries to retain their proper highlighting.
1911 if (mSuggestionItems.size() == newSuggestions.size()) { 1972 if (mSuggestionItems.size() == newSuggestions.size()) {
1912 for (int index = 0; index < newSuggestions.size(); index++) { 1973 for (int index = 0; index < newSuggestions.size(); index++) {
1913 OmniboxResultItem suggestionItem = mSuggestionItems.get(index); 1974 OmniboxResultItem suggestionItem = mSuggestionItems.get(index);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1972 if (!isUrlFocusChangeInProgress()) { 2033 if (!isUrlFocusChangeInProgress()) {
1973 mShowSuggestions.run(); 2034 mShowSuggestions.run();
1974 } else { 2035 } else {
1975 postDelayed(mShowSuggestions, ToolbarPhone.URL_FOCUS_CHANGE_ANIM ATION_DURATION_MS); 2036 postDelayed(mShowSuggestions, ToolbarPhone.URL_FOCUS_CHANGE_ANIM ATION_DURATION_MS);
1976 } 2037 }
1977 } 2038 }
1978 2039
1979 // Update the navigation button to show the default suggestion's icon. 2040 // Update the navigation button to show the default suggestion's icon.
1980 updateNavigationButton(); 2041 updateNavigationButton();
1981 2042
1982 if (!CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_INSTANT) 2043 if (mNativeInitialized
2044 && !CommandLine.getInstance().hasSwitch(ChromeSwitches.DISABLE_I NSTANT)
1983 && PrivacyPreferencesManager.getInstance().shouldPrerender()) { 2045 && PrivacyPreferencesManager.getInstance().shouldPrerender()) {
1984 mOmniboxPrerender.prerenderMaybe( 2046 mOmniboxPrerender.prerenderMaybe(
1985 userText, 2047 userText,
1986 getOriginalUrl(), 2048 getOriginalUrl(),
1987 mAutocomplete.getCurrentNativeAutocompleteResult(), 2049 mAutocomplete.getCurrentNativeAutocompleteResult(),
1988 getCurrentTab().getProfile(), 2050 getCurrentTab().getProfile(),
1989 getCurrentTab()); 2051 getCurrentTab());
1990 } 2052 }
1991 } 2053 }
1992 2054
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
2119 2181
2120 private void loadUrlFromOmniboxMatch(String url, int transition, int matchPo sition, int type) { 2182 private void loadUrlFromOmniboxMatch(String url, int transition, int matchPo sition, int type) {
2121 // loadUrl modifies AutocompleteController's state clearing the native 2183 // loadUrl modifies AutocompleteController's state clearing the native
2122 // AutocompleteResults needed by onSuggestionsSelected. Therefore, 2184 // AutocompleteResults needed by onSuggestionsSelected. Therefore,
2123 // loadUrl should should be invoked last. 2185 // loadUrl should should be invoked last.
2124 Tab currentTab = getCurrentTab(); 2186 Tab currentTab = getCurrentTab();
2125 String currentPageUrl = getCurrentTabUrl(); 2187 String currentPageUrl = getCurrentTabUrl();
2126 WebContents webContents = currentTab != null ? currentTab.getWebContents () : null; 2188 WebContents webContents = currentTab != null ? currentTab.getWebContents () : null;
2127 long elapsedTimeSinceModified = mNewOmniboxEditSessionTimestamp > 0 2189 long elapsedTimeSinceModified = mNewOmniboxEditSessionTimestamp > 0
2128 ? (SystemClock.elapsedRealtime() - mNewOmniboxEditSessionTimesta mp) : -1; 2190 ? (SystemClock.elapsedRealtime() - mNewOmniboxEditSessionTimesta mp) : -1;
2129 mAutocomplete.onSuggestionSelected(matchPosition, type, currentPageUrl, 2191 boolean shouldSkipNativeLog = mShowCachedZeroSuggestResults
2130 mUrlFocusedFromFakebox, elapsedTimeSinceModified, mUrlBar.getAut ocompleteLength(), 2192 && (mDeferredOnSelection != null)
2131 webContents); 2193 && !mDeferredOnSelection.shouldLog();
2194 if (!shouldSkipNativeLog) {
2195 mAutocomplete.onSuggestionSelected(matchPosition, type, currentPageU rl,
2196 mUrlFocusedFromFakebox, elapsedTimeSinceModified,
2197 mUrlBar.getAutocompleteLength(),
2198 webContents);
2199 }
2132 loadUrl(url, transition); 2200 loadUrl(url, transition);
2133 } 2201 }
2134 2202
2135 /** 2203 /**
2136 * Load the url given with the given transition. Exposed for child classes t o overwrite as 2204 * Load the url given with the given transition. Exposed for child classes t o overwrite as
2137 * necessary. 2205 * necessary.
2138 */ 2206 */
2139 protected void loadUrl(String url, int transition) { 2207 protected void loadUrl(String url, int transition) {
2140 Tab currentTab = getCurrentTab(); 2208 Tab currentTab = getCurrentTab();
2141 2209
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
2463 public void setTitleToPageTitle() { } 2531 public void setTitleToPageTitle() { }
2464 2532
2465 @Override 2533 @Override
2466 public void setShowTitle(boolean showTitle) { } 2534 public void setShowTitle(boolean showTitle) { }
2467 2535
2468 @Override 2536 @Override
2469 public boolean mustQueryUrlBarLocationForSuggestions() { 2537 public boolean mustQueryUrlBarLocationForSuggestions() {
2470 return DeviceFormFactor.isTablet(getContext()); 2538 return DeviceFormFactor.isTablet(getContext());
2471 } 2539 }
2472 } 2540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698