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

EditFdnContactScreen

public class EditFdnContactScreen extends android.app.Activity
Activity to let the user add or edit an FDN contact.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
private static final int
MENU_IMPORT
private static final int
MENU_DELETE
private static final String
INTENT_EXTRA_NAME
private static final String
INTENT_EXTRA_NUMBER
private static final int
PIN2_REQUEST_CODE
private String
mName
private String
mNumber
private String
mPin2
private boolean
mAddContact
private QueryHandler
mQueryHandler
private android.widget.EditText
mNameField
private android.widget.EditText
mNumberField
private android.widget.LinearLayout
mPinFieldContainer
private android.widget.Button
mButton
private android.os.Handler
mHandler
private static final int
CONTACTS_PICKER_CODE
request code when invoking subactivity
private static final String[]
NUM_PROJECTION
projection for phone number query
private static final android.content.Intent
CONTACT_IMPORT_INTENT
static intent to invoke phone number picker
private boolean
mDataBusy
flag to track saving state
private View.OnClickListener
mClicked
View.OnFocusChangeListener
mOnFocusChangeHandler
Constructors Summary
Methods Summary
private voidaddContact()

        if (DBG) log("addContact");

        if (!isValidNumber(getNumberFromTextField())) {
            handleResult(false, true);
            return;
        }

        Uri uri = getContentURI();

        ContentValues bundle = new ContentValues(3);
        bundle.put("tag", getNameFromTextField());
        bundle.put("number", getNumberFromTextField());
        bundle.put("pin2", mPin2);


        mQueryHandler = new QueryHandler(getContentResolver());
        mQueryHandler.startInsert(0, null, uri, bundle);
        displayProgress(true);
        showStatus(getResources().getText(R.string.adding_fdn_contact));
    
private voidauthenticatePin2()

        Intent intent = new Intent();
        intent.setClass(this, GetPin2Screen.class);
        startActivityForResult(intent, PIN2_REQUEST_CODE);
    
private voiddeleteSelected()
Handle the delete command, based upon the state of the Activity.

        // delete ONLY if this is NOT a new contact.
        if (!mAddContact) {
            Intent intent = new Intent();
            intent.setClass(this, DeleteFdnContactScreen.class);
            intent.putExtra(INTENT_EXTRA_NAME, mName);
            intent.putExtra(INTENT_EXTRA_NUMBER, mNumber);
            startActivity(intent);
        }
        finish();
    
private voiddisplayProgress(boolean flag)

        // indicate we are busy.
        mDataBusy = flag;
        getWindow().setFeatureInt(
                Window.FEATURE_INDETERMINATE_PROGRESS,
                mDataBusy ? PROGRESS_VISIBILITY_ON : PROGRESS_VISIBILITY_OFF);
        // make sure we don't allow calls to save when we're
        // not ready for them.
        mButton.setClickable(!mDataBusy);
    
private android.net.UrigetContentURI()

        return Uri.parse("content://sim/fdn");
    
private java.lang.StringgetNameFromTextField()

        return mNameField.getText().toString();
    
private java.lang.StringgetNumberFromTextField()

        return mNumberField.getText().toString();
    
private voidhandleResult(boolean success, boolean invalidNumber)

        if (success) {
            if (DBG) log("handleResult: success!");
            showStatus(getResources().getText(mAddContact ?
                    R.string.fdn_contact_added : R.string.fdn_contact_updated));
        } else {
            if (DBG) log("handleResult: failed!");
            if (invalidNumber)
                showStatus(getResources().getText(R.string.fdn_invalid_number));
            else
                showStatus(getResources().getText(R.string.pin2_invalid));
        }

        mHandler.postDelayed(new Runnable() {
            public void run() {
                finish();
            }
        }, 2000);

    
private booleanisValidNumber(java.lang.String number)

param
number is voice mail number
return
true if number length is less than 20-digit limit

         return (number.length() <= 20);
     
private voidlog(java.lang.String msg)

        Log.d(LOG_TAG, "[EditFdnContact] " + msg);
    
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent intent)
We now want to bring up the pin request screen AFTER the contact information is displayed, to help with user experience. Also, process the results from the contact picker.

        if (DBG) log("onActivityResult request:" + requestCode + " result:" + resultCode);

        switch (requestCode) {
            case PIN2_REQUEST_CODE:
                Bundle extras = (intent != null) ? intent.getExtras() : null;
                if (extras != null) {
                    mPin2 = extras.getString("pin2");
                    if (mAddContact) {
                        addContact();
                    } else {
                        updateContact();
                    }
                } else if (resultCode != RESULT_OK) {
                    // if they cancelled, then we just cancel too.
                    if (DBG) log("onActivityResult: cancelled.");
                    finish();
                }
                break;

            // look for the data associated with this number, and update
            // the display with it.
            case CONTACTS_PICKER_CODE:
                if (resultCode != RESULT_OK) {
                    if (DBG) log("onActivityResult: cancelled.");
                    return;
                }
                Cursor cursor = getContentResolver().query(intent.getData(),
                        NUM_PROJECTION, null, null, null);
                if ((cursor == null) || (!cursor.moveToFirst())) {
                    Log.w(LOG_TAG,"onActivityResult: bad contact data, no results found.");
                    return;
                }
                mNameField.setText(cursor.getString(0));
                mNumberField.setText(cursor.getString(1));
                break;
        }
    
protected voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        resolveIntent();

        getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.edit_fdn_contact_screen);
        setupView();
        setTitle(mAddContact ?
                R.string.add_fdn_contact : R.string.edit_fdn_contact);

        mDataBusy = false;
    
public booleanonCreateOptionsMenu(android.view.Menu menu)
Overridden to display the import and delete commands.

        super.onCreateOptionsMenu(menu);

        Resources r = getResources();

        // Added the icons to the context menu
        menu.add(0, MENU_IMPORT, 0, r.getString(R.string.importToFDNfromContacts))
                .setIcon(R.drawable.ic_menu_contact);
        menu.add(0, MENU_DELETE, 0, r.getString(R.string.menu_delete))
                .setIcon(android.R.drawable.ic_menu_delete);
        return true;
    
public booleanonOptionsItemSelected(android.view.MenuItem item)
Overridden to allow for handling of delete and import.

        switch (item.getItemId()) {
            case MENU_IMPORT:
                startActivityForResult(CONTACT_IMPORT_INTENT, CONTACTS_PICKER_CODE);
                return true;

            case MENU_DELETE:
                deleteSelected();
                return true;
        }

        return super.onOptionsItemSelected(item);
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)
Allow the menu to be opened ONLY if we're not busy.

        boolean result = super.onPrepareOptionsMenu(menu);
        return mDataBusy ? false : result;
    
private voidresolveIntent()

        Intent intent = getIntent();

        mName =  intent.getStringExtra(INTENT_EXTRA_NAME);
        mNumber =  intent.getStringExtra(INTENT_EXTRA_NUMBER);

        if (TextUtils.isEmpty(mName)) {
            mAddContact = true;
        }
    
private voidsetupView()
We have multiple layouts, one to indicate that the user needs to open the keyboard to enter information (if the keybord is hidden). So, we need to make sure that the layout here matches that in the layout file.

        mNameField = (EditText) findViewById(R.id.fdn_name);
        if (mNameField != null) {
            mNameField.setOnFocusChangeListener(mOnFocusChangeHandler);
            mNameField.setOnClickListener(mClicked);
        }

        mNumberField = (EditText) findViewById(R.id.fdn_number);
        if (mNumberField != null) {
            mNumberField.setKeyListener(DialerKeyListener.getInstance());
            mNumberField.setOnFocusChangeListener(mOnFocusChangeHandler);
            mNumberField.setOnClickListener(mClicked);
        }

        if (!mAddContact) {
            if (mNameField != null) {
                mNameField.setText(mName);
            }
            if (mNumberField != null) {
                mNumberField.setText(mNumber);
            }
        }

        mButton = (Button) findViewById(R.id.button);
        if (mButton != null) {
            mButton.setOnClickListener(mClicked);
        }

        mPinFieldContainer = (LinearLayout) findViewById(R.id.pinc);

    
private voidshowStatus(java.lang.CharSequence statusMsg)
Removed the status field, with preference to displaying a toast to match the rest of settings UI.

        if (statusMsg != null) {
            Toast.makeText(this, statusMsg, Toast.LENGTH_SHORT)
            .show();
        }
    
private voidupdateContact()

        if (DBG) log("updateContact");

        if (!isValidNumber(getNumberFromTextField())) {
            handleResult(false, true);
            return;
        }
        Uri uri = getContentURI();

        ContentValues bundle = new ContentValues();
        bundle.put("tag", mName);
        bundle.put("number", mNumber);
        bundle.put("newTag", getNameFromTextField());
        bundle.put("newNumber", getNumberFromTextField());
        bundle.put("pin2", mPin2);

        mQueryHandler = new QueryHandler(getContentResolver());
        mQueryHandler.startUpdate(0, null, uri, bundle, null, null);
        displayProgress(true);
        showStatus(getResources().getText(R.string.updating_fdn_contact));