am a524b0f7: am 8df099df: Make CaptivePortalLogin app exit if underlying Network disconnects.

* commit 'a524b0f7869c7dc9b5687721883acfd9a535e440':
  Make CaptivePortalLogin app exit if underlying Network disconnects.
diff --git a/packages/CaptivePortalLogin/AndroidManifest.xml b/packages/CaptivePortalLogin/AndroidManifest.xml
index c5fb167..2ec15be 100644
--- a/packages/CaptivePortalLogin/AndroidManifest.xml
+++ b/packages/CaptivePortalLogin/AndroidManifest.xml
@@ -20,6 +20,7 @@
     package="com.android.captiveportallogin" >
 
     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
     <application android:label="@string/app_name" >
         <activity
diff --git a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
index ae52a1e..b3a6e88 100644
--- a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
+++ b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
@@ -20,7 +20,10 @@
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.ConnectivityManager;
+import android.net.ConnectivityManager.NetworkCallback;
 import android.net.Network;
+import android.net.NetworkCapabilities;
+import android.net.NetworkRequest;
 import android.os.Bundle;
 import android.provider.Settings;
 import android.provider.Settings.Global;
@@ -55,6 +58,7 @@
 
     private URL mURL;
     private int mNetId;
+    private NetworkCallback mNetworkCallback;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -73,7 +77,27 @@
         getActionBar().setDisplayShowHomeEnabled(false);
 
         mNetId = Integer.parseInt(getIntent().getStringExtra(Intent.EXTRA_TEXT));
-        ConnectivityManager.setProcessDefaultNetwork(new Network(mNetId));
+        final Network network = new Network(mNetId);
+        ConnectivityManager.setProcessDefaultNetwork(network);
+
+        // Exit app if Network disappears.
+        final NetworkCapabilities networkCapabilities =
+                ConnectivityManager.from(this).getNetworkCapabilities(network);
+        if (networkCapabilities == null) {
+            finish();
+            return;
+        }
+        mNetworkCallback = new NetworkCallback() {
+            @Override
+            public void onLost(Network lostNetwork) {
+                if (network.equals(lostNetwork)) done(false);
+            }
+        };
+        final NetworkRequest.Builder builder = new NetworkRequest.Builder();
+        for (int transportType : networkCapabilities.getTransportTypes()) {
+            builder.addTransportType(transportType);
+        }
+        ConnectivityManager.from(this).registerNetworkCallback(builder.build(), mNetworkCallback);
 
         WebView myWebView = (WebView) findViewById(R.id.webview);
         WebSettings webSettings = myWebView.getSettings();
@@ -84,6 +108,7 @@
     }
 
     private void done(boolean use_network) {
+        ConnectivityManager.from(this).unregisterNetworkCallback(mNetworkCallback);
         Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN);
         intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId));
         intent.putExtra(LOGGED_IN_RESULT, use_network ? "1" : "0");