2010-08-25 09:17:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-08-25 09:17:46 +01:00
|
|
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public class SwipeTracker {
|
2010-08-25 08:47:16 +01:00
|
|
|
private static final int NUM_PAST = 4;
|
|
|
|
private static final int LONGEST_PAST_TIME = 200;
|
2010-08-25 09:17:46 +01:00
|
|
|
|
2010-08-25 08:47:16 +01:00
|
|
|
final EventRingBuffer mBuffer = new EventRingBuffer(NUM_PAST);
|
2010-08-25 09:17:46 +01:00
|
|
|
|
2010-08-25 08:47:16 +01:00
|
|
|
private float mYVelocity;
|
|
|
|
private float mXVelocity;
|
2010-08-25 09:17:46 +01:00
|
|
|
|
|
|
|
public void addMovement(MotionEvent ev) {
|
|
|
|
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
|
2010-08-25 08:47:16 +01:00
|
|
|
mBuffer.clear();
|
2010-08-25 09:17:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
long time = ev.getEventTime();
|
2010-08-25 08:47:16 +01:00
|
|
|
final int count = ev.getHistorySize();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
addPoint(ev.getHistoricalX(i), ev.getHistoricalY(i), ev.getHistoricalEventTime(i));
|
2010-08-25 09:17:46 +01:00
|
|
|
}
|
|
|
|
addPoint(ev.getX(), ev.getY(), time);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addPoint(float x, float y, long time) {
|
2010-08-25 08:47:16 +01:00
|
|
|
final EventRingBuffer buffer = mBuffer;
|
|
|
|
while (buffer.size() > 0) {
|
|
|
|
long lastT = buffer.getTime(0);
|
|
|
|
if (lastT >= time - LONGEST_PAST_TIME)
|
2010-08-25 09:17:46 +01:00
|
|
|
break;
|
2010-08-25 08:47:16 +01:00
|
|
|
buffer.dropOldest();
|
2010-08-25 09:17:46 +01:00
|
|
|
}
|
2010-08-25 08:47:16 +01:00
|
|
|
buffer.add(x, y, time);
|
2010-08-25 09:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void computeCurrentVelocity(int units) {
|
|
|
|
computeCurrentVelocity(units, Float.MAX_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void computeCurrentVelocity(int units, float maxVelocity) {
|
2010-08-25 08:47:16 +01:00
|
|
|
final EventRingBuffer buffer = mBuffer;
|
|
|
|
final float oldestX = buffer.getX(0);
|
|
|
|
final float oldestY = buffer.getY(0);
|
|
|
|
final long oldestTime = buffer.getTime(0);
|
2010-08-25 09:17:46 +01:00
|
|
|
|
|
|
|
float accumX = 0;
|
|
|
|
float accumY = 0;
|
2010-08-25 08:47:16 +01:00
|
|
|
final int count = buffer.size();
|
|
|
|
for (int pos = 1; pos < count; pos++) {
|
|
|
|
final int dur = (int)(buffer.getTime(pos) - oldestTime);
|
2010-08-25 09:17:46 +01:00
|
|
|
if (dur == 0) continue;
|
2010-08-25 08:47:16 +01:00
|
|
|
float dist = buffer.getX(pos) - oldestX;
|
|
|
|
float vel = (dist / dur) * units; // pixels/frame.
|
2010-08-25 09:17:46 +01:00
|
|
|
if (accumX == 0) accumX = vel;
|
|
|
|
else accumX = (accumX + vel) * .5f;
|
|
|
|
|
2010-08-25 08:47:16 +01:00
|
|
|
dist = buffer.getY(pos) - oldestY;
|
|
|
|
vel = (dist / dur) * units; // pixels/frame.
|
2010-08-25 09:17:46 +01:00
|
|
|
if (accumY == 0) accumY = vel;
|
|
|
|
else accumY = (accumY + vel) * .5f;
|
|
|
|
}
|
|
|
|
mXVelocity = accumX < 0.0f ? Math.max(accumX, -maxVelocity)
|
|
|
|
: Math.min(accumX, maxVelocity);
|
|
|
|
mYVelocity = accumY < 0.0f ? Math.max(accumY, -maxVelocity)
|
|
|
|
: Math.min(accumY, maxVelocity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getXVelocity() {
|
|
|
|
return mXVelocity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getYVelocity() {
|
|
|
|
return mYVelocity;
|
|
|
|
}
|
2010-08-25 08:47:16 +01:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public static class EventRingBuffer {
|
2010-08-25 08:47:16 +01:00
|
|
|
private final int bufSize;
|
|
|
|
private final float xBuf[];
|
|
|
|
private final float yBuf[];
|
|
|
|
private final long timeBuf[];
|
|
|
|
private int top; // points new event
|
|
|
|
private int end; // points oldest event
|
|
|
|
private int count; // the number of valid data
|
|
|
|
|
|
|
|
public EventRingBuffer(int max) {
|
|
|
|
this.bufSize = max;
|
|
|
|
xBuf = new float[max];
|
|
|
|
yBuf = new float[max];
|
|
|
|
timeBuf = new long[max];
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
top = end = count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position 0 points oldest event
|
|
|
|
private int index(int pos) {
|
|
|
|
return (end + pos) % bufSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int advance(int index) {
|
|
|
|
return (index + 1) % bufSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void add(float x, float y, long time) {
|
|
|
|
xBuf[top] = x;
|
|
|
|
yBuf[top] = y;
|
|
|
|
timeBuf[top] = time;
|
|
|
|
top = advance(top);
|
|
|
|
if (count < bufSize) {
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
end = advance(end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getX(int pos) {
|
|
|
|
return xBuf[index(pos)];
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getY(int pos) {
|
|
|
|
return yBuf[index(pos)];
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getTime(int pos) {
|
|
|
|
return timeBuf[index(pos)];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dropOldest() {
|
|
|
|
count--;
|
|
|
|
end = advance(end);
|
|
|
|
}
|
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|