Bug 1377287: Use FFiOS label algorithm for BottomSheetContextMenu. r=sebastian
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 19 Jul 2017 14:36:14 -0700
changeset 371301 93c07d38f88036aabdd785bf4ef834a8a658e753
parent 371300 e244d146570c181f2d3108aebb50d515d5b0ca87
child 371302 4da81d3d8c65be8fecb9c1d4452ca463cb136c67
push id93049
push usercbook@mozilla.com
push dateThu, 27 Jul 2017 09:30:07 +0000
treeherdermozilla-inbound@5e9f7561c2eb [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: Use FFiOS label algorithm for BottomSheetContextMenu. r=sebastian MozReview-Commit-ID: LWan7wVNASb
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/BottomSheetContextMenu.java
mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/BottomSheetContextMenu.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/BottomSheetContextMenu.java
@@ -1,36 +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.menu;
 
 import android.app.Activity;
 import android.content.Context;
+import android.os.AsyncTask;
+import android.support.annotation.Nullable;
 import android.support.design.widget.BottomSheetBehavior;
 import android.support.design.widget.BottomSheetDialog;
 import android.support.design.widget.NavigationView;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
-
 import org.mozilla.gecko.R;
-import org.mozilla.gecko.activitystream.ActivityStream;
 import org.mozilla.gecko.activitystream.ActivityStreamTelemetry;
+import org.mozilla.gecko.activitystream.homepanel.model.Item;
 import org.mozilla.gecko.home.HomePager;
-import org.mozilla.gecko.activitystream.homepanel.model.Item;
 import org.mozilla.gecko.icons.IconCallback;
 import org.mozilla.gecko.icons.IconResponse;
 import org.mozilla.gecko.icons.Icons;
+import org.mozilla.gecko.util.StringUtils;
+import org.mozilla.gecko.util.URIUtils;
 import org.mozilla.gecko.widget.FaviconView;
 
-import static org.mozilla.gecko.activitystream.ActivityStream.extractLabel;
+import java.lang.ref.WeakReference;
+import java.net.URI;
 
 /* package-private */ class BottomSheetContextMenu
         extends ActivityStreamContextMenu {
 
 
     private final BottomSheetDialog bottomSheetDialog;
 
     private final NavigationView navigationView;
@@ -57,23 +61,24 @@ import static org.mozilla.gecko.activity
         this.activityView = ((Activity) context).findViewById(android.R.id.content);
 
         bottomSheetDialog = new BottomSheetDialog(context);
         final LayoutInflater inflater = LayoutInflater.from(context);
         this.content = inflater.inflate(R.layout.activity_stream_contextmenu_bottomsheet, (ViewGroup) activityView, false);
 
         bottomSheetDialog.setContentView(content);
 
-        ((TextView) content.findViewById(R.id.title)).setText(item.getTitle());
+        final String pageTitle = item.getTitle();
+        final String sheetPageTitle = !TextUtils.isEmpty(pageTitle) ? pageTitle : item.getUrl();
+        ((TextView) content.findViewById(R.id.title)).setText(sheetPageTitle);
 
-        extractLabel(context, item.getUrl(), false, new ActivityStream.LabelCallback() {
-                public void onLabelExtracted(String label) {
-                    ((TextView) content.findViewById(R.id.url)).setText(label);
-                }
-        });
+        final TextView pageDomainView = (TextView) content.findViewById(R.id.url);
+        final UpdatePageDomainAsyncTask updateDomainAsyncTask = new UpdatePageDomainAsyncTask(context, pageDomainView,
+                item.getUrl());
+        updateDomainAsyncTask.execute();
 
         // Copy layouted parameters from the Highlights / TopSites items to ensure consistency
         final FaviconView faviconView = (FaviconView) content.findViewById(R.id.icon);
         ViewGroup.LayoutParams layoutParams = faviconView.getLayoutParams();
         layoutParams.width = tilesWidth;
         layoutParams.height = tilesHeight;
         faviconView.setLayoutParams(layoutParams);
 
@@ -116,9 +121,60 @@ import static org.mozilla.gecko.activity
 
         bottomSheetDialog.show();
     }
 
     public void dismiss() {
         bottomSheetDialog.dismiss();
     }
 
+    /** Updates the given TextView's text to the page domain. */
+    private static class UpdatePageDomainAsyncTask extends AsyncTask<Void, Void, String> {
+        private final WeakReference<Context> contextWeakReference;
+        private final WeakReference<TextView> pageDomainViewWeakReference;
+
+        private final String uriString;
+        @Nullable private final URI uri;
+
+        private UpdatePageDomainAsyncTask(final Context context, final TextView pageDomainView, final String uriString) {
+            this.contextWeakReference = new WeakReference<>(context);
+            this.pageDomainViewWeakReference = new WeakReference<>(pageDomainView);
+
+            this.uriString = uriString;
+            this.uri = URIUtils.uriOrNull(uriString);
+        }
+
+        @Override
+        protected String doInBackground(final Void... params) {
+            final Context context = contextWeakReference.get();
+            if (context == null || uri == null) {
+                return null;
+            }
+
+            return URIUtils.getBaseDomain(context, uri);
+        }
+
+        @Override
+        protected void onPostExecute(final String baseDomain) {
+            super.onPostExecute(baseDomain);
+
+            final TextView pageDomainView = pageDomainViewWeakReference.get();
+            if (pageDomainView == null) {
+                return;
+            }
+
+            final String updateText;
+            if (!TextUtils.isEmpty(baseDomain)) {
+                updateText = baseDomain;
+
+            // In the unlikely error case, we leave the field blank (null) rather than setting it to the url because
+            // the page title view sets itself to the url on error.
+            } else if (uri != null) {
+                final String normalizedHost = StringUtils.stripCommonSubdomains(uri.getHost());
+                updateText = !TextUtils.isEmpty(normalizedHost) ? normalizedHost : null;
+            } else {
+                updateText = null;
+            }
+
+            pageDomainView.setText(updateText);
+        }
+    }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
@@ -18,16 +18,26 @@ import java.net.URI;
 import java.net.URISyntaxException;
 
 /** Utilities for operating on URLs. */
 public class URIUtils {
     private static final String LOGTAG = "GeckoURIUtils";
 
     private URIUtils() {}
 
+    /** @return a {@link URI} if possible, else null. */
+    @Nullable
+    public static URI uriOrNull(final String uriString) {
+        try {
+            return new URI(uriString);
+        } catch (final URISyntaxException e) {
+            return null;
+        }
+    }
+
     /**
      * Returns the second level domain (SLD) of a url. It removes any subdomain/TLD.
      * e.g. https://m.foo.com/bar/baz?noo=abc#123  => foo
      *
      * This implementation is taken from Firefox for iOS:
      *   https://github.com/mozilla-mobile/firefox-ios/blob/deb9736c905cdf06822ecc4a20152df7b342925d/Shared/Extensions/NSURLExtensions.swift#L152
      *
      * @param uriString A url from which to extract the second level domain.