Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame^] | 1 | package com.android.contacts; |
| 2 | |
| 3 | |
| 4 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 5 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 6 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 7 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 8 | import android.provider.ContactsContract.CommonDataKinds.Postal; |
| 9 | import android.provider.Im.ProviderNames; |
| 10 | |
| 11 | import android.content.Context; |
| 12 | import android.text.TextUtils; |
| 13 | |
| 14 | public class ContactsUtils { |
| 15 | |
| 16 | public static final CharSequence getDisplayLabel(Context context, String mimetype, int type, |
| 17 | CharSequence label) { |
| 18 | CharSequence display = ""; |
| 19 | final int customType; |
| 20 | final int defaultType; |
| 21 | final int arrayResId; |
| 22 | |
| 23 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 24 | defaultType = Phone.TYPE_HOME; |
| 25 | customType = Phone.TYPE_CUSTOM; |
| 26 | arrayResId = com.android.internal.R.array.phoneTypes; |
| 27 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 28 | defaultType = Email.TYPE_HOME; |
| 29 | customType = Email.TYPE_CUSTOM; |
| 30 | arrayResId = com.android.internal.R.array.emailAddressTypes; |
| 31 | } else if (Postal.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 32 | defaultType = Postal.TYPE_HOME; |
| 33 | customType = Postal.TYPE_CUSTOM; |
| 34 | arrayResId = com.android.internal.R.array.postalAddressTypes; |
| 35 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 36 | defaultType = Organization.TYPE_HOME; |
| 37 | customType = Organization.TYPE_CUSTOM; |
| 38 | arrayResId = com.android.internal.R.array.organizationTypes; |
| 39 | } else { |
| 40 | // Can't return display label for given mimetype. |
| 41 | return display; |
| 42 | } |
| 43 | |
| 44 | if (type != customType) { |
| 45 | CharSequence[] labels = context.getResources().getTextArray(arrayResId); |
| 46 | try { |
| 47 | display = labels[type - 1]; |
| 48 | } catch (ArrayIndexOutOfBoundsException e) { |
| 49 | display = labels[defaultType - 1]; |
| 50 | } |
| 51 | } else { |
| 52 | if (!TextUtils.isEmpty(label)) { |
| 53 | display = label; |
| 54 | } |
| 55 | } |
| 56 | return display; |
| 57 | } |
| 58 | |
| 59 | public static Object decodeImProtocol(String encodedString) { |
| 60 | if (encodedString == null) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | if (encodedString.startsWith("pre:")) { |
| 65 | return Integer.parseInt(encodedString.substring(4)); |
| 66 | } |
| 67 | |
| 68 | if (encodedString.startsWith("custom:")) { |
| 69 | return encodedString.substring(7); |
| 70 | } |
| 71 | |
| 72 | throw new IllegalArgumentException( |
| 73 | "the value is not a valid encoded protocol, " + encodedString); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * This looks up the provider name defined in |
| 80 | * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id. |
| 81 | * This is used for interacting with the IM application. |
| 82 | * |
| 83 | * @param protocol the protocol ID |
| 84 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 85 | * provider is defined for the given protocol |
| 86 | * @hide |
| 87 | */ |
| 88 | public static String lookupProviderNameFromId(int protocol) { |
| 89 | switch (protocol) { |
| 90 | case Im.PROTOCOL_GOOGLE_TALK: |
| 91 | return ProviderNames.GTALK; |
| 92 | case Im.PROTOCOL_AIM: |
| 93 | return ProviderNames.AIM; |
| 94 | case Im.PROTOCOL_MSN: |
| 95 | return ProviderNames.MSN; |
| 96 | case Im.PROTOCOL_YAHOO: |
| 97 | return ProviderNames.YAHOO; |
| 98 | case Im.PROTOCOL_ICQ: |
| 99 | return ProviderNames.ICQ; |
| 100 | case Im.PROTOCOL_JABBER: |
| 101 | return ProviderNames.JABBER; |
| 102 | case Im.PROTOCOL_SKYPE: |
| 103 | return ProviderNames.SKYPE; |
| 104 | case Im.PROTOCOL_QQ: |
| 105 | return ProviderNames.QQ; |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | } |