Minor UI fixes for name fix flow tooltip and landscape edit mode

Tooltip padding changed to match specs.
Tooltip right aligned to the tooltip icon
Demo: https://screenshot.googleplex.com/9EUAb1o5MU6

Done button renamed to 'Save card' when name input is in edit mode in landscape view.
Demo: https://screenshot.googleplex.com/3dhKo7Bi7Pf

Bug: 848955
Change-Id: Iea8dc81755bee8adbeed33274ae1020fa22d4def
Reviewed-on: https://chromium-review.googlesource.com/c/1448237
Commit-Queue: Siddharth Shah <siashah@chromium.org>
Reviewed-by: Matthew Jones <mdjones@chromium.org>
Reviewed-by: Becky Zhou <huayinz@chromium.org>
Reviewed-by: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: Jared Saul <jsaul@google.com>
Cr-Commit-Position: refs/heads/master@{#629352}
diff --git a/chrome/android/java/res/layout/autofill_name_fixflow.xml b/chrome/android/java/res/layout/autofill_name_fixflow.xml
index 45cce8b2..67efc715 100644
--- a/chrome/android/java/res/layout/autofill_name_fixflow.xml
+++ b/chrome/android/java/res/layout/autofill_name_fixflow.xml
@@ -33,6 +33,7 @@
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:imeOptions="actionDone"
+            android:imeActionLabel="@string/autofill_name_fix_flow_prompt_save_card"
             android:inputType="textCapWords"/>
 
     </org.chromium.chrome.browser.widget.CompatibilityTextInputLayout>
diff --git a/chrome/android/java/res/values/dimens.xml b/chrome/android/java/res/values/dimens.xml
index b5c0d59..42403e8 100644
--- a/chrome/android/java/res/values/dimens.xml
+++ b/chrome/android/java/res/values/dimens.xml
@@ -202,9 +202,9 @@
     <dimen name="signin_promo_account_image_size">48dp</dimen>
     <dimen name="signin_promo_cold_state_image_size">24dp</dimen>
 
-    <!-- Autofill card unmasking prompt dimensions -->
-    <dimen name="autofill_card_unmask_tooltip_horizontal_padding">8dp</dimen>
-    <dimen name="autofill_card_unmask_tooltip_vertical_padding">8dp</dimen>
+    <!-- Autofill tooltip dimensions -->
+    <dimen name="autofill_tooltip_horizontal_padding">16dp</dimen>
+    <dimen name="autofill_tooltip_vertical_padding">16dp</dimen>
 
     <!-- Button bar dimensions -->
     <dimen name="button_bar_stacked_margin">8dp</dimen>
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillNameFixFlowPrompt.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillNameFixFlowPrompt.java
index 3b549e4..851c84d7 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillNameFixFlowPrompt.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillNameFixFlowPrompt.java
@@ -5,7 +5,7 @@
 package org.chromium.chrome.browser.autofill;
 
 import android.content.Context;
-import android.support.v4.view.MarginLayoutParamsCompat;
+import android.support.v4.text.TextUtilsCompat;
 import android.support.v4.view.ViewCompat;
 import android.text.Editable;
 import android.text.TextWatcher;
@@ -14,8 +14,8 @@
 import android.view.inputmethod.EditorInfo;
 import android.widget.EditText;
 import android.widget.ImageView;
-import android.widget.LinearLayout;
 import android.widget.PopupWindow;
+import android.widget.TextView;
 import android.widget.TextView.BufferType;
 
 import org.chromium.chrome.R;
@@ -25,6 +25,7 @@
 import org.chromium.ui.modaldialog.ModalDialogProperties;
 import org.chromium.ui.modelutil.PropertyModel;
 
+import java.util.Locale;
 /**
  * Prompt that asks users to confirm user's name before saving card to Google.
  */
@@ -129,15 +130,29 @@
         if (mNameFixFlowTooltipPopup != null) return;
 
         mNameFixFlowTooltipPopup = new PopupWindow(mContext);
-        int textWidth = mDialogView.getWidth() - ViewCompat.getPaddingEnd(mNameFixFlowTooltipIcon)
-                - MarginLayoutParamsCompat.getMarginEnd(
-                        (LinearLayout.LayoutParams) mNameFixFlowTooltipIcon.getLayoutParams());
         Runnable dismissAction = () -> {
             mNameFixFlowTooltipPopup = null;
         };
+        boolean isLeftToRight = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
+                == ViewCompat.LAYOUT_DIRECTION_LTR;
         AutofillUiUtils.showTooltip(mContext, mNameFixFlowTooltipPopup,
-                R.string.autofill_save_card_prompt_cardholder_name_tooltip, textWidth,
-                mUserNameInput, dismissAction);
+                R.string.autofill_save_card_prompt_cardholder_name_tooltip,
+                new AutofillUiUtils.OffsetProvider() {
+                    @Override
+                    public int getXOffset(TextView textView) {
+                        int xOffset =
+                                mNameFixFlowTooltipIcon.getLeft() - textView.getMeasuredWidth();
+                        return Math.max(0, xOffset);
+                    }
+
+                    @Override
+                    public int getYOffset(TextView textView) {
+                        return 0;
+                    }
+                },
+                // If the layout is right to left then anchor on the edit text field else anchor on
+                // the tooltip icon, which would be on the left.
+                isLeftToRight ? mUserNameInput : mNameFixFlowTooltipIcon, dismissAction);
     }
 
     @Override
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillUiUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillUiUtils.java
index 2367e1b6..fe950892 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillUiUtils.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/AutofillUiUtils.java
@@ -7,9 +7,9 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.os.Handler;
-import android.support.v4.view.ViewCompat;
 import android.support.v4.widget.TextViewCompat;
 import android.view.View;
+import android.view.View.MeasureSpec;
 import android.view.ViewGroup;
 import android.widget.PopupWindow;
 import android.widget.TextView;
@@ -21,6 +21,16 @@
  * Helper methods that can be used across multiple Autofill UIs.
  */
 public class AutofillUiUtils {
+    /**
+     * Interface to provide the horizontal and vertical offset for the tooltip.
+     */
+    public interface OffsetProvider {
+        /** Returns the X offset for the tooltip. */
+        int getXOffset(TextView textView);
+        /** Returns the Y offset for the tooltip. */
+        int getYOffset(TextView textView);
+    }
+
     // 200ms is chosen small enough not to be detectable to human eye, but big
     // enough for to avoid any race conditions on modern machines.
     private static final int TOOLTIP_DEFERRED_PERIOD_MS = 200;
@@ -31,22 +41,20 @@
      * @param context Context required to get resources.
      * @param popup {@PopupWindow} that shows tooltip UI.
      * @param text  Text to be shown in tool tip UI.
-     * @param width Width of the textview.
+     * @param offsetProvider Interface to provide the X and Y offsets.
      * @param anchorView Anchor view under which tooltip popup has to be shown
      * @param dismissAction Tooltip dismissive action.
      */
-    public static void showTooltip(Context context, PopupWindow popup, int text, int width,
-            View anchorView, final Runnable dismissAction) {
+    public static void showTooltip(Context context, PopupWindow popup, int text,
+            OffsetProvider offsetProvider, View anchorView, final Runnable dismissAction) {
         TextView textView = new TextView(context);
         textView.setText(text);
-        textView.setWidth(width);
         TextViewCompat.setTextAppearance(textView, R.style.TextAppearance_WhiteBody);
         Resources resources = context.getResources();
-        int hPadding = resources.getDimensionPixelSize(
-                R.dimen.autofill_card_unmask_tooltip_horizontal_padding);
-        int vPadding = resources.getDimensionPixelSize(
-                R.dimen.autofill_card_unmask_tooltip_vertical_padding);
+        int hPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_horizontal_padding);
+        int vPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_vertical_padding);
         textView.setPadding(hPadding, vPadding, hPadding, vPadding);
+        textView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
 
         popup.setContentView(textView);
         popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
@@ -58,7 +66,8 @@
             Handler h = new Handler();
             h.postDelayed(dismissAction, TOOLTIP_DEFERRED_PERIOD_MS);
         });
-        popup.showAsDropDown(anchorView, ViewCompat.getPaddingStart(anchorView), 0);
+        popup.showAsDropDown(anchorView, offsetProvider.getXOffset(textView),
+                offsetProvider.getYOffset(textView));
         textView.announceForAccessibility(textView.getText());
     }
 }
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/autofill/CardUnmaskPrompt.java b/chrome/android/java/src/org/chromium/chrome/browser/autofill/CardUnmaskPrompt.java
index b9b4c2d..d6f123f 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/autofill/CardUnmaskPrompt.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/autofill/CardUnmaskPrompt.java
@@ -410,10 +410,8 @@
                         mStoreLocallyTooltipIcon.getLayoutParams()));
         text.setTextColor(Color.WHITE);
         Resources resources = mContext.getResources();
-        int hPadding = resources.getDimensionPixelSize(
-                R.dimen.autofill_card_unmask_tooltip_horizontal_padding);
-        int vPadding = resources.getDimensionPixelSize(
-                R.dimen.autofill_card_unmask_tooltip_vertical_padding);
+        int hPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_horizontal_padding);
+        int vPadding = resources.getDimensionPixelSize(R.dimen.autofill_tooltip_vertical_padding);
         text.setPadding(hPadding, vPadding, hPadding, vPadding);
 
         mStoreLocallyTooltipPopup.setContentView(text);
diff --git a/components/autofill_strings.grdp b/components/autofill_strings.grdp
index 9996ab79..2016de6 100644
--- a/components/autofill_strings.grdp
+++ b/components/autofill_strings.grdp
@@ -214,7 +214,7 @@
   <message name="IDS_AUTOFILL_SAVE_CARD_PROMPT_TITLE_LOCAL" desc="Title text for the Autofill save card prompt when the card is to be saved locally. The prompt can be either a bubble or an infobar.">
     Save card?
   </message>
-  <message name="IDS_AUTOFILL_NAME_FIX_FLOW_PROMPT_SAVE_CARD" desc="Text to show for the Autofill save credit card prompt card holder name fix flow button.">
+  <message name="IDS_AUTOFILL_NAME_FIX_FLOW_PROMPT_SAVE_CARD" desc="Text to show for the Autofill save credit card prompt card holder name fix flow button." formatter_data="android_java">
     Save card
   </message>
   <if expr="is_linux and not chromeos">