Bug 898501: Re-use FaviconsLoader from HomeFragment in BrowserSearch. [r=lucasr]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 29 Jul 2013 15:05:23 -0700
changeset 151464 4fa359c591e579c98b7abd3d955aa781eba60fbc
parent 151463 f2d68c93e066df60706cffd8a09fe8f941accf3c
child 151465 a9f2a85bf1ebc1fb86bd575150cf158a10c0566a
push idunknown
push userunknown
push dateunknown
reviewerslucasr
bugs898501
milestone25.0a1
Bug 898501: Re-use FaviconsLoader from HomeFragment in BrowserSearch. [r=lucasr]
mobile/android/base/home/BrowserSearch.java
--- a/mobile/android/base/home/BrowserSearch.java
+++ b/mobile/android/base/home/BrowserSearch.java
@@ -57,23 +57,20 @@ import java.util.List;
  * Fragment that displays frecency search results in a ListView.
  */
 public class BrowserSearch extends HomeFragment
                            implements GeckoEventListener {
     // Logging tag name
     private static final String LOGTAG = "GeckoBrowserSearch";
 
     // Cursor loader ID for search query
-    private static final int SEARCH_LOADER_ID = 0;
-
-    // Cursor loader ID for favicons query
-    private static final int FAVICONS_LOADER_ID = 1;
+    private static final int LOADER_ID_SEARCH = 0;
 
     // AsyncTask loader ID for suggestion query
-    private static final int SUGGESTION_LOADER_ID = 2;
+    private static final int LOADER_ID_SUGGESTION = 1;
 
     // Timeout for the suggestion client to respond
     private static final int SUGGESTION_TIMEOUT = 3000;
 
     // Maximum number of results returned by the suggestion client
     private static final int SUGGESTION_MAX = 3;
 
     // The maximum number of rows deep in a search we'll dig
@@ -286,17 +283,17 @@ public class BrowserSearch extends HomeF
                     setSearchEngines(message);
                 }
             });
         }
     }
 
     @Override
     protected void load() {
-        getLoaderManager().initLoader(SEARCH_LOADER_ID, null, mCursorLoaderCallbacks);
+        getLoaderManager().initLoader(LOADER_ID_SEARCH, null, mCursorLoaderCallbacks);
     }
 
     private void handleAutocomplete(String searchTerm, Cursor c) {
         if (TextUtils.isEmpty(mSearchTerm) || c == null || mAutocompleteHandler == null) {
             return;
         }
 
         // Avoid searching the path if we don't have to. Currently just
@@ -354,17 +351,17 @@ public class BrowserSearch extends HomeF
         if (mSuggestClient == null || !mSuggestionsEnabled) {
             return;
         }
 
         if (mSuggestionLoaderCallbacks == null) {
             mSuggestionLoaderCallbacks = new SuggestionLoaderCallbacks();
         }
 
-        getLoaderManager().restartLoader(SUGGESTION_LOADER_ID, null, mSuggestionLoaderCallbacks);
+        getLoaderManager().restartLoader(LOADER_ID_SUGGESTION, null, mSuggestionLoaderCallbacks);
     }
 
     private void setSuggestions(ArrayList<String> suggestions) {
         mSearchEngines.get(0).suggestions = suggestions;
         mAdapter.notifyDataSetChanged();
     }
 
     private void setSearchEngines(JSONObject data) {
@@ -562,17 +559,17 @@ public class BrowserSearch extends HomeF
         mAutocompleteHandler = handler;
 
         if (isVisible()) {
             // The adapter depends on the search term to determine its number
             // of items. Make it we notify the view about it.
             mAdapter.notifyDataSetChanged();
 
             // Restart loaders with the new search term
-            SearchLoader.restart(getLoaderManager(), SEARCH_LOADER_ID, mCursorLoaderCallbacks, mSearchTerm, false);
+            SearchLoader.restart(getLoaderManager(), LOADER_ID_SEARCH, mCursorLoaderCallbacks, mSearchTerm, false);
             filterSuggestions();
         }
     }
 
     private static class SuggestionAsyncLoader extends AsyncTaskLoader<ArrayList<String>> {
         private final SuggestClient mSuggestClient;
         private final String mSearchTerm;
         private ArrayList<String> mSuggestions;
@@ -757,67 +754,55 @@ public class BrowserSearch extends HomeF
                 return -1;
             }
 
             // Return search engine index
             return position - resultCount;
         }
     }
 
-    private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
+    private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
-            switch(id) {
-            case SEARCH_LOADER_ID:
+            if (id == LOADER_ID_SEARCH) {
                 return SearchLoader.createInstance(getActivity(), args);
-
-            case FAVICONS_LOADER_ID:
-                return FaviconsLoader.createInstance(getActivity(), args);
+            } else {
+                return super.onCreateLoader(id, args);
             }
-
-            return null;
         }
 
         @Override
         public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
-            final int loaderId = loader.getId();
-            switch(loaderId) {
-            case SEARCH_LOADER_ID:
+            if (loader.getId() == LOADER_ID_SEARCH) {
                 mAdapter.swapCursor(c);
 
                 // We should handle autocompletion based on the search term
                 // associated with the currently loader that has just provided
                 // the results.
                 SearchCursorLoader searchLoader = (SearchCursorLoader) loader;
                 handleAutocomplete(searchLoader.getSearchTerm(), c);
 
-                FaviconsLoader.restartFromCursor(getLoaderManager(), FAVICONS_LOADER_ID,
-                        mCursorLoaderCallbacks, c);
-                break;
-
-            case FAVICONS_LOADER_ID:
-                // Causes the listview to recreate its children and use the
-                // now in-memory favicons.
-                mAdapter.notifyDataSetChanged();
-                break;
+                loadFavicons(c);
+            } else {
+                super.onLoadFinished(loader, c);
             }
         }
 
         @Override
         public void onLoaderReset(Loader<Cursor> loader) {
-            final int loaderId = loader.getId();
-            switch(loaderId) {
-            case SEARCH_LOADER_ID:
+            if (loader.getId() == LOADER_ID_SEARCH) {
                 mAdapter.swapCursor(null);
-                break;
+            } else {
+                super.onLoaderReset(loader);
+            }
+        }
 
-            case FAVICONS_LOADER_ID:
-                // Do nothing
-                break;
-            }
+        @Override
+        public void onFaviconsLoaded() {
+            mAdapter.notifyDataSetChanged();
         }
     }
 
     private class SuggestionLoaderCallbacks implements LoaderCallbacks<ArrayList<String>> {
         @Override
         public Loader<ArrayList<String>> onCreateLoader(int id, Bundle args) {
             // mSuggestClient is set to null in onDestroyView(), so using it
             // safely here relies on the fact that onCreateLoader() is called