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 android.content.res.TypedArray;
|
|
|
|
import android.graphics.Canvas;
|
2013-05-09 10:16:11 +00:00
|
|
|
import android.graphics.Color;
|
2012-08-20 03:57:34 +00:00
|
|
|
import android.graphics.Paint;
|
2012-09-14 09:10:39 +00:00
|
|
|
import android.graphics.Path;
|
2012-09-12 07:28:54 +00:00
|
|
|
import android.graphics.Rect;
|
2012-08-20 03:57:34 +00:00
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.Constants;
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.ResizableIntArray;
|
|
|
|
|
2013-01-21 06:57:32 +00:00
|
|
|
/*
|
2013-01-21 07:16:48 +00:00
|
|
|
* @attr ref R.styleable#MainKeyboardView_gesturePreviewTrailFadeoutStartDelay
|
|
|
|
* @attr ref R.styleable#MainKeyboardView_gesturePreviewTrailFadeoutDuration
|
|
|
|
* @attr ref R.styleable#MainKeyboardView_gesturePreviewTrailUpdateInterval
|
|
|
|
* @attr ref R.styleable#MainKeyboardView_gesturePreviewTrailColor
|
|
|
|
* @attr ref R.styleable#MainKeyboardView_gesturePreviewTrailWidth
|
2013-01-21 06:57:32 +00:00
|
|
|
*/
|
2012-09-06 03:12:10 +00:00
|
|
|
final class GesturePreviewTrail {
|
2013-05-09 10:16:11 +00:00
|
|
|
public static final boolean DBG_SHOW_POINTS = false;
|
|
|
|
public static final int POINT_TYPE_SAMPLED = 0;
|
|
|
|
public static final int POINT_TYPE_INTERPOLATED = 1;
|
|
|
|
public static final int POINT_TYPE_COMPROMISED = 2;
|
|
|
|
|
2012-09-14 09:10:39 +00:00
|
|
|
private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewPoints.PREVIEW_CAPACITY;
|
2012-08-20 03:57:34 +00:00
|
|
|
|
2013-04-10 08:57:07 +00:00
|
|
|
// These three {@link ResizableIntArray}s should be synchronized by {@link #mEventTimes}.
|
2012-08-20 03:57:34 +00:00
|
|
|
private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY);
|
2013-05-09 10:16:11 +00:00
|
|
|
private final ResizableIntArray mPointTypes = new ResizableIntArray(
|
|
|
|
DBG_SHOW_POINTS ? DEFAULT_CAPACITY : 0);
|
2012-08-24 08:48:23 +00:00
|
|
|
private int mCurrentStrokeId = -1;
|
2012-09-07 04:25:34 +00:00
|
|
|
// The wall time of the zero value in {@link #mEventTimes}
|
|
|
|
private long mCurrentTimeBase;
|
2012-08-27 05:43:39 +00:00
|
|
|
private int mTrailStartIndex;
|
2013-03-07 10:18:13 +00:00
|
|
|
private int mLastInterpolatedDrawIndex;
|
2012-08-20 03:57:34 +00:00
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
static final class Params {
|
|
|
|
public final int mTrailColor;
|
|
|
|
public final float mTrailStartWidth;
|
|
|
|
public final float mTrailEndWidth;
|
2013-04-16 07:44:23 +00:00
|
|
|
public final float mTrailBodyRatio;
|
|
|
|
public boolean mTrailShadowEnabled;
|
|
|
|
public final float mTrailShadowRatio;
|
2012-08-20 03:57:34 +00:00
|
|
|
public final int mFadeoutStartDelay;
|
|
|
|
public final int mFadeoutDuration;
|
|
|
|
public final int mUpdateInterval;
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
public final int mTrailLingerDuration;
|
|
|
|
|
2013-01-21 07:16:48 +00:00
|
|
|
public Params(final TypedArray mainKeyboardViewAttr) {
|
|
|
|
mTrailColor = mainKeyboardViewAttr.getColor(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailColor, 0);
|
|
|
|
mTrailStartWidth = mainKeyboardViewAttr.getDimension(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailStartWidth, 0.0f);
|
|
|
|
mTrailEndWidth = mainKeyboardViewAttr.getDimension(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailEndWidth, 0.0f);
|
2013-04-16 07:44:23 +00:00
|
|
|
final int PERCENTAGE_INT = 100;
|
|
|
|
mTrailBodyRatio = (float)mainKeyboardViewAttr.getInt(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailBodyRatio, PERCENTAGE_INT)
|
|
|
|
/ (float)PERCENTAGE_INT;
|
|
|
|
final int trailShadowRatioInt = mainKeyboardViewAttr.getInt(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailShadowRatio, 0);
|
|
|
|
mTrailShadowEnabled = (trailShadowRatioInt > 0);
|
|
|
|
mTrailShadowRatio = (float)trailShadowRatioInt / (float)PERCENTAGE_INT;
|
2013-05-09 10:54:29 +00:00
|
|
|
mFadeoutStartDelay = DBG_SHOW_POINTS ? 2000 : mainKeyboardViewAttr.getInt(
|
2013-01-21 07:16:48 +00:00
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailFadeoutStartDelay, 0);
|
2013-05-09 10:54:29 +00:00
|
|
|
mFadeoutDuration = DBG_SHOW_POINTS ? 200 : mainKeyboardViewAttr.getInt(
|
2013-01-21 07:16:48 +00:00
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailFadeoutDuration, 0);
|
2012-09-06 03:12:10 +00:00
|
|
|
mTrailLingerDuration = mFadeoutStartDelay + mFadeoutDuration;
|
2013-01-21 07:16:48 +00:00
|
|
|
mUpdateInterval = mainKeyboardViewAttr.getInt(
|
|
|
|
R.styleable.MainKeyboardView_gesturePreviewTrailUpdateInterval, 0);
|
2012-08-20 03:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 04:25:34 +00:00
|
|
|
// Use this value as imaginary zero because x-coordinates may be zero.
|
|
|
|
private static final int DOWN_EVENT_MARKER = -128;
|
|
|
|
|
2012-08-20 03:57:34 +00:00
|
|
|
private static int markAsDownEvent(final int xCoord) {
|
|
|
|
return DOWN_EVENT_MARKER - xCoord;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isDownEventXCoord(final int xCoordOrMark) {
|
|
|
|
return xCoordOrMark <= DOWN_EVENT_MARKER;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getXCoordValue(final int xCoordOrMark) {
|
|
|
|
return isDownEventXCoord(xCoordOrMark)
|
|
|
|
? DOWN_EVENT_MARKER - xCoordOrMark : xCoordOrMark;
|
|
|
|
}
|
|
|
|
|
2012-09-14 09:10:39 +00:00
|
|
|
public void addStroke(final GestureStrokeWithPreviewPoints stroke, final long downTime) {
|
2013-04-10 08:57:07 +00:00
|
|
|
synchronized (mEventTimes) {
|
|
|
|
addStrokeLocked(stroke, downTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addStrokeLocked(final GestureStrokeWithPreviewPoints stroke, final long downTime) {
|
2013-04-17 02:58:09 +00:00
|
|
|
final int trailSize = mEventTimes.getLength();
|
2012-08-20 03:57:34 +00:00
|
|
|
stroke.appendPreviewStroke(mEventTimes, mXCoordinates, mYCoordinates);
|
2012-09-07 04:25:34 +00:00
|
|
|
if (mEventTimes.getLength() == trailSize) {
|
2012-08-20 03:57:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-07 04:25:34 +00:00
|
|
|
final int[] eventTimes = mEventTimes.getPrimitiveArray();
|
|
|
|
final int strokeId = stroke.getGestureStrokeId();
|
2013-03-07 10:18:13 +00:00
|
|
|
// Because interpolation algorithm in {@link GestureStrokeWithPreviewPoints} can't determine
|
|
|
|
// the interpolated points in the last segment of gesture stroke, it may need recalculation
|
|
|
|
// of interpolation when new segments are added to the stroke.
|
|
|
|
// {@link #mLastInterpolatedDrawIndex} holds the start index of the last segment. It may
|
|
|
|
// be updated by the interpolation
|
|
|
|
// {@link GestureStrokeWithPreviewPoints#interpolatePreviewStroke}
|
|
|
|
// or by animation {@link #drawGestureTrail(Canvas,Paint,Rect,Params)} below.
|
|
|
|
final int lastInterpolatedIndex = (strokeId == mCurrentStrokeId)
|
|
|
|
? mLastInterpolatedDrawIndex : trailSize;
|
|
|
|
mLastInterpolatedDrawIndex = stroke.interpolateStrokeAndReturnStartIndexOfLastSegment(
|
2013-05-09 10:16:11 +00:00
|
|
|
lastInterpolatedIndex, mEventTimes, mXCoordinates, mYCoordinates, mPointTypes);
|
2012-09-07 04:25:34 +00:00
|
|
|
if (strokeId != mCurrentStrokeId) {
|
|
|
|
final int elapsedTime = (int)(downTime - mCurrentTimeBase);
|
2012-08-27 05:43:39 +00:00
|
|
|
for (int i = mTrailStartIndex; i < trailSize; i++) {
|
2012-09-07 04:25:34 +00:00
|
|
|
// Decay the previous strokes' event times.
|
2012-08-20 03:57:34 +00:00
|
|
|
eventTimes[i] -= elapsedTime;
|
|
|
|
}
|
2012-09-07 04:25:34 +00:00
|
|
|
final int[] xCoords = mXCoordinates.getPrimitiveArray();
|
|
|
|
final int downIndex = trailSize;
|
|
|
|
xCoords[downIndex] = markAsDownEvent(xCoords[downIndex]);
|
|
|
|
mCurrentTimeBase = downTime - eventTimes[downIndex];
|
2012-08-20 03:57:34 +00:00
|
|
|
mCurrentStrokeId = strokeId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 06:13:19 +00:00
|
|
|
/**
|
|
|
|
* Calculate the alpha of a gesture trail.
|
|
|
|
* A gesture trail starts from fully opaque. After mFadeStartDelay has been passed, the alpha
|
|
|
|
* of a trail reduces in proportion to the elapsed time. Then after mFadeDuration has been
|
|
|
|
* passed, a trail becomes fully transparent.
|
|
|
|
*
|
|
|
|
* @param elapsedTime the elapsed time since a trail has been made.
|
|
|
|
* @param params gesture trail display parameters
|
|
|
|
* @return the width of a gesture trail
|
|
|
|
*/
|
2012-09-06 03:12:10 +00:00
|
|
|
private static int getAlpha(final int elapsedTime, final Params params) {
|
|
|
|
if (elapsedTime < params.mFadeoutStartDelay) {
|
2012-08-20 03:57:34 +00:00
|
|
|
return Constants.Color.ALPHA_OPAQUE;
|
|
|
|
}
|
|
|
|
final int decreasingAlpha = Constants.Color.ALPHA_OPAQUE
|
2012-09-06 03:12:10 +00:00
|
|
|
* (elapsedTime - params.mFadeoutStartDelay)
|
|
|
|
/ params.mFadeoutDuration;
|
2012-08-20 03:57:34 +00:00
|
|
|
return Constants.Color.ALPHA_OPAQUE - decreasingAlpha;
|
|
|
|
}
|
|
|
|
|
2012-12-14 06:13:19 +00:00
|
|
|
/**
|
|
|
|
* Calculate the width of a gesture trail.
|
|
|
|
* A gesture trail starts from the width of mTrailStartWidth and reduces its width in proportion
|
|
|
|
* to the elapsed time. After mTrailEndWidth has been passed, the width becomes mTraiLEndWidth.
|
|
|
|
*
|
|
|
|
* @param elapsedTime the elapsed time since a trail has been made.
|
|
|
|
* @param params gesture trail display parameters
|
|
|
|
* @return the width of a gesture trail
|
|
|
|
*/
|
2012-09-06 03:12:10 +00:00
|
|
|
private static float getWidth(final int elapsedTime, final Params params) {
|
2012-12-14 06:13:19 +00:00
|
|
|
final float deltaWidth = params.mTrailStartWidth - params.mTrailEndWidth;
|
2013-01-18 08:37:21 +00:00
|
|
|
return params.mTrailStartWidth - (deltaWidth * elapsedTime) / params.mTrailLingerDuration;
|
2012-09-06 03:12:10 +00:00
|
|
|
}
|
|
|
|
|
2012-11-29 05:28:59 +00:00
|
|
|
private final RoundedLine mRoundedLine = new RoundedLine();
|
2013-01-16 08:47:54 +00:00
|
|
|
private final Rect mRoundedLineBounds = new Rect();
|
2012-09-14 09:10:39 +00:00
|
|
|
|
2012-08-20 03:57:34 +00:00
|
|
|
/**
|
|
|
|
* Draw gesture preview trail
|
|
|
|
* @param canvas The canvas to draw the gesture preview trail
|
|
|
|
* @param paint The paint object to be used to draw the gesture preview trail
|
2012-09-12 07:28:54 +00:00
|
|
|
* @param outBoundsRect the bounding box of this gesture trail drawing
|
2012-09-06 03:12:10 +00:00
|
|
|
* @param params The drawing parameters of gesture preview trail
|
2012-08-20 03:57:34 +00:00
|
|
|
* @return true if some gesture preview trails remain to be drawn
|
|
|
|
*/
|
2012-09-12 07:28:54 +00:00
|
|
|
public boolean drawGestureTrail(final Canvas canvas, final Paint paint,
|
|
|
|
final Rect outBoundsRect, final Params params) {
|
2013-04-10 08:57:07 +00:00
|
|
|
synchronized (mEventTimes) {
|
|
|
|
return drawGestureTrailLocked(canvas, paint, outBoundsRect, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean drawGestureTrailLocked(final Canvas canvas, final Paint paint,
|
|
|
|
final Rect outBoundsRect, final Params params) {
|
2013-01-16 08:47:54 +00:00
|
|
|
// Initialize bounds rectangle.
|
|
|
|
outBoundsRect.setEmpty();
|
2012-08-20 03:57:34 +00:00
|
|
|
final int trailSize = mEventTimes.getLength();
|
|
|
|
if (trailSize == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final int[] eventTimes = mEventTimes.getPrimitiveArray();
|
|
|
|
final int[] xCoords = mXCoordinates.getPrimitiveArray();
|
|
|
|
final int[] yCoords = mYCoordinates.getPrimitiveArray();
|
2013-05-09 10:16:11 +00:00
|
|
|
final int[] pointTypes = mPointTypes.getPrimitiveArray();
|
2012-09-07 04:25:34 +00:00
|
|
|
final int sinceDown = (int)(SystemClock.uptimeMillis() - mCurrentTimeBase);
|
2012-08-20 03:57:34 +00:00
|
|
|
int startIndex;
|
2012-08-27 05:43:39 +00:00
|
|
|
for (startIndex = mTrailStartIndex; startIndex < trailSize; startIndex++) {
|
2012-08-20 03:57:34 +00:00
|
|
|
final int elapsedTime = sinceDown - eventTimes[startIndex];
|
|
|
|
// Skip too old trail points.
|
2012-09-06 03:12:10 +00:00
|
|
|
if (elapsedTime < params.mTrailLingerDuration) {
|
2012-08-20 03:57:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 05:43:39 +00:00
|
|
|
mTrailStartIndex = startIndex;
|
2012-08-20 03:57:34 +00:00
|
|
|
|
|
|
|
if (startIndex < trailSize) {
|
2012-09-06 03:12:10 +00:00
|
|
|
paint.setColor(params.mTrailColor);
|
2012-09-14 09:10:39 +00:00
|
|
|
paint.setStyle(Paint.Style.FILL);
|
2013-01-16 08:47:54 +00:00
|
|
|
final RoundedLine roundedLine = mRoundedLine;
|
2012-11-29 06:14:44 +00:00
|
|
|
int p1x = getXCoordValue(xCoords[startIndex]);
|
|
|
|
int p1y = yCoords[startIndex];
|
2012-12-14 06:13:19 +00:00
|
|
|
final int lastTime = sinceDown - eventTimes[startIndex];
|
2013-01-16 08:47:54 +00:00
|
|
|
float r1 = getWidth(lastTime, params) / 2.0f;
|
2012-12-05 19:39:54 +00:00
|
|
|
for (int i = startIndex + 1; i < trailSize; i++) {
|
2012-08-20 03:57:34 +00:00
|
|
|
final int elapsedTime = sinceDown - eventTimes[i];
|
2012-11-29 06:14:44 +00:00
|
|
|
final int p2x = getXCoordValue(xCoords[i]);
|
|
|
|
final int p2y = yCoords[i];
|
2013-01-16 08:47:54 +00:00
|
|
|
final float r2 = getWidth(elapsedTime, params) / 2.0f;
|
2012-08-20 03:57:34 +00:00
|
|
|
// Draw trail line only when the current point isn't a down point.
|
2012-09-14 09:10:39 +00:00
|
|
|
if (!isDownEventXCoord(xCoords[i])) {
|
2013-04-16 07:44:23 +00:00
|
|
|
final float body1 = r1 * params.mTrailBodyRatio;
|
|
|
|
final float body2 = r2 * params.mTrailBodyRatio;
|
|
|
|
final Path path = roundedLine.makePath(p1x, p1y, body1, p2x, p2y, body2);
|
2012-11-29 05:28:59 +00:00
|
|
|
if (path != null) {
|
2013-04-16 07:44:23 +00:00
|
|
|
roundedLine.getBounds(mRoundedLineBounds);
|
|
|
|
if (params.mTrailShadowEnabled) {
|
|
|
|
final float shadow2 = r2 * params.mTrailShadowRatio;
|
|
|
|
paint.setShadowLayer(shadow2, 0.0f, 0.0f, params.mTrailColor);
|
|
|
|
final int shadowInset = -(int)Math.ceil(shadow2);
|
|
|
|
mRoundedLineBounds.inset(shadowInset, shadowInset);
|
|
|
|
}
|
|
|
|
// Take union for the bounds.
|
|
|
|
outBoundsRect.union(mRoundedLineBounds);
|
2012-12-14 06:13:19 +00:00
|
|
|
final int alpha = getAlpha(elapsedTime, params);
|
|
|
|
paint.setAlpha(alpha);
|
2012-09-14 09:10:39 +00:00
|
|
|
canvas.drawPath(path, paint);
|
2013-05-09 10:16:11 +00:00
|
|
|
if (DBG_SHOW_POINTS) {
|
|
|
|
if (pointTypes[i] == POINT_TYPE_INTERPOLATED) {
|
|
|
|
paint.setColor(Color.RED);
|
|
|
|
} else if (pointTypes[i] == POINT_TYPE_SAMPLED) {
|
2013-05-09 10:54:29 +00:00
|
|
|
paint.setColor(0xFFA000FF);
|
2013-05-09 10:16:11 +00:00
|
|
|
} else {
|
|
|
|
paint.setColor(Color.GREEN);
|
|
|
|
}
|
|
|
|
canvas.drawCircle(p1x - 1, p1y - 1, 2, paint);
|
|
|
|
paint.setColor(params.mTrailColor);
|
|
|
|
}
|
2012-09-14 09:10:39 +00:00
|
|
|
}
|
2012-08-20 03:57:34 +00:00
|
|
|
}
|
2012-11-29 06:14:44 +00:00
|
|
|
p1x = p2x;
|
|
|
|
p1y = p2y;
|
|
|
|
r1 = r2;
|
2012-08-20 03:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int newSize = trailSize - startIndex;
|
2012-08-27 05:43:39 +00:00
|
|
|
if (newSize < startIndex) {
|
|
|
|
mTrailStartIndex = 0;
|
|
|
|
if (newSize > 0) {
|
|
|
|
System.arraycopy(eventTimes, startIndex, eventTimes, 0, newSize);
|
|
|
|
System.arraycopy(xCoords, startIndex, xCoords, 0, newSize);
|
|
|
|
System.arraycopy(yCoords, startIndex, yCoords, 0, newSize);
|
|
|
|
}
|
|
|
|
mEventTimes.setLength(newSize);
|
|
|
|
mXCoordinates.setLength(newSize);
|
|
|
|
mYCoordinates.setLength(newSize);
|
2013-05-09 10:21:49 +00:00
|
|
|
if (DBG_SHOW_POINTS) {
|
|
|
|
mPointTypes.setLength(newSize);
|
|
|
|
}
|
2013-04-17 02:58:09 +00:00
|
|
|
// The start index of the last segment of the stroke
|
|
|
|
// {@link mLastInterpolatedDrawIndex} should also be updated because all array
|
|
|
|
// elements have just been shifted for compaction or been zeroed.
|
|
|
|
mLastInterpolatedDrawIndex = Math.max(mLastInterpolatedDrawIndex - startIndex, 0);
|
2012-08-27 05:43:39 +00:00
|
|
|
}
|
2012-08-20 03:57:34 +00:00
|
|
|
return newSize > 0;
|
|
|
|
}
|
|
|
|
}
|