Update bitmap drawables with RTL support.

Change-Id: Iafcbb41208b2e7e3396f2b86bee994cbaf57009f
diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
index 67884df..ca429ee 100644
--- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
+++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
@@ -73,6 +73,8 @@
     private DecodeTask mTask;
     private Cancelable mCreateFileDescriptorFactoryTask;
 
+    private int mLayoutDirection;
+
     // based on framework CL:I015d77
     private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
     private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
@@ -128,6 +130,40 @@
     }
 
     /**
+     * Set layout direction.
+     * It ends with Local so as not conflict with hidden Drawable.setLayoutDirection.
+     * @param layoutDirection the resolved layout direction for the drawable,
+     *                        either {@link android.view.View#LAYOUT_DIRECTION_LTR}
+     *                        or {@link android.view.View#LAYOUT_DIRECTION_RTL}
+     */
+    public void setLayoutDirectionLocal(int layoutDirection) {
+        if (mLayoutDirection != layoutDirection) {
+            mLayoutDirection = layoutDirection;
+            onLayoutDirectionChangeLocal(layoutDirection);
+        }
+    }
+
+    /**
+     * Called when the drawable's resolved layout direction changes.
+     * It ends with Local so as not conflict with hidden Drawable.onLayoutDirectionChange.
+     *
+     * @param layoutDirection the new resolved layout direction
+     */
+    public void onLayoutDirectionChangeLocal(int layoutDirection) {}
+
+    /**
+     * Returns the resolved layout direction for this Drawable.
+     * It ends with Local so as not conflict with hidden Drawable.getLayoutDirection.
+     *
+     * @return One of {@link android.view.View#LAYOUT_DIRECTION_LTR},
+     *         {@link android.view.View#LAYOUT_DIRECTION_RTL}
+     * @see #setLayoutDirectionLocal(int)
+     */
+    public int getLayoutDirectionLocal() {
+        return mLayoutDirection;
+    }
+
+    /**
      * Binds to the given key and start the decode process. This will first look in the cache, then
      * decode from the request key if not found.
      *
diff --git a/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java b/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java
index 953bfd0..5582344 100644
--- a/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java
+++ b/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java
@@ -25,6 +25,7 @@
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.util.Log;
+import android.view.View;
 
 import com.android.bitmap.BitmapCache;
 
@@ -65,6 +66,12 @@
     private int mTopRightCornerStyle = CORNER_STYLE_SHARP;
     private int mBottomRightCornerStyle = CORNER_STYLE_SHARP;
     private int mBottomLeftCornerStyle = CORNER_STYLE_SHARP;
+
+    private int mTopStartCornerStyle = CORNER_STYLE_SHARP;
+    private int mTopEndCornerStyle = CORNER_STYLE_SHARP;
+    private int mBottomEndCornerStyle = CORNER_STYLE_SHARP;
+    private int mBottomStartCornerStyle = CORNER_STYLE_SHARP;
+
     private int mScrimColor;
     private float mBorderWidth;
     private boolean mIsCompatibilityMode;
@@ -122,21 +129,18 @@
         }
     }
 
-    /** Set the corner styles for all four corners */
-    public void setCornerStyles(int topLeft, int topRight, int bottomRight, int bottomLeft) {
-        boolean changed = mTopLeftCornerStyle != topLeft
-                || mTopRightCornerStyle != topRight
-                || mBottomRightCornerStyle != bottomRight
-                || mBottomLeftCornerStyle != bottomLeft;
+    /** Set the corner styles for all four corners specified in RTL friendly ways */
+    public void setCornerStylesRelative(int topStart, int topEnd, int bottomEnd, int bottomStart) {
+        mTopStartCornerStyle = topStart;
+        mTopEndCornerStyle = topEnd;
+        mBottomEndCornerStyle = bottomEnd;
+        mBottomStartCornerStyle = bottomStart;
+        resolveCornerStyles();
+    }
 
-        mTopLeftCornerStyle = topLeft;
-        mTopRightCornerStyle = topRight;
-        mBottomRightCornerStyle = bottomRight;
-        mBottomLeftCornerStyle = bottomLeft;
-
-        if (changed) {
-            recalculatePath();
-        }
+    @Override
+    public void onLayoutDirectionChangeLocal(int layoutDirection) {
+        resolveCornerStyles();
     }
 
     /**
@@ -462,4 +466,30 @@
         // Finish.
         mClipPath.close();
     }
+
+    private void resolveCornerStyles() {
+        boolean isLtr = getLayoutDirectionLocal() == View.LAYOUT_DIRECTION_LTR;
+        setCornerStyles(
+            isLtr ? mTopStartCornerStyle : mTopEndCornerStyle,
+            isLtr ? mTopEndCornerStyle : mTopStartCornerStyle,
+            isLtr ? mBottomEndCornerStyle : mBottomStartCornerStyle,
+            isLtr ? mBottomStartCornerStyle : mBottomEndCornerStyle);
+    }
+
+    /** Set the corner styles for all four corners */
+    private void setCornerStyles(int topLeft, int topRight, int bottomRight, int bottomLeft) {
+        boolean changed = mTopLeftCornerStyle != topLeft
+            || mTopRightCornerStyle != topRight
+            || mBottomRightCornerStyle != bottomRight
+            || mBottomLeftCornerStyle != bottomLeft;
+
+        mTopLeftCornerStyle = topLeft;
+        mTopRightCornerStyle = topRight;
+        mBottomRightCornerStyle = bottomRight;
+        mBottomLeftCornerStyle = bottomLeft;
+
+        if (changed) {
+            recalculatePath();
+        }
+    }
 }
diff --git a/src/com/android/bitmap/view/BitmapDrawableImageView.java b/src/com/android/bitmap/view/BitmapDrawableImageView.java
index 5cbc58d..12e5b0e 100644
--- a/src/com/android/bitmap/view/BitmapDrawableImageView.java
+++ b/src/com/android/bitmap/view/BitmapDrawableImageView.java
@@ -157,4 +157,12 @@
             unbindDrawable(TEMPORARY);
         }
     }
+
+    @Override
+    public void onRtlPropertiesChanged(int layoutDirection) {
+        super.onRtlPropertiesChanged(layoutDirection);
+        if (mDrawable != null) {
+          mDrawable.setLayoutDirectionLocal(layoutDirection);
+        }
+    }
 }