blob: d2a01ca17775e53d397498a3ca41815a93410e06 [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;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070031import android.content.Intent;
Evan Millar45e0ed32009-06-01 16:44:38 -070032import android.database.Cursor;
33import android.graphics.Bitmap;
34import android.graphics.BitmapFactory;
35import android.provider.Contacts;
36import android.provider.Contacts.Photos;
Evan Millar66388be2009-05-28 15:41:07 -070037import android.provider.ContactsContract.CommonDataKinds.Email;
38import android.provider.ContactsContract.CommonDataKinds.Im;
39import android.provider.ContactsContract.CommonDataKinds.Organization;
40import android.provider.ContactsContract.CommonDataKinds.Phone;
Evan Millar2c1cc832009-07-13 11:08:06 -070041import android.provider.ContactsContract.CommonDataKinds.Photo;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070042import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Evan Millar66388be2009-05-28 15:41:07 -070043import android.provider.Im.ProviderNames;
Jeff Sharkey39261272009-06-03 19:15:09 -070044import android.database.Cursor;
Evan Millar66388be2009-05-28 15:41:07 -070045import android.text.TextUtils;
Jeff Sharkey39261272009-06-03 19:15:09 -070046import android.util.Log;
Evan Millar66388be2009-05-28 15:41:07 -070047
48public class ContactsUtils {
Evan Millar45e0ed32009-06-01 16:44:38 -070049
Jeff Sharkey39261272009-06-03 19:15:09 -070050 /**
51 * Build the display title for the {@link Data#CONTENT_URI} entry in the
52 * provided cursor, assuming the given mimeType.
53 */
54 public static final CharSequence getDisplayLabel(Context context,
55 String mimeType, Cursor cursor) {
56 // Try finding the type and label for this mimetype
57 int colType;
58 int colLabel;
59
60 // TODO: move the SMS mime-type to a central location
61 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
62 || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) {
63 // Reset to phone mimetype so we generate a label for SMS case
64 mimeType = Phone.CONTENT_ITEM_TYPE;
65 colType = cursor.getColumnIndex(Phone.TYPE);
66 colLabel = cursor.getColumnIndex(Phone.LABEL);
67 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
68 colType = cursor.getColumnIndex(Email.TYPE);
69 colLabel = cursor.getColumnIndex(Email.LABEL);
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -070070 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType)) {
71 colType = cursor.getColumnIndex(StructuredPostal.TYPE);
72 colLabel = cursor.getColumnIndex(StructuredPostal.LABEL);
Jeff Sharkey39261272009-06-03 19:15:09 -070073 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
74 colType = cursor.getColumnIndex(Organization.TYPE);
75 colLabel = cursor.getColumnIndex(Organization.LABEL);
76 } else {
77 return null;
78 }
79
80 final int type = cursor.getInt(colType);
81 final CharSequence label = cursor.getString(colLabel);
82
83 return getDisplayLabel(context, mimeType, type, label);
84 }
85
Evan Millar66388be2009-05-28 15:41:07 -070086 public static final CharSequence getDisplayLabel(Context context, String mimetype, int type,
87 CharSequence label) {
88 CharSequence display = "";
89 final int customType;
90 final int defaultType;
91 final int arrayResId;
92
93 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
94 defaultType = Phone.TYPE_HOME;
95 customType = Phone.TYPE_CUSTOM;
96 arrayResId = com.android.internal.R.array.phoneTypes;
97 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
98 defaultType = Email.TYPE_HOME;
99 customType = Email.TYPE_CUSTOM;
100 arrayResId = com.android.internal.R.array.emailAddressTypes;
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700101 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)) {
102 defaultType = StructuredPostal.TYPE_HOME;
103 customType = StructuredPostal.TYPE_CUSTOM;
Evan Millar66388be2009-05-28 15:41:07 -0700104 arrayResId = com.android.internal.R.array.postalAddressTypes;
105 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
Dmitri Plotnikov48cf72b2009-07-17 11:00:26 -0700106 defaultType = Organization.TYPE_WORK;
Evan Millar66388be2009-05-28 15:41:07 -0700107 customType = Organization.TYPE_CUSTOM;
108 arrayResId = com.android.internal.R.array.organizationTypes;
109 } else {
110 // Can't return display label for given mimetype.
111 return display;
112 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700113
Evan Millar66388be2009-05-28 15:41:07 -0700114 if (type != customType) {
115 CharSequence[] labels = context.getResources().getTextArray(arrayResId);
116 try {
117 display = labels[type - 1];
118 } catch (ArrayIndexOutOfBoundsException e) {
119 display = labels[defaultType - 1];
120 }
121 } else {
122 if (!TextUtils.isEmpty(label)) {
123 display = label;
124 }
125 }
126 return display;
127 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700128
Evan Millar45e0ed32009-06-01 16:44:38 -0700129 /**
130 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
131 * If the person's photo isn't present returns null.
132 *
133 * @param aggCursor the Cursor pointing to the data record containing the photo.
134 * @param bitmapColumnIndex the column index where the photo Uri is stored.
135 * @param options the decoding options, can be set to null
136 * @return the photo Bitmap
137 */
Evan Millar0a40ffa2009-06-18 16:49:08 -0700138 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700139 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700140 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700141 return null;
142 }
143
Evan Millar7911ff52009-07-21 15:55:18 -0700144 byte[] data = cursor.getBlob(bitmapColumnIndex);
Evan Millar45e0ed32009-06-01 16:44:38 -0700145 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
146 }
147
148 /**
149 * Loads a placeholder photo.
150 *
151 * @param placeholderImageResource the resource to use for the placeholder image
152 * @param context the Context
153 * @param options the decoding options, can be set to null
154 * @return the placeholder Bitmap.
155 */
156 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
157 BitmapFactory.Options options) {
158 if (placeholderImageResource == 0) {
159 return null;
160 }
161 return BitmapFactory.decodeResource(context.getResources(),
162 placeholderImageResource, options);
163 }
164
Evan Millar7911ff52009-07-21 15:55:18 -0700165 public static Bitmap loadContactPhoto(Context context, long photoId,
Evan Millar2c1cc832009-07-13 11:08:06 -0700166 BitmapFactory.Options options) {
167 Cursor photoCursor = null;
168 Bitmap photoBm = null;
169
170 try {
171 photoCursor = context.getContentResolver().query(
172 ContentUris.withAppendedId(Data.CONTENT_URI, photoId),
173 new String[] { Photo.PHOTO },
174 null, null, null);
175
176 if (photoCursor.moveToFirst() && !photoCursor.isNull(0)) {
177 byte[] photoData = photoCursor.getBlob(0);
178 photoBm = BitmapFactory.decodeByteArray(photoData, 0,
179 photoData.length, options);
180 }
181 } finally {
182 if (photoCursor != null) {
183 photoCursor.close();
184 }
185 }
186
187 return photoBm;
188 }
189
Evan Millar66388be2009-05-28 15:41:07 -0700190 /**
191 * This looks up the provider name defined in
192 * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id.
193 * This is used for interacting with the IM application.
194 *
195 * @param protocol the protocol ID
196 * @return the provider name the IM app uses for the given protocol, or null if no
197 * provider is defined for the given protocol
198 * @hide
199 */
200 public static String lookupProviderNameFromId(int protocol) {
201 switch (protocol) {
202 case Im.PROTOCOL_GOOGLE_TALK:
203 return ProviderNames.GTALK;
204 case Im.PROTOCOL_AIM:
205 return ProviderNames.AIM;
206 case Im.PROTOCOL_MSN:
207 return ProviderNames.MSN;
208 case Im.PROTOCOL_YAHOO:
209 return ProviderNames.YAHOO;
210 case Im.PROTOCOL_ICQ:
211 return ProviderNames.ICQ;
212 case Im.PROTOCOL_JABBER:
213 return ProviderNames.JABBER;
214 case Im.PROTOCOL_SKYPE:
215 return ProviderNames.SKYPE;
216 case Im.PROTOCOL_QQ:
217 return ProviderNames.QQ;
218 }
219 return null;
220 }
221
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -0700222 public static Intent getPhotoPickIntent() {
223 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
224 intent.setType("image/*");
225 intent.putExtra("crop", "true");
226 intent.putExtra("aspectX", 1);
227 intent.putExtra("aspectY", 1);
228 intent.putExtra("outputX", 96);
229 intent.putExtra("outputY", 96);
230 intent.putExtra("return-data", true);
231 return intent;
232 }
Evan Millar66388be2009-05-28 15:41:07 -0700233}