Reworked ContactHelper to work with normalized numbers + normalize numbers from contacts
This commit is contained in:
parent
18f4b0dab3
commit
a47614d27b
2 changed files with 135 additions and 138 deletions
|
@ -19,10 +19,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.linphone.compatibility.Compatibility;
|
import org.linphone.compatibility.Compatibility;
|
||||||
|
import org.linphone.core.LinphoneCore;
|
||||||
import org.linphone.core.LinphoneProxyConfig;
|
import org.linphone.core.LinphoneProxyConfig;
|
||||||
import org.linphone.mediastream.Log;
|
import org.linphone.mediastream.Log;
|
||||||
import org.linphone.ui.AvatarWithShadow;
|
import org.linphone.ui.AvatarWithShadow;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.ContentProviderOperation;
|
import android.content.ContentProviderOperation;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
@ -51,7 +54,22 @@ public class ContactFragment extends Fragment implements OnClickListener {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (LinphoneActivity.isInstanciated()) {
|
if (LinphoneActivity.isInstanciated()) {
|
||||||
LinphoneActivity.instance().setAddresGoToDialerAndCall(v.getTag().toString(), contact.getName(), contact.getPhotoUri());
|
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
||||||
|
if (lc != null) {
|
||||||
|
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
|
||||||
|
String to;
|
||||||
|
if (lpc != null) {
|
||||||
|
String address = v.getTag().toString();
|
||||||
|
if (address.contains("@")) {
|
||||||
|
to = lpc.normalizePhoneNumber(address.split("@")[0]);
|
||||||
|
} else {
|
||||||
|
to = lpc.normalizePhoneNumber(address);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
to = v.getTag().toString();
|
||||||
|
}
|
||||||
|
LinphoneActivity.instance().setAddresGoToDialerAndCall(to, contact.getName(), contact.getPhotoUri());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,15 +19,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
package org.linphone;
|
package org.linphone;
|
||||||
|
|
||||||
import org.linphone.core.LinphoneAddress;
|
import org.linphone.core.LinphoneAddress;
|
||||||
|
import org.linphone.core.LinphoneCore;
|
||||||
|
import org.linphone.core.LinphoneProxyConfig;
|
||||||
import org.linphone.mediastream.Version;
|
import org.linphone.mediastream.Version;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.ContactsContract.Contacts;
|
import android.provider.ContactsContract.Contacts;
|
||||||
import android.telephony.PhoneNumberUtils;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
public final class ContactHelper {
|
public final class ContactHelper {
|
||||||
|
@ -54,13 +56,7 @@ public final class ContactHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean query() {
|
public boolean query() {
|
||||||
boolean succeeded;
|
boolean succeeded = queryContact();
|
||||||
if (Version.sdkAboveOrEqual(Version.API05_ECLAIR_20)) {
|
|
||||||
ContactHelperNew helper = new ContactHelperNew();
|
|
||||||
succeeded = helper.queryNewContactAPI();
|
|
||||||
} else {
|
|
||||||
succeeded = queryOldContactAPI();
|
|
||||||
}
|
|
||||||
if (succeeded && !TextUtils.isEmpty(displayName)) {
|
if (succeeded && !TextUtils.isEmpty(displayName)) {
|
||||||
address.setDisplayName(displayName);
|
address.setDisplayName(displayName);
|
||||||
}
|
}
|
||||||
|
@ -102,148 +98,131 @@ public final class ContactHelper {
|
||||||
return testPhotoUriAndCloseCursor(cursor);
|
return testPhotoUriAndCloseCursor(cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// OLD API
|
private void checkPhotosUris(ContentResolver resolver, Cursor c, String idCol, String nameCol) {
|
||||||
@SuppressWarnings("deprecation")
|
displayName = c.getString(c.getColumnIndex(nameCol));
|
||||||
private final boolean queryOldContactAPI() {
|
|
||||||
String normalizedNumber = PhoneNumberUtils.getStrippedReversed(username);
|
|
||||||
if (TextUtils.isEmpty(normalizedNumber)) {
|
|
||||||
// non phone username
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String[] projection = {android.provider.Contacts.Phones.PERSON_ID, android.provider.Contacts.Phones.DISPLAY_NAME};
|
|
||||||
String selection = android.provider.Contacts.Phones.NUMBER_KEY + "=" + normalizedNumber;
|
|
||||||
Cursor c = resolver.query(android.provider.Contacts.Phones.CONTENT_URI, projection, selection, null, null);
|
|
||||||
if (c == null) return false;
|
|
||||||
|
|
||||||
while (c.moveToNext()) {
|
long id = c.getLong(c.getColumnIndex(idCol));
|
||||||
long id = c.getLong(c.getColumnIndex(android.provider.Contacts.Phones.PERSON_ID));
|
Uri contactUri = ContentUris.withAppendedId(android.provider.ContactsContract.Contacts.CONTENT_URI, id);
|
||||||
Uri personUri = ContentUris.withAppendedId(android.provider.Contacts.People.CONTENT_URI, id);
|
Uri photoUri = Uri.withAppendedPath(contactUri, android.provider.ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
||||||
Uri potentialPictureUri = Uri.withAppendedPath(personUri, android.provider.Contacts.Photos.CONTENT_DIRECTORY);
|
|
||||||
boolean valid = testPhotoUri(resolver, potentialPictureUri, android.provider.Contacts.Photos.DATA);
|
if (photoUri != null) {
|
||||||
if (valid) {
|
String[] projection = { android.provider.ContactsContract.CommonDataKinds.Photo.PHOTO };
|
||||||
displayName = c.getString(c.getColumnIndex(android.provider.Contacts.Phones.DISPLAY_NAME));
|
Cursor photoCursor = resolver.query(photoUri, projection, null, null, null);
|
||||||
foundPhotoUri = personUri; // hack (not returning pictureUri as it crashes when reading from it)
|
|
||||||
c.close();
|
boolean isPhotoValid = testPhotoUriAndCloseCursor(photoCursor);
|
||||||
return true;
|
if (isPhotoValid) {
|
||||||
|
foundPhotoUri = photoUri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.close();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// END OLD API
|
private boolean checkSIPQueryResult(Cursor c, String columnSip) {
|
||||||
|
boolean contactFound = false;
|
||||||
|
|
||||||
// START NEW CONTACT API
|
if (c != null) {
|
||||||
|
while (!contactFound && c.moveToNext()) {
|
||||||
private class ContactHelperNew {
|
String contact = c.getString(c.getColumnIndex(columnSip));
|
||||||
|
if (contact.equals(username + "@" + domain) || contact.equals(username)) {
|
||||||
private final boolean checkPhotosUris(ContentResolver resolver, Cursor c, String idCol, String nameCol) {
|
contactFound = true;
|
||||||
if (c == null) return false;
|
checkPhotosUris(resolver, c, android.provider.ContactsContract.Data.CONTACT_ID, android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
||||||
while (c.moveToNext()) {
|
} else {
|
||||||
long id = c.getLong(c.getColumnIndex(idCol));
|
String normalizedUsername = null;
|
||||||
Uri contactUri = ContentUris.withAppendedId(android.provider.ContactsContract.Contacts.CONTENT_URI, id);
|
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
||||||
Uri photoUri = Uri.withAppendedPath(contactUri, android.provider.ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
if (lc != null) {
|
||||||
if (photoUri == null) {
|
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
|
||||||
return false;
|
if (lpc != null) {
|
||||||
}
|
if (contact.contains("@")) {
|
||||||
String[] projection = {android.provider.ContactsContract.CommonDataKinds.Photo.PHOTO};
|
normalizedUsername = lpc.normalizePhoneNumber(contact.split("@")[0]);
|
||||||
Cursor photoCursor = resolver.query(photoUri, projection, null, null, null);
|
} else {
|
||||||
String maybeDisplayName = c.getString(c.getColumnIndex(nameCol));
|
normalizedUsername = lpc.normalizePhoneNumber(contact);
|
||||||
boolean isPhotoValid = testPhotoUriAndCloseCursor(photoCursor);
|
}
|
||||||
if (isPhotoValid) {
|
}
|
||||||
foundPhotoUri = photoUri;
|
}
|
||||||
displayName = maybeDisplayName;
|
if (normalizedUsername != null && normalizedUsername.equals(username)) {
|
||||||
return true;
|
contactFound = true;
|
||||||
} else if (maybeDisplayName != null) {
|
checkPhotosUris(resolver, c, android.provider.ContactsContract.Data.CONTACT_ID, android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
||||||
foundPhotoUri = null;
|
}
|
||||||
displayName = maybeDisplayName;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.close();
|
c.close();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final boolean queryNewContactAPI() {
|
return contactFound;
|
||||||
String sipUri = username + "@" + domain;
|
}
|
||||||
|
|
||||||
// Try first using sip field
|
private boolean checkPhoneQueryResult(Cursor c, String columnPhone) {
|
||||||
Uri uri = android.provider.ContactsContract.Data.CONTENT_URI;
|
boolean contactFound = false;
|
||||||
String[] projection = {
|
|
||||||
android.provider.ContactsContract.Data.CONTACT_ID,
|
|
||||||
android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
|
|
||||||
|
|
||||||
// Then using custom SIP field
|
if (c != null) {
|
||||||
if (Version.sdkAboveOrEqual(Version.API09_GINGERBREAD_23)) {
|
while (!contactFound && c.moveToNext()) {
|
||||||
String selection = new StringBuilder()
|
String contact = c.getString(c.getColumnIndex(columnPhone));
|
||||||
.append(android.provider.ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS)
|
if (contact.equals(username)) {
|
||||||
.append(" = ? AND ")
|
contactFound = true;
|
||||||
.append(android.provider.ContactsContract.Data.MIMETYPE)
|
checkPhotosUris(resolver, c, android.provider.ContactsContract.PhoneLookup._ID, android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
|
||||||
.append(" = '")
|
} else {
|
||||||
.append(android.provider.ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)
|
String normalizedUsername = null;
|
||||||
.append("'")
|
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
|
||||||
.toString();
|
if (lc != null) {
|
||||||
Cursor c = resolver.query(uri, projection, selection, new String[] {sipUri}, null);
|
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
|
||||||
boolean valid = checkPhotosUris(resolver, c,
|
if (lpc != null) {
|
||||||
android.provider.ContactsContract.Data.CONTACT_ID,
|
normalizedUsername = lpc.normalizePhoneNumber(contact);
|
||||||
android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
}
|
||||||
c.close();
|
}
|
||||||
if (valid) return true;
|
if (normalizedUsername != null && normalizedUsername.equals(username)) {
|
||||||
|
contactFound = true;
|
||||||
|
checkPhotosUris(resolver, c, android.provider.ContactsContract.PhoneLookup._ID, android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
|
||||||
String selection = new StringBuilder()
|
return contactFound;
|
||||||
.append(android.provider.ContactsContract.CommonDataKinds.Im.DATA).append(" = ? AND ")
|
}
|
||||||
|
|
||||||
|
@SuppressLint("InlinedApi")
|
||||||
|
private final boolean queryContact() {
|
||||||
|
boolean contactFound = false;
|
||||||
|
|
||||||
|
Uri uri = android.provider.ContactsContract.Data.CONTENT_URI;
|
||||||
|
String[] projection = { android.provider.ContactsContract.Data.CONTACT_ID, android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, android.provider.ContactsContract.CommonDataKinds.Im.DATA };
|
||||||
|
|
||||||
|
String selection = new StringBuilder()
|
||||||
|
.append(android.provider.ContactsContract.Data.MIMETYPE)
|
||||||
|
.append(" = '")
|
||||||
|
.append(android.provider.ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
|
||||||
|
.append("' AND lower(")
|
||||||
|
.append(android.provider.ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL)
|
||||||
|
.append(") = 'sip'").toString();
|
||||||
|
|
||||||
|
Cursor c = resolver.query(uri, projection, selection, null, null);
|
||||||
|
contactFound = checkSIPQueryResult(c, android.provider.ContactsContract.CommonDataKinds.Im.DATA);
|
||||||
|
if (contactFound) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Version.sdkAboveOrEqual(Version.API09_GINGERBREAD_23)) {
|
||||||
|
selection = new StringBuilder()
|
||||||
.append(android.provider.ContactsContract.Data.MIMETYPE)
|
.append(android.provider.ContactsContract.Data.MIMETYPE)
|
||||||
.append(" = '")
|
.append(" = '")
|
||||||
.append(android.provider.ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
|
.append(android.provider.ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)
|
||||||
.append("' AND lower(")
|
.append("'")
|
||||||
.append(android.provider.ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL)
|
.toString();
|
||||||
.append(") = 'sip'").toString();
|
c = resolver.query(uri, projection, selection, null, null);
|
||||||
Cursor c = resolver.query(uri, projection, selection, new String[] {sipUri}, null);
|
contactFound = checkSIPQueryResult(c, android.provider.ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS);
|
||||||
boolean valid = checkPhotosUris(resolver, c,
|
if (contactFound) {
|
||||||
android.provider.ContactsContract.Data.CONTACT_ID,
|
|
||||||
android.provider.ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
|
||||||
c.close();
|
|
||||||
if (valid) return true;
|
|
||||||
|
|
||||||
// Finally using phone number
|
|
||||||
String normalizedNumber = PhoneNumberUtils.getStrippedReversed(username);
|
|
||||||
if (TextUtils.isEmpty(normalizedNumber)) {
|
|
||||||
// non phone username
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Uri lookupUri = Uri.withAppendedPath(android.provider.ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(username));
|
|
||||||
projection = new String[]{
|
|
||||||
android.provider.ContactsContract.PhoneLookup._ID,
|
|
||||||
android.provider.ContactsContract.PhoneLookup.NUMBER,
|
|
||||||
android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME};
|
|
||||||
c = resolver.query(lookupUri, projection, null, null, null);
|
|
||||||
while (c != null && c.moveToNext()) {
|
|
||||||
long id = c.getLong(c.getColumnIndex(android.provider.ContactsContract.PhoneLookup._ID));
|
|
||||||
String enteredNumber = c.getString(c.getColumnIndex(android.provider.ContactsContract.PhoneLookup.NUMBER));
|
|
||||||
if (!normalizedNumber.equals(PhoneNumberUtils.getStrippedReversed(enteredNumber))) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uri contactUri = ContentUris.withAppendedId(android.provider.ContactsContract.Contacts.CONTENT_URI, id);
|
|
||||||
Uri photoUri = Uri.withAppendedPath(contactUri, android.provider.ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
|
||||||
// if (photoUri == null) {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
String[] photoProj = {android.provider.ContactsContract.CommonDataKinds.Photo.PHOTO};
|
|
||||||
Cursor cursor = resolver.query(photoUri, photoProj, null, null, null);
|
|
||||||
valid = testPhotoUriAndCloseCursor(cursor);
|
|
||||||
displayName = c.getString(c.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME));
|
|
||||||
if (valid) {
|
|
||||||
foundPhotoUri = photoUri;
|
|
||||||
} else {
|
|
||||||
foundPhotoUri = null;
|
|
||||||
}
|
|
||||||
c.close();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
c.close();
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uri lookupUri = Uri.withAppendedPath(android.provider.ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(username));
|
||||||
|
projection = new String[] {
|
||||||
|
android.provider.ContactsContract.PhoneLookup._ID,
|
||||||
|
android.provider.ContactsContract.PhoneLookup.NUMBER,
|
||||||
|
android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME };
|
||||||
|
c = resolver.query(lookupUri, projection, null, null, null);
|
||||||
|
contactFound = checkPhoneQueryResult(c, android.provider.ContactsContract.PhoneLookup.NUMBER);
|
||||||
|
|
||||||
|
return contactFound;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue