Merge "Fix addtional proximity correction" into jb-mr1-dev
diff --git a/java/res/values-fr/strings.xml b/java/res/values-fr/strings.xml
index 31f7047..58393b0 100644
--- a/java/res/values-fr/strings.xml
+++ b/java/res/values-fr/strings.xml
@@ -61,8 +61,8 @@
     <string name="auto_correction_threshold_mode_very_aggeressive" msgid="3386782235540547678">"Très exigeante"</string>
     <string name="bigram_prediction" msgid="1084449187723948550">"Suggestions pour le mot suivant"</string>
     <string name="bigram_prediction_summary" msgid="3896362682751109677">"Utiliser le mot précédent pour les suggestions"</string>
-    <string name="gesture_input" msgid="826951152254563827">"Activer saisie gestuelle"</string>
-    <string name="gesture_input_summary" msgid="9180350639305731231">"Saisir un mot en traçant les lettres sans interruption"</string>
+    <string name="gesture_input" msgid="826951152254563827">"Activer la saisie gestuelle"</string>
+    <string name="gesture_input_summary" msgid="9180350639305731231">"Saisir un mot en faisant glisser le doigt sur les lettres"</string>
     <string name="gesture_preview_trail" msgid="3802333369335722221">"Afficher le tracé du geste"</string>
     <string name="gesture_floating_preview_text" msgid="4443240334739381053">"Aperçu flottant dynamique"</string>
     <string name="gesture_floating_preview_text_summary" msgid="4472696213996203533">"Afficher le mot suggéré lors des gestes"</string>
diff --git a/java/res/values-pl/strings.xml b/java/res/values-pl/strings.xml
index 414aa9a..9b73ad1 100644
--- a/java/res/values-pl/strings.xml
+++ b/java/res/values-pl/strings.xml
@@ -91,7 +91,7 @@
     <string name="spoken_description_settings" msgid="4627462689603838099">"Ustawienia"</string>
     <string name="spoken_description_tab" msgid="2667716002663482248">"Tab"</string>
     <string name="spoken_description_space" msgid="2582521050049860859">"Spacja"</string>
-    <string name="spoken_description_mic" msgid="615536748882611950">"Wprowadzanie głosowe"</string>
+    <string name="spoken_description_mic" msgid="615536748882611950">"Rozpoznawanie mowy"</string>
     <string name="spoken_description_smiley" msgid="2256309826200113918">"Uśmiechnięta buźka"</string>
     <string name="spoken_description_return" msgid="8178083177238315647">"Enter"</string>
     <string name="spoken_description_search" msgid="1247236163755920808">"Szukaj"</string>
@@ -112,7 +112,7 @@
     <string name="voice_input_modes_off" msgid="3745699748218082014">"Wyłącz"</string>
     <string name="voice_input_modes_summary_main_keyboard" msgid="6586544292900314339">"Mikrofon na klawiaturze głównej"</string>
     <string name="voice_input_modes_summary_symbols_keyboard" msgid="5233725927281932391">"Mikrofon na klawiaturze z symbolami"</string>
-    <string name="voice_input_modes_summary_off" msgid="63875609591897607">"Wprowadzanie głosowe jest wyłączone"</string>
+    <string name="voice_input_modes_summary_off" msgid="63875609591897607">"Rozpoznawanie mowy jest wyłączone"</string>
     <string name="configure_input_method" msgid="373356270290742459">"Konfiguruj metody wprowadzania"</string>
     <string name="language_selection_title" msgid="1651299598555326750">"Języki wprowadzania"</string>
     <string name="select_language" msgid="3693815588777926848">"Języki wprowadzania"</string>
diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index 7e8c77e..72dcb0e 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -127,6 +127,25 @@
         <attr name="ignoreAltCodeKeyTimeout" format="integer" />
         <!-- More keys keyboard will shown at touched point. -->
         <attr name="showMoreKeysKeyboardAtTouchedPoint" format="boolean" />
+        <!-- Static threshold for gesture after fast typing (msec) -->
+        <attr name="gestureStaticTimeThresholdAfterFastTyping" format="integer" />
+        <!-- Static threshold for starting gesture detection (keyWidth%/sec) -->
+        <attr name="gestureDetectFastMoveSpeedThreshold" format="fraction" />
+        <!-- Dynamic threshold for gesture after fast typing (msec) -->
+        <attr name="gestureDynamicThresholdDecayDuration" format="integer" />
+        <!-- Time based threshold values for gesture detection (msec) -->
+        <attr name="gestureDynamicTimeThresholdFrom" format="integer" />
+        <attr name="gestureDynamicTimeThresholdTo" format="integer" />
+        <!-- Distance based threshold values for gesture detection (keyWidth%/sec) -->
+        <attr name="gestureDynamicDistanceThresholdFrom" format="fraction" />
+        <attr name="gestureDynamicDistanceThresholdTo" format="fraction" />
+        <!-- Parameter for gesture sampling (keyWidth%/sec) -->
+        <attr name="gestureSamplingMinimumDistance" format="fraction" />
+        <!-- Parameters for gesture recognition (msec) and (keyWidth%/sec) -->
+        <attr name="gestureRecognitionMinimumTime" format="integer" />
+        <attr name="gestureRecognitionSpeedThreshold" format="fraction" />
+        <!-- Suppress showing key preview duration after batch input in millisecond -->
+        <attr name="suppressKeyPreviewAfterBatchInputDuration" format="integer" />
     </declare-styleable>
 
     <declare-styleable name="SuggestionStripView">
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 8e2d43e..7c57f1f 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -69,6 +69,25 @@
     <!-- Showing more keys keyboard, just above the touched point if true, aligned to the key if
          false -->
     <bool name="config_show_more_keys_keyboard_at_touched_point">false</bool>
+    <!-- Static threshold for gesture after fast typing (msec) -->
+    <integer name="config_gesture_static_time_threshold_after_fast_typing">1000</integer>
+    <!-- Static threshold for starting gesture detection (keyWidth%/sec) -->
+    <fraction name="config_gesture_detect_fast_move_speed_threshold">150%</fraction>
+    <!-- Dynamic threshold for gesture after fast typing (msec) -->
+    <integer name="config_gesture_dynamic_threshold_decay_duration">450</integer>
+    <!-- Time based threshold values for gesture detection (msec) -->
+    <integer name="config_gesture_dynamic_time_threshold_from">300</integer>
+    <integer name="config_gesture_dynamic_time_threshold_to">20</integer>
+    <!-- Distance based threshold values for gesture detection (keyWidth%/sec) -->
+    <fraction name="config_gesture_dynamic_distance_threshold_from">600%</fraction>
+    <fraction name="config_gesture_dynamic_distance_threshold_to">35%</fraction>
+    <!-- Parameter for gesture sampling (keyWidth%/sec) -->
+    <fraction name="config_gesture_sampling_minimum_distance">16.6666%</fraction>
+    <!-- Parameters for gesture recognition (msec) and (keyWidth%/sec) -->
+    <integer name="config_gesture_recognition_minimum_time">100</integer>
+    <fraction name="config_gesture_recognition_speed_threshold">550%</fraction>
+    <!-- Suppress showing key preview duration after batch input in millisecond -->
+    <integer name="config_suppress_key_preview_after_batch_input_duration">1000</integer>
     <!--
         Configuration for auto correction
      -->
diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml
index 9d3d716..589962c 100644
--- a/java/res/values/styles.xml
+++ b/java/res/values/styles.xml
@@ -94,6 +94,18 @@
         <item name="languageOnSpacebarFadeoutAnimator">@anim/language_on_spacebar_fadeout</item>
         <item name="altCodeKeyWhileTypingFadeoutAnimator">@anim/alt_code_key_while_typing_fadeout</item>
         <item name="altCodeKeyWhileTypingFadeinAnimator">@anim/alt_code_key_while_typing_fadein</item>
+        <!-- Common attributes of MainKeyboardView for gesture typing detection and recognition -->
+        <item name="gestureStaticTimeThresholdAfterFastTyping">@integer/config_gesture_static_time_threshold_after_fast_typing</item>
+        <item name="gestureDetectFastMoveSpeedThreshold">@fraction/config_gesture_detect_fast_move_speed_threshold</item>
+        <item name="gestureDynamicThresholdDecayDuration">@integer/config_gesture_dynamic_threshold_decay_duration</item>
+        <item name="gestureDynamicTimeThresholdFrom">@integer/config_gesture_dynamic_time_threshold_from</item>
+        <item name="gestureDynamicTimeThresholdTo">@integer/config_gesture_dynamic_time_threshold_to</item>
+        <item name="gestureDynamicDistanceThresholdFrom">@fraction/config_gesture_dynamic_distance_threshold_from</item>
+        <item name="gestureDynamicDistanceThresholdTo">@fraction/config_gesture_dynamic_distance_threshold_to</item>
+        <item name="gestureSamplingMinimumDistance">@fraction/config_gesture_sampling_minimum_distance</item>
+        <item name="gestureRecognitionMinimumTime">@integer/config_gesture_recognition_minimum_time</item>
+        <item name="gestureRecognitionSpeedThreshold">@fraction/config_gesture_recognition_speed_threshold</item>
+        <item name="suppressKeyPreviewAfterBatchInputDuration">@integer/config_suppress_key_preview_after_batch_input_duration</item>
     </style>
     <style
         name="MainKeyboardView"
diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
index b41631e..20d7847 100644
--- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
@@ -82,6 +82,17 @@
  * @attr ref R.styleable#MainKeyboardView_longPressShiftKeyTimeout
  * @attr ref R.styleable#MainKeyboardView_ignoreAltCodeKeyTimeout
  * @attr ref R.styleable#MainKeyboardView_showMoreKeysKeyboardAtTouchPoint
+ * @attr ref R.styleable#MainKeyboardView_gestureStaticTimeThresholdAfterFastTyping
+ * @attr ref R.styleable#MainKeyboardView_gestureDetectFastMoveSpeedThreshold
+ * @attr ref R.styleable#MainKeyboardView_gestureDynamicThresholdDecayDuration
+ * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdFrom
+ * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdTo
+ * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdFrom
+ * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdTo
+ * @attr ref R.styleable#MainKeyboardView_gestureSamplingMinimumDistance
+ * @attr ref R.styleable#MainKeyboardView_gestureRecognitionMinimumTime
+ * @attr ref R.styleable#MainKeyboardView_gestureRecognitionSpeedThreshold
+ * @attr ref R.styleable#MainKeyboardView_suppressKeyPreviewAfterBatchInputDuration
  */
 public final class MainKeyboardView extends KeyboardView implements PointerTracker.KeyEventHandler,
         SuddenJumpingTouchEventHandler.ProcessMotionEvent {
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index ad7d0a3..7aeddc2 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -23,6 +23,7 @@
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
 import com.android.inputmethod.keyboard.internal.GestureStroke;
+import com.android.inputmethod.keyboard.internal.GestureStroke.GestureStrokeParams;
 import com.android.inputmethod.keyboard.internal.GestureStrokeWithPreviewPoints;
 import com.android.inputmethod.keyboard.internal.PointerTrackerQueue;
 import com.android.inputmethod.latin.CollectionUtils;
@@ -47,9 +48,6 @@
     private static boolean sGestureHandlingEnabledByInputField = false;
     private static boolean sGestureHandlingEnabledByUser = false;
 
-    // TODO: Move this to resource.
-    private static final int SUPPRESS_KEY_PREVIEW_AFTER_LAST_BATCH_INPUT_DURATION = 1000; // msec
-
     public interface KeyEventHandler {
         /**
          * Get KeyDetector object that is used for this PointerTracker.
@@ -125,6 +123,7 @@
         public final int mTouchNoiseThresholdTime;
         public final float mTouchNoiseThresholdDistance;
         public final int mTouchNoiseThresholdDistanceSquared;
+        public final int mSuppressKeyPreviewAfterBatchInputDuration;
 
         public static final PointerTrackerParams DEFAULT = new PointerTrackerParams();
 
@@ -133,9 +132,10 @@
             mTouchNoiseThresholdTime = 0;
             mTouchNoiseThresholdDistance = 0.0f;
             mTouchNoiseThresholdDistanceSquared = 0;
+            mSuppressKeyPreviewAfterBatchInputDuration = 0;
         }
 
-        public PointerTrackerParams(TypedArray mainKeyboardViewAttr) {
+        public PointerTrackerParams(final TypedArray mainKeyboardViewAttr) {
             mSlidingKeyInputEnabled = mainKeyboardViewAttr.getBoolean(
                     R.styleable.MainKeyboardView_slidingKeyInputEnable, false);
             mTouchNoiseThresholdTime = mainKeyboardViewAttr.getInt(
@@ -145,11 +145,14 @@
             mTouchNoiseThresholdDistance = touchNouseThresholdDistance;
             mTouchNoiseThresholdDistanceSquared =
                     (int)(touchNouseThresholdDistance * touchNouseThresholdDistance);
+            mSuppressKeyPreviewAfterBatchInputDuration = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_suppressKeyPreviewAfterBatchInputDuration, 0);
         }
     }
 
     // Parameters for pointer handling.
     private static PointerTrackerParams sParams;
+    private static GestureStrokeParams sGestureStrokeParams;
     private static boolean sNeedsPhantomSuddenMoveEventHack;
 
     private static final ArrayList<PointerTracker> sTrackers = CollectionUtils.newArrayList();
@@ -168,13 +171,80 @@
     private boolean mIsDetectingGesture = false; // per PointerTracker.
     private static boolean sInGesture = false;
     private static long sGestureFirstDownTime;
-    private static long sLastBatchInputTime;
-    private static long sLastLetterTypingUpTime;
+    private static TimeRecorder sTimeRecorder;
     private static final InputPointers sAggregratedPointers = new InputPointers(
             GestureStroke.DEFAULT_CAPACITY);
     private static int sLastRecognitionPointSize = 0; // synchronized using sAggregratedPointers
     private static long sLastRecognitionTime = 0; // synchronized using sAggregratedPointers
 
+    static final class TimeRecorder {
+        private final int mSuppressKeyPreviewAfterBatchInputDuration;
+        private final int mStaticTimeThresholdAfterFastTyping; // msec
+        private long mLastTypingTime;
+        private long mLastLetterTypingTime;
+        private long mLastBatchInputTime;
+
+        public TimeRecorder(final PointerTrackerParams pointerTrackerParams,
+                final GestureStrokeParams gestureStrokeParams) {
+            mSuppressKeyPreviewAfterBatchInputDuration =
+                    pointerTrackerParams.mSuppressKeyPreviewAfterBatchInputDuration;
+            mStaticTimeThresholdAfterFastTyping =
+                    gestureStrokeParams.mStaticTimeThresholdAfterFastTyping;
+        }
+
+        private void recordTyping(final long eventTime) {
+            mLastTypingTime = eventTime;
+        }
+
+        private void recordLetterTyping(final long eventTime) {
+            mLastLetterTypingTime = eventTime;
+            // Reset gesture typing time
+            mLastBatchInputTime = 0;
+        }
+
+        private void recordGestureTyping(final long eventTime) {
+            mLastBatchInputTime = eventTime;
+            // Reset typing time.
+            mLastTypingTime = 0;
+        }
+
+        private boolean isInTyping() {
+            return mLastTypingTime != 0;
+        }
+
+        private boolean isInBatchInput() {
+            return mLastBatchInputTime != 0;
+        }
+
+        public void onCodeInput(final int code, final long eventTime) {
+            if (Keyboard.isLetterCode(code) && code != Keyboard.CODE_SPACE) {
+                if (isInTyping()
+                        && eventTime - mLastTypingTime < mStaticTimeThresholdAfterFastTyping) {
+                    recordLetterTyping(eventTime);
+                }
+            } else {
+                if (eventTime - mLastLetterTypingTime < mStaticTimeThresholdAfterFastTyping) {
+                    // This non-letter typing should be treated as a part of fast typing.
+                    recordLetterTyping(eventTime);
+                }
+            }
+            recordTyping(eventTime);
+        }
+
+        public void onEndBatchInput(final long eventTime) {
+            recordGestureTyping(eventTime);
+        }
+
+        public long getLastLetterTypingTime() {
+            return mLastLetterTypingTime;
+        }
+
+        public boolean needsToSuppressKeyPreviewPopup(final long eventTime) {
+            return !isInTyping() && isInBatchInput()
+                    && eventTime - mLastBatchInputTime < mSuppressKeyPreviewAfterBatchInputDuration;
+        }
+    }
+
     // The position and time at which first down event occurred.
     private long mDownTime;
     private long mUpTime;
@@ -222,10 +292,14 @@
         }
         sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack;
         sParams = PointerTrackerParams.DEFAULT;
+        sGestureStrokeParams = GestureStrokeParams.DEFAULT;
+        sTimeRecorder = new TimeRecorder(sParams, sGestureStrokeParams);
     }
 
     public static void setParameters(final TypedArray mainKeyboardViewAttr) {
         sParams = new PointerTrackerParams(mainKeyboardViewAttr);
+        sGestureStrokeParams = new GestureStrokeParams(mainKeyboardViewAttr);
+        sTimeRecorder = new TimeRecorder(sParams, sGestureStrokeParams);
     }
 
     private static void updateGestureHandlingMode() {
@@ -296,7 +370,8 @@
             throw new NullPointerException();
         }
         mPointerId = id;
-        mGestureStrokeWithPreviewPoints = new GestureStrokeWithPreviewPoints(id);
+        mGestureStrokeWithPreviewPoints = new GestureStrokeWithPreviewPoints(
+                id, sGestureStrokeParams);
         setKeyDetectorInner(handler.getKeyDetector());
         mListener = handler.getKeyboardActionListener();
         mDrawingProxy = handler.getDrawingProxy();
@@ -331,7 +406,7 @@
     // Note that we need primaryCode argument because the keyboard may in shifted state and the
     // primaryCode is different from {@link Key#mCode}.
     private void callListenerOnCodeInput(final Key key, final int primaryCode, final int x,
-            final int y) {
+            final int y, final long eventTime) {
         final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
         final boolean altersCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
         final int code = altersCode ? key.getAltCode() : primaryCode;
@@ -351,7 +426,7 @@
         }
         // Even if the key is disabled, it should respond if it is in the altCodeWhileTyping state.
         if (key.isEnabled() || altersCode) {
-            sLastBatchInputTime = 0; // reset time
+            sTimeRecorder.onCodeInput(code, eventTime);
             if (code == Keyboard.CODE_OUTPUT_TEXT) {
                 mListener.onTextInput(key.getOutputText());
             } else if (code != Keyboard.CODE_UNSPECIFIED) {
@@ -466,10 +541,7 @@
 
     private static boolean needsToSuppressKeyPreviewPopup(final long eventTime) {
         if (!sShouldHandleGesture) return false;
-        if (sLastBatchInputTime == 0) return false;
-        final long elapsedTimeAfterTheLastBatchInput = eventTime - sLastBatchInputTime;
-        return elapsedTimeAfterTheLastBatchInput
-                < SUPPRESS_KEY_PREVIEW_AFTER_LAST_BATCH_INPUT_DURATION;
+        return sTimeRecorder.needsToSuppressKeyPreviewPopup(eventTime);
     }
 
     private void setPressedKeyGraphics(final Key key, final long eventTime) {
@@ -587,10 +659,11 @@
     private void mayUpdateBatchInput(final long eventTime, final Key key) {
         if (key != null) {
             synchronized (sAggregratedPointers) {
-                mGestureStrokeWithPreviewPoints.appendIncrementalBatchPoints(sAggregratedPointers);
+                final GestureStroke stroke = mGestureStrokeWithPreviewPoints;
+                stroke.appendIncrementalBatchPoints(sAggregratedPointers);
                 final int size = sAggregratedPointers.getPointerSize();
                 if (size > sLastRecognitionPointSize
-                        && GestureStroke.hasRecognitionTimePast(eventTime, sLastRecognitionTime)) {
+                        && stroke.hasRecognitionTimePast(eventTime, sLastRecognitionTime)) {
                     sLastRecognitionPointSize = size;
                     sLastRecognitionTime = eventTime;
                     if (DEBUG_LISTENER) {
@@ -614,7 +687,7 @@
                             mPointerId, sAggregratedPointers.getPointerSize()));
                 }
                 sInGesture = false;
-                sLastBatchInputTime = eventTime;
+                sTimeRecorder.onEndBatchInput(eventTime);
                 mListener.onEndBatchInput(sAggregratedPointers);
             }
         }
@@ -692,7 +765,7 @@
                 sGestureFirstDownTime = eventTime;
             }
             mGestureStrokeWithPreviewPoints.onDownEvent(x, y, eventTime, sGestureFirstDownTime,
-                    sLastLetterTypingUpTime);
+                    sTimeRecorder.getLastLetterTypingTime());
         }
     }
 
@@ -933,11 +1006,7 @@
             return;
         }
         if (currentKey != null && !currentKey.isRepeatable()) {
-            detectAndSendKey(currentKey, mKeyX, mKeyY);
-            final int code = currentKey.mCode;
-            if (Keyboard.isLetterCode(code) && code != Keyboard.CODE_SPACE) {
-                sLastLetterTypingUpTime = eventTime;
-            }
+            detectAndSendKey(currentKey, mKeyX, mKeyY, eventTime);
         }
     }
 
@@ -988,7 +1057,7 @@
 
     public void onRegisterKey(final Key key) {
         if (key != null) {
-            detectAndSendKey(key, key.mX, key.mY);
+            detectAndSendKey(key, key.mX, key.mY, SystemClock.uptimeMillis());
             mTimerProxy.startTypingStateTimer(key);
         }
     }
@@ -1014,14 +1083,14 @@
         }
     }
 
-    private void detectAndSendKey(final Key key, final int x, final int y) {
+    private void detectAndSendKey(final Key key, final int x, final int y, final long eventTime) {
         if (key == null) {
             callListenerOnCancelInput();
             return;
         }
 
         final int code = key.mCode;
-        callListenerOnCodeInput(key, code, x, y);
+        callListenerOnCodeInput(key, code, x, y, eventTime);
         callListenerOnRelease(key, code, false);
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
index c0e92df..9b1a201 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
@@ -14,10 +14,13 @@
 
 package com.android.inputmethod.keyboard.internal;
 
+import android.content.res.TypedArray;
 import android.util.Log;
 
 import com.android.inputmethod.latin.InputPointers;
+import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.ResizableIntArray;
+import com.android.inputmethod.latin.ResourceUtils;
 
 public class GestureStroke {
     private static final String TAG = GestureStroke.class.getSimpleName();
@@ -31,6 +34,8 @@
     private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
     private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
 
+    private final GestureStrokeParams mParams;
+
     private int mKeyWidth; // pixel
     // Static threshold for starting gesture detection
     private int mDetectFastMoveSpeedThreshold; // pixel /sec
@@ -51,53 +56,100 @@
     private int mIncrementalRecognitionSize;
     private int mLastIncrementalBatchSize;
 
-    // TODO: Move some of these to resource.
+    public static final class GestureStrokeParams {
+        // Static threshold for gesture after fast typing
+        public final int mStaticTimeThresholdAfterFastTyping; // msec
+        // Static threshold for starting gesture detection
+        public final float mDetectFastMoveSpeedThreshold; // keyWidth/sec
+        // Dynamic threshold for gesture after fast typing
+        public final int mDynamicThresholdDecayDuration; // msec
+        // Time based threshold values
+        public final int mDynamicTimeThresholdFrom; // msec
+        public final int mDynamicTimeThresholdTo; // msec
+        // Distance based threshold values
+        public final float mDynamicDistanceThresholdFrom; // keyWidth
+        public final float mDynamicDistanceThresholdTo; // keyWidth
+        // Parameters for gesture sampling
+        public final float mSamplingMinimumDistance; // keyWidth
+        // Parameters for gesture recognition
+        public final int mRecognitionMinimumTime; // msec
+        public final float mRecognitionSpeedThreshold; // keyWidth/sec
 
-    // Static threshold for gesture after fast typing
-    public static final int GESTURE_STATIC_TIME_THRESHOLD_AFTER_FAST_TYPING = 350; // msec
+        // Default GestureStroke parameters for test.
+        public static final GestureStrokeParams FOR_TEST = new GestureStrokeParams();
+        public static final GestureStrokeParams DEFAULT = FOR_TEST;
 
-    // Static threshold for starting gesture detection
-    private static final float DETECT_FAST_MOVE_SPEED_THRESHOLD = 1.5f; // keyWidth / sec
+        private GestureStrokeParams() {
+            // These parameter values are default and intended for testing.
+            mStaticTimeThresholdAfterFastTyping = 350; // msec
+            mDetectFastMoveSpeedThreshold = 1.5f; // keyWidth / sec
+            mDynamicThresholdDecayDuration = 450; // msec
+            mDynamicTimeThresholdFrom = 300; // msec
+            mDynamicTimeThresholdTo = 20; // msec
+            mDynamicDistanceThresholdFrom = 6.0f; // keyWidth
+            mDynamicDistanceThresholdTo = 0.35f; // keyWidth
+            // The following parameters' change will affect the result of regression test.
+            mSamplingMinimumDistance = 1.0f / 6.0f; // keyWidth
+            mRecognitionMinimumTime = 100; // msec
+            mRecognitionSpeedThreshold = 5.5f; // keyWidth / sec
+        }
 
-    // Dynamic threshold for gesture after fast typing
-    private static final int GESTURE_DYNAMIC_THRESHOLD_DECAY_DURATION = 450; // msec
-    // Time based threshold values
-    private static final int GESTURE_DYNAMIC_TIME_THRESHOLD_FROM = 300; // msec
-    private static final int GESTURE_DYNAMIC_TIME_THRESHOLD_TO = 20; // msec
-    // Distance based threshold values
-    private static final float GESTURE_DYNAMIC_DISTANCE_THRESHOLD_FROM = 6.0f; // keyWidth
-    private static final float GESTURE_DYNAMIC_DISTANCE_THRESHOLD_TO = 0.35f; // keyWidth
-
-    // Parameters for gesture sampling
-    private static final float GESTURE_SAMPLING_MINIMUM_DISTANCE = 1.0f / 6.0f; // keyWidth
-
-    // Parameters for gesture recognition
-    private static final int GESTURE_RECOGNITION_MINIMUM_TIME = 100; // msec
-    private static final float GESTURE_RECOGNITION_SPEED_THRESHOLD = 5.5f; // keyWidth / sec
+        public GestureStrokeParams(final TypedArray mainKeyboardViewAttr) {
+            mStaticTimeThresholdAfterFastTyping = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_gestureStaticTimeThresholdAfterFastTyping,
+                    DEFAULT.mStaticTimeThresholdAfterFastTyping);
+            mDetectFastMoveSpeedThreshold = ResourceUtils.getFraction(mainKeyboardViewAttr,
+                    R.styleable.MainKeyboardView_gestureDetectFastMoveSpeedThreshold,
+                    DEFAULT.mDetectFastMoveSpeedThreshold);
+            mDynamicThresholdDecayDuration = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_gestureDynamicThresholdDecayDuration,
+                    DEFAULT.mDynamicThresholdDecayDuration);
+            mDynamicTimeThresholdFrom = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_gestureDynamicTimeThresholdFrom,
+                    DEFAULT.mDynamicTimeThresholdFrom);
+            mDynamicTimeThresholdTo = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_gestureDynamicTimeThresholdTo,
+                    DEFAULT.mDynamicTimeThresholdTo);
+            mDynamicDistanceThresholdFrom = ResourceUtils.getFraction(mainKeyboardViewAttr,
+                    R.styleable.MainKeyboardView_gestureDynamicDistanceThresholdFrom,
+                    DEFAULT.mDynamicDistanceThresholdFrom);
+            mDynamicDistanceThresholdTo = ResourceUtils.getFraction(mainKeyboardViewAttr,
+                    R.styleable.MainKeyboardView_gestureDynamicDistanceThresholdTo,
+                    DEFAULT.mDynamicDistanceThresholdTo);
+            mSamplingMinimumDistance = ResourceUtils.getFraction(mainKeyboardViewAttr,
+                    R.styleable.MainKeyboardView_gestureSamplingMinimumDistance,
+                    DEFAULT.mSamplingMinimumDistance);
+            mRecognitionMinimumTime = mainKeyboardViewAttr.getInt(
+                    R.styleable.MainKeyboardView_gestureRecognitionMinimumTime,
+                    DEFAULT.mRecognitionMinimumTime);
+            mRecognitionSpeedThreshold = ResourceUtils.getFraction(mainKeyboardViewAttr,
+                    R.styleable.MainKeyboardView_gestureRecognitionSpeedThreshold,
+                    DEFAULT.mRecognitionSpeedThreshold);
+        }
+    }
 
     private static final int MSEC_PER_SEC = 1000;
 
-    public GestureStroke(final int pointerId) {
+    public GestureStroke(final int pointerId, final GestureStrokeParams params) {
         mPointerId = pointerId;
+        mParams = params;
     }
 
     public void setKeyboardGeometry(final int keyWidth) {
         mKeyWidth = keyWidth;
         // TODO: Find an appropriate base metric for these length. Maybe diagonal length of the key?
-        mDetectFastMoveSpeedThreshold = (int)(keyWidth * DETECT_FAST_MOVE_SPEED_THRESHOLD);
+        mDetectFastMoveSpeedThreshold = (int)(keyWidth * mParams.mDetectFastMoveSpeedThreshold);
         mGestureDynamicDistanceThresholdFrom =
-                (int)(keyWidth * GESTURE_DYNAMIC_DISTANCE_THRESHOLD_FROM);
-        mGestureDynamicDistanceThresholdTo =
-                (int)(keyWidth * GESTURE_DYNAMIC_DISTANCE_THRESHOLD_TO);
-        mGestureSamplingMinimumDistance = (int)(keyWidth * GESTURE_SAMPLING_MINIMUM_DISTANCE);
-        mGestureRecognitionSpeedThreshold =
-                (int)(keyWidth * GESTURE_RECOGNITION_SPEED_THRESHOLD);
+                (int)(keyWidth * mParams.mDynamicDistanceThresholdFrom);
+        mGestureDynamicDistanceThresholdTo = (int)(keyWidth * mParams.mDynamicDistanceThresholdTo);
+        mGestureSamplingMinimumDistance = (int)(keyWidth * mParams.mSamplingMinimumDistance);
+        mGestureRecognitionSpeedThreshold = (int)(keyWidth * mParams.mRecognitionSpeedThreshold);
         if (DEBUG) {
             Log.d(TAG, String.format(
                     "[%d] setKeyboardGeometry: keyWidth=%3d tT=%3d >> %3d tD=%3d >> %3d",
                     mPointerId, keyWidth,
-                    GESTURE_DYNAMIC_TIME_THRESHOLD_FROM,
-                    GESTURE_DYNAMIC_TIME_THRESHOLD_TO,
+                    mParams.mDynamicTimeThresholdFrom,
+                    mParams.mDynamicTimeThresholdTo,
                     mGestureDynamicDistanceThresholdFrom,
                     mGestureDynamicDistanceThresholdTo));
         }
@@ -107,7 +159,7 @@
             final long gestureFirstDownTime, final long lastTypingTime) {
         reset();
         final long elapsedTimeAfterTyping = downTime - lastTypingTime;
-        if (elapsedTimeAfterTyping < GESTURE_STATIC_TIME_THRESHOLD_AFTER_FAST_TYPING) {
+        if (elapsedTimeAfterTyping < mParams.mStaticTimeThresholdAfterFastTyping) {
             mAfterFastTyping = true;
         }
         if (DEBUG) {
@@ -119,23 +171,23 @@
     }
 
     private int getGestureDynamicDistanceThreshold(final int deltaTime) {
-        if (!mAfterFastTyping || deltaTime >= GESTURE_DYNAMIC_THRESHOLD_DECAY_DURATION) {
+        if (!mAfterFastTyping || deltaTime >= mParams.mDynamicThresholdDecayDuration) {
             return mGestureDynamicDistanceThresholdTo;
         }
         final int decayedThreshold =
                 (mGestureDynamicDistanceThresholdFrom - mGestureDynamicDistanceThresholdTo)
-                * deltaTime / GESTURE_DYNAMIC_THRESHOLD_DECAY_DURATION;
+                * deltaTime / mParams.mDynamicThresholdDecayDuration;
         return mGestureDynamicDistanceThresholdFrom - decayedThreshold;
     }
 
     private int getGestureDynamicTimeThreshold(final int deltaTime) {
-        if (!mAfterFastTyping || deltaTime >= GESTURE_DYNAMIC_THRESHOLD_DECAY_DURATION) {
-            return GESTURE_DYNAMIC_TIME_THRESHOLD_TO;
+        if (!mAfterFastTyping || deltaTime >= mParams.mDynamicThresholdDecayDuration) {
+            return mParams.mDynamicTimeThresholdTo;
         }
         final int decayedThreshold =
-                (GESTURE_DYNAMIC_TIME_THRESHOLD_FROM - GESTURE_DYNAMIC_TIME_THRESHOLD_TO)
-                * deltaTime / GESTURE_DYNAMIC_THRESHOLD_DECAY_DURATION;
-        return GESTURE_DYNAMIC_TIME_THRESHOLD_FROM - decayedThreshold;
+                (mParams.mDynamicTimeThresholdFrom - mParams.mDynamicTimeThresholdTo)
+                * deltaTime / mParams.mDynamicThresholdDecayDuration;
+        return mParams.mDynamicTimeThresholdFrom - decayedThreshold;
     }
 
     public boolean isStartOfAGesture() {
@@ -249,9 +301,9 @@
         }
     }
 
-    public static final boolean hasRecognitionTimePast(
+    public final boolean hasRecognitionTimePast(
             final long currentTime, final long lastRecognitionTime) {
-        return currentTime > lastRecognitionTime + GESTURE_RECOGNITION_MINIMUM_TIME;
+        return currentTime > lastRecognitionTime + mParams.mRecognitionMinimumTime;
     }
 
     public void appendAllBatchPoints(final InputPointers out) {
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
index 05e0a2e..8192c90 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
@@ -33,8 +33,8 @@
     // 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);
+    public GestureStrokeWithPreviewPoints(final int pointerId, final GestureStrokeParams params) {
+        super(pointerId, params);
     }
 
     @Override