aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java18
-rw-r--r--java/src/com/android/inputmethod/latin/InputPointers.java13
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java45
3 files changed, 37 insertions, 39 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
index 37eced5d6..236c198ad 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
@@ -149,7 +149,8 @@ public class BinaryDictionaryFileDumper {
final int MODE_MAX = NONE;
final Uri.Builder wordListUriBuilder = getProviderUriBuilder(id);
- final String outputFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context);
+ final String finalFileName = BinaryDictionaryGetter.getCacheFileName(id, locale, context);
+ final String tempFileName = finalFileName + ".tmp";
for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) {
InputStream originalSourceStream = null;
@@ -165,7 +166,10 @@ public class BinaryDictionaryFileDumper {
if (null == afd) return null;
originalSourceStream = afd.createInputStream();
// Open output.
- outputFile = new File(outputFileName);
+ outputFile = new File(tempFileName);
+ // Just to be sure, delete the file. This may fail silently, and return false: this
+ // is the right thing to do, as we just want to continue anyway.
+ outputFile.delete();
outputStream = new FileOutputStream(outputFile);
// Get the appropriate decryption method for this try
switch (mode) {
@@ -194,14 +198,20 @@ public class BinaryDictionaryFileDumper {
break;
}
checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream);
+ outputStream.flush();
+ outputStream.close();
+ final File finalFile = new File(finalFileName);
+ if (!outputFile.renameTo(finalFile)) {
+ throw new IOException("Can't move the file to its final name");
+ }
wordListUriBuilder.appendQueryParameter(QUERY_PARAMETER_DELETE_RESULT,
QUERY_PARAMETER_SUCCESS);
if (0 >= resolver.delete(wordListUriBuilder.build(), null, null)) {
Log.e(TAG, "Could not have the dictionary pack delete a word list");
}
- BinaryDictionaryGetter.removeFilesWithIdExcept(context, id, outputFile);
+ BinaryDictionaryGetter.removeFilesWithIdExcept(context, id, finalFile);
// Success! Close files (through the finally{} clause) and return.
- return AssetFileAddress.makeFromFileName(outputFileName);
+ return AssetFileAddress.makeFromFileName(finalFileName);
} catch (Exception e) {
if (DEBUG) {
Log.i(TAG, "Can't open word list in mode " + mode + " : " + e);
diff --git a/java/src/com/android/inputmethod/latin/InputPointers.java b/java/src/com/android/inputmethod/latin/InputPointers.java
index 9d77d4e96..cd53bcd13 100644
--- a/java/src/com/android/inputmethod/latin/InputPointers.java
+++ b/java/src/com/android/inputmethod/latin/InputPointers.java
@@ -16,9 +16,6 @@
package com.android.inputmethod.latin;
-import java.util.Arrays;
-
-// TODO: Add unit test
public class InputPointers {
private final ScalableIntArray mXCoordinates = new ScalableIntArray();
private final ScalableIntArray mYCoordinates = new ScalableIntArray();
@@ -118,9 +115,10 @@ public class InputPointers {
}
public void add(int val) {
- ensureCapacity(mLength);
+ final int nextLength = mLength + 1;
+ ensureCapacity(nextLength);
mArray[mLength] = val;
- ++mLength;
+ mLength = nextLength;
}
public void ensureCapacity(int minimumCapacity) {
@@ -132,7 +130,7 @@ public class InputPointers {
private void grow(int newCapacity) {
final int[] newArray = new int[newCapacity];
- System.arraycopy(mArray, 0, newArray, 0, mLength);
+ System.arraycopy(mArray, 0, newArray, 0, mArray.length);
mArray = newArray;
}
@@ -150,7 +148,8 @@ public class InputPointers {
}
public void copy(ScalableIntArray ip) {
- mArray = Arrays.copyOf(ip.mArray, ip.mArray.length);
+ ensureCapacity(ip.mLength);
+ System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength);
mLength = ip.mLength;
}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 4be2a1799..7b4aedeb6 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1675,42 +1675,40 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
final String typedWord = mWordComposer.getTypedWord();
- final SuggestedWords suggestions;
if (!mWordComposer.isComposingWord() && !mCurrentSettings.mBigramPredictionEnabled) {
setPunctuationSuggestions();
return;
}
- if (!mWordComposer.isComposingWord()) {
- suggestions = updateBigramPredictions();
- } else {
- suggestions = updateSuggestions(typedWord);
- }
+ // Get the word on which we should search the bigrams. If we are composing a word, it's
+ // whatever is *before* the half-committed word in the buffer, hence 2; if we aren't, we
+ // should just skip whitespace if any, so 1.
+ // TODO: this is slow (2-way IPC) - we should probably cache this instead.
+ final CharSequence prevWord =
+ mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators,
+ mWordComposer.isComposingWord() ? 2 : 1);
+ SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer,
+ prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(),
+ mCurrentSettings.mCorrectionEnabled, !mWordComposer.isComposingWord());
+ suggestedWords = maybeRetrieveOlderSuggestions(typedWord, suggestedWords);
- if (null != suggestions && suggestions.size() > 0) {
- showSuggestions(suggestions, typedWord);
+ if (null != suggestedWords && suggestedWords.size() > 0) {
+ showSuggestions(suggestedWords, typedWord);
} else {
clearSuggestions();
}
}
- private SuggestedWords updateSuggestions(final CharSequence typedWord) {
- // TODO: May need a better way of retrieving previous word
- final CharSequence prevWord =
- mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators, 2);
- // getSuggestedWords handles gracefully a null value of prevWord
- final SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer,
- prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(),
- // !mWordComposer.isComposingWord() is known to be false
- mCurrentSettings.mCorrectionEnabled, !mWordComposer.isComposingWord());
-
+ private SuggestedWords maybeRetrieveOlderSuggestions(final CharSequence typedWord,
+ final SuggestedWords suggestedWords) {
+ // TODO: consolidate this into getSuggestedWords
// Basically, we update the suggestion strip only when suggestion count > 1. However,
// there is an exception: We update the suggestion strip whenever typed word's length
// is 1 or typed word is found in dictionary, regardless of suggestion count. Actually,
// in most cases, suggestion count is 1 when typed word's length is 1, but we do always
// need to clear the previous state when the user starts typing a word (i.e. typed word's
// length == 1).
- if (suggestedWords.size() > 1 || typedWord.length() == 1
+ if (suggestedWords.size() > 1 || typedWord.length() == 1 || !mWordComposer.isComposingWord()
|| !suggestedWords.mTypedWordValid
|| mSuggestionsView.isShowingAddToDictionaryHint()) {
return suggestedWords;
@@ -1891,15 +1889,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
separatorCode, prevWord);
}
- private SuggestedWords updateBigramPredictions() {
- final CharSequence prevWord =
- mConnection.getNthPreviousWord(mCurrentSettings.mWordSeparators, 1);
- return mSuggest.getSuggestedWords(mWordComposer,
- prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(),
- // !mWordComposer.isComposingWord() is known to be true
- mCurrentSettings.mCorrectionEnabled, !mWordComposer.isComposingWord());
- }
-
public void setPunctuationSuggestions() {
if (mCurrentSettings.mBigramPredictionEnabled) {
clearSuggestions();