diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 5a5674f8f..49b35137f 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1291,7 +1291,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @UsedForTesting public boolean isShowingPunctuationList() { if (mInputLogic.mSuggestedWords == null) return false; - return mSettings.getCurrent().mSuggestPuncList == mInputLogic.mSuggestedWords; + return mSettings.getCurrent().mSpacingAndPunctuations.mSuggestPuncList + == mInputLogic.mSuggestedWords; } // TODO[IL]: Define a clear interface for this @@ -1407,7 +1408,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private SuggestedWords getOlderSuggestions(final String typedWord) { SuggestedWords previousSuggestedWords = mInputLogic.mSuggestedWords; - if (previousSuggestedWords == mSettings.getCurrent().mSuggestPuncList) { + if (previousSuggestedWords + == mSettings.getCurrent().mSpacingAndPunctuations.mSuggestPuncList) { previousSuggestedWords = SuggestedWords.EMPTY; } if (typedWord == null) { @@ -1560,7 +1562,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (currentSettings.mBigramPredictionEnabled) { clearSuggestionStrip(); } else { - setSuggestedWords(currentSettings.mSuggestPuncList); + setSuggestedWords(currentSettings.mSpacingAndPunctuations.mSuggestPuncList); } setAutoCorrectionIndicator(false); setSuggestionStripShown(isSuggestionsStripVisible()); diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index b365003a5..cc42d8785 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -529,7 +529,7 @@ public final class InputLogic { // In languages with spaces, we only start composing a word when we are not already // touching a word. In languages without spaces, the above conditions are sufficient. (!mConnection.isCursorTouchingWord(settingsValues) - || !settingsValues.mCurrentLanguageHasSpaces)) { + || !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) { // Reset entirely the composing state anyway, then start composing a new word unless // 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 @@ -594,7 +594,7 @@ public final class InputLogic { boolean didAutoCorrect = false; // We avoid sending spaces in languages without spaces if we were composing. final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint - && !settingsValues.mCurrentLanguageHasSpaces + && !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces && mWordComposer.isComposingWord(); if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) { // If we are in the middle of a recorrection, we need to commit the recorrection @@ -813,7 +813,7 @@ public final class InputLogic { } } if (settingsValues.isSuggestionStripVisible() - && settingsValues.mCurrentLanguageHasSpaces) { + && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { restartSuggestionsOnWordTouchedByCursor(settingsValues, deleteCountAtStart - mDeleteCount /* offset */, true /* includeResumedWordInSuggestions */, keyboardSwitcher); @@ -911,8 +911,8 @@ public final class InputLogic { if (canBeFollowedByDoubleSpacePeriod(firstCodePoint)) { handler.cancelDoubleSpacePeriodTimer(); mConnection.deleteSurroundingText(2, 0); - final String textToInsert = new String( - new int[] { settingsValues.mSentenceSeparator, Constants.CODE_SPACE }, 0, 2); + final String textToInsert = + settingsValues.mSpacingAndPunctuations.mSentenceSeparatorAndSpace; mConnection.commitText(textToInsert, 1); if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { ResearchLogger.latinIME_maybeDoubleSpacePeriod(textToInsert, @@ -966,7 +966,7 @@ public final class InputLogic { if (TextUtils.isEmpty(selectedText)) return; // Race condition with the input connection mRecapitalizeStatus.initialize(mLastSelectionStart, mLastSelectionEnd, selectedText.toString(), - settingsValues.mLocale, settingsValues.mWordSeparators); + settingsValues.mLocale, settingsValues.mSpacingAndPunctuations.mWordSeparators); // We trim leading and trailing whitespace. mRecapitalizeStatus.trim(); // Trimming the object may have changed the length of the string, and we need to @@ -1065,7 +1065,7 @@ public final class InputLogic { if (!mLatinIME.isSuggestionsStripVisible()) return; // Recorrection is not supported in languages without spaces because we don't know // how to segment them yet. - if (!settingsValues.mCurrentLanguageHasSpaces) return; + if (!settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) return; // If the cursor is not touching a word, or if there is a selection, return right away. if (mLastSelectionStart != mLastSelectionEnd) return; // If we don't know the cursor location, return. @@ -1073,7 +1073,8 @@ public final class InputLogic { final int expectedCursorPosition = mLastSelectionStart + offset; // We know Start == End if (!mConnection.isCursorTouchingWord(settingsValues)) return; final TextRange range = mConnection.getWordRangeAtCursor( - settingsValues.mWordSeparators, 0 /* additionalPrecedingWordsCount */); + settingsValues.mSpacingAndPunctuations.mWordSeparators, + 0 /* additionalPrecedingWordsCount */); if (null == range) return; // Happens if we don't have an input connection at all if (range.length() <= 0) return; // Race condition. No text to resume on, so bail out. // If for some strange reason (editor bug or so) we measure the text before the cursor as @@ -1195,7 +1196,7 @@ public final class InputLogic { } } final String stringToCommit = originallyTypedWord + mLastComposedWord.mSeparatorString; - if (settingsValues.mCurrentLanguageHasSpaces) { + if (settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { // For languages with spaces, we revert to the typed string, but the cursor is still // after the separator so we don't resume suggestions. If the user wants to correct // the word, they have to press backspace again. @@ -1295,7 +1296,7 @@ public final class InputLogic { // TODO: Make this private public String getNthPreviousWordForSuggestion(final SettingsValues currentSettings, final int nthPreviousWord) { - if (currentSettings.mCurrentLanguageHasSpaces) { + if (currentSettings.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { // If we are typing in a language with spaces we can just look up the previous // word from textview. return mConnection.getNthPreviousWord(currentSettings, nthPreviousWord); @@ -1386,7 +1387,7 @@ public final class InputLogic { if (settingsValues.mBigramPredictionEnabled) { mLatinIME.clearSuggestionStrip(); } else { - mLatinIME.setSuggestedWords(settingsValues.mSuggestPuncList); + mLatinIME.setSuggestedWords(settingsValues.mSpacingAndPunctuations.mSuggestPuncList); } mConnection.resetCachesUponCursorMoveAndReturnSuccess(newSelStart, newSelEnd, shouldFinishComposition); @@ -1495,7 +1496,7 @@ public final class InputLogic { // TODO: Make this private. public void promotePhantomSpace(final SettingsValues settingsValues) { if (settingsValues.shouldInsertSpacesAutomatically() - && settingsValues.mCurrentLanguageHasSpaces + && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces && !mConnection.textBeforeCursorLooksLikeURL()) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { ResearchLogger.latinIME_promotePhantomSpace(); diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java index 75c7258ae..84ba7223a 100644 --- a/java/src/com/android/inputmethod/latin/settings/Settings.java +++ b/java/src/com/android/inputmethod/latin/settings/Settings.java @@ -176,7 +176,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang } public String getWordSeparators() { - return mSettingsValues.mWordSeparators; + return mSettingsValues.mSpacingAndPunctuations.mWordSeparators; } public boolean isWordSeparator(final int code) { diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java index a07a0cecf..3fa686ba7 100644 --- a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java @@ -26,20 +26,12 @@ import android.view.inputmethod.EditorInfo; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.compat.AppWorkaroundsUtils; -import com.android.inputmethod.keyboard.internal.KeySpecParser; -import com.android.inputmethod.latin.Constants; -import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.RichInputMethodManager; -import com.android.inputmethod.latin.SuggestedWords; -import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.utils.AsyncResultHolder; -import com.android.inputmethod.latin.utils.CollectionUtils; -import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask; -import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; @@ -56,15 +48,9 @@ public final class SettingsValues { private static final int TIMEOUT_TO_GET_TARGET_PACKAGE = 5; // seconds // From resources: + public final SpacingAndPunctuations mSpacingAndPunctuations; public final int mDelayUpdateOldSuggestions; - public final int[] mSymbolsPrecededBySpace; - public final int[] mSymbolsFollowedBySpace; - public final int[] mWordConnectors; - public final SuggestedWords mSuggestPuncList; - public final String mWordSeparators; - public final int mSentenceSeparator; public final CharSequence mHintToSaveText; - public final boolean mCurrentLanguageHasSpaces; // From preferences, in the same order as xml/prefs.xml: public final boolean mAutoCap; @@ -115,22 +101,8 @@ public final class SettingsValues { mLocale = locale; // Get the resources mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions); - mSymbolsPrecededBySpace = - StringUtils.toCodePointArray(res.getString(R.string.symbols_preceded_by_space)); - Arrays.sort(mSymbolsPrecededBySpace); - mSymbolsFollowedBySpace = - StringUtils.toCodePointArray(res.getString(R.string.symbols_followed_by_space)); - Arrays.sort(mSymbolsFollowedBySpace); - mWordConnectors = - StringUtils.toCodePointArray(res.getString(R.string.symbols_word_connectors)); - Arrays.sort(mWordConnectors); - final String[] suggestPuncsSpec = KeySpecParser.splitKeySpecs(res.getString( - R.string.suggested_punctuations)); - mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); - mWordSeparators = res.getString(R.string.symbols_word_separators); - mSentenceSeparator = res.getInteger(R.integer.sentence_separator); + mSpacingAndPunctuations = new SpacingAndPunctuations(res); mHintToSaveText = res.getText(R.string.hint_add_to_dictionary); - mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces); // Store the input attributes if (null == inputAttributes) { @@ -199,18 +171,8 @@ public final class SettingsValues { // TODO: locale is saved, but not used yet. May have to change this if tests require. mLocale = locale; mDelayUpdateOldSuggestions = 0; - mSymbolsPrecededBySpace = new int[] { '(', '[', '{', '&' }; - Arrays.sort(mSymbolsPrecededBySpace); - mSymbolsFollowedBySpace = new int[] { '.', ',', ';', ':', '!', '?', ')', ']', '}', '&' }; - Arrays.sort(mSymbolsFollowedBySpace); - mWordConnectors = new int[] { '\'', '-' }; - Arrays.sort(mWordConnectors); - mSentenceSeparator = Constants.CODE_PERIOD; - final String[] suggestPuncsSpec = new String[] { "!", "?", ",", ":", ";" }; - mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); - mWordSeparators = "&\t \n()[]{}*&<>+=|.,;:!?/_\""; + mSpacingAndPunctuations = SpacingAndPunctuations.DEFAULT; mHintToSaveText = "Touch again to save"; - mCurrentLanguageHasSpaces = true; mInputAttributes = new InputAttributes(null, false /* isFullscreenMode */); mAutoCap = true; mVibrateOn = true; @@ -265,11 +227,11 @@ public final class SettingsValues { } public boolean isWordSeparator(final int code) { - return mWordSeparators.contains(String.valueOf((char)code)); + return mSpacingAndPunctuations.isWordSeparator(code); } public boolean isWordConnector(final int code) { - return Arrays.binarySearch(mWordConnectors, code) >= 0; + return mSpacingAndPunctuations.isWordConnector(code); } public boolean isWordCodePoint(final int code) { @@ -277,11 +239,11 @@ public final class SettingsValues { } public boolean isUsuallyPrecededBySpace(final int code) { - return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0; + return mSpacingAndPunctuations.isUsuallyPrecededBySpace(code); } public boolean isUsuallyFollowedBySpace(final int code) { - return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0; + return mSpacingAndPunctuations.isUsuallyFollowedBySpace(code); } public boolean shouldInsertSpacesAutomatically() { @@ -320,27 +282,6 @@ public final class SettingsValues { return null == appWorkaroundUtils ? false : appWorkaroundUtils.isBrokenByRecorrection(); } - // Helper functions to create member values. - private static SuggestedWords createSuggestPuncList(final String[] puncs) { - final ArrayList puncList = CollectionUtils.newArrayList(); - if (puncs != null) { - for (final String puncSpec : puncs) { - // TODO: Stop using KeySpceParser.getLabel(). - puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec), - SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED, - Dictionary.DICTIONARY_HARDCODED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); - } - } - return new SuggestedWords(puncList, - false /* typedWordValid */, - false /* hasAutoCorrectionCandidate */, - true /* isPunctuationSuggestions */, - false /* isObsoleteSuggestions */, - false /* isPrediction */); - } - private static final int SUGGESTION_VISIBILITY_SHOW_VALUE = R.string.prefs_suggestion_visibility_show_value; private static final int SUGGESTION_VISIBILITY_SHOW_ONLY_PORTRAIT_VALUE = diff --git a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java new file mode 100644 index 000000000..0500f4ad4 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java @@ -0,0 +1,124 @@ +/* + * 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.settings; + +import android.content.res.Resources; + +import com.android.inputmethod.keyboard.internal.KeySpecParser; +import com.android.inputmethod.latin.Constants; +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.utils.CollectionUtils; +import com.android.inputmethod.latin.utils.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; + +public final class SpacingAndPunctuations { + private final int[] mSymbolsPrecededBySpace; + private final int[] mSymbolsFollowedBySpace; + private final int[] mWordConnectors; + public final SuggestedWords mSuggestPuncList; + public final String mWordSeparators; + private final int mSentenceSeparator; + public final String mSentenceSeparatorAndSpace; + public final boolean mCurrentLanguageHasSpaces; + + public static final SpacingAndPunctuations DEFAULT = new SpacingAndPunctuations(); + + private SpacingAndPunctuations() { + mSymbolsPrecededBySpace = new int[] { '(', '[', '{', '&' }; + Arrays.sort(mSymbolsPrecededBySpace); + mSymbolsFollowedBySpace = new int[] { '.', ',', ';', ':', '!', '?', ')', ']', '}', '&' }; + Arrays.sort(mSymbolsFollowedBySpace); + mWordConnectors = new int[] { '\'', '-' }; + Arrays.sort(mWordConnectors); + mSentenceSeparator = Constants.CODE_PERIOD; + mSentenceSeparatorAndSpace = ". "; + final String[] suggestPuncsSpec = new String[] { "!", "?", ",", ":", ";" }; + mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); + mWordSeparators = "&\t \n()[]{}*&<>+=|.,;:!?/_\""; + mCurrentLanguageHasSpaces = true; + } + + public SpacingAndPunctuations(final Resources res) { + mSymbolsPrecededBySpace = + StringUtils.toCodePointArray(res.getString(R.string.symbols_preceded_by_space)); + Arrays.sort(mSymbolsPrecededBySpace); + mSymbolsFollowedBySpace = + StringUtils.toCodePointArray(res.getString(R.string.symbols_followed_by_space)); + Arrays.sort(mSymbolsFollowedBySpace); + mWordConnectors = + StringUtils.toCodePointArray(res.getString(R.string.symbols_word_connectors)); + Arrays.sort(mWordConnectors); + final String[] suggestPuncsSpec = KeySpecParser.splitKeySpecs(res.getString( + R.string.suggested_punctuations)); + mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); + mWordSeparators = res.getString(R.string.symbols_word_separators); + mSentenceSeparator = res.getInteger(R.integer.sentence_separator); + mSentenceSeparatorAndSpace = new String(new int[] { + mSentenceSeparator, Constants.CODE_SPACE }, 0, 2); + mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces); + } + + // Helper functions to create member values. + private static SuggestedWords createSuggestPuncList(final String[] puncs) { + final ArrayList puncList = CollectionUtils.newArrayList(); + if (puncs != null) { + for (final String puncSpec : puncs) { + // TODO: Stop using KeySpceParser.getLabel(). + puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec), + SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED, + Dictionary.DICTIONARY_HARDCODED, + SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, + SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); + } + } + return new SuggestedWords(puncList, + false /* typedWordValid */, + false /* hasAutoCorrectionCandidate */, + true /* isPunctuationSuggestions */, + false /* isObsoleteSuggestions */, + false /* isPrediction */); + } + + public boolean isWordSeparator(final int code) { + return mWordSeparators.contains(String.valueOf((char)code)); + } + + public boolean isWordConnector(final int code) { + return Arrays.binarySearch(mWordConnectors, code) >= 0; + } + + public boolean isWordCodePoint(final int code) { + return Character.isLetter(code) || isWordConnector(code); + } + + public boolean isUsuallyPrecededBySpace(final int code) { + return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0; + } + + public boolean isUsuallyFollowedBySpace(final int code) { + return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0; + } + + public boolean isSentenceSeparator(final int code) { + return code == mSentenceSeparator; + } +} diff --git a/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java b/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java index 3d4404a98..6ad5c77d5 100644 --- a/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/CapsModeUtils.java @@ -191,7 +191,7 @@ public final class CapsModeUtils { if (c == Constants.CODE_QUESTION_MARK || c == Constants.CODE_EXCLAMATION_MARK) { return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_SENTENCES) & reqModes; } - if (settingsValues.mSentenceSeparator != c || j <= 0) { + if (!settingsValues.mSpacingAndPunctuations.isSentenceSeparator(c) || j <= 0) { return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; } @@ -241,7 +241,7 @@ public final class CapsModeUtils { case WORD: if (Character.isLetter(c)) { state = WORD; - } else if (settingsValues.mSentenceSeparator == c) { + } else if (settingsValues.mSpacingAndPunctuations.isSentenceSeparator(c)) { state = PERIOD; } else { return caps; @@ -257,7 +257,7 @@ public final class CapsModeUtils { case LETTER: if (Character.isLetter(c)) { state = LETTER; - } else if (settingsValues.mSentenceSeparator == c) { + } else if (settingsValues.mSpacingAndPunctuations.isSentenceSeparator(c)) { state = PERIOD; } else { return noCaps;