/tags/20110815/src/com/anysoftkeyboard/keyboards/HardKeyboardSequenceHandler.java

http://softkeyboard.googlecode.com/ · Java · 88 lines · 67 code · 18 blank · 3 comment · 11 complexity · 1bdb628ff0236c4b799ac3c26433ab60 MD5 · raw file

  1. package com.anysoftkeyboard.keyboards;
  2. import java.security.InvalidParameterException;
  3. import com.anysoftkeyboard.keyboards.Keyboard;
  4. import android.view.KeyEvent;
  5. import com.anysoftkeyboard.AnyKeyboardContextProvider;
  6. import com.anysoftkeyboard.keyboards.KeyEventStateMachine.State;
  7. public class HardKeyboardSequenceHandler
  8. {
  9. private static final int[] msQwerty = new int[]{
  10. KeyEvent.KEYCODE_Q, KeyEvent.KEYCODE_W, KeyEvent.KEYCODE_E, KeyEvent.KEYCODE_R, KeyEvent.KEYCODE_T, KeyEvent.KEYCODE_Y,KeyEvent.KEYCODE_U,KeyEvent.KEYCODE_I,KeyEvent.KEYCODE_O,KeyEvent.KEYCODE_P,
  11. KeyEvent.KEYCODE_A,KeyEvent.KEYCODE_S,KeyEvent.KEYCODE_D,KeyEvent.KEYCODE_F,KeyEvent.KEYCODE_G,KeyEvent.KEYCODE_H,KeyEvent.KEYCODE_J,KeyEvent.KEYCODE_K,KeyEvent.KEYCODE_L,
  12. KeyEvent.KEYCODE_Z,KeyEvent.KEYCODE_X,KeyEvent.KEYCODE_C,KeyEvent.KEYCODE_V,KeyEvent.KEYCODE_B,KeyEvent.KEYCODE_N,KeyEvent.KEYCODE_M
  13. };
  14. //See 'getSequenceCharacter' function for usage for msSequenceLivingTime and mLastTypedKeyEventTime.
  15. private static final long msSequenceLivingTime = 600;
  16. private long mLastTypedKeyEventTime;
  17. private final KeyEventStateMachine mCurrentSequence;
  18. public HardKeyboardSequenceHandler()
  19. {
  20. mCurrentSequence = new KeyEventStateMachine();
  21. mLastTypedKeyEventTime = System.currentTimeMillis();
  22. }
  23. public void addQwertyTranslation(String targetCharacters)
  24. {
  25. if (msQwerty.length != targetCharacters.length())
  26. throw new InvalidParameterException("'targetCharacters' should be the same lenght as the latin QWERTY keys strings: "+msQwerty);
  27. for(int qwertyIndex=0; qwertyIndex<msQwerty.length; qwertyIndex++)
  28. {
  29. char latinCharacter = (char)msQwerty[qwertyIndex];
  30. char otherCharacter = targetCharacters.charAt(qwertyIndex);
  31. if (otherCharacter > 0) {
  32. this.addSequence(new int[] { latinCharacter }, otherCharacter);
  33. this.addSequence(new int[] { Keyboard.KEYCODE_SHIFT, latinCharacter }, Character.toUpperCase(otherCharacter) );
  34. }
  35. }
  36. }
  37. public void addSequence(int[] sequence, int result) {
  38. this.mCurrentSequence.addSequence(sequence, result);
  39. }
  40. public void addShiftSequence(int[] sequence, int result) {
  41. this.mCurrentSequence.addSpecialKeySequence(sequence, AnyKeyboard.KEYCODE_SHIFT, result);
  42. }
  43. public void addAltSequence(int[] sequence, int result) {
  44. this.mCurrentSequence.addSpecialKeySequence(sequence, AnyKeyboard.KEYCODE_ALT, result);
  45. }
  46. private State addNewKey(int currentKeyEvent) {
  47. //sequence does not live forever!
  48. //I say, let it live for msSequenceLivingTime milliseconds.
  49. long currentTime = System.currentTimeMillis();
  50. if ((currentTime - mLastTypedKeyEventTime) >= msSequenceLivingTime)
  51. mCurrentSequence.reset();
  52. mLastTypedKeyEventTime = currentTime;
  53. return mCurrentSequence.addKeyCode(currentKeyEvent);
  54. }
  55. public boolean addSpecialKey(int currentKeyEvent) {
  56. State result = this.addNewKey(currentKeyEvent);
  57. return (result != State.RESET);
  58. }
  59. public int getCurrentCharacter(int currentKeyEvent, AnyKeyboardContextProvider inputHandler) {
  60. State result = this.addNewKey(currentKeyEvent);
  61. if (result == State.FULLMATCH || result == State.PARTMATCH) {
  62. int mappedChar = mCurrentSequence.getCharacter();
  63. final int charactersToDelete = mCurrentSequence.getSequenceLength() - 1;
  64. if (charactersToDelete > 0)
  65. inputHandler.deleteLastCharactersFromInput(charactersToDelete);
  66. return mappedChar;
  67. }
  68. return 0;
  69. }
  70. }