From ed4bfaaa3c62f1e597da4b527a7fd84f9f8fc607 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Sun, 5 Dec 2010 18:58:19 -0500 Subject: Enhance AutoText to support user-defined correction pairs Change-Id: I79d43b233476fcd556228d344e26e869b088c2fc --- core/java/android/text/AutoText.java | 77 +++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/core/java/android/text/AutoText.java b/core/java/android/text/AutoText.java index 04730ec..a68c1c5 100644 --- a/core/java/android/text/AutoText.java +++ b/core/java/android/text/AutoText.java @@ -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) { - mLocale = resources.getConfiguration().locale; - init(resources); + private AutoText(Resources resources, boolean isUserInstance) { + if (!isUserInstance) { + mLocale = resources.getConfiguration().locale; + } + 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) { - return getInstance(view).lookup(src, start, end); + 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. - r.flushLayoutCache(); + 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) { -- cgit v1.1