blob: ea567fbd6725de856547fd895280417816123625 [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
Daisuke Miyakawafadd5e12011-12-01 17:34:18 -080019import com.android.contacts.activities.DialtactsActivity;
Makoto Onuki6f74c0f2011-09-27 13:47:15 -070020import com.android.contacts.model.AccountType;
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070021import com.android.contacts.model.AccountTypeManager;
22import com.android.contacts.model.AccountWithDataSet;
Hugo Hudsone86b7532011-09-08 17:16:52 +010023import com.android.contacts.test.NeededForTesting;
Daisuke Miyakawa499ea322012-01-24 12:45:51 -080024import com.android.contacts.util.Constants;
Shaopeng Jia8cebd7f2011-08-12 13:41:42 +020025import com.android.i18n.phonenumbers.PhoneNumberUtil;
Daisuke Miyakawa0a393872011-03-09 10:17:23 -080026
Evan Millar45e0ed32009-06-01 16:44:38 -070027import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070028import android.content.Intent;
Daniel Lehmann2f77c852012-03-30 15:25:31 -070029import android.database.Cursor;
Katherine Kuan7a700cf2011-10-27 15:49:18 -070030import android.graphics.Rect;
Bai Taoba344222010-07-28 17:50:23 -070031import android.location.CountryDetector;
Katherine Kuan08bcf712011-10-09 13:43:53 -070032import android.net.Uri;
33import android.provider.ContactsContract;
Evan Millar66388be2009-05-28 15:41:07 -070034import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070035import android.provider.ContactsContract.CommonDataKinds.Phone;
Daniel Lehmann2f77c852012-03-30 15:25:31 -070036import android.provider.ContactsContract.DisplayPhoto;
Daisuke Miyakawafadd5e12011-12-01 17:34:18 -080037import android.provider.ContactsContract.QuickContact;
Makoto Onukic710b0e2009-10-13 15:43:24 -070038import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070039import android.text.TextUtils;
Katherine Kuanf7689c32011-10-24 11:05:55 -070040import android.view.View;
41import android.widget.TextView;
Neel Parekh2ad90a32009-09-20 19:08:50 -070042
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070043import java.util.List;
44
Evan Millar66388be2009-05-28 15:41:07 -070045public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070046 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070047 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070048
Daniel Lehmann2f77c852012-03-30 15:25:31 -070049 private static int sThumbnailSize = -1;
Evan Millar2c1cc832009-07-13 11:08:06 -070050
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060051 // TODO find a proper place for the canonical version of these
52 public interface ProviderNames {
53 String YAHOO = "Yahoo";
54 String GTALK = "GTalk";
55 String MSN = "MSN";
56 String ICQ = "ICQ";
57 String AIM = "AIM";
58 String XMPP = "XMPP";
59 String JABBER = "JABBER";
60 String SKYPE = "SKYPE";
61 String QQ = "QQ";
62 }
63
Evan Millar66388be2009-05-28 15:41:07 -070064 /**
65 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060066 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070067 * This is used for interacting with the IM application.
68 *
69 * @param protocol the protocol ID
70 * @return the provider name the IM app uses for the given protocol, or null if no
71 * provider is defined for the given protocol
72 * @hide
73 */
74 public static String lookupProviderNameFromId(int protocol) {
75 switch (protocol) {
76 case Im.PROTOCOL_GOOGLE_TALK:
77 return ProviderNames.GTALK;
78 case Im.PROTOCOL_AIM:
79 return ProviderNames.AIM;
80 case Im.PROTOCOL_MSN:
81 return ProviderNames.MSN;
82 case Im.PROTOCOL_YAHOO:
83 return ProviderNames.YAHOO;
84 case Im.PROTOCOL_ICQ:
85 return ProviderNames.ICQ;
86 case Im.PROTOCOL_JABBER:
87 return ProviderNames.JABBER;
88 case Im.PROTOCOL_SKYPE:
89 return ProviderNames.SKYPE;
90 case Im.PROTOCOL_QQ:
91 return ProviderNames.QQ;
92 }
93 return null;
94 }
95
Jeff Sharkeye31dac82009-10-08 11:13:47 -070096 /**
97 * Test if the given {@link CharSequence} contains any graphic characters,
98 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
99 */
100 public static boolean isGraphic(CharSequence str) {
101 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
102 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700103
104 /**
105 * Returns true if two objects are considered equal. Two null references are equal here.
106 */
Hugo Hudsone86b7532011-09-08 17:16:52 +0100107 @NeededForTesting
Makoto Onukic710b0e2009-10-13 15:43:24 -0700108 public static boolean areObjectsEqual(Object a, Object b) {
109 return a == b || (a != null && a.equals(b));
110 }
111
112 /**
113 * Returns true if two data with mimetypes which represent values in contact entries are
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700114 * considered equal for collapsing in the GUI. For caller-id, use
115 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -0700116 */
Dave Santoro01a9fac2011-09-15 16:08:29 -0700117 public static final boolean shouldCollapse(CharSequence mimetype1, CharSequence data1,
118 CharSequence mimetype2, CharSequence data2) {
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700119 // different mimetypes? don't collapse
120 if (!TextUtils.equals(mimetype1, mimetype2)) return false;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700121
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700122 // exact same string? good, bail out early
123 if (TextUtils.equals(data1, data2)) return true;
Daniel Lehmannd8b0a052010-03-25 17:41:00 -0700124
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700125 // so if either is null, these two must be different
126 if (data1 == null || data2 == null) return false;
127
128 // if this is not about phone numbers, we know this is not a match (of course, some
129 // mimetypes could have more sophisticated matching is the future, e.g. addresses)
130 if (!TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)) return false;
131
132 // Now do the full phone number thing. split into parts, seperated by waiting symbol
133 // and compare them individually
134 final String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
135 final String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
136 if (dataParts1.length != dataParts2.length) return false;
137 final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
138 for (int i = 0; i < dataParts1.length; i++) {
139 final String dataPart1 = dataParts1[i];
140 final String dataPart2 = dataParts2[i];
141
142 // substrings equal? shortcut, don't parse
143 if (TextUtils.equals(dataPart1, dataPart2)) continue;
144
145 // do a full parse of the numbers
146 switch (util.isNumberMatch(dataPart1, dataPart2)) {
147 case NOT_A_NUMBER:
148 // don't understand the numbers? let's play it safe
149 return false;
150 case NO_MATCH:
151 return false;
152 case EXACT_MATCH:
153 case SHORT_NSN_MATCH:
154 case NSN_MATCH:
155 break;
156 default:
157 throw new IllegalStateException("Unknown result value from phone number " +
158 "library");
Makoto Onukic710b0e2009-10-13 15:43:24 -0700159 }
Makoto Onukic710b0e2009-10-13 15:43:24 -0700160 }
Daniel Lehmanndabe1232011-09-07 11:51:13 -0700161 return true;
Makoto Onukic710b0e2009-10-13 15:43:24 -0700162 }
163
164 /**
165 * Returns true if two {@link Intent}s are both null, or have the same action.
166 */
167 public static final boolean areIntentActionEqual(Intent a, Intent b) {
168 if (a == b) {
169 return true;
170 }
171 if (a == null || b == null) {
172 return false;
173 }
174 return TextUtils.equals(a.getAction(), b.getAction());
175 }
Bai Taoba344222010-07-28 17:50:23 -0700176
177 /**
178 * @return The ISO 3166-1 two letters country code of the country the user
179 * is in.
180 */
181 public static final String getCurrentCountryIso(Context context) {
182 CountryDetector detector =
183 (CountryDetector) context.getSystemService(Context.COUNTRY_DETECTOR);
Bai Tao09eb04f2010-09-01 15:34:16 +0800184 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700185 }
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700186
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700187 public static boolean areContactWritableAccountsAvailable(Context context) {
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700188 final List<AccountWithDataSet> accounts =
189 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
190 return !accounts.isEmpty();
191 }
192
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700193 public static boolean areGroupWritableAccountsAvailable(Context context) {
194 final List<AccountWithDataSet> accounts =
195 AccountTypeManager.getInstance(context).getGroupWritableAccounts();
196 return !accounts.isEmpty();
197 }
Katherine Kuan08bcf712011-10-09 13:43:53 -0700198
199 /**
200 * Returns the intent to launch for the given invitable account type and contact lookup URI.
201 * This will return null if the account type is not invitable (i.e. there is no
202 * {@link AccountType#getInviteContactActivityClassName()} or
203 * {@link AccountType#resPackageName}).
204 */
205 public static Intent getInvitableIntent(AccountType accountType, Uri lookupUri) {
206 String resPackageName = accountType.resPackageName;
207 String className = accountType.getInviteContactActivityClassName();
208 if (TextUtils.isEmpty(resPackageName) || TextUtils.isEmpty(className)) {
209 return null;
210 }
211 Intent intent = new Intent();
212 intent.setClassName(resPackageName, className);
213
214 intent.setAction(ContactsContract.Intents.INVITE_CONTACT);
215
216 // Data is the lookup URI.
217 intent.setData(lookupUri);
218 return intent;
219 }
Katherine Kuanf7689c32011-10-24 11:05:55 -0700220
221 /**
Daisuke Miyakawafadd5e12011-12-01 17:34:18 -0800222 * Return Uri with an appropriate scheme, accepting Voicemail, SIP, and usual phone call
223 * numbers.
224 */
225 public static Uri getCallUri(String number) {
226 if (PhoneNumberUtils.isVoiceMailNumber(number)) {
227 return Uri.parse("voicemail:");
228 }
229 if (PhoneNumberUtils.isUriNumber(number)) {
Daisuke Miyakawa499ea322012-01-24 12:45:51 -0800230 return Uri.fromParts(Constants.SCHEME_SIP, number, null);
Daisuke Miyakawafadd5e12011-12-01 17:34:18 -0800231 }
Daisuke Miyakawa499ea322012-01-24 12:45:51 -0800232 return Uri.fromParts(Constants.SCHEME_TEL, number, null);
Daisuke Miyakawafadd5e12011-12-01 17:34:18 -0800233 }
234
235 /**
236 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
237 * automatically.
238 */
239 public static Intent getCallIntent(String number) {
240 return getCallIntent(number, null);
241 }
242
243 /**
244 * Return an Intent for making a phone call. A given Uri will be used as is (without any
245 * sanity check).
246 */
247 public static Intent getCallIntent(Uri uri) {
248 return getCallIntent(uri, null);
249 }
250
251 /**
252 * A variant of {@link #getCallIntent(String)} but also accept a call origin. For more
253 * information about call origin, see comments in Phone package (PhoneApp).
254 */
255 public static Intent getCallIntent(String number, String callOrigin) {
256 return getCallIntent(getCallUri(number), callOrigin);
257 }
258
259 /**
260 * A variant of {@link #getCallIntent(Uri)} but also accept a call origin. For more
261 * information about call origin, see comments in Phone package (PhoneApp).
262 */
263 public static Intent getCallIntent(Uri uri, String callOrigin) {
264 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
265 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
266 if (callOrigin != null) {
267 intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN, callOrigin);
268 }
269 return intent;
270 }
271
272 /**
273 * Return an Intent for launching voicemail screen.
274 */
275 public static Intent getVoicemailIntent() {
276 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
277 Uri.fromParts("voicemail", "", null));
278 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
279 return intent;
280 }
281
282 /**
Katherine Kuanf7689c32011-10-24 11:05:55 -0700283 * Returns a header view based on the R.layout.list_separator, where the
284 * containing {@link TextView} is set using the given textResourceId.
285 */
286 public static View createHeaderView(Context context, int textResourceId) {
287 View view = View.inflate(context, R.layout.list_separator, null);
288 TextView textView = (TextView) view.findViewById(R.id.title);
289 textView.setText(context.getString(textResourceId));
290 return view;
291 }
Katherine Kuan7a700cf2011-10-27 15:49:18 -0700292
293 /**
294 * Returns the {@link Rect} with left, top, right, and bottom coordinates
295 * that are equivalent to the given {@link View}'s bounds. This is equivalent to how the
296 * target {@link Rect} is calculated in {@link QuickContact#showQuickContact}.
297 */
298 public static Rect getTargetRectFromView(Context context, View view) {
299 final float appScale = context.getResources().getCompatibilityInfo().applicationScale;
300 final int[] pos = new int[2];
301 view.getLocationOnScreen(pos);
302
303 final Rect rect = new Rect();
304 rect.left = (int) (pos[0] * appScale + 0.5f);
305 rect.top = (int) (pos[1] * appScale + 0.5f);
306 rect.right = (int) ((pos[0] + view.getWidth()) * appScale + 0.5f);
307 rect.bottom = (int) ((pos[1] + view.getHeight()) * appScale + 0.5f);
308 return rect;
309 }
Daniel Lehmann2f77c852012-03-30 15:25:31 -0700310
311 /**
312 * Returns the size (width and height) of thumbnail pictures as configured in the provider. This
313 * can safely be called from the UI thread, as the provider can serve this without performing
314 * a database access
315 */
316 public static int getThumbnailSize(Context context) {
317 if (sThumbnailSize == -1) {
318 final Cursor c = context.getContentResolver().query(
319 DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
320 new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null);
321 try {
322 c.moveToFirst();
323 sThumbnailSize = c.getInt(0);
324 } finally {
325 c.close();
326 }
327 }
328 return sThumbnailSize;
329 }
Evan Millar66388be2009-05-28 15:41:07 -0700330}