OSDN Git Service

DO NOT MERGE Captive portals: login activity probes like NetworkMonitor
authorHugo Benichi <hugobenichi@google.com>
Tue, 13 Dec 2016 23:23:40 +0000 (08:23 +0900)
committerHugo Benichi <hugobenichi@google.com>
Thu, 5 Jan 2017 03:12:50 +0000 (12:12 +0900)
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

(cherry picked from commit cdf3ba48ccef0f9c6ca8724c1c106df0dd725ad0)

Change-Id: I559eb0497475daad758ba3b3395225dcd0a27a57

core/java/android/net/ConnectivityManager.java
packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
services/core/java/com/android/server/connectivity/NetworkMonitor.java

index 51431eb..042481f 100644 (file)
@@ -223,6 +223,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.
      * The network becomes active when data transmission is started, or
index 03f0b4d..23a8655 100644 (file)
@@ -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) {
index 5e98859..9ffe2b7 100644 (file)
@@ -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);