DO NOT MERGE 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

(cherry picked from commit cdf3ba48cc)

Change-Id: I559eb0497475daad758ba3b3395225dcd0a27a57
This commit is contained in:
Hugo Benichi
2016-12-14 08:23:40 +09:00
parent 7f086e162b
commit 2c02197bdd
3 changed files with 17 additions and 1 deletions

View File

@@ -222,6 +222,13 @@ public class ConnectivityManager {
*/ */
public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; 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 * Broadcast action to indicate the change of data activity status
* (idle or active) on a network in a recent period. * (idle or active) on a network in a recent period.

View File

@@ -63,6 +63,7 @@ public class CaptivePortalLoginActivity extends Activity {
private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS }; private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };
private URL mUrl; private URL mUrl;
private String mUserAgent;
private Network mNetwork; private Network mNetwork;
private CaptivePortal mCaptivePortal; private CaptivePortal mCaptivePortal;
private NetworkCallback mNetworkCallback; private NetworkCallback mNetworkCallback;
@@ -76,6 +77,8 @@ public class CaptivePortalLoginActivity extends Activity {
mCm = ConnectivityManager.from(this); mCm = ConnectivityManager.from(this);
mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK); mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL); mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
mUserAgent = getIntent().getParcelableExtra(
ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
mUrl = getUrl(); mUrl = getUrl();
if (mUrl == null) { if (mUrl == null) {
// getUrl() failed to parse the url provided in the intent: bail out in a way that // 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() { private void testForCaptivePortal() {
// TODO: reuse NetworkMonitor facilities for consistent captive portal detection.
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
// Give time for captive portal to open. // Give time for captive portal to open.
@@ -262,11 +266,14 @@ public class CaptivePortalLoginActivity extends Activity {
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
int httpResponseCode = 500; int httpResponseCode = 500;
try { try {
urlConnection = (HttpURLConnection) mUrl.openConnection(); urlConnection = (HttpURLConnection) mNetwork.openConnection(mUrl);
urlConnection.setInstanceFollowRedirects(false); urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS); urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS); urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false); urlConnection.setUseCaches(false);
if (mUserAgent != null) {
urlConnection.setRequestProperty("User-Agent", mUserAgent);
}
urlConnection.getInputStream(); urlConnection.getInputStream();
httpResponseCode = urlConnection.getResponseCode(); httpResponseCode = urlConnection.getResponseCode();
} catch (IOException e) { } catch (IOException e) {

View File

@@ -433,6 +433,8 @@ public class NetworkMonitor extends StateMachine {
})); }));
intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL, intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL,
mLastPortalProbeResult.detectUrl); mLastPortalProbeResult.detectUrl);
intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT,
getCaptivePortalUserAgent(mContext));
intent.setFlags( intent.setFlags(
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivityAsUser(intent, UserHandle.CURRENT); mContext.startActivityAsUser(intent, UserHandle.CURRENT);