blob: d3d8f34a6a8a1b68847fc180a34332da61f759a0 [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.PhoneNumberUtil;
Daisuke Miyakawa0a393872011-03-09 10:17:23 -080022
Evan Millar45e0ed32009-06-01 16:44:38 -070023import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070024import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070025import android.location.CountryDetector;
Evan Millar66388be2009-05-28 15:41:07 -070026import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070027import android.provider.ContactsContract.CommonDataKinds.Phone;
Makoto Onukic710b0e2009-10-13 15:43:24 -070028import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070029import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070030
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070031import java.util.List;
32
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
Jeff Sharkeye31dac82009-10-08 11:13:47 -070083 /**
84 * Test if the given {@link CharSequence} contains any graphic characters,
85 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
86 */
87 public static boolean isGraphic(CharSequence str) {
88 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
89 }
Makoto Onukic710b0e2009-10-13 15:43:24 -070090
91 /**
92 * Returns true if two objects are considered equal. Two null references are equal here.
93 */
94 public static boolean areObjectsEqual(Object a, Object b) {
95 return a == b || (a != null && a.equals(b));
96 }
97
98 /**
99 * Returns true if two data with mimetypes which represent values in contact entries are
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700100 * considered equal for collapsing in the GUI. For caller-id, use
101 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -0700102 */
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700103 public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
Makoto Onukic710b0e2009-10-13 15:43:24 -0700104 CharSequence data1, CharSequence mimetype2, CharSequence data2) {
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700105 // different mimetypes? don't collapse
106 if (!TextUtils.equals(mimetype1, mimetype2)) return false;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700107
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700108 // exact same string? good, bail out early
109 if (TextUtils.equals(data1, data2)) return true;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700110
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700111 // so if either is null, these two must be different
112 if (data1 == null || data2 == null) return false;
113
114 // if this is not about phone numbers, we know this is not a match (of course, some
115 // mimetypes could have more sophisticated matching is the future, e.g. addresses)
116 if (!TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)) return false;
117
118 // Now do the full phone number thing. split into parts, seperated by waiting symbol
119 // and compare them individually
120 final String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
121 final String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
122 if (dataParts1.length != dataParts2.length) return false;
123 final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
124 for (int i = 0; i < dataParts1.length; i++) {
125 final String dataPart1 = dataParts1[i];
126 final String dataPart2 = dataParts2[i];
127
128 // substrings equal? shortcut, don't parse
129 if (TextUtils.equals(dataPart1, dataPart2)) continue;
130
131 // do a full parse of the numbers
132 switch (util.isNumberMatch(dataPart1, dataPart2)) {
133 case NOT_A_NUMBER:
134 // don't understand the numbers? let's play it safe
135 return false;
136 case NO_MATCH:
137 return false;
138 case EXACT_MATCH:
139 case SHORT_NSN_MATCH:
140 case NSN_MATCH:
141 break;
142 default:
143 throw new IllegalStateException("Unknown result value from phone number " +
144 "library");
Makoto Onukic710b0e2009-10-13 15:43:24 -0700145 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700146 }
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700147 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700148 }
149
150 /**
151 * Returns true if two {@link Intent}s are both null, or have the same action.
152 */
153 public static final boolean areIntentActionEqual(Intent a, Intent b) {
154 if (a == b) {
155 return true;
156 }
157 if (a == null || b == null) {
158 return false;
159 }
160 return TextUtils.equals(a.getAction(), b.getAction());
161 }
Bai Taoba344222010-07-28 17:50:23 -0700162
163 /**
164 * @return The ISO 3166-1 two letters country code of the country the user
165 * is in.
166 */
167 public static final String getCurrentCountryIso(Context context) {
168 CountryDetector detector =
169 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800170 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700171 }
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700172
173 public static boolean areAccountsAvailable(Context context) {
174 final List<AccountWithDataSet> accounts =
175 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
176 return !accounts.isEmpty();
177 }
178
179
Evan Millar66388be2009-05-28 15:41:07 -0700180}