Avaya Client SDK

< Back to Package Overview

Displaying Local Contacts

Using the Avaya Client SDK, you can easily access and use the database of local address book contacts users managed through the contacts app.

As you become more comfortable working with the Avaya Client SDK, the benefits of using the contacts API's and object definitions discussed in this article, rather than using the platform API's directly, will become clearer. This will be especially evident when working with enterprise shared contacts and accessing enterprise directory providers like Microsoft Active Directory which are additional capabilities that are exposed by the Avaya Client SDK and discussed in future articles.

To display contacts from your device, you must complete the following activities.

  • Request User Permissions
  • Enable local contacts service
  • Implement the data retrieval watcher and contact event handlers
  • Request local contacts

Request User Permissions

You do not need to request user permissions to access the local contacts on the device. Client SDK will do it for you.

Enable local contacts service

You can enable local contacts service using the LocalContactConfiguration object and set it into the UserConfiguration object using LocalContactConfiguration property.

LocalContactConfiguration localContactConfig = new LocalContactConfiguration();
localContactConfig.Enabled = true;
config.LocalContactConfiguration = localContactConfig;

This configuration step should be done before the User object is created.

Implement the data retrieval watcher and contact event handlers

In order to monitor the progress of retrieving the contacts, your application needs to create a ContactRetrievalWatcher object and set event handlers. The key functions for this objects are as follows:

  • When the DataRetrievalDone method of the ContactRetrievalWatcher is called (ie, data retrieval process is finished), GetSnapshot() method of the ContactRetrievalWatcher return list which contains the local contacts.
  • Iterate over the snapshot array to get contacts and display these contacts on the user interface as appropriate.
  • Implement the DataRetrievalCollectionChanged of the ContactRetrievalWatcher to handle adding, updating and deleting local contacts on the device after the initial retrieval is completed.
  • Optionally register ContactChanged and PresenceChanged event handlers for each Contact object in the snapshot array to handle their updates individually.

class AppContactDelegate{
    private ContactRetrievalWatcher contactRetrievalWatcher = 
    new ContactRetrievalWatcher();

    List contacts = new List();


    AppContactDelegate()
    {
        contactRetrievalWatcher.DataRetrievalProgress +=
        ContactDataRetrievalProgress;
        contactRetrievalWatcher.DataRetrievalDone += 
        ContactDataRetrievalDone;
        contactRetrievalWatcher.DataRetrievalCollectionChanged += 
        ContactListChanged;
    }

    private void ContactDataRetrievalProgress
    (object sender, DataRetrievalEventArgs e)
    {
        // Add code to update application UI 
        // for contact retrieval indication in progress
    }

    private void ContactDataRetrievalDone(object sender, EventArgs e)
    {
        //Initialize your contact array from the ContactRetrievalWatcher object
        List contactList = ((ContactRetrievalWatcher)sender)
        .GetSnapshot();
        foreach (var contact in contactList)
        {
            //For example, add contact to the internal list
            contacts.Add(contact);

            // Add delegate in order to  handle contact updates
            contact.ContactChanged += ContactChanged;
        }
        // Add code to refresh the UI with the internal list
      }

    private void ContactListChanged(object sender, 
    DataCollectionChangedEventArgs e)
    {
            // Add code to update application UI 
            // for added, updated and deleted local contacts

            //For example
            DataCollectionChangeType changeType = e.ChangeType;
            switch (changeType)
            {
                case DataCollectionChangeType.ItemsAdded:
                    {
                        foreach (Contact contact in e.ChangedItems)
                        {
                            if (!contacts.Contains(contact))
                            {
                                contacts.Add(contact);
                            }
                        }
                    }
                case DataCollectionChangeType.ItemsDeleted:
                    {
                        foreach (Contact contact in e.ChangedItems)
                        {
                            contacts.Remove(contact);
                        }
                    }
            }
        }
    }

    private void ContactChanged(object sender, ContactChangedEventArgs e)
    {
        // Add code to update application UI for updated contact individually.
    }

    public void GetContacts(ContactService contactService, 
    ContactSourceType sourceType)
    {
        contactService.GetContacts(contactRetrievalWatcher, sourceType);
    }
}

Retrieving local contacts

You can use the ContactService object to access the contacts on your local device and instantiate the AppContactDelegate to handle the contact retrieval progress.

ContactService contactService = user.ContactService;

// Create an application contact handler (AppContactDelegate) object 
// that receives contacts
AppContactDelegate appContactDelegate = new AppContactDelegate();

// Start receiving of the contact list and handling its updates
appContactDelegate.GetContacts(contactService, ContactSourceType.Local);