FileDocCategorySizeDatePackage
ProxySelector.javaAPI DocAndroid 1.5 API8334Wed May 06 22:42:48 BST 2009com.android.settings

ProxySelector

public class ProxySelector extends android.app.Activity
To start the Proxy Selector activity, create the following intent. Intent intent = new Intent(); intent.setClassName("com.android.browser.ProxySelector"); startActivity(intent); you can add extra options to the intent by using intent.putExtra(key, value); the extra options are: button-label: a string label to display for the okay button title: the title of the window error-text: If not null, will be used as the label of the error message.

Fields Summary
private static final String
LOGTAG
android.widget.EditText
mHostnameField
android.widget.EditText
mPortField
android.widget.Button
mOKButton
private static final String
HOSTNAME_REGEXP
private static final Pattern
HOSTNAME_PATTERN
android.view.View.OnClickListener
mOKHandler
android.view.View.OnClickListener
mClearHandler
android.view.View.OnClickListener
mDefaultHandler
android.view.View.OnFocusChangeListener
mOnFocusChangeHandler
Constructors Summary
Methods Summary
voidinitView()


        mHostnameField = (EditText)findViewById(R.id.hostname);
        mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler);

        mPortField = (EditText)findViewById(R.id.port);
        mPortField.setOnClickListener(mOKHandler);
        mPortField.setOnFocusChangeListener(mOnFocusChangeHandler);

        mOKButton = (Button)findViewById(R.id.action);
        mOKButton.setOnClickListener(mOKHandler);

        Button b = (Button)findViewById(R.id.clear);
        b.setOnClickListener(mClearHandler);

        b = (Button)findViewById(R.id.defaultView);
        b.setOnClickListener(mDefaultHandler);
    
public voidonCreate(android.os.Bundle icicle)

     
        HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
    
        super.onCreate(icicle);

        if (android.util.Config.LOGV) Log.v(LOGTAG, "[ProxySelector] onStart");

        setContentView(R.layout.proxy);
        initView();
        populateFields(false);
    
voidpopulateFields(boolean useDefault)

        String hostname = null;
        int port = -1;
        if (useDefault) {
            // Use the default proxy settings provided by the carrier
            hostname = Proxy.getDefaultHost();
            port = Proxy.getDefaultPort();
        } else {
            // Use the last setting given by the user
            hostname = Proxy.getHost(this);
            port = Proxy.getPort(this);
        }

        if (hostname == null) {
            hostname = "";
        }

        mHostnameField.setText(hostname);

        String portStr = port == -1 ? "" : Integer.toString(port);
        mPortField.setText(portStr);

        Intent intent = getIntent();

        String buttonLabel = intent.getStringExtra("button-label");
        if (!TextUtils.isEmpty(buttonLabel)) {
            mOKButton.setText(buttonLabel);
        }

        String title = intent.getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }
    
booleansaveToDb()
returns true on success, false if the user must correct something


        String hostname = mHostnameField.getText().toString().trim();
        String portStr = mPortField.getText().toString().trim();
        int port = -1;

        int result = validate(hostname, portStr);
        if (result > 0) {
            showError(result);
            return false;
        }

        if (portStr.length() > 0) {
            try {
                port = Integer.parseInt(portStr);
            } catch (NumberFormatException ex) {
                return false;
            }
        }

        // FIXME: The best solution would be to make a better UI that would
        // disable editing of the text boxes if the user chooses to use the
        // default settings. i.e. checking a box to always use the default
        // carrier. http:/b/issue?id=756480
        // FIXME: This currently will not work if the default host is blank and
        // the user has cleared the input boxes in order to not use a proxy.
        // This is a UI problem and can be solved with some better form
        // controls.
        // FIXME: If the user types in a proxy that matches the default, should
        // we keep that setting? Can be fixed with a new UI.
        ContentResolver res = getContentResolver();
        if (hostname.equals(Proxy.getDefaultHost())
                && port == Proxy.getDefaultPort()) {
            // If the user hit the default button and didn't change any of
            // the input boxes, treat it as if the user has not specified a
            // proxy.
            hostname = null;
        }

        if (!TextUtils.isEmpty(hostname)) {
            hostname += ':" + portStr;
        }
        Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);
        sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION));

        return true;
    
protected voidshowError(int error)


        new AlertDialog.Builder(this)
                .setTitle(R.string.proxy_error)
                .setMessage(error)
                .setPositiveButton(R.string.proxy_error_dismiss, null)
                .show();
    
intvalidate(java.lang.String hostname, java.lang.String port)
validate syntax of hostname and port entries

return
0 on success, string resource ID on failure

        Matcher match = HOSTNAME_PATTERN.matcher(hostname);

        if (!match.matches()) return R.string.proxy_error_invalid_host;

        if (hostname.length() > 0 && port.length() == 0) {
            return R.string.proxy_error_empty_port;
        }

        if (port.length() > 0) {
            if (hostname.length() == 0) {
                return R.string.proxy_error_empty_host_set_port;
            }
            int portVal = -1;
            try {
                portVal = Integer.parseInt(port);
            } catch (NumberFormatException ex) {
                return R.string.proxy_error_invalid_port;
            }
            if (portVal <= 0 || portVal > 0xFFFF) {
                return R.string.proxy_error_invalid_port;
            }
        }
        return 0;