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 17 package com.android.contacts.common; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.provider.ContactsContract.CommonDataKinds.Im; 24 import android.provider.ContactsContract.DisplayPhoto; 25 import android.support.annotation.IntDef; 26 import android.text.TextUtils; 27 import android.util.Pair; 28 import com.android.contacts.common.compat.ContactsCompat; 29 import com.android.contacts.common.compat.DirectoryCompat; 30 import com.android.contacts.common.model.AccountTypeManager; 31 import com.android.contacts.common.model.account.AccountWithDataSet; 32 import com.android.contacts.common.model.dataitem.ImDataItem; 33 import java.lang.annotation.Retention; 34 import java.lang.annotation.RetentionPolicy; 35 import java.util.List; 36 37 public class ContactsUtils { 38 39 // Telecomm related schemes are in CallUtil 40 public static final String SCHEME_IMTO = "imto"; 41 public static final String SCHEME_MAILTO = "mailto"; 42 public static final String SCHEME_SMSTO = "smsto"; 43 public static final long USER_TYPE_CURRENT = 0; 44 public static final long USER_TYPE_WORK = 1; 45 private static final String TAG = "ContactsUtils"; 46 private static final int DEFAULT_THUMBNAIL_SIZE = 96; 47 private static int sThumbnailSize = -1; 48 49 /** 50 * This looks up the provider name defined in ProviderNames from the predefined IM protocol id. 51 * This is used for interacting with the IM application. 52 * 53 * @param protocol the protocol ID 54 * @return the provider name the IM app uses for the given protocol, or null if no provider is 55 * defined for the given protocol 56 * @hide 57 */ lookupProviderNameFromId(int protocol)58 public static String lookupProviderNameFromId(int protocol) { 59 switch (protocol) { 60 case Im.PROTOCOL_GOOGLE_TALK: 61 return ProviderNames.GTALK; 62 case Im.PROTOCOL_AIM: 63 return ProviderNames.AIM; 64 case Im.PROTOCOL_MSN: 65 return ProviderNames.MSN; 66 case Im.PROTOCOL_YAHOO: 67 return ProviderNames.YAHOO; 68 case Im.PROTOCOL_ICQ: 69 return ProviderNames.ICQ; 70 case Im.PROTOCOL_JABBER: 71 return ProviderNames.JABBER; 72 case Im.PROTOCOL_SKYPE: 73 return ProviderNames.SKYPE; 74 case Im.PROTOCOL_QQ: 75 return ProviderNames.QQ; 76 } 77 return null; 78 } 79 80 /** 81 * Test if the given {@link CharSequence} contains any graphic characters, first checking {@link 82 * TextUtils#isEmpty(CharSequence)} to handle null. 83 */ isGraphic(CharSequence str)84 public static boolean isGraphic(CharSequence str) { 85 return !TextUtils.isEmpty(str) && TextUtils.isGraphic(str); 86 } 87 88 /** Returns true if two objects are considered equal. Two null references are equal here. */ areObjectsEqual(Object a, Object b)89 public static boolean areObjectsEqual(Object a, Object b) { 90 return a == b || (a != null && a.equals(b)); 91 } 92 93 /** Returns true if two {@link Intent}s are both null, or have the same action. */ areIntentActionEqual(Intent a, Intent b)94 public static final boolean areIntentActionEqual(Intent a, Intent b) { 95 if (a == b) { 96 return true; 97 } 98 if (a == null || b == null) { 99 return false; 100 } 101 return TextUtils.equals(a.getAction(), b.getAction()); 102 } 103 areGroupWritableAccountsAvailable(Context context)104 public static boolean areGroupWritableAccountsAvailable(Context context) { 105 final List<AccountWithDataSet> accounts = 106 AccountTypeManager.getInstance(context).getGroupWritableAccounts(); 107 return !accounts.isEmpty(); 108 } 109 110 /** 111 * Returns the size (width and height) of thumbnail pictures as configured in the provider. This 112 * can safely be called from the UI thread, as the provider can serve this without performing a 113 * database access 114 */ getThumbnailSize(Context context)115 public static int getThumbnailSize(Context context) { 116 if (sThumbnailSize == -1) { 117 final Cursor c = 118 context 119 .getContentResolver() 120 .query( 121 DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI, 122 new String[] {DisplayPhoto.THUMBNAIL_MAX_DIM}, 123 null, 124 null, 125 null); 126 if (c != null) { 127 try { 128 if (c.moveToFirst()) { 129 sThumbnailSize = c.getInt(0); 130 } 131 } finally { 132 c.close(); 133 } 134 } 135 } 136 return sThumbnailSize != -1 ? sThumbnailSize : DEFAULT_THUMBNAIL_SIZE; 137 } 138 getCustomImIntent(ImDataItem im, int protocol)139 private static Intent getCustomImIntent(ImDataItem im, int protocol) { 140 String host = im.getCustomProtocol(); 141 final String data = im.getData(); 142 if (TextUtils.isEmpty(data)) { 143 return null; 144 } 145 if (protocol != Im.PROTOCOL_CUSTOM) { 146 // Try bringing in a well-known host for specific protocols 147 host = ContactsUtils.lookupProviderNameFromId(protocol); 148 } 149 if (TextUtils.isEmpty(host)) { 150 return null; 151 } 152 final String authority = host.toLowerCase(); 153 final Uri imUri = 154 new Uri.Builder().scheme(SCHEME_IMTO).authority(authority).appendPath(data).build(); 155 final Intent intent = new Intent(Intent.ACTION_SENDTO, imUri); 156 return intent; 157 } 158 159 /** 160 * Returns the proper Intent for an ImDatItem. If available, a secondary intent is stored in the 161 * second Pair slot 162 */ buildImIntent(Context context, ImDataItem im)163 public static Pair<Intent, Intent> buildImIntent(Context context, ImDataItem im) { 164 Intent intent = null; 165 Intent secondaryIntent = null; 166 final boolean isEmail = im.isCreatedFromEmail(); 167 168 if (!isEmail && !im.isProtocolValid()) { 169 return new Pair<>(null, null); 170 } 171 172 final String data = im.getData(); 173 if (TextUtils.isEmpty(data)) { 174 return new Pair<>(null, null); 175 } 176 177 final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol(); 178 179 if (protocol == Im.PROTOCOL_GOOGLE_TALK) { 180 final int chatCapability = im.getChatCapability(); 181 if ((chatCapability & Im.CAPABILITY_HAS_CAMERA) != 0) { 182 intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")); 183 secondaryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")); 184 } else if ((chatCapability & Im.CAPABILITY_HAS_VOICE) != 0) { 185 // Allow Talking and Texting 186 intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")); 187 secondaryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?call")); 188 } else { 189 intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("xmpp:" + data + "?message")); 190 } 191 } else { 192 // Build an IM Intent 193 intent = getCustomImIntent(im, protocol); 194 } 195 return new Pair<>(intent, secondaryIntent); 196 } 197 198 /** 199 * Determine UserType from directory id and contact id. 200 * 201 * <p>3 types of query 202 * 203 * <p>1. 2 profile query: content://com.android.contacts/phone_lookup_enterprise/1234567890 204 * personal and work contact are mixed into one cursor. no directory id. contact_id indicates if 205 * it's work contact 206 * 207 * <p>2. work local query: 208 * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000000 either 209 * directory_id or contact_id is enough to identify work contact 210 * 211 * <p>3. work remote query: 212 * content://com.android.contacts/phone_lookup_enterprise/1234567890?directory=1000000003 213 * contact_id is random. only directory_id is available 214 * 215 * <p>Summary: If directory_id is not null, always use directory_id to identify work contact. 216 * (which is the case here) Otherwise, use contact_id. 217 * 218 * @param directoryId directory id of ContactsProvider query 219 * @param contactId contact id 220 * @return UserType indicates the user type of the contact. A directory id or contact id larger 221 * than a thredshold indicates that the contact is stored in Work Profile, but not in current 222 * user. It's a contract by ContactsProvider and check by Contacts.isEnterpriseDirectoryId and 223 * Contacts.isEnterpriseContactId. Currently, only 2 kinds of users can be detected from the 224 * directoryId and contactId as ContactsProvider can only access current and work user's 225 * contacts 226 */ determineUserType(Long directoryId, Long contactId)227 public static @UserType long determineUserType(Long directoryId, Long contactId) { 228 // First check directory id 229 if (directoryId != null) { 230 return DirectoryCompat.isEnterpriseDirectoryId(directoryId) 231 ? USER_TYPE_WORK 232 : USER_TYPE_CURRENT; 233 } 234 // Only check contact id if directory id is null 235 if (contactId != null && contactId != 0L && ContactsCompat.isEnterpriseContactId(contactId)) { 236 return USER_TYPE_WORK; 237 } else { 238 return USER_TYPE_CURRENT; 239 } 240 } 241 242 // TODO find a proper place for the canonical version of these 243 public interface ProviderNames { 244 245 String YAHOO = "Yahoo"; 246 String GTALK = "GTalk"; 247 String MSN = "MSN"; 248 String ICQ = "ICQ"; 249 String AIM = "AIM"; 250 String XMPP = "XMPP"; 251 String JABBER = "JABBER"; 252 String SKYPE = "SKYPE"; 253 String QQ = "QQ"; 254 } 255 256 /** 257 * UserType indicates the user type of the contact. If the contact is from Work User (Work Profile 258 * in Android Multi-User System), it's {@link #USER_TYPE_WORK}, otherwise, {@link 259 * #USER_TYPE_CURRENT}. Please note that current user can be in work profile, where the dialer is 260 * running inside Work Profile. 261 */ 262 @Retention(RetentionPolicy.SOURCE) 263 @IntDef({USER_TYPE_CURRENT, USER_TYPE_WORK}) 264 public @interface UserType {} 265 } 266