FileDocCategorySizeDatePackage
AutoText.javaAPI DocAndroid 5.1 API8678Thu Mar 12 22:22:10 GMT 2015android.text

AutoText

public class AutoText extends Object
This class accesses a dictionary of corrections to frequent misspellings.

Fields Summary
private static final int
TRIE_C
private static final int
TRIE_OFF
private static final int
TRIE_CHILD
private static final int
TRIE_NEXT
private static final int
TRIE_SIZEOF
private static final char
TRIE_NULL
private static final int
TRIE_ROOT
private static final int
INCREMENT
private static final int
DEFAULT
private static final int
RIGHT
private static AutoText
sInstance
private static Object
sLock
private char[]
mTrie
private char
mTrieUsed
private String
mText
private Locale
mLocale
private int
mSize
Constructors Summary
private AutoText(android.content.res.Resources resources)


       
        mLocale = resources.getConfiguration().locale;
        init(resources);
    
Methods Summary
private voidadd(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.Stringget(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.AutoTextgetInstance(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.

param
view to get the resources from
return
the single instance of AutoText

        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 intgetSize(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.

param
view used to retrieve the current Locale and Resources.
return
the number of entries in the auto text dictionary


        return getInstance(view).getSize(); 
    
private intgetSize()
Returns the size of the dictionary.

        return mSize;
    
private voidinit(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.Stringlookup(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 charnewTrieNode()

        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;