package cn.sharp.android.ncr.display;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import cn.sharp.android.ncr.MessageId;
import cn.sharp.android.ncr.R;
import cn.sharp.android.ncr.display.domain.Address;
import cn.sharp.android.ncr.display.domain.BaseDomain;
import cn.sharp.android.ncr.display.domain.Email;
import cn.sharp.android.ncr.display.domain.Organization;
import cn.sharp.android.ncr.display.domain.Phone;
import cn.sharp.android.ncr.display.domain.Url;
import cn.sharp.android.ncr.ocr.OCRItems;
public class ContactPerson {
private final static String TAG = "ContactPerson";
public final static int ITEM_NAME = 1;
public final static int ITEM_PHONE = 2;
public final static int ITEM_EMAIL = 3;
public final static int ITEM_ADDRESS = 4;
public final static int ITEM_ORG = 5;
public final static int ITEM_URL = 6;
public final static int ITEM_NOTE = 7;
public final static int TYPE_HOME = 1;
public final static int TYPE_WORK = 2;
public final static int TYPE_OTHER = 3;
public final static int TYPE_PAGER = 4;
public final static int TYPE_MOBILE = 5;
public final static int TYPE_FAX_HOME = 6;
public final static int TYPE_FAX_WORK = 7;
private long groupId;
private String name;
private List<Email> emails;
private List<Phone> phones;
private List<Organization> orgs;
private List<Address> addresses;
private List<Url> urls;
private String note;
public String typeHome, typeWork, typeOther, typePager, typeMobile,
typeFaxHome, typeFaxWork;
private int[] supportedItemTypes1;
private int[] supportedItemTypes2;
private int[] supportedItemTypes3;
private String[] supportedItemTypeStr1;
private String[] supportedItemTypeStr2;
private String[] supportedItemTypeStr3;
private OnSelectItemType onSelectItemType;
private ContentResolver contentResolver;
private SelectItemTypeListener onSelectTypeListener;
private RemoveItemListener removeItemListener;
private EditText nameView, noteView;
private ViewGroup phoneViewGroup, emailViewGroup, addressViewGroup,
orgViewGroup, urlViewGroup;
private Button itemType;
private EditText itemValue, itemValue2;
private ImageButton removeItem;
private LayoutInflater inflater;
/**
* remove all Context reference, or it will result in memory leak
*/
public void removeAllContextObject() {
onSelectItemType = null;
contentResolver = null;
onSelectTypeListener = null;
removeItemListener = null;
nameView = null;
noteView = null;
phoneViewGroup = emailViewGroup = addressViewGroup = orgViewGroup = urlViewGroup = null;
inflater = null;
itemType = null;
itemValue = null;
itemValue2 = null;
removeItem = null;
}
public void registerNewContext(Context context) {
init(context);
}
private void initPrimitiveObj() {
groupId = -1;
phones = new ArrayList<Phone>();
orgs = new ArrayList<Organization>();
emails = new ArrayList<Email>();
addresses = new ArrayList<Address>();
urls = new ArrayList<Url>();
supportedItemTypes1 = new int[] { TYPE_HOME, TYPE_WORK, TYPE_MOBILE,
TYPE_OTHER, TYPE_PAGER, TYPE_FAX_HOME, TYPE_FAX_WORK };
supportedItemTypes2 = new int[] { TYPE_WORK, TYPE_OTHER };
supportedItemTypes3 = new int[] { TYPE_WORK, TYPE_HOME, TYPE_OTHER };
supportedItemTypeStr1 = new String[] { typeHome, typeWork, typeMobile,
typeOther, typePager, typeFaxHome, typeFaxWork };
supportedItemTypeStr2 = new String[] { typeWork, typeOther };
supportedItemTypeStr3 = new String[] { typeWork, typeHome, typeOther };
onSelectTypeListener = new SelectItemTypeListener();
removeItemListener = new RemoveItemListener();
}
private void init(Context context) {
inflater = LayoutInflater.from(context);
contentResolver = context.getContentResolver();
Resources resources = context.getResources();
typeHome = resources.getString(R.string.type_home);
typeWork = resources.getString(R.string.type_work);
typeOther = resources.getString(R.string.type_other);
typePager = resources.getString(R.string.type_pager);
typeMobile = resources.getString(R.string.type_mobile);
typeFaxHome = resources.getString(R.string.type_fax_home);
typeFaxWork = resources.getString(R.string.type_fax_work);
}
public ContactPerson(Context context) {
init(context);
initPrimitiveObj();
}
public ContactPerson(Context context, OCRItems items) {
init(context);
initPrimitiveObj();
if (items.name != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < items.name.length; i++) {
sb.append(items.name[i]);
}
name = sb.toString();
}
if (items.fax != null) {
for (int i = 0; i < items.fax.length; i++) {
Phone phone = new Phone();
phone.type = TYPE_FAX_WORK;
phone.value = items.fax[i];
phones.add(phone);
}
}
if (items.telephone != null) {
for (int i = 0; i < items.telephone.length; i++) {
Phone phone = new Phone();
phone.type = TYPE_MOBILE;
phone.value = items.telephone[i];
phones.add(phone);
}
}
if (items.cellphone != null) {
for (int i = 0; i < items.cellphone.length; i++) {
Phone phone = new Phone();
phone.type = TYPE_MOBILE;
phone.value = items.cellphone[i];
phones.add(phone);
}
}
if (items.organization != null) {
for (int i = 0; i < items.organization.length; i++) {
Organization org = new Organization();
org.company = items.organization[i];
org.type = TYPE_WORK;
if (items.department != null && items.department.length > i) {
org.company += "," + items.department[i];
}
if (items.title != null && items.title.length > i) {
org.title = items.title[i];
} else {
org.title = "";
}
orgs.add(org);
}
/**
* add the remained items.department fields
*/
if (items.department != null) {
for (int i = items.organization.length; i < items.department.length; i++) {
Organization org = new Organization();
org.company = items.department[i];
org.type = TYPE_WORK;
if (items.title != null && items.title.length > i) {
org.title = items.title[i];
} else {
org.title = "";
}
orgs.add(org);
}
}
} else if (items.department != null) {
/**
* if items.orgnization==null&&items.department!=null, treat
* items.department as org.company value
*/
for (int i = 0; i < items.department.length; i++) {
Organization org = new Organization();
org.company = items.department[i];
org.type = TYPE_WORK;
if (items.title != null && items.title.length > i) {
org.title = items.title[i];
} else {
org.title = "";
}
orgs.add(org);
}
}
if (items.email != null) {
for (int i = 0; i < items.email.length; i++) {
Email email = new Email();
email.type = TYPE_WORK;
email.value = items.email[i];
emails.add(email);
}
}
if (items.address != null) {
for (int i = 0; i < items.address.length; i++) {
Address address = new Address();
address.type = TYPE_WORK;
address.value = items.address[i];
addresses.add(address);
if (items.postcode != null && items.postcode.length > i) {
address.postcode = items.postcode[i];
} else {
address.postcode = "";
}
}
}
if (items.url != null) {
for (int i = 0; i < items.url.length; i++) {
Url url = new Url();
url.type = TYPE_WORK;
url.value = items.url[i];
urls.add(url);
}
}
}
/**
* bind the data to corresponding views
*
* @param nameView
* @param phoneViewG