blob: 0bba75c7b6189c65c23a645add5986b1864def22 [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 Millar7e4accf2009-06-08 10:43:26 -070026import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070027import android.content.Context;
28import android.database.Cursor;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
31import android.provider.Contacts;
32import android.provider.Contacts.Photos;
Evan Millar66388be2009-05-28 15:41:07 -070033import android.provider.ContactsContract.CommonDataKinds.Email;
34import android.provider.ContactsContract.CommonDataKinds.Im;
35import android.provider.ContactsContract.CommonDataKinds.Organization;
36import android.provider.ContactsContract.CommonDataKinds.Phone;
37import android.provider.ContactsContract.CommonDataKinds.Postal;
38import android.provider.Im.ProviderNames;
Jeff Sharkey39261272009-06-03 19:15:09 -070039import android.database.Cursor;
Evan Millar66388be2009-05-28 15:41:07 -070040import android.text.TextUtils;
Jeff Sharkey39261272009-06-03 19:15:09 -070041import android.util.Log;
Evan Millar66388be2009-05-28 15:41:07 -070042
43public class ContactsUtils {
Evan Millar45e0ed32009-06-01 16:44:38 -070044
Jeff Sharkey39261272009-06-03 19:15:09 -070045 /**
46 * Build the display title for the {@link Data#CONTENT_URI} entry in the
47 * provided cursor, assuming the given mimeType.
48 */
49 public static final CharSequence getDisplayLabel(Context context,
50 String mimeType, Cursor cursor) {
51 // Try finding the type and label for this mimetype
52 int colType;
53 int colLabel;
54
55 // TODO: move the SMS mime-type to a central location
56 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
57 || FastTrackWindow.MIME_SMS_ADDRESS.equals(mimeType)) {
58 // Reset to phone mimetype so we generate a label for SMS case
59 mimeType = Phone.CONTENT_ITEM_TYPE;
60 colType = cursor.getColumnIndex(Phone.TYPE);
61 colLabel = cursor.getColumnIndex(Phone.LABEL);
62 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
63 colType = cursor.getColumnIndex(Email.TYPE);
64 colLabel = cursor.getColumnIndex(Email.LABEL);
65 } else if (Postal.CONTENT_ITEM_TYPE.equals(mimeType)) {
66 colType = cursor.getColumnIndex(Postal.TYPE);
67 colLabel = cursor.getColumnIndex(Postal.LABEL);
68 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
69 colType = cursor.getColumnIndex(Organization.TYPE);
70 colLabel = cursor.getColumnIndex(Organization.LABEL);
71 } else {
72 return null;
73 }
74
75 final int type = cursor.getInt(colType);
76 final CharSequence label = cursor.getString(colLabel);
77
78 return getDisplayLabel(context, mimeType, type, label);
79 }
80
Evan Millar66388be2009-05-28 15:41:07 -070081 public static final CharSequence getDisplayLabel(Context context, String mimetype, int type,
82 CharSequence label) {
83 CharSequence display = "";
84 final int customType;
85 final int defaultType;
86 final int arrayResId;
87
88 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
89 defaultType = Phone.TYPE_HOME;
90 customType = Phone.TYPE_CUSTOM;
91 arrayResId = com.android.internal.R.array.phoneTypes;
92 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
93 defaultType = Email.TYPE_HOME;
94 customType = Email.TYPE_CUSTOM;
95 arrayResId = com.android.internal.R.array.emailAddressTypes;
96 } else if (Postal.CONTENT_ITEM_TYPE.equals(mimetype)) {
97 defaultType = Postal.TYPE_HOME;
98 customType = Postal.TYPE_CUSTOM;
99 arrayResId = com.android.internal.R.array.postalAddressTypes;
100 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
101 defaultType = Organization.TYPE_HOME;
102 customType = Organization.TYPE_CUSTOM;
103 arrayResId = com.android.internal.R.array.organizationTypes;
104 } else {
105 // Can't return display label for given mimetype.
106 return display;
107 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700108
Evan Millar66388be2009-05-28 15:41:07 -0700109 if (type != customType) {
110 CharSequence[] labels = context.getResources().getTextArray(arrayResId);
111 try {
112 display = labels[type - 1];
113 } catch (ArrayIndexOutOfBoundsException e) {
114 display = labels[defaultType - 1];
115 }
116 } else {
117 if (!TextUtils.isEmpty(label)) {
118 display = label;
119 }
120 }
121 return display;
122 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700123
Evan Millar7e4accf2009-06-08 10:43:26 -0700124 public static String encodePredefinedImProtocol(int protocol) {
125 return "pre:" + protocol;
126 }
127
128 public static String encodeCustomImProtocol(String protocolString) {
129 return "custom:" + protocolString;
130 }
131
Evan Millar66388be2009-05-28 15:41:07 -0700132 public static Object decodeImProtocol(String encodedString) {
133 if (encodedString == null) {
134 return null;
135 }
136
137 if (encodedString.startsWith("pre:")) {
138 return Integer.parseInt(encodedString.substring(4));
139 }
140
141 if (encodedString.startsWith("custom:")) {
142 return encodedString.substring(7);
143 }
144
145 throw new IllegalArgumentException(
146 "the value is not a valid encoded protocol, " + encodedString);
147 }
Evan Millar66388be2009-05-28 15:41:07 -0700148
Evan Millar45e0ed32009-06-01 16:44:38 -0700149 /**
150 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
151 * If the person's photo isn't present returns null.
152 *
153 * @param aggCursor the Cursor pointing to the data record containing the photo.
154 * @param bitmapColumnIndex the column index where the photo Uri is stored.
155 * @param options the decoding options, can be set to null
156 * @return the photo Bitmap
157 */
Evan Millar0a40ffa2009-06-18 16:49:08 -0700158 public static Bitmap loadContactPhoto(Cursor cursor, int bitmapColumnIndex,
Evan Millar45e0ed32009-06-01 16:44:38 -0700159 BitmapFactory.Options options) {
Evan Millar0a40ffa2009-06-18 16:49:08 -0700160 if (cursor == null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700161 return null;
162 }
163
Evan Millar0a40ffa2009-06-18 16:49:08 -0700164 byte[] data = cursor.getBlob(bitmapColumnIndex);;
Evan Millar45e0ed32009-06-01 16:44:38 -0700165 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
166 }
167
168 /**
169 * Loads a placeholder photo.
170 *
171 * @param placeholderImageResource the resource to use for the placeholder image
172 * @param context the Context
173 * @param options the decoding options, can be set to null
174 * @return the placeholder Bitmap.
175 */
176 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
177 BitmapFactory.Options options) {
178 if (placeholderImageResource == 0) {
179 return null;
180 }
181 return BitmapFactory.decodeResource(context.getResources(),
182 placeholderImageResource, options);
183 }
184
Evan Millar66388be2009-05-28 15:41:07 -0700185 /**
186 * This looks up the provider name defined in
187 * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id.
188 * This is used for interacting with the IM application.
189 *
190 * @param protocol the protocol ID
191 * @return the provider name the IM app uses for the given protocol, or null if no
192 * provider is defined for the given protocol
193 * @hide
194 */
195 public static String lookupProviderNameFromId(int protocol) {
196 switch (protocol) {
197 case Im.PROTOCOL_GOOGLE_TALK:
198 return ProviderNames.GTALK;
199 case Im.PROTOCOL_AIM:
200 return ProviderNames.AIM;
201 case Im.PROTOCOL_MSN:
202 return ProviderNames.MSN;
203 case Im.PROTOCOL_YAHOO:
204 return ProviderNames.YAHOO;
205 case Im.PROTOCOL_ICQ:
206 return ProviderNames.ICQ;
207 case Im.PROTOCOL_JABBER:
208 return ProviderNames.JABBER;
209 case Im.PROTOCOL_SKYPE:
210 return ProviderNames.SKYPE;
211 case Im.PROTOCOL_QQ:
212 return ProviderNames.QQ;
213 }
214 return null;
215 }
216
217}