Bug 1377287 - review: Only update views if they're the same as when asyncTask began. r=mcomella

MozReview-Commit-ID: 8XA32VFNSH7

--HG--
extra : rebase_source : fc0bc57794c54b7fce325437a7b080b7147d04db
This commit is contained in:
Michael Comella 2017-07-26 15:32:14 -07:00
Родитель 5690789c4b
Коммит b5558f142f
2 изменённых файлов: 39 добавлений и 2 удалений

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

@ -7,6 +7,7 @@ package org.mozilla.gecko.activitystream.homepanel.stream;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.UiThread;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
@ -30,6 +31,7 @@ import org.mozilla.gecko.util.ViewUtil;
import org.mozilla.gecko.widget.FaviconView;
import java.lang.ref.WeakReference;
import java.util.UUID;
import java.util.concurrent.Future;
public class HighlightItem extends StreamItem implements IconCallback {
@ -158,18 +160,26 @@ public class HighlightItem extends StreamItem implements IconCallback {
/** Updates the text of the given view to the host second level domain. */
private static class UpdatePageDomainAsyncTask extends URIUtils.GetHostSecondLevelDomainAsyncTask {
private static final int VIEW_TAG_ID = R.id.page; // same as the view.
private final WeakReference<TextView> pageDomainViewWeakReference;
private final UUID viewTagAtStart;
UpdatePageDomainAsyncTask(final Context contextReference, final String uriString, final TextView pageDomainView) {
super(contextReference, uriString);
this.pageDomainViewWeakReference = new WeakReference<>(pageDomainView);
// See isTagSameAsStartTag for details.
viewTagAtStart = UUID.randomUUID();
pageDomainView.setTag(VIEW_TAG_ID, viewTagAtStart);
}
@Override
protected void onPostExecute(final String hostSLD) {
super.onPostExecute(hostSLD);
final TextView viewToUpdate = pageDomainViewWeakReference.get();
if (viewToUpdate == null) {
if (viewToUpdate == null || !isTagSameAsStartTag(viewToUpdate)) {
return;
}
@ -177,5 +187,14 @@ public class HighlightItem extends StreamItem implements IconCallback {
// and the page title will be URL if there's an error there so we wouldn't want to write the URL again here.
viewToUpdate.setText(hostSLD);
}
/**
* Returns true if the tag on the given view matches the tag from the constructor. We do this to ensure
* the View we're making this request for hasn't been re-used by the time this request completes.
*/
@UiThread
private boolean isTagSameAsStartTag(final View viewToCheck) {
return viewTagAtStart.equals(viewToCheck.getTag(VIEW_TAG_ID));
}
}
}

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

@ -7,6 +7,7 @@ package org.mozilla.gecko.activitystream.homepanel.topsites;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.UiThread;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
@ -30,6 +31,7 @@ import org.mozilla.gecko.util.ViewUtil;
import org.mozilla.gecko.widget.FaviconView;
import java.lang.ref.WeakReference;
import java.util.UUID;
import java.util.concurrent.Future;
/* package-local */ class TopSitesCard extends RecyclerView.ViewHolder
@ -128,18 +130,25 @@ import java.util.concurrent.Future;
/** Updates the text of the given view to the page domain. */
private static class UpdateCardTitleAsyncTask extends URIUtils.GetHostSecondLevelDomainAsyncTask {
private static final int VIEW_TAG_ID = R.id.title; // same as the view.
private final WeakReference<TextView> titleViewWeakReference;
private final UUID viewTagAtStart;
UpdateCardTitleAsyncTask(final Context contextReference, final String uriString, final TextView titleView) {
super(contextReference, uriString);
this.titleViewWeakReference = new WeakReference<>(titleView);
// See isTagSameAsStartTag for details.
viewTagAtStart = UUID.randomUUID();
titleView.setTag(VIEW_TAG_ID, viewTagAtStart);
}
@Override
protected void onPostExecute(final String hostSLD) {
super.onPostExecute(hostSLD);
final TextView titleView = titleViewWeakReference.get();
if (titleView == null) {
if (titleView == null || !isTagSameAsStartTag(titleView)) {
return;
}
@ -149,5 +158,14 @@ import java.util.concurrent.Future;
// so we can pass that in as the default padding:
ViewUtil.setCenteredText(titleView, updateText, titleView.getPaddingTop());
}
/**
* Returns true if the tag on the given view matches the tag from the constructor. We do this to ensure
* the View we're making this request for hasn't been re-used by the time this request completes.
*/
@UiThread
private boolean isTagSameAsStartTag(final View viewToCheck) {
return viewTagAtStart.equals(viewToCheck.getTag(VIEW_TAG_ID));
}
}
}