mirror of
https://github.com/M66B/FairEmail.git
synced 2025-03-04 02:28:18 +00:00
Refactoring: IPInfo
This commit is contained in:
parent
d77997a73b
commit
fc480e80b4
3 changed files with 39 additions and 30 deletions
|
@ -1968,7 +1968,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
|
||||||
protected String onExecute(Context context, Bundle args) throws Throwable {
|
protected String onExecute(Context context, Bundle args) throws Throwable {
|
||||||
Uri uri = args.getParcelable("uri");
|
Uri uri = args.getParcelable("uri");
|
||||||
String host = uri.getHost();
|
String host = uri.getHost();
|
||||||
return (TextUtils.isEmpty(host) ? null : Helper.getOrganization(host));
|
return (TextUtils.isEmpty(host) ? null : IPInfo.getOrganization(host));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -83,8 +83,6 @@ import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
@ -93,7 +91,6 @@ import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -105,14 +102,11 @@ import javax.mail.Address;
|
||||||
import javax.mail.FolderClosedException;
|
import javax.mail.FolderClosedException;
|
||||||
import javax.mail.MessageRemovedException;
|
import javax.mail.MessageRemovedException;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
|
||||||
|
|
||||||
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
||||||
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
|
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
|
||||||
|
|
||||||
public class Helper {
|
public class Helper {
|
||||||
private static Map<String, String> hostOrganization = new HashMap<>();
|
|
||||||
|
|
||||||
static final int NOTIFICATION_SYNCHRONIZE = 1;
|
static final int NOTIFICATION_SYNCHRONIZE = 1;
|
||||||
static final int NOTIFICATION_SEND = 2;
|
static final int NOTIFICATION_SEND = 2;
|
||||||
static final int NOTIFICATION_EXTERNAL = 3;
|
static final int NOTIFICATION_EXTERNAL = 3;
|
||||||
|
@ -896,29 +890,6 @@ public class Helper {
|
||||||
return (name == null ? null : name.replaceAll("[^a-zA-Z0-9\\.\\-]", "_"));
|
return (name == null ? null : name.replaceAll("[^a-zA-Z0-9\\.\\-]", "_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getOrganization(String host) throws IOException {
|
|
||||||
synchronized (hostOrganization) {
|
|
||||||
if (hostOrganization.containsKey(host))
|
|
||||||
return hostOrganization.get(host);
|
|
||||||
}
|
|
||||||
InetAddress address = InetAddress.getByName(host);
|
|
||||||
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
|
|
||||||
Log.i("GET " + url);
|
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
|
||||||
connection.setRequestMethod("GET");
|
|
||||||
connection.setReadTimeout(15 * 1000);
|
|
||||||
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(host, organization);
|
|
||||||
}
|
|
||||||
return organization;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getSize(Bundle bundle) {
|
static int getSize(Bundle bundle) {
|
||||||
Parcel p = Parcel.obtain();
|
Parcel p = Parcel.obtain();
|
||||||
bundle.writeToParcel(p, 0);
|
bundle.writeToParcel(p, 0);
|
||||||
|
|
38
app/src/main/java/eu/faircode/email/IPInfo.java
Normal file
38
app/src/main/java/eu/faircode/email/IPInfo.java
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package eu.faircode.email;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
|
public class IPInfo {
|
||||||
|
private static Map<String, String> hostOrganization = new HashMap<>();
|
||||||
|
|
||||||
|
static String getOrganization(String host) throws IOException {
|
||||||
|
synchronized (hostOrganization) {
|
||||||
|
if (hostOrganization.containsKey(host))
|
||||||
|
return hostOrganization.get(host);
|
||||||
|
}
|
||||||
|
InetAddress address = InetAddress.getByName(host);
|
||||||
|
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
|
||||||
|
Log.i("GET " + url);
|
||||||
|
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("GET");
|
||||||
|
connection.setReadTimeout(15 * 1000);
|
||||||
|
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(host, organization);
|
||||||
|
}
|
||||||
|
return organization;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue