From 98127aeb6e8f1d0016dee1230b7ff45bb4827fdf Mon Sep 17 00:00:00 2001 From: Allison Naaktgeboren Date: Tue, 22 Sep 2015 13:39:48 -0700 Subject: [PATCH] Bug 1200367 - saved searches should have an opt-out pref in settings.r=liuche --- mobile/android/base/home/SearchEngineRow.java | 99 ++++++++++++++----- .../base/locales/en-US/android_strings.dtd | 1 + .../base/preferences/GeckoPreferences.java | 8 ++ .../resources/xml-v11/preferences_search.xml | 5 + .../base/resources/xml/preferences_search.xml | 5 + mobile/android/base/strings.xml.in | 1 + 6 files changed, 95 insertions(+), 24 deletions(-) diff --git a/mobile/android/base/home/SearchEngineRow.java b/mobile/android/base/home/SearchEngineRow.java index 994c031aee05..15a9141a3422 100644 --- a/mobile/android/base/home/SearchEngineRow.java +++ b/mobile/android/base/home/SearchEngineRow.java @@ -7,12 +7,14 @@ package org.mozilla.gecko.home; import org.mozilla.gecko.AppConstants; import org.mozilla.gecko.db.BrowserContract.SearchHistory; +import org.mozilla.gecko.GeckoSharedPrefs; import org.mozilla.gecko.R; import org.mozilla.gecko.Telemetry; import org.mozilla.gecko.TelemetryContract; import org.mozilla.gecko.home.BrowserSearch.OnEditSuggestionListener; import org.mozilla.gecko.home.BrowserSearch.OnSearchListener; import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; +import org.mozilla.gecko.preferences.GeckoPreferences; import org.mozilla.gecko.util.StringUtils; import org.mozilla.gecko.util.HardwareUtils; import org.mozilla.gecko.widget.AnimatedHeightLayout; @@ -22,6 +24,7 @@ import org.mozilla.gecko.widget.FlowLayout; import android.database.Cursor; import android.content.ContentResolver; import android.content.Context; +import android.content.SharedPreferences; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -218,6 +221,14 @@ class SearchEngineRow extends AnimatedHeightLayout { } } + /** + * Displays search suggestions from previous searches. + * + * @param c The Cursor to iterate over for saved search suggestion to display + * @param suggestionCounter global index of where to start adding suggestion "buttons" in the search engine row + * @param animate whether or not to animate suggestions for visual polish + * @param recycledSuggestionCount How many suggestion "button" views we could recycle from previous calls + */ private void updateFromSavedSearches(Cursor c, boolean animate, int suggestionCounter, int recycledSuggestionCount) { if (c == null) { return; @@ -241,7 +252,13 @@ class SearchEngineRow extends AnimatedHeightLayout { hideRecycledSuggestions(suggestionCounter, recycledSuggestionCount); } - private Cursor getSavedSearches(String searchTerm, boolean isTablet) { + /** + * Gets matching suggestions from search history. + * + * @param searchTerm the string with which to look for matches in the saved searches + * @return matching prior searches that contain searchTerm + */ + private Cursor getSavedSearches(String searchTerm) { if (!AppConstants.NIGHTLY_BUILD) { return null; } @@ -250,20 +267,28 @@ class SearchEngineRow extends AnimatedHeightLayout { String[] columns = new String[] { SearchHistory.QUERY }; String actualQuery = SearchHistory.QUERY + " LIKE ?"; String[] queryArgs = new String[] { '%' + searchTerm + '%' }; - final int limit = isTablet ? TABLET_MAX : PHONE_MAX; + final int limit = HardwareUtils.isTablet() ? TABLET_MAX : PHONE_MAX; String sortOrderAndLimit = SearchHistory.DATE +" DESC LIMIT "+limit; return cr.query(SearchHistory.CONTENT_URI, columns, actualQuery, queryArgs, sortOrderAndLimit); } - private int updateFromSearchEngine(boolean animate, int recycledSuggestionCount, boolean isTablet, int savedCount) { + /** + * Displays suggestions supplied by the search engine, relative to number of suggestions from search history. + * + * @param animate whether or not to animate suggestions for visual polish + * @param recycledSuggestionCount How many suggestion "button" views we could recycle from previous calls + * @param savedCount how many saved searches this searchTerm has + * @return the global count of how many suggestions have been bound/shown in the search engine row + */ + private int updateFromSearchEngine(boolean animate, int recycledSuggestionCount, int savedCount) { // Remove this default limit value in Bug 1201325 int limit = TABLET_MAX; if (AppConstants.NIGHTLY_BUILD) { - limit = isTablet ? TABLET_MAX : PHONE_MAX; + limit = HardwareUtils.isTablet() ? TABLET_MAX : PHONE_MAX; // If there are less than max saved searches on phones, fill the space with more search engine suggestions - if (!isTablet && savedCount < PHONE_MAX) { + if (!HardwareUtils.isTablet() && savedCount < PHONE_MAX) { limit += PHONE_MAX - savedCount; } } @@ -288,32 +313,58 @@ class SearchEngineRow extends AnimatedHeightLayout { return suggestionCounter; } - public void updateSuggestions(boolean suggestionsEnabled, SearchEngine searchEngine, String searchTerm, boolean animate) { - // Update search engine reference. Even if the user has not seen the prompt, we need to set the engine for the mSearchTerm suggestion + /** + * Updates the whole suggestions UI, the search engine UI, suggestions from the default search engine, + * and suggestions from search history. + * + * This can be called before the opt-in permission prompt is shown or set. + * Even if both suggestion types are disabled, we need to update the search engine, its image, and the content description. + * + * @param searchSuggestionsEnabled whether or not suggestions from the default search engine are enabled + * @param searchEngine the search engine to use throughout the SearchEngineRow class + * @param searchTerm the text from the url to get suggestions on + * @param animate whether or not to use animations + **/ + public void updateSuggestions(boolean searchSuggestionsEnabled, SearchEngine searchEngine, String searchTerm, boolean animate) { mSearchEngine = searchEngine; // Set the search engine icon (e.g., Google) for the row. mIconView.updateAndScaleImage(mSearchEngine.getIcon(), mSearchEngine.getEngineIdentifier()); // Set the initial content description. setDescriptionOnSuggestion(mUserEnteredTextView, mUserEnteredTextView.getText().toString()); - // This can be called before the opt-in permission prompt is shown or set. Check first. - if (suggestionsEnabled) { - final int recycledSuggestionCount = mSuggestionView.getChildCount(); - if (AppConstants.NIGHTLY_BUILD) { - final boolean isTablet = HardwareUtils.isTablet(); - final Cursor c = getSavedSearches(searchTerm, isTablet); - try { - final int savedSearchCount = (c != null) ? c.getCount(): 0; - final int suggestionViewCount = updateFromSearchEngine(animate, recycledSuggestionCount, isTablet, savedSearchCount); - updateFromSavedSearches(c, animate, suggestionViewCount, recycledSuggestionCount); - } finally { - if (c != null) { - c.close(); - } - } - } else { - updateFromSearchEngine(animate, recycledSuggestionCount, true, 0); + if (!AppConstants.NIGHTLY_BUILD) { + if (searchSuggestionsEnabled) { + updateFromSearchEngine(animate, mSuggestionView.getChildCount(), 0); } + return; + } + + final int recycledSuggestionCount = mSuggestionView.getChildCount(); + final SharedPreferences prefs = GeckoSharedPrefs.forApp(getContext()); + final boolean savedSearchesEnabled = prefs.getBoolean(GeckoPreferences.PREFS_HISTORY_SAVED_SEARCH, true); + + if (searchSuggestionsEnabled && savedSearchesEnabled) { + final Cursor c = getSavedSearches(searchTerm); + try { + final int savedSearchCount = (c != null) ? c.getCount() : 0; + final int suggestionViewCount = updateFromSearchEngine(animate, recycledSuggestionCount, savedSearchCount); + updateFromSavedSearches(c, animate, suggestionViewCount, recycledSuggestionCount); + } finally { + if (c != null) { + c.close(); + } + } + } else if (savedSearchesEnabled) { + final Cursor c = getSavedSearches(searchTerm); + try { + updateFromSavedSearches(c, animate, 0, recycledSuggestionCount); + } finally { + if (c != null) { + c.close(); + } + } + } else if (searchSuggestionsEnabled) { + updateFromSearchEngine(animate, recycledSuggestionCount, 0); } } diff --git a/mobile/android/base/locales/en-US/android_strings.dtd b/mobile/android/base/locales/en-US/android_strings.dtd index f088141c5883..976007fd4eb3 100644 --- a/mobile/android/base/locales/en-US/android_strings.dtd +++ b/mobile/android/base/locales/en-US/android_strings.dtd @@ -290,6 +290,7 @@ size. --> + diff --git a/mobile/android/base/preferences/GeckoPreferences.java b/mobile/android/base/preferences/GeckoPreferences.java index 205bd7799a35..a71f9575305c 100644 --- a/mobile/android/base/preferences/GeckoPreferences.java +++ b/mobile/android/base/preferences/GeckoPreferences.java @@ -137,6 +137,7 @@ OnSharedPreferenceChangeListener private static final String PREFS_TRACKING_PROTECTION_LEARN_MORE = NON_PREF_PREFIX + "trackingprotection.learn_more"; private static final String PREFS_CATEGORY_PRIVATE_DATA = NON_PREF_PREFIX + "category_private_data"; public static final String PREFS_HOMEPAGE = NON_PREF_PREFIX + "homepage"; + public static final String PREFS_HISTORY_SAVED_SEARCH = NON_PREF_PREFIX + "search.search_history.enabled"; private static final String ACTION_STUMBLER_UPLOAD_PREF = AppConstants.ANDROID_PACKAGE_NAME + ".STUMBLER_PREF"; @@ -755,6 +756,13 @@ OnSharedPreferenceChangeListener i--; continue; } + } else if (PREFS_HISTORY_SAVED_SEARCH.equals(key)) { + // Remove settings UI if not on Nightly + if (!AppConstants.NIGHTLY_BUILD) { + preferences.removePreference(pref); + i--; + continue; + } } else if (PREFS_TELEMETRY_ENABLED.equals(key)) { if (!AppConstants.MOZ_TELEMETRY_REPORTING) { preferences.removePreference(pref); diff --git a/mobile/android/base/resources/xml-v11/preferences_search.xml b/mobile/android/base/resources/xml-v11/preferences_search.xml index ce8a61258598..5b986e730680 100644 --- a/mobile/android/base/resources/xml-v11/preferences_search.xml +++ b/mobile/android/base/resources/xml-v11/preferences_search.xml @@ -13,6 +13,11 @@ android:defaultValue="false" android:persistent="false" /> + + diff --git a/mobile/android/base/resources/xml/preferences_search.xml b/mobile/android/base/resources/xml/preferences_search.xml index 3f2ecfbc3019..819237f16248 100644 --- a/mobile/android/base/resources/xml/preferences_search.xml +++ b/mobile/android/base/resources/xml/preferences_search.xml @@ -13,6 +13,11 @@ android:defaultValue="false" android:persistent="false" /> + + diff --git a/mobile/android/base/strings.xml.in b/mobile/android/base/strings.xml.in index 432e945404c7..79bbc5f6748b 100644 --- a/mobile/android/base/strings.xml.in +++ b/mobile/android/base/strings.xml.in @@ -248,6 +248,7 @@ &pref_sync; &pref_sync_summary2; &pref_search_suggestions; + &pref_history_search_suggestions; &pref_private_data_history2; &pref_private_data_searchHistory; &pref_private_data_formdata2;