diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
14 files changed, 288 insertions, 246 deletions
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 851ecc042..a3a329a71 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -40,7 +40,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Locale; -import java.util.Map; /** * Implements a static, compacted, binary dictionary of standard words. @@ -142,8 +141,6 @@ public final class BinaryDictionary extends Dictionary { JniUtils.loadNativeLibrary(); } - private static native boolean createEmptyDictFileNative(String filePath, long dictVersion, - String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray); private static native long openNative(String sourceDir, long dictOffset, long dictSize, boolean isUpdatable); private static native void getHeaderInfoNative(long dict, int[] outHeaderSize, @@ -167,8 +164,6 @@ public final class BinaryDictionary extends Dictionary { int[] suggestOptions, int[] prevWordCodePointArray, int[] outputCodePoints, int[] outputScores, int[] outputIndices, int[] outputTypes, int[] outputAutoCommitFirstWordConfidence); - private static native float calcNormalizedScoreNative(int[] before, int[] after, int score); - private static native int editDistanceNative(int[] before, int[] after); private static native void addUnigramWordNative(long dict, int[] word, int probability, int[] shortcutTarget, int shortcutProbability, boolean isNotAWord, boolean isBlacklisted, int timestamp); @@ -179,24 +174,9 @@ public final class BinaryDictionary extends Dictionary { LanguageModelParam[] languageModelParams, int startIndex); private static native int calculateProbabilityNative(long dict, int unigramProbability, int bigramProbability); - private static native int setCurrentTimeForTestNative(int currentTime); private static native String getPropertyNative(long dict, String query); private static native boolean isCorruptedNative(long dict); - public static boolean createEmptyDictFile(final String filePath, final long dictVersion, - final Locale locale, final Map<String, String> attributeMap) { - final String[] keyArray = new String[attributeMap.size()]; - final String[] valueArray = new String[attributeMap.size()]; - int index = 0; - for (final String key : attributeMap.keySet()) { - keyArray[index] = key; - valueArray[index] = attributeMap.get(key); - index++; - } - return createEmptyDictFileNative(filePath, dictVersion, locale.toString(), keyArray, - valueArray); - } - // TODO: Move native dict into session private final void loadDictionary(final String path, final long startOffset, final long length, final boolean isUpdatable) { @@ -219,7 +199,6 @@ public final class BinaryDictionary extends Dictionary { return true; } - @UsedForTesting public DictionaryHeader getHeader() throws UnsupportedFormatException { if (mNativeDict == 0) { return null; @@ -323,20 +302,6 @@ public final class BinaryDictionary extends Dictionary { return getFormatVersionNative(mNativeDict); } - public static float calcNormalizedScore(final String before, final String after, - final int score) { - return calcNormalizedScoreNative(StringUtils.toCodePointArray(before), - StringUtils.toCodePointArray(after), score); - } - - public static int editDistance(final String before, final String after) { - if (before == null || after == null) { - throw new IllegalArgumentException(); - } - return editDistanceNative(StringUtils.toCodePointArray(before), - StringUtils.toCodePointArray(after)); - } - @Override public boolean isValidWord(final String word) { return getFrequency(word) != NOT_A_PROBABILITY; @@ -497,22 +462,6 @@ public final class BinaryDictionary extends Dictionary { return calculateProbabilityNative(mNativeDict, unigramProbability, bigramProbability); } - /** - * Control the current time to be used in the native code. If currentTime >= 0, this method sets - * the current time and gets into test mode. - * In test mode, set timestamp is used as the current time in the native code. - * If currentTime < 0, quit the test mode and returns to using time() to get the current time. - * - * @param currentTime seconds since the unix epoch - * @return current time got in the native code. - */ - @UsedForTesting - public static int setCurrentTimeForTest(final int currentTime) { - final int currentNativeTimestamp = setCurrentTimeForTestNative(currentTime); - PersonalizationHelper.currentTimeChangedForTesting(currentNativeTimestamp); - return currentNativeTimestamp; - } - @UsedForTesting public String getPropertyForTest(final String query) { if (!isValidDictionary()) return ""; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index acbd919cd..4c49cb31c 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -21,10 +21,9 @@ import android.content.SharedPreferences; import android.content.res.AssetFileDescriptor; import android.util.Log; -import com.android.inputmethod.latin.makedict.DictDecoder; import com.android.inputmethod.latin.makedict.DictionaryHeader; -import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.UnsupportedFormatException; +import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.DictionaryInfoUtils; import com.android.inputmethod.latin.utils.LocaleUtils; @@ -226,12 +225,10 @@ final public class BinaryDictionaryGetter { // ## HACK ## we prevent usage of a dictionary before version 18. The reason for this is, since // those do not include whitelist entries, the new code with an old version of the dictionary // would lose whitelist functionality. - private static boolean hackCanUseDictionaryFile(final Locale locale, final File f) { + private static boolean hackCanUseDictionaryFile(final Locale locale, final File file) { try { // Read the version of the file - final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f, 0, f.length()); - final DictionaryHeader header = dictDecoder.readHeader(); - + final DictionaryHeader header = BinaryDictionaryUtils.getHeader(file); final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY); if (null == version) { // No version in the options : the format is unexpected diff --git a/java/src/com/android/inputmethod/latin/Constants.java b/java/src/com/android/inputmethod/latin/Constants.java index d1ff714fc..e71723a15 100644 --- a/java/src/com/android/inputmethod/latin/Constants.java +++ b/java/src/com/android/inputmethod/latin/Constants.java @@ -144,6 +144,7 @@ public final class Constants { public static final int NOT_A_CODE = -1; public static final int NOT_A_CURSOR_POSITION = -1; + // TODO: replace the following constants with state in InputTransaction? public static final int NOT_A_COORDINATE = -1; public static final int SUGGESTION_STRIP_COORDINATE = -2; public static final int SPELL_CHECKER_COORDINATE = -3; diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index 26545acbd..7847738e0 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -27,6 +27,7 @@ import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.WordProperty; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.utils.AsyncResultHolder; +import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CombinedFormatUtils; import com.android.inputmethod.latin.utils.ExecutorUtils; @@ -228,7 +229,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { } private void createBinaryDictionaryLocked() { - BinaryDictionary.createEmptyDictFile(mDictFile.getAbsolutePath(), + BinaryDictionaryUtils.createEmptyDictFile(mDictFile.getAbsolutePath(), DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap()); } diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 1747eeeda..ba64028ca 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -22,6 +22,7 @@ import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.utils.AutoCorrectionUtils; +import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.BoundedTreeSet; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.StringUtils; @@ -309,7 +310,7 @@ public final class Suggest { // than i because we added the typed word to mSuggestions without touching mScores. for (int i = 0; i < suggestionsSize - 1; ++i) { final SuggestedWordInfo cur = suggestions.get(i + 1); - final float normalizedScore = BinaryDictionary.calcNormalizedScore( + final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( typedWord, cur.toString(), cur.mScore); final String scoreInfoString; if (normalizedScore > 0) { diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index f7cf8dea1..df0068266 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -23,12 +23,12 @@ import android.text.style.SuggestionSpan; import android.util.Log; import android.view.KeyCharacterMap; import android.view.KeyEvent; -import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CorrectionInfo; import android.view.inputmethod.EditorInfo; import com.android.inputmethod.compat.SuggestionSpanUtils; import com.android.inputmethod.event.EventInterpreter; +import com.android.inputmethod.event.InputTransaction; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Dictionary; @@ -361,40 +361,37 @@ public final class InputLogic { final SettingsValues settingsValues, // TODO: remove these two arguments final LatinIME.UIHandler handler, final KeyboardSwitcher keyboardSwitcher) { + final InputTransaction inputTransaction = new InputTransaction(settingsValues, code, x, y, + SystemClock.uptimeMillis(), mSpaceState, + getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode())); if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.latinIME_onCodeInput(code, x, y); + ResearchLogger.latinIME_onCodeInput(inputTransaction.mKeyCode, + inputTransaction.mX, inputTransaction.mY); } - final long when = SystemClock.uptimeMillis(); - if (code != Constants.CODE_DELETE - || when > mLastKeyTime + Constants.LONG_PRESS_MILLISECONDS) { + if (inputTransaction.mKeyCode != Constants.CODE_DELETE + || inputTransaction.mTimestamp > mLastKeyTime + Constants.LONG_PRESS_MILLISECONDS) { mDeleteCount = 0; } - mLastKeyTime = when; + mLastKeyTime = inputTransaction.mTimestamp; mConnection.beginBatchEdit(); - // The space state depends only on the last character pressed and its own previous - // state. Here, we revert the space state to neutral if the key is actually modifying - // the input contents (any non-shift key), which is what we should do for - // all inputs that do not result in a special state. Each character handling is then - // free to override the state as they see fit. - final int spaceState = mSpaceState; if (!mWordComposer.isComposingWord()) { mIsAutoCorrectionIndicatorOn = false; } // TODO: Consolidate the double-space period timer, mLastKeyTime, and the space state. - if (code != Constants.CODE_SPACE) { + if (inputTransaction.mKeyCode != Constants.CODE_SPACE) { handler.cancelDoubleSpacePeriodTimer(); } boolean didAutoCorrect = false; - switch (code) { + switch (inputTransaction.mKeyCode) { case Constants.CODE_DELETE: - handleBackspace(settingsValues, spaceState, handler, keyboardSwitcher); - LatinImeLogger.logOnDelete(x, y); + handleBackspace(inputTransaction, handler); + LatinImeLogger.logOnDelete(inputTransaction.mX, inputTransaction.mY); break; case Constants.CODE_SHIFT: - performRecapitalization(settingsValues); - keyboardSwitcher.updateShiftState(); + performRecapitalization(inputTransaction.mSettingsValues); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); break; case Constants.CODE_CAPSLOCK: // Note: Changing keyboard to shift lock state is handled in @@ -448,32 +445,43 @@ public final class InputLogic { } else { // No action label, and the action from imeOptions is NONE: this is a regular // enter key that should input a carriage return. - didAutoCorrect = handleNonSpecialCharacter(settingsValues, Constants.CODE_ENTER, - x, y, spaceState, keyboardSwitcher, handler); + didAutoCorrect = handleNonSpecialCharacter(inputTransaction, handler); } break; case Constants.CODE_SHIFT_ENTER: - didAutoCorrect = handleNonSpecialCharacter(settingsValues, Constants.CODE_ENTER, - x, y, spaceState, keyboardSwitcher, handler); + // TODO: remove this object + final InputTransaction tmpTransaction = new InputTransaction( + inputTransaction.mSettingsValues, inputTransaction.mKeyCode, + inputTransaction.mX, inputTransaction.mY, inputTransaction.mTimestamp, + inputTransaction.mSpaceState, inputTransaction.mShiftState); + didAutoCorrect = handleNonSpecialCharacter(tmpTransaction, handler); break; case Constants.CODE_ALPHA_FROM_EMOJI: // Note: Switching back from Emoji keyboard to the main keyboard is being handled in // {@link KeyboardState#onCodeInput(int,int)}. break; default: - didAutoCorrect = handleNonSpecialCharacter(settingsValues, - code, x, y, spaceState, keyboardSwitcher, handler); + didAutoCorrect = handleNonSpecialCharacter(inputTransaction, handler); break; } // Reset after any single keystroke, except shift, capslock, and symbol-shift - if (!didAutoCorrect && code != Constants.CODE_SHIFT - && code != Constants.CODE_CAPSLOCK - && code != Constants.CODE_SWITCH_ALPHA_SYMBOL) + if (!didAutoCorrect && inputTransaction.mKeyCode != Constants.CODE_SHIFT + && inputTransaction.mKeyCode != Constants.CODE_CAPSLOCK + && inputTransaction.mKeyCode != Constants.CODE_SWITCH_ALPHA_SYMBOL) mLastComposedWord.deactivate(); - if (Constants.CODE_DELETE != code) { + if (Constants.CODE_DELETE != inputTransaction.mKeyCode) { mEnteredText = null; } mConnection.endBatchEdit(); + switch (inputTransaction.getRequiredShiftUpdate()) { + case InputTransaction.SHIFT_UPDATE_LATER: + mLatinIME.mHandler.postUpdateShiftState(); + break; + case InputTransaction.SHIFT_UPDATE_NOW: + keyboardSwitcher.updateShiftState(); + break; + default: // SHIFT_NO_UPDATE + } } public void onStartBatchInput(final SettingsValues settingsValues, @@ -617,31 +625,26 @@ public final class InputLogic { * manage keyboard-related stuff like shift, language switch, settings, layout switch, or * any key that results in multiple code points like the ".com" key. * - * @param settingsValues The current settings values. - * @param codePoint the code point associated with the key. - * @param x the x-coordinate of the key press, or Contants.NOT_A_COORDINATE if not applicable. - * @param y the y-coordinate of the key press, or Contants.NOT_A_COORDINATE if not applicable. - * @param spaceState the space state at start of the batch input. + * @param inputTransaction The transaction in progress. * @return whether this caused an auto-correction to happen. */ - private boolean handleNonSpecialCharacter(final SettingsValues settingsValues, - final int codePoint, final int x, final int y, final int spaceState, - // TODO: remove these arguments - final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) { + private boolean handleNonSpecialCharacter(final InputTransaction inputTransaction, + // TODO: remove this argument + final LatinIME.UIHandler handler) { mSpaceState = SpaceState.NONE; final boolean didAutoCorrect; - if (settingsValues.isWordSeparator(codePoint) - || Character.getType(codePoint) == Character.OTHER_SYMBOL) { - didAutoCorrect = handleSeparator(settingsValues, codePoint, - Constants.SUGGESTION_STRIP_COORDINATE == x, spaceState, keyboardSwitcher, - handler); - if (settingsValues.mIsInternal) { - LatinImeLoggerUtils.onSeparator((char)codePoint, x, y); + if (inputTransaction.mSettingsValues.isWordSeparator(inputTransaction.mKeyCode) + || Character.getType(inputTransaction.mKeyCode) == Character.OTHER_SYMBOL) { + didAutoCorrect = handleSeparator(inputTransaction, + Constants.SUGGESTION_STRIP_COORDINATE == inputTransaction.mX, handler); + if (inputTransaction.mSettingsValues.mIsInternal) { + LatinImeLoggerUtils.onSeparator((char)inputTransaction.mKeyCode, + inputTransaction.mX, inputTransaction.mY); } } else { didAutoCorrect = false; - if (SpaceState.PHANTOM == spaceState) { - if (settingsValues.mIsInternal) { + if (SpaceState.PHANTOM == inputTransaction.mSpaceState) { + if (inputTransaction.mSettingsValues.mIsInternal) { if (mWordComposer.isComposingWord() && mWordComposer.isBatchMode()) { LatinImeLoggerUtils.onAutoCorrection("", mWordComposer.getTypedWord(), " ", mWordComposer); @@ -653,11 +656,10 @@ public final class InputLogic { resetEntireInputState(mConnection.getExpectedSelectionStart(), mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */); } else { - commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR); + commitTyped(inputTransaction.mSettingsValues, LastComposedWord.NOT_A_SEPARATOR); } } - handleNonSeparator(settingsValues, codePoint, x, y, spaceState, - keyboardSwitcher, handler); + handleNonSeparator(inputTransaction.mSettingsValues, inputTransaction, handler); } return didAutoCorrect; } @@ -665,15 +667,12 @@ public final class InputLogic { /** * Handle a non-separator. * @param settingsValues The current settings values. - * @param codePoint the code point associated with the key. - * @param x the x-coordinate of the key press, or Contants.NOT_A_COORDINATE if not applicable. - * @param y the y-coordinate of the key press, or Contants.NOT_A_COORDINATE if not applicable. - * @param spaceState the space state at start of the batch input. + * @param inputTransaction The transaction in progress. */ private void handleNonSeparator(final SettingsValues settingsValues, - final int codePoint, final int x, final int y, final int spaceState, - // TODO: Remove these arguments - final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) { + final InputTransaction inputTransaction, + // TODO: Remove this argument + final LatinIME.UIHandler handler) { // TODO: refactor this method to stop flipping isComposingWord around all the time, and // make it shorter (possibly cut into several pieces). Also factor handleNonSpecialCharacter // which has the same name as other handle* methods but is not the same. @@ -681,7 +680,8 @@ public final class InputLogic { // TODO: remove isWordConnector() and use isUsuallyFollowedBySpace() instead. // See onStartBatchInput() to see how to do it. - if (SpaceState.PHANTOM == spaceState && !settingsValues.isWordConnector(codePoint)) { + if (SpaceState.PHANTOM == inputTransaction.mSpaceState + && !settingsValues.isWordConnector(inputTransaction.mKeyCode)) { if (isComposingWord) { // Sanity check throw new RuntimeException("Should not be composing here"); @@ -703,7 +703,7 @@ public final class InputLogic { if (!isComposingWord // We only start composing if this is a word code point. Essentially that means it's a // a letter or a word connector. - && settingsValues.isWordCodePoint(codePoint) + && settingsValues.isWordCodePoint(inputTransaction.mKeyCode) // We never go into composing state if suggestions are not requested. && settingsValues.isSuggestionsRequested() && // In languages with spaces, we only start composing a word when we are not already @@ -714,8 +714,8 @@ public final class InputLogic { // the character is a single quote or a dash. The idea here is, single quote and dash // are not separators and they should be treated as normal characters, except in the // first position where they should not start composing a word. - isComposingWord = (Constants.CODE_SINGLE_QUOTE != codePoint - && Constants.CODE_DASH != codePoint); + isComposingWord = (Constants.CODE_SINGLE_QUOTE != inputTransaction.mKeyCode + && Constants.CODE_DASH != inputTransaction.mKeyCode); // Here we don't need to reset the last composed word. It will be reset // when we commit this one, if we ever do; if on the other hand we backspace // it entirely and resume suggestions on the previous word, we'd like to still @@ -723,26 +723,25 @@ public final class InputLogic { resetComposingState(false /* alsoResetLastComposedWord */); } if (isComposingWord) { - mWordComposer.add(codePoint, x, y); + mWordComposer.add(inputTransaction.mKeyCode, inputTransaction.mX, inputTransaction.mY); // If it's the first letter, make note of auto-caps state if (mWordComposer.size() == 1) { // We pass 1 to getPreviousWordForSuggestion because we were not composing a word // yet, so the word we want is the 1st word before the cursor. mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime( - getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode()), - getNthPreviousWordForSuggestion( + inputTransaction.mShiftState, getNthPreviousWordForSuggestion( settingsValues.mSpacingAndPunctuations, 1 /* nthPreviousWord */)); } mConnection.setComposingText(getTextWithUnderline( mWordComposer.getTypedWord()), 1); } else { - final boolean swapWeakSpace = maybeStripSpace(settingsValues, - codePoint, spaceState, Constants.SUGGESTION_STRIP_COORDINATE == x); + final boolean swapWeakSpace = maybeStripSpace(inputTransaction, + Constants.SUGGESTION_STRIP_COORDINATE == inputTransaction.mX); - sendKeyCodePoint(settingsValues, codePoint); + sendKeyCodePoint(settingsValues, inputTransaction.mKeyCode); if (swapWeakSpace) { - swapSwapperAndSpace(keyboardSwitcher); + swapSwapperAndSpace(inputTransaction); mSpaceState = SpaceState.WEAK; } // In case the "add to dictionary" hint was still displayed. @@ -750,26 +749,26 @@ public final class InputLogic { } handler.postUpdateSuggestionStrip(); if (settingsValues.mIsInternal) { - LatinImeLoggerUtils.onNonSeparator((char)codePoint, x, y); + LatinImeLoggerUtils.onNonSeparator((char)inputTransaction.mKeyCode, inputTransaction.mX, + inputTransaction.mY); } } /** * Handle input of a separator code point. - * @param settingsValues The current settings values. - * @param codePoint the code point associated with the key. + * @param inputTransaction The transaction in progress. * @param isFromSuggestionStrip whether this code point comes from the suggestion strip. - * @param spaceState the space state at start of the batch input. * @return whether this caused an auto-correction to happen. */ - private boolean handleSeparator(final SettingsValues settingsValues, - final int codePoint, final boolean isFromSuggestionStrip, final int spaceState, - // TODO: remove these arguments - final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) { + private boolean handleSeparator(final InputTransaction inputTransaction, + final boolean isFromSuggestionStrip, + // TODO: remove this argument + final LatinIME.UIHandler handler) { boolean didAutoCorrect = false; // We avoid sending spaces in languages without spaces if we were composing. - final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint - && !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces + final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == inputTransaction.mKeyCode + && !inputTransaction.mSettingsValues.mSpacingAndPunctuations + .mCurrentLanguageHasSpaces && mWordComposer.isComposingWord(); if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { // If we are in the middle of a recorrection, we need to commit the recorrection @@ -779,48 +778,51 @@ public final class InputLogic { } // isComposingWord() may have changed since we stored wasComposing if (mWordComposer.isComposingWord()) { - if (settingsValues.mCorrectionEnabled) { + if (inputTransaction.mSettingsValues.mCorrectionEnabled) { final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR - : StringUtils.newSingleCodePointString(codePoint); - commitCurrentAutoCorrection(settingsValues, separator, handler); + : StringUtils.newSingleCodePointString(inputTransaction.mKeyCode); + commitCurrentAutoCorrection(inputTransaction.mSettingsValues, separator, handler); didAutoCorrect = true; } else { - commitTyped(settingsValues, StringUtils.newSingleCodePointString(codePoint)); + commitTyped(inputTransaction.mSettingsValues, + StringUtils.newSingleCodePointString(inputTransaction.mKeyCode)); } } - final boolean swapWeakSpace = maybeStripSpace(settingsValues, codePoint, spaceState, - isFromSuggestionStrip); + final boolean swapWeakSpace = maybeStripSpace(inputTransaction, isFromSuggestionStrip); - final boolean isInsideDoubleQuoteOrAfterDigit = Constants.CODE_DOUBLE_QUOTE == codePoint + final boolean isInsideDoubleQuoteOrAfterDigit = + Constants.CODE_DOUBLE_QUOTE == inputTransaction.mKeyCode && mConnection.isInsideDoubleQuoteOrAfterDigit(); final boolean needsPrecedingSpace; - if (SpaceState.PHANTOM != spaceState) { + if (SpaceState.PHANTOM != inputTransaction.mSpaceState) { needsPrecedingSpace = false; - } else if (Constants.CODE_DOUBLE_QUOTE == codePoint) { + } else if (Constants.CODE_DOUBLE_QUOTE == inputTransaction.mKeyCode) { // Double quotes behave like they are usually preceded by space iff we are // not inside a double quote or after a digit. needsPrecedingSpace = !isInsideDoubleQuoteOrAfterDigit; } else { - needsPrecedingSpace = settingsValues.isUsuallyPrecededBySpace(codePoint); + needsPrecedingSpace = inputTransaction.mSettingsValues.isUsuallyPrecededBySpace( + inputTransaction.mKeyCode); } if (needsPrecedingSpace) { - promotePhantomSpace(settingsValues); + promotePhantomSpace(inputTransaction.mSettingsValues); } if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.latinIME_handleSeparator(codePoint, mWordComposer.isComposingWord()); + ResearchLogger.latinIME_handleSeparator(inputTransaction.mKeyCode, + mWordComposer.isComposingWord()); } if (!shouldAvoidSendingCode) { - sendKeyCodePoint(settingsValues, codePoint); + sendKeyCodePoint(inputTransaction.mSettingsValues, inputTransaction.mKeyCode); } - if (Constants.CODE_SPACE == codePoint) { - if (settingsValues.isSuggestionsRequested()) { - if (maybeDoubleSpacePeriod(settingsValues, handler)) { - keyboardSwitcher.updateShiftState(); + if (Constants.CODE_SPACE == inputTransaction.mKeyCode) { + if (inputTransaction.mSettingsValues.isSuggestionsRequested()) { + if (maybeDoubleSpacePeriod(inputTransaction.mSettingsValues, handler)) { + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); mSpaceState = SpaceState.DOUBLE; } else if (!mSuggestedWords.isPunctuationSuggestions()) { mSpaceState = SpaceState.WEAK; @@ -831,11 +833,12 @@ public final class InputLogic { handler.postUpdateSuggestionStrip(); } else { if (swapWeakSpace) { - swapSwapperAndSpace(keyboardSwitcher); + swapSwapperAndSpace(inputTransaction); mSpaceState = SpaceState.SWAP_PUNCTUATION; - } else if ((SpaceState.PHANTOM == spaceState - && settingsValues.isUsuallyFollowedBySpace(codePoint)) - || (Constants.CODE_DOUBLE_QUOTE == codePoint + } else if ((SpaceState.PHANTOM == inputTransaction.mSpaceState + && inputTransaction.mSettingsValues.isUsuallyFollowedBySpace( + inputTransaction.mKeyCode)) + || (Constants.CODE_DOUBLE_QUOTE == inputTransaction.mKeyCode && isInsideDoubleQuoteOrAfterDigit)) { // If we are in phantom space state, and the user presses a separator, we want to // stay in phantom space state so that the next keypress has a chance to add the @@ -856,25 +859,24 @@ public final class InputLogic { mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); } - keyboardSwitcher.updateShiftState(); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); return didAutoCorrect; } /** * Handle a press on the backspace key. - * @param settingsValues The current settings values. - * @param spaceState The space state at start of this batch edit. + * @param inputTransaction The transaction in progress. */ - private void handleBackspace(final SettingsValues settingsValues, final int spaceState, - // TODO: remove these arguments - final LatinIME.UIHandler handler, final KeyboardSwitcher keyboardSwitcher) { + private void handleBackspace(final InputTransaction inputTransaction, + // TODO: remove this argument + final LatinIME.UIHandler handler) { mSpaceState = SpaceState.NONE; mDeleteCount++; // In many cases, we may have to put the keyboard in auto-shift state again. However // we want to wait a few milliseconds before doing it to avoid the keyboard flashing // during key repeat. - handler.postUpdateShiftState(); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_LATER); if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { // If we are in the middle of a recorrection, we need to commit the recorrection @@ -900,14 +902,14 @@ public final class InputLogic { if (!mWordComposer.isComposingWord()) { // If we just removed the last character, auto-caps mode may have changed so we // need to re-evaluate. - keyboardSwitcher.updateShiftState(); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); } } else { if (mLastComposedWord.canRevertCommit()) { - if (settingsValues.mIsInternal) { + if (inputTransaction.mSettingsValues.mIsInternal) { LatinImeLoggerUtils.onAutoCorrectionCancellation(); } - revertCommit(settingsValues, handler); + revertCommit(inputTransaction.mSettingsValues, handler); return; } if (mEnteredText != null && mConnection.sameAsTextBeforeCursor(mEnteredText)) { @@ -924,14 +926,14 @@ public final class InputLogic { // reverting any autocorrect at this point. So we can safely return. return; } - if (SpaceState.DOUBLE == spaceState) { + if (SpaceState.DOUBLE == inputTransaction.mSpaceState) { handler.cancelDoubleSpacePeriodTimer(); if (mConnection.revertDoubleSpacePeriod()) { // No need to reset mSpaceState, it has already be done (that's why we // receive it as a parameter) return; } - } else if (SpaceState.SWAP_PUNCTUATION == spaceState) { + } else if (SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) { if (mConnection.revertSwapPunctuation()) { // Likewise return; @@ -957,8 +959,8 @@ public final class InputLogic { // This should never happen. Log.e(TAG, "Backspace when we don't know the selection position"); } - if (settingsValues.isBeforeJellyBean() || - settingsValues.mInputAttributes.isTypeNull()) { + if (inputTransaction.mSettingsValues.isBeforeJellyBean() || + inputTransaction.mSettingsValues.mInputAttributes.isTypeNull()) { // There are two possible reasons to send a key event: either the field has // type TYPE_NULL, in which case the keyboard should send events, or we are // running in backward compatibility mode. Before Jelly bean, the keyboard @@ -1004,15 +1006,16 @@ public final class InputLogic { } } } - if (settingsValues.isSuggestionStripVisible() - && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces + if (inputTransaction.mSettingsValues.isSuggestionStripVisible() + && inputTransaction.mSettingsValues.mSpacingAndPunctuations + .mCurrentLanguageHasSpaces && !mConnection.isCursorFollowedByWordCharacter( - settingsValues.mSpacingAndPunctuations)) { - restartSuggestionsOnWordTouchedByCursor(settingsValues, + inputTransaction.mSettingsValues.mSpacingAndPunctuations)) { + restartSuggestionsOnWordTouchedByCursor(inputTransaction.mSettingsValues, true /* includeResumedWordInSuggestions */); } // We just removed at least one character. We need to update the auto-caps state. - keyboardSwitcher.updateShiftState(); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); } } @@ -1028,9 +1031,9 @@ public final class InputLogic { * * This method will check that there are two characters before the cursor and that the first * one is a space before it does the actual swapping. + * @param inputTransaction The transaction in progress. */ - // TODO: Remove this argument - private void swapSwapperAndSpace(final KeyboardSwitcher keyboardSwitcher) { + private void swapSwapperAndSpace(final InputTransaction inputTransaction) { final CharSequence lastTwo = mConnection.getTextBeforeCursor(2, 0); // It is guaranteed lastTwo.charAt(1) is a swapper - else this method is not called. if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == Constants.CODE_SPACE) { @@ -1040,28 +1043,34 @@ public final class InputLogic { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { ResearchLogger.latinIME_swapSwapperAndSpace(lastTwo, text); } - keyboardSwitcher.updateShiftState(); + inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW); } } /* * Strip a trailing space if necessary and returns whether it's a swap weak space situation. - * @param settingsValues The current settings values. - * @param codePoint The code point that is about to be inserted. - * @param spaceState The space state at start of this batch edit. + * @param inputTransaction The transaction in progress. * @param isFromSuggestionStrip Whether this code point is coming from the suggestion strip. * @return whether we should swap the space instead of removing it. */ - private boolean maybeStripSpace(final SettingsValues settingsValues, - final int code, final int spaceState, final boolean isFromSuggestionStrip) { - if (Constants.CODE_ENTER == code && SpaceState.SWAP_PUNCTUATION == spaceState) { + private boolean maybeStripSpace(final InputTransaction inputTransaction, + final boolean isFromSuggestionStrip) { + if (Constants.CODE_ENTER == inputTransaction.mKeyCode && + SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) { mConnection.removeTrailingSpace(); return false; } - if ((SpaceState.WEAK == spaceState || SpaceState.SWAP_PUNCTUATION == spaceState) + if ((SpaceState.WEAK == inputTransaction.mSpaceState + || SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) && isFromSuggestionStrip) { - if (settingsValues.isUsuallyPrecededBySpace(code)) return false; - if (settingsValues.isUsuallyFollowedBySpace(code)) return true; + if (inputTransaction.mSettingsValues.isUsuallyPrecededBySpace( + inputTransaction.mKeyCode)) { + return false; + } + if (inputTransaction.mSettingsValues.isUsuallyFollowedBySpace( + inputTransaction.mKeyCode)) { + return true; + } mConnection.removeTrailingSpace(); } return false; @@ -1479,7 +1488,9 @@ public final class InputLogic { */ private int getActualCapsMode(final SettingsValues settingsValues, final int keyboardShiftMode) { - if (keyboardShiftMode != WordComposer.CAPS_MODE_AUTO_SHIFTED) return keyboardShiftMode; + if (keyboardShiftMode != WordComposer.CAPS_MODE_AUTO_SHIFTED) { + return keyboardShiftMode; + } final int auto = getCurrentAutoCapsState(settingsValues); if (0 != (auto & TextUtils.CAP_MODE_CHARACTERS)) { return WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED; @@ -1721,6 +1732,7 @@ public final class InputLogic { * @param settingsValues the current values of the settings. * @param codePoint the code point to send. */ + // TODO: replace these two parameters with an InputTransaction private void sendKeyCodePoint(final SettingsValues settingsValues, final int codePoint) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { ResearchLogger.latinIME_sendKeyCodePoint(codePoint); diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java index 90e7400fb..caf3cf354 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java @@ -222,54 +222,6 @@ public final class BinaryDictIOUtils { return countSize; } - private static final int HEADER_READING_BUFFER_SIZE = 16384; - /** - * Convenience method to read the header of a binary file. - * - * This is quite resource intensive - don't call when performance is critical. - * - * @param file The file to read. - * @param offset The offset in the file where to start reading the data. - * @param length The length of the data file. - * @return the header of the specified dictionary file. - */ - private static DictionaryHeader getDictionaryFileHeader( - final File file, final long offset, final long length) - throws FileNotFoundException, IOException, UnsupportedFormatException { - final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE]; - final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, offset, length, - new DictDecoder.DictionaryBufferFactory() { - @Override - public DictBuffer getDictionaryBuffer(File file) - throws FileNotFoundException, IOException { - final FileInputStream inStream = new FileInputStream(file); - try { - inStream.skip(offset); - inStream.read(buffer); - return new ByteArrayDictBuffer(buffer); - } finally { - inStream.close(); - } - } - }); - if (dictDecoder == null) { - return null; - } - return dictDecoder.readHeader(); - } - - public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file, final long offset, - final long length) { - try { - final DictionaryHeader header = getDictionaryFileHeader(file, offset, length); - return header; - } catch (UnsupportedFormatException e) { - return null; - } catch (IOException e) { - return null; - } - } - /** * Helper method to hide the actual value of the no children address. */ diff --git a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java index 9abecbfec..484bb4b23 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java +++ b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java @@ -341,6 +341,7 @@ public final class FormatSpec { return null; } + @UsedForTesting public static DictDecoder getDictDecoder(final File dictFile, final long offset, final long length, final DictionaryBufferFactory factory) { if (dictFile.isDirectory()) { @@ -351,6 +352,7 @@ public final class FormatSpec { return null; } + @UsedForTesting public static DictDecoder getDictDecoder(final File dictFile, final long offset, final long length) { return getDictDecoder(dictFile, offset, length, DictDecoder.USE_READONLY_BYTEBUFFER); diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java index ae1e443c5..ab24fbc84 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java +++ b/java/src/com/android/inputmethod/latin/makedict/Ver2DictDecoder.java @@ -123,6 +123,7 @@ public class Ver2DictDecoder extends AbstractDictDecoder { private final DictionaryBufferFactory mBufferFactory; protected DictBuffer mDictBuffer; + @UsedForTesting /* package */ Ver2DictDecoder(final File file, final long offset, final long length, final int factoryFlag) { mDictionaryBinaryFile = file; diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java index 1050d1b0e..a50bad90a 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java +++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java @@ -22,6 +22,7 @@ import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; +import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.LocaleUtils; import java.io.File; @@ -54,7 +55,7 @@ public class Ver4DictEncoder implements DictEncoder { if (!mDictPlacedDir.isDirectory()) { throw new UnsupportedFormatException("Given path is not a directory."); } - if (!BinaryDictionary.createEmptyDictFile(mDictPlacedDir.getAbsolutePath(), + if (!BinaryDictionaryUtils.createEmptyDictFile(mDictPlacedDir.getAbsolutePath(), FormatSpec.VERSION4, LocaleUtils.constructLocaleFromString( dict.mOptions.mAttributes.get(DictionaryHeader.DICTIONARY_LOCALE_KEY)), dict.mOptions.mAttributes)) { diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index dae36f7dd..a07e8eb6a 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -37,6 +37,7 @@ import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary; import com.android.inputmethod.latin.UserBinaryDictionary; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; +import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.LocaleUtils; import com.android.inputmethod.latin.utils.StringUtils; @@ -320,7 +321,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService hasRecommendedSuggestions = false; } else { gatheredSuggestions = EMPTY_STRING_ARRAY; - final float normalizedScore = BinaryDictionary.calcNormalizedScore( + final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( mOriginalText, mBestSuggestion, mBestScore); hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold); } @@ -355,7 +356,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService final int bestScore = mScores[mLength - 1]; final String bestSuggestion = mSuggestions.get(0); final float normalizedScore = - BinaryDictionary.calcNormalizedScore( + BinaryDictionaryUtils.calcNormalizedScore( mOriginalText, bestSuggestion.toString(), bestScore); hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold); if (DBG) { diff --git a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java b/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java index 37c173f96..22b9b77d2 100644 --- a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java @@ -40,7 +40,7 @@ public final class AutoCorrectionUtils { final int autoCorrectionSuggestionScore = suggestion.mScore; // TODO: when the normalized score of the first suggestion is nearly equals to // the normalized score of the second suggestion, behave less aggressive. - final float normalizedScore = BinaryDictionary.calcNormalizedScore( + final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( consideredWord, suggestion.mWord, autoCorrectionSuggestionScore); if (DBG) { Log.d(TAG, "Normalized " + consideredWord + "," + suggestion + "," @@ -71,9 +71,8 @@ public final class AutoCorrectionUtils { if (typedWordLength < MINIMUM_SAFETY_NET_CHAR_LENGTH) { return false; } - final int maxEditDistanceOfNativeDictionary = - (typedWordLength < 5 ? 2 : typedWordLength / 2) + 1; - final int distance = BinaryDictionary.editDistance(typedWord, suggestion); + final int maxEditDistanceOfNativeDictionary = (typedWordLength / 2) + 1; + final int distance = BinaryDictionaryUtils.editDistance(typedWord, suggestion); if (DBG) { Log.d(TAG, "Autocorrected edit distance = " + distance + ", " + maxEditDistanceOfNativeDictionary); diff --git a/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java b/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java new file mode 100644 index 000000000..638830046 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/BinaryDictionaryUtils.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 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.utils; + +import com.android.inputmethod.annotations.UsedForTesting; +import com.android.inputmethod.latin.BinaryDictionary; +import com.android.inputmethod.latin.makedict.DictionaryHeader; +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; +import com.android.inputmethod.latin.personalization.PersonalizationHelper; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; +import java.util.Map; + +public final class BinaryDictionaryUtils { + private static final String TAG = BinaryDictionaryUtils.class.getSimpleName(); + + private BinaryDictionaryUtils() { + // This utility class is not publicly instantiable. + } + + static { + JniUtils.loadNativeLibrary(); + } + + private static native boolean createEmptyDictFileNative(String filePath, long dictVersion, + String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray); + private static native float calcNormalizedScoreNative(int[] before, int[] after, int score); + private static native int editDistanceNative(int[] before, int[] after); + private static native int setCurrentTimeForTestNative(int currentTime); + + public static DictionaryHeader getHeader(final File dictFile) + throws IOException, UnsupportedFormatException { + return getHeaderWithOffsetAndLength(dictFile, 0 /* offset */, dictFile.length()); + } + + public static DictionaryHeader getHeaderWithOffsetAndLength(final File dictFile, + final long offset, final long length) throws IOException, UnsupportedFormatException { + // dictType is never used for reading the header. Passing an empty string. + final BinaryDictionary binaryDictionary = new BinaryDictionary( + dictFile.getAbsolutePath(), offset, length, + true /* useFullEditDistance */, null /* locale */, "" /* dictType */, + false /* isUpdatable */); + final DictionaryHeader header = binaryDictionary.getHeader(); + binaryDictionary.close(); + if (header == null) { + throw new IOException(); + } + return header; + } + + public static boolean createEmptyDictFile(final String filePath, final long dictVersion, + final Locale locale, final Map<String, String> attributeMap) { + final String[] keyArray = new String[attributeMap.size()]; + final String[] valueArray = new String[attributeMap.size()]; + int index = 0; + for (final String key : attributeMap.keySet()) { + keyArray[index] = key; + valueArray[index] = attributeMap.get(key); + index++; + } + return createEmptyDictFileNative(filePath, dictVersion, locale.toString(), keyArray, + valueArray); + } + + public static float calcNormalizedScore(final String before, final String after, + final int score) { + return calcNormalizedScoreNative(StringUtils.toCodePointArray(before), + StringUtils.toCodePointArray(after), score); + } + + public static int editDistance(final String before, final String after) { + if (before == null || after == null) { + throw new IllegalArgumentException(); + } + return editDistanceNative(StringUtils.toCodePointArray(before), + StringUtils.toCodePointArray(after)); + } + + /** + * Control the current time to be used in the native code. If currentTime >= 0, this method sets + * the current time and gets into test mode. + * In test mode, set timestamp is used as the current time in the native code. + * If currentTime < 0, quit the test mode and returns to using time() to get the current time. + * + * @param currentTime seconds since the unix epoch + * @return current time got in the native code. + */ + @UsedForTesting + public static int setCurrentTimeForTest(final int currentTime) { + final int currentNativeTimestamp = setCurrentTimeForTestNative(currentTime); + PersonalizationHelper.currentTimeChangedForTesting(currentNativeTimestamp); + return currentNativeTimestamp; + } +} diff --git a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java index a15556511..e531d4b09 100644 --- a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java @@ -30,9 +30,11 @@ import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; import com.android.inputmethod.latin.makedict.DictionaryHeader; +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.settings.SpacingAndPunctuations; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Locale; @@ -283,7 +285,20 @@ public class DictionaryInfoUtils { } public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file) { - return BinaryDictIOUtils.getDictionaryFileHeaderOrNull(file, 0, file.length()); + return getDictionaryFileHeaderOrNull(file, 0, file.length()); + } + + private static DictionaryHeader getDictionaryFileHeaderOrNull(final File file, + final long offset, final long length) { + try { + final DictionaryHeader header = + BinaryDictionaryUtils.getHeaderWithOffsetAndLength(file, offset, length); + return header; + } catch (UnsupportedFormatException e) { + return null; + } catch (IOException e) { + return null; + } } /** @@ -294,7 +309,7 @@ public class DictionaryInfoUtils { */ private static DictionaryInfo createDictionaryInfoFromFileAddress( final AssetFileAddress fileAddress) { - final DictionaryHeader header = BinaryDictIOUtils.getDictionaryFileHeaderOrNull( + final DictionaryHeader header = getDictionaryFileHeaderOrNull( new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength); if (header == null) { return null; |