FileDocCategorySizeDatePackage
AccessPointDialog.javaAPI DocAndroid 1.5 API21246Wed May 06 22:42:48 BST 2009com.android.settings.wifi

AccessPointDialog

public class AccessPointDialog extends android.app.AlertDialog implements DialogInterface.OnClickListener, View.OnClickListener, AdapterView.OnItemSelectedListener

Fields Summary
private static final String
TAG
private static final String
INSTANCE_KEY_ACCESS_POINT_STATE
private static final String
INSTANCE_KEY_MODE
private static final String
INSTANCE_KEY_CUSTOM_TITLE
private static final String
INSTANCE_KEY_AUTO_SECURITY_ALLOWED
private static final int
POSITIVE_BUTTON
private static final int
NEGATIVE_BUTTON
private static final int
NEUTRAL_BUTTON
public static final int
MODE_INFO
The dialog should show info connectivity functionality
public static final int
MODE_CONFIGURE
The dialog should configure the detailed AP properties
public static final int
MODE_RETRY_PASSWORD
The dialog should have the password field and connect/cancel
private static final int
SECURITY_AUTO
private static final int
SECURITY_NONE
private static final int
SECURITY_WEP
private static final int
SECURITY_WPA_PERSONAL
private static final int
SECURITY_WPA2_PERSONAL
private static final int[]
WEP_TYPE_VALUES
private int
mConnectButtonPos
private int
mForgetButtonPos
private int
mSaveButtonPos
private int
mMode
private boolean
mAutoSecurityAllowed
private CharSequence
mCustomTitle
private WifiLayer
mWifiLayer
private AccessPointState
mState
private android.view.View
mView
private android.widget.TextView
mPasswordText
private android.widget.EditText
mPasswordEdit
private android.widget.CheckBox
mShowPasswordCheckBox
private android.view.ViewGroup
mTable
private android.widget.EditText
mSsidEdit
private android.widget.Spinner
mSecuritySpinner
private android.widget.Spinner
mWepTypeSpinner
Constructors Summary
public AccessPointDialog(android.content.Context context, WifiLayer wifiLayer)

    
         
        super(context);

        mWifiLayer = wifiLayer;
    
Methods Summary
private voidaddInfoRow(int nameResId, java.lang.String value)

        View rowView = getLayoutInflater().inflate(R.layout.wifi_ap_info_row, mTable, false);
        ((TextView) rowView.findViewById(R.id.name)).setText(nameResId);
        ((TextView) rowView.findViewById(R.id.value)).setText(value);
        mTable.addView(rowView);
    
private voidaddInfoRow(int nameResId, int valueResId)

        addInfoRow(nameResId, getContext().getString(valueResId));
    
private java.lang.StringgetEnteredPassword()

        return mPasswordEdit != null ? mPasswordEdit.getText().toString() : null;
    
private intgetSecurityTypeFromSpinner()

        int position = mSecuritySpinner.getSelectedItemPosition();
        // If there is no AUTO choice, the position needs 1 added to get
        // to the proper spinner position -> security constants mapping
        return mAutoSecurityAllowed ? position : position + 1;
    
private static intgetSignalResId(int signal)

        switch (WifiManager.calculateSignalLevel(signal, 4)) {
            case 0: {
                return R.string.wifi_signal_0;
            }
            case 1: {
                return R.string.wifi_signal_1;
            }
            case 2: {
                return R.string.wifi_signal_2;
            }
            case 3: {
                return R.string.wifi_signal_3;
            }
        }
        
        return 0;
    
private voidhandleConnect()

        if (!replaceStateWithWifiLayerInstance()) {
            Log.w(TAG, "Assuming connecting to a new network.");
        }
        
        /*
         * If the network is secured and they haven't entered a password, popup
         * an error. Allow empty passwords if the state already has a password
         * set (since in that scenario, an empty password means keep the old
         * password).
         */
        String password = getEnteredPassword();
        boolean passwordIsEmpty = TextUtils.isEmpty(password);
        
        /*
         * When 'retry password', they can not enter a blank password. In any
         * other mode, we let them enter a blank password if the state already
         * has a password.
         */
        if (passwordIsEmpty && (!mState.hasPassword() || mMode == MODE_RETRY_PASSWORD)
                && (mState.security != null) && !mState.security.equals(AccessPointState.OPEN)) {
            new AlertDialog.Builder(getContext())
                    .setTitle(R.string.error_title)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(R.string.wifi_password_incorrect_error)
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
            return;
        }
        
        if (!passwordIsEmpty) { 
            mState.setPassword(password);
        }
        
        mWifiLayer.connectToNetwork(mState);            
    
private voidhandleForget()

        if (!replaceStateWithWifiLayerInstance()) return;
        mWifiLayer.forgetNetwork(mState);
    
private voidhandleSave()

        replaceStateWithWifiLayerInstance();

        String ssid = mSsidEdit.getText().toString();
        String password = mPasswordEdit.getText().toString();
        
        mState.setSsid(ssid);
        
        int securityType = getSecurityTypeFromSpinner();
        
        if (!TextUtils.isEmpty(password)) {
            switch (securityType) {
             
                case SECURITY_WPA_PERSONAL: {
                    mState.setSecurity(AccessPointState.WPA);
                    mState.setPassword(password);
                    break;
                }
                    
                case SECURITY_WPA2_PERSONAL: {
                    mState.setSecurity(AccessPointState.WPA2);
                    mState.setPassword(password);
                    break;
                }
                
                case SECURITY_AUTO: {
                    mState.setPassword(password);
                    break;
                }
                    
                case SECURITY_WEP: {
                    mState.setSecurity(AccessPointState.WEP);
                    mState.setPassword(password,
                            WEP_TYPE_VALUES[mWepTypeSpinner.getSelectedItemPosition()]);
                    break;
                }
                
            }
        } else {
            mState.setSecurity(AccessPointState.OPEN);
        }
        
        if (securityType == SECURITY_NONE) {
            mState.setSecurity(AccessPointState.OPEN);
        }
            
        if (!mWifiLayer.saveNetwork(mState)) {
            return;
        }
        
        // Connect right away if they've touched it
        if (!mWifiLayer.connectToNetwork(mState)) {
            return;
        }
        
    
private voidhandleSecurityChange(int security)

        
        switch (security) {
            
            case SECURITY_NONE: {
                setWepVisible(false);
                setGenericPasswordVisible(false);
                break;
            }
            
            case SECURITY_WEP: {
                setGenericPasswordVisible(false);
                setWepVisible(true);
                updatePasswordCaption(AccessPointState.WEP);
                break;
            }
            
            case SECURITY_AUTO: {
                setWepVisible(false);
                setGenericPasswordVisible(mState.hasSecurity());
                // Shows the generic 'wireless password'
                updatePasswordCaption(AccessPointState.WPA);
                break;
            }
            
            case SECURITY_WPA_PERSONAL:
            case SECURITY_WPA2_PERSONAL: {
                setWepVisible(false);
                setGenericPasswordVisible(true);
                // Both WPA and WPA2 show the same caption, so either is ok
                updatePasswordCaption(AccessPointState.WPA);
                break;
            }
        }
    
public voidonClick(android.content.DialogInterface dialog, int which)

        if (which == mForgetButtonPos) {
            handleForget();
        } else if (which == mConnectButtonPos) {
            handleConnect();
        } else if (which == mSaveButtonPos) {
            handleSave();
        }
    
public voidonClick(android.view.View v)

        if (v == mShowPasswordCheckBox) {
            setShowPassword(mShowPasswordCheckBox.isChecked());
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        onLayout();
        onFill();

        super.onCreate(savedInstanceState);
    
private voidonFill()
Called when the widgets are in-place waiting to be filled with data


        // Appears in the order added
        if (mMode == MODE_INFO) {
            if (mState.primary) {
                addInfoRow(R.string.wifi_status, mState.getSummarizedStatus());
                addInfoRow(R.string.wifi_link_speed, mState.linkSpeed + WifiInfo.LINK_SPEED_UNITS);
            }
    
            if (mState.seen) {
                addInfoRow(R.string.signal, getSignalResId(mState.signal));
            }
            
            if (mState.security != null) {
                addInfoRow(R.string.security, mState.getHumanReadableSecurity());
            }
    
            if (mState.primary && mState.ipAddress != 0) {
                addInfoRow(R.string.ip_address, Formatter.formatIpAddress(mState.ipAddress));
            }
            
        } else if (mMode == MODE_CONFIGURE) {
            String ssid = mState.getHumanReadableSsid();
            if (!TextUtils.isEmpty(ssid)) {
                mSsidEdit.setText(ssid);
            }
            
            mPasswordEdit.setHint(R.string.wifi_password_unchanged);
        }

        updatePasswordCaption(mState.security);
    
public voidonItemSelected(android.widget.AdapterView parent, android.view.View view, int position, long id)

        if (parent == mSecuritySpinner) {
            handleSecurityChange(getSecurityTypeFromSpinner());
        }
    
private voidonLayout()
Called after flags are set, the dialog's layout/etc should be set up here

        final Context context = getContext();
        final String ssid = mState.getHumanReadableSsid();
        
        int positiveButtonResId = 0;
        int negativeButtonResId = R.string.cancel;
        int neutralButtonResId = 0;

        if (mCustomTitle == null) {
            // Generic title is the SSID
            // We don't want to trigger this as a custom title, so call super's
            super.setTitle(ssid);
        }
        setInverseBackgroundForced(true);

        boolean defaultPasswordVisibility = true;
        
        if (mMode == MODE_CONFIGURE) {
            setLayout(R.layout.wifi_ap_configure);

            positiveButtonResId = R.string.wifi_save_config;
            mSaveButtonPos = POSITIVE_BUTTON;
            
        } else if (mMode == MODE_INFO) {
            setLayout(R.layout.wifi_ap_info);

            if (mState.isConnectable()) {
                if (mCustomTitle == null) {
                    // We don't want to trigger this as a custom title, so call super's
                    super.setTitle(context.getString(R.string.connect_to_blank, ssid));
                }
                positiveButtonResId = R.string.connect;
                mConnectButtonPos = POSITIVE_BUTTON;
            }

            if (mState.isForgetable()) {
                if (positiveButtonResId == 0) {
                    positiveButtonResId = R.string.forget_network;
                    mForgetButtonPos = POSITIVE_BUTTON;
                } else {
                    neutralButtonResId = R.string.forget_network;
                    mForgetButtonPos = NEUTRAL_BUTTON;
                }
            }
        } else if (mMode == MODE_RETRY_PASSWORD) {
            setLayout(R.layout.wifi_ap_retry_password);
            
            positiveButtonResId = R.string.connect;
            mConnectButtonPos = POSITIVE_BUTTON;
            
            setGenericPasswordVisible(true);
            defaultPasswordVisibility = false;
        }

        if (defaultPasswordVisibility) {
            if (!mState.configured && mState.seen && mState.hasSecurity()) {
                setGenericPasswordVisible(true);
            } else {
                setGenericPasswordVisible(false);
            }
        }
        
        setButtons(positiveButtonResId, negativeButtonResId, neutralButtonResId);
    
public voidonNothingSelected(android.widget.AdapterView parent)

    
private voidonReferenceViews(android.view.View view)
Called when we need to set our member variables to point to the views.

        mPasswordText = (TextView) view.findViewById(R.id.password_text);
        mPasswordEdit = (EditText) view.findViewById(R.id.password_edit);
        
        mShowPasswordCheckBox = (CheckBox) view.findViewById(R.id.show_password_checkbox);
        if (mShowPasswordCheckBox != null) {
            mShowPasswordCheckBox.setOnClickListener(this);
        }
        
        if (mMode == MODE_CONFIGURE) {
            mSsidEdit = (EditText) view.findViewById(R.id.ssid_edit);
            mSecuritySpinner = (Spinner) view.findViewById(R.id.security_spinner);
            mSecuritySpinner.setOnItemSelectedListener(this);
            setSecuritySpinnerAdapter();
            mWepTypeSpinner = (Spinner) view.findViewById(R.id.wep_type_spinner);
            
        } else if (mMode == MODE_INFO) {
            mTable = (ViewGroup) view.findViewById(R.id.table);
        }
        
    
public voidonRestoreInstanceState(android.os.Bundle savedInstanceState)

        // Set to a class loader that can find AccessPointState
        savedInstanceState.setClassLoader(getClass().getClassLoader());
        
        mState = savedInstanceState.getParcelable(INSTANCE_KEY_ACCESS_POINT_STATE);
        mState.setContext(getContext());
        
        mMode = savedInstanceState.getInt(INSTANCE_KEY_MODE, mMode);
        mAutoSecurityAllowed = savedInstanceState.getBoolean(INSTANCE_KEY_AUTO_SECURITY_ALLOWED,
                mAutoSecurityAllowed);
        mCustomTitle = savedInstanceState.getCharSequence(INSTANCE_KEY_CUSTOM_TITLE);
        if (mCustomTitle != null) {
            setTitle(mCustomTitle);
        }

        // This is called last since it depends on the above values 
        super.onRestoreInstanceState(savedInstanceState);
        
        if (mShowPasswordCheckBox != null) {
            // Restore the show-password-state on the edit text
            setShowPassword(mShowPasswordCheckBox.isChecked());
        }
    
public android.os.BundleonSaveInstanceState()

        Bundle bundle = super.onSaveInstanceState();
        bundle.putParcelable(INSTANCE_KEY_ACCESS_POINT_STATE, mState);
        bundle.putInt(INSTANCE_KEY_MODE, mMode);
        bundle.putBoolean(INSTANCE_KEY_AUTO_SECURITY_ALLOWED, mAutoSecurityAllowed);
        bundle.putCharSequence(INSTANCE_KEY_CUSTOM_TITLE, mCustomTitle);
        return bundle;
    
private booleanreplaceStateWithWifiLayerInstance()
Replaces our {@link #mState} with the equal WifiLayer instance. This is useful after we unparceled the state previously and before we are calling methods on {@link #mWifiLayer}.

return
Whether WifiLayer was able to find an equal state in its set.

        AccessPointState state = mWifiLayer.getWifiLayerApInstance(mState);
        if (state == null) {
            return false;
        }
        
        mState = state;
        return true;
    
public voidsetAutoSecurityAllowed(boolean autoSecurityAllowed)

        mAutoSecurityAllowed = autoSecurityAllowed;
    
private voidsetButtons(int positiveResId, int negativeResId, int neutralResId)

        final Context context = getContext();
        
        if (positiveResId > 0) {
            setButton(context.getString(positiveResId), this);
        }
        
        if (negativeResId > 0) {
            setButton2(context.getString(negativeResId), this);
        }

        if (neutralResId > 0) {
            setButton3(context.getString(neutralResId), this);
        }
    
private voidsetGenericPasswordVisible(boolean visible)

see
#setWepVisible(boolean)

        int visibility = visible ? View.VISIBLE : View.GONE;
        mPasswordText.setVisibility(visibility);
        mPasswordEdit.setVisibility(visibility);
        mShowPasswordCheckBox.setVisibility(visibility);
    
private voidsetLayout(int layoutResId)

        setView(mView = getLayoutInflater().inflate(layoutResId, null));
        onReferenceViews(mView);
    
public voidsetMode(int mode)
Sets the dialog mode.

param
mode One of {@link #MODE_CONFIGURE} or {@link #MODE_INFO}

        mMode = mode;
    
private voidsetSecuritySpinnerAdapter()

        Context context = getContext();
        int arrayResId = mAutoSecurityAllowed ? R.array.wifi_security_entries
                : R.array.wifi_security_without_auto_entries;         

        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context,
                android.R.layout.simple_spinner_item,
                context.getResources().getStringArray(arrayResId));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSecuritySpinner.setAdapter(adapter);
    
private voidsetShowPassword(boolean showPassword)

        if (mPasswordEdit != null) {
            mPasswordEdit.setInputType(InputType.TYPE_CLASS_TEXT |
                    (showPassword ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                            : InputType.TYPE_TEXT_VARIATION_PASSWORD));
        }
    
public voidsetState(AccessPointState state)
Sets state to show in this dialog.

param
state The state.

        mState = state;
    
public voidsetTitle(java.lang.CharSequence title)

        super.setTitle(title);
        mCustomTitle = title;
    
public voidsetTitle(int titleId)

        setTitle(getContext().getString(titleId));
    
private voidsetWepVisible(boolean visible)
Call the one you want to hide first.

        setGenericPasswordVisible(visible);
        int visibility = visible ? View.VISIBLE : View.GONE;
        mWepTypeSpinner.setVisibility(visibility);
    
private voidupdatePasswordCaption(java.lang.String security)

        
        if (mPasswordText != null && security != null
                && security.equals(AccessPointState.WEP)) {
            mPasswordText.setText(R.string.please_type_hex_key);
        } else {
            mPasswordText.setText(R.string.please_type_passphrase);
        }