implement MotionView.eventTime and VelocityTracker

This is needed to make androidx ViewPager work with touch input
This commit is contained in:
Julian Winkler
2024-02-25 17:41:26 +01:00
parent 8dafa41ea9
commit bcf252d3a6
6 changed files with 52 additions and 19 deletions

View File

@@ -1369,15 +1369,17 @@ public final class MotionEvent extends InputEvent {
int action;
float coord_x;
float coord_y;
long eventTime;
private MotionEvent() {
}
public MotionEvent(int source, int action, float coord_x, float coord_y) {
public MotionEvent(int source, int action, float coord_x, float coord_y, long eventTime) {
this.source = source;
this.action = action;
this.coord_x = coord_x;
this.coord_y = coord_y;
this.eventTime = eventTime;
}
@Override
@@ -1550,6 +1552,7 @@ public final class MotionEvent extends InputEvent {
ev.action = action;
ev.coord_x = x;
ev.coord_y = y;
ev.eventTime = eventTime;
return ev;
}
}
@@ -1816,7 +1819,7 @@ public final class MotionEvent extends InputEvent {
*/
@Override
public final long getEventTime() {
return System.currentTimeMillis();
return eventTime;
}
/**
@@ -1835,7 +1838,7 @@ public final class MotionEvent extends InputEvent {
*/
@Override
public final long getEventTimeNano() {
return System.currentTimeMillis() * 1000; // FIXME
return eventTime * 1000; // FIXME
}
/**
@@ -3030,7 +3033,7 @@ public final class MotionEvent extends InputEvent {
msg.append(", edgeFlags=0x").append(/*Integer.toHexString(getEdgeFlags())*/ "FIXME");
msg.append(", pointerCount=").append(pointerCount);
msg.append(", historySize=").append(/*getHistorySize()*/ "FIXME");
msg.append(", eventTime=").append(/*getEventTime()*/ "FIXME");
msg.append(", eventTime=").append(getEventTime());
msg.append(", downTime=").append(/*getDownTime()*/ "FIXME");
msg.append(", deviceId=").append(getDeviceId());
msg.append(", source=0x").append(Integer.toHexString(getSource()));

View File

@@ -6,15 +6,40 @@ public class VelocityTracker {
return new VelocityTracker();
}
public void addMovement(MotionEvent event) {}
private float startX;
private float startY;
private long startEventTime;
private float currentX;
private float currentY;
private long currentEventTime;
public void addMovement(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
startX = currentX = event.getX();
startY = currentY = event.getY();
startEventTime = currentEventTime = event.getEventTime();
} else {
currentX = event.getX();
currentY = event.getY();
currentEventTime = event.getEventTime();
}
}
public void recycle() {}
public void computeCurrentVelocity(int units, float maxVelocity) {}
public void computeCurrentVelocity(int units) {}
public float getXVelocity(int id) {return 0.f;}
public float getYVelocity(int id) {return 0.f;}
public float getXVelocity(int id) {
if (currentEventTime == startEventTime)
return 0.f;
return (currentX - startX) / (currentEventTime - startEventTime) * 1000;
}
public float getYVelocity(int id) {
if (currentEventTime == startEventTime)
return 0.f;
return (currentY - startY) / (currentEventTime - startEventTime) * 1000;
}
public void clear() {}
}

View File

@@ -2,6 +2,9 @@ package android.view;
import android.content.Context;
/**
* default values are mainly based on AOSPs defaults. Does not account for scaling yet.
*/
public class ViewConfiguration {
public static ViewConfiguration get(Context context) {
@@ -9,15 +12,15 @@ public class ViewConfiguration {
}
public int getScaledTouchSlop() {
return 0;
return 8;
}
public int getScaledMaximumFlingVelocity() {
return 0;
return 8000;
}
public int getScaledMinimumFlingVelocity() {
return 0;
return 50;
}
public static int getTapTimeout() {
@@ -29,7 +32,7 @@ public class ViewConfiguration {
}
public int getScaledPagingTouchSlop(){
return 0;
return 16;
}
public boolean hasPermanentMenuKey() {
@@ -45,6 +48,6 @@ public class ViewConfiguration {
}
public static float getScrollFriction() {
return 0.f;
return 0.015f;
}
}