/tags/20090726/src/com/menny/android/anysoftkeyboard/WordComposer.java

http://softkeyboard.googlecode.com/ · Java · 141 lines · 52 code · 17 blank · 72 comment · 3 complexity · afcc96d7e441e71bfc1982ac4303ec8b MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.menny.android.anysoftkeyboard;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. /**
  20. * A place to store the currently composing word with information such as adjacent key codes as well
  21. */
  22. public class WordComposer {
  23. /**
  24. * The list of unicode values for each keystroke (including surrounding keys)
  25. */
  26. private List<int[]> mCodes;
  27. /**
  28. * The word chosen from the candidate list, until it is committed.
  29. */
  30. private String mPreferredWord;
  31. private StringBuilder mTypedWord;
  32. /**
  33. * Whether the user chose to capitalize the word.
  34. */
  35. private boolean mIsCapitalized;
  36. WordComposer() {
  37. mCodes = new ArrayList<int[]>(12);
  38. mTypedWord = new StringBuilder(20);
  39. }
  40. /**
  41. * Clear out the keys registered so far.
  42. */
  43. public void reset() {
  44. mCodes.clear();
  45. mIsCapitalized = false;
  46. mPreferredWord = null;
  47. mTypedWord.setLength(0);
  48. }
  49. /**
  50. * Number of keystrokes in the composing word.
  51. * @return the number of keystrokes
  52. */
  53. public int size() {
  54. return mCodes.size();
  55. }
  56. /**
  57. * Returns the codes at a particular position in the word.
  58. * @param index the position in the word
  59. * @return the unicode for the pressed and surrounding keys
  60. */
  61. public int[] getCodesAt(int index) {
  62. return mCodes.get(index);
  63. }
  64. /**
  65. * Add a new keystroke, with codes[0] containing the pressed key's unicode and the rest of
  66. * the array containing unicode for adjacent keys, sorted by reducing probability/proximity.
  67. * @param codes the array of unicode values
  68. */
  69. public void add(int primaryCode, int[] codes) {
  70. mTypedWord.append((char) primaryCode);
  71. mCodes.add(codes);
  72. }
  73. /**
  74. * Delete the last keystroke as a result of hitting backspace.
  75. */
  76. public void deleteLast() {
  77. mCodes.remove(mCodes.size() - 1);
  78. mTypedWord.deleteCharAt(mTypedWord.length() - 1);
  79. }
  80. /**
  81. * Returns the word as it was typed, without any correction applied.
  82. * @return the word that was typed so far
  83. */
  84. public CharSequence getTypedWord() {
  85. int wordSize = mCodes.size();
  86. if (wordSize == 0) {
  87. return null;
  88. }
  89. // StringBuffer sb = new StringBuffer(wordSize);
  90. // for (int i = 0; i < wordSize; i++) {
  91. // char c = (char) mCodes.get(i)[0];
  92. // if (i == 0 && mIsCapitalized) {
  93. // c = Character.toUpperCase(c);
  94. // }
  95. // sb.append(c);
  96. // }
  97. // return sb;
  98. return mTypedWord;
  99. }
  100. public void setCapitalized(boolean capitalized) {
  101. mIsCapitalized = capitalized;
  102. }
  103. /**
  104. * Whether or not the user typed a capital letter as the first letter in the word
  105. * @return capitalization preference
  106. */
  107. public boolean isCapitalized() {
  108. return mIsCapitalized;
  109. }
  110. /**
  111. * Stores the user's selected word, before it is actually committed to the text field.
  112. * @param preferred
  113. */
  114. public void setPreferredWord(String preferred) {
  115. mPreferredWord = preferred;
  116. }
  117. /**
  118. * Return the word chosen by the user, or the typed word if no other word was chosen.
  119. * @return the preferred word
  120. */
  121. public CharSequence getPreferredWord() {
  122. return mPreferredWord != null ? mPreferredWord : getTypedWord();
  123. }
  124. }