From 2f81757c3a5eb50d41ce19fb534f72cbf607a997 Mon Sep 17 00:00:00 2001
From: Tom Ouyang <ouyang@google.com>
Date: Fri, 20 Jul 2012 21:01:44 +0900
Subject: [PATCH] Add dynamic floating preview for incremental gesture
 recognition.

Change-Id: I7ba7ac24aa96a0ff19267997c5b58853079bc6dc
---
 java/res/values/dimens.xml                    |   8 +
 .../inputmethod/keyboard/KeyboardView.java    |  70 ++-----
 .../inputmethod/keyboard/PointerTracker.java  |  21 +--
 .../keyboard/internal/GestureStroke.java      |   7 -
 .../keyboard/internal/PreviewPlacerView.java  | 172 ++++++++++++++++++
 .../android/inputmethod/latin/LatinIME.java   |   4 +
 6 files changed, 207 insertions(+), 75 deletions(-)
 create mode 100644 java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java

diff --git a/java/res/values/dimens.xml b/java/res/values/dimens.xml
index 925eb55fa6..b47897f3c2 100644
--- a/java/res/values/dimens.xml
+++ b/java/res/values/dimens.xml
@@ -93,4 +93,12 @@
     <dimen name="more_suggestions_hint_text_size">27dp</dimen>
     <integer name="suggestions_count_in_strip">3</integer>
     <integer name="center_suggestion_percentile">36</integer>
+
+    <!-- Gesture preview parameters -->
+    <dimen name="gesture_preview_trail_width">2.5dp</dimen>
+    <dimen name="gesture_preview_text_size">35dp</dimen>
+    <dimen name="gesture_preview_text_offset">75dp</dimen>
+    <dimen name="gesture_preview_text_shadow_border">17.5dp</dimen>
+    <dimen name="gesture_preview_text_shading_border">7.5dp</dimen>
+    <dimen name="gesture_preview_text_connector_width">1.0dp</dimen>
 </resources>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index b9c591d4a7..f1f942ccbf 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -35,9 +35,9 @@ import android.util.TypedValue;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.RelativeLayout;
 import android.widget.TextView;
 
+import com.android.inputmethod.keyboard.internal.PreviewPlacerView;
 import com.android.inputmethod.latin.Constants;
 import com.android.inputmethod.latin.LatinImeLogger;
 import com.android.inputmethod.latin.R;
@@ -97,9 +97,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     // The maximum key label width in the proportion to the key width.
     private static final float MAX_LABEL_RATIO = 0.90f;
 
-    private final static int GESTURE_DRAWING_WIDTH = 5;
-    private final static int GESTURE_DRAWING_COLOR = 0xff33b5e5;
-
     // Main keyboard
     private Keyboard mKeyboard;
     protected final KeyDrawParams mKeyDrawParams;
@@ -109,7 +106,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     protected final KeyPreviewDrawParams mKeyPreviewDrawParams;
     private boolean mShowKeyPreviewPopup = true;
     private int mDelayAfterPreview;
-    private ViewGroup mPreviewPlacer;
+    private PreviewPlacerView mPreviewPlacer;
 
     /** True if {@link KeyboardView} should handle gesture events. */
     protected boolean mShouldHandleGesture;
@@ -125,14 +122,11 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     private final HashSet<Key> mInvalidatedKeys = new HashSet<Key>();
     /** The region of invalidated keys */
     private final Rect mInvalidatedKeysRect = new Rect();
-    /** The region of invalidated gestures */
-    private final Rect mInvalidatedGesturesRect = new Rect();
     /** The keyboard bitmap buffer for faster updates */
     private Bitmap mBuffer;
     /** The canvas for the above mutable keyboard bitmap */
     private Canvas mCanvas;
     private final Paint mPaint = new Paint();
-    private final Paint mGesturePaint = new Paint();
     private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
     // This sparse array caches key label text height in pixel indexed by key label text size.
     private static final SparseArray<Float> sTextHeightCache = new SparseArray<Float>();
@@ -382,13 +376,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         mDelayAfterPreview = mKeyPreviewDrawParams.mLingerTimeout;
 
         mPaint.setAntiAlias(true);
-
-        // TODO: These paint parameters should be specified via attribute of the view and styleable.
-        mGesturePaint.setAntiAlias(true);
-        mGesturePaint.setStyle(Paint.Style.STROKE);
-        mGesturePaint.setStrokeJoin(Paint.Join.ROUND);
-        mGesturePaint.setColor(GESTURE_DRAWING_COLOR);
-        mGesturePaint.setStrokeWidth(GESTURE_DRAWING_WIDTH);
     }
 
     // Read fraction value in TypedArray as float.
@@ -888,29 +875,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         mDrawingHandler.dismissKeyPreview(mDelayAfterPreview, tracker);
     }
 
-    private static class PreviewPlacerView extends RelativeLayout {
-        private final Paint mGesturePaint;
-        final int mCoordinateX;
-        final int mCoordinateY;
-
-        public PreviewPlacerView(Context context, int coordinateX, int coordinateY,
-                Paint gesturePaint) {
-            super(context);
-            setWillNotDraw(false);
-            mGesturePaint = gesturePaint;
-            mCoordinateX = coordinateX;
-            mCoordinateY = coordinateY;
-        }
-
-        @Override
-        public void onDraw(Canvas canvas) {
-            super.onDraw(canvas);
-            canvas.translate(mCoordinateX, mCoordinateY);
-            PointerTracker.drawGestureTrailForAllPointerTrackers(canvas, mGesturePaint);
-            canvas.translate(-mCoordinateX, -mCoordinateY);
-        }
-    }
-
     private void addKeyPreview(TextView keyPreview) {
         if (mPreviewPlacer == null) {
             createPreviewPlacer();
@@ -920,31 +884,31 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     }
 
     private void createPreviewPlacer() {
-        getLocationInWindow(mKeyPreviewDrawParams.mCoordinates);
-        mPreviewPlacer = new PreviewPlacerView(getContext(), mKeyPreviewDrawParams.mCoordinates[0],
-                mKeyPreviewDrawParams.mCoordinates[1], mGesturePaint);
+        mPreviewPlacer = new PreviewPlacerView(getContext());
+        final int[] viewOrigin = new int[2];
+        getLocationInWindow(viewOrigin);
+        mPreviewPlacer.setOrigin(viewOrigin[0], viewOrigin[1]);
         final ViewGroup windowContentView =
                 (ViewGroup)getRootView().findViewById(android.R.id.content);
         windowContentView.addView(mPreviewPlacer);
     }
 
+    public void showGesturePreviewText(String gesturePreviewText) {
+        // TDOD: Add user settings option to control drawing gesture trail.
+        if (mPreviewPlacer == null) {
+            createPreviewPlacer();
+        }
+        mPreviewPlacer.setGesturePreviewText(gesturePreviewText);
+        mPreviewPlacer.invalidate();
+    }
+
     @Override
     public void showGestureTrail(PointerTracker tracker) {
+        // TDOD: Add user settings option to control drawing gesture trail.
         if (mPreviewPlacer == null) {
             createPreviewPlacer();
         }
-        final Rect r = tracker.getBoundingBox();
-        if (!r.isEmpty()) {
-            // Invalidate the rectangular region encompassing the gesture. This is needed because
-            // past points along the gesture will fade and gradually disappear.
-            final KeyPreviewDrawParams params = mKeyPreviewDrawParams;
-            mInvalidatedGesturesRect.set(r);
-            mInvalidatedGesturesRect.offset(params.mCoordinates[0], params.mCoordinates[1]);
-            mInvalidatedGesturesRect.inset(-GESTURE_DRAWING_WIDTH, -GESTURE_DRAWING_WIDTH);
-            mPreviewPlacer.invalidate(mInvalidatedGesturesRect);
-        } else {
-            mPreviewPlacer.invalidate();
-        }
+        mPreviewPlacer.invalidatePointer(tracker);
     }
 
     @SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index 56c3d46b9a..67857d1118 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard;
 
 import android.graphics.Canvas;
 import android.graphics.Paint;
-import android.graphics.Rect;
 import android.os.SystemClock;
 import android.util.Log;
 import android.view.MotionEvent;
@@ -296,17 +295,6 @@ public class PointerTracker {
         sAggregratedPointers.reset();
     }
 
-    // TODO: To handle multi-touch gestures we may want to move this method to
-    // {@link PointerTrackerQueue}.
-    public static void drawGestureTrailForAllPointerTrackers(Canvas canvas, Paint paint) {
-        final int trackersSize = sTrackers.size();
-        for (int i = 0; i < trackersSize; ++i) {
-            final PointerTracker tracker = sTrackers.get(i);
-            tracker.mGestureStroke.drawGestureTrail(canvas, paint, tracker.getLastX(),
-                    tracker.getLastY());
-        }
-    }
-
     private PointerTracker(int id, KeyEventHandler handler) {
         if (handler == null)
             throw new NullPointerException();
@@ -524,6 +512,12 @@ public class PointerTracker {
         mDrawingProxy.invalidateKey(key);
     }
 
+    public void drawGestureTrail(Canvas canvas, Paint paint) {
+        if (mInGesture) {
+            mGestureStroke.drawGestureTrail(canvas, paint, mLastX, mLastY);
+        }
+    }
+
     public int getLastX() {
         return mLastX;
     }
@@ -535,9 +529,6 @@ public class PointerTracker {
     public long getDownTime() {
         return mDownTime;
     }
-    public Rect getBoundingBox() {
-        return mGestureStroke.getBoundingBox();
-    }
 
     private Key onDownKey(int x, int y, long eventTime) {
         mDownTime = eventTime;
diff --git a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
index c16b70ef01..28d6c1d077 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/GestureStroke.java
@@ -16,7 +16,6 @@ package com.android.inputmethod.keyboard.internal;
 
 import android.graphics.Canvas;
 import android.graphics.Paint;
-import android.graphics.Rect;
 import android.util.FloatMath;
 
 import com.android.inputmethod.latin.Constants;
@@ -41,7 +40,6 @@ public class GestureStroke {
     private int mMinGestureLength;
     private int mMinGestureLengthWhileInGesture;
     private int mMinGestureSampleLength;
-    private final Rect mDrawingRect = new Rect();
 
     // TODO: Move some of these to resource.
     private static final float MIN_GESTURE_LENGTH_RATIO_TO_KEY_WIDTH = 1.0f;
@@ -90,7 +88,6 @@ public class GestureStroke {
         mEventTimes.setLength(0);
         mXCoordinates.setLength(0);
         mYCoordinates.setLength(0);
-        mDrawingRect.setEmpty();
     }
 
     private void updateLastPoint(final int x, final int y, final int time) {
@@ -198,8 +195,4 @@ public class GestureStroke {
             }
         }
     }
-
-    public Rect getBoundingBox() {
-        return mDrawingRect;
-    }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java b/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java
new file mode 100644
index 0000000000..2a53c59bb1
--- /dev/null
+++ b/java/src/com/android/inputmethod/keyboard/internal/PreviewPlacerView.java
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.keyboard.internal;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Paint.Align;
+import android.text.TextUtils;
+import android.util.SparseArray;
+import android.widget.RelativeLayout;
+
+import com.android.inputmethod.keyboard.PointerTracker;
+import com.android.inputmethod.latin.R;
+
+public class PreviewPlacerView extends RelativeLayout {
+    // TODO: Move these parameters to attributes of {@link KeyboardView}.
+    private final static int GESTURE_DRAWING_COLOR = 0xff33b5e5;
+    private static final int GESTURE_PREVIEW_TEXT_COLOR = Color.WHITE;
+    private static final int GESTURE_PREVIEW_TEXT_SHADING_COLOR = 0xff33b5e5;
+    private static final int GESTURE_PREVIEW_TEXT_SHADOW_COLOR = 0xff252525;
+    private static final int GESTURE_PREVIEW_CONNECTOR_COLOR = Color.WHITE;
+
+    private final Paint mGesturePaint;
+    private final int mGesturePreviewTraileWidth;
+    private final Paint mTextPaint;
+    private final int mGesturePreviewTextOffset;
+    private final int mGesturePreviewTextShadowBorder;
+    private final int mGesturePreviewTextShadingBorder;
+    private final int mGesturePreviewTextConnectorWidth;
+
+    private int mXOrigin;
+    private int mYOrigin;
+
+    private final SparseArray<PointerTracker> mPointers = new SparseArray<PointerTracker>();
+
+    private String mGesturePreviewText;
+
+    public PreviewPlacerView(Context context) {
+        super(context);
+        setWillNotDraw(false);
+
+        final Resources res = getResources();
+        // TODO: Move these parameters to attributes of {@link KeyboardView}.
+        mGesturePreviewTraileWidth = res.getDimensionPixelSize(
+                R.dimen.gesture_preview_trail_width);
+        final int textSize = res.getDimensionPixelSize(R.dimen.gesture_preview_text_size);
+        mGesturePreviewTextOffset = res.getDimensionPixelSize(
+                R.dimen.gesture_preview_text_offset);
+        mGesturePreviewTextShadowBorder = res.getDimensionPixelOffset(
+                R.dimen.gesture_preview_text_shadow_border);
+        mGesturePreviewTextShadingBorder = res.getDimensionPixelOffset(
+                R.dimen.gesture_preview_text_shading_border);
+        mGesturePreviewTextConnectorWidth = res.getDimensionPixelOffset(
+                R.dimen.gesture_preview_text_connector_width);
+
+        mGesturePaint = new Paint();
+        mGesturePaint.setAntiAlias(true);
+        mGesturePaint.setStyle(Paint.Style.STROKE);
+        mGesturePaint.setStrokeJoin(Paint.Join.ROUND);
+        mGesturePaint.setColor(GESTURE_DRAWING_COLOR);
+        mGesturePaint.setStrokeWidth(mGesturePreviewTraileWidth);
+
+        mTextPaint = new Paint();
+        mTextPaint.setAntiAlias(true);
+        mTextPaint.setStrokeJoin(Paint.Join.ROUND);
+        mTextPaint.setTextAlign(Align.CENTER);
+        mTextPaint.setTextSize(textSize);
+    }
+
+    public void setOrigin(int x, int y) {
+        mXOrigin = x;
+        mYOrigin = y;
+    }
+
+    public void invalidatePointer(PointerTracker tracker) {
+        synchronized (mPointers) {
+            mPointers.put(tracker.mPointerId, tracker);
+            // TODO: Should narrow the invalidate region.
+            invalidate();
+        }
+    }
+
+    @Override
+    public void onDraw(Canvas canvas) {
+        super.onDraw(canvas);
+        // TDOD: Add user settings option to control drawing gesture trail and gesture preview.
+        synchronized (mPointers) {
+            canvas.translate(mXOrigin, mYOrigin);
+            final int trackerCount = mPointers.size();
+            boolean floatingPreviewHasDrawn = false;
+            for (int index = 0; index < trackerCount; index++) {
+                final PointerTracker tracker = mPointers.valueAt(index);
+                tracker.drawGestureTrail(canvas, mGesturePaint);
+                // TODO: Figure out more cleaner way to draw gesture preview text.
+                if (!floatingPreviewHasDrawn) {
+                    drawGesturePreviewText(canvas, tracker, mGesturePreviewText);
+                    floatingPreviewHasDrawn = true;
+                }
+            }
+            canvas.translate(-mXOrigin, -mYOrigin);
+        }
+    }
+
+    public void setGesturePreviewText(String gesturePreviewText) {
+        mGesturePreviewText = gesturePreviewText;
+        invalidate();
+    }
+
+    private void drawGesturePreviewText(Canvas canvas, PointerTracker tracker,
+            String gesturePreviewText) {
+        if (TextUtils.isEmpty(gesturePreviewText)) {
+            return;
+        }
+
+        final Paint paint = mTextPaint;
+        final int lastX = tracker.getLastX();
+        final int lastY = tracker.getLastY();
+        final int textSize = (int)paint.getTextSize();
+        final int canvasWidth = canvas.getWidth();
+
+        final int halfTextWidth = (int)paint.measureText(gesturePreviewText) / 2 + textSize;
+        final int textX = Math.min(Math.max(lastX, halfTextWidth), canvasWidth - halfTextWidth);
+
+        int textY = Math.max(-textSize, lastY - mGesturePreviewTextOffset);
+        if (textY < 0) {
+            // Paint black text shadow if preview extends above keyboard region.
+            paint.setStyle(Paint.Style.FILL_AND_STROKE);
+            paint.setColor(GESTURE_PREVIEW_TEXT_SHADOW_COLOR);
+            paint.setStrokeWidth(mGesturePreviewTextShadowBorder);
+            canvas.drawText(gesturePreviewText, textX, textY, paint);
+        }
+
+        // Paint the vertical line connecting the touch point to the preview text.
+        paint.setStyle(Paint.Style.STROKE);
+        paint.setColor(GESTURE_PREVIEW_CONNECTOR_COLOR);
+        paint.setStrokeWidth(mGesturePreviewTextConnectorWidth);
+        final int lineTopY = textY - textSize / 4;
+        canvas.drawLine(lastX, lastY, lastX, lineTopY, paint);
+        if (lastX != textX) {
+            // Paint the horizontal line connection the touch point to the preview text.
+            canvas.drawLine(lastX, lineTopY, textX, lineTopY, paint);
+        }
+
+        // Paint the shading for the text preview
+        paint.setStyle(Paint.Style.FILL_AND_STROKE);
+        paint.setColor(GESTURE_PREVIEW_TEXT_SHADING_COLOR);
+        paint.setStrokeWidth(mGesturePreviewTextShadingBorder);
+        canvas.drawText(gesturePreviewText, textX, textY, paint);
+
+        // Paint the text preview
+        paint.setColor(GESTURE_PREVIEW_TEXT_COLOR);
+        paint.setStyle(Paint.Style.FILL);
+        canvas.drawText(gesturePreviewText, textX, textY, paint);
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 7d79886faa..9f9d07b3ac 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1343,6 +1343,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
         mWordComposer.setBatchInputPointers(batchPointers);
         final SuggestedWords suggestedWords = getSuggestedWords();
         showSuggestionStrip(suggestedWords, null);
+        final String gesturePreviewText = (suggestedWords.size() > 0)
+                ? suggestedWords.getWord(0) : null;
+        mKeyboardSwitcher.getKeyboardView().showGesturePreviewText(gesturePreviewText);
     }
 
     @Override
@@ -1350,6 +1353,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
         mWordComposer.setBatchInputPointers(batchPointers);
         final SuggestedWords suggestedWords = getSuggestedWords();
         showSuggestionStrip(suggestedWords, null);
+        mKeyboardSwitcher.getKeyboardView().showGesturePreviewText(null);
         if (suggestedWords == null || suggestedWords.size() == 0) {
             return;
         }
-- 
GitLab