Merge "Revert "Separate spacing and punctuation related settings values""

main
Jean Chalard 2014-01-08 09:03:14 +00:00 committed by Android (Google) Code Review
commit 1e7f2809d2
6 changed files with 85 additions and 153 deletions

View File

@ -1301,8 +1301,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
@UsedForTesting
public boolean isShowingPunctuationList() {
if (mInputLogic.mSuggestedWords == null) return false;
return mSettings.getCurrent().mSpacingAndPunctuations.mSuggestPuncList
== mInputLogic.mSuggestedWords;
return mSettings.getCurrent().mSuggestPuncList == mInputLogic.mSuggestedWords;
}
// TODO[IL]: Define a clear interface for this
@ -1418,8 +1417,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private SuggestedWords getOlderSuggestions(final String typedWord) {
SuggestedWords previousSuggestedWords = mInputLogic.mSuggestedWords;
if (previousSuggestedWords
== mSettings.getCurrent().mSpacingAndPunctuations.mSuggestPuncList) {
if (previousSuggestedWords == mSettings.getCurrent().mSuggestPuncList) {
previousSuggestedWords = SuggestedWords.EMPTY;
}
if (typedWord == null) {
@ -1572,7 +1570,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (currentSettings.mBigramPredictionEnabled) {
clearSuggestionStrip();
} else {
setSuggestedWords(currentSettings.mSpacingAndPunctuations.mSuggestPuncList);
setSuggestedWords(currentSettings.mSuggestPuncList);
}
setAutoCorrectionIndicator(false);
setSuggestionStripShown(isSuggestionsStripVisible());

View File

@ -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.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) {
|| !settingsValues.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.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
&& !settingsValues.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.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) {
&& settingsValues.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 =
settingsValues.mSpacingAndPunctuations.mSentenceSeparatorAndSpace;
final String textToInsert = new String(
new int[] { settingsValues.mSentenceSeparator, Constants.CODE_SPACE }, 0, 2);
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.mSpacingAndPunctuations.mWordSeparators);
settingsValues.mLocale, settingsValues.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.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) return;
if (!settingsValues.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,8 +1073,7 @@ public final class InputLogic {
final int expectedCursorPosition = mLastSelectionStart + offset; // We know Start == End
if (!mConnection.isCursorTouchingWord(settingsValues)) return;
final TextRange range = mConnection.getWordRangeAtCursor(
settingsValues.mSpacingAndPunctuations.mWordSeparators,
0 /* additionalPrecedingWordsCount */);
settingsValues.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
@ -1199,7 +1198,7 @@ public final class InputLogic {
}
}
final String stringToCommit = originallyTypedWord + mLastComposedWord.mSeparatorString;
if (settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) {
if (settingsValues.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.
@ -1299,7 +1298,7 @@ public final class InputLogic {
// TODO: Make this private
public String getNthPreviousWordForSuggestion(final SettingsValues currentSettings,
final int nthPreviousWord) {
if (currentSettings.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) {
if (currentSettings.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);
@ -1390,7 +1389,7 @@ public final class InputLogic {
if (settingsValues.mBigramPredictionEnabled) {
mLatinIME.clearSuggestionStrip();
} else {
mLatinIME.setSuggestedWords(settingsValues.mSpacingAndPunctuations.mSuggestPuncList);
mLatinIME.setSuggestedWords(settingsValues.mSuggestPuncList);
}
mConnection.resetCachesUponCursorMoveAndReturnSuccess(newSelStart, newSelEnd,
shouldFinishComposition);
@ -1499,7 +1498,7 @@ public final class InputLogic {
// TODO: Make this private.
public void promotePhantomSpace(final SettingsValues settingsValues) {
if (settingsValues.shouldInsertSpacesAutomatically()
&& settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
&& settingsValues.mCurrentLanguageHasSpaces
&& !mConnection.textBeforeCursorLooksLikeURL()) {
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.latinIME_promotePhantomSpace();

View File

@ -176,7 +176,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
}
public String getWordSeparators() {
return mSettingsValues.mSpacingAndPunctuations.mWordSeparators;
return mSettingsValues.mWordSeparators;
}
public boolean isWordSeparator(final int code) {

View File

@ -26,12 +26,20 @@ 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;
@ -48,9 +56,15 @@ 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;
@ -101,8 +115,22 @@ public final class SettingsValues {
mLocale = locale;
// Get the resources
mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions);
mSpacingAndPunctuations = new SpacingAndPunctuations(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);
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) {
@ -171,8 +199,18 @@ public final class SettingsValues {
// TODO: locale is saved, but not used yet. May have to change this if tests require.
mLocale = locale;
mDelayUpdateOldSuggestions = 0;
mSpacingAndPunctuations = SpacingAndPunctuations.DEFAULT;
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()[]{}*&<>+=|.,;:!?/_\"";
mHintToSaveText = "Touch again to save";
mCurrentLanguageHasSpaces = true;
mInputAttributes = new InputAttributes(null, false /* isFullscreenMode */);
mAutoCap = true;
mVibrateOn = true;
@ -227,11 +265,11 @@ public final class SettingsValues {
}
public boolean isWordSeparator(final int code) {
return mSpacingAndPunctuations.isWordSeparator(code);
return mWordSeparators.contains(String.valueOf((char)code));
}
public boolean isWordConnector(final int code) {
return mSpacingAndPunctuations.isWordConnector(code);
return Arrays.binarySearch(mWordConnectors, code) >= 0;
}
public boolean isWordCodePoint(final int code) {
@ -239,11 +277,11 @@ public final class SettingsValues {
}
public boolean isUsuallyPrecededBySpace(final int code) {
return mSpacingAndPunctuations.isUsuallyPrecededBySpace(code);
return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0;
}
public boolean isUsuallyFollowedBySpace(final int code) {
return mSpacingAndPunctuations.isUsuallyFollowedBySpace(code);
return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0;
}
public boolean shouldInsertSpacesAutomatically() {
@ -282,6 +320,27 @@ 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<SuggestedWordInfo> 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 =

View File

@ -1,124 +0,0 @@
/*
* 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<SuggestedWordInfo> 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;
}
}

View File

@ -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.mSpacingAndPunctuations.isSentenceSeparator(c) || j <= 0) {
if (settingsValues.mSentenceSeparator != 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.mSpacingAndPunctuations.isSentenceSeparator(c)) {
} else if (settingsValues.mSentenceSeparator == 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.mSpacingAndPunctuations.isSentenceSeparator(c)) {
} else if (settingsValues.mSentenceSeparator == c) {
state = PERIOD;
} else {
return noCaps;