2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-02-21 10:06:52 +00:00
|
|
|
import android.os.Handler;
|
2014-10-22 16:38:44 +00:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class EditMessage extends EditText {
|
|
|
|
|
|
|
|
public EditMessage(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
public EditMessage(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
protected Handler mTypingHandler = new Handler();
|
|
|
|
|
|
|
|
protected Runnable mTypingTimeout = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (isUserTyping && keyboardListener != null) {
|
|
|
|
keyboardListener.onTypingStopped();
|
|
|
|
isUserTyping = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private boolean isUserTyping = false;
|
|
|
|
|
2015-09-27 17:17:44 +00:00
|
|
|
private boolean lastInputWasTab = false;
|
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
protected KeyboardListener keyboardListener;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
@Override
|
2015-09-28 12:36:10 +00:00
|
|
|
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
|
|
|
}
|
2015-09-28 12:36:10 +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
|
|
|
}
|
2015-09-28 12:36:10 +00:00
|
|
|
return super.onKeyDown(keyCode, e);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
|
|
|
|
super.onTextChanged(text,start,lengthBefore,lengthAfter);
|
2015-09-27 17:17:44 +00:00
|
|
|
lastInputWasTab = false;
|
2015-02-21 10:06:52 +00:00
|
|
|
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();
|
|
|
|
}
|
2016-02-15 22:15:04 +00:00
|
|
|
this.keyboardListener.onTextChanged();
|
2015-02-21 10:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeyboardListener(KeyboardListener listener) {
|
|
|
|
this.keyboardListener = listener;
|
|
|
|
if (listener != null) {
|
|
|
|
this.isUserTyping = false;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 10:06:52 +00:00
|
|
|
public interface KeyboardListener {
|
2015-09-27 17:17:44 +00:00
|
|
|
boolean onEnterPressed();
|
|
|
|
void onTypingStarted();
|
|
|
|
void onTypingStopped();
|
|
|
|
void onTextDeleted();
|
2016-02-15 22:15:04 +00:00
|
|
|
void onTextChanged();
|
2015-09-27 17:17:44 +00:00
|
|
|
boolean onTabPressed(boolean repeated);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|