blob: 9bc8f74668e8b443f9566791251098431e9e0539 [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 Millar45e0ed32009-06-01 16:44:38 -070024import java.io.InputStream;
25
26import android.net.Uri;
27import android.content.ContentResolver;
Evan Millar2c1cc832009-07-13 11:08:06 -070028import android.content.ContentUris;
Evan Millar7e4accf2009-06-08 10:43:26 -070029import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070030import android.content.Context;
31import android.database.Cursor;
32import android.graphics.Bitmap;
33import android.graphics.BitmapFactory;
34import android.provider.Contacts;
35import android.provider.Contacts.Photos;
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;
Evan Millar66388be2009-05-28 15:41:07 -070042import android.provider.Im.ProviderNames;
Jeff Sharkey39261272009-06-03 19:15:09 -070043import android.database.Cursor;
Evan Millar66388be2009-05-28 15:41:07 -070044import android.text.TextUtils;
Jeff Sharkey39261272009-06-03 19:15:09 -070045import android.util.Log;
Evan Millar66388be2009-05-28 15:41:07 -070046
47public class ContactsUtils {
Evan Millar45e0ed32009-06-01 16:44:38 -070048
Jeff Sharkey39261272009-06-03 19:15:09 -070049 /**
50 * Build the display title for the {@link Data#CONTENT_URI} entry in the
51 * provided cursor, assuming the given mimeType.
52 */
53 public static final CharSequence getDisplayLabel(Context context,
54 String mimeType, Cursor cursor) {
55 // Try finding the type and label for this mimetype
56 int colType;
57 int colLabel;
58
59 // TODO: move the SMS mime-type to a central location
60 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
61 || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) {
62 // Reset to phone mimetype so we generate a label for SMS case
63 mimeType = Phone.CONTENT_ITEM_TYPE;
64 colType = cursor.getColumnIndex(Phone.TYPE);
65 colLabel = cursor.getColumnIndex(Phone.LABEL);
66 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
67 colType = cursor.getColumnIndex(Email.TYPE);
68 colLabel = cursor.getColumnIndex(Email.LABEL);
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070069 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
70 colType = cursor.getColumnIndex(StructuredPostal.TYPE);
71 colLabel = cursor.getColumnIndex(StructuredPostal.LABEL);
Jeff Sharkey39261272009-06-03 19:15:09 -070072 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
73 colType = cursor.getColumnIndex(Organization.TYPE);
74 colLabel = cursor.getColumnIndex(Organization.LABEL);
75 } else {
76 return null;
77 }
78
79 final int type = cursor.getInt(colType);
80 final CharSequence label = cursor.getString(colLabel);
81
82 return getDisplayLabel(context, mimeType, type, label);
83 }
84
Evan Millar66388be2009-05-28 15:41:07 -070085 public static final CharSequence getDisplayLabel(Context context, String mimetype, int type,
86 CharSequence label) {
87 CharSequence display = "";
88 final int customType;
89 final int defaultType;
90 final int arrayResId;
91
92 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
93 defaultType = Phone.TYPE_HOME;
94 customType = Phone.TYPE_CUSTOM;
95 arrayResId = com.android.internal.R.array.phoneTypes;
96 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
97 defaultType = Email.TYPE_HOME;
98 customType = Email.TYPE_CUSTOM;
99 arrayResId = com.android.internal.R.array.emailAddressTypes;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700100 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) {
101 defaultType = StructuredPostal.TYPE_HOME;
102 customType = StructuredPostal.TYPE_CUSTOM;
Evan Millar66388be2009-05-28 15:41:07 -0700103 arrayResId = com.android.internal.R.array.postalAddressTypes;
104 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
Dmitri Plotnikov48cf72b2009-07-17 11:00:26 -0700105 defaultType = Organization.TYPE_WORK;
Evan Millar66388be2009-05-28 15:41:07 -0700106 customType = Organization.TYPE_CUSTOM;
107 arrayResId = com.android.internal.R.array.organizationTypes;
108 } else {
109 // Can't return display label for given mimetype.
110 return display;
111 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700112
Evan Millar66388be2009-05-28 15:41:07 -0700113 if (type != customType) {
114 CharSequence[] labels = context.getResources().getTextArray(arrayResId);
115 try {
116 display = labels[type - 1];
117 } catch (ArrayIndexOutOfBoundsException e) {
118 display = labels[defaultType - 1];
119 }
120 } else {
121 if (!TextUtils.isEmpty(label)) {
122 display = label;
123 }
124 }
125 return display;
126 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700127
Evan Millar45e0ed32009-06-01 16:44:38 -0700128 /**
129 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
130 * If the person's photo isn't present returns null.
131 *
132 * @param aggCursor the Cursor pointing to the data record containing the photo.
133 * @param bitmapColumnIndex the column index where the photo Uri is stored.
134 * @param options the decoding options, can be set to null
135 * @return the photo Bitmap
136 */
Evan Millar0a40ffa2009-06-18 16:49:08 -0700137 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700138 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700139 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700140 return null;
141 }
142
Evan Millar7911ff52009-07-21 15:55:18 -0700143 byte[] data = cursor.getBlob(bitmapColumnIndex);
Evan Millar45e0ed32009-06-01 16:44:38 -0700144 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
145 }
146
147 /**
148 * Loads a placeholder photo.
149 *
150 * @param placeholderImageResource the resource to use for the placeholder image
151 * @param context the Context
152 * @param options the decoding options, can be set to null
153 * @return the placeholder Bitmap.
154 */
155 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
156 BitmapFactory.Options options) {
157 if (placeholderImageResource == 0) {
158 return null;
159 }
160 return BitmapFactory.decodeResource(context.getResources(),
161 placeholderImageResource, options);
162 }
163
Evan Millar7911ff52009-07-21 15:55:18 -0700164 public static Bitmap loadContactPhoto(Context context, long photoId,
Evan Millar2c1cc832009-07-13 11:08:06 -0700165 BitmapFactory.Options options) {
166 Cursor photoCursor = null;
167 Bitmap photoBm = null;
168
169 try {
170 photoCursor = context.getContentResolver().query(
171 ContentUris.withAppendedId(Data.CONTENT_URI, photoId),
172 new String[] { Photo.PHOTO },
173 null, null, null);
174
175 if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) {
176 byte[] photoData = photoCursor.getBlob(0);
177 photoBm = BitmapFactory.decodeByteArray(photoData, 0,
178 photoData.length, options);
179 }
180 } finally {
181 if (photoCursor != null) {
182 photoCursor.close();
183 }
184 }
185
186 return photoBm;
187 }
188
Evan Millar66388be2009-05-28 15:41:07 -0700189 /**
190 * This looks up the provider name defined in
191 * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id.
192 * This is used for interacting with the IM application.
193 *
194 * @param protocol the protocol ID
195 * @return the provider name the IM app uses for the given protocol, or null if no
196 * provider is defined for the given protocol
197 * @hide
198 */
199 public static String lookupProviderNameFromId(int protocol) {
200 switch (protocol) {
201 case Im.PROTOCOL_GOOGLE_TALK:
202 return ProviderNames.GTALK;
203 case Im.PROTOCOL_AIM:
204 return ProviderNames.AIM;
205 case Im.PROTOCOL_MSN:
206 return ProviderNames.MSN;
207 case Im.PROTOCOL_YAHOO:
208 return ProviderNames.YAHOO;
209 case Im.PROTOCOL_ICQ:
210 return ProviderNames.ICQ;
211 case Im.PROTOCOL_JABBER:
212 return ProviderNames.JABBER;
213 case Im.PROTOCOL_SKYPE:
214 return ProviderNames.SKYPE;
215 case Im.PROTOCOL_QQ:
216 return ProviderNames.QQ;
217 }
218 return null;
219 }
220
221}