Bug 898501: HomeCursorLoaderCallbacks should be a separate class. [r=lucasr]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 29 Jul 2013 15:25:46 -0700
changeset 151475 41e8b95f56679783cd32962e30b537e9ce2b6350
parent 151474 329abf307997308f1a1fd24645224907d12f6965
child 151476 31027d675f80c44b4b4e20ad32309b9946717bd0
push idunknown
push userunknown
push dateunknown
reviewerslucasr
bugs898501
milestone25.0a1
Bug 898501: HomeCursorLoaderCallbacks should be a separate class. [r=lucasr]
mobile/android/base/Makefile.in
mobile/android/base/home/BookmarksPage.java
mobile/android/base/home/BrowserSearch.java
mobile/android/base/home/HomeCursorLoaderCallbacks.java
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/MostRecentPage.java
mobile/android/base/home/MostVisitedPage.java
mobile/android/base/home/PinBookmarkDialog.java
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -217,16 +217,17 @@ FENNEC_JAVA_FILES = \
   gfx/VirtualLayer.java \
   home/BookmarksListAdapter.java \
   home/BookmarksListView.java \
   home/BookmarksPage.java \
   home/BookmarkFolderView.java \
   home/BookmarkThumbnailView.java \
   home/BrowserSearch.java \
   home/HistoryPage.java \
+  home/HomeCursorLoaderCallbacks.java \
   home/HomeFragment.java \
   home/HomeListView.java \
   home/HomePager.java \
   home/HomePagerTabStrip.java \
   home/FadedTextView.java \
   home/FaviconsLoader.java \
   home/LastTabsPage.java \
   home/MostRecentPage.java \
--- a/mobile/android/base/home/BookmarksPage.java
+++ b/mobile/android/base/home/BookmarksPage.java
@@ -124,35 +124,37 @@ public class BookmarksPage extends HomeF
         registerForContextMenu(mList);
         registerForContextMenu(mTopBookmarks);
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        final Activity activity = getActivity();
+
         // Setup the top bookmarks adapter.
-        mTopBookmarksAdapter = new TopBookmarksAdapter(getActivity(), null);
+        mTopBookmarksAdapter = new TopBookmarksAdapter(activity, null);
         mTopBookmarks.setAdapter(mTopBookmarksAdapter);
 
         // Setup the list adapter.
-        mListAdapter = new BookmarksListAdapter(getActivity(), null);
+        mListAdapter = new BookmarksListAdapter(activity, null);
         mListAdapter.setOnRefreshFolderListener(new OnRefreshFolderListener() {
             @Override
             public void onRefreshFolder(int folderId) {
                 // Restart the loader with folder as the argument.
                 Bundle bundle = new Bundle();
                 bundle.putInt(BOOKMARKS_FOLDER_KEY, folderId);
                 getLoaderManager().restartLoader(LOADER_ID_BOOKMARKS_LIST, bundle, mLoaderCallbacks);
             }
         });
         mList.setAdapter(mListAdapter);
 
         // Create callbacks before the initial loader is started.
-        mLoaderCallbacks = new CursorLoaderCallbacks();
+        mLoaderCallbacks = new CursorLoaderCallbacks(activity, getLoaderManager());
         mThumbnailsLoaderCallbacks = new ThumbnailsLoaderCallbacks();
         loadIfVisible();
     }
 
     @Override
     public void onDestroyView() {
         mList = null;
         mListAdapter = null;
@@ -364,16 +366,20 @@ public class BookmarksPage extends HomeF
             return BrowserDB.getTopSites(getContext().getContentResolver(), max);
         }
     }
 
     /**
      * Loader callbacks for the LoaderManager of this fragment.
      */
     private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
+        public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+            super(context, loaderManager);
+        }
+
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             switch(id) {
                 case LOADER_ID_BOOKMARKS_LIST: {
                     if (args == null) {
                         return new BookmarksLoader(getActivity());
                     } else {
                         return new BookmarksLoader(getActivity(), args.getInt(BOOKMARKS_FOLDER_KEY));
--- a/mobile/android/base/home/BrowserSearch.java
+++ b/mobile/android/base/home/BrowserSearch.java
@@ -25,16 +25,17 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.app.Activity;
 import android.content.Context;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
+import android.support.v4.app.LoaderManager;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.AsyncTaskLoader;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.SimpleCursorAdapter;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -257,25 +258,27 @@ public class BrowserSearch extends HomeF
 
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:Get", null));
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        final Activity activity = getActivity();
+
         // Intialize the search adapter
-        mAdapter = new SearchAdapter(getActivity());
+        mAdapter = new SearchAdapter(activity);
         mList.setAdapter(mAdapter);
 
         // Only create an instance when we need it
         mSuggestionLoaderCallbacks = null;
 
         // Create callbacks before the initial loader is started
-        mCursorLoaderCallbacks = new CursorLoaderCallbacks();
+        mCursorLoaderCallbacks = new CursorLoaderCallbacks(activity, getLoaderManager());
         loadIfVisible();
     }
 
     @Override
     public void handleMessage(String event, final JSONObject message) {
         if (event.equals("SearchEngines:Data")) {
             ThreadUtils.postToUiThread(new Runnable() {
                 @Override
@@ -755,16 +758,20 @@ public class BrowserSearch extends HomeF
             }
 
             // Return search engine index
             return position - resultCount;
         }
     }
 
     private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
+        public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+            super(context, loaderManager);
+        }
+
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             if (id == LOADER_ID_SEARCH) {
                 return SearchLoader.createInstance(getActivity(), args);
             } else {
                 return super.onCreateLoader(id, args);
             }
         }
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/home/HomeCursorLoaderCallbacks.java
@@ -0,0 +1,58 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.home;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.support.v4.app.LoaderManager;
+import android.support.v4.app.LoaderManager.LoaderCallbacks;
+import android.support.v4.content.Loader;
+
+/**
+ * Cursor loader callbacks that takes care loading favicons into memory.
+ */
+abstract class HomeCursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
+
+    // Cursor loader ID for favicons query
+    private static final int LOADER_ID_FAVICONS = 100;
+
+    private final Context mContext;
+    private final LoaderManager mLoaderManager;
+
+    public HomeCursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+        mContext = context;
+        mLoaderManager = loaderManager;
+    }
+
+    public void loadFavicons(Cursor cursor) {
+        FaviconsLoader.restartFromCursor(mLoaderManager, LOADER_ID_FAVICONS, this, cursor);
+    }
+
+    @Override
+    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
+        if (id == LOADER_ID_FAVICONS) {
+            return FaviconsLoader.createInstance(mContext, args);
+        }
+
+        return null;
+    }
+
+    @Override
+    public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
+        if (loader.getId() == LOADER_ID_FAVICONS) {
+            onFaviconsLoaded();
+        }
+    }
+
+    @Override
+    public void onLoaderReset(Loader<Cursor> loader) {
+        // Do nothing by default.
+    }
+
+    // Callback for favicons loaded in memory.
+    public abstract void onFaviconsLoaded();
+}
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -10,29 +10,24 @@ import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.ReaderModeUtils;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.home.HomeListView.HomeContextMenuInfo;
-import org.mozilla.gecko.ReaderModeUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.util.UiAsyncTask;
 
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
-import android.database.Cursor;
 import android.graphics.Bitmap;
-import android.os.Bundle;
 import android.support.v4.app.Fragment;
-import android.support.v4.app.LoaderManager.LoaderCallbacks;
-import android.support.v4.content.Loader;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Toast;
@@ -46,19 +41,16 @@ abstract class HomeFragment extends Frag
     private static final String LOGTAG="GeckoHomeFragment";
 
     // Share MIME type.
     private static final String SHARE_MIME_TYPE = "text/plain";
 
     // URL to Title replacement regex.
     private static final String REGEX_URL_TO_TITLE = "^([a-z]+://)?(www\\.)?";
 
-    // Cursor loader ID for favicons query
-    private static final int LOADER_ID_FAVICONS = 100;
-
     @Override
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
         if (menuInfo == null || !(menuInfo instanceof HomeContextMenuInfo)) {
             return;
         }
 
         HomeContextMenuInfo info = (HomeContextMenuInfo) menuInfo;
 
@@ -247,43 +239,9 @@ abstract class HomeFragment extends Frag
 
     protected void loadIfVisible() {
         if (!isVisible() || !getUserVisibleHint()) {
             return;
         }
 
         load();
     }
-
-    /**
-     * Cursor loader callbacks that takes care loading favicons into memory.
-     */
-    abstract class HomeCursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
-
-        // Callback for favicons loaded in memory.
-        public abstract void onFaviconsLoaded();
-
-        public void loadFavicons(Cursor cursor) {
-            FaviconsLoader.restartFromCursor(getLoaderManager(), LOADER_ID_FAVICONS, this, cursor);
-        }
-
-        @Override
-        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
-            if (id == LOADER_ID_FAVICONS) {
-                return FaviconsLoader.createInstance(getActivity(), args);
-            }
-
-            return null;
-        }
-
-        @Override
-        public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
-            if (loader.getId() == LOADER_ID_FAVICONS) {
-                onFaviconsLoaded();
-            }
-        }
-
-        @Override
-        public void onLoaderReset(Loader<Cursor> loader) {
-            // Do nothing by default.
-        }
-    }
 }
--- a/mobile/android/base/home/MostRecentPage.java
+++ b/mobile/android/base/home/MostRecentPage.java
@@ -12,16 +12,17 @@ import org.mozilla.gecko.home.HomePager.
 import org.mozilla.gecko.home.TwoLinePageRow;
 
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.SimpleCursorAdapter;
 import android.util.SparseArray;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.LayoutInflater;
 import android.widget.AdapterView;
 import android.widget.ListView;
@@ -135,25 +136,27 @@ public class MostRecentPage extends Home
         super.onDestroyView();
         mList = null;
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        final Activity activity = getActivity();
+
         // Initialize map of history sections
         mMostRecentSections = new SparseArray<MostRecentSection>();
 
         // Intialize adapter
-        mAdapter = new MostRecentAdapter(getActivity());
+        mAdapter = new MostRecentAdapter(activity);
         mList.setAdapter(mAdapter);
 
         // Create callbacks before the initial loader is started
-        mCursorLoaderCallbacks = new CursorLoaderCallbacks();
+        mCursorLoaderCallbacks = new CursorLoaderCallbacks(activity, getLoaderManager());
         loadIfVisible();
     }
 
     @Override
     protected void load() {
         getLoaderManager().initLoader(LOADER_ID_HISTORY, null, mCursorLoaderCallbacks);
     }
 
@@ -340,16 +343,20 @@ public class MostRecentPage extends Home
                 row.updateFromCursor(c);
 
                 return row;
             }
         }
     }
 
     private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
+        public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+            super(context, loaderManager);
+        }
+
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             if (id == LOADER_ID_HISTORY) {
                 return new MostRecentCursorLoader(getActivity());
             } else {
                 return super.onCreateLoader(id, args);
             }
         }
--- a/mobile/android/base/home/MostVisitedPage.java
+++ b/mobile/android/base/home/MostVisitedPage.java
@@ -10,16 +10,17 @@ import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
 
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.CursorAdapter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -111,22 +112,24 @@ public class MostVisitedPage extends Hom
         mList = null;
         mEmptyMessage = null;
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        final Activity activity = getActivity();
+
         // Intialize the search adapter
-        mAdapter = new VisitedAdapter(getActivity(), null);
+        mAdapter = new VisitedAdapter(activity, null);
         mList.setAdapter(mAdapter);
 
         // Create callbacks before the initial loader is started
-        mCursorLoaderCallbacks = new CursorLoaderCallbacks();
+        mCursorLoaderCallbacks = new CursorLoaderCallbacks(activity, getLoaderManager());
         loadIfVisible();
     }
 
     @Override
     protected void load() {
         getLoaderManager().initLoader(LOADER_ID_FRECENCY, null, mCursorLoaderCallbacks);
     }
 
@@ -158,16 +161,20 @@ public class MostVisitedPage extends Hom
 
         @Override
         public View newView(Context context, Cursor cursor, ViewGroup parent) {
             return LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_row, parent, false);
         }
     }
 
     private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
+        public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+            super(context, loaderManager);
+        }
+
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             if (id == LOADER_ID_FRECENCY) {
                 return new FrecencyCursorLoader(getActivity());
             } else {
                 return super.onCreateLoader(id, args);
             }
         }
--- a/mobile/android/base/home/PinBookmarkDialog.java
+++ b/mobile/android/base/home/PinBookmarkDialog.java
@@ -3,21 +3,22 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.home;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 
+import android.app.Activity;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
-import android.support.v4.app.LoaderManager.LoaderCallbacks;
+import android.support.v4.app.LoaderManager;
 import android.support.v4.content.Loader;
 import android.support.v4.widget.CursorAdapter;
 import android.text.Editable;
 import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -119,25 +120,28 @@ class PinBookmarkDialog extends DialogFr
             }
         });
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
+        final Activity activity = getActivity();
+        final LoaderManager manager = getLoaderManager();
+
         // Initialize the search adapter
-        mAdapter = new SearchAdapter(getActivity());
+        mAdapter = new SearchAdapter(activity);
         mList.setAdapter(mAdapter);
 
         // Create callbacks before the initial loader is started
-        mLoaderCallbacks = new CursorLoaderCallbacks();
+        mLoaderCallbacks = new CursorLoaderCallbacks(activity, manager);
 
         // Reconnect to the loader only if present
-        getLoaderManager().initLoader(LOADER_ID_SEARCH, null, mLoaderCallbacks);
+        manager.initLoader(LOADER_ID_SEARCH, null, mLoaderCallbacks);
 
         // Default filter.
         filter("");
     }
 
     private void filter(String searchTerm) {
         if (!TextUtils.isEmpty(searchTerm) &&
             TextUtils.equals(mSearchTerm, searchTerm)) {
@@ -170,55 +174,47 @@ class PinBookmarkDialog extends DialogFr
         }
 
         @Override
         public View newView(Context context, Cursor cursor, ViewGroup parent) {
             return (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, parent, false);
         }
     }
 
-    private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
+    private class CursorLoaderCallbacks extends HomeCursorLoaderCallbacks {
+        public CursorLoaderCallbacks(Context context, LoaderManager loaderManager) {
+            super(context, loaderManager);
+        }
+
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
-            switch(id) {
-            case LOADER_ID_SEARCH:
+            if (id == LOADER_ID_SEARCH) {
                 return SearchLoader.createInstance(getActivity(), args);
-
-            case LOADER_ID_FAVICONS:
-                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 LOADER_ID_SEARCH:
+            if (loader.getId() == LOADER_ID_SEARCH) {
                 mAdapter.swapCursor(c);
-
-                FaviconsLoader.restartFromCursor(getLoaderManager(), LOADER_ID_FAVICONS,
-                        mLoaderCallbacks, c);
-                break;
-
-            case LOADER_ID_FAVICONS:
-                // Force the list to use the 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 LOADER_ID_SEARCH:
+            if (loader.getId() == LOADER_ID_SEARCH) {
                 mAdapter.swapCursor(null);
-                break;
+            } else {
+                super.onLoaderReset(loader);
+            }
+        }
 
-            case LOADER_ID_FAVICONS:
-                // Do nothing
-                break;
-            }
+        @Override
+        public void onFaviconsLoaded() {
+            mAdapter.notifyDataSetChanged();
         }
     }
 }