3333
3434namespace android {
3535
36+ // Nanoseconds per milliseconds.
37+ static const nsecs_t NANOS_PER_MS = 1000000 ;
38+
39+ // Threshold for determining that a pointer has stopped moving.
40+ // Some input devices do not send ACTION_MOVE events in the case where a pointer has
41+ // stopped. We need to detect this case so that we can accurately predict the
42+ // velocity after the pointer starts moving again.
43+ static const nsecs_t ASSUME_POINTER_STOPPED_TIME = 40 * NANOS_PER_MS;
44+
45+
3646static float vectorDot (const float * a, const float * b, uint32_t m) {
3747 float r = 0 ;
3848 while (m--) {
@@ -89,15 +99,10 @@ static String8 matrixToString(const float* a, uint32_t m, uint32_t n, bool rowMa
8999// --- VelocityTracker ---
90100
91101VelocityTracker::VelocityTracker () :
92- mCurrentPointerIdBits (0 ), mActivePointerId (-1 ),
102+ mLastEventTime ( 0 ), mCurrentPointerIdBits (0 ), mActivePointerId (-1 ),
93103 mStrategy (new LeastSquaresVelocityTrackerStrategy()) {
94104}
95105
96- VelocityTracker::VelocityTracker (VelocityTrackerStrategy* strategy) :
97- mCurrentPointerIdBits (0 ), mActivePointerId (-1 ),
98- mStrategy (strategy) {
99- }
100-
101106VelocityTracker::~VelocityTracker () {
102107 delete mStrategy ;
103108}
@@ -125,6 +130,18 @@ void VelocityTracker::addMovement(nsecs_t eventTime, BitSet32 idBits, const Posi
125130 idBits.clearLastMarkedBit ();
126131 }
127132
133+ if ((mCurrentPointerIdBits .value & idBits.value )
134+ && eventTime >= mLastEventTime + ASSUME_POINTER_STOPPED_TIME) {
135+ #if DEBUG_VELOCITY
136+ ALOGD (" VelocityTracker: stopped for %0.3f ms, clearing state." ,
137+ (eventTime - mLastEventTime ) * 0 .000001f );
138+ #endif
139+ // We have not received any movements for too long. Assume that all pointers
140+ // have stopped.
141+ mStrategy ->clear ();
142+ }
143+ mLastEventTime = eventTime;
144+
128145 mCurrentPointerIdBits = idBits;
129146 if (mActivePointerId < 0 || !idBits.hasBit (mActivePointerId )) {
130147 mActivePointerId = idBits.isEmpty () ? -1 : idBits.firstMarkedBit ();
@@ -467,6 +484,7 @@ bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
467484 uint32_t n = degree + 1 ;
468485 if (solveLeastSquares (time, x, m, n, outEstimator->xCoeff , &xdet)
469486 && solveLeastSquares (time, y, m, n, outEstimator->yCoeff , &ydet)) {
487+ outEstimator->time = newestMovement.eventTime ;
470488 outEstimator->degree = degree;
471489 outEstimator->confidence = xdet * ydet;
472490#if DEBUG_LEAST_SQUARES
@@ -483,6 +501,7 @@ bool LeastSquaresVelocityTrackerStrategy::getEstimator(uint32_t id,
483501 // No velocity data available for this pointer, but we do have its current position.
484502 outEstimator->xCoeff [0 ] = x[0 ];
485503 outEstimator->yCoeff [0 ] = y[0 ];
504+ outEstimator->time = newestMovement.eventTime ;
486505 outEstimator->degree = 0 ;
487506 outEstimator->confidence = 1 ;
488507 return true ;
0 commit comments