blob: ba1f4fac0c303e4b0188b2a406b2b690c2916abf [file] [log] [blame]
Evan Millar45e0ed32009-06-01 16:44:38 -07001/*
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 Millar66388be2009-05-28 15:41:07 -070017package com.android.contacts;
18
19
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060020import com.android.contacts.model.ContactsSource;
21import com.android.contacts.util.Constants;
22
Evan Millar45e0ed32009-06-01 16:44:38 -070023import android.content.ContentResolver;
Evan Millar2c1cc832009-07-13 11:08:06 -070024import android.content.ContentUris;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070025import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070026import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070027import android.content.Intent;
Evan Millar45e0ed32009-06-01 16:44:38 -070028import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
Evan Millarf19104c2009-09-02 17:53:25 -070031import android.graphics.drawable.Drawable;
Neel Parekh2ad90a32009-09-20 19:08:50 -070032import android.net.Uri;
33import android.provider.ContactsContract.Contacts;
34import android.provider.ContactsContract.Data;
35import android.provider.ContactsContract.RawContacts;
Evan Millar66388be2009-05-28 15:41:07 -070036import android.provider.ContactsContract.CommonDataKinds.Email;
37import android.provider.ContactsContract.CommonDataKinds.Im;
38import android.provider.ContactsContract.CommonDataKinds.Organization;
39import android.provider.ContactsContract.CommonDataKinds.Phone;
Evan Millar2c1cc832009-07-13 11:08:06 -070040import android.provider.ContactsContract.CommonDataKinds.Photo;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070041import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Makoto Onukic710b0e2009-10-13 15:43:24 -070042import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070043import android.text.TextUtils;
Evan Millar11d628c2009-09-02 08:55:01 -070044import android.view.LayoutInflater;
45import android.view.View;
46import android.view.ViewGroup;
47import android.widget.ImageView;
48import android.widget.TextView;
Evan Millar66388be2009-05-28 15:41:07 -070049
Neel Parekh2ad90a32009-09-20 19:08:50 -070050import java.util.ArrayList;
51
Evan Millar66388be2009-05-28 15:41:07 -070052public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070053 private static final String TAG = "ContactsUtils";
Jeff Sharkey39261272009-06-03 19:15:09 -070054 /**
55 * Build the display title for the {@link Data#CONTENT_URI} entry in the
56 * provided cursor, assuming the given mimeType.
57 */
58 public static final CharSequence getDisplayLabel(Context context,
59 String mimeType, Cursor cursor) {
60 // Try finding the type and label for this mimetype
61 int colType;
62 int colLabel;
63
Jeff Sharkey39261272009-06-03 19:15:09 -070064 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Jeff Sharkey49d17b32009-09-07 02:14:21 -070065 || Constants.MIME_SMS_ADDRESS.equals(mimeType)) {
Jeff Sharkey39261272009-06-03 19:15:09 -070066 // Reset to phone mimetype so we generate a label for SMS case
67 mimeType = Phone.CONTENT_ITEM_TYPE;
68 colType = cursor.getColumnIndex(Phone.TYPE);
69 colLabel = cursor.getColumnIndex(Phone.LABEL);
70 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
71 colType = cursor.getColumnIndex(Email.TYPE);
72 colLabel = cursor.getColumnIndex(Email.LABEL);
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070073 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
74 colType = cursor.getColumnIndex(StructuredPostal.TYPE);
75 colLabel = cursor.getColumnIndex(StructuredPostal.LABEL);
Jeff Sharkey39261272009-06-03 19:15:09 -070076 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
77 colType = cursor.getColumnIndex(Organization.TYPE);
78 colLabel = cursor.getColumnIndex(Organization.LABEL);
79 } else {
80 return null;
81 }
82
83 final int type = cursor.getInt(colType);
84 final CharSequence label = cursor.getString(colLabel);
85
86 return getDisplayLabel(context, mimeType, type, label);
87 }
88
Evan Millar66388be2009-05-28 15:41:07 -070089 public static final CharSequence getDisplayLabel(Context context, String mimetype, int type,
90 CharSequence label) {
91 CharSequence display = "";
92 final int customType;
93 final int defaultType;
94 final int arrayResId;
95
96 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
97 defaultType = Phone.TYPE_HOME;
98 customType = Phone.TYPE_CUSTOM;
99 arrayResId = com.android.internal.R.array.phoneTypes;
100 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
101 defaultType = Email.TYPE_HOME;
102 customType = Email.TYPE_CUSTOM;
103 arrayResId = com.android.internal.R.array.emailAddressTypes;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700104 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) {
105 defaultType = StructuredPostal.TYPE_HOME;
106 customType = StructuredPostal.TYPE_CUSTOM;
Evan Millar66388be2009-05-28 15:41:07 -0700107 arrayResId = com.android.internal.R.array.postalAddressTypes;
108 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
Dmitri Plotnikov48cf72b2009-07-17 11:00:26 -0700109 defaultType = Organization.TYPE_WORK;
Evan Millar66388be2009-05-28 15:41:07 -0700110 customType = Organization.TYPE_CUSTOM;
111 arrayResId = com.android.internal.R.array.organizationTypes;
112 } else {
113 // Can't return display label for given mimetype.
114 return display;
115 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700116
Evan Millar66388be2009-05-28 15:41:07 -0700117 if (type != customType) {
118 CharSequence[] labels = context.getResources().getTextArray(arrayResId);
119 try {
120 display = labels[type - 1];
121 } catch (ArrayIndexOutOfBoundsException e) {
122 display = labels[defaultType - 1];
123 }
124 } else {
125 if (!TextUtils.isEmpty(label)) {
126 display = label;
127 }
128 }
129 return display;
130 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700131
Evan Millar45e0ed32009-06-01 16:44:38 -0700132 /**
133 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
134 * If the person's photo isn't present returns null.
135 *
136 * @param aggCursor the Cursor pointing to the data record containing the photo.
137 * @param bitmapColumnIndex the column index where the photo Uri is stored.
138 * @param options the decoding options, can be set to null
139 * @return the photo Bitmap
140 */
Evan Millar0a40ffa2009-06-18 16:49:08 -0700141 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700142 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700143 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700144 return null;
145 }
146
Evan Millar7911ff52009-07-21 15:55:18 -0700147 byte[] data = cursor.getBlob(bitmapColumnIndex);
Evan Millar45e0ed32009-06-01 16:44:38 -0700148 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
149 }
150
151 /**
152 * Loads a placeholder photo.
153 *
154 * @param placeholderImageResource the resource to use for the placeholder image
155 * @param context the Context
156 * @param options the decoding options, can be set to null
157 * @return the placeholder Bitmap.
158 */
159 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
160 BitmapFactory.Options options) {
161 if (placeholderImageResource == 0) {
162 return null;
163 }
164 return BitmapFactory.decodeResource(context.getResources(),
165 placeholderImageResource, options);
166 }
167
Evan Millar7911ff52009-07-21 15:55:18 -0700168 public static Bitmap loadContactPhoto(Context context, long photoId,
Evan Millar2c1cc832009-07-13 11:08:06 -0700169 BitmapFactory.Options options) {
170 Cursor photoCursor = null;
171 Bitmap photoBm = null;
172
173 try {
174 photoCursor = context.getContentResolver().query(
175 ContentUris.withAppendedId(Data.CONTENT_URI, photoId),
176 new String[] { Photo.PHOTO },
177 null, null, null);
178
179 if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) {
180 byte[] photoData = photoCursor.getBlob(0);
181 photoBm = BitmapFactory.decodeByteArray(photoData, 0,
182 photoData.length, options);
183 }
184 } finally {
185 if (photoCursor != null) {
186 photoCursor.close();
187 }
188 }
189
190 return photoBm;
191 }
192
Jeff Hamilton1bf258e2009-12-15 16:55:49 -0600193 // TODO find a proper place for the canonical version of these
194 public interface ProviderNames {
195 String YAHOO = "Yahoo";
196 String GTALK = "GTalk";
197 String MSN = "MSN";
198 String ICQ = "ICQ";
199 String AIM = "AIM";
200 String XMPP = "XMPP";
201 String JABBER = "JABBER";
202 String SKYPE = "SKYPE";
203 String QQ = "QQ";
204 }
205
Evan Millar66388be2009-05-28 15:41:07 -0700206 /**
207 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -0600208 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -0700209 * This is used for interacting with the IM application.
210 *
211 * @param protocol the protocol ID
212 * @return the provider name the IM app uses for the given protocol, or null if no
213 * provider is defined for the given protocol
214 * @hide
215 */
216 public static String lookupProviderNameFromId(int protocol) {
217 switch (protocol) {
218 case Im.PROTOCOL_GOOGLE_TALK:
219 return ProviderNames.GTALK;
220 case Im.PROTOCOL_AIM:
221 return ProviderNames.AIM;
222 case Im.PROTOCOL_MSN:
223 return ProviderNames.MSN;
224 case Im.PROTOCOL_YAHOO:
225 return ProviderNames.YAHOO;
226 case Im.PROTOCOL_ICQ:
227 return ProviderNames.ICQ;
228 case Im.PROTOCOL_JABBER:
229 return ProviderNames.JABBER;
230 case Im.PROTOCOL_SKYPE:
231 return ProviderNames.SKYPE;
232 case Im.PROTOCOL_QQ:
233 return ProviderNames.QQ;
234 }
235 return null;
236 }
237
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700238 /**
239 * Build {@link Intent} to launch an action for the given {@link Im} or
240 * {@link Email} row. Returns null when missing protocol or data.
241 */
242 public static Intent buildImIntent(ContentValues values) {
243 final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE));
Evan Millar43455182009-10-08 10:24:12 -0700244
245 if (!isEmail && !isProtocolValid(values)) {
246 return null;
247 }
248
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700249 final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL);
250
251 String host = values.getAsString(Im.CUSTOM_PROTOCOL);
252 String data = values.getAsString(isEmail ? Email.DATA : Im.DATA);
253 if (protocol != Im.PROTOCOL_CUSTOM) {
254 // Try bringing in a well-known host for specific protocols
255 host = ContactsUtils.lookupProviderNameFromId(protocol);
256 }
257
258 if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(data)) {
259 final String authority = host.toLowerCase();
260 final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
261 authority).appendPath(data).build();
262 return new Intent(Intent.ACTION_SENDTO, imUri);
263 } else {
264 return null;
265 }
266 }
267
Evan Millar43455182009-10-08 10:24:12 -0700268 private static boolean isProtocolValid(ContentValues values) {
269 String protocolString = values.getAsString(Im.PROTOCOL);
270 if (protocolString == null) {
271 return false;
272 }
273 try {
274 Integer.valueOf(protocolString);
275 } catch (NumberFormatException e) {
276 return false;
277 }
278 return true;
279 }
280
Evan Millar8a79cee2009-08-19 17:20:49 -0700281 public static long queryForContactId(ContentResolver cr, long rawContactId) {
282 Cursor contactIdCursor = null;
283 long contactId = -1;
284 try {
285 contactIdCursor = cr.query(RawContacts.CONTENT_URI,
286 new String[] {RawContacts.CONTACT_ID},
287 RawContacts._ID + "=" + rawContactId, null, null);
288 if (contactIdCursor != null && contactIdCursor.moveToFirst()) {
289 contactId = contactIdCursor.getLong(0);
290 }
291 } finally {
292 if (contactIdCursor != null) {
293 contactIdCursor.close();
294 }
295 }
296 return contactId;
297 }
Evan Millar11d628c2009-09-02 08:55:01 -0700298
Evan Millar2cd51002009-09-02 14:33:38 -0700299 public static String querySuperPrimaryPhone(ContentResolver cr, long contactId) {
300 Cursor c = null;
301 String phone = null;
302 try {
303 Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700304 Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY);
Evan Millar2cd51002009-09-02 14:33:38 -0700305
306 c = cr.query(dataUri,
307 new String[] {Phone.NUMBER},
308 Data.MIMETYPE + "=" + Phone.MIMETYPE +
309 " AND " + Data.IS_SUPER_PRIMARY + "=1",
310 null, null);
311 if (c != null && c.moveToFirst()) {
312 // Just return the first one.
313 phone = c.getString(0);
314 }
315 } finally {
316 if (c != null) {
317 c.close();
318 }
319 }
320 return phone;
321 }
322
323 public static long queryForRawContactId(ContentResolver cr, long contactId) {
324 Cursor rawContactIdCursor = null;
325 long rawContactId = -1;
326 try {
327 rawContactIdCursor = cr.query(RawContacts.CONTENT_URI,
328 new String[] {RawContacts._ID},
329 RawContacts.CONTACT_ID + "=" + contactId, null, null);
330 if (rawContactIdCursor != null && rawContactIdCursor.moveToFirst()) {
331 // Just return the first one.
332 rawContactId = rawContactIdCursor.getLong(0);
333 }
334 } finally {
335 if (rawContactIdCursor != null) {
336 rawContactIdCursor.close();
337 }
338 }
339 return rawContactId;
340 }
341
Neel Parekh2ad90a32009-09-20 19:08:50 -0700342 public static ArrayList<Long> queryForAllRawContactIds(ContentResolver cr, long contactId) {
343 Cursor rawContactIdCursor = null;
344 ArrayList<Long> rawContactIds = new ArrayList<Long>();
345 try {
346 rawContactIdCursor = cr.query(RawContacts.CONTENT_URI,
347 new String[] {RawContacts._ID},
348 RawContacts.CONTACT_ID + "=" + contactId, null, null);
349 if (rawContactIdCursor != null) {
350 while (rawContactIdCursor.moveToNext()) {
351 rawContactIds.add(rawContactIdCursor.getLong(0));
352 }
353 }
354 } finally {
355 if (rawContactIdCursor != null) {
356 rawContactIdCursor.close();
357 }
358 }
359 return rawContactIds;
360 }
361
Evan Millar11d628c2009-09-02 08:55:01 -0700362
363 /**
364 * Utility for creating a standard tab indicator view.
365 *
366 * @param parent The parent ViewGroup to attach the new view to.
367 * @param label The label to display in the tab indicator. If null, not label will be displayed.
368 * @param icon The icon to display. If null, no icon will be displayed.
369 * @return The tab indicator View.
370 */
371 public static View createTabIndicatorView(ViewGroup parent, CharSequence label, Drawable icon) {
372 final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(
373 Context.LAYOUT_INFLATER_SERVICE);
374 final View tabIndicator = inflater.inflate(R.layout.tab_indicator, parent, false);
375 tabIndicator.getBackground().setDither(true);
376
377 final TextView tv = (TextView) tabIndicator.findViewById(R.id.tab_title);
378 tv.setText(label);
379
380 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
381 iconView.setImageDrawable(icon);
382
383 return tabIndicator;
384 }
385
386 /**
387 * Utility for creating a standard tab indicator view.
388 *
Evan Millar11d628c2009-09-02 08:55:01 -0700389 * @param parent The parent ViewGroup to attach the new view to.
390 * @param source The {@link ContactsSource} to build the tab view from.
391 * @return The tab indicator View.
392 */
393 public static View createTabIndicatorView(ViewGroup parent, ContactsSource source) {
394 Drawable icon = null;
395 if (source != null) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700396 icon = source.getDisplayIcon(parent.getContext());
Evan Millar11d628c2009-09-02 08:55:01 -0700397 }
398 return createTabIndicatorView(parent, null, icon);
399 }
Evan Millar14fecb62009-09-09 09:23:12 -0700400
401 /**
402 * Kick off an intent to initiate a call.
Daisuke Miyakawa46befc32009-10-28 10:13:57 +0900403 *
404 * @param phoneNumber must not be null.
405 * @throws NullPointerException when the given argument is null.
Evan Millar14fecb62009-09-09 09:23:12 -0700406 */
407 public static void initiateCall(Context context, CharSequence phoneNumber) {
408 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
409 Uri.fromParts("tel", phoneNumber.toString(), null));
410 context.startActivity(intent);
411 }
412
413 /**
414 * Kick off an intent to initiate an Sms/Mms message.
Daisuke Miyakawa46befc32009-10-28 10:13:57 +0900415 *
416 * @param phoneNumber must not be null.
417 * @throws NullPointerException when the given argument is null.
Evan Millar14fecb62009-09-09 09:23:12 -0700418 */
419 public static void initiateSms(Context context, CharSequence phoneNumber) {
420 Intent intent = new Intent(Intent.ACTION_SENDTO,
421 Uri.fromParts("sms", phoneNumber.toString(), null));
422 context.startActivity(intent);
423 }
Jeff Sharkeye31dac82009-10-08 11:13:47 -0700424
425 /**
426 * Test if the given {@link CharSequence} contains any graphic characters,
427 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
428 */
429 public static boolean isGraphic(CharSequence str) {
430 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
431 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700432
433 /**
434 * Returns true if two objects are considered equal. Two null references are equal here.
435 */
436 public static boolean areObjectsEqual(Object a, Object b) {
437 return a == b || (a != null && a.equals(b));
438 }
439
440 /**
441 * Returns true if two data with mimetypes which represent values in contact entries are
442 * considered equal.
443 */
444 public static final boolean areDataEqual(Context context, CharSequence mimetype1,
445 CharSequence data1, CharSequence mimetype2, CharSequence data2) {
446 if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
447 && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
448 if (data1 == data2) {
449 return true;
450 }
451 if (data1 == null || data2 == null) {
452 return false;
453 }
454 return PhoneNumberUtils.compare(context, data1.toString(), data2.toString());
455 } else {
456 if (mimetype1 == mimetype2 && data1 == data2) {
457 return true;
458 }
459 return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
460 }
461 }
462
463 /**
464 * Returns true if two {@link Intent}s are both null, or have the same action.
465 */
466 public static final boolean areIntentActionEqual(Intent a, Intent b) {
467 if (a == b) {
468 return true;
469 }
470 if (a == null || b == null) {
471 return false;
472 }
473 return TextUtils.equals(a.getAction(), b.getAction());
474 }
Evan Millar66388be2009-05-28 15:41:07 -0700475}