blob: 4b8224615a61d2095d676434ae3f8c27ac32ceae [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;
21import java.io.InputStream;
22
23import android.net.Uri;
24import android.content.ContentResolver;
Evan Millar7e4accf2009-06-08 10:43:26 -070025import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070026import android.content.Context;
27import android.database.Cursor;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30import android.provider.Contacts;
31import android.provider.Contacts.Photos;
Evan Millar66388be2009-05-28 15:41:07 -070032import android.provider.ContactsContract.CommonDataKinds.Email;
33import android.provider.ContactsContract.CommonDataKinds.Im;
34import android.provider.ContactsContract.CommonDataKinds.Organization;
35import android.provider.ContactsContract.CommonDataKinds.Phone;
36import android.provider.ContactsContract.CommonDataKinds.Postal;
37import android.provider.Im.ProviderNames;
Evan Millar66388be2009-05-28 15:41:07 -070038import android.text.TextUtils;
39
40public class ContactsUtils {
Evan Millar45e0ed32009-06-01 16:44:38 -070041
Evan Millar66388be2009-05-28 15:41:07 -070042 public static final CharSequence getDisplayLabel(Context context, String mimetype, int type,
43 CharSequence label) {
44 CharSequence display = "";
45 final int customType;
46 final int defaultType;
47 final int arrayResId;
48
49 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
50 defaultType = Phone.TYPE_HOME;
51 customType = Phone.TYPE_CUSTOM;
52 arrayResId = com.android.internal.R.array.phoneTypes;
53 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
54 defaultType = Email.TYPE_HOME;
55 customType = Email.TYPE_CUSTOM;
56 arrayResId = com.android.internal.R.array.emailAddressTypes;
57 } else if (Postal.CONTENT_ITEM_TYPE.equals(mimetype)) {
58 defaultType = Postal.TYPE_HOME;
59 customType = Postal.TYPE_CUSTOM;
60 arrayResId = com.android.internal.R.array.postalAddressTypes;
61 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
62 defaultType = Organization.TYPE_HOME;
63 customType = Organization.TYPE_CUSTOM;
64 arrayResId = com.android.internal.R.array.organizationTypes;
65 } else {
66 // Can't return display label for given mimetype.
67 return display;
68 }
Evan Millar45e0ed32009-06-01 16:44:38 -070069
Evan Millar66388be2009-05-28 15:41:07 -070070 if (type != customType) {
71 CharSequence[] labels = context.getResources().getTextArray(arrayResId);
72 try {
73 display = labels[type - 1];
74 } catch (ArrayIndexOutOfBoundsException e) {
75 display = labels[defaultType - 1];
76 }
77 } else {
78 if (!TextUtils.isEmpty(label)) {
79 display = label;
80 }
81 }
82 return display;
83 }
Evan Millar45e0ed32009-06-01 16:44:38 -070084
Evan Millar7e4accf2009-06-08 10:43:26 -070085 public static String encodePredefinedImProtocol(int protocol) {
86 return "pre:" + protocol;
87 }
88
89 public static String encodeCustomImProtocol(String protocolString) {
90 return "custom:" + protocolString;
91 }
92
Evan Millar66388be2009-05-28 15:41:07 -070093 public static Object decodeImProtocol(String encodedString) {
94 if (encodedString == null) {
95 return null;
96 }
97
98 if (encodedString.startsWith("pre:")) {
99 return Integer.parseInt(encodedString.substring(4));
100 }
101
102 if (encodedString.startsWith("custom:")) {
103 return encodedString.substring(7);
104 }
105
106 throw new IllegalArgumentException(
107 "the value is not a valid encoded protocol, " + encodedString);
108 }
Evan Millar66388be2009-05-28 15:41:07 -0700109
Evan Millar45e0ed32009-06-01 16:44:38 -0700110 /**
111 * Opens an InputStream for the person's photo and returns the photo as a Bitmap.
112 * If the person's photo isn't present returns null.
113 *
114 * @param aggCursor the Cursor pointing to the data record containing the photo.
115 * @param bitmapColumnIndex the column index where the photo Uri is stored.
116 * @param options the decoding options, can be set to null
117 * @return the photo Bitmap
118 */
119 public static Bitmap loadContactPhoto(Cursor aggCursor, int bitmapColumnIndex,
120 BitmapFactory.Options options) {
121 if (aggCursor == null) {
122 return null;
123 }
124
125 byte[] data = aggCursor.getBlob(bitmapColumnIndex);;
126 return BitmapFactory.decodeByteArray(data, 0, data.length, options);
127 }
128
129 /**
130 * Loads a placeholder photo.
131 *
132 * @param placeholderImageResource the resource to use for the placeholder image
133 * @param context the Context
134 * @param options the decoding options, can be set to null
135 * @return the placeholder Bitmap.
136 */
137 public static Bitmap loadPlaceholderPhoto(int placeholderImageResource, Context context,
138 BitmapFactory.Options options) {
139 if (placeholderImageResource == 0) {
140 return null;
141 }
142 return BitmapFactory.decodeResource(context.getResources(),
143 placeholderImageResource, options);
144 }
145
Evan Millar66388be2009-05-28 15:41:07 -0700146 /**
147 * This looks up the provider name defined in
148 * {@link android.provider.Im.ProviderNames} from the predefined IM protocol id.
149 * This is used for interacting with the IM application.
150 *
151 * @param protocol the protocol ID
152 * @return the provider name the IM app uses for the given protocol, or null if no
153 * provider is defined for the given protocol
154 * @hide
155 */
156 public static String lookupProviderNameFromId(int protocol) {
157 switch (protocol) {
158 case Im.PROTOCOL_GOOGLE_TALK:
159 return ProviderNames.GTALK;
160 case Im.PROTOCOL_AIM:
161 return ProviderNames.AIM;
162 case Im.PROTOCOL_MSN:
163 return ProviderNames.MSN;
164 case Im.PROTOCOL_YAHOO:
165 return ProviderNames.YAHOO;
166 case Im.PROTOCOL_ICQ:
167 return ProviderNames.ICQ;
168 case Im.PROTOCOL_JABBER:
169 return ProviderNames.JABBER;
170 case Im.PROTOCOL_SKYPE:
171 return ProviderNames.SKYPE;
172 case Im.PROTOCOL_QQ:
173 return ProviderNames.QQ;
174 }
175 return null;
176 }
177
178}