Refactoring

This commit is contained in:
M66B
2020-04-15 20:08:17 +02:00
parent c239a1245d
commit f1e9b23a96
8 changed files with 209 additions and 159 deletions

View File

@@ -1,10 +1,28 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.accounts.AccountsException;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
@@ -17,27 +35,13 @@ import androidx.preference.PreferenceManager;
import com.sun.mail.iap.ConnectionException;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.Type;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
public class ConnectionHelper {
// https://dns.watch/
private static final String DEFAULT_DNS = "84.200.69.80";
// Roam like at home
// https://en.wikipedia.org/wiki/European_Union_roaming_regulations
private static final List<String> RLAH_COUNTRY_CODES = Collections.unmodifiableList(Arrays.asList(
@@ -332,106 +336,4 @@ public class ConnectionHelper {
return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
static String getDnsServer(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return DEFAULT_DNS;
LinkProperties props = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
for (Network network : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni != null && ni.isConnected()) {
props = cm.getLinkProperties(network);
Log.i("Old props=" + props);
break;
}
}
else {
Network active = cm.getActiveNetwork();
if (active == null)
return DEFAULT_DNS;
props = cm.getLinkProperties(active);
Log.i("New props=" + props);
}
if (props == null)
return DEFAULT_DNS;
List<InetAddress> dns = props.getDnsServers();
if (dns.size() == 0)
return DEFAULT_DNS;
else
return dns.get(0).getHostAddress();
}
static boolean lookupMx(Address[] addresses, Context context) throws UnknownHostException {
boolean ok = true;
if (addresses != null)
for (Address address : addresses)
try {
String email = ((InternetAddress) address).getAddress();
if (email == null)
continue;
int d = email.lastIndexOf("@");
if (d < 0)
continue;
String domain = email.substring(d + 1);
Lookup lookup = new Lookup(domain, Type.MX);
SimpleResolver resolver = new SimpleResolver(ConnectionHelper.getDnsServer(context));
lookup.setResolver(resolver);
Log.i("Lookup MX=" + domain + " @" + resolver.getAddress());
lookup.run();
if (lookup.getResult() == Lookup.HOST_NOT_FOUND ||
lookup.getResult() == Lookup.TYPE_NOT_FOUND) {
Log.i("Lookup MX=" + domain + " result=" + lookup.getErrorString());
throw new UnknownHostException(context.getString(R.string.title_no_server, domain));
} else if (lookup.getResult() != Lookup.SUCCESSFUL)
ok = false;
} catch (UnknownHostException ex) {
throw ex;
} catch (Throwable ex) {
Log.e(ex);
ok = false;
}
return ok;
}
static InetAddress lookupMx(String domain, Context context) {
try {
Lookup lookup = new Lookup(domain, Type.MX);
SimpleResolver resolver = new SimpleResolver(getDnsServer(context));
lookup.setResolver(resolver);
Log.i("Lookup MX=" + domain + " @" + resolver.getAddress());
lookup.run();
if (lookup.getResult() == Lookup.SUCCESSFUL) {
Record[] answers = lookup.getAnswers();
if (answers != null && answers.length > 0 && answers[0] instanceof MXRecord) {
MXRecord mx = (MXRecord) answers[0];
return InetAddress.getByName(mx.getTarget().toString(true));
}
}
} catch (Throwable ex) {
Log.w(ex);
}
return null;
}
static String getParentDomain(String host) {
if (host != null) {
String[] h = host.split("\\.");
if (h.length >= 2)
return h[h.length - 2] + "." + h[h.length - 1];
}
return host;
}
}