Bug 938141 - Avoid showing favicons before the thumbnails are loaded (r=rnewman, a=bajaj)
authorLucas Rocha <lucasr@mozilla.com>
Fri, 22 Nov 2013 14:35:27 +0000
changeset 167536 d129e2349785eb17aeb45f2630e3d1b11ad1a260
parent 167535 a55d3e9d16f3aaed908f9d8a0947b066e6a70a16
child 167537 64b99c299be99f63f077b06ac1451e1faa54421a
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, bajaj
bugs938141
milestone27.0a2
Bug 938141 - Avoid showing favicons before the thumbnails are loaded (r=rnewman, a=bajaj)
mobile/android/base/home/TopSitesGridItemView.java
mobile/android/base/home/TopSitesPage.java
--- a/mobile/android/base/home/TopSitesGridItemView.java
+++ b/mobile/android/base/home/TopSitesGridItemView.java
@@ -43,16 +43,19 @@ public class TopSitesGridItemView extend
     private String mUrl;
     private String mFaviconURL;
 
     private boolean mThumbnailSet;
 
     // Pinned state.
     private boolean mIsPinned = false;
 
+    // Dirty state.
+    private boolean mIsDirty = false;
+
     // Empty state.
     private boolean mIsEmpty = true;
     private int mLoadId = Favicons.NOT_LOADING;
 
     public TopSitesGridItemView(Context context) {
         this(context, null);
     }
 
@@ -141,16 +144,20 @@ public class TopSitesGridItemView extend
         mTitle = "";
         mIsPinned = false;
         updateTitleView();
         mTitleView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
         setLoadId(Favicons.NOT_LOADING);
         displayThumbnail(R.drawable.top_site_add);
     }
 
+    public void markAsDirty() {
+        mIsDirty = true;
+    }
+
     /**
      * Updates the title, URL, and pinned state of this view.
      *
      * Also resets our loadId to NOT_LOADING.
      *
      * Returns true if any fields changed.
      */
     public boolean updateState(final String title, final String url, final boolean pinned, final Bitmap thumbnail) {
@@ -179,16 +186,22 @@ public class TopSitesGridItemView extend
         }
 
         if (mIsPinned != pinned) {
             mIsPinned = pinned;
             mTitleView.setCompoundDrawablesWithIntrinsicBounds(pinned ? R.drawable.pin : 0, 0, 0, 0);
             changed = true;
         }
 
+        // The dirty state forces the state update to return true
+        // so that the adapter loads favicons once the thumbnails
+        // are loaded in TopSitesPage/TopSitesGridAdapter.
+        changed = (changed || mIsDirty);
+        mIsDirty = false;
+
         return changed;
     }
 
     /**
      * Display the thumbnail from a resource.
      *
      * @param resId Resource ID of the drawable to show.
      */
--- a/mobile/android/base/home/TopSitesPage.java
+++ b/mobile/android/base/home/TopSitesPage.java
@@ -553,16 +553,27 @@ public class TopSitesPage extends HomeFr
 
         /**
          * Update the thumbnails returned by the db.
          *
          * @param thumbnails A map of urls and their thumbnail bitmaps.
          */
         public void updateThumbnails(Map<String, Bitmap> thumbnails) {
             mThumbnails = thumbnails;
+
+            final int count = mGrid.getChildCount();
+            for (int i = 0; i < count; i++) {
+                TopSitesGridItemView gridItem = (TopSitesGridItemView) mGrid.getChildAt(i);
+
+                // All the views have already got their initial state at this point.
+                // This will force each view to load favicons for the missing
+                // thumbnails if necessary.
+                gridItem.markAsDirty();
+            }
+
             notifyDataSetChanged();
         }
 
         @Override
         public void bindView(View bindView, Context context, Cursor cursor) {
             String url = "";
             String title = "";
             boolean pinned = false;
@@ -573,29 +584,34 @@ public class TopSitesPage extends HomeFr
                 title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
                 pinned = ((TopSitesCursorWrapper) cursor).isPinned();
             }
 
             final TopSitesGridItemView view = (TopSitesGridItemView) bindView;
 
             // If there is no url, then show "add bookmark".
             if (TextUtils.isEmpty(url)) {
-                view.blankOut();
+                // Wait until thumbnails are loaded before showing anything.
+                if (mThumbnails != null) {
+                    view.blankOut();
+                }
+
                 return;
             }
 
             // Show the thumbnail, if any.
             Bitmap thumbnail = (mThumbnails != null ? mThumbnails.get(url) : null);
 
             // Debounce bindView calls to avoid redundant redraws and favicon
             // fetches.
             final boolean updated = view.updateState(title, url, pinned, thumbnail);
 
-            // If we sent in a thumbnail, we're done now.
-            if (thumbnail != null) {
+            // If thumbnails are still being loaded, don't try to load favicons
+            // just yet. If we sent in a thumbnail, we're done now.
+            if (mThumbnails == null || thumbnail != null) {
                 return;
             }
 
             // Thumbnails are delivered late, so we can't short-circuit any
             // sooner than this. But we can avoid a duplicate favicon
             // fetch...
             if (!updated) {
                 debug("bindView called twice for same values; short-circuiting.");