blob: 0e75a7f0ae1189874f7621f8eb1ed63fd0968e70 [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
Evan Millar45e0ed32009-06-01 16:44:38 -070019import android.content.Context;
Jeff Sharkey3f0b7b82009-08-12 11:28:53 -070020import android.content.Intent;
Bai Taoba344222010-07-28 17:50:23 -070021import android.location.CountryDetector;
Evan Millar66388be2009-05-28 15:41:07 -070022import android.provider.ContactsContract.CommonDataKinds.Im;
Evan Millar66388be2009-05-28 15:41:07 -070023import android.provider.ContactsContract.CommonDataKinds.Phone;
Makoto Onukic710b0e2009-10-13 15:43:24 -070024import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070025import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070026
Evan Millar66388be2009-05-28 15:41:07 -070027public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070028 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070029 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070030
Evan Millar2c1cc832009-07-13 11:08:06 -070031
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060032 // 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 Millar66388be2009-05-28 15:41:07 -070045 /**
46 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060047 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070048 * 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 Sharkeye31dac82009-10-08 11:13:47 -070077 /**
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 Onukic710b0e2009-10-13 15:43:24 -070084
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 Lehmannd8b0a052010-03-25 17:41:00 -070094 * considered equal for collapsing in the GUI. For caller-id, use
95 * {@link PhoneNumberUtils#compare(Context, String, String)} instead
Makoto Onukic710b0e2009-10-13 15:43:24 -070096 */
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070097 public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
Makoto Onukic710b0e2009-10-13 15:43:24 -070098 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 Lehmannd8b0a052010-03-25 17:41:00 -0700107
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 Onukic710b0e2009-10-13 15:43:24 -0700124 } 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 Taoba344222010-07-28 17:50:23 -0700144
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 Tao09eb04f2010-09-01 15:34:16 +0800152 return detector.detectCountry().getCountryIso();
Bai Taoba344222010-07-28 17:50:23 -0700153 }
Evan Millar66388be2009-05-28 15:41:07 -0700154}