1 /* 2 * Copyright (C) 2015 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.cts.managedprofile; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.annotation.TargetApi; 22 import android.app.admin.DevicePolicyManager; 23 import android.content.ContentProviderOperation; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.content.OperationApplicationException; 27 import android.content.res.Resources; 28 import android.content.res.Resources.NotFoundException; 29 import android.database.Cursor; 30 import android.database.DatabaseUtils; 31 import android.graphics.Rect; 32 import android.net.Uri; 33 import android.os.Build; 34 import android.os.RemoteException; 35 import android.provider.ContactsContract; 36 import android.provider.ContactsContract.CommonDataKinds.Callable; 37 import android.provider.ContactsContract.CommonDataKinds.Email; 38 import android.provider.ContactsContract.CommonDataKinds.Phone; 39 import android.provider.ContactsContract.CommonDataKinds.Photo; 40 import android.provider.ContactsContract.Contacts; 41 import android.provider.ContactsContract.Directory; 42 import android.provider.ContactsContract.PhoneLookup; 43 import android.provider.ContactsContract.RawContacts; 44 import android.test.AndroidTestCase; 45 import android.util.Log; 46 47 import java.io.ByteArrayOutputStream; 48 import java.io.FileNotFoundException; 49 import java.io.IOException; 50 import java.io.InputStream; 51 import java.util.ArrayList; 52 import java.util.Arrays; 53 54 public class ContactsTest extends AndroidTestCase { 55 56 private static final String TAG = "ContactsTest"; 57 58 private static final String TEST_ACCOUNT_NAME = AccountAuthenticator.ACCOUNT_NAME; 59 private static final String TEST_ACCOUNT_TYPE = AccountAuthenticator.ACCOUNT_TYPE; 60 // details of a sample primary contact 61 private static final String PRIMARY_CONTACT_DISPLAY_NAME = "Primary"; 62 private static final String PRIMARY_CONTACT_PHONE = "00000001"; 63 private static final String PRIMARY_CONTACT_EMAIL = "one (at) primary.com"; 64 private static final String PRIMARY_CONTACT_SIP = "foo@sip"; 65 66 // details of a sample managed contact 67 private static final String MANAGED_CONTACT_DISPLAY_NAME = "Managed"; 68 private static final String MANAGED_CONTACT_PHONE = "6891999"; 69 private static final String MANAGED_CONTACT_EMAIL = "one (at) managed.com"; 70 private static final String MANAGED_CONTACT_SIP = "bar@sip"; 71 72 // details of a sample primary and a sample managed contact, with the same phone & email 73 private static final String PRIMARY_CONTACT_DISPLAY_NAME_2 = "PrimaryShared"; 74 private static final String MANAGED_CONTACT_DISPLAY_NAME_2 = "ManagedShared"; 75 private static final String SHARED_CONTACT_PHONE = "00000002"; 76 private static final String SHARED_CONTACT_EMAIL = "shared (at) shared.com"; 77 private static final String SHARED_CONTACT_SIP = "baz@sip"; 78 79 // Directory display name 80 private static final String PRIMARY_DIRECTORY_NAME = "PrimaryDirectory"; 81 private static final String MANAGED_DIRECTORY_NAME = "ManagedDirectory"; 82 private static final String PRIMARY_DIRECTORY_CONTACT_NAME = "PrimaryDirectoryContact"; 83 private static final String MANAGED_DIRECTORY_CONTACT_NAME = "ManagedDirectoryContact"; 84 85 // Directory Authority 86 private static final String DIRECTORY_PROVIDER_AUTHORITY = "com.android.cts.contact.directory.provider"; 87 88 89 // Retry directory query so we can make sure directory info in cp2 is updated 90 private static final int MAX_RETRY_DIRECTORY_QUERY = 10; 91 private static final int RETRY_DIRECTORY_QUERY_INTERVAL = 1000; // 1s 92 93 private DevicePolicyManager mDevicePolicyManager; 94 private ContentResolver mResolver; 95 96 private class ContactInfo { // Not static to access outer world. 97 98 String contactId; 99 String displayName; 100 String photoUri; 101 String photoThumbnailUri; 102 String photoId; 103 104 public ContactInfo(String contactId, String displayName, String photoUri, 105 String photoThumbnailUri, String photoId) { 106 this.contactId = contactId; 107 this.displayName = displayName; 108 this.photoUri = photoUri; 109 this.photoThumbnailUri = photoThumbnailUri; 110 this.photoId = photoId; 111 } 112 113 private void assertNoPhotoUri() { 114 assertNull(photoUri); 115 assertNull(photoThumbnailUri); 116 } 117 118 private void assertPhotoUrisReadable() { 119 assertPhotoUriReadable(photoUri); 120 assertPhotoUriReadable(photoThumbnailUri); 121 } 122 123 private void assertThumbnailUri(int resId) { 124 Resources resources = mContext.getResources(); 125 assertNotNull(this.photoThumbnailUri); 126 byte[] actualPhotoThumbnail = getByteFromStreamForTest( 127 getInputStreamFromUriForTest(this.photoThumbnailUri)); 128 byte[] expectedPhotoThumbnail = getByteFromStreamForTest( 129 resources.openRawResource(resId)); 130 assertTrue(Arrays.equals(expectedPhotoThumbnail, actualPhotoThumbnail)); 131 } 132 133 private void assertPhotoUri(int resId) { 134 Resources resources = mContext.getResources(); 135 assertNotNull(this.photoUri); 136 byte[] actualPhoto = getByteFromStreamForTest( 137 getInputStreamFromUriForTest(this.photoUri)); 138 byte[] expectedPhoto = getByteFromStreamForTest( 139 resources.openRawResource(resId)); 140 assertTrue(Arrays.equals(expectedPhoto, actualPhoto)); 141 } 142 143 private boolean hasPhotoId() { 144 return photoId != null && Long.parseLong(photoId) > 0; 145 } 146 } 147 148 @Override 149 protected void setUp() throws Exception { 150 super.setUp(); 151 mResolver = getContext().getContentResolver(); 152 mDevicePolicyManager = (DevicePolicyManager) mContext 153 .getSystemService(Context.DEVICE_POLICY_SERVICE); 154 } 155 156 public void testAddTestAccount() { 157 Account account = new Account(TEST_ACCOUNT_NAME, TEST_ACCOUNT_TYPE); 158 AccountManager.get(getContext()).addAccountExplicitly(account, null, null); 159 } 160 161 public void testPrimaryProfilePhoneAndEmailLookup_insertedAndfound() throws RemoteException, 162 OperationApplicationException, NotFoundException, IOException { 163 assertFalse(isManagedProfile()); 164 // Do not insert to primary contact 165 insertContact(PRIMARY_CONTACT_DISPLAY_NAME, PRIMARY_CONTACT_PHONE, 166 PRIMARY_CONTACT_EMAIL, PRIMARY_CONTACT_SIP, 0); 167 168 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 169 PRIMARY_CONTACT_PHONE); 170 assertNotNull(contactInfo); 171 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, contactInfo.displayName); 172 contactInfo.assertNoPhotoUri(); 173 assertFalse(contactInfo.hasPhotoId()); 174 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 175 176 contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise*/, 177 PRIMARY_CONTACT_EMAIL); 178 assertNotNull(contactInfo); 179 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, contactInfo.displayName); 180 contactInfo.assertNoPhotoUri(); 181 assertFalse(contactInfo.hasPhotoId()); 182 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 183 184 } 185 186 public void testManagedProfilePhoneAndEmailLookup_insertedAndfound() throws RemoteException, 187 OperationApplicationException, NotFoundException, IOException { 188 assertTrue(isManagedProfile()); 189 // Insert ic_contact_picture as photo in managed contact 190 insertContact(MANAGED_CONTACT_DISPLAY_NAME, 191 MANAGED_CONTACT_PHONE, 192 MANAGED_CONTACT_EMAIL, 193 MANAGED_CONTACT_SIP, 194 com.android.cts.managedprofile.R.raw.ic_contact_picture); 195 196 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 197 MANAGED_CONTACT_PHONE); 198 assertNotNull(contactInfo); 199 assertEquals(MANAGED_CONTACT_DISPLAY_NAME, contactInfo.displayName); 200 contactInfo.assertPhotoUrisReadable(); 201 assertTrue(contactInfo.hasPhotoId()); 202 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 203 204 contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise*/, 205 MANAGED_CONTACT_EMAIL); 206 assertNotNull(contactInfo); 207 assertEquals(MANAGED_CONTACT_DISPLAY_NAME, contactInfo.displayName); 208 contactInfo.assertPhotoUrisReadable(); 209 assertTrue(contactInfo.hasPhotoId()); 210 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 211 } 212 213 public void testPrimaryProfileDuplicatedPhoneEmailContact_insertedAndfound() throws 214 RemoteException, OperationApplicationException, NotFoundException, IOException { 215 assertFalse(isManagedProfile()); 216 insertContact(PRIMARY_CONTACT_DISPLAY_NAME_2, 217 SHARED_CONTACT_PHONE, 218 SHARED_CONTACT_EMAIL, 219 SHARED_CONTACT_SIP, 220 com.android.cts.managedprofile.R.raw.ic_contact_picture); 221 222 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 223 SHARED_CONTACT_PHONE); 224 assertNotNull(contactInfo); 225 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 226 contactInfo.assertPhotoUrisReadable(); 227 assertTrue(contactInfo.hasPhotoId()); 228 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 229 230 contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise*/, SHARED_CONTACT_EMAIL); 231 assertNotNull(contactInfo); 232 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 233 contactInfo.assertPhotoUrisReadable(); 234 assertTrue(contactInfo.hasPhotoId()); 235 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 236 } 237 238 public void testManagedProfileDuplicatedPhoneEmailContact_insertedAndfound() throws 239 RemoteException, OperationApplicationException, NotFoundException, IOException { 240 assertTrue(isManagedProfile()); 241 insertContact(MANAGED_CONTACT_DISPLAY_NAME_2, SHARED_CONTACT_PHONE, 242 SHARED_CONTACT_EMAIL, SHARED_CONTACT_SIP , 0); 243 244 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 245 SHARED_CONTACT_PHONE); 246 assertNotNull(contactInfo); 247 assertEquals(MANAGED_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 248 contactInfo.assertNoPhotoUri(); 249 assertFalse(contactInfo.hasPhotoId()); 250 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 251 252 contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise*/, SHARED_CONTACT_EMAIL); 253 assertNotNull(contactInfo); 254 assertEquals(MANAGED_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 255 contactInfo.assertNoPhotoUri(); 256 assertFalse(contactInfo.hasPhotoId()); 257 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 258 } 259 260 public void testPrimaryProfileEnterprisePhoneLookup_canAccessEnterpriseContact() 261 throws IOException { 262 assertFalse(isManagedProfile()); 263 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 264 MANAGED_CONTACT_PHONE); 265 assertManagedLocalContact(contactInfo); 266 contactInfo.assertPhotoUrisReadable(); 267 // Cannot get photo id in ENTERPRISE_CONTENT_FILTER_URI 268 assertFalse(contactInfo.hasPhotoId()); 269 assertTrue(isEnterpriseContactId(contactInfo.contactId)); 270 } 271 272 public void testPrimaryProfileEnterpriseSipLookup_canAccessEnterpriseContact() 273 throws IOException { 274 assertFalse(isManagedProfile()); 275 ContactInfo contactInfo = getContactInfoFromEnterprisePhoneLookupUriWithSipAddress( 276 true /*isEnterprise*/, MANAGED_CONTACT_SIP); 277 assertManagedLocalContact(contactInfo); 278 contactInfo.assertPhotoUrisReadable(); 279 assertFalse(contactInfo.hasPhotoId()); 280 assertTrue(isEnterpriseContactId(contactInfo.contactId)); 281 } 282 283 public void testPrimaryProfileEnterpriseEmailLookup_canAccessEnterpriseContact() 284 throws IOException { 285 assertFalse(isManagedProfile()); 286 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 287 MANAGED_CONTACT_EMAIL); 288 assertManagedLocalContact(contactInfo); 289 contactInfo.assertPhotoUrisReadable(); 290 // Cannot get photo id in ENTERPRISE_CONTENT_FILTER_URI 291 assertFalse(contactInfo.hasPhotoId()); 292 assertTrue(isEnterpriseContactId(contactInfo.contactId)); 293 } 294 295 public void testPrimaryProfileEnterprisePhoneLookupDuplicated_canAccessPrimaryContact() 296 throws IOException { 297 assertFalse(isManagedProfile()); 298 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 299 SHARED_CONTACT_PHONE); 300 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 301 contactInfo.assertPhotoUrisReadable(); 302 assertTrue(contactInfo.hasPhotoId()); 303 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 304 } 305 306 public void testPrimaryProfileEnterpriseEmailLookupDuplicated_canAccessPrimaryContact() 307 throws IOException { 308 assertFalse(isManagedProfile()); 309 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 310 SHARED_CONTACT_EMAIL); 311 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 312 contactInfo.assertPhotoUrisReadable(); 313 assertTrue(contactInfo.hasPhotoId()); 314 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 315 } 316 317 public void testManagedProfileEnterprisePhoneLookupDuplicated_canAccessEnterpriseContact() { 318 assertTrue(isManagedProfile()); 319 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 320 SHARED_CONTACT_PHONE); 321 assertEquals(MANAGED_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 322 contactInfo.assertNoPhotoUri(); 323 assertFalse(contactInfo.hasPhotoId()); 324 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 325 } 326 327 public void testManagedProfileEnterpriseEmailLookupDuplicated_canAccessEnterpriseContact() { 328 assertTrue(isManagedProfile()); 329 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 330 SHARED_CONTACT_EMAIL); 331 assertEquals(MANAGED_CONTACT_DISPLAY_NAME_2, contactInfo.displayName); 332 contactInfo.assertNoPhotoUri(); 333 assertFalse(contactInfo.hasPhotoId()); 334 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 335 } 336 337 public void testPrimaryProfilePhoneLookup_canNotAccessEnterpriseContact() { 338 assertFalse(isManagedProfile()); 339 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 340 MANAGED_CONTACT_PHONE); 341 assertNull(contactInfo); 342 } 343 344 public void testPrimaryProfileEmailLookup_canNotAccessEnterpriseContact() { 345 assertFalse(isManagedProfile()); 346 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise*/, 347 MANAGED_CONTACT_EMAIL); 348 assertNull(contactInfo); 349 } 350 351 public void testPrimaryProfileEnterprisePhoneLookup_canAccessPrimaryContact() { 352 assertFalse(isManagedProfile()); 353 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 354 PRIMARY_CONTACT_PHONE); 355 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, contactInfo.displayName); 356 contactInfo.assertNoPhotoUri(); 357 assertFalse(contactInfo.hasPhotoId()); 358 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 359 } 360 361 public void testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryContact() { 362 assertFalse(isManagedProfile()); 363 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 364 PRIMARY_CONTACT_EMAIL); 365 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, contactInfo.displayName); 366 contactInfo.assertNoPhotoUri(); 367 assertFalse(contactInfo.hasPhotoId()); 368 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 369 } 370 371 public void testManagedProfileEnterprisePhoneLookup_canAccessEnterpriseContact() { 372 assertTrue(isManagedProfile()); 373 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 374 MANAGED_CONTACT_PHONE); 375 assertManagedLocalContact(contactInfo); 376 contactInfo.assertPhotoUrisReadable(); 377 assertTrue(contactInfo.hasPhotoId()); 378 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 379 } 380 381 public void testManagedProfileEnterpriseEmailLookup_canAccessEnterpriseContact() { 382 assertTrue(isManagedProfile()); 383 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 384 MANAGED_CONTACT_EMAIL); 385 assertManagedLocalContact(contactInfo); 386 contactInfo.assertPhotoUrisReadable(); 387 assertTrue(contactInfo.hasPhotoId()); 388 assertFalse(isEnterpriseContactId(contactInfo.contactId)); 389 } 390 391 public void testManagedProfileEnterprisePhoneLookup_canNotAccessPrimaryContact() { 392 assertTrue(isManagedProfile()); 393 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 394 PRIMARY_CONTACT_PHONE); 395 assertNull(contactInfo); 396 } 397 398 public void testManagedProfileEnterpriseEmailLookup_canNotAccessPrimaryContact() { 399 assertTrue(isManagedProfile()); 400 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 401 PRIMARY_CONTACT_EMAIL); 402 assertNull(contactInfo); 403 } 404 405 public void testManagedProfilePhoneLookup_canNotAccessPrimaryContact() { 406 assertTrue(isManagedProfile()); 407 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(false /*isEnterprise*/, 408 PRIMARY_CONTACT_PHONE); 409 assertNull(contactInfo); 410 } 411 412 public void testManagedProfileEmailLookup_canNotAccessPrimaryContact() { 413 assertTrue(isManagedProfile()); 414 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(false /*isEnterprise */, 415 PRIMARY_CONTACT_EMAIL); 416 assertNull(contactInfo); 417 } 418 419 public void testPrimaryProfileEnterpriseEmailLookup_canNotAccessEnterpriseContact() { 420 assertFalse(isManagedProfile()); 421 ContactInfo contactInfo = getContactInfoFromEmailLookupUri(true /*isEnterprise*/, 422 MANAGED_CONTACT_EMAIL); 423 assertNull(contactInfo); 424 } 425 426 public void testPrimaryProfileEnterprisePhoneLookup_canNotAccessEnterpriseContact() { 427 assertFalse(isManagedProfile()); 428 ContactInfo contactInfo = getContactInfoFromPhoneLookupUri(true /*isEnterprise*/, 429 MANAGED_CONTACT_PHONE); 430 assertNull(contactInfo); 431 } 432 433 public void testPrimaryProfileEnterprisePhoneLookup_canNotAccessManagedDirectories() { 434 assertFalse(isManagedProfile()); 435 436 // local directory 437 final ContactInfo defaultContactInfo = 438 getContactInfoFromEnterprisePhoneLookupUriInDirectory(MANAGED_CONTACT_PHONE, 439 Directory.ENTERPRISE_DEFAULT); 440 assertNull(defaultContactInfo); 441 442 // remote directory 443 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 444 if (directoryId != 0) { // if directoryId == 0, it means it can't access managed directory 445 final ContactInfo directoryContactInfo = 446 getContactInfoFromEnterprisePhoneLookupUriInDirectory(MANAGED_CONTACT_PHONE, 447 directoryId); 448 assertNull(directoryContactInfo); 449 } 450 } 451 452 public void testPrimaryProfileEnterpriseEmailLookup_canNotAccessManagedDirectories() { 453 assertFalse(isManagedProfile()); 454 455 // local directory 456 final ContactInfo defaultContactInfo = 457 getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL, 458 Directory.ENTERPRISE_DEFAULT); 459 assertNull(defaultContactInfo); 460 461 // remote directory 462 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 463 if (directoryId != 0) { // if directoryId == 0, it means it can't access managed directory 464 final ContactInfo directoryContactInfo = 465 getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL, 466 directoryId); 467 assertNull(directoryContactInfo); 468 } 469 } 470 471 public void testSetCrossProfileCallerIdDisabled_true() { 472 assertTrue(isManagedProfile()); 473 mDevicePolicyManager.setCrossProfileCallerIdDisabled( 474 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, true); 475 } 476 477 public void testSetCrossProfileCallerIdDisabled_false() { 478 assertTrue(isManagedProfile()); 479 mDevicePolicyManager.setCrossProfileCallerIdDisabled( 480 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, false); 481 } 482 483 public void testSetCrossProfileContactsSearchDisabled_true() { 484 assertTrue(isManagedProfile()); 485 mDevicePolicyManager.setCrossProfileContactsSearchDisabled( 486 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, true); 487 assertTrue(mDevicePolicyManager.getCrossProfileContactsSearchDisabled( 488 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT)); 489 } 490 491 public void testSetCrossProfileContactsSearchDisabled_false() { 492 assertTrue(isManagedProfile()); 493 mDevicePolicyManager.setCrossProfileContactsSearchDisabled( 494 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, false); 495 assertFalse(mDevicePolicyManager.getCrossProfileContactsSearchDisabled( 496 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT)); 497 } 498 499 public void testCurrentProfileContacts_removeContacts() { 500 removeAllTestContactsInProfile(); 501 } 502 503 public void testSetBluetoothContactSharingDisabled_setterAndGetter() { 504 mDevicePolicyManager.setBluetoothContactSharingDisabled( 505 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, false); 506 assertFalse(mDevicePolicyManager.getBluetoothContactSharingDisabled( 507 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT)); 508 mDevicePolicyManager.setBluetoothContactSharingDisabled( 509 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT, true); 510 assertTrue(mDevicePolicyManager.getBluetoothContactSharingDisabled( 511 BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT)); 512 } 513 514 public void testGetDirectoryListInPrimaryProfile() { 515 assertFalse(isManagedProfile()); 516 // As directory content in CP2 may not be updated, we will try 10 times to see if it's 517 // updated 518 for (int i = 0; i < MAX_RETRY_DIRECTORY_QUERY; i++) { 519 final Cursor cursor = mResolver.query(Directory.ENTERPRISE_CONTENT_URI, 520 new String[]{ 521 Directory._ID, 522 Directory.DISPLAY_NAME 523 }, null, null, null); 524 525 boolean hasPrimaryDefault = false; 526 boolean hasPrimaryInvisible = false; 527 boolean hasManagedDefault = false; 528 boolean hasManagedInvisible = false; 529 boolean hasPrimaryDirectory = false; 530 boolean hasManagedDirectory = false; 531 532 try { 533 while(cursor.moveToNext()) { 534 final long directoryId = cursor.getLong(0); 535 if (directoryId == Directory.DEFAULT) { 536 hasPrimaryDefault = true; 537 } else if (directoryId == Directory.LOCAL_INVISIBLE) { 538 hasPrimaryInvisible = true; 539 } else if (directoryId == Directory.ENTERPRISE_DEFAULT) { 540 hasManagedDefault = true; 541 } else if (directoryId == Directory.ENTERPRISE_LOCAL_INVISIBLE) { 542 hasManagedInvisible = true; 543 } else { 544 final String displayName = cursor.getString(1); 545 if (Directory.isEnterpriseDirectoryId(directoryId) 546 && displayName.equals(MANAGED_DIRECTORY_NAME)) { 547 hasManagedDirectory = true; 548 } 549 if (!Directory.isEnterpriseDirectoryId(directoryId) 550 && displayName.equals(PRIMARY_DIRECTORY_NAME)) { 551 hasPrimaryDirectory = true; 552 } 553 } 554 } 555 if (i + 1 == MAX_RETRY_DIRECTORY_QUERY) { 556 DatabaseUtils.dumpCursor(cursor); 557 assertTrue(hasPrimaryDefault); 558 assertTrue(hasPrimaryInvisible); 559 assertTrue(hasManagedDefault); 560 assertTrue(hasManagedInvisible); 561 assertTrue(hasPrimaryDirectory); 562 assertTrue(hasManagedDirectory); 563 } 564 } finally { 565 cursor.close(); 566 } 567 if (hasPrimaryDefault && hasPrimaryInvisible && hasManagedDefault 568 && hasManagedInvisible && hasPrimaryDirectory && hasManagedDirectory) { 569 // Success 570 return; 571 } else { 572 // Failed, sleep and retry 573 try { 574 Log.i(TAG, "Failed " + (i+1) + " times, retry"); 575 Thread.sleep(RETRY_DIRECTORY_QUERY_INTERVAL); 576 } catch (InterruptedException e) {} 577 } 578 } 579 } 580 581 public void testPrimaryProfileEnterpriseEmailLookup_canAccessPrimaryDirectories() { 582 assertFalse(isManagedProfile()); 583 584 // local directory 585 final ContactInfo defaultContactInfo 586 = getContactInfoFromEnterpriseEmailLookupUriInDirectory(PRIMARY_CONTACT_EMAIL, 587 Directory.DEFAULT); 588 assertPrimaryLocalContact(defaultContactInfo); 589 590 // remote directory 591 final long directoryId = getPrimaryRemoteDirectoryId(); 592 final ContactInfo directoryContactInfo 593 = getContactInfoFromEnterpriseEmailLookupUriInDirectory(PRIMARY_CONTACT_EMAIL, 594 directoryId); 595 assertPrimaryDirectoryContact(directoryContactInfo); 596 } 597 598 public void testPrimaryProfileEnterpriseEmailLookup_canAccessManagedDirectories() { 599 assertFalse(isManagedProfile()); 600 601 // local directory 602 final ContactInfo defaultContactInfo 603 = getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL, 604 Directory.ENTERPRISE_DEFAULT); 605 assertManagedLocalContact(defaultContactInfo); 606 607 // remote directory 608 final long directoryId = getEnterpriseRemoteDirectoryId(); 609 final ContactInfo directoryContactInfo 610 = getContactInfoFromEnterpriseEmailLookupUriInDirectory(MANAGED_CONTACT_EMAIL, 611 directoryId); 612 assertManagedDirectoryContact(directoryContactInfo); 613 } 614 615 public void testPrimaryProfileEnterprisePhoneLookup_canAccessPrimaryDirectories() { 616 assertFalse(isManagedProfile()); 617 618 // local directory 619 final ContactInfo defaultContactInfo 620 = getContactInfoFromEnterprisePhoneLookupUriInDirectory(PRIMARY_CONTACT_PHONE, 621 Directory.DEFAULT); 622 assertPrimaryLocalContact(defaultContactInfo); 623 624 // remote directory 625 final long directoryId = getPrimaryRemoteDirectoryId(); 626 final ContactInfo directoryContactInfo 627 = getContactInfoFromEnterprisePhoneLookupUriInDirectory(PRIMARY_CONTACT_PHONE, 628 directoryId); 629 assertPrimaryDirectoryContact(directoryContactInfo); 630 } 631 632 public void testPrimaryProfileEnterprisePhoneLookup_canAccessManagedDirectories() { 633 assertFalse(isManagedProfile()); 634 635 // local directory 636 final ContactInfo defaultContactInfo 637 = getContactInfoFromEnterprisePhoneLookupUriInDirectory(MANAGED_CONTACT_PHONE, 638 Directory.ENTERPRISE_DEFAULT); 639 assertManagedLocalContact(defaultContactInfo); 640 641 // remote directory 642 final long directoryId = getEnterpriseRemoteDirectoryId(); 643 final ContactInfo directoryContactInfo 644 = getContactInfoFromEnterprisePhoneLookupUriInDirectory(MANAGED_CONTACT_PHONE, 645 directoryId); 646 assertManagedDirectoryContact(directoryContactInfo); 647 } 648 649 public void testPrimaryProfileEnterpriseCallableFilter_canAccessPrimaryDirectories() { 650 assertFalse(isManagedProfile()); 651 final ContactInfo defaultContactInfo 652 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 653 PRIMARY_CONTACT_PHONE, Directory.DEFAULT); 654 assertPrimaryLocalContact(defaultContactInfo); 655 656 final ContactInfo directoryContactInfo 657 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 658 PRIMARY_CONTACT_PHONE, getPrimaryRemoteDirectoryId()); 659 assertPrimaryDirectoryContact(directoryContactInfo); 660 661 } 662 663 public void testManagedProfileEnterpriseCallableFilter_canAccessManagedDirectories() { 664 assertTrue(isManagedProfile()); 665 final ContactInfo defaultContactInfo 666 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 667 MANAGED_CONTACT_PHONE, Directory.DEFAULT); 668 assertManagedLocalContact(defaultContactInfo); 669 670 final ContactInfo directoryContactInfo 671 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 672 MANAGED_CONTACT_PHONE, getEnterpriseRemoteDirectoryIdInManagedProfile()); 673 assertManagedDirectoryContact(directoryContactInfo); 674 } 675 676 public void testPrimaryProfileEnterpriseCallableFilter_canAccessManagedDirectories() { 677 assertFalse(isManagedProfile()); 678 679 // local directory 680 final ContactInfo defaultContactInfo 681 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 682 MANAGED_CONTACT_PHONE, Directory.ENTERPRISE_DEFAULT); 683 assertManagedLocalContact(defaultContactInfo); 684 685 // remote directory 686 final long directoryId = getEnterpriseRemoteDirectoryId(); 687 final ContactInfo directoryContactInfo 688 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 689 MANAGED_CONTACT_PHONE, directoryId); 690 assertManagedDirectoryContact(directoryContactInfo); 691 } 692 693 public void testPrimaryProfileEnterpriseCallableFilter_canNotAccessManagedDirectories() { 694 assertFalse(isManagedProfile()); 695 696 // local directory 697 final ContactInfo defaultContactInfo 698 = getContactInfoFromEnterpriseCallableFilterUriInDirectory( 699 MANAGED_CONTACT_PHONE, Directory.ENTERPRISE_DEFAULT); 700 assertNull(defaultContactInfo); 701 702 // remote directory 703 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 704 if (directoryId == 0L) { 705 // if no enterprise directory id is found, the test succeeds. 706 return; 707 } else { 708 final ContactInfo directoryContactInfo = 709 getContactInfoFromEnterpriseCallableFilterUriInDirectory(MANAGED_CONTACT_PHONE, 710 directoryId); 711 assertNull(directoryContactInfo); 712 } 713 } 714 715 public void testPrimaryProfileEnterpriseEmailFilter_canAccessPrimaryDirectories() { 716 assertFalse(isManagedProfile()); 717 final ContactInfo defaultContactInfo 718 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 719 PRIMARY_CONTACT_EMAIL, Directory.DEFAULT); 720 assertPrimaryLocalContact(defaultContactInfo); 721 722 final ContactInfo directoryContactInfo 723 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 724 PRIMARY_CONTACT_EMAIL, getPrimaryRemoteDirectoryId()); 725 assertPrimaryDirectoryContact(directoryContactInfo); 726 } 727 728 public void testEnterpriseProfileEnterpriseEmailFilter_canAccessManagedDirectories() { 729 assertTrue(isManagedProfile()); 730 final ContactInfo defaultContactInfo 731 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 732 MANAGED_CONTACT_EMAIL, Directory.DEFAULT); 733 assertManagedLocalContact(defaultContactInfo); 734 735 final ContactInfo directoryContactInfo 736 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 737 MANAGED_CONTACT_EMAIL, getEnterpriseRemoteDirectoryIdInManagedProfile()); 738 assertManagedDirectoryContact(directoryContactInfo); 739 } 740 741 public void testPrimaryProfileEnterpriseEmailFilter_canAccessManagedDirectories() { 742 assertFalse(isManagedProfile()); 743 744 // local directory 745 final ContactInfo defaultContactInfo 746 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 747 MANAGED_CONTACT_EMAIL, Directory.ENTERPRISE_DEFAULT); 748 assertManagedLocalContact(defaultContactInfo); 749 750 // remote directory 751 final long directoryId = getEnterpriseRemoteDirectoryId(); 752 final ContactInfo directoryContactInfo 753 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 754 MANAGED_CONTACT_EMAIL, directoryId); 755 assertManagedDirectoryContact(directoryContactInfo); 756 } 757 758 public void testPrimaryProfileEnterpriseEmailFilter_canNotAccessManagedDirectories() { 759 assertFalse(isManagedProfile()); 760 761 // local directory 762 final ContactInfo defaultContactInfo = 763 getContactInfoFromEnterpriseEmailFilterUriInDirectory(MANAGED_CONTACT_EMAIL, 764 Directory.ENTERPRISE_DEFAULT); 765 assertNull(defaultContactInfo); 766 767 // remote directory 768 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 769 if (directoryId == 0L) { 770 // if no enterprise directory id is found, the test succeeds. 771 return; 772 } else { 773 final ContactInfo directoryContactInfo = 774 getContactInfoFromEnterpriseEmailFilterUriInDirectory(MANAGED_CONTACT_EMAIL, 775 directoryId); 776 assertNull(directoryContactInfo); 777 } 778 } 779 780 public void testPrimaryProfileEnterpriseContactFilter_canAccessPrimaryDirectories() { 781 assertFalse(isManagedProfile()); 782 final ContactInfo defaultContactInfo 783 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 784 PRIMARY_CONTACT_DISPLAY_NAME, Directory.DEFAULT); 785 assertPrimaryLocalContact(defaultContactInfo); 786 787 final ContactInfo directoryContactInfo 788 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 789 PRIMARY_DIRECTORY_CONTACT_NAME, getPrimaryRemoteDirectoryId()); 790 assertPrimaryDirectoryContact(directoryContactInfo); 791 } 792 793 public void testManagedProfileEnterpriseContactFilter_canAccessManagedDirectories() { 794 assertTrue(isManagedProfile()); 795 final ContactInfo defaultContactInfo 796 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 797 MANAGED_CONTACT_DISPLAY_NAME, Directory.DEFAULT); 798 assertManagedLocalContact(defaultContactInfo); 799 800 final ContactInfo directoryContactInfo 801 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 802 MANAGED_DIRECTORY_CONTACT_NAME, getEnterpriseRemoteDirectoryIdInManagedProfile()); 803 assertManagedDirectoryContact(directoryContactInfo); 804 } 805 806 public void testPrimaryProfileEnterpriseContactFilter_canAccessManagedDirectories() { 807 assertFalse(isManagedProfile()); 808 809 // local directory 810 final ContactInfo defaultContactInfo 811 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 812 MANAGED_CONTACT_DISPLAY_NAME, Directory.ENTERPRISE_DEFAULT); 813 assertManagedLocalContact(defaultContactInfo); 814 815 // remote directory 816 final long directoryId = getEnterpriseRemoteDirectoryId(); 817 final ContactInfo directoryContactInfo 818 = getContactInfoFromEnterpriseEmailFilterUriInDirectory( 819 MANAGED_CONTACT_DISPLAY_NAME, directoryId); 820 assertManagedDirectoryContact(directoryContactInfo); 821 } 822 823 public void testPrimaryProfileEnterpriseContactFilter_canNotAccessManagedDirectories() { 824 assertFalse(isManagedProfile()); 825 826 // local directory 827 final ContactInfo defaultContactInfo 828 = getContactInfoFromEnterpriseContactFilterUriInDirectory( 829 MANAGED_CONTACT_DISPLAY_NAME, Directory.ENTERPRISE_DEFAULT); 830 assertNull(defaultContactInfo); 831 832 // remote directory 833 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 834 if (directoryId == 0L) { 835 // if no enterprise directory id is found, the test succeeds. 836 return; 837 } else { 838 final ContactInfo directoryContactInfo = 839 getContactInfoFromEnterpriseEmailFilterUriInDirectory( 840 MANAGED_CONTACT_DISPLAY_NAME, directoryId); 841 assertNull(directoryContactInfo); 842 } 843 } 844 845 public void testPrimaryProfileEnterprisePhoneFilter_canAccessPrimaryDirectories() { 846 assertFalse(isManagedProfile()); 847 final ContactInfo defaultContactInfo 848 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 849 PRIMARY_CONTACT_PHONE, Directory.DEFAULT); 850 assertPrimaryLocalContact(defaultContactInfo); 851 852 final ContactInfo directoryContactInfo 853 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 854 PRIMARY_CONTACT_PHONE, getPrimaryRemoteDirectoryId()); 855 assertPrimaryDirectoryContact(directoryContactInfo); 856 } 857 858 public void testManagedProfileEnterprisePhoneFilter_canAccessManagedDirectories() { 859 assertTrue(isManagedProfile()); 860 final ContactInfo defaultContactInfo 861 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 862 MANAGED_CONTACT_PHONE, Directory.DEFAULT); 863 assertManagedLocalContact(defaultContactInfo); 864 865 final ContactInfo directoryContactInfo 866 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 867 MANAGED_CONTACT_PHONE, getEnterpriseRemoteDirectoryIdInManagedProfile()); 868 assertManagedDirectoryContact(directoryContactInfo); 869 } 870 871 public void testPrimaryProfileEnterprisePhoneFilter_canAccessManagedDirectories() { 872 assertFalse(isManagedProfile()); 873 874 // local directory 875 final ContactInfo defaultContactInfo 876 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 877 MANAGED_CONTACT_PHONE, Directory.ENTERPRISE_DEFAULT); 878 assertManagedLocalContact(defaultContactInfo); 879 880 // remote directory 881 final long directoryId = getEnterpriseRemoteDirectoryId(); 882 final ContactInfo directoryContactInfo 883 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 884 MANAGED_CONTACT_PHONE, directoryId); 885 assertManagedDirectoryContact(directoryContactInfo); 886 } 887 888 public void testPrimaryProfileEnterprisePhoneFilter_canNotAccessManagedDirectories() { 889 assertFalse(isManagedProfile()); 890 891 // local directory 892 final ContactInfo defaultContactInfo 893 = getContactInfoFromEnterprisePhoneFilterUriInDirectory( 894 MANAGED_CONTACT_PHONE, Directory.ENTERPRISE_DEFAULT); 895 assertNull(defaultContactInfo); 896 897 // remote directory 898 final long directoryId = getEnterpriseRemoteDirectoryIdSliently(); 899 if (directoryId == 0L) { 900 // if no enterprise directory id is found, the test succeeds. 901 return; 902 } else { 903 final ContactInfo directoryContactInfo = 904 getContactInfoFromEnterprisePhoneFilterUriInDirectory( 905 MANAGED_CONTACT_PHONE, directoryId); 906 assertNull(directoryContactInfo); 907 } 908 } 909 910 public void testPrimaryProfileEnterpriseDirectories_canNotAccessManagedDirectories() { 911 assertFalse(isManagedProfile()); 912 913 final Cursor cursor = mResolver.query(Directory.ENTERPRISE_CONTENT_URI, 914 new String[]{Directory._ID}, null, null, null); 915 try { 916 while (cursor.moveToNext()) { 917 final long directoryId = cursor.getLong(0); 918 if (Directory.isEnterpriseDirectoryId(directoryId)) { 919 fail("found enterprise directories"); 920 } 921 } 922 } finally { 923 cursor.close(); 924 } 925 } 926 927 928 public void testFilterUriWhenDirectoryParamMissing() { 929 assertFailWhenDirectoryParamMissing(Phone.ENTERPRISE_CONTENT_FILTER_URI); 930 assertFailWhenDirectoryParamMissing(Email.ENTERPRISE_CONTENT_FILTER_URI); 931 assertFailWhenDirectoryParamMissing(Contacts.ENTERPRISE_CONTENT_FILTER_URI); 932 assertFailWhenDirectoryParamMissing(Callable.ENTERPRISE_CONTENT_FILTER_URI); 933 } 934 935 public void testQuickContact() throws Exception { 936 showQuickContactInternal(null); 937 showQuickContactInternal(Directory.ENTERPRISE_DEFAULT); 938 showQuickContactInternal(getEnterpriseRemoteDirectoryId()); 939 } 940 941 private void showQuickContactInternal(Long directoryId) throws Exception { 942 final Uri phoneLookupUri = 943 Uri.withAppendedPath( 944 PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, MANAGED_CONTACT_PHONE); 945 if (directoryId != null) { 946 phoneLookupUri.buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 947 String.valueOf(directoryId)).build(); 948 } 949 final Cursor cursor = 950 getContext().getContentResolver().query(phoneLookupUri, null, null, null, null); 951 try { 952 assertTrue(cursor.moveToFirst()); 953 final long contactId = 954 cursor.getLong(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); 955 final String lookupKey = 956 cursor.getString( 957 cursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY)); 958 final Uri lookupUri = Contacts.getLookupUri(contactId, lookupKey); 959 // TODO: It is better to verify the uri received by quick contacts, but it is difficult 960 // to verify it as the quick contacts in managed profile is started. We now just make 961 // sure no exception is thrown due to invalid uri (eg: directory id is missing). 962 // Also, consider using UiAutomator to verify the activtiy is started. 963 ContactsContract.QuickContact.showQuickContact(getContext(), (Rect) null, lookupUri, 964 ContactsContract.QuickContact.MODE_LARGE, null); 965 } finally { 966 if (cursor != null) { 967 cursor.close(); 968 } 969 } 970 } 971 972 private long getPrimaryRemoteDirectoryId() { 973 assertFalse(isManagedProfile()); 974 return getRemoteDirectoryIdInternal(); 975 } 976 977 private long getEnterpriseRemoteDirectoryIdInManagedProfile() { 978 assertTrue(isManagedProfile()); 979 return getRemoteDirectoryIdInternal(); 980 } 981 982 private long getRemoteDirectoryIdInternal() { 983 final Cursor cursor = mResolver.query(Directory.ENTERPRISE_CONTENT_URI, 984 new String[]{ 985 Directory._ID, Directory.DIRECTORY_AUTHORITY 986 }, null, null, null); 987 try { 988 while (cursor.moveToNext()) { 989 final long directoryId = cursor.getLong(0); 990 final String directoryAuthority = cursor.getString(1); 991 if (!Directory.isEnterpriseDirectoryId(directoryId) 992 && Directory.isRemoteDirectoryId(directoryId) 993 && DIRECTORY_PROVIDER_AUTHORITY.equals(directoryAuthority)) { 994 return directoryId; 995 } 996 } 997 } finally { 998 cursor.close(); 999 } 1000 fail("Cannot find primary directory id"); 1001 return 0; 1002 } 1003 1004 private long getEnterpriseRemoteDirectoryId() { 1005 final long enterpriseDirectoryId = getEnterpriseRemoteDirectoryIdSliently(); 1006 assertNotSame("Cannot find enterprise directory id", 0L, enterpriseDirectoryId); 1007 return enterpriseDirectoryId; 1008 } 1009 1010 private long getEnterpriseRemoteDirectoryIdSliently() { 1011 assertFalse(isManagedProfile()); 1012 final Cursor cursor = mResolver.query(Directory.ENTERPRISE_CONTENT_URI, 1013 new String[] { 1014 Directory._ID, Directory.DIRECTORY_AUTHORITY 1015 }, null, null, null); 1016 try { 1017 while (cursor.moveToNext()) { 1018 final long directoryId = cursor.getLong(0); 1019 final String directoryAuthority = cursor.getString(1); 1020 if (Directory.isEnterpriseDirectoryId(directoryId) 1021 && Directory.isRemoteDirectoryId(directoryId) 1022 && DIRECTORY_PROVIDER_AUTHORITY.equals(directoryAuthority)) { 1023 return directoryId; 1024 } 1025 } 1026 } finally { 1027 cursor.close(); 1028 } 1029 return 0; 1030 } 1031 1032 private boolean isManagedProfile() { 1033 String adminPackage = BaseManagedProfileTest.ADMIN_RECEIVER_COMPONENT.getPackageName(); 1034 return mDevicePolicyManager.isProfileOwnerApp(adminPackage); 1035 } 1036 1037 private void insertContact(String displayName, String phoneNumber, String email, 1038 String sipAddress, int photoResId) 1039 throws RemoteException, OperationApplicationException, NotFoundException, IOException { 1040 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 1041 ops.add(ContentProviderOperation 1042 .newInsert(ContactsContract.RawContacts.CONTENT_URI) 1043 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, TEST_ACCOUNT_TYPE) 1044 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, TEST_ACCOUNT_NAME) 1045 .build()); 1046 ops.add(ContentProviderOperation 1047 .newInsert(ContactsContract.Data.CONTENT_URI) 1048 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 1049 .withValue( 1050 ContactsContract.Data.MIMETYPE, 1051 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 1052 .withValue( 1053 ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 1054 displayName) 1055 .build()); 1056 ops.add(ContentProviderOperation 1057 .newInsert(ContactsContract.Data.CONTENT_URI) 1058 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 1059 .withValue( 1060 ContactsContract.Data.MIMETYPE, 1061 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 1062 .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 1063 phoneNumber) 1064 .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1065 Phone.TYPE_MOBILE) 1066 .build()); 1067 ops.add(ContentProviderOperation 1068 .newInsert(ContactsContract.Data.CONTENT_URI) 1069 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 1070 .withValue( 1071 ContactsContract.Data.MIMETYPE, 1072 ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 1073 .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, 1074 email) 1075 .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 1076 Email.TYPE_WORK) 1077 .build()); 1078 ops.add(ContentProviderOperation 1079 .newInsert(ContactsContract.Data.CONTENT_URI) 1080 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 1081 .withValue( 1082 ContactsContract.Data.MIMETYPE, 1083 ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE) 1084 .withValue(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS, 1085 sipAddress) 1086 .withValue(ContactsContract.CommonDataKinds.SipAddress.TYPE, 1087 ContactsContract.CommonDataKinds.SipAddress.TYPE_WORK) 1088 .build()); 1089 1090 if (photoResId != 0) { 1091 InputStream phoneInputStream = mContext.getResources().openRawResource(photoResId); 1092 try { 1093 byte[] rawPhoto = getByteFromStream(phoneInputStream); 1094 ops.add(ContentProviderOperation 1095 .newInsert(ContactsContract.Data.CONTENT_URI) 1096 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 1097 .withValue( 1098 ContactsContract.Data.MIMETYPE, 1099 ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) 1100 .withValue(Photo.PHOTO, rawPhoto) 1101 .build()); 1102 } finally { 1103 phoneInputStream.close(); 1104 } 1105 } 1106 1107 mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 1108 } 1109 1110 private void assertPrimaryLocalContact(ContactInfo contactInfo) { 1111 assertNotNull(contactInfo); 1112 assertEquals(PRIMARY_CONTACT_DISPLAY_NAME, contactInfo.displayName); 1113 contactInfo.assertNoPhotoUri(); 1114 assertFalse(contactInfo.hasPhotoId()); 1115 } 1116 1117 private void assertManagedLocalContact(ContactInfo contactInfo) { 1118 assertNotNull(contactInfo); 1119 assertEquals(MANAGED_CONTACT_DISPLAY_NAME, contactInfo.displayName); 1120 contactInfo.assertPhotoUrisReadable(); 1121 } 1122 1123 private void assertPrimaryDirectoryContact(ContactInfo contactInfo) { 1124 assertNotNull(contactInfo); 1125 assertEquals(PRIMARY_DIRECTORY_CONTACT_NAME, contactInfo.displayName); 1126 contactInfo.assertThumbnailUri(R.raw.primary_thumbnail); 1127 contactInfo.assertPhotoUri(R.raw.primary_photo); 1128 } 1129 1130 private void assertManagedDirectoryContact(ContactInfo contactInfo) { 1131 assertNotNull(contactInfo); 1132 assertEquals(MANAGED_DIRECTORY_CONTACT_NAME, contactInfo.displayName); 1133 contactInfo.assertThumbnailUri(R.raw.managed_thumbnail); 1134 contactInfo.assertPhotoUri(R.raw.managed_photo); 1135 } 1136 1137 private void assertContactInfoEquals(ContactInfo lhs, ContactInfo rhs) { 1138 if (lhs == null) { 1139 assertNull(rhs); 1140 } else { 1141 assertNotNull(rhs); 1142 assertEquals(lhs.contactId, rhs.contactId); 1143 assertEquals(lhs.displayName, rhs.displayName); 1144 assertEquals(lhs.photoId, rhs.photoId); 1145 assertEquals(lhs.photoThumbnailUri, rhs.photoThumbnailUri); 1146 assertEquals(lhs.photoUri, rhs.photoUri); 1147 } 1148 } 1149 1150 private ContactInfo getContactInfoFromPhoneLookupUri(boolean isEnterprise, String phoneNumber) { 1151 Uri baseUri = (isEnterprise) ? PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI 1152 : PhoneLookup.CONTENT_FILTER_URI; 1153 Uri uri = baseUri.buildUpon().appendPath(phoneNumber).build(); 1154 ContactInfo contactInfo = getContactInfoFromUri(uri, PhoneLookup._ID, 1155 PhoneLookup.DISPLAY_NAME, 1156 PhoneLookup.PHOTO_URI, PhoneLookup.PHOTO_THUMBNAIL_URI, PhoneLookup.PHOTO_ID); 1157 1158 ContactInfo contactInfo2 = getContactInfoFromUri(uri, PhoneLookup.CONTACT_ID, 1159 PhoneLookup.DISPLAY_NAME, 1160 PhoneLookup.PHOTO_URI, PhoneLookup.PHOTO_THUMBNAIL_URI, PhoneLookup.PHOTO_ID); 1161 assertContactInfoEquals(contactInfo, contactInfo2); 1162 return contactInfo; 1163 } 1164 1165 private ContactInfo getContactInfoFromEnterprisePhoneLookupUriWithSipAddress( 1166 boolean isEnterprise, String sipAddress) { 1167 Uri baseUri = (isEnterprise) ? PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI 1168 : PhoneLookup.CONTENT_FILTER_URI; 1169 Uri uri = baseUri.buildUpon().appendPath(sipAddress) 1170 .appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, "1").build(); 1171 return getContactInfoFromUri(uri, PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME, 1172 PhoneLookup.PHOTO_URI, PhoneLookup.PHOTO_THUMBNAIL_URI, PhoneLookup.PHOTO_ID); 1173 } 1174 1175 private ContactInfo getContactInfoFromEnterprisePhoneLookupUriInDirectory(String phoneNumber, 1176 long directoryId) { 1177 Uri uri = PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon().appendPath(phoneNumber) 1178 .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1179 String.valueOf(directoryId)).build(); 1180 return getContactInfoFromUri(uri, PhoneLookup._ID, PhoneLookup.DISPLAY_NAME, 1181 PhoneLookup.PHOTO_URI, PhoneLookup.PHOTO_THUMBNAIL_URI, PhoneLookup.PHOTO_ID); 1182 } 1183 1184 private ContactInfo getContactInfoFromEmailLookupUri(boolean isEnterprise, String email) { 1185 Uri baseUri = (isEnterprise) ? Email.ENTERPRISE_CONTENT_LOOKUP_URI 1186 : Email.CONTENT_LOOKUP_URI; 1187 Uri uri = Uri.withAppendedPath(baseUri, email); 1188 return getContactInfoFromUri(uri, Email.CONTACT_ID, Email.DISPLAY_NAME_PRIMARY, 1189 Email.PHOTO_URI, Email.PHOTO_THUMBNAIL_URI, Email.PHOTO_ID); 1190 } 1191 1192 private ContactInfo getContactInfoFromEnterpriseEmailLookupUriInDirectory(String email, 1193 long directoryId) { 1194 Uri uri = Email.ENTERPRISE_CONTENT_LOOKUP_URI.buildUpon().appendPath(email) 1195 .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1196 String.valueOf(directoryId)).build(); 1197 return getContactInfoFromUri(uri, Email.CONTACT_ID, Email.DISPLAY_NAME_PRIMARY, 1198 Email.PHOTO_URI, Email.PHOTO_THUMBNAIL_URI, Email.PHOTO_ID); 1199 } 1200 1201 private ContactInfo getContactInfoFromEnterpriseCallableFilterUriInDirectory(String filter, 1202 long directoryId) { 1203 final Uri uri = Uri.withAppendedPath(Callable.ENTERPRISE_CONTENT_FILTER_URI, filter) 1204 .buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1205 String.valueOf(directoryId)).build(); 1206 return getContactInfoFromUri(uri, Callable.CONTACT_ID, Callable.DISPLAY_NAME_PRIMARY, 1207 Callable.PHOTO_URI, Callable.PHOTO_THUMBNAIL_URI, Callable.PHOTO_ID); 1208 } 1209 1210 private ContactInfo getContactInfoFromEnterpriseEmailFilterUriInDirectory(String filter, 1211 long directoryId) { 1212 final Uri uri = Uri.withAppendedPath(Email.ENTERPRISE_CONTENT_FILTER_URI, filter) 1213 .buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1214 String.valueOf(directoryId)).build(); 1215 return getContactInfoFromUri(uri, Email.CONTACT_ID, Email.DISPLAY_NAME_PRIMARY, 1216 Email.PHOTO_URI, Email.PHOTO_THUMBNAIL_URI, Email.PHOTO_ID); 1217 } 1218 1219 private ContactInfo getContactInfoFromEnterpriseContactFilterUriInDirectory(String filter, 1220 long directoryId) { 1221 final Uri uri = Uri.withAppendedPath(Contacts.ENTERPRISE_CONTENT_FILTER_URI, filter) 1222 .buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1223 String.valueOf(directoryId)).build(); 1224 return getContactInfoFromUri(uri, Contacts._ID, Contacts.DISPLAY_NAME_PRIMARY, 1225 Contacts.PHOTO_URI, Contacts.PHOTO_THUMBNAIL_URI, Contacts.PHOTO_ID); 1226 } 1227 1228 private ContactInfo getContactInfoFromEnterprisePhoneFilterUriInDirectory(String filter, 1229 long directoryId) { 1230 final Uri uri = Uri.withAppendedPath(Phone.ENTERPRISE_CONTENT_FILTER_URI, filter) 1231 .buildUpon().appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 1232 String.valueOf(directoryId)).build(); 1233 return getContactInfoFromUri(uri, Phone.CONTACT_ID, Phone.DISPLAY_NAME_PRIMARY, 1234 Phone.PHOTO_URI, Phone.PHOTO_THUMBNAIL_URI, Phone.PHOTO_ID); 1235 } 1236 1237 1238 private ContactInfo getContactInfoFromUri(Uri uri, String idColumn, 1239 String displayNameColumn, String photoUriColumn, String photoThumbnailColumn, 1240 String photoIdColumn) { 1241 Cursor cursor = mResolver.query(uri, 1242 new String[] { 1243 idColumn, 1244 displayNameColumn, 1245 photoUriColumn, 1246 photoIdColumn, 1247 photoThumbnailColumn, 1248 }, null, null, null); 1249 if (cursor == null) { 1250 return null; 1251 } 1252 try { 1253 if (cursor.moveToFirst()) { 1254 return new ContactInfo( 1255 cursor.getString(cursor.getColumnIndexOrThrow(idColumn)), 1256 cursor.getString(cursor.getColumnIndexOrThrow(displayNameColumn)), 1257 cursor.getString(cursor.getColumnIndexOrThrow(photoUriColumn)), 1258 cursor.getString(cursor.getColumnIndexOrThrow(photoThumbnailColumn)), 1259 cursor.getString(cursor.getColumnIndexOrThrow(photoIdColumn))); 1260 } 1261 } finally { 1262 cursor.close(); 1263 } 1264 return null; 1265 } 1266 1267 private void removeAllTestContactsInProfile() { 1268 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 1269 ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI) 1270 .withSelection(RawContacts.ACCOUNT_TYPE + "=?", new String[] {TEST_ACCOUNT_TYPE}) 1271 .build()); 1272 try { 1273 mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 1274 } catch (Exception e) { 1275 // Catch all exceptions to let tearDown() run smoothly 1276 e.printStackTrace(); 1277 } 1278 1279 Account account = new Account(TEST_ACCOUNT_NAME, TEST_ACCOUNT_TYPE); 1280 AccountManager.get(getContext()).removeAccountExplicitly(account); 1281 } 1282 1283 private InputStream getInputStreamFromUriForTest(String uriString) { 1284 try { 1285 return mResolver.openInputStream(Uri.parse(uriString)); 1286 } catch (FileNotFoundException e) { 1287 fail(e.getMessage()); 1288 throw new RuntimeException(e); 1289 } 1290 } 1291 1292 private static byte[] getByteFromStreamForTest(InputStream is) { 1293 assertNotNull(is); 1294 try (InputStream in = is) { 1295 return getByteFromStream(in); 1296 } catch (IOException e) { 1297 fail(e.getMessage()); 1298 throw new RuntimeException(e); 1299 } 1300 } 1301 1302 private static byte[] getByteFromStream(InputStream is) throws IOException { 1303 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 1304 byte[] buf = new byte[1024 * 10]; 1305 int i = 0; 1306 while ((i = is.read(buf, 0, buf.length)) > 0) { 1307 outputStream.write(buf, 0, i); 1308 } 1309 return outputStream.toByteArray(); 1310 } 1311 1312 private boolean isEnterpriseContactId(String contactId) { 1313 return ContactsContract.Contacts.isEnterpriseContactId(Long.valueOf(contactId)); 1314 } 1315 1316 private void assertPhotoUriReadable(String uri) { 1317 assertNotNull(uri); 1318 try (InputStream is = mResolver.openInputStream(Uri.parse(uri))) { 1319 // Make sure it's readabe. Don't have to read all content. 1320 is.read(); 1321 } catch (IOException e) { 1322 fail(e.getMessage()); 1323 throw new RuntimeException(e); 1324 } 1325 } 1326 1327 private void assertFailWhenDirectoryParamMissing(Uri uri) { 1328 try { 1329 mResolver.query(uri, null, null, null, null); 1330 fail("IllegalArgumentException is not thrown"); 1331 } catch (IllegalArgumentException ex) { 1332 } 1333 } 1334 } 1335