Merge "Remove getEstimator from VelocityTracker"

This commit is contained in:
Siarhei Vishniakou
2023-01-14 06:44:38 +00:00
committed by Android (Google) Code Review
3 changed files with 3 additions and 142 deletions

View File

@@ -19,7 +19,6 @@ package android.view;
import android.annotation.IntDef;
import android.compat.annotation.UnsupportedAppUsage;
import android.hardware.input.InputManager;
import android.os.Build;
import android.util.ArrayMap;
import android.util.Pools.SynchronizedPool;
@@ -190,8 +189,6 @@ public final class VelocityTracker {
private static native void nativeAddMovement(long ptr, MotionEvent event);
private static native void nativeComputeCurrentVelocity(long ptr, int units, float maxVelocity);
private static native float nativeGetVelocity(long ptr, int axis, int id);
private static native boolean nativeGetEstimator(
long ptr, int axis, int id, Estimator outEstimator);
private static native boolean nativeIsAxisSupported(int axis);
static {
@@ -436,7 +433,7 @@ public final class VelocityTracker {
* method:
* <ul>
* <li> {@link MotionEvent#AXIS_SCROLL}: supported starting
* {@link Build.VERSION_CODES#UPSIDE_DOWN_CAKE}
* {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}
* </ul>
*
* <p>Before accessing velocities of an axis using this method, check that your
@@ -467,89 +464,4 @@ public final class VelocityTracker {
public float getAxisVelocity(@VelocityTrackableMotionEventAxis int axis) {
return nativeGetVelocity(mPtr, axis, ACTIVE_POINTER_ID);
}
/**
* Get an estimator for the movements of a pointer using past movements of the
* pointer to predict future movements.
*
* It is not necessary to call {@link #computeCurrentVelocity(int)} before calling
* this method.
*
* @param axis Which axis's velocity to return.
* Should be one of the axes defined in {@link MotionEvent}.
* @param id Which pointer's velocity to return.
* @param outEstimator The estimator to populate.
* @return True if an estimator was obtained, false if there is no information
* available about the pointer.
*
* @hide For internal use only. Not a final API.
*/
public boolean getEstimator(int axis, int id, Estimator outEstimator) {
if (outEstimator == null) {
throw new IllegalArgumentException("outEstimator must not be null");
}
return nativeGetEstimator(mPtr, axis, id, outEstimator);
}
/**
* An estimator for the movements of a pointer based on a polynomial model.
*
* The last recorded position of the pointer is at time zero seconds.
* Past estimated positions are at negative times and future estimated positions
* are at positive times.
*
* First coefficient is position (in units), second is velocity (in units per second),
* third is acceleration (in units per second squared).
*
* @hide For internal use only. Not a final API.
*/
public static final class Estimator {
// Must match VelocityTracker::Estimator::MAX_DEGREE
private static final int MAX_DEGREE = 4;
/**
* Polynomial coefficients describing motion.
*/
public final float[] coeff = new float[MAX_DEGREE + 1];
/**
* Polynomial degree, or zero if only position information is available.
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public int degree;
/**
* Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public float confidence;
/**
* Gets an estimate of the position of the pointer at the specified time point.
* @param time The time point in seconds, 0 is the last recorded time.
* @return The estimated axis value.
*/
public float estimate(float time) {
return estimate(time, coeff);
}
/**
* Gets the coefficient with the specified index.
* @param index The index of the coefficient to return.
* @return The coefficient, or 0 if the index is greater than the degree.
*/
public float getCoeff(int index) {
return index <= degree ? coeff[index] : 0;
}
private float estimate(float time, float[] c) {
float a = 0;
float scale = 1;
for (int i = 0; i <= degree; i++) {
a += c[i] * scale;
scale *= time;
}
return a;
}
}
}

View File

@@ -92,12 +92,6 @@ public class PointerLocationView extends View implements InputDeviceListener,
private float mBoundingRight;
private float mBoundingBottom;
// Position estimator.
private VelocityTracker.Estimator mEstimatorX = new VelocityTracker.Estimator();
private VelocityTracker.Estimator mAltEstimatorX = new VelocityTracker.Estimator();
private VelocityTracker.Estimator mEstimatorY = new VelocityTracker.Estimator();
private VelocityTracker.Estimator mAltEstimatorY = new VelocityTracker.Estimator();
@UnsupportedAppUsage
public PointerState() {
}
@@ -669,13 +663,9 @@ public class PointerLocationView extends View implements InputDeviceListener,
ps.addTrace(coords.x, coords.y, true);
ps.mXVelocity = mVelocity.getXVelocity(id);
ps.mYVelocity = mVelocity.getYVelocity(id);
mVelocity.getEstimator(MotionEvent.AXIS_X, id, ps.mEstimatorX);
mVelocity.getEstimator(MotionEvent.AXIS_Y, id, ps.mEstimatorY);
if (mAltVelocity != null) {
ps.mAltXVelocity = mAltVelocity.getXVelocity(id);
ps.mAltYVelocity = mAltVelocity.getYVelocity(id);
mAltVelocity.getEstimator(MotionEvent.AXIS_X, id, ps.mAltEstimatorX);
mAltVelocity.getEstimator(MotionEvent.AXIS_Y, id, ps.mAltEstimatorY);
}
ps.mToolType = event.getToolType(i);

View File

@@ -31,13 +31,6 @@ namespace android {
// Special constant to request the velocity of the active pointer.
static const int ACTIVE_POINTER_ID = -1;
static struct {
jfieldID coeff;
jfieldID degree;
jfieldID confidence;
} gEstimatorClassInfo;
// --- VelocityTrackerState ---
class VelocityTrackerState {
@@ -50,7 +43,6 @@ public:
// a subset of the supported axes.
void computeCurrentVelocity(int32_t units, float maxVelocity);
float getVelocity(int32_t axis, int32_t id);
bool getEstimator(int32_t axis, int32_t id, VelocityTracker::Estimator* outEstimator);
private:
VelocityTracker mVelocityTracker;
@@ -80,11 +72,6 @@ float VelocityTrackerState::getVelocity(int32_t axis, int32_t id) {
return mComputedVelocity.getVelocity(axis, id).value_or(0);
}
bool VelocityTrackerState::getEstimator(int32_t axis, int32_t id,
VelocityTracker::Estimator* outEstimator) {
return mVelocityTracker.getEstimator(axis, id, outEstimator);
}
// Return a strategy enum from integer value.
inline static VelocityTracker::Strategy getStrategyFromInt(const int32_t strategy) {
if (strategy < static_cast<int32_t>(VelocityTracker::Strategy::MIN) ||
@@ -135,24 +122,6 @@ static jfloat android_view_VelocityTracker_nativeGetVelocity(JNIEnv* env, jclass
return state->getVelocity(axis, id);
}
static jboolean android_view_VelocityTracker_nativeGetEstimator(JNIEnv* env, jclass clazz,
jlong ptr, jint axis, jint id,
jobject outEstimatorObj) {
VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
VelocityTracker::Estimator estimator;
bool result = state->getEstimator(axis, id, &estimator);
jfloatArray coeffObj =
jfloatArray(env->GetObjectField(outEstimatorObj, gEstimatorClassInfo.coeff));
env->SetFloatArrayRegion(coeffObj, 0, VelocityTracker::Estimator::MAX_DEGREE + 1,
estimator.coeff);
env->SetIntField(outEstimatorObj, gEstimatorClassInfo.degree, estimator.degree);
env->SetFloatField(outEstimatorObj, gEstimatorClassInfo.confidence, estimator.confidence);
return result;
}
static jboolean android_view_VelocityTracker_nativeIsAxisSupported(JNIEnv* env, jclass clazz,
jint axis) {
return VelocityTracker::isAxisSupported(axis);
@@ -170,23 +139,13 @@ static const JNINativeMethod gVelocityTrackerMethods[] = {
{"nativeComputeCurrentVelocity", "(JIF)V",
(void*)android_view_VelocityTracker_nativeComputeCurrentVelocity},
{"nativeGetVelocity", "(JII)F", (void*)android_view_VelocityTracker_nativeGetVelocity},
{"nativeGetEstimator", "(JIILandroid/view/VelocityTracker$Estimator;)Z",
(void*)android_view_VelocityTracker_nativeGetEstimator},
{"nativeIsAxisSupported", "(I)Z",
(void*)android_view_VelocityTracker_nativeIsAxisSupported},
};
int register_android_view_VelocityTracker(JNIEnv* env) {
int res = RegisterMethodsOrDie(env, "android/view/VelocityTracker", gVelocityTrackerMethods,
NELEM(gVelocityTrackerMethods));
jclass clazz = FindClassOrDie(env, "android/view/VelocityTracker$Estimator");
gEstimatorClassInfo.coeff = GetFieldIDOrDie(env, clazz, "coeff", "[F");
gEstimatorClassInfo.degree = GetFieldIDOrDie(env, clazz, "degree", "I");
gEstimatorClassInfo.confidence = GetFieldIDOrDie(env, clazz, "confidence", "F");
return res;
return RegisterMethodsOrDie(env, "android/view/VelocityTracker", gVelocityTrackerMethods,
NELEM(gVelocityTrackerMethods));
}
} // namespace android