Make a network the default when it connects if we have no default.

When a network comes online, is a candidate for being the default network
(i.e. satisfies default NetworkRequest), and the device has no default
network, then make the new network the default network for the purposes
of routing network traffic.  This does not affect NetworkRequests or
NetworkCallbacks.  This ignores but does not affect network validation.

Benefits:
1. Offers a fail-safe in case network validation returns a false negative.
   For example:  It would be nice if every Android device didn't fail when
   clients3.google.com/generate_204 went down.
2. Offers a method to debug connectivity issues.
   For example:  If WiFi is failing, disabling Cellular would rule out
   interference from WiFi network validation.
3. Reduces delay between no connectivity and any connectivity.
4. Offers a fail-safe in cases of unreliable networks.
   For example: You need rescuing from a remote location with a weak signal
   offering 90% packet loss.  You just want your distress call to go out
   but are infuriated to find network validation blocks connectivity.

Change-Id: I78621a1fe8ed2a336591f65bf7b07a6cbcc7ba5e
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 86f8777..9945909 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -5720,6 +5720,17 @@
         oldNetwork.asyncChannel.disconnect();
     }
 
+    private void makeDefault(NetworkAgentInfo newNetwork) {
+        if (VDBG) log("Switching to new default network: " + newNetwork);
+        setupDataActivityTracking(newNetwork);
+        try {
+            mNetd.setDefaultNetId(newNetwork.network.netId);
+        } catch (Exception e) {
+            loge("Exception setting default network :" + e);
+        }
+        handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy());
+    }
+
     private void handleConnectionValidated(NetworkAgentInfo newNetwork) {
         if (newNetwork == null) {
             loge("Unknown NetworkAgentInfo in handleConnectionValidated");
@@ -5813,16 +5824,7 @@
         }
         if (keep) {
             if (isNewDefault) {
-                if (VDBG) log("Switching to new default network: " + newNetwork);
-                setupDataActivityTracking(newNetwork);
-                try {
-                    mNetd.setDefaultNetId(newNetwork.network.netId);
-                } catch (Exception e) {
-                    loge("Exception setting default network :" + e);
-                }
-                if (newNetwork.equals(mNetworkForRequestId.get(mDefaultRequest.requestId))) {
-                    handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy());
-                }
+                makeDefault(newNetwork);
                 synchronized (ConnectivityService.this) {
                     // have a new default network, release the transition wakelock in
                     // a second if it's held.  The second pause is to allow apps
@@ -5919,6 +5921,13 @@
                 }
                 // TODO: support proxy per network.
             }
+            // Make default network if we have no default.  Any network is better than no network.
+            if (mNetworkForRequestId.get(mDefaultRequest.requestId) == null &&
+                    networkAgent.isVPN() == false &&
+                    mDefaultRequest.networkCapabilities.satisfiedByNetworkCapabilities(
+                    networkAgent.networkCapabilities)) {
+                makeDefault(networkAgent);
+            }
         } else if (state == NetworkInfo.State.DISCONNECTED ||
                 state == NetworkInfo.State.SUSPENDED) {
             networkAgent.asyncChannel.disconnect();