Merge "Cosmetic cleanup" into jb-mr1-dev
diff --git a/java/res/drawable/keyboard_key_feedback.xml b/java/res/drawable/keyboard_key_feedback.xml
index 159ba86..397e948 100644
--- a/java/res/drawable/keyboard_key_feedback.xml
+++ b/java/res/drawable/keyboard_key_feedback.xml
@@ -14,9 +14,11 @@
      limitations under the License.
 -->
 
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_long_pressable="true"
-            android:drawable="@drawable/keyboard_key_feedback_more_background" />
-
+<selector
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <item latin:state_has_morekeys="true"
+          android:drawable="@drawable/keyboard_key_feedback_more_background" />
     <item android:drawable="@drawable/keyboard_key_feedback_background" />
 </selector>
diff --git a/java/res/drawable/keyboard_key_feedback_ics.xml b/java/res/drawable/keyboard_key_feedback_ics.xml
index 04c8679..3c8850e 100644
--- a/java/res/drawable/keyboard_key_feedback_ics.xml
+++ b/java/res/drawable/keyboard_key_feedback_ics.xml
@@ -14,8 +14,23 @@
      limitations under the License.
 -->
 
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_long_pressable="true"
-            android:drawable="@drawable/keyboard_key_feedback_more_background_holo" />
+<selector
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <!-- Left edge -->
+    <item latin:state_left_edge="true" latin:state_has_morekeys="true"
+          android:drawable="@drawable/keyboard_key_feedback_left_more_background_holo" />
+    <item latin:state_left_edge="true"
+          android:drawable="@drawable/keyboard_key_feedback_left_background_holo" />
+
+    <!-- Right edge -->
+    <item latin:state_right_edge="true" latin:state_has_morekeys="true"
+          android:drawable="@drawable/keyboard_key_feedback_right_more_background_holo" />
+    <item latin:state_right_edge="true"
+          android:drawable="@drawable/keyboard_key_feedback_right_background_holo" />
+
+    <item latin:state_has_morekeys="true"
+          android:drawable="@drawable/keyboard_key_feedback_more_background_holo" />
     <item android:drawable="@drawable/keyboard_key_feedback_background_holo" />
 </selector>
diff --git a/java/res/drawable/keyboard_key_feedback_left_ics.xml b/java/res/drawable/keyboard_key_feedback_left_ics.xml
deleted file mode 100644
index b68b37f..0000000
--- a/java/res/drawable/keyboard_key_feedback_left_ics.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 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.
--->
-
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_long_pressable="true"
-            android:drawable="@drawable/keyboard_key_feedback_left_more_background_holo" />
-    <item android:drawable="@drawable/keyboard_key_feedback_left_background_holo" />
-</selector>
diff --git a/java/res/drawable/keyboard_key_feedback_right_ics.xml b/java/res/drawable/keyboard_key_feedback_right_ics.xml
deleted file mode 100644
index 830678a..0000000
--- a/java/res/drawable/keyboard_key_feedback_right_ics.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 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.
--->
-
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_long_pressable="true"
-            android:drawable="@drawable/keyboard_key_feedback_right_more_background_holo" />
-    <item android:drawable="@drawable/keyboard_key_feedback_right_background_holo" />
-</selector>
diff --git a/java/res/layout/key_preview.xml b/java/res/layout/key_preview.xml
index 6ed892e..2fcd0c4 100644
--- a/java/res/layout/key_preview.xml
+++ b/java/res/layout/key_preview.xml
@@ -20,8 +20,8 @@
 
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
-    android:layout_height="80dp"
-    android:textSize="40dp"
+    android:layout_height="wrap_content"
+    android:background="@drawable/keyboard_key_feedback"
     android:minWidth="32dp"
     android:gravity="center"
-    />
+/>
diff --git a/java/res/layout/key_preview_ics.xml b/java/res/layout/key_preview_ics.xml
new file mode 100644
index 0000000..222e884
--- /dev/null
+++ b/java/res/layout/key_preview_ics.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 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.
+*/
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:background="@drawable/keyboard_key_feedback_ics"
+    android:minWidth="32dp"
+    android:gravity="center"
+/>
diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index c8f6435..05c53a4 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -54,12 +54,10 @@
 
         <!-- Layout resource for key press feedback.-->
         <attr name="keyPreviewLayout" format="reference" />
-        <!-- The background for key press feedback. -->
-        <attr name="keyPreviewBackground" format="reference" />
-        <!-- The background for the left edge key press feedback. -->
-        <attr name="keyPreviewLeftBackground" format="reference" />
-        <!-- The background for the right edge key press feedback. -->
-        <attr name="keyPreviewRightBackground" format="reference" />
+        <!-- Key preview background states -->
+        <attr name="state_left_edge" format="boolean" />
+        <attr name="state_right_edge" format="boolean" />
+        <attr name="state_has_morekeys" format="boolean" />
         <!-- Vertical offset of the key press feedback from the key. -->
         <attr name="keyPreviewOffset" format="dimension" />
         <!-- Height of the key press feedback popup. -->
diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml
index 634b32a..40f1ff3 100644
--- a/java/res/values/styles.xml
+++ b/java/res/values/styles.xml
@@ -54,9 +54,6 @@
         <item name="keyPopupHintLetterPadding">@dimen/key_popup_hint_letter_padding</item>
         <item name="keyShiftedLetterHintPadding">@dimen/key_uppercase_letter_padding</item>
         <item name="keyPreviewLayout">@layout/key_preview</item>
-        <item name="keyPreviewBackground">@drawable/keyboard_key_feedback</item>
-        <item name="keyPreviewLeftBackground">@null</item>
-        <item name="keyPreviewRightBackground">@null</item>
         <item name="keyPreviewTextColor">#FFFFFFFF</item>
         <item name="keyPreviewOffset">@dimen/key_preview_offset</item>
         <item name="keyPreviewHeight">@dimen/key_preview_height</item>
@@ -323,9 +320,7 @@
         <item name="keyHintLabelColor">#A0FFFFFF</item>
         <item name="keyShiftedLetterHintInactivatedColor">#66E0E4E5</item>
         <item name="keyShiftedLetterHintActivatedColor">#FFFFFFFF</item>
-        <item name="keyPreviewBackground">@drawable/keyboard_key_feedback_ics</item>
-        <item name="keyPreviewLeftBackground">@drawable/keyboard_key_feedback_left_ics</item>
-        <item name="keyPreviewRightBackground">@drawable/keyboard_key_feedback_right_ics</item>
+        <item name="keyPreviewLayout">@layout/key_preview_ics</item>
         <item name="keyPreviewTextColor">#FFFFFFFF</item>
         <item name="keyPreviewOffset">@dimen/key_preview_offset_ics</item>
         <item name="keyTextShadowColor">#00000000</item>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index 5b02f9f..ce7d797 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -59,9 +59,6 @@
  * @attr ref R.styleable#KeyboardView_keyBackground
  * @attr ref R.styleable#KeyboardView_moreKeysLayout
  * @attr ref R.styleable#KeyboardView_keyPreviewLayout
- * @attr ref R.styleable#KeyboardView_keyPreviewBackground
- * @attr ref R.styleable#KeyboardView_keyPreviewLeftBackground
- * @attr ref R.styleable#KeyboardView_keyPreviewRightBackground
  * @attr ref R.styleable#KeyboardView_keyPreviewOffset
  * @attr ref R.styleable#KeyboardView_keyPreviewHeight
  * @attr ref R.styleable#KeyboardView_keyPreviewLingerTimeout
@@ -108,9 +105,6 @@
 public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     private static final String TAG = KeyboardView.class.getSimpleName();
 
-    // Miscellaneous constants
-    private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable };
-
     // XML attributes
     private final KeyVisualAttributes mKeyVisualAttributes;
     private final int mKeyLabelHorizontalPadding;
@@ -139,12 +133,13 @@
     private Keyboard mKeyboard;
     protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
 
+    // Preview placer view
+    private final PreviewPlacerView mPreviewPlacerView;
+    private final int[] mCoordinates = new int[2];
+
     // Key preview
     private static final int PREVIEW_ALPHA = 240;
     private final int mKeyPreviewLayoutId;
-    private final Drawable mPreviewBackground;
-    private final Drawable mPreviewLeftBackground;
-    private final Drawable mPreviewRightBackground;
     private final int mPreviewOffset;
     private final int mPreviewHeight;
     private final int mPreviewLingerTimeout;
@@ -152,7 +147,28 @@
     protected final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams();
     private boolean mShowKeyPreviewPopup = true;
     private int mDelayAfterPreview;
-    private final PreviewPlacerView mPreviewPlacerView;
+    // Background state set
+    private static final int[][][] KEY_PREVIEW_BACKGROUND_STATE_TABLE = {
+        { // STATE_MIDDLE
+            EMPTY_STATE_SET,
+            { R.attr.state_has_morekeys }
+        },
+        { // STATE_LEFT
+            { R.attr.state_left_edge },
+            { R.attr.state_left_edge, R.attr.state_has_morekeys }
+        },
+        { // STATE_RIGHT
+            { R.attr.state_right_edge },
+            { R.attr.state_right_edge, R.attr.state_has_morekeys }
+        }
+    };
+    private static final int STATE_MIDDLE = 0;
+    private static final int STATE_LEFT = 1;
+    private static final int STATE_RIGHT = 2;
+    private static final int STATE_NORMAL = 0;
+    private static final int STATE_HAS_MOREKEYS = 1;
+    private static final int[] KEY_PREVIEW_BACKGROUND_DEFAULT_STATE =
+            KEY_PREVIEW_BACKGROUND_STATE_TABLE[STATE_MIDDLE][STATE_NORMAL];
 
     // Drawing
     /** True if the entire keyboard needs to be dimmed. */
@@ -230,15 +246,6 @@
                 R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
         mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground);
         mKeyBackground.getPadding(mKeyBackgroundPadding);
-        mPreviewBackground = keyboardViewAttr.getDrawable(
-                R.styleable.KeyboardView_keyPreviewBackground);
-        mPreviewLeftBackground = keyboardViewAttr.getDrawable(
-                R.styleable.KeyboardView_keyPreviewLeftBackground);
-        mPreviewRightBackground = keyboardViewAttr.getDrawable(
-                R.styleable.KeyboardView_keyPreviewRightBackground);
-        setAlpha(mPreviewBackground, PREVIEW_ALPHA);
-        setAlpha(mPreviewLeftBackground, PREVIEW_ALPHA);
-        setAlpha(mPreviewRightBackground, PREVIEW_ALPHA);
         mPreviewOffset = keyboardViewAttr.getDimensionPixelOffset(
                 R.styleable.KeyboardView_keyPreviewOffset, 0);
         mPreviewHeight = keyboardViewAttr.getDimensionPixelSize(
@@ -278,11 +285,6 @@
         mPaint.setAntiAlias(true);
     }
 
-    private static void setAlpha(final Drawable drawable, final int alpha) {
-        if (drawable == null) return;
-        drawable.setAlpha(alpha);
-    }
-
     private static void blendAlpha(final Paint paint, final int alpha) {
         final int color = paint.getColor();
         paint.setARGB((paint.getAlpha() * alpha) / Constants.Color.ALPHA_OPAQUE,
@@ -498,7 +500,7 @@
     }
 
     // Draw key background.
-    protected void onDrawKeyBackground(Key key, Canvas canvas) {
+    protected void onDrawKeyBackground(final Key key, final Canvas canvas) {
         final Rect padding = mKeyBackgroundPadding;
         final int bgWidth = key.getDrawWidth() + padding.left + padding.right;
         final int bgHeight = key.mHeight + padding.top + padding.bottom;
@@ -520,7 +522,8 @@
     }
 
     // Draw key top visuals.
-    protected void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
+    protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint,
+            final KeyDrawParams params) {
         final int keyWidth = key.getDrawWidth();
         final int keyHeight = key.mHeight;
         final float centerX = keyWidth * 0.5f;
@@ -676,7 +679,8 @@
     }
 
     // Draw popup hint "..." at the bottom right corner of the key.
-    protected void drawKeyPopupHint(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
+    protected void drawKeyPopupHint(final Key key, final Canvas canvas, final Paint paint,
+            final KeyDrawParams params) {
         final int keyWidth = key.getDrawWidth();
         final int keyHeight = key.mHeight;
 
@@ -696,7 +700,7 @@
         }
     }
 
-    private static int getCharGeometryCacheKey(char referenceChar, Paint paint) {
+    private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
         final int labelSize = (int)paint.getTextSize();
         final Typeface face = paint.getTypeface();
         final int codePointOffset = referenceChar << 15;
@@ -714,7 +718,7 @@
     // Working variable for the following methods.
     private final Rect mTextBounds = new Rect();
 
-    private float getCharHeight(char[] referenceChar, Paint paint) {
+    private float getCharHeight(final char[] referenceChar, final Paint paint) {
         final int key = getCharGeometryCacheKey(referenceChar[0], paint);
         final Float cachedValue = sTextHeightCache.get(key);
         if (cachedValue != null)
@@ -726,7 +730,7 @@
         return height;
     }
 
-    private float getCharWidth(char[] referenceChar, Paint paint) {
+    private float getCharWidth(final char[] referenceChar, final Paint paint) {
         final int key = getCharGeometryCacheKey(referenceChar[0], paint);
         final Float cachedValue = sTextWidthCache.get(key);
         if (cachedValue != null)
@@ -738,36 +742,37 @@
         return width;
     }
 
-    public float getLabelWidth(String label, Paint paint) {
-        paint.getTextBounds(label.toString(), 0, label.length(), mTextBounds);
+    public float getLabelWidth(final String label, final Paint paint) {
+        paint.getTextBounds(label, 0, label.length(), mTextBounds);
         return mTextBounds.width();
     }
 
-    protected static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width,
-            int height) {
+    protected static void drawIcon(final Canvas canvas, final Drawable icon, final int x,
+            final int y, final int width, final int height) {
         canvas.translate(x, y);
         icon.setBounds(0, 0, width, height);
         icon.draw(canvas);
         canvas.translate(-x, -y);
     }
 
-    private static void drawHorizontalLine(Canvas canvas, float y, float w, int color,
-            Paint paint) {
+    private static void drawHorizontalLine(final Canvas canvas, final float y, final float w,
+            final int color, final Paint paint) {
         paint.setStyle(Paint.Style.STROKE);
         paint.setStrokeWidth(1.0f);
         paint.setColor(color);
         canvas.drawLine(0, y, w, y, paint);
     }
 
-    private static void drawVerticalLine(Canvas canvas, float x, float h, int color, Paint paint) {
+    private static void drawVerticalLine(final Canvas canvas, final float x, final float h,
+            final int color, final Paint paint) {
         paint.setStyle(Paint.Style.STROKE);
         paint.setStrokeWidth(1.0f);
         paint.setColor(color);
         canvas.drawLine(x, 0, x, h, paint);
     }
 
-    private static void drawRectangle(Canvas canvas, float x, float y, float w, float h, int color,
-            Paint paint) {
+    private static void drawRectangle(final Canvas canvas, final float x, final float y,
+            final float w, final float h, final int color, final Paint paint) {
         paint.setStyle(Paint.Style.STROKE);
         paint.setStrokeWidth(1.0f);
         paint.setColor(color);
@@ -849,7 +854,7 @@
         }
     }
 
-    public void showGestureFloatingPreviewText(String gestureFloatingPreviewText) {
+    public void showGestureFloatingPreviewText(final String gestureFloatingPreviewText) {
         locatePreviewPlacerView();
         mPreviewPlacerView.setGestureFloatingPreviewText(gestureFloatingPreviewText);
     }
@@ -865,7 +870,6 @@
         mPreviewPlacerView.invalidatePointer(tracker);
     }
 
-    @SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16
     @Override
     public void showKeyPreview(final PointerTracker tracker) {
         final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams;
@@ -886,10 +890,17 @@
         // If key is invalid or IME is already closed, we must not show key preview.
         // Trying to show key preview while root window is closed causes
         // WindowManager.BadTokenException.
-        if (key == null)
+        if (key == null) {
             return;
+        }
 
         final KeyDrawParams drawParams = mKeyDrawParams;
+        previewText.setTextColor(drawParams.mPreviewTextColor);
+        final Drawable background = previewText.getBackground();
+        if (background != null) {
+            background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE);
+            background.setAlpha(PREVIEW_ALPHA);
+        }
         final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel;
         // What we show as preview should match what we show on a key top in onDraw().
         if (label != null) {
@@ -908,7 +919,6 @@
                     key.getPreviewIcon(mKeyboard.mIconsSet));
             previewText.setText(null);
         }
-        previewText.setBackgroundDrawable(mPreviewBackground);
 
         previewText.measure(
                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
@@ -924,32 +934,29 @@
         // The distance between the top edge of the parent key and the bottom of the visible part
         // of the key preview background.
         previewParams.mPreviewVisibleOffset = mPreviewOffset - previewText.getPaddingBottom();
-        getLocationInWindow(previewParams.mCoordinates);
+        getLocationInWindow(mCoordinates);
         // The key preview is horizontally aligned with the center of the visible part of the
         // parent key. If it doesn't fit in this {@link KeyboardView}, it is moved inward to fit and
         // the left/right background is used if such background is specified.
-        int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2
-                + previewParams.mCoordinates[0];
+        final int statePosition;
+        int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2 + mCoordinates[0];
         if (previewX < 0) {
             previewX = 0;
-            if (mPreviewLeftBackground != null) {
-                previewText.setBackgroundDrawable(mPreviewLeftBackground);
-            }
+            statePosition = STATE_LEFT;
         } else if (previewX > getWidth() - previewWidth) {
             previewX = getWidth() - previewWidth;
-            if (mPreviewRightBackground != null) {
-                previewText.setBackgroundDrawable(mPreviewRightBackground);
-            }
+            statePosition = STATE_RIGHT;
+        } else {
+            statePosition = STATE_MIDDLE;
         }
         // The key preview is placed vertically above the top edge of the parent key with an
         // arbitrary offset.
-        final int previewY = key.mY - previewHeight + mPreviewOffset
-                + previewParams.mCoordinates[1];
+        final int previewY = key.mY - previewHeight + mPreviewOffset + mCoordinates[1];
 
-        // Set the preview background state
-        previewText.getBackground().setState(
-                key.mMoreKeys != null ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
-        previewText.setTextColor(drawParams.mPreviewTextColor);
+        if (background != null) {
+            final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL;
+            background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]);
+        }
         ViewLayoutUtils.placeViewAt(
                 previewText, previewX, previewY, previewWidth, previewHeight);
         previewText.setVisibility(VISIBLE);
@@ -975,7 +982,7 @@
      * @see #invalidateAllKeys
      */
     @Override
-    public void invalidateKey(Key key) {
+    public void invalidateKey(final Key key) {
         if (mInvalidateAllKeys) return;
         if (key == null) return;
         mInvalidatedKeys.add(key);
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
index 203bab6..5dcd842 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
@@ -20,7 +20,7 @@
 
 import com.android.inputmethod.latin.ResourceUtils;
 
-public class KeyDrawParams {
+public final class KeyDrawParams {
     public Typeface mTypeface;
 
     public int mLetterSize;
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java
index 996a722..609d1a5 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java
@@ -16,7 +16,7 @@
 
 package com.android.inputmethod.keyboard.internal;
 
-public class KeyPreviewDrawParams {
+public final class KeyPreviewDrawParams {
     // The graphical geometry of the key preview.
     // <-width->
     // +-------+   ^
@@ -41,6 +41,4 @@
     // offset between the top edge of parent key and the bottom of the visible part of key
     // preview background.
     public int mPreviewVisibleOffset;
-
-    public final int[] mCoordinates = new int[2];
 }
diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java
index cd9ff85..70c38e9 100644
--- a/java/src/com/android/inputmethod/research/ResearchLog.java
+++ b/java/src/com/android/inputmethod/research/ResearchLog.java
@@ -93,7 +93,7 @@
         mFile = outputFile;
     }
 
-    public synchronized void close() {
+    public synchronized void close(final Runnable onClosed) {
         mExecutor.submit(new Callable<Object>() {
             @Override
             public Object call() throws Exception {
@@ -102,7 +102,14 @@
                         mJsonWriter.endArray();
                         mJsonWriter.flush();
                         mJsonWriter.close();
+                        if (DEBUG) {
+                            Log.d(TAG, "wrote log to " + mFile);
+                        }
                         mHasWrittenData = false;
+                    } else {
+                        if (DEBUG) {
+                            Log.d(TAG, "close() called, but no data, not outputting");
+                        }
                     }
                 } catch (Exception e) {
                     Log.d(TAG, "error when closing ResearchLog:");
@@ -111,6 +118,9 @@
                     if (mFile.exists()) {
                         mFile.setWritable(false, false);
                     }
+                    if (onClosed != null) {
+                        onClosed.run();
+                    }
                 }
                 return null;
             }
diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java
index 5c24871..763fd6e 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -84,6 +84,7 @@
  */
 public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String TAG = ResearchLogger.class.getSimpleName();
+    private static final boolean DEBUG = false;
     private static final boolean OUTPUT_ENTIRE_BUFFER = false;  // true may disclose private info
     public static final boolean DEFAULT_USABILITY_STUDY_MODE = false;
     /* package */ static boolean sIsLogging = false;
@@ -344,6 +345,9 @@
     }
 
     private void start() {
+        if (DEBUG) {
+            Log.d(TAG, "start called");
+        }
         maybeShowSplashScreen();
         updateSuspendedState();
         requestIndicatorRedraw();
@@ -371,21 +375,27 @@
     }
 
     /* package */ void stop() {
+        if (DEBUG) {
+            Log.d(TAG, "stop called");
+        }
         logStatistics();
         commitCurrentLogUnit();
 
         if (mMainLogBuffer != null) {
             publishLogBuffer(mMainLogBuffer, mMainResearchLog, false /* isIncludingPrivateData */);
-            mMainResearchLog.close();
+            mMainResearchLog.close(null /* callback */);
             mMainLogBuffer = null;
         }
         if (mFeedbackLogBuffer != null) {
-            mFeedbackLog.close();
+            mFeedbackLog.close(null /* callback */);
             mFeedbackLogBuffer = null;
         }
     }
 
     public boolean abort() {
+        if (DEBUG) {
+            Log.d(TAG, "abort called");
+        }
         boolean didAbortMainLog = false;
         if (mMainLogBuffer != null) {
             mMainLogBuffer.clear();
@@ -549,12 +559,19 @@
                 false /* isPotentiallyPrivate */);
         mFeedbackLogBuffer.shiftIn(feedbackLogUnit);
         publishLogBuffer(mFeedbackLogBuffer, mFeedbackLog, true /* isIncludingPrivateData */);
-        mFeedbackLog.close();
-        uploadNow();
+        mFeedbackLog.close(new Runnable() {
+            @Override
+            public void run() {
+                uploadNow();
+            }
+        });
         mFeedbackLog = new ResearchLog(createLogFile(mFilesDir));
     }
 
     public void uploadNow() {
+        if (DEBUG) {
+            Log.d(TAG, "calling uploadNow()");
+        }
         mInputMethodService.startService(mUploadIntent);
     }
 
@@ -574,6 +591,13 @@
     }
 
     private boolean isAllowedToLog() {
+        if (DEBUG) {
+            Log.d(TAG, "iatl: " +
+                "mipw=" + mIsPasswordView +
+                ", mils=" + mIsLoggingSuspended +
+                ", sil=" + sIsLogging +
+                ", mInFeedbackDialog=" + mInFeedbackDialog);
+        }
         return !mIsPasswordView && !mIsLoggingSuspended && sIsLogging && !mInFeedbackDialog;
     }
 
@@ -662,6 +686,9 @@
     }
 
     /* package for test */ void commitCurrentLogUnit() {
+        if (DEBUG) {
+            Log.d(TAG, "commitCurrentLogUnit");
+        }
         if (!mCurrentLogUnit.isEmpty()) {
             if (mMainLogBuffer != null) {
                 mMainLogBuffer.shiftIn(mCurrentLogUnit);
@@ -1014,26 +1041,26 @@
             final boolean isPasswordView = kid.passwordInput();
             getInstance().setIsPasswordView(isPasswordView);
             final Object[] values = {
-                    KeyboardId.elementIdToName(kid.mElementId),
-                    kid.mLocale + ":" + kid.mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET),
-                    kid.mOrientation,
-                    kid.mWidth,
-                    KeyboardId.modeName(kid.mMode),
-                    kid.imeAction(),
-                    kid.navigateNext(),
-                    kid.navigatePrevious(),
-                    kid.mClobberSettingsKey,
-                    isPasswordView,
-                    kid.mShortcutKeyEnabled,
-                    kid.mHasShortcutKey,
-                    kid.mLanguageSwitchKeyEnabled,
-                    kid.isMultiLine(),
-                    keyboard.mOccupiedWidth,
-                    keyboard.mOccupiedHeight,
-                    keyboard.mKeys
-                };
-            getInstance().enqueueEvent(EVENTKEYS_MAINKEYBOARDVIEW_SETKEYBOARD, values);
+                KeyboardId.elementIdToName(kid.mElementId),
+                kid.mLocale + ":" + kid.mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET),
+                kid.mOrientation,
+                kid.mWidth,
+                KeyboardId.modeName(kid.mMode),
+                kid.imeAction(),
+                kid.navigateNext(),
+                kid.navigatePrevious(),
+                kid.mClobberSettingsKey,
+                isPasswordView,
+                kid.mShortcutKeyEnabled,
+                kid.mHasShortcutKey,
+                kid.mLanguageSwitchKeyEnabled,
+                kid.isMultiLine(),
+                keyboard.mOccupiedWidth,
+                keyboard.mOccupiedHeight,
+                keyboard.mKeys
+            };
             getInstance().setIsPasswordView(isPasswordView);
+            getInstance().enqueueEvent(EVENTKEYS_MAINKEYBOARDVIEW_SETKEYBOARD, values);
         }
     }
 
diff --git a/native/jni/src/proximity_info_state.cpp b/native/jni/src/proximity_info_state.cpp
index 4c67ea8..b363dcc 100644
--- a/native/jni/src/proximity_info_state.cpp
+++ b/native/jni/src/proximity_info_state.cpp
@@ -395,6 +395,11 @@
         const int index = inputIndex * mProximityInfo->getKeyCount() + keyId;
         return min(mDistanceCache[index] * scale, mMaxPointToKeyLength);
     }
+    // TODO: Do not hardcode here
+    // No penalty to ' and -
+    if (codePoint == '\'' || codePoint == '-') {
+        return 0;
+    }
     // If the char is not a key on the keyboard then return the max length.
     return MAX_POINT_TO_KEY_LENGTH;
 }