From 5439d4ec74028fe55e5fb03372f17257d3acd1c9 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Wed, 26 Oct 2016 21:50:14 +0200 Subject: Remove spans on copying or pasting a text --- .../java/de/pixart/messenger/ui/EditMessage.java | 36 ++++++++++++ .../messenger/ui/adapter/MessageAdapter.java | 19 ++++-- .../pixart/messenger/ui/widget/CopyTextView.java | 68 ++++++++++++++++++++++ 3 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/pixart/messenger/ui/widget/CopyTextView.java (limited to 'src/main/java/de/pixart') diff --git a/src/main/java/de/pixart/messenger/ui/EditMessage.java b/src/main/java/de/pixart/messenger/ui/EditMessage.java index 5415767e9..ea56e4d56 100644 --- a/src/main/java/de/pixart/messenger/ui/EditMessage.java +++ b/src/main/java/de/pixart/messenger/ui/EditMessage.java @@ -1,7 +1,11 @@ package de.pixart.messenger.ui; import android.content.Context; +import android.os.Build; import android.os.Handler; +import android.text.Editable; +import android.text.InputFilter; +import android.text.Spanned; import android.util.AttributeSet; import android.view.KeyEvent; @@ -89,4 +93,36 @@ public class EditMessage extends EmojiconEditText { boolean onTabPressed(boolean repeated); } + private static final InputFilter SPAN_FILTER = new InputFilter() { + + @Override + public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { + return source instanceof Spanned ? source.toString() : source; + } + }; + + @Override + public boolean onTextContextMenuItem(int id) { + if (id == android.R.id.paste) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + return super.onTextContextMenuItem(android.R.id.pasteAsPlainText); + } else { + Editable editable = getEditableText(); + InputFilter[] filters = editable.getFilters(); + InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1]; + if (filters != null) { + System.arraycopy(filters, 0, tempFilters, 1, filters.length); + } + tempFilters[0] = SPAN_FILTER; + editable.setFilters(tempFilters); + try { + return super.onTextContextMenuItem(id); + } finally { + editable.setFilters(filters); + } + } + } else { + return super.onTextContextMenuItem(id); + } + } } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 6dc6a9ed1..9e9ccb944 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -57,13 +57,14 @@ import de.pixart.messenger.persistance.FileBackend; import de.pixart.messenger.ui.ConversationActivity; import de.pixart.messenger.ui.ShowFullscreenMessageActivity; import de.pixart.messenger.ui.widget.ClickableMovementMethod; +import de.pixart.messenger.ui.widget.CopyTextView; import de.pixart.messenger.ui.widget.ListSelectionManager; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.GeoHelper; import de.pixart.messenger.utils.UIHelper; import nl.changer.audiowife.AudioWife; -public class MessageAdapter extends ArrayAdapter { +public class MessageAdapter extends ArrayAdapter implements CopyTextView.CopyHandler { private static final int SENT = 0; private static final int RECEIVED = 1; @@ -548,7 +549,7 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator); viewHolder.image = (ImageView) view .findViewById(R.id.message_image); - viewHolder.messageBody = (TextView) view + viewHolder.messageBody = (CopyTextView) view .findViewById(R.id.message_body); viewHolder.time = (TextView) view .findViewById(R.id.message_time); @@ -572,7 +573,7 @@ public class MessageAdapter extends ArrayAdapter { viewHolder.edit_indicator = (ImageView) view.findViewById(R.id.edit_indicator); viewHolder.image = (ImageView) view .findViewById(R.id.message_image); - viewHolder.messageBody = (TextView) view + viewHolder.messageBody = (CopyTextView) view .findViewById(R.id.message_body); viewHolder.time = (TextView) view .findViewById(R.id.message_time); @@ -591,7 +592,10 @@ public class MessageAdapter extends ArrayAdapter { viewHolder = null; break; } - if (viewHolder.messageBody != null) listSelectionManager.onCreate(viewHolder.messageBody); + if (viewHolder.messageBody != null) { + listSelectionManager.onCreate(viewHolder.messageBody); + viewHolder.messageBody.setCopyHandler(this); + } view.setTag(viewHolder); if (viewHolder == null) { return view; @@ -755,6 +759,11 @@ public class MessageAdapter extends ArrayAdapter { listSelectionManager.onAfterNotifyDataSetChanged(); } + @Override + public String transformTextForCopy(CharSequence text, int start, int end) { + return text.toString().substring(start, end); + } + public void openDownloadable(Message message) { DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); if (!file.exists()) { @@ -853,7 +862,7 @@ public class MessageAdapter extends ArrayAdapter { protected ImageView indicatorReceived; protected ImageView indicatorRead; protected TextView time; - protected TextView messageBody; + protected CopyTextView messageBody; protected ImageView contact_picture; protected TextView status_message; protected TextView encryption; diff --git a/src/main/java/de/pixart/messenger/ui/widget/CopyTextView.java b/src/main/java/de/pixart/messenger/ui/widget/CopyTextView.java new file mode 100644 index 000000000..d7099b786 --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/widget/CopyTextView.java @@ -0,0 +1,68 @@ +package de.pixart.messenger.ui.widget; + +import android.annotation.TargetApi; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.os.Build; +import android.util.AttributeSet; +import android.widget.TextView; + +import github.ankushsachdeva.emojicon.EmojiconTextView; + +public class CopyTextView extends EmojiconTextView { + + public CopyTextView(Context context) { + super(context); + } + + public CopyTextView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public CopyTextView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @SuppressWarnings("unused") + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public CopyTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr); //, defStyleRes); + } + + public interface CopyHandler { + public String transformTextForCopy(CharSequence text, int start, int end); + } + + private CopyHandler copyHandler; + + public void setCopyHandler(CopyHandler copyHandler) { + this.copyHandler = copyHandler; + } + + @Override + public boolean onTextContextMenuItem(int id) { + CharSequence text = getText(); + int min = 0; + int max = text.length(); + if (isFocused()) { + final int selStart = getSelectionStart(); + final int selEnd = getSelectionEnd(); + min = Math.max(0, Math.min(selStart, selEnd)); + max = Math.max(0, Math.max(selStart, selEnd)); + } + String textForCopy = null; + if (id == android.R.id.copy && copyHandler != null) { + textForCopy = copyHandler.transformTextForCopy(getText(), min, max); + } + try { + return super.onTextContextMenuItem(id); + } finally { + if (textForCopy != null) { + ClipboardManager clipboard = (ClipboardManager) getContext(). + getSystemService(Context.CLIPBOARD_SERVICE); + clipboard.setPrimaryClip(ClipData.newPlainText(null, textForCopy)); + } + } + } +} \ No newline at end of file -- cgit v1.2.3