Implemented quick addressbook display feature

This commit is contained in:
Sylvain Berfini 2016-07-22 11:53:13 +02:00
parent b8109c898d
commit 9e4d31d370
2 changed files with 97 additions and 68 deletions

View file

@ -19,7 +19,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
package org.linphone; package org.linphone;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -50,8 +52,6 @@ interface ContactsUpdatedListener {
} }
public class ContactsManager extends ContentObserver { public class ContactsManager extends ContentObserver {
private static final int CONTACTS_UPDATED = 543;
private static ContactsManager instance; private static ContactsManager instance;
private List<LinphoneContact> contacts, sipContacts; private List<LinphoneContact> contacts, sipContacts;
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false; private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
@ -67,16 +67,9 @@ public class ContactsManager extends ContentObserver {
} }
private static Handler handler = new Handler() { private static Handler handler = new Handler() {
@SuppressWarnings("unchecked")
@Override @Override
public void handleMessage (Message msg) { public void handleMessage (Message msg) {
if (msg.what == CONTACTS_UPDATED && msg.obj instanceof List<?>) {
List<LinphoneContact> c = (List<LinphoneContact>) msg.obj;
ContactsManager.getInstance().setContacts(c);
for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
listener.onContactsUpdated();
}
}
} }
}; };
@ -225,64 +218,78 @@ public class ContactsManager extends ContentObserver {
new ContactsFetchTask().execute(); new ContactsFetchTask().execute();
} }
public List<LinphoneContact> fetchContactsSync() {
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>(); private class ContactsFetchTask extends AsyncTask<Void, List<LinphoneContact>, List<LinphoneContact>> {
@SuppressWarnings("unchecked")
if (hasContactsAccess()) { protected List<LinphoneContact> doInBackground(Void... params) {
Cursor c = Compatibility.getContactsCursor(contentResolver, null); List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
if (c != null) {
while (c.moveToNext()) { if (hasContactsAccess()) {
String id = c.getString(c.getColumnIndex(Data.CONTACT_ID)); Cursor c = Compatibility.getContactsCursor(contentResolver, null);
LinphoneContact contact = new LinphoneContact(); if (c != null) {
contact.setAndroidId(id); while (c.moveToNext()) {
contacts.add(contact); String id = c.getString(c.getColumnIndex(Data.CONTACT_ID));
}
c.close();
}
}
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())) {
// Native matching contact found, link the friend to it
contact.setFriend(friend);
found = true;
break;
}
}
if (!found) {
if (hasContactAccess) {
// If refkey != null and hasContactAccess but there isn't a native contact with this value, then this contact has been deleted. Let's do the same with the LinphoneFriend
LinphoneManager.getLc().removeFriend(friend);
} else {
// Refkey not null but no contact access => can't link it to native contact so display it on is own
LinphoneContact contact = new LinphoneContact(); LinphoneContact contact = new LinphoneContact();
contact.setFriend(friend); contact.setAndroidId(id);
contacts.add(contact); contacts.add(contact);
} }
c.close();
} }
} else {
// No refkey so it's a standalone contact
LinphoneContact contact = new LinphoneContact();
contact.setFriend(friend);
contacts.add(contact);
} }
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())) {
// Native matching contact found, link the friend to it
contact.setFriend(friend);
found = true;
break;
}
}
if (!found) {
if (hasContactAccess) {
// If refkey != null and hasContactAccess but there isn't a native contact with this value, then this contact has been deleted. Let's do the same with the LinphoneFriend
LinphoneManager.getLc().removeFriend(friend);
} else {
// Refkey not null but no contact access => can't link it to native contact so display it on is own
LinphoneContact contact = new LinphoneContact();
contact.setFriend(friend);
contacts.add(contact);
}
}
} else {
// No refkey so it's a standalone contact
LinphoneContact contact = new LinphoneContact();
contact.setFriend(friend);
contacts.add(contact);
}
}
for (LinphoneContact contact : contacts) {
// This will only get name & picture informations to be able to quickly display contacts list
contact.minimalRefresh();
}
Collections.sort(contacts);
// Public the current list of contacts without all the informations populated
publishProgress(contacts);
for (LinphoneContact contact : contacts) {
// This time fetch all informations including phone numbers and SIP addresses
contact.refresh();
}
return contacts;
} }
for (LinphoneContact contact : contacts) { protected void onProgressUpdate(List<LinphoneContact>... result) {
contact.refresh(); setContacts(result[0]);
} for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
Collections.sort(contacts); listener.onContactsUpdated();
}
return contacts;
}
private class ContactsFetchTask extends AsyncTask<Void, Void, List<LinphoneContact>> {
protected List<LinphoneContact> doInBackground(Void... params) {
return fetchContactsSync();
} }
protected void onPostExecute(List<LinphoneContact> result) { protected void onPostExecute(List<LinphoneContact> result) {

View file

@ -447,6 +447,27 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
LinphoneManager.getLcIfManagerNotDestroyedOrNull().removeFriend(friend); LinphoneManager.getLcIfManagerNotDestroyedOrNull().removeFriend(friend);
} }
} }
public void minimalRefresh() {
hasSipAddress = false;
if (isAndroidContact()) {
getContactNames();
setThumbnailUri(getContactThumbnailPictureUri());
setPhotoUri(getContactPictureUri());
if (isLinphoneFriend()) {
hasSipAddress = friend.getAddress() != null;
}
} else if (isLinphoneFriend()) {
fullName = friend.getName();
lastName = friend.getFamillyName();
firstName = friend.getGivenName();
thumbnailUri = null;
photoUri = null;
hasSipAddress = friend.getAddress() != null;
}
}
public void refresh() { public void refresh() {
addresses = new ArrayList<LinphoneNumberOrAddress>(); addresses = new ArrayList<LinphoneNumberOrAddress>();
@ -454,18 +475,19 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
if (isAndroidContact()) { if (isAndroidContact()) {
getContactNames(); getContactNames();
setThumbnailUri(getContactPictureUri()); setThumbnailUri(getContactThumbnailPictureUri());
setPhotoUri(getContactPhotoUri()); setPhotoUri(getContactPictureUri());
androidRawId = findRawContactID(); androidRawId = findRawContactID();
if (LinphoneManager.getInstance().getContext().getResources().getBoolean(R.bool.use_linphone_tag)) { if (LinphoneManager.getInstance().getContext().getResources().getBoolean(R.bool.use_linphone_tag)) {
androidTagId = findLinphoneRawContactId(); androidTagId = findLinphoneRawContactId();
} }
for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact()) { for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact()) {
addNumberOrAddress(noa); addNumberOrAddress(noa);
} }
createOrUpdateFriend(); createOrUpdateFriend();
} else if (isLinphoneFriend()) { } else if (isLinphoneFriend()) {
fullName = friend.getName(); fullName = friend.getName();
@ -513,12 +535,12 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
return firstLetter.compareTo(contactfirstLetter); return firstLetter.compareTo(contactfirstLetter);
} }
private Uri getContactPictureUri() { private Uri getContactThumbnailPictureUri() {
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getAndroidId())); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getAndroidId()));
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
} }
private Uri getContactPhotoUri() { private Uri getContactPictureUri() {
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getAndroidId())); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getAndroidId()));
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.DISPLAY_PHOTO); return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
} }