diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java b/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java new file mode 100644 index 000000000..1c0fca8bb --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/AndroidUsingLinkProperties.java @@ -0,0 +1,91 @@ +package de.pixart.messenger.utils; + +import android.annotation.TargetApi; +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkInfo; +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); + List<String> servers = new ArrayList<>(); + int vpnOffset = 0; + for (Network network : networks) { + LinkProperties linkProperties = connectivityManager.getLinkProperties(network); + if (linkProperties == null) { + continue; + } + NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); + final boolean isActiveNetwork = network.equals(activeNetwork); + if (isActiveNetwork) { + if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_VPN) { + final List<String> tmp = getIPv4First(linkProperties.getDnsServers()); + servers.addAll(0, tmp); + vpnOffset += tmp.size(); + } else { + servers.addAll(vpnOffset, 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; + } + + private static List<String> getIPv4First(List<InetAddress> in) { + List<String> out = new ArrayList<>(); + for (InetAddress addr : in) { + if (addr instanceof Inet4Address) { + out.add(0, addr.getHostAddress()); + } else { + out.add(addr.getHostAddress()); + } + } + 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; + } +}
\ No newline at end of file |