conversations-classic/src/main/java/eu/siacs/conversations/ui/widget/EditMessage.java

196 lines
6.4 KiB
Java
Raw Normal View History

2017-09-29 17:44:30 +00:00
package eu.siacs.conversations.ui.widget;
2014-10-22 16:38:44 +00:00
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2018-02-16 12:39:40 +00:00
import android.support.text.emoji.widget.EmojiAppCompatEditText;
import android.support.v13.view.inputmethod.EditorInfoCompat;
import android.support.v13.view.inputmethod.InputConnectionCompat;
import android.support.v13.view.inputmethod.InputContentInfoCompat;
2014-10-22 16:38:44 +00:00
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
2014-10-22 16:38:44 +00:00
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
2014-10-22 16:38:44 +00:00
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
2018-02-16 12:39:40 +00:00
public class EditMessage extends EmojiAppCompatEditText {
2014-10-22 16:38:44 +00:00
2018-02-16 12:39:40 +00:00
private static final InputFilter SPAN_FILTER = (source, start, end, dest, dstart, dend) -> source instanceof Spanned ? source.toString() : source;
2017-11-08 12:25:38 +00:00
protected Handler mTypingHandler = new Handler();
protected KeyboardListener keyboardListener;
private OnCommitContentListener mCommitContentListener = null;
private String[] mimeTypes = null;
2017-11-08 12:25:38 +00:00
private boolean isUserTyping = false;
protected Runnable mTypingTimeout = new Runnable() {
@Override
public void run() {
if (isUserTyping && keyboardListener != null) {
keyboardListener.onTypingStopped();
isUserTyping = false;
}
}
};
2015-09-27 17:17:44 +00:00
private boolean lastInputWasTab = false;
2017-11-08 12:25:38 +00:00
public EditMessage(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditMessage(Context context) {
super(context);
}
2014-10-22 16:38:44 +00:00
@Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
if (keyCode == KeyEvent.KEYCODE_ENTER && !e.isShiftPressed()) {
2015-09-27 17:17:44 +00:00
lastInputWasTab = false;
2015-03-06 21:22:50 +00:00
if (keyboardListener != null && keyboardListener.onEnterPressed()) {
return true;
2014-10-22 16:38:44 +00:00
}
} else if (keyCode == KeyEvent.KEYCODE_TAB && !e.isAltPressed() && !e.isCtrlPressed()) {
2015-09-27 17:17:44 +00:00
if (keyboardListener != null && keyboardListener.onTabPressed(this.lastInputWasTab)) {
lastInputWasTab = true;
return true;
}
} else {
lastInputWasTab = false;
2014-10-22 16:38:44 +00:00
}
return super.onKeyDown(keyCode, e);
2014-10-22 16:38:44 +00:00
}
2017-11-08 12:25:38 +00:00
@Override
public int getAutofillType() {
return AUTOFILL_TYPE_NONE;
}
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
2017-11-08 12:25:38 +00:00
super.onTextChanged(text, start, lengthBefore, lengthAfter);
2015-09-27 17:17:44 +00:00
lastInputWasTab = false;
if (this.mTypingHandler != null && this.keyboardListener != null) {
this.mTypingHandler.removeCallbacks(mTypingTimeout);
this.mTypingHandler.postDelayed(mTypingTimeout, Config.TYPING_TIMEOUT * 1000);
final int length = text.length();
if (!isUserTyping && length > 0) {
this.isUserTyping = true;
this.keyboardListener.onTypingStarted();
} else if (length == 0) {
this.isUserTyping = false;
this.keyboardListener.onTextDeleted();
}
this.keyboardListener.onTextChanged();
}
}
public void setKeyboardListener(KeyboardListener listener) {
this.keyboardListener = listener;
if (listener != null) {
this.isUserTyping = false;
}
2014-10-22 16:38:44 +00:00
}
@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
} else {
Editable editable = getEditableText();
InputFilter[] filters = editable.getFilters();
InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
if (filters != null) {
System.arraycopy(filters, 0, tempFilters, 1, filters.length);
}
tempFilters[0] = SPAN_FILTER;
editable.setFilters(tempFilters);
try {
return super.onTextContextMenuItem(id);
} finally {
editable.setFilters(filters);
}
}
} else {
return super.onTextContextMenuItem(id);
}
}
public void setRichContentListener(String[] mimeTypes, OnCommitContentListener listener) {
this.mimeTypes = mimeTypes;
this.mCommitContentListener = listener;
}
2018-06-11 13:32:18 +00:00
public void insertAsQuote(String text) {
text = text.replaceAll("(\n *){2,}", "\n").replaceAll("(^|\n)", "$1> ").replaceAll("\n$", "");
Editable editable = getEditableText();
int position = getSelectionEnd();
if (position == -1) position = editable.length();
if (position > 0 && editable.charAt(position - 1) != '\n') {
editable.insert(position++, "\n");
}
editable.insert(position, text);
position += text.length();
editable.insert(position++, "\n");
if (position < editable.length() && editable.charAt(position) != '\n') {
editable.insert(position, "\n");
}
setSelection(position);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection ic = super.onCreateInputConnection(editorInfo);
if (mimeTypes != null && mCommitContentListener != null) {
EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
2018-06-06 17:59:30 +00:00
return InputConnectionCompat.createWrapper(ic, editorInfo, (inputContentInfo, flags, opts) -> EditMessage.this.mCommitContentListener.onCommitContent(inputContentInfo, flags, opts, mimeTypes));
2017-11-08 12:25:38 +00:00
} else {
return ic;
}
}
2017-11-08 12:25:38 +00:00
public void refreshIme() {
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getContext());
final boolean usingEnterKey = p.getBoolean("display_enter_key", getResources().getBoolean(R.bool.display_enter_key));
final boolean enterIsSend = p.getBoolean("enter_is_send", getResources().getBoolean(R.bool.enter_is_send));
if (usingEnterKey && enterIsSend) {
setInputType(getInputType() & (~InputType.TYPE_TEXT_FLAG_MULTI_LINE));
setInputType(getInputType() & (~InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE));
} else if (usingEnterKey) {
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
setInputType(getInputType() & (~InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE));
} else {
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
setInputType(getInputType() | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
}
}
2017-11-08 12:25:38 +00:00
public interface OnCommitContentListener {
boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts, String[] mimeTypes);
}
public interface KeyboardListener {
boolean onEnterPressed();
void onTypingStarted();
void onTypingStopped();
void onTextDeleted();
void onTextChanged();
boolean onTabPressed(boolean repeated);
}
2014-10-22 16:38:44 +00:00
}