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 | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.database.Cursor; |
| 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.BitmapFactory; |
| 32 | import android.provider.Contacts; |
| 33 | import android.provider.Contacts.Photos; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 34 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 35 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 37 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 39 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 40 | import android.provider.Im.ProviderNames; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 41 | import android.database.Cursor; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 42 | import android.text.TextUtils; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 43 | import android.util.Log; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 44 | |
| 45 | public class ContactsUtils { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 46 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Build the display title for the {@link Data#CONTENT_URI} entry in the |
| 49 | * provided cursor, assuming the given mimeType. |
| 50 | */ |
| 51 | public static final CharSequence getDisplayLabel(Context context, |
| 52 | String mimeType, Cursor cursor) { |
| 53 | // Try finding the type and label for this mimetype |
| 54 | int colType; |
| 55 | int colLabel; |
| 56 | |
| 57 | // TODO: move the SMS mime-type to a central location |
| 58 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
| 59 | || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) { |
| 60 | // Reset to phone mimetype so we generate a label for SMS case |
| 61 | mimeType = Phone.CONTENT_ITEM_TYPE; |
| 62 | colType = cursor.getColumnIndex(Phone.TYPE); |
| 63 | colLabel = cursor.getColumnIndex(Phone.LABEL); |
| 64 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 65 | colType = cursor.getColumnIndex(Email.TYPE); |
| 66 | colLabel = cursor.getColumnIndex(Email.LABEL); |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 67 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 68 | colType = cursor.getColumnIndex(StructuredPostal.TYPE); |
| 69 | colLabel = cursor.getColumnIndex(StructuredPostal.LABEL); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 70 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 71 | colType = cursor.getColumnIndex(Organization.TYPE); |
| 72 | colLabel = cursor.getColumnIndex(Organization.LABEL); |
| 73 | } else { |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | final int type = cursor.getInt(colType); |
| 78 | final CharSequence label = cursor.getString(colLabel); |
| 79 | |
| 80 | return getDisplayLabel(context, mimeType, type, label); |
| 81 | } |
| 82 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 83 | public static final CharSequence getDisplayLabel(Context context, String mimetype, int type, |
| 84 | CharSequence label) { |
| 85 | CharSequence display = ""; |
| 86 | final int customType; |
| 87 | final int defaultType; |
| 88 | final int arrayResId; |
| 89 | |
| 90 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 91 | defaultType = Phone.TYPE_HOME; |
| 92 | customType = Phone.TYPE_CUSTOM; |
| 93 | arrayResId = com.android.internal.R.array.phoneTypes; |
| 94 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 95 | defaultType = Email.TYPE_HOME; |
| 96 | customType = Email.TYPE_CUSTOM; |
| 97 | arrayResId = com.android.internal.R.array.emailAddressTypes; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 98 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 99 | defaultType = StructuredPostal.TYPE_HOME; |
| 100 | customType = StructuredPostal.TYPE_CUSTOM; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 101 | arrayResId = com.android.internal.R.array.postalAddressTypes; |
| 102 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { |
Dmitri Plotnikov | 48cf72b | 2009-07-17 11:00:26 -0700 | [diff] [blame] | 103 | defaultType = Organization.TYPE_WORK; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 104 | customType = Organization.TYPE_CUSTOM; |
| 105 | arrayResId = com.android.internal.R.array.organizationTypes; |
| 106 | } else { |
| 107 | // Can't return display label for given mimetype. |
| 108 | return display; |
| 109 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 110 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 111 | if (type != customType) { |
| 112 | CharSequence[] labels = context.getResources().getTextArray(arrayResId); |
| 113 | try { |
| 114 | display = labels[type - 1]; |
| 115 | } catch (ArrayIndexOutOfBoundsException e) { |
| 116 | display = labels[defaultType - 1]; |
| 117 | } |
| 118 | } else { |
| 119 | if (!TextUtils.isEmpty(label)) { |
| 120 | display = label; |
| 121 | } |
| 122 | } |
| 123 | return display; |
| 124 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 125 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Opens an InputStream for the person's photo and returns the photo as a Bitmap. |
| 128 | * If the person's photo isn't present returns null. |
| 129 | * |
| 130 | * @param aggCursor the Cursor pointing to the data record containing the photo. |
| 131 | * @param bitmapColumnIndex the column index where the photo Uri is stored. |
| 132 | * @param options the decoding options, can be set to null |
| 133 | * @return the photo Bitmap |
| 134 | */ |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 135 | public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex, |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 136 | BitmapFactory.Options options) { |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 137 | if (cursor == null) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 138 | return null; |
| 139 | } |
| 140 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 141 | byte[] data = cursor.getBlob(bitmapColumnIndex); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 142 | return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Loads a placeholder photo. |
| 147 | * |
| 148 | * @param placeholderImageResource the resource to use for the placeholder image |
| 149 | * @param context the Context |
| 150 | * @param options the decoding options, can be set to null |
| 151 | * @return the placeholder Bitmap. |
| 152 | */ |
| 153 | public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context, |
| 154 | BitmapFactory.Options options) { |
| 155 | if (placeholderImageResource == 0) { |
| 156 | return null; |
| 157 | } |
| 158 | return BitmapFactory.decodeResource(context.getResources(), |
| 159 | placeholderImageResource, options); |
| 160 | } |
| 161 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 162 | public static Bitmap loadContactPhoto(Context context, long photoId, |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 163 | BitmapFactory.Options options) { |
| 164 | Cursor photoCursor = null; |
| 165 | Bitmap photoBm = null; |
| 166 | |
| 167 | try { |
| 168 | photoCursor = context.getContentResolver().query( |
| 169 | ContentUris.withAppendedId(Data.CONTENT_URI, photoId), |
| 170 | new String[] { Photo.PHOTO }, |
| 171 | null, null, null); |
| 172 | |
| 173 | if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) { |
| 174 | byte[] photoData = photoCursor.getBlob(0); |
| 175 | photoBm = BitmapFactory.decodeByteArray(photoData, 0, |
| 176 | photoData.length, options); |
| 177 | } |
| 178 | } finally { |
| 179 | if (photoCursor != null) { |
| 180 | photoCursor.close(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return photoBm; |
| 185 | } |
| 186 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 187 | /** |
| 188 | * This looks up the provider name defined in |
| 189 | * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id. |
| 190 | * This is used for interacting with the IM application. |
| 191 | * |
| 192 | * @param protocol the protocol ID |
| 193 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 194 | * provider is defined for the given protocol |
| 195 | * @hide |
| 196 | */ |
| 197 | public static String lookupProviderNameFromId(int protocol) { |
| 198 | switch (protocol) { |
| 199 | case Im.PROTOCOL_GOOGLE_TALK: |
| 200 | return ProviderNames.GTALK; |
| 201 | case Im.PROTOCOL_AIM: |
| 202 | return ProviderNames.AIM; |
| 203 | case Im.PROTOCOL_MSN: |
| 204 | return ProviderNames.MSN; |
| 205 | case Im.PROTOCOL_YAHOO: |
| 206 | return ProviderNames.YAHOO; |
| 207 | case Im.PROTOCOL_ICQ: |
| 208 | return ProviderNames.ICQ; |
| 209 | case Im.PROTOCOL_JABBER: |
| 210 | return ProviderNames.JABBER; |
| 211 | case Im.PROTOCOL_SKYPE: |
| 212 | return ProviderNames.SKYPE; |
| 213 | case Im.PROTOCOL_QQ: |
| 214 | return ProviderNames.QQ; |
| 215 | } |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | } |