/** * Copyright (c) 2012 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.TimeZone; import javax.microedition.lcdui.*; import javax.microedition.pim.*; // Form used to create a new event and send it to a contact class EventCreationForm extends Form implements CommandListener { private final Command sendCommand, exitCommand, findCommand; private final TextField number, duration, topic; private final DateField dateTime; private final EventSharingMIDlet parent; public EventCreationForm(EventSharingMIDlet parent, int port) { super("Create an event"); this.parent = parent; // init UI sendCommand = new Command("Send", Command.SCREEN, 1); findCommand = new Command("Find Contacts", Command.SCREEN, 2); exitCommand = new Command("Exit", Command.EXIT, 1); number = new TextField("Dest. number", "", 20, TextField.PHONENUMBER); dateTime = new DateField("Date", DateField.DATE_TIME, TimeZone.getDefault()); dateTime.setDate(new Date()); duration = new TextField("Duration (min)", "30", 24, TextField.NUMERIC); topic = new TextField("Subject", "", 24, TextField.ANY); // create the form append(number); append(dateTime); append(duration); append(topic); addCommand(findCommand); addCommand(sendCommand); addCommand(exitCommand); setCommandListener(this); } void setTargetPhoneNumber(String phoneNumber) { number.setString(phoneNumber); } public void commandAction(Command cmd, Displayable displayable) { if (cmd == sendCommand) { // do a sanity check and then // send the message in a separate thread parent.enqueueOperation(new SendEventMessageOperation(number.getString(), dateTime.getDate(), duration.getString(), topic.getString())); } else if (cmd == exitCommand) { parent.notifyDestroyed(); } else if (cmd == findCommand) { parent.showContactsList(); } } private class SendEventMessageOperation implements Operation { private final String number, duration, topic; private final Date date; SendEventMessageOperation(String number, Date date, String duration, String topic) { this.number = number; this.date = date; this.duration = duration; this.topic = topic; } public void execute() { int length = 0; try { length = Integer.parseInt(duration); } catch (NumberFormatException e) { parent.showMessage("Duration not valid", EventCreationForm.this); return; } if (length <= 0) { parent.showMessage("Duration needs to be positive", EventCreationForm.this); } else if (number == null || number.length() == 0) { parent.showMessage("Number not entered", EventCreationForm.this); } else if (topic == null || topic.length() == 0) { parent.showMessage("Subject not entered", EventCreationForm.this); } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); EventList eventList = null; try { PIM pim = PIM.getInstance(); String listNames[] = pim.listPIMLists(PIM.EVENT_LIST); if (listNames.length > 0) { eventList = (EventList) pim.openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE, listNames[0]); Event newEvent = eventList.createEvent(); if (eventList.isSupportedField(Event.SUMMARY)) { newEvent.addString(Event.SUMMARY, PIMItem.ATTR_NONE, topic); } if (eventList.isSupportedField(Event.START)) { newEvent.addDate(Event.START, PIMItem.ATTR_NONE, date.getTime()); } if (eventList.isSupportedField(Event.END)) { newEvent.addDate(Event.END, PIMItem.ATTR_NONE, date.getTime() + 60 * 1000 * length); } // let's check that VCALENDAR/1.0 is supported String supportedFormats[] = PIM.getInstance().supportedSerialFormats(PIM.EVENT_LIST); for (int i = 0; i < supportedFormats.length; i++) { if (supportedFormats[i].equals("VCALENDAR/1.0")) { PIM.getInstance().toSerialFormat(newEvent, out, "UTF-8", "VCALENDAR/1.0"); break; } } if (out.size() == 0) { parent.showMessage("VCALENDAR/1.0 not supported", EventCreationForm.this); } // let's add the event locally // an advanced version should wait for an ack eventList.importEvent(newEvent); newEvent.commit(); } else { parent.showMessage("No Event list available", EventCreationForm.this); } } catch (PIMException e) { parent.showMessage("PIMException: " + e.getMessage(), EventCreationForm.this); } catch (SecurityException e) { parent.showMessage("SecurityException: " + e.getMessage(), EventCreationForm.this); } catch (UnsupportedEncodingException e) { // should not happen since UTF-8 is mandatory } finally { try { if (eventList != null) { eventList.close(); } } catch (PIMException e) { // ignore, we are closing anyway } } // out could be empty if there is no support // for VCALENDAR/1.0 if (out.size() > 0) { parent.sendMessage(number, out.toByteArray()); } } } } }