Bug 1200319 - How many, which ones of which type of pills should be displayed on phones or tablets for search suggestions.r=mcomella

This commit is contained in:
Allison Naaktgeboren 2015-09-15 18:58:52 -07:00
parent 4fff2cbe40
commit 71e89e6c6e

View File

@ -14,6 +14,7 @@ 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.util.StringUtils;
import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.widget.AnimatedHeightLayout;
import org.mozilla.gecko.widget.FaviconView;
import org.mozilla.gecko.widget.FlowLayout;
@ -64,6 +65,10 @@ class SearchEngineRow extends AnimatedHeightLayout {
// Selected suggestion view
private int mSelectedView;
// Maximums for suggestions based on form factor
private static final int TABLET_MAX = 4;
private static final int PHONE_MAX = 2;
public SearchEngineRow(Context context) {
this(context, null);
}
@ -215,16 +220,7 @@ class SearchEngineRow extends AnimatedHeightLayout {
}
}
private void updateFromSavedSearches(String searchTerm, boolean animate, int suggestionCounter, int recycledSuggestionCount) {
final ContentResolver cr = getContext().getContentResolver();
String[] columns = new String[] { SearchHistory.QUERY };
String actualQuery = SearchHistory.QUERY + " LIKE ?";
String[] queryArgs = new String[] { '%' + searchTerm + '%' };
String sortOrderAndLimit = SearchHistory.DATE + " DESC LIMIT 4";
final Cursor c = cr.query(SearchHistory.CONTENT_URI, columns, actualQuery, queryArgs, sortOrderAndLimit);
private void updateFromSavedSearches(Cursor c, boolean animate, int suggestionCounter, int recycledSuggestionCount) {
if (c == null) {
return;
}
@ -243,10 +239,38 @@ class SearchEngineRow extends AnimatedHeightLayout {
hideRecycledSuggestions(suggestionCounter, recycledSuggestionCount);
}
private int updateFromSearchEngine(boolean animate, int recycledSuggestionCount) {
private Cursor getSavedSearches(String searchTerm, boolean isTablet) {
if (!AppConstants.NIGHTLY_BUILD) {
return null;
}
final ContentResolver cr = getContext().getContentResolver();
String[] columns = new String[] { SearchHistory.QUERY };
String actualQuery = SearchHistory.QUERY + " LIKE ?";
String[] queryArgs = new String[] { '%' + searchTerm + '%' };
final int limit = 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) {
// Remove this default limit value in Bug 1201325
int limit = TABLET_MAX;
if (AppConstants.NIGHTLY_BUILD) {
limit = 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) {
limit += PHONE_MAX - savedCount;
}
}
int suggestionCounter = 0;
// Apply Search Engine's suggestions
for (String suggestion : mSearchEngine.getSuggestions()) {
if (suggestionCounter == limit) {
break;
}
bindSuggestionView(suggestion, animate, recycledSuggestionCount, suggestionCounter, false);
++suggestionCounter;
}
@ -271,9 +295,21 @@ class SearchEngineRow extends AnimatedHeightLayout {
// This can be called before the opt-in permission prompt is shown or set. Check first.
if (suggestionsEnabled) {
final int recycledSuggestionCount = mSuggestionView.getChildCount();
final int suggestionViewCount = updateFromSearchEngine(animate, recycledSuggestionCount);
if (AppConstants.NIGHTLY_BUILD) {
updateFromSavedSearches(searchTerm, animate, suggestionViewCount, recycledSuggestionCount);
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);
}
}
}