Fix bug in alt-key key processing.

Rename mCharcodes to mCharSequence (matches the public getter's name.)

Rename the public getter from getCharSequence to extractCharSequence, to
signify that each time you call it the current character sequence is
reset.

Bug repro:

Switch to Alt-key-does-not-send-ESC mode.
Press and release the letter X
Hold down the alt key.
Expected behavior: nothing happens.
Actual behavior: The letter x repeats as long as you hold down the Alt key.
This commit is contained in:
Jack Palevich 2012-09-22 16:29:30 -07:00
parent f15fa5c08a
commit 332928b665
4 changed files with 26 additions and 18 deletions

View File

@ -969,8 +969,8 @@ public class EmulatorView extends View implements GestureDetector.OnGestureListe
}
boolean isHandled = mTermKeyListener.keyDown(event);
if (isHandled) {
if (mTermKeyListener.getCharSequence() != null) {
byte[] seq = mTermKeyListener.getCharSequence();
byte[] seq = mTermKeyListener.extractCharSequence();
if (seq != null) {
mTermSession.write(seq, 0, seq.length);
}
} else if (isSystemKey(keyCode, event)) {

View File

@ -26,7 +26,7 @@ class TermKeyListener {
private final int mBackBehavior;
private final boolean mAllowToggle;
private final boolean mAppMode;
private byte[] mCharcodes;
private byte[] mCharSequence;
private boolean mAltSendsEscape;
private Integer mDeadChar;
@ -58,7 +58,7 @@ class TermKeyListener {
mFnKey.reset();
mCapsKey.reset();
mAltKey.reset();
mCharcodes = null;
mCharSequence = null;
}
/**
@ -247,12 +247,12 @@ class TermKeyListener {
int unicodeMask = KeyEvent.META_CTRL_MASK | (prefixEscFlag ? KeyEvent.META_ALT_MASK : 0);
byte[] directMapping = lookupDirectMap(packKeyCode(e.getKeyCode()), mAppMode, prefixEscFlag);
if (directMapping != null) { // don't handle the key event any further when there is a direct map entry.
mCharcodes = directMapping;
mCharSequence = directMapping;
} else if (e.getKeyCode() == KeyEvent.KEYCODE_BACK) {
mCharcodes = new byte[] { (byte) mBackBehavior };
mCharSequence = new byte[] { (byte) mBackBehavior };
} else {
int charCode = handleDeadKey(e, metaState, unicodeMask);
mCharcodes = lookupDirectMap(charCode, mAppMode, prefixEscFlag);
mCharSequence = lookupDirectMap(charCode, mAppMode, prefixEscFlag);
}
return true;
}
@ -291,8 +291,15 @@ class TermKeyListener {
return data;
}
public byte[] getCharSequence() {
return mCharcodes;
/**
* extracts the current character sequence from the key listener.
* Automatically resets the character sequence to null.
* @return the current character sequence
*/
public byte[] extractCharSequence() {
byte[] result = mCharSequence;
mCharSequence = null;
return result;
}
public boolean keyDown(KeyEvent e) {

View File

@ -26,7 +26,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_a() {
KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x61, res[0]);
}
@ -34,7 +34,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_X() {
KeyEvent event = new KeyEvent(1,2,KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_X, 0, KeyEvent.META_SHIFT_ON);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x58, res[0]);
}
@ -42,7 +42,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_CTRL_c() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_C, 0, KeyEvent.META_CTRL_ON);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x03, res[0]);
}
@ -50,7 +50,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_CTRL_c_no_esc() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_C, 0, KeyEvent.META_CTRL_ON);
tkl_AltNotEsc.keyDown(event);
byte[] res = tkl_AltNotEsc.getCharSequence();
byte[] res = tkl_AltNotEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x03, res[0]);
}
@ -58,7 +58,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_Alt_x() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_X, 0, KeyEvent.META_ALT_ON);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x1b, res[0]);
assertEquals(0x78, res[1]);
@ -67,7 +67,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_Alt_x_no_esc() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_X, 0, KeyEvent.META_ALT_ON);
tkl_AltNotEsc.keyDown(event);
byte[] res = tkl_AltNotEsc.getCharSequence();
byte[] res = tkl_AltNotEsc.extractCharSequence();
assertNull(res);
}
@ -75,7 +75,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_Alt_e() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_E, 0, KeyEvent.META_ALT_ON);
tkl_AltIsEsc.keyDown(event);
byte res[] = tkl_AltIsEsc.getCharSequence();
byte res[] = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x1b, res[0]);
assertEquals(0x65, res[1]);
@ -84,7 +84,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_enter() {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER, 0, 0);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
assertNotNull(res);
assertEquals(0x0d, res[0]);
}
@ -92,7 +92,7 @@ public class TermKeyListenerTest extends AndroidTestCase {
public void testKey_del() throws UnsupportedEncodingException {
KeyEvent event = new KeyEvent(1,2, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL,0 ,0);
tkl_AltIsEsc.keyDown(event);
byte[] res = tkl_AltIsEsc.getCharSequence();
byte[] res = tkl_AltIsEsc.extractCharSequence();
byte[] exp = "\177".getBytes("UTF-8");
assertNotNull(res);
assertEquals(exp.length, res.length);

View File

@ -4,4 +4,5 @@ set -e
cd tests/emulatorview-test
ant debug
adb install -r bin/emulatorview-test-debug.apk
adb shell am instrument jackpal.androidterm.test/android.test.InstrumentationTestRunner