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