Fix bug #11256076 Spinner text is too close from the opening triangle in RTL Locales

Background Drawable padding was not taken into account in RTL Locales

- make sure the Drawables are resolved before resolving padding
- during padding resolution take care about background padding

Change-Id: Ib0c722adf5341ab4fa2182a0d0ac2ca639e85cfc
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 9c388dd..4f8368e 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -3087,6 +3087,8 @@
      */
     private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
 
+    private boolean mUseBackgroundPadding = false;
+
     /**
      * @hide
      */
@@ -12111,12 +12113,14 @@
         if (!isTextAlignmentResolved()) {
             resolveTextAlignment();
         }
-        if (!isPaddingResolved()) {
-            resolvePadding();
-        }
+        // Should resolve Drawables before Padding because we need the layout direction of the
+        // Drawable to correctly resolve Padding.
         if (!isDrawablesResolved()) {
             resolveDrawables();
         }
+        if (!isPaddingResolved()) {
+            resolvePadding();
+        }
         onRtlPropertiesChanged(getLayoutDirection());
         return true;
     }
@@ -12319,6 +12323,16 @@
             // If start / end padding are defined, they will be resolved (hence overriding) to
             // left / right or right / left depending on the resolved layout direction.
             // If start / end padding are not defined, use the left / right ones.
+            if (mBackground != null && mUseBackgroundPadding) {
+                Rect padding = sThreadLocal.get();
+                if (padding == null) {
+                    padding = new Rect();
+                    sThreadLocal.set(padding);
+                }
+                mBackground.getPadding(padding);
+                mUserPaddingLeftInitial = padding.left;
+                mUserPaddingRightInitial = padding.right;
+            }
             switch (resolvedLayoutDirection) {
                 case LAYOUT_DIRECTION_RTL:
                     if (mUserPaddingStart != UNDEFINED_PADDING) {
@@ -15314,6 +15328,9 @@
                         mUserPaddingRightInitial = padding.right;
                         internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
                 }
+                mUseBackgroundPadding = true;
+            } else {
+                mUseBackgroundPadding = false;
             }
 
             // Compare the minimum sizes of the old Drawable and the new.  If there isn't an old or
@@ -15339,6 +15356,8 @@
             /* Remove the background */
             mBackground = null;
 
+            mUseBackgroundPadding = false;
+
             if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
                 /*
                  * This view ONLY drew the background before and we're removing
@@ -15410,6 +15429,8 @@
         mUserPaddingLeftInitial = left;
         mUserPaddingRightInitial = right;
 
+        mUseBackgroundPadding = false;
+
         internalSetPadding(left, top, right, bottom);
     }
 
@@ -15496,6 +15517,8 @@
         mUserPaddingStart = start;
         mUserPaddingEnd = end;
 
+        mUseBackgroundPadding = false;
+
         switch(getLayoutDirection()) {
             case LAYOUT_DIRECTION_RTL:
                 mUserPaddingLeftInitial = end;
diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java
index e350e8d..60e2376 100644
--- a/graphics/java/android/graphics/drawable/DrawableContainer.java
+++ b/graphics/java/android/graphics/drawable/DrawableContainer.java
@@ -23,6 +23,7 @@
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
 import android.os.SystemClock;
+import android.util.LayoutDirection;
 import android.util.SparseArray;
 
 /**
@@ -59,6 +60,8 @@
     private long mExitAnimationEnd;
     private Drawable mLastDrawable;
 
+    private Insets mInsets;
+
     // overrides from Drawable
 
     @Override
@@ -78,18 +81,30 @@
                 | mDrawableContainerState.mChildrenChangingConfigurations;
     }
 
+    private boolean needsMirroring() {
+        return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL;
+    }
+
     @Override
     public boolean getPadding(Rect padding) {
         final Rect r = mDrawableContainerState.getConstantPadding();
+        boolean result = true;
         if (r != null) {
             padding.set(r);
-            return true;
-        }
-        if (mCurrDrawable != null) {
-            return mCurrDrawable.getPadding(padding);
         } else {
-            return super.getPadding(padding);
+            if (mCurrDrawable != null) {
+                result = mCurrDrawable.getPadding(padding);
+            } else {
+                result = super.getPadding(padding);
+            }
         }
+        if (needsMirroring()) {
+            final int left = padding.left;
+            final int right = padding.right;
+            padding.left = right;
+            padding.right = left;
+        }
+        return result;
     }
 
     /**
@@ -97,7 +112,7 @@
      */
     @Override
     public Insets getOpticalInsets() {
-        return (mCurrDrawable == null) ? Insets.NONE : mCurrDrawable.getOpticalInsets();
+        return mInsets;
     }
 
     @Override
@@ -334,6 +349,7 @@
             mCurrDrawable = d;
             mCurIndex = idx;
             if (d != null) {
+                mInsets = d.getOpticalInsets();
                 d.mutate();
                 if (mDrawableContainerState.mEnterFadeDuration > 0) {
                     mEnterAnimationEnd = now + mDrawableContainerState.mEnterFadeDuration;
@@ -348,9 +364,12 @@
                 d.setBounds(getBounds());
                 d.setLayoutDirection(getLayoutDirection());
                 d.setAutoMirrored(mDrawableContainerState.mAutoMirrored);
+            } else {
+                mInsets = Insets.NONE;
             }
         } else {
             mCurrDrawable = null;
+            mInsets = Insets.NONE;
             mCurIndex = -1;
         }