Bug 1158275 - Remove old method of selecting non-primary search engines in BrowserSearch. r=mcomella
authorSebastian Kaspari <s.kaspari@gmail.com>
Tue, 02 Jun 2015 15:43:28 -0700
changeset 248022 8e5abcb55c356a3a4e34205453ff3e9d66f79c2d
parent 248021 853fae656e759f4cf7055338be43a1f54d5c1bdb
child 248023 d7ed7208e7f432758f5d3cff1b158417744fcfaa
push id60888
push userkwierso@gmail.com
push dateThu, 11 Jun 2015 01:38:38 +0000
treeherdermozilla-inbound@39e638ed06bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1158275
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1158275 - Remove old method of selecting non-primary search engines in BrowserSearch. r=mcomella
mobile/android/base/home/BrowserSearch.java
--- a/mobile/android/base/home/BrowserSearch.java
+++ b/mobile/android/base/home/BrowserSearch.java
@@ -289,17 +289,17 @@ public class BrowserSearch extends HomeF
                 // Perform the user-entered search if the user clicks on a search engine row.
                 // This row will be disabled if suggestions (in addition to the user-entered term) are showing.
                 if (view instanceof SearchEngineRow) {
                     ((SearchEngineRow) view).performUserEnteredSearch();
                     return;
                 }
 
                 // Account for the search engine rows.
-                position -= getSuggestEngineCount();
+                position -= getPrimaryEngineCount();
                 final Cursor c = mAdapter.getCursor(position);
                 final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
 
                 // The "urlbar" and "frecency" sessions can be open at the same time. Use the LIST_ITEM
                 // method to set this LOAD_URL event apart from the case where the user commits what's in
                 // the url bar.
                 Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.LIST_ITEM);
 
@@ -312,17 +312,17 @@ public class BrowserSearch extends HomeF
             @Override
             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                 // Don't do anything when the user long-clicks on a search engine row.
                 if (view instanceof SearchEngineRow) {
                     return true;
                 }
 
                 // Account for the search engine rows.
-                position -= getSuggestEngineCount();
+                position -= getPrimaryEngineCount();
                 return mList.onItemLongClick(parent, view, position, id);
             }
         });
 
         final ListSelectionListener listener = new ListSelectionListener();
         mList.setOnItemSelectedListener(listener);
         mList.setOnFocusChangeListener(listener);
 
@@ -340,17 +340,19 @@ public class BrowserSearch extends HomeF
 
         registerForContextMenu(mList);
         EventDispatcher.getInstance().registerGeckoThreadListener(this,
             "SearchEngines:Data");
 
         // If the view backed by this Fragment is being recreated, we will not receive
         // a new search engine data event so refresh the new search engine bar's data
         // & Views with the data we have.
-        mSearchEngineBar.setSearchEngines(mSearchEngines);
+        mSearchEngineBar.setSearchEngines(
+                mSearchEngines.subList(getPrimaryEngineCount(), mSearchEngines.size())
+        );
         mSearchEngineBar.setOnSearchBarClickListener(this);
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
         // Initialize the search adapter
@@ -579,17 +581,19 @@ public class BrowserSearch extends HomeF
 
             mSearchEngines = Collections.unmodifiableList(searchEngines);
             mLastLocale = Locale.getDefault();
 
             if (mAdapter != null) {
                 mAdapter.notifyDataSetChanged();
             }
 
-            mSearchEngineBar.setSearchEngines(mSearchEngines);
+            mSearchEngineBar.setSearchEngines(
+                    mSearchEngines.subList(getPrimaryEngineCount(), mSearchEngines.size())
+            );
 
             // Show suggestions opt-in prompt only if suggestions are not enabled yet,
             // user hasn't been prompted and we're not on a private browsing tab.
             if (!mSuggestionsEnabled && !suggestionsPrompted && mSuggestClient != null) {
                 showSuggestionsOptIn();
             }
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error getting search engine JSON", e);
@@ -728,18 +732,18 @@ public class BrowserSearch extends HomeF
             }
         });
 
         prompt.startAnimation(slideAnimation);
         mSuggestionsOptInPrompt.startAnimation(shrinkAnimation);
         mList.startAnimation(shrinkAnimation);
     }
 
-    private int getSuggestEngineCount() {
-        return (TextUtils.isEmpty(mSearchTerm) || mSuggestClient == null || !mSuggestionsEnabled) ? 0 : 1;
+    private int getPrimaryEngineCount() {
+        return mSearchEngines.size() > 0 ? 1 : 0;
     }
 
     private void restartSearchLoader() {
         SearchLoader.restart(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm);
     }
 
     private void initSearchLoader() {
         SearchLoader.init(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm);
@@ -834,111 +838,92 @@ public class BrowserSearch extends HomeF
                                              ROW_SUGGEST },
                                  new int[] { R.layout.home_item_row,
                                              R.layout.home_search_item_row,
                                              R.layout.home_search_item_row });
         }
 
         @Override
         public int getItemViewType(int position) {
-            final int engine = getEngineIndex(position);
+            if (position < getPrimaryEngineCount()) {
+                if (mSuggestionsEnabled && mSearchEngines.get(position).hasSuggestions()) {
+                    // Give suggestion views their own type to prevent them from
+                    // sharing other recycled search result views. Using other
+                    // recycled views for the suggestion row can break animations
+                    // (bug 815937).
 
-            if (engine == -1) {
-                return ROW_STANDARD;
+                    return ROW_SUGGEST;
+                } else {
+                    return ROW_SEARCH;
+                }
             }
 
-            if (engine == 0 && mSuggestionsEnabled) {
-                // Give suggestion views their own type to prevent them from
-                // sharing other recycled search engine views. Using other
-                // recycled views for the suggestion row can break animations
-                // (bug 815937).
-                return ROW_SUGGEST;
-            }
-
-            return ROW_SEARCH;
+            return ROW_STANDARD;
         }
 
         @Override
         public boolean isEnabled(int position) {
             // If we're using a gamepad or keyboard, allow the row to be
             // focused so it can pass the focus to its child suggestion views.
             if (!mList.isInTouchMode()) {
                 return true;
             }
 
             // If the suggestion row only contains one item (the user-entered
             // query), allow the entire row to be clickable; clicking the row
             // has the same effect as clicking the single suggestion. If the
             // row contains multiple items, clicking the row will do nothing.
-            final int index = getEngineIndex(position);
-            if (index != -1) {
-                return !mSearchEngines.get(index).hasSuggestions();
+
+            if (position < getPrimaryEngineCount()) {
+                return !mSearchEngines.get(position).hasSuggestions();
             }
 
             return true;
         }
 
         // Add the search engines to the number of reported results.
         @Override
         public int getCount() {
             final int resultCount = super.getCount();
 
             // Don't show search engines or suggestions if search field is empty
             if (TextUtils.isEmpty(mSearchTerm)) {
                 return resultCount;
             }
 
-            return resultCount + mSearchEngines.size();
+            return resultCount + getPrimaryEngineCount();
         }
 
         @Override
         public void bindView(View view, Context context, int position) {
             final int type = getItemViewType(position);
 
             if (type == ROW_SEARCH || type == ROW_SUGGEST) {
                 final SearchEngineRow row = (SearchEngineRow) view;
                 row.setOnUrlOpenListener(mUrlOpenListener);
                 row.setOnSearchListener(mSearchListener);
                 row.setOnEditSuggestionListener(mEditSuggestionListener);
                 row.setSearchTerm(mSearchTerm);
 
-                final SearchEngine engine = mSearchEngines.get(getEngineIndex(position));
+                final SearchEngine engine = mSearchEngines.get(position);
                 final boolean animate = (mAnimateSuggestions && engine.hasSuggestions());
                 row.updateFromSearchEngine(engine, animate);
                 if (animate) {
                     // Only animate suggestions the first time they are shown
                     mAnimateSuggestions = false;
                 }
             } else {
                 // Account for the search engines
-                position -= getSuggestEngineCount();
+                position -= getPrimaryEngineCount();
 
                 final Cursor c = getCursor(position);
                 final TwoLinePageRow row = (TwoLinePageRow) view;
                 row.updateFromCursor(c);
             }
         }
-
-        private int getEngineIndex(int position) {
-            final int resultCount = super.getCount();
-            final int suggestEngineCount = getSuggestEngineCount();
-
-            // Return suggest engine index
-            if (position < suggestEngineCount) {
-                return position;
-            }
-
-            // Not an engine
-            if (position - suggestEngineCount < resultCount) {
-                return -1;
-            }
-
-            // Return search engine index
-            return position - resultCount;
-        }
     }
 
     private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             return SearchLoader.createInstance(getActivity(), args);
         }