summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Kondik <shade@chemlab.org>2010-12-17 13:21:46 +0000
committerGerrit Code Review <gerrit@review.cyanogenmod.com>2010-12-17 13:21:46 +0000
commitc0b870db917db767932cbe9f79eb62d637ebe27d (patch)
tree69e18ff5b5a019f58e5978924e75503266419e27
parent0688d9b792be3ee4418bfae65f214070e305f10a (diff)
parented4bfaaa3c62f1e597da4b527a7fd84f9f8fc607 (diff)
downloadframeworks_base-c0b870db917db767932cbe9f79eb62d637ebe27d.zip
frameworks_base-c0b870db917db767932cbe9f79eb62d637ebe27d.tar.gz
frameworks_base-c0b870db917db767932cbe9f79eb62d637ebe27d.tar.bz2
Merge "Enhance AutoText to support user-defined correction pairs" into froyo
-rw-r--r--core/java/android/text/AutoText.java77
1 files 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) {