Bug 1377287: Update TopSitesCard label to iOS algorithm. r=sebastian
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 19 Jul 2017 14:08:32 -0700
changeset 422311 e244d146570c181f2d3108aebb50d515d5b0ca87
parent 422310 cdc31f6fc0840fb5fa76a9fefed1801c3625ce96
child 422312 93c07d38f88036aabdd785bf4ef834a8a658e753
push id1517
push userjlorenzo@mozilla.com
push dateThu, 14 Sep 2017 16:50:54 +0000
treeherdermozilla-release@3b41fd564418 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1377287
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1377287: Update TopSitesCard label to iOS algorithm. r=sebastian MozReview-Commit-ID: CIfFqABGmq7
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topsites/TopSitesCard.java
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topsites/TopSitesCard.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/topsites/TopSitesCard.java
@@ -1,37 +1,40 @@
 /* -*- 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.activitystream.homepanel.topsites;
 
+import android.content.Context;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.support.v4.widget.TextViewCompat;
 import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.TextView;
-
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
-import org.mozilla.gecko.activitystream.ActivityStream;
 import org.mozilla.gecko.activitystream.ActivityStreamTelemetry;
 import org.mozilla.gecko.activitystream.homepanel.menu.ActivityStreamContextMenu;
 import org.mozilla.gecko.activitystream.homepanel.model.TopSite;
 import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.icons.IconCallback;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
 import org.mozilla.gecko.util.DrawableUtil;
+import org.mozilla.gecko.util.TouchTargetUtil;
+import org.mozilla.gecko.util.URIUtils;
 import org.mozilla.gecko.util.ViewUtil;
 import org.mozilla.gecko.widget.FaviconView;
 
+import java.lang.ref.WeakReference;
 import java.util.concurrent.Future;
 
 /* package-local */ class TopSitesCard extends RecyclerView.ViewHolder
         implements IconCallback {
     private final FaviconView faviconView;
 
     private final TextView title;
     private Future<IconResponse> ongoingIconLoad;
@@ -97,25 +100,54 @@ import java.util.concurrent.Future;
         final Drawable pinDrawable;
         if (topSite.isPinned()) {
             pinDrawable = DrawableUtil.tintDrawable(itemView.getContext(), R.drawable.as_pin, Color.WHITE);
         } 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());
-            }
-        });
+        // Our AsyncTask calls setCenteredText(), which 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.
+        //
+        // todo (bug 1382036): iOS' algorithm actually looks like:
+        // if let provider = site.metadata?.providerName {
+        //     titleLabel.text = provider.lowercased()
+        // } else {
+        //     titleLabel.text = site.tileURL.hostSLD
+        // }
+        //
+        // But it's non-trivial to get the provider name with the top site so it's a follow-up.
+        final UpdateCardTitleAsyncTask titleAsyncTask = new UpdateCardTitleAsyncTask(itemView.getContext(),
+                topSite.getUrl(), title);
+        titleAsyncTask.execute();
     }
 
     @Override
     public void onIconResponse(IconResponse response) {
         faviconView.updateImage(response);
     }
+
+    /** Updates the text of the given view to the page domain. */
+    private static class UpdateCardTitleAsyncTask extends URIUtils.GetHostSecondLevelDomainAsyncTask {
+        private final WeakReference<TextView> titleViewWeakReference;
+
+        UpdateCardTitleAsyncTask(final Context contextReference, final String uriString, final TextView titleView) {
+            super(contextReference, uriString);
+            this.titleViewWeakReference = new WeakReference<>(titleView);
+        }
+
+        @Override
+        protected void onPostExecute(final String hostSLD) {
+            super.onPostExecute(hostSLD);
+            final TextView titleView = titleViewWeakReference.get();
+            if (titleView == null) {
+                return;
+            }
+
+            final String updateText = !TextUtils.isEmpty(hostSLD) ? hostSLD : uriString;
+
+            // 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(titleView, updateText, titleView.getPaddingTop());
+        }
+    }
 }