Fixed paste crash

This commit is contained in:
M66B 2020-10-03 09:31:29 +02:00
parent 004cc290e2
commit f2b420de0a
1 changed files with 97 additions and 70 deletions

View File

@ -40,11 +40,16 @@ import androidx.core.view.inputmethod.InputContentInfoCompat;
import org.jsoup.nodes.Document;
import java.util.concurrent.ExecutorService;
public class EditTextCompose extends FixedEditText {
private boolean raw = false;
private ISelection selectionListener = null;
private IInputContentListener inputContentListener = null;
private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "paste");
public EditTextCompose(Context context) {
super(context);
}
@ -96,13 +101,17 @@ public class EditTextCompose extends FixedEditText {
}
}
} else if (id == android.R.id.paste) {
Context context = getContext();
final Context context = getContext();
ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (cbm != null && cbm.hasPrimaryClip()) {
if (cbm == null || !cbm.hasPrimaryClip())
return false;
ClipData.Item item = cbm.getPrimaryClip().getItemAt(0);
String html = item.getHtmlText();
if (html == null) {
final String html;
String h = item.getHtmlText();
if (h == null) {
CharSequence text = item.getText();
if (text == null)
return false;
@ -110,25 +119,30 @@ public class EditTextCompose extends FixedEditText {
html = text.toString();
else
html = "<div>" + HtmlHelper.formatPre(text.toString(), false) + "</div>";
}
} else
html = h;
final int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
final int dp3 = Helper.dp2pixels(context, 3);
final int dp6 = Helper.dp2pixels(context, 6);
executor.submit(new Runnable() {
@Override
public void run() {
try {
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() {
Spanned paste = HtmlHelper.fromDocument(context, document, true, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
return ImageHelper.decodeImage(getContext(),
return ImageHelper.decodeImage(context,
-1, source, true, 0, 1.0f, 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) {
@ -145,6 +159,10 @@ public class EditTextCompose extends FixedEditText {
}
}
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
try {
int start = getSelectionStart();
int end = getSelectionEnd();
@ -163,13 +181,8 @@ public class EditTextCompose extends FixedEditText {
getText().insert(start, ssb);
else
getText().replace(start, end, ssb);
return true;
}
}
return super.onTextContextMenuItem(id);
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.RuntimeException: PARAGRAPH span must start at paragraph boundary
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:619)
@ -180,6 +193,20 @@ public class EditTextCompose extends FixedEditText {
at android.widget.TextView.paste(TextView.java:8891)
at android.widget.TextView.onTextContextMenuItem(TextView.java:8706)
*/
}
}
});
} catch (Throwable ex) {
Log.e(ex);
}
}
});
return true;
}
return super.onTextContextMenuItem(id);
} catch (Throwable ex) {
Log.e(ex);
return false;
}