Created
June 30, 2021 19:53
-
-
Save NaserKhoshfetrat/dd37fe93b7cca2f404c0e7d9b71316f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.ContentResolver; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.provider.ContactsContract; | |
import android.widget.ListView; | |
import android.widget.SimpleCursorAdapter; | |
import java.util.ArrayList; | |
public class ContactUtils { | |
private static ContactUtils instance; | |
public static ContactUtils getInstance() { | |
if (instance == null) | |
instance = new ContactUtils(); | |
return instance; | |
} | |
public ArrayList<ContactModel> getWhatsappContacts(Activity activity) { | |
ArrayList<ContactModel> contactList = new ArrayList<>(); | |
//This class provides applications access to the content model. | |
ContentResolver cr = activity.getContentResolver(); | |
Uri uriRawContacts = ContactsContract.RawContacts.CONTENT_URI; | |
String[] projectionRawContacts = new String[]{ContactsContract.RawContacts._ID, ContactsContract.RawContacts.CONTACT_ID}; | |
String selectionRawContacts = ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?"; | |
String[] selectionArgsRawContacts = new String[]{"com.whatsapp"}; | |
//RowContacts for filter Account Types | |
Cursor contactCursor = cr.query( | |
uriRawContacts, | |
projectionRawContacts, | |
selectionRawContacts, | |
selectionArgsRawContacts, | |
null); | |
if (contactCursor != null) { | |
if (contactCursor.getCount() > 0) { | |
if (contactCursor.moveToFirst()) { | |
do { | |
//whatsappContactId for get Number,Name,Id ect... from ContactsContract.CommonDataKinds.Phone | |
String whatsappContactId = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID)); | |
if (whatsappContactId != null) { | |
//Get Data from ContactsContract.CommonDataKinds.Phone of Specific CONTACT_ID | |
Uri uriPhone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; | |
String[] projectionPhone = new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; | |
String selectionPhone = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"; | |
String[] selectionArgsPhone = new String[]{whatsappContactId}; | |
Cursor whatsAppContactCursor = cr.query( | |
uriPhone, | |
projectionPhone, | |
selectionPhone, | |
selectionArgsPhone, | |
null); | |
if (whatsAppContactCursor != null) { | |
whatsAppContactCursor.moveToFirst(); | |
String id = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); | |
String name = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); | |
String number = whatsAppContactCursor.getString(whatsAppContactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | |
whatsAppContactCursor.close(); | |
//Add Number to ArrayList | |
//initialize contact model | |
ContactModel model = new ContactModel(); | |
model.setName(name); | |
model.setPhone(number); | |
//add model to array | |
contactList.add(model); | |
} | |
} | |
} while (contactCursor.moveToNext()); | |
contactCursor.close(); | |
} | |
} | |
} | |
return contactList; | |
} | |
public ArrayList<ContactModel> getAllContacts(Activity activity) { | |
ArrayList<ContactModel> contactList = new ArrayList<>(); | |
Uri uriContacts = ContactsContract.Contacts.CONTENT_URI; | |
String sort = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"; | |
Cursor cursor = activity.getContentResolver().query(uriContacts, | |
null, | |
null, | |
null, | |
sort); | |
if (cursor.getCount() > 0) { | |
while (cursor.moveToNext()) { | |
//get contact id | |
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); | |
//get contact name | |
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); | |
//initialize phone uri | |
Uri uriPhone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; | |
//initialize selection | |
String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " =?"; | |
//initialize phone cursor | |
Cursor phoneCursor = activity.getContentResolver().query( | |
uriPhone, | |
null, | |
selection, | |
new String[]{id}, | |
null); | |
//check | |
if (phoneCursor.moveToNext()) { | |
String number = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | |
String accountType = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.ACCOUNT_TYPE_AND_DATA_SET)); | |
//initialize contact model | |
ContactModel model = new ContactModel(); | |
model.setName(name); | |
model.setPhone(number); | |
model.setAccountType(accountType); | |
//add model to array | |
contactList.add(model); | |
//close phone cursor | |
phoneCursor.close(); | |
} | |
} | |
//close cursor | |
cursor.close(); | |
} | |
return contactList; | |
} | |
public void getAllContacts(Activity activity, ListView listView) { | |
ArrayList<ContactModel> arrayList = new ArrayList<>(); | |
Cursor cursor = activity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); | |
activity.startManagingCursor(cursor); | |
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, | |
ContactsContract.CommonDataKinds.Phone.NUMBER, | |
ContactsContract.CommonDataKinds.Phone._ID | |
}; | |
int[] to = {android.R.id.text1, android.R.id.text2}; | |
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(activity, android.R.layout.simple_list_item_2, cursor, from, to); | |
listView.setAdapter(simpleCursorAdapter); | |
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); | |
} | |
public class ContactModel implements Serializable { | |
private String name; | |
private String phone; | |
private String accountType; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPhone() { | |
return phone; | |
} | |
public void setPhone(String phone) { | |
this.phone = phone; | |
} | |
public String getAccountType() { | |
return accountType; | |
} | |
public void setAccountType(String accountType) { | |
this.accountType = accountType; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment