2012-08-20 03:57:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-08-20 03:57:34 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-08-20 03:57:34 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-08-20 03:57:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.ResizableIntArray;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class GestureStrokeWithPreviewPoints extends GestureStroke {
|
2012-08-20 03:57:34 +00:00
|
|
|
public static final int PREVIEW_CAPACITY = 256;
|
|
|
|
|
|
|
|
private final ResizableIntArray mPreviewEventTimes = new ResizableIntArray(PREVIEW_CAPACITY);
|
|
|
|
private final ResizableIntArray mPreviewXCoordinates = new ResizableIntArray(PREVIEW_CAPACITY);
|
|
|
|
private final ResizableIntArray mPreviewYCoordinates = new ResizableIntArray(PREVIEW_CAPACITY);
|
|
|
|
|
|
|
|
private int mStrokeId;
|
|
|
|
private int mLastPreviewSize;
|
|
|
|
|
2012-09-14 09:10:39 +00:00
|
|
|
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;
|
|
|
|
|
2012-10-05 03:12:54 +00:00
|
|
|
public GestureStrokeWithPreviewPoints(final int pointerId, final GestureStrokeParams params) {
|
|
|
|
super(pointerId, params);
|
2012-08-20 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-09-27 10:01:17 +00:00
|
|
|
protected void reset() {
|
2012-08-20 03:57:34 +00:00
|
|
|
super.reset();
|
|
|
|
mStrokeId++;
|
|
|
|
mLastPreviewSize = 0;
|
|
|
|
mPreviewEventTimes.setLength(0);
|
|
|
|
mPreviewXCoordinates.setLength(0);
|
|
|
|
mPreviewYCoordinates.setLength(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getGestureStrokeId() {
|
|
|
|
return mStrokeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getGestureStrokePreviewSize() {
|
|
|
|
return mPreviewEventTimes.getLength();
|
|
|
|
}
|
|
|
|
|
2012-09-14 09:10:39 +00:00
|
|
|
@Override
|
2012-11-22 06:39:28 +00:00
|
|
|
public void setKeyboardGeometry(final int keyWidth, final int keyboardHeight) {
|
|
|
|
super.setKeyboardGeometry(keyWidth, keyboardHeight);
|
2012-09-14 09:10:39 +00:00
|
|
|
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;
|
2012-09-21 03:15:02 +00:00
|
|
|
return dx * dx + dy * dy >= mMinPreviewSampleLengthSquare;
|
2012-09-14 09:10:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-20 03:57:34 +00:00
|
|
|
@Override
|
2012-11-22 06:39:28 +00:00
|
|
|
public boolean addPointOnKeyboard(final int x, final int y, final int time,
|
|
|
|
final boolean isMajorEvent) {
|
|
|
|
final boolean onValidArea = super.addPointOnKeyboard(x, y, time, isMajorEvent);
|
2012-09-21 03:15:02 +00:00
|
|
|
if (isMajorEvent || needsSampling(x, y)) {
|
2012-09-14 09:10:39 +00:00
|
|
|
mPreviewEventTimes.add(time);
|
|
|
|
mPreviewXCoordinates.add(x);
|
|
|
|
mPreviewYCoordinates.add(y);
|
2012-09-21 03:15:02 +00:00
|
|
|
mLastX = x;
|
|
|
|
mLastY = y;
|
2012-09-14 09:10:39 +00:00
|
|
|
}
|
2012-11-22 06:39:28 +00:00
|
|
|
return onValidArea;
|
2012-08-20 03:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void appendPreviewStroke(final ResizableIntArray eventTimes,
|
|
|
|
final ResizableIntArray xCoords, final ResizableIntArray yCoords) {
|
|
|
|
final int length = mPreviewEventTimes.getLength() - mLastPreviewSize;
|
|
|
|
if (length <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
eventTimes.append(mPreviewEventTimes, mLastPreviewSize, length);
|
|
|
|
xCoords.append(mPreviewXCoordinates, mLastPreviewSize, length);
|
|
|
|
yCoords.append(mPreviewYCoordinates, mLastPreviewSize, length);
|
|
|
|
mLastPreviewSize = mPreviewEventTimes.getLength();
|
|
|
|
}
|
|
|
|
}
|