|
October 2005 |
|
|
DFollowing part one, in this article we will access the phone's native PIM database with the PIM API. This API allows us to alter, remove and add items to the mobile phone's phone book, calendar and task lists. The Sony Ericsson implementation of the JSR 75 API supports these three types of lists. There are some fields and functionality that are not supported like the RepeatRule class and the use of categories, you can read more about this in the Sony Ericsson MIDP 2.0 Developers' Guidelines>> At the end of this article there is a example MIDlet that is used to create recurring calendar events, a feature that is missing on some Sony Ericsson phones. To access the native PIM database we need a PIM object. The PIM object has methods for retrieving the different lists that resides in the PIM database. There are three types of lists in the Sony Ericsson implementation:
All of these lists are interfaces and implement the PIMList interface. When having a reference to one of the list, we can list the existing PIMItems, alter, add and remove PIMItems from the list. The Todo, Event and Contact interfaces are all implementing the PIMItem interface. A PIMItem must be of the same type as the type of list, so when using an EventList we can only create and add Event objects. The PIM object is the first object that has to be created to access the native PIM database and it is used to retrieve the different types of lists that reside in the native PIM database. To get a hold of the PIM object we have to use the PIMs factory method getInstance: PIM pim = PIM.getInstance(); When we have a PIM object, we can list the available lists that reside on the mobile phone and we can get a reference to one of the lists with the method: PIMList openPIMList(int pimListType, int mode) The pimListType defines which list we want a access, the mode sets the access mode to the list like read, write and read & write. EventList eventList = (EventList)pim.openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE); As mentioned before there are three different types of pimListTypes:
If the list type is not supported a PIMException will be thrown. Since the user will be prompted to give the MIDlet access to the PIM database, there is a chance that a SecurityException will be thrown and this should be managed as well. When we have an EventList we can list all the existing Event items in the list by calling Enumeration eventEnum= eventList.items(); There are also methods for listing existing PIMitems with filtering functionalities. PIMItems and Fields A PimItem represents an entry in the corresponding list in the PIM database, for example an Event object represents an entry in the EventList (Calendar) in the PIM database. The data that a PIMItem object is encapsulating are called fields. All fields are not supported (see MIDP 2.0 Developers' Guidelines). This can be checked with the method PIMList.isSupportedField(int field) and should be used. Some fields are ints, some are Dates and so on. This is the available fields in an Event object:
The following code snippet lists all the entries in the mobile phone's Calendar and writes the NOTE field for each of them: try{ }catch(Exception e){System.out.println(e.toString());} Creating and adding PIMItems Event event = eventList.createEvent(); Since the fields in the Event interface have three different data types (Date, String, int) there are three different methods for setting the different types of fields: void addDate(int field, int attributes, long value) void addInt(int field, int attributes, int value) addString(int field, int attributes, java.lang.String value) The field parameter determines which field we want to use, for example Event.NOTE. The attribute value is used for further description of the data values for a field, if no attributes are to be associated, then the ATTR_NONE is used. The value parameter contains the actual value for the field. In the code snippet below we are setting the NOTE field and use the addString method since the NOTE field is of the data type String: This will not change anything in the list and the native PIM database, to create permanent event data we need to use the method commit: try{ The same thing applies for all PIMItems. Serialization void toSerialFormat(PIMItem item, java.io.OutputStream os, java.lang.String enc, The enc parameter specifies the encoding and must be "UTF-8" for Sony Ericsson mobile phones. The dataFormat String should be "VCARD/2.1" for Contact objects and "VCALENDAR/1.0" for Todo and Event objects. This parameter can be retrieved with the method supportedSerialFormats(int pimListType) in the PIM class. String[] data_formats = pim.supportedSerialFormats(PIM.CONTACT_LIST); PIMItem[] fromSerialFormat(java.io.InputStream is, java.lang.String enc) The enc parameter has to be "UTF-8" as with the toSerialFormat method. Here is a code example for a MIDlet that is used for creating recurrent Events in the native Calendar without the use of the RepeatRule class: More information:
| |

Technorati
