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

Commit 9c5c3681 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Use CaptivePortalProbeSpec method in portal app

This only changes behavior if the EXTRA_CAPTIVE_PORTAL_PROBE_SPEC extra
is passed, which only happens if extra probe specs have been defined in
settings.

Bug: b/79499239
Test: manual: on 2 different portals, works w/ and w/o the setting
Change-Id: I70acfd9213bf620cbb79ad999b8ad03472e8d43f
parent 8255c2d6
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/";