Methods Summary |
---|
private void | add(java.lang.String src, char off)
int slen = src.length();
int herep = TRIE_ROOT;
// Keep track of the size of the dictionary
mSize++;
for (int i = 0; i < slen; i++) {
char c = src.charAt(i);
boolean found = false;
for (; mTrie[herep] != TRIE_NULL;
herep = mTrie[herep] + TRIE_NEXT) {
if (c == mTrie[mTrie[herep] + TRIE_C]) {
// There is a node for this letter, and this is the
// end, so fill in the right hand side fields.
if (i == slen - 1) {
mTrie[mTrie[herep] + TRIE_OFF] = off;
return;
}
// There is a node for this letter, and we need
// to go deeper into it to fill in the rest.
herep = mTrie[herep] + TRIE_CHILD;
found = true;
break;
}
}
if (!found) {
// No node for this letter yet. Make one.
char node = newTrieNode();
mTrie[herep] = node;
mTrie[mTrie[herep] + TRIE_C] = c;
mTrie[mTrie[herep] + TRIE_OFF] = TRIE_NULL;
mTrie[mTrie[herep] + TRIE_NEXT] = TRIE_NULL;
mTrie[mTrie[herep] + TRIE_CHILD] = TRIE_NULL;
// If this is the end of the word, fill in the offset.
if (i == slen - 1) {
mTrie[mTrie[herep] + TRIE_OFF] = off;
return;
}
// Otherwise, step in deeper and go to the next letter.
herep = mTrie[herep] + TRIE_CHILD;
}
}
|
public static java.lang.String | get(java.lang.CharSequence src, int start, int end, android.view.View view)Retrieves a possible spelling correction for the specified range
of text. Returns null if no correction can be found.
The View is used to get the current Locale and Resources.
return getInstance(view).lookup(src, start, end);
|
private static android.text.AutoText | getInstance(android.view.View view)Returns the instance of AutoText. If the locale has changed, it will create a new
instance of AutoText for the locale.
Resources res = view.getContext().getResources();
Locale locale = res.getConfiguration().locale;
AutoText instance;
synchronized (sLock) {
instance = sInstance;
if (!locale.equals(instance.mLocale)) {
instance = new AutoText(res);
sInstance = instance;
}
}
return instance;
|
public static int | getSize(android.view.View view)Returns the size of the auto text dictionary. The return value can be zero if there is
no auto correction data available for the current locale.
return getInstance(view).getSize();
|
private int | getSize()Returns the size of the dictionary.
return mSize;
|
private void | init(android.content.res.Resources r)
XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
StringBuilder right = new StringBuilder(RIGHT);
mTrie = new char[DEFAULT];
mTrie[TRIE_ROOT] = TRIE_NULL;
mTrieUsed = TRIE_ROOT + 1;
try {
XmlUtils.beginDocument(parser, "words");
String odest = "";
char ooff = 0;
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null || !(element.equals("word"))) {
break;
}
String src = parser.getAttributeValue(null, "src");
if (parser.next() == XmlPullParser.TEXT) {
String dest = parser.getText();
char off;
if (dest.equals(odest)) {
off = ooff;
} else {
off = (char) right.length();
right.append((char) dest.length());
right.append(dest);
}
add(src, off);
}
}
// Don't let Resources cache a copy of all these strings.
r.flushLayoutCache();
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
parser.close();
}
mText = right.toString();
|
private java.lang.String | lookup(java.lang.CharSequence src, int start, int end)
int here = mTrie[TRIE_ROOT];
for (int i = start; i < end; i++) {
char c = src.charAt(i);
for (; here != TRIE_NULL; here = mTrie[here + TRIE_NEXT]) {
if (c == mTrie[here + TRIE_C]) {
if ((i == end - 1)
&& (mTrie[here + TRIE_OFF] != TRIE_NULL)) {
int off = mTrie[here + TRIE_OFF];
int len = mText.charAt(off);
return mText.substring(off + 1, off + 1 + len);
}
here = mTrie[here + TRIE_CHILD];
break;
}
}
if (here == TRIE_NULL) {
return null;
}
}
return null;
|
private char | newTrieNode()
if (mTrieUsed + TRIE_SIZEOF > mTrie.length) {
char[] copy = new char[mTrie.length + INCREMENT];
System.arraycopy(mTrie, 0, copy, 0, mTrie.length);
mTrie = copy;
}
char ret = mTrieUsed;
mTrieUsed += TRIE_SIZEOF;
return ret;
|