Blackberry provides PIMListener to get notification regarding changes in the PIMItems. Following is a library class created to get notification regarding changes in contact, task, appointment and/or memo on blackberry device.
1. Create object of BBPIMListener.
2. Start listening for specified PIM types by calling method 'fnAddListener()'. Multiple types can be specified by using | (OR) operator. e.g. fnAddListener(BBPIMListener.CONTACT_LIST | BBPIMListener.MEMO_LIST)
3. Override methods to provide specific functionality. e.g. override method 'fnOnContactAdded()' to specify what has to be done on contact addition.
Here is code:
import net.rim.blackberry.api.pdap.*;
import javax.microedition.pim.*;
import java.util.*;
/**
* Library class for listening changes in contact list, task list, appointments
* and/or memos.
*
* @author Dipak
*/
public class BBPIMListener implements PIMListListener
{
public final int CONTACT_LIST = 1;
public final int TASK_LIST = 2;
public final int APPOINTMENT_LIST = 4;
public final int MEMO_LIST = 8;
public final int ADD = 1;
public final int DELETE = 2;
public final int UPDATE = 4;
private BlackBerryPIMList m_oContactList;
private BlackBerryPIMList m_oToDoList;
private BlackBerryPIMList m_oEventList;
private BlackBerryPIMList m_oMemoList;
/**
* Adds listener for specified PIM types and gives information related to
* changes in those.
* @param nType List type; one of CONTACT_LIST, TASK_LIST, APPOINTMENT_LIST,
* MEMO_LIST
*/
public void fnAddListener(int nType)
{
try
{
PIM oPIM = PIM.getInstance();
if(CONTACT_LIST == (nType & CONTACT_LIST))
{
// Contact listener
m_oContactList = (BlackBerryPIMList)oPIM.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
m_oContactList.addListener(this);
}
if(TASK_LIST == (nType & TASK_LIST))
{
// Task listener
m_oToDoList = (BlackBerryPIMList)oPIM.openPIMList(PIM.TODO_LIST, PIM.READ_WRITE);
m_oToDoList.addListener(this);
}
if(APPOINTMENT_LIST == (nType & APPOINTMENT_LIST))
{
// Calendar listener
m_oEventList = (BlackBerryPIMList)oPIM.openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE);
m_oEventList.addListener(this);
}
if(MEMO_LIST == (nType & MEMO_LIST))
{
// Memo listener
m_oMemoList = (BlackBerryMemoList)oPIM.openPIMList(BlackBerryPIM.MEMO_LIST, PIM.READ_WRITE);
m_oMemoList.addListener(this);
}
}
catch(PIMException e)
{
e.printStackTrace();
}
}
/**
* Removes listener added to the list specified by the user.
*/
public void fnRemoveListener()
{
if(null != m_oContactList)
{
m_oContactList.removeListener(this);
}
if(null != m_oToDoList)
{
m_oToDoList.removeListener(this);
}
if(null != m_oEventList)
{
m_oEventList.removeListener(this);
}
if(null != m_oMemoList)
{
m_oMemoList.removeListener(this);
}
}
// Occurs when an item is added to the PIM list.
public void itemAdded(PIMItem oItem)
{
fnNotifyPIMChange(ADD, null, oItem);
}
// Occurs when an item is removed from the PIM list.
public void itemRemoved(PIMItem oItem)
{
fnNotifyPIMChange(DELETE, null, oItem);
}
// Occurs when an item is updated within the PIM list.
public void itemUpdated(PIMItem oOldItem, PIMItem oNewItem)
{
fnNotifyPIMChange(UPDATE, oOldItem, oNewItem);
}
private void fnNotifyPIMChange(int nOperation, PIMItem oOldItem, PIMItem oNewItem)
{
if(oNewItem instanceof Contact)
{
switch(nOperation)
{
case ADD:
{
fnOnContactAdded((Contact)oNewItem);
break;
}
case DELETE:
{
fnOnContactDeleted((Contact)oNewItem);
break;
}
case UPDATE:
{
fnOnContactUpdated((Contact)oOldItem, (Contact)oNewItem);
break;
}
}
}
else if(oNewItem instanceof ToDo)
{
switch(nOperation)
{
case ADD:
{
fnOnTaskAdded((ToDo)oNewItem);
break;
}
case DELETE:
{
fnOnTaskDeleted((ToDo)oNewItem);
break;
}
case UPDATE:
{
fnOnTaskUpdated((ToDo)oOldItem, (ToDo)oNewItem);
break;
}
}
}
else if(oNewItem instanceof Event)
{
switch(nOperation)
{
case ADD:
{
fnOnAppointmentAdded((Event)oNewItem);
break;
}
case DELETE:
{
fnOnAppointmentDeleted((Event)oNewItem);
break;
}
case UPDATE:
{
fnOnAppointmentUpdated((Event)oOldItem, (Event)oNewItem);
break;
}
}
}
else if(oNewItem instanceof BlackBerryMemo)
{
switch(nOperation)
{
case ADD:
{
fnOnMemoAdded((BlackBerryMemo)oNewItem);
break;
}
case DELETE:
{
fnOnMemoDeleted((BlackBerryMemo)oNewItem);
break;
}
case UPDATE:
{
fnOnMemoUpdated((BlackBerryMemo)oOldItem, (BlackBerryMemo)oNewItem);
break;
}
}
}
}
/**
* Occurs when contact is added to contact list.
* @param oItem Contact that is added to the contact list.
*/
public void fnOnContactAdded(Contact oContact)
{
}
/**
* Occurs when Contact is deleted from contact list.
* @param oItem Contact that is deleted from the contact list.
*/
public void fnOnContactDeleted(Contact oContact)
{
}
/**
* Occurs when Contact is updated in contact list.
* @param oOldItem State of Contact before update.
* @param oNewItem New state of Contact as a result of update.
*/
public void fnOnContactUpdated(Contact oOldContact, Contact oNewContact)
{
}
/**
* Occurs when ToDo(i.e. task) is added to task list.
* @param oItem ToDo(i.e. task) that is added to the task list.
*/
public void fnOnTaskAdded(ToDo oToDo)
{
}
/**
* Occurs when ToDo(i.e. task) is deleted from task list.
* @param oItem ToDo(i.e. task) that is deleted from the task list.
*/
public void fnOnTaskDeleted(ToDo oToDo)
{
}
/**
* Occurs when ToDo(i.e. task) is updated in task list.
* @param oOldItem State of ToDo(i.e. task) before update.
* @param oNewItem New state of ToDo(i.e. task) as a result of update.
*/
public void fnOnTaskUpdated(ToDo oOldToDo, ToDo oNewToDo)
{
}
/**
* Occurs when Event(i.e. appointment) is added to appointment list.
* @param oItem Event(i.e. appointment) that is added to the appointment list.
*/
public void fnOnAppointmentAdded(Event oEvent)
{
}
/**
* Occurs when Event(i.e. appointment) is deleted from appointment list.
* @param oItem Event(i.e. appointment) that is deleted from the appointment list.
*/
public void fnOnAppointmentDeleted(Event oEvent)
{
}
/**
* Occurs when Event(i.e. appointment) is updated in appointment list.
* @param oOldItem State of Event(i.e. appointment) before update.
* @param oNewItem New state of Event(i.e. appointment) as a result of update.
*/
public void fnOnAppointmentUpdated(Event oOldEvent, Event oNewEvent)
{
}
/**
* Occurs when BlackBerryMemo(i.e. memo) is added to memo list.
* @param oItem BlackBerryMemo(i.e. memo) that is added to the memo list.
*/
public void fnOnMemoAdded(BlackBerryMemo oBlackBerryMemo)
{
}
/**
* Occurs when BlackBerryMemo(i.e. memo) is deleted from memo list.
* @param oItem BlackBerryMemo(i.e. memo) that is deleted from the memo list.
*/
public void fnOnMemoDeleted(BlackBerryMemo oBlackBerryMemo)
{
}
/**
* Occurs when BlackBerryMemo(i.e. memo) is updated in memo list.
* @param oOldItem State of BlackBerryMemo(i.e. memo) before update.
* @param oNewItem New state of BlackBerryMemo(i.e. memo) as a result of update.
*/
public void fnOnMemoUpdated(BlackBerryMemo oOldBlackBerryMemo, BlackBerryMemo oNewBlackBerryMemo)
{
}
}