FileDocCategorySizeDatePackage
EditPhoneNumberPreference.javaAPI DocAndroid 1.5 API17631Wed May 06 22:42:46 BST 2009com.android.phone

EditPhoneNumberPreference

public class EditPhoneNumberPreference extends android.preference.EditTextPreference

Fields Summary
private static final int
CM_CONFIRM
simple confirmation (OK / CANCEL)
private static final int
CM_ACTIVATION
toggle [(ENABLE / CANCEL) or (DISABLE / CANCEL)], use isToggled() to see requested state.
private int
mConfirmationMode
private static final String
VALUE_SEPARATOR
private static final String
VALUE_OFF
private static final String
VALUE_ON
private android.widget.ImageButton
mContactPickButton
private View.OnFocusChangeListener
mDialogFocusChangeListener
Called when focus is changed between fields
private OnDialogClosedListener
mDialogOnClosedListener
Called when the Dialog is closed.
private GetDefaultNumberListener
mGetDefaultNumberListener
Used to indicate that we are going to request for a default number. for the dialog.
private android.app.Activity
mParentActivity
private android.content.Intent
mContactListIntent
private int
mPrefId
Arbitrary activity-assigned preference id value
private CharSequence
mEnableText
private CharSequence
mDisableText
private CharSequence
mChangeNumberText
private CharSequence
mSummaryOn
private CharSequence
mSummaryOff
private int
mButtonClicked
private String
mPhoneNumber
private boolean
mChecked
private String
mEncodedText
Override persistString so that we can get a hold of the EditTextPreference's text field.
Constructors Summary
public EditPhoneNumberPreference(android.content.Context context, android.util.AttributeSet attrs)

    
    
                                     
      
            
    
    
                            
      
                                                     
          
    

    /*
     * Constructors
     */
         
        super(context, attrs);
        
        setDialogLayoutResource(R.layout.pref_dialog_editphonenumber);
        
        //create intent to bring up contact list
        mContactListIntent = new Intent(Intent.ACTION_GET_CONTENT);
        mContactListIntent.setType(android.provider.Contacts.Phones.CONTENT_ITEM_TYPE);
        
        //get the edit phone number default settings
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.EditPhoneNumberPreference, 0, R.style.EditPhoneNumberPreference);
        mEnableText = a.getString(R.styleable.EditPhoneNumberPreference_enableButtonText);
        mDisableText = a.getString(R.styleable.EditPhoneNumberPreference_disableButtonText);
        mChangeNumberText = a.getString(R.styleable.EditPhoneNumberPreference_changeNumButtonText);
        mConfirmationMode = a.getInt(R.styleable.EditPhoneNumberPreference_confirmMode, 0);
        a.recycle();

        //get the summary settings, use CheckBoxPreference as the standard.
        a = context.obtainStyledAttributes(attrs, android.R.styleable.CheckBoxPreference, 0, 0);
        mSummaryOn = a.getString(android.R.styleable.CheckBoxPreference_summaryOn);
        mSummaryOff = a.getString(android.R.styleable.CheckBoxPreference_summaryOff);
        a.recycle();
    
public EditPhoneNumberPreference(android.content.Context context)

        this(context, null);
    
Methods Summary
public java.lang.StringgetPhoneNumber()
Phone number handling code

        // return the phone number, after it has been stripped of all 
        // irrelevant text.
        return PhoneNumberUtils.stripSeparators(mPhoneNumber);
    
protected java.lang.StringgetStringValue()

        return ((isToggled() ? VALUE_ON : VALUE_OFF) + VALUE_SEPARATOR + getPhoneNumber());
    
public java.lang.CharSequencegetSummaryOff()

        return mSummaryOff;
    
public java.lang.CharSequencegetSummaryOn()

        return mSummaryOn;
    
public booleanisToggled()

        return mChecked;
    
protected voidonAddEditTextToDialogView(android.view.View dialogView, android.widget.EditText editText)
Overriding EditTextPreference's onAddEditTextToDialogView. This method attaches the EditText to the container specific to this preference's dialog layout.

        
        // look for the container object
        ViewGroup container = (ViewGroup) dialogView
                .findViewById(R.id.edit_container);
        
        // add the edittext to the container.
        if (container != null) {
            container.addView(editText, ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    
protected voidonBindDialogView(android.view.View view)

        // default the button clicked to be the cancel button.
        mButtonClicked = DialogInterface.BUTTON2;
        
        super.onBindDialogView(view);
        
        //get the edittext component within the number field
        EditText editText = getEditText();
        //get the contact pick button within the number field
        mContactPickButton = (ImageButton) view.findViewById(R.id.select_contact);
        
        //setup number entry
        if (editText != null) {
            // see if there is a means to get a default number,
            // and set it accordingly.
            if (mGetDefaultNumberListener != null) {
                String defaultNumber = mGetDefaultNumberListener.onGetDefaultNumber(this);
                if (defaultNumber != null) {
                    mPhoneNumber = defaultNumber;
                }
            }
            editText.setText(mPhoneNumber);
            editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
            editText.setKeyListener(DialerKeyListener.getInstance());
            editText.setOnFocusChangeListener(mDialogFocusChangeListener);
        }
        
        //set contact picker
        if (mContactPickButton != null) {
            mContactPickButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (mParentActivity != null) {
                        mParentActivity.startActivityForResult(mContactListIntent, mPrefId);
                    }
                }
            });
        }
    
protected voidonBindView(android.view.View view)

        super.onBindView(view);
        
        // Sync the summary view
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
        if (summaryView != null) {
            CharSequence sum;
            int vis;

            //set summary depending upon mode
            if (mConfirmationMode == CM_ACTIVATION) {
                if (mChecked) {
                    sum = (mSummaryOn == null) ? getSummary() : mSummaryOn;
                } else {
                    sum = (mSummaryOff == null) ? getSummary() : mSummaryOff;
                }
            } else {
                sum = getSummary();
            }
            
            if (sum != null) {
                summaryView.setText(sum);
                vis = View.VISIBLE;
            } else {
                vis = View.GONE;
            }
            
            if (vis != summaryView.getVisibility()) {
                summaryView.setVisibility(vis);
            }
        }
    
public voidonClick(android.content.DialogInterface dialog, int which)

        // The neutral button (button3) is always the toggle.
        if ((mConfirmationMode == CM_ACTIVATION) && (which == DialogInterface.BUTTON3)) {
            //flip the toggle if we are in the correct mode.
            setToggled(!isToggled());
        }
        // record the button that was clicked.
        mButtonClicked = which;
        super.onClick(dialog, which);
    
protected voidonDialogClosed(boolean positiveResult)

        // A positive result is technically either button1 or button3.
        if ((mButtonClicked == DialogInterface.BUTTON1) || 
                (mButtonClicked == DialogInterface.BUTTON3)){
            setPhoneNumber(getEditText().getText().toString());
            super.onDialogClosed(positiveResult);
            setText(getStringValue());
        } else {
            super.onDialogClosed(positiveResult);
        }
        
        // send the clicked button over to the listener.    
        if (mDialogOnClosedListener != null) {
            mDialogOnClosedListener.onDialogClosed(this, mButtonClicked);
        }
    
public voidonPickActivityResult(java.lang.String pickedValue)

        EditText editText = getEditText();
        if (editText != null) {
            editText.setText(pickedValue);
        }
    
protected voidonPrepareDialogBuilder(AlertDialog.Builder builder)

        // modified so that we just worry about the buttons being
        // displayed, since there is no need to hide the edittext 
        // field anymore.
        if (mConfirmationMode == CM_ACTIVATION) {
            if (mChecked) {
                builder.setPositiveButton(mChangeNumberText, this);
                builder.setNeutralButton(mDisableText, this);
            } else {
                builder.setPositiveButton(null, null);
                builder.setNeutralButton(mEnableText, this);
            }
        }
        // set the call icon on the title.
        builder.setIcon(R.drawable.ic_dialog_call);
    
protected voidonSetInitialValue(boolean restoreValue, java.lang.Object defaultValue)

        setValueFromString(restoreValue ? getPersistedString(getStringValue())
                : (String) defaultValue);
    
protected booleanpersistString(java.lang.String value)

    
        
        mEncodedText = value;
        return super.persistString(value);
    
public voidsetDialogOnClosedListener(com.android.phone.EditPhoneNumberPreference$OnDialogClosedListener l)

        mDialogOnClosedListener = l;
    
public voidsetDialogOnFocusChangeListener(View.OnFocusChangeListener l)

        mDialogFocusChangeListener = l;
    
public voidsetParentActivity(android.app.Activity parent, int identifier, com.android.phone.EditPhoneNumberPreference$GetDefaultNumberListener l)

        mParentActivity = parent;
        mPrefId = identifier;
        mGetDefaultNumberListener = l;
    
public voidsetParentActivity(android.app.Activity parent, int identifier)

        mParentActivity = parent;
        mPrefId = identifier;
        mGetDefaultNumberListener = null;
    
public com.android.phone.EditPhoneNumberPreferencesetPhoneNumber(java.lang.String number)

        mPhoneNumber = number;
        setText(getStringValue());
        notifyChanged();
        
        return this;
    
public com.android.phone.EditPhoneNumberPreferencesetSummaryOff(java.lang.CharSequence summary)

        mSummaryOff = summary;
        if (!isToggled()) {
            notifyChanged();
        }
        return this;
    
public com.android.phone.EditPhoneNumberPreferencesetSummaryOff(int summaryResId)

        return setSummaryOff(getContext().getString(summaryResId));
    
public com.android.phone.EditPhoneNumberPreferencesetSummaryOn(java.lang.CharSequence summary)

        mSummaryOn = summary;
        if (isToggled()) {
            notifyChanged();
        }
        return this;
    
public com.android.phone.EditPhoneNumberPreferencesetSummaryOn(int summaryResId)

        return setSummaryOn(getContext().getString(summaryResId));
    
public com.android.phone.EditPhoneNumberPreferencesetToggled(boolean checked)

        mChecked = checked;
        setText(getStringValue());
        notifyChanged();
        
        return this;
    
protected voidsetValueFromString(java.lang.String value)

        String[] inValues = value.split(":", 2);
        setToggled(inValues[0].equals(VALUE_ON));
        setPhoneNumber(inValues[1]);
    
public booleanshouldDisableDependents()
Decides how to disable dependents.

        // There is really only one case we care about, but for consistency
        // we fill out the dependency tree for all of the cases.  If this
        // is in activation mode (CF), we look for the encoded toggle value
        // in the string.  If this in confirm mode (VM), then we just 
        // examine the number field.
        // Note: The toggle value is stored in the string in an encoded 
        // manner (refer to setValueFromString and getStringValue below).
        boolean shouldDisable = false;
        if ((mConfirmationMode == CM_ACTIVATION) && (mEncodedText != null)) {
            String[] inValues = mEncodedText.split(":", 2);
            shouldDisable = inValues[0].equals(VALUE_ON);
        } else {
            shouldDisable = (TextUtils.isEmpty(mPhoneNumber) && (mConfirmationMode == CM_CONFIRM));
        }
        return shouldDisable;
    
public voidshowPhoneNumberDialog()
Externally visible method to bring up the dialog. Generally used when we are navigating the user to this preference.

        showDialog(null);