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 android.content.ContentResolver; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 21 | import android.content.ContentUris; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 22 | import android.content.ContentValues; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 25 | import android.database.Cursor; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.BitmapFactory; |
Evan Millar | f19104c | 2009-09-02 17:53:25 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.provider.ContactsContract.Contacts; |
| 31 | import android.provider.ContactsContract.Data; |
| 32 | import android.provider.ContactsContract.RawContacts; |
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; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 39 | import android.provider.Im.ProviderNames; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 40 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 41 | import android.text.TextUtils; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
| 45 | import android.widget.ImageView; |
| 46 | import android.widget.TextView; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 47 | |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 48 | import com.android.contacts.model.ContactsSource; |
| 49 | import com.android.contacts.util.Constants; |
| 50 | |
| 51 | import java.util.ArrayList; |
| 52 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 53 | public class ContactsUtils { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 54 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 55 | private static final String TAG = "ContactsUtils"; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Build the display title for the {@link Data#CONTENT_URI} entry in the |
| 58 | * provided cursor, assuming the given mimeType. |
| 59 | */ |
| 60 | public static final CharSequence getDisplayLabel(Context context, |
| 61 | String mimeType, Cursor cursor) { |
| 62 | // Try finding the type and label for this mimetype |
| 63 | int colType; |
| 64 | int colLabel; |
| 65 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 66 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 67 | || Constants.MIME_SMS_ADDRESS.equals(mimeType)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 68 | // Reset to phone mimetype so we generate a label for SMS case |
| 69 | mimeType = Phone.CONTENT_ITEM_TYPE; |
| 70 | colType = cursor.getColumnIndex(Phone.TYPE); |
| 71 | colLabel = cursor.getColumnIndex(Phone.LABEL); |
| 72 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 73 | colType = cursor.getColumnIndex(Email.TYPE); |
| 74 | colLabel = cursor.getColumnIndex(Email.LABEL); |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 75 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 76 | colType = cursor.getColumnIndex(StructuredPostal.TYPE); |
| 77 | colLabel = cursor.getColumnIndex(StructuredPostal.LABEL); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 78 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 79 | colType = cursor.getColumnIndex(Organization.TYPE); |
| 80 | colLabel = cursor.getColumnIndex(Organization.LABEL); |
| 81 | } else { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | final int type = cursor.getInt(colType); |
| 86 | final CharSequence label = cursor.getString(colLabel); |
| 87 | |
| 88 | return getDisplayLabel(context, mimeType, type, label); |
| 89 | } |
| 90 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 91 | public static final CharSequence getDisplayLabel(Context context, String mimetype, int type, |
| 92 | CharSequence label) { |
| 93 | CharSequence display = ""; |
| 94 | final int customType; |
| 95 | final int defaultType; |
| 96 | final int arrayResId; |
| 97 | |
| 98 | if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 99 | defaultType = Phone.TYPE_HOME; |
| 100 | customType = Phone.TYPE_CUSTOM; |
| 101 | arrayResId = com.android.internal.R.array.phoneTypes; |
| 102 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 103 | defaultType = Email.TYPE_HOME; |
| 104 | customType = Email.TYPE_CUSTOM; |
| 105 | arrayResId = com.android.internal.R.array.emailAddressTypes; |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 106 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) { |
| 107 | defaultType = StructuredPostal.TYPE_HOME; |
| 108 | customType = StructuredPostal.TYPE_CUSTOM; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 109 | arrayResId = com.android.internal.R.array.postalAddressTypes; |
| 110 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) { |
Dmitri Plotnikov | 48cf72b | 2009-07-17 11:00:26 -0700 | [diff] [blame] | 111 | defaultType = Organization.TYPE_WORK; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 112 | customType = Organization.TYPE_CUSTOM; |
| 113 | arrayResId = com.android.internal.R.array.organizationTypes; |
| 114 | } else { |
| 115 | // Can't return display label for given mimetype. |
| 116 | return display; |
| 117 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 118 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 119 | if (type != customType) { |
| 120 | CharSequence[] labels = context.getResources().getTextArray(arrayResId); |
| 121 | try { |
| 122 | display = labels[type - 1]; |
| 123 | } catch (ArrayIndexOutOfBoundsException e) { |
| 124 | display = labels[defaultType - 1]; |
| 125 | } |
| 126 | } else { |
| 127 | if (!TextUtils.isEmpty(label)) { |
| 128 | display = label; |
| 129 | } |
| 130 | } |
| 131 | return display; |
| 132 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 133 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Opens an InputStream for the person's photo and returns the photo as a Bitmap. |
| 136 | * If the person's photo isn't present returns null. |
| 137 | * |
| 138 | * @param aggCursor the Cursor pointing to the data record containing the photo. |
| 139 | * @param bitmapColumnIndex the column index where the photo Uri is stored. |
| 140 | * @param options the decoding options, can be set to null |
| 141 | * @return the photo Bitmap |
| 142 | */ |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 143 | public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex, |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 144 | BitmapFactory.Options options) { |
Evan Millar | 0a40ffa | 2009-06-18 16:49:08 -0700 | [diff] [blame] | 145 | if (cursor == null) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 146 | return null; |
| 147 | } |
| 148 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 149 | byte[] data = cursor.getBlob(bitmapColumnIndex); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 150 | return BitmapFactory.decodeByteArray(data, 0, data.length, options); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Loads a placeholder photo. |
| 155 | * |
| 156 | * @param placeholderImageResource the resource to use for the placeholder image |
| 157 | * @param context the Context |
| 158 | * @param options the decoding options, can be set to null |
| 159 | * @return the placeholder Bitmap. |
| 160 | */ |
| 161 | public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context, |
| 162 | BitmapFactory.Options options) { |
| 163 | if (placeholderImageResource == 0) { |
| 164 | return null; |
| 165 | } |
| 166 | return BitmapFactory.decodeResource(context.getResources(), |
| 167 | placeholderImageResource, options); |
| 168 | } |
| 169 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 170 | public static Bitmap loadContactPhoto(Context context, long photoId, |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 171 | BitmapFactory.Options options) { |
| 172 | Cursor photoCursor = null; |
| 173 | Bitmap photoBm = null; |
| 174 | |
| 175 | try { |
| 176 | photoCursor = context.getContentResolver().query( |
| 177 | ContentUris.withAppendedId(Data.CONTENT_URI, photoId), |
| 178 | new String[] { Photo.PHOTO }, |
| 179 | null, null, null); |
| 180 | |
| 181 | if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) { |
| 182 | byte[] photoData = photoCursor.getBlob(0); |
| 183 | photoBm = BitmapFactory.decodeByteArray(photoData, 0, |
| 184 | photoData.length, options); |
| 185 | } |
| 186 | } finally { |
| 187 | if (photoCursor != null) { |
| 188 | photoCursor.close(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return photoBm; |
| 193 | } |
| 194 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 195 | /** |
| 196 | * This looks up the provider name defined in |
| 197 | * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id. |
| 198 | * This is used for interacting with the IM application. |
| 199 | * |
| 200 | * @param protocol the protocol ID |
| 201 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 202 | * provider is defined for the given protocol |
| 203 | * @hide |
| 204 | */ |
| 205 | public static String lookupProviderNameFromId(int protocol) { |
| 206 | switch (protocol) { |
| 207 | case Im.PROTOCOL_GOOGLE_TALK: |
| 208 | return ProviderNames.GTALK; |
| 209 | case Im.PROTOCOL_AIM: |
| 210 | return ProviderNames.AIM; |
| 211 | case Im.PROTOCOL_MSN: |
| 212 | return ProviderNames.MSN; |
| 213 | case Im.PROTOCOL_YAHOO: |
| 214 | return ProviderNames.YAHOO; |
| 215 | case Im.PROTOCOL_ICQ: |
| 216 | return ProviderNames.ICQ; |
| 217 | case Im.PROTOCOL_JABBER: |
| 218 | return ProviderNames.JABBER; |
| 219 | case Im.PROTOCOL_SKYPE: |
| 220 | return ProviderNames.SKYPE; |
| 221 | case Im.PROTOCOL_QQ: |
| 222 | return ProviderNames.QQ; |
| 223 | } |
| 224 | return null; |
| 225 | } |
| 226 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Build {@link Intent} to launch an action for the given {@link Im} or |
| 229 | * {@link Email} row. Returns null when missing protocol or data. |
| 230 | */ |
| 231 | public static Intent buildImIntent(ContentValues values) { |
| 232 | final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE)); |
Evan Millar | 4345518 | 2009-10-08 10:24:12 -0700 | [diff] [blame] | 233 | |
| 234 | if (!isEmail && !isProtocolValid(values)) { |
| 235 | return null; |
| 236 | } |
| 237 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 238 | final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL); |
| 239 | |
| 240 | String host = values.getAsString(Im.CUSTOM_PROTOCOL); |
| 241 | String data = values.getAsString(isEmail ? Email.DATA : Im.DATA); |
| 242 | if (protocol != Im.PROTOCOL_CUSTOM) { |
| 243 | // Try bringing in a well-known host for specific protocols |
| 244 | host = ContactsUtils.lookupProviderNameFromId(protocol); |
| 245 | } |
| 246 | |
| 247 | if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(data)) { |
| 248 | final String authority = host.toLowerCase(); |
| 249 | final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority( |
| 250 | authority).appendPath(data).build(); |
| 251 | return new Intent(Intent.ACTION_SENDTO, imUri); |
| 252 | } else { |
| 253 | return null; |
| 254 | } |
| 255 | } |
| 256 | |
Evan Millar | 4345518 | 2009-10-08 10:24:12 -0700 | [diff] [blame] | 257 | private static boolean isProtocolValid(ContentValues values) { |
| 258 | String protocolString = values.getAsString(Im.PROTOCOL); |
| 259 | if (protocolString == null) { |
| 260 | return false; |
| 261 | } |
| 262 | try { |
| 263 | Integer.valueOf(protocolString); |
| 264 | } catch (NumberFormatException e) { |
| 265 | return false; |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 270 | public static Intent getPhotoPickIntent() { |
| 271 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); |
| 272 | intent.setType("image/*"); |
| 273 | intent.putExtra("crop", "true"); |
| 274 | intent.putExtra("aspectX", 1); |
| 275 | intent.putExtra("aspectY", 1); |
| 276 | intent.putExtra("outputX", 96); |
| 277 | intent.putExtra("outputY", 96); |
| 278 | intent.putExtra("return-data", true); |
| 279 | return intent; |
| 280 | } |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 281 | |
| 282 | public static long queryForContactId(ContentResolver cr, long rawContactId) { |
| 283 | Cursor contactIdCursor = null; |
| 284 | long contactId = -1; |
| 285 | try { |
| 286 | contactIdCursor = cr.query(RawContacts.CONTENT_URI, |
| 287 | new String[] {RawContacts.CONTACT_ID}, |
| 288 | RawContacts._ID + "=" + rawContactId, null, null); |
| 289 | if (contactIdCursor != null && contactIdCursor.moveToFirst()) { |
| 290 | contactId = contactIdCursor.getLong(0); |
| 291 | } |
| 292 | } finally { |
| 293 | if (contactIdCursor != null) { |
| 294 | contactIdCursor.close(); |
| 295 | } |
| 296 | } |
| 297 | return contactId; |
| 298 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 299 | |
Evan Millar | 2cd5100 | 2009-09-02 14:33:38 -0700 | [diff] [blame] | 300 | public static String querySuperPrimaryPhone(ContentResolver cr, long contactId) { |
| 301 | Cursor c = null; |
| 302 | String phone = null; |
| 303 | try { |
| 304 | Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 305 | Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY); |
Evan Millar | 2cd5100 | 2009-09-02 14:33:38 -0700 | [diff] [blame] | 306 | |
| 307 | c = cr.query(dataUri, |
| 308 | new String[] {Phone.NUMBER}, |
| 309 | Data.MIMETYPE + "=" + Phone.MIMETYPE + |
| 310 | " AND " + Data.IS_SUPER_PRIMARY + "=1", |
| 311 | null, null); |
| 312 | if (c != null && c.moveToFirst()) { |
| 313 | // Just return the first one. |
| 314 | phone = c.getString(0); |
| 315 | } |
| 316 | } finally { |
| 317 | if (c != null) { |
| 318 | c.close(); |
| 319 | } |
| 320 | } |
| 321 | return phone; |
| 322 | } |
| 323 | |
| 324 | public static long queryForRawContactId(ContentResolver cr, long contactId) { |
| 325 | Cursor rawContactIdCursor = null; |
| 326 | long rawContactId = -1; |
| 327 | try { |
| 328 | rawContactIdCursor = cr.query(RawContacts.CONTENT_URI, |
| 329 | new String[] {RawContacts._ID}, |
| 330 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
| 331 | if (rawContactIdCursor != null && rawContactIdCursor.moveToFirst()) { |
| 332 | // Just return the first one. |
| 333 | rawContactId = rawContactIdCursor.getLong(0); |
| 334 | } |
| 335 | } finally { |
| 336 | if (rawContactIdCursor != null) { |
| 337 | rawContactIdCursor.close(); |
| 338 | } |
| 339 | } |
| 340 | return rawContactId; |
| 341 | } |
| 342 | |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 343 | public static ArrayList<Long> queryForAllRawContactIds(ContentResolver cr, long contactId) { |
| 344 | Cursor rawContactIdCursor = null; |
| 345 | ArrayList<Long> rawContactIds = new ArrayList<Long>(); |
| 346 | try { |
| 347 | rawContactIdCursor = cr.query(RawContacts.CONTENT_URI, |
| 348 | new String[] {RawContacts._ID}, |
| 349 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
| 350 | if (rawContactIdCursor != null) { |
| 351 | while (rawContactIdCursor.moveToNext()) { |
| 352 | rawContactIds.add(rawContactIdCursor.getLong(0)); |
| 353 | } |
| 354 | } |
| 355 | } finally { |
| 356 | if (rawContactIdCursor != null) { |
| 357 | rawContactIdCursor.close(); |
| 358 | } |
| 359 | } |
| 360 | return rawContactIds; |
| 361 | } |
| 362 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * Utility for creating a standard tab indicator view. |
| 366 | * |
| 367 | * @param parent The parent ViewGroup to attach the new view to. |
| 368 | * @param label The label to display in the tab indicator. If null, not label will be displayed. |
| 369 | * @param icon The icon to display. If null, no icon will be displayed. |
| 370 | * @return The tab indicator View. |
| 371 | */ |
| 372 | public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) { |
| 373 | final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService( |
| 374 | Context.LAYOUT_INFLATER_SERVICE); |
| 375 | final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false); |
| 376 | tabIndicator.getBackground().setDither(true); |
| 377 | |
| 378 | final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title); |
| 379 | tv.setText(label); |
| 380 | |
| 381 | final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon); |
| 382 | iconView.setImageDrawable(icon); |
| 383 | |
| 384 | return tabIndicator; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Utility for creating a standard tab indicator view. |
| 389 | * |
| 390 | * @param context The label to display in the tab indicator. If null, not label will be displayed. |
| 391 | * @param parent The parent ViewGroup to attach the new view to. |
| 392 | * @param source The {@link ContactsSource} to build the tab view from. |
| 393 | * @return The tab indicator View. |
| 394 | */ |
| 395 | public static View createTabIndicatorView(ViewGroup parent, ContactsSource source) { |
| 396 | Drawable icon = null; |
| 397 | if (source != null) { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 398 | icon = source.getDisplayIcon(parent.getContext()); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 399 | } |
| 400 | return createTabIndicatorView(parent, null, icon); |
| 401 | } |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * Kick off an intent to initiate a call. |
| 405 | */ |
| 406 | public static void initiateCall(Context context, CharSequence phoneNumber) { |
| 407 | Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 408 | Uri.fromParts("tel", phoneNumber.toString(), null)); |
| 409 | context.startActivity(intent); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Kick off an intent to initiate an Sms/Mms message. |
| 414 | */ |
| 415 | public static void initiateSms(Context context, CharSequence phoneNumber) { |
| 416 | Intent intent = new Intent(Intent.ACTION_SENDTO, |
| 417 | Uri.fromParts("sms", phoneNumber.toString(), null)); |
| 418 | context.startActivity(intent); |
| 419 | } |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 420 | |
| 421 | /** |
| 422 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 423 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 424 | */ |
| 425 | public static boolean isGraphic(CharSequence str) { |
| 426 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 427 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 428 | |
| 429 | /** |
| 430 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 431 | */ |
| 432 | public static boolean areObjectsEqual(Object a, Object b) { |
| 433 | return a == b || (a != null && a.equals(b)); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Returns true if two data with mimetypes which represent values in contact entries are |
| 438 | * considered equal. |
| 439 | */ |
| 440 | public static final boolean areDataEqual(Context context, CharSequence mimetype1, |
| 441 | CharSequence data1, CharSequence mimetype2, CharSequence data2) { |
| 442 | if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1) |
| 443 | && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) { |
| 444 | if (data1 == data2) { |
| 445 | return true; |
| 446 | } |
| 447 | if (data1 == null || data2 == null) { |
| 448 | return false; |
| 449 | } |
| 450 | return PhoneNumberUtils.compare(context, data1.toString(), data2.toString()); |
| 451 | } else { |
| 452 | if (mimetype1 == mimetype2 && data1 == data2) { |
| 453 | return true; |
| 454 | } |
| 455 | return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 461 | */ |
| 462 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 463 | if (a == b) { |
| 464 | return true; |
| 465 | } |
| 466 | if (a == null || b == null) { |
| 467 | return false; |
| 468 | } |
| 469 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 470 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 471 | } |