From 32c6d4caea8e1391804f405d78e33e5c0fe89b4b Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 26 Feb 2014 14:39:51 +0900 Subject: [PATCH] [IL111] Remove a member. ...and improve encapsulation and readability. Bug: 8636060 Change-Id: I36407630b307df0721ea7fdb7e7a22cdf2b018b3 --- .../android/inputmethod/latin/LatinIME.java | 6 ---- .../inputmethod/latin/SuggestedWords.java | 33 ++++++++++++++----- .../latin/inputlogic/InputLogic.java | 13 ++++---- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 4c2454c32..13ac18791 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -124,9 +124,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private View mKeyPreviewBackingView; private SuggestionStripView mSuggestionStripView; - // TODO[IL]: remove this member completely. - public CompletionInfo[] mApplicationSpecifiedCompletions; - private RichInputMethodManager mRichImm; @UsedForTesting final KeyboardSwitcher mKeyboardSwitcher; private final SubtypeSwitcher mSubtypeSwitcher; @@ -812,7 +809,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // The EditorInfo might have a flag that affects fullscreen mode. // Note: This call should be done by InputMethodService? updateFullscreenMode(); - mApplicationSpecifiedCompletions = null; // The app calling setText() has the effect of clearing the composing // span, so we should reset our state unconditionally, even if restarting is true. @@ -1034,8 +1030,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } return; } - mApplicationSpecifiedCompletions = - CompletionInfoUtils.removeNulls(applicationSpecifiedCompletions); final ArrayList applicationSuggestedWords = SuggestedWords.getFromApplicationSpecifiedCompletions( diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 06bc90c97..dc2c9fd0e 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -167,15 +167,10 @@ public class SuggestedWords { final CompletionInfo[] infos) { final ArrayList result = CollectionUtils.newArrayList(); for (final CompletionInfo info : infos) { - if (info == null) continue; - final CharSequence text = info.getText(); - if (null == text) continue; - final SuggestedWordInfo suggestedWordInfo = new SuggestedWordInfo(text.toString(), - SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_APP_DEFINED, - Dictionary.DICTIONARY_APPLICATION_DEFINED, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); - result.add(suggestedWordInfo); + if (null == info || null == info.getText()) { + continue; + } + result.add(new SuggestedWordInfo(info)); } return result; } @@ -234,6 +229,9 @@ public class SuggestedWords { public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; public final String mWord; + // The completion info from the application. Null for suggestions that don't come from + // the application (including keyboard-computed ones, so this is almost always null) + public final CompletionInfo mApplicationSpecifiedCompletionInfo; public final int mScore; public final int mKind; // one of the KIND_* constants above public final int mCodePointCount; @@ -260,6 +258,7 @@ public class SuggestedWords { final Dictionary sourceDict, final int indexOfTouchPointOfSecondWord, final int autoCommitFirstWordConfidence) { mWord = word; + mApplicationSpecifiedCompletionInfo = null; mScore = score; mKind = kind; mSourceDict = sourceDict; @@ -268,6 +267,22 @@ public class SuggestedWords { mAutoCommitFirstWordConfidence = autoCommitFirstWordConfidence; } + /** + * Create a new suggested word info from an application-specified completion. + * If the passed argument or its contained text is null, this throws a NPE. + * @param applicationSpecifiedCompletion The application-specified completion info. + */ + public SuggestedWordInfo(final CompletionInfo applicationSpecifiedCompletion) { + mWord = applicationSpecifiedCompletion.getText().toString(); + mApplicationSpecifiedCompletionInfo = applicationSpecifiedCompletion; + mScore = SuggestedWordInfo.MAX_SCORE; + mKind = SuggestedWordInfo.KIND_APP_DEFINED; + mSourceDict = Dictionary.DICTIONARY_APPLICATION_DEFINED; + mCodePointCount = StringUtils.codePointCount(mWord); + mIndexOfTouchPointOfSecondWord = SuggestedWordInfo.NOT_AN_INDEX; + mAutoCommitFirstWordConfidence = SuggestedWordInfo.NOT_A_CONFIDENCE; + } + public boolean isEligibleForAutoCommit() { return (KIND_CORRECTION == mKind && NOT_AN_INDEX != mIndexOfTouchPointOfSecondWord); } diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index f2f9f1e68..8b83155ea 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -227,17 +227,16 @@ public final class InputLogic { } } - // 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) { + // TODO: We should not need the following branch. We should be able to take the same + // code path as for other kinds, use commitChosenWord, and do everything normally. We will + // however need to reset the suggestion strip right away, because we know we can't take + // the risk of calling commitCompletion twice because we don't know how the app will react. + if (SuggestedWordInfo.KIND_APP_DEFINED == suggestionInfo.mKind) { mSuggestedWords = SuggestedWords.EMPTY; mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); keyboardSwitcher.updateShiftState(); resetComposingState(true /* alsoResetLastComposedWord */); - final CompletionInfo completionInfo = mLatinIME.mApplicationSpecifiedCompletions[index]; - mConnection.commitCompletion(completionInfo); + mConnection.commitCompletion(suggestionInfo.mApplicationSpecifiedCompletionInfo); mConnection.endBatchEdit(); return; }