run keyboard listeners on background executor

This commit is contained in:
Daniel Gultsch 2019-04-17 18:25:21 +02:00
parent 39bc067405
commit 7a825231fb
1 changed files with 155 additions and 147 deletions

View File

@ -19,18 +19,22 @@ import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
public class EditMessage extends EmojiWrapperEditText {
private static final InputFilter SPAN_FILTER = (source, start, end, dest, dstart, dend) -> source instanceof Spanned ? source.toString() : source;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
protected Handler mTypingHandler = new Handler();
protected KeyboardListener keyboardListener;
private OnCommitContentListener mCommitContentListener = null;
private String[] mimeTypes = null;
private boolean isUserTyping = false;
protected Runnable mTypingTimeout = new Runnable() {
private final Runnable mTypingTimeout = new Runnable() {
@Override
public void run() {
if (isUserTyping && keyboardListener != null) {
@ -72,14 +76,19 @@ public class EditMessage extends EmojiWrapperEditText {
return AUTOFILL_TYPE_NONE;
}
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
lastInputWasTab = false;
if (this.mTypingHandler != null && this.keyboardListener != null) {
executor.execute(() -> triggerKeyboardEvents(text.length()));
}
}
private void triggerKeyboardEvents(final int length) {
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();
@ -89,7 +98,6 @@ public class EditMessage extends EmojiWrapperEditText {
}
this.keyboardListener.onTextChanged();
}
}
public void setKeyboardListener(KeyboardListener listener) {
this.keyboardListener = listener;