DO NOT MERGE Logging improvements in CaptivePortalLoginActivity
Logging improvements to help debugging captive portal issues.
Test: manually tested
Bug: 33126342
(cherry picked from commit 87de0c2067)
Change-Id: I3ac1773e07827194d854ebad4c27c48da106154b
This commit is contained in:
@@ -55,12 +55,14 @@ import java.lang.reflect.Method;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class CaptivePortalLoginActivity extends Activity {
|
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 static final int SOCKET_TIMEOUT_MS = 10000;
|
||||||
|
|
||||||
private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };
|
private enum Result { DISMISSED, UNWANTED, WANTED_AS_IS };
|
||||||
|
|
||||||
private URL mURL;
|
private URL mUrl;
|
||||||
private Network mNetwork;
|
private Network mNetwork;
|
||||||
private CaptivePortal mCaptivePortal;
|
private CaptivePortal mCaptivePortal;
|
||||||
private NetworkCallback mNetworkCallback;
|
private NetworkCallback mNetworkCallback;
|
||||||
@@ -72,17 +74,18 @@ public class CaptivePortalLoginActivity extends Activity {
|
|||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
mCm = ConnectivityManager.from(this);
|
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);
|
mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
|
||||||
mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
|
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.
|
// Also initializes proxy system properties.
|
||||||
mCm.bindProcessToNetwork(mNetwork);
|
mCm.bindProcessToNetwork(mNetwork);
|
||||||
@@ -149,6 +152,9 @@ public class CaptivePortalLoginActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void done(Result result) {
|
private void done(Result result) {
|
||||||
|
if (DBG) {
|
||||||
|
Log.d(TAG, String.format("Result %s for %s", result.name(), mUrl.toString()));
|
||||||
|
}
|
||||||
if (mNetworkCallback != null) {
|
if (mNetworkCallback != null) {
|
||||||
mCm.unregisterNetworkCallback(mNetworkCallback);
|
mCm.unregisterNetworkCallback(mNetworkCallback);
|
||||||
mNetworkCallback = null;
|
mNetworkCallback = null;
|
||||||
@@ -185,22 +191,31 @@ public class CaptivePortalLoginActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
int id = item.getItemId();
|
final Result result;
|
||||||
if (id == R.id.action_use_network) {
|
final String action;
|
||||||
done(Result.WANTED_AS_IS);
|
final int id = item.getItemId();
|
||||||
return true;
|
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) {
|
if (DBG) {
|
||||||
done(Result.UNWANTED);
|
Log.d(TAG, String.format("onOptionsItemSelect %s for %s", action, mUrl.toString()));
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
done(result);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
if (mNetworkCallback != null) {
|
if (mNetworkCallback != null) {
|
||||||
mCm.unregisterNetworkCallback(mNetworkCallback);
|
mCm.unregisterNetworkCallback(mNetworkCallback);
|
||||||
mNetworkCallback = null;
|
mNetworkCallback = null;
|
||||||
@@ -215,10 +230,27 @@ public class CaptivePortalLoginActivity extends Activity {
|
|||||||
} catch (InterruptedException e) {
|
} 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() {
|
private void testForCaptivePortal() {
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -230,7 +262,7 @@ 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) mUrl.openConnection();
|
||||||
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);
|
||||||
@@ -292,7 +324,7 @@ public class CaptivePortalLoginActivity extends Activity {
|
|||||||
// settings. Now prompt the WebView read the Network-specific proxy settings.
|
// settings. Now prompt the WebView read the Network-specific proxy settings.
|
||||||
setWebViewProxy();
|
setWebViewProxy();
|
||||||
// Load the real page.
|
// Load the real page.
|
||||||
view.loadUrl(mURL.toString());
|
view.loadUrl(mUrl.toString());
|
||||||
return;
|
return;
|
||||||
} else if (mPagesLoaded == 2) {
|
} else if (mPagesLoaded == 2) {
|
||||||
// Prevent going back to empty first page.
|
// Prevent going back to empty first page.
|
||||||
|
|||||||
Reference in New Issue
Block a user