Common host name cache

This commit is contained in:
M66B 2016-01-27 12:44:15 +01:00
parent bce1a26c7d
commit 94ec8a5266
4 changed files with 67 additions and 61 deletions

View File

@ -2,7 +2,6 @@ package eu.faircode.netguard;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -10,8 +9,6 @@ import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.TextView;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
public class AccessAdapter extends CursorAdapter {
@ -46,32 +43,15 @@ public class AccessAdapter extends CursorAdapter {
// Get views
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
CheckBox cbIp = (CheckBox) view.findViewById(R.id.cbIp);
final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
TextView tvPort = (TextView) view.findViewById(R.id.tvPort);
// Set values
tvTime.setText(new SimpleDateFormat("HH:mm:ss").format(time));
cbIp.setChecked(allowed != 0);
tvDest.setText(dest);
tvPort.setText(dport > 0 ? Integer.toString(dport) : "");
new AsyncTask<String, Object, String>() {
@Override
protected void onPreExecute() {
tvDest.setText(dest + ":" + dport);
}
@Override
protected String doInBackground(String... args) {
try {
// This requires internet permission
return InetAddress.getByName(args[0]).getHostName();
} catch (UnknownHostException ignored) {
return args[0];
}
}
@Override
protected void onPostExecute(String host) {
tvDest.setText(host + ":" + dport);
}
}.execute(dest);
Util.resolveName(dest, tvDest);
}
}

View File

@ -6,7 +6,6 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
@ -22,8 +21,6 @@ import com.squareup.picasso.Picasso;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
public class LogAdapter extends CursorAdapter {
private static String TAG = "NetGuard.Log";
@ -44,7 +41,6 @@ public class LogAdapter extends CursorAdapter {
private int colInteractive;
private InetAddress vpn4 = null;
private InetAddress vpn6 = null;
private Map<String, String> mapIPHost = new HashMap<String, String>();
public LogAdapter(Context context, Cursor cursor, boolean resolve) {
super(context, cursor, 0);
@ -175,33 +171,7 @@ public class LogAdapter extends CursorAdapter {
tvSAddr.setText(getKnownAddress(saddr));
if (resolve && !isKnownAddress(daddr))
synchronized (mapIPHost) {
if (mapIPHost.containsKey(daddr))
tvDaddr.setText(mapIPHost.get(daddr));
else {
tvDaddr.setText(daddr);
new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... args) {
try {
// This requires internet permission
return InetAddress.getByName(args[0]).getHostName();
} catch (UnknownHostException ignored) {
return args[0];
}
}
@Override
protected void onPostExecute(String host) {
synchronized (mapIPHost) {
if (!mapIPHost.containsKey(host))
mapIPHost.put(host, host);
}
tvDaddr.setText(host);
}
}.execute(daddr);
}
}
Util.resolveName(daddr, tvDaddr);
else
tvDaddr.setText(getKnownAddress(daddr));

View File

@ -45,6 +45,7 @@ import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
@ -55,10 +56,13 @@ import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -349,6 +353,39 @@ public class Util {
}
}
private static Map<String, String> mapIPHost = new HashMap<String, String>();
public static void resolveName(String name, final TextView tv) {
synchronized (mapIPHost) {
if (mapIPHost.containsKey(name))
tv.setText(mapIPHost.get(name));
else {
tv.setText(name);
new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... args) {
try {
// This requires internet permission
return InetAddress.getByName(args[0]).getHostName();
} catch (UnknownHostException ignored) {
return args[0];
}
}
@Override
protected void onPostExecute(String host) {
synchronized (mapIPHost) {
if (!mapIPHost.containsKey(host))
mapIPHost.put(host, host);
}
tv.setText(host);
}
}.execute(name);
}
}
}
public static void setTheme(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dark = prefs.getBoolean("dark_theme", false);

View File

@ -23,12 +23,31 @@
android:scaleX="0.8"
android:scaleY="0.8" />
<TextView
android:id="@+id/tvDest"
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textSize="12sp" />
android:layout_weight="1">
<TextView
android:id="@+id/tvDest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textSize="12sp" />
<TextView
android:id="@+id/tvPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>