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

Commit 95ea872e authored by Chalard Jean's avatar Chalard Jean Committed by android-build-merger
Browse files

Merge "Use CaptivePortalProbeSpec method in portal app" into pi-dev

am: 9a963e11

Change-Id: Iebd307b5d73a7b4679baea2a1c12c3baba6a7447
parents a6b326b9 9a963e11
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.captiveportallogin;

import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_PROBE_SPEC;
import static android.net.captiveportal.CaptivePortalProbeSpec.HTTP_LOCATION_HEADER_NAME;

import android.app.Activity;
import android.app.LoadedApk;
import android.content.Context;
@@ -29,6 +32,7 @@ import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Proxy;
import android.net.Uri;
import android.net.captiveportal.CaptivePortalProbeSpec;
import android.net.dns.ResolvUtil;
import android.net.http.SslError;
import android.net.wifi.WifiInfo;
@@ -85,6 +89,7 @@ public class CaptivePortalLoginActivity extends Activity {
    };

    private URL mUrl;
    private CaptivePortalProbeSpec mProbeSpec;
    private String mUserAgent;
    private Network mNetwork;
    private CaptivePortal mCaptivePortal;
@@ -118,6 +123,14 @@ public class CaptivePortalLoginActivity extends Activity {
            Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
        }

        final String spec = getIntent().getStringExtra(EXTRA_CAPTIVE_PORTAL_PROBE_SPEC);
        try {
            mProbeSpec = CaptivePortalProbeSpec.parseSpecOrNull(spec);
        } catch (Exception e) {
            // Make extra sure that invalid configurations do not cause crashes
            mProbeSpec = null;
        }

        // Also initializes proxy system properties.
        mCm.bindProcessToNetwork(mNetwork);
        mCm.setProcessDefaultNetworkForHostResolution(
@@ -329,6 +342,7 @@ public class CaptivePortalLoginActivity extends Activity {
                }
                HttpURLConnection urlConnection = null;
                int httpResponseCode = 500;
                String locationHeader = null;
                try {
                    urlConnection = (HttpURLConnection) network.openConnection(mUrl);
                    urlConnection.setInstanceFollowRedirects(false);
@@ -343,6 +357,7 @@ public class CaptivePortalLoginActivity extends Activity {

                    urlConnection.getInputStream();
                    httpResponseCode = urlConnection.getResponseCode();
                    locationHeader = urlConnection.getHeaderField(HTTP_LOCATION_HEADER_NAME);
                    if (DBG) {
                        Log.d(TAG, "probe at " + mUrl +
                                " ret=" + httpResponseCode +
@@ -353,13 +368,20 @@ public class CaptivePortalLoginActivity extends Activity {
                } finally {
                    if (urlConnection != null) urlConnection.disconnect();
                }
                if (httpResponseCode == 204) {
                if (isDismissed(httpResponseCode, locationHeader, mProbeSpec)) {
                    done(Result.DISMISSED);
                }
            }
        }).start();
    }

    private static boolean isDismissed(
            int httpResponseCode, String locationHeader, CaptivePortalProbeSpec probeSpec) {
        return (probeSpec != null)
                ? probeSpec.getResult(httpResponseCode, locationHeader).isSuccessful()
                : (httpResponseCode == 204);
    }

    private class MyWebViewClient extends WebViewClient {
        private static final String INTERNAL_ASSETS = "file:///android_asset/";