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 | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 20 | import com.android.contacts.model.ContactsSource; |
Jeff Sharkey | 802b205 | 2009-08-04 14:21:06 -0700 | [diff] [blame] | 21 | import com.android.contacts.ui.FastTrackWindow; |
| 22 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 23 | import java.io.ByteArrayInputStream; |
Evan Millar | 2cd5100 | 2009-09-02 14:33:38 -0700 | [diff] [blame^] | 24 | |
| 25 | import android.provider.Contacts.People.Phones; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 26 | import android.provider.ContactsContract.Data; |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 27 | import android.provider.ContactsContract.RawContacts; |
| 28 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 29 | import java.io.InputStream; |
| 30 | |
| 31 | import android.net.Uri; |
| 32 | import android.content.ContentResolver; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 33 | import android.content.ContentUris; |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 34 | import android.content.ContentValues; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 35 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 38 | import android.database.Cursor; |
| 39 | import android.graphics.Bitmap; |
| 40 | import android.graphics.BitmapFactory; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 42 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 43 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 44 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
Evan Millar | 2cd5100 | 2009-09-02 14:33:38 -0700 | [diff] [blame^] | 47 | import android.provider.ContactsContract.Contacts; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 48 | import android.provider.Im.ProviderNames; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 49 | import android.database.Cursor; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 50 | import android.text.TextUtils; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 51 | import android.util.Log; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 52 | import android.view.LayoutInflater; |
| 53 | import android.view.View; |
| 54 | import android.view.ViewGroup; |
| 55 | import android.widget.ImageView; |
| 56 | import android.widget.TextView; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 57 | |
| 58 | public class ContactsUtils { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 59 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 60 | private static final String TAG = "ContactsUtils"; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Build the display title for the {@link Data#CONTENT_URI} entry in the |
| 63 | * provided cursor, assuming the given mimeType. |
| 64 | */ |
| 65 | public static final CharSequence getDisplayLabel(Context context, |
| 66 | String mimeType, Cursor cursor) { |
| 67 | // Try finding the type and label for this mimetype |
| 68 | int colType; |
| 69 | int colLabel; |
| 70 | |
| 71 | // TODO: move the SMS mime-type to a central location |
| 72 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
| 73 | || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) { |
| 74 | // Reset to phone mimetype so we generate a label for SMS case |
| 75 | mimeType = Phone.CONTENT_ITEM_TYPE; |
| 76 | colType = cursor.getColumnIndex(Phone.TYPE); |
| 77 | colLabel = cursor.getColumnIndex(Phone.LABEL); |
| 78 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 79 | colType = cursor.getColumnIndex(Email.TYPE); |
| 80 | colLabel = cursor.getColumnIndex(Email.LABEL); |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 81 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 82 | colType = cursor.getColumnIndex(StructuredPostal.TYPE); |
| 83 | colLabel = cursor.getColumnIndex(StructuredPostal.LABEL); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 84 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 85 | colType = cursor.getColumnIndex(Organization.TYPE); |
| 86 | colLabel = cursor.getColumnIndex(Organization.LABEL); |
| 87 | } else { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | final int type = cursor.getInt(colType); |
| 92 | final CharSequence label = cursor.getString(colLabel); |
| 93 | |
| 94 | return getDisplayLabel(context, mimeType, type, label); |
| 95 | } |
| 96 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 97 | public static final CharSequence getDisplayLabel(Context context, String mimetype, int type, |
| 98 | CharSequence label) { |
| 99 | CharSequence display = ""; |
| 100 | final int customType; |
| 101 | final int defaultType; |
| 102 | final int arrayResId; |
| 103 | |
| 104 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 105 | defaultType = Phone.TYPE_HOME; |
| 106 | customType = Phone.TYPE_CUSTOM; |
| 107 | arrayResId = com.android.internal.R.array.phoneTypes; |
| 108 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 109 | defaultType = Email.TYPE_HOME; |
| 110 | customType = Email.TYPE_CUSTOM; |
| 111 | arrayResId = com.android.internal.R.array.emailAddressTypes; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 112 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 113 | defaultType = StructuredPostal.TYPE_HOME; |
| 114 | customType = StructuredPostal.TYPE_CUSTOM; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 115 | arrayResId = com.android.internal.R.array.postalAddressTypes; |
| 116 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { |
Dmitri Plotnikov | 48cf72b | 2009-07-17 11:00:26 -0700 | [diff] [blame] | 117 | defaultType = Organization.TYPE_WORK; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 118 | customType = Organization.TYPE_CUSTOM; |
| 119 | arrayResId = com.android.internal.R.array.organizationTypes; |
| 120 | } else { |
| 121 | // Can't return display label for given mimetype. |
| 122 | return display; |
| 123 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 124 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 125 | if (type != customType) { |
| 126 | CharSequence[] labels = context.getResources().getTextArray(arrayResId); |
| 127 | try { |
| 128 | display = labels[type - 1]; |
| 129 | } catch (ArrayIndexOutOfBoundsException e) { |
| 130 | display = labels[defaultType - 1]; |
| 131 | } |
| 132 | } else { |
| 133 | if (!TextUtils.isEmpty(label)) { |
| 134 | display = label; |
| 135 | } |
| 136 | } |
| 137 | return display; |
| 138 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 139 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Opens an InputStream for the person's photo and returns the photo as a Bitmap. |
| 142 | * If the person's photo isn't present returns null. |
| 143 | * |
| 144 | * @param aggCursor the Cursor pointing to the data record containing the photo. |
| 145 | * @param bitmapColumnIndex the column index where the photo Uri is stored. |
| 146 | * @param options the decoding options, can be set to null |
| 147 | * @return the photo Bitmap |
| 148 | */ |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 149 | public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex, |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 150 | BitmapFactory.Options options) { |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 151 | if (cursor == null) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 152 | return null; |
| 153 | } |
| 154 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 155 | byte[] data = cursor.getBlob(bitmapColumnIndex); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 156 | return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Loads a placeholder photo. |
| 161 | * |
| 162 | * @param placeholderImageResource the resource to use for the placeholder image |
| 163 | * @param context the Context |
| 164 | * @param options the decoding options, can be set to null |
| 165 | * @return the placeholder Bitmap. |
| 166 | */ |
| 167 | public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context, |
| 168 | BitmapFactory.Options options) { |
| 169 | if (placeholderImageResource == 0) { |
| 170 | return null; |
| 171 | } |
| 172 | return BitmapFactory.decodeResource(context.getResources(), |
| 173 | placeholderImageResource, options); |
| 174 | } |
| 175 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 176 | public static Bitmap loadContactPhoto(Context context, long photoId, |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 177 | BitmapFactory.Options options) { |
| 178 | Cursor photoCursor = null; |
| 179 | Bitmap photoBm = null; |
| 180 | |
| 181 | try { |
| 182 | photoCursor = context.getContentResolver().query( |
| 183 | ContentUris.withAppendedId(Data.CONTENT_URI, photoId), |
| 184 | new String[] { Photo.PHOTO }, |
| 185 | null, null, null); |
| 186 | |
| 187 | if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) { |
| 188 | byte[] photoData = photoCursor.getBlob(0); |
| 189 | photoBm = BitmapFactory.decodeByteArray(photoData, 0, |
| 190 | photoData.length, options); |
| 191 | } |
| 192 | } finally { |
| 193 | if (photoCursor != null) { |
| 194 | photoCursor.close(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return photoBm; |
| 199 | } |
| 200 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 201 | /** |
| 202 | * This looks up the provider name defined in |
| 203 | * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id. |
| 204 | * This is used for interacting with the IM application. |
| 205 | * |
| 206 | * @param protocol the protocol ID |
| 207 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 208 | * provider is defined for the given protocol |
| 209 | * @hide |
| 210 | */ |
| 211 | public static String lookupProviderNameFromId(int protocol) { |
| 212 | switch (protocol) { |
| 213 | case Im.PROTOCOL_GOOGLE_TALK: |
| 214 | return ProviderNames.GTALK; |
| 215 | case Im.PROTOCOL_AIM: |
| 216 | return ProviderNames.AIM; |
| 217 | case Im.PROTOCOL_MSN: |
| 218 | return ProviderNames.MSN; |
| 219 | case Im.PROTOCOL_YAHOO: |
| 220 | return ProviderNames.YAHOO; |
| 221 | case Im.PROTOCOL_ICQ: |
| 222 | return ProviderNames.ICQ; |
| 223 | case Im.PROTOCOL_JABBER: |
| 224 | return ProviderNames.JABBER; |
| 225 | case Im.PROTOCOL_SKYPE: |
| 226 | return ProviderNames.SKYPE; |
| 227 | case Im.PROTOCOL_QQ: |
| 228 | return ProviderNames.QQ; |
| 229 | } |
| 230 | return null; |
| 231 | } |
| 232 | |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 233 | public static Intent getPhotoPickIntent() { |
| 234 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); |
| 235 | intent.setType("image/*"); |
| 236 | intent.putExtra("crop", "true"); |
| 237 | intent.putExtra("aspectX", 1); |
| 238 | intent.putExtra("aspectY", 1); |
| 239 | intent.putExtra("outputX", 96); |
| 240 | intent.putExtra("outputY", 96); |
| 241 | intent.putExtra("return-data", true); |
| 242 | return intent; |
| 243 | } |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 244 | |
| 245 | public static long queryForContactId(ContentResolver cr, long rawContactId) { |
| 246 | Cursor contactIdCursor = null; |
| 247 | long contactId = -1; |
| 248 | try { |
| 249 | contactIdCursor = cr.query(RawContacts.CONTENT_URI, |
| 250 | new String[] {RawContacts.CONTACT_ID}, |
| 251 | RawContacts._ID + "=" + rawContactId, null, null); |
| 252 | if (contactIdCursor != null && contactIdCursor.moveToFirst()) { |
| 253 | contactId = contactIdCursor.getLong(0); |
| 254 | } |
| 255 | } finally { |
| 256 | if (contactIdCursor != null) { |
| 257 | contactIdCursor.close(); |
| 258 | } |
| 259 | } |
| 260 | return contactId; |
| 261 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 262 | |
Evan Millar | 2cd5100 | 2009-09-02 14:33:38 -0700 | [diff] [blame^] | 263 | public static String querySuperPrimaryPhone(ContentResolver cr, long contactId) { |
| 264 | Cursor c = null; |
| 265 | String phone = null; |
| 266 | try { |
| 267 | Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); |
| 268 | Uri dataUri = Uri.withAppendedPath(baseUri, "data"); |
| 269 | |
| 270 | c = cr.query(dataUri, |
| 271 | new String[] {Phone.NUMBER}, |
| 272 | Data.MIMETYPE + "=" + Phone.MIMETYPE + |
| 273 | " AND " + Data.IS_SUPER_PRIMARY + "=1", |
| 274 | null, null); |
| 275 | if (c != null && c.moveToFirst()) { |
| 276 | // Just return the first one. |
| 277 | phone = c.getString(0); |
| 278 | } |
| 279 | } finally { |
| 280 | if (c != null) { |
| 281 | c.close(); |
| 282 | } |
| 283 | } |
| 284 | return phone; |
| 285 | } |
| 286 | |
| 287 | public static long queryForRawContactId(ContentResolver cr, long contactId) { |
| 288 | Cursor rawContactIdCursor = null; |
| 289 | long rawContactId = -1; |
| 290 | try { |
| 291 | rawContactIdCursor = cr.query(RawContacts.CONTENT_URI, |
| 292 | new String[] {RawContacts._ID}, |
| 293 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
| 294 | if (rawContactIdCursor != null && rawContactIdCursor.moveToFirst()) { |
| 295 | // Just return the first one. |
| 296 | rawContactId = rawContactIdCursor.getLong(0); |
| 297 | } |
| 298 | } finally { |
| 299 | if (rawContactIdCursor != null) { |
| 300 | rawContactIdCursor.close(); |
| 301 | } |
| 302 | } |
| 303 | return rawContactId; |
| 304 | } |
| 305 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * Utility for creating a standard tab indicator view. |
| 309 | * |
| 310 | * @param parent The parent ViewGroup to attach the new view to. |
| 311 | * @param label The label to display in the tab indicator. If null, not label will be displayed. |
| 312 | * @param icon The icon to display. If null, no icon will be displayed. |
| 313 | * @return The tab indicator View. |
| 314 | */ |
| 315 | public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { |
| 316 | final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService( |
| 317 | Context.LAYOUT_INFLATER_SERVICE); |
| 318 | final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); |
| 319 | tabIndicator.getBackground().setDither(true); |
| 320 | |
| 321 | final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); |
| 322 | tv.setText(label); |
| 323 | |
| 324 | final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); |
| 325 | iconView.setImageDrawable(icon); |
| 326 | |
| 327 | return tabIndicator; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Utility for creating a standard tab indicator view. |
| 332 | * |
| 333 | * @param context The label to display in the tab indicator. If null, not label will be displayed. |
| 334 | * @param parent The parent ViewGroup to attach the new view to. |
| 335 | * @param source The {@link ContactsSource} to build the tab view from. |
| 336 | * @return The tab indicator View. |
| 337 | */ |
| 338 | public static View createTabIndicatorView(ViewGroup parent, ContactsSource source) { |
| 339 | Drawable icon = null; |
| 340 | if (source != null) { |
| 341 | final String packageName = source.resPackageName; |
| 342 | if (source.iconRes > 0) { |
| 343 | try { |
| 344 | final Context authContext = parent.getContext(). |
| 345 | createPackageContext(packageName, 0); |
| 346 | icon = authContext.getResources().getDrawable(source.iconRes); |
| 347 | |
| 348 | } catch (PackageManager.NameNotFoundException e) { |
| 349 | Log.d(TAG, "error getting the Package Context for " + packageName, e); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return createTabIndicatorView(parent, null, icon); |
| 354 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 355 | } |