improved click link to open media

fixed #210
This commit is contained in:
Mariotaku Lee 2016-02-05 03:21:11 +08:00
parent 47d4b5b54d
commit 20b09ae809
3 changed files with 26 additions and 10 deletions

View File

@ -390,6 +390,14 @@ public class ParcelableMedia implements Parcelable {
return false; 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, @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}) Type.TYPE_CARD_ANIMATED_GIF, Type.TYPE_EXTERNAL_PLAYER, Type.TYPE_VARIABLE_TYPE})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)

View File

@ -1392,24 +1392,26 @@ public class StatusFragment extends BaseSupportFragment implements LoaderCallbac
@Override @Override
public void onLinkClick(String link, String orig, long accountId, long extraId, int type, boolean sensitive, int start, int end) { public void onLinkClick(String link, String orig, long accountId, long extraId, int type, boolean sensitive, int start, int end) {
final ParcelableStatus status = adapter.getStatus(); final ParcelableStatus status = adapter.getStatus();
if (status.media != null) { ParcelableMedia current = null;
for (final ParcelableMedia media : status.media) { if ((current = ParcelableMedia.findByUrl(status.media, link)) != null) {
if (link.equals(media.url)) { expandOrOpenMedia(current);
expandOrOpenMedia();
return; return;
} }
} if ((current = ParcelableMedia.findByUrl(status.quoted_media, link)) != null) {
expandOrOpenMedia(current);
return;
} }
if (type == TwidereLinkify.LINK_TYPE_STATUS && status.id == NumberUtils.toLong(link)) { if (type == TwidereLinkify.LINK_TYPE_STATUS && status.id == NumberUtils.toLong(link)) {
expandOrOpenMedia(); expandOrOpenMedia(current);
return; return;
} }
super.onLinkClick(link, orig, accountId, extraId, type, sensitive, start, end); super.onLinkClick(link, orig, accountId, extraId, type, sensitive, start, end);
} }
private void expandOrOpenMedia() { private void expandOrOpenMedia(ParcelableMedia current) {
if (adapter.isDetailMediaExpanded()) { if (adapter.isDetailMediaExpanded()) {
//TODO open first media //TODO open first media
Utils.openMedia(adapter.getContext(), adapter.getStatus(), current, null);
return; return;
} }
adapter.setDetailMediaExpanded(true); adapter.setDetailMediaExpanded(true);

View File

@ -63,6 +63,7 @@ import okhttp3.Dns;
public class TwidereDns implements Constants, Dns { public class TwidereDns implements Constants, Dns {
private static final String RESOLVER_LOGTAG = "TwidereDns"; private static final String RESOLVER_LOGTAG = "TwidereDns";
private static final boolean CHECK_ADDRESS = Boolean.parseBoolean("false");
private final SharedPreferences mHostMapping, mPreferences; private final SharedPreferences mHostMapping, mPreferences;
private final HostCache mHostCache = new HostCache(512); private final HostCache mHostCache = new HostCache(512);
@ -217,7 +218,7 @@ public class TwidereDns implements Constants, Dns {
} else { } else {
continue; 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())); resolvedAddresses.add(InetAddress.getByAddress(originalHost, inetAddress.getAddress()));
} }
} }
@ -239,6 +240,11 @@ public class TwidereDns implements Constants, Dns {
// return addresses.toArray(new InetAddress[addresses.size()]); // 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) { private void putCache(String host, InetAddress[] addresses, long ttl, TimeUnit unit) {
if (ArrayUtils.isEmpty(addresses) || ArrayUtils.contains(addresses, null)) return; if (ArrayUtils.isEmpty(addresses) || ArrayUtils.contains(addresses, null)) return;
mHostCache.put(host, addresses, ttl, unit); mHostCache.put(host, addresses, ttl, unit);