From 6499a14c7ba6c64a7f4821e846a725f7ed9da949 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 15 Jul 2019 16:53:47 +0200 Subject: [PATCH] Get DNS server for older Android versions --- .../eu/faircode/email/ConnectionHelper.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index ea98077822..9e9e9f1f43 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -275,17 +275,32 @@ public class ConnectionHelper { } static String getDnsServer(Context context) { - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) - return DEFAULT_DNS; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return DEFAULT_DNS; - Network active = cm.getActiveNetwork(); - if (active == null) - return DEFAULT_DNS; - LinkProperties props = cm.getLinkProperties(active); + + 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 dns = props.getDnsServers(); if (dns.size() == 0) return DEFAULT_DNS;