Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Access Address Book contacts

by Retired ‎02-16-2010 01:13 PM - edited ‎09-16-2010 02:57 PM (11,837 Views)

Summary

 

This article applies to the following:

 

  • BlackBerry® smartphones based on Java® technology


Procedure


BlackBerry smartphone applications can make use of contacts stored in the Address Book. The following sample application gives a listing of all contacts in the Address Book and then displays all information for a selected contact.

 

Note: This example uses the BlackBerryContact interface that is an extension of Contact. It is possible to use the Contact interface if you do not require access to any BlackBerry smartphone specific fields that include four user-defined fields (string values) and a personal identification number (PIN) field (hexadecimal number).

 

 

/**
* ViewContact.java
*
* Contact Access Sample
*/

package com.samples.viewContact;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

public final class ViewContact extends UiApplication
{
private ContactsScreen contactsScreen;

public static void main(String[] args)
{
new ViewContact().enterEventDispatcher();
}

private ViewContact()
{
contactsScreen = new ContactsScreen();
pushScreen(contactsScreen);
}
}

/**
* ContactsScreen.java
*
* This screen displays a list of contacts
*
*/

package com.samples.viewContact;

import java.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import net.rim.blackberry.api.pdap.*;

import javax.microedition.pim.*;

import net.rim.device.api.system.Display;

public final class ContactsScreen
extends MainScreen implements ListFieldCallback
{
private ListField listField;
private ContactList blackBerryContactList;
private BlackBerryContact blackBerryContact;
private Vector blackBerryContacts;
private SelectContact selectContact;

//Passes the selected contact to the ContactDetailScreen class.
private class SelectContact extends MenuItem
{
public SelectContact()
{
super("View Contact", 100000, 10);
}

public void run()
{
int index = listField.getSelectedIndex();
if (index != -1 && !blackBerryContacts.isEmpty())
{
blackBerryContact =
(BlackBerryContact)blackBerryContacts.
elementAt(listField.getSelectedIndex());

ContactDetailsScreen contactDetailsScreen =
new ContactDetailsScreen(blackBerryContact);

UiApplication.getUiApplication().
pushScreen(contactDetailsScreen);
}
}
}

//Constructor, sets up the screen.
public ContactsScreen()
{
selectContact = new SelectContact();
setTitle(new LabelField
( "Contacts",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH
));

listField = new ListField();
listField.setCallback(this);
add(listField);

reloadContactList();
}

//Reloads the list of contacts from the
//BlackBerry Contacts database.
private boolean reloadContactList()
{
try {
blackBerryContactList =
(ContactList)PIM.getInstance().openPIMList

(PIM.CONTACT_LIST, PIM.READ_ONLY);

Enumeration allContacts = blackBerryContactList.items();
blackBerryContacts = enumToVector(allContacts);
listField.setSize(blackBerryContacts.size());

return true;
}
catch (PIMException e)
{
return false;
}
}

//Convert the list of contacts from an Enumeration to a Vector
private Vector enumToVector(Enumeration contactEnum) {
Vector v = new Vector();

if (contactEnum == null)
return v;

while (contactEnum.hasMoreElements()) {
v.addElement(contactEnum.nextElement());
}

return v;
}

//ListFieldCallback methods
public void drawListRow(
ListField fieldVar,
Graphics graphics,
int index, int y, int width
){
if ( listField == fieldVar
&& index < blackBerryContacts.size())
{
BlackBerryContact item =
(BlackBerryContact)blackBerryContacts.elementAt(index);
String displayName = getDisplayName(item);

graphics.drawText(displayName, 0, y, 0, width);
}
}

public Object get(ListField fieldVar, int index)
{
if (listField == fieldVar)
{
//If index is out of bounds an exception will be thrown,
//but that's the behaviour we want in that case.
return blackBerryContacts.elementAt(index);
}
return null;
}

public int getPreferredWidth(ListField fieldVar)
{
//use all the width of the current LCD
return Display.getWidth();
}

public int indexOfList
(ListField fieldVar, String prefix, int start)
{
return -1; // not implemented
}

//Setup the menu.
protected void makeMenu(Menu menu, int instance)
{
menu.add(selectContact);

super.makeMenu(menu, instance);
}

/**
* Returns the name to be displayed for a given Contact
*
* @param contact The Contact for which to extract the display name
* @return The name to be displayed in the list field
*/
public static String getDisplayName(Contact contact) {
if (contact == null) {
return null; }

String displayName = null;

// First, see if there is a meaningful name set for the contact.
if (contact.countValues(Contact.NAME) > 0) {
final String[] name = contact.getStringArray(Contact.NAME, 0);
final String firstName = name[Contact.NAME_GIVEN];
final String lastName = name[Contact.NAME_FAMILY];
if (firstName != null && lastName != null) {
displayName = firstName + " " + lastName;
} else if (firstName != null) {
displayName = firstName;
} else if (lastName != null) {
displayName = lastName;
}

if (displayName != null) {
final String namePrefix = name[Contact.NAME_PREFIX];
if (namePrefix != null) {
displayName = namePrefix + " " + displayName;
}
return displayName;
}
}

// If not, use the company name.
if (contact.countValues(Contact.ORG) > 0) {
final String companyName = contact.getString(Contact.ORG, 0);
if (companyName != null) {
return companyName;
}
}
return displayName;
}
}

/**
* ContactDetailsScreen.java
*
* This screen displays the details of a single contact.
*
*/

package com.samples.viewContact;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;
import net.rim.blackberry.api.pdap.*;

public final class ContactDetailsScreen extends MainScreen
{
//BlackBerryContact extends Contact.
public ContactDetailsScreen(BlackBerryContact bbContact)
{
super();

int fieldCount, atrCount;
//Array to store the name values.
String[] name = new String[3];

//Array to store the address values.
String[] address = new String[6];

BasicEditField[] detailFields = new BasicEditField[32];
VerticalFieldManager detailsList = new VerticalFieldManager();

//Set the screen title.
LabelField title = new LabelField
("Contact Details", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);

//Setup the fields to hold the data.
detailFields[0] = new BasicEditField
("Salutation: ", "", 50, BasicEditField.READONLY);
detailFields[1] = new BasicEditField
("First: ", "", 50, BasicEditField.READONLY);
detailFields[2] = new BasicEditField
("Last: ", "", 50, BasicEditField.READONLY);
detailFields[3] = new BasicEditField
("Email: ", "", 50, BasicEditField.READONLY);
detailFields[4] = new BasicEditField
("Company: ", "", 50, BasicEditField.READONLY);
detailFields[5] = new BasicEditField
("Title: ", "", 50, BasicEditField.READONLY);
detailFields[6] = new BasicEditField
("Work: ", "", 50, BasicEditField.READONLY);
detailFields[7] = new BasicEditField
("Work 2: ", "", 50, BasicEditField.READONLY);
detailFields[8] = new BasicEditField
("Home: ", "", 50, BasicEditField.READONLY);
detailFields[9] = new BasicEditField
("Home 2: ", "", 50, BasicEditField.READONLY);
detailFields[10] = new BasicEditField
("Mobile: ", "", 50, BasicEditField.READONLY);
detailFields[11] = new BasicEditField
("Pager: ", "", 50, BasicEditField.READONLY);
detailFields[12] = new BasicEditField
("Fax: ", "", 50, BasicEditField.READONLY);
detailFields[13] = new BasicEditField
("PIN: ", "", 50, BasicEditField.READONLY);
detailFields[14] = new BasicEditField
("Home Address1: ", "", 50, BasicEditField.READONLY);
detailFields[15] = new BasicEditField
("Home Address2: ", "", 50, BasicEditField.READONLY);
detailFields[16] = new BasicEditField
("Home City: ", "", 50, BasicEditField.READONLY);
detailFields[17] = new BasicEditField
("Home State/Prov: ", "", 50, BasicEditField.READONLY);
detailFields[18] = new BasicEditField
("Home Zip/Postal Code: ", "", 50, BasicEditField.READONLY);
detailFields[19] = new BasicEditField
("Home Country: ", "", 50, BasicEditField.READONLY);
detailFields[20] = new BasicEditField
("Work Address1: ", "", 50, BasicEditField.READONLY);
detailFields[21] = new BasicEditField
("Work Address2: ", "", 50, BasicEditField.READONLY);
detailFields[22] = new BasicEditField
("Work City: ", "", 50, BasicEditField.READONLY);
detailFields[23] = new BasicEditField
("Work State/Prov: ", "", 50, BasicEditField.READONLY);
detailFields[24] = new BasicEditField
("Work Zip/Postal Code: ", "", 50, BasicEditField.READONLY);
detailFields[25] = new BasicEditField
("Work Country: ", "", 50, BasicEditField.READONLY);
detailFields[26] = new BasicEditField
("User1: ", "", 50, BasicEditField.READONLY);
detailFields[27] = new BasicEditField
("User2: ", "", 50, BasicEditField.READONLY);
detailFields[28] = new BasicEditField
("User3: ", "", 50, BasicEditField.READONLY);
detailFields[29] = new BasicEditField
("User4: ", "", 50, BasicEditField.READONLY);
detailFields[30] = new BasicEditField
("Notes: ", "", 50, BasicEditField.READONLY);
detailFields[31] = new BasicEditField
("Anniversary : ", "", 50, BasicEditField.READONLY);


//Get an array of all populated contact fields.
int fieldsWithData[] = bbContact.getFields();

//Loop through all populated contact fields.
for ( fieldCount = 0;
fieldCount < fieldsWithData.length;
++fieldCount)
{
//Populate the appropriate BasicEditfield.
switch (fieldsWithData[fieldCount])
{
case BlackBerryContact.NAME:
//Verify that the contact has a name.
if (bbContact.countValues(BlackBerryContact.NAME) > 0)
{
//Retrieve the name fields in a string array.
name = bbContact.getStringArray(BlackBerryContact.NAME, 0);

//Populate the name fields with
//their values from the contact.
detailFields[0].setText
(name[BlackBerryContact.NAME_PREFIX]);
detailFields[1].setText
(name[BlackBerryContact.NAME_GIVEN]);
detailFields[2].setText
(name[BlackBerryContact.NAME_FAMILY]);
}
break;

case BlackBerryContact.EMAIL:
detailFields[3].setText(bbContact.getString
(BlackBerryContact.EMAIL, 0));
break;

case BlackBerryContact.ORG:
detailFields[4].setText
(bbContact.getString(BlackBerryContact.ORG, 0));
break;

case BlackBerryContact.TITLE:
detailFields[5].setText
(bbContact.getString(BlackBerryContact.TITLE, 0));
break;

//Extract all telephone numbers and
//display them in the appropriate field.
case BlackBerryContact.TEL:
for (atrCount = 0;
atrCount < bbContact.countValues(BlackBerryContact.TEL);
++atrCount)
{
switch(bbContact.getAttributes(BlackBerryContact.TEL, atrCount))
{
case BlackBerryContact.ATTR_WORK:
detailFields[6].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_WORK2:
detailFields[7].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_HOME:
detailFields[8].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_HOME2:
detailFields[9].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_MOBILE:
detailFields[10].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_PAGER:
detailFields[11].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;

case BlackBerryContact.ATTR_FAX:
detailFields[12].setText(bbContact.getString
(BlackBerryContact.TEL, atrCount));
break;
}
}
break;

case BlackBerryContact.PIN:
detailFields[13].setText(bbContact.getString
(BlackBerryContact.PIN, 0));
break;

case BlackBerryContact.ADDR:
//Extract all addresses and display them in the appropriate field.
for (atrCount = 0; atrCount < bbContact.countValues
(BlackBerryContact.ADDR); ++atrCount)
{
//Extract the address fields in a string array.
address = bbContact.getStringArray
(BlackBerryContact.ADDR, atrCount);
if (bbContact.getAttributes
(BlackBerryContact.ADDR, atrCount) ==
BlackBerryContact.ATTR_HOME)
{
//We have a home address.
detailFields[14].setText
(address[BlackBerryContact.ADDR_STREET]);
detailFields[15].setText
(address[BlackBerryContact.ADDR_EXTRA]);
detailFields[16].setText
(address[BlackBerryContact.ADDR_LOCALITY]);
detailFields[17].setText
(address[BlackBerryContact.ADDR_REGION]);
detailFields[18].setText
(address[BlackBerryContact.ADDR_POSTALCODE]);
detailFields[19].setText
(address[BlackBerryContact.ADDR_COUNTRY]);
}
else if (bbContact.getAttributes
(BlackBerryContact.ADDR, atrCount) ==
BlackBerryContact.ATTR_WORK)
{
//We have a work address.
detailFields[20].setText(
address[BlackBerryContact.ADDR_STREET]);
detailFields[21].setText
(address[BlackBerryContact.ADDR_EXTRA]);
detailFields[22].setText
(address[BlackBerryContact.ADDR_LOCALITY]);
detailFields[23].setText
(address[BlackBerryContact.ADDR_REGION]);
detailFields[24].setText
(address[BlackBerryContact.ADDR_POSTALCODE]);
detailFields[25].setText
(address[BlackBerryContact.ADDR_COUNTRY]);
}
}
break;

case BlackBerryContact.USER1:
detailFields[26].setText(bbContact.getString
(BlackBerryContact.USER1, 0));
break;

case BlackBerryContact.USER2:
detailFields[27].setText(bbContact.getString
(BlackBerryContact.USER2, 0));
break;

case BlackBerryContact.USER3:
detailFields[28].setText(bbContact.getString
(BlackBerryContact.USER3, 0));
break;

case BlackBerryContact.USER4:
detailFields[29].setText(bbContact.getString
(BlackBerryContact.USER4, 0));
break;

case BlackBerryContact.NOTE:
detailFields[30].setText(bbContact.getString
(BlackBerryContact.NOTE, 0));
break;

case BlackBerryContact.ANNIVERSARY:
detailFields[31].setText(bbContact.getString
(BlackBerryContact.NOTE, 0));
break;
}
}

//Add all of the BasicEditFields to the VerticalFieldManager.
for (fieldCount = 0; fieldCount < 31; ++fieldCount)
{
detailsList.add(detailFields[fieldCount]);
}

//Add the VerticalFieldManager to the screen.
add(detailsList);
}
}

 

Contributors
Users Online
Currently online: 7 members 900 guests
Please welcome our newest community members: