PageRenderTime 96ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/frameworks/base/core/java/android/text/AutoText.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 279 lines | 161 code | 57 blank | 61 comment | 31 complexity | 395b362ee1c8cafd1f0a59f99a46315c MD5 | raw file
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.text;
  17. import android.content.res.Resources;
  18. import android.content.res.XmlResourceParser;
  19. import com.android.internal.util.XmlUtils;
  20. import android.view.View;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;
  23. import java.io.IOException;
  24. import java.util.Locale;
  25. /**
  26. * This class accesses a dictionary of corrections to frequent misspellings.
  27. */
  28. public class AutoText {
  29. // struct trie {
  30. // char c;
  31. // int off;
  32. // struct trie *child;
  33. // struct trie *next;
  34. // };
  35. private static final int TRIE_C = 0;
  36. private static final int TRIE_OFF = 1;
  37. private static final int TRIE_CHILD = 2;
  38. private static final int TRIE_NEXT = 3;
  39. private static final int TRIE_SIZEOF = 4;
  40. private static final char TRIE_NULL = (char) -1;
  41. private static final int TRIE_ROOT = 0;
  42. private static final int INCREMENT = 1024;
  43. private static final int DEFAULT = 14337; // Size of the Trie 13 Aug 2007
  44. private static final int RIGHT = 9300; // Size of 'right' 13 Aug 2007
  45. private static AutoText sInstance = new AutoText(Resources.getSystem());
  46. private static Object sLock = new Object();
  47. // TODO:
  48. //
  49. // Note the assumption that the destination strings total less than
  50. // 64K characters and that the trie for the source side totals less
  51. // than 64K chars/offsets/child pointers/next pointers.
  52. //
  53. // This seems very safe for English (currently 7K of destination,
  54. // 14K of trie) but may need to be revisited.
  55. private char[] mTrie;
  56. private char mTrieUsed;
  57. private String mText;
  58. private Locale mLocale;
  59. private int mSize;
  60. private AutoText(Resources resources) {
  61. mLocale = resources.getConfiguration().locale;
  62. init(resources);
  63. }
  64. /**
  65. * Returns the instance of AutoText. If the locale has changed, it will create a new
  66. * instance of AutoText for the locale.
  67. * @param view to get the resources from
  68. * @return the single instance of AutoText
  69. */
  70. private static AutoText getInstance(View view) {
  71. Resources res = view.getContext().getResources();
  72. Locale locale = res.getConfiguration().locale;
  73. AutoText instance;
  74. synchronized (sLock) {
  75. instance = sInstance;
  76. if (!locale.equals(instance.mLocale)) {
  77. instance = new AutoText(res);
  78. sInstance = instance;
  79. }
  80. }
  81. return instance;
  82. }
  83. /**
  84. * Retrieves a possible spelling correction for the specified range
  85. * of text. Returns null if no correction can be found.
  86. * The View is used to get the current Locale and Resources.
  87. */
  88. public static String get(CharSequence src, final int start, final int end,
  89. View view) {
  90. return getInstance(view).lookup(src, start, end);
  91. }
  92. /**
  93. * Returns the size of the auto text dictionary. The return value can be zero if there is
  94. * no auto correction data available for the current locale.
  95. * @param view used to retrieve the current Locale and Resources.
  96. * @return the number of entries in the auto text dictionary
  97. */
  98. public static int getSize(View view) {
  99. return getInstance(view).getSize();
  100. }
  101. /**
  102. * Returns the size of the dictionary.
  103. */
  104. private int getSize() {
  105. return mSize;
  106. }
  107. private String lookup(CharSequence src, final int start, final int end) {
  108. int here = mTrie[TRIE_ROOT];
  109. for (int i = start; i < end; i++) {
  110. char c = src.charAt(i);
  111. for (; here != TRIE_NULL; here = mTrie[here + TRIE_NEXT]) {
  112. if (c == mTrie[here + TRIE_C]) {
  113. if ((i == end - 1)
  114. && (mTrie[here + TRIE_OFF] != TRIE_NULL)) {
  115. int off = mTrie[here + TRIE_OFF];
  116. int len = mText.charAt(off);
  117. return mText.substring(off + 1, off + 1 + len);
  118. }
  119. here = mTrie[here + TRIE_CHILD];
  120. break;
  121. }
  122. }
  123. if (here == TRIE_NULL) {
  124. return null;
  125. }
  126. }
  127. return null;
  128. }
  129. private void init(Resources r) {
  130. XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
  131. StringBuilder right = new StringBuilder(RIGHT);
  132. mTrie = new char[DEFAULT];
  133. mTrie[TRIE_ROOT] = TRIE_NULL;
  134. mTrieUsed = TRIE_ROOT + 1;
  135. try {
  136. XmlUtils.beginDocument(parser, "words");
  137. String odest = "";
  138. char ooff = 0;
  139. while (true) {
  140. XmlUtils.nextElement(parser);
  141. String element = parser.getName();
  142. if (element == null || !(element.equals("word"))) {
  143. break;
  144. }
  145. String src = parser.getAttributeValue(null, "src");
  146. if (parser.next() == XmlPullParser.TEXT) {
  147. String dest = parser.getText();
  148. char off;
  149. if (dest.equals(odest)) {
  150. off = ooff;
  151. } else {
  152. off = (char) right.length();
  153. right.append((char) dest.length());
  154. right.append(dest);
  155. }
  156. add(src, off);
  157. }
  158. }
  159. // Don't let Resources cache a copy of all these strings.
  160. r.flushLayoutCache();
  161. } catch (XmlPullParserException e) {
  162. throw new RuntimeException(e);
  163. } catch (IOException e) {
  164. throw new RuntimeException(e);
  165. } finally {
  166. parser.close();
  167. }
  168. mText = right.toString();
  169. }
  170. private void add(String src, char off) {
  171. int slen = src.length();
  172. int herep = TRIE_ROOT;
  173. // Keep track of the size of the dictionary
  174. mSize++;
  175. for (int i = 0; i < slen; i++) {
  176. char c = src.charAt(i);
  177. boolean found = false;
  178. for (; mTrie[herep] != TRIE_NULL;
  179. herep = mTrie[herep] + TRIE_NEXT) {
  180. if (c == mTrie[mTrie[herep] + TRIE_C]) {
  181. // There is a node for this letter, and this is the
  182. // end, so fill in the right hand side fields.
  183. if (i == slen - 1) {
  184. mTrie[mTrie[herep] + TRIE_OFF] = off;
  185. return;
  186. }
  187. // There is a node for this letter, and we need
  188. // to go deeper into it to fill in the rest.
  189. herep = mTrie[herep] + TRIE_CHILD;
  190. found = true;
  191. break;
  192. }
  193. }
  194. if (!found) {
  195. // No node for this letter yet. Make one.
  196. char node = newTrieNode();
  197. mTrie[herep] = node;
  198. mTrie[mTrie[herep] + TRIE_C] = c;
  199. mTrie[mTrie[herep] + TRIE_OFF] = TRIE_NULL;
  200. mTrie[mTrie[herep] + TRIE_NEXT] = TRIE_NULL;
  201. mTrie[mTrie[herep] + TRIE_CHILD] = TRIE_NULL;
  202. // If this is the end of the word, fill in the offset.
  203. if (i == slen - 1) {
  204. mTrie[mTrie[herep] + TRIE_OFF] = off;
  205. return;
  206. }
  207. // Otherwise, step in deeper and go to the next letter.
  208. herep = mTrie[herep] + TRIE_CHILD;
  209. }
  210. }
  211. }
  212. private char newTrieNode() {
  213. if (mTrieUsed + TRIE_SIZEOF > mTrie.length) {
  214. char[] copy = new char[mTrie.length + INCREMENT];
  215. System.arraycopy(mTrie, 0, copy, 0, mTrie.length);
  216. mTrie = copy;
  217. }
  218. char ret = mTrieUsed;
  219. mTrieUsed += TRIE_SIZEOF;
  220. return ret;
  221. }
  222. }