Bug 1377287: Update TopSitesCard label to iOS algorithm. r=sebastian

MozReview-Commit-ID: CIfFqABGmq7

--HG--
extra : rebase_source : d5d18f5bf2824c6b86488aaa2ebd58f080176830
This commit is contained in:
Michael Comella 2017-07-19 14:08:32 -07:00
Родитель d310bde422
Коммит 787d50fb2d
1 изменённых файлов: 44 добавлений и 12 удалений

Просмотреть файл

@ -4,18 +4,18 @@
* 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;
@ -24,9 +24,12 @@ 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
@ -102,20 +105,49 @@ import java.util.concurrent.Future;
}
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());
}
}
}