aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--java/res/values-sw768dp/config.xml1
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java173
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java12
-rw-r--r--java/src/com/android/inputmethod/latin/FileTransforms.java40
-rw-r--r--native/src/correction.cpp66
5 files changed, 205 insertions, 87 deletions
diff --git a/java/res/values-sw768dp/config.xml b/java/res/values-sw768dp/config.xml
index 0f8f106b7..d88020952 100644
--- a/java/res/values-sw768dp/config.xml
+++ b/java/res/values-sw768dp/config.xml
@@ -32,7 +32,6 @@
<bool name="config_auto_correction_spacebar_led_enabled">false</bool>
<!-- Showing mini keyboard, just above the touched point if true, aligned to the key if false -->
<bool name="config_show_mini_keyboard_at_touched_point">true</bool>
- <integer name="config_delay_update_suggestions">180</integer>
<!-- Long pressing space will invoke IME switcher if > 0, never invoke IME switcher if == 0 -->
<integer name="config_long_press_space_key_timeout">0</integer>
<!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
index ed5f83b3b..24bb7b78a 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
@@ -63,10 +63,10 @@ public class BinaryDictionaryFileDumper {
}
/**
- * Queries a content provider for the list of dictionaries for a specific locale
+ * Queries a content provider for the list of word lists for a specific locale
* available to copy into Latin IME.
*/
- private static List<String> getDictIdList(final Locale locale, final Context context) {
+ private static List<String> getWordListIds(final Locale locale, final Context context) {
final ContentResolver resolver = context.getContentResolver();
final Uri dictionaryPackUri = getProviderUri(locale.toString());
@@ -87,77 +87,150 @@ public class BinaryDictionaryFileDumper {
return list;
}
+
/**
- * Queries a content provider for dictionary data for some locale and cache the returned files
- *
- * This will query a content provider for dictionary data for a given locale, and copy the
- * files locally so that they can be mmap'ed. This may overwrite previously cached dictionaries
- * with newer versions if a newer version is made available by the content provider.
- * @returns the addresses of the files, or null if no data could be obtained.
- * @throw FileNotFoundException if the provider returns non-existent data.
- * @throw IOException if the provider-returned data could not be read.
+ * Helper method to encapsulate exception handling.
*/
- public static List<AssetFileAddress> cacheDictionariesFromContentProvider(final Locale locale,
- final Context context) {
- final ContentResolver resolver = context.getContentResolver();
- final List<String> idList = getDictIdList(locale, context);
- final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
- for (String id : idList) {
- final Uri wordListUri = getProviderUri(id);
+ private static AssetFileDescriptor openAssetFileDescriptor(final ContentResolver resolver,
+ final Uri uri) {
+ try {
+ return resolver.openAssetFileDescriptor(uri, "r");
+ } catch (FileNotFoundException e) {
+ // I don't want to log the word list URI here for security concerns
+ Log.e(TAG, "Could not find a word list from the dictionary provider.");
+ return null;
+ }
+ }
+
+ /**
+ * Caches a word list the id of which is passed as an argument. This will write the file
+ * to the cache file name designated by its id and locale, overwriting it if already present
+ * and creating it (and its containing directory) if necessary.
+ */
+ private static AssetFileAddress cacheWordList(final String id, final Locale locale,
+ final ContentResolver resolver, final Context context) {
+
+ final int COMPRESSED_CRYPTED_COMPRESSED = 0;
+ final int CRYPTED_COMPRESSED = 1;
+ final int COMPRESSED_CRYPTED = 2;
+ final int COMPRESSED_ONLY = 3;
+ final int CRYPTED_ONLY = 4;
+ final int NONE = 5;
+ final int MODE_MIN = COMPRESSED_CRYPTED_COMPRESSED;
+ final int MODE_MAX = NONE;
+
+ final Uri wordListUri = getProviderUri(id);
+ final String outputFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context);
+
+ for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) {
+ InputStream originalSourceStream = null;
+ InputStream inputStream = null;
+ FileOutputStream outputStream = null;
AssetFileDescriptor afd = null;
try {
- afd = resolver.openAssetFileDescriptor(wordListUri, "r");
- } catch (FileNotFoundException e) {
- // leave null inside afd and continue
- }
- if (null == afd) continue;
- try {
- final String fileName = copyFileTo(afd.createInputStream(),
- BinaryDictionaryGetter.getCacheFileName(id, locale, context));
- afd.close();
+ // Open input.
+ afd = openAssetFileDescriptor(resolver, wordListUri);
+ // If we can't open it at all, don't even try a number of times.
+ if (null == afd) return null;
+ originalSourceStream = afd.createInputStream();
+ // Open output.
+ outputStream = new FileOutputStream(outputFileName);
+ // Get the appropriate decryption method for this try
+ switch (mode) {
+ case COMPRESSED_CRYPTED_COMPRESSED:
+ inputStream = FileTransforms.getUncompressedStream(
+ FileTransforms.getDecryptedStream(
+ FileTransforms.getUncompressedStream(
+ originalSourceStream)));
+ break;
+ case CRYPTED_COMPRESSED:
+ inputStream = FileTransforms.getUncompressedStream(
+ FileTransforms.getDecryptedStream(originalSourceStream));
+ break;
+ case COMPRESSED_CRYPTED:
+ inputStream = FileTransforms.getDecryptedStream(
+ FileTransforms.getUncompressedStream(originalSourceStream));
+ break;
+ case COMPRESSED_ONLY:
+ inputStream = FileTransforms.getUncompressedStream(originalSourceStream);
+ break;
+ case CRYPTED_ONLY:
+ inputStream = FileTransforms.getDecryptedStream(originalSourceStream);
+ break;
+ case NONE:
+ inputStream = originalSourceStream;
+ break;
+ }
+ copyFileTo(inputStream, outputStream);
if (0 >= resolver.delete(wordListUri, null, null)) {
- // I'd rather not print the word list ID to the log out of security concerns
Log.e(TAG, "Could not have the dictionary pack delete a word list");
}
- fileAddressList.add(AssetFileAddress.makeFromFileName(fileName));
- } catch (IOException e) {
- // Can't read the file for some reason. Continue onto the next file.
- Log.e(TAG, "Cannot read a word list from the dictionary pack : " + e);
+ // Success! Close files (through the finally{} clause) and return.
+ return AssetFileAddress.makeFromFileName(outputFileName);
+ } catch (Exception e) {
+ Log.e(TAG, "Can't open word list in mode " + mode + " : " + e);
+ // Try the next method.
+ } finally {
+ // Ignore exceptions while closing files.
+ try {
+ // afd.close() will close inputStream, we should not call inputStream.close().
+ if (null != afd) afd.close();
+ } catch (Exception e) {
+ Log.e(TAG, "Exception while closing a cross-process file descriptor : " + e);
+ }
+ try {
+ if (null != outputStream) outputStream.close();
+ } catch (Exception e) {
+ Log.e(TAG, "Exception while closing a file : " + e);
+ }
}
}
- return fileAddressList;
+
+ // We could not copy the file at all. This is very unexpected.
+ // I'd rather not print the word list ID to the log out of security concerns
+ Log.e(TAG, "Could not copy a word list. Will not be able to use it.");
+ // If we can't copy it we should probably delete it to avoid trying to copy it over
+ // and over each time we open LatinIME.
+ if (0 >= resolver.delete(wordListUri, null, null)) {
+ Log.e(TAG, "In addition, we were unable to delete it.");
+ }
+ return null;
}
/**
- * Accepts a resource number as dictionary data for some locale and returns the name of a file.
+ * Queries a content provider for word list data for some locale and cache the returned files
*
- * This will make the resource the cached dictionary for this locale, overwriting any previous
- * cached data.
+ * This will query a content provider for word list data for a given locale, and copy the
+ * files locally so that they can be mmap'ed. This may overwrite previously cached word lists
+ * with newer versions if a newer version is made available by the content provider.
+ * @returns the addresses of the word list files, or null if no data could be obtained.
+ * @throw FileNotFoundException if the provider returns non-existent data.
+ * @throw IOException if the provider-returned data could not be read.
*/
- public static String getDictionaryFileFromResource(int resource, Locale locale,
- Context context) throws FileNotFoundException, IOException {
- final Resources res = context.getResources();
- final Locale savedLocale = Utils.setSystemLocale(res, locale);
- final InputStream stream = res.openRawResource(resource);
- Utils.setSystemLocale(res, savedLocale);
- return copyFileTo(stream,
- BinaryDictionaryGetter.getCacheFileName(Integer.toString(resource),
- locale, context));
+ public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale,
+ final Context context) {
+ final ContentResolver resolver = context.getContentResolver();
+ final List<String> idList = getWordListIds(locale, context);
+ final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
+ for (String id : idList) {
+ final AssetFileAddress afd = cacheWordList(id, locale, resolver, context);
+ if (null != afd) {
+ fileAddressList.add(afd);
+ }
+ }
+ return fileAddressList;
}
/**
- * Copies the data in an input stream to a target file, creating the file if necessary and
- * overwriting it if it already exists.
+ * Copies the data in an input stream to a target file.
* @param input the stream to be copied.
- * @param outputFileName the name of a file to copy the data to. It is created if necessary.
+ * @param outputFile an outputstream to copy the data to.
*/
- private static String copyFileTo(final InputStream input, final String outputFileName)
+ private static void copyFileTo(final InputStream input, final FileOutputStream output)
throws FileNotFoundException, IOException {
final byte[] buffer = new byte[FILE_READ_BUFFER_SIZE];
- final FileOutputStream output = new FileOutputStream(outputFileName);
for (int readBytes = input.read(buffer); readBytes >= 0; readBytes = input.read(buffer))
output.write(buffer, 0, readBytes);
input.close();
- return outputFileName;
}
}
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
index 5d2dab0a9..38344300c 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
@@ -205,7 +205,7 @@ class BinaryDictionaryGetter {
* @param context the context on which to open the files upon.
* @return an array of binary dictionary files, which may be empty but may not be null.
*/
- private static File[] getCachedDictionaryList(final Locale locale,
+ private static File[] getCachedWordLists(final Locale locale,
final Context context) {
final String directoryName = getCacheDirectoryForLocale(locale, context);
final File[] cacheFiles = new File(directoryName).listFiles();
@@ -235,11 +235,11 @@ class BinaryDictionaryGetter {
public static List<AssetFileAddress> getDictionaryFiles(final Locale locale,
final Context context, final int fallbackResId) {
- // cacheDictionariesFromContentProvider returns the list of files it copied to local
+ // cacheWordListsFromContentProvider returns the list of files it copied to local
// storage, but we don't really care about what was copied NOW: what we want is the
// list of everything we ever cached, so we ignore the return value.
- BinaryDictionaryFileDumper.cacheDictionariesFromContentProvider(locale, context);
- final File[] cachedDictionaryList = getCachedDictionaryList(locale, context);
+ BinaryDictionaryFileDumper.cacheWordListsFromContentProvider(locale, context);
+ final File[] cachedWordLists = getCachedWordLists(locale, context);
final String mainDictId = getMainDictId(locale);
@@ -247,8 +247,8 @@ class BinaryDictionaryGetter {
boolean foundMainDict = false;
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
- // cachedDictionaryList may not be null, see doc for getCachedDictionaryList
- for (final File f : cachedDictionaryList) {
+ // cachedWordLists may not be null, see doc for getCachedDictionaryList
+ for (final File f : cachedWordLists) {
final String wordListId = getWordListIdFromFileName(f.getName());
if (wordListId.equals(mainDictId)) {
foundMainDict = true;
diff --git a/java/src/com/android/inputmethod/latin/FileTransforms.java b/java/src/com/android/inputmethod/latin/FileTransforms.java
new file mode 100644
index 000000000..d0374e01e
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/FileTransforms.java
@@ -0,0 +1,40 @@
+/*
+ * 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.util.Log;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.GZIPInputStream;
+
+public class FileTransforms {
+ public static OutputStream getCryptedStream(OutputStream out) {
+ // Crypt the stream.
+ return out;
+ }
+
+ public static InputStream getDecryptedStream(InputStream in) {
+ // Decrypt the stream.
+ return in;
+ }
+
+ public static InputStream getUncompressedStream(InputStream in) throws IOException {
+ return new GZIPInputStream(in);
+ }
+}
diff --git a/native/src/correction.cpp b/native/src/correction.cpp
index 72cb3715f..fcb8bea5c 100644
--- a/native/src/correction.cpp
+++ b/native/src/correction.cpp
@@ -210,6 +210,10 @@ Correction::CorrectionType Correction::processSkipChar(
Correction::CorrectionType Correction::processCharAndCalcState(
const int32_t c, const bool isTerminal) {
+ const int correctionCount = (mSkippedCount + mExcessiveCount + mTransposedCount);
+ // TODO: Change the limit if we'll allow two or more corrections
+ const bool noCorrectionsHappenedSoFar = correctionCount == 0;
+ const bool canTryCorrection = noCorrectionsHappenedSoFar;
if (mNeedsToTraverseAllNodes || isQuote(c)) {
bool incremented = false;
@@ -235,7 +239,7 @@ Correction::CorrectionType Correction::processCharAndCalcState(
mExcessivePos = mOutputIndex;
}
if (mExcessivePos < mInputLength - 1) {
- mExceeding = mExcessivePos == mInputIndex;
+ mExceeding = mExcessivePos == mInputIndex && canTryCorrection;
}
}
@@ -246,7 +250,7 @@ Correction::CorrectionType Correction::processCharAndCalcState(
}
mSkipPos = mOutputIndex;
}
- mSkipping = mSkipPos == mOutputIndex;
+ mSkipping = mSkipPos == mOutputIndex && canTryCorrection;
}
if (mTransposedPos >= 0) {
@@ -254,7 +258,7 @@ Correction::CorrectionType Correction::processCharAndCalcState(
mTransposedPos = mOutputIndex;
}
if (mTransposedPos < mInputLength - 1) {
- mTransposing = mInputIndex == mTransposedPos;
+ mTransposing = mInputIndex == mTransposedPos && canTryCorrection;
}
}
@@ -280,10 +284,8 @@ Correction::CorrectionType Correction::processCharAndCalcState(
}
}
- const bool noCorrectionsHappenedSoFar =
- (mSkippedCount + mExcessiveCount + mTransposedCount) == 0;
- // TODO: sum counters
- const bool checkProximityChars = noCorrectionsHappenedSoFar;
+ // TODO: Change the limit if we'll allow two or more proximity chars with corrections
+ const bool checkProximityChars = noCorrectionsHappenedSoFar || mProximityCount == 0;
const int matchedProximityCharId = secondTransposing
? ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR
: mProximityInfo->getMatchedProximityId(mInputIndex, c, checkProximityChars);
@@ -293,16 +295,14 @@ Correction::CorrectionType Correction::processCharAndCalcState(
// As the current char turned out to be an unrelated char,
// we will try other correction-types. Please note that mCorrectionStates[mOutputIndex]
// here refers to the previous state.
- if (noCorrectionsHappenedSoFar
- && mCorrectionStates[mOutputIndex].mProximityMatching
+ if (canTryCorrection && mCorrectionStates[mOutputIndex].mProximityMatching
&& mCorrectionStates[mOutputIndex].mExceeding
&& mProximityInfo->getMatchedProximityId(mInputIndex, mWord[mOutputIndex], false)
== ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
- // TODO: check transpose in the same way?
+ // Conversion p->e
++mExcessiveCount;
--mProximityCount;
- } else if (mInputIndex < mInputLength - 1 && mOutputIndex > 0
- && mTransposedCount > 0 && mExcessiveCount == 0
+ } else if (mInputIndex < mInputLength - 1 && mOutputIndex > 0 && mTransposedCount > 0
&& !mCorrectionStates[mOutputIndex].mTransposing
&& mCorrectionStates[mOutputIndex - 1].mTransposing
&& mProximityInfo->getMatchedProximityId(
@@ -310,47 +310,50 @@ Correction::CorrectionType Correction::processCharAndCalcState(
== ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR
&& mProximityInfo->getMatchedProximityId(mInputIndex + 1, c, false)
== ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
+ // Conversion t->e
// Example:
// occaisional -> occa sional
// mmmmttx -> mmmm(E)mmmmmm
mTransposedCount -= 2;
++mExcessiveCount;
++mInputIndex;
- } else if (mOutputIndex > 0 && mInputIndex > 0 && mTransposedCount > 0 && mSkippedCount == 0
+ } else if (mOutputIndex > 0 && mInputIndex > 0 && mTransposedCount > 0
&& !mCorrectionStates[mOutputIndex].mTransposing
&& mCorrectionStates[mOutputIndex - 1].mTransposing
&& mProximityInfo->getMatchedProximityId(mInputIndex - 1, c, false)
== ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
+ // Conversion t->s
// Example:
// chcolate -> chocolate
// mmttx -> mmsmmmmmm
mTransposedCount -= 2;
++mSkippedCount;
--mInputIndex;
- } else if (mInputIndex - 1 < mInputLength && (mExceeding || mTransposing)
+ } else if (canTryCorrection && mInputIndex > 0
+ && mCorrectionStates[mOutputIndex].mProximityMatching
+ && mCorrectionStates[mOutputIndex].mSkipping
+ && mProximityInfo->getMatchedProximityId(mInputIndex - 1, c, false)
+ == ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
+ // Conversion p->s
+ // Note: This logic tries saving cases like contrst --> contrast -- "a" is one of
+ // proximity chars of "s", but it should rather be handled as a skipped char.
+ ++mSkippedCount;
+ --mProximityCount;
+ return processSkipChar(c, isTerminal, false);
+ } else if ((mExceeding || mTransposing) && mInputIndex - 1 < mInputLength
&& mProximityInfo->getMatchedProximityId(mInputIndex + 1, c, false)
== ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
+ // 1.2. Excessive or transpose correction
if (mTransposing) {
++mTransposedCount;
} else {
++mExcessiveCount;
incrementInputIndex();
}
- } else if (mProximityCount == 0 && noCorrectionsHappenedSoFar) {
- // Skip this letter and continue deeper
+ } else if (mSkipping) {
+ // 3. Skip correction
++mSkippedCount;
return processSkipChar(c, isTerminal, false);
- } else if (noCorrectionsHappenedSoFar
- && mInputIndex > 0
- && mCorrectionStates[mOutputIndex].mProximityMatching
- && mCorrectionStates[mOutputIndex].mSkipping
- && mProximityInfo->getMatchedProximityId(mInputIndex - 1, c, false)
- == ProximityInfo::SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR) {
- // Note: This logic tries saving cases like contrst --> contrast -- "a" is one of
- // proximity chars of "s", but it should rather be handled as a skipped char.
- ++mSkippedCount;
- --mProximityCount;
- return processSkipChar(c, isTerminal, false);
} else {
if (DEBUG_CORRECTION) {
DUMP_WORD(mWord, mOutputIndex);
@@ -371,9 +374,9 @@ Correction::CorrectionType Correction::processCharAndCalcState(
mWord[mOutputIndex] = c;
- mLastCharExceeded = mExcessiveCount == 0 && mSkippedCount == 0
- && mProximityCount == 0 && mTransposedCount == 0
- && (mInputIndex == mInputLength - 2);
+ // 4. Last char excessive correction
+ mLastCharExceeded = mExcessiveCount == 0 && mSkippedCount == 0 && mTransposedCount == 0
+ && mProximityCount == 0 && (mInputIndex == mInputLength - 2);
const bool isSameAsUserTypedLength = (mInputLength == mInputIndex + 1) || mLastCharExceeded;
if (mLastCharExceeded) {
++mExcessiveCount;
@@ -663,6 +666,9 @@ int Correction::RankingAlgorithm::calculateFinalFreq(const int inputIndex, const
m ... matching
s ... skipping
a ... traversing all
+ t ... transposing
+ e ... exceeding
+ p ... proximity matching
*/
if (matchCount == inputLength && matchCount >= 2 && !skipped
&& word[matchCount] == word[matchCount - 1]) {