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