diff --git a/src/org/linphone/ContactsManager.java b/src/org/linphone/ContactsManager.java index e688ab74a..acc90561c 100644 --- a/src/org/linphone/ContactsManager.java +++ b/src/org/linphone/ContactsManager.java @@ -193,25 +193,44 @@ public class ContactsManager extends ContentObserver { public List fetchContactsAsync() { List contacts = new ArrayList(); - for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) { - LinphoneContact contact = new LinphoneContact(); - contact.setFriend(friend); - contact.refresh(); - contacts.add(contact); + if (mAccount != null && hasContactsAccess()) { + Cursor c = Compatibility.getContactsCursor(contentResolver, null); + if (c != null) { + while (c.moveToNext()) { + String id = c.getString(c.getColumnIndex(Data.CONTACT_ID)); + LinphoneContact contact = new LinphoneContact(); + contact.setAndroidId(id); + contacts.add(contact); + } + c.close(); + } } - - if (mAccount == null || !hasContactsAccess()) return contacts; - Cursor c = Compatibility.getContactsCursor(contentResolver, null); - if (c != null) { - while (c.moveToNext()) { - String id = c.getString(c.getColumnIndex(Data.CONTACT_ID)); + for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) { + String refkey = friend.getRefKey(); + if (refkey != null) { + boolean found = false; + for (LinphoneContact contact : contacts) { + if (refkey.equals(contact.getAndroidId())) { + contact.setFriend(friend); + found = true; + break; + } + } + if (!found) { + LinphoneContact contact = new LinphoneContact(); + contact.setFriend(friend); + contacts.add(contact); + } + } else { LinphoneContact contact = new LinphoneContact(); - contact.setAndroidId(id); - contact.refresh(); + contact.setFriend(friend); contacts.add(contact); } - c.close(); + } + + for (LinphoneContact contact : contacts) { + contact.refresh(); } return contacts; diff --git a/src/org/linphone/LinphoneContact.java b/src/org/linphone/LinphoneContact.java index 983063bd2..ef6b497f2 100644 --- a/src/org/linphone/LinphoneContact.java +++ b/src/org/linphone/LinphoneContact.java @@ -331,7 +331,7 @@ public class LinphoneContact implements Serializable { } friend.done(); - if (!isAndroidContact() && friend.getAddress() != null) { + if (friend.getAddress() != null) { if (lc.findFriendByAddress(friend.getAddress().asString()) == null) { try { lc.addFriend(friend); @@ -404,6 +404,14 @@ public class LinphoneContact implements Serializable { } } } + LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); + if (lc != null) { + try { + lc.addFriend(friend); + } catch (LinphoneCoreException e) { + Log.e(e); + } + } } } }