Allow to specify the padding of FocusArea highlight

Bug: 155698037
Test: manual
Change-Id: Ia9d54a9f3b845194a807fe2a147bbed9ae7eaff5
diff --git a/car-ui-lib/res/values/attrs.xml b/car-ui-lib/res/values/attrs.xml
index c42bdc7..7bab2e7 100644
--- a/car-ui-lib/res/values/attrs.xml
+++ b/car-ui-lib/res/values/attrs.xml
@@ -150,5 +150,21 @@
         <!-- The ID of a focusable descendant view which should be focused when the user nudges to
              this FocusArea. -->
         <attr name="defaultFocus" format="reference"/>
+        <!-- The paddings of FocusArea highlight. It does't impact the paddings on its child views,
+             or vice versa. -->
+        <!-- The start padding of the FocusArea highlight. -->
+        <attr name="highlightPaddingStart" format="dimension"/>
+        <!-- The end padding of the FocusArea highlight. -->
+        <attr name="highlightPaddingEnd" format="dimension"/>
+        <!-- The top padding of the FocusArea highlight. -->
+        <attr name="highlightPaddingTop" format="dimension"/>
+        <!-- The bottom padding of the FocusArea highlight. -->
+        <attr name="highlightPaddingBottom" format="dimension"/>
+        <!-- The horizontal padding of the FocusArea highlight. It can be overridden by
+             highlightPaddingStart or highlightPaddingEnd. -->
+        <attr name="highlightPaddingHorizontal" format="dimension"/>
+        <!-- The vertical padding of the FocusArea highlight.  It can be overridden by
+             highlightPaddingTop or highlightPaddingBottom. -->
+        <attr name="highlightPaddingVertical" format="dimension"/>
     </declare-styleable>
 </resources>
diff --git a/car-ui-lib/src/com/android/car/ui/FocusArea.java b/car-ui-lib/src/com/android/car/ui/FocusArea.java
index ef28c38..53bba33 100644
--- a/car-ui-lib/src/com/android/car/ui/FocusArea.java
+++ b/car-ui-lib/src/com/android/car/ui/FocusArea.java
@@ -63,6 +63,8 @@
 
     private static final String TAG = "FocusArea";
 
+    private static final int INVALID_PADDING = -1;
+
     /** Whether the FocusArea's descendant has focus (the FocusArea itself is not focusable). */
     private boolean mHasFocus;
 
@@ -160,6 +162,44 @@
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FocusArea);
         try {
             mDefaultFocusId = a.getResourceId(R.styleable.FocusArea_defaultFocus, View.NO_ID);
+
+            // Initialize the highlight padding. The padding, for example, left padding, is set in
+            // the following order:
+            // 1. if highlightPaddingStart (or highlightPaddingEnd in RTL layout) specified, use it
+            // 2. otherwise, if highlightPaddingHorizontal is specified, use it
+            // 3. otherwise use 0
+
+            int paddingStart = a.getDimensionPixelSize(
+                    R.styleable.FocusArea_highlightPaddingStart, INVALID_PADDING);
+            if (paddingStart == INVALID_PADDING) {
+                paddingStart = a.getDimensionPixelSize(
+                        R.styleable.FocusArea_highlightPaddingHorizontal, 0);
+            }
+
+            int paddingEnd = a.getDimensionPixelSize(
+                    R.styleable.FocusArea_highlightPaddingEnd, INVALID_PADDING);
+            if (paddingEnd == INVALID_PADDING) {
+                paddingEnd = a.getDimensionPixelSize(
+                        R.styleable.FocusArea_highlightPaddingHorizontal, 0);
+            }
+
+            boolean rtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
+            mPaddingLeft = rtl ? paddingEnd : paddingStart;
+            mPaddingRight = rtl ? paddingStart : paddingEnd;
+
+            mPaddingTop = a.getDimensionPixelSize(
+                    R.styleable.FocusArea_highlightPaddingTop, INVALID_PADDING);
+            if (mPaddingTop == INVALID_PADDING) {
+                mPaddingTop = a.getDimensionPixelSize(
+                        R.styleable.FocusArea_highlightPaddingVertical, 0);
+            }
+
+            mPaddingBottom = a.getDimensionPixelSize(
+                    R.styleable.FocusArea_highlightPaddingBottom, INVALID_PADDING);
+            if (mPaddingBottom == INVALID_PADDING) {
+                mPaddingBottom = a.getDimensionPixelSize(
+                        R.styleable.FocusArea_highlightPaddingVertical, 0);
+            }
         } finally {
             a.recycle();
         }