Merge "Logging improvements in CaptivePortalLoginActivity"

This commit is contained in:
Hugo Benichi
2016-12-08 04:00:45 +00:00
committed by Android (Google) Code Review

View File

@@ -55,12 +55,14 @@ import java.lang.reflect.Method;
import java.util.Random;
public class CaptivePortalLoginActivity extends Activity {
private static final String TAG = "CaptivePortalLogin";
private static final String TAG = CaptivePortalLoginActivity.class.getSimpleName();
private static final boolean DBG = true;
private static final int SOCKET_TIMEOUT_MS = 10000;
private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };
private URL mURL;
private URL mUrl;
private Network mNetwork;
private CaptivePortal mCaptivePortal;
private NetworkCallback mNetworkCallback;
@@ -72,17 +74,18 @@ public class CaptivePortalLoginActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCm = ConnectivityManager.from(this);
String url = getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL);
if (url == null) url = mCm.getCaptivePortalServerUrl();
try {
mURL = new URL(url);
} catch (MalformedURLException e) {
// System misconfigured, bail out in a way that at least provides network access.
Log.e(TAG, "Invalid captive portal URL, url=" + url);
done(Result.WANTED_AS_IS);
}
mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
mUrl = getUrl();
if (mUrl == null) {
// getUrl() failed to parse the url provided in the intent: bail out in a way that
// at least provides network access.
done(Result.WANTED_AS_IS);
return;
}
if (DBG) {
Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
}
// Also initializes proxy system properties.
mCm.bindProcessToNetwork(mNetwork);
@@ -149,6 +152,9 @@ public class CaptivePortalLoginActivity extends Activity {
}
private void done(Result result) {
if (DBG) {
Log.d(TAG, String.format("Result %s for %s", result.name(), mUrl.toString()));
}
if (mNetworkCallback != null) {
mCm.unregisterNetworkCallback(mNetworkCallback);
mNetworkCallback = null;
@@ -185,22 +191,31 @@ public class CaptivePortalLoginActivity extends Activity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_use_network) {
done(Result.WANTED_AS_IS);
return true;
final Result result;
final String action;
final int id = item.getItemId();
switch (id) {
case R.id.action_use_network:
result = Result.WANTED_AS_IS;
action = "USE_NETWORK";
break;
case R.id.action_do_not_use_network:
result = Result.UNWANTED;
action = "DO_NOT_USE_NETWORK";
break;
default:
return super.onOptionsItemSelected(item);
}
if (id == R.id.action_do_not_use_network) {
done(Result.UNWANTED);
return true;
if (DBG) {
Log.d(TAG, String.format("onOptionsItemSelect %s for %s", action, mUrl.toString()));
}
return super.onOptionsItemSelected(item);
done(result);
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mNetworkCallback != null) {
mCm.unregisterNetworkCallback(mNetworkCallback);
mNetworkCallback = null;
@@ -215,10 +230,27 @@ public class CaptivePortalLoginActivity extends Activity {
} catch (InterruptedException e) {
}
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mURL.toString())));
final String url = mUrl.toString();
if (DBG) {
Log.d(TAG, "starting activity with intent ACTION_VIEW for " + url);
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
private URL getUrl() {
String url = getIntent().getStringExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL);
if (url == null) {
url = mCm.getCaptivePortalServerUrl();
}
try {
return new URL(url);
} catch (MalformedURLException e) {
Log.e(TAG, "Invalid captive portal URL " + url);
}
return null;
}
private void testForCaptivePortal() {
new Thread(new Runnable() {
public void run() {
@@ -230,7 +262,7 @@ public class CaptivePortalLoginActivity extends Activity {
HttpURLConnection urlConnection = null;
int httpResponseCode = 500;
try {
urlConnection = (HttpURLConnection) mURL.openConnection();
urlConnection = (HttpURLConnection) mUrl.openConnection();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
@@ -292,7 +324,7 @@ public class CaptivePortalLoginActivity extends Activity {
// settings. Now prompt the WebView read the Network-specific proxy settings.
setWebViewProxy();
// Load the real page.
view.loadUrl(mURL.toString());
view.loadUrl(mUrl.toString());
return;
} else if (mPagesLoaded == 2) {
// Prevent going back to empty first page.