From 20b09ae809999f6c71df35e0b29e1d314e870021 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 5 Feb 2016 03:21:11 +0800 Subject: [PATCH] improved click link to open media fixed #210 --- .../twidere/model/ParcelableMedia.java | 8 ++++++++ .../fragment/support/StatusFragment.java | 20 ++++++++++--------- .../twidere/util/net/TwidereDns.java | 8 +++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java index 774afae1f..aa24c81e2 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMedia.java @@ -390,6 +390,14 @@ public class ParcelableMedia implements Parcelable { return false; } + public static ParcelableMedia findByUrl(@Nullable ParcelableMedia[] media, @Nullable String url) { + if (media == null || url == null) return null; + for (ParcelableMedia item : media) { + if (url.equals(item.url)) return item; + } + return null; + } + @IntDef({Type.TYPE_UNKNOWN, Type.TYPE_IMAGE, Type.TYPE_VIDEO, Type.TYPE_ANIMATED_GIF, Type.TYPE_CARD_ANIMATED_GIF, Type.TYPE_EXTERNAL_PLAYER, Type.TYPE_VARIABLE_TYPE}) @Retention(RetentionPolicy.SOURCE) diff --git a/twidere/src/main/java/org/mariotaku/twidere/fragment/support/StatusFragment.java b/twidere/src/main/java/org/mariotaku/twidere/fragment/support/StatusFragment.java index 08ee9e3c2..ec961bb53 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/fragment/support/StatusFragment.java +++ b/twidere/src/main/java/org/mariotaku/twidere/fragment/support/StatusFragment.java @@ -1392,24 +1392,26 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac @Override public void onLinkClick(String link, String orig, long accountId, long extraId, int type, boolean sensitive, int start, int end) { final ParcelableStatus status = adapter.getStatus(); - if (status.media != null) { - for (final ParcelableMedia media : status.media) { - if (link.equals(media.url)) { - expandOrOpenMedia(); - return; - } - } + ParcelableMedia current = null; + if ((current = ParcelableMedia.findByUrl(status.media, link)) != null) { + expandOrOpenMedia(current); + return; + } + if ((current = ParcelableMedia.findByUrl(status.quoted_media, link)) != null) { + expandOrOpenMedia(current); + return; } if (type == TwidereLinkify.LINK_TYPE_STATUS && status.id == NumberUtils.toLong(link)) { - expandOrOpenMedia(); + expandOrOpenMedia(current); return; } super.onLinkClick(link, orig, accountId, extraId, type, sensitive, start, end); } - private void expandOrOpenMedia() { + private void expandOrOpenMedia(ParcelableMedia current) { if (adapter.isDetailMediaExpanded()) { //TODO open first media + Utils.openMedia(adapter.getContext(), adapter.getStatus(), current, null); return; } adapter.setDetailMediaExpanded(true); diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/net/TwidereDns.java b/twidere/src/main/java/org/mariotaku/twidere/util/net/TwidereDns.java index a690c6206..a350b9b4b 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/net/TwidereDns.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/net/TwidereDns.java @@ -63,6 +63,7 @@ import okhttp3.Dns; public class TwidereDns implements Constants, Dns { private static final String RESOLVER_LOGTAG = "TwidereDns"; + private static final boolean CHECK_ADDRESS = Boolean.parseBoolean("false"); private final SharedPreferences mHostMapping, mPreferences; private final HostCache mHostCache = new HostCache(512); @@ -217,7 +218,7 @@ public class TwidereDns implements Constants, Dns { } else { continue; } - if (mConnnectTimeout == 0 || inetAddress.isReachable(TwidereMathUtils.clamp((int) mConnnectTimeout / 2, 1000, 3000))) { + if (mConnnectTimeout == 0 || checkAddress(inetAddress)) { resolvedAddresses.add(InetAddress.getByAddress(originalHost, inetAddress.getAddress())); } } @@ -239,6 +240,11 @@ public class TwidereDns implements Constants, Dns { // return addresses.toArray(new InetAddress[addresses.size()]); } + private boolean checkAddress(InetAddress inetAddress) throws IOException { + if (!CHECK_ADDRESS) return true; + return inetAddress.isReachable(TwidereMathUtils.clamp((int) mConnnectTimeout / 2, 1000, 3000)); + } + private void putCache(String host, InetAddress[] addresses, long ttl, TimeUnit unit) { if (ArrayUtils.isEmpty(addresses) || ArrayUtils.contains(addresses, null)) return; mHostCache.put(host, addresses, ttl, unit);