conversations-classic/src/main/java/eu/siacs/conversations/utils/AndroidUsingLinkProperties.java

91 lines
3.3 KiB
Java
Raw Normal View History

2017-06-21 21:28:01 +00:00
package eu.siacs.conversations.utils;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
2017-07-12 20:19:45 +00:00
import android.net.NetworkInfo;
2017-06-21 21:28:01 +00:00
import android.net.RouteInfo;
import android.os.Build;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import de.measite.minidns.dnsserverlookup.AbstractDNSServerLookupMechanism;
import de.measite.minidns.dnsserverlookup.AndroidUsingExec;
public class AndroidUsingLinkProperties extends AbstractDNSServerLookupMechanism {
private final Context context;
protected AndroidUsingLinkProperties(Context context) {
super(AndroidUsingLinkProperties.class.getSimpleName(), AndroidUsingExec.PRIORITY - 1);
this.context = context;
}
@Override
public boolean isAvailable() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
@Override
@TargetApi(21)
public String[] getDnsServerAddresses() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks();
if (networks == null) {
return new String[0];
}
final Network activeNetwork = getActiveNetwork(connectivityManager);
2017-06-21 21:28:01 +00:00
List<String> servers = new ArrayList<>();
2017-07-12 20:19:45 +00:00
int vpnOffset = 0;
2017-06-21 21:28:01 +00:00
for(Network network : networks) {
LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
2017-07-12 20:19:45 +00:00
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
final boolean isActiveNetwork = network.equals(activeNetwork);
2017-06-21 21:28:01 +00:00
if (linkProperties != null) {
if (networkInfo != null && isActiveNetwork && networkInfo.getType() == ConnectivityManager.TYPE_VPN) {
2017-07-12 20:19:45 +00:00
final List<String> tmp = getIPv4First(linkProperties.getDnsServers());
servers.addAll(0, tmp);
vpnOffset += tmp.size();
} else if (hasDefaultRoute(linkProperties)) {
servers.addAll(vpnOffset, getIPv4First(linkProperties.getDnsServers()));
2017-06-21 21:28:01 +00:00
} else {
servers.addAll(getIPv4First(linkProperties.getDnsServers()));
}
}
}
return servers.toArray(new String[servers.size()]);
}
@TargetApi(23)
private static Network getActiveNetwork(ConnectivityManager cm) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? cm.getActiveNetwork() : null;
}
2017-06-21 21:28:01 +00:00
private static List<String> getIPv4First(List<InetAddress> in) {
List<String> out = new ArrayList<>();
for(InetAddress addr : in) {
if (addr instanceof Inet4Address) {
2017-06-21 22:09:59 +00:00
out.add(0, addr.getHostAddress());
2017-06-21 21:28:01 +00:00
} else {
2017-06-21 22:09:59 +00:00
out.add(addr.getHostAddress());
2017-06-21 21:28:01 +00:00
}
}
return out;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean hasDefaultRoute(LinkProperties linkProperties) {
for(RouteInfo route: linkProperties.getRoutes()) {
if (route.isDefaultRoute()) {
return true;
}
}
return false;
}
}