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

PIMImpl

public final class PIMImpl extends javax.microedition.pim.PIM
Implementation of PIM.

Fields Summary
private static final PIMFormat[]
formats
Supported serial formats.
Constructors Summary
public PIMImpl()
PIMImpl constructor, called from PIM.getInstance().

    
          
      
    
Methods Summary
private voidcheckPermissions(int pimListType, int mode)
Checks for all the permissions that need to be present to open a list

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
mode READ_ONLY, WRITE_ONLY or READ_WRITE
throws
IllegalArgumentException if one of the parameters is out of bounds
throws
SecurityException if the application does not have the required permissions

        int[] permissions = getPermissions(pimListType, mode);
        MIDletSuite suite = Scheduler.getScheduler().getMIDletSuite();
        /*
         * Do a first pass on the permissions to make sure that none is
         * automatically denied. This is for the case when both read permission
         * and write permission are required. It is possible that, for example,
         * read permission is granted only after asking the user, but write
         * permission is automatically denied. In this case, the user should
         * not be asked a question at all.
         */
        for (int i = 0; i < permissions.length; i++) {
            String name = Permissions.getName(permissions[i]);
            int status = suite.checkPermission(name);
            if (status == 0) {
                // throw an exception
                suite.checkIfPermissionAllowed(permissions[i]);
            } else if (status == 1) {
                // don't check this permission again
                permissions[i] = -1;
            }
        }
        for (int i = 0; i < permissions.length; i++) {
            if (permissions[i] != -1) {
                try {
                    suite.checkForPermission(permissions[i], null);
                } catch (InterruptedException e) {
                    throw new SecurityException("Security check interrupted: "
                        + e.getMessage());
                }
            }
        }
    
public javax.microedition.pim.PIMItem[]fromSerialFormat(java.io.InputStream is, java.lang.String enc)
Gets an array of PIM items from an encoded input stream.

param
is data input stream
param
enc character encoding of stream data
return
array of PIM items
throws
PIMException if any error reading the PIM data
throws
UnsupportedEncodingException if encoding is not supported

        return fromSerialFormat(is, enc, null);
    
private javax.microedition.pim.PIMItem[]fromSerialFormat(java.io.InputStream is, java.lang.String enc, javax.microedition.pim.PIMList list)
Gets an array of PIM items from an encoded input stream and list.

param
is data input stream
param
enc character encoding of stream data
param
list populate from list
return
array of PIM items
throws
PIMException if any error reading the PIM data
throws
UnsupportedEncodingException if encoding is not supported

        
        if (enc == null) {
            enc = "UTF-8" /* NO I18N */;
        }
        InputStream in = new MarkableInputStream(is);
        in.mark(Integer.MAX_VALUE);
        try {
            for (int i = 0; i < formats.length; i++) {
                try {
                    PIMItem[] items = formats[i].decode(in, enc, list);
                    if (items == null) {
                        throw new PIMException(
                            "Empty stream or insufficient data");
                    }
                    return items;
                } catch (UnsupportedPIMFormatException e) {
                    in.reset();
                    in.mark(Integer.MAX_VALUE);
                }
            }
            throw new PIMException("Format is not recognized");
        } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (IOException e) {
            throw new PIMException(e.getMessage());
        }
    
private int[]getPermissions(int listType, int mode)
Gets the permissions that need to be present to open a list

param
listType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
mode READ_ONLY, WRITE_ONLY or READ_WRITE
return
list of permissions to be checked
throws
IllegalArgumentException if one of the parameters is out of bounds

        switch (listType) {
            case CONTACT_LIST:
                switch (mode) {
                    case READ_ONLY:
                        return new int[] { Permissions.PIM_CONTACT_READ };
                    case WRITE_ONLY:
                        return new int[] { Permissions.PIM_CONTACT_WRITE};
                    case READ_WRITE:
                        return new int[] {
                            Permissions.PIM_CONTACT_READ,
                            Permissions.PIM_CONTACT_WRITE };
                    default:
                        throw new IllegalArgumentException("Not a valid mode: "
                            + mode);
                }
            case EVENT_LIST:
                switch (mode) {
                    case READ_ONLY:
                        return new int[] { Permissions.PIM_EVENT_READ };
                    case WRITE_ONLY:
                        return new int[] { Permissions.PIM_EVENT_WRITE};
                    case READ_WRITE:
                        return new int[] {
                            Permissions.PIM_EVENT_READ,
                            Permissions.PIM_EVENT_WRITE };
                    default:
                        throw new IllegalArgumentException("Not a valid mode: "
                            + mode);
                }
            case TODO_LIST:
                switch (mode) {
                    case READ_ONLY:
                        return new int[] { Permissions.PIM_TODO_READ };
                    case WRITE_ONLY:
                        return new int[] { Permissions.PIM_TODO_WRITE};
                    case READ_WRITE:
                        return new int[] {
                            Permissions.PIM_TODO_READ,
                            Permissions.PIM_TODO_WRITE };
                    default:
                        throw new IllegalArgumentException("Not a valid mode: "
                            + mode);
                }
            default:
                throw new IllegalArgumentException("Not a valid list type: " 
                    + listType);
        }
    
public java.lang.String[]listPIMLists(int pimListType)
Gets the current PIM lists.

param
pimListType type of list to return
return
array of list names

        checkPermissions(pimListType, PIM.READ_ONLY);
        validatePimListType(pimListType);
        return PIMHandler.getInstance().getListNames(pimListType);
    
public javax.microedition.pim.PIMListopenPIMList(int pimListType, int mode)
Opens the PIM list.

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
mode READ_ONLY, WRITE_ONLY or READ_WRITE
return
handle to opened PIM list
throws
PIMException if the list is not found

        validatePimListType(pimListType);
        validateMode(mode);
        checkPermissions(pimListType, mode);
        String listName = PIMHandler.getInstance()
            .getDefaultListName(pimListType);
        if (listName == null) {
            throw new PIMException("List not available");
        }
        return openPIMListImpl(pimListType, mode, listName);
    
public javax.microedition.pim.PIMListopenPIMList(int pimListType, int mode, java.lang.String name)
Opens the PIM list.

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
mode READ_ONLY, WRITE_ONLY or READ_WRITE
param
name name of the list
return
handle to opened PIM list
throws
PIMException if the list is not found

        if (name == null) {
            throw new NullPointerException("PIM list name cannot be null");
        }
        validatePimListType(pimListType);
        validateMode(mode);
        checkPermissions(pimListType, mode);
        validateName(pimListType, name);
        return openPIMListImpl(pimListType, mode, name);
    
private javax.microedition.pim.PIMListopenPIMListImpl(int pimListType, int mode, java.lang.String name)
Does the same as openPIMList, without any validation

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
mode READ_ONLY, WRITE_ONLY or READ_WRITE
param
name name of the list
return
handle to opened PIM list
throws
PIMException if the list is not found

    
        AbstractPIMList list;
        PIMHandler handler = PIMHandler.getInstance();
        Object listHandle = handler.openList(pimListType, name, mode);
        
        switch (pimListType) {
            case PIM.CONTACT_LIST:
                list = new ContactListImpl(name, mode, listHandle);
                break;
            case PIM.EVENT_LIST:
                list = new EventListImpl(name, mode, listHandle);
                break;
            case PIM.TODO_LIST:
                list = new ToDoListImpl(name, mode, listHandle);
                break;
            default:
                // pimListType has been verified
                throw new Error("Unreachable code");
        }
        Object[] keys = handler.getListKeys(listHandle);
        for (int i = 0; i < keys.length; i++) {
            byte[] data = handler.getListElement(listHandle, keys[i]);
            String [] categories = handler.getListElementCategories(listHandle,
                keys[i]);
            try {
                PIMItem[] items =
                    fromSerialFormat(new ByteArrayInputStream(data), 
                        "UTF-8", list);
                for (int j = 0; j < items.length; j++) {
                    AbstractPIMItem item = (AbstractPIMItem) items[j];
                    item.setKey(keys[i]);
                    list.addItem(item);
                    item.setDefaultValues();
                    for (int index = 0; index < categories.length; index ++) {
                        item.addToCategory(categories[index]);
                    }
                    item.setModified(false);
                }
            } catch (UnsupportedEncodingException e) {
                throw new Error("UTF-8 not supported");
            } catch (PIMException e) {
                // skip element
            }
        }
        return list;
    
public java.lang.String[]supportedSerialFormats(int pimListType)
Gets the list of supported serial formats.

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
return
array of format names

        validatePimListType(pimListType);
        int supportedFormatCount = 0;
        for (int i = 0; i < formats.length; i++) {
            if (formats[i].isTypeSupported(pimListType)) {
                supportedFormatCount ++;
            }
        }
        String[] supportedFormats = new String[supportedFormatCount];
        for (int i = 0; i < formats.length; i++) {
            if (formats[i].isTypeSupported(pimListType)) {
                supportedFormats[--supportedFormatCount] = formats[i].getName();
            }
        }
        return supportedFormats;
    
public voidtoSerialFormat(javax.microedition.pim.PIMItem item, java.io.OutputStream os, java.lang.String enc, java.lang.String dataFormat)
Converts to serial format.

param
item the PIM item to be processed
param
os the target output stream
param
enc the character encoding for output strings
param
dataFormat the serialized format
throws
PIMException if any error writing the PIM data
throws
UnsupportedEncodingException if encoding is not supported

        
        if (enc == null) {
            enc = "UTF-8" /* NO I18N */;
        }
        if (dataFormat == null) {
            throw new NullPointerException("Null data format");
        }
        if (dataFormat.trim().length() == 0) {
            throw new IllegalArgumentException("Empty data format");
        }
        if (item == null) {
            throw new NullPointerException("Null PIM item");
        }

        try {
            for (int i = 0; i < formats.length; i++) {
                if (formats[i].getName().equals(dataFormat)) {
                    formats[i].encode(os, enc, item);
                    return;
                }
            }
            throw new IllegalArgumentException("Data format '" + dataFormat
                + "' is not supported");
        } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (IOException e) {
            throw new PIMException(e.getMessage());
        }
    
private voidvalidateMode(int mode)
Ensures that the given PIM list mode is valid.

param
mode READ_ONLY, WRITE_ONLY or READ_WRITE

        switch (mode) {
            case READ_ONLY:
            case WRITE_ONLY:
            case READ_WRITE:
                break;
            default:
                throw new IllegalArgumentException(
                    "Invalid PIM list mode: " + mode);
        }
    
private voidvalidateName(int pimListType, java.lang.String name)
Ensures that the given PIM list name is valid.

param
pimListType CONTACT_LIST, EVENT_LIST or TODO_LIST
param
name name of the list
throws
PIMException if list with the specified name is not found

        String[] names = PIMHandler.getInstance().getListNames(pimListType);
        for (int i = 0; i < names.length; i++) {
            if (name.equals(names[i])) {
                return;
            }
        }
        throw new PIMException("PIM list does not exist: '" + name + "'");
    
private voidvalidatePimListType(int pimListType)
Ensures that the given PIM list type is valid.

param
pimListType a PIM list type
throws
IllegalArgumentException if the list type is not valid.

        switch (pimListType) {
            case PIM.CONTACT_LIST:
            case PIM.EVENT_LIST:
            case PIM.TODO_LIST:
                // ok
                break;
            default:
                throw new IllegalArgumentException("Not a valid PIM list type: "
                    + pimListType);
        }