merge in jb-mr1-release history after reset to jb-mr1-dev
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index ef6be3e..f7c993d 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -138,11 +138,11 @@
                 android:persistent="true"
                 android:defaultValue="true" />
             <CheckBoxPreference
-                android:key="pref_gesture_floating_preview_text"
+                android:key="pref_show_gesture_floating_preview_text"
                 android:title="@string/gesture_floating_preview_text"
                 android:summary="@string/gesture_floating_preview_text_summary"
                 android:persistent="true"
-                android:defaultValue="true" />
+                android:defaultValue="false" />
         </PreferenceScreen>
     </PreferenceCategory>
 </PreferenceScreen>
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index a6439c4..2bde8d2 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -23,7 +23,7 @@
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
 import com.android.inputmethod.keyboard.internal.GestureStroke;
-import com.android.inputmethod.keyboard.internal.GestureStrokeWithPreviewTrail;
+import com.android.inputmethod.keyboard.internal.GestureStrokeWithPreviewPoints;
 import com.android.inputmethod.keyboard.internal.PointerTrackerQueue;
 import com.android.inputmethod.latin.CollectionUtils;
 import com.android.inputmethod.latin.InputPointers;
@@ -208,7 +208,7 @@
     private static final KeyboardActionListener EMPTY_LISTENER =
             new KeyboardActionListener.Adapter();
 
-    private final GestureStrokeWithPreviewTrail mGestureStrokeWithPreviewTrail;
+    private final GestureStrokeWithPreviewPoints mGestureStrokeWithPreviewPoints;
 
     public static void init(boolean hasDistinctMultitouch,
             boolean needsPhantomSuddenMoveEventHack) {
@@ -293,7 +293,7 @@
             throw new NullPointerException();
         }
         mPointerId = id;
-        mGestureStrokeWithPreviewTrail = new GestureStrokeWithPreviewTrail(id);
+        mGestureStrokeWithPreviewPoints = new GestureStrokeWithPreviewPoints(id);
         setKeyDetectorInner(handler.getKeyDetector());
         mListener = handler.getKeyboardActionListener();
         mDrawingProxy = handler.getDrawingProxy();
@@ -392,7 +392,7 @@
     private void setKeyDetectorInner(final KeyDetector keyDetector) {
         mKeyDetector = keyDetector;
         mKeyboard = keyDetector.getKeyboard();
-        mGestureStrokeWithPreviewTrail.setGestureSampleLength(mKeyboard.mMostCommonKeyWidth);
+        mGestureStrokeWithPreviewPoints.setKeyboardGeometry(mKeyboard.mMostCommonKeyWidth);
         final Key newKey = mKeyDetector.detectHitKey(mKeyX, mKeyY);
         if (newKey != mCurrentKey) {
             if (mDrawingProxy != null) {
@@ -502,8 +502,8 @@
         mDrawingProxy.invalidateKey(key);
     }
 
-    public GestureStrokeWithPreviewTrail getGestureStrokeWithPreviewTrail() {
-        return mGestureStrokeWithPreviewTrail;
+    public GestureStrokeWithPreviewPoints getGestureStrokeWithPreviewPoints() {
+        return mGestureStrokeWithPreviewPoints;
     }
 
     public int getLastX() {
@@ -544,8 +544,8 @@
         return (sPointerTrackerQueue == null) ? 1 : sPointerTrackerQueue.size();
     }
 
-    private void startBatchInput() {
-        if (sInGesture || !mGestureStrokeWithPreviewTrail.isStartOfAGesture()) {
+    private void mayStartBatchInput() {
+        if (sInGesture || !mGestureStrokeWithPreviewPoints.isStartOfAGesture()) {
             return;
         }
         if (DEBUG_LISTENER) {
@@ -559,7 +559,7 @@
 
     private void updateBatchInput(final long eventTime) {
         synchronized (sAggregratedPointers) {
-            mGestureStrokeWithPreviewTrail.appendIncrementalBatchPoints(sAggregratedPointers);
+            mGestureStrokeWithPreviewPoints.appendIncrementalBatchPoints(sAggregratedPointers);
             final int size = sAggregratedPointers.getPointerSize();
             if (size > sLastRecognitionPointSize
                     && eventTime > sLastRecognitionTime + MIN_GESTURE_RECOGNITION_TIME) {
@@ -575,10 +575,10 @@
         mDrawingProxy.showGesturePreviewTrail(this, isOldestTracker);
     }
 
-    private void endBatchInput() {
+    private void mayEndBatchInput() {
         synchronized (sAggregratedPointers) {
-            mGestureStrokeWithPreviewTrail.appendAllBatchPoints(sAggregratedPointers);
-            mGestureStrokeWithPreviewTrail.reset();
+            mGestureStrokeWithPreviewPoints.appendAllBatchPoints(sAggregratedPointers);
+            mGestureStrokeWithPreviewPoints.reset();
             if (getActivePointerTrackerCount() == 1) {
                 if (DEBUG_LISTENER) {
                     Log.d(TAG, "onEndBatchInput: batchPoints="
@@ -601,7 +601,7 @@
         final int trackersSize = sTrackers.size();
         for (int i = 0; i < trackersSize; ++i) {
             final PointerTracker tracker = sTrackers.get(i);
-            tracker.mGestureStrokeWithPreviewTrail.reset();
+            tracker.mGestureStrokeWithPreviewPoints.reset();
         }
         sAggregratedPointers.reset();
         sLastRecognitionPointSize = 0;
@@ -678,18 +678,21 @@
                     && mKeyboard.mId.isAlphabetKeyboard();
             if (isAlphabetKeyboard && !mIsShowingMoreKeysPanel && key != null
                     && Keyboard.isLetterCode(key.mCode)) {
-                mIsDetectingGesture = true;
                 sGestureFirstDownTime = eventTime;
-                mGestureStrokeWithPreviewTrail.addPoint(x, y, 0, false /* isHistorical */);
+                onGestureDownEvent(x, y, eventTime);
             }
         } else if (sInGesture && activePointerTrackerCount > 1) {
-            mIsDetectingGesture = true;
-            final int elapsedTimeFromFirstDown = (int)(eventTime - sGestureFirstDownTime);
-            mGestureStrokeWithPreviewTrail.addPoint(x, y, elapsedTimeFromFirstDown,
-                    false /* isHistorical */);
+            onGestureDownEvent(x, y, eventTime);
         }
     }
 
+    private void onGestureDownEvent(final int x, final int y, final long eventTime) {
+        mIsDetectingGesture = true;
+        final int elapsedTimeFromFirstDown = (int)(eventTime - sGestureFirstDownTime);
+        mGestureStrokeWithPreviewPoints.addPoint(x, y, elapsedTimeFromFirstDown,
+                false /* isHistorical */);
+    }
+
     private void onDownEventInternal(final int x, final int y, final long eventTime) {
         Key key = onDownKey(x, y, eventTime);
         // Sliding key is allowed when 1) enabled by configuration, 2) this pointer starts sliding
@@ -726,8 +729,8 @@
             final boolean isHistorical, final Key key) {
         final int gestureTime = (int)(eventTime - sGestureFirstDownTime);
         if (mIsDetectingGesture) {
-            mGestureStrokeWithPreviewTrail.addPoint(x, y, gestureTime, isHistorical);
-            startBatchInput();
+            mGestureStrokeWithPreviewPoints.addPoint(x, y, gestureTime, isHistorical);
+            mayStartBatchInput();
             if (sInGesture && key != null) {
                 updateBatchInput(eventTime);
             }
@@ -919,7 +922,7 @@
             if (currentKey != null) {
                 callListenerOnRelease(currentKey, currentKey.mCode, true);
             }
-            endBatchInput();
+            mayEndBatchInput();
             return;
         }
         // This event will be recognized as a regular code input. Clear unused possible batch points
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java
index 4311fa7..699aaea 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java
@@ -17,7 +17,9 @@
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Paint;
+import android.graphics.Path;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.os.SystemClock;
 
 import com.android.inputmethod.latin.Constants;
@@ -25,7 +27,7 @@
 import com.android.inputmethod.latin.ResizableIntArray;
 
 final class GesturePreviewTrail {
-    private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewTrail.PREVIEW_CAPACITY;
+    private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewPoints.PREVIEW_CAPACITY;
 
     private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
     private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
@@ -78,7 +80,7 @@
                 ? DOWN_EVENT_MARKER - xCoordOrMark : xCoordOrMark;
     }
 
-    public void addStroke(final GestureStrokeWithPreviewTrail stroke, final long downTime) {
+    public void addStroke(final GestureStrokeWithPreviewPoints stroke, final long downTime) {
         final int trailSize = mEventTimes.getLength();
         stroke.appendPreviewStroke(mEventTimes, mXCoordinates, mYCoordinates);
         if (mEventTimes.getLength() == trailSize) {
@@ -116,6 +118,99 @@
                 / params.mTrailLingerDuration, 0.0f);
     }
 
+    static final class WorkingSet {
+        // Input
+        // Previous point (P1) coordinates and trail radius.
+        public float p1x, p1y;
+        public float r1;
+        // Current point (P2) coordinates and trail radius.
+        public float p2x, p2y;
+        public float r2;
+
+        // Output
+        // Closing point of arc at P1.
+        public float p1ax, p1ay;
+        // Opening point of arc at P1.
+        public float p1bx, p1by;
+        // Opening point of arc at P2.
+        public float p2ax, p2ay;
+        // Closing point of arc at P2.
+        public float p2bx, p2by;
+        // Start angle of the trail arcs.
+        public float aa;
+        // Sweep angle of the trail arc at P1.
+        public float a1;
+        public RectF arc1 = new RectF();
+        // Sweep angle of the trail arc at P2.
+        public float a2;
+        public RectF arc2 = new RectF();
+    }
+
+    private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d);
+    private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI);
+
+    private static boolean calculatePathPoints(final WorkingSet w) {
+        final float dx = w.p2x - w.p1x;
+        final float dy = w.p2y - w.p1y;
+        // Distance of the points.
+        final double l = Math.hypot(dx, dy);
+        if (Double.compare(0.0d, l) == 0) {
+            return false;
+        }
+        // Angle of the line p1-p2
+        final float a = (float)Math.atan2(dy, dx);
+        // Difference of trail cap radius.
+        final float dr = w.r2 - w.r1;
+        // Variation of angle at trail cap.
+        final float ar = (float)Math.asin(dr / l);
+        // The start angle of trail cap arc at P1.
+        final float aa = a - (RIGHT_ANGLE + ar);
+        // The end angle of trail cap arc at P2.
+        final float ab = a + (RIGHT_ANGLE + ar);
+        final float cosa = (float)Math.cos(aa);
+        final float sina = (float)Math.sin(aa);
+        final float cosb = (float)Math.cos(ab);
+        final float sinb = (float)Math.sin(ab);
+        w.p1ax = w.p1x + w.r1 * cosa;
+        w.p1ay = w.p1y + w.r1 * sina;
+        w.p1bx = w.p1x + w.r1 * cosb;
+        w.p1by = w.p1y + w.r1 * sinb;
+        w.p2ax = w.p2x + w.r2 * cosa;
+        w.p2ay = w.p2y + w.r2 * sina;
+        w.p2bx = w.p2x + w.r2 * cosb;
+        w.p2by = w.p2y + w.r2 * sinb;
+        w.aa = aa * RADIAN_TO_DEGREE;
+        final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE;
+        w.a1 = -180.0f + ar2degree;
+        w.a2 = 180.0f + ar2degree;
+        w.arc1.set(w.p1x, w.p1y, w.p1x, w.p1y);
+        w.arc1.inset(-w.r1, -w.r1);
+        w.arc2.set(w.p2x, w.p2y, w.p2x, w.p2y);
+        w.arc2.inset(-w.r2, -w.r2);
+        return true;
+    }
+
+    private static void createPath(final Path path, final WorkingSet w) {
+        path.rewind();
+        // Trail cap at P1.
+        path.moveTo(w.p1x, w.p1y);
+        path.arcTo(w.arc1, w.aa, w.a1);
+        // Trail cap at P2.
+        path.moveTo(w.p2x, w.p2y);
+        path.arcTo(w.arc2, w.aa, w.a2);
+        // Two trapezoids connecting P1 and P2.
+        path.moveTo(w.p1ax, w.p1ay);
+        path.lineTo(w.p1x, w.p1y);
+        path.lineTo(w.p1bx, w.p1by);
+        path.lineTo(w.p2bx, w.p2by);
+        path.lineTo(w.p2x, w.p2y);
+        path.lineTo(w.p2ax, w.p2ay);
+        path.close();
+    }
+
+    private final WorkingSet mWorkingSet = new WorkingSet();
+    private final Path mPath = new Path();
+
     /**
      * Draw gesture preview trail
      * @param canvas The canvas to draw the gesture preview trail
@@ -147,30 +242,38 @@
 
         if (startIndex < trailSize) {
             paint.setColor(params.mTrailColor);
-            paint.setStyle(Paint.Style.STROKE);
-            paint.setStrokeCap(Paint.Cap.ROUND);
-            int lastX = getXCoordValue(xCoords[startIndex]);
-            int lastY = yCoords[startIndex];
-            float maxWidth = getWidth(sinceDown - eventTimes[startIndex], params);
+            paint.setStyle(Paint.Style.FILL);
+            final Path path = mPath;
+            final WorkingSet w = mWorkingSet;
+            w.p1x = getXCoordValue(xCoords[startIndex]);
+            w.p1y = yCoords[startIndex];
+            int lastTime = sinceDown - eventTimes[startIndex];
+            float maxWidth = getWidth(lastTime, params);
+            w.r1 = maxWidth / 2.0f;
             // Initialize bounds rectangle.
-            outBoundsRect.set(lastX, lastY, lastX, lastY);
+            outBoundsRect.set((int)w.p1x, (int)w.p1y, (int)w.p1x, (int)w.p1y);
             for (int i = startIndex + 1; i < trailSize - 1; i++) {
-                final int x = xCoords[i];
-                final int y = yCoords[i];
                 final int elapsedTime = sinceDown - eventTimes[i];
+                w.p2x = getXCoordValue(xCoords[i]);
+                w.p2y = yCoords[i];
                 // Draw trail line only when the current point isn't a down point.
-                if (!isDownEventXCoord(x)) {
+                if (!isDownEventXCoord(xCoords[i])) {
                     final int alpha = getAlpha(elapsedTime, params);
                     paint.setAlpha(alpha);
                     final float width = getWidth(elapsedTime, params);
-                    paint.setStrokeWidth(width);
-                    canvas.drawLine(lastX, lastY, x, y, paint);
+                    w.r2 = width / 2.0f;
+                    if (calculatePathPoints(w)) {
+                        createPath(path, w);
+                        canvas.drawPath(path, paint);
+                        outBoundsRect.union((int)w.p2x, (int)w.p2y);
+                    }
                     // Take union for the bounds.
-                    outBoundsRect.union(x, y);
                     maxWidth = Math.max(maxWidth, width);
                 }
-                lastX = getXCoordValue(x);
-                lastY = y;
+                w.p1x = w.p2x;
+                w.p1y = w.p2y;
+                w.r1 = w.r2;
+                lastTime = elapsedTime;
             }
             // Take care of trail line width.
             final int inset = -((int)maxWidth + 1);
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
index 8251344..093a530 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
@@ -25,7 +25,6 @@
     private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
     private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
     private float mLength;
-    private float mAngle;
     private int mIncrementalRecognitionSize;
     private int mLastIncrementalBatchSize;
     private long mLastPointTime;
@@ -40,15 +39,12 @@
     private static final int MIN_GESTURE_DURATION = 100; // msec
     private static final float MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH = 1.0f / 6.0f;
     private static final float GESTURE_RECOG_SPEED_THRESHOLD = 0.4f; // dip/msec
-    private static final float GESTURE_RECOG_CURVATURE_THRESHOLD = (float)(Math.PI / 4.0f);
-
-    private static final float DOUBLE_PI = (float)(2.0f * Math.PI);
 
     public GestureStroke(final int pointerId) {
         mPointerId = pointerId;
     }
 
-    public void setGestureSampleLength(final int keyWidth) {
+    public void setKeyboardGeometry(final int keyWidth) {
         // TODO: Find an appropriate base metric for these length. Maybe diagonal length of the key?
         mMinGestureLength = (int)(keyWidth * MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH);
         mMinGestureSampleLength = (int)(keyWidth * MIN_GESTURE_SAMPLING_RATIO_TO_KEY_WIDTH);
@@ -62,7 +58,6 @@
 
     public void reset() {
         mLength = 0;
-        mAngle = 0;
         mIncrementalRecognitionSize = 0;
         mLastIncrementalBatchSize = 0;
         mLastPointTime = 0;
@@ -97,16 +92,6 @@
             mXCoordinates.add(x);
             mYCoordinates.add(y);
             mLength += dist;
-            final float angle = getAngle(lastX, lastY, x, y);
-            if (size > 1) {
-                final float curvature = getAngleDiff(angle, mAngle);
-                if (curvature > GESTURE_RECOG_CURVATURE_THRESHOLD) {
-                    if (size > mIncrementalRecognitionSize) {
-                        mIncrementalRecognitionSize = size;
-                    }
-                }
-            }
-            mAngle = angle;
         }
 
         if (!isHistorical) {
@@ -146,21 +131,4 @@
         // java.lang.Math due to the way the JIT optimizes java.lang.Math.
         return (float)Math.sqrt(dx * dx + dy * dy);
     }
-
-    private static float getAngle(final int x1, final int y1, final int x2, final int y2) {
-        final int dx = x1 - x2;
-        final int dy = y1 - y2;
-        if (dx == 0 && dy == 0) return 0;
-        // Would it be faster to call atan2f() directly via JNI?  Not sure about what the JIT
-        // does with Math.atan2().
-        return (float)Math.atan2(dy, dx);
-    }
-
-    private static float getAngleDiff(final float a1, final float a2) {
-        final float diff = Math.abs(a1 - a2);
-        if (diff > Math.PI) {
-            return DOUBLE_PI - diff;
-        }
-        return diff;
-    }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
similarity index 65%
rename from java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewTrail.java
rename to java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
index 6c1a9bc..ce39140 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewTrail.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
@@ -16,7 +16,7 @@
 
 import com.android.inputmethod.latin.ResizableIntArray;
 
-public class GestureStrokeWithPreviewTrail extends GestureStroke {
+public class GestureStrokeWithPreviewPoints extends GestureStroke {
     public static final int PREVIEW_CAPACITY = 256;
 
     private final ResizableIntArray mPreviewEventTimes = new ResizableIntArray(PREVIEW_CAPACITY);
@@ -26,7 +26,14 @@
     private int mStrokeId;
     private int mLastPreviewSize;
 
-    public GestureStrokeWithPreviewTrail(final int pointerId) {
+    private int mMinPreviewSampleLengthSquare;
+    private int mLastX;
+    private int mLastY;
+
+    // TODO: Move this to resource.
+    private static final float MIN_PREVIEW_SAMPLE_LENGTH_RATIO_TO_KEY_WIDTH = 0.1f;
+
+    public GestureStrokeWithPreviewPoints(final int pointerId) {
         super(pointerId);
     }
 
@@ -49,11 +56,31 @@
     }
 
     @Override
+    public void setKeyboardGeometry(final int keyWidth) {
+        super.setKeyboardGeometry(keyWidth);
+        final float sampleLength = keyWidth * MIN_PREVIEW_SAMPLE_LENGTH_RATIO_TO_KEY_WIDTH;
+        mMinPreviewSampleLengthSquare = (int)(sampleLength * sampleLength);
+    }
+
+    private boolean needsSampling(final int x, final int y) {
+        final int dx = x - mLastX;
+        final int dy = y - mLastY;
+        final boolean needsSampling = (dx * dx + dy * dy >= mMinPreviewSampleLengthSquare);
+        if (needsSampling) {
+            mLastX = x;
+            mLastY = y;
+        }
+        return needsSampling;
+    }
+
+    @Override
     public void addPoint(final int x, final int y, final int time, final boolean isHistorical) {
         super.addPoint(x, y, time, isHistorical);
-        mPreviewEventTimes.add(time);
-        mPreviewXCoordinates.add(x);
-        mPreviewYCoordinates.add(y);
+        if (mPreviewEventTimes.getLength() == 0 || isHistorical || needsSampling(x, y)) {
+            mPreviewEventTimes.add(time);
+            mPreviewXCoordinates.add(x);
+            mPreviewYCoordinates.add(y);
+        }
     }
 
     public void appendPreviewStroke(final ResizableIntArray eventTimes,
diff --git a/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java b/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java
index 3a85009..075a9bb 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java
@@ -189,22 +189,29 @@
     }
 
     public void invalidatePointer(final PointerTracker tracker, final boolean isOldestTracker) {
-        GesturePreviewTrail trail;
-        synchronized (mGesturePreviewTrails) {
-            trail = mGesturePreviewTrails.get(tracker.mPointerId);
-            if (trail == null) {
-                trail = new GesturePreviewTrail();
-                mGesturePreviewTrails.put(tracker.mPointerId, trail);
-            }
-        }
-        trail.addStroke(tracker.getGestureStrokeWithPreviewTrail(), tracker.getDownTime());
-
-        if (isOldestTracker) {
+        final boolean needsToUpdateLastPointer =
+                isOldestTracker && mDrawsGestureFloatingPreviewText;
+        if (needsToUpdateLastPointer) {
             mLastPointerX = tracker.getLastX();
             mLastPointerY = tracker.getLastY();
         }
+
+        if (mDrawsGesturePreviewTrail) {
+            GesturePreviewTrail trail;
+            synchronized (mGesturePreviewTrails) {
+                trail = mGesturePreviewTrails.get(tracker.mPointerId);
+                if (trail == null) {
+                    trail = new GesturePreviewTrail();
+                    mGesturePreviewTrails.put(tracker.mPointerId, trail);
+                }
+            }
+            trail.addStroke(tracker.getGestureStrokeWithPreviewPoints(), tracker.getDownTime());
+        }
+
         // TODO: Should narrow the invalidate region.
-        invalidate();
+        if (mDrawsGesturePreviewTrail || needsToUpdateLastPointer) {
+            invalidate();
+        }
     }
 
     @Override
@@ -262,6 +269,7 @@
     }
 
     public void setGestureFloatingPreviewText(final String gestureFloatingPreviewText) {
+        if (!mDrawsGestureFloatingPreviewText) return;
         mGestureFloatingPreviewText = gestureFloatingPreviewText;
         invalidate();
     }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 10a6e95..bf64b4f 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -72,6 +72,7 @@
 import com.android.inputmethod.keyboard.KeyboardView;
 import com.android.inputmethod.keyboard.MainKeyboardView;
 import com.android.inputmethod.latin.LocaleUtils.RunInLocale;
+import com.android.inputmethod.latin.Utils.Stats;
 import com.android.inputmethod.latin.define.ProductionFlag;
 import com.android.inputmethod.latin.suggestions.SuggestionStripView;
 import com.android.inputmethod.research.ResearchLogger;
@@ -148,7 +149,7 @@
     private boolean mIsUserDictionaryAvailable;
 
     private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
-    private WordComposer mWordComposer = new WordComposer();
+    private final WordComposer mWordComposer = new WordComposer();
     private RichInputConnection mConnection = new RichInputConnection(this);
 
     // Keep track of the last selection range to decide if we need to show word alternatives
@@ -1331,6 +1332,12 @@
                 didAutoCorrect = handleSeparator(primaryCode, x, y, spaceState);
             } else {
                 if (SPACE_STATE_PHANTOM == spaceState) {
+                    if (ProductionFlag.IS_INTERNAL) {
+                        if (mWordComposer.isComposingWord() && mWordComposer.isBatchMode()) {
+                            Stats.onAutoCorrection(
+                                    "", mWordComposer.getTypedWord(), " ", mWordComposer);
+                        }
+                    }
                     commitTyped(LastComposedWord.NOT_A_SEPARATOR);
                 }
                 final int keyX, keyY;
@@ -1389,13 +1396,18 @@
     public void onStartBatchInput() {
         mConnection.beginBatchEdit();
         if (mWordComposer.isComposingWord()) {
+            if (ProductionFlag.IS_INTERNAL) {
+                if (mWordComposer.isBatchMode()) {
+                    Stats.onAutoCorrection("", mWordComposer.getTypedWord(), " ", mWordComposer);
+                }
+            }
             commitTyped(LastComposedWord.NOT_A_SEPARATOR);
             mExpectingUpdateSelection = true;
-            // TODO: Can we remove this?
+            // The following is necessary for the case where the user typed something but didn't
+            // manual pick it and didn't input any separator.
             mSpaceState = SPACE_STATE_PHANTOM;
         }
         mConnection.endBatchEdit();
-        // TODO: Should handle TextUtils.CAP_MODE_CHARACTER.
         mWordComposer.setCapitalizedModeAtStartComposingTime(getActualCapsMode());
     }
 
@@ -1547,7 +1559,9 @@
             }
         } else {
             if (mLastComposedWord.canRevertCommit()) {
-                Utils.Stats.onAutoCorrectionCancellation();
+                if (ProductionFlag.IS_INTERNAL) {
+                    Stats.onAutoCorrectionCancellation();
+                }
                 revertCommit();
                 return;
             }
@@ -1696,7 +1710,9 @@
             if (null != mSuggestionStripView) mSuggestionStripView.dismissAddToDictionaryHint();
         }
         mHandler.postUpdateSuggestionStrip();
-        Utils.Stats.onNonSeparator((char)primaryCode, x, y);
+        if (ProductionFlag.IS_INTERNAL) {
+            Utils.Stats.onNonSeparator((char)primaryCode, x, y);
+        }
     }
 
     // Returns true if we did an autocorrection, false otherwise.
@@ -1760,8 +1776,9 @@
             // already displayed or not, so it's okay.
             setPunctuationSuggestions();
         }
-
-        Utils.Stats.onSeparator((char)primaryCode, x, y);
+        if (ProductionFlag.IS_INTERNAL) {
+            Utils.Stats.onSeparator((char)primaryCode, x, y);
+        }
 
         mHandler.postUpdateShiftState();
         return didAutoCorrect;
@@ -1930,7 +1947,10 @@
                 throw new RuntimeException("We have an auto-correction but the typed word "
                         + "is empty? Impossible! I must commit suicide.");
             }
-            Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorString);
+            if (ProductionFlag.IS_INTERNAL) {
+                Stats.onAutoCorrection(
+                        typedWord, autoCorrection.toString(), separatorString, mWordComposer);
+            }
             mExpectingUpdateSelection = true;
             commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD,
                     separatorString);
@@ -2020,8 +2040,10 @@
                 // If the suggestion is not in the dictionary, the hint should be shown.
                 && !AutoCorrection.isValidWord(mSuggest.getUnigramDictionaries(), suggestion, true);
 
-        Utils.Stats.onSeparator((char)Keyboard.CODE_SPACE,
-                Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
+        if (ProductionFlag.IS_INTERNAL) {
+            Stats.onSeparator((char)Keyboard.CODE_SPACE,
+                    Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
+        }
         if (showingAddToDictionaryHint && mIsUserDictionaryAvailable) {
             mSuggestionStripView.showAddToDictionaryHint(
                     suggestion, mCurrentSettings.mHintToSaveText);
@@ -2138,8 +2160,10 @@
                     previousWord.toString(), committedWord.toString());
         }
         mConnection.commitText(originallyTypedWord + mLastComposedWord.mSeparatorString, 1);
-        Utils.Stats.onSeparator(mLastComposedWord.mSeparatorString,
-                Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
+        if (ProductionFlag.IS_INTERNAL) {
+            Stats.onSeparator(mLastComposedWord.mSeparatorString,
+                    Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
+        }
         if (ProductionFlag.IS_EXPERIMENTAL) {
             ResearchLogger.latinIME_revertCommit(originallyTypedWord);
         }
diff --git a/java/src/com/android/inputmethod/latin/LatinImeLogger.java b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
index a5b4c68..9eab19c 100644
--- a/java/src/com/android/inputmethod/latin/LatinImeLogger.java
+++ b/java/src/com/android/inputmethod/latin/LatinImeLogger.java
@@ -49,7 +49,7 @@
     }
 
     public static void logOnAutoCorrectionForGeometric(String before, String after,
-            int separatorCode, int[] xCoordinates, int[] yCoordinates, int[] relativeTimes) {
+            int separatorCode, InputPointers inputPointers) {
     }
 
     public static void logOnAutoCorrectionCancelled() {
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index cbc6a93..28c0c0f 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -209,7 +209,16 @@
             final boolean hasSpaceBefore) {
         mIC = mParent.getCurrentInputConnection();
         if (null == mIC) return Constants.TextUtils.CAP_MODE_OFF;
-        if (!TextUtils.isEmpty(mComposingText)) return Constants.TextUtils.CAP_MODE_OFF;
+        if (!TextUtils.isEmpty(mComposingText)) {
+            if (hasSpaceBefore) {
+                // If we have some composing text and a space before, then we should have
+                // MODE_CHARACTERS and MODE_WORDS on.
+                return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & inputType;
+            } else {
+                // We have some composing text - we should be in MODE_CHARACTERS only.
+                return TextUtils.CAP_MODE_CHARACTERS & inputType;
+            }
+        }
         // TODO: this will generally work, but there may be cases where the buffer contains SOME
         // information but not enough to determine the caps mode accurately. This may happen after
         // heavy pressing of delete, for example DEFAULT_TEXT_CACHE_SIZE - 5 times or so.
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index 180f6c5..c2dec25 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -77,8 +77,8 @@
     public static final String PREF_KEYPRESS_SOUND_VOLUME =
             "pref_keypress_sound_volume";
     public static final String PREF_GESTURE_PREVIEW_TRAIL = "pref_gesture_preview_trail";
-    public static final String PREF_GESTURE_FLOATING_PREVIEW_TEXT =
-            "pref_gesture_floating_preview_text";
+    public static final String PREF_SHOW_GESTURE_FLOATING_PREVIEW_TEXT =
+            "pref_show_gesture_floating_preview_text";
 
     public static final String PREF_INPUT_LANGUAGE = "input_language";
     public static final String PREF_SELECTED_LANGUAGES = "selected_languages";
@@ -207,7 +207,7 @@
                 R.bool.config_gesture_input_enabled_by_build_config);
         final Preference gesturePreviewTrail = findPreference(PREF_GESTURE_PREVIEW_TRAIL);
         final Preference gestureFloatingPreviewText = findPreference(
-                PREF_GESTURE_FLOATING_PREVIEW_TEXT);
+                PREF_SHOW_GESTURE_FLOATING_PREVIEW_TEXT);
         if (!gestureInputEnabledByBuildConfig) {
             miscSettings.removePreference(findPreference(PREF_GESTURE_INPUT));
             miscSettings.removePreference(gesturePreviewTrail);
@@ -304,7 +304,7 @@
                         PREF_GESTURE_INPUT, true);
                 setPreferenceEnabled(findPreference(PREF_GESTURE_PREVIEW_TRAIL),
                         gestureInputEnabledByUser);
-                setPreferenceEnabled(findPreference(PREF_GESTURE_FLOATING_PREVIEW_TEXT),
+                setPreferenceEnabled(findPreference(PREF_SHOW_GESTURE_FLOATING_PREVIEW_TEXT),
                         gestureInputEnabledByUser);
             }
         }
diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index 5e9c870..d9cf270 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -178,7 +178,7 @@
                 && prefs.getBoolean(Settings.PREF_GESTURE_INPUT, true);
         mGesturePreviewTrailEnabled = prefs.getBoolean(Settings.PREF_GESTURE_PREVIEW_TRAIL, true);
         mGestureFloatingPreviewTextEnabled = prefs.getBoolean(
-                Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, true);
+                Settings.PREF_SHOW_GESTURE_FLOATING_PREVIEW_TEXT, false);
         mCorrectionEnabled = mAutoCorrectEnabled && !mInputAttributes.mInputTypeNoAutoCorrect;
         mSuggestionVisibility = createSuggestionVisibility(res);
     }
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 0418d31..278c4b9 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -306,6 +306,10 @@
                     wordComposer, prevWordForBigram, proximityInfo, sessionId));
         }
 
+        for (SuggestedWordInfo wordInfo : suggestionsSet) {
+            LatinImeLogger.onAddSuggestedWord(wordInfo.mWord.toString(), wordInfo.mSourceDict);
+        }
+
         final ArrayList<SuggestedWordInfo> suggestionsContainer =
                 CollectionUtils.newArrayList(suggestionsSet);
         final int suggestionsCount = suggestionsContainer.size();
diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictIOUtils.java b/java/src/com/android/inputmethod/latin/UserHistoryDictIOUtils.java
index 550e4e5..4a3d11a 100644
--- a/java/src/com/android/inputmethod/latin/UserHistoryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/UserHistoryDictIOUtils.java
@@ -18,6 +18,7 @@
 
 import android.util.Log;
 
+import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
 import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
 import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
 import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
@@ -96,6 +97,11 @@
         public void put(final byte b) {
             mBuffer[mPosition++] = b;
         }
+
+        @Override
+        public int limit() {
+            return mBuffer.length;
+        }
     }
 
     /**
@@ -162,7 +168,7 @@
         final Map<Integer, ArrayList<PendingAttribute>> bigrams = CollectionUtils.newTreeMap();
 
         try {
-            BinaryDictInputOutput.readUnigramsAndBigramsBinary(buffer, unigrams, frequencies,
+            BinaryDictIOUtils.readUnigramsAndBigramsBinary(buffer, unigrams, frequencies,
                     bigrams);
             addWordsFromWordMap(unigrams, frequencies, bigrams, dict);
         } catch (IOException e) {
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 63b6428..876bc8e 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -412,14 +412,24 @@
         }
 
         public static void onAutoCorrection(final String typedWord, final String correctedWord,
-                final String separatorString) {
-            if (TextUtils.isEmpty(typedWord)) return;
+                final String separatorString, final WordComposer wordComposer) {
+            final boolean isBatchMode = wordComposer.isBatchMode();
+            if (!isBatchMode && TextUtils.isEmpty(typedWord)) return;
             // TODO: this fails when the separator is more than 1 code point long, but
             // the backend can't handle it yet. The only case when this happens is with
             // smileys and other multi-character keys.
             final int codePoint = TextUtils.isEmpty(separatorString) ? Constants.NOT_A_CODE
                     : separatorString.codePointAt(0);
-            LatinImeLogger.logOnAutoCorrectionForTyping(typedWord, correctedWord, codePoint);
+            if (!isBatchMode) {
+                LatinImeLogger.logOnAutoCorrectionForTyping(typedWord, correctedWord, codePoint);
+            } else {
+                if (!TextUtils.isEmpty(correctedWord)) {
+                    // We must make sure that InputPointer contains only the relative timestamps,
+                    // not actual timestamps.
+                    LatinImeLogger.logOnAutoCorrectionForGeometric(
+                            "", correctedWord, codePoint, wordComposer.getInputPointers());
+                }
+            }
         }
 
         public static void onAutoCorrectionCancellation() {
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index 4b7adf2..275ebf3 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -265,9 +265,12 @@
      * @return true if all user typed chars are upper case, false otherwise
      */
     public boolean isAllUpperCase() {
-        return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED
-                || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFT_LOCKED
-                || (mCapsCount > 0) && (mCapsCount == size());
+        if (size() <= 1) {
+            return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED
+                    || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFT_LOCKED;
+        } else {
+            return mCapsCount == size();
+        }
     }
 
     public boolean wasShiftedNoLock() {
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
new file mode 100644
index 0000000..1a85e71
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
@@ -0,0 +1,127 @@
+/*
+ * 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.makedict;
+
+import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
+import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
+import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader;
+import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Stack;
+
+public class BinaryDictIOUtils {
+    private static final boolean DBG = false;
+
+    private static class Position {
+        public static final int NOT_READ_GROUPCOUNT = -1;
+
+        public int mAddress;
+        public int mNumOfCharGroup;
+        public int mPosition;
+        public int mLength;
+
+        public Position(int address, int length) {
+            mAddress = address;
+            mLength = length;
+            mNumOfCharGroup = NOT_READ_GROUPCOUNT;
+        }
+    }
+
+    /**
+     * Tours all node without recursive call.
+     */
+    private static void readUnigramsAndBigramsBinaryInner(
+            final FusionDictionaryBufferInterface buffer, final int headerSize,
+            final Map<Integer, String> words, final Map<Integer, Integer> frequencies,
+            final Map<Integer, ArrayList<PendingAttribute>> bigrams,
+            final FormatOptions formatOptions) {
+        int[] pushedChars = new int[FormatSpec.MAX_WORD_LENGTH + 1];
+
+        Stack<Position> stack = new Stack<Position>();
+        int index = 0;
+
+        Position initPos = new Position(headerSize, 0);
+        stack.push(initPos);
+
+        while (!stack.empty()) {
+            Position p = stack.peek();
+
+            if (DBG) {
+                MakedictLog.d("read: address=" + p.mAddress + ", numOfCharGroup=" +
+                        p.mNumOfCharGroup + ", position=" + p.mPosition + ", length=" + p.mLength);
+            }
+
+            if (buffer.position() != p.mAddress) buffer.position(p.mAddress);
+            if (index != p.mLength) index = p.mLength;
+
+            if (p.mNumOfCharGroup == Position.NOT_READ_GROUPCOUNT) {
+                p.mNumOfCharGroup = BinaryDictInputOutput.readCharGroupCount(buffer);
+                p.mAddress += BinaryDictInputOutput.getGroupCountSize(p.mNumOfCharGroup);
+                p.mPosition = 0;
+            }
+
+            CharGroupInfo info = BinaryDictInputOutput.readCharGroup(buffer,
+                    p.mAddress - headerSize, formatOptions);
+            for (int i = 0; i < info.mCharacters.length; ++i) {
+                pushedChars[index++] = info.mCharacters[i];
+            }
+            p.mPosition++;
+
+            if (info.mFrequency != FusionDictionary.CharGroup.NOT_A_TERMINAL) { // found word
+                words.put(info.mOriginalAddress, new String(pushedChars, 0, index));
+                frequencies.put(info.mOriginalAddress, info.mFrequency);
+                if (info.mBigrams != null) bigrams.put(info.mOriginalAddress, info.mBigrams);
+            }
+
+            if (p.mPosition == p.mNumOfCharGroup) {
+                stack.pop();
+            } else {
+                // the node has more groups.
+                p.mAddress = buffer.position();
+            }
+
+            if (BinaryDictInputOutput.hasChildrenAddress(info.mChildrenAddress)) {
+                Position childrenPos = new Position(info.mChildrenAddress + headerSize, index);
+                stack.push(childrenPos);
+            }
+        }
+    }
+
+    /**
+     * Reads unigrams and bigrams from the binary file.
+     * Doesn't make the memory representation of the dictionary.
+     *
+     * @param buffer the buffer to read.
+     * @param words the map to store the address as a key and the word as a value.
+     * @param frequencies the map to store the address as a key and the frequency as a value.
+     * @param bigrams the map to store the address as a key and the list of address as a value.
+     * @throws IOException
+     * @throws UnsupportedFormatException
+     */
+    public static void readUnigramsAndBigramsBinary(final FusionDictionaryBufferInterface buffer,
+            final Map<Integer, String> words, final Map<Integer, Integer> frequencies,
+            final Map<Integer, ArrayList<PendingAttribute>> bigrams) throws IOException,
+            UnsupportedFormatException {
+        // Read header
+        final FileHeader header = BinaryDictInputOutput.readHeader(buffer);
+        readUnigramsAndBigramsBinaryInner(buffer, header.mHeaderSize, words, frequencies, bigrams,
+                header.mFormatOptions);
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java
index 72d1229..c865702 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java
@@ -63,6 +63,7 @@
         public int position();
         public void position(int newPosition);
         public void put(final byte b);
+        public int limit();
     }
 
     public static final class ByteBufferWrapper implements FusionDictionaryBufferInterface {
@@ -107,6 +108,11 @@
         public void put(final byte b) {
             mBuffer.put(b);
         }
+
+        @Override
+        public int limit() {
+            return mBuffer.limit();
+        }
     }
 
     /**
@@ -284,7 +290,7 @@
      * @param count the group count
      * @return the size of the group count, either 1 or 2 bytes.
      */
-    private static int getGroupCountSize(final int count) {
+    public static int getGroupCountSize(final int count) {
         if (FormatSpec.MAX_CHARGROUPS_FOR_ONE_BYTE_CHARGROUP_COUNT >= count) {
             return 1;
         } else if (FormatSpec.MAX_CHARGROUPS_IN_A_NODE >= count) {
@@ -379,7 +385,7 @@
     /**
      * Helper method to hide the actual value of the no children address.
      */
-    private static boolean hasChildrenAddress(final int address) {
+    public static boolean hasChildrenAddress(final int address) {
         return FormatSpec.NO_CHILDREN_ADDRESS != address;
     }
 
@@ -1099,7 +1105,7 @@
     // readDictionaryBinary is the public entry point for them.
 
     private static final int[] CHARACTER_BUFFER = new int[FormatSpec.MAX_WORD_LENGTH];
-    private static CharGroupInfo readCharGroup(final FusionDictionaryBufferInterface buffer,
+    public static CharGroupInfo readCharGroup(final FusionDictionaryBufferInterface buffer,
             final int originalGroupAddress, final FormatOptions options) {
         int addressPointer = originalGroupAddress;
         final int flags = buffer.readUnsignedByte();
@@ -1212,7 +1218,7 @@
     /**
      * Reads and returns the char group count out of a buffer and forwards the pointer.
      */
-    private static int readCharGroupCount(final FusionDictionaryBufferInterface buffer) {
+    public static int readCharGroupCount(final FusionDictionaryBufferInterface buffer) {
         final int msb = buffer.readUnsignedByte();
         if (FormatSpec.MAX_CHARGROUPS_FOR_ONE_BYTE_CHARGROUP_COUNT >= msb) {
             return msb;
@@ -1341,146 +1347,67 @@
             final Map<Integer, Node> reverseNodeMap, final Map<Integer, CharGroup> reverseGroupMap,
             final FormatOptions options)
             throws IOException {
-        final int nodeOrigin = buffer.position() - headerSize;
-        final int count = readCharGroupCount(buffer);
         final ArrayList<CharGroup> nodeContents = new ArrayList<CharGroup>();
-        int groupOffset = nodeOrigin + getGroupCountSize(count);
-        for (int i = count; i > 0; --i) {
-            CharGroupInfo info = readCharGroup(buffer, groupOffset, options);
-            ArrayList<WeightedString> shortcutTargets = info.mShortcutTargets;
-            ArrayList<WeightedString> bigrams = null;
-            if (null != info.mBigrams) {
-                bigrams = new ArrayList<WeightedString>();
-                for (PendingAttribute bigram : info.mBigrams) {
-                    final String word = getWordAtAddress(
-                            buffer, headerSize, bigram.mAddress, options);
-                    bigrams.add(new WeightedString(word, bigram.mFrequency));
+        final int nodeOrigin = buffer.position() - headerSize;
+
+        do { // Scan the linked-list node.
+            final int nodeHeadPosition = buffer.position() - headerSize;
+            final int count = readCharGroupCount(buffer);
+            int groupOffset = nodeHeadPosition + getGroupCountSize(count);
+            for (int i = count; i > 0; --i) { // Scan the array of CharGroup.
+                CharGroupInfo info = readCharGroup(buffer, groupOffset, options);
+                ArrayList<WeightedString> shortcutTargets = info.mShortcutTargets;
+                ArrayList<WeightedString> bigrams = null;
+                if (null != info.mBigrams) {
+                    bigrams = new ArrayList<WeightedString>();
+                    for (PendingAttribute bigram : info.mBigrams) {
+                        final String word = getWordAtAddress(
+                                buffer, headerSize, bigram.mAddress, options);
+                        bigrams.add(new WeightedString(word, bigram.mFrequency));
+                    }
+                }
+                if (hasChildrenAddress(info.mChildrenAddress)) {
+                    Node children = reverseNodeMap.get(info.mChildrenAddress);
+                    if (null == children) {
+                        final int currentPosition = buffer.position();
+                        buffer.position(info.mChildrenAddress + headerSize);
+                        children = readNode(
+                                buffer, headerSize, reverseNodeMap, reverseGroupMap, options);
+                        buffer.position(currentPosition);
+                    }
+                    nodeContents.add(
+                            new CharGroup(info.mCharacters, shortcutTargets, bigrams,
+                                    info.mFrequency,
+                                    0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
+                                    0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED), children));
+                } else {
+                    nodeContents.add(
+                            new CharGroup(info.mCharacters, shortcutTargets, bigrams,
+                                    info.mFrequency,
+                                    0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
+                                    0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED)));
+                }
+                groupOffset = info.mEndAddress;
+            }
+
+            // reach the end of the array.
+            if (options.mHasLinkedListNode) {
+                final int nextAddress = buffer.readUnsignedInt24();
+                if (nextAddress >= 0 && nextAddress < buffer.limit()) {
+                    buffer.position(nextAddress);
+                } else {
+                    break;
                 }
             }
-            if (hasChildrenAddress(info.mChildrenAddress)) {
-                Node children = reverseNodeMap.get(info.mChildrenAddress);
-                if (null == children) {
-                    final int currentPosition = buffer.position();
-                    buffer.position(info.mChildrenAddress + headerSize);
-                    children = readNode(
-                            buffer, headerSize, reverseNodeMap, reverseGroupMap, options);
-                    buffer.position(currentPosition);
-                }
-                nodeContents.add(
-                        new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency,
-                                0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
-                                0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED), children));
-            } else {
-                nodeContents.add(
-                        new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency,
-                                0 != (info.mFlags & FormatSpec.FLAG_IS_NOT_A_WORD),
-                                0 != (info.mFlags & FormatSpec.FLAG_IS_BLACKLISTED)));
-            }
-            groupOffset = info.mEndAddress;
-        }
+        } while (options.mHasLinkedListNode &&
+                buffer.position() != FormatSpec.NO_FORWARD_LINK_ADDRESS);
+
         final Node node = new Node(nodeContents);
         node.mCachedAddress = nodeOrigin;
         reverseNodeMap.put(node.mCachedAddress, node);
         return node;
     }
 
-    // TODO: move these methods (readUnigramsAndBigramsBinary(|Inner)) and an inner class (Position)
-    // out of this class.
-    private static class Position {
-        public static final int NOT_READ_GROUPCOUNT = -1;
-
-        public int mAddress;
-        public int mNumOfCharGroup;
-        public int mPosition;
-        public int mLength;
-
-        public Position(int address, int length) {
-            mAddress = address;
-            mLength = length;
-            mNumOfCharGroup = NOT_READ_GROUPCOUNT;
-        }
-    }
-
-    /**
-     * Tours all node without recursive call.
-     */
-    private static void readUnigramsAndBigramsBinaryInner(
-            final FusionDictionaryBufferInterface buffer, final int headerSize,
-            final Map<Integer, String> words, final Map<Integer, Integer> frequencies,
-            final Map<Integer, ArrayList<PendingAttribute>> bigrams,
-            final FormatOptions formatOptions) {
-        int[] pushedChars = new int[FormatSpec.MAX_WORD_LENGTH + 1];
-
-        Stack<Position> stack = new Stack<Position>();
-        int index = 0;
-
-        Position initPos = new Position(headerSize, 0);
-        stack.push(initPos);
-
-        while (!stack.empty()) {
-            Position p = stack.peek();
-
-            if (DBG) {
-                MakedictLog.d("read: address=" + p.mAddress + ", numOfCharGroup=" +
-                        p.mNumOfCharGroup + ", position=" + p.mPosition + ", length=" + p.mLength);
-            }
-
-            if (buffer.position() != p.mAddress) buffer.position(p.mAddress);
-            if (index != p.mLength) index = p.mLength;
-
-            if (p.mNumOfCharGroup == Position.NOT_READ_GROUPCOUNT) {
-                p.mNumOfCharGroup = readCharGroupCount(buffer);
-                p.mAddress += getGroupCountSize(p.mNumOfCharGroup);
-                p.mPosition = 0;
-            }
-
-            CharGroupInfo info = readCharGroup(buffer, p.mAddress - headerSize, formatOptions);
-            for (int i = 0; i < info.mCharacters.length; ++i) {
-                pushedChars[index++] = info.mCharacters[i];
-            }
-            p.mPosition++;
-
-            if (info.mFrequency != FusionDictionary.CharGroup.NOT_A_TERMINAL) { // found word
-                words.put(info.mOriginalAddress, new String(pushedChars, 0, index));
-                frequencies.put(info.mOriginalAddress, info.mFrequency);
-                if (info.mBigrams != null) bigrams.put(info.mOriginalAddress, info.mBigrams);
-            }
-
-            if (p.mPosition == p.mNumOfCharGroup) {
-                stack.pop();
-            } else {
-                // the node has more groups.
-                p.mAddress = buffer.position();
-            }
-
-            if (hasChildrenAddress(info.mChildrenAddress)) {
-                Position childrenPos = new Position(info.mChildrenAddress + headerSize, index);
-                stack.push(childrenPos);
-            }
-        }
-    }
-
-    /**
-     * Reads unigrams and bigrams from the binary file.
-     * Doesn't make the memory representation of the dictionary.
-     *
-     * @param buffer the buffer to read.
-     * @param words the map to store the address as a key and the word as a value.
-     * @param frequencies the map to store the address as a key and the frequency as a value.
-     * @param bigrams the map to store the address as a key and the list of address as a value.
-     * @throws IOException
-     * @throws UnsupportedFormatException
-     */
-    public static void readUnigramsAndBigramsBinary(final FusionDictionaryBufferInterface buffer,
-            final Map<Integer, String> words, final Map<Integer, Integer> frequencies,
-            final Map<Integer, ArrayList<PendingAttribute>> bigrams) throws IOException,
-            UnsupportedFormatException {
-        // Read header
-        final FileHeader header = readHeader(buffer);
-        readUnigramsAndBigramsBinaryInner(buffer, header.mHeaderSize, words, frequencies, bigrams,
-                header.mFormatOptions);
-    }
-
     /**
      * Helper function to get the binary format version from the header.
      * @throws IOException
@@ -1517,7 +1444,7 @@
      * @throws IOException
      * @throws UnsupportedFormatException
      */
-    private static FileHeader readHeader(final FusionDictionaryBufferInterface buffer)
+    public static FileHeader readHeader(final FusionDictionaryBufferInterface buffer)
             throws IOException, UnsupportedFormatException {
         final int version = checkFormatVersion(buffer);
         final int optionsFlags = buffer.readUnsignedShort();
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
index f4784ff..d9b622a 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
@@ -16,6 +16,9 @@
 
 package com.android.inputmethod.latin.spellcheck;
 
+import android.content.ContentResolver;
+import android.database.ContentObserver;
+import android.provider.UserDictionary.Words;
 import android.service.textservice.SpellCheckerService.Session;
 import android.text.TextUtils;
 import android.util.Log;
@@ -45,6 +48,7 @@
     private int mScript; // One of SCRIPT_LATIN or SCRIPT_CYRILLIC for now.
     private final AndroidSpellCheckerService mService;
     protected final SuggestionsCache mSuggestionsCache = new SuggestionsCache();
+    private final ContentObserver mObserver;
 
     private static class SuggestionsParams {
         public final String[] mSuggestions;
@@ -83,10 +87,23 @@
             mUnigramSuggestionsInfoCache.put(
                     generateKey(query, prevWord), new SuggestionsParams(suggestions, flags));
         }
+
+        public void clearCache() {
+            mUnigramSuggestionsInfoCache.evictAll();
+        }
     }
 
     AndroidWordLevelSpellCheckerSession(final AndroidSpellCheckerService service) {
         mService = service;
+        final ContentResolver cres = service.getContentResolver();
+
+        mObserver = new ContentObserver(null) {
+            @Override
+            public void onChange(boolean self) {
+                mSuggestionsCache.clearCache();
+            }
+        };
+        cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
     }
 
     @Override
@@ -97,6 +114,12 @@
         mScript = AndroidSpellCheckerService.getScriptFromLocale(mLocale);
     }
 
+    @Override
+    public void onClose() {
+        final ContentResolver cres = mService.getContentResolver();
+        cres.unregisterContentObserver(mObserver);
+    }
+
     /*
      * Returns whether the code point is a letter that makes sense for the specified
      * locale for this spell checker.
diff --git a/native/jni/src/geometry_utils.h b/native/jni/src/geometry_utils.h
index bad5eda..3892b46 100644
--- a/native/jni/src/geometry_utils.h
+++ b/native/jni/src/geometry_utils.h
@@ -64,17 +64,8 @@
     return diff;
 }
 
-// static float pointToLineSegSquaredDistanceFloat(
-//         float x, float y, float x1, float y1, float x2, float y2) {
-//     float A = x - x1;
-//     float B = y - y1;
-//     float C = x2 - x1;
-//     float D = y2 - y1;
-//     return fabsf(A * D - C * B) / sqrtf(C * C + D * D);
-// }
-
 static inline float pointToLineSegSquaredDistanceFloat(
-        float x, float y, float x1, float y1, float x2, float y2) {
+        float x, float y, float x1, float y1, float x2, float y2, bool extend) {
     const float ray1x = x - x1;
     const float ray1y = y - y1;
     const float ray2x = x2 - x1;
@@ -86,10 +77,10 @@
 
     float projectionX;
     float projectionY;
-    if (projectionLengthSqr < 0.0f) {
+    if (!extend && projectionLengthSqr < 0.0f) {
         projectionX = x1;
         projectionY = y1;
-    } else if (projectionLengthSqr > 1.0f) {
+    } else if (!extend && projectionLengthSqr > 1.0f) {
         projectionX = x2;
         projectionY = y2;
     } else {
diff --git a/native/jni/src/proximity_info_state.cpp b/native/jni/src/proximity_info_state.cpp
index 9b18918..b9fce5f 100644
--- a/native/jni/src/proximity_info_state.cpp
+++ b/native/jni/src/proximity_info_state.cpp
@@ -409,11 +409,11 @@
                 }
                 NearKeysDistanceMap::const_iterator itPP =
                         prevNearKeysDistances->find(minChar);
-                if (DEBUG_GEO_FULL) {
-                    AKLOGI("p1: char = %c, minDist = %f, prevNear key minDist = %f",
-                            minChar, itPP->second, minDist);
-                }
                 if (itPP != prevNearKeysDistances->end() && minDist > itPP->second) {
+                    if (DEBUG_GEO_FULL) {
+                        AKLOGI("p1: char = %c, minDist = %f, prevNear key minDist = %f",
+                                minChar, itPP->second, minDist);
+                    }
                     return popped;
                 }
             }
@@ -504,7 +504,7 @@
     if (index >= mInputXs.size()) {
         return filterSize;
     }
-    int i = filterSize;
+    int newFilterSize = filterSize;
     for (int j = 0; j < mProximityInfo->getKeyCount(); ++j) {
         if (mNearKeysVector[index].test(j)) {
             const int32_t keyCodePoint = mProximityInfo->getCodePointOf(j);
@@ -517,11 +517,11 @@
                 }
             }
             if (insert) {
-                filter[i++] = keyCodePoint;
+                filter[newFilterSize++] = keyCodePoint;
             }
         }
     }
-    return i;
+    return newFilterSize;
 }
 
 float ProximityInfoState::getAveragePointDuration() const {
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
index a87146e..4c2d3f6 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java
@@ -391,7 +391,7 @@
         assertNotNull("Can't get buffer.", buffer);
         try {
             now = System.currentTimeMillis();
-            BinaryDictInputOutput.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs,
+            BinaryDictIOUtils.readUnigramsAndBigramsBinary(buffer, resultWords, resultFreqs,
                     resultBigrams);
             diff = System.currentTimeMillis() - now;
         } catch (IOException e) {