Skip to content

Commit 78daa75

Browse files
committed
Merge pull request kivy#194 from kivy/voiceinput
improve voiceinput handeling
2 parents 0afbecb + 4c1e641 commit 78daa75

File tree

1 file changed

+74
-23
lines changed

1 file changed

+74
-23
lines changed

src/src/org/renpy/android/SDLSurfaceView.java

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import android.opengl.GLSurfaceView;
4646
import android.net.Uri;
4747
import android.os.PowerManager;
48+
import android.os.Handler;
4849
import android.content.pm.PackageManager;
4950
import android.content.pm.ApplicationInfo;
5051
import android.graphics.PixelFormat;
@@ -1058,32 +1059,26 @@ public boolean onKeyUp(int keyCode, final KeyEvent event) {
10581059

10591060
@Override
10601061
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event){
1062+
String keys = event.getCharacters();
10611063
if (DEBUG)
10621064
Log.d(TAG, String.format(
1063-
"onKeyMultiple() keyCode=%d count=%d", keyCode, count));
1064-
String keys = event.getCharacters();
1065+
"onKeyMultiple() keyCode=%d count=%d, keys=%s", keyCode, count, keys));
10651066
char[] keysBuffer = new char[keys.length()];
10661067
if (mDelLen > 0){
10671068
mDelLen = 0;
10681069
return true;
10691070
}
10701071
if (keyCode == 0){
1071-
// FIXME: here is hardcoed value of "q" key
1072+
// FIXME: here is hardcoded value of "q" key
10721073
// on hacker's keyboard. It is passed to
10731074
// nativeKey function to get it worked if
10741075
// we get 9 and some non-ascii characters
10751076
// but it my cause some odd behaviour
10761077
keyCode = 45;
10771078
}
10781079
if (mInputActivated && event.getAction() == KeyEvent.ACTION_MULTIPLE){
1079-
keys.getChars(0, keys.length(), keysBuffer, 0);
1080-
1081-
for(char c: keysBuffer){
1082-
//Log.i("python", "Char from multiply " + (int) c);
1083-
// Calls both up/down events to emulate key pressing
1084-
nativeKey(keyCode, 1, (int) c);
1085-
nativeKey(keyCode, 0, (int) c);
1086-
}
1080+
String message = String.format("INSERTN:%s", keys);
1081+
dispatchCommand(message);
10871082
return true;
10881083
}else {
10891084
return super.onKeyMultiple(keyCode, count, event);
@@ -1110,6 +1105,50 @@ public boolean onKeyPreIme(int keyCode, final KeyEvent event){
11101105
return super.onKeyPreIme(keyCode, event);
11111106
}
11121107

1108+
public void delayed_message(String message, int delay){
1109+
Handler handler = new Handler();
1110+
final String msg = message;
1111+
final int d = delay;
1112+
handler.postDelayed(new Runnable(){
1113+
@Override
1114+
public void run(){
1115+
dispatchCommand(msg);
1116+
}
1117+
}, d);
1118+
}
1119+
1120+
public void dispatchCommand(String message){
1121+
1122+
Boolean ret = false;
1123+
int delay = 0;
1124+
while (message.length() > 50){
1125+
delayed_message(message.substring(0, 50), delay);
1126+
delay += 100;
1127+
message = String.format("INSERTN:%s", message.substring(50, message.length()));
1128+
if (message.length() <= 50){
1129+
delayed_message(message, delay+50);
1130+
return;
1131+
}
1132+
}
1133+
1134+
if (DEBUG) Log.d(TAG, String.format("dispatch :%s", message));
1135+
int keyCode = 45;
1136+
//send control sequence start \x01
1137+
nativeKey(keyCode, 1, 1);
1138+
nativeKey(keyCode, 0, 1);
1139+
1140+
for(int i=0; i < message.length(); i++){
1141+
//Calls both up/down events to emulate key pressing
1142+
nativeKey(keyCode, 1, (int) message.charAt(i));
1143+
nativeKey(keyCode, 0, (int) message.charAt(i));
1144+
}
1145+
1146+
//send control sequence start \x01
1147+
nativeKey(keyCode, 1, 2);
1148+
nativeKey(keyCode, 0, 2);
1149+
1150+
}
1151+
11131152
@Override
11141153
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
11151154
// setting inputtype to TYPE_CLASS_TEXT is necessary for swiftkey to enable
@@ -1118,27 +1157,28 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
11181157
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
11191158
return new BaseInputConnection(this, false){
11201159

1121-
11221160
private void deleteLastText(){
11231161
// send back space keys
1124-
for (int i = 0; i < mDelLen; i++){
1125-
nativeKey(KeyEvent.KEYCODE_DEL, 1, 23);
1126-
nativeKey(KeyEvent.KEYCODE_DEL, 0, 23);
1162+
if (DEBUG){
1163+
Log.i("Python:", String.format("delete text%s", mDelLen));
1164+
}
1165+
1166+
if (mDelLen == 0){
1167+
return;
11271168
}
1169+
1170+
String message = String.format("DEL:%s", mDelLen);
1171+
dispatchCommand(message);
11281172
}
11291173

11301174
@Override
11311175
public boolean setComposingText(CharSequence text,
11321176
int newCursorPosition){
1133-
if (DEBUG) Log.i("Python:", String.format("set Composing Text %s", text));
11341177
this.deleteLastText();
1178+
if (DEBUG) Log.i("Python:", String.format("set Composing Text %s", text));
11351179
// send text
1136-
for(int i = 0; i < text.length(); i++){
1137-
// Calls both up/down events to emulate key pressing
1138-
char c = text.charAt(i);
1139-
nativeKey(45, 1, (int) c);
1140-
nativeKey(45, 0, (int) c);
1141-
}
1180+
String message = String.format("INSERT:%s", text);
1181+
dispatchCommand(message);
11421182
// store len to be deleted for next time
11431183
mDelLen = text.length();
11441184
return super.setComposingText(text, newCursorPosition);
@@ -1147,8 +1187,8 @@ public boolean setComposingText(CharSequence text,
11471187
@Override
11481188
public boolean commitText(CharSequence text, int newCursorPosition) {
11491189
// some code which takes the input and manipulates it and calls editText.getText().replace() afterwards
1150-
if (DEBUG) Log.i("Python:", String.format("Commit Text %s", text));
11511190
this.deleteLastText();
1191+
if (DEBUG) Log.i("Python:", String.format("Commit Text %s", text));
11521192
mDelLen = 0;
11531193
return super.commitText(text, newCursorPosition);
11541194
}
@@ -1168,6 +1208,17 @@ public boolean setComposingRegion(int start, int end){
11681208
@Override
11691209
public boolean deleteSurroundingText (int beforeLength, int afterLength){
11701210
if (DEBUG) Log.d("Python:", String.format("deleteLastText %s %s", beforeLength, afterLength));
1211+
// move cursor to place from where to start deleting
1212+
// send right arrow keys
1213+
for (int i = 0; i < afterLength; i++){
1214+
nativeKey(45, 1, 39);
1215+
nativeKey(45, 0, 39);
1216+
}
1217+
// remove text before cursor
1218+
mDelLen = beforeLength + afterLength;
1219+
this.deleteLastText();
1220+
mDelLen = 0;
1221+
11711222
return super.deleteSurroundingText(beforeLength, afterLength);
11721223
}
11731224

0 commit comments

Comments
 (0)