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

Commit 7363d4cb authored by James Nylen's avatar James Nylen Committed by Steve Kondik
Browse files

enhanced AutoText to support user-defined correction pairs

parent 10217be5
Loading
Loading
Loading
Loading
+73 −15
Original line number Diff line number Diff line
@@ -23,10 +23,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.
 */
@@ -53,9 +57,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
@@ -70,10 +78,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);
    }

    /**
@@ -91,7 +102,7 @@ public class AutoText {
            instance = sInstance;

            if (!locale.equals(instance.mLocale)) {
                instance = new AutoText(res);
                instance = new AutoText(res, false);
                sInstance = instance;
            }
        }
@@ -106,7 +117,16 @@ 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;
        }
    }

    /**
@@ -127,6 +147,14 @@ public class AutoText {
        return mSize;
    }
    
    /**
     * Refreshes the list of user-defined corrections in the file
     * /data/local/user_autotext.xml .
     */
    private static void refreshUserCorrections() {
      sUserInstance = new AutoText(null, true);
    }

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

@@ -156,13 +184,30 @@ 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];
        mTrie[TRIE_ROOT] = TRIE_NULL;
        mTrieUsed = TRIE_ROOT + 1;
        StringBuilder right = new StringBuilder(RIGHT);
        
        try {
            XmlUtils.beginDocument(parser, "words");
@@ -178,6 +223,7 @@ public class AutoText {
                }

                String src = parser.getAttributeValue(null, "src");
                
                if (parser.next() == XmlPullParser.TEXT) {
                    String dest = parser.getText();
                    char off;
@@ -194,17 +240,29 @@ public class AutoText {
                }
            }
            
            if(r != null) {
              // 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();
            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) {