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 | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 19 | import com.android.contacts.activities.DialtactsActivity; |
| 20 | import com.android.contacts.calllog.PhoneNumberHelper; |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 21 | import com.android.contacts.model.AccountType; |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 22 | import com.android.contacts.model.AccountTypeManager; |
| 23 | import com.android.contacts.model.AccountWithDataSet; |
Hugo Hudson | e86b753 | 2011-09-08 17:16:52 +0100 | [diff] [blame] | 24 | import com.android.contacts.test.NeededForTesting; |
Daisuke Miyakawa | 499ea32 | 2012-01-24 12:45:51 -0800 | [diff] [blame] | 25 | import com.android.contacts.util.Constants; |
Shaopeng Jia | 8cebd7f | 2011-08-12 13:41:42 +0200 | [diff] [blame] | 26 | import com.android.i18n.phonenumbers.PhoneNumberUtil; |
Daisuke Miyakawa | 0a39387 | 2011-03-09 10:17:23 -0800 | [diff] [blame] | 27 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Katherine Kuan | 7a700cf | 2011-10-27 15:49:18 -0700 | [diff] [blame] | 30 | import android.graphics.Rect; |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 31 | import android.location.CountryDetector; |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.provider.ContactsContract; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 34 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Daisuke Miyakawa | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 36 | import android.provider.ContactsContract.QuickContact; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 37 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 38 | import android.text.TextUtils; |
Katherine Kuan | f7689c3 | 2011-10-24 11:05:55 -0700 | [diff] [blame] | 39 | import android.view.View; |
| 40 | import android.widget.TextView; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 41 | |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 44 | public class ContactsUtils { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 45 | private static final String TAG = "ContactsUtils"; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 46 | private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 47 | |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 48 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 49 | // TODO find a proper place for the canonical version of these |
| 50 | public interface ProviderNames { |
| 51 | String YAHOO = "Yahoo"; |
| 52 | String GTALK = "GTalk"; |
| 53 | String MSN = "MSN"; |
| 54 | String ICQ = "ICQ"; |
| 55 | String AIM = "AIM"; |
| 56 | String XMPP = "XMPP"; |
| 57 | String JABBER = "JABBER"; |
| 58 | String SKYPE = "SKYPE"; |
| 59 | String QQ = "QQ"; |
| 60 | } |
| 61 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 62 | /** |
| 63 | * This looks up the provider name defined in |
Jeff Hamilton | effb7ff | 2009-12-17 16:29:40 -0600 | [diff] [blame] | 64 | * ProviderNames from the predefined IM protocol id. |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 65 | * This is used for interacting with the IM application. |
| 66 | * |
| 67 | * @param protocol the protocol ID |
| 68 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 69 | * provider is defined for the given protocol |
| 70 | * @hide |
| 71 | */ |
| 72 | public static String lookupProviderNameFromId(int protocol) { |
| 73 | switch (protocol) { |
| 74 | case Im.PROTOCOL_GOOGLE_TALK: |
| 75 | return ProviderNames.GTALK; |
| 76 | case Im.PROTOCOL_AIM: |
| 77 | return ProviderNames.AIM; |
| 78 | case Im.PROTOCOL_MSN: |
| 79 | return ProviderNames.MSN; |
| 80 | case Im.PROTOCOL_YAHOO: |
| 81 | return ProviderNames.YAHOO; |
| 82 | case Im.PROTOCOL_ICQ: |
| 83 | return ProviderNames.ICQ; |
| 84 | case Im.PROTOCOL_JABBER: |
| 85 | return ProviderNames.JABBER; |
| 86 | case Im.PROTOCOL_SKYPE: |
| 87 | return ProviderNames.SKYPE; |
| 88 | case Im.PROTOCOL_QQ: |
| 89 | return ProviderNames.QQ; |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 96 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 97 | */ |
| 98 | public static boolean isGraphic(CharSequence str) { |
| 99 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 100 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 104 | */ |
Hugo Hudson | e86b753 | 2011-09-08 17:16:52 +0100 | [diff] [blame] | 105 | @NeededForTesting |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 106 | public static boolean areObjectsEqual(Object a, Object b) { |
| 107 | return a == b || (a != null && a.equals(b)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * 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] | 112 | * considered equal for collapsing in the GUI. For caller-id, use |
| 113 | * {@link PhoneNumberUtils#compare(Context, String, String)} instead |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 114 | */ |
Dave Santoro | 01a9fac | 2011-09-15 16:08:29 -0700 | [diff] [blame] | 115 | public static final boolean shouldCollapse(CharSequence mimetype1, CharSequence data1, |
| 116 | CharSequence mimetype2, CharSequence data2) { |
Daniel Lehmann | dabe123 | 2011-09-07 11:51:13 -0700 | [diff] [blame] | 117 | // different mimetypes? don't collapse |
| 118 | if (!TextUtils.equals(mimetype1, mimetype2)) return false; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 119 | |
Daniel Lehmann | dabe123 | 2011-09-07 11:51:13 -0700 | [diff] [blame] | 120 | // exact same string? good, bail out early |
| 121 | if (TextUtils.equals(data1, data2)) return true; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 122 | |
Daniel Lehmann | dabe123 | 2011-09-07 11:51:13 -0700 | [diff] [blame] | 123 | // so if either is null, these two must be different |
| 124 | if (data1 == null || data2 == null) return false; |
| 125 | |
| 126 | // if this is not about phone numbers, we know this is not a match (of course, some |
| 127 | // mimetypes could have more sophisticated matching is the future, e.g. addresses) |
| 128 | if (!TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)) return false; |
| 129 | |
| 130 | // Now do the full phone number thing. split into parts, seperated by waiting symbol |
| 131 | // and compare them individually |
| 132 | final String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING); |
| 133 | final String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING); |
| 134 | if (dataParts1.length != dataParts2.length) return false; |
| 135 | final PhoneNumberUtil util = PhoneNumberUtil.getInstance(); |
| 136 | for (int i = 0; i < dataParts1.length; i++) { |
| 137 | final String dataPart1 = dataParts1[i]; |
| 138 | final String dataPart2 = dataParts2[i]; |
| 139 | |
| 140 | // substrings equal? shortcut, don't parse |
| 141 | if (TextUtils.equals(dataPart1, dataPart2)) continue; |
| 142 | |
| 143 | // do a full parse of the numbers |
| 144 | switch (util.isNumberMatch(dataPart1, dataPart2)) { |
| 145 | case NOT_A_NUMBER: |
| 146 | // don't understand the numbers? let's play it safe |
| 147 | return false; |
| 148 | case NO_MATCH: |
| 149 | return false; |
| 150 | case EXACT_MATCH: |
| 151 | case SHORT_NSN_MATCH: |
| 152 | case NSN_MATCH: |
| 153 | break; |
| 154 | default: |
| 155 | throw new IllegalStateException("Unknown result value from phone number " + |
| 156 | "library"); |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 157 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 158 | } |
Daniel Lehmann | dabe123 | 2011-09-07 11:51:13 -0700 | [diff] [blame] | 159 | return true; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 164 | */ |
| 165 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 166 | if (a == b) { |
| 167 | return true; |
| 168 | } |
| 169 | if (a == null || b == null) { |
| 170 | return false; |
| 171 | } |
| 172 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 173 | } |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * @return The ISO 3166-1 two letters country code of the country the user |
| 177 | * is in. |
| 178 | */ |
| 179 | public static final String getCurrentCountryIso(Context context) { |
| 180 | CountryDetector detector = |
| 181 | (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR); |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 182 | return detector.detectCountry().getCountryIso(); |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 183 | } |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 184 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 185 | public static boolean areContactWritableAccountsAvailable(Context context) { |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 186 | final List<AccountWithDataSet> accounts = |
| 187 | AccountTypeManager.getInstance(context).getAccounts(true /* writeable */); |
| 188 | return !accounts.isEmpty(); |
| 189 | } |
| 190 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 191 | public static boolean areGroupWritableAccountsAvailable(Context context) { |
| 192 | final List<AccountWithDataSet> accounts = |
| 193 | AccountTypeManager.getInstance(context).getGroupWritableAccounts(); |
| 194 | return !accounts.isEmpty(); |
| 195 | } |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Returns the intent to launch for the given invitable account type and contact lookup URI. |
| 199 | * This will return null if the account type is not invitable (i.e. there is no |
| 200 | * {@link AccountType#getInviteContactActivityClassName()} or |
| 201 | * {@link AccountType#resPackageName}). |
| 202 | */ |
| 203 | public static Intent getInvitableIntent(AccountType accountType, Uri lookupUri) { |
| 204 | String resPackageName = accountType.resPackageName; |
| 205 | String className = accountType.getInviteContactActivityClassName(); |
| 206 | if (TextUtils.isEmpty(resPackageName) || TextUtils.isEmpty(className)) { |
| 207 | return null; |
| 208 | } |
| 209 | Intent intent = new Intent(); |
| 210 | intent.setClassName(resPackageName, className); |
| 211 | |
| 212 | intent.setAction(ContactsContract.Intents.INVITE_CONTACT); |
| 213 | |
| 214 | // Data is the lookup URI. |
| 215 | intent.setData(lookupUri); |
| 216 | return intent; |
| 217 | } |
Katherine Kuan | f7689c3 | 2011-10-24 11:05:55 -0700 | [diff] [blame] | 218 | |
| 219 | /** |
Daisuke Miyakawa | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 220 | * Return Uri with an appropriate scheme, accepting Voicemail, SIP, and usual phone call |
| 221 | * numbers. |
| 222 | */ |
| 223 | public static Uri getCallUri(String number) { |
| 224 | if (PhoneNumberUtils.isVoiceMailNumber(number)) { |
| 225 | return Uri.parse("voicemail:"); |
| 226 | } |
| 227 | if (PhoneNumberUtils.isUriNumber(number)) { |
Daisuke Miyakawa | 499ea32 | 2012-01-24 12:45:51 -0800 | [diff] [blame] | 228 | return Uri.fromParts(Constants.SCHEME_SIP, number, null); |
Daisuke Miyakawa | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 229 | } |
Daisuke Miyakawa | 499ea32 | 2012-01-24 12:45:51 -0800 | [diff] [blame] | 230 | return Uri.fromParts(Constants.SCHEME_TEL, number, null); |
Daisuke Miyakawa | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined |
| 235 | * automatically. |
| 236 | */ |
| 237 | public static Intent getCallIntent(String number) { |
| 238 | return getCallIntent(number, null); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Return an Intent for making a phone call. A given Uri will be used as is (without any |
| 243 | * sanity check). |
| 244 | */ |
| 245 | public static Intent getCallIntent(Uri uri) { |
| 246 | return getCallIntent(uri, null); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * A variant of {@link #getCallIntent(String)} but also accept a call origin. For more |
| 251 | * information about call origin, see comments in Phone package (PhoneApp). |
| 252 | */ |
| 253 | public static Intent getCallIntent(String number, String callOrigin) { |
| 254 | return getCallIntent(getCallUri(number), callOrigin); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * A variant of {@link #getCallIntent(Uri)} but also accept a call origin. For more |
| 259 | * information about call origin, see comments in Phone package (PhoneApp). |
| 260 | */ |
| 261 | public static Intent getCallIntent(Uri uri, String callOrigin) { |
| 262 | final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri); |
| 263 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 264 | if (callOrigin != null) { |
| 265 | intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN, callOrigin); |
| 266 | } |
| 267 | return intent; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Return an Intent for launching voicemail screen. |
| 272 | */ |
| 273 | public static Intent getVoicemailIntent() { |
| 274 | final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 275 | Uri.fromParts("voicemail", "", null)); |
| 276 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 277 | return intent; |
| 278 | } |
| 279 | |
| 280 | /** |
Katherine Kuan | f7689c3 | 2011-10-24 11:05:55 -0700 | [diff] [blame] | 281 | * Returns a header view based on the R.layout.list_separator, where the |
| 282 | * containing {@link TextView} is set using the given textResourceId. |
| 283 | */ |
| 284 | public static View createHeaderView(Context context, int textResourceId) { |
| 285 | View view = View.inflate(context, R.layout.list_separator, null); |
| 286 | TextView textView = (TextView) view.findViewById(R.id.title); |
| 287 | textView.setText(context.getString(textResourceId)); |
| 288 | return view; |
| 289 | } |
Katherine Kuan | 7a700cf | 2011-10-27 15:49:18 -0700 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * Returns the {@link Rect} with left, top, right, and bottom coordinates |
| 293 | * that are equivalent to the given {@link View}'s bounds. This is equivalent to how the |
| 294 | * target {@link Rect} is calculated in {@link QuickContact#showQuickContact}. |
| 295 | */ |
| 296 | public static Rect getTargetRectFromView(Context context, View view) { |
| 297 | final float appScale = context.getResources().getCompatibilityInfo().applicationScale; |
| 298 | final int[] pos = new int[2]; |
| 299 | view.getLocationOnScreen(pos); |
| 300 | |
| 301 | final Rect rect = new Rect(); |
| 302 | rect.left = (int) (pos[0] * appScale + 0.5f); |
| 303 | rect.top = (int) (pos[1] * appScale + 0.5f); |
| 304 | rect.right = (int) ((pos[0] + view.getWidth()) * appScale + 0.5f); |
| 305 | rect.bottom = (int) ((pos[1] + view.getHeight()) * appScale + 0.5f); |
| 306 | return rect; |
| 307 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 308 | } |