Merge "Refactor most probable string"
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index bece719..61d3874 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -1013,7 +1013,7 @@
     public void closing() {
         dismissAllKeyPreviews();
         cancelAllMessages();
-
+        onCancelMoreKeysPanel();
         mInvalidateAllKeys = true;
         requestLayout();
     }
@@ -1031,11 +1031,11 @@
         return (mMoreKeysPanel != null);
     }
 
-    public boolean dismissMoreKeysPanel() {
+    @Override
+    public void onCancelMoreKeysPanel() {
         if (isShowingMoreKeysPanel()) {
-            return mMoreKeysPanel.dismissMoreKeysPanel();
+            mMoreKeysPanel.dismissMoreKeysPanel();
         }
-        return false;
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
index 767297a..584d2fe 100644
--- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
@@ -844,11 +844,17 @@
     @Override
     public void closing() {
         super.closing();
-        dismissMoreKeysPanel();
+        onCancelMoreKeysPanel();
         mMoreKeysPanelCache.clear();
     }
 
     @Override
+    public void onCancelMoreKeysPanel() {
+        super.onCancelMoreKeysPanel();
+        PointerTracker.dismissAllMoreKeysPanels();
+    }
+
+    @Override
     public boolean onDismissMoreKeysPanel() {
         dimEntireKeyboard(false /* dimmed */);
         return super.onDismissMoreKeysPanel();
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
index d7186d3..8a5b7da 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
@@ -120,7 +120,15 @@
 
     @Override
     public void onMoveEvent(int x, int y, final int pointerId, long eventTime) {
+        if (mActivePointerId != pointerId) {
+            return;
+        }
+        final boolean hasOldKey = (mCurrentKey != null);
         onMoveKeyInternal(x, y, pointerId);
+        if (hasOldKey && mCurrentKey == null) {
+            // If the pointer has moved too far away from any target then cancel the panel.
+            mController.onCancelMoreKeysPanel();
+        }
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysPanel.java b/java/src/com/android/inputmethod/keyboard/MoreKeysPanel.java
index 8f43c9c..9c677e5 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysPanel.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysPanel.java
@@ -30,6 +30,11 @@
          * Remove the current {@link MoreKeysPanel} from the target view.
          */
         public boolean onDismissMoreKeysPanel();
+
+        /**
+         * Instructs the parent to cancel the panel (e.g., when entering a different input mode).
+         */
+        public void onCancelMoreKeysPanel();
     }
 
     /**
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index 7d91aed..0f55607 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -412,6 +412,17 @@
         }
     }
 
+    public static void dismissAllMoreKeysPanels() {
+        final int trackersSize = sTrackers.size();
+        for (int i = 0; i < trackersSize; ++i) {
+            final PointerTracker tracker = sTrackers.get(i);
+            if (tracker.isShowingMoreKeysPanel()) {
+                tracker.mMoreKeysPanel.dismissMoreKeysPanel();
+                tracker.mMoreKeysPanel = null;
+            }
+        }
+    }
+
     private PointerTracker(final int id, final KeyEventHandler handler) {
         if (handler == null) {
             throw new NullPointerException();
@@ -715,6 +726,7 @@
             sLastRecognitionPointSize = 0;
             sLastRecognitionTime = 0;
             mListener.onStartBatchInput();
+            dismissAllMoreKeysPanels();
         }
         mTimerProxy.cancelLongPressTimer();
         mDrawingProxy.showGesturePreviewTrail(this, isOldestTrackerInQueue(this));
@@ -846,7 +858,7 @@
         }
         // A gesture should start only from a non-modifier key.
         mIsDetectingGesture = (mKeyboard != null) && mKeyboard.mId.isAlphabetKeyboard()
-                && !isShowingMoreKeysPanel() && key != null && !key.isModifier();
+                && key != null && !key.isModifier();
         if (mIsDetectingGesture) {
             if (getActivePointerTrackerCount() == 1) {
                 sGestureFirstDownTime = eventTime;
@@ -907,6 +919,11 @@
                 cancelBatchInput();
                 return;
             }
+            // If the MoreKeysPanel is showing then do not attempt to enter gesture mode. However,
+            // the gestured touch points are still being recorded in case the panel is dismissed.
+            if (isShowingMoreKeysPanel()) {
+                return;
+            }
             mayStartBatchInput(key);
             if (sInGesture) {
                 mayUpdateBatchInput(eventTime, key);
@@ -926,7 +943,6 @@
             final int translatedX = mMoreKeysPanel.translateX(x);
             final int translatedY = mMoreKeysPanel.translateY(y);
             mMoreKeysPanel.onMoveEvent(translatedX, translatedY, mPointerId, eventTime);
-            return;
         }
 
         if (sShouldHandleGesture && me != null) {
@@ -941,6 +957,11 @@
                         false /* isMajorEvent */, null);
             }
         }
+
+        if (isShowingMoreKeysPanel()) {
+            // Do not handle sliding keys (or show key pop-ups) when the MoreKeysPanel is visible.
+            return;
+        }
         onMoveEventInternal(x, y, eventTime);
     }
 
@@ -1199,8 +1220,10 @@
         mTimerProxy.cancelKeyTimers();
         setReleasedKeyGraphics(mCurrentKey);
         resetSlidingKeyInput();
-        mMoreKeysPanel.dismissMoreKeysPanel();
-        mMoreKeysPanel = null;
+        if (isShowingMoreKeysPanel()) {
+            mMoreKeysPanel.dismissMoreKeysPanel();
+            mMoreKeysPanel = null;
+        }
     }
 
     private void startRepeatKey(final Key key) {
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index dcbbfca..6a19800 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -149,6 +149,8 @@
     private boolean mIsUserDictionaryAvailable;
 
     private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
+    private PositionalInfoForUserDictPendingAddition
+            mPositionalInfoForUserDictPendingAddition = null;
     private final WordComposer mWordComposer = new WordComposer();
     private RichInputConnection mConnection = new RichInputConnection(this);
 
@@ -779,6 +781,19 @@
         mainKeyboardView.setGesturePreviewMode(mCurrentSettings.mGesturePreviewTrailEnabled,
                 mCurrentSettings.mGestureFloatingPreviewTextEnabled);
 
+        // If we have a user dictionary addition in progress, we should check now if we should
+        // replace the previously committed string with the word that has actually been added
+        // to the user dictionary.
+        if (null != mPositionalInfoForUserDictPendingAddition
+                && mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord(
+                        mConnection, editorInfo, mLastSelectionEnd)) {
+            mPositionalInfoForUserDictPendingAddition = null;
+        }
+        // If tryReplaceWithActualWord returns false, we don't know what word was
+        // added to the user dictionary yet, so we keep the data and defer processing. The word will
+        // be replaced when the user dictionary reports back with the actual word, which ends
+        // up calling #onWordAddedToUserDictionary() in this class.
+
         if (TRACE) Debug.startMethodTracing("/data/trace/latinime");
     }
 
@@ -824,6 +839,7 @@
         }
         // Remove pending messages related to update suggestions
         mHandler.cancelUpdateSuggestionStrip();
+        resetComposingState(true /* alsoResetLastComposedWord */);
     }
 
     @Override
@@ -1209,9 +1225,31 @@
     // Callback for the {@link SuggestionStripView}, to call when the "add to dictionary" hint is
     // pressed.
     @Override
-    public boolean addWordToUserDictionary(final String word) {
+    public void addWordToUserDictionary(final String word) {
+        if (TextUtils.isEmpty(word)) {
+            // Probably never supposed to happen, but just in case.
+            mPositionalInfoForUserDictPendingAddition = null;
+            return;
+        }
+        mPositionalInfoForUserDictPendingAddition =
+                new PositionalInfoForUserDictPendingAddition(
+                        word, mLastSelectionEnd, getCurrentInputEditorInfo());
         mUserDictionary.addWordToUserDictionary(word, 128);
-        return true;
+    }
+
+    public void onWordAddedToUserDictionary(final String newSpelling) {
+        // If word was added but not by us, bail out
+        if (null == mPositionalInfoForUserDictPendingAddition) return;
+        if (mWordComposer.isComposingWord()) {
+            // We are late... give up and return
+            mPositionalInfoForUserDictPendingAddition = null;
+            return;
+        }
+        mPositionalInfoForUserDictPendingAddition.setActualWordBeingAdded(newSpelling);
+        if (mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord(
+                mConnection, getCurrentInputEditorInfo(), mLastSelectionEnd)) {
+            mPositionalInfoForUserDictPendingAddition = null;
+        }
     }
 
     private static boolean isAlphabet(final int code) {
diff --git a/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java
new file mode 100644
index 0000000..8a2d222
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import android.view.inputmethod.EditorInfo;
+
+/**
+ * Holder class for data about a word already committed but that may still be edited.
+ *
+ * When the user chooses to add a word to the user dictionary by pressing the appropriate
+ * suggestion, a dialog is presented to give a chance to edit the word before it is actually
+ * registered as a user dictionary word. If the word is actually modified, the IME needs to
+ * go back and replace the word that was committed with the amended version.
+ * The word we need to replace with will only be known after it's actually committed, so
+ * the IME needs to take a note of what it has to replace and where it is.
+ * This class encapsulates this data.
+ */
+public class PositionalInfoForUserDictPendingAddition {
+    final private String mOriginalWord;
+    final private int mCursorPos; // Position of the cursor after the word
+    final private EditorInfo mEditorInfo; // On what binding this has been added
+    private String mActualWordBeingAdded;
+
+    public PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos,
+            final EditorInfo editorInfo) {
+        mOriginalWord = word;
+        mCursorPos = cursorPos;
+        mEditorInfo = editorInfo;
+    }
+
+    public void setActualWordBeingAdded(final String actualWordBeingAdded) {
+        mActualWordBeingAdded = actualWordBeingAdded;
+    }
+
+    /**
+     * Try to replace the string at the remembered position with the actual word being added.
+     *
+     * After the user validated the word being added, the IME has to replace the old version
+     * (which has been committed in the text view) with the amended version if it's different.
+     * This method tries to do that, but may fail because the IME is not yet ready to do so -
+     * for example, it is still waiting for the new string, or it is waiting to return to the text
+     * view in which the amendment should be made. In these cases, we should keep the data
+     * and wait until all conditions are met.
+     * This method returns true if the replacement has been successfully made and this data
+     * can be forgotten; it returns false if the replacement can't be made yet and we need to
+     * keep this until a later time.
+     * The IME knows about the actual word being added through a callback called by the
+     * user dictionary facility of the device. When this callback comes, the keyboard may still
+     * be connected to the edition dialog, or it may have already returned to the original text
+     * field. Replacement has to work in both cases.
+     * Accordingly, this method is called at two different points in time : upon getting the
+     * event that a new word was added to the user dictionary, and upon starting up in a
+     * new text field.
+     * @param connection The RichInputConnection through which to contact the editor.
+     * @param editorInfo Information pertaining to the editor we are currently in.
+     * @param currentCursorPosition The current cursor position, for checking purposes.
+     * @return true if the edit has been successfully made, false if we need to try again later
+     */
+    public boolean tryReplaceWithActualWord(final RichInputConnection connection,
+            final EditorInfo editorInfo, final int currentCursorPosition) {
+        // If we still don't know the actual word being added, we need to try again later.
+        if (null == mActualWordBeingAdded) return false;
+        // The entered text and the registered text were the same anyway : we can
+        // return success right away even if focus has not returned yet to the text field we
+        // want to amend.
+        if (mActualWordBeingAdded.equals(mOriginalWord)) return true;
+        // Not the same text field : we need to try again later. This happens when the addition
+        // is reported by the user dictionary provider before the focus has moved back to the
+        // original text view, so the IME is still in the text view of the dialog and has no way to
+        // edit the original text view at this time.
+        if (!mEditorInfo.packageName.equals(editorInfo.packageName)
+                || mEditorInfo.fieldId != editorInfo.fieldId) {
+            return false;
+        }
+        // Same text field, but not the same cursor position : we give up, so we return success
+        // so that it won't be tried again
+        if (currentCursorPosition != mCursorPos) return true;
+        // We have made all the checks : do the replacement and report success
+        connection.setComposingRegion(currentCursorPosition - mOriginalWord.length(),
+                currentCursorPosition);
+        connection.commitText(mActualWordBeingAdded, mActualWordBeingAdded.length());
+        return true;
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 8612746..d1d9206 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -331,6 +331,24 @@
         }
     }
 
+    public void setComposingRegion(final int start, final int end) {
+        if (DEBUG_BATCH_NESTING) checkBatchEdit();
+        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
+        mCurrentCursorPosition = end;
+        final CharSequence textBeforeCursor =
+                getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE + (end - start), 0);
+        final int indexOfStartOfComposingText =
+                Math.max(textBeforeCursor.length() - (end - start), 0);
+        mComposingText.append(textBeforeCursor.subSequence(indexOfStartOfComposingText,
+                textBeforeCursor.length()));
+        mCommittedTextBeforeComposingText.setLength(0);
+        mCommittedTextBeforeComposingText.append(
+                textBeforeCursor.subSequence(0, indexOfStartOfComposingText));
+        if (null != mIC) {
+            mIC.setComposingRegion(start, end);
+        }
+    }
+
     public void setComposingText(final CharSequence text, final int i) {
         if (DEBUG_BATCH_NESTING) checkBatchEdit();
         if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
index 00c3cbe..ddae5ac 100644
--- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
@@ -18,10 +18,12 @@
 
 import android.content.ContentProviderClient;
 import android.content.ContentResolver;
+import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
 import android.database.ContentObserver;
 import android.database.Cursor;
+import android.net.Uri;
 import android.provider.UserDictionary.Words;
 import android.text.TextUtils;
 
@@ -87,8 +89,25 @@
 
         mObserver = new ContentObserver(null) {
             @Override
-            public void onChange(boolean self) {
+            public void onChange(final boolean self) {
+                // This hook is deprecated as of API level 16, but should still be supported for
+                // cases where the IME is running on an older version of the platform.
+                onChange(self, null);
+            }
+            // The following hook is only available as of API level 16, and as such it will only
+            // work on JellyBean+ devices. On older versions of the platform, the hook
+            // above will be called instead.
+            @Override
+            public void onChange(final boolean self, final Uri uri) {
                 setRequiresReload(true);
+                // We want to report back to Latin IME in case the user just entered the word.
+                // If the user changed the word in the dialog box, then we want to replace
+                // what was entered in the text field.
+                if (null == uri || !(context instanceof LatinIME)) return;
+                final long changedRowId = ContentUris.parseId(uri);
+                if (-1 == changedRowId) return; // Unknown content... Not sure why we're here
+                final String changedWord = getChangedWordForUri(uri);
+                ((LatinIME)context).onWordAddedToUserDictionary(changedWord);
             }
         };
         cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
@@ -96,6 +115,19 @@
         loadDictionary();
     }
 
+    private String getChangedWordForUri(final Uri uri) {
+        final Cursor cursor = mContext.getContentResolver().query(uri,
+                PROJECTION_QUERY, null, null, null);
+        if (cursor == null) return null;
+        try {
+            if (!cursor.moveToFirst()) return null;
+            final int indexWord = cursor.getColumnIndex(Words.WORD);
+            return cursor.getString(indexWord);
+        } finally {
+            cursor.close();
+        }
+    }
+
     @Override
     public synchronized void close() {
         if (mObserver != null) {
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
index 1888912..14bb95b 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -71,7 +71,7 @@
 public final class SuggestionStripView extends RelativeLayout implements OnClickListener,
         OnLongClickListener {
     public interface Listener {
-        public boolean addWordToUserDictionary(String word);
+        public void addWordToUserDictionary(String word);
         public void pickSuggestionManually(int index, String word);
     }
 
@@ -684,6 +684,11 @@
         public void onShowMoreKeysPanel(MoreKeysPanel panel) {
             mKeyboardView.onShowMoreKeysPanel(panel);
         }
+
+        @Override
+        public void onCancelMoreKeysPanel() {
+            dismissMoreSuggestions();
+        }
     };
 
     boolean dismissMoreSuggestions() {
diff --git a/java/src/com/android/inputmethod/research/LogUnit.java b/java/src/com/android/inputmethod/research/LogUnit.java
index d8b3a29..0aec80a 100644
--- a/java/src/com/android/inputmethod/research/LogUnit.java
+++ b/java/src/com/android/inputmethod/research/LogUnit.java
@@ -18,7 +18,7 @@
 
 import com.android.inputmethod.latin.CollectionUtils;
 
-import java.util.ArrayList;
+import java.util.List;
 
 /**
  * A group of log statements related to each other.
@@ -35,16 +35,39 @@
  * been published recently, or whether the LogUnit contains numbers, etc.
  */
 /* package */ class LogUnit {
-    private final ArrayList<String[]> mKeysList = CollectionUtils.newArrayList();
-    private final ArrayList<Object[]> mValuesList = CollectionUtils.newArrayList();
-    private final ArrayList<Boolean> mIsPotentiallyPrivate = CollectionUtils.newArrayList();
+    private final List<String[]> mKeysList;
+    private final List<Object[]> mValuesList;
+    // Assume that mTimeList is sorted in increasing order.  Do not insert null values into
+    // mTimeList.
+    private final List<Long> mTimeList;
+    private final List<Boolean> mIsPotentiallyPrivate;
     private String mWord;
-    private boolean mContainsDigit;
+    private boolean mMayContainDigit;
 
+    public LogUnit() {
+        mKeysList = CollectionUtils.newArrayList();
+        mValuesList = CollectionUtils.newArrayList();
+        mTimeList = CollectionUtils.newArrayList();
+        mIsPotentiallyPrivate = CollectionUtils.newArrayList();
+    }
+
+    private LogUnit(final List<String[]> keysList, final List<Object[]> valuesList,
+            final List<Long> timeList, final List<Boolean> isPotentiallyPrivate) {
+        mKeysList = keysList;
+        mValuesList = valuesList;
+        mTimeList = timeList;
+        mIsPotentiallyPrivate = isPotentiallyPrivate;
+    }
+
+    /**
+     * Adds a new log statement.  The time parameter in successive calls to this method must be
+     * monotonically increasing, or splitByTime() will not work.
+     */
     public void addLogStatement(final String[] keys, final Object[] values,
-            final Boolean isPotentiallyPrivate) {
+            final long time, final boolean isPotentiallyPrivate) {
         mKeysList.add(keys);
         mValuesList.add(values);
+        mTimeList.add(time);
         mIsPotentiallyPrivate.add(isPotentiallyPrivate);
     }
 
@@ -52,7 +75,7 @@
         final int size = mKeysList.size();
         for (int i = 0; i < size; i++) {
             if (!mIsPotentiallyPrivate.get(i) || isIncludingPrivateData) {
-                researchLog.outputEvent(mKeysList.get(i), mValuesList.get(i));
+                researchLog.outputEvent(mKeysList.get(i), mValuesList.get(i), mTimeList.get(i));
             }
         }
     }
@@ -69,15 +92,37 @@
         return mWord != null;
     }
 
-    public void setContainsDigit() {
-        mContainsDigit = true;
+    public void setMayContainDigit() {
+        mMayContainDigit = true;
     }
 
-    public boolean hasDigit() {
-        return mContainsDigit;
+    public boolean mayContainDigit() {
+        return mMayContainDigit;
     }
 
     public boolean isEmpty() {
         return mKeysList.isEmpty();
     }
+
+    /**
+     * Split this logUnit, with all events before maxTime staying in the current logUnit, and all
+     * events after maxTime going into a new LogUnit that is returned.
+     */
+    public LogUnit splitByTime(final long maxTime) {
+        // Assume that mTimeList is in sorted order.
+        final int length = mTimeList.size();
+        for (int index = 0; index < length; index++) {
+            if (mTimeList.get(index) >= maxTime) {
+                final LogUnit newLogUnit = new LogUnit(
+                        mKeysList.subList(index, length),
+                        mValuesList.subList(index, length),
+                        mTimeList.subList(index, length),
+                        mIsPotentiallyPrivate.subList(index, length));
+                newLogUnit.mWord = null;
+                newLogUnit.mMayContainDigit = mMayContainDigit;
+                return newLogUnit;
+            }
+        }
+        return new LogUnit();
+    }
 }
diff --git a/java/src/com/android/inputmethod/research/MainLogBuffer.java b/java/src/com/android/inputmethod/research/MainLogBuffer.java
index 94dbf39..f665e59 100644
--- a/java/src/com/android/inputmethod/research/MainLogBuffer.java
+++ b/java/src/com/android/inputmethod/research/MainLogBuffer.java
@@ -113,7 +113,7 @@
             final String word = logUnit.getWord();
             if (word == null) {
                 // Digits outside words are a privacy threat.
-                if (logUnit.hasDigit()) {
+                if (logUnit.mayContainDigit()) {
                     return false;
                 }
             } else {
diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java
index 70c38e9..96dac55 100644
--- a/java/src/com/android/inputmethod/research/ResearchLog.java
+++ b/java/src/com/android/inputmethod/research/ResearchLog.java
@@ -207,7 +207,7 @@
     private static final String UPTIME_KEY = "_ut";
     private static final String EVENT_TYPE_KEY = "_ty";
 
-    void outputEvent(final String[] keys, final Object[] values) {
+    void outputEvent(final String[] keys, final Object[] values, final long time) {
         // Not thread safe.
         if (keys.length == 0) {
             return;
@@ -225,7 +225,7 @@
             }
             mJsonWriter.beginObject();
             mJsonWriter.name(CURRENT_TIME_KEY).value(System.currentTimeMillis());
-            mJsonWriter.name(UPTIME_KEY).value(SystemClock.uptimeMillis());
+            mJsonWriter.name(UPTIME_KEY).value(time);
             mJsonWriter.name(EVENT_TYPE_KEY).value(keys[0]);
             final int length = values.length;
             for (int i = 0; i < length; i++) {
diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java
index 982d104..2657da2 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -377,7 +377,7 @@
             Log.d(TAG, "stop called");
         }
         logStatistics();
-        commitCurrentLogUnit();
+        commitCurrentLogUnit(SystemClock.uptimeMillis());
 
         if (mMainLogBuffer != null) {
             publishLogBuffer(mMainLogBuffer, mMainResearchLog, false /* isIncludingPrivateData */);
@@ -530,7 +530,7 @@
             return;
         }
         if (includeHistory) {
-            commitCurrentLogUnit();
+            commitCurrentLogUnit(SystemClock.uptimeMillis());
         } else {
             mFeedbackLogBuffer.clear();
         }
@@ -539,7 +539,7 @@
             feedbackContents
         };
         feedbackLogUnit.addLogStatement(EVENTKEYS_FEEDBACK, values,
-                false /* isPotentiallyPrivate */);
+                SystemClock.uptimeMillis(), false /* isPotentiallyPrivate */);
         mFeedbackLogBuffer.shiftIn(feedbackLogUnit);
         publishLogBuffer(mFeedbackLogBuffer, mFeedbackLog, true /* isIncludingPrivateData */);
         mFeedbackLog.close(new Runnable() {
@@ -641,12 +641,13 @@
             final Object[] values) {
         assert values.length + 1 == keys.length;
         if (isAllowedToLog()) {
-            mCurrentLogUnit.addLogStatement(keys, values, true /* isPotentiallyPrivate */);
+            final long time = SystemClock.uptimeMillis();
+            mCurrentLogUnit.addLogStatement(keys, values, time, true /* isPotentiallyPrivate */);
         }
     }
 
     private void setCurrentLogUnitContainsDigitFlag() {
-        mCurrentLogUnit.setContainsDigit();
+        mCurrentLogUnit.setMayContainDigit();
     }
 
     /**
@@ -664,16 +665,18 @@
     private synchronized void enqueueEvent(final String[] keys, final Object[] values) {
         assert values.length + 1 == keys.length;
         if (isAllowedToLog()) {
-            mCurrentLogUnit.addLogStatement(keys, values, false /* isPotentiallyPrivate */);
+            final long time = SystemClock.uptimeMillis();
+            mCurrentLogUnit.addLogStatement(keys, values, time, false /* isPotentiallyPrivate */);
         }
     }
 
-    /* package for test */ void commitCurrentLogUnit() {
+    /* package for test */ void commitCurrentLogUnit(final long maxTime) {
         if (DEBUG) {
             Log.d(TAG, "commitCurrentLogUnit" + (mCurrentLogUnit.hasWord() ?
                     ": " + mCurrentLogUnit.getWord() : ""));
         }
         if (!mCurrentLogUnit.isEmpty()) {
+            final LogUnit newLogUnit = mCurrentLogUnit.splitByTime(maxTime);
             if (mMainLogBuffer != null) {
                 mMainLogBuffer.shiftIn(mCurrentLogUnit);
                 if (mMainLogBuffer.isSafeToLog() && mMainResearchLog != null) {
@@ -685,7 +688,7 @@
             if (mFeedbackLogBuffer != null) {
                 mFeedbackLogBuffer.shiftIn(mCurrentLogUnit);
             }
-            mCurrentLogUnit = new LogUnit();
+            mCurrentLogUnit = newLogUnit;
             Log.d(TAG, "commitCurrentLogUnit");
         }
     }
@@ -703,7 +706,7 @@
             isIncludingPrivateData
         };
         openingLogUnit.addLogStatement(EVENTKEYS_LOG_SEGMENT_START, values,
-                false /* isPotentiallyPrivate */);
+                SystemClock.uptimeMillis(), false /* isPotentiallyPrivate */);
         researchLog.publish(openingLogUnit, true /* isIncludingPrivateData */);
         LogUnit logUnit;
         while ((logUnit = logBuffer.shiftOut()) != null) {
@@ -711,7 +714,7 @@
         }
         final LogUnit closingLogUnit = new LogUnit();
         closingLogUnit.addLogStatement(EVENTKEYS_LOG_SEGMENT_END, EVENTKEYS_NULLVALUES,
-                false /* isPotentiallyPrivate */);
+                SystemClock.uptimeMillis(), false /* isPotentiallyPrivate */);
         researchLog.publish(closingLogUnit, true /* isIncludingPrivateData */);
     }
 
@@ -726,13 +729,13 @@
         return false;
     }
 
-    private void onWordComplete(final String word) {
+    private void onWordComplete(final String word, final long maxTime) {
         Log.d(TAG, "onWordComplete: " + word);
         if (word != null && word.length() > 0 && hasLetters(word)) {
             mCurrentLogUnit.setWord(word);
             mStatistics.recordWordEntered();
         }
-        commitCurrentLogUnit();
+        commitCurrentLogUnit(maxTime);
     }
 
     private static int scrubDigitFromCodePoint(int codePoint) {
@@ -943,7 +946,7 @@
             }
             final ResearchLogger researchLogger = getInstance();
             researchLogger.enqueueEvent(EVENTKEYS_LATINIME_ONWINDOWHIDDEN, values);
-            researchLogger.commitCurrentLogUnit();
+            researchLogger.commitCurrentLogUnit(SystemClock.uptimeMillis());
             getInstance().stop();
         }
     }
@@ -1189,7 +1192,8 @@
         final ResearchLogger researchLogger = getInstance();
         researchLogger.enqueuePotentiallyPrivateEvent(EVENTKEYS_RICHINPUTCONNECTION_COMMITTEXT,
                 values);
-        researchLogger.onWordComplete(scrubbedWord);
+        // TODO: Replace Long.MAX_VALUE with timestamp of last data to include
+        researchLogger.onWordComplete(scrubbedWord, Long.MAX_VALUE);
     }
 
     private static final String[] EVENTKEYS_RICHINPUTCONNECTION_DELETESURROUNDINGTEXT = {