Added observer to refresh contacts on changes
This commit is contained in:
parent
4193cefa60
commit
da2214936a
6 changed files with 117 additions and 19 deletions
|
@ -36,7 +36,7 @@ import android.widget.TextView;
|
|||
/**
|
||||
* @author Sylvain Berfini
|
||||
*/
|
||||
public class ContactDetailsFragment extends Fragment implements OnClickListener {
|
||||
public class ContactDetailsFragment extends Fragment implements OnClickListener, ContactsUpdatedListener {
|
||||
private LinphoneContact contact;
|
||||
private ImageView editContact, deleteContact, back;
|
||||
private LayoutInflater inflater;
|
||||
|
@ -106,6 +106,12 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
|||
contact = newContact;
|
||||
displayContact(inflater, view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContactsUpdated() {
|
||||
contact.refresh();
|
||||
displayContact(inflater, view);
|
||||
}
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
private void displayContact(LayoutInflater inflater, View view) {
|
||||
|
@ -205,9 +211,16 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
ContactsManager.removeContactsListener(this);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
ContactsManager.addContactsListener(this);
|
||||
|
||||
if (LinphoneActivity.isInstanciated()) {
|
||||
LinphoneActivity.instance().selectMenu(FragmentsAvailable.CONTACT_DETAIL);
|
||||
|
@ -217,6 +230,7 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
|||
//Contact has been deleted, return
|
||||
LinphoneActivity.instance().displayContacts(false);
|
||||
} else {
|
||||
contact.refresh();
|
||||
displayContact(inflater, view);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ import android.widget.TextView;
|
|||
* @author Sylvain Berfini
|
||||
*/
|
||||
@SuppressLint("DefaultLocale")
|
||||
public class ContactsListFragment extends Fragment implements OnClickListener, OnItemClickListener {
|
||||
public class ContactsListFragment extends Fragment implements OnClickListener, OnItemClickListener, ContactsUpdatedListener {
|
||||
private LayoutInflater mInflater;
|
||||
private ListView contactsList;
|
||||
private TextView noSipContact, noContact;
|
||||
|
@ -369,6 +369,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
|||
@Override
|
||||
public void onResume() {
|
||||
instance = this;
|
||||
ContactsManager.addContactsListener(this);
|
||||
super.onResume();
|
||||
|
||||
if (editConsumed) {
|
||||
|
@ -388,8 +389,14 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
|||
@Override
|
||||
public void onPause() {
|
||||
instance = null;
|
||||
ContactsManager.removeContactsListener(this);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContactsUpdated() {
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void invalidate() {
|
||||
if (searchField != null && searchField.getText().toString().length() > 0) {
|
||||
|
|
|
@ -31,27 +31,74 @@ import android.accounts.Account;
|
|||
import android.accounts.AccountManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.ContactsContract.Data;
|
||||
|
||||
public class ContactsManager {
|
||||
interface ContactsUpdatedListener {
|
||||
void onContactsUpdated();
|
||||
}
|
||||
|
||||
public class ContactsManager extends ContentObserver {
|
||||
private static final int CONTACTS_UPDATED = 543;
|
||||
|
||||
private static ContactsManager instance;
|
||||
private List<LinphoneContact> contacts;
|
||||
//private Cursor contactCursor, sipContactCursor;
|
||||
private Account mAccount;
|
||||
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
|
||||
private ContentResolver contentResolver;
|
||||
private static Handler handler = new Handler() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
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 : contactsListeners) {
|
||||
listener.onContactsUpdated();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static ArrayList<ContactsUpdatedListener> contactsListeners;
|
||||
public static void addContactsListener(ContactsUpdatedListener listener) {
|
||||
contactsListeners.add(listener);
|
||||
}
|
||||
public static void removeContactsListener(ContactsUpdatedListener listener) {
|
||||
contactsListeners.remove(listener);
|
||||
}
|
||||
|
||||
private ContactsManager() {}
|
||||
private ContactsManager(Handler handler) {
|
||||
super(handler);
|
||||
contactsListeners = new ArrayList<ContactsUpdatedListener>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
onChange(selfChange, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
List<LinphoneContact> contacts = fetchContactsInBackground();
|
||||
Message msg = handler.obtainMessage();
|
||||
msg.what = CONTACTS_UPDATED;
|
||||
msg.obj = contacts;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
|
||||
public ContentResolver getContentResolver() {
|
||||
return contentResolver;
|
||||
}
|
||||
|
||||
public static final synchronized ContactsManager getInstance() {
|
||||
if (instance == null) instance = new ContactsManager();
|
||||
if (instance == null) instance = new ContactsManager(handler);
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
@ -121,6 +168,10 @@ public class ContactsManager {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized void setContacts(List<LinphoneContact> c) {
|
||||
contacts = c;
|
||||
}
|
||||
|
||||
public synchronized void fetchContacts() {
|
||||
contacts = new ArrayList<LinphoneContact>();
|
||||
|
@ -148,6 +199,34 @@ public class ContactsManager {
|
|||
}
|
||||
}
|
||||
|
||||
public List<LinphoneContact> fetchContactsInBackground() {
|
||||
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
|
||||
|
||||
for (LinphoneFriend friend : LinphoneManager.getLc().getFriendList()) {
|
||||
LinphoneContact contact = new LinphoneContact();
|
||||
LinphoneAddress addr = friend.getAddress();
|
||||
contact.setFullName(addr.getDisplayName());
|
||||
contact.addNumberOrAddress(new LinphoneNumberOrAddress(addr.asStringUriOnly(), true));
|
||||
contacts.add(contact);
|
||||
}
|
||||
|
||||
if (mAccount == null || !hasContactAccess) return contacts;
|
||||
|
||||
Cursor c = Compatibility.getContactsCursor(contentResolver, null);
|
||||
if (c != null) {
|
||||
while (c.moveToNext()) {
|
||||
String id = c.getString(c.getColumnIndex(Data.CONTACT_ID));
|
||||
LinphoneContact contact = new LinphoneContact();
|
||||
contact.setAndroidId(id);
|
||||
contact.refresh();
|
||||
contacts.add(contact);
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public static String getAddressOrNumberForAndroidContact(ContentResolver resolver, Uri contactUri) {
|
||||
// Phone Numbers
|
||||
String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER };
|
||||
|
|
|
@ -38,9 +38,9 @@ public class LinphoneContact implements Serializable {
|
|||
*/
|
||||
private static final long serialVersionUID = 9015568163905205244L;
|
||||
|
||||
private LinphoneFriend friend;
|
||||
private transient LinphoneFriend friend;
|
||||
private String fullName, androidId;
|
||||
private Uri photoUri, thumbnailUri;
|
||||
private transient Uri photoUri, thumbnailUri;
|
||||
private List<LinphoneNumberOrAddress> addresses;
|
||||
|
||||
public LinphoneContact() {
|
||||
|
|
|
@ -19,7 +19,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
|
||||
package org.linphone;
|
||||
|
||||
public class LinphoneNumberOrAddress {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class LinphoneNumberOrAddress implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -2301689469730072896L;
|
||||
|
||||
private boolean isSIPAddress;
|
||||
private String value;
|
||||
|
||||
|
|
|
@ -243,7 +243,7 @@ public final class LinphoneService extends Service {
|
|||
}
|
||||
}
|
||||
|
||||
this.getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, mObserver);
|
||||
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, ContactsManager.getInstance());
|
||||
|
||||
startForegroundCompat(NOTIF_ID, mNotif);
|
||||
|
||||
|
@ -264,15 +264,6 @@ public final class LinphoneService extends Service {
|
|||
, 600000
|
||||
, mkeepAlivePendingIntent);
|
||||
}
|
||||
|
||||
private ContentObserver mObserver = new ContentObserver(new Handler()) {
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
super.onChange(selfChange);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
private enum IncallIconState {INCALL, PAUSE, VIDEO, IDLE}
|
||||
|
@ -581,7 +572,7 @@ public final class LinphoneService extends Service {
|
|||
mNM.cancel(MESSAGE_NOTIF_ID);
|
||||
|
||||
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).cancel(mkeepAlivePendingIntent);
|
||||
getContentResolver().unregisterContentObserver(mObserver);
|
||||
getContentResolver().unregisterContentObserver(ContactsManager.getInstance());
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue