aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorsatok <satok@google.com>2011-04-20 18:16:18 +0900
committersatok <satok@google.com>2011-04-20 18:16:18 +0900
commite75e4463cdd2b9ba672faa6b0b1a6e3083e61543 (patch)
tree199f30b9b0b640d5b800cb974dd30550e1984e41 /java/src
parentd8cf67623903adc66db8dffb88805e71ec4b3425 (diff)
parent5454ff5a66e681a034ceae6ffc9847ed9eb959d3 (diff)
downloadlatinime-e75e4463cdd2b9ba672faa6b0b1a6e3083e61543.tar.gz
latinime-e75e4463cdd2b9ba672faa6b0b1a6e3083e61543.tar.xz
latinime-e75e4463cdd2b9ba672faa6b0b1a6e3083e61543.zip
Merge remote-tracking branch 'goog/master' into merge
Conflicts: java/res/xml/method.xml java/res/xml/prefs.xml Change-Id: I466a43c56ec01ddac2f8ae4f15dd3a7f8c21175d
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java11
-rw-r--r--java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java8
-rw-r--r--java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java4
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionary.java34
-rw-r--r--java/src/com/android/inputmethod/latin/Flag.java64
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java8
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java8
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java38
8 files changed, 130 insertions, 45 deletions
diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
index 0c2a58ea9..d99f40765 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
@@ -67,7 +67,8 @@ public class InputMethodManagerCompatWrapper {
// For the compatibility, IMM will create dummy subtypes if subtypes are not found.
// This is required to be false if the current behavior is broken. For now, it's ok to be true.
- private static final boolean HAS_VOICE_FUNCTION = true;
+ public static final boolean FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES =
+ !InputMethodServiceCompatWrapper.CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED;
private static final String VOICE_MODE = "voice";
private static final String KEYBOARD_MODE = "keyboard";
@@ -118,8 +119,7 @@ public class InputMethodManagerCompatWrapper {
Object retval = CompatUtils.invoke(mImm, null, METHOD_getEnabledInputMethodSubtypeList,
(imi != null ? imi.getInputMethodInfo() : null), allowsImplicitlySelectedSubtypes);
if (retval == null || !(retval instanceof List) || ((List<?>)retval).isEmpty()) {
- if (InputMethodServiceCompatWrapper.
- CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) {
+ if (!FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) {
// Returns an empty list
return Collections.emptyList();
}
@@ -148,7 +148,7 @@ public class InputMethodManagerCompatWrapper {
@SuppressWarnings("unused")
private InputMethodSubtypeCompatWrapper getLastResortSubtype(String mode) {
- if (VOICE_MODE.equals(mode) && !HAS_VOICE_FUNCTION)
+ if (VOICE_MODE.equals(mode) && !FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES)
return null;
Locale inputLocale = SubtypeSwitcher.getInstance().getInputLocale();
if (inputLocale == null)
@@ -160,8 +160,7 @@ public class InputMethodManagerCompatWrapper {
getShortcutInputMethodsAndSubtypes() {
Object retval = CompatUtils.invoke(mImm, null, METHOD_getShortcutInputMethodsAndSubtypes);
if (retval == null || !(retval instanceof Map) || ((Map<?, ?>)retval).isEmpty()) {
- if (InputMethodServiceCompatWrapper.
- CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) {
+ if (!FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) {
// Returns an empty map
return Collections.emptyMap();
}
diff --git a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java
index 6f96fb3c2..c04181b55 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodServiceCompatWrapper.java
@@ -47,11 +47,17 @@ public class InputMethodServiceCompatWrapper extends InputMethodService {
@SuppressWarnings("unused")
public void notifyOnCurrentInputMethodSubtypeChanged(InputMethodSubtypeCompatWrapper subtype) {
// Do nothing when the API level is 11 or later
- if (CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED) return;
+ // and FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES is not true
+ if (CAN_HANDLE_ON_CURRENT_INPUT_METHOD_SUBTYPE_CHANGED && !InputMethodManagerCompatWrapper.
+ FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES) {
+ return;
+ }
if (subtype == null) {
subtype = mImm.getCurrentInputMethodSubtype();
}
if (subtype != null) {
+ if (!InputMethodManagerCompatWrapper.FORCE_ENABLE_VOICE_EVEN_WITH_NO_VOICE_SUBTYPES
+ && !subtype.isDummy()) return;
if (!InputMethodManagerCompatWrapper.SUBTYPE_SUPPORTED) {
LanguageSwitcherProxy.getInstance().setLocale(subtype.getLocale());
}
diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
index 86c8af37f..806c355a9 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
@@ -116,6 +116,10 @@ public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper
return (String)CompatUtils.invoke(mObj, null, METHOD_getExtraValueOf, key);
}
+ public boolean isDummy() {
+ return !hasOriginalObject();
+ }
+
@Override
public boolean equals(Object o) {
if (o instanceof InputMethodSubtypeCompatWrapper) {
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index fa90fce67..58e9099a9 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -63,18 +63,12 @@ public class BinaryDictionary extends Dictionary {
private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance();
- public static class Flag {
- public final String mName;
- public final int mValue;
-
- public Flag(String name, int value) {
- mName = name;
- mValue = value;
- }
- }
-
public static final Flag FLAG_REQUIRES_GERMAN_UMLAUT_PROCESSING =
- new Flag("requiresGermanUmlautProcessing", 0x1);
+ new Flag(R.bool.config_require_umlaut_processing, 0x1);
+
+ // Can create a new flag from extravalue :
+ // public static final Flag FLAG_MYFLAG =
+ // new Flag("my_flag", 0x02);
private static final Flag[] ALL_FLAGS = {
// Here should reside all flags that trigger some special processing
@@ -118,12 +112,12 @@ public class BinaryDictionary extends Dictionary {
return null;
}
}
- sInstance.mFlags = initFlags(ALL_FLAGS, SubtypeSwitcher.getInstance());
+ sInstance.mFlags = Flag.initFlags(ALL_FLAGS, context, SubtypeSwitcher.getInstance());
return sInstance;
}
- /* package for test */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
- long length, int dicTypeId, Flag[] flagArray) {
+ /* package for test */ static BinaryDictionary initDictionary(Context context, File dictionary,
+ long startOffset, long length, int dicTypeId, Flag[] flagArray) {
synchronized (sInstance) {
sInstance.closeInternal();
if (dictionary.isFile()) {
@@ -134,19 +128,10 @@ public class BinaryDictionary extends Dictionary {
return null;
}
}
- sInstance.mFlags = initFlags(flagArray, null);
+ sInstance.mFlags = Flag.initFlags(flagArray, context, null);
return sInstance;
}
- private static int initFlags(Flag[] flagArray, SubtypeSwitcher switcher) {
- int flags = 0;
- for (Flag entry : flagArray) {
- if (switcher == null || switcher.currentSubtypeContainsExtraValueKey(entry.mName))
- flags |= entry.mValue;
- }
- return flags;
- }
-
static {
Utils.loadNativeLibrary();
}
@@ -179,6 +164,7 @@ public class BinaryDictionary extends Dictionary {
sInstance.mDicTypeId = dicTypeId;
}
}
+ sInstance.mFlags = Flag.initFlags(ALL_FLAGS, context, SubtypeSwitcher.getInstance());
return sInstance;
}
diff --git a/java/src/com/android/inputmethod/latin/Flag.java b/java/src/com/android/inputmethod/latin/Flag.java
new file mode 100644
index 000000000..3cb8f7e17
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/Flag.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import android.content.Context;
+import android.content.res.Resources;
+
+public class Flag {
+ public final String mName;
+ public final int mResource;
+ public final int mMask;
+ public final int mSource;
+
+ static private final int SOURCE_CONFIG = 1;
+ static private final int SOURCE_EXTRAVALUE = 2;
+
+ public Flag(int resourceId, int mask) {
+ mName = null;
+ mResource = resourceId;
+ mSource = SOURCE_CONFIG;
+ mMask = mask;
+ }
+
+ public Flag(String name, int mask) {
+ mName = name;
+ mResource = 0;
+ mSource = SOURCE_EXTRAVALUE;
+ mMask = mask;
+ }
+
+ // If context/switcher are null, set all related flags in flagArray to on.
+ public static int initFlags(Flag[] flagArray, Context context, SubtypeSwitcher switcher) {
+ int flags = 0;
+ final Resources res = null == context ? null : context.getResources();
+ for (Flag entry : flagArray) {
+ switch (entry.mSource) {
+ case Flag.SOURCE_CONFIG:
+ if (res == null || res.getBoolean(entry.mResource))
+ flags |= entry.mMask;
+ break;
+ case Flag.SOURCE_EXTRAVALUE:
+ if (switcher == null ||
+ switcher.currentSubtypeContainsExtraValueKey(entry.mName))
+ flags |= entry.mMask;
+ break;
+ }
+ }
+ return flags;
+ }
+}
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 0cc9d4198..cd7f71c2a 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -112,10 +112,10 @@ public class Suggest implements Dictionary.WordCallback {
dictionaryResId));
}
- /* package for test */ Suggest(File dictionary, long startOffset, long length,
- BinaryDictionary.Flag[] flagArray) {
- init(null, BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN,
- flagArray));
+ /* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
+ Flag[] flagArray) {
+ init(null, BinaryDictionary.initDictionary(context, dictionary, startOffset, length,
+ DIC_MAIN, flagArray));
}
private void init(Context context, BinaryDictionary mainDict) {
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index fe7aac7c2..a8cdfc02e 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -32,14 +32,14 @@ public class SuggestedWords {
public final List<SuggestedWordInfo> mSuggestedWordInfoList;
private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
- boolean hasMinamlSuggestion, List<SuggestedWordInfo> suggestedWordInfoList) {
+ boolean hasMinimalSuggestion, List<SuggestedWordInfo> suggestedWordInfoList) {
if (words != null) {
mWords = words;
} else {
mWords = Collections.emptyList();
}
mTypedWordValid = typedWordValid;
- mHasMinimalSuggestion = hasMinamlSuggestion;
+ mHasMinimalSuggestion = hasMinimalSuggestion;
mSuggestedWordInfoList = suggestedWordInfoList;
}
@@ -113,8 +113,8 @@ public class SuggestedWords {
return this;
}
- public Builder setHasMinimalSuggestion(boolean hasMinamlSuggestion) {
- mHasMinimalSuggestion = hasMinamlSuggestion;
+ public Builder setHasMinimalSuggestion(boolean hasMinimalSuggestion) {
+ mHasMinimalSuggestion = hasMinimalSuggestion;
return this;
}
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 69552a390..9244e4560 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -19,6 +19,7 @@ package com.android.inputmethod.latin;
import com.android.inputmethod.compat.InputMethodInfoCompatWrapper;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.compat.InputTypeCompatUtils;
+import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardId;
import android.content.Context;
@@ -258,6 +259,8 @@ public class Utils {
}
}
+
+ /* Damerau-Levenshtein distance */
public static int editDistance(CharSequence s, CharSequence t) {
if (s == null || t == null) {
throw new IllegalArgumentException("editDistance: Arguments should not be null.");
@@ -273,14 +276,29 @@ public class Utils {
}
for (int i = 0; i < sl; ++i) {
for (int j = 0; j < tl; ++j) {
- if (Character.toLowerCase(s.charAt(i)) == Character.toLowerCase(t.charAt(j))) {
- dp[i + 1][j + 1] = dp[i][j];
- } else {
- dp[i + 1][j + 1] = 1 + Math.min(dp[i][j],
- Math.min(dp[i + 1][j], dp[i][j + 1]));
+ final char sc = Character.toLowerCase(s.charAt(i));
+ final char tc = Character.toLowerCase(t.charAt(j));
+ final int cost = sc == tc ? 0 : 1;
+ dp[i + 1][j + 1] = Math.min(
+ dp[i][j + 1] + 1, Math.min(dp[i + 1][j] + 1, dp[i][j] + cost));
+ // Overwrite for transposition cases
+ if (i > 0 && j > 0
+ && sc == Character.toLowerCase(t.charAt(j - 1))
+ && tc == Character.toLowerCase(s.charAt(i - 1))) {
+ dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i - 1][j - 1] + cost);
}
}
}
+ if (LatinImeLogger.sDBG) {
+ Log.d(TAG, "editDistance:" + s + "," + t);
+ for (int i = 0; i < dp.length; ++i) {
+ StringBuffer sb = new StringBuffer();
+ for (int j = 0; j < dp[i].length; ++j) {
+ sb.append(dp[i][j]).append(',');
+ }
+ Log.d(TAG, i + ":" + sb.toString());
+ }
+ }
return dp[sl][tl];
}
@@ -327,8 +345,16 @@ public class Utils {
final int distance = editDistance(before, after);
// If afterLength < beforeLength, the algorithm is suggesting a word by excessive character
// correction.
+ int spaceCount = 0;
+ for (int i = 0; i < afterLength; ++i) {
+ if (after.charAt(i) == Keyboard.CODE_SPACE) {
+ ++spaceCount;
+ }
+ }
+ if (spaceCount == afterLength) return 0;
final double maximumScore = MAX_INITIAL_SCORE
- * Math.pow(TYPED_LETTER_MULTIPLIER, Math.min(beforeLength, afterLength))
+ * Math.pow(
+ TYPED_LETTER_MULTIPLIER, Math.min(beforeLength, afterLength - spaceCount))
* FULL_WORD_MULTIPLIER;
// add a weight based on edit distance.
// distance <= max(afterLength, beforeLength) == afterLength,