FileDocCategorySizeDatePackage
Address.javaAPI DocAndroid 1.5 API8097Wed May 06 22:42:46 BST 2009com.android.email.mail

Address

public class Address extends Object

Fields Summary
String
mAddress
String
mPersonal
Constructors Summary
public Address(String address, String personal)

        this.mAddress = address;
        this.mPersonal = personal;
    
public Address(String address)

        this.mAddress = address;
    
Methods Summary
public booleanequals(java.lang.Object o)

        if (o instanceof Address) {
            return getAddress().equals(((Address) o).getAddress());
        }
        return super.equals(o);
    
public java.lang.StringgetAddress()

        return mAddress;
    
public java.lang.StringgetPersonal()

        return mPersonal;
    
public static java.lang.Stringpack(com.android.email.mail.Address[] addresses)
Packs an address list into a String that is very quick to read and parse. Packed lists can be unpacked with unpackAddressList() The packed list is a comma separated list of: URLENCODE(address)[;URLENCODE(personal)]

param
list
return

        if (addresses == null) {
            return null;
        } else if (addresses.length == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0, count = addresses.length; i < count; i++) {
            Address address = addresses[i];
            try {
                sb.append(URLEncoder.encode(address.getAddress(), "UTF-8"));
                if (address.getPersonal() != null) {
                    sb.append(';");
                    sb.append(URLEncoder.encode(address.getPersonal(), "UTF-8"));
                }
                if (i < count - 1) {
                    sb.append(',");
                }
            }
            catch (UnsupportedEncodingException uee) {
                return null;
            }
        }
        return sb.toString();
    
public static com.android.email.mail.Address[]parse(java.lang.String addressList)
Parse a comma separated list of addresses in RFC-822 format and return an array of Address objects.

param
addressList
return
An array of 0 or more Addresses.

        if (addressList == null || addressList.length() == 0) {
            return new Address[] {};
        }
        ArrayList<Address> addresses = new ArrayList<Address>();
        try {
            MailboxList parsedList = AddressList.parse(addressList).flatten();
            for (int i = 0, count = parsedList.size(); i < count; i++) {
                org.apache.james.mime4j.field.address.Address address = parsedList.get(i);
                if (address instanceof NamedMailbox) {
                    NamedMailbox namedMailbox = (NamedMailbox)address;
                    addresses.add(new Address(namedMailbox.getLocalPart() + "@"
                            + namedMailbox.getDomain(), namedMailbox.getName()));
                } else if (address instanceof Mailbox) {
                    Mailbox mailbox = (Mailbox)address;
                    addresses.add(new Address(mailbox.getLocalPart() + "@" + mailbox.getDomain()));
                } else {
                    Log.e(Email.LOG_TAG, "Unknown address type from Mime4J: "
                            + address.getClass().toString());
                }

            }
        } catch (ParseException pe) {
        }
        return addresses.toArray(new Address[] {});
    
public voidsetAddress(java.lang.String address)

        this.mAddress = address;
    
public voidsetPersonal(java.lang.String personal)

        this.mPersonal = personal;
    
public java.lang.StringtoFriendly()

return
the personal portion of this Address, or the address portion if the personal portion is not available

        if (mPersonal != null && mPersonal.length() > 0) {
            return  mPersonal;
        }
        else {
            return mAddress;
        }
    
public static java.lang.StringtoFriendly(com.android.email.mail.Address[] addresses)
Creates a comma-delimited list of addresses in the "friendly" format (see toFriendly() for details on the per-address conversion).

param
addresses array of Address[] values
return
A comma-delimited string listing all of the addresses supplied. Null if source was null or empty.

        if (addresses == null || addresses.length == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < addresses.length; i++) {
            if (i > 0) {
                sb.append(',");
            }
            sb.append(addresses[i].toFriendly());
        }
        return sb.toString();
    
public static java.lang.StringtoString(com.android.email.mail.Address[] addresses)

        if (addresses == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < addresses.length; i++) {
            sb.append(addresses[i].toString());
            if (i < addresses.length - 1) {
                sb.append(',");
            }
        }
        return sb.toString();
    
public java.lang.StringtoString()

        if (mPersonal != null) {
            if (mPersonal.matches(".*[\\(\\)<>@,;:\\\\\".\\[\\]].*")) {
                return Utility.quoteString(mPersonal) + " <" + mAddress + ">";
            } else {
                return mPersonal + " <" + mAddress + ">";
            }
        } else {
            return mAddress;
        }
    
public static com.android.email.mail.Address[]unpack(java.lang.String addressList)
Unpacks an address list previously packed with packAddressList()

param
list
return

        if (addressList == null || addressList.length() == 0) {
            return new Address[] { };
        }
        ArrayList<Address> addresses = new ArrayList<Address>();
        int length = addressList.length();
        int pairStartIndex = 0;
        int pairEndIndex = 0;
        int addressEndIndex = 0;
        while (pairStartIndex < length) {
            pairEndIndex = addressList.indexOf(',", pairStartIndex);
            if (pairEndIndex == -1) {
                pairEndIndex = length;
            }
            addressEndIndex = addressList.indexOf(';", pairStartIndex);
            String address = null;
            String personal = null;
            if (addressEndIndex == -1 || addressEndIndex > pairEndIndex) {
                address = Utility.fastUrlDecode(addressList.substring(pairStartIndex, pairEndIndex));
            }
            else {
                address = Utility.fastUrlDecode(addressList.substring(pairStartIndex, addressEndIndex));
                personal = Utility.fastUrlDecode(addressList.substring(addressEndIndex + 1, pairEndIndex));
            }
            addresses.add(new Address(address, personal));
            pairStartIndex = pairEndIndex + 1;
        }
        return addresses.toArray(new Address[] { });