RTL - properly align from address in compose.

b/12687526.

Change-Id: I5f09303674da08b7870a84ef6477d7bfac4015cd
diff --git a/res/layout/custom_from_dropdown_item.xml b/res/layout/custom_from_dropdown_item.xml
index 684dadf..e13843f 100644
--- a/res/layout/custom_from_dropdown_item.xml
+++ b/res/layout/custom_from_dropdown_item.xml
@@ -21,6 +21,7 @@
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:paddingLeft="24dip"
+    android:paddingRight="24dip"
     android:layout_gravity="center_vertical"
     android:paddingTop="10dip"
     android:paddingBottom="10dip">
diff --git a/res/layout/custom_from_item.xml b/res/layout/custom_from_item.xml
index ccffd48..e7dcd9d 100644
--- a/res/layout/custom_from_item.xml
+++ b/res/layout/custom_from_item.xml
@@ -17,14 +17,15 @@
 -->
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="horizontal"
     android:layout_height="wrap_content"
-    android:layout_width="match_parent">
+    android:layout_width="match_parent"
+    android:orientation="horizontal"
+    android:paddingLeft="6dip"
+    android:paddingRight="6dip">
 
     <TextView android:id="@+id/spinner_account_name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:paddingLeft="6dip"
         android:singleLine="true"
         android:ellipsize="end"
         android:textAppearance="?android:attr/textAppearanceMedium"/>
@@ -32,8 +33,8 @@
     <TextView android:id="@+id/spinner_account_address"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:paddingLeft="6dip"
         android:singleLine="true"
-        android:ellipsize="end"/>
+        android:ellipsize="end"
+        style="@style/SpinnerAccountAddressStyle" />
 
 </LinearLayout>
diff --git a/res/layout/from_dropdown_item.xml b/res/layout/from_dropdown_item.xml
index 892ee14..f445127 100644
--- a/res/layout/from_dropdown_item.xml
+++ b/res/layout/from_dropdown_item.xml
@@ -29,6 +29,7 @@
         android:layout_weight="1"
         android:ellipsize="end"
         android:paddingLeft="8dip"
+        android:paddingRight="8dip"
         android:gravity="center_vertical" />
 
 </LinearLayout>
diff --git a/res/layout/from_item.xml b/res/layout/from_item.xml
index 62ed7f4..9c90d14 100644
--- a/res/layout/from_item.xml
+++ b/res/layout/from_item.xml
@@ -23,4 +23,5 @@
     android:ellipsize="end"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:paddingLeft="8dip"
-    android:gravity="center_vertical"/>
\ No newline at end of file
+    android:paddingRight="8dip"
+    android:gravity="center_vertical"/>
diff --git a/res/values-ldrtl/styles-ldrtl.xml b/res/values-ldrtl/styles-ldrtl.xml
index bd3a08d..e4d28b0 100644
--- a/res/values-ldrtl/styles-ldrtl.xml
+++ b/res/values-ldrtl/styles-ldrtl.xml
@@ -377,4 +377,8 @@
     </style>
     <!-- END Conversation list styles -->
 
+    <style name="SpinnerAccountAddressStyle">
+        <item name="android:paddingStart">@dimen/custom_from_inner_padding</item>
+    </style>
+
 </resources>
diff --git a/res/values/dimen.xml b/res/values/dimen.xml
index d121151..4b7fde0 100644
--- a/res/values/dimen.xml
+++ b/res/values/dimen.xml
@@ -187,4 +187,5 @@
     <dimen name="badge_padding_extra_width">6dip</dimen>
     <dimen name="badge_rounded_corner_radius">2dip</dimen>
 
+    <dimen name="custom_from_inner_padding">6dip</dimen>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 3445a96..e5de1a6 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -920,4 +920,8 @@
         <item name="android:textStyle">bold</item>
     </style>
 
+    <style name="SpinnerAccountAddressStyle">
+        <item name="android:paddingLeft">@dimen/custom_from_inner_padding</item>
+    </style>
+
 </resources>
diff --git a/src/com/android/mail/compose/ComposeActivity.java b/src/com/android/mail/compose/ComposeActivity.java
index fd82c98..b2bd5c1 100644
--- a/src/com/android/mail/compose/ComposeActivity.java
+++ b/src/com/android/mail/compose/ComposeActivity.java
@@ -45,6 +45,7 @@
 import android.os.ParcelFileDescriptor;
 import android.os.Parcelable;
 import android.provider.BaseColumns;
+import android.support.v4.text.BidiFormatter;
 import android.text.Editable;
 import android.text.Html;
 import android.text.SpannableString;
@@ -295,6 +296,8 @@
     private boolean mRespondedInline;
     private boolean mPerformedSendOrDiscard = false;
 
+    private final BidiFormatter mBidiFormatter = BidiFormatter.getInstance();
+
     /**
      * Can be called from a non-UI thread.
      */
@@ -770,7 +773,7 @@
         // Update the from spinner as other accounts
         // may now be available.
         if (mFromSpinner != null && mAccount != null) {
-            mFromSpinner.initialize(mComposeMode, mAccount, mAccounts, mRefMessage);
+            mFromSpinner.initialize(mComposeMode, mAccount, mAccounts, mRefMessage, mBidiFormatter);
         }
     }
 
@@ -982,7 +985,7 @@
         if (action == EDIT_DRAFT && mDraft.draftType == UIProvider.DraftType.COMPOSE) {
             action = COMPOSE;
         }
-        mFromSpinner.initialize(action, mAccount, mAccounts, mRefMessage);
+        mFromSpinner.initialize(action, mAccount, mAccounts, mRefMessage, mBidiFormatter);
 
         if (bundle != null) {
             if (bundle.containsKey(EXTRA_SELECTED_REPLY_FROM_ACCOUNT)) {
diff --git a/src/com/android/mail/compose/FromAddressSpinner.java b/src/com/android/mail/compose/FromAddressSpinner.java
index 7d82d1e..aeed90b 100644
--- a/src/com/android/mail/compose/FromAddressSpinner.java
+++ b/src/com/android/mail/compose/FromAddressSpinner.java
@@ -16,6 +16,7 @@
 package com.android.mail.compose;
 
 import android.content.Context;
+import android.support.v4.text.BidiFormatter;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.View;
@@ -90,7 +91,7 @@
      * @param syncingAccounts
      */
     public void initialize(int action, Account currentAccount, Account[] syncingAccounts,
-                Message refMessage) {
+                Message refMessage, BidiFormatter bidiFormatter) {
         final List<Account> accounts = AccountUtils.mergeAccountLists(mAccounts,
                 syncingAccounts, true /* prioritizeAccountList */);
         if (action == ComposeActivity.COMPOSE) {
@@ -111,11 +112,11 @@
             }
             mAccounts = ImmutableList.of(replyAccount);
         }
-        initFromSpinner();
+        initFromSpinner(bidiFormatter);
     }
 
     @VisibleForTesting
-    protected void initFromSpinner() {
+    protected void initFromSpinner(BidiFormatter bidiFormatter) {
         // If there are not yet any accounts in the cached synced accounts
         // because this is the first time mail was opened, and it was opened
         // directly to the compose activity, don't bother populating the reply
@@ -123,7 +124,8 @@
         if (mAccounts == null || mAccounts.size() == 0) {
             return;
         }
-        FromAddressSpinnerAdapter adapter = new FromAddressSpinnerAdapter(getContext());
+        FromAddressSpinnerAdapter adapter =
+                new FromAddressSpinnerAdapter(getContext(), bidiFormatter);
 
         mReplyFromAccounts.clear();
         for (Account account : mAccounts) {
diff --git a/src/com/android/mail/compose/FromAddressSpinnerAdapter.java b/src/com/android/mail/compose/FromAddressSpinnerAdapter.java
index 88528fd..980d419 100644
--- a/src/com/android/mail/compose/FromAddressSpinnerAdapter.java
+++ b/src/com/android/mail/compose/FromAddressSpinnerAdapter.java
@@ -16,6 +16,7 @@
 package com.android.mail.compose;
 
 import android.content.Context;
+import android.support.v4.text.BidiFormatter;
 import android.text.TextUtils;
 import android.text.util.Rfc822Tokenizer;
 import android.view.LayoutInflater;
@@ -46,11 +47,14 @@
 
     public static int ACCOUNT_ADDRESS = 1;
 
+    private final BidiFormatter mBidiFormatter;
+
     private LayoutInflater mInflater;
 
-    public FromAddressSpinnerAdapter(Context context) {
+    public FromAddressSpinnerAdapter(Context context, BidiFormatter bidiFormatter) {
         super(context, R.layout.from_item, R.id.spinner_account_address);
         sFormatString = getContext().getString(R.string.formatted_email_address);
+        mBidiFormatter = bidiFormatter;
     }
 
     protected LayoutInflater getInflater() {
@@ -78,13 +82,14 @@
         int res = fromItem.isCustomFrom ? R.layout.custom_from_item : R.layout.from_item;
         View fromEntry = convertView == null ? getInflater().inflate(res, null) : convertView;
         if (fromItem.isCustomFrom) {
-            ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(fromItem.name);
+            ((TextView) fromEntry.findViewById(R.id.spinner_account_name)).setText(
+                    mBidiFormatter.unicodeWrap(fromItem.name));
 
             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
                     .setText(formatAddress(fromItem.address));
         } else {
             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
-                    .setText(fromItem.address);
+                    .setText(mBidiFormatter.unicodeWrap(fromItem.address));
         }
         return fromEntry;
     }
@@ -97,21 +102,22 @@
         View fromEntry = getInflater().inflate(res, null);
         if (fromItem.isCustomFrom) {
             ((TextView) fromEntry.findViewById(R.id.spinner_account_name))
-                    .setText(fromItem.name);
+                    .setText(mBidiFormatter.unicodeWrap(fromItem.name));
             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
                     .setText(formatAddress(fromItem.address));
         } else {
             ((TextView) fromEntry.findViewById(R.id.spinner_account_address))
-                    .setText(fromItem.address);
+                    .setText(mBidiFormatter.unicodeWrap(fromItem.address));
         }
         return fromEntry;
     }
 
-    private static CharSequence formatAddress(String address) {
+    private CharSequence formatAddress(String address) {
         if (TextUtils.isEmpty(address)) {
             return "";
         }
-        return String.format(sFormatString, Rfc822Tokenizer.tokenize(address)[0].getAddress());
+        return String.format(sFormatString,
+                mBidiFormatter.unicodeWrap(Rfc822Tokenizer.tokenize(address)[0].getAddress()));
     }
 
     public void addAccounts(List<ReplyFromAccount> replyFromAccounts) {
diff --git a/tests/src/com/android/mail/compose/ComposeActivityTest.java b/tests/src/com/android/mail/compose/ComposeActivityTest.java
index d2cc308..52066e9 100644
--- a/tests/src/com/android/mail/compose/ComposeActivityTest.java
+++ b/tests/src/com/android/mail/compose/ComposeActivityTest.java
@@ -22,6 +22,7 @@
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.net.Uri;
+import android.support.v4.text.BidiFormatter;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.text.Html;
@@ -149,7 +150,7 @@
         activity.mFromSpinner.setCurrentAccount(currentAccount);
 
         activity.mFromSpinner.initialize(ComposeActivity.REPLY_ALL,
-                currentAccount.account, EMPTY_ACCOUNT_LIST, null);
+                currentAccount.account, EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
         runTestOnUiThread(new Runnable() {
             @Override
             public void run() {
@@ -183,7 +184,7 @@
         activity.mFromSpinner.setCurrentAccount(currentAccount);
 
         activity.mFromSpinner.initialize(ComposeActivity.REPLY_ALL,
-                currentAccount.account, EMPTY_ACCOUNT_LIST, null);
+                currentAccount.account, EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
         runTestOnUiThread(new Runnable() {
             @Override
             public void run() {
@@ -222,7 +223,7 @@
         activity.mFromSpinner.setCurrentAccount(currentAccount);
 
         activity.mFromSpinner.initialize(ComposeActivity.REPLY_ALL,
-                currentAccount.account, EMPTY_ACCOUNT_LIST, null);
+                currentAccount.account, EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
         runTestOnUiThread(new Runnable() {
             @Override
             public void run() {
@@ -550,7 +551,7 @@
                 mAccount.name, mAccount.name, mAccount.name, true, false);
         activity.mFromSpinner.setCurrentAccount(currentAccount);
         activity.mFromSpinner.initialize(ComposeActivity.REPLY, currentAccount.account,
-                EMPTY_ACCOUNT_LIST, null);
+                EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
 
         runTestOnUiThread(new Runnable() {
             @Override
@@ -590,7 +591,7 @@
                 mAccount.name, mAccount.name, mAccount.name, true, false);
         activity.mFromSpinner.setCurrentAccount(currentAccount);
         activity.mFromSpinner.initialize(ComposeActivity.REPLY_ALL,
-                currentAccount.account, EMPTY_ACCOUNT_LIST, null);
+                currentAccount.account, EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
         runTestOnUiThread(new Runnable() {
             @Override
             public void run() {
@@ -632,7 +633,7 @@
                 mAccount.name, mAccount.name, mAccount.name, true, false);
         activity.mFromSpinner.setCurrentAccount(currentAccount);
         activity.mFromSpinner.initialize(ComposeActivity.REPLY_ALL,
-                currentAccount.account, EMPTY_ACCOUNT_LIST, null);
+                currentAccount.account, EMPTY_ACCOUNT_LIST, null, BidiFormatter.getInstance());
         runTestOnUiThread(new Runnable() {
             @Override
             public void run() {