Remove captive portal code that has no effect.

Note that this CL does not change any behaviour.

At the center of this change is
CaptivePortalTracker#detectCaptivePortal(), which does nothing
except call back into ConnectivityService. Removing it allows us to
simplify code in ConnectivityService. It also allows us to remove
ConnectivityService#captivePortalCheckComplete which was only ever
called in response to this method.

While this does not change any behaviour, it preserves existing
bad behaviour, i.e, that the CAPTIVE_PORTAL_CHECK NetworkInfo
state does not correspond to actual captive portal detection.
We transition into that state and immediately (and unconditionally)
out of it and into CONNECTED.

Change-Id: Ib3797f956d2db5e3cacaaa53e899d81aa8e958af
diff --git a/core/java/android/bluetooth/BluetoothTetheringDataTracker.java b/core/java/android/bluetooth/BluetoothTetheringDataTracker.java
index a9b7176..ca7749d 100644
--- a/core/java/android/bluetooth/BluetoothTetheringDataTracker.java
+++ b/core/java/android/bluetooth/BluetoothTetheringDataTracker.java
@@ -143,11 +143,6 @@
     }
 
     @Override
-    public void captivePortalCheckComplete() {
-        // not implemented
-    }
-
-    @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         // not implemented
     }
diff --git a/core/java/android/net/BaseNetworkStateTracker.java b/core/java/android/net/BaseNetworkStateTracker.java
index 476fefe..804f8ee 100644
--- a/core/java/android/net/BaseNetworkStateTracker.java
+++ b/core/java/android/net/BaseNetworkStateTracker.java
@@ -20,10 +20,10 @@
 import android.os.Handler;
 import android.os.Messenger;
 
-import com.android.internal.util.Preconditions;
-
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import com.android.internal.util.Preconditions;
+
 /**
  * Interface to control and observe state of a specific network, hiding
  * network-specific details from {@link ConnectivityManager}. Surfaces events
@@ -108,11 +108,6 @@
     }
 
     @Override
-    public void captivePortalCheckComplete() {
-        // not implemented
-    }
-
-    @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         // not implemented
     }
diff --git a/core/java/android/net/CaptivePortalTracker.java b/core/java/android/net/CaptivePortalTracker.java
index d678f1e..31b207c 100644
--- a/core/java/android/net/CaptivePortalTracker.java
+++ b/core/java/android/net/CaptivePortalTracker.java
@@ -84,13 +84,12 @@
     private String mServer;
     private String mUrl;
     private boolean mIsCaptivePortalCheckEnabled = false;
-    private IConnectivityManager mConnService;
-    private TelephonyManager mTelephonyManager;
-    private WifiManager mWifiManager;
-    private Context mContext;
+    private final IConnectivityManager mConnService;
+    private final TelephonyManager mTelephonyManager;
+    private final WifiManager mWifiManager;
+    private final Context mContext;
     private NetworkInfo mNetworkInfo;
 
-    private static final int CMD_DETECT_PORTAL          = 0;
     private static final int CMD_CONNECTIVITY_CHANGE    = 1;
     private static final int CMD_DELAYED_CAPTIVE_CHECK  = 2;
 
@@ -98,14 +97,15 @@
     private static final int DELAYED_CHECK_INTERVAL_MS = 10000;
     private int mDelayedCheckToken = 0;
 
-    private State mDefaultState = new DefaultState();
-    private State mNoActiveNetworkState = new NoActiveNetworkState();
-    private State mActiveNetworkState = new ActiveNetworkState();
-    private State mDelayedCaptiveCheckState = new DelayedCaptiveCheckState();
+    private final State mDefaultState = new DefaultState();
+    private final State mNoActiveNetworkState = new NoActiveNetworkState();
+    private final State mActiveNetworkState = new ActiveNetworkState();
+    private final State mDelayedCaptiveCheckState = new DelayedCaptiveCheckState();
 
     private static final String SETUP_WIZARD_PACKAGE = "com.google.android.setupwizard";
     private boolean mDeviceProvisioned = false;
-    private ProvisioningObserver mProvisioningObserver;
+    @SuppressWarnings("unused")
+    private final ProvisioningObserver mProvisioningObserver;
 
     private CaptivePortalTracker(Context context, IConnectivityManager cs) {
         super(TAG);
@@ -174,29 +174,11 @@
         return captivePortal;
     }
 
-    public void detectCaptivePortal(NetworkInfo info) {
-        sendMessage(obtainMessage(CMD_DETECT_PORTAL, info));
-    }
-
     private class DefaultState extends State {
 
         @Override
         public boolean processMessage(Message message) {
-            if (DBG) log(getName() + message.toString());
-            switch (message.what) {
-                case CMD_DETECT_PORTAL:
-                    NetworkInfo info = (NetworkInfo) message.obj;
-                    // Checking on a secondary connection is not supported
-                    // yet
-                    notifyPortalCheckComplete(info);
-                    break;
-                case CMD_CONNECTIVITY_CHANGE:
-                case CMD_DELAYED_CAPTIVE_CHECK:
-                    break;
-                default:
-                    loge("Ignoring " + message);
-                    break;
-            }
+            loge("Ignoring " + message);
             return HANDLED;
         }
     }
@@ -316,19 +298,6 @@
         }
     }
 
-    private void notifyPortalCheckComplete(NetworkInfo info) {
-        if (info == null) {
-            loge("notifyPortalCheckComplete on null");
-            return;
-        }
-        try {
-            if (DBG) log("notifyPortalCheckComplete: ni=" + info);
-            mConnService.captivePortalCheckComplete(info);
-        } catch(RemoteException e) {
-            e.printStackTrace();
-        }
-    }
-
     private void notifyPortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
         if (info == null) {
             loge("notifyPortalCheckComplete on null");
@@ -464,7 +433,6 @@
                 latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType());
                 List<CellInfo> info = mTelephonyManager.getAllCellInfo();
                 if (info == null) return;
-                StringBuffer uniqueCellId = new StringBuffer();
                 int numRegisteredCellInfo = 0;
                 for (CellInfo cellInfo : info) {
                     if (cellInfo.isRegistered()) {
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index c78a973..0f8dc7a 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -1330,24 +1330,6 @@
 
     /**
      * Signal that the captive portal check on the indicated network
-     * is complete and we can turn the network on for general use.
-     *
-     * @param info the {@link NetworkInfo} object for the networkType
-     *        in question.
-     *
-     * <p>This method requires the call to hold the permission
-     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
-     * {@hide}
-     */
-    public void captivePortalCheckComplete(NetworkInfo info) {
-        try {
-            mService.captivePortalCheckComplete(info);
-        } catch (RemoteException e) {
-        }
-    }
-
-    /**
-     * Signal that the captive portal check on the indicated network
      * is complete and whether its a captive portal or not.
      *
      * @param info the {@link NetworkInfo} object for the networkType
diff --git a/core/java/android/net/DummyDataStateTracker.java b/core/java/android/net/DummyDataStateTracker.java
index 51a1191..a5d059e 100644
--- a/core/java/android/net/DummyDataStateTracker.java
+++ b/core/java/android/net/DummyDataStateTracker.java
@@ -117,11 +117,6 @@
     }
 
     @Override
-    public void captivePortalCheckComplete() {
-        // not implemented
-    }
-
-    @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         // not implemented
     }
diff --git a/core/java/android/net/EthernetDataTracker.java b/core/java/android/net/EthernetDataTracker.java
index 501484c..c7e98c5 100644
--- a/core/java/android/net/EthernetDataTracker.java
+++ b/core/java/android/net/EthernetDataTracker.java
@@ -270,11 +270,6 @@
     }
 
     @Override
-    public void captivePortalCheckComplete() {
-        // not implemented
-    }
-
-    @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         // not implemented
     }
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index c1da2e3..b3217eb 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -129,8 +129,6 @@
 
     boolean updateLockdownVpn();
 
-    void captivePortalCheckComplete(in NetworkInfo info);
-
     void captivePortalCheckCompleted(in NetworkInfo info, boolean isCaptivePortal);
 
     void supplyMessenger(int networkType, in Messenger messenger);
diff --git a/core/java/android/net/MobileDataStateTracker.java b/core/java/android/net/MobileDataStateTracker.java
index c106514..d9c35c0 100644
--- a/core/java/android/net/MobileDataStateTracker.java
+++ b/core/java/android/net/MobileDataStateTracker.java
@@ -460,11 +460,6 @@
     }
 
     @Override
-    public void captivePortalCheckComplete() {
-        // not implemented
-    }
-
-    @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         if (mIsCaptivePortal.getAndSet(isCaptivePortal) != isCaptivePortal) {
             // Captive portal change enable/disable failing fast
diff --git a/core/java/android/net/NetworkStateTracker.java b/core/java/android/net/NetworkStateTracker.java
index 1ca9255..c49b1d1 100644
--- a/core/java/android/net/NetworkStateTracker.java
+++ b/core/java/android/net/NetworkStateTracker.java
@@ -144,11 +144,6 @@
     public boolean reconnect();
 
     /**
-     * Ready to switch on to the network after captive portal check
-     */
-    public void captivePortalCheckComplete();
-
-    /**
      * Captive portal check has completed
      */
     public void captivePortalCheckCompleted(boolean isCaptive);
diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java
index b5f0697..5418b3b 100644
--- a/services/java/com/android/server/ConnectivityService.java
+++ b/services/java/com/android/server/ConnectivityService.java
@@ -2316,36 +2316,6 @@
         }
     }
 
-    private void handleCaptivePortalTrackerCheck(NetworkInfo info) {
-        if (DBG) log("Captive portal check " + info);
-        int type = info.getType();
-        final NetworkStateTracker thisNet = mNetTrackers[type];
-        if (mNetConfigs[type].isDefault()) {
-            if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
-                if (isNewNetTypePreferredOverCurrentNetType(type)) {
-                    if (DBG) log("Captive check on " + info.getTypeName());
-                    mCaptivePortalTracker.detectCaptivePortal(new NetworkInfo(info));
-                    return;
-                } else {
-                    if (DBG) log("Tear down low priority net " + info.getTypeName());
-                    teardown(thisNet);
-                    return;
-                }
-            }
-        }
-
-        if (DBG) log("handleCaptivePortalTrackerCheck: call captivePortalCheckComplete ni=" + info);
-        thisNet.captivePortalCheckComplete();
-    }
-
-    /** @hide */
-    @Override
-    public void captivePortalCheckComplete(NetworkInfo info) {
-        enforceConnectivityInternalPermission();
-        if (DBG) log("captivePortalCheckComplete: ni=" + info);
-        mNetTrackers[info.getType()].captivePortalCheckComplete();
-    }
-
     /** @hide */
     @Override
     public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
@@ -2972,9 +2942,6 @@
                     if (info.getDetailedState() ==
                             NetworkInfo.DetailedState.FAILED) {
                         handleConnectionFailure(info);
-                    } else if (info.getDetailedState() ==
-                            DetailedState.CAPTIVE_PORTAL_CHECK) {
-                        handleCaptivePortalTrackerCheck(info);
                     } else if (info.isConnectedToProvisioningNetwork()) {
                         /**
                          * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING
diff --git a/services/java/com/android/server/wifi/WifiService.java b/services/java/com/android/server/wifi/WifiService.java
index 86c68f3..0854883 100644
--- a/services/java/com/android/server/wifi/WifiService.java
+++ b/services/java/com/android/server/wifi/WifiService.java
@@ -930,11 +930,6 @@
         mWifiStateMachine.reconnectCommand();
     }
 
-    public void captivePortalCheckComplete() {
-        enforceConnectivityInternalPermission();
-        mWifiStateMachine.captivePortalCheckComplete();
-    }
-
     /**
      * see {@link android.net.wifi.WifiManager#stopWifi}
      *
diff --git a/wifi/java/android/net/wifi/IWifiManager.aidl b/wifi/java/android/net/wifi/IWifiManager.aidl
index 5a1928c..bfcc208 100644
--- a/wifi/java/android/net/wifi/IWifiManager.aidl
+++ b/wifi/java/android/net/wifi/IWifiManager.aidl
@@ -111,8 +111,6 @@
 
     String getConfigFile();
 
-    void captivePortalCheckComplete();
-
     void enableTdls(String remoteIPAddress, boolean enable);
 
     void enableTdlsWithMacAddress(String remoteMacAddress, boolean enable);
diff --git a/wifi/java/android/net/wifi/WifiManager.java b/wifi/java/android/net/wifi/WifiManager.java
index 7fc8bef..1fa7e85 100644
--- a/wifi/java/android/net/wifi/WifiManager.java
+++ b/wifi/java/android/net/wifi/WifiManager.java
@@ -2157,13 +2157,6 @@
         }
     }
 
-    /** @hide */
-    public void captivePortalCheckComplete() {
-        try {
-            mService.captivePortalCheckComplete();
-        } catch (RemoteException e) {}
-    }
-
     protected void finalize() throws Throwable {
         try {
             synchronized (sThreadRefLock) {
diff --git a/wifi/java/android/net/wifi/WifiStateMachine.java b/wifi/java/android/net/wifi/WifiStateMachine.java
index 4bc3dda..f877c48 100644
--- a/wifi/java/android/net/wifi/WifiStateMachine.java
+++ b/wifi/java/android/net/wifi/WifiStateMachine.java
@@ -56,13 +56,11 @@
 import android.net.wifi.p2p.WifiP2pManager;
 import android.net.wifi.p2p.WifiP2pService;
 import android.os.BatteryStats;
-import android.os.Binder;
 import android.os.IBinder;
 import android.os.INetworkManagementService;
 import android.os.Message;
 import android.os.Messenger;
 import android.os.PowerManager;
-import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
@@ -70,7 +68,6 @@
 import android.os.UserHandle;
 import android.os.WorkSource;
 import android.provider.Settings;
-import android.util.Log;
 import android.util.LruCache;
 import android.text.TextUtils;
 
@@ -86,7 +83,6 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.net.InetAddress;
-import java.net.Inet6Address;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -303,8 +299,6 @@
     static final int CMD_DELAYED_STOP_DRIVER              = BASE + 18;
     /* A delayed message sent to start driver when it fail to come up */
     static final int CMD_DRIVER_START_TIMED_OUT           = BASE + 19;
-    /* Ready to switch to network as default */
-    static final int CMD_CAPTIVE_CHECK_COMPLETE           = BASE + 20;
 
     /* Start the soft access point */
     static final int CMD_START_AP                         = BASE + 21;
@@ -536,8 +530,6 @@
     private State mObtainingIpState = new ObtainingIpState();
     /* Waiting for link quality verification to be complete */
     private State mVerifyingLinkState = new VerifyingLinkState();
-    /* Waiting for captive portal check to be complete */
-    private State mCaptivePortalCheckState = new CaptivePortalCheckState();
     /* Connected with IP addr */
     private State mConnectedState = new ConnectedState();
     /* disconnect issued, waiting for network disconnect confirmation */
@@ -774,7 +766,6 @@
                         addState(mL2ConnectedState, mConnectModeState);
                             addState(mObtainingIpState, mL2ConnectedState);
                             addState(mVerifyingLinkState, mL2ConnectedState);
-                            addState(mCaptivePortalCheckState, mL2ConnectedState);
                             addState(mConnectedState, mL2ConnectedState);
                         addState(mDisconnectingState, mConnectModeState);
                         addState(mDisconnectedState, mConnectModeState);
@@ -1277,10 +1268,6 @@
         }
     }
 
-    public void captivePortalCheckComplete() {
-        sendMessage(CMD_CAPTIVE_CHECK_COMPLETE);
-    }
-
     /**
      * TODO: doc
      */
@@ -2459,7 +2446,6 @@
                 case CMD_STOP_DRIVER:
                 case CMD_DELAYED_STOP_DRIVER:
                 case CMD_DRIVER_START_TIMED_OUT:
-                case CMD_CAPTIVE_CHECK_COMPLETE:
                 case CMD_START_AP:
                 case CMD_START_AP_SUCCESS:
                 case CMD_START_AP_FAILURE:
@@ -3731,29 +3717,17 @@
                     break;
                 case WifiWatchdogStateMachine.GOOD_LINK_DETECTED:
                     log(getName() + " GOOD_LINK_DETECTED: transition to captive portal check");
-                    transitionTo(mCaptivePortalCheckState);
-                    break;
-                default:
-                    if (DBG) log(getName() + " what=" + message.what + " NOT_HANDLED");
-                    return NOT_HANDLED;
-            }
-            return HANDLED;
-        }
-    }
+                    // Send out a broadcast with the CAPTIVE_PORTAL_CHECK to preserve
+                    // existing behaviour. The captive portal check really happens after we
+                    // transition into DetailedState.CONNECTED.
+                    setNetworkDetailedState(DetailedState.CAPTIVE_PORTAL_CHECK);
+                    mWifiConfigStore.updateStatus(mLastNetworkId,
+                            DetailedState.CAPTIVE_PORTAL_CHECK);
+                    sendNetworkStateChangeBroadcast(mLastBssid);
 
-    class CaptivePortalCheckState extends State {
-        @Override
-        public void enter() {
-            log(getName() + " enter");
-            setNetworkDetailedState(DetailedState.CAPTIVE_PORTAL_CHECK);
-            mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.CAPTIVE_PORTAL_CHECK);
-            sendNetworkStateChangeBroadcast(mLastBssid);
-        }
-        @Override
-        public boolean processMessage(Message message) {
-            switch (message.what) {
-                case CMD_CAPTIVE_CHECK_COMPLETE:
-                    log(getName() + " CMD_CAPTIVE_CHECK_COMPLETE");
+                    // NOTE: This might look like an odd place to enable IPV6 but this is in
+                    // response to transitioning into GOOD_LINK_DETECTED. Similarly, we disable
+                    // ipv6 when we transition into POOR_LINK_DETECTED in mConnectedState.
                     try {
                         mNwService.enableIpv6(mInterfaceName);
                     } catch (RemoteException re) {
@@ -3761,12 +3735,16 @@
                     } catch (IllegalStateException e) {
                         loge("Failed to enable IPv6: " + e);
                     }
+
+                    log(getName() + " GOOD_LINK_DETECTED: transition to CONNECTED");
                     setNetworkDetailedState(DetailedState.CONNECTED);
                     mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.CONNECTED);
                     sendNetworkStateChangeBroadcast(mLastBssid);
                     transitionTo(mConnectedState);
+
                     break;
                 default:
+                    if (DBG) log(getName() + " what=" + message.what + " NOT_HANDLED");
                     return NOT_HANDLED;
             }
             return HANDLED;
@@ -3798,6 +3776,7 @@
             }
             return HANDLED;
         }
+
         @Override
         public void exit() {
             /* Request a CS wakelock during transition to mobile */
diff --git a/wifi/java/android/net/wifi/WifiStateTracker.java b/wifi/java/android/net/wifi/WifiStateTracker.java
index 615c893..7ded171 100644
--- a/wifi/java/android/net/wifi/WifiStateTracker.java
+++ b/wifi/java/android/net/wifi/WifiStateTracker.java
@@ -115,14 +115,6 @@
         return true;
     }
 
-    /**
-     * Captive check is complete, switch to network
-     */
-    @Override
-    public void captivePortalCheckComplete() {
-        mWifiManager.captivePortalCheckComplete();
-    }
-
     @Override
     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
         // not implemented