Search This Blog

Thursday, July 18, 2013

sim card - How to get all android contacts but without those which are on SIM

sim card - How to get all android contacts but without those which are on SIM - Stack Overflow:


i used the next function to get the contacts  with phone, include/exclude from sim  , in one query
the function is part of big program so you might need to change /add it

// the idea is get the contacts with the phone first
public static List<Contact> getContacts(Context context,
boolean favoritesOnly, boolean removeSimContacts) {

boolean withEmail = true;
List<Contact> contactsList = null;
HashMap<String, Contact> contactsMap = new HashMap<String, Contact>();
String starred = favoritesOnly ? " AND starred=1 " : "";
String sNOSimContacs = removeSimContacts ? "%sim%": "%%";
String filter = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1 " + starred + " AND " ;
if(removeSimContacts)
filter += RawContacts.ACCOUNT_TYPE + " NOT LIKE ? AND ";
else
filter += RawContacts.ACCOUNT_TYPE + " LIKE ? AND ";

filter += Data.MIMETYPE + "=?";

Cursor cursor = null;
try {

String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.STARRED,
ContactsContract.CommonDataKinds.Phone.DATA,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.RawContacts.CONTACT_ID};

cursor = context.getContentResolver().query(Data.CONTENT_URI,
projection,
filter, new String[] { sNOSimContacs,Phone.CONTENT_ITEM_TYPE},
ContactsContract.Contacts.DISPLAY_NAME);

while (cursor != null && cursor.moveToNext()) {

String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));
// if we dont have this contact add it
// if the user have multi numbers we get the same contact here
if(!contactsMap.containsKey(contactId)){
Contact contact;

if(Const.IS_USE_SHARED_PREF_ONLY)
contact = ContactProviderAlternative.getContact(context,
Integer.parseInt(contactId));
else
contact = ContactProvider.getContact(context,
Integer.parseInt(contactId));

// if (favoritesOnly)
// Log.d("Info", "pos: " + contact.getContactPosition());


boolean isFav  = (cursor.getShort(cursor
.getColumnIndex(ContactsContract.Contacts.STARRED)) == 1);

contact.setFavorite(isFav);
contact.setContactId(contactId);
contact.setContactName(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
contact.setContactPhoto(Utils.openPhoto(context,
Long.parseLong(contactId), false));
contact.setContactUri(Integer.parseInt(contactId));
contact.setContactPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
contact.setContactPhoneType(type == 1 ? Const.NEW_CONTACT_PHONE_TYPE_LANDLINE : Const.NEW_CONTACT_PHONE_TYPE_MOBILE);

contactsMap.put(contactId,contact);
}else{
// we have this conatct lets update his phone the latest one we get
Contact contact = contactsMap.get(contactId);
contact.setContactPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)));
int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
contact.setContactPhoneType(type == 1 ? Const.NEW_CONTACT_PHONE_TYPE_LANDLINE : Const.NEW_CONTACT_PHONE_TYPE_MOBILE);
}
}
if(cursor != null){
cursor.close();
cursor = null;
}

contactsList = new ArrayList<Contact>(contactsMap.values());
if(withEmail){
String[] projectionEmail = new String[] {
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.LABEL
};

String where= Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE + "=?";

for(Contact c : contactsList){
//email & phone

String[] params = new String[]{c.getContactId(), Email.CONTENT_ITEM_TYPE};

cursor = context.getContentResolver().query(
Data.CONTENT_URI,
       projectionEmail,
       where,
       params,
       null);

while(cursor != null && cursor.moveToNext()) {
c.setContactEmail(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
    }
if(cursor != null){
cursor.close();
cursor = null;
}

}
}

}

catch (Exception e) {
e.printStackTrace();
}finally{
if(cursor != null)
cursor.close();
}

return contactsList;
}

Yaniv Tzannay

No comments: