FileDocCategorySizeDatePackage
RecipientsEditor.javaAPI DocAndroid 1.5 API9053Wed May 06 22:42:46 BST 2009com.android.mms.ui

RecipientsEditor

public class RecipientsEditor extends android.widget.MultiAutoCompleteTextView
Provide UI for editing the recipients of multi-media messages.

Fields Summary
private int
mLongPressedPosition
private final RecipientsEditorTokenizer
mTokenizer
Constructors Summary
public RecipientsEditor(android.content.Context context, android.util.AttributeSet attrs)


         
        super(context, attrs, android.R.attr.autoCompleteTextViewStyle);
        mTokenizer = new RecipientsEditorTokenizer(context, this);
        setTokenizer(mTokenizer);

        /*
         * The point of this TextWatcher is that when the user chooses
         * an address completion from the AutoCompleteTextView menu, it
         * is marked up with Annotation objects to tie it back to the
         * address book entry that it came from.  If the user then goes
         * back and edits that part of the text, it no longer corresponds
         * to that address book entry and needs to have the Annotations
         * claiming that it does removed.
         */
        addTextChangedListener(new TextWatcher() {
            private Annotation[] mAffected;

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
                mAffected = ((Spanned) s).getSpans(start, start + count,
                                                   Annotation.class);
            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int after) {

            }

            public void afterTextChanged(Editable s) {
                if (mAffected != null) {
                    for (Annotation a : mAffected) {
                        s.removeSpan(a);
                    }
                }

                mAffected = null;
            }
        });
    
Methods Summary
private static java.lang.StringgetAnnotation(android.text.Annotation[] a, java.lang.String key)

        for (int i = 0; i < a.length; i++) {
            if (a[i].getKey().equals(key)) {
                return a[i].getValue();
            }
        }

        return "";
    
protected android.view.ContextMenu.ContextMenuInfogetContextMenuInfo()

        if ((mLongPressedPosition >= 0)) {
            Spanned text = getText();
            if (mLongPressedPosition <= text.length()) {
                int start = mTokenizer.findTokenStart(text, mLongPressedPosition);
                int end = mTokenizer.findTokenEnd(text, start);

                if (end != start) {
                    Recipient r = getRecipientAt(getText(), start, end, mContext);
                    return new RecipientContextMenuInfo(r);
                }
            }
        }
        return null;
    
private static com.android.mms.ui.RecipientList.RecipientgetRecipientAt(android.text.Spanned sp, int start, int end, android.content.Context context)

        Annotation[] a = sp.getSpans(start, end, Annotation.class);
        String person_id = getAnnotation(a, "person_id");
        String name = getAnnotation(a, "name");
        String label = getAnnotation(a, "label");
        String bcc = getAnnotation(a, "bcc");
        String number = getAnnotation(a, "number");

        Recipient r = new Recipient();

        r.name = name;
        r.label = label;
        r.bcc = bcc.equals("true");
        r.number = TextUtils.isEmpty(number) ? TextUtils.substring(sp, start, end) : number;
        
        if (TextUtils.isEmpty(r.name) && Mms.isEmailAddress(r.number)) {
            ContactInfoCache cache = ContactInfoCache.getInstance();
            r.name = cache.getDisplayName(context, r.number);
        }

        r.nameAndNumber = Recipient.buildNameAndNumber(r.name, r.number);
        
        if (person_id.length() > 0) {
            r.person_id = Long.parseLong(person_id);
        } else {
            r.person_id = -1;
        }

        return r;
    
public RecipientListgetRecipientList()

        return mTokenizer.getRecipientList();
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

        if (isPopupShowing()) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_COMMA:
                    ListAdapter adapter = getAdapter();
                    // There is at least one item in the dropdown list
                    // when isPopupShowing() is true.
                    Object selectedItem = adapter.getItem(0);
                    replaceText(convertSelectionToString(selectedItem));
                    dismissDropDown();
                    return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    
public booleanonTouchEvent(android.view.MotionEvent ev)

        final int action = ev.getAction();
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();

        if (action == MotionEvent.ACTION_DOWN) {
            mLongPressedPosition = pointToPosition(x, y);
        }

        return super.onTouchEvent(ev);
    
private intpointToPosition(int x, int y)

        x -= getCompoundPaddingLeft();
        y -= getExtendedPaddingTop();


        x += getScrollX();
        y += getScrollY();

        Layout layout = getLayout();
        if (layout == null) {
            return -1;
        }

        int line = layout.getLineForVertical(y);
        int off = layout.getOffsetForHorizontal(line, x);

        return off;
    
public voidpopulate(RecipientList list)

        SpannableStringBuilder sb = new SpannableStringBuilder();

        Iterator<Recipient> iter = list.iterator();
        while (iter.hasNext()) {
            if (sb.length() != 0) {
                sb.append(", ");
            }

            Recipient r = iter.next();
            sb.append(r.toToken());
        }

        setText(sb);