Fix find contacts
This commit is contained in:
parent
23b801c09b
commit
b2893a4d20
2 changed files with 114 additions and 17 deletions
|
@ -13,7 +13,7 @@ dependencies {
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 19
|
compileSdkVersion 22
|
||||||
buildToolsVersion "20.0.0"
|
buildToolsVersion "20.0.0"
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
|
|
|
@ -26,11 +26,14 @@ import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
|
|
||||||
import org.linphone.compatibility.Compatibility;
|
import org.linphone.compatibility.Compatibility;
|
||||||
import org.linphone.core.LinphoneAddress;
|
import org.linphone.core.LinphoneAddress;
|
||||||
|
import org.linphone.core.LinphoneCore;
|
||||||
import org.linphone.core.LinphoneCoreException;
|
import org.linphone.core.LinphoneCoreException;
|
||||||
import org.linphone.core.LinphoneCoreFactory;
|
import org.linphone.core.LinphoneCoreFactory;
|
||||||
import org.linphone.core.LinphoneFriend;
|
import org.linphone.core.LinphoneFriend;
|
||||||
|
import org.linphone.core.LinphoneProxyConfig;
|
||||||
import org.linphone.mediastream.Log;
|
import org.linphone.mediastream.Log;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -101,7 +104,7 @@ public class ContactsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Contacts
|
//Contacts
|
||||||
public void createNewContact(ArrayList<ContentProviderOperation> ops, String firstName, String lastName){
|
public void createNewContact(ArrayList<ContentProviderOperation> ops, String firstName, String lastName) {
|
||||||
int contactID = 0;
|
int contactID = 0;
|
||||||
|
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
|
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.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
|
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
|
.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()
|
.build()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -125,8 +128,8 @@ public class ContactsManager {
|
||||||
|
|
||||||
public void updateExistingContact(ArrayList<ContentProviderOperation> ops, Contact contact, String firstName, String lastName) {
|
public void updateExistingContact(ArrayList<ContentProviderOperation> ops, Contact contact, String firstName, String lastName) {
|
||||||
if (getDisplayName(firstName, lastName) != null) {
|
if (getDisplayName(firstName, lastName) != null) {
|
||||||
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'" ;
|
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[] args = new String[]{String.valueOf(contact.getID())};
|
||||||
|
|
||||||
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
||||||
.withSelection(select, args)
|
.withSelection(select, args)
|
||||||
|
@ -145,7 +148,7 @@ public class ContactsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
LinphoneFriend friend = LinphoneCoreFactory.instance().createLinphoneFriend(sipUri);
|
LinphoneFriend friend = LinphoneCoreFactory.instance().createLinphoneFriend(sipUri);
|
||||||
if(friend != null) {
|
if (friend != null) {
|
||||||
friend.edit();
|
friend.edit();
|
||||||
friend.setRefKey(contact.getID());
|
friend.setRefKey(contact.getID());
|
||||||
friend.done();
|
friend.done();
|
||||||
|
@ -171,7 +174,7 @@ public class ContactsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
LinphoneFriend friend = LinphoneManager.getLc().findFriendByAddress(oldSipUri);
|
LinphoneFriend friend = LinphoneManager.getLc().findFriendByAddress(oldSipUri);
|
||||||
if(friend != null) {
|
if (friend != null) {
|
||||||
friend.edit();
|
friend.edit();
|
||||||
try {
|
try {
|
||||||
friend.setAddress(LinphoneCoreFactory.instance().createLinphoneAddress(newSipUri));
|
friend.setAddress(LinphoneCoreFactory.instance().createLinphoneAddress(newSipUri));
|
||||||
|
@ -196,27 +199,50 @@ public class ContactsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeAllFriends(Contact contact) {
|
public void removeAllFriends(Contact contact) {
|
||||||
for(LinphoneFriend friend : LinphoneManager.getLc().getFriendList()){
|
for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) {
|
||||||
if(friend.getRefKey().equals(contact.getID())) {
|
if (friend.getRefKey().equals(contact.getID())) {
|
||||||
LinphoneManager.getLc().removeFriend(friend);
|
LinphoneManager.getLc().removeFriend(friend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contact findContactWithDisplayName(String displayName) {
|
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()
|
String selection = new StringBuilder()
|
||||||
.append(ContactsContract.Data.DISPLAY_NAME)
|
.append(ContactsContract.Data.DISPLAY_NAME)
|
||||||
.append(" = ?").toString();
|
.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);
|
new String[]{displayName}, null);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
if (c.moveToFirst()) {
|
if (c.moveToFirst()) {
|
||||||
Contact contact = Compatibility.getContact(contentResolver,c,c.getPosition());
|
Contact contact = Compatibility.getContact(contentResolver, c, c.getPosition());
|
||||||
c.close();
|
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;
|
return contact;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
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){
|
public Contact findContactWithAddress(LinphoneAddress address){
|
||||||
for(Contact contact : contactList){
|
String sipUri = address.asStringUriOnly();
|
||||||
if(contact.getNumbersOrAddresses().contains(address.asStringUriOnly()) || contact.getNumbersOrAddresses().contains(address.getUserName())){
|
if (sipUri.startsWith("sip:"))
|
||||||
return contact;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,7 +536,6 @@ public class ContactsManager {
|
||||||
if (contactCursor != null) {
|
if (contactCursor != null) {
|
||||||
for (int i = 0; i < contactCursor.getCount(); i++) {
|
for (int i = 0; i < contactCursor.getCount(); i++) {
|
||||||
Contact contact = Compatibility.getContact(contentResolver, contactCursor, i);
|
Contact contact = Compatibility.getContact(contentResolver, contactCursor, i);
|
||||||
|
|
||||||
if (contact == null)
|
if (contact == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue