Wednesday, April 9, 2008

Adding contacts using JSR 75

Following code snippets show how to add contacts using JSR 75.

PIMWrite class
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class PIMWrite extends MIDlet implements CommandListener
{
private Command PIMWriteCmd,exitCommand,generateName;
PIMImpl pimImpl;
private static Random random = new Random();
static String[] names;

public static Display display;
public static Form result;
private String sp,si,s1;
private String[] strJSR = new String[12];
private long l;
int noOfContacts;

TextField textfield = new TextField("Enter No of Contacts: ","",3,TextField.NUMERIC);


/*Constructs an Object
*/

public PIMWrite() {

display = Display.getDisplay(this);

PIMWriteCmd = new Command("PIMWrite", Command.SCREEN, 0);


exitCommand = new Command("Exit", Command.SCREEN, 0);

result=new Form("Write Contacts To PIM");
result.append(textfield);
System.out.println(" noOfContacts: "+noOfContacts);

result.addCommand(PIMWriteCmd);
result.addCommand(exitCommand);
result.setCommandListener(this);

}

public void startApp() throws MIDletStateChangeException {
display.setCurrent(result);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}

else if(c == PIMWriteCmd)
{
noOfContacts = Integer.parseInt(textfield.getString());
createConatactName();
System.out.println(" noOfContacts: "+noOfContacts);
//Start the PIM thread and write contacts .
pimImpl = new PIMImpl();
pimImpl.start();


}
}

public void createConatactName()
{
names = new String[noOfContacts];
for (int i = 0; i < style="color: rgb(102, 204, 102);">){
names[i] = PIMWrite.generateName()+" "+PIMWrite.generateName();
System.out.println(" Names :"+ names[i]+ " i "+i);
}
}


public static String generateName()
{
return generateName(4, 7, true);
}


/** This method generates the random names
*
* @param minLength Minimum length of name
* @param maxLength Max length of name
* @param capitalize Captilize
* @return name
*/

public static String generateName(int minLength, int maxLength, boolean capitalize)
{

int limit = (random.nextInt(maxLength - minLength + 1)) + minLength;
StringBuffer s = new StringBuffer(limit);
for (int i = 0; i < style="color: rgb(102, 204, 102);">)
{
char c = (char)((random.nextInt(26)) + 'a');
s.append(c);
}
if (capitalize && (s.length() > 0)) s.setCharAt(0, Character.toUpperCase(s.charAt(0)));
return s.toString();
}
}
PIM Implementation class

/**
* PIM Implementation class
*/

import javax.microedition.pim.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Command;

public class PIMImpl extends Thread
{
static Alert check;

StringItem si;
//----------------------------------------Phone Book---------------------------------

int noOfRecords;
public static boolean pimTemp = false;
Random randPh = new Random();


public PIMImpl()
{

}

public void run()
{
writeData();
}



public synchronized void writeData()
{

String ph[] = new String[PIMWrite.names.length];
for (int i=0;inames.length ;i++ )
{

String r = Long.toString(randPh.nextLong());
if(r.startsWith("-"))
{
r = r.replace('-','9');
}
ph[i] =(r.length()>10)?(r.substring(0,10)):(r);



// System.out.println(" PH :"+ph[i] +"i:"+i);
}


int tc = 0;
PIM pim;
try{

ContactList contacts = null;
ContactList conT = null;
pim = PIM.getInstance();
try {

contacts = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
} catch (PIMException e) {
// An error occurred
return;
}
for(int i=0;inames.length;i++)
{

Contact contact = contacts.createContact();
String[] name = new String[contacts.stringArraySize(Contact.NAME)];

// System.out.println("i = "+i);
if (contacts.isSupportedField(Contact.TEL))
{
contact.addString(Contact.TEL, Contact.ATTR_MOBILE, ph[i]);
}

if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_GIVEN))
name[Contact.NAME_GIVEN] = PIMWrite.names[i];
contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);


try {
contact.commit();
} catch (PIMException e) {
// An error occured
}
PIMWrite.result.deleteAll();
String msg=" Contacts Adding :"+i;
si = new StringItem("",msg);
PIMWrite.result.append(si);

}
check = new Alert("Added","Added ",null,AlertType.CONFIRMATION);
check.setTimeout(Alert.FOREVER);
PIMWrite.display.setCurrent(check);

try {
contacts.close();
} catch (PIMException e) {
}

}catch(Exception e){
String er = e+" ; tc="+tc;
check = new Alert( "Response",er,null,AlertType.CONFIRMATION );
check.setTimeout(Alert.FOREVER);

PIMWrite.display.setCurrent(check);
}
// return true;

}

}

No comments: