diff --git a/build.gradle b/build.gradle index e86b4947a..c341bf0f5 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ dependencies { } android { - compileSdkVersion 19 + compileSdkVersion 22 buildToolsVersion "20.0.0" sourceSets { diff --git a/src/org/linphone/ContactsManager.java b/src/org/linphone/ContactsManager.java index bdfb75f2f..61013fb80 100644 --- a/src/org/linphone/ContactsManager.java +++ b/src/org/linphone/ContactsManager.java @@ -26,11 +26,14 @@ import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; + import org.linphone.compatibility.Compatibility; import org.linphone.core.LinphoneAddress; +import org.linphone.core.LinphoneCore; import org.linphone.core.LinphoneCoreException; import org.linphone.core.LinphoneCoreFactory; import org.linphone.core.LinphoneFriend; +import org.linphone.core.LinphoneProxyConfig; import org.linphone.mediastream.Log; import java.util.ArrayList; @@ -101,7 +104,7 @@ public class ContactsManager { } //Contacts - public void createNewContact(ArrayList ops, String firstName, String lastName){ + public void createNewContact(ArrayList ops, String firstName, String lastName) { int contactID = 0; ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) @@ -117,7 +120,7 @@ public class ContactsManager { .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName) .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName) - .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getDisplayName(firstName,lastName)) + .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getDisplayName(firstName, lastName)) .build() ); } @@ -125,8 +128,8 @@ public class ContactsManager { public void updateExistingContact(ArrayList ops, Contact contact, String firstName, String lastName) { if (getDisplayName(firstName, lastName) != null) { - String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'" ; - String[] args = new String[] { String.valueOf(contact.getID()) }; + String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'"; + String[] args = new String[]{String.valueOf(contact.getID())}; ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(select, args) @@ -145,7 +148,7 @@ public class ContactsManager { } LinphoneFriend friend = LinphoneCoreFactory.instance().createLinphoneFriend(sipUri); - if(friend != null) { + if (friend != null) { friend.edit(); friend.setRefKey(contact.getID()); friend.done(); @@ -171,7 +174,7 @@ public class ContactsManager { } LinphoneFriend friend = LinphoneManager.getLc().findFriendByAddress(oldSipUri); - if(friend != null) { + if (friend != null) { friend.edit(); try { friend.setAddress(LinphoneCoreFactory.instance().createLinphoneAddress(newSipUri)); @@ -196,27 +199,50 @@ public class ContactsManager { } public void removeAllFriends(Contact contact) { - for(LinphoneFriend friend : LinphoneManager.getLc().getFriendList()){ - if(friend.getRefKey().equals(contact.getID())) { + for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) { + if (friend.getRefKey().equals(contact.getID())) { LinphoneManager.getLc().removeFriend(friend); } } } public Contact findContactWithDisplayName(String displayName) { - String[] projection = { ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME }; + String[] projection = {ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME}; String selection = new StringBuilder() .append(ContactsContract.Data.DISPLAY_NAME) .append(" = ?").toString(); - Cursor c = contentResolver.query(ContactsContract.Data.CONTENT_URI,projection,selection, + Cursor c = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selection, new String[]{displayName}, null); if (c != null) { if (c.moveToFirst()) { - Contact contact = Compatibility.getContact(contentResolver,c,c.getPosition()); + Contact contact = Compatibility.getContact(contentResolver, c, c.getPosition()); c.close(); - if(contact != null) { + if (contact != null) { + return contact; + } else { + return null; + } + } + c.close(); + } + return null; + } + + public Contact getContact(String id){ + String[] projection = {ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME}; + String selection = new StringBuilder() + .append(ContactsContract.Data.CONTACT_ID) + .append(" = ?").toString(); + + Cursor c = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selection, new String[]{id}, null); + if(c!=null){ + if (c.moveToFirst()) { + Contact contact = Compatibility.getContact(contentResolver, c, c.getPosition()); + c.close(); + + if (contact != null) { return contact; } else { return null; @@ -270,12 +296,84 @@ public class ContactsManager { } } + private Contact checkPhoneQueryResult(Cursor c, String columnPhone, String columnId, String username) { + boolean contactFound = false; + + if (c != null) { + while (!contactFound && c.moveToNext()) { + String phone = c.getString(c.getColumnIndex(columnPhone)); + if (phone.equals(username)) { + contactFound = true; + } else { + String normalizedUsername = null; + LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); + if (lc != null) { + LinphoneProxyConfig lpc = lc.getDefaultProxyConfig(); + if (lpc != null) { + normalizedUsername = lpc.normalizePhoneNumber(phone); + } + } + + if (normalizedUsername != null && normalizedUsername.equals(username)) { + contactFound = true; + } + } + + if(contactFound){ + Contact contact = getContact(c.getString(c.getColumnIndex(columnId))); + c.close(); + return contact; + } + } + c.close(); + } + return null; + } + public Contact findContactWithAddress(LinphoneAddress address){ - for(Contact contact : contactList){ - if(contact.getNumbersOrAddresses().contains(address.asStringUriOnly()) || contact.getNumbersOrAddresses().contains(address.getUserName())){ - return contact; + String sipUri = address.asStringUriOnly(); + if (sipUri.startsWith("sip:")) + sipUri = sipUri.substring(4); + + if(LinphoneManager.getLc().getFriendList() != null && LinphoneManager.getLc().getFriendList().length > 0) { + for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) { + if (friend.getAddress().equals(address)) { + return getContact(friend.getRefKey()); + } } } + + //Find Sip address + Contact contact; + String [] projection = new String[] {ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME}; + String selection = new StringBuilder() + .append(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS) + .append(" = ?").toString(); + + Cursor cur = contentResolver.query(ContactsContract.Data.CONTENT_URI, projection, selection, + new String[]{sipUri}, null); + if (cur != null) { + if (cur.moveToFirst()) { + contact = Compatibility.getContact(contentResolver, cur, cur.getPosition()); + cur.close(); + + if (contact != null) { + return contact; + } + } + cur.close(); + } + + //Find number + Uri lookupUri = Uri.withAppendedPath(android.provider.ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address.getUserName())); + projection = new String[] {ContactsContract.PhoneLookup._ID,ContactsContract.PhoneLookup.NUMBER,ContactsContract.PhoneLookup.DISPLAY_NAME }; + Cursor c = contentResolver.query(lookupUri, projection, null, null, null); + contact = checkPhoneQueryResult(c, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup._ID, address.getUserName()); + + if (contact != null) { + return contact; + } + return null; } @@ -438,7 +536,6 @@ public class ContactsManager { if (contactCursor != null) { for (int i = 0; i < contactCursor.getCount(); i++) { Contact contact = Compatibility.getContact(contentResolver, contactCursor, i); - if (contact == null) continue;