blob: 33bbf1c28fc9cb21e8507b100639c50b42baebcb [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 Sharkey802b2052009-08-04 14:21:06 -070020import com.android.contacts.ui.FastTrackWindow;
21
Evan Millar45e0ed32009-06-01 16:44:38 -070022import java.io.ByteArrayInputStream;
Jeff Sharkey39261272009-06-03 19:15:09 -070023import android.provider.ContactsContract.Data;
Evan Millar8a79cee2009-08-19 17:20:49 -070024import android.provider.ContactsContract.RawContacts;
25
Evan Millar45e0ed32009-06-01 16:44:38 -070026import java.io.InputStream;
27
28import android.net.Uri;
29import android.content.ContentResolver;
Evan Millar2c1cc832009-07-13 11:08:06 -070030import android.content.ContentUris;
Evan Millar7e4accf2009-06-08 10:43:26 -070031import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070032import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070033import android.content.Intent;
Evan Millar45e0ed32009-06-01 16:44:38 -070034import android.database.Cursor;
35import android.graphics.Bitmap;
36import android.graphics.BitmapFactory;
37import android.provider.Contacts;
38import android.provider.Contacts.Photos;
Evan Millar66388be2009-05-28 15:41:07 -070039import android.provider.ContactsContract.CommonDataKinds.Email;
40import android.provider.ContactsContract.CommonDataKinds.Im;
41import android.provider.ContactsContract.CommonDataKinds.Organization;
42import android.provider.ContactsContract.CommonDataKinds.Phone;
Evan Millar2c1cc832009-07-13 11:08:06 -070043import android.provider.ContactsContract.CommonDataKinds.Photo;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070044import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Evan Millar66388be2009-05-28 15:41:07 -070045import android.provider.Im.ProviderNames;
Jeff Sharkey39261272009-06-03 19:15:09 -070046import android.database.Cursor;
Evan Millar66388be2009-05-28 15:41:07 -070047import android.text.TextUtils;
Jeff Sharkey39261272009-06-03 19:15:09 -070048import android.util.Log;
Evan Millar66388be2009-05-28 15:41:07 -070049
50public class ContactsUtils {
Evan Millar45e0ed32009-06-01 16:44:38 -070051
Jeff Sharkey39261272009-06-03 19:15:09 -070052 /**
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 Sharkeyc6ad3ab2009-07-21 19:30:15 -070072 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
73 colType = cursor.getColumnIndex(StructuredPostal.TYPE);
74 colLabel = cursor.getColumnIndex(StructuredPostal.LABEL);
Jeff Sharkey39261272009-06-03 19:15:09 -070075 } 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 Millar66388be2009-05-28 15:41:07 -070088 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 Sharkeyc6ad3ab2009-07-21 19:30:15 -0700103 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) {
104 defaultType = StructuredPostal.TYPE_HOME;
105 customType = StructuredPostal.TYPE_CUSTOM;
Evan Millar66388be2009-05-28 15:41:07 -0700106 arrayResId = com.android.internal.R.array.postalAddressTypes;
107 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
Dmitri Plotnikov48cf72b2009-07-17 11:00:26 -0700108 defaultType = Organization.TYPE_WORK;
Evan Millar66388be2009-05-28 15:41:07 -0700109 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 Millar45e0ed32009-06-01 16:44:38 -0700115
Evan Millar66388be2009-05-28 15:41:07 -0700116 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 Millar45e0ed32009-06-01 16:44:38 -0700130
Evan Millar45e0ed32009-06-01 16:44:38 -0700131 /**
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 Millar0a40ffa2009-06-18 16:49:08 -0700140 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700141 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700142 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700143 return null;
144 }
145
Evan Millar7911ff52009-07-21 15:55:18 -0700146 byte[] data = cursor.getBlob(bitmapColumnIndex);
Evan Millar45e0ed32009-06-01 16:44:38 -0700147 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 Millar7911ff52009-07-21 15:55:18 -0700167 public static Bitmap loadContactPhoto(Context context, long photoId,
Evan Millar2c1cc832009-07-13 11:08:06 -0700168 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 Millar66388be2009-05-28 15:41:07 -0700192 /**
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 Sharkey3f0b7b82009-08-12 11:28:53 -0700224 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 Millar8a79cee2009-08-19 17:20:49 -0700235
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 Millar66388be2009-05-28 15:41:07 -0700253}