Bug 898501: HomeFragment should handle FaviconsLoader. [r=lucasr]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 29 Jul 2013 15:05:11 -0700
changeset 151461 0a243d3f33c2b922ea6e267fe5553e5ecc4485ed
parent 151460 769616494a6ec1b1a339c8fca337a564dbe902f7
child 151462 0398421459d76af1ccd8f677fd2fe315177e34e8
push idunknown
push userunknown
push dateunknown
reviewerslucasr
bugs898501
milestone25.0a1
Bug 898501: HomeFragment should handle FaviconsLoader. [r=lucasr]
mobile/android/base/home/HomeFragment.java
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -17,18 +17,22 @@ 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;
@@ -42,16 +46,19 @@ 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;
+
     protected void showSubPage(Fragment subPage) {
         getActivity().getSupportFragmentManager().beginTransaction()
                 .addToBackStack(null).replace(R.id.home_pager_container, subPage, HomePager.SUBPAGE_TAG)
                 .commitAllowingStateLoss();
     }
 
     @Override
     public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
@@ -245,9 +252,43 @@ 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.
+        }
+    }
 }