Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0432ab44 authored by Irfan Sheriff's avatar Irfan Sheriff Committed by Android (Google) Code Review
Browse files

Merge "Fix wifi watchdog to use InetAddress"

parents 06f992c6 8e9abc5b
Loading
Loading
Loading
Loading
+51 −47
Original line number Diff line number Diff line
@@ -22,8 +22,9 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.NetworkInfo;
import android.net.DhcpInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
@@ -42,6 +43,7 @@ import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.List;
import java.util.Random;

@@ -77,6 +79,7 @@ public class WifiWatchdogService {
    private Context mContext;
    private ContentResolver mContentResolver;
    private WifiManager mWifiManager;
    private ConnectivityManager mConnectivityManager;

    /**
     * The main watchdog thread.
@@ -310,17 +313,24 @@ public class WifiWatchdogService {
    }
    
    /**
     * Gets the DNS of the current AP.
     * Gets the first DNS of the current AP.
     * 
     * @return The DNS of the current AP.
     * @return The first DNS of the current AP.
     */
    private int getDns() {
        DhcpInfo addressInfo = mWifiManager.getDhcpInfo();
        if (addressInfo != null) {
            return addressInfo.dns1;
        } else {
            return -1;
    private InetAddress getDns() {
        if (mConnectivityManager == null) {
            mConnectivityManager = (ConnectivityManager)mContext.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
        }

        LinkProperties linkProperties = mConnectivityManager.getLinkProperties(
                ConnectivityManager.TYPE_WIFI);
        if (linkProperties == null) return null;

        Collection<InetAddress> dnses = linkProperties.getDnses();
        if (dnses == null || dnses.size() == 0) return null;

        return dnses.iterator().next();
    }

    /**
@@ -330,8 +340,8 @@ public class WifiWatchdogService {
     * @return Whether the DNS is reachable
     */
    private boolean checkDnsConnectivity() {
        int dns = getDns();
        if (dns == -1) {
        InetAddress dns = getDns();
        if (dns == null) {
            if (V) {
                myLogV("checkDnsConnectivity: Invalid DNS, returning false");
            }
@@ -339,8 +349,7 @@ public class WifiWatchdogService {
        }

        if (V) {
            myLogV("checkDnsConnectivity: Checking 0x" +
                    Integer.toHexString(Integer.reverseBytes(dns)) + " for connectivity");
            myLogV("checkDnsConnectivity: Checking " + dns.getHostAddress() + " for connectivity");
        }

        int numInitialIgnoredPings = getInitialIgnoredPingCount();
@@ -419,19 +428,20 @@ public class WifiWatchdogService {
    }

    private boolean backgroundCheckDnsConnectivity() {
        int dns = getDns();
        if (false && V) {
            myLogV("backgroundCheckDnsConnectivity: Background checking " + dns +
                    " for connectivity");
        }
        InetAddress dns = getDns();

        if (dns == -1) {
        if (dns == null) {
            if (V) {
                myLogV("backgroundCheckDnsConnectivity: DNS is empty, returning false");
            }
            return false;
        }

        if (false && V) {
            myLogV("backgroundCheckDnsConnectivity: Background checking " +
                    dns.getHostAddress() + " for connectivity");
        }

        return DnsPinger.isDnsReachable(dns, getBackgroundCheckTimeoutMs());
    }

@@ -1208,7 +1218,7 @@ public class WifiWatchdogService {
        /** Used to generate IDs */
        private static Random sRandom = new Random();

        static boolean isDnsReachable(int dns, int timeout) {
        static boolean isDnsReachable(InetAddress dnsAddress, int timeout) {
            DatagramSocket socket = null;
            try {
                socket = new DatagramSocket();
@@ -1220,13 +1230,7 @@ public class WifiWatchdogService {
                fillQuery(buf);

                // Send the DNS query
                byte parts[] = new byte[4];
                parts[0] = (byte)(dns & 0xff);
                parts[1] = (byte)((dns >> 8) & 0xff);
                parts[2] = (byte)((dns >> 16) & 0xff);
                parts[3] = (byte)((dns >> 24) & 0xff);

                InetAddress dnsAddress = InetAddress.getByAddress(parts);
                DatagramPacket packet = new DatagramPacket(buf,
                        buf.length, dnsAddress, DNS_PORT);
                socket.send(packet);