Prevent pasting styled email addresses

This commit is contained in:
M66B 2023-01-24 22:06:17 +01:00
parent ba611dd78c
commit 49b18f2b57
2 changed files with 33 additions and 1 deletions

View File

@ -19,6 +19,9 @@ package eu.faircode.email;
Copyright 2018-2023 by Marcel Bokhorst (M66B) Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/ */
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
@ -221,6 +224,32 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
setSelection(edit.length()); setSelection(edit.length());
} }
@Override
public boolean onTextContextMenuItem(int id) {
try {
if (id == android.R.id.paste) {
Context context = getContext();
ClipboardManager cbm = Helper.getSystemService(context, ClipboardManager.class);
if (cbm != null && cbm.hasPrimaryClip()) {
ClipData data = cbm.getPrimaryClip();
ClipDescription description = (data == null ? null : data.getDescription());
ClipData.Item item = (data == null ? null : data.getItemAt(0));
CharSequence text = (item == null ? null : item.coerceToText(context));
if (text != null) {
CharSequence label = (description == null ? "coerced_plain_text" : description.getLabel());
data = ClipData.newPlainText(label, text.toString());
cbm.setPrimaryClip(data);
}
}
}
return super.onTextContextMenuItem(id);
} catch (Throwable ex) {
Log.e(ex);
return false;
}
}
private final Runnable update = new Runnable() { private final Runnable update = new Runnable() {
@Override @Override
public void run() { public void run() {

View File

@ -20,6 +20,7 @@ package eu.faircode.email;
*/ */
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
@ -126,10 +127,12 @@ public class EditTextPlain extends FixedEditText {
ClipboardManager cbm = Helper.getSystemService(context, ClipboardManager.class); ClipboardManager cbm = Helper.getSystemService(context, ClipboardManager.class);
if (cbm != null && cbm.hasPrimaryClip()) { if (cbm != null && cbm.hasPrimaryClip()) {
ClipData data = cbm.getPrimaryClip(); ClipData data = cbm.getPrimaryClip();
ClipDescription description = (data == null ? null : data.getDescription());
ClipData.Item item = (data == null ? null : data.getItemAt(0)); ClipData.Item item = (data == null ? null : data.getItemAt(0));
CharSequence text = (item == null ? null : item.coerceToText(context)); CharSequence text = (item == null ? null : item.coerceToText(context));
if (text != null) { if (text != null) {
data = ClipData.newPlainText("coerced_plain_text", text.toString()); CharSequence label = (description == null ? "coerced_plain_text" : description.getLabel());
data = ClipData.newPlainText(label, text.toString());
cbm.setPrimaryClip(data); cbm.setPrimaryClip(data);
} }
} }