Remove a trailing "auto space" at the end of the line when the user added a new line.

Bug: 2384116
diff --git a/src/com/android/inputmethod/latin/LatinIME.java b/src/com/android/inputmethod/latin/LatinIME.java
index 3f3793b..a99811c 100644
--- a/src/com/android/inputmethod/latin/LatinIME.java
+++ b/src/com/android/inputmethod/latin/LatinIME.java
@@ -192,6 +192,7 @@
     private boolean mCompletionOn;
     private boolean mHasDictionary;
     private boolean mAutoSpace;
+    private boolean mJustAddedAutoSpace;
     private boolean mAutoCorrectEnabled;
     private boolean mAutoCorrectOn;
     private boolean mCapsLock;
@@ -530,6 +531,7 @@
         mComposing.setLength(0);
         mPredicting = false;
         mDeleteCount = 0;
+        mJustAddedAutoSpace = false;
         loadSettings();
 
         setCandidatesViewShown(false);
@@ -618,9 +620,15 @@
                 ic.finishComposingText();
             }
             mVoiceInputHighlighted = false;
-        } else if (!mPredicting && !mJustAccepted
-                && TextEntryState.getState() == TextEntryState.STATE_ACCEPTED_DEFAULT) {
-            TextEntryState.reset();
+        } else if (!mPredicting && !mJustAccepted) {
+            switch (TextEntryState.getState()) {
+                case TextEntryState.STATE_ACCEPTED_DEFAULT:
+                    TextEntryState.reset();
+                    // fall through
+                case TextEntryState.STATE_SPACE_AFTER_PICKED:
+                    mJustAddedAutoSpace = false;  // The user moved the cursor.
+                    break;
+            }
         }
         mJustAccepted = false;
         postUpdateShiftKeyState();
@@ -841,6 +849,7 @@
             ic.commitText(lastTwo.charAt(1) + " ", 1);
             ic.endBatchEdit();
             updateShiftKeyState(getCurrentInputEditorInfo());
+            mJustAddedAutoSpace = true;
         }
     }
 
@@ -858,6 +867,7 @@
             ic.commitText(". ", 1);
             ic.endBatchEdit();
             updateShiftKeyState(getCurrentInputEditorInfo());
+            mJustAddedAutoSpace = true;
         }
     }
 
@@ -874,6 +884,17 @@
         }
     }
 
+    private void removeTrailingSpace() {
+        final InputConnection ic = getCurrentInputConnection();
+        if (ic == null) return;
+
+        CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
+        if (lastOne != null && lastOne.length() == 1
+                && lastOne.charAt(0) == KEYCODE_SPACE) {
+            ic.deleteSurroundingText(1, 0);
+        }
+    }
+
     public boolean addWordToDictionary(String word) {
         mUserDictionary.addWord(word, 128);
         return true;
@@ -937,6 +958,9 @@
                 sendDownUpKeyEvents(KeyEvent.KEYCODE_TAB);
                 break;
             default:
+                if (primaryCode != KEYCODE_ENTER) {
+                    mJustAddedAutoSpace = false;
+                }
                 if (isWordSeparator(primaryCode)) {
                     handleSeparator(primaryCode);
                 } else {
@@ -962,6 +986,7 @@
         ic.endBatchEdit();
         updateShiftKeyState(getCurrentInputEditorInfo());
         mJustRevertedSeparator = null;
+        mJustAddedAutoSpace = false;
     }
 
     private void handleBackspace() {
@@ -1077,16 +1102,25 @@
                             || mJustRevertedSeparator.charAt(0) != primaryCode)) {
                 pickDefaultSuggestion();
                 pickedDefault = true;
+                // Picked the suggestion by the space key.  We consider this
+                // as "added an auto space".
+                if (primaryCode == KEYCODE_SPACE) {
+                    mJustAddedAutoSpace = true;
+                }
             } else {
                 commitTyped(ic);
             }
         }
+        if (mJustAddedAutoSpace && primaryCode == KEYCODE_ENTER) {
+            removeTrailingSpace();
+            mJustAddedAutoSpace = false;
+        }
         sendKeyChar((char)primaryCode);
         TextEntryState.typedCharacter((char) primaryCode, true);
         if (TextEntryState.getState() == TextEntryState.STATE_PUNCTUATION_AFTER_ACCEPTED
                 && primaryCode != KEYCODE_ENTER) {
             swapPunctuationAndSpace();
-        } else if (isPredictionOn() && primaryCode == ' ') {
+        } else if (isPredictionOn() && primaryCode == KEYCODE_SPACE) {
         //else if (TextEntryState.STATE_SPACE_AFTER_ACCEPTED) {
             doubleSpace();
         }
@@ -1390,10 +1424,13 @@
     public void pickSuggestionManually(int index, CharSequence suggestion) {
         if (mAfterVoiceInput && mShowingVoiceSuggestions) mVoiceInput.logNBestChoose(index);
 
+        InputConnection ic = getCurrentInputConnection();
+        if (ic != null) {
+            ic.beginBatchEdit();
+        }
         if (mCompletionOn && mCompletions != null && index >= 0
                 && index < mCompletions.length) {
             CompletionInfo ci = mCompletions[index];
-            InputConnection ic = getCurrentInputConnection();
             if (ic != null) {
                 ic.commitCompletion(ci);
             }
@@ -1402,25 +1439,36 @@
                 mCandidateView.clear();
             }
             updateShiftKeyState(getCurrentInputEditorInfo());
+            if (ic != null) {
+                ic.endBatchEdit();
+            }
             return;
         }
 
         // If this is a punctuation, apply it through the normal key press
         if (suggestion.length() == 1 && isWordSeparator(suggestion.charAt(0))) {
             onKey(suggestion.charAt(0), null);
+            if (ic != null) {
+                ic.endBatchEdit();
+            }
             return;
         }
+        mJustAccepted = true;
         pickSuggestion(suggestion);
         TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion);
         // Follow it with a space
         if (mAutoSpace) {
             sendSpace();
+            mJustAddedAutoSpace = true;
         }
         // Fool the state watcher so that a subsequent backspace will not do a revert
         TextEntryState.typedCharacter((char) KEYCODE_SPACE, true);
         if (index == 0 && !mSuggest.isValidWord(suggestion)) {
             mCandidateView.showAddToDictionaryHint(suggestion);
         }
+        if (ic != null) {
+            ic.endBatchEdit();
+        }
     }
 
     private void pickSuggestion(CharSequence suggestion) {