From 97643972748a26d23615fd38d7a1693a5b5c55aa Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Tue, 21 May 2013 07:24:58 +0900
Subject: [PATCH] Fix drawing debug points of gesture trails

Bug: 7167303
Change-Id: I9a75b1b9b9127c5364da49b2a04b22c16a7ad535
---
 .../GestureStrokeWithPreviewPoints.java       | 23 +++++--
 .../keyboard/internal/GestureTrail.java       | 64 ++++++++++++-------
 2 files changed, 61 insertions(+), 26 deletions(-)

diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
index b31f00b625..8deadbf969 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStrokeWithPreviewPoints.java
@@ -58,7 +58,7 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
         }
 
         private static double degreeToRadian(final int degree) {
-            return (double)degree / 180.0d * Math.PI;
+            return degree / 180.0d * Math.PI;
         }
 
         public GestureStrokePreviewParams(final TypedArray mainKeyboardViewAttr) {
@@ -125,8 +125,18 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
 
     }
 
+    /**
+     * Append sampled preview points.
+     *
+     * @param eventTimes the event time array of gesture trail to be drawn.
+     * @param xCoords the x-coordinates array of gesture trail to be drawn.
+     * @param yCoords the y-coordinates array of gesture trail to be drawn.
+     * @param types the point types array of gesture trail. This is valid only when
+     * {@link GestureTrail#DEBUG_SHOW_POINTS} is true.
+     */
     public void appendPreviewStroke(final ResizableIntArray eventTimes,
-            final ResizableIntArray xCoords, final ResizableIntArray yCoords) {
+            final ResizableIntArray xCoords, final ResizableIntArray yCoords,
+            final ResizableIntArray types) {
         final int length = mPreviewEventTimes.getLength() - mLastPreviewSize;
         if (length <= 0) {
             return;
@@ -134,6 +144,9 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
         eventTimes.append(mPreviewEventTimes, mLastPreviewSize, length);
         xCoords.append(mPreviewXCoordinates, mLastPreviewSize, length);
         yCoords.append(mPreviewYCoordinates, mLastPreviewSize, length);
+        if (GestureTrail.DEBUG_SHOW_POINTS) {
+            types.fill(GestureTrail.POINT_TYPE_SAMPLED, types.getLength(), length);
+        }
         mLastPreviewSize = mPreviewEventTimes.getLength();
     }
 
@@ -148,6 +161,8 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
      * @param eventTimes the event time array of gesture trail to be drawn.
      * @param xCoords the x-coordinates array of gesture trail to be drawn.
      * @param yCoords the y-coordinates array of gesture trail to be drawn.
+     * @param types the point types array of gesture trail. This is valid only when
+     * {@link GestureTrail#DEBUG_SHOW_POINTS} is true.
      * @return the start index of the last interpolated segment of input arrays.
      */
     public int interpolateStrokeAndReturnStartIndexOfLastSegment(final int lastInterpolatedIndex,
@@ -189,7 +204,7 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
                 eventTimes.add(d1, (int)(dt * t) + t1);
                 xCoords.add(d1, (int)mInterpolator.mInterpolatedX);
                 yCoords.add(d1, (int)mInterpolator.mInterpolatedY);
-                if (GestureTrail.DBG_SHOW_POINTS) {
+                if (GestureTrail.DEBUG_SHOW_POINTS) {
                     types.add(d1, GestureTrail.POINT_TYPE_INTERPOLATED);
                 }
                 d1++;
@@ -197,7 +212,7 @@ public final class GestureStrokeWithPreviewPoints extends GestureStroke {
             eventTimes.add(d1, pt[p2]);
             xCoords.add(d1, px[p2]);
             yCoords.add(d1, py[p2]);
-            if (GestureTrail.DBG_SHOW_POINTS) {
+            if (GestureTrail.DEBUG_SHOW_POINTS) {
                 types.add(d1, GestureTrail.POINT_TYPE_SAMPLED);
             }
         }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GestureTrail.java
index 03dd1c372b..0f3cd7887f 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureTrail.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureTrail.java
@@ -36,10 +36,11 @@ import com.android.inputmethod.latin.ResizableIntArray;
  * @attr ref R.styleable#MainKeyboardView_gestureTrailWidth
  */
 final class GestureTrail {
-    public static final boolean DBG_SHOW_POINTS = false;
-    public static final int POINT_TYPE_SAMPLED = 0;
-    public static final int POINT_TYPE_INTERPOLATED = 1;
-    public static final int POINT_TYPE_COMPROMISED = 2;
+    public static final boolean DEBUG_SHOW_POINTS = false;
+    public static final int POINT_TYPE_SAMPLED = 1;
+    public static final int POINT_TYPE_INTERPOLATED = 2;
+    private static final int FADEOUT_START_DELAY_FOR_DEBUG = 2000; // millisecond
+    private static final int FADEOUT_DURATION_FOR_DEBUG = 200; // millisecond
 
     private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewPoints.PREVIEW_CAPACITY;
 
@@ -48,7 +49,7 @@ final class GestureTrail {
     private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
     private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY);
     private final ResizableIntArray mPointTypes = new ResizableIntArray(
-            DBG_SHOW_POINTS ? DEFAULT_CAPACITY : 0);
+            DEBUG_SHOW_POINTS ? DEFAULT_CAPACITY : 0);
     private int mCurrentStrokeId = -1;
     // The wall time of the zero value in {@link #mEventTimes}
     private long mCurrentTimeBase;
@@ -83,10 +84,12 @@ final class GestureTrail {
                     R.styleable.MainKeyboardView_gestureTrailShadowRatio, 0);
             mTrailShadowEnabled = (trailShadowRatioInt > 0);
             mTrailShadowRatio = (float)trailShadowRatioInt / (float)PERCENTAGE_INT;
-            mFadeoutStartDelay = DBG_SHOW_POINTS ? 2000 : mainKeyboardViewAttr.getInt(
-                    R.styleable.MainKeyboardView_gestureTrailFadeoutStartDelay, 0);
-            mFadeoutDuration = DBG_SHOW_POINTS ? 200 : mainKeyboardViewAttr.getInt(
-                    R.styleable.MainKeyboardView_gestureTrailFadeoutDuration, 0);
+            mFadeoutStartDelay = DEBUG_SHOW_POINTS ? FADEOUT_START_DELAY_FOR_DEBUG
+                    : mainKeyboardViewAttr.getInt(
+                            R.styleable.MainKeyboardView_gestureTrailFadeoutStartDelay, 0);
+            mFadeoutDuration = DEBUG_SHOW_POINTS ? FADEOUT_DURATION_FOR_DEBUG
+                    : mainKeyboardViewAttr.getInt(
+                            R.styleable.MainKeyboardView_gestureTrailFadeoutDuration, 0);
             mTrailLingerDuration = mFadeoutStartDelay + mFadeoutDuration;
             mUpdateInterval = mainKeyboardViewAttr.getInt(
                     R.styleable.MainKeyboardView_gestureTrailUpdateInterval, 0);
@@ -117,7 +120,7 @@ final class GestureTrail {
 
     private void addStrokeLocked(final GestureStrokeWithPreviewPoints stroke, final long downTime) {
         final int trailSize = mEventTimes.getLength();
-        stroke.appendPreviewStroke(mEventTimes, mXCoordinates, mYCoordinates);
+        stroke.appendPreviewStroke(mEventTimes, mXCoordinates, mYCoordinates, mPointTypes);
         if (mEventTimes.getLength() == trailSize) {
             return;
         }
@@ -255,23 +258,15 @@ final class GestureTrail {
                         final int alpha = getAlpha(elapsedTime, params);
                         paint.setAlpha(alpha);
                         canvas.drawPath(path, paint);
-                        if (DBG_SHOW_POINTS) {
-                            if (pointTypes[i] == POINT_TYPE_INTERPOLATED) {
-                                paint.setColor(Color.RED);
-                            } else if (pointTypes[i] == POINT_TYPE_SAMPLED) {
-                                paint.setColor(0xFFA000FF);
-                            } else {
-                                paint.setColor(Color.GREEN);
-                            }
-                            canvas.drawCircle(p1x - 1, p1y - 1, 2, paint);
-                            paint.setColor(params.mTrailColor);
-                        }
                     }
                 }
                 p1x = p2x;
                 p1y = p2y;
                 r1 = r2;
             }
+            if (DEBUG_SHOW_POINTS) {
+                debugDrawPoints(canvas, startIndex, trailSize, paint);
+            }
         }
 
         final int newSize = trailSize - startIndex;
@@ -281,11 +276,14 @@ final class GestureTrail {
                 System.arraycopy(eventTimes, startIndex, eventTimes, 0, newSize);
                 System.arraycopy(xCoords, startIndex, xCoords, 0, newSize);
                 System.arraycopy(yCoords, startIndex, yCoords, 0, newSize);
+                if (DEBUG_SHOW_POINTS) {
+                    System.arraycopy(pointTypes, startIndex, pointTypes, 0, newSize);
+                }
             }
             mEventTimes.setLength(newSize);
             mXCoordinates.setLength(newSize);
             mYCoordinates.setLength(newSize);
-            if (DBG_SHOW_POINTS) {
+            if (DEBUG_SHOW_POINTS) {
                 mPointTypes.setLength(newSize);
             }
             // The start index of the last segment of the stroke
@@ -295,4 +293,26 @@ final class GestureTrail {
         }
         return newSize > 0;
     }
+
+    private void debugDrawPoints(final Canvas canvas, final int startIndex, final int endIndex,
+            final Paint paint) {
+        final int[] xCoords = mXCoordinates.getPrimitiveArray();
+        final int[] yCoords = mYCoordinates.getPrimitiveArray();
+        final int[] pointTypes = mPointTypes.getPrimitiveArray();
+        // {@link Paint} that is zero width stroke and anti alias off draws exactly 1 pixel.
+        paint.setAntiAlias(false);
+        paint.setStrokeWidth(0);
+        for (int i = startIndex; i < endIndex; i++) {
+            final int pointType = pointTypes[i];
+            if (pointType == POINT_TYPE_INTERPOLATED) {
+                paint.setColor(Color.RED);
+            } else if (pointType == POINT_TYPE_SAMPLED) {
+                paint.setColor(0xFFA000FF);
+            } else {
+                paint.setColor(Color.GREEN);
+            }
+            canvas.drawPoint(getXCoordValue(xCoords[i]), yCoords[i], paint);
+        }
+        paint.setAntiAlias(true);
+    }
 }
-- 
GitLab