Tuesday, 21 August 2012

Retrieving Contacts in a J2ME phone using PIM API withoud any exception


Retrieving Contacts in a J2ME phone using PIM API

Retrieving Contacts in J2ME
The mobile market in India is huge and most of the people currently have basic JAVA enabled handsets. The era of SmartPhones have just begun in India. So for a j2me application sometimes it’s required to access the contacts from the phone which user has saved. The applications can be like saving a user’s contact on a server. In one of the applications that we started development it was required to implement a module which provides user to  send messages from within the application to any of his contacts. So this is how we implemented it.


Challenges in retrieving Contacts
The contacts are saved in different format in different phones , and each user has a different way of saving it e.g: I would save the names in FIRST_NAME field and LAST_NAME field while others would save the complete name in some other field. So accessing this information was an issue that we came across. The example that is with the WTK PDAP DEMO did not helped as there were handsets like Nokia E72, Nokia 5233 which gave exception with that sample they have given. So we came up with the idea of accessing all the fields that a PIM API provides and then checking in which of the FIELDS of PIM API we can find the desired contacts for a handset.
This helped us to retrieve the contacts from many devices that we checked for.

Proposed Solution Overview:

First of all for retrieving contacts we need to get the contacts list that a phone supports . The new phones have aggregated the contacts and list then as “Contacts” while the old ones have two list as “SIM” and “PHONE”.

String []contactsLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
This will give us the list that we need to see if phone supports (SIM,PHONE) or the (Contacts) way of providing the contacts. We need to catch the SecurityException if the application is not signed as default settings for reading data may be set to (NOT ACCESS) in several phones.
To enable application to read user data i.e. Contacts ,the property can be set by pressing the left soft key on the application icon where the application is installed and ->Application Access Menu->Read User Data->Set this to allow or ask every time .

The user will be selecting a list i.e Contacts,Sim , Phone from which he wants to see the contact. SIM have a common format of storing the names but phone contacts are saved in several ways.


Reading Contacts From Selected List(i.e. SIM,Phone,Contacts)

To read a contacts there are several fields in which a contact can be saved. For name there is Contact.NAME and for telephone numbers there is Contact.TEL. Accessing these fields can throw FieldEmptyException , IndexOutOfBoundsException , UnsupportedFieldException ,  illegalArgumentException,FieldFullException so we need to handle all these exception so that if the data for any field is not available we can have the rest of the phonebook.

For every field we can check the data type and filed count values of that FIELD.

PIMList pimList = PIM.getInstance().openPIMList(PIM.CONTACT_LIST,
   PIM.READ_ONLY, contactsFrom);

contactsFrom can have values as “contacts”,”sim”,”phone” which needs to be passed from the previous form from where user selects the list to view the contacts.

The pimList object that we have can now be used to check various fields that is supported by the phone.

Before using any field first of all we should check that PIMItem supports that field or not using .
We can check the support using:- pimList.isSupportedFIeld(FieldName which needs to be checked).
If it returns true than we can access this field.
Similarly we also need to check for the data type of particular field using
pimList.getFieldDataType(Contact.NAME) which can have values like Contact.STRING_ARRAY,Contact.STRING based on which we can retrieve the values.

These are the basic things that we should take care while retrieving contacts using PIM API.
The sample program will help you understand the same a little better. It is structured so as to retrieve contacts from maximum phones but it will retrieve contacts from all phones is not guaranteed. 


View the code here:

https://docs.google.com/document/d/1qla0Fs3-GRn3RjiT3HyBEpN2BPkE8lYqkdfaQ11dTVs/edit 


Do post your comments and feedback.