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

Commit 9f855054 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
Merged-In: Iebd307b5d73a7b4679baea2a1c12c3baba6a7447
Merged-In: I851d8c41d42dccb03f813e549439f44c83f7d5ac
(clean cherry-pick of pi-dev I70acfd9213bf620cbb79ad999b8ad03472e8d43f)

Change-Id: I3b3bcbbd380f8cbd0b663556d55276f43e30b3e5
parent 13e6e21d
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;
@@ -30,6 +33,7 @@ import android.net.NetworkInfo;
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.os.Build;
@@ -84,6 +88,7 @@ public class CaptivePortalLoginActivity extends Activity {
    };

    private URL mUrl;
    private CaptivePortalProbeSpec mProbeSpec;
    private String mUserAgent;
    private Network mNetwork;
    private CaptivePortal mCaptivePortal;
@@ -117,6 +122,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(
@@ -328,6 +341,7 @@ public class CaptivePortalLoginActivity extends Activity {
                }
                HttpURLConnection urlConnection = null;
                int httpResponseCode = 500;
                String locationHeader = null;
                try {
                    urlConnection = (HttpURLConnection) network.openConnection(mUrl);
                    urlConnection.setInstanceFollowRedirects(false);
@@ -342,6 +356,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 +
@@ -352,13 +367,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/";