From 1c551251106e506c70fad7ba0cb8b1e2a7dff3a9 Mon Sep 17 00:00:00 2001
From: Amith Yamasani <yamasani@google.com>
Date: Sun, 24 Jan 2010 07:34:07 -0800
Subject: [PATCH] Auto-added words will only be capitalized if user intended
 to. #2373284

If a user creates a new word at the beginning of a sentence, then don't
add it as a capitalized word in the dictionary.
---
 .../android/inputmethod/latin/LatinIME.java   | 30 ++++++++++++++-----
 .../inputmethod/latin/WordComposer.java       | 19 ++++++++++++
 2 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/src/com/android/inputmethod/latin/LatinIME.java b/src/com/android/inputmethod/latin/LatinIME.java
index a71f3d867b..a9a61c3a2b 100644
--- a/src/com/android/inputmethod/latin/LatinIME.java
+++ b/src/com/android/inputmethod/latin/LatinIME.java
@@ -815,15 +815,19 @@ public class LatinIME extends InputMethodService
         InputConnection ic = getCurrentInputConnection();
         if (attr != null && mInputView != null && mKeyboardSwitcher.isAlphabetMode()
                 && ic != null) {
-            int caps = 0;
-            EditorInfo ei = getCurrentInputEditorInfo();
-            if (mAutoCap && ei != null && ei.inputType != EditorInfo.TYPE_NULL) {
-                caps = ic.getCursorCapsMode(attr.inputType);
-            }
-            mInputView.setShifted(mCapsLock || caps != 0);
+            mInputView.setShifted(mCapsLock || getCursorCapsMode(ic, attr) != 0);
         }
     }
-    
+
+    private int getCursorCapsMode(InputConnection ic, EditorInfo attr) {
+        int caps = 0;
+        EditorInfo ei = getCurrentInputEditorInfo();
+        if (mAutoCap && ei != null && ei.inputType != EditorInfo.TYPE_NULL) {
+            caps = ic.getCursorCapsMode(attr.inputType);
+        }
+        return caps;
+    }
+
     private void swapPunctuationAndSpace() {
         final InputConnection ic = getCurrentInputConnection();
         if (ic == null) return;
@@ -837,7 +841,7 @@ public class LatinIME extends InputMethodService
             updateShiftKeyState(getCurrentInputEditorInfo());
         }
     }
-    
+
     private void doubleSpace() {
         //if (!mAutoPunctuate) return;
         if (mCorrectionMode == Suggest.CORRECTION_NONE) return;
@@ -1014,6 +1018,11 @@ public class LatinIME extends InputMethodService
             mWord.add(primaryCode, keyCodes);
             InputConnection ic = getCurrentInputConnection();
             if (ic != null) {
+                // If it's the first letter, make note of auto-caps state
+                if (mWord.size() == 1) {
+                    mWord.setAutoCapitalized(
+                            getCursorCapsMode(ic, getCurrentInputEditorInfo()) != 0);
+                }
                 ic.setComposingText(mComposing, 1);
             }
             postUpdateSuggestions();
@@ -1847,6 +1856,11 @@ public class LatinIME extends InputMethodService
             final int length = word.length();
             // Don't add very short or very long words.
             if (length < 2 || length > getMaxWordLength()) return;
+            if (mWord.isAutoCapitalized()) {
+                // Remove caps before adding
+                word = Character.toLowerCase(word.charAt(0))
+                        + word.substring(1);
+            }
             int freq = getWordFrequency(word);
             freq = freq < 0 ? addFrequency : freq + addFrequency;
             super.addWord(word, freq);
diff --git a/src/com/android/inputmethod/latin/WordComposer.java b/src/com/android/inputmethod/latin/WordComposer.java
index 50725d481e..e97cb24bac 100644
--- a/src/com/android/inputmethod/latin/WordComposer.java
+++ b/src/com/android/inputmethod/latin/WordComposer.java
@@ -36,6 +36,8 @@ public class WordComposer {
     private StringBuilder mTypedWord;
 
     private int mCapsCount;
+
+    private boolean mAutoCapitalized;
     
     /**
      * Whether the user chose to capitalize the word.
@@ -152,4 +154,21 @@ public class WordComposer {
     public boolean isMostlyCaps() {
         return mCapsCount > 1;
     }
+
+    /** 
+     * Saves the reason why the word is capitalized - whether it was automatic or
+     * due to the user hitting shift in the middle of a sentence.
+     * @param auto whether it was an automatic capitalization due to start of sentence
+     */
+    public void setAutoCapitalized(boolean auto) {
+        mAutoCapitalized = auto;
+    }
+
+    /**
+     * Returns whether the word was automatically capitalized.
+     * @return whether the word was automatically capitalized
+     */
+    public boolean isAutoCapitalized() {
+        return mAutoCapitalized;
+    }
 }
-- 
GitLab