blob: a53a423efc8c6ec96e3bede8c5e16c0c7e7bbfbd [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;
Daniel Lehmann2f77c852012-03-30 15:25:31 -070021import android.database.Cursor;
Katherine Kuan08bcf712011-10-09 13:43:53 -070022import android.net.Uri;
23import android.provider.ContactsContract;
Evan Millar66388be2009-05-28 15:41:07 -070024import android.provider.ContactsContract.CommonDataKinds.Im;
Daniel Lehmann2f77c852012-03-30 15:25:31 -070025import android.provider.ContactsContract.DisplayPhoto;
Makoto Onukic710b0e2009-10-13 15:43:24 -070026import android.telephony.PhoneNumberUtils;
Evan Millar66388be2009-05-28 15:41:07 -070027import android.text.TextUtils;
Neel Parekh2ad90a32009-09-20 19:08:50 -070028
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070029import com.android.contacts.model.AccountTypeManager;
Chiao Cheng428f0082012-11-13 18:38:56 -080030import com.android.contacts.common.model.account.AccountType;
31import com.android.contacts.common.model.account.AccountWithDataSet;
32import com.android.contacts.common.test.NeededForTesting;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070033
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -070034import java.util.List;
35
Evan Millar66388be2009-05-28 15:41:07 -070036public class ContactsUtils {
Evan Millar11d628c2009-09-02 08:55:01 -070037 private static final String TAG = "ContactsUtils";
Daniel Lehmannd8b0a052010-03-25 17:41:00 -070038 private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT);
Jeff Sharkey39261272009-06-03 19:15:09 -070039
Daniel Lehmann2f77c852012-03-30 15:25:31 -070040 private static int sThumbnailSize = -1;
Evan Millar2c1cc832009-07-13 11:08:06 -070041
Jeff Hamilton1bf258e2009-12-15 16:55:49 -060042 // TODO find a proper place for the canonical version of these
43 public interface ProviderNames {
44 String YAHOO = "Yahoo";
45 String GTALK = "GTalk";
46 String MSN = "MSN";
47 String ICQ = "ICQ";
48 String AIM = "AIM";
49 String XMPP = "XMPP";
50 String JABBER = "JABBER";
51 String SKYPE = "SKYPE";
52 String QQ = "QQ";
53 }
54
Evan Millar66388be2009-05-28 15:41:07 -070055 /**
56 * This looks up the provider name defined in
Jeff Hamiltoneffb7ff2009-12-17 16:29:40 -060057 * ProviderNames from the predefined IM protocol id.
Evan Millar66388be2009-05-28 15:41:07 -070058 * This is used for interacting with the IM application.
59 *
60 * @param protocol the protocol ID
61 * @return the provider name the IM app uses for the given protocol, or null if no
62 * provider is defined for the given protocol
63 * @hide
64 */
65 public static String lookupProviderNameFromId(int protocol) {
66 switch (protocol) {
67 case Im.PROTOCOL_GOOGLE_TALK:
68 return ProviderNames.GTALK;
69 case Im.PROTOCOL_AIM:
70 return ProviderNames.AIM;
71 case Im.PROTOCOL_MSN:
72 return ProviderNames.MSN;
73 case Im.PROTOCOL_YAHOO:
74 return ProviderNames.YAHOO;
75 case Im.PROTOCOL_ICQ:
76 return ProviderNames.ICQ;
77 case Im.PROTOCOL_JABBER:
78 return ProviderNames.JABBER;
79 case Im.PROTOCOL_SKYPE:
80 return ProviderNames.SKYPE;
81 case Im.PROTOCOL_QQ:
82 return ProviderNames.QQ;
83 }
84 return null;
85 }
86
Jeff Sharkeye31dac82009-10-08 11:13:47 -070087 /**
88 * Test if the given {@link CharSequence} contains any graphic characters,
89 * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null.
90 */
91 public static boolean isGraphic(CharSequence str) {
92 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str);
93 }
Makoto Onukic710b0e2009-10-13 15:43:24 -070094
95 /**
96 * Returns true if two objects are considered equal. Two null references are equal here.
97 */
Hugo Hudsone86b7532011-09-08 17:16:52 +010098 @NeededForTesting
Makoto Onukic710b0e2009-10-13 15:43:24 -070099 public static boolean areObjectsEqual(Object a, Object b) {
100 return a == b || (a != null && a.equals(b));
101 }
102
103 /**
Makoto Onukic710b0e2009-10-13 15:43:24 -0700104 * Returns true if two {@link Intent}s are both null, or have the same action.
105 */
106 public static final boolean areIntentActionEqual(Intent a, Intent b) {
107 if (a == b) {
108 return true;
109 }
110 if (a == null || b == null) {
111 return false;
112 }
113 return TextUtils.equals(a.getAction(), b.getAction());
114 }
Bai Taoba344222010-07-28 17:50:23 -0700115
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700116 public static boolean areContactWritableAccountsAvailable(Context context) {
Isaac Katzenelsona1bbf612011-08-15 16:49:41 -0700117 final List<AccountWithDataSet> accounts =
118 AccountTypeManager.getInstance(context).getAccounts(true /* writeable */);
119 return !accounts.isEmpty();
120 }
121
Makoto Onuki6f74c0f2011-09-27 13:47:15 -0700122 public static boolean areGroupWritableAccountsAvailable(Context context) {
123 final List<AccountWithDataSet> accounts =
124 AccountTypeManager.getInstance(context).getGroupWritableAccounts();
125 return !accounts.isEmpty();
126 }
Katherine Kuan08bcf712011-10-09 13:43:53 -0700127
128 /**
129 * Returns the intent to launch for the given invitable account type and contact lookup URI.
130 * This will return null if the account type is not invitable (i.e. there is no
131 * {@link AccountType#getInviteContactActivityClassName()} or
Makoto Onuki82a4f442012-05-07 17:18:33 -0700132 * {@link AccountType#syncAdapterPackageName}).
Katherine Kuan08bcf712011-10-09 13:43:53 -0700133 */
134 public static Intent getInvitableIntent(AccountType accountType, Uri lookupUri) {
Makoto Onuki82a4f442012-05-07 17:18:33 -0700135 String syncAdapterPackageName = accountType.syncAdapterPackageName;
Katherine Kuan08bcf712011-10-09 13:43:53 -0700136 String className = accountType.getInviteContactActivityClassName();
Makoto Onuki82a4f442012-05-07 17:18:33 -0700137 if (TextUtils.isEmpty(syncAdapterPackageName) || TextUtils.isEmpty(className)) {
Katherine Kuan08bcf712011-10-09 13:43:53 -0700138 return null;
139 }
140 Intent intent = new Intent();
Makoto Onuki82a4f442012-05-07 17:18:33 -0700141 intent.setClassName(syncAdapterPackageName, className);
Katherine Kuan08bcf712011-10-09 13:43:53 -0700142
143 intent.setAction(ContactsContract.Intents.INVITE_CONTACT);
144
145 // Data is the lookup URI.
146 intent.setData(lookupUri);
147 return intent;
148 }
Katherine Kuanf7689c32011-10-24 11:05:55 -0700149
150 /**
Daniel Lehmann2f77c852012-03-30 15:25:31 -0700151 * Returns the size (width and height) of thumbnail pictures as configured in the provider. This
152 * can safely be called from the UI thread, as the provider can serve this without performing
153 * a database access
154 */
155 public static int getThumbnailSize(Context context) {
156 if (sThumbnailSize == -1) {
157 final Cursor c = context.getContentResolver().query(
158 DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
159 new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null);
160 try {
161 c.moveToFirst();
162 sThumbnailSize = c.getInt(0);
163 } finally {
164 c.close();
165 }
166 }
167 return sThumbnailSize;
168 }
Geobio Boo4356dac2012-08-06 16:28:46 -0700169
Evan Millar66388be2009-05-28 15:41:07 -0700170}