Insert logging code

- Add log of auto suggestion
- Add log of cancelling auto suggestion
- Add log of actual number of charactors
- Add log of manually clicking suggestion

Change-Id: I8fc1cef356bf1a98b0676ed171bfb17825e18425
diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java
index 3a199bb..8355d67 100755
--- a/java/src/com/android/inputmethod/latin/CandidateView.java
+++ b/java/src/com/android/inputmethod/latin/CandidateView.java
@@ -167,7 +167,7 @@
                 if (scrollX < 0) {
                     scrollX = 0;
                 }
-                if (distanceX > 0 && scrollX + width > mTotalWidth) {                    
+                if (distanceX > 0 && scrollX + width > mTotalWidth) {
                     scrollX -= (int) distanceX;
                 }
                 mTargetScrollX = scrollX;
@@ -412,7 +412,11 @@
             if (y <= 0) {
                 // Fling up!?
                 if (mSelectedString != null) {
+                    // If there are completions from the application, we don't change the state to
+                    // STATE_PICKED_SUGGESTION
                     if (!mShowingCompletions) {
+                        // This "acceptedSuggestion" will not be counted as a word because
+                        // it will be counted in pickSuggestion instead.
                         TextEntryState.acceptedSuggestion(mSuggestions.get(0),
                                 mSelectedString);
                     }
@@ -447,25 +451,6 @@
         }
         return true;
     }
-    
-    /**
-     * For flick through from keyboard, call this method with the x coordinate of the flick 
-     * gesture.
-     * @param x
-     */
-    public void takeSuggestionAt(float x) {
-        mTouchX = (int) x;
-        // To detect candidate
-        onDraw(null);
-        if (mSelectedString != null) {
-            if (!mShowingCompletions) {
-                TextEntryState.acceptedSuggestion(mSuggestions.get(0), mSelectedString);
-            }
-            mService.pickSuggestionManually(mSelectedIndex, mSelectedString);
-        }
-        invalidate();
-        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_REMOVE_THROUGH_PREVIEW), 200);
-    }
 
     private void hidePreview() {
         mCurrentWordIndex = OUT_OF_BOUNDS;
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 1586a75..dfbde87 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -701,7 +701,7 @@
                 CompletionInfo ci = completions[i];
                 if (ci != null) stringList.add(ci.getText());
             }
-            //CharSequence typedWord = mWord.getTypedWord();
+            // When in fullscreen mode, show completions generated by the application
             setSuggestions(stringList, true, true, true);
             mBestWord = null;
             setCandidatesViewShown(isCandidateStripVisible() || mCompletionOn);
@@ -1526,6 +1526,10 @@
 
         // If this is a punctuation, apply it through the normal key press
         if (suggestion.length() == 1 && isWordSeparator(suggestion.charAt(0))) {
+            // Word separators are suggested before the user inputs something.
+            // So, LatinImeLogger logs suggestion.charAt(0) as a user's input.
+            LatinImeLogger.logOnClickSuggestion(
+                    suggestion.toString(), suggestion.toString(), index);
             onKey(suggestion.charAt(0), null);
             if (ic != null) {
                 ic.endBatchEdit();
@@ -1538,6 +1542,8 @@
         if (index == 0) {
             checkAddToDictionary(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED);
         }
+        LatinImeLogger.logOnClickSuggestion(
+                mComposing.toString(), suggestion.toString(), index);
         TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion);
         // Follow it with a space
         if (mAutoSpace) {
diff --git a/java/src/com/android/inputmethod/latin/LatinImeLogger.java b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
index a04e1cf..c03d1a7 100644
--- a/java/src/com/android/inputmethod/latin/LatinImeLogger.java
+++ b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
@@ -41,11 +41,16 @@
     private static final int ID_INPUT_COUNT = 3;
     private static final int ID_DELETE_COUNT = 4;
     private static final int ID_WORD_COUNT = 5;
+    private static final int ID_ACTUAL_CHAR_COUNT = 6;
 
     private static final String PREF_ENABLE_LOG = "enable_log";
 
-    private static LatinImeLogger sLatinImeLogger = new LatinImeLogger();
     public static boolean sLogEnabled = true;
+    private static LatinImeLogger sLatinImeLogger = new LatinImeLogger();
+    // Store the last auto suggested word.
+    // This is required for a cancellation log of auto suggestion of that word.
+    private static String sLastAutoSuggestBefore;
+    private static String sLastAutoSuggestAfter;
 
     private ArrayList<LogEntry> mLogBuffer = null;
     private ArrayList<LogEntry> mPrivacyLogBuffer = null;
@@ -58,6 +63,8 @@
     private int mDeleteCount;
     private int mInputCount;
     private int mWordCount;
+    // ActualCharCount includes all characters that were completed.
+    private int mActualCharCount;
 
     private static class LogEntry implements Comparable<LogEntry> {
         public final int mTag;
@@ -90,7 +97,10 @@
         mLastTimeCountEntry = mLastTimeSend;
         mDeleteCount = 0;
         mInputCount = 0;
+        mWordCount = 0;
+        mActualCharCount = 0;
         mLogBuffer = new ArrayList<LogEntry>();
+        mPrivacyLogBuffer = new ArrayList<LogEntry>();
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
         sLogEnabled = prefs.getBoolean(PREF_ENABLE_LOG, DEFAULT_LOG_ENABLED);
         prefs.registerOnSharedPreferenceChangeListener(this);
@@ -102,7 +112,10 @@
     private void reset() {
         mDeleteCount = 0;
         mInputCount = 0;
+        mWordCount = 0;
+        mActualCharCount = 0;
         mLogBuffer.clear();
+        mPrivacyLogBuffer.clear();
     }
 
     /**
@@ -134,9 +147,12 @@
                 new String[] {String.valueOf(mInputCount)}));
         mLogBuffer.add(new LogEntry (time, ID_WORD_COUNT,
                 new String[] {String.valueOf(mWordCount)}));
+        mLogBuffer.add(new LogEntry (time, ID_ACTUAL_CHAR_COUNT,
+                new String[] {String.valueOf(mActualCharCount)}));
         mDeleteCount = 0;
         mInputCount = 0;
         mWordCount = 0;
+        mActualCharCount = 0;
     }
 
     private void flushPrivacyLogSafely() {
@@ -174,6 +190,7 @@
             case ID_AUTOSUGGESTION:
                 ++mWordCount;
                 String[] dataStrings = (String[]) data;
+                mActualCharCount += dataStrings[1].length();
                 if (checkStringsDataSafe(dataStrings)) {
                     mPrivacyLogBuffer.add(
                             new LogEntry (System.currentTimeMillis(), tag, dataStrings));
@@ -186,6 +203,7 @@
             case ID_AUTOSUGGESTIONCANCELED:
                 --mWordCount;
                 dataStrings = (String[]) data;
+                mActualCharCount -= dataStrings[1].length();
                 if (checkStringsDataSafe(dataStrings)) {
                     mPrivacyLogBuffer.add(
                             new LogEntry (System.currentTimeMillis(), tag, dataStrings));
@@ -215,7 +233,7 @@
         mLastTimeSend = System.currentTimeMillis();
     }
 
-    private void sendLogToDropBox(int tag, Object s) {
+    private synchronized void sendLogToDropBox(int tag, Object s) {
         long now = System.currentTimeMillis();
         if (DBG) {
             Log.d(TAG, "SendLog: " + tag + ";" + s + ","
@@ -255,6 +273,7 @@
         }
     }
 
+    // TODO: Handle CharSequence instead of String
     public static void logOnClickSuggestion(String before, String after, int position) {
         if (sLogEnabled) {
             String[] strings = new String[] {before, after, String.valueOf(position)};
@@ -265,14 +284,22 @@
     public static void logOnAutoSuggestion(String before, String after) {
         if (sLogEnabled) {
             String[] strings = new String[] {before, after};
-            sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTION, strings);
+            synchronized (sLastAutoSuggestBefore) {
+                sLastAutoSuggestBefore = before;
+            }
+            synchronized (sLastAutoSuggestAfter) {
+                sLastAutoSuggestAfter = after;
+            }
+            sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTIONCANCELED, strings);
         }
     }
 
-    public static void logOnAutoSuggestionCanceled(String before, String after) {
+    public static void logOnAutoSuggestionCanceled() {
         if (sLogEnabled) {
-            String[] strings = new String[] {before, after};
-            sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTIONCANCELED, strings);
+            if (sLastAutoSuggestBefore != null && sLastAutoSuggestAfter != null) {
+                String[] strings = new String[] {sLastAutoSuggestBefore, sLastAutoSuggestAfter};
+                sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTION, strings);
+            }
         }
     }
 
@@ -296,7 +323,8 @@
             sb.append(SEPARATER);
         }
 
-        private static void appendLogEntry(StringBuffer sb, String time, String tag, String[] data) {
+        private static void appendLogEntry(StringBuffer sb, String time, String tag,
+                String[] data) {
             if (data.length > 0) {
                 appendWithLength(sb, String.valueOf(data.length + 2));
                 appendWithLength(sb, time);
diff --git a/java/src/com/android/inputmethod/latin/TextEntryState.java b/java/src/com/android/inputmethod/latin/TextEntryState.java
index d056ceb..d291af6 100644
--- a/java/src/com/android/inputmethod/latin/TextEntryState.java
+++ b/java/src/com/android/inputmethod/latin/TextEntryState.java
@@ -130,6 +130,7 @@
         sTypedChars += typedWord.length();
         sActualChars += actualWord.length();
         sState = STATE_ACCEPTED_DEFAULT;
+        LatinImeLogger.logOnAutoSuggestion(typedWord.toString(), actualWord.toString());
     }
     
     public static void acceptedTyped(CharSequence typedWord) {
@@ -199,6 +200,7 @@
         if (sState == STATE_ACCEPTED_DEFAULT) {
             sState = STATE_UNDO_COMMIT;
             sAutoSuggestUndoneCount++;
+            LatinImeLogger.logOnAutoSuggestionCanceled();
         } else if (sState == STATE_UNDO_COMMIT) {
             sState = STATE_IN_WORD;
         }