blob: 181fb37ab6cec32dca90296552be81c63b79afac [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
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060019import com.android.contacts.util.Constants;
20
Jeff Sharkey624ddc32009-10-01 21:32:19 -070021import android.content.ContentValues;
Evan Millar45e0ed32009-06-01 16:44:38 -070022import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070023import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070024import android.location.CountryDetector;
Neel Parekh2ad90a32009-09-20 19:08:50 -070025import android.net.Uri;
Evan Millar66388be2009-05-28 15:41:07 -070026import android.provider.ContactsContract.CommonDataKinds.Email;
27import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070028import android.provider.ContactsContract.CommonDataKinds.Phone;
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -080029import android.provider.ContactsContract.Data;
Makoto Onukic710b0e2009-10-13 15:43:24 -070030import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070031import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070032
Evan Millar66388be2009-05-28 15:41:07 -070033public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070034 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070035 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070036
Evan Millar2c1cc832009-07-13 11:08:06 -070037
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060038 // TODO find a proper place for the canonical version of these
39 public interface ProviderNames {
40 String YAHOO = "Yahoo";
41 String GTALK = "GTalk";
42 String MSN = "MSN";
43 String ICQ = "ICQ";
44 String AIM = "AIM";
45 String XMPP = "XMPP";
46 String JABBER = "JABBER";
47 String SKYPE = "SKYPE";
48 String QQ = "QQ";
49 }
50
Evan Millar66388be2009-05-28 15:41:07 -070051 /**
52 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060053 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070054 * This is used for interacting with the IM application.
55 *
56 * @param protocol the protocol ID
57 * @return the provider name the IM app uses for the given protocol, or null if no
58 * provider is defined for the given protocol
59 * @hide
60 */
61 public static String lookupProviderNameFromId(int protocol) {
62 switch (protocol) {
63 case Im.PROTOCOL_GOOGLE_TALK:
64 return ProviderNames.GTALK;
65 case Im.PROTOCOL_AIM:
66 return ProviderNames.AIM;
67 case Im.PROTOCOL_MSN:
68 return ProviderNames.MSN;
69 case Im.PROTOCOL_YAHOO:
70 return ProviderNames.YAHOO;
71 case Im.PROTOCOL_ICQ:
72 return ProviderNames.ICQ;
73 case Im.PROTOCOL_JABBER:
74 return ProviderNames.JABBER;
75 case Im.PROTOCOL_SKYPE:
76 return ProviderNames.SKYPE;
77 case Im.PROTOCOL_QQ:
78 return ProviderNames.QQ;
79 }
80 return null;
81 }
82
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -070083 public static final class ImActions {
84 private final Intent mPrimaryIntent;
85 private final Intent mSecondaryIntent;
86 private final int mPrimaryActionIcon;
87 private final int mSecondaryActionIcon;
88
89 private ImActions(Intent primaryIntent, Intent secondaryIntent, int primaryActionIcon,
90 int secondaryActionIcon) {
91 mPrimaryIntent = primaryIntent;
92 mSecondaryIntent = secondaryIntent;
93 mPrimaryActionIcon = primaryActionIcon;
94 mSecondaryActionIcon = secondaryActionIcon;
95 }
96
97 public Intent getPrimaryIntent() {
98 return mPrimaryIntent;
99 }
100
101 public Intent getSecondaryIntent() {
102 return mSecondaryIntent;
103 }
104
105 public int getPrimaryActionIcon() {
106 return mPrimaryActionIcon;
107 }
108
109 public int getSecondaryActionIcon() {
110 return mSecondaryActionIcon;
111 }
112 }
113
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700114 /**
115 * Build {@link Intent} to launch an action for the given {@link Im} or
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700116 * {@link Email} row. If the result is non-null, it either contains one or two Intents
117 * (e.g. [Text, Videochat] or just [Text])
118 * Returns null when missing protocol or data.
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700119 */
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700120 public static ImActions buildImActions(ContentValues values) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700121 final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE));
Evan Millar43455182009-10-08 10:24:12 -0700122
123 if (!isEmail && !isProtocolValid(values)) {
124 return null;
125 }
126
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700127 final String data = values.getAsString(isEmail ? Email.DATA : Im.DATA);
128 if (TextUtils.isEmpty(data)) return null;
129
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700130 final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL);
131
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700132 if (protocol == Im.PROTOCOL_GOOGLE_TALK) {
133 final Integer chatCapabilityObj = values.getAsInteger(Im.CHAT_CAPABILITY);
134 final int chatCapability = chatCapabilityObj == null ? 0 : chatCapabilityObj;
135 if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) {
136 // Allow Video chat and Texting
137 return new ImActions(
138 new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
139 new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")),
Daniel Lehmann06156642010-11-22 18:22:23 -0800140 R.drawable.sym_action_talk_holo_light,
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700141 R.drawable.sym_action_videochat
142 );
143 } else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) {
144 // Allow Talking and Texting
145 return new ImActions(
146 new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
147 new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")),
Daniel Lehmann06156642010-11-22 18:22:23 -0800148 R.drawable.sym_action_talk_holo_light,
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700149 R.drawable.sym_action_audiochat
150 );
151 } else {
152 return new ImActions(
153 new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")),
154 null,
Daniel Lehmann06156642010-11-22 18:22:23 -0800155 R.drawable.sym_action_talk_holo_light,
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700156 -1
157 );
158 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700159 } else {
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700160 // Build an IM Intent
161 String host = values.getAsString(Im.CUSTOM_PROTOCOL);
162
163 if (protocol != Im.PROTOCOL_CUSTOM) {
164 // Try bringing in a well-known host for specific protocols
165 host = ContactsUtils.lookupProviderNameFromId(protocol);
166 }
167
168 if (!TextUtils.isEmpty(host)) {
169 final String authority = host.toLowerCase();
170 final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
171 authority).appendPath(data).build();
172 return new ImActions(
173 new Intent(Intent.ACTION_SENDTO, imUri),
174 null,
Daniel Lehmann06156642010-11-22 18:22:23 -0800175 R.drawable.sym_action_talk_holo_light,
Daniel Lehmann8fd7bb62010-08-13 20:50:31 -0700176 -1
177 );
178 } else {
179 return null;
180 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700181 }
182 }
183
Evan Millar43455182009-10-08 10:24:12 -0700184 private static boolean isProtocolValid(ContentValues values) {
185 String protocolString = values.getAsString(Im.PROTOCOL);
186 if (protocolString == null) {
187 return false;
188 }
189 try {
190 Integer.valueOf(protocolString);
191 } catch (NumberFormatException e) {
192 return false;
193 }
194 return true;
195 }
196
Jeff Sharkeye31dac82009-10-08 11:13:47 -0700197 /**
198 * Test if the given {@link CharSequence} contains any graphic characters,
199 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
200 */
201 public static boolean isGraphic(CharSequence str) {
202 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
203 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700204
205 /**
206 * Returns true if two objects are considered equal. Two null references are equal here.
207 */
208 public static boolean areObjectsEqual(Object a, Object b) {
209 return a == b || (a != null && a.equals(b));
210 }
211
212 /**
213 * Returns true if two data with mimetypes which represent values in contact entries are
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700214 * considered equal for collapsing in the GUI. For caller-id, use
215 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -0700216 */
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700217 public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
Makoto Onukic710b0e2009-10-13 15:43:24 -0700218 CharSequence data1, CharSequence mimetype2, CharSequence data2) {
219 if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
220 && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
221 if (data1 == data2) {
222 return true;
223 }
224 if (data1 == null || data2 == null) {
225 return false;
226 }
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700227
228 // If the number contains semicolons, PhoneNumberUtils.compare
229 // only checks the substring before that (which is fine for caller-id usually)
230 // but not for collapsing numbers. so we check each segment indidually to be more strict
231 // TODO: This should be replaced once we have a more robust phonenumber-library
232 String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
233 String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
234 if (dataParts1.length != dataParts2.length) {
235 return false;
236 }
237 for (int i = 0; i < dataParts1.length; i++) {
238 if (!PhoneNumberUtils.compare(context, dataParts1[i], dataParts2[i])) {
239 return false;
240 }
241 }
242
243 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700244 } else {
245 if (mimetype1 == mimetype2 && data1 == data2) {
246 return true;
247 }
248 return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
249 }
250 }
251
252 /**
253 * Returns true if two {@link Intent}s are both null, or have the same action.
254 */
255 public static final boolean areIntentActionEqual(Intent a, Intent b) {
256 if (a == b) {
257 return true;
258 }
259 if (a == null || b == null) {
260 return false;
261 }
262 return TextUtils.equals(a.getAction(), b.getAction());
263 }
Bai Taoba344222010-07-28 17:50:23 -0700264
265 /**
266 * @return The ISO 3166-1 two letters country code of the country the user
267 * is in.
268 */
269 public static final String getCurrentCountryIso(Context context) {
270 CountryDetector detector =
271 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800272 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700273 }
Evan Millar66388be2009-05-28 15:41:07 -0700274}