Improved contacts loading
This commit is contained in:
parent
86795d0b0a
commit
ada6eee4ba
2 changed files with 45 additions and 55 deletions
|
@ -21,11 +21,13 @@ package org.linphone;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.linphone.core.LinphoneAddress;
|
||||
import org.linphone.core.LinphoneCore;
|
||||
|
@ -290,13 +292,19 @@ public class ContactsManager extends ContentObserver {
|
|||
@SuppressWarnings("unchecked")
|
||||
protected List<LinphoneContact> doInBackground(Void... params) {
|
||||
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
|
||||
|
||||
Date contactsTime = new Date();
|
||||
|
||||
if (hasContactsAccess()) {
|
||||
Cursor c = getContactsCursor(contentResolver);
|
||||
if (c != null) {
|
||||
while (c.moveToNext()) {
|
||||
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();
|
||||
contact.setFirstNameAndLastName(givenName, familyName);
|
||||
contact.setFullName(displayName);
|
||||
contact.setAndroidId(id);
|
||||
contacts.add(contact);
|
||||
}
|
||||
|
@ -346,6 +354,12 @@ public class ContactsManager extends ContentObserver {
|
|||
}
|
||||
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
|
||||
publishProgress(contacts);
|
||||
|
||||
|
@ -353,11 +367,18 @@ public class ContactsManager extends ContentObserver {
|
|||
// This time fetch all informations including phone numbers and SIP addresses
|
||||
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;
|
||||
}
|
||||
|
||||
protected void onProgressUpdate(List<LinphoneContact>... result) {
|
||||
contactsCache.clear();
|
||||
setContacts(result[0]);
|
||||
for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
|
||||
listener.onContactsUpdated();
|
||||
|
@ -365,8 +386,6 @@ public class ContactsManager extends ContentObserver {
|
|||
}
|
||||
|
||||
protected void onPostExecute(List<LinphoneContact> result) {
|
||||
setContacts(result);
|
||||
contactsCache.clear();
|
||||
for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
|
||||
listener.onContactsUpdated();
|
||||
}
|
||||
|
@ -432,8 +451,8 @@ public class ContactsManager extends ContentObserver {
|
|||
String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
|
||||
+ "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL "
|
||||
+ " OR (" + Data.MIMETYPE + " = '" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
|
||||
+ "' AND " + ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))";
|
||||
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME };
|
||||
+ "' AND " + CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))";
|
||||
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 + ")";
|
||||
|
||||
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 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[displayName] = cursor.getString(displayName);
|
||||
newRow[givenName] = cursor.getString(givenName);
|
||||
newRow[familyName] = cursor.getString(familyName);
|
||||
|
||||
result.addRow(newRow);
|
||||
}
|
||||
|
|
|
@ -556,16 +556,16 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
|
|||
}
|
||||
|
||||
public void minimalRefresh() {
|
||||
addresses = new ArrayList<LinphoneNumberOrAddress>();
|
||||
hasSipAddress = false;
|
||||
|
||||
if (isAndroidContact()) {
|
||||
getContactNames();
|
||||
getNativeContactOrganization();
|
||||
setThumbnailUri(getContactThumbnailPictureUri());
|
||||
setPhotoUri(getContactPictureUri());
|
||||
getNativeContactOrganization();
|
||||
|
||||
if (isLinphoneFriend()) {
|
||||
hasSipAddress = friend.getAddress() != null;
|
||||
for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact()) {
|
||||
addNumberOrAddress(noa);
|
||||
}
|
||||
} else if (isLinphoneFriend()) {
|
||||
fullName = friend.getName();
|
||||
|
@ -575,35 +575,6 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
|
|||
photoUri = null;
|
||||
hasSipAddress = friend.getAddress() != null;
|
||||
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();
|
||||
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() {
|
||||
return androidId != null;
|
||||
}
|
||||
|
@ -671,22 +654,6 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
|
|||
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() {
|
||||
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
|
||||
String[] proj = new String[]{ ContactsContract.CommonDataKinds.Organization.COMPANY };
|
||||
|
|
Loading…
Reference in a new issue