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