Work on issue #2144454: Inconsistent swipes...

This introduces some hacks in the framework to try to clean up the
data we are getting from the touch screen.  There are two main things
being done here:

1. Look for changes in position that are unreasonably large, and
ignore them.  This is intended to eliminate the spurious jumps that
often happen when releasing.

2. Add some simple adaptive averaging of the touch data.  If the
difference between the last and next point is large enough, we
disable the averaging; otherwise we average up to the last 5 points.
The goal is to get rid of the noise of small movements so that things
like taps don't look like short flings, while still responding quickly
to rapid movement.

For averaging pressure, we also weight each averaged coordinate by
the reported pressure at that point.  This is intended to keep the
coordinates closer together during a release, when the pressure is
going down and the accuracy decreasing.  It may also result in some
other interesting artifacts, but hopefully nothing problematic.

Change-Id: I1369e9ab015c406946a45c2d72547da9c604178f
diff --git a/services/java/com/android/server/KeyInputQueue.java b/services/java/com/android/server/KeyInputQueue.java
index 35ed448..09591f4 100644
--- a/services/java/com/android/server/KeyInputQueue.java
+++ b/services/java/com/android/server/KeyInputQueue.java
@@ -52,6 +52,12 @@
     static final boolean DEBUG_VIRTUAL_KEYS = false;
     static final boolean DEBUG_POINTERS = false;
     
+    /**
+     * Turn on some hacks we have to improve the touch interaction with a
+     * certain device whose screen currently is not all that good.
+     */
+    static final boolean BAD_TOUCH_HACK = true;
+    
     private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
 
     final SparseArray<InputDevice> mDevices = new SparseArray<InputDevice>();
@@ -540,19 +546,17 @@
                                             keycode, 0, scancode,
                                             ((ev.flags & WindowManagerPolicy.FLAG_WOKE_HERE) != 0)
                                              ? KeyEvent.FLAG_WOKE_HERE : 0));
+                            
                         } else if (ev.type == RawInputEvent.EV_KEY) {
+                            // Single touch protocol: touch going down or up.
                             if (ev.scancode == RawInputEvent.BTN_TOUCH &&
                                     (classes&(RawInputEvent.CLASS_TOUCHSCREEN
                                             |RawInputEvent.CLASS_TOUCHSCREEN_MT))
                                             == RawInputEvent.CLASS_TOUCHSCREEN) {
                                 di.mAbs.changed = true;
                                 di.mAbs.mDown[0] = ev.value != 0;
-                            } else if (ev.scancode == RawInputEvent.BTN_2 &&
-                                    (classes&(RawInputEvent.CLASS_TOUCHSCREEN
-                                            |RawInputEvent.CLASS_TOUCHSCREEN_MT))
-                                            == RawInputEvent.CLASS_TOUCHSCREEN) {
-                                di.mAbs.changed = true;
-                                di.mAbs.mDown[1] = ev.value != 0;
+                            
+                            // Trackball (mouse) protocol: press down or up.
                             } else if (ev.scancode == RawInputEvent.BTN_MOUSE &&
                                     (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
                                 di.mRel.changed = true;
@@ -560,6 +564,7 @@
                                 send = true;
                             }
     
+                        // Process position events from multitouch protocol.
                         } else if (ev.type == RawInputEvent.EV_ABS &&
                                 (classes&RawInputEvent.CLASS_TOUCHSCREEN_MT) != 0) {
                             if (ev.scancode == RawInputEvent.ABS_MT_TOUCH_MAJOR) {
@@ -585,10 +590,10 @@
                                 di.mAbs.mNextData[di.mAbs.mAddingPointerOffset
                                     + MotionEvent.SAMPLE_SIZE] = ev.value;
                             }
-                            
+                        
+                        // Process position events from single touch protocol.
                         } else if (ev.type == RawInputEvent.EV_ABS &&
                                 (classes&RawInputEvent.CLASS_TOUCHSCREEN) != 0) {
-                            // Finger 1
                             if (ev.scancode == RawInputEvent.ABS_X) {
                                 di.mAbs.changed = true;
                                 di.curTouchVals[MotionEvent.SAMPLE_X] = ev.value;
@@ -605,18 +610,9 @@
                                 di.curTouchVals[MotionEvent.SAMPLE_SIZE] = ev.value;
                                 di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
                                                  + MotionEvent.SAMPLE_SIZE] = ev.value;
-
-                            // Finger 2
-                            } else if (ev.scancode == RawInputEvent.ABS_HAT0X) {
-                                di.mAbs.changed = true;
-                                di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA 
-                                         + MotionEvent.SAMPLE_X] = ev.value;
-                            } else if (ev.scancode == RawInputEvent.ABS_HAT0Y) {
-                                di.mAbs.changed = true;
-                                di.curTouchVals[MotionEvent.NUM_SAMPLE_DATA
-                                        + MotionEvent.SAMPLE_Y] = ev.value;
                             }
     
+                        // Process movement events from trackball (mouse) protocol.
                         } else if (ev.type == RawInputEvent.EV_REL &&
                                 (classes&RawInputEvent.CLASS_TRACKBALL) != 0) {
                             // Add this relative movement into our totals.
@@ -629,6 +625,9 @@
                             }
                         }
                         
+                        // Handle multitouch protocol sync: tells us that the
+                        // driver has returned all data for -one- of the pointers
+                        // that is currently down.
                         if (ev.type == RawInputEvent.EV_SYN
                                 && ev.scancode == RawInputEvent.SYN_MT_REPORT
                                 && di.mAbs != null) {
@@ -654,6 +653,9 @@
                                     if (DEBUG_POINTERS) Log.v(TAG, "MT_REPORT: no pointer");
                                 }
                             }
+                        
+                        // Handle general event sync: all data for the current
+                        // event update has been delivered.
                         } else if (send || (ev.type == RawInputEvent.EV_SYN
                                 && ev.scancode == RawInputEvent.SYN_REPORT)) {
                             if (mDisplay != null) {
@@ -677,15 +679,10 @@
                                                     MotionEvent.NUM_SAMPLE_DATA);
                                             ms.mNextNumPointers++;
                                         }
-                                        if (ms.mDown[1]) {
-                                            System.arraycopy(di.curTouchVals,
-                                                    MotionEvent.NUM_SAMPLE_DATA,
-                                                    ms.mNextData,
-                                                    ms.mNextNumPointers
-                                                    * MotionEvent.NUM_SAMPLE_DATA,
-                                                    MotionEvent.NUM_SAMPLE_DATA);
-                                            ms.mNextNumPointers++;
-                                        }
+                                    }
+                                    
+                                    if (BAD_TOUCH_HACK) {
+                                        ms.dropBadPoint(di);
                                     }
                                     
                                     boolean doMotion = !monitorVirtualKey(di,
@@ -719,6 +716,16 @@
                                                         RawInputEvent.CLASS_TOUCHSCREEN, me);
                                             }
                                         } while (ms.hasMore());
+                                    } else {
+                                        // We are consuming movement in the
+                                        // virtual key area...  but still
+                                        // propagate this to the previous
+                                        // data for comparisons.
+                                        System.arraycopy(ms.mNextData, 0,
+                                                ms.mLastData, 0,
+                                                ms.mNextNumPointers
+                                                        * MotionEvent.NUM_SAMPLE_DATA);
+                                        ms.mLastNumPointers = ms.mNextNumPointers;
                                     }
                                     
                                     ms.finish();