Improved multiple contacts deletion + fixed list update
This commit is contained in:
parent
1d879f2bf2
commit
4d976609bf
3 changed files with 61 additions and 8 deletions
|
@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
|
||||
package org.linphone;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.linphone.core.LinphoneFriend;
|
||||
|
@ -51,7 +52,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;
|
||||
|
@ -273,14 +274,23 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
|||
}
|
||||
}
|
||||
|
||||
private void removeContacts(){
|
||||
private void removeContacts() {
|
||||
ArrayList<String> ids = new ArrayList<String>();
|
||||
int size = contactsList.getAdapter().getCount();
|
||||
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
if (contactsList.isItemChecked(i)) {
|
||||
LinphoneContact contact = (LinphoneContact) contactsList.getAdapter().getItem(i);
|
||||
contact.delete();
|
||||
if (contact.isAndroidContact()) {
|
||||
contact.deleteFriend();
|
||||
ids.add(contact.getAndroidId());
|
||||
} else {
|
||||
contact.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ContactsManager.getInstance().deleteMultipleContactsAtOnce(ids);
|
||||
}
|
||||
|
||||
public void quitEditMode(){
|
||||
|
@ -369,6 +379,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
|
|||
@Override
|
||||
public void onResume() {
|
||||
instance = this;
|
||||
ContactsManager.addContactsListener(this);
|
||||
super.onResume();
|
||||
|
||||
if (editConsumed) {
|
||||
|
@ -388,9 +399,15 @@ 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) {
|
||||
searchContacts(searchField.getText().toString());
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.linphone.mediastream.Log;
|
|||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.content.ContentProviderOperation;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
|
@ -52,6 +53,15 @@ public class ContactsManager extends ContentObserver {
|
|||
private Account mAccount;
|
||||
private boolean preferLinphoneContacts = false, isContactPresenceDisabled = true, hasContactAccess = false;
|
||||
private ContentResolver contentResolver;
|
||||
|
||||
private static ArrayList<ContactsUpdatedListener> contactsUpdatedListeners;
|
||||
public static void addContactsListener(ContactsUpdatedListener listener) {
|
||||
contactsUpdatedListeners.add(listener);
|
||||
}
|
||||
public static void removeContactsListener(ContactsUpdatedListener listener) {
|
||||
contactsUpdatedListeners.remove(listener);
|
||||
}
|
||||
|
||||
private static Handler handler = new Handler() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
|
@ -59,12 +69,16 @@ public class ContactsManager extends ContentObserver {
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private ContactsManager(Handler handler) {
|
||||
super(handler);
|
||||
contactsUpdatedListeners = new ArrayList<ContactsUpdatedListener>();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -156,10 +170,6 @@ public class ContactsManager extends ContentObserver {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public synchronized void removeContact(LinphoneContact linphoneContact) {
|
||||
contacts.remove(linphoneContact);
|
||||
}
|
||||
|
||||
public synchronized void setContacts(List<LinphoneContact> c) {
|
||||
contacts = c;
|
||||
|
@ -224,4 +234,27 @@ public class ContactsManager extends ContentObserver {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void delete(String id) {
|
||||
ArrayList<String> ids = new ArrayList<String>();
|
||||
ids.add(id);
|
||||
deleteMultipleContactsAtOnce(ids);
|
||||
}
|
||||
|
||||
public void deleteMultipleContactsAtOnce(List<String> ids) {
|
||||
String select = ContactsContract.Data.CONTACT_ID + " = ?";
|
||||
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
|
||||
|
||||
for (String id : ids) {
|
||||
String[] args = new String[] { id };
|
||||
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(select, args).build());
|
||||
}
|
||||
|
||||
ContentResolver cr = ContactsManager.getInstance().getContentResolver();
|
||||
try {
|
||||
cr.applyBatch(ContactsContract.AUTHORITY, ops);
|
||||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -137,8 +137,11 @@ public class LinphoneContact implements Serializable {
|
|||
} catch (Exception e) {
|
||||
Log.e(e);
|
||||
}
|
||||
ContactsManager.getInstance().removeContact(this);
|
||||
}
|
||||
deleteFriend();
|
||||
}
|
||||
|
||||
public void deleteFriend() {
|
||||
if (friend != null) {
|
||||
LinphoneManager.getLcIfManagerNotDestroyedOrNull().removeFriend(friend);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue