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