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 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 19 | import com.android.contacts.util.Constants; |
| 20 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 21 | import android.content.ContentValues; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 24 | import android.location.CountryDetector; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 26 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 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; |
Dmitri Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame^] | 29 | import android.provider.ContactsContract.Data; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 30 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 32 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 33 | public class ContactsUtils { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 34 | private static final String TAG = "ContactsUtils"; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 35 | private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 36 | |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 37 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 38 | // 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 Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 51 | /** |
| 52 | * This looks up the provider name defined in |
Jeff Hamilton | effb7ff | 2009-12-17 16:29:40 -0600 | [diff] [blame] | 53 | * ProviderNames from the predefined IM protocol id. |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 54 | * 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 | |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 83 | public static final class ImActions { |
| 84 | private final Intent mPrimaryIntent; |
| 85 | private final Intent mSecondaryIntent; |
| 86 | private final int mPrimaryActionIcon; |
| 87 | private final int mSecondaryActionIcon; |
| 88 | |
| 89 | private ImActions(Intent primaryIntent, Intent secondaryIntent, int primaryActionIcon, |
| 90 | int secondaryActionIcon) { |
| 91 | mPrimaryIntent = primaryIntent; |
| 92 | mSecondaryIntent = secondaryIntent; |
| 93 | mPrimaryActionIcon = primaryActionIcon; |
| 94 | mSecondaryActionIcon = secondaryActionIcon; |
| 95 | } |
| 96 | |
| 97 | public Intent getPrimaryIntent() { |
| 98 | return mPrimaryIntent; |
| 99 | } |
| 100 | |
| 101 | public Intent getSecondaryIntent() { |
| 102 | return mSecondaryIntent; |
| 103 | } |
| 104 | |
| 105 | public int getPrimaryActionIcon() { |
| 106 | return mPrimaryActionIcon; |
| 107 | } |
| 108 | |
| 109 | public int getSecondaryActionIcon() { |
| 110 | return mSecondaryActionIcon; |
| 111 | } |
| 112 | } |
| 113 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Build {@link Intent} to launch an action for the given {@link Im} or |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 116 | * {@link Email} row. If the result is non-null, it either contains one or two Intents |
| 117 | * (e.g. [Text, Videochat] or just [Text]) |
| 118 | * Returns null when missing protocol or data. |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 119 | */ |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 120 | public static ImActions buildImActions(ContentValues values) { |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 121 | final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(values.getAsString(Data.MIMETYPE)); |
Evan Millar | 4345518 | 2009-10-08 10:24:12 -0700 | [diff] [blame] | 122 | |
| 123 | if (!isEmail && !isProtocolValid(values)) { |
| 124 | return null; |
| 125 | } |
| 126 | |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 127 | final String data = values.getAsString(isEmail ? Email.DATA : Im.DATA); |
| 128 | if (TextUtils.isEmpty(data)) return null; |
| 129 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 130 | final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : values.getAsInteger(Im.PROTOCOL); |
| 131 | |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 132 | if (protocol == Im.PROTOCOL_GOOGLE_TALK) { |
| 133 | final Integer chatCapabilityObj = values.getAsInteger(Im.CHAT_CAPABILITY); |
| 134 | final int chatCapability = chatCapabilityObj == null ? 0 : chatCapabilityObj; |
| 135 | if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) { |
| 136 | // Allow Video chat and Texting |
| 137 | return new ImActions( |
| 138 | new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")), |
| 139 | new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")), |
Daniel Lehmann | 0615664 | 2010-11-22 18:22:23 -0800 | [diff] [blame] | 140 | R.drawable.sym_action_talk_holo_light, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 141 | R.drawable.sym_action_videochat |
| 142 | ); |
| 143 | } else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) { |
| 144 | // Allow Talking and Texting |
| 145 | return new ImActions( |
| 146 | new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")), |
| 147 | new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")), |
Daniel Lehmann | 0615664 | 2010-11-22 18:22:23 -0800 | [diff] [blame] | 148 | R.drawable.sym_action_talk_holo_light, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 149 | R.drawable.sym_action_audiochat |
| 150 | ); |
| 151 | } else { |
| 152 | return new ImActions( |
| 153 | new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")), |
| 154 | null, |
Daniel Lehmann | 0615664 | 2010-11-22 18:22:23 -0800 | [diff] [blame] | 155 | R.drawable.sym_action_talk_holo_light, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 156 | -1 |
| 157 | ); |
| 158 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 159 | } else { |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 160 | // Build an IM Intent |
| 161 | String host = values.getAsString(Im.CUSTOM_PROTOCOL); |
| 162 | |
| 163 | if (protocol != Im.PROTOCOL_CUSTOM) { |
| 164 | // Try bringing in a well-known host for specific protocols |
| 165 | host = ContactsUtils.lookupProviderNameFromId(protocol); |
| 166 | } |
| 167 | |
| 168 | if (!TextUtils.isEmpty(host)) { |
| 169 | final String authority = host.toLowerCase(); |
| 170 | final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority( |
| 171 | authority).appendPath(data).build(); |
| 172 | return new ImActions( |
| 173 | new Intent(Intent.ACTION_SENDTO, imUri), |
| 174 | null, |
Daniel Lehmann | 0615664 | 2010-11-22 18:22:23 -0800 | [diff] [blame] | 175 | R.drawable.sym_action_talk_holo_light, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 176 | -1 |
| 177 | ); |
| 178 | } else { |
| 179 | return null; |
| 180 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Evan Millar | 4345518 | 2009-10-08 10:24:12 -0700 | [diff] [blame] | 184 | private static boolean isProtocolValid(ContentValues values) { |
| 185 | String protocolString = values.getAsString(Im.PROTOCOL); |
| 186 | if (protocolString == null) { |
| 187 | return false; |
| 188 | } |
| 189 | try { |
| 190 | Integer.valueOf(protocolString); |
| 191 | } catch (NumberFormatException e) { |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 199 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 200 | */ |
| 201 | public static boolean isGraphic(CharSequence str) { |
| 202 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 203 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 207 | */ |
| 208 | public static boolean areObjectsEqual(Object a, Object b) { |
| 209 | return a == b || (a != null && a.equals(b)); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * 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] | 214 | * considered equal for collapsing in the GUI. For caller-id, use |
| 215 | * {@link PhoneNumberUtils#compare(Context, String, String)} instead |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 216 | */ |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 217 | public static final boolean shouldCollapse(Context context, CharSequence mimetype1, |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 218 | CharSequence data1, CharSequence mimetype2, CharSequence data2) { |
| 219 | if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1) |
| 220 | && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) { |
| 221 | if (data1 == data2) { |
| 222 | return true; |
| 223 | } |
| 224 | if (data1 == null || data2 == null) { |
| 225 | return false; |
| 226 | } |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 227 | |
| 228 | // If the number contains semicolons, PhoneNumberUtils.compare |
| 229 | // only checks the substring before that (which is fine for caller-id usually) |
| 230 | // but not for collapsing numbers. so we check each segment indidually to be more strict |
| 231 | // TODO: This should be replaced once we have a more robust phonenumber-library |
| 232 | String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING); |
| 233 | String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING); |
| 234 | if (dataParts1.length != dataParts2.length) { |
| 235 | return false; |
| 236 | } |
| 237 | for (int i = 0; i < dataParts1.length; i++) { |
| 238 | if (!PhoneNumberUtils.compare(context, dataParts1[i], dataParts2[i])) { |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return true; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 244 | } else { |
| 245 | if (mimetype1 == mimetype2 && data1 == data2) { |
| 246 | return true; |
| 247 | } |
| 248 | return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 254 | */ |
| 255 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 256 | if (a == b) { |
| 257 | return true; |
| 258 | } |
| 259 | if (a == null || b == null) { |
| 260 | return false; |
| 261 | } |
| 262 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 263 | } |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * @return The ISO 3166-1 two letters country code of the country the user |
| 267 | * is in. |
| 268 | */ |
| 269 | public static final String getCurrentCountryIso(Context context) { |
| 270 | CountryDetector detector = |
| 271 | (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR); |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 272 | return detector.detectCountry().getCountryIso(); |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 273 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 274 | } |