diff options
author | Christian Schneppe <christian@pix-art.de> | 2016-10-26 21:50:14 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2016-10-26 21:50:14 +0200 |
commit | 5439d4ec74028fe55e5fb03372f17257d3acd1c9 (patch) | |
tree | 904f4e95c596e0f7ab57b955e55ea188aa578ec4 | |
parent | ee55f4c234cea2ee89a60c606bd20e4cce230cb2 (diff) |
Remove spans on copying or pasting a text
5 files changed, 120 insertions, 7 deletions
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<Message> { +public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextView.CopyHandler { private static final int SENT = 0; private static final int RECEIVED = 1; @@ -548,7 +549,7 @@ public class MessageAdapter extends ArrayAdapter<Message> { 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<Message> { 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<Message> { 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<Message> { 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<Message> { 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 diff --git a/src/main/res/layout/message_received.xml b/src/main/res/layout/message_received.xml index 155a2bbc2..5d5fb7c43 100644 --- a/src/main/res/layout/message_received.xml +++ b/src/main/res/layout/message_received.xml @@ -56,7 +56,7 @@ android:maxHeight="400dp" android:maxWidth="400dp" /> - <github.ankushsachdeva.emojicon.EmojiconTextView + <de.pixart.messenger.ui.widget.CopyTextView android:id="@+id/message_body" emojicon:emojiconSize="28sp" android:layout_width="wrap_content" diff --git a/src/main/res/layout/message_sent.xml b/src/main/res/layout/message_sent.xml index 7dcf0932b..5c414ed2c 100644 --- a/src/main/res/layout/message_sent.xml +++ b/src/main/res/layout/message_sent.xml @@ -57,7 +57,7 @@ android:maxHeight="400dp" android:maxWidth="400dp" /> - <github.ankushsachdeva.emojicon.EmojiconTextView + <de.pixart.messenger.ui.widget.CopyTextView android:id="@+id/message_body" emojicon:emojiconSize="28sp" android:layout_width="wrap_content" |