diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 68f8c63541..5b553fd404 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -5357,7 +5357,7 @@ public class AdapterMessage extends RecyclerView.Adapter() { + new SimpleTask>() { @Override protected void onPreExecute(Bundle args) { btnOwner.setEnabled(false); @@ -5373,17 +5373,15 @@ public class AdapterMessage extends RecyclerView.Adapter onExecute(Context context, Bundle args) throws Throwable { Uri uri = args.getParcelable("uri"); return IPInfo.getOrganization(uri, context); } @Override - protected void onExecuted(Bundle args, String[] data) { - String host = data[0]; - String organization = data[1]; - tvHost.setText(host); - tvOwner.setText(organization == null ? "?" : organization); + protected void onExecuted(Bundle args, Pair data) { + tvHost.setText(data.first); + tvOwner.setText(data.second.name == null ? "?" : data.second.name); new Handler().post(new Runnable() { @Override public void run() { diff --git a/app/src/main/java/eu/faircode/email/IPInfo.java b/app/src/main/java/eu/faircode/email/IPInfo.java index 1b3597d27a..10e8c31dcb 100644 --- a/app/src/main/java/eu/faircode/email/IPInfo.java +++ b/app/src/main/java/eu/faircode/email/IPInfo.java @@ -23,24 +23,24 @@ import android.content.Context; import android.net.MailTo; import android.net.ParseException; import android.net.Uri; +import android.util.Pair; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HttpsURLConnection; public class IPInfo { - private static Map hostOrganization = new HashMap<>(); + private static Map addressOrganization = new HashMap<>(); private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds - static String[] getOrganization(Uri uri, Context context) throws IOException, ParseException { + static Pair getOrganization(Uri uri, Context context) throws IOException, ParseException { if ("mailto".equals(uri.getScheme())) { MailTo email = MailTo.parse(uri.toString()); String to = email.getTo(); @@ -50,35 +50,48 @@ public class IPInfo { InetAddress address = ConnectionHelper.lookupMx(domain, context); if (address == null) throw new UnknownHostException(); - return new String[]{domain, getOrganization(address)}; + return new Pair<>(domain, getOrganization(address)); } else { String host = uri.getHost(); if (host == null) throw new UnknownHostException(); InetAddress address = InetAddress.getByName(host); - return new String[]{host, getOrganization(address)}; + return new Pair<>(host, getOrganization(address)); } } - private static String getOrganization(InetAddress address) throws IOException { - synchronized (hostOrganization) { - if (hostOrganization.containsKey(address)) - return hostOrganization.get(address); + private static Organization getOrganization(InetAddress address) throws IOException { + synchronized (addressOrganization) { + if (addressOrganization.containsKey(address)) + return addressOrganization.get(address); } + + // https://ipinfo.io/developers URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org"); Log.i("GET " + url); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(FETCH_TIMEOUT); connection.connect(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { - String organization = reader.readLine(); - if ("undefined".equals(organization)) - organization = null; - synchronized (hostOrganization) { - hostOrganization.put(address, organization); - } - return organization; + + Organization organization = new Organization(); + try { + String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name()); + organization.name = response.trim(); + if ("".equals(organization.name) || "undefined".equals(organization.name)) + organization.name = null; + } finally { + connection.disconnect(); } + + synchronized (addressOrganization) { + addressOrganization.put(address, organization); + } + + return organization; + } + + static class Organization { + String name; } }