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

Commit cdf3ba48 authored by Hugo Benichi's avatar Hugo Benichi
Browse files

Captive portals: login activity probes like NetworkMonitor

This patch changes CaptivePortalLoginActivity captive portal test to be
consistent with NetworkMonitor by:
 - using Network.java to open the http connection.
 - adding a UserAgent property to the request header.

Test: manually tested.
Bug: 32369183
Change-Id: I33cbcdab9809c712d998da6d925bdd4e3093f8ff
parent 206d947d
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -229,6 +229,13 @@ public class ConnectivityManager {
     */
    public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";

    /**
     * Key for passing a user agent string to the captive portal login activity.
     * {@hide}
     */
    public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
            "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";

    /**
     * Broadcast action to indicate the change of data activity status
     * (idle or active) on a network in a recent period.
+8 −1
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ public class CaptivePortalLoginActivity extends Activity {
    private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };

    private URL mUrl;
    private String mUserAgent;
    private Network mNetwork;
    private CaptivePortal mCaptivePortal;
    private NetworkCallback mNetworkCallback;
@@ -76,6 +77,8 @@ public class CaptivePortalLoginActivity extends Activity {
        mCm = ConnectivityManager.from(this);
        mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
        mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
        mUserAgent = getIntent().getParcelableExtra(
                ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
        mUrl = getUrl();
        if (mUrl == null) {
            // getUrl() failed to parse the url provided in the intent: bail out in a way that
@@ -252,6 +255,7 @@ public class CaptivePortalLoginActivity extends Activity {
    }

    private void testForCaptivePortal() {
        // TODO: reuse NetworkMonitor facilities for consistent captive portal detection.
        new Thread(new Runnable() {
            public void run() {
                // Give time for captive portal to open.
@@ -262,11 +266,14 @@ public class CaptivePortalLoginActivity extends Activity {
                HttpURLConnection urlConnection = null;
                int httpResponseCode = 500;
                try {
                    urlConnection = (HttpURLConnection) mUrl.openConnection();
                    urlConnection = (HttpURLConnection) mNetwork.openConnection(mUrl);
                    urlConnection.setInstanceFollowRedirects(false);
                    urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
                    urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
                    urlConnection.setUseCaches(false);
                    if (mUserAgent != null) {
                       urlConnection.setRequestProperty("User-Agent", mUserAgent);
                    }
                    urlConnection.getInputStream();
                    httpResponseCode = urlConnection.getResponseCode();
                } catch (IOException e) {
+2 −0
Original line number Diff line number Diff line
@@ -433,6 +433,8 @@ public class NetworkMonitor extends StateMachine {
                            }));
                    intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL,
                            mLastPortalProbeResult.detectUrl);
                    intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT,
                            getCaptivePortalUserAgent(mContext));
                    intent.setFlags(
                            Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivityAsUser(intent, UserHandle.CURRENT);