FairEmail/app/src/main/java/eu/faircode/email/EditTextCompose.java

184 lines
7.0 KiB
Java
Raw Normal View History

2019-03-05 08:05:46 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-09-25 18:48:57 +00:00
import android.content.ClipData;
import android.content.ClipboardManager;
2019-03-05 08:05:46 +00:00
import android.content.Context;
2019-05-04 07:28:38 +00:00
import android.net.Uri;
2019-03-05 08:05:46 +00:00
import android.os.Build;
2019-05-04 07:28:38 +00:00
import android.os.Bundle;
2019-11-17 12:04:05 +00:00
import android.text.SpannableStringBuilder;
2019-09-25 18:48:57 +00:00
import android.text.Spanned;
2019-11-17 12:04:05 +00:00
import android.text.style.QuoteSpan;
2019-03-05 08:05:46 +00:00
import android.util.AttributeSet;
2019-05-04 07:28:38 +00:00
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
2019-03-05 08:05:46 +00:00
import androidx.appcompat.widget.AppCompatEditText;
2019-05-04 07:28:38 +00:00
import androidx.core.view.inputmethod.EditorInfoCompat;
import androidx.core.view.inputmethod.InputConnectionCompat;
import androidx.core.view.inputmethod.InputContentInfoCompat;
2019-03-05 08:05:46 +00:00
2019-11-19 20:53:12 +00:00
import org.jsoup.nodes.Document;
public class EditTextCompose extends AppCompatEditText {
2019-09-26 12:55:15 +00:00
private ISelection selectionListener = null;
private IInputContentListener inputContentListener = null;
2019-05-04 07:28:38 +00:00
2019-03-05 08:05:46 +00:00
public EditTextCompose(Context context) {
super(context);
}
public EditTextCompose(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextCompose(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
2019-09-26 12:55:15 +00:00
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
if (selectionListener != null)
selectionListener.onSelected(hasSelection());
}
2019-03-05 08:05:46 +00:00
@Override
public boolean onTextContextMenuItem(int id) {
2019-09-06 13:54:07 +00:00
try {
2019-09-25 18:48:57 +00:00
if (id == android.R.id.paste) {
Context context = getContext();
2020-02-23 10:16:40 +00:00
ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (cbm != null && cbm.hasPrimaryClip()) {
ClipData.Item item = cbm.getPrimaryClip().getItemAt(0);
2019-09-25 18:48:57 +00:00
2019-12-27 11:34:01 +00:00
String html = item.getHtmlText();
2020-01-24 13:20:24 +00:00
if (html == null) {
CharSequence text = item.getText();
if (text == null)
return false;
html = "<div>" + HtmlHelper.formatPre(text.toString()) + "</div>";
}
2020-03-25 19:25:06 +00:00
Document document = HtmlHelper.sanitizeCompose(context, html, false);
2019-11-19 20:53:12 +00:00
Spanned paste = HtmlHelper.fromHtml(document.html());
2019-09-25 18:48:57 +00:00
2019-11-17 12:04:05 +00:00
int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
SpannableStringBuilder ssb = new SpannableStringBuilder(paste);
QuoteSpan[] spans = ssb.getSpans(0, ssb.length(), QuoteSpan.class);
for (QuoteSpan span : spans) {
ssb.setSpan(
new StyledQuoteSpan(context, colorPrimary),
ssb.getSpanStart(span),
ssb.getSpanEnd(span),
2020-01-30 08:31:41 +00:00
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2019-11-17 12:04:05 +00:00
ssb.removeSpan(span);
}
2019-09-25 18:48:57 +00:00
int start = getSelectionStart();
int end = getSelectionEnd();
if (start < 0)
start = 0;
if (end < 0)
end = 0;
if (start > end) {
int tmp = start;
start = end;
end = tmp;
}
if (start == end)
2019-11-17 12:04:05 +00:00
getText().insert(start, ssb);
2019-09-25 18:48:57 +00:00
else
2019-11-17 12:04:05 +00:00
getText().replace(start, end, ssb);
2019-09-25 18:48:57 +00:00
return true;
}
}
return super.onTextContextMenuItem(id);
2019-09-06 13:54:07 +00:00
} catch (Throwable ex) {
/*
java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:619)
at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:391)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
at android.widget.TextView.paste(TextView.java:8891)
at android.widget.TextView.onTextContextMenuItem(TextView.java:8706)
*/
Log.w(ex);
return false;
}
2019-03-05 08:05:46 +00:00
}
2019-05-04 07:28:38 +00:00
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
//https://developer.android.com/guide/topics/text/image-keyboard
InputConnection ic = super.onCreateInputConnection(editorInfo);
2019-05-06 19:04:44 +00:00
if (ic == null)
return null;
2019-05-04 07:28:38 +00:00
EditorInfoCompat.setContentMimeTypes(editorInfo, new String[]{"image/*"});
return InputConnectionCompat.createWrapper(ic, editorInfo, new InputConnectionCompat.OnCommitContentListener() {
@Override
public boolean onCommitContent(InputContentInfoCompat info, int flags, Bundle opts) {
Log.i("Uri=" + info.getContentUri());
try {
2019-09-26 12:55:15 +00:00
if (inputContentListener == null)
2019-05-04 07:28:38 +00:00
throw new IllegalArgumentException("InputContent listener not set");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 &&
(flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0)
info.requestPermission();
2019-09-26 12:55:15 +00:00
inputContentListener.onInputContent(info.getContentUri());
2019-05-04 07:28:38 +00:00
return true;
} catch (Throwable ex) {
Log.w(ex);
return false;
}
}
});
}
void setInputContentListener(IInputContentListener listener) {
2019-09-26 12:55:15 +00:00
this.inputContentListener = listener;
2019-05-04 07:28:38 +00:00
}
interface IInputContentListener {
void onInputContent(Uri uri);
}
2019-09-26 12:55:15 +00:00
void setSelectionListener(ISelection listener) {
this.selectionListener = listener;
}
interface ISelection {
void onSelected(boolean selection);
}
2019-03-05 08:05:46 +00:00
}