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 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Jeff Sharkey | 3f0b7b8 | 2009-08-12 11:28:53 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 21 | import android.database.Cursor; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 22 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract.DisplayPhoto; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 24 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 26 | |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 28 | import com.android.contacts.common.test.NeededForTesting; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.model.AccountTypeManager; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 30 | |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 31 | import java.util.List; |
| 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"; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 35 | |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 36 | private static int sThumbnailSize = -1; |
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 | |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 85 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 86 | */ |
| 87 | public static boolean isGraphic(CharSequence str) { |
| 88 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 89 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 93 | */ |
Hugo Hudson | e86b753 | 2011-09-08 17:16:52 +0100 | [diff] [blame] | 94 | @NeededForTesting |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 95 | public static boolean areObjectsEqual(Object a, Object b) { |
| 96 | return a == b || (a != null && a.equals(b)); |
| 97 | } |
| 98 | |
| 99 | /** |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 100 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 101 | */ |
| 102 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 103 | if (a == b) { |
| 104 | return true; |
| 105 | } |
| 106 | if (a == null || b == null) { |
| 107 | return false; |
| 108 | } |
| 109 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 110 | } |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 111 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 112 | public static boolean areContactWritableAccountsAvailable(Context context) { |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 113 | final List<AccountWithDataSet> accounts = |
| 114 | AccountTypeManager.getInstance(context).getAccounts(true /* writeable */); |
| 115 | return !accounts.isEmpty(); |
| 116 | } |
| 117 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 118 | public static boolean areGroupWritableAccountsAvailable(Context context) { |
| 119 | final List<AccountWithDataSet> accounts = |
| 120 | AccountTypeManager.getInstance(context).getGroupWritableAccounts(); |
| 121 | return !accounts.isEmpty(); |
| 122 | } |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 125 | * Returns the size (width and height) of thumbnail pictures as configured in the provider. This |
| 126 | * can safely be called from the UI thread, as the provider can serve this without performing |
| 127 | * a database access |
| 128 | */ |
| 129 | public static int getThumbnailSize(Context context) { |
| 130 | if (sThumbnailSize == -1) { |
| 131 | final Cursor c = context.getContentResolver().query( |
| 132 | DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI, |
| 133 | new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null); |
| 134 | try { |
| 135 | c.moveToFirst(); |
| 136 | sThumbnailSize = c.getInt(0); |
| 137 | } finally { |
| 138 | c.close(); |
| 139 | } |
| 140 | } |
| 141 | return sThumbnailSize; |
| 142 | } |
Geobio Boo | 4356dac | 2012-08-06 16:28:46 -0700 | [diff] [blame] | 143 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 144 | } |