1package eu.siacs.conversations.ui;
2
3import android.support.v13.view.inputmethod.EditorInfoCompat;
4import android.support.v13.view.inputmethod.InputConnectionCompat;
5import android.support.v13.view.inputmethod.InputContentInfoCompat;
6
7import android.content.Context;
8import android.os.Build;
9import android.os.Bundle;
10import android.os.Handler;
11import android.text.Editable;
12import android.text.InputFilter;
13import android.text.Spanned;
14import android.util.AttributeSet;
15import android.view.KeyEvent;
16import android.view.inputmethod.EditorInfo;
17import android.view.inputmethod.InputConnection;
18import android.widget.EditText;
19
20import eu.siacs.conversations.Config;
21
22public class EditMessage extends EditText {
23
24 public interface OnCommitContentListener {
25 boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts, String[] mimeTypes);
26 }
27
28 private OnCommitContentListener mCommitContentListener = null;
29 private String[] mimeTypes = null;
30
31 public EditMessage(Context context, AttributeSet attrs) {
32 super(context, attrs);
33 }
34
35 public EditMessage(Context context) {
36 super(context);
37 }
38
39 protected Handler mTypingHandler = new Handler();
40
41 protected Runnable mTypingTimeout = new Runnable() {
42 @Override
43 public void run() {
44 if (isUserTyping && keyboardListener != null) {
45 keyboardListener.onTypingStopped();
46 isUserTyping = false;
47 }
48 }
49 };
50
51 private boolean isUserTyping = false;
52
53 private boolean lastInputWasTab = false;
54
55 protected KeyboardListener keyboardListener;
56
57 @Override
58 public boolean onKeyDown(int keyCode, KeyEvent e) {
59 if (keyCode == KeyEvent.KEYCODE_ENTER && !e.isShiftPressed()) {
60 lastInputWasTab = false;
61 if (keyboardListener != null && keyboardListener.onEnterPressed()) {
62 return true;
63 }
64 } else if (keyCode == KeyEvent.KEYCODE_TAB && !e.isAltPressed() && !e.isCtrlPressed()) {
65 if (keyboardListener != null && keyboardListener.onTabPressed(this.lastInputWasTab)) {
66 lastInputWasTab = true;
67 return true;
68 }
69 } else {
70 lastInputWasTab = false;
71 }
72 return super.onKeyDown(keyCode, e);
73 }
74
75 @Override
76 public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
77 super.onTextChanged(text,start,lengthBefore,lengthAfter);
78 lastInputWasTab = false;
79 if (this.mTypingHandler != null && this.keyboardListener != null) {
80 this.mTypingHandler.removeCallbacks(mTypingTimeout);
81 this.mTypingHandler.postDelayed(mTypingTimeout, Config.TYPING_TIMEOUT * 1000);
82 final int length = text.length();
83 if (!isUserTyping && length > 0) {
84 this.isUserTyping = true;
85 this.keyboardListener.onTypingStarted();
86 } else if (length == 0) {
87 this.isUserTyping = false;
88 this.keyboardListener.onTextDeleted();
89 }
90 this.keyboardListener.onTextChanged();
91 }
92 }
93
94 public void setKeyboardListener(KeyboardListener listener) {
95 this.keyboardListener = listener;
96 if (listener != null) {
97 this.isUserTyping = false;
98 }
99 }
100
101 public interface KeyboardListener {
102 boolean onEnterPressed();
103 void onTypingStarted();
104 void onTypingStopped();
105 void onTextDeleted();
106 void onTextChanged();
107 boolean onTabPressed(boolean repeated);
108 }
109
110 private static final InputFilter SPAN_FILTER = new InputFilter() {
111
112 @Override
113 public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
114 return source instanceof Spanned ? source.toString() : source;
115 }
116 };
117
118 @Override
119 public boolean onTextContextMenuItem(int id) {
120 if (id == android.R.id.paste) {
121 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
122 return super.onTextContextMenuItem(android.R.id.pasteAsPlainText);
123 } else {
124 Editable editable = getEditableText();
125 InputFilter[] filters = editable.getFilters();
126 InputFilter[] tempFilters = new InputFilter[filters != null ? filters.length + 1 : 1];
127 if (filters != null) {
128 System.arraycopy(filters, 0, tempFilters, 1, filters.length);
129 }
130 tempFilters[0] = SPAN_FILTER;
131 editable.setFilters(tempFilters);
132 try {
133 return super.onTextContextMenuItem(id);
134 } finally {
135 editable.setFilters(filters);
136 }
137 }
138 } else {
139 return super.onTextContextMenuItem(id);
140 }
141 }
142
143 public void setRichContentListener(String[] mimeTypes, OnCommitContentListener listener) {
144 this.mimeTypes = mimeTypes;
145 this.mCommitContentListener = listener;
146 }
147
148 @Override
149 public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
150 final InputConnection ic = super.onCreateInputConnection(editorInfo);
151
152 if (mimeTypes != null && mCommitContentListener != null) {
153 EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes);
154 return InputConnectionCompat.createWrapper(ic, editorInfo, new InputConnectionCompat.OnCommitContentListener() {
155 @Override
156 public boolean onCommitContent(InputContentInfoCompat inputContentInfo, int flags, Bundle opts) {
157 return EditMessage.this.mCommitContentListener.onCommitContent(inputContentInfo, flags, opts, mimeTypes);
158 }
159 });
160 }
161 else {
162 return ic;
163 }
164 }
165}