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

404 lines
16 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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 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;
import android.content.SharedPreferences;
2020-05-22 05:50:21 +00:00
import android.graphics.drawable.Drawable;
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;
2021-07-02 20:35:41 +00:00
import android.os.Parcel;
import android.os.Parcelable;
2020-05-22 05:50:21 +00:00
import android.text.Html;
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;
2022-01-04 21:19:16 +00:00
import android.view.ActionMode;
2022-01-05 17:31:28 +00:00
import android.view.KeyEvent;
2022-01-04 21:19:16 +00:00
import android.view.Menu;
import android.view.MenuItem;
2021-07-02 20:35:41 +00:00
import android.view.View;
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
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;
import androidx.preference.PreferenceManager;
2019-03-05 08:05:46 +00:00
2019-11-19 20:53:12 +00:00
import org.jsoup.nodes.Document;
2020-10-03 07:31:29 +00:00
import java.util.concurrent.ExecutorService;
2020-04-10 18:08:48 +00:00
public class EditTextCompose extends FixedEditText {
2020-07-26 05:58:12 +00:00
private boolean raw = false;
2019-09-26 12:55:15 +00:00
private ISelection selectionListener = null;
private IInputContentListener inputContentListener = null;
2019-05-04 07:28:38 +00:00
2022-01-05 17:31:28 +00:00
private Boolean canUndo = null;
private Boolean canRedo = null;
private boolean checkKeyEvent = false;
2020-10-03 07:31:29 +00:00
private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "paste");
2019-03-05 08:05:46 +00:00
public EditTextCompose(Context context) {
super(context);
2022-01-04 21:19:16 +00:00
init(context);
2019-03-05 08:05:46 +00:00
}
public EditTextCompose(Context context, AttributeSet attrs) {
super(context, attrs);
2022-01-04 21:19:16 +00:00
init(context);
2019-03-05 08:05:46 +00:00
}
public EditTextCompose(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
2022-01-04 21:19:16 +00:00
init(context);
}
void init(Context context) {
2021-01-17 08:56:37 +00:00
Helper.setKeyboardIncognitoMode(this, context);
2022-01-04 21:19:16 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean undo_manager = prefs.getBoolean("undo_manager", false);
if (undo_manager &&
2022-01-04 21:19:16 +00:00
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setCustomInsertionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (can(android.R.id.undo))
2022-01-04 21:19:16 +00:00
menu.add(Menu.CATEGORY_ALTERNATIVE, R.string.title_undo, 1, R.string.title_undo);
if (can(android.R.id.redo))
2022-01-04 21:19:16 +00:00
menu.add(Menu.CATEGORY_ALTERNATIVE, R.string.title_redo, 2, R.string.title_redo);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getGroupId() == Menu.CATEGORY_ALTERNATIVE) {
int id = item.getItemId();
if (id == R.string.title_undo)
return EditTextCompose.super.onTextContextMenuItem(android.R.id.undo);
else if (id == R.string.title_redo)
return EditTextCompose.super.onTextContextMenuItem(android.R.id.redo);
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Do nothing
}
});
}
}
2022-01-05 17:31:28 +00:00
private boolean can(int what) {
canUndo = null;
canRedo = null;
2022-01-04 21:19:16 +00:00
try {
2022-01-05 17:31:28 +00:00
checkKeyEvent = true;
int meta = KeyEvent.META_CTRL_ON;
if (what == android.R.id.redo)
meta = meta | KeyEvent.META_SHIFT_ON;
KeyEvent ke = new KeyEvent(0, 0, 0, 0, 0, meta);
onKeyShortcut(KeyEvent.KEYCODE_Z, ke);
} catch (Throwable ex) {
Log.e(ex);
} finally {
checkKeyEvent = false;
2022-01-04 21:19:16 +00:00
}
2022-01-05 17:31:28 +00:00
return Boolean.TRUE.equals(what == android.R.id.redo ? canRedo : canUndo);
2019-03-05 08:05:46 +00:00
}
2021-07-02 20:35:41 +00:00
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
return new SavedState(superState, this.raw);
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
setRaw(savedState.getRaw());
}
2020-11-28 11:25:17 +00:00
@Override
protected void onAttachedToWindow() {
// Spellchecker workaround
boolean enabled = isEnabled();
super.setEnabled(true);
super.onAttachedToWindow();
super.setEnabled(enabled);
}
2020-07-26 05:58:12 +00:00
public void setRaw(boolean raw) {
this.raw = raw;
}
2021-05-09 18:21:48 +00:00
public boolean isRaw() {
2020-07-26 05:58:12 +00:00
return raw;
}
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 {
2020-08-22 09:57:30 +00:00
if (id == android.R.id.copy) {
int start = getSelectionStart();
int end = getSelectionEnd();
if (start > end) {
int s = start;
start = end;
end = s;
}
Context context = getContext();
ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (start != end && cbm != null) {
CharSequence selected = getEditableText().subSequence(start, end);
if (selected instanceof Spanned) {
String html = HtmlHelper.toHtml((Spanned) selected, context);
cbm.setPrimaryClip(ClipData.newHtmlText(context.getString(R.string.app_name), selected, html));
setSelection(end);
2022-01-02 14:44:33 +00:00
return true;
2020-08-22 09:57:30 +00:00
}
}
} else if (id == android.R.id.paste) {
2020-10-03 07:31:29 +00:00
final Context context = getContext();
2020-02-23 10:16:40 +00:00
ClipboardManager cbm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
2020-10-03 07:31:29 +00:00
if (cbm == null || !cbm.hasPrimaryClip())
return false;
ClipData.Item item = cbm.getPrimaryClip().getItemAt(0);
2020-10-03 07:31:29 +00:00
final String html;
String h = (raw ? null : item.getHtmlText());
2020-10-03 07:31:29 +00:00
if (h == null) {
CharSequence text = item.getText();
if (text == null)
return false;
2020-07-26 05:58:12 +00:00
if (raw)
2020-10-03 07:31:29 +00:00
html = text.toString();
else
html = "<div>" + HtmlHelper.formatPre(text.toString(), false) + "</div>";
} else
html = h;
final int colorPrimary = Helper.resolveColor(context, R.attr.colorPrimary);
2021-07-05 04:53:21 +00:00
final int colorBlockquote = Helper.resolveColor(context, R.attr.colorBlockquote, colorPrimary);
2021-05-07 12:19:59 +00:00
final int quoteGap = context.getResources().getDimensionPixelSize(R.dimen.quote_gap_size);
final int quoteStripe = context.getResources().getDimensionPixelSize(R.dimen.quote_stripe_width);
2020-10-03 07:31:29 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
try {
SpannableStringBuilder ssb;
if (raw)
2021-09-09 10:52:10 +00:00
ssb = new SpannableStringBuilderEx(html);
2020-10-03 07:31:29 +00:00
else {
Document document = HtmlHelper.sanitizeCompose(context, html, false);
2020-11-08 20:09:31 +00:00
Spanned paste = HtmlHelper.fromDocument(context, document, new Html.ImageGetter() {
2020-10-03 07:31:29 +00:00
@Override
public Drawable getDrawable(String source) {
return ImageHelper.decodeImage(context,
-1, source, true, 0, 1.0f, EditTextCompose.this);
}
}, null);
2021-09-09 10:52:10 +00:00
ssb = new SpannableStringBuilderEx(paste);
2020-10-03 07:31:29 +00:00
QuoteSpan[] spans = ssb.getSpans(0, ssb.length(), QuoteSpan.class);
for (QuoteSpan span : spans) {
QuoteSpan q;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P)
2021-07-05 04:53:21 +00:00
q = new QuoteSpan(colorBlockquote);
2020-10-03 07:31:29 +00:00
else
2021-07-05 04:53:21 +00:00
q = new QuoteSpan(colorBlockquote, quoteStripe, quoteGap);
2020-10-03 07:31:29 +00:00
ssb.setSpan(q,
ssb.getSpanStart(span),
ssb.getSpanEnd(span),
ssb.getSpanFlags(span));
ssb.removeSpan(span);
}
2020-07-26 05:58:12 +00:00
}
2019-09-25 18:48:57 +00:00
2020-10-03 07:31:29 +00:00
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
try {
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)
getText().insert(start, ssb);
else
getText().replace(start, end, ssb);
} catch (Throwable ex) {
Log.e(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)
*/
}
}
});
} catch (Throwable ex) {
Log.e(ex);
}
2019-09-25 18:48:57 +00:00
}
2020-10-03 07:31:29 +00:00
});
2019-09-25 18:48:57 +00:00
2022-01-05 17:31:28 +00:00
return true;
} else if (id == android.R.id.undo) {
canUndo = true;
return true;
} else if (id == android.R.id.redo) {
canRedo = true;
2020-10-03 07:31:29 +00:00
return true;
2019-09-25 18:48:57 +00:00
}
return super.onTextContextMenuItem(id);
2019-09-06 13:54:07 +00:00
} catch (Throwable ex) {
2020-08-26 08:05:32 +00:00
Log.e(ex);
2019-09-06 13:54:07 +00:00
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);
}
2021-07-02 20:35:41 +00:00
static class SavedState extends View.BaseSavedState {
private boolean raw;
private SavedState(Parcelable superState, boolean raw) {
super(superState);
this.raw = raw;
}
private SavedState(Parcel in) {
super(in);
raw = (in.readInt() != 0);
}
public boolean getRaw() {
return this.raw;
}
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
destination.writeInt(raw ? 1 : 0);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
2019-03-05 08:05:46 +00:00
}