Program: 157135
Project: 157135
Buggy source:
@Override public boolean onKeyDown(final int keyEventKeyCode,@NonNull KeyEvent event){
  InputConnection ic=getCurrentInputConnection();
  if (handleSelectionExpending(keyEventKeyCode,ic,mGlobalSelectionStartPosition,mGlobalCursorPosition))   return true;
  final boolean shouldTranslateSpecialKeys=isInputViewShown();
  if (event.getDeviceId() > 0 && event.isPrintingKey())   onPhysicalKeyboardKeyPressed();
  mHardKeyboardAction.initializeAction(event,mMetaState);
switch (keyEventKeyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
    if (shouldTranslateSpecialKeys && mUseVolumeKeyForLeftRight) {
      sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_LEFT);
      return true;
    }
  return super.onKeyDown(keyEventKeyCode,event);
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (shouldTranslateSpecialKeys && mUseVolumeKeyForLeftRight) {
  sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
  return true;
}
return super.onKeyDown(keyEventKeyCode,event);
case KeyEvent.KEYCODE_BACK:
mBackKeyPressed=true;
if (event.getRepeatCount() == 0 && getInputView() != null) {
if (getInputView().handleBack()) {
if (ic != null) {
ic.clearMetaKeyStates(Integer.MAX_VALUE);
}
mMetaState=0;
return true;
}
}
if (mQuickTextKeyboardShown) {
hideWindow();
return true;
}
break;
case 0x000000cc:
switchToNextPhysicalKeyboard(ic);
return true;
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
if (event.isAltPressed() && Workarounds.isAltSpaceLangSwitchNotPossible()) {
switchToNextPhysicalKeyboard(ic);
return true;
}
case KeyEvent.KEYCODE_ALT_LEFT:
case KeyEvent.KEYCODE_ALT_RIGHT:
case KeyEvent.KEYCODE_SYM:
mMetaState=MyMetaKeyKeyListener.handleKeyDown(mMetaState,keyEventKeyCode,event);
break;
case KeyEvent.KEYCODE_SPACE:
if ((event.isAltPressed() && !Workarounds.isAltSpaceLangSwitchNotPossible()) || event.isShiftPressed()) {
switchToNextPhysicalKeyboard(ic);
return true;
}
default :
if (!mUseKeyRepeat && event.getRepeatCount() > 0) return true;
AnyKeyboard.HardKeyboardTranslator keyTranslator=(AnyKeyboard.HardKeyboardTranslator)getCurrentAlphabetKeyboard();
if (getKeyboardSwitcher().isCurrentKeyboardPhysical() && keyTranslator != null) {
if (ic != null) ic.beginBatchEdit();
try {
if (mUseBackWord && keyEventKeyCode == KeyEvent.KEYCODE_DEL && event.isShiftPressed()) {
handleBackWord(ic);
return true;
}
 else {
keyTranslator.translatePhysicalCharacter(mHardKeyboardAction,this,mMultiTapTimeout);
if (mHardKeyboardAction.getKeyCodeWasChanged()) {
final int translatedChar=mHardKeyboardAction.getKeyCode();
onKey(translatedChar,null,-1,new int[]{translatedChar},true);
mMetaState=MyMetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
return true;
}
}
}
  finally {
if (ic != null) ic.endBatchEdit();
}
}
if (event.isPrintingKey()) {
mMetaState=MyMetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
}
}
return super.onKeyDown(keyEventKeyCode,event);
}

Fixed version (ground truth):
@Override public boolean onKeyDown(final int keyEventKeyCode,@NonNull KeyEvent event){
  InputConnection ic=getCurrentInputConnection();
  if (handleSelectionExpending(keyEventKeyCode,ic,mGlobalSelectionStartPosition,mGlobalCursorPosition))   return true;
  final boolean shouldTranslateSpecialKeys=isInputViewShown();
  if (event.getDeviceId() > 0 && event.isPrintingKey())   onPhysicalKeyboardKeyPressed();
  mHardKeyboardAction.initializeAction(event,mMetaState);
switch (keyEventKeyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
    if (shouldTranslateSpecialKeys && mUseVolumeKeyForLeftRight) {
      sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_LEFT);
      return true;
    }
  return super.onKeyDown(keyEventKeyCode,event);
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (shouldTranslateSpecialKeys && mUseVolumeKeyForLeftRight) {
  sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
  return true;
}
return super.onKeyDown(keyEventKeyCode,event);
case KeyEvent.KEYCODE_BACK:
mBackKeyPressed=true;
if (mUtilityKeyboardShown) {
mUtilityKeyboardShown=false;
}
if (event.getRepeatCount() == 0 && getInputView() != null) {
if (getInputView().handleBack()) {
if (ic != null) {
ic.clearMetaKeyStates(Integer.MAX_VALUE);
}
mMetaState=0;
return true;
}
}
if (mQuickTextKeyboardShown) {
hideWindow();
return true;
}
break;
case 0x000000cc:
switchToNextPhysicalKeyboard(ic);
return true;
case KeyEvent.KEYCODE_SHIFT_LEFT:
case KeyEvent.KEYCODE_SHIFT_RIGHT:
if (event.isAltPressed() && Workarounds.isAltSpaceLangSwitchNotPossible()) {
switchToNextPhysicalKeyboard(ic);
return true;
}
case KeyEvent.KEYCODE_ALT_LEFT:
case KeyEvent.KEYCODE_ALT_RIGHT:
case KeyEvent.KEYCODE_SYM:
mMetaState=MyMetaKeyKeyListener.handleKeyDown(mMetaState,keyEventKeyCode,event);
break;
case KeyEvent.KEYCODE_SPACE:
if ((event.isAltPressed() && !Workarounds.isAltSpaceLangSwitchNotPossible()) || event.isShiftPressed()) {
switchToNextPhysicalKeyboard(ic);
return true;
}
default :
if (!mUseKeyRepeat && event.getRepeatCount() > 0) return true;
AnyKeyboard.HardKeyboardTranslator keyTranslator=(AnyKeyboard.HardKeyboardTranslator)getCurrentAlphabetKeyboard();
if (getKeyboardSwitcher().isCurrentKeyboardPhysical() && keyTranslator != null) {
if (ic != null) ic.beginBatchEdit();
try {
if (mUseBackWord && keyEventKeyCode == KeyEvent.KEYCODE_DEL && event.isShiftPressed()) {
handleBackWord(ic);
return true;
}
 else {
keyTranslator.translatePhysicalCharacter(mHardKeyboardAction,this,mMultiTapTimeout);
if (mHardKeyboardAction.getKeyCodeWasChanged()) {
final int translatedChar=mHardKeyboardAction.getKeyCode();
onKey(translatedChar,null,-1,new int[]{translatedChar},true);
mMetaState=MyMetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
return true;
}
}
}
  finally {
if (ic != null) ic.endBatchEdit();
}
}
if (event.isPrintingKey()) {
mMetaState=MyMetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
}
}
return super.onKeyDown(keyEventKeyCode,event);
}