diff options
Diffstat (limited to 'java/src')
8 files changed, 231 insertions, 162 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java index c51941d32..2925a4b76 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java @@ -56,10 +56,9 @@ public final class KeySpecParser { return keySpec.startsWith(KeyboardIconsSet.PREFIX_ICON); } - private static boolean hasCode(final String keySpec) { - final int end = indexOfLabelEnd(keySpec, 0); - if (end > 0 && end + 1 < keySpec.length() && keySpec.startsWith( - KeyboardCodesSet.PREFIX_CODE, end + 1)) { + private static boolean hasCode(final String keySpec, final int labelEnd) { + if (labelEnd > 0 && labelEnd + 1 < keySpec.length() + && keySpec.startsWith(KeyboardCodesSet.PREFIX_CODE, labelEnd + 1)) { return true; } return false; @@ -84,16 +83,20 @@ public final class KeySpecParser { return sb.toString(); } - private static int indexOfLabelEnd(final String keySpec, final int start) { - if (keySpec.indexOf(BACKSLASH, start) < 0) { - final int end = keySpec.indexOf(VERTICAL_BAR, start); - if (end == 0) { - throw new KeySpecParserError(VERTICAL_BAR + " at " + start + ": " + keySpec); + private static int indexOfLabelEnd(final String keySpec) { + final int length = keySpec.length(); + if (keySpec.indexOf(BACKSLASH) < 0) { + final int labelEnd = keySpec.indexOf(VERTICAL_BAR); + if (labelEnd == 0) { + if (length == 1) { + // Treat a sole vertical bar as a special case of key label. + return -1; + } + throw new KeySpecParserError("Empty label"); } - return end; + return labelEnd; } - final int length = keySpec.length(); - for (int pos = start; pos < length; pos++) { + for (int pos = 0; pos < length; pos++) { final char c = keySpec.charAt(pos); if (c == BACKSLASH && pos + 1 < length) { // Skip escape char @@ -105,35 +108,55 @@ public final class KeySpecParser { return -1; } + private static String getBeforeLabelEnd(final String keySpec, final int labelEnd) { + return (labelEnd < 0) ? keySpec : keySpec.substring(0, labelEnd); + } + + private static String getAfterLabelEnd(final String keySpec, final int labelEnd) { + return keySpec.substring(labelEnd + /* VERTICAL_BAR */1); + } + + private static void checkDoubleLabelEnd(final String keySpec, final int labelEnd) { + if (indexOfLabelEnd(getAfterLabelEnd(keySpec, labelEnd)) < 0) { + return; + } + throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec); + } + public static String getLabel(final String keySpec) { + if (keySpec == null) { + // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory. + return null; + } if (hasIcon(keySpec)) { return null; } - final int end = indexOfLabelEnd(keySpec, 0); - final String label = (end > 0) ? parseEscape(keySpec.substring(0, end)) - : parseEscape(keySpec); + final int labelEnd = indexOfLabelEnd(keySpec); + final String label = parseEscape(getBeforeLabelEnd(keySpec, labelEnd)); if (label.isEmpty()) { throw new KeySpecParserError("Empty label: " + keySpec); } return label; } - private static String getOutputTextInternal(final String keySpec) { - final int end = indexOfLabelEnd(keySpec, 0); - if (end <= 0) { + private static String getOutputTextInternal(final String keySpec, final int labelEnd) { + if (labelEnd <= 0) { return null; } - if (indexOfLabelEnd(keySpec, end + 1) >= 0) { - throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec); - } - return parseEscape(keySpec.substring(end + /* VERTICAL_BAR */1)); + checkDoubleLabelEnd(keySpec, labelEnd); + return parseEscape(getAfterLabelEnd(keySpec, labelEnd)); } public static String getOutputText(final String keySpec) { - if (hasCode(keySpec)) { + if (keySpec == null) { + // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory. return null; } - final String outputText = getOutputTextInternal(keySpec); + final int labelEnd = indexOfLabelEnd(keySpec); + if (hasCode(keySpec, labelEnd)) { + return null; + } + final String outputText = getOutputTextInternal(keySpec, labelEnd); if (outputText != null) { if (StringUtils.codePointCount(outputText) == 1) { // If output text is one code point, it should be treated as a code. @@ -154,14 +177,16 @@ public final class KeySpecParser { } public static int getCode(final String keySpec, final KeyboardCodesSet codesSet) { - if (hasCode(keySpec)) { - final int end = indexOfLabelEnd(keySpec, 0); - if (indexOfLabelEnd(keySpec, end + 1) >= 0) { - throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec); - } - return parseCode(keySpec.substring(end + 1), codesSet, CODE_UNSPECIFIED); + if (keySpec == null) { + // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory. + return CODE_UNSPECIFIED; } - final String outputText = getOutputTextInternal(keySpec); + final int labelEnd = indexOfLabelEnd(keySpec); + if (hasCode(keySpec, labelEnd)) { + checkDoubleLabelEnd(keySpec, labelEnd); + return parseCode(getAfterLabelEnd(keySpec, labelEnd), codesSet, CODE_UNSPECIFIED); + } + final String outputText = getOutputTextInternal(keySpec, labelEnd); if (outputText != null) { // If output text is one code point, it should be treated as a code. // See {@link #getOutputText(String)}. @@ -171,35 +196,40 @@ public final class KeySpecParser { return CODE_OUTPUT_TEXT; } final String label = getLabel(keySpec); - // Code is automatically generated for one letter label. - if (StringUtils.codePointCount(label) == 1) { - return label.codePointAt(0); + if (label == null) { + throw new KeySpecParserError("Empty label: " + keySpec); } - return CODE_OUTPUT_TEXT; + // Code is automatically generated for one letter label. + return (StringUtils.codePointCount(label) == 1) ? label.codePointAt(0) : CODE_OUTPUT_TEXT; } + // TODO: Make this method private once Key.code attribute is removed. public static int parseCode(final String text, final KeyboardCodesSet codesSet, final int defCode) { - if (text == null) return defCode; + if (text == null) { + return defCode; + } if (text.startsWith(KeyboardCodesSet.PREFIX_CODE)) { return codesSet.getCode(text.substring(KeyboardCodesSet.PREFIX_CODE.length())); - } else if (text.startsWith(PREFIX_HEX)) { + } + if (text.startsWith(PREFIX_HEX)) { return Integer.parseInt(text.substring(PREFIX_HEX.length()), 16); - } else { - return Integer.parseInt(text); } + return Integer.parseInt(text); } public static int getIconId(final String keySpec) { - if (keySpec != null && hasIcon(keySpec)) { - final int end = keySpec.indexOf( - VERTICAL_BAR, KeyboardIconsSet.PREFIX_ICON.length()); - final String name = (end < 0) - ? keySpec.substring(KeyboardIconsSet.PREFIX_ICON.length()) - : keySpec.substring(KeyboardIconsSet.PREFIX_ICON.length(), end); - return KeyboardIconsSet.getIconId(name); - } - return KeyboardIconsSet.ICON_UNDEFINED; + if (keySpec == null) { + // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory. + return KeyboardIconsSet.ICON_UNDEFINED; + } + if (!hasIcon(keySpec)) { + return KeyboardIconsSet.ICON_UNDEFINED; + } + final int labelEnd = indexOfLabelEnd(keySpec); + final String iconName = getBeforeLabelEnd(keySpec, labelEnd) + .substring(KeyboardIconsSet.PREFIX_ICON.length()); + return KeyboardIconsSet.getIconId(iconName); } @SuppressWarnings("serial") diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java index d3bc0c2b2..0551e9e98 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java +++ b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java @@ -46,6 +46,9 @@ public final class MoreKeySpec { public MoreKeySpec(final String moreKeySpec, boolean needsToUpperCase, final Locale locale, final KeyboardCodesSet codesSet) { + if (TextUtils.isEmpty(moreKeySpec)) { + throw new KeySpecParser.KeySpecParserError("Empty more key spec"); + } mLabel = StringUtils.toUpperCaseOfStringForLocale( KeySpecParser.getLabel(moreKeySpec), needsToUpperCase, locale); final int code = StringUtils.toUpperCaseOfCodeForLocale( diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 80a27e23f..b20bcd1f9 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -139,7 +139,7 @@ public final class BinaryDictionary extends Dictionary { } private static native boolean createEmptyDictFileNative(String filePath, long dictVersion, - String[] attributeKeyStringArray, String[] attributeValueStringArray); + 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, @@ -179,7 +179,7 @@ public final class BinaryDictionary extends Dictionary { private static native String getPropertyNative(long dict, String query); public static boolean createEmptyDictFile(final String filePath, final long dictVersion, - final Map<String, String> attributeMap) { + 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; @@ -188,7 +188,8 @@ public final class BinaryDictionary extends Dictionary { valueArray[index] = attributeMap.get(key); index++; } - return createEmptyDictFileNative(filePath, dictVersion, keyArray, valueArray); + return createEmptyDictFileNative(filePath, dictVersion, locale.toString(), keyArray, + valueArray); } // TODO: Move native dict into session diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index f0dc7720d..565d6a1f9 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -289,10 +289,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { Log.e(TAG, "Can't remove a file: " + file.getName()); } BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(), - DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap()); + DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap()); mBinaryDictionary = new BinaryDictionary( file.getAbsolutePath(), 0 /* offset */, file.length(), - true /* useFullEditDistance */, null, mDictType, mIsUpdatable); + true /* useFullEditDistance */, mLocale, mDictType, mIsUpdatable); } else { mDictionaryWriter.clear(); } @@ -594,7 +594,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { Log.e(TAG, "Can't remove a file: " + file.getName()); } BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(), - DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap()); + DICTIONARY_FORMAT_VERSION, mLocale, getHeaderAttributeMap()); } else { if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) { mBinaryDictionary.flushWithGC(); diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 8d0f4128e..e55c08dae 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -67,7 +67,6 @@ import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.inputlogic.InputLogic; -import com.android.inputmethod.latin.inputlogic.SpaceState; import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegistrar; import com.android.inputmethod.latin.personalization.PersonalizationHelper; @@ -83,7 +82,6 @@ import com.android.inputmethod.latin.utils.CoordinateUtils; import com.android.inputmethod.latin.utils.ImportantNoticeUtils; import com.android.inputmethod.latin.utils.IntentUtils; import com.android.inputmethod.latin.utils.JniUtils; -import com.android.inputmethod.latin.utils.LatinImeLoggerUtils; import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.research.ResearchLogger; @@ -117,13 +115,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private static final String SCHEME_PACKAGE = "package"; private final Settings mSettings; - private final InputLogic mInputLogic = new InputLogic(this); + private final InputLogic mInputLogic = new InputLogic(this /* LatinIME */, + this /* SuggestionStripViewAccessor */); private View mExtractArea; private View mKeyPreviewBackingView; private SuggestionStripView mSuggestionStripView; - private CompletionInfo[] mApplicationSpecifiedCompletions; + // TODO[IL]: remove this member completely. + public CompletionInfo[] mApplicationSpecifiedCompletions; private RichInputMethodManager mRichImm; @UsedForTesting final KeyboardSwitcher mKeyboardSwitcher; @@ -1306,12 +1306,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Nothing to do so far. } - // TODO[IL]: Move this to InputLogic and make it private - @Override + // TODO: remove this, read this directly from mInputLogic or something in the tests + @UsedForTesting public boolean isShowingPunctuationList() { - if (mInputLogic.mSuggestedWords == null) return false; - return mSettings.getCurrent().mSpacingAndPunctuations.mSuggestPuncList - == mInputLogic.mSuggestedWords; + return mInputLogic.isShowingPunctuationList(mSettings.getCurrent()); } // TODO[IL]: Define a clear interface for this @@ -1456,94 +1454,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // interface @Override public void pickSuggestionManually(final int index, final SuggestedWordInfo suggestionInfo) { - final SuggestedWords suggestedWords = mInputLogic.mSuggestedWords; - final String suggestion = suggestionInfo.mWord; - // If this is a punctuation picked from the suggestion strip, pass it to onCodeInput - if (suggestion.length() == 1 && isShowingPunctuationList()) { - // Word separators are suggested before the user inputs something. - // So, LatinImeLogger logs "" as a user's input. - LatinImeLogger.logOnManualSuggestion("", suggestion, index, suggestedWords); - // Rely on onCodeInput to do the complicated swapping/stripping logic consistently. - final int primaryCode = suggestion.charAt(0); - onCodeInput(primaryCode, - Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE); - if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.latinIME_punctuationSuggestion(index, suggestion, - false /* isBatchMode */, suggestedWords.mIsPrediction); - } - return; - } - - mInputLogic.mConnection.beginBatchEdit(); - final SettingsValues currentSettings = mSettings.getCurrent(); - if (SpaceState.PHANTOM == mInputLogic.mSpaceState && suggestion.length() > 0 - // In the batch input mode, a manually picked suggested word should just replace - // the current batch input text and there is no need for a phantom space. - && !mInputLogic.mWordComposer.isBatchMode()) { - final int firstChar = Character.codePointAt(suggestion, 0); - if (!currentSettings.isWordSeparator(firstChar) - || currentSettings.isUsuallyPrecededBySpace(firstChar)) { - mInputLogic.promotePhantomSpace(currentSettings); - } - } - - if (currentSettings.isApplicationSpecifiedCompletionsOn() - && mApplicationSpecifiedCompletions != null - && index >= 0 && index < mApplicationSpecifiedCompletions.length) { - mInputLogic.mSuggestedWords = SuggestedWords.EMPTY; - if (mSuggestionStripView != null) { - mSuggestionStripView.clear(); - } - mKeyboardSwitcher.updateShiftState(); - mInputLogic.resetComposingState(true /* alsoResetLastComposedWord */); - final CompletionInfo completionInfo = mApplicationSpecifiedCompletions[index]; - mInputLogic.mConnection.commitCompletion(completionInfo); - mInputLogic.mConnection.endBatchEdit(); - return; - } - - // We need to log before we commit, because the word composer will store away the user - // typed word. - final String replacedWord = mInputLogic.mWordComposer.getTypedWord(); - LatinImeLogger.logOnManualSuggestion(replacedWord, suggestion, index, suggestedWords); - mInputLogic.commitChosenWord(currentSettings, suggestion, - LastComposedWord.COMMIT_TYPE_MANUAL_PICK, LastComposedWord.NOT_A_SEPARATOR); - if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.latinIME_pickSuggestionManually(replacedWord, index, suggestion, - mInputLogic.mWordComposer.isBatchMode(), suggestionInfo.mScore, - suggestionInfo.mKind, suggestionInfo.mSourceDict.mDictType); - } - mInputLogic.mConnection.endBatchEdit(); - // Don't allow cancellation of manual pick - mInputLogic.mLastComposedWord.deactivate(); - // Space state must be updated before calling updateShiftState - mInputLogic.mSpaceState = SpaceState.PHANTOM; - mKeyboardSwitcher.updateShiftState(); + mInputLogic.onPickSuggestionManually(mSettings.getCurrent(), index, suggestionInfo, + mHandler, mKeyboardSwitcher); + } - // We should show the "Touch again to save" hint if the user pressed the first entry - // AND it's in none of our current dictionaries (main, user or otherwise). - // Please note that if mSuggest is null, it means that everything is off: suggestion - // and correction, so we shouldn't try to show the hint - final Suggest suggest = mInputLogic.mSuggest; - final boolean showingAddToDictionaryHint = - (SuggestedWordInfo.KIND_TYPED == suggestionInfo.mKind - || SuggestedWordInfo.KIND_OOV_CORRECTION == suggestionInfo.mKind) - && suggest != null - // If the suggestion is not in the dictionary, the hint should be shown. - && !suggest.mDictionaryFacilitator.isValidWord(suggestion, - true /* ignoreCase */); - - if (currentSettings.mIsInternal) { - LatinImeLoggerUtils.onSeparator((char)Constants.CODE_SPACE, - Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); - } - if (showingAddToDictionaryHint - && suggest.mDictionaryFacilitator.isUserDictionaryEnabled()) { - mSuggestionStripView.showAddToDictionaryHint(suggestion); - } else { - // If we're not showing the "Touch again to save", then update the suggestion strip. - mHandler.postUpdateSuggestionStrip(); - } + @Override + public void showAddToDictionaryHint(final String word) { + if (null == mSuggestionStripView) return; + mSuggestionStripView.showAddToDictionaryHint(word); } // TODO[IL]: Define a clean interface for this diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index a994a43af..3ecf5f0fb 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -23,6 +23,7 @@ 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; @@ -44,6 +45,7 @@ import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.settings.SpacingAndPunctuations; +import com.android.inputmethod.latin.suggestions.SuggestionStripViewAccessor; import com.android.inputmethod.latin.utils.AsyncResultHolder; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.InputTypeUtils; @@ -65,6 +67,7 @@ public final class InputLogic { // TODO : Remove this member when we can. private final LatinIME mLatinIME; + private final SuggestionStripViewAccessor mSuggestionStripViewAccessor; // Never null. private InputLogicHandler mInputLogicHandler = InputLogicHandler.NULL_HANDLER; @@ -94,8 +97,10 @@ public final class InputLogic { // Find a way to remove it for readability. public boolean mIsAutoCorrectionIndicatorOn; - public InputLogic(final LatinIME latinIME) { + public InputLogic(final LatinIME latinIME, + final SuggestionStripViewAccessor suggestionStripViewAccessor) { mLatinIME = latinIME; + mSuggestionStripViewAccessor = suggestionStripViewAccessor; mWordComposer = new WordComposer(); mEventInterpreter = new EventInterpreter(latinIME); mConnection = new RichInputConnection(latinIME); @@ -179,6 +184,108 @@ public final class InputLogic { } /** + * A suggestion was picked from the suggestion strip. + * @param settingsValues the current values of the settings. + * @param index the index of the suggestion. + * @param suggestionInfo the suggestion info. + */ + // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener} + // interface + public void onPickSuggestionManually(final SettingsValues settingsValues, + final int index, final SuggestedWordInfo suggestionInfo, + // TODO: remove these two arguments + final LatinIME.UIHandler handler, final KeyboardSwitcher keyboardSwitcher) { + final SuggestedWords suggestedWords = mSuggestedWords; + final String suggestion = suggestionInfo.mWord; + // If this is a punctuation picked from the suggestion strip, pass it to onCodeInput + if (suggestion.length() == 1 && isShowingPunctuationList(settingsValues)) { + // Word separators are suggested before the user inputs something. + // So, LatinImeLogger logs "" as a user's input. + LatinImeLogger.logOnManualSuggestion("", suggestion, index, suggestedWords); + // Rely on onCodeInput to do the complicated swapping/stripping logic consistently. + final int primaryCode = suggestion.charAt(0); + onCodeInput(primaryCode, + Constants.SUGGESTION_STRIP_COORDINATE, Constants.SUGGESTION_STRIP_COORDINATE, + settingsValues, handler, keyboardSwitcher); + if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { + ResearchLogger.latinIME_punctuationSuggestion(index, suggestion, + false /* isBatchMode */, suggestedWords.mIsPrediction); + } + return; + } + + mConnection.beginBatchEdit(); + if (SpaceState.PHANTOM == mSpaceState && suggestion.length() > 0 + // In the batch input mode, a manually picked suggested word should just replace + // the current batch input text and there is no need for a phantom space. + && !mWordComposer.isBatchMode()) { + final int firstChar = Character.codePointAt(suggestion, 0); + if (!settingsValues.isWordSeparator(firstChar) + || settingsValues.isUsuallyPrecededBySpace(firstChar)) { + promotePhantomSpace(settingsValues); + } + } + + // TODO: stop relying on mApplicationSpecifiedCompletions. The SuggestionInfo object + // should contain a reference to the CompletionInfo instead. + if (settingsValues.isApplicationSpecifiedCompletionsOn() + && mLatinIME.mApplicationSpecifiedCompletions != null + && index >= 0 && index < mLatinIME.mApplicationSpecifiedCompletions.length) { + mSuggestedWords = SuggestedWords.EMPTY; + mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); + keyboardSwitcher.updateShiftState(); + resetComposingState(true /* alsoResetLastComposedWord */); + final CompletionInfo completionInfo = mLatinIME.mApplicationSpecifiedCompletions[index]; + mConnection.commitCompletion(completionInfo); + mConnection.endBatchEdit(); + return; + } + + // We need to log before we commit, because the word composer will store away the user + // typed word. + final String replacedWord = mWordComposer.getTypedWord(); + LatinImeLogger.logOnManualSuggestion(replacedWord, suggestion, index, suggestedWords); + commitChosenWord(settingsValues, suggestion, + LastComposedWord.COMMIT_TYPE_MANUAL_PICK, LastComposedWord.NOT_A_SEPARATOR); + if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { + ResearchLogger.latinIME_pickSuggestionManually(replacedWord, index, suggestion, + mWordComposer.isBatchMode(), suggestionInfo.mScore, + suggestionInfo.mKind, suggestionInfo.mSourceDict.mDictType); + } + mConnection.endBatchEdit(); + // Don't allow cancellation of manual pick + mLastComposedWord.deactivate(); + // Space state must be updated before calling updateShiftState + mSpaceState = SpaceState.PHANTOM; + keyboardSwitcher.updateShiftState(); + + // We should show the "Touch again to save" hint if the user pressed the first entry + // AND it's in none of our current dictionaries (main, user or otherwise). + // Please note that if mSuggest is null, it means that everything is off: suggestion + // and correction, so we shouldn't try to show the hint + final Suggest suggest = mSuggest; + final boolean showingAddToDictionaryHint = + (SuggestedWordInfo.KIND_TYPED == suggestionInfo.mKind + || SuggestedWordInfo.KIND_OOV_CORRECTION == suggestionInfo.mKind) + && suggest != null + // If the suggestion is not in the dictionary, the hint should be shown. + && !suggest.mDictionaryFacilitator.isValidWord(suggestion, + true /* ignoreCase */); + + if (settingsValues.mIsInternal) { + LatinImeLoggerUtils.onSeparator((char)Constants.CODE_SPACE, + Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); + } + if (showingAddToDictionaryHint + && suggest.mDictionaryFacilitator.isUserDictionaryEnabled()) { + mSuggestionStripViewAccessor.showAddToDictionaryHint(suggestion); + } else { + // If we're not showing the "Touch again to save", then update the suggestion strip. + handler.postUpdateSuggestionStrip(); + } + } + + /** * Consider an update to the cursor position. Evaluate whether this update has happened as * part of normal typing or whether it was an explicit cursor move by the user. In any case, * do the necessary adjustments. @@ -638,7 +745,7 @@ public final class InputLogic { mSpaceState = SpaceState.WEAK; } // In case the "add to dictionary" hint was still displayed. - mLatinIME.dismissAddToDictionaryHint(); + mSuggestionStripViewAccessor.dismissAddToDictionaryHint(); } handler.postUpdateSuggestionStrip(); if (settingsValues.mIsInternal) { @@ -714,7 +821,7 @@ public final class InputLogic { if (maybeDoubleSpacePeriod(settingsValues, handler)) { keyboardSwitcher.updateShiftState(); mSpaceState = SpaceState.DOUBLE; - } else if (!mLatinIME.isShowingPunctuationList()) { + } else if (!isShowingPunctuationList(settingsValues)) { mSpaceState = SpaceState.WEAK; } } @@ -745,7 +852,7 @@ public final class InputLogic { // Set punctuation right away. onUpdateSelection will fire but tests whether it is // already displayed or not, so it's okay. - mLatinIME.setNeutralSuggestionStrip(); + mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); } keyboardSwitcher.updateShiftState(); @@ -1098,7 +1205,7 @@ public final class InputLogic { } if (!mWordComposer.isComposingWord() && !settingsValues.mBigramPredictionEnabled) { - mLatinIME.setNeutralSuggestionStrip(); + mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); return; } @@ -1120,7 +1227,7 @@ public final class InputLogic { final SuggestedWords suggestedWords = holder.get(null, Constants.GET_SUGGESTED_WORDS_TIMEOUT); if (suggestedWords != null) { - mLatinIME.showSuggestionStrip(suggestedWords); + mSuggestionStripViewAccessor.showSuggestionStrip(suggestedWords); } } @@ -1326,6 +1433,15 @@ public final class InputLogic { } /** + * Find out if the punctuation list is shown in the suggestion strip. + * @return whether the current suggestions are the punctuation list. + */ + // TODO: make this private. It's used through LatinIME for tests. + public boolean isShowingPunctuationList(final SettingsValues settingsValues) { + return settingsValues.mSpacingAndPunctuations.mSuggestPuncList == mSuggestedWords; + } + + /** * Factor in auto-caps and manual caps and compute the current caps mode. * @param settingsValues the current settings values. * @param keyboardShiftMode the current shift mode of the keyboard. See @@ -1482,7 +1598,7 @@ public final class InputLogic { final int newSelStart, final int newSelEnd) { final boolean shouldFinishComposition = mWordComposer.isComposingWord(); resetComposingState(true /* alsoResetLastComposedWord */); - mLatinIME.setNeutralSuggestionStrip(); + mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); mConnection.resetCachesUponCursorMoveAndReturnSuccess(newSelStart, newSelEnd, shouldFinishComposition); } @@ -1723,9 +1839,8 @@ public final class InputLogic { // the segment of text starting at the supplied index and running for the length // of the auto-correction flash. At this moment, the "typedWord" argument is // ignored by TextView. - mConnection.commitCorrection( - new CorrectionInfo( - mConnection.getExpectedSelectionEnd() - typedWord.length(), + mConnection.commitCorrection(new CorrectionInfo( + mConnection.getExpectedSelectionEnd() - autoCorrection.length(), typedWord, autoCorrection)); } } diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java index 160775d63..4b0efbad0 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java +++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java @@ -55,7 +55,9 @@ public class Ver4DictEncoder implements DictEncoder { throw new UnsupportedFormatException("Given path is not a directory."); } if (!BinaryDictionary.createEmptyDictFile(mDictPlacedDir.getAbsolutePath(), - FormatSpec.VERSION4, dict.mOptions.mAttributes)) { + FormatSpec.VERSION4, LocaleUtils.constructLocaleFromString( + dict.mOptions.mAttributes.get(DictionaryHeader.DICTIONARY_LOCALE_KEY)), + dict.mOptions.mAttributes)) { throw new IOException("Cannot create dictionary file : " + mDictPlacedDir.getAbsolutePath()); } diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java index 91bf73d30..60f1c7a4e 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripViewAccessor.java @@ -23,9 +23,9 @@ import com.android.inputmethod.latin.SuggestedWords; */ public interface SuggestionStripViewAccessor { public boolean hasSuggestionStripView(); + public void showAddToDictionaryHint(final String word); public boolean isShowingAddToDictionaryHint(); public void dismissAddToDictionaryHint(); - public boolean isShowingPunctuationList(); public void setNeutralSuggestionStrip(); public void showSuggestionStrip(final SuggestedWords suggestedWords); } |