blob: 2f13481f36a49d9a3e7a70b3dce03a9f19d59004 [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
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070019import com.android.contacts.model.AccountTypeManager;
20import com.android.contacts.model.AccountWithDataSet;
Shaopeng Jia8cebd7f2011-08-12 13:41:42 +020021import com.android.i18n.phonenumbers.NumberParseException;
22import com.android.i18n.phonenumbers.PhoneNumberUtil;
23import com.android.i18n.phonenumbers.PhoneNumberUtil.MatchType;
24import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
Daisuke Miyakawa0a393872011-03-09 10:17:23 -080025
Evan Millar45e0ed32009-06-01 16:44:38 -070026import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070027import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070028import android.location.CountryDetector;
Evan Millar66388be2009-05-28 15:41:07 -070029import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070030import android.provider.ContactsContract.CommonDataKinds.Phone;
Makoto Onukic710b0e2009-10-13 15:43:24 -070031import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070032import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070033
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070034import java.util.List;
35
Evan Millar66388be2009-05-28 15:41:07 -070036public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070037 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070038 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070039
Evan Millar2c1cc832009-07-13 11:08:06 -070040
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060041 // TODO find a proper place for the canonical version of these
42 public interface ProviderNames {
43 String YAHOO = "Yahoo";
44 String GTALK = "GTalk";
45 String MSN = "MSN";
46 String ICQ = "ICQ";
47 String AIM = "AIM";
48 String XMPP = "XMPP";
49 String JABBER = "JABBER";
50 String SKYPE = "SKYPE";
51 String QQ = "QQ";
52 }
53
Evan Millar66388be2009-05-28 15:41:07 -070054 /**
55 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060056 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070057 * This is used for interacting with the IM application.
58 *
59 * @param protocol the protocol ID
60 * @return the provider name the IM app uses for the given protocol, or null if no
61 * provider is defined for the given protocol
62 * @hide
63 */
64 public static String lookupProviderNameFromId(int protocol) {
65 switch (protocol) {
66 case Im.PROTOCOL_GOOGLE_TALK:
67 return ProviderNames.GTALK;
68 case Im.PROTOCOL_AIM:
69 return ProviderNames.AIM;
70 case Im.PROTOCOL_MSN:
71 return ProviderNames.MSN;
72 case Im.PROTOCOL_YAHOO:
73 return ProviderNames.YAHOO;
74 case Im.PROTOCOL_ICQ:
75 return ProviderNames.ICQ;
76 case Im.PROTOCOL_JABBER:
77 return ProviderNames.JABBER;
78 case Im.PROTOCOL_SKYPE:
79 return ProviderNames.SKYPE;
80 case Im.PROTOCOL_QQ:
81 return ProviderNames.QQ;
82 }
83 return null;
84 }
85
Jeff Sharkeye31dac82009-10-08 11:13:47 -070086 /**
87 * Test if the given {@link CharSequence} contains any graphic characters,
88 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
89 */
90 public static boolean isGraphic(CharSequence str) {
91 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
92 }
Makoto Onukic710b0e2009-10-13 15:43:24 -070093
94 /**
95 * Returns true if two objects are considered equal. Two null references are equal here.
96 */
97 public static boolean areObjectsEqual(Object a, Object b) {
98 return a == b || (a != null && a.equals(b));
99 }
100
101 /**
102 * Returns true if two data with mimetypes which represent values in contact entries are
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700103 * considered equal for collapsing in the GUI. For caller-id, use
104 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -0700105 */
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700106 public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
Makoto Onukic710b0e2009-10-13 15:43:24 -0700107 CharSequence data1, CharSequence mimetype2, CharSequence data2) {
108 if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
109 && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
110 if (data1 == data2) {
111 return true;
112 }
113 if (data1 == null || data2 == null) {
114 return false;
115 }
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700116
117 // If the number contains semicolons, PhoneNumberUtils.compare
118 // only checks the substring before that (which is fine for caller-id usually)
119 // but not for collapsing numbers. so we check each segment indidually to be more strict
120 // TODO: This should be replaced once we have a more robust phonenumber-library
121 String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
122 String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
123 if (dataParts1.length != dataParts2.length) {
124 return false;
125 }
Daisuke Miyakawa0a393872011-03-09 10:17:23 -0800126 PhoneNumberUtil util = PhoneNumberUtil.getInstance();
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700127 for (int i = 0; i < dataParts1.length; i++) {
Daisuke Miyakawa0a393872011-03-09 10:17:23 -0800128 try {
129 PhoneNumber phoneNumber1 = util.parse(dataParts1[i], "ZZ" /* Unknown */);
130 PhoneNumber phoneNumber2 = util.parse(dataParts2[i], "ZZ" /* Unknown */);
131 MatchType matchType = util.isNumberMatch(phoneNumber1, phoneNumber2);
132 if (matchType != MatchType.SHORT_NSN_MATCH) {
133 return false;
134 }
135 } catch (NumberParseException e) {
136 if (!TextUtils.equals(dataParts1[i], dataParts2[i])) {
137 return false;
138 }
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700139 }
140 }
141
142 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700143 } else {
144 if (mimetype1 == mimetype2 && data1 == data2) {
145 return true;
146 }
147 return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
148 }
149 }
150
151 /**
152 * Returns true if two {@link Intent}s are both null, or have the same action.
153 */
154 public static final boolean areIntentActionEqual(Intent a, Intent b) {
155 if (a == b) {
156 return true;
157 }
158 if (a == null || b == null) {
159 return false;
160 }
161 return TextUtils.equals(a.getAction(), b.getAction());
162 }
Bai Taoba344222010-07-28 17:50:23 -0700163
164 /**
165 * @return The ISO 3166-1 two letters country code of the country the user
166 * is in.
167 */
168 public static final String getCurrentCountryIso(Context context) {
169 CountryDetector detector =
170 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800171 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700172 }
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700173
174 public static boolean areAccountsAvailable(Context context) {
175 final List<AccountWithDataSet> accounts =
176 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
177 return !accounts.isEmpty();
178 }
179
180
Evan Millar66388be2009-05-28 15:41:07 -0700181}