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 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 21 | import android.location.CountryDetector; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 22 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 24 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 26 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 27 | public class ContactsUtils { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 28 | private static final String TAG = "ContactsUtils"; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 29 | private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 30 | |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 31 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 32 | // TODO find a proper place for the canonical version of these |
| 33 | public interface ProviderNames { |
| 34 | String YAHOO = "Yahoo"; |
| 35 | String GTALK = "GTalk"; |
| 36 | String MSN = "MSN"; |
| 37 | String ICQ = "ICQ"; |
| 38 | String AIM = "AIM"; |
| 39 | String XMPP = "XMPP"; |
| 40 | String JABBER = "JABBER"; |
| 41 | String SKYPE = "SKYPE"; |
| 42 | String QQ = "QQ"; |
| 43 | } |
| 44 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 45 | /** |
| 46 | * This looks up the provider name defined in |
Jeff Hamilton | effb7ff | 2009-12-17 16:29:40 -0600 | [diff] [blame] | 47 | * ProviderNames from the predefined IM protocol id. |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 48 | * This is used for interacting with the IM application. |
| 49 | * |
| 50 | * @param protocol the protocol ID |
| 51 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 52 | * provider is defined for the given protocol |
| 53 | * @hide |
| 54 | */ |
| 55 | public static String lookupProviderNameFromId(int protocol) { |
| 56 | switch (protocol) { |
| 57 | case Im.PROTOCOL_GOOGLE_TALK: |
| 58 | return ProviderNames.GTALK; |
| 59 | case Im.PROTOCOL_AIM: |
| 60 | return ProviderNames.AIM; |
| 61 | case Im.PROTOCOL_MSN: |
| 62 | return ProviderNames.MSN; |
| 63 | case Im.PROTOCOL_YAHOO: |
| 64 | return ProviderNames.YAHOO; |
| 65 | case Im.PROTOCOL_ICQ: |
| 66 | return ProviderNames.ICQ; |
| 67 | case Im.PROTOCOL_JABBER: |
| 68 | return ProviderNames.JABBER; |
| 69 | case Im.PROTOCOL_SKYPE: |
| 70 | return ProviderNames.SKYPE; |
| 71 | case Im.PROTOCOL_QQ: |
| 72 | return ProviderNames.QQ; |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 | |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 79 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 80 | */ |
| 81 | public static boolean isGraphic(CharSequence str) { |
| 82 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 83 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 87 | */ |
| 88 | public static boolean areObjectsEqual(Object a, Object b) { |
| 89 | return a == b || (a != null && a.equals(b)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * 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] | 94 | * considered equal for collapsing in the GUI. For caller-id, use |
| 95 | * {@link PhoneNumberUtils#compare(Context, String, String)} instead |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 96 | */ |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 97 | public static final boolean shouldCollapse(Context context, CharSequence mimetype1, |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 98 | CharSequence data1, CharSequence mimetype2, CharSequence data2) { |
| 99 | if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1) |
| 100 | && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) { |
| 101 | if (data1 == data2) { |
| 102 | return true; |
| 103 | } |
| 104 | if (data1 == null || data2 == null) { |
| 105 | return false; |
| 106 | } |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 107 | |
| 108 | // If the number contains semicolons, PhoneNumberUtils.compare |
| 109 | // only checks the substring before that (which is fine for caller-id usually) |
| 110 | // but not for collapsing numbers. so we check each segment indidually to be more strict |
| 111 | // TODO: This should be replaced once we have a more robust phonenumber-library |
| 112 | String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING); |
| 113 | String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING); |
| 114 | if (dataParts1.length != dataParts2.length) { |
| 115 | return false; |
| 116 | } |
| 117 | for (int i = 0; i < dataParts1.length; i++) { |
| 118 | if (!PhoneNumberUtils.compare(context, dataParts1[i], dataParts2[i])) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return true; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 124 | } else { |
| 125 | if (mimetype1 == mimetype2 && data1 == data2) { |
| 126 | return true; |
| 127 | } |
| 128 | return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 134 | */ |
| 135 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 136 | if (a == b) { |
| 137 | return true; |
| 138 | } |
| 139 | if (a == null || b == null) { |
| 140 | return false; |
| 141 | } |
| 142 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 143 | } |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * @return The ISO 3166-1 two letters country code of the country the user |
| 147 | * is in. |
| 148 | */ |
| 149 | public static final String getCurrentCountryIso(Context context) { |
| 150 | CountryDetector detector = |
| 151 | (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR); |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 152 | return detector.detectCountry().getCountryIso(); |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 153 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 154 | } |