package me.yoqi.deleteallcontacts.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

/**
 * 黑莓联系人管理，与 android 联系人管理不一样。
 * 
 * @author liuyuqi
 *
 */
public class BlackBerryContactManager {
	ContentResolver resolver;
	static String TAG = "me.yoqi.deleteallcontacts.utils.ContactManager";

	public BlackBerryContactManager(Context context) {
		resolver = context.getContentResolver();
	}

	/**
	 * 查询所有联系人
	 */
	public ArrayList<String> getAllContact() {
		ArrayList<String> res = new ArrayList<String>();
		Uri uri = ContactsContract.Data.CONTENT_URI;
		Cursor cursorUser = resolver.query(uri,
				new String[] { ContactsContract.CommonDataKinds.Phone._ID,
						ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
						ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID },
				null, null, null);

		while (cursorUser.moveToNext()) {
			int id = cursorUser.getInt(0); // 按上面数组的声明顺序获取
			String name = cursorUser.getString(1);
			int rawContactsId = cursorUser.getInt(2);
			res.add("id:" + id + " ,name:" + name + " ,rawContacntsID:" + rawContactsId + "\r\n");
		}
		return res;
	}

	public void deleteAllContacts() throws UnsupportedEncodingException, Exception {
//		ContactList contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
//		Enumeration e = contactList.items();
//		while (e.hasMoreElements()) {
//			Contact c = (Contact) e.nextElement();
//			contactList.removeContact(c);
//		}
	}
}
