blob: 825ded0bfb443642ea51353682b3a926942a2073 [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 Millar7e4accf2009-06-08 10:43:26 -0700126 public static String encodePredefinedImProtocol(int protocol) {
127 return "pre:" + protocol;
128 }
129
130 public static String encodeCustomImProtocol(String protocolString) {
131 return "custom:" + protocolString;
132 }
133
Evan Millar66388be2009-05-28 15:41:07 -0700134 public static Object decodeImProtocol(String encodedString) {
135 if (encodedString == null) {
136 return null;
137 }
138
139 if (encodedString.startsWith("pre:")) {
140 return Integer.parseInt(encodedString.substring(4));
141 }
142
143 if (encodedString.startsWith("custom:")) {
144 return encodedString.substring(7);
145 }
146
147 throw new IllegalArgumentException(
148 "the value is not a valid encoded protocol, " + encodedString);
149 }
Evan Millar66388be2009-05-28 15:41:07 -0700150
Evan Millar45e0ed32009-06-01 16:44:38 -0700151 /**
152 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
153 * If the person's photo isn't present returns null.
154 *
155 * @param aggCursor the Cursor pointing to the data record containing the photo.
156 * @param bitmapColumnIndex the column index where the photo Uri is stored.
157 * @param options the decoding options, can be set to null
158 * @return the photo Bitmap
159 */
Evan Millar0a40ffa2009-06-18 16:49:08 -0700160 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700161 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700162 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700163 return null;
164 }
165
Evan Millar7911ff52009-07-21 15:55:18 -0700166 byte[] data = cursor.getBlob(bitmapColumnIndex);
Evan Millar45e0ed32009-06-01 16:44:38 -0700167 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
168 }
169
170 /**
171 * Loads a placeholder photo.
172 *
173 * @param placeholderImageResource the resource to use for the placeholder image
174 * @param context the Context
175 * @param options the decoding options, can be set to null
176 * @return the placeholder Bitmap.
177 */
178 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
179 BitmapFactory.Options options) {
180 if (placeholderImageResource == 0) {
181 return null;
182 }
183 return BitmapFactory.decodeResource(context.getResources(),
184 placeholderImageResource, options);
185 }
186
Evan Millar7911ff52009-07-21 15:55:18 -0700187 public static Bitmap loadContactPhoto(Context context, long photoId,
Evan Millar2c1cc832009-07-13 11:08:06 -0700188 BitmapFactory.Options options) {
189 Cursor photoCursor = null;
190 Bitmap photoBm = null;
191
192 try {
193 photoCursor = context.getContentResolver().query(
194 ContentUris.withAppendedId(Data.CONTENT_URI, photoId),
195 new String[] { Photo.PHOTO },
196 null, null, null);
197
198 if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) {
199 byte[] photoData = photoCursor.getBlob(0);
200 photoBm = BitmapFactory.decodeByteArray(photoData, 0,
201 photoData.length, options);
202 }
203 } finally {
204 if (photoCursor != null) {
205 photoCursor.close();
206 }
207 }
208
209 return photoBm;
210 }
211
Evan Millar66388be2009-05-28 15:41:07 -0700212 /**
213 * This looks up the provider name defined in
214 * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id.
215 * This is used for interacting with the IM application.
216 *
217 * @param protocol the protocol ID
218 * @return the provider name the IM app uses for the given protocol, or null if no
219 * provider is defined for the given protocol
220 * @hide
221 */
222 public static String lookupProviderNameFromId(int protocol) {
223 switch (protocol) {
224 case Im.PROTOCOL_GOOGLE_TALK:
225 return ProviderNames.GTALK;
226 case Im.PROTOCOL_AIM:
227 return ProviderNames.AIM;
228 case Im.PROTOCOL_MSN:
229 return ProviderNames.MSN;
230 case Im.PROTOCOL_YAHOO:
231 return ProviderNames.YAHOO;
232 case Im.PROTOCOL_ICQ:
233 return ProviderNames.ICQ;
234 case Im.PROTOCOL_JABBER:
235 return ProviderNames.JABBER;
236 case Im.PROTOCOL_SKYPE:
237 return ProviderNames.SKYPE;
238 case Im.PROTOCOL_QQ:
239 return ProviderNames.QQ;
240 }
241 return null;
242 }
243
244}