Search Wikipedia

Search results

May 1, 2014

Working With Android Contacts (Part 2)

Gluing together (API 2.0 and API 1.6 & Before)

To put this together into an application there are a few glue pieces that need to be setup along with creating classes to manage accessing the data. First we need to create a set of classes to hold the data. Also we'll create a class to handle 2.0 API calls and a class to handle 1.6 and earlier API calls. There's also a wrapper class that determines and loads the proper class.
Contact Data Classes
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
We will create classes to represent the address and email. Most of these classes are mere data storage classes with variables and getter/setters.

ContactList
The ContactList class is a very basic class designed to hold an ArrayList of instances of the Contact class below. We've left this class very plain and ready to be expanded to suit your needs. 

package com.kais.android.ContactAPI.objects;
import java.util.ArrayList;
 public class ContactList {
 private ArrayList<Contact> contacts = new ArrayList<Contact>();
 public ArrayList<Contact> getContacts() {
   return contacts;
  }
 public void setContacts(ArrayList<Contact> contacts) {
   this.contacts = contacts;
  }
  public void addContact(Contact contact) {
   this.contacts.add(contact);
  }
   public ContactList() {
   }
  }

Contact
The Contact class is used to store the details about each contact. There are a series of private class variables to hold this data. Singular data such as name and database ID are stored as strings. Complex data is stored either as an instance or ArrayList of data specific classes. This class is mainly getters and setters with a few methods to add to the internal ArrayLists. 


package com.kais.android.ContactAPI.objects;
 
import java.util.ArrayList;
 
public class Contact {
  private String id;
  private String displayName;
  private ArrayList<Phone> phone;
  private ArrayList<Email> email;
  
 public ArrayList<Email> getEmail() {
   return email;
  }
  public void setEmail(ArrayList<Email> email) {
   this.email = email;
  }
  public void addEmail(Email e) {
   this.email.add(e);
  } 
  public String getId() {
   return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getDisplayName() {
   return displayName;
  }
  public void setDisplayName(String dName) {
   this.displayName = dName;
  }
  public ArrayList<Phone> getPhone() {
   return phone;
  }
  public void setPhone(ArrayList<Phone> phone) {
   this.phone = phone;
  }
  public void addPhone(Phone phone) {
   this.phone.add(phone);
  }
 }

Phone
Class to hold the phone records. 

public class Phone {
  private String number;
  private String type;
  
  public String getNumber() {
   return number;
  }
 public void setNumber(String number) {
   this.number = number;
  }
 public String getType() {
   return type;
  }
  public void setType(String type) {
   this.type = type;
  }
 public Phone(String n, String t) {
   this.number = n;
   this.type = t;
  }
  }

Email
Another getter/setter and data storage class. The email class stores the email address and address type (work, home, etc). 

public class Email {
  private String address;
  private String type;
  public String getAddress() {
   return address;
  }
  public void setAddress(String address) {
   this.address = address;
  }
  public String getType() {
   return type;
  }
  public void setType(String t) {
   this.type = t;
  }
  
  public Email(String a, String t) {
   this.address = a;
   this.type = t;
  }
 }

Wrapper Class

The wrapper class below is what will be invoked by applications. This class will determine the API level running on the device/emulator and load the correct class created on the next pages. To determine the correct Android API running the Build.VERSION.SDK variable is queried. This version code is then compared against the Eclair (2.0) version code stored in Build.VERSION_CODES.ECLAIR. Finally the proper API class is loaded. 

package com.kais.android.ContactAPI;
 
 import android.os.Build;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.database.Cursor;
 import android.net.Uri;
 
 import com.highercollaboration.android.ContactAPI.objects.*;
 public abstract class ContactAPI {
 
  private static ContactAPI api;
  private Cursor cur;
  private ContentResolver cr;
  
  public static ContactAPI getAPI() {
  if (api == null) {
  String apiClass;
        if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.ECLAIR) {
  apiClass = "com.highercollaboration.android.ContactAPI.ContactAPISdk5";
  } else {
  apiClass = "com.highercollaboration.android.ContactAPI.ContactAPISdk3";
   }
  try {
Class<? extends ContactAPI> realClass = Class.forName(apiClass).asSubclass(ContactAPI.class);
  api = realClass.newInstance();
  } catch (Exception e) {
  throw new IllegalStateException(e);
    }
    }
   return api;
  }
  public abstract Intent getContactIntent();
  public abstract ContactList newContactList();
  public abstract Cursor getCur();
  public abstract void setCur(Cursor cur);
  public abstract ContentResolver getCr();
  public abstract void setCr(ContentResolver cr);
  }


2.0 Data Access

This class takes what was covered on page 1 of the tutorial about the Android 2.0 Contact API and turns it into a class. This class extends and will be invoked by the wrapper class created previously. 

package com.kais.android.ContactAPI;
 
 import java.util.ArrayList;
 
 import com.highercollaboration.android.ContactAPI.objects.*; 
 
 import android.content.Intent;
 import android.database.Cursor;
 import android.provider.ContactsContract;
 import android.content.ContentResolver;
 
 public class ContactAPISdk5 extends ContactAPI {
        private Cursor cur;
  private ContentResolver cr;
  
  public Cursor getCur() {
   return cur;
  }
 public void setCur(Cursor cur) {
   this.cur = cur;
  }
 public ContentResolver getCr() {
   return cr;
  }
 public void setCr(ContentResolver cr) {
   this.cr = cr;
  }
 public Intent getContactIntent() {
  return(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI));
  }
  public ContactList newContactList() {
   ContactList contacts = new ContactList();
   String id;
   this.cur = this.cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
   if (this.cur.getCount() > 0) {
      while (cur.moveToNext()) {
        Contact c = new Contact();
        id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        c.setId(id);
 c.setDisplayName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
    c.setPhone(this.getPhoneNumbers(id));
  }
c.setEmail(this.getEmailAddresses(id));
contacts.addContact(c);
}
}
   return(contacts);
 }
public ArrayList<Phone> getPhoneNumbers(String id) {
  ArrayList<Phone> phones = new ArrayList<Phone>();
  Cursor pCur = this.cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    null, 
  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
   while (pCur.moveToNext()) {
    phones.add(new Phone(
      pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
      , pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
    ));
 } 
   pCur.close();
   return(phones);
  }
public ArrayList<Email> getEmailAddresses(String id) {
  ArrayList<Email> emails = new ArrayList<Email>();
  Cursor emailCur = this.cr.query( 
  ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); 
  while (emailCur.moveToNext()) { 
       // This would allow you get several email addresses
  Email e = new Email(emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)),emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))  
   );
    emails.add(e);
   } 
   emailCur.close();
   return(emails);
  }
}


1.X Data Access Class

This class is made up of the code explained covering version 1.x of the Android contact API. As with the 2.0 class this class extends the ContactAPI wrapper class and will be invoked by it if the 1.x version of Android is in use. 

package com.kais.android.ContactAPI;
 
 import java.util.ArrayList; 
 
 import com.higherpass.android.ContactAPI.objects.Address;
 import com.higherpass.android.ContactAPI.objects.Contact;
 import com.higherpass.android.ContactAPI.objects.ContactList;
 import com.higherpass.android.ContactAPI.objects.Email;
 import com.higherpass.android.ContactAPI.objects.IM;
 import com.higherpass.android.ContactAPI.objects.Organization;
 import com.higherpass.android.ContactAPI.objects.Phone; 
 
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.database.Cursor;
 import android.provider.Contacts;
 import android.provider.Contacts.People;
 
 public class ContactAPISdk3 extends ContactAPI {

private Cursor cur;
  private ContentResolver cr;
  
  public Cursor getCur() {
   return cur;
  }
 public void setCur(Cursor cur) {
   this.cur = cur;
  }
 public ContentResolver getCr() {
   return cr;
  }
 public void setCr(ContentResolver cr) {
   this.cr = cr;
  }
 public Intent getContactIntent() {
   return(new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
  }
  public ContactList newContactList() {
   ContactList contacts = new ContactList();
   String id;
     this.cur = this.cr.query(People.CONTENT_URI, 
     null, null, null, null);
   if (this.cur.getCount() > 0) {
    while (cur.moveToNext()) {
     Contact c = new Contact();
     id = cur.getString(cur.getColumnIndex(People._ID));
     c.setId(id);
     c.setDisplayName(cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
     if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
      c.setPhone(this.getPhoneNumbers(id));
     }
     c.setEmail(this.getEmailAddresses(id));
     contacts.addContact(c);
    }
   }
   return(contacts);
  }
public ArrayList<Phone> getPhoneNumbers(String id) {
   ArrayList<Phone> phones = new ArrayList<Phone>();
   
   Cursor pCur = this.cr.query(
     Contacts.Phones.CONTENT_URI, 
     null, 
     Contacts.Phones.PERSON_ID +" = ?", 
     new String[]{id}, null);
   while (pCur.moveToNext()) {
    phones.add(new Phone(
         pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER))
  , pCur.getString(pCur.getColumnIndex(Contacts.PhonesTYPE))
    ));
} 
   pCur.close();
   return(phones);
  }
public ArrayList<Email> getEmailAddresses(String id) {
   ArrayList<Email> emails = new ArrayList<Email>();
   
   Cursor emailCur = this.cr.query( 
     Contacts.ContactMethods.CONTENT_EMAIL_URI, 
     null,
     Contacts.ContactMethods.PERSON_ID + " = ?", 
     new String[]{id}, null); 
   while (emailCur.moveToNext()) { 
       // This would allow you get several email addresses
Email e = new Email(emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.DATA))
 ,emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.CONTENT_EMAIL_TYPE))  
      );
    emails.add(e);
   } 
   emailCur.close();
   return(emails);
  }
}

No comments:

Post a Comment

Did this post help you? Do you have any questions? Drop your thoughts here...

}