Created
April 30, 2017 00:37
-
-
Save JosiasSena/dd54418e99c7623a1ab7d1ca6c661731 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
public class PhoneBookManager { | |
private final ContentResolver contentResolver; | |
public PhoneBookManager(final Context context) { | |
contentResolver = context.getContentResolver(); | |
} | |
private boolean isContactWithNumberExists(@NonNull final String number) { | |
final String pathSegment = Uri.encode(number); | |
final Uri baseUri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI; | |
final Uri uri = Uri.withAppendedPath(baseUri, pathSegment); | |
final String[] projection = { | |
ContactsContract.PhoneLookup._ID, | |
ContactsContract.PhoneLookup.NUMBER, | |
ContactsContract.PhoneLookup.DISPLAY_NAME}; | |
final Cursor cursor = contentResolver.query(uri, projection, null, null, null); | |
try { | |
if (cursor != null && cursor.moveToFirst()) { | |
return true; | |
} | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
return false; | |
} | |
public boolean isContactWithNameExists(@NonNull String name) { | |
final Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; | |
final String[] projection = new String[] { | |
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, | |
ContactsContract.CommonDataKinds.Phone.NUMBER}; | |
final String where = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "= ?"; | |
final String[] selectionArgs = {name}; | |
final Cursor cursor = | |
contentResolver.query(uri, projection, where, selectionArgs, null); | |
try { | |
if (cursor != null && cursor.moveToFirst()) { | |
final int indexName = | |
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); | |
do { | |
final String contactName = cursor.getString(indexName); | |
if (contactName.equalsIgnoreCase(name)) { | |
return true; | |
} | |
} while (cursor.moveToNext()); | |
} | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
return false; | |
} | |
public void saveContactToPhoneBook(@NonNull final Contact contact) { | |
if (isContactWithNameExists(contact.getName())) { | |
updateContactWithNewNumber(contact); | |
} else { | |
createNewContact(contact); | |
} | |
} | |
private void updateContactWithNewNumber(@NonNull final Contact contact) { | |
final ArrayList<ContentProviderOperation> operations = new ArrayList<>(); | |
String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + | |
ContactsContract.Data.MIMETYPE + " = ? AND " + | |
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? "; | |
String[] params = new String[] {contact.getName(), | |
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, | |
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MAIN)}; | |
operations.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) | |
.withSelection(where, params) | |
.withValue(ContactsContract.CommonDataKinds.Phone.DATA, contact.getPhoneNumber()) | |
.build()); | |
try { | |
contentResolver.applyBatch(ContactsContract.AUTHORITY, operations); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void createNewContact(@NonNull final Contact contact) { | |
final ArrayList<ContentProviderOperation> operations = new ArrayList<>(); | |
final int contactIndex = operations.size(); | |
// Newly Inserted contact | |
// A raw contact will be inserted ContactsContract.RawContacts table in contacts database. | |
final Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI; | |
operations.add(ContentProviderOperation.newInsert(rawContactUri) | |
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) | |
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) | |
.build()); | |
// Display name will be inserted in ContactsContract.Data table | |
final Uri contactsContractUri = ContactsContract.Data.CONTENT_URI; | |
operations.add(ContentProviderOperation.newInsert(contactsContractUri) | |
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex) | |
.withValue(ContactsContract.Data.MIMETYPE, | |
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) | |
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, | |
contact.getName()) // Name of the contact | |
.build()); | |
// Main number will be inserted in ContactsContract.Data table | |
operations.add(ContentProviderOperation.newInsert(contactsContractUri) | |
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex) | |
.withValue(ContactsContract.Data.MIMETYPE, | |
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) | |
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, | |
contact.getPhoneNumber()) // Number to be added | |
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, | |
ContactsContract.CommonDataKinds.Phone.TYPE_MAIN) // Phone number type Home, Mobile, etc. | |
.build()); | |
updatePhoneBook(operations); | |
} | |
private void updatePhoneBook(final ArrayList<ContentProviderOperation> operations) { | |
try { | |
// We will do batch operation to insert all above data. | |
// Apply above data insertion into contacts list | |
contentResolver.applyBatch(ContactsContract.AUTHORITY, operations); | |
} catch (RemoteException | OperationApplicationException exp) { | |
exp.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment