Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7b1fc89e authored by James Nylen's avatar James Nylen
Browse files

Enhance AutoText to support user-defined correction pairs

Change-Id: Icb43efe6c53b57868f58be1437e15cc386a63c9e
parent 9e455359
Loading
Loading
Loading
Loading
+67 −10
Original line number Diff line number Diff line
@@ -25,10 +25,14 @@ import android.view.View;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.util.Locale;

import java.io.FileInputStream;
import java.io.BufferedInputStream;

/**
 * This class accesses a dictionary of corrections to frequent misspellings.
 */
@@ -55,9 +59,13 @@ public class AutoText {

    private static final int RIGHT = 9300; // Size of 'right' 13 Aug 2007

    private static AutoText sInstance = new AutoText(Resources.getSystem());
    private static AutoText sInstance = new AutoText(Resources.getSystem(), false);
    private static Object sLock = new Object();

    private static AutoText sUserInstance = new AutoText(null, true);

    private static final String TAG = "AutoText";

    // TODO:
    //
    // Note the assumption that the destination strings total less than
@@ -72,10 +80,13 @@ public class AutoText {
    private String mText;
    private Locale mLocale;
    private int mSize;
    private boolean initialized;

    private AutoText(Resources resources) {
    private AutoText(Resources resources, boolean isUserInstance) {
        if (!isUserInstance) {
            mLocale = resources.getConfiguration().locale;
        init(resources);
        }
        initialized = init(resources, isUserInstance);
    }

    /**
@@ -93,7 +104,7 @@ public class AutoText {
            instance = sInstance;

            if (!locale.equals(instance.mLocale)) {
                instance = new AutoText(res);
                instance = new AutoText(res, false);
                sInstance = instance;
            }
        }
@@ -108,7 +119,15 @@ public class AutoText {
     */
    public static String get(CharSequence src, final int start, final int end,
                             View view) {
        String userCorrection = null;
        if (sUserInstance.initialized) {
            userCorrection = sUserInstance.lookup(src, start, end);
        }
        if (userCorrection == null) {
            return getInstance(view).lookup(src, start, end);
        } else {
            return userCorrection;
        }
    }

    /**
@@ -129,6 +148,15 @@ public class AutoText {
        return mSize;
    }

    /**
     * Refreshes the list of user-defined corrections in the file
     * /data/local/user_autotext.xml .
     * @hide
     */
    public static void refreshUserCorrections() {
        sUserInstance = new AutoText(null, true);
    }

    private String lookup(CharSequence src, final int start, final int end) {
        int here = mTrie[TRIE_ROOT];

@@ -158,8 +186,25 @@ public class AutoText {
        return null;
    }

    private void init(Resources r) {
        XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
    private boolean init(Resources r, boolean isUserInstance) {
        XmlPullParser parser = null;
        BufferedInputStream bufStream = null;
        FileInputStream fStream = null;

        if (isUserInstance) {
            try {
                parser = XmlPullParserFactory.newInstance().newPullParser();
                fStream = new FileInputStream("/data/local/user_autotext.xml");
                bufStream = new BufferedInputStream(fStream);
                parser.setInput(bufStream, null);
            } catch (IOException e) {
                return false;
            } catch (XmlPullParserException e) {
                return false;
            }
        } else {
            parser = r.getXml(com.android.internal.R.xml.autotext);
        }

        StringBuilder right = new StringBuilder(RIGHT);
        mTrie = new char[DEFAULT];
@@ -197,16 +242,28 @@ public class AutoText {
            }

            // Don't let Resources cache a copy of all these strings.
            if (r != null) {
                r.flushLayoutCache();
            }
        } catch (XmlPullParserException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            parser.close();
            if (isUserInstance) {
                try {
                    fStream.close();
                    bufStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                ((XmlResourceParser)parser).close();
            }
        }

        mText = right.toString();
        return true;
    }

    private void add(String src, char off) {