Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 17 | package com.android.contacts; |
| 18 | |
Daisuke Miyakawa | 0a39387 | 2011-03-09 10:17:23 -0800 | [diff] [blame^] | 19 | import com.google.i18n.phonenumbers.NumberParseException; |
| 20 | import com.google.i18n.phonenumbers.PhoneNumberUtil; |
| 21 | import com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType; |
| 22 | import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; |
| 23 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 26 | import android.location.CountryDetector; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 27 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 28 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 29 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 31 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 32 | public class ContactsUtils { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 33 | private static final String TAG = "ContactsUtils"; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 34 | private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 35 | |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 36 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 37 | // 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 Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 50 | /** |
| 51 | * This looks up the provider name defined in |
Jeff Hamilton | effb7ff | 2009-12-17 16:29:40 -0600 | [diff] [blame] | 52 | * ProviderNames from the predefined IM protocol id. |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 53 | * 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 Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 82 | /** |
| 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 Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 89 | |
| 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 Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 99 | * considered equal for collapsing in the GUI. For caller-id, use |
| 100 | * {@link PhoneNumberUtils#compare(Context, String, String)} instead |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 101 | */ |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 102 | public static final boolean shouldCollapse(Context context, CharSequence mimetype1, |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 103 | 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 Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 112 | |
| 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 Miyakawa | 0a39387 | 2011-03-09 10:17:23 -0800 | [diff] [blame^] | 122 | PhoneNumberUtil util = PhoneNumberUtil.getInstance(); |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 123 | for (int i = 0; i < dataParts1.length; i++) { |
Daisuke Miyakawa | 0a39387 | 2011-03-09 10:17:23 -0800 | [diff] [blame^] | 124 | 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 Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | return true; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 139 | } 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 Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 159 | |
| 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 Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 167 | return detector.detectCountry().getCountryIso(); |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 168 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 169 | } |