From c2d7ac79bdadb4335ed69a4e9631e0ec2e3058f6 Mon Sep 17 00:00:00 2001
From: Ken Wakasa <kwakasa@google.com>
Date: Sun, 10 Oct 2010 15:40:54 +0900
Subject: [PATCH] Tweak key width for 'o'-popup chars.

We'll merge this to master for now, but we should have new attr (such as popupCharWidth) to handle this reasonably in the newly created BaseKeyboard class.

bug: 3082178
Change-Id: I024b946aafbbeb07b865a09f489dacb56f0478d0
---
 java/res/xml/kbd_popup_narrow_template.xml    | 27 +++++++++++++++++++
 java/res/xml/kbd_qwerty.xml                   |  2 +-
 java/res/xml/kbd_qwerty_black.xml             |  2 +-
 .../latin/LatinKeyboardBaseView.java          | 22 ++++++++++-----
 4 files changed, 45 insertions(+), 8 deletions(-)
 create mode 100644 java/res/xml/kbd_popup_narrow_template.xml

diff --git a/java/res/xml/kbd_popup_narrow_template.xml b/java/res/xml/kbd_popup_narrow_template.xml
new file mode 100644
index 0000000000..ed3b130031
--- /dev/null
+++ b/java/res/xml/kbd_popup_narrow_template.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2010, 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.
+*/
+-->
+
+<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
+    android:keyWidth="9.45%p"
+    android:horizontalGap="0px"
+    android:verticalGap="0px"
+    android:keyHeight="@dimen/key_height"
+    >
+</Keyboard>
diff --git a/java/res/xml/kbd_qwerty.xml b/java/res/xml/kbd_qwerty.xml
index 446467fa42..c6113abf79 100644
--- a/java/res/xml/kbd_qwerty.xml
+++ b/java/res/xml/kbd_qwerty.xml
@@ -61,7 +61,7 @@
             android:popupCharacters="@string/alternates_for_i" />
         <Key
             android:keyLabel="o"
-            android:popupKeyboard="@xml/kbd_popup_template"
+            android:popupKeyboard="@xml/kbd_popup_narrow_template"
             android:popupCharacters="@string/alternates_for_o" />
         <Key
             android:keyLabel="p"
diff --git a/java/res/xml/kbd_qwerty_black.xml b/java/res/xml/kbd_qwerty_black.xml
index f44c24fad2..33cec94746 100644
--- a/java/res/xml/kbd_qwerty_black.xml
+++ b/java/res/xml/kbd_qwerty_black.xml
@@ -61,7 +61,7 @@
             android:popupCharacters="@string/alternates_for_i" />
         <Key
             android:keyLabel="o"
-            android:popupKeyboard="@xml/kbd_popup_template"
+            android:popupKeyboard="@xml/kbd_popup_narrow_template"
             android:popupCharacters="@string/alternates_for_o" />
         <Key
             android:keyLabel="p"
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
index f96a3599de..0989dbdd34 100644
--- a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
+++ b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
@@ -1087,8 +1087,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
         return container;
     }
 
-    private static boolean isOneRowKeyboard(Keyboard keyboard) {
-        final List<Key> keys = keyboard.getKeys();
+    private static boolean isOneRowKeys(List<Key> keys) {
         if (keys.size() == 0) return false;
         final int edgeFlags = keys.get(0).edgeFlags;
         // HACK: The first key of mini keyboard which was inflated from xml and has multiple rows,
@@ -1125,24 +1124,35 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
             getLocationInWindow(mWindowOffset);
         }
 
+        // Get width of a key in the mini popup keyboard = "miniKeyWidth".
+        // On the other hand, "popupKey.width" is width of the pressed key on the main keyboard.
+        // We adjust the position of mini popup keyboard with the edge key in it:
+        //  a) When we have the leftmost key in popup keyboard directly above the pressed key
+        //     Right edges of both keys should be aligned for consistent default selection
+        //  b) When we have the rightmost key in popup keyboard directly above the pressed key
+        //     Left edges of both keys should be aligned for consistent default selection
+        final List<Key> miniKeys = mMiniKeyboard.getKeyboard().getKeys();
+        final int miniKeyWidth = miniKeys.size() > 0 ? miniKeys.get(0).width : 0;
+
         // HACK: Have the leftmost number in the popup characters right above the key
         boolean isNumberAtLeftmost =
                 hasMultiplePopupChars(popupKey) && isNumberAtLeftmostPopupChar(popupKey);
         int popupX = popupKey.x + mWindowOffset[0];
-        int popupY = popupKey.y + mWindowOffset[1];
+        popupX += getPaddingLeft();
         if (isNumberAtLeftmost) {
+            popupX += popupKey.width - miniKeyWidth;  // adjustment for a) described above
             popupX -= container.getPaddingLeft();
         } else {
-            popupX += popupKey.width + getPaddingLeft();
+            popupX += miniKeyWidth;  // adjustment for b) described above
             popupX -= container.getMeasuredWidth();
             popupX += container.getPaddingRight();
         }
+        int popupY = popupKey.y + mWindowOffset[1];
         popupY += getPaddingTop();
         popupY -= container.getMeasuredHeight();
         popupY += container.getPaddingBottom();
         final int x = popupX;
-        final int y = mShowPreview && isOneRowKeyboard(mMiniKeyboard.getKeyboard())
-                ? mPopupPreviewDisplayedY : popupY;
+        final int y = mShowPreview && isOneRowKeys(miniKeys) ? mPopupPreviewDisplayedY : popupY;
 
         int adjustedX = x;
         if (x < 0) {
-- 
GitLab