Merge "Insert into user dict in lower case if auto-caps (D2)"
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index fd7aac4..f9d51ff 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -269,8 +269,7 @@
     <string name="research_feedback_dialog_title" translatable="false">Send feedback</string>
     <!-- Text for checkbox option to include user data in feedback for research purposes [CHAR LIMIT=50] -->
     <!-- TODO: remove translatable=false attribute once text is stable -->
-    <!-- TODO: handle multilingual plurals -->
-    <string name="research_feedback_include_history_label" translatable="false">Include last <xliff:g id="word">%d</xliff:g> words entered</string>
+    <string name="research_feedback_include_history_label" translatable="false">Include session history</string>
     <!-- Hint to user about the text entry field where they should enter research feedback [CHAR LIMIT=40] -->
     <!-- TODO: remove translatable=false attribute once text is stable -->
     <string name="research_feedback_hint" translatable="false">Enter your feedback here.</string>
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index 572f290..b7ca60f 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -65,10 +65,6 @@
         return mSuggestedWordInfoList.get(pos).mWord;
     }
 
-    public SuggestedWordInfo getWordInfo(int pos) {
-        return mSuggestedWordInfoList.get(pos);
-    }
-
     public SuggestedWordInfo getInfo(int pos) {
         return mSuggestedWordInfoList.get(pos);
     }
@@ -113,7 +109,7 @@
         alreadySeen.add(typedWord.toString());
         final int previousSize = previousSuggestions.size();
         for (int pos = 1; pos < previousSize; pos++) {
-            final SuggestedWordInfo prevWordInfo = previousSuggestions.getWordInfo(pos);
+            final SuggestedWordInfo prevWordInfo = previousSuggestions.getInfo(pos);
             final String prevWord = prevWordInfo.mWord;
             // Filter out duplicate suggestion.
             if (!alreadySeen.contains(prevWord)) {
diff --git a/java/src/com/android/inputmethod/research/JsonUtils.java b/java/src/com/android/inputmethod/research/JsonUtils.java
index 1dfd01c..ceba08d 100644
--- a/java/src/com/android/inputmethod/research/JsonUtils.java
+++ b/java/src/com/android/inputmethod/research/JsonUtils.java
@@ -98,7 +98,7 @@
         jsonWriter.beginArray();
         final int size = words.size();
         for (int j = 0; j < size; j++) {
-            final SuggestedWordInfo wordInfo = words.getWordInfo(j);
+            final SuggestedWordInfo wordInfo = words.getInfo(j);
             jsonWriter.value(wordInfo.toString());
         }
         jsonWriter.endArray();
diff --git a/java/src/com/android/inputmethod/research/LogUnit.java b/java/src/com/android/inputmethod/research/LogUnit.java
index cfba289..70bbf9d 100644
--- a/java/src/com/android/inputmethod/research/LogUnit.java
+++ b/java/src/com/android/inputmethod/research/LogUnit.java
@@ -60,6 +60,7 @@
     private String mWord;
     private boolean mMayContainDigit;
     private boolean mIsPartOfMegaword;
+    private boolean mContainsCorrection;
 
     public LogUnit() {
         mLogStatementList = new ArrayList<LogStatement>();
@@ -274,6 +275,14 @@
         return mMayContainDigit;
     }
 
+    public void setContainsCorrection() {
+        mContainsCorrection = true;
+    }
+
+    public boolean containsCorrection() {
+        return mContainsCorrection;
+    }
+
     public boolean isEmpty() {
         return mLogStatementList.isEmpty();
     }
@@ -301,6 +310,7 @@
                         true /* isPartOfMegaword */);
                 newLogUnit.mWord = null;
                 newLogUnit.mMayContainDigit = mMayContainDigit;
+                newLogUnit.mContainsCorrection = mContainsCorrection;
 
                 // Purge the logStatements and associated data from this LogUnit.
                 laterLogStatements.clear();
@@ -320,6 +330,7 @@
         mTimeList.addAll(logUnit.mTimeList);
         mWord = null;
         mMayContainDigit = mMayContainDigit || logUnit.mMayContainDigit;
+        mContainsCorrection = mContainsCorrection || logUnit.mContainsCorrection;
         mIsPartOfMegaword = false;
     }
 }
diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java
index f4249a0..a46216c 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -106,7 +106,8 @@
     // Change the default indicator to something very visible.  Currently two red vertical bars on
     // either side of they keyboard.
     private static final boolean IS_SHOWING_INDICATOR_CLEARLY = false || IS_LOGGING_EVERYTHING;
-    public static final int FEEDBACK_WORD_BUFFER_SIZE = 5;
+    // FEEDBACK_WORD_BUFFER_SIZE should add 1 because it must also hold the feedback LogUnit itself.
+    public static final int FEEDBACK_WORD_BUFFER_SIZE = (Integer.MAX_VALUE - 1) + 1;
 
     // constants related to specific log points
     private static final String WHITESPACE_SEPARATORS = " \t\n\r";
@@ -391,9 +392,7 @@
         }
         if (mFeedbackLogBuffer == null) {
             mFeedbackLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME);
-            // LogBuffer is one more than FEEDBACK_WORD_BUFFER_SIZE, because it must also hold
-            // the feedback LogUnit itself.
-            mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE + 1);
+            mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE);
         }
     }
 
@@ -522,8 +521,25 @@
     */
 
     private boolean mInFeedbackDialog = false;
+
+    // The feedback dialog causes stop() to be called for the keyboard connected to the original
+    // window.  This is because the feedback dialog must present its own EditText box that displays
+    // a keyboard.  stop() normally causes mFeedbackLogBuffer, which contains the user's data, to be
+    // cleared, and causes mFeedbackLog, which is ready to collect information in case the user
+    // wants to upload, to be closed.  This is good because we don't need to log information about
+    // what the user is typing in the feedback dialog, but bad because this data must be uploaded.
+    // Here we save the LogBuffer and Log so the feedback dialog can later access their data.
+    private LogBuffer mSavedFeedbackLogBuffer;
+    private ResearchLog mSavedFeedbackLog;
+
     public void presentFeedbackDialog(LatinIME latinIME) {
         mInFeedbackDialog = true;
+        mSavedFeedbackLogBuffer = mFeedbackLogBuffer;
+        mSavedFeedbackLog = mFeedbackLog;
+        // Set the non-saved versions to null so that the stop() caused by switching to the
+        // Feedback dialog will not close them.
+        mFeedbackLogBuffer = null;
+        mFeedbackLog = null;
         latinIME.launchKeyboardedDialogActivity(FeedbackActivity.class);
     }
 
@@ -589,28 +605,25 @@
     }
 
     private static final LogStatement LOGSTATEMENT_FEEDBACK =
-            new LogStatement("UserTimestamp", false, false, "contents");
+            new LogStatement("UserFeedback", false, false, "contents");
     public void sendFeedback(final String feedbackContents, final boolean includeHistory) {
-        if (mFeedbackLogBuffer == null) {
+        if (mSavedFeedbackLogBuffer == null) {
             return;
         }
-        if (includeHistory) {
-            commitCurrentLogUnit();
-        } else {
-            mFeedbackLogBuffer.clear();
+        if (!includeHistory) {
+            mSavedFeedbackLogBuffer.clear();
         }
         final LogUnit feedbackLogUnit = new LogUnit();
         feedbackLogUnit.addLogStatement(LOGSTATEMENT_FEEDBACK, SystemClock.uptimeMillis(),
                 feedbackContents);
         mFeedbackLogBuffer.shiftIn(feedbackLogUnit);
-        publishLogBuffer(mFeedbackLogBuffer, mFeedbackLog, true /* isIncludingPrivateData */);
-        mFeedbackLog.close(new Runnable() {
+        publishLogBuffer(mFeedbackLogBuffer, mSavedFeedbackLog, true /* isIncludingPrivateData */);
+        mSavedFeedbackLog.close(new Runnable() {
             @Override
             public void run() {
                 uploadNow();
             }
         });
-        mFeedbackLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME);
     }
 
     public void uploadNow() {
@@ -643,13 +656,6 @@
     }
 
     private boolean isAllowedToLog() {
-        if (DEBUG) {
-            Log.d(TAG, "iatl: " +
-                "mipw=" + mIsPasswordView +
-                ", mils=" + mIsLoggingSuspended +
-                ", sil=" + sIsLogging +
-                ", mInFeedbackDialog=" + mInFeedbackDialog);
-        }
         return !mIsPasswordView && !mIsLoggingSuspended && sIsLogging && !mInFeedbackDialog;
     }
 
@@ -714,6 +720,10 @@
         mCurrentLogUnit.setMayContainDigit();
     }
 
+    private void setCurrentLogUnitContainsCorrection() {
+        mCurrentLogUnit.setContainsCorrection();
+    }
+
     /* package for test */ void commitCurrentLogUnit() {
         if (DEBUG) {
             Log.d(TAG, "commitCurrentLogUnit" + (mCurrentLogUnit.hasWord() ?
@@ -844,7 +854,7 @@
             mCurrentLogUnit.setWord(word);
             final boolean isDictionaryWord = dictionary != null
                     && dictionary.isValidWord(word);
-            mStatistics.recordWordEntered(isDictionaryWord);
+            mStatistics.recordWordEntered(isDictionaryWord, mCurrentLogUnit.containsCorrection());
         }
         final LogUnit newLogUnit = mCurrentLogUnit.splitByTime(maxTime);
         enqueueCommitText(word, isBatchMode);
@@ -852,6 +862,11 @@
         mCurrentLogUnit = newLogUnit;
     }
 
+    /**
+     * Record the time of a MotionEvent.ACTION_DOWN.
+     *
+     * Warning: Not thread safe.  Only call from the main thread.
+     */
     private void setSavedDownEventTime(final long time) {
         mSavedDownEventTime = time;
     }
@@ -1170,6 +1185,7 @@
                 scrubDigitsFromString(replacedWord), index,
                 suggestion == null ? null : scrubbedWord, Constants.SUGGESTION_STRIP_COORDINATE,
                 Constants.SUGGESTION_STRIP_COORDINATE);
+        researchLogger.setCurrentLogUnitContainsCorrection();
         researchLogger.commitCurrentLogUnitAsWord(scrubbedWord, Long.MAX_VALUE, isBatchMode);
         researchLogger.mStatistics.recordManualSuggestion(SystemClock.uptimeMillis());
     }
@@ -1329,6 +1345,9 @@
         researchLogger.enqueueEvent(logUnit != null ? logUnit : researchLogger.mCurrentLogUnit,
                 LOGSTATEMENT_LATINIME_REVERTCOMMIT, committedWord, originallyTypedWord,
                 separatorString);
+        if (logUnit != null) {
+            logUnit.setContainsCorrection();
+        }
         researchLogger.mStatistics.recordRevertCommit(SystemClock.uptimeMillis());
         researchLogger.commitCurrentLogUnitAsWord(originallyTypedWord, Long.MAX_VALUE, isBatchMode);
     }
@@ -1475,20 +1494,21 @@
 
     private boolean isExpectingCommitText = false;
     /**
-     * Log a call to RichInputConnection.commitPartialText
+     * Log a call to (UnknownClass).commitPartialText
      *
      * SystemResponse: The IME is committing part of a word.  This happens if a space is
      * automatically inserted to split a single typed string into two or more words.
      */
     // TODO: This method is currently unused.  Find where it should be called from in the IME and
     // add invocations.
-    private static final LogStatement LOGSTATEMENT_LATINIME_COMMIT_PARTIAL_TEXT =
-            new LogStatement("LatinIMECommitPartialText", true, false, "newCursorPosition");
-    public static void latinIME_commitPartialText(final String committedWord,
+    private static final LogStatement LOGSTATEMENT_COMMIT_PARTIAL_TEXT =
+            new LogStatement("CommitPartialText", true, false, "newCursorPosition");
+    public static void commitPartialText(final String committedWord,
             final long lastTimestampOfWordData, final boolean isBatchMode) {
         final ResearchLogger researchLogger = getInstance();
         final String scrubbedWord = scrubDigitsFromString(committedWord);
-        researchLogger.enqueueEvent(LOGSTATEMENT_LATINIME_COMMIT_PARTIAL_TEXT);
+        researchLogger.enqueueEvent(LOGSTATEMENT_COMMIT_PARTIAL_TEXT);
+        researchLogger.mStatistics.recordAutoCorrection(SystemClock.uptimeMillis());
         researchLogger.commitCurrentLogUnitAsWord(scrubbedWord, lastTimestampOfWordData,
                 isBatchMode);
     }
@@ -1729,7 +1749,7 @@
                     "averageTimeDuringRepeatedDelete", "averageTimeAfterDelete",
                     "dictionaryWordCount", "splitWordsCount", "gestureInputCount",
                     "gestureCharsCount", "gesturesDeletedCount", "manualSuggestionsCount",
-                    "revertCommitsCount");
+                    "revertCommitsCount", "correctedWordsCount", "autoCorrectionsCount");
     private static void logStatistics() {
         final ResearchLogger researchLogger = getInstance();
         final Statistics statistics = researchLogger.mStatistics;
@@ -1743,6 +1763,7 @@
                 statistics.mDictionaryWordCount, statistics.mSplitWordsCount,
                 statistics.mGesturesInputCount, statistics.mGesturesCharsCount,
                 statistics.mGesturesDeletedCount, statistics.mManualSuggestionsCount,
-                statistics.mRevertCommitsCount);
+                statistics.mRevertCommitsCount, statistics.mCorrectedWordsCount,
+                statistics.mAutoCorrectionsCount);
     }
 }
diff --git a/java/src/com/android/inputmethod/research/Statistics.java b/java/src/com/android/inputmethod/research/Statistics.java
index a920265..f0cb157 100644
--- a/java/src/com/android/inputmethod/research/Statistics.java
+++ b/java/src/com/android/inputmethod/research/Statistics.java
@@ -25,6 +25,7 @@
     private static final String TAG = Statistics.class.getSimpleName();
     private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
 
+    // TODO: Cleanup comments to only including those giving meaningful information.
     // Number of characters entered during a typing session
     int mCharCount;
     // Number of letter characters entered during a typing session
@@ -41,6 +42,8 @@
     int mDictionaryWordCount;
     // Number of words split and spaces automatically entered.
     int mSplitWordsCount;
+    // Number of words entered during a session.
+    int mCorrectedWordsCount;
     // Number of gestures that were input.
     int mGesturesInputCount;
     // Number of gestures that were deleted.
@@ -49,6 +52,8 @@
     int mGesturesCharsCount;
     // Number of manual suggestions chosen.
     int mManualSuggestionsCount;
+    // Number of times that autocorrection was invoked.
+    int mAutoCorrectionsCount;
     // Number of times a commit was reverted in this session.
     int mRevertCommitsCount;
     // Whether the text field was empty upon editing
@@ -113,10 +118,12 @@
         mWordCount = 0;
         mDictionaryWordCount = 0;
         mSplitWordsCount = 0;
+        mCorrectedWordsCount = 0;
         mGesturesInputCount = 0;
         mGesturesDeletedCount = 0;
         mManualSuggestionsCount = 0;
         mRevertCommitsCount = 0;
+        mAutoCorrectionsCount = 0;
         mIsEmptyUponStarting = true;
         mIsEmptinessStateKnown = false;
         mKeyCounter.reset();
@@ -152,11 +159,15 @@
         }
     }
 
-    public void recordWordEntered(final boolean isDictionaryWord) {
+    public void recordWordEntered(final boolean isDictionaryWord,
+            final boolean containsCorrection) {
         mWordCount++;
         if (isDictionaryWord) {
             mDictionaryWordCount++;
         }
+        if (containsCorrection) {
+            mCorrectedWordsCount++;
+        }
     }
 
     public void recordSplitWords() {
@@ -184,6 +195,11 @@
         recordUserAction(time, false /* isDeletion */);
     }
 
+    public void recordAutoCorrection(final long time) {
+        mAutoCorrectionsCount++;
+        recordUserAction(time, false /* isDeletion */);
+    }
+
     public void recordRevertCommit(final long time) {
         mRevertCommitsCount++;
         recordUserAction(time, true /* isDeletion */);