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.

Monday, 23 July 2012

Range from Minimum And Maximum Price


Product Range from Minimum and Maximum Price

Range From Minimum and Maximum Price
This algorithm is designed to find out a clean range of prices for several categories in any ecommerce website. Nowadays the products like mobile have ranges starting less than one thousand to let’s say 70,000. If user wants to see a range of products between a starting price and maximum upto some price then there should be a way by which we can provide a clean range . In this case the range should be something similar to

1. 1000-10000
2. 10000-25000
3. 25000-40000
4. 40000-70000

This gives a clean range taking care of the fact that the number of products in latter range would be less if we compare it with the initial range so the initial range is closely separated while the latter is sparsely separated so that it takes minimum tries for the user to get the product he was searching for.
Also as a lot of mobile accessories are sold by websites so this algorithm is all the more important if the accessories and mobiles are shown as 1 single search result .This is one such example , similarly we can see this for other categories as well.

Design of the Algorithm
For implementing this algorithm we started with several hit and trial methods untill we came with a clean range of numbers for providing the range.
Consider minimum price of a product = X
maximum price of a product = Y
Step -1 Round off the minimum X to the lower 100 value.
eg: 763 would be rounded of to 700
Step -2 Round off the maximum Y to the upper 100 or 1000 value.
eg: 8976 would be rounded of to 10000

Taking the above figures as examples we will be proceding to explain the steps

Step -3 Get the difference of the minimum and maximum rounded off numbers.
Here in this case we will get 10000-700=9300.

Step -4 Now as we need the earlier part of the range to be closely spaced and latter to be sparsely spaced we will use the weighted mean  so the difference will be divided by 10.
10 because we need to have 4 ranges total and for weighted range we use( 1 +2+3+4).
If we want 5 ranges in total what we can do is divide it by 15 as (1+2+3+4+5) and so on.
So the difference /10 = 9300/10=930

Step - 5 We will have 5 numbers from this as
Number1=Minimum = 700
Number2=Number1+1*difference = 700+1*930=1630
Number3= Number 2+2*difference = 1630+2*930=3490
Number4= Number 3+3*difference = 3490+3*930=6280
Number5=Number 4+4*difference = 6280+4*930=10000

So our ranges are  700-1630
      1630-3490
       3490-6280
      6280-10000


These ranges are not clean and looks very odd if we as a user get these to filter out our search results. To improve this we can have a range of values and for lower limit check for the lower limit from that list and for higher limit get the greater value from the list forming the clean range.

Paging Component In LWUIT


Paging Component for J2ME using the LWUIT API


Paging Component:
J2ME phones have very limited memory so its very important for the developers to use that memory efficiently . Paging is one such method which can help us save memory by providing the complete content to the user in an organized manner. If the server side API is designed so as to provide paging the paging component can be used to provide user to switch between pages in a very organized manner.
The paging component accepts a PaginableObject , Total Item count.
Other than this the ItemCount for 1 page and total pages shown at once can be constant as these are not going to change for an application.

Code Snippet:-
public PagingComponent(final Paginable p, int noOfFriends) {
//code to show paging component
}


Paginable Interface

This interface is implemented by all the forms on which we want a PagingComponent to be shown. It has a method onPageChange which accepts the current pageNumber for which the list needs to be updated.

public interface Paginable {
   public void onPageChange(int pageNumber);
}

Use Cases:

The PagingComponent object is created on a form on which we want to implement the Pagination. Generally considering the mobile screen sizes we have shown 10 items in one page and total 5 buttons for changing pages and a left and right button to increase or decrease the page number. We need to have a variable in our form to keep track of current page .

Limit variable or argument at server side will accept the value = no of items that you want to show on single page.

While offset will have the value = currentPage*No of items on one page


For the code please refer the following:


https://docs.google.com/open?id=0B8ZY9NBZj0LPbTBqWEFkRTFIdWs