*/ import javax.microedition.pim.*; import java.util.Enumeration; import java.util.Calendar; import java.util.Date; public class PIMTest { public PIMTest() {
}
/**************************************************************************
* Contact/ContactList sample code
***************************************************************************/ public void createAContact() {
// Create a support contact entry in the device's
//local address book
// so that the users have has the contact
//information for anything that they
// need help with about your application
PIM pim = PIM.getInstance();
ContactList cl = null;
Contact new_contact = null; try {
// Open write only since you're just going to
// add your support contact
// info to the device's database
cl = (ContactList)
pim.openPIMList(PIM.CONTACT_LIST, PIM.WRITE_ONLY);
} catch (PIMException e) {
// failed opening the default contact list!
// Error case - abort this attempt
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request for
//write access to contact list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't add the contact"); return;
}
// Create an "empty" contact to work with
new_contact = cl.createContact();
// Add your company's info: company name,
//two support phone numbers, a support
// email, and a note about what product the user has. Add whatever
// information that the native contact list
//supports and don't add it if
// the field is not supported. if (cl.isSupportedField(Contact.ORG))
new_contact.addString(Contact.ORG, PIMItem.ATTR_NONE,
"Acme, Inc."); if (cl.isSupportedField(Contact.TEL)) {
new_contact.addString(Contact.TEL, PIMItem.ATTR_NONE,
"800-888-8888");
new_contact.addString(Contact.TEL, PIMItem.ATTR_NONE,
"800-888-8889");
} if (cl.isSupportedField(Contact.EMAIL))
new_contact.addString(Contact.EMAIL,
PIMItem.ATTR_NONE, "[email protected]"); if (cl.isSupportedField(Contact.NOTE))
new_contact.addString(Contact.NOTE, PIMItem.ATTR_NONE,
"You've purchased application XXX with registration number NNN."); try {
// commits it to the list and the native database
new_contact.commit(); // commits it to the list and the native database
} catch (PIMException e) {
// failed committing the contact
System.err.println(
"This application cannot add the contact info");
} try {
cl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void retrieveContacts() {
// Get all contacts with last name starting with "S" (e.g.
// Smith, Sanders, Stargell, etc.) for a listing screen
PIM pim = PIM.getInstance();
ContactList cl = null;
Contact search_template = null;
Enumeration s_contacts = null; try {
cl = (ContactList) pim.openPIMList(PIM.CONTACT_LIST,
PIM.READ_ONLY);
} catch (PIMException e) {
// failed opening the default contact list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request for
// read access to contact list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't get contacts"); return;
}
// first create a "template" contact which we'll use for matching
search_template = cl.createContact(); if (cl.isSupportedArrayElement(Contact.NAME,
Contact.NAME_FAMILY)) {
// this particular contact list does contain last names, so we
// can now do the search
// now fill in the search parameters of last name
// starting with 'S'
String[] name_struct = new String[Contact.NAMESIZE];
name_struct[Contact.NAME_FAMILY] = "S";
search_template.addStringArray(Contact.NAME,
PIMItem.ATTR_NONE, name_struct);
} else if (cl.isSupportedField(Contact.FORMATTED_NAME)) {
// the contact implementation doesn't have individual name
// fields, so try the single name field FORMATTED_NAME
search_template.addString(Contact.FORMATTED_NAME,
PIMItem.ATTR_NONE, "S");
} try {
// Get the enumeration of matching elements
s_contacts = cl.items(search_template);
} catch (PIMException e) {
// failed to retrieve elements due to error!
System.err.println(
"This application cannot retrieve the contacts");
} try {
cl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void modifyAContact() {
// Code sample:
// Update John Smith's home phone number
// from "555-0000" to "555-1212"
// since he moved...
PIM pim = PIM.getInstance();
ContactList cl = null;
Enumeration contacts = null; try {
cl = (ContactList) pim.openPIMList(
PIM.CONTACT_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// failed opening the default contact list!
System.err.println(
"This application failed to open the contact list");
} catch (SecurityException e) {
// user rejected application's request
// for read/write access to contact list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't get contacts"); return;
}
// first create a "template" contact which we'll use for matching
// to find John Smith's contact entry
Contact template = cl.createContact();
String tel_number = ""; if (cl.isSupportedField(Contact.NAME)) {
String[] name_struct = new String[Contact.NAMESIZE];
name_struct[Contact.NAME_FAMILY] = "Smith";
name_struct[Contact.NAME_FAMILY] = "John";
template.addStringArray(
Contact.NAME, PIMItem.ATTR_NONE, name_struct);
} if (cl.isSupportedField(Contact.TEL)) {
template.addString(Contact.TEL, Contact.ATTR_HOME, "555-0000");
} try {
// Get the enumeration of matching elements
contacts = cl.items(template);
} catch (PIMException e) {
// failed retrieving the items enumeration due to an error
System.err.println(
"This application cannot retrieve the contact");
}
// update all John Smith entries with old home numbers of 555-0000 while (contacts!= null && contacts.hasMoreElements()) {
Contact c = (Contact) contacts.nextElement(); for (int index = c.countValues(Contact.TEL); index != 0; index--)
{ if (c.getString(Contact.TEL, index).equals("555-0000")) {
c.setString(Contact.TEL, index, Contact.ATTR_HOME,
"555-1212"); try {
// save change to the database
c.commit();
} catch (PIMException e) {
// Oops couldn't save the data...
System.err.println(
"This application cannot commit the contact info");
} break; // go to next matching element
}
}
} try {
cl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void deleteContacts() {
// Delete all contacts at company WorldCom
// since they won't be answering
// phone calls anymore...
PIM pim = PIM.getInstance();
ContactList cl = null;
Enumeration contacts = null; try {
cl = (ContactList) pim.openPIMList(
PIM.CONTACT_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// failed opening the default contact list!
System.err.println(
"This application failed to open the contact list"); return;
} catch (SecurityException e) {
// user rejected application's request for
// read/write access to contact list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't get contacts"); return;
}
// first create a "template" contact which we'll use for matching
// to find WorldCom contact entries
Contact template = cl.createContact(); if (cl.isSupportedField(Contact.ORG)) {
template.addString(Contact.ORG,
PIMItem.ATTR_NONE, "WorldCom"); try {
// Get the enumeration of matching elements
contacts = cl.items(template);
} catch (PIMException e) {
// failed retrieving the items enumeration due to an error
System.err.println(
"This application cannot commit the contact info");
}
}
// delete all WorldCom entries while (contacts != null && contacts.hasMoreElements()) {
Contact c = (Contact) contacts.nextElement(); try {
cl.removeContact(c);
} catch (PIMException e) {
// couldn't delete the entry for some
// reason (probably shredded)
System.err.println(
"This application cannot remove the contact info");
}
} try {
cl.close();
} catch (PIMException e) {
// failed to close the list
}
}
/***************************************************************************
Event/EventList sample code
***************************************************************************/ public void createAnEvent() {
// Create an event entry in the device's local calendar
// reminding the user to register your application
PIM pim = PIM.getInstance();
EventList el = null;
Event new_event = null; try {
// Open write only since you're just going to
//add your registration
// event info to the device's database
el = (EventList) pim.openPIMList(
PIM.EVENT_LIST, PIM.WRITE_ONLY);
} catch (PIMException e) {
// failed opening the default event list!
// Error case - abort this attempt
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request
// for write access to event list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't add the event"); return;
}
// Create an "empty" event to work with
new_event = el.createEvent();
// Add a registration reminder event:
// make it two weeks from now with an
// alarm 10 minutes before the occurrence, and
// add a note with the phone or email to call. if (el.isSupportedField(Event.START)) {
Date d = new Date(); long l = d.getTime() + (long)1209600000;
new_event.addDate(Event.START, PIMItem.ATTR_NONE, l);
} if (el.isSupportedField(Event.ALARM))
new_event.addInt(Event.ALARM, PIMItem.ATTR_NONE, 600); if (el.isSupportedField(Event.SUMMARY))
new_event.addString(Event.SUMMARY, PIMItem.ATTR_NONE,
"Register Your Product!"); if (el.isSupportedField(Event.NOTE))
new_event.addString(Event.NOTE, PIMItem.ATTR_NONE, "You've purchased application XXX with registration number NNN. Please register it now. Look in the Contact List for information on how to contact us."); try {
// commits it to the list and the native database
new_event.commit(); // commits it to the list and the native database
} catch (PIMException e) {
// failed committing the event
System.err.println("This application cannot add the event");
} try {
el.close();
} catch (PIMException e) {
// failed to close the list
}
} public void retrieveEvents() {
// Get all events occurring for the coming week,
// for a listing screen
PIM pim = PIM.getInstance();
EventList el = null;
Event search_template = null;
Enumeration this_weeks_events = null; try {
el = (EventList) pim.openPIMList(
PIM.EVENT_LIST, PIM.READ_ONLY);
} catch (PIMException e) {
// failed opening the default event list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request for
// read access to event list
// This is not an error condition and can be normal
System.out.println("Okay, this application won't get events"); return;
}
// calculcate today's date and next week's date long current_time = (new Date()).getTime(); long next_week = current_time + 604800000; try {
// Get the enumeration of matching elements
this_weeks_events = el.items(
EventList.OCCURRING, current_time, next_week, true);
} catch (PIMException e) {
// failed to retrieve elements due to error!
// Error case - abort this attempt
System.err.println(
"This application cannot retrieve the events");
} try {
el.close();
} catch (PIMException e) {
// failed to close the list
}
} public void modifyEvents() {
// Code sample:
// Postpone all events from today until
// tomorrow (sick day today...)
PIM pim = PIM.getInstance();
EventList el = null;
Enumeration todays_events = null; try {
el = (EventList) pim.openPIMList(
PIM.EVENT_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// failed opening the default event list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's requestfor read/write access to contact list
// This is not an error condition and can be normal
System.out.println("Okay, this application won't modify any event"); return;
}
// calculate today's start and end times
Calendar start_of_day = Calendar.getInstance();
// start of work day is 7am
start_of_day.set(Calendar.HOUR_OF_DAY, 7); // start of work day is 7am
Calendar end_of_day = Calendar.getInstance();
// end of work day is 8pm
end_of_day.set(Calendar.HOUR_OF_DAY, 20); // end of work day is 8pm try {
// Get the enumeration of matching elements
todays_events = el.items(Event.OCCURRING, start_of_day.getTime().getTime(), end_of_day.getTime().getTime(), true);
} catch (PIMException e) {
// failed to retrieve elements due to error!
System.err.println(
"This application cannot retrieve the events");
}
// update all events by one day while (todays_events != null && todays_events.hasMoreElements()) {
Event e = (Event) todays_events.nextElement();
e.setDate(Event.START, 0, PIMItem.ATTR_NONE,
e.getDate(Event.START, 0) + 86400000); try {
// save change to the database
e.commit();
} catch (PIMException exe) {
// Oops couldn't save the data...
System.err.println(
"This application cannot commit the event");
}
} try {
el.close();
} catch (PIMException e) {
// failed to close the list
}
} public void deleteEvents() {
// Delete all events having to do with Christmas (bah humbug!)
PIM pim = PIM.getInstance();
EventList el = null;
Enumeration xmas_events = null; try {
el = (EventList) pim.openPIMList(
PIM.EVENT_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// failed opening the default event list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request
// for read/write access to event list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't modify any event"); return;
} try {
// Get the enumeration of matching elements
xmas_events = el.items("Christmas");
} catch (PIMException e) {
// failed retrieving the items enumeration due to an error
System.err.println(
"This application cannot retrieve the events"); return;
}
// delete all event entries containing Christmas while (xmas_events != null && xmas_events.hasMoreElements()) {
Event e = (Event) xmas_events.nextElement(); try {
el.removeEvent(e);
} catch (PIMException exe) {
// couldn't delete the entry for some reason
System.err.println(
"This application cannot remove the event info");
}
} try {
el.close();
} catch (PIMException e) {
// failed to close the list
}
}
/**************************************************************************
* ToDo/ToDoList sample code
***************************************************************************/ public void createAToDo() {
// Create a todo entry in the device's local todo list
// reminding the user to register your application
PIM pim = PIM.getInstance();
ToDoList tl = null;
ToDo new_todo = null; try {
// Open write only since you're just going to
// add your registration
// todo info to the device's todo database
tl = (ToDoList) pim.openPIMList(PIM.TODO_LIST, PIM.WRITE_ONLY);
} catch (PIMException e) {
// failed opening the default todo list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request
// for write access to todo list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't add the todo"); return;
}
// Create an "empty" todo to work with
new_todo = tl.createToDo();
// Add a registration todo: make it have a
// due date of two weeks from now
// with a low priority, and
// add a note with the phone or email to call. if (tl.isSupportedField(ToDo.DUE)) {
Date d = new Date(); long l = d.getTime() + (long)1209600000;
new_todo.addDate(ToDo.DUE, PIMItem.ATTR_NONE, l);
} if (tl.isSupportedField(ToDo.PRIORITY))
new_todo.addInt(ToDo.PRIORITY, PIMItem.ATTR_NONE, 5); if (tl.isSupportedField(ToDo.SUMMARY))
new_todo.addString(ToDo.SUMMARY, PIMItem.ATTR_NONE,
"Register Your Product!"); if (tl.isSupportedField(ToDo.NOTE))
new_todo.addString(ToDo.NOTE, PIMItem.ATTR_NONE,
"You've purchased application XXX with registration number NNN. Please register it now.Look in the Contact List for information on how to contact us."); try {
// commits it to the list and the native database
new_todo.commit(); // commits it to the list and the native database
} catch (PIMException e) {
// failed committing the todo
System.err.println("This application cannot add the todo");
} try {
tl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void retrieveToDos() {
// Get all todos due today, for a listing screen
PIM pim = PIM.getInstance();
ToDoList tl = null;
ToDo search_template = null;
Enumeration todos = null; try {
tl = (ToDoList) pim.openPIMList(PIM.TODO_LIST, PIM.READ_ONLY);
} catch (PIMException e) {
// failed opening the default todo list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request for
// read access to todo list
// This is not an error condition and can be normal
System.out.println("Okay, this application won't get todo items"); return;
}
// calculate today's start and end times
Calendar start_of_day = Calendar.getInstance();
start_of_day.set(Calendar.HOUR_OF_DAY, 0);
Calendar end_of_day = Calendar.getInstance();
end_of_day.set(Calendar.HOUR_OF_DAY, 24); try {
// Get the enumeration of matching elements
todos = tl.items(
ToDo.DUE, start_of_day.getTime().getTime(),
end_of_day.getTime().getTime());
} catch (PIMException e) {
// failed to retrieve elements due to error!
// Error case - abort this attempt
System.err.println(
"This application cannot retrieve the todos");
} try {
tl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void modifyToDos() {
// Mark all stuff from yesterday as completed
PIM pim = PIM.getInstance();
ToDoList tl = null;
ToDo search_template = null;
Enumeration todos = null; try {
tl = (ToDoList) pim.openPIMList(PIM.TODO_LIST, PIM.READ_ONLY);
} catch (PIMException e) {
// failed opening the default todo list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request for
// read access to todo list
// This is not an error condition and can be normal
System.out.println("Okay, this application won't get todo items"); return;
}
// calculate today's start and end times
Calendar start_of_day = Calendar.getInstance();
start_of_day.set(Calendar.HOUR_OF_DAY, 0);
Calendar end_of_day = Calendar.getInstance();
end_of_day.set(Calendar.HOUR_OF_DAY, 24); try {
// Get the enumeration of matching elements
todos = tl.items(
ToDo.DUE, start_of_day.getTime().getTime() - 86400000,
end_of_day.getTime().getTime() - 86400000);
} catch (PIMException e) {
// failed to retrieve elements due to error!
// Error case - abort this attempt
System.err.println("This application cannot retrieve the todos");
}
// set all todos due yesterday to completed
//with updated completion date while (todos != null && todos.hasMoreElements()) {
ToDo t = (ToDo) todos.nextElement(); if (tl.isSupportedField(ToDo.COMPLETED))
t.setBoolean(ToDo.COMPLETED, 0, PIMItem.ATTR_NONE, true); if (tl.isSupportedField(ToDo.COMPLETION_DATE))
t.setDate(ToDo.COMPLETION_DATE, 0,
PIMItem.ATTR_NONE, (new Date()).getTime()); try {
// save change to the database
t.commit();
} catch (PIMException exe) {
// Oops couldn't save the data...
System.err.println(
"This application cannot commit the todo");
}
} try {
tl.close();
} catch (PIMException e) {
// failed to close the list
}
} public void deleteToDos() {
// Delete all ToDos having to do with cleaning (hired a maid instead)
PIM pim = PIM.getInstance();
ToDoList tl = null;
Enumeration todos = null; try {
tl = (ToDoList) pim.openPIMList(PIM.TODO_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// failed opening the default todo list!
System.err.println(
"Error accessing database - aborting action"); return;
} catch (SecurityException e) {
// user rejected application's request
// for read/write access to todo list
// This is not an error condition and can be normal
System.out.println(
"Okay, this application won't modify any todo"); return;
} try {
// Get the enumeration of matching elements
todos = tl.items("clean");
} catch (PIMException e) {
// failed retrieving the items enumeration due to an error
System.err.println(
"This application cannot retrieve the todos"); return;
}
// delete all event entries containing 'clean' while (todos != null && todos.hasMoreElements()) {
ToDo t = (ToDo) todos.nextElement(); try {
tl.removeToDo(t);
} catch (PIMException exe) {
// couldn't delete the entry for some reason
System.err.println(
"This application cannot remove the todo info");
}
} try {
tl.close();
} catch (PIMException e) {
// failed to close the list
}
}
}