Move current x coordinate value into Row class

This is a preparation to address the issue.

Bug: 5245837
Change-Id: If5d463304c1689a840c8b761fedb0a8e2193734b
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index b919bcf..c2f0a3d 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -243,14 +243,13 @@
      * parser.
      * @param res resources associated with the caller's context
      * @param params the keyboard building parameters.
-     * @param row the row that this key belongs to.
-     * @param x the x coordinate of the top-left
-     * @param y the y coordinate of the top-left
+     * @param row the row that this key belongs to. row's x-coordinate will be the right edge of
+     *        this key.
      * @param parser the XML parser containing the attributes for this key
      * @param keyStyles active key styles set
      */
-    public Key(Resources res, KeyboardParams params, Row row, int x, int y,
-            XmlResourceParser parser, KeyStyles keyStyles) {
+    public Key(Resources res, KeyboardParams params, Row row, XmlResourceParser parser,
+            KeyStyles keyStyles) {
 
         final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
                 R.styleable.Keyboard);
@@ -284,6 +283,7 @@
             }
 
             final int keyboardWidth = params.mOccupiedWidth;
+            final int x = row.mCurrentX;
             int keyXPos = KeyboardBuilder.getDimensionOrFraction(keyAttr,
                     R.styleable.Keyboard_Key_keyXPos, keyboardWidth, x);
             if (keyXPos < 0) {
@@ -309,9 +309,12 @@
 
             // Horizontal gap is divided equally to both sides of the key.
             mX = keyXPos + mHorizontalGap / 2;
-            mY = y;
+            mY = row.mCurrentY;
             mWidth = keyWidth - mHorizontalGap;
 
+            // Update row to have x-coordinate of the right edge of this key.
+            row.mCurrentX = keyXPos + keyWidth;
+
             final CharSequence[] moreKeys = style.getTextArray(
                     keyAttr, R.styleable.Keyboard_Key_moreKeys);
             // In Arabic symbol layouts, we'd like to keep digits in more keys regardless of
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
index c605deb..ff26b59 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
@@ -128,7 +128,6 @@
     protected final Resources mResources;
     private final DisplayMetrics mDisplayMetrics;
 
-    private int mCurrentX = 0;
     private int mCurrentY = 0;
     private Row mCurrentRow = null;
     private boolean mLeftEdge;
@@ -314,7 +313,7 @@
                 throw new IllegalAttribute(parser, "horizontalGap");
             if (a.hasValue(R.styleable.Keyboard_verticalGap))
                 throw new IllegalAttribute(parser, "verticalGap");
-            return new Row(mResources, mParams, parser);
+            return new Row(mResources, mParams, parser, mCurrentY);
         } finally {
             a.recycle();
         }
@@ -344,7 +343,7 @@
                 if (TAG_ROW.equals(tag)) {
                     if (DEBUG) Log.d(TAG, String.format("</%s>", TAG_ROW));
                     if (!skip)
-                        endRow();
+                        endRow(row);
                     break;
                 } else if (TAG_CASE.equals(tag) || TAG_DEFAULT.equals(tag)
                         || TAG_MERGE.equals(tag)) {
@@ -364,7 +363,7 @@
         if (skip) {
             checkEndTag(TAG_KEY, parser);
         } else {
-            Key key = new Key(mResources, mParams, row, mCurrentX, mCurrentY, parser, mKeyStyles);
+            Key key = new Key(mResources, mParams, row, parser, mKeyStyles);
             if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d moreKeys=%s />",
                     TAG_KEY, (key.isEnabled() ? "" : " disabled"), key.mLabel, key.mCode,
                     Arrays.toString(key.mMoreKeys)));
@@ -392,14 +391,14 @@
             final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
                     R.styleable.Keyboard_Key);
             int keyXPos = KeyboardBuilder.getDimensionOrFraction(keyAttr,
-                    R.styleable.Keyboard_Key_keyXPos, keyboardWidth, mCurrentX);
+                    R.styleable.Keyboard_Key_keyXPos, keyboardWidth, row.mCurrentX);
             if (keyXPos < 0) {
                 // If keyXPos is negative, the actual x-coordinate will be display_width + keyXPos.
                 keyXPos += keyboardWidth;
             }
 
             checkEndTag(TAG_SPACER, parser);
-            setSpacer(keyXPos, keyWidth);
+            setSpacer(keyXPos, keyWidth, row);
         }
     }
 
@@ -655,28 +654,27 @@
     }
 
     private void startRow(Row row) {
-        mCurrentX = 0;
-        setSpacer(mCurrentX, mParams.mHorizontalEdgesPadding);
+        row.mCurrentX = 0;
+        setSpacer(row.mCurrentX, mParams.mHorizontalEdgesPadding, row);
         mCurrentRow = row;
         mLeftEdge = true;
         mRightEdgeKey = null;
     }
 
-    private void endRow() {
+    private void endRow(Row row) {
         if (mCurrentRow == null)
             throw new InflateException("orphant end row tag");
         if (mRightEdgeKey != null) {
             mRightEdgeKey.addEdgeFlags(Keyboard.EDGE_RIGHT);
             mRightEdgeKey = null;
         }
-        setSpacer(mCurrentX, mParams.mHorizontalEdgesPadding);
+        setSpacer(row.mCurrentX, mParams.mHorizontalEdgesPadding, row);
         mCurrentY += mCurrentRow.mRowHeight;
         mCurrentRow = null;
         mTopEdge = false;
     }
 
     private void endKey(Key key) {
-        mCurrentX = key.mX - key.mHorizontalGap / 2 + key.mWidth + key.mHorizontalGap;
         if (mLeftEdge) {
             key.addEdgeFlags(Keyboard.EDGE_LEFT);
             mLeftEdge = false;
@@ -690,8 +688,8 @@
     private void endKeyboard() {
     }
 
-    private void setSpacer(int keyXPos, int width) {
-        mCurrentX = keyXPos + width;
+    private void setSpacer(int keyXPos, int width, Row row) {
+        row.mCurrentX = keyXPos + width;
         mLeftEdge = false;
         mRightEdgeKey = null;
     }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/Row.java b/java/src/com/android/inputmethod/keyboard/internal/Row.java
index d53fe12..fdf1dec 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/Row.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/Row.java
@@ -35,7 +35,11 @@
     /** Default height of a key in this row. */
     public final int mRowHeight;
 
-    public Row(Resources res, KeyboardParams params, XmlResourceParser parser) {
+    public final int mCurrentY;
+    // Will be updated by {@link Key}'s constructor.
+    public int mCurrentX;
+
+    public Row(Resources res, KeyboardParams params, XmlResourceParser parser, int y) {
         final int keyboardWidth = params.mWidth;
         final int keyboardHeight = params.mHeight;
         TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
@@ -45,5 +49,8 @@
         mRowHeight = KeyboardBuilder.getDimensionOrFraction(a,
                 R.styleable.Keyboard_rowHeight, keyboardHeight, params.mDefaultRowHeight);
         a.recycle();
+
+        mCurrentY = y;
+        mCurrentX = 0;
     }
 }