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
|
* @author Sylvain Berfini
|
||||||
*/
|
*/
|
||||||
public class ContactDetailsFragment extends Fragment implements OnClickListener {
|
public class ContactDetailsFragment extends Fragment implements OnClickListener, ContactsUpdatedListener {
|
||||||
private LinphoneContact contact;
|
private LinphoneContact contact;
|
||||||
private ImageView editContact, deleteContact, back;
|
private ImageView editContact, deleteContact, back;
|
||||||
private LayoutInflater inflater;
|
private LayoutInflater inflater;
|
||||||
|
@ -107,6 +107,12 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
||||||
displayContact(inflater, view);
|
displayContact(inflater, view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onContactsUpdated() {
|
||||||
|
contact.refresh();
|
||||||
|
displayContact(inflater, view);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("InflateParams")
|
@SuppressLint("InflateParams")
|
||||||
private void displayContact(LayoutInflater inflater, View view) {
|
private void displayContact(LayoutInflater inflater, View view) {
|
||||||
ImageView contactPicture = (ImageView) view.findViewById(R.id.contact_picture);
|
ImageView contactPicture = (ImageView) view.findViewById(R.id.contact_picture);
|
||||||
|
@ -205,9 +211,16 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
ContactsManager.removeContactsListener(this);
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
ContactsManager.addContactsListener(this);
|
||||||
|
|
||||||
if (LinphoneActivity.isInstanciated()) {
|
if (LinphoneActivity.isInstanciated()) {
|
||||||
LinphoneActivity.instance().selectMenu(FragmentsAvailable.CONTACT_DETAIL);
|
LinphoneActivity.instance().selectMenu(FragmentsAvailable.CONTACT_DETAIL);
|
||||||
|
@ -217,6 +230,7 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
|
||||||
//Contact has been deleted, return
|
//Contact has been deleted, return
|
||||||
LinphoneActivity.instance().displayContacts(false);
|
LinphoneActivity.instance().displayContacts(false);
|
||||||
} else {
|
} else {
|
||||||
|
contact.refresh();
|
||||||
displayContact(inflater, view);
|
displayContact(inflater, view);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ import android.widget.TextView;
|
||||||
* @author Sylvain Berfini
|
* @author Sylvain Berfini
|
||||||
*/
|
*/
|
||||||
@SuppressLint("DefaultLocale")
|
@SuppressLint("DefaultLocale")
|
||||||
public class ContactsListFragment extends Fragment implements OnClickListener, OnItemClickListener {
|
public class ContactsListFragment extends Fragment implements OnClickListener, OnItemClickListener, ContactsUpdatedListener {
|
||||||
private LayoutInflater mInflater;
|
private LayoutInflater mInflater;
|
||||||
private ListView contactsList;
|
private ListView contactsList;
|
||||||
private TextView noSipContact, noContact;
|
private TextView noSipContact, noContact;
|
||||||
|
@ -369,6 +369,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
ContactsManager.addContactsListener(this);
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
if (editConsumed) {
|
if (editConsumed) {
|
||||||
|
@ -388,9 +389,15 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
instance = null;
|
instance = null;
|
||||||
|
ContactsManager.removeContactsListener(this);
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onContactsUpdated() {
|
||||||
|
invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
public void invalidate() {
|
public void invalidate() {
|
||||||
if (searchField != null && searchField.getText().toString().length() > 0) {
|
if (searchField != null && searchField.getText().toString().length() > 0) {
|
||||||
searchContacts(searchField.getText().toString());
|
searchContacts(searchField.getText().toString());
|
||||||
|
|
|
@ -31,27 +31,74 @@ import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.database.ContentObserver;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
import android.provider.ContactsContract.Data;
|
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 static ContactsManager instance;
|
||||||
private List<LinphoneContact> contacts;
|
private List<LinphoneContact> contacts;
|
||||||
//private Cursor contactCursor, sipContactCursor;
|
//private Cursor contactCursor, sipContactCursor;
|
||||||
private Account mAccount;
|
private Account mAccount;
|
||||||
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
|
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
|
||||||
private ContentResolver contentResolver;
|
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 ContactsManager() {}
|
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(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() {
|
public ContentResolver getContentResolver() {
|
||||||
return contentResolver;
|
return contentResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final synchronized ContactsManager getInstance() {
|
public static final synchronized ContactsManager getInstance() {
|
||||||
if (instance == null) instance = new ContactsManager();
|
if (instance == null) instance = new ContactsManager(handler);
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +169,10 @@ public class ContactsManager {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void setContacts(List<LinphoneContact> c) {
|
||||||
|
contacts = c;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void fetchContacts() {
|
public synchronized void fetchContacts() {
|
||||||
contacts = new ArrayList<LinphoneContact>();
|
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) {
|
public static String getAddressOrNumberForAndroidContact(ContentResolver resolver, Uri contactUri) {
|
||||||
// Phone Numbers
|
// Phone Numbers
|
||||||
String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER };
|
String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER };
|
||||||
|
|
|
@ -38,9 +38,9 @@ public class LinphoneContact implements Serializable {
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 9015568163905205244L;
|
private static final long serialVersionUID = 9015568163905205244L;
|
||||||
|
|
||||||
private LinphoneFriend friend;
|
private transient LinphoneFriend friend;
|
||||||
private String fullName, androidId;
|
private String fullName, androidId;
|
||||||
private Uri photoUri, thumbnailUri;
|
private transient Uri photoUri, thumbnailUri;
|
||||||
private List<LinphoneNumberOrAddress> addresses;
|
private List<LinphoneNumberOrAddress> addresses;
|
||||||
|
|
||||||
public LinphoneContact() {
|
public LinphoneContact() {
|
||||||
|
|
|
@ -19,7 +19,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
package org.linphone;
|
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 boolean isSIPAddress;
|
||||||
private String value;
|
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);
|
startForegroundCompat(NOTIF_ID, mNotif);
|
||||||
|
|
||||||
|
@ -265,15 +265,6 @@ public final class LinphoneService extends Service {
|
||||||
, mkeepAlivePendingIntent);
|
, mkeepAlivePendingIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ContentObserver mObserver = new ContentObserver(new Handler()) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onChange(boolean selfChange) {
|
|
||||||
super.onChange(selfChange);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
private enum IncallIconState {INCALL, PAUSE, VIDEO, IDLE}
|
private enum IncallIconState {INCALL, PAUSE, VIDEO, IDLE}
|
||||||
private IncallIconState mCurrentIncallIconState = IncallIconState.IDLE;
|
private IncallIconState mCurrentIncallIconState = IncallIconState.IDLE;
|
||||||
|
@ -581,7 +572,7 @@ public final class LinphoneService extends Service {
|
||||||
mNM.cancel(MESSAGE_NOTIF_ID);
|
mNM.cancel(MESSAGE_NOTIF_ID);
|
||||||
|
|
||||||
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).cancel(mkeepAlivePendingIntent);
|
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).cancel(mkeepAlivePendingIntent);
|
||||||
getContentResolver().unregisterContentObserver(mObserver);
|
getContentResolver().unregisterContentObserver(ContactsManager.getInstance());
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue