made dns query thread safe - seems fixed slow loading issue

fixed #335 again
This commit is contained in:
Mariotaku Lee 2016-02-09 11:07:13 +08:00
parent 6f7cd002a5
commit e6322d11b8
4 changed files with 53 additions and 40 deletions

View File

@ -252,6 +252,7 @@ public abstract class AbsActivitiesAdapter<Data> extends LoadMoreSupportAdapter<
return ITEM_VIEW_TYPE_GAP;
}
final String action = getActivityAction(position);
if (action == null) throw new NullPointerException();
switch (action) {
case Activity.Action.MENTION: {
if (ArrayUtils.isEmpty(activity.target_object_statuses)) {
@ -325,6 +326,7 @@ public abstract class AbsActivitiesAdapter<Data> extends LoadMoreSupportAdapter<
protected abstract void bindTitleSummaryViewHolder(ActivityTitleSummaryViewHolder holder, int position);
@Nullable
public abstract String getActivityAction(int position);
public abstract long getTimestamp(int position);

View File

@ -58,36 +58,39 @@ public abstract class AbsParcelableStatusesAdapter extends AbsStatusesAdapter<Li
}
@Override
public long getItemId(int position) {
if (position == getStatusCount()) return RecyclerView.NO_ID;
public long getItemId(int adapterPosition) {
int dataPosition = adapterPosition - getStatusStartIndex();
if (dataPosition < 0 || dataPosition >= getStatusCount()) return RecyclerView.NO_ID;
if (mData instanceof ObjectCursor) {
final Cursor cursor = ((ObjectCursor) mData).getCursor(position);
final Cursor cursor = ((ObjectCursor) mData).getCursor(dataPosition);
final ParcelableStatusCursorIndices indices = (ParcelableStatusCursorIndices) ((ObjectCursor) mData).getIndices();
return cursor.getLong(indices._id);
}
return mData.get(position).hashCode();
return System.identityHashCode(mData.get(dataPosition));
}
@Override
public long getStatusId(int position) {
if (position == getStatusCount()) return RecyclerView.NO_ID;
public long getStatusId(int adapterPosition) {
int dataPosition = adapterPosition - getStatusStartIndex();
if (dataPosition < 0 || dataPosition >= getStatusCount()) return RecyclerView.NO_ID;
if (mData instanceof ObjectCursor) {
final Cursor cursor = ((ObjectCursor) mData).getCursor(position);
final Cursor cursor = ((ObjectCursor) mData).getCursor(dataPosition);
final ParcelableStatusCursorIndices indices = (ParcelableStatusCursorIndices) ((ObjectCursor) mData).getIndices();
return cursor.getLong(indices.id);
}
return mData.get(position).id;
return mData.get(dataPosition).id;
}
@Override
public long getAccountId(int position) {
if (position == getStatusCount()) return RecyclerView.NO_ID;
public long getAccountId(int adapterPosition) {
int dataPosition = adapterPosition - getStatusStartIndex();
if (dataPosition < 0 || dataPosition >= getStatusCount()) return RecyclerView.NO_ID;
if (mData instanceof ObjectCursor) {
final Cursor cursor = ((ObjectCursor) mData).getCursor(position);
final Cursor cursor = ((ObjectCursor) mData).getCursor(dataPosition);
final ParcelableStatusCursorIndices indices = (ParcelableStatusCursorIndices) ((ObjectCursor) mData).getIndices();
return cursor.getLong(indices.account_id);
}
return mData.get(position).account_id;
return mData.get(dataPosition).account_id;
}
@Override

View File

@ -20,6 +20,8 @@
package org.mariotaku.twidere.adapter;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import org.mariotaku.library.objectcursor.ObjectCursor;
import org.mariotaku.twidere.model.ParcelableActivity;
@ -47,22 +49,27 @@ public class ParcelableActivitiesAdapter extends AbsActivitiesAdapter<List<Parce
}
@Nullable
@Override
public String getActivityAction(int position) {
public String getActivityAction(int adapterPosition) {
int dataPosition = adapterPosition - getActivityStartIndex();
if (dataPosition < 0 || dataPosition >= getActivityCount()) return null;
if (mData instanceof ObjectCursor) {
final ParcelableActivityCursorIndices indices = (ParcelableActivityCursorIndices) ((ObjectCursor) mData).getIndices();
return ((ObjectCursor) mData).getCursor(position).getString(indices.action);
return ((ObjectCursor) mData).getCursor(dataPosition).getString(indices.action);
}
return mData.get(position).action;
return mData.get(dataPosition).action;
}
@Override
public long getTimestamp(int position) {
public long getTimestamp(int adapterPosition) {
int dataPosition = adapterPosition - getActivityStartIndex();
if (dataPosition < 0 || dataPosition >= getActivityCount()) return RecyclerView.NO_ID;
if (mData instanceof ObjectCursor) {
final ParcelableActivityCursorIndices indices = (ParcelableActivityCursorIndices) ((ObjectCursor) mData).getIndices();
return ((ObjectCursor) mData).getCursor(position).getLong(indices.timestamp);
return ((ObjectCursor) mData).getCursor(dataPosition).getLong(indices.timestamp);
}
return mData.get(position).timestamp;
return mData.get(dataPosition).timestamp;
}
@Override

View File

@ -71,7 +71,7 @@ public class TwidereDns implements Constants, Dns {
private Resolver mResolver;
private TimingLogger mLogger;
private long mConnnectTimeout;
private long mConnectTimeout;
public TwidereDns(final Context context) {
mLogger = new TimingLogger(RESOLVER_LOGTAG, "resolve");
@ -81,14 +81,14 @@ public class TwidereDns implements Constants, Dns {
reloadDnsSettings();
}
private static boolean hostMatches(final String host, final String rule) {
if (rule == null || host == null) return false;
if (rule.startsWith(".")) return StringUtils.endsWithIgnoreCase(host, rule);
return host.equalsIgnoreCase(rule);
}
private static boolean isValidIpAddress(final String address) {
return InetAddressUtils.getInetAddressType(address) != 0;
@Override
public synchronized List<InetAddress> lookup(String hostname) throws UnknownHostException {
try {
return Arrays.asList(resolveInternal(hostname, hostname, 0, true));
} catch (IOException e) {
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
}
}
@SuppressWarnings("unused")
@ -96,6 +96,11 @@ public class TwidereDns implements Constants, Dns {
mHostCache.remove(host);
}
public synchronized void reloadDnsSettings() {
mResolver = null;
mConnectTimeout = TimeUnit.SECONDS.toMillis(mPreferences.getInt(KEY_CONNECTION_TIMEOUT, 10));
}
@NonNull
private InetAddress[] resolveInternal(String originalHost, String host, int depth, boolean putCache) throws IOException {
resetLog(originalHost);
@ -218,7 +223,7 @@ public class TwidereDns implements Constants, Dns {
} else {
continue;
}
if (mConnnectTimeout == 0 || checkAddress(inetAddress)) {
if (mConnectTimeout == 0 || checkAddress(inetAddress)) {
resolvedAddresses.add(InetAddress.getByAddress(originalHost, inetAddress.getAddress()));
}
}
@ -242,7 +247,7 @@ public class TwidereDns implements Constants, Dns {
private boolean checkAddress(InetAddress inetAddress) throws IOException {
if (!CHECK_ADDRESS) return true;
return inetAddress.isReachable(TwidereMathUtils.clamp((int) mConnnectTimeout / 2, 1000, 3000));
return inetAddress.isReachable(TwidereMathUtils.clamp((int) mConnectTimeout / 2, 1000, 3000));
}
private void putCache(String host, InetAddress[] addresses, long ttl, TimeUnit unit) {
@ -296,19 +301,15 @@ public class TwidereDns implements Constants, Dns {
return mResolver = resolver;
}
public void reloadDnsSettings() {
mResolver = null;
mConnnectTimeout = TimeUnit.SECONDS.toMillis(mPreferences.getInt(KEY_CONNECTION_TIMEOUT, 10));
private static boolean hostMatches(final String host, final String rule) {
if (rule == null || host == null) return false;
if (rule.startsWith(".")) return StringUtils.endsWithIgnoreCase(host, rule);
return host.equalsIgnoreCase(rule);
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
try {
return Arrays.asList(resolveInternal(hostname, hostname, 0, true));
} catch (IOException e) {
if (e instanceof UnknownHostException) throw (UnknownHostException) e;
throw new UnknownHostException("Unable to resolve address " + e.getMessage());
}
private static boolean isValidIpAddress(final String address) {
return InetAddressUtils.getInetAddressType(address) != 0;
}
private static class HostCache extends LruCache<String, InetAddress[]> {