Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 17 | package com.android.contacts; |
| 18 | |
| 19 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 20 | import java.io.ByteArrayInputStream; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 21 | import android.provider.ContactsContract.Data; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 22 | import java.io.InputStream; |
| 23 | |
| 24 | import android.net.Uri; |
| 25 | import android.content.ContentResolver; |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 26 | import android.content.ContentValues; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.database.Cursor; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
| 31 | import android.provider.Contacts; |
| 32 | import android.provider.Contacts.Photos; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 34 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 35 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 37 | import android.provider.ContactsContract.CommonDataKinds.Postal; |
| 38 | import android.provider.Im.ProviderNames; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 39 | import android.database.Cursor; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 41 | import android.util.Log; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 42 | |
| 43 | public class ContactsUtils { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 44 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Build the display title for the {@link Data#CONTENT_URI} entry in the |
| 47 | * provided cursor, assuming the given mimeType. |
| 48 | */ |
| 49 | public static final CharSequence getDisplayLabel(Context context, |
| 50 | String mimeType, Cursor cursor) { |
| 51 | // Try finding the type and label for this mimetype |
| 52 | int colType; |
| 53 | int colLabel; |
| 54 | |
| 55 | // TODO: move the SMS mime-type to a central location |
| 56 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
| 57 | || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) { |
| 58 | // Reset to phone mimetype so we generate a label for SMS case |
| 59 | mimeType = Phone.CONTENT_ITEM_TYPE; |
| 60 | colType = cursor.getColumnIndex(Phone.TYPE); |
| 61 | colLabel = cursor.getColumnIndex(Phone.LABEL); |
| 62 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 63 | colType = cursor.getColumnIndex(Email.TYPE); |
| 64 | colLabel = cursor.getColumnIndex(Email.LABEL); |
| 65 | } else if (Postal.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 66 | colType = cursor.getColumnIndex(Postal.TYPE); |
| 67 | colLabel = cursor.getColumnIndex(Postal.LABEL); |
| 68 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 69 | colType = cursor.getColumnIndex(Organization.TYPE); |
| 70 | colLabel = cursor.getColumnIndex(Organization.LABEL); |
| 71 | } else { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | final int type = cursor.getInt(colType); |
| 76 | final CharSequence label = cursor.getString(colLabel); |
| 77 | |
| 78 | return getDisplayLabel(context, mimeType, type, label); |
| 79 | } |
| 80 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 81 | public static final CharSequence getDisplayLabel(Context context, String mimetype, int type, |
| 82 | CharSequence label) { |
| 83 | CharSequence display = ""; |
| 84 | final int customType; |
| 85 | final int defaultType; |
| 86 | final int arrayResId; |
| 87 | |
| 88 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 89 | defaultType = Phone.TYPE_HOME; |
| 90 | customType = Phone.TYPE_CUSTOM; |
| 91 | arrayResId = com.android.internal.R.array.phoneTypes; |
| 92 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 93 | defaultType = Email.TYPE_HOME; |
| 94 | customType = Email.TYPE_CUSTOM; |
| 95 | arrayResId = com.android.internal.R.array.emailAddressTypes; |
| 96 | } else if (Postal.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 97 | defaultType = Postal.TYPE_HOME; |
| 98 | customType = Postal.TYPE_CUSTOM; |
| 99 | arrayResId = com.android.internal.R.array.postalAddressTypes; |
| 100 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 101 | defaultType = Organization.TYPE_HOME; |
| 102 | customType = Organization.TYPE_CUSTOM; |
| 103 | arrayResId = com.android.internal.R.array.organizationTypes; |
| 104 | } else { |
| 105 | // Can't return display label for given mimetype. |
| 106 | return display; |
| 107 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 108 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 109 | if (type != customType) { |
| 110 | CharSequence[] labels = context.getResources().getTextArray(arrayResId); |
| 111 | try { |
| 112 | display = labels[type - 1]; |
| 113 | } catch (ArrayIndexOutOfBoundsException e) { |
| 114 | display = labels[defaultType - 1]; |
| 115 | } |
| 116 | } else { |
| 117 | if (!TextUtils.isEmpty(label)) { |
| 118 | display = label; |
| 119 | } |
| 120 | } |
| 121 | return display; |
| 122 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 123 | |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 124 | public static String encodePredefinedImProtocol(int protocol) { |
| 125 | return "pre:" + protocol; |
| 126 | } |
| 127 | |
| 128 | public static String encodeCustomImProtocol(String protocolString) { |
| 129 | return "custom:" + protocolString; |
| 130 | } |
| 131 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 132 | public static Object decodeImProtocol(String encodedString) { |
| 133 | if (encodedString == null) { |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | if (encodedString.startsWith("pre:")) { |
| 138 | return Integer.parseInt(encodedString.substring(4)); |
| 139 | } |
| 140 | |
| 141 | if (encodedString.startsWith("custom:")) { |
| 142 | return encodedString.substring(7); |
| 143 | } |
| 144 | |
| 145 | throw new IllegalArgumentException( |
| 146 | "the value is not a valid encoded protocol, " + encodedString); |
| 147 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 148 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Opens an InputStream for the person's photo and returns the photo as a Bitmap. |
| 151 | * If the person's photo isn't present returns null. |
| 152 | * |
| 153 | * @param aggCursor the Cursor pointing to the data record containing the photo. |
| 154 | * @param bitmapColumnIndex the column index where the photo Uri is stored. |
| 155 | * @param options the decoding options, can be set to null |
| 156 | * @return the photo Bitmap |
| 157 | */ |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 158 | public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex, |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 159 | BitmapFactory.Options options) { |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 160 | if (cursor == null) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 161 | return null; |
| 162 | } |
| 163 | |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 164 | byte[] data = cursor.getBlob(bitmapColumnIndex);; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 165 | return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Loads a placeholder photo. |
| 170 | * |
| 171 | * @param placeholderImageResource the resource to use for the placeholder image |
| 172 | * @param context the Context |
| 173 | * @param options the decoding options, can be set to null |
| 174 | * @return the placeholder Bitmap. |
| 175 | */ |
| 176 | public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context, |
| 177 | BitmapFactory.Options options) { |
| 178 | if (placeholderImageResource == 0) { |
| 179 | return null; |
| 180 | } |
| 181 | return BitmapFactory.decodeResource(context.getResources(), |
| 182 | placeholderImageResource, options); |
| 183 | } |
| 184 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 185 | /** |
| 186 | * This looks up the provider name defined in |
| 187 | * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id. |
| 188 | * This is used for interacting with the IM application. |
| 189 | * |
| 190 | * @param protocol the protocol ID |
| 191 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 192 | * provider is defined for the given protocol |
| 193 | * @hide |
| 194 | */ |
| 195 | public static String lookupProviderNameFromId(int protocol) { |
| 196 | switch (protocol) { |
| 197 | case Im.PROTOCOL_GOOGLE_TALK: |
| 198 | return ProviderNames.GTALK; |
| 199 | case Im.PROTOCOL_AIM: |
| 200 | return ProviderNames.AIM; |
| 201 | case Im.PROTOCOL_MSN: |
| 202 | return ProviderNames.MSN; |
| 203 | case Im.PROTOCOL_YAHOO: |
| 204 | return ProviderNames.YAHOO; |
| 205 | case Im.PROTOCOL_ICQ: |
| 206 | return ProviderNames.ICQ; |
| 207 | case Im.PROTOCOL_JABBER: |
| 208 | return ProviderNames.JABBER; |
| 209 | case Im.PROTOCOL_SKYPE: |
| 210 | return ProviderNames.SKYPE; |
| 211 | case Im.PROTOCOL_QQ: |
| 212 | return ProviderNames.QQ; |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | } |