1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.keyboard.internal;
18 
19 import android.content.res.TypedArray;
20 
21 import com.android.inputmethod.latin.R;
22 import com.android.inputmethod.latin.utils.ResourceUtils;
23 
24 /**
25  * This class holds parameters to control how a gesture stroke is sampled and recognized.
26  * This class also has parameters to distinguish gesture input events from fast typing events.
27  *
28  * @attr ref R.styleable#MainKeyboardView_gestureStaticTimeThresholdAfterFastTyping
29  * @attr ref R.styleable#MainKeyboardView_gestureDetectFastMoveSpeedThreshold
30  * @attr ref R.styleable#MainKeyboardView_gestureDynamicThresholdDecayDuration
31  * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdFrom
32  * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdTo
33  * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdFrom
34  * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdTo
35  * @attr ref R.styleable#MainKeyboardView_gestureSamplingMinimumDistance
36  * @attr ref R.styleable#MainKeyboardView_gestureRecognitionMinimumTime
37  * @attr ref R.styleable#MainKeyboardView_gestureRecognitionSpeedThreshold
38  */
39 public final class GestureStrokeRecognitionParams {
40     // Static threshold for gesture after fast typing
41     public final int mStaticTimeThresholdAfterFastTyping; // msec
42     // Static threshold for starting gesture detection
43     public final float mDetectFastMoveSpeedThreshold; // keyWidth/sec
44     // Dynamic threshold for gesture after fast typing
45     public final int mDynamicThresholdDecayDuration; // msec
46     // Time based threshold values
47     public final int mDynamicTimeThresholdFrom; // msec
48     public final int mDynamicTimeThresholdTo; // msec
49     // Distance based threshold values
50     public final float mDynamicDistanceThresholdFrom; // keyWidth
51     public final float mDynamicDistanceThresholdTo; // keyWidth
52     // Parameters for gesture sampling
53     public final float mSamplingMinimumDistance; // keyWidth
54     // Parameters for gesture recognition
55     public final int mRecognitionMinimumTime; // msec
56     public final float mRecognitionSpeedThreshold; // keyWidth/sec
57 
58     // Default GestureStrokeRecognitionPoints parameters.
59     public static final GestureStrokeRecognitionParams DEFAULT =
60             new GestureStrokeRecognitionParams();
61 
GestureStrokeRecognitionParams()62     private GestureStrokeRecognitionParams() {
63         // These parameter values are default and intended for testing.
64         mStaticTimeThresholdAfterFastTyping = 350; // msec
65         mDetectFastMoveSpeedThreshold = 1.5f; // keyWidth/sec
66         mDynamicThresholdDecayDuration = 450; // msec
67         mDynamicTimeThresholdFrom = 300; // msec
68         mDynamicTimeThresholdTo = 20; // msec
69         mDynamicDistanceThresholdFrom = 6.0f; // keyWidth
70         mDynamicDistanceThresholdTo = 0.35f; // keyWidth
71         // The following parameters' change will affect the result of regression test.
72         mSamplingMinimumDistance = 1.0f / 6.0f; // keyWidth
73         mRecognitionMinimumTime = 100; // msec
74         mRecognitionSpeedThreshold = 5.5f; // keyWidth/sec
75     }
76 
GestureStrokeRecognitionParams(final TypedArray mainKeyboardViewAttr)77     public GestureStrokeRecognitionParams(final TypedArray mainKeyboardViewAttr) {
78         mStaticTimeThresholdAfterFastTyping = mainKeyboardViewAttr.getInt(
79                 R.styleable.MainKeyboardView_gestureStaticTimeThresholdAfterFastTyping,
80                 DEFAULT.mStaticTimeThresholdAfterFastTyping);
81         mDetectFastMoveSpeedThreshold = ResourceUtils.getFraction(mainKeyboardViewAttr,
82                 R.styleable.MainKeyboardView_gestureDetectFastMoveSpeedThreshold,
83                 DEFAULT.mDetectFastMoveSpeedThreshold);
84         mDynamicThresholdDecayDuration = mainKeyboardViewAttr.getInt(
85                 R.styleable.MainKeyboardView_gestureDynamicThresholdDecayDuration,
86                 DEFAULT.mDynamicThresholdDecayDuration);
87         mDynamicTimeThresholdFrom = mainKeyboardViewAttr.getInt(
88                 R.styleable.MainKeyboardView_gestureDynamicTimeThresholdFrom,
89                 DEFAULT.mDynamicTimeThresholdFrom);
90         mDynamicTimeThresholdTo = mainKeyboardViewAttr.getInt(
91                 R.styleable.MainKeyboardView_gestureDynamicTimeThresholdTo,
92                 DEFAULT.mDynamicTimeThresholdTo);
93         mDynamicDistanceThresholdFrom = ResourceUtils.getFraction(mainKeyboardViewAttr,
94                 R.styleable.MainKeyboardView_gestureDynamicDistanceThresholdFrom,
95                 DEFAULT.mDynamicDistanceThresholdFrom);
96         mDynamicDistanceThresholdTo = ResourceUtils.getFraction(mainKeyboardViewAttr,
97                 R.styleable.MainKeyboardView_gestureDynamicDistanceThresholdTo,
98                 DEFAULT.mDynamicDistanceThresholdTo);
99         mSamplingMinimumDistance = ResourceUtils.getFraction(mainKeyboardViewAttr,
100                 R.styleable.MainKeyboardView_gestureSamplingMinimumDistance,
101                 DEFAULT.mSamplingMinimumDistance);
102         mRecognitionMinimumTime = mainKeyboardViewAttr.getInt(
103                 R.styleable.MainKeyboardView_gestureRecognitionMinimumTime,
104                 DEFAULT.mRecognitionMinimumTime);
105         mRecognitionSpeedThreshold = ResourceUtils.getFraction(mainKeyboardViewAttr,
106                 R.styleable.MainKeyboardView_gestureRecognitionSpeedThreshold,
107                 DEFAULT.mRecognitionSpeedThreshold);
108     }
109 }
110