• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.android.inputmethod.keyboard.internal;
16 
17 import com.android.inputmethod.latin.ResizableIntArray;
18 
19 public final class GestureStrokeWithPreviewPoints extends GestureStroke {
20     public static final int PREVIEW_CAPACITY = 256;
21 
22     private final ResizableIntArray mPreviewEventTimes = new ResizableIntArray(PREVIEW_CAPACITY);
23     private final ResizableIntArray mPreviewXCoordinates = new ResizableIntArray(PREVIEW_CAPACITY);
24     private final ResizableIntArray mPreviewYCoordinates = new ResizableIntArray(PREVIEW_CAPACITY);
25 
26     private int mStrokeId;
27     private int mLastPreviewSize;
28 
29     private int mMinPreviewSampleLengthSquare;
30     private int mLastX;
31     private int mLastY;
32 
33     // TODO: Move this to resource.
34     private static final float MIN_PREVIEW_SAMPLE_LENGTH_RATIO_TO_KEY_WIDTH = 0.1f;
35 
GestureStrokeWithPreviewPoints(final int pointerId, final GestureStrokeParams params)36     public GestureStrokeWithPreviewPoints(final int pointerId, final GestureStrokeParams params) {
37         super(pointerId, params);
38     }
39 
40     @Override
reset()41     protected void reset() {
42         super.reset();
43         mStrokeId++;
44         mLastPreviewSize = 0;
45         mPreviewEventTimes.setLength(0);
46         mPreviewXCoordinates.setLength(0);
47         mPreviewYCoordinates.setLength(0);
48     }
49 
getGestureStrokeId()50     public int getGestureStrokeId() {
51         return mStrokeId;
52     }
53 
getGestureStrokePreviewSize()54     public int getGestureStrokePreviewSize() {
55         return mPreviewEventTimes.getLength();
56     }
57 
58     @Override
setKeyboardGeometry(final int keyWidth)59     public void setKeyboardGeometry(final int keyWidth) {
60         super.setKeyboardGeometry(keyWidth);
61         final float sampleLength = keyWidth * MIN_PREVIEW_SAMPLE_LENGTH_RATIO_TO_KEY_WIDTH;
62         mMinPreviewSampleLengthSquare = (int)(sampleLength * sampleLength);
63     }
64 
needsSampling(final int x, final int y)65     private boolean needsSampling(final int x, final int y) {
66         final int dx = x - mLastX;
67         final int dy = y - mLastY;
68         return dx * dx + dy * dy >= mMinPreviewSampleLengthSquare;
69     }
70 
71     @Override
addPoint(final int x, final int y, final int time, final boolean isMajorEvent)72     public void addPoint(final int x, final int y, final int time, final boolean isMajorEvent) {
73         super.addPoint(x, y, time, isMajorEvent);
74         if (isMajorEvent || needsSampling(x, y)) {
75             mPreviewEventTimes.add(time);
76             mPreviewXCoordinates.add(x);
77             mPreviewYCoordinates.add(y);
78             mLastX = x;
79             mLastY = y;
80         }
81     }
82 
appendPreviewStroke(final ResizableIntArray eventTimes, final ResizableIntArray xCoords, final ResizableIntArray yCoords)83     public void appendPreviewStroke(final ResizableIntArray eventTimes,
84             final ResizableIntArray xCoords, final ResizableIntArray yCoords) {
85         final int length = mPreviewEventTimes.getLength() - mLastPreviewSize;
86         if (length <= 0) {
87             return;
88         }
89         eventTimes.append(mPreviewEventTimes, mLastPreviewSize, length);
90         xCoords.append(mPreviewXCoordinates, mLastPreviewSize, length);
91         yCoords.append(mPreviewYCoordinates, mLastPreviewSize, length);
92         mLastPreviewSize = mPreviewEventTimes.getLength();
93     }
94 }
95