From de17e77ef889eff80d3bd9e6901e618645eab638 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 26 Jul 2020 07:58:12 +0200 Subject: [PATCH] Allow pasting raw signature --- .../eu/faircode/email/ActivitySignature.java | 15 ++--- .../eu/faircode/email/EditTextCompose.java | 64 +++++++++++-------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivitySignature.java b/app/src/main/java/eu/faircode/email/ActivitySignature.java index 8d7123eeb8..68a5d9517e 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySignature.java +++ b/app/src/main/java/eu/faircode/email/ActivitySignature.java @@ -54,7 +54,6 @@ public class ActivitySignature extends ActivityBase { private BottomNavigationView style_bar; private BottomNavigationView bottom_navigation; - private boolean raw = false; private boolean dirty = false; private static final int REQUEST_IMAGE = 1; @@ -64,7 +63,7 @@ public class ActivitySignature extends ActivityBase { super.onCreate(savedInstanceState); if (savedInstanceState != null) - raw = savedInstanceState.getBoolean("fair:raw"); + etText.setRaw(savedInstanceState.getBoolean("fair:raw")); getSupportActionBar().setSubtitle(getString(R.string.title_edit_signature)); @@ -79,7 +78,7 @@ public class ActivitySignature extends ActivityBase { etText.setSelectionListener(new EditTextCompose.ISelection() { @Override public void onSelected(boolean selection) { - style_bar.setVisibility(selection && !raw ? View.VISIBLE : View.GONE); + style_bar.setVisibility(selection && !etText.getRaw() ? View.VISIBLE : View.GONE); } }); @@ -142,7 +141,7 @@ public class ActivitySignature extends ActivityBase { @Override protected void onSaveInstanceState(Bundle outState) { - outState.putBoolean("fair:raw", raw); + outState.putBoolean("fair:raw", etText.getRaw()); super.onSaveInstanceState(outState); } @@ -186,7 +185,7 @@ public class ActivitySignature extends ActivityBase { String html = getIntent().getStringExtra("html"); if (html == null) etText.setText(null); - else if (raw) + else if (etText.getRaw()) etText.setText(html); else etText.setText(HtmlHelper.fromHtml(html, false, new Html.ImageGetter() { @@ -207,7 +206,7 @@ public class ActivitySignature extends ActivityBase { private void save() { etText.clearComposingText(); - String html = (raw + String html = (etText.getRaw() ? etText.getText().toString() : HtmlHelper.toHtml(etText.getText(), this)); Intent result = new Intent(); @@ -217,7 +216,7 @@ public class ActivitySignature extends ActivityBase { } private void html(boolean raw) { - this.raw = raw; + etText.setRaw(raw); if (!raw || dirty) { String html = (raw @@ -288,7 +287,7 @@ public class ActivitySignature extends ActivityBase { getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); int start = etText.getSelectionStart(); - if (raw) + if (etText.getRaw()) etText.getText().insert(start, ""); else { SpannableStringBuilder ssb = new SpannableStringBuilder(etText.getText()); diff --git a/app/src/main/java/eu/faircode/email/EditTextCompose.java b/app/src/main/java/eu/faircode/email/EditTextCompose.java index 0eacf6a581..273543db12 100644 --- a/app/src/main/java/eu/faircode/email/EditTextCompose.java +++ b/app/src/main/java/eu/faircode/email/EditTextCompose.java @@ -41,6 +41,7 @@ import androidx.core.view.inputmethod.InputContentInfoCompat; import org.jsoup.nodes.Document; public class EditTextCompose extends FixedEditText { + private boolean raw = false; private ISelection selectionListener = null; private IInputContentListener inputContentListener = null; @@ -56,6 +57,14 @@ public class EditTextCompose extends FixedEditText { super(context, attrs, defStyleAttr); } + public void setRaw(boolean raw) { + this.raw = raw; + } + + public boolean getRaw() { + return raw; + } + @Override protected void onSelectionChanged(int selStart, int selEnd) { super.onSelectionChanged(selStart, selEnd); @@ -80,32 +89,37 @@ public class EditTextCompose extends FixedEditText { html = "
" + HtmlHelper.formatPre(text.toString(), false) + "
"; } - Document document = HtmlHelper.sanitizeCompose(context, html, false); - Spanned paste = HtmlHelper.fromDocument(getContext(), document, true, new Html.ImageGetter() { - @Override - public Drawable getDrawable(String source) { - return ImageHelper.decodeImage(getContext(), - -1, source, true, 0, EditTextCompose.this); + SpannableStringBuilder ssb; + if (raw) + ssb = new SpannableStringBuilder(html); + else { + Document document = HtmlHelper.sanitizeCompose(context, html, false); + Spanned paste = HtmlHelper.fromDocument(getContext(), document, true, new Html.ImageGetter() { + @Override + public Drawable getDrawable(String source) { + return ImageHelper.decodeImage(getContext(), + -1, source, true, 0, EditTextCompose.this); + } + }, null); + + int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary); + int dp3 = Helper.dp2pixels(context, 3); + int dp6 = Helper.dp2pixels(context, 6); + + ssb = new SpannableStringBuilder(paste); + QuoteSpan[] spans = ssb.getSpans(0, ssb.length(), QuoteSpan.class); + for (QuoteSpan span : spans) { + QuoteSpan q; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) + q = new QuoteSpan(colorPrimary); + else + q = new QuoteSpan(colorPrimary, dp3, dp6); + ssb.setSpan(q, + ssb.getSpanStart(span), + ssb.getSpanEnd(span), + ssb.getSpanFlags(span)); + ssb.removeSpan(span); } - }, null); - - int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary); - int dp3 = Helper.dp2pixels(context, 3); - int dp6 = Helper.dp2pixels(context, 6); - - SpannableStringBuilder ssb = new SpannableStringBuilder(paste); - QuoteSpan[] spans = ssb.getSpans(0, ssb.length(), QuoteSpan.class); - for (QuoteSpan span : spans) { - QuoteSpan q; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) - q = new QuoteSpan(colorPrimary); - else - q = new QuoteSpan(colorPrimary, dp3, dp6); - ssb.setSpan(q, - ssb.getSpanStart(span), - ssb.getSpanEnd(span), - ssb.getSpanFlags(span)); - ssb.removeSpan(span); } int start = getSelectionStart();