About Contacts

About Contacts

Contents

Introduction

Call getContacts to retrieve all contacts stored in a user’s Personal Cloud Storage account.

Examples

  1. Add Cloud SDK for Android to an existing or a new Android project.
  2. Initialize the CloudAPI class object with the application key, secret and the callback URL you obtained from the ThingSpace portal.
  3. Authorize the application by calling CloudAPI#authorize().
  4. Use the code sample below to create, list, update, and delete contacts stored in the user’s Personal Cloud Storage Account.

Create Contacts

try {
	Contact.Builder contactBuilder = new Contact.Builder();
	contactBuilder.setFirstName("First Name");
    contactBuilder.setMiddleName("Middle Name");
    contactBuilder.setLastName("Last Name");
    contactBuilder.setOrgName("Org Name");
    contactBuilder.setJobTitle("Job Title");
    
    Phone.Builder phoneBuilder = new Phone.Builder();
    phoneBuilder.setNumber("1234567890");
    phoneBuilder.setType("Mobile");
    phoneBuilder.setIndex(1);
    phoneBuilder.setPreference(1);
    contactBuilder.addPhone(phoneBuilder);
    
    Email.Builder emailBuilder = new Email.Builder();
    emailBuilder.setAddress("John@verizon.com");
	emailBuilder.setType("Business");
    emailBuilder.setIndex(1);
    emailBuilder.setPreference(1);
	contactBuilder.addEmail(emailBuilder);
	
	IM.Builder imBuilder = new IM.Builder();
    imBuilder.setAddress("imAddress");
    imBuilder.setType("skype");
    imBuilder.setPreference(1);
    contactBuilder.addIM(imBuilder);
    
    Address.Builder addressBuilder = new Address.Builder();
    addressBuilder.setIndex(1);
    addressBuilder.setApartment("apartment");
    addressBuilder.setStreet("street");
    addressBuilder.setCity("city");
    addressBuilder.setCity(city);
    addressBuilder.setState("state");
	addressBuilder.setCountry("country");
	addressBuilder.setZipcode("zipcode");
	addressBuilder.setPObox("POBox");
	addressBuilder.setPreference(1);
	addressBuilder.setType("home");
	contactBuilder.addAddress(addressBuilder);
    
    Contact newContact = cloudAPI.createContact(context, contactBuilder);
    
} catch (CloudAPIException cloudAPIException) {
    if (cloudAPIException.getCode() == CloudAPIException.ErrorCode.USER_UNAUTHORIZED) {
        Log.d("Create Contact", "User not authorized, please call CloudAPI#authorize()");
    }
}

Delete Contacts

try {
	// Retrieve the contact to delete
	ItemList<Contact> itemListContacts = cloudAPI.getContacts(context);
    Contact contact = itemListContacts.getItem(0);
    
    // Delete the contact
    boolean success = cloudAPI.deleteContact(context, contact.getId());
    
} catch (CloudAPIException cloudAPIException) {
    if (cloudAPIException.getCode() == CloudAPIException.ErrorCode.USER_UNAUTHORIZED) {
        Log.d("Delete Contact", "User not authorized, please call CloudAPI#authorize()");
    }
}

List All Contacts

Code Sample

try {
    ItemList<Contact> itemListContacts = cloudAPI.getContacts(context);
    for (int i=0; i<itemListContacts.getCount(); i++) {
        Contact contact = itemListContacts.getItem(i);
        Log.d("Contacts", "Name: " + contact.getFirstName());
    }
} catch (CloudAPIException cloudAPIException) {
    if (cloudAPIException.getCode() == CloudAPIException.ErrorCode.USER_UNAUTHORIZED) {
        Log.d("Get Contacts", "User not authorized, please call CloudAPI#authorize()");
    }
}

Search for a Contact

Code Sample

try {
	String query = "name:John"; // "tel:201*", "address.home.street:8 Spruce st" 
    ItemList<Contact> itemListContacts = cloudAPI.getContacts(context, query, SortType.FIRST_NAME.setOrder(SortType.SortOrder.ASC));
    for (int i=0; i<itemListContacts.getCount(); i++) {
        Contact contact = itemListContacts.getItem(i);
        Log.d("Contacts", "Name: " + contact.getFirstName());
    }
} catch (CloudAPIException cloudAPIException) {
    if (cloudAPIException.getCode() == CloudAPIException.ErrorCode.USER_UNAUTHORIZED) {
        Log.d("Get Contacts", "User not authorized, please call CloudAPI#authorize()");
    }
}

Update Contacts

try {
	// Retrieve the contact to update
	ItemList<Contact> itemListContacts = cloudAPI.getContacts(context);
    Contact contact = itemListContacts.getItem(0);
    
    // Initialize Contact Builder with pre-populated Contact object and modify the fields that need to be updated
	Contact.Builder contactBuilder = new Contact.Builder(contact);
    contactBuilder.setLastName("New Last Name");
    contactBuilder.setOrgName("New Org Name");
    contactBuilder.setJobTitle("New Job Title");
    
    // if there are multiple phone numbers then clearPhoneList() method can be called to 
    // delete all the numbers from the list followed by adding them back with the update.
    contactBuilder.clearPhoneList();
    
    ItemList<Phone> phoneItemList = contact.getPhoneList();
    for (int i = 0; i < phoneItemList.getCount(); i++) {
        Phone phone = phoneItemList.getItem(i);
        if (phone.getIndex() == 1 && "Mobile".equalsIgnoreCase(phone.getType())) {
			Phone.Builder phoneBuilder = new Phone.Builder(phone);
		    phoneBuilder.setNumber("9088098989");
		    contactBuilder.addPhone(phoneBuilder);		
        } else {
            contactBuilder.addPhone(phone);
        }
    }
    
    Contact updatedContact = cloudAPI.updateContact(context, contact.getId(), contactBuilder);
    
} catch (CloudAPIException cloudAPIException) {
    if (cloudAPIException.getCode() == CloudAPIException.ErrorCode.USER_UNAUTHORIZED) {
        Log.d("Update Contact", "User not authorized, please call CloudAPI#authorize()");
    }
}

Copyright © 2015-2017, Verizon and/or its Licensors. All rights reserved.