blob: 45ce4fe5e284364d2014f5d8fbc81fafa356b777 [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
Shaopeng Jia8cebd7f2011-08-12 13:41:42 +020019import com.android.i18n.phonenumbers.NumberParseException;
20import com.android.i18n.phonenumbers.PhoneNumberUtil;
21import com.android.i18n.phonenumbers.PhoneNumberUtil.MatchType;
22import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
Daisuke Miyakawa0a393872011-03-09 10:17:23 -080023
Evan Millar45e0ed32009-06-01 16:44:38 -070024import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070025import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070026import android.location.CountryDetector;
Evan Millar66388be2009-05-28 15:41:07 -070027import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070028import android.provider.ContactsContract.CommonDataKinds.Phone;
Makoto Onukic710b0e2009-10-13 15:43:24 -070029import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070030import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070031
Evan Millar66388be2009-05-28 15:41:07 -070032public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070033 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070034 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070035
Evan Millar2c1cc832009-07-13 11:08:06 -070036
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060037 // TODO find a proper place for the canonical version of these
38 public interface ProviderNames {
39 String YAHOO = "Yahoo";
40 String GTALK = "GTalk";
41 String MSN = "MSN";
42 String ICQ = "ICQ";
43 String AIM = "AIM";
44 String XMPP = "XMPP";
45 String JABBER = "JABBER";
46 String SKYPE = "SKYPE";
47 String QQ = "QQ";
48 }
49
Evan Millar66388be2009-05-28 15:41:07 -070050 /**
51 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060052 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070053 * This is used for interacting with the IM application.
54 *
55 * @param protocol the protocol ID
56 * @return the provider name the IM app uses for the given protocol, or null if no
57 * provider is defined for the given protocol
58 * @hide
59 */
60 public static String lookupProviderNameFromId(int protocol) {
61 switch (protocol) {
62 case Im.PROTOCOL_GOOGLE_TALK:
63 return ProviderNames.GTALK;
64 case Im.PROTOCOL_AIM:
65 return ProviderNames.AIM;
66 case Im.PROTOCOL_MSN:
67 return ProviderNames.MSN;
68 case Im.PROTOCOL_YAHOO:
69 return ProviderNames.YAHOO;
70 case Im.PROTOCOL_ICQ:
71 return ProviderNames.ICQ;
72 case Im.PROTOCOL_JABBER:
73 return ProviderNames.JABBER;
74 case Im.PROTOCOL_SKYPE:
75 return ProviderNames.SKYPE;
76 case Im.PROTOCOL_QQ:
77 return ProviderNames.QQ;
78 }
79 return null;
80 }
81
Jeff Sharkeye31dac82009-10-08 11:13:47 -070082 /**
83 * Test if the given {@link CharSequence} contains any graphic characters,
84 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
85 */
86 public static boolean isGraphic(CharSequence str) {
87 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
88 }
Makoto Onukic710b0e2009-10-13 15:43:24 -070089
90 /**
91 * Returns true if two objects are considered equal. Two null references are equal here.
92 */
93 public static boolean areObjectsEqual(Object a, Object b) {
94 return a == b || (a != null && a.equals(b));
95 }
96
97 /**
98 * Returns true if two data with mimetypes which represent values in contact entries are
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070099 * considered equal for collapsing in the GUI. For caller-id, use
100 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -0700101 */
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700102 public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
Makoto Onukic710b0e2009-10-13 15:43:24 -0700103 CharSequence data1, CharSequence mimetype2, CharSequence data2) {
104 if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
105 && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
106 if (data1 == data2) {
107 return true;
108 }
109 if (data1 == null || data2 == null) {
110 return false;
111 }
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700112
113 // If the number contains semicolons, PhoneNumberUtils.compare
114 // only checks the substring before that (which is fine for caller-id usually)
115 // but not for collapsing numbers. so we check each segment indidually to be more strict
116 // TODO: This should be replaced once we have a more robust phonenumber-library
117 String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
118 String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
119 if (dataParts1.length != dataParts2.length) {
120 return false;
121 }
Daisuke Miyakawa0a393872011-03-09 10:17:23 -0800122 PhoneNumberUtil util = PhoneNumberUtil.getInstance();
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700123 for (int i = 0; i < dataParts1.length; i++) {
Daisuke Miyakawa0a393872011-03-09 10:17:23 -0800124 try {
125 PhoneNumber phoneNumber1 = util.parse(dataParts1[i], "ZZ" /* Unknown */);
126 PhoneNumber phoneNumber2 = util.parse(dataParts2[i], "ZZ" /* Unknown */);
127 MatchType matchType = util.isNumberMatch(phoneNumber1, phoneNumber2);
128 if (matchType != MatchType.SHORT_NSN_MATCH) {
129 return false;
130 }
131 } catch (NumberParseException e) {
132 if (!TextUtils.equals(dataParts1[i], dataParts2[i])) {
133 return false;
134 }
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700135 }
136 }
137
138 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700139 } else {
140 if (mimetype1 == mimetype2 && data1 == data2) {
141 return true;
142 }
143 return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
144 }
145 }
146
147 /**
148 * Returns true if two {@link Intent}s are both null, or have the same action.
149 */
150 public static final boolean areIntentActionEqual(Intent a, Intent b) {
151 if (a == b) {
152 return true;
153 }
154 if (a == null || b == null) {
155 return false;
156 }
157 return TextUtils.equals(a.getAction(), b.getAction());
158 }
Bai Taoba344222010-07-28 17:50:23 -0700159
160 /**
161 * @return The ISO 3166-1 two letters country code of the country the user
162 * is in.
163 */
164 public static final String getCurrentCountryIso(Context context) {
165 CountryDetector detector =
166 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800167 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700168 }
Evan Millar66388be2009-05-28 15:41:07 -0700169}