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; |
Katherine Kuan | 7a700cf | 2011-10-27 15:49:18 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 23 | import android.net.Uri; |
| 24 | import android.provider.ContactsContract; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 25 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 26 | import android.provider.ContactsContract.DisplayPhoto; |
Daisuke Miyakawa | fadd5e1 | 2011-12-01 17:34:18 -0800 | [diff] [blame] | 27 | import android.provider.ContactsContract.QuickContact; |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 28 | import android.telephony.PhoneNumberUtils; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 29 | import android.text.TextUtils; |
Katherine Kuan | f7689c3 | 2011-10-24 11:05:55 -0700 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.widget.TextView; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 32 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 33 | import com.android.contacts.model.AccountTypeManager; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 34 | import com.android.contacts.model.account.AccountType; |
| 35 | import com.android.contacts.model.account.AccountWithDataSet; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 36 | import com.android.contacts.test.NeededForTesting; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 37 | |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 40 | public class ContactsUtils { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 41 | private static final String TAG = "ContactsUtils"; |
Daniel Lehmann | d8b0a05 | 2010-03-25 17:41:00 -0700 | [diff] [blame] | 42 | private static final String WAIT_SYMBOL_AS_STRING = String.valueOf(PhoneNumberUtils.WAIT); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 43 | |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 44 | private static int sThumbnailSize = -1; |
Evan Millar | 2c1cc83 | 2009-07-13 11:08:06 -0700 | [diff] [blame] | 45 | |
Jeff Hamilton | 1bf258e | 2009-12-15 16:55:49 -0600 | [diff] [blame] | 46 | // TODO find a proper place for the canonical version of these |
| 47 | public interface ProviderNames { |
| 48 | String YAHOO = "Yahoo"; |
| 49 | String GTALK = "GTalk"; |
| 50 | String MSN = "MSN"; |
| 51 | String ICQ = "ICQ"; |
| 52 | String AIM = "AIM"; |
| 53 | String XMPP = "XMPP"; |
| 54 | String JABBER = "JABBER"; |
| 55 | String SKYPE = "SKYPE"; |
| 56 | String QQ = "QQ"; |
| 57 | } |
| 58 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 59 | /** |
| 60 | * This looks up the provider name defined in |
Jeff Hamilton | effb7ff | 2009-12-17 16:29:40 -0600 | [diff] [blame] | 61 | * ProviderNames from the predefined IM protocol id. |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 62 | * This is used for interacting with the IM application. |
| 63 | * |
| 64 | * @param protocol the protocol ID |
| 65 | * @return the provider name the IM app uses for the given protocol, or null if no |
| 66 | * provider is defined for the given protocol |
| 67 | * @hide |
| 68 | */ |
| 69 | public static String lookupProviderNameFromId(int protocol) { |
| 70 | switch (protocol) { |
| 71 | case Im.PROTOCOL_GOOGLE_TALK: |
| 72 | return ProviderNames.GTALK; |
| 73 | case Im.PROTOCOL_AIM: |
| 74 | return ProviderNames.AIM; |
| 75 | case Im.PROTOCOL_MSN: |
| 76 | return ProviderNames.MSN; |
| 77 | case Im.PROTOCOL_YAHOO: |
| 78 | return ProviderNames.YAHOO; |
| 79 | case Im.PROTOCOL_ICQ: |
| 80 | return ProviderNames.ICQ; |
| 81 | case Im.PROTOCOL_JABBER: |
| 82 | return ProviderNames.JABBER; |
| 83 | case Im.PROTOCOL_SKYPE: |
| 84 | return ProviderNames.SKYPE; |
| 85 | case Im.PROTOCOL_QQ: |
| 86 | return ProviderNames.QQ; |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
Jeff Sharkey | e31dac8 | 2009-10-08 11:13:47 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Test if the given {@link CharSequence} contains any graphic characters, |
| 93 | * first checking {@link TextUtils#isEmpty(CharSequence)} to handle null. |
| 94 | */ |
| 95 | public static boolean isGraphic(CharSequence str) { |
| 96 | return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); |
| 97 | } |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Returns true if two objects are considered equal. Two null references are equal here. |
| 101 | */ |
Hugo Hudson | e86b753 | 2011-09-08 17:16:52 +0100 | [diff] [blame] | 102 | @NeededForTesting |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 103 | public static boolean areObjectsEqual(Object a, Object b) { |
| 104 | return a == b || (a != null && a.equals(b)); |
| 105 | } |
| 106 | |
| 107 | /** |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 108 | * Returns true if two {@link Intent}s are both null, or have the same action. |
| 109 | */ |
| 110 | public static final boolean areIntentActionEqual(Intent a, Intent b) { |
| 111 | if (a == b) { |
| 112 | return true; |
| 113 | } |
| 114 | if (a == null || b == null) { |
| 115 | return false; |
| 116 | } |
| 117 | return TextUtils.equals(a.getAction(), b.getAction()); |
| 118 | } |
Bai Tao | ba34422 | 2010-07-28 17:50:23 -0700 | [diff] [blame] | 119 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 120 | public static boolean areContactWritableAccountsAvailable(Context context) { |
Isaac Katzenelson | a1bbf61 | 2011-08-15 16:49:41 -0700 | [diff] [blame] | 121 | final List<AccountWithDataSet> accounts = |
| 122 | AccountTypeManager.getInstance(context).getAccounts(true /* writeable */); |
| 123 | return !accounts.isEmpty(); |
| 124 | } |
| 125 | |
Makoto Onuki | 6f74c0f | 2011-09-27 13:47:15 -0700 | [diff] [blame] | 126 | public static boolean areGroupWritableAccountsAvailable(Context context) { |
| 127 | final List<AccountWithDataSet> accounts = |
| 128 | AccountTypeManager.getInstance(context).getGroupWritableAccounts(); |
| 129 | return !accounts.isEmpty(); |
| 130 | } |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Returns the intent to launch for the given invitable account type and contact lookup URI. |
| 134 | * This will return null if the account type is not invitable (i.e. there is no |
| 135 | * {@link AccountType#getInviteContactActivityClassName()} or |
Makoto Onuki | 82a4f44 | 2012-05-07 17:18:33 -0700 | [diff] [blame] | 136 | * {@link AccountType#syncAdapterPackageName}). |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 137 | */ |
| 138 | public static Intent getInvitableIntent(AccountType accountType, Uri lookupUri) { |
Makoto Onuki | 82a4f44 | 2012-05-07 17:18:33 -0700 | [diff] [blame] | 139 | String syncAdapterPackageName = accountType.syncAdapterPackageName; |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 140 | String className = accountType.getInviteContactActivityClassName(); |
Makoto Onuki | 82a4f44 | 2012-05-07 17:18:33 -0700 | [diff] [blame] | 141 | if (TextUtils.isEmpty(syncAdapterPackageName) || TextUtils.isEmpty(className)) { |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 142 | return null; |
| 143 | } |
| 144 | Intent intent = new Intent(); |
Makoto Onuki | 82a4f44 | 2012-05-07 17:18:33 -0700 | [diff] [blame] | 145 | intent.setClassName(syncAdapterPackageName, className); |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 146 | |
| 147 | intent.setAction(ContactsContract.Intents.INVITE_CONTACT); |
| 148 | |
| 149 | // Data is the lookup URI. |
| 150 | intent.setData(lookupUri); |
| 151 | return intent; |
| 152 | } |
Katherine Kuan | f7689c3 | 2011-10-24 11:05:55 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Returns a header view based on the R.layout.list_separator, where the |
| 156 | * containing {@link TextView} is set using the given textResourceId. |
| 157 | */ |
| 158 | public static View createHeaderView(Context context, int textResourceId) { |
| 159 | View view = View.inflate(context, R.layout.list_separator, null); |
| 160 | TextView textView = (TextView) view.findViewById(R.id.title); |
| 161 | textView.setText(context.getString(textResourceId)); |
| 162 | return view; |
| 163 | } |
Katherine Kuan | 7a700cf | 2011-10-27 15:49:18 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Returns the {@link Rect} with left, top, right, and bottom coordinates |
| 167 | * that are equivalent to the given {@link View}'s bounds. This is equivalent to how the |
| 168 | * target {@link Rect} is calculated in {@link QuickContact#showQuickContact}. |
| 169 | */ |
| 170 | public static Rect getTargetRectFromView(Context context, View view) { |
| 171 | final float appScale = context.getResources().getCompatibilityInfo().applicationScale; |
| 172 | final int[] pos = new int[2]; |
| 173 | view.getLocationOnScreen(pos); |
| 174 | |
| 175 | final Rect rect = new Rect(); |
| 176 | rect.left = (int) (pos[0] * appScale + 0.5f); |
| 177 | rect.top = (int) (pos[1] * appScale + 0.5f); |
| 178 | rect.right = (int) ((pos[0] + view.getWidth()) * appScale + 0.5f); |
| 179 | rect.bottom = (int) ((pos[1] + view.getHeight()) * appScale + 0.5f); |
| 180 | return rect; |
| 181 | } |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Returns the size (width and height) of thumbnail pictures as configured in the provider. This |
| 185 | * can safely be called from the UI thread, as the provider can serve this without performing |
| 186 | * a database access |
| 187 | */ |
| 188 | public static int getThumbnailSize(Context context) { |
| 189 | if (sThumbnailSize == -1) { |
| 190 | final Cursor c = context.getContentResolver().query( |
| 191 | DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI, |
| 192 | new String[] { DisplayPhoto.THUMBNAIL_MAX_DIM }, null, null, null); |
| 193 | try { |
| 194 | c.moveToFirst(); |
| 195 | sThumbnailSize = c.getInt(0); |
| 196 | } finally { |
| 197 | c.close(); |
| 198 | } |
| 199 | } |
| 200 | return sThumbnailSize; |
| 201 | } |
Geobio Boo | 4356dac | 2012-08-06 16:28:46 -0700 | [diff] [blame] | 202 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 203 | } |