Bug 1325931 - Use compound drawable text centering hack for AS Topsites draft
authorAndrzej Hunt <ahunt@mozilla.com>
Thu, 09 Feb 2017 13:27:20 -0800
changeset 481480 5e09e52aa19bcaf5525ca8d2431c9cad98daa43c
parent 481479 da17e49e06328320082d1654b9ec92e86aeb4e53
child 545197 267f6d2299483c9f5cfbe308c51107063544c822
push id44814
push userahunt@mozilla.com
push dateThu, 09 Feb 2017 22:32:53 +0000
bugs1325931
milestone54.0a1
Bug 1325931 - Use compound drawable text centering hack for AS Topsites MozReview-Commit-ID: 4reNZTHHZNw
mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/topsites/TopSitesCard.java
@@ -1,15 +1,16 @@
 /* -*- 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.activitystream.topsites;
 
 import android.graphics.Color;
+import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.support.v4.widget.TextViewCompat;
 import android.support.v7.widget.RecyclerView;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -82,23 +83,16 @@ import java.util.concurrent.Future;
 
         ViewUtil.enableTouchRipple(menuButton);
     }
 
     void bind(final TopSite topSite, final int absolutePosition) {
         this.topSite = topSite;
         this.absolutePosition = absolutePosition;
 
-        ActivityStream.extractLabel(itemView.getContext(), topSite.getUrl(), true, new ActivityStream.LabelCallback() {
-            @Override
-            public void onLabelExtracted(String label) {
-                title.setText(label);
-            }
-        });
-
         if (ongoingIconLoad != null) {
             ongoingIconLoad.cancel(true);
         }
 
         ongoingIconLoad = Icons.with(itemView.getContext())
                 .pageUrl(topSite.getUrl())
                 .skipNetwork()
                 .build()
@@ -107,16 +101,26 @@ import java.util.concurrent.Future;
         final Drawable pinDrawable;
         if (topSite.isPinned()) {
             pinDrawable = DrawableUtil.tintDrawable(itemView.getContext(), R.drawable.as_pin, itemView.getResources().getColor(R.color.placeholder_grey));
         } else {
             pinDrawable = null;
         }
         TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(title, pinDrawable, null, null, null);
 
+        // setCenteredText() needs to have all drawable's in place to correctly layout the text,
+        // so we need to wait with requesting the title until we've set our pin icon.
+        ActivityStream.extractLabel(itemView.getContext(), topSite.getUrl(), true, new ActivityStream.LabelCallback() {
+            @Override
+            public void onLabelExtracted(String label) {
+                // We use consistent padding all around the title, and the top padding is never modified,
+                // so we can pass that in as the default padding:
+                ViewUtil.setCenteredText(title, label, title.getPaddingTop());
+            }
+        });
     }
 
     @Override
     public void onIconResponse(IconResponse response) {
         faviconView.updateImage(response);
 
         final int tintColor = !response.hasColor() || response.getColor() == Color.WHITE ? Color.LTGRAY : Color.WHITE;