Improved contacts loading

This commit is contained in:
Sylvain Berfini 2017-01-06 16:05:05 +01:00
parent 86795d0b0a
commit ada6eee4ba
2 changed files with 45 additions and 55 deletions

View file

@ -21,11 +21,13 @@ package org.linphone;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.linphone.core.LinphoneAddress; import org.linphone.core.LinphoneAddress;
import org.linphone.core.LinphoneCore; import org.linphone.core.LinphoneCore;
@ -290,13 +292,19 @@ public class ContactsManager extends ContentObserver {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected List<LinphoneContact> doInBackground(Void... params) { protected List<LinphoneContact> doInBackground(Void... params) {
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>(); List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
Date contactsTime = new Date();
if (hasContactsAccess()) { if (hasContactsAccess()) {
Cursor c = getContactsCursor(contentResolver); Cursor c = getContactsCursor(contentResolver);
if (c != null) { if (c != null) {
while (c.moveToNext()) { while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(Data.CONTACT_ID)); String id = c.getString(c.getColumnIndex(Data.CONTACT_ID));
String displayName = c.getString(c.getColumnIndex(Data.DISPLAY_NAME));
String givenName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME));
String familyName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME));
LinphoneContact contact = new LinphoneContact(); LinphoneContact contact = new LinphoneContact();
contact.setFirstNameAndLastName(givenName, familyName);
contact.setFullName(displayName);
contact.setAndroidId(id); contact.setAndroidId(id);
contacts.add(contact); contacts.add(contact);
} }
@ -346,6 +354,12 @@ public class ContactsManager extends ContentObserver {
} }
Collections.sort(contacts); Collections.sort(contacts);
long timeElapsed = (new Date()).getTime() - contactsTime.getTime();
String time = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(timeElapsed),
TimeUnit.MILLISECONDS.toSeconds(timeElapsed) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
Log.d("[ContactsManager] minimal informations for " + contacts.size() + " contacts gathered in " + time);
// Public the current list of contacts without all the informations populated // Public the current list of contacts without all the informations populated
publishProgress(contacts); publishProgress(contacts);
@ -353,11 +367,18 @@ public class ContactsManager extends ContentObserver {
// This time fetch all informations including phone numbers and SIP addresses // This time fetch all informations including phone numbers and SIP addresses
contact.refresh(); contact.refresh();
} }
timeElapsed = (new Date()).getTime() - contactsTime.getTime();
time = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(timeElapsed),
TimeUnit.MILLISECONDS.toSeconds(timeElapsed) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeElapsed)));
Log.d("[ContactsManager] complete informations for " + contacts.size() + " contacts gathered in " + time);
return contacts; return contacts;
} }
protected void onProgressUpdate(List<LinphoneContact>... result) { protected void onProgressUpdate(List<LinphoneContact>... result) {
contactsCache.clear();
setContacts(result[0]); setContacts(result[0]);
for (ContactsUpdatedListener listener : contactsUpdatedListeners) { for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
listener.onContactsUpdated(); listener.onContactsUpdated();
@ -365,8 +386,6 @@ public class ContactsManager extends ContentObserver {
} }
protected void onPostExecute(List<LinphoneContact> result) { protected void onPostExecute(List<LinphoneContact> result) {
setContacts(result);
contactsCache.clear();
for (ContactsUpdatedListener listener : contactsUpdatedListeners) { for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
listener.onContactsUpdated(); listener.onContactsUpdated();
} }
@ -432,8 +451,8 @@ public class ContactsManager extends ContentObserver {
String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL " + "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL "
+ " OR (" + Data.MIMETYPE + " = '" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + " OR (" + Data.MIMETYPE + " = '" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
+ "' AND " + ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))"; + "' AND " + CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))";
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME }; String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, CommonDataKinds.StructuredName.GIVEN_NAME, CommonDataKinds.StructuredName.FAMILY_NAME };
String query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + req + ")"; String query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + req + ")";
Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null, " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC"); Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null, " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC");
@ -451,9 +470,13 @@ public class ContactsManager extends ContentObserver {
int contactID = cursor.getColumnIndex(Data.CONTACT_ID); int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME); int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);
int givenName = cursor.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME);
int familyName = cursor.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME);
newRow[contactID] = cursor.getString(contactID); newRow[contactID] = cursor.getString(contactID);
newRow[displayName] = cursor.getString(displayName); newRow[displayName] = cursor.getString(displayName);
newRow[givenName] = cursor.getString(givenName);
newRow[familyName] = cursor.getString(familyName);
result.addRow(newRow); result.addRow(newRow);
} }

View file

@ -556,16 +556,16 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
} }
public void minimalRefresh() { public void minimalRefresh() {
addresses = new ArrayList<LinphoneNumberOrAddress>();
hasSipAddress = false; hasSipAddress = false;
if (isAndroidContact()) { if (isAndroidContact()) {
getContactNames(); getNativeContactOrganization();
setThumbnailUri(getContactThumbnailPictureUri()); setThumbnailUri(getContactThumbnailPictureUri());
setPhotoUri(getContactPictureUri()); setPhotoUri(getContactPictureUri());
getNativeContactOrganization();
if (isLinphoneFriend()) { for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact()) {
hasSipAddress = friend.getAddress() != null; addNumberOrAddress(noa);
} }
} else if (isLinphoneFriend()) { } else if (isLinphoneFriend()) {
fullName = friend.getName(); fullName = friend.getName();
@ -575,35 +575,6 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
photoUri = null; photoUri = null;
hasSipAddress = friend.getAddress() != null; hasSipAddress = friend.getAddress() != null;
organization = friend.getOrganization(); organization = friend.getOrganization();
}
}
public void refresh() {
addresses = new ArrayList<LinphoneNumberOrAddress>();
hasSipAddress = false;
if (isAndroidContact()) {
getContactNames();
setThumbnailUri(getContactThumbnailPictureUri());
setPhotoUri(getContactPictureUri());
androidRawId = findRawContactID();
if (LinphoneManager.getInstance().getContext().getResources().getBoolean(R.bool.use_linphone_tag)) {
androidTagId = findLinphoneRawContactId();
}
for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact()) {
addNumberOrAddress(noa);
}
createOrUpdateFriend();
} else if (isLinphoneFriend()) {
fullName = friend.getName();
lastName = friend.getFamilyName();
firstName = friend.getGivenName();
thumbnailUri = null;
photoUri = null;
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc != null && lc.isVCardSupported()) { if (lc != null && lc.isVCardSupported()) {
@ -624,6 +595,18 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
} }
} }
public void refresh() {
if (isAndroidContact()) {
androidRawId = findRawContactID();
if (LinphoneManager.getInstance().getContext().getResources().getBoolean(R.bool.use_linphone_tag)) {
androidTagId = findLinphoneRawContactId();
}
createOrUpdateFriend();
}
}
public boolean isAndroidContact() { public boolean isAndroidContact() {
return androidId != null; return androidId != null;
} }
@ -671,22 +654,6 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
} }
private void getContactNames() {
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
String[] proj = new String[]{ ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.Contacts.DISPLAY_NAME };
String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] args = new String[]{ getAndroidId(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, proj, select, args, null);
if (c != null) {
if (c.moveToFirst()) {
firstName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
lastName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
fullName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
c.close();
}
}
private void getNativeContactOrganization() { private void getNativeContactOrganization() {
ContentResolver resolver = ContactsManager.getInstance().getContentResolver(); ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
String[] proj = new String[]{ ContactsContract.CommonDataKinds.Organization.COMPANY }; String[] proj = new String[]{ ContactsContract.CommonDataKinds.Organization.COMPANY };