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;
|
package org.linphone;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.linphone.core.LinphoneFriend;
|
import org.linphone.core.LinphoneFriend;
|
||||||
|
@ -51,7 +52,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;
|
||||||
|
@ -273,16 +274,25 @@ 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();
|
int size = contactsList.getAdapter().getCount();
|
||||||
|
|
||||||
for (int i = size - 1; i >= 0; i--) {
|
for (int i = size - 1; i >= 0; i--) {
|
||||||
if (contactsList.isItemChecked(i)) {
|
if (contactsList.isItemChecked(i)) {
|
||||||
LinphoneContact contact = (LinphoneContact) contactsList.getAdapter().getItem(i);
|
LinphoneContact contact = (LinphoneContact) contactsList.getAdapter().getItem(i);
|
||||||
|
if (contact.isAndroidContact()) {
|
||||||
|
contact.deleteFriend();
|
||||||
|
ids.add(contact.getAndroidId());
|
||||||
|
} else {
|
||||||
contact.delete();
|
contact.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ContactsManager.getInstance().deleteMultipleContactsAtOnce(ids);
|
||||||
|
}
|
||||||
|
|
||||||
public void quitEditMode(){
|
public void quitEditMode(){
|
||||||
isEditMode = false;
|
isEditMode = false;
|
||||||
editList.setVisibility(View.GONE);
|
editList.setVisibility(View.GONE);
|
||||||
|
@ -369,6 +379,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 +399,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());
|
||||||
|
|
|
@ -29,6 +29,7 @@ import org.linphone.mediastream.Log;
|
||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
|
import android.content.ContentProviderOperation;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
|
@ -52,6 +53,15 @@ public class ContactsManager extends ContentObserver {
|
||||||
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 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() {
|
private static Handler handler = new Handler() {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
|
@ -59,12 +69,16 @@ public class ContactsManager extends ContentObserver {
|
||||||
if (msg.what == CONTACTS_UPDATED && msg.obj instanceof List<?>) {
|
if (msg.what == CONTACTS_UPDATED && msg.obj instanceof List<?>) {
|
||||||
List<LinphoneContact> c = (List<LinphoneContact>) msg.obj;
|
List<LinphoneContact> c = (List<LinphoneContact>) msg.obj;
|
||||||
ContactsManager.getInstance().setContacts(c);
|
ContactsManager.getInstance().setContacts(c);
|
||||||
|
for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
|
||||||
|
listener.onContactsUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private ContactsManager(Handler handler) {
|
private ContactsManager(Handler handler) {
|
||||||
super(handler);
|
super(handler);
|
||||||
|
contactsUpdatedListeners = new ArrayList<ContactsUpdatedListener>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -157,10 +171,6 @@ public class ContactsManager extends ContentObserver {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void removeContact(LinphoneContact linphoneContact) {
|
|
||||||
contacts.remove(linphoneContact);
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void setContacts(List<LinphoneContact> c) {
|
public synchronized void setContacts(List<LinphoneContact> c) {
|
||||||
contacts = c;
|
contacts = c;
|
||||||
}
|
}
|
||||||
|
@ -224,4 +234,27 @@ public class ContactsManager extends ContentObserver {
|
||||||
}
|
}
|
||||||
return null;
|
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) {
|
} catch (Exception e) {
|
||||||
Log.e(e);
|
Log.e(e);
|
||||||
}
|
}
|
||||||
ContactsManager.getInstance().removeContact(this);
|
|
||||||
}
|
}
|
||||||
|
deleteFriend();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFriend() {
|
||||||
if (friend != null) {
|
if (friend != null) {
|
||||||
LinphoneManager.getLcIfManagerNotDestroyedOrNull().removeFriend(friend);
|
LinphoneManager.getLcIfManagerNotDestroyedOrNull().removeFriend(friend);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue