blob: 9a3f2ef7622abf62fe55310a562051fd12d0767b [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
Makoto Onuki6f74c0f2011-09-27 13:47:15 -070019import com.android.contacts.model.AccountType;
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070020import com.android.contacts.model.AccountTypeManager;
21import com.android.contacts.model.AccountWithDataSet;
Hugo Hudsone86b7532011-09-08 17:16:52 +010022import com.android.contacts.test.NeededForTesting;
Shaopeng Jia8cebd7f2011-08-12 13:41:42 +020023import com.android.i18n.phonenumbers.PhoneNumberUtil;
Daisuke Miyakawa0a393872011-03-09 10:17:23 -080024
Evan Millar45e0ed32009-06-01 16:44:38 -070025import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070026import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070027import android.location.CountryDetector;
Evan Millar66388be2009-05-28 15:41:07 -070028import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070029import android.provider.ContactsContract.CommonDataKinds.Phone;
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
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070033import java.util.List;
34
Evan Millar66388be2009-05-28 15:41:07 -070035public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070036 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070037 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070038
Evan Millar2c1cc832009-07-13 11:08:06 -070039
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060040 // TODO find a proper place for the canonical version of these
41 public interface ProviderNames {
42 String YAHOO = "Yahoo";
43 String GTALK = "GTalk";
44 String MSN = "MSN";
45 String ICQ = "ICQ";
46 String AIM = "AIM";
47 String XMPP = "XMPP";
48 String JABBER = "JABBER";
49 String SKYPE = "SKYPE";
50 String QQ = "QQ";
51 }
52
Evan Millar66388be2009-05-28 15:41:07 -070053 /**
54 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060055 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070056 * This is used for interacting with the IM application.
57 *
58 * @param protocol the protocol ID
59 * @return the provider name the IM app uses for the given protocol, or null if no
60 * provider is defined for the given protocol
61 * @hide
62 */
63 public static String lookupProviderNameFromId(int protocol) {
64 switch (protocol) {
65 case Im.PROTOCOL_GOOGLE_TALK:
66 return ProviderNames.GTALK;
67 case Im.PROTOCOL_AIM:
68 return ProviderNames.AIM;
69 case Im.PROTOCOL_MSN:
70 return ProviderNames.MSN;
71 case Im.PROTOCOL_YAHOO:
72 return ProviderNames.YAHOO;
73 case Im.PROTOCOL_ICQ:
74 return ProviderNames.ICQ;
75 case Im.PROTOCOL_JABBER:
76 return ProviderNames.JABBER;
77 case Im.PROTOCOL_SKYPE:
78 return ProviderNames.SKYPE;
79 case Im.PROTOCOL_QQ:
80 return ProviderNames.QQ;
81 }
82 return null;
83 }
84
Jeff Sharkeye31dac82009-10-08 11:13:47 -070085 /**
86 * Test if the given {@link CharSequence} contains any graphic characters,
87 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
88 */
89 public static boolean isGraphic(CharSequence str) {
90 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
91 }
Makoto Onukic710b0e2009-10-13 15:43:24 -070092
93 /**
94 * Returns true if two objects are considered equal. Two null references are equal here.
95 */
Hugo Hudsone86b7532011-09-08 17:16:52 +010096 @NeededForTesting
Makoto Onukic710b0e2009-10-13 15:43:24 -070097 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 */
Dave Santoro01a9fac2011-09-15 16:08:29 -0700106 public static final boolean shouldCollapse(CharSequence mimetype1, CharSequence data1,
107 CharSequence mimetype2, CharSequence data2) {
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700108 // different mimetypes? don't collapse
109 if (!TextUtils.equals(mimetype1, mimetype2)) return false;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700110
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700111 // exact same string? good, bail out early
112 if (TextUtils.equals(data1, data2)) return true;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700113
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700114 // so if either is null, these two must be different
115 if (data1 == null || data2 == null) return false;
116
117 // if this is not about phone numbers, we know this is not a match (of course, some
118 // mimetypes could have more sophisticated matching is the future, e.g. addresses)
119 if (!TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)) return false;
120
121 // Now do the full phone number thing. split into parts, seperated by waiting symbol
122 // and compare them individually
123 final String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
124 final String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
125 if (dataParts1.length != dataParts2.length) return false;
126 final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
127 for (int i = 0; i < dataParts1.length; i++) {
128 final String dataPart1 = dataParts1[i];
129 final String dataPart2 = dataParts2[i];
130
131 // substrings equal? shortcut, don't parse
132 if (TextUtils.equals(dataPart1, dataPart2)) continue;
133
134 // do a full parse of the numbers
135 switch (util.isNumberMatch(dataPart1, dataPart2)) {
136 case NOT_A_NUMBER:
137 // don't understand the numbers? let's play it safe
138 return false;
139 case NO_MATCH:
140 return false;
141 case EXACT_MATCH:
142 case SHORT_NSN_MATCH:
143 case NSN_MATCH:
144 break;
145 default:
146 throw new IllegalStateException("Unknown result value from phone number " +
147 "library");
Makoto Onukic710b0e2009-10-13 15:43:24 -0700148 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700149 }
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700150 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700151 }
152
153 /**
154 * Returns true if two {@link Intent}s are both null, or have the same action.
155 */
156 public static final boolean areIntentActionEqual(Intent a, Intent b) {
157 if (a == b) {
158 return true;
159 }
160 if (a == null || b == null) {
161 return false;
162 }
163 return TextUtils.equals(a.getAction(), b.getAction());
164 }
Bai Taoba344222010-07-28 17:50:23 -0700165
166 /**
167 * @return The ISO 3166-1 two letters country code of the country the user
168 * is in.
169 */
170 public static final String getCurrentCountryIso(Context context) {
171 CountryDetector detector =
172 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800173 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700174 }
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700175
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700176 public static boolean areContactWritableAccountsAvailable(Context context) {
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700177 final List<AccountWithDataSet> accounts =
178 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
179 return !accounts.isEmpty();
180 }
181
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700182 public static boolean areGroupWritableAccountsAvailable(Context context) {
183 final List<AccountWithDataSet> accounts =
184 AccountTypeManager.getInstance(context).getGroupWritableAccounts();
185 return !accounts.isEmpty();
186 }
Evan Millar66388be2009-05-28 15:41:07 -0700187}