From f9c5f133d5eb6745e6ebcbee9cdb2207a89565d6 Mon Sep 17 00:00:00 2001 From: "daniel.boehrs" Date: Fri, 5 Oct 2018 11:54:01 +0200 Subject: [PATCH] add option to delete a single contact #37 --- res/layout/contact_selection_list_item.xml | 3 +- ..._list_batch.xml => action_mode_delete.xml} | 0 res/values/strings.xml | 1 + .../ContactSelectionListFragment.java | 95 ++++++++++- .../securesms/ConversationListFragment.java | 2 +- .../contacts/ContactSelectionListAdapter.java | 159 ++++++++++++++---- .../contacts/ContactSelectionListItem.java | 15 +- 7 files changed, 233 insertions(+), 42 deletions(-) rename res/menu/{conversation_list_batch.xml => action_mode_delete.xml} (100%) diff --git a/res/layout/contact_selection_list_item.xml b/res/layout/contact_selection_list_item.xml index 521df276c..83272640e 100644 --- a/res/layout/contact_selection_list_item.xml +++ b/res/layout/contact_selection_list_item.xml @@ -1,5 +1,4 @@ - Delta Chat requires the Contacts permission in order to display your contacts, but it has been permanently denied. Please continue to the app settings menu, select \"Permissions\", and enable \"Contacts\". Error retrieving contacts, check your network connection + Error deleting contacts, some contacts couldn\'t get deleted. Please delete existing conversations before. No blocked contacts diff --git a/src/org/thoughtcrime/securesms/ContactSelectionListFragment.java b/src/org/thoughtcrime/securesms/ContactSelectionListFragment.java index 4f6e7fe5d..523729cfc 100644 --- a/src/org/thoughtcrime/securesms/ContactSelectionListFragment.java +++ b/src/org/thoughtcrime/securesms/ContactSelectionListFragment.java @@ -30,7 +30,12 @@ import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; +import android.util.SparseIntArray; +import android.view.ActionMode; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; @@ -59,6 +64,7 @@ import org.thoughtcrime.securesms.util.ViewUtil; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.Set; /** @@ -93,6 +99,8 @@ public class ContactSelectionListFragment extends Fragment private String cursorFilter; private RecyclerView recyclerView; private RecyclerViewFastScroller fastScroller; + private ActionMode actionMode; + private ActionMode.Callback actionModeCallback; @Override public void onActivityCreated(Bundle icicle) { @@ -148,6 +156,41 @@ public class ContactSelectionListFragment extends Fragment showContactsDescription = view.findViewById(R.id.show_contacts_description); showContactsProgress = view.findViewById(R.id.progress); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + actionModeCallback = new ActionMode.Callback() { + @Override + public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { + MenuInflater inflater = getActivity().getMenuInflater(); + inflater.inflate(R.menu.action_mode_delete, menu); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + getActivity().getWindow().setStatusBarColor(getResources().getColor(R.color.action_mode_status_bar)); + } + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { + switch (menuItem.getItemId()) { + case R.id.menu_select_all: + handleSelectAll(); + return true; + case R.id.menu_delete_selected: + handleDeleteSelected(); + return true; + } + return false; + } + + @Override + public void onDestroyActionMode(ActionMode actionMode) { + ContactSelectionListFragment.this.actionMode = null; + getContactSelectionListAdapter().resetActionModeSelection(); + } + }; swipeRefresh.setEnabled(getActivity().getIntent().getBooleanExtra(REFRESHABLE, true) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN); @@ -155,6 +198,33 @@ public class ContactSelectionListFragment extends Fragment return view; } + private void handleSelectAll() { + getContactSelectionListAdapter().selectAll(); + } + + private void handleDeleteSelected() { + ContactSelectionListAdapter adapter = getContactSelectionListAdapter(); + SparseIntArray actionModeSelection = adapter.getActionModeSelection(); + boolean failed = false; + for (int index = 0; index < actionModeSelection.size(); index++) { + int contactId = actionModeSelection.valueAt(index); + boolean currentFailed = DcHelper.getContext(getContext()).deleteContact(contactId) == 0; + failed = currentFailed || failed; + if (!currentFailed) { + adapter.removeActionModeSelection(contactId); + } + } + if (failed) { + Toast.makeText(getActivity(), R.string.ContactSelectionListFragment_error_deleting_contacts_check_existing_conversations, Toast.LENGTH_LONG).show(); + } + adapter.resetActionModeSelection(); + actionMode.finish(); + } + + private ContactSelectionListAdapter getContactSelectionListAdapter() { + return (ContactSelectionListAdapter) recyclerView.getAdapter(); + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults); @@ -302,8 +372,14 @@ public class ContactSelectionListFragment extends Fragment private class ListClickListener implements ContactSelectionListAdapter.ItemClickListener { @Override - public void onItemClick(ContactSelectionListItem contact) + public void onItemClick(ContactSelectionListItem contact, boolean handleActionMode) { + if (handleActionMode) { + if (actionMode != null) { + finishActionModeIfSelectionIsEmpty(); + } + return; + } int specialId = contact.getSpecialId(); String addr = contact.getNumber(); if (!isMulti() || !selectedContacts.contains(addr)) @@ -337,9 +413,24 @@ public class ContactSelectionListFragment extends Fragment } } } + + @Override + public void onItemLongClick(ContactSelectionListItem view) { + if (actionMode == null) { + actionMode = Objects.requireNonNull(getActivity()).startActionMode(actionModeCallback); + } else { + finishActionModeIfSelectionIsEmpty(); + } + } } - public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) { + private void finishActionModeIfSelectionIsEmpty() { + if (getContactSelectionListAdapter().getActionModeSelection().size() == 0) { + actionMode.finish(); + } + } + + public void setOnContactSelectedListener(OnContactSelectedListener onContactSelectedListener) { this.onContactSelectedListener = onContactSelectedListener; } diff --git a/src/org/thoughtcrime/securesms/ConversationListFragment.java b/src/org/thoughtcrime/securesms/ConversationListFragment.java index 01beb5948..93d1f7f55 100644 --- a/src/org/thoughtcrime/securesms/ConversationListFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationListFragment.java @@ -410,7 +410,7 @@ public class ConversationListFragment extends Fragment if (archive) inflater.inflate(R.menu.conversation_list_batch_unarchive, menu); else inflater.inflate(R.menu.conversation_list_batch_archive, menu); - inflater.inflate(R.menu.conversation_list_batch, menu); + inflater.inflate(R.menu.action_mode_delete, menu); mode.setTitle("1"); diff --git a/src/org/thoughtcrime/securesms/contacts/ContactSelectionListAdapter.java b/src/org/thoughtcrime/securesms/contacts/ContactSelectionListAdapter.java index 653b6f50a..d08db4284 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactSelectionListAdapter.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactSelectionListAdapter.java @@ -22,6 +22,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; +import android.util.SparseIntArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -78,14 +79,14 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter private final ItemClickListener clickListener; private final GlideRequests glideRequests; private final Set selectedContacts = new HashSet<>(); // TODO: maybe better use contact-id here + private final SparseIntArray actionModeSelection = new SparseIntArray(); @Override public int getItemCount() { return dcContactList.length; } - private - @NonNull DcContact getContact(int position) { + private @NonNull DcContact getContact(int position) { if(position<0 || position>=dcContactList.length) { return new DcContact(0); } @@ -103,33 +104,93 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter return fromDb; } + public void resetActionModeSelection() { + actionModeSelection.clear(); + notifyDataSetChanged(); + } + + public void selectAll() { + actionModeSelection.clear(); + for(int index = 0; index < dcContactList.length; index++) { + int value = dcContactList[index]; + if (value > 0) { + actionModeSelection.put(index, value); + } + } + notifyDataSetChanged(); + } + + public void removeActionModeSelection(int contactId) { + int indexOfContactId = actionModeSelection.indexOfValue(contactId); + actionModeSelection.removeAt(indexOfContactId); + } + + private boolean isActionModeEnabled() { + return actionModeSelection.size() != 0; + } + public abstract static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } - public abstract void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect); + public abstract void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled); public abstract void unbind(@NonNull GlideRequests glideRequests); public abstract void setChecked(boolean checked); - } + public abstract void setSelected(boolean enabled); + public abstract void setEnabled(boolean enabled); + } + + public class ContactViewHolder extends ViewHolder { - public static class ContactViewHolder extends ViewHolder { ContactViewHolder(@NonNull final View itemView, - @Nullable final ItemClickListener clickListener) - { + @Nullable final ItemClickListener clickListener) { super(itemView); - itemView.setOnClickListener(v -> { - if (clickListener != null) clickListener.onItemClick(getView()); + itemView.setOnClickListener(view -> { + if (clickListener != null) { + if (isActionModeEnabled()) { + toggleSelection(); + clickListener.onItemClick(getView(), true); + } else { + clickListener.onItemClick(getView(), false); + } + } }); + itemView.setOnLongClickListener(view -> { + if (clickListener != null) { + int contactId = getContactId(getAdapterPosition()); + if (contactId > 0) { + toggleSelection(); + clickListener.onItemLongClick(getView()); + } + } + return true; + }); + } + + private int getContactId(int adapterPosition) { + return ContactSelectionListAdapter.this.dcContactList[adapterPosition]; + } + + private void toggleSelection() { + int adapterPosition = getAdapterPosition(); + int contactId = getContactId(adapterPosition); + boolean enabled = actionModeSelection.indexOfKey(adapterPosition) > -1; + if (enabled) { + ContactSelectionListAdapter.this.actionModeSelection.delete(adapterPosition); + } else { + ContactSelectionListAdapter.this.actionModeSelection.put(adapterPosition, contactId); + } + notifyDataSetChanged(); } public ContactSelectionListItem getView() { return (ContactSelectionListItem) itemView; } - public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String addr, String label, int color, boolean multiSelect) { - getView().set(glideRequests, type, contact, name, addr, label, color, multiSelect); + public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String addr, String label, int color, boolean multiSelect, boolean enabled) { + getView().set(glideRequests, type, contact, name, addr, label, color, multiSelect, enabled); } @Override @@ -141,6 +202,16 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter public void setChecked(boolean checked) { getView().setChecked(checked); } + + @Override + public void setSelected(boolean enabled) { + getView().setSelected(enabled); + } + + @Override + public void setEnabled(boolean enabled) { + getView().setEnabled(enabled); + } } public static class DividerViewHolder extends ViewHolder { @@ -153,7 +224,7 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter } @Override - public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect) { + public void bind(@NonNull GlideRequests glideRequests, int type, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled) { this.label.setText(name); } @@ -162,6 +233,15 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter @Override public void setChecked(boolean checked) {} + + @Override + public void setSelected(boolean enabled) { + } + + @Override + public void setEnabled(boolean enabled) { + + } } static class HeaderViewHolder extends RecyclerView.ViewHolder { @@ -197,40 +277,46 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { - int id = dcContactList[i]; - int color = drawables.getColor(0, 0xff000000); + int id = dcContactList[i]; + int color = drawables.getColor(0, 0xff000000); DcContact dcContact = null; - String label = null; - String name; - String addr = null; - boolean itemMultiSelect = multiSelect; + String label = null; + String name; + String addr = null; + boolean itemMultiSelect = multiSelect; - if(id==DcContact.DC_CONTACT_ID_NEW_CONTACT) { + if (id == DcContact.DC_CONTACT_ID_NEW_CONTACT) { name = context.getString(R.string.contact_selection_list__new_contact); itemMultiSelect = false; // the item creates a new contact in the list that will be selected instead - if(dcContext.mayBeValidAddr(query)) { + if (dcContext.mayBeValidAddr(query)) { addr = query == null ? "" : query; label = "\u2026"; - } - else { + } else { addr = context.getString(R.string.contact_selection_list__type_email_above); } - } - else if(id==DcContact.DC_CONTACT_ID_NEW_GROUP) { + } else if (id == DcContact.DC_CONTACT_ID_NEW_GROUP) { name = context.getString(R.string.contact_selection_list__new_group); - } - else if(id==DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) { + } else if (id == DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) { name = context.getString(R.string.contact_selection_list__new_verified_group); - } - else { + } else { dcContact = getContact(i); - name = dcContact.getDisplayName(); - addr = dcContact.getAddr(); + name = dcContact.getDisplayName(); + addr = dcContact.getAddr(); } - ViewHolder holder = (ViewHolder)viewHolder; + ViewHolder holder = (ViewHolder) viewHolder; holder.unbind(glideRequests); - holder.bind(glideRequests, id, dcContact, name, addr, label, color, itemMultiSelect); + boolean enabled = true; + if (dcContact == null) { + holder.setEnabled(!isActionModeEnabled()); + if (isActionModeEnabled()) { + enabled = false; + } + } else { + boolean selected = actionModeSelection.indexOfValue(id) > -1; + holder.setSelected(selected); + } + holder.bind(glideRequests, id, dcContact, name, addr, label, color, itemMultiSelect, enabled); holder.setChecked(selectedContacts.contains(addr)); } @@ -248,10 +334,15 @@ public class ContactSelectionListAdapter extends RecyclerView.Adapter return selectedContacts; } - public interface ItemClickListener { - void onItemClick(ContactSelectionListItem item); + public SparseIntArray getActionModeSelection() { + return actionModeSelection; } + public interface ItemClickListener { + void onItemClick(ContactSelectionListItem item, boolean handleActionMode); + + void onItemLongClick(ContactSelectionListItem view); + } @Override public long getHeaderId(int position) { diff --git a/src/org/thoughtcrime/securesms/contacts/ContactSelectionListItem.java b/src/org/thoughtcrime/securesms/contacts/ContactSelectionListItem.java index 3d3b02d8b..541f7cfab 100644 --- a/src/org/thoughtcrime/securesms/contacts/ContactSelectionListItem.java +++ b/src/org/thoughtcrime/securesms/contacts/ContactSelectionListItem.java @@ -17,7 +17,6 @@ import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.mms.GlideRequests; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; -import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.ViewUtil; @@ -59,7 +58,7 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM ViewUtil.setTextViewGravityStart(this.nameView, getContext()); } - public void set(@NonNull GlideRequests glideRequests, int specialId, DcContact contact, String name, String number, String label, int color, boolean multiSelect) { + public void set(@NonNull GlideRequests glideRequests, int specialId, DcContact contact, String name, String number, String label, int color, boolean multiSelect, boolean enabled) { this.glideRequests = glideRequests; this.specialId = specialId; this.number = number; @@ -67,15 +66,20 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM if(specialId==DcContact.DC_CONTACT_ID_NEW_CONTACT || specialId==DcContact.DC_CONTACT_ID_NEW_GROUP || specialId==DcContact.DC_CONTACT_ID_NEW_VERIFIED_GROUP) { this.recipient = null; this.contactPhotoImage.setAvatar(glideRequests, Recipient.from(getContext(), Address.UNKNOWN, true), false); + if (!enabled) { + this.setBackgroundColor(getResources().getColor(R.color.gray27)); + } else { + resetBackground(); + } } else { + resetBackground(); this.recipient = DcHelper.getContext(getContext()).getRecipient(contact); this.recipient.addListener(this); if (this.recipient.getName() != null) { name = this.recipient.getName(); } } - this.nameView.setTextColor(color); this.numberView.setTextColor(color); this.contactPhotoImage.setAvatar(glideRequests, recipient, false); @@ -86,6 +90,11 @@ public class ContactSelectionListItem extends LinearLayout implements RecipientM else this.checkBox.setVisibility(View.GONE); } + private void resetBackground() { + this.setBackgroundColor(getResources().getColor(R.color.transparent)); + this.setBackgroundResource(R.drawable.conversation_list_item_background); + } + public void setChecked(boolean selected) { this.checkBox.setChecked(selected); }