FileDocCategorySizeDatePackage
PIMProxy.javaAPI DocphoneME MR2 API (J2ME)33564Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim

PIMProxy

public class PIMProxy extends PIMHandler
Porting layer implementation for PIM functionality.

Fields Summary
private Hashtable
listFields
Table of fields for any list.
private Hashtable
listAttributes
Table of attributes for any list.
private Object
initialized
List handle for which fields have been already initialized.
private int
dataHandler
Field for storing data handler between native calls
private int
itemCounter
Field for storing items counter between native calls
public static final int
CONTACT_LIST
Constant representing a Contact List. List handle for which fields have been already initialized.
public static final int
EVENT_LIST
Constant representing an Event List.
public static final int
TODO_LIST
Constant representing a ToDo List.
Constructors Summary
Methods Summary
public voidaddCategory(java.lang.Object listHandle, java.lang.String category)
Adds a category to the categories defined for a list.

param
listHandle handle of list
param
category category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        if (getCategories(listHandle).length ==
            getListMaxCategories0(((List)listHandle).handle)) {
            throw new PIMException("Maximum number of categories exceeded",
                PIMException.MAX_CATEGORIES_EXCEEDED);
        }
        if (!addListCategory0(((List)listHandle).handle, category)) {
            throw new PIMException("Unable to add category",
                PIMException.UPDATE_ERROR);
        }
    
private native intaddItem0(int listHandle, byte[] data, java.lang.String categories)
Adds item to the list.

param
listHandle native handle of the list that was previously opened
param
data raw data of the item
param
categories item's categories, separated by comma
return
native handle of the item

private native booleanaddListCategory0(int listHandle, java.lang.String category)
Adds category to the specified list.

param
listHandle native handle of the list that was previously opened
param
category name of the category to add
return
true on success, false otherwise

public voidcloseList(java.lang.Object listHandle)
Closes list.

param
listHandle handle of list
throws
PIMException in case of I/O error.

        if (!listClose0(((List)listHandle).handle)) {
            throw new PIMException();
        }
    
private native booleancommitItemData0(int listHandle, int itemHandle, byte[] data, java.lang.String categories)
Writes modified item data to persistent storage.

param
listHandle native handle of the list that was previously opened
param
itemHandle native handle of the item
param
data raw data of the item
param
categories item's categories, separated by comma
return
true on success, false otherwise

public synchronized java.lang.ObjectcommitListElement(java.lang.Object listHandle, java.lang.Object elementKey, byte[] element, java.lang.String[] categories)
Commit a list element.

param
listHandle handle of the list
param
elementKey the key of the element to be stored, or null if this is a new element.
param
element element data in a form that can be interpreted by getListElement()
param
categories list of categories which the list element belongs to
return
a non-null key for this element, to be used in future calls to commitListElement() and getListElement()
throws
PIMException in case of I/O error.

        Item item = (Item)elementKey;
        List list = (List)listHandle;

        if (elementKey == null) {
            /* Add new item */
            int itemHandle = addItem0(list.handle, element, categories == null ?
                null : FormatSupport.join(categories, ","));
            if (itemHandle == 0) {
                throw new PIMException("Unable to add new item");
            }
            item = new Item(list, itemHandle, element.length, categories);
            item.rawData = element;
        } else if (element == null) {
            /* Remove item */
            if (!removeItem0(list.handle, item.handle)) {
                throw new PIMException("Unable to delete item");
            }
        } else {
            item.rawData = element;
            if (!commitItemData0(list.handle, item.handle,
                element, categories == null ?
                null : FormatSupport.join(categories, ","))) {
                throw new PIMException("Unable to update",
                    PIMException.UPDATE_ERROR);
            }
        }
        return item;
    
public voiddeleteCategory(java.lang.Object listHandle, java.lang.String category)
Deletes a category from the categories defined for a list.

param
listHandle handle of list
param
category category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        if (!deleteListCategory0(((List)listHandle).handle, category)) {
            throw new PIMException("Unable to delete category",
                PIMException.UPDATE_ERROR);
        }
    
private native booleandeleteListCategory0(int listHandle, java.lang.String category)
Removes category from the specified list.

param
listHandle native handle of the list that was previously opened
param
category name of the category to delete
return
true on success, false otherwise

public java.lang.StringgetArrayElementLabel(java.lang.Object listHandle, int field, int arrayElement)
Gets the array element label.

param
listHandle handle of the list
param
field the field number
param
arrayElement the element identifier
return
label fro the array element

        initialize(listHandle);
        return getFieldDescriptor(field).getElementlabel(arrayElement);
    
public java.lang.StringgetAttributeLabel(java.lang.Object listHandle, int attribute)
Gets attribute label for the given field attribute.

param
listHandle handle of the list
param
attribute identifier of attribute
return
attribute label

        initialize(listHandle);
        if (attribute == PIMItem.ATTR_NONE) {
            String tag = "PIM.Attributes.None";
            return Configuration.getPropertyDefault(tag, "Label_" + tag);
        }
        try {
            return ((PIMAttribute)listAttributes.
                    get(new Integer(attribute))).getLabel();
        } catch (NullPointerException npe) {
            return null;
        }
    
private native voidgetAttributes0(int listHandle, PIMAttribute[] attr, int dataHandle)
Retrieves information about all attributes supported by the list.

param
listHandle native handle of the list that was previously opened
param
attr array where attribute descriptions will be stored
param
dataHandle data handle

private native intgetAttributesCount0(int listHandle, int[] dataHandle)
Returns number of attributes supported by the given list.

param
listHandle native handle of the list that was previously opened
param
dataHandle array to store data handle in
return
number of supported attributes

public java.lang.String[]getCategories(java.lang.Object listHandle)
Gets the set of categories defined for a list.

param
listHandle handle of the list
return
the set of defined categories
throws
PIMException If an error occurs or the list is no longer accessible or closed.

        String categories = getListCategories0(((List)listHandle).handle);
        return categories != null ? FormatSupport.split(categories, ',", 0) :
                                    new String[0];
    
public byte[]getDefaultBinaryValue(java.lang.Object listHandle, int field)
Gets the default byte[] value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize(listHandle);
        return null;
    
public booleangetDefaultBooleanValue(java.lang.Object listHandle, int field)
Gets the default boolean value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize(listHandle);
        return false;
    
public longgetDefaultDateValue(java.lang.Object listHandle, int field)
Gets the default date value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize(listHandle);
        return 0;
    
public intgetDefaultIntValue(java.lang.Object listHandle, int field)
Gets the default integer value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize(listHandle);
        PIMFieldDescriptor descriptor = getFieldDescriptor(field);
        return ((Integer) descriptor.getDefaultValue()).intValue();
    
public java.lang.StringgetDefaultListName(int listType)
Get the name of the default list for the given type.

param
listType the type of the list
return
the name of the default list, or null if no list of this type is supported.

        String propertyName;

        if (listType == PIM.CONTACT_LIST) {
            propertyName = "DefaultContactList";
        } else if (listType == PIM.EVENT_LIST) {
            propertyName = "DefaultEventList";
        } else if (listType == PIM.TODO_LIST) {
            propertyName = "DefaultTodoList";
        } else {
            return null;
        }

        return Configuration
		.getProperty(propertyName);
    
public java.lang.String[]getDefaultStringArrayValue(java.lang.Object listHandle, int field)
Gets the default String[] value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        int length = getStringArraySize(listHandle, field);
        return new String[length];
    
public java.lang.StringgetDefaultStringValue(java.lang.Object listHandle, int field)
Gets the default string value for the given field. This will only return a valid value if hasDefaultValue(listType, field) returns true.

param
listHandle handle of the list
param
field identifier of field
return
default value of the field

        initialize(listHandle);
        return null;
    
public intgetFieldDataType(java.lang.Object listHandle, int field)
Gets the data type of the field.

param
listHandle handle of the list
param
field identifier of field
return
data type identifier

        initialize(listHandle);
        try {
            return getFieldDescriptor(field).getDataType();
        } catch (NullPointerException npe) {
            return -1;
        }
    
private PIMFieldDescriptorgetFieldDescriptor(int field)
Gets the descriptor for given field.

param
field the field ID
return
field descriptor

        return (PIMFieldDescriptor)listFields.get(new Integer(field));
    
public java.lang.StringgetFieldLabel(java.lang.Object listHandle, int field)
Gets the label of the field.

param
listHandle handle of the list
param
field identifier of field
return
label of the field

        initialize(listHandle);
        try {
            return getFieldDescriptor(field).getLabel();
        } catch (NullPointerException npe) {
            return null;
        }
    
private native intgetFieldLabelsCount0(int listHandle, int fieldIndex, int dataHandle)
Returns number of labels for the specified field.

param
listHandle native handle of the list that was previously opened
param
fieldIndex index of the field
param
dataHandle handle of data
return
number of labels

private native voidgetFields0(int listHandle, PIMFieldDescriptor[] desc, int dataHandle)
Retrieves information about all fields supported by the list.

param
listHandle native handle of the list that was previously opened
param
desc array where field descriptions will be stored
param
dataHandle handle of data

private native intgetFieldsCount0(int listHandle, int[] dataHandle)
Returns number of fields supported by the given list.

param
listHandle native handle of the list that was previously opened
param
dataHandle to save data handle in
return
number of supported fields

private native java.lang.StringgetItemCategories0(int itemHandle, int dataHandle)
Retrieves list of categories the specified item belongs to.

param
itemHandle native handle of the item
param
dataHandle handle for data buffer
return
item's categories, separated by comma

private native java.lang.StringgetListCategories0(int listHandle)
Retrieves categories defined for the specified list.

param
listHandle native handle of the list that was previously opened
return
item's categories, separated by comma

public byte[]getListElement(java.lang.Object listHandle, java.lang.Object elementKey)
Get the data for a list element.

param
listHandle handle of the list
param
elementKey the key of the requested element
return
a byte array containing the element data in a supported format
throws
PIMException in case of I/O error.

        return ((Item)elementKey).rawData;
    
public java.lang.String[]getListElementCategories(java.lang.Object listHandle, java.lang.Object elementKey)
Get categories for the specified list element.

param
listHandle handle of list
param
elementKey the key of the requested element
return
an array of categories names
throws
PIMException in case of I/O error.


        return ((Item)elementKey).getCategories();
    
public synchronized java.lang.Object[]getListKeys(java.lang.Object listHandle)
Get list element keys.

param
listHandle handle of the list
return
an array of objects representing PIM element keys. These keys are to be passed to getListElement() and commitListElement().
throws
PIMException in case of I/O error.

        Vector keys = new Vector();
        int[] itemDesc = new int[4];
        int handle = ((List)listHandle).handle;

        while (getNextItemDescription0(handle, itemDesc)) {
            Item nextItem = new Item((List)listHandle, itemDesc[0],
                itemDesc[1]);
            getNextItemData0(nextItem.handle, nextItem.rawData, itemDesc[3]);
            keys.addElement(nextItem);
            String catList = getItemCategories0(nextItem.handle, itemDesc[3]);
            if (catList != null) {
                nextItem.setCategories(FormatSupport.split(catList, ',", 0));
            }
        }
        Item[] items = new Item[keys.size()];
        for (int index = 0; index < items.length; index++) {
            items[index] = (Item)keys.elementAt(index);
        }
        return items;
    
private native intgetListMaxCategories0(int listHandle)
Returns maximum number of categories supported for the given list.

param
listHandle native handle of the list that was previously opened
return
maximum number of categories for the list

public synchronized java.lang.String[]getListNames(int listType)
Get the supported list names for the given list type. All list elements must be unique within the list.

param
listType the type of the list
return
a non-null array of supported list names. A copy of this array is returned by PIM.listPIMLists()

        int namesCount = getListNamesCount0(listType);
        String[] listNames = new String[namesCount];

        if (namesCount != 0) {
            getListNames0(listNames);
        }

        return listNames;
    
private native voidgetListNames0(java.lang.String[] names)
Retrieves list names for the selected list type.

param
names array where list names will be stored (must have sufficient number of elements for list names)
see
#getListNamesCount0

private native intgetListNamesCount0(int listType)
Returns number of lists of the specified type.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
return
number of lists
see
#getListNames0

public intgetMaximumValues(java.lang.Object listHandle, int field)
Get the maximum number of values that can be stored in the given field.

param
listHandle handle of the list
param
field the field type
return
the maximum value

        initialize(listHandle);
        return getFieldDescriptor(field).getMaximumValues();
    
private native booleangetNextItemData0(int itemHandle, byte[] data, int dataHandle)
Retrieves next item's data from the list.

param
itemHandle native handle of the item
param
data buffer to store the item's data
param
dataHandle handle for data buffer
return
true on success, false otherwise

private native booleangetNextItemDescription0(int listHandle, int[] description)
Retrieves general information about the next item in the list.

param
listHandle native handle of the list that was previously opened
param
description buffer to store the item description
return
true on success, false otherwise

public intgetStringArraySize(java.lang.Object listHandle, int field)
Checks if size of the string array.

param
listHandle handle of the list
param
field the field number
return
size of the string array

        initialize(listHandle);
        try {
            return getFieldDescriptor(field).getStringArraySize();
        } catch (NullPointerException e) {
            // debug.exception(Debug.LIGHT, e);
            return 0;
        }
    
public int[]getSupportedArrayElements(java.lang.Object listHandle, int field)
Gets the array of supported elements.

param
listHandle handle of the list
param
field the field number
return
array of supported elements

        int size = getStringArraySize(listHandle, field);
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = i;
        }
        return result;
    
public int[]getSupportedAttributes(java.lang.Object listHandle, int field)
Gets the supported attributes for the given field.

param
listHandle handle of the list
param
field identifier of field
return
array of supported attributes of the field

        initialize(listHandle);
        long attributes = getFieldDescriptor(field).getSupportedAttributes();
        int listType = ((List)listHandle).type;
        // ATTR_NONE is supported for all Contact fields
        int elementCount = listType == PIM.CONTACT_LIST ? 1 : 0;
        for (long a = attributes; a > 0; a >>= 1) {
            if ((a & 1) == 1) {
                elementCount++;
            }
        }
        int[] result = new int[elementCount];
        if (elementCount > 0) {
            int a = 1;
            int i;
            if (listType == PIM.CONTACT_LIST) {
                result[0] = PIMItem.ATTR_NONE;
                i = 1;
            } else {
                i = 0;
            }
            for (; i < elementCount; i++) {
                while ((attributes & a) == 0) a <<= 1;
                result[i] = a;
                a <<= 1;
            }
        }
        return result;
    
public intgetSupportedAttributesMask(java.lang.Object listHandle, int field)
Gets a mask containing all possible attributes for the given field.

param
listHandle handle of the list
param
field the field number
return
supported attribute mask

        initialize(listHandle);
        return (int)getFieldDescriptor(field).getSupportedAttributes();
    
public int[]getSupportedFields(java.lang.Object listHandle)
Gets all fields that are supported in this list. All fields supported by this list, including both standard and extended, are returned in this array.

param
listHandle handle of the list
return
an int array containing all fields supported by this list. The order of the fields returned is unspecified. If there are no supported fields, a zero-length array is returned.

        initialize(listHandle);
        int[] result = new int[listFields.size()];
        Enumeration fieldNumbers = listFields.keys();
        for (int i = 0; i < result.length; i++) {
	    result[i] = ((Integer)fieldNumbers.nextElement()).intValue();
        }
        return result;
    
public booleanhasDefaultValue(java.lang.Object listHandle, int field)
Checks if field has default value.

param
listHandle handle of the list
param
field identifier of field
return
true if field supported

        initialize(listHandle);
        return getFieldDescriptor(field).hasDefaultValue();
    
private synchronized voidinitialize(java.lang.Object listHandle)
Set up field and attribute descriptions.

param
listHandle descriptor of the list which fields and attributes will be retrieved

        if (initialized != listHandle) {
            listFields.clear();
            listAttributes.clear();
            int list = ((List)listHandle).handle;

            int [] tmpArray = new int [1];
            int numFields = getFieldsCount0(list, tmpArray);
            if (numFields <= 0) {
                return;
            }
            PIMFieldDescriptor[] desc = new PIMFieldDescriptor[numFields];
            for (int i = 0; i < numFields; i++) {
                int numLabels = getFieldLabelsCount0(list, i, tmpArray[0]);
                desc[i] = new PIMFieldDescriptor(0, 0, false, null, " ",
                        new String[numLabels], 0L, 0);
            }
            getFields0(list, desc, tmpArray[0]);
            for (int i = 0; i < numFields; i++) {
                listFields.put(new Integer(desc[i].getField()), desc[i]);
            }

            tmpArray[0] = 0;
            int numAttributes = getAttributesCount0(list, tmpArray);
            PIMAttribute[] attr = new PIMAttribute[numAttributes];
            for (int i = 0; i < numAttributes; i++) {
                attr[i] = new PIMAttribute();
            }
            getAttributes0(list, attr, tmpArray[0]);
            for (int i = 0; i < numAttributes; i++) {
                listAttributes.put(new Integer(attr[i].getAttr()), attr[i]);
            }

            initialized = listHandle;
        }
    
public booleanisSupportedArrayElement(java.lang.Object listHandle, int field, int arrayElement)
Checks if the array element is supported.

param
listHandle handle of the list
param
field the field number
param
arrayElement the element identifier
return
true if attribute element is supported

        return arrayElement >= 0 &&
	    arrayElement < getStringArraySize(listHandle, field);
    
public booleanisSupportedAttribute(java.lang.Object listHandle, int field, int attribute)
Checks if attribute is supported.

param
listHandle handle of the list
param
field the field number
param
attribute identifier of attribute
return
true if attribute is supported

        initialize(listHandle);
        if (attribute == PIMItem.ATTR_NONE) {
            return true;
        } else {
            long attributes = getFieldDescriptor(field).
                    getSupportedAttributes();
            return (attributes & attribute) != 0;
        }
    
public booleanisSupportedField(java.lang.Object listHandle, int field)
Checks if field is supported in list.

param
listHandle handle of the list
param
field identifier of field
return
true if field supported

        initialize(listHandle);
        return getFieldDescriptor(field) != null;
    
private native booleanlistClose0(int listHandle)
Closes the specified list.

param
listHandle native handle of the list that was previously opened
return
true on success, false otherwise
see
#listOpen0

private native intlistOpen0(int listType, java.lang.String listName, int mode)
Opens the specified list.

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
listName name of the list to open
param
mode open mode:
  • {@link javax.microedition.pim.PIM#READ_ONLY}
  • {@link javax.microedition.pim.PIM#WRITE_ONLY}
  • {@link javax.microedition.pim.PIM#READ_WRITE}
return
native handle of the opened list
see
#listClose0

public java.lang.ObjectopenList(int listType, java.lang.String listName, int openMode)
Opens list.

param
listType the type of the list
param
listName the name of the list
param
openMode open mode:
  • {@link javax.microedition.pim.PIM#READ_ONLY}
  • {@link javax.microedition.pim.PIM#WRITE_ONLY}
  • {@link javax.microedition.pim.PIM#READ_WRITE}
return
list handle that will be used to access this list
throws
PIMException in case of I/O error.

        int listHandle = listOpen0(listType, listName, openMode);
        if (listHandle == 0) {
            throw new PIMException("Unable to open list");
        }
        return new List(listHandle, listType);
    
private native booleanremoveItem0(int listHandle, int itemHandle)
Removes specified item from the list.

param
listHandle native handle of the list that was previously opened
param
itemHandle native handle of the item
return
true on success, false otherwise

public voidrenameCategory(java.lang.Object listHandle, java.lang.String currentCategory, java.lang.String newCategory)
Rename a category.

param
listHandle handle of list
param
currentCategory current category name
param
newCategory new category name
throws
PIMException If an error occurs or the list is no longer accessible or closed.
see
#getCategories

        if (!renameListCategory0(((List)listHandle).handle, currentCategory,
            newCategory)) {
            throw new PIMException("Unable to rename category",
                PIMException.UPDATE_ERROR);
        }
    
private native booleanrenameListCategory0(int listHandle, java.lang.String currentCategory, java.lang.String newCategory)
Renames category supported by the given list.

param
listHandle native handle of the list that was previously opened
param
currentCategory old category name
param
newCategory new category name
return
true on success, false otherwise