FileDocCategorySizeDatePackage
CaptivePortalLoginActivity.javaAPI DocAndroid 5.1 API11415Thu Mar 12 22:22:40 GMT 2015com.android.captiveportallogin

CaptivePortalLoginActivity

public class CaptivePortalLoginActivity extends android.app.Activity

Fields Summary
private static final String
TAG
private static final String
DEFAULT_SERVER
private static final int
SOCKET_TIMEOUT_MS
private static final String
ACTION_CAPTIVE_PORTAL_LOGGED_IN
private static final String
LOGGED_IN_RESULT
private static final int
CAPTIVE_PORTAL_APP_RETURN_APPEASED
private static final int
CAPTIVE_PORTAL_APP_RETURN_UNWANTED
private static final int
CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS
private static final String
RESPONSE_TOKEN
private URL
mURL
private int
mNetId
private String
mResponseToken
private android.net.ConnectivityManager.NetworkCallback
mNetworkCallback
Constructors Summary
Methods Summary
private voiddone(int result)

        if (mNetworkCallback != null) {
            ConnectivityManager.from(this).unregisterNetworkCallback(mNetworkCallback);
        }
        Intent intent = new Intent(ACTION_CAPTIVE_PORTAL_LOGGED_IN);
        intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(mNetId));
        intent.putExtra(LOGGED_IN_RESULT, String.valueOf(result));
        intent.putExtra(RESPONSE_TOKEN, mResponseToken);
        sendBroadcast(intent);
        finish();
    
public voidonBackPressed()

        WebView myWebView = (WebView) findViewById(R.id.webview);
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            super.onBackPressed();
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);

        String server = Settings.Global.getString(getContentResolver(), "captive_portal_server");
        if (server == null) server = DEFAULT_SERVER;
        try {
            mURL = new URL("http", server, "/generate_204");
            final Uri dataUri = getIntent().getData();
            if (!dataUri.getScheme().equals("netid")) {
                throw new MalformedURLException();
            }
            mNetId = Integer.parseInt(dataUri.getSchemeSpecificPart());
            mResponseToken = dataUri.getFragment();
        } catch (MalformedURLException|NumberFormatException e) {
            // System misconfigured, bail out in a way that at least provides network access.
            done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS);
        }

        final ConnectivityManager cm = ConnectivityManager.from(this);
        final Network network = new Network(mNetId);
        // Also initializes proxy system properties.
        cm.setProcessDefaultNetwork(network);

        // Proxy system properties must be initialized before setContentView is called because
        // setContentView initializes the WebView logic which in turn reads the system properties.
        setContentView(R.layout.activity_captive_portal_login);

        getActionBar().setDisplayShowHomeEnabled(false);

        // Exit app if Network disappears.
        final NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
        if (networkCapabilities == null) {
            finish();
            return;
        }
        mNetworkCallback = new NetworkCallback() {
            @Override
            public void onLost(Network lostNetwork) {
                if (network.equals(lostNetwork)) done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
            }
        };
        final NetworkRequest.Builder builder = new NetworkRequest.Builder();
        for (int transportType : networkCapabilities.getTransportTypes()) {
            builder.addTransportType(transportType);
        }
        cm.registerNetworkCallback(builder.build(), mNetworkCallback);

        final WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.clearCache(true);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.setWebChromeClient(new MyWebChromeClient());
        // Start initial page load so WebView finishes loading proxy settings.
        // Actual load of mUrl is initiated by MyWebViewClient.
        myWebView.loadData("", "text/html", null);
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        getMenuInflater().inflate(R.menu.captive_portal_login, menu);
        return true;
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        int id = item.getItemId();
        if (id == R.id.action_use_network) {
            done(CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS);
            return true;
        }
        if (id == R.id.action_do_not_use_network) {
            done(CAPTIVE_PORTAL_APP_RETURN_UNWANTED);
            return true;
        }
        return super.onOptionsItemSelected(item);
    
private voidsetWebViewProxy()

        LoadedApk loadedApk = getApplication().mLoadedApk;
        try {
            Field receiversField = LoadedApk.class.getDeclaredField("mReceivers");
            receiversField.setAccessible(true);
            ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
            for (Object receiverMap : receivers.values()) {
                for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                    Class clazz = rec.getClass();
                    if (clazz.getName().contains("ProxyChangeListener")) {
                        Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class,
                                Intent.class);
                        Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                        onReceiveMethod.invoke(rec, getApplicationContext(), intent);
                        Log.v(TAG, "Prompting WebView proxy reload.");
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception while setting WebView proxy: " + e);
        }
    
private voidtestForCaptivePortal()

        new Thread(new Runnable() {
            public void run() {
                // Give time for captive portal to open.
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                HttpURLConnection urlConnection = null;
                int httpResponseCode = 500;
                try {
                    urlConnection = (HttpURLConnection) mURL.openConnection();
                    urlConnection.setInstanceFollowRedirects(false);
                    urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
                    urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
                    urlConnection.setUseCaches(false);
                    urlConnection.getInputStream();
                    httpResponseCode = urlConnection.getResponseCode();
                } catch (IOException e) {
                } finally {
                    if (urlConnection != null) urlConnection.disconnect();
                }
                if (httpResponseCode == 204) {
                    done(CAPTIVE_PORTAL_APP_RETURN_APPEASED);
                }
            }
        }).start();