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 17 package com.android.inputmethod.latin; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 /** 23 * A place to store the currently composing word with information such as adjacent key codes as well 24 */ 25 public class WordComposer { 26 /** 27 * The list of unicode values for each keystroke (including surrounding keys) 28 */ 29 private ArrayList<int[]> mCodes; 30 31 /** 32 * The word chosen from the candidate list, until it is committed. 33 */ 34 private String mPreferredWord; 35 36 private StringBuilder mTypedWord; 37 38 private int mCapsCount; 39 40 /** 41 * Whether the user chose to capitalize the word. 42 */ 43 private boolean mIsCapitalized; 44 WordComposer()45 WordComposer() { 46 mCodes = new ArrayList<int[]>(12); 47 mTypedWord = new StringBuilder(20); 48 } 49 50 /** 51 * Clear out the keys registered so far. 52 */ reset()53 public void reset() { 54 mCodes.clear(); 55 mIsCapitalized = false; 56 mPreferredWord = null; 57 mTypedWord.setLength(0); 58 mCapsCount = 0; 59 } 60 61 /** 62 * Number of keystrokes in the composing word. 63 * @return the number of keystrokes 64 */ size()65 public int size() { 66 return mCodes.size(); 67 } 68 69 /** 70 * Returns the codes at a particular position in the word. 71 * @param index the position in the word 72 * @return the unicode for the pressed and surrounding keys 73 */ getCodesAt(int index)74 public int[] getCodesAt(int index) { 75 return mCodes.get(index); 76 } 77 78 /** 79 * Add a new keystroke, with codes[0] containing the pressed key's unicode and the rest of 80 * the array containing unicode for adjacent keys, sorted by reducing probability/proximity. 81 * @param codes the array of unicode values 82 */ add(int primaryCode, int[] codes)83 public void add(int primaryCode, int[] codes) { 84 mTypedWord.append((char) primaryCode); 85 mCodes.add(codes); 86 if (Character.isUpperCase((char) primaryCode)) mCapsCount++; 87 } 88 89 /** 90 * Delete the last keystroke as a result of hitting backspace. 91 */ deleteLast()92 public void deleteLast() { 93 mCodes.remove(mCodes.size() - 1); 94 final int lastPos = mTypedWord.length() - 1; 95 char last = mTypedWord.charAt(lastPos); 96 mTypedWord.deleteCharAt(lastPos); 97 if (Character.isUpperCase(last)) mCapsCount--; 98 } 99 100 /** 101 * Returns the word as it was typed, without any correction applied. 102 * @return the word that was typed so far 103 */ getTypedWord()104 public CharSequence getTypedWord() { 105 int wordSize = mCodes.size(); 106 if (wordSize == 0) { 107 return null; 108 } 109 // StringBuffer sb = new StringBuffer(wordSize); 110 // for (int i = 0; i < wordSize; i++) { 111 // char c = (char) mCodes.get(i)[0]; 112 // if (i == 0 && mIsCapitalized) { 113 // c = Character.toUpperCase(c); 114 // } 115 // sb.append(c); 116 // } 117 // return sb; 118 return mTypedWord; 119 } 120 setCapitalized(boolean capitalized)121 public void setCapitalized(boolean capitalized) { 122 mIsCapitalized = capitalized; 123 } 124 125 /** 126 * Whether or not the user typed a capital letter as the first letter in the word 127 * @return capitalization preference 128 */ isCapitalized()129 public boolean isCapitalized() { 130 return mIsCapitalized; 131 } 132 133 /** 134 * Stores the user's selected word, before it is actually committed to the text field. 135 * @param preferred 136 */ setPreferredWord(String preferred)137 public void setPreferredWord(String preferred) { 138 mPreferredWord = preferred; 139 } 140 141 /** 142 * Return the word chosen by the user, or the typed word if no other word was chosen. 143 * @return the preferred word 144 */ getPreferredWord()145 public CharSequence getPreferredWord() { 146 return mPreferredWord != null ? mPreferredWord : getTypedWord(); 147 } 148 149 /** 150 * Returns true if more than one character is upper case, otherwise returns false. 151 */ isMostlyCaps()152 public boolean isMostlyCaps() { 153 return mCapsCount > 1; 154 } 155 } 156