[go: nahoru, domu]

Add attributes to customize slice row style

- Add new attributes to customize paddings and divider height of row view
- seperate headerDividerPadding into bottomDividerStartPadding and bottomDividerEndPadding

Test: ./gradlew slice-view:connectedCheck
Bug: 114807643
Change-Id: I2cdf563fcada8944d9735a46fe5baeb2a0b1479c
diff --git a/slices/view/src/main/java/androidx/slice/widget/RowStyle.java b/slices/view/src/main/java/androidx/slice/widget/RowStyle.java
new file mode 100644
index 0000000..2f8e5e4
--- /dev/null
+++ b/slices/view/src/main/java/androidx/slice/widget/RowStyle.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.slice.widget;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+
+import androidx.annotation.RequiresApi;
+import androidx.annotation.RestrictTo;
+import androidx.slice.view.R;
+
+/**
+ * Holds style information shared between child views of a row
+ * @hide
+ */
+@RestrictTo(RestrictTo.Scope.LIBRARY)
+@RequiresApi(19)
+public class RowStyle {
+    private int mTitleItemEndPadding;
+    private int mContentStartPadding;
+    private int mContentEndPadding;
+    private int mEndItemStartPadding;
+    private int mEndItemEndPadding;
+    private int mBottomDividerStartPadding;
+    private int mBottomDividerEndPadding;
+    private int mActionDividerHeight;
+
+    public RowStyle(Context context, int resId) {
+        TypedArray a = context.getTheme().obtainStyledAttributes(resId, R.styleable.RowStyle);
+        try {
+            mTitleItemEndPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_titleItemEndPadding, -1);
+            mContentStartPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_contentStartPadding, -1);
+            mContentEndPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_contentEndPadding, -1);
+            mEndItemStartPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_endItemStartPadding, -1);
+            mEndItemEndPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_endItemEndPadding, -1);
+            mBottomDividerStartPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_bottomDividerStartPadding, -1);
+            mBottomDividerEndPadding = (int) a.getDimension(
+                    R.styleable.RowStyle_bottomDividerEndPadding, -1);
+            mActionDividerHeight = (int) a.getDimension(
+                    R.styleable.RowStyle_actionDividerHeight, -1);
+        } finally {
+            a.recycle();
+        }
+    }
+
+    public int getTitleItemEndPadding() {
+        return mTitleItemEndPadding;
+    }
+
+    public int getContentStartPadding() {
+        return mContentStartPadding;
+    }
+
+    public int getContentEndPadding() {
+        return mContentEndPadding;
+    }
+
+    public int getEndItemStartPadding() {
+        return mEndItemStartPadding;
+    }
+
+    public int getEndItemEndPadding() {
+        return mEndItemEndPadding;
+    }
+
+    public int getBottomDividerStartPadding() {
+        return mBottomDividerStartPadding;
+    }
+
+    public int getBottomDividerEndPadding() {
+        return mBottomDividerEndPadding;
+    }
+
+    public int getActionDividerHeight() {
+        return mActionDividerHeight;
+    }
+}
diff --git a/slices/view/src/main/java/androidx/slice/widget/RowView.java b/slices/view/src/main/java/androidx/slice/widget/RowView.java
index bc7a6ac..54bde96 100644
--- a/slices/view/src/main/java/androidx/slice/widget/RowView.java
+++ b/slices/view/src/main/java/androidx/slice/widget/RowView.java
@@ -156,7 +156,6 @@
     private int mIdealRangeHeight;
     // How big mRangeBar wants to be.
     private int mMeasuredRangeHeight;
-    private int mMaxSmallHeight;
 
     public RowView(Context context) {
         super(context);
@@ -183,6 +182,57 @@
     }
 
     @Override
+    public void setStyle(SliceStyle styles) {
+        super.setStyle(styles);
+        applyRowStyle();
+    }
+
+    private void applyRowStyle() {
+        if (mSliceStyle == null || mSliceStyle.getRowStyle() == null) {
+            return;
+        }
+
+        final RowStyle rowStyle = mSliceStyle.getRowStyle();
+        setViewPaddingEnd(mStartContainer, rowStyle.getTitleItemEndPadding());
+        setViewSidePaddings(mContent,
+                rowStyle.getContentStartPadding(), rowStyle.getContentEndPadding());
+        setViewSidePaddings(mEndContainer,
+                rowStyle.getEndItemStartPadding(), rowStyle.getEndItemEndPadding());
+        setViewSideMargins(mBottomDivider,
+                rowStyle.getBottomDividerStartPadding(), rowStyle.getBottomDividerEndPadding());
+        setViewHeight(mActionDivider, rowStyle.getActionDividerHeight());
+    }
+
+    private void setViewPaddingEnd(View v, int end) {
+        if (v != null && end >= 0) {
+            v.setPaddingRelative(v.getPaddingStart(), v.getPaddingTop(), end, v.getPaddingBottom());
+        }
+    }
+
+    private void setViewSidePaddings(View v, int start, int end) {
+        if (v != null && start >= 0 && end >= 0) {
+            v.setPaddingRelative(start, v.getPaddingTop(), end, v.getPaddingBottom());
+        }
+    }
+
+    private void setViewSideMargins(View v, int start, int end) {
+        if (v != null && start >= 0 && end >= 0) {
+            final MarginLayoutParams params = (MarginLayoutParams) v.getLayoutParams();
+            params.setMarginStart(start);
+            params.setMarginEnd(end);
+            mBottomDivider.setLayoutParams(params);
+        }
+    }
+
+    private void setViewHeight(View v, int height) {
+        if (v != null && height >= 0) {
+            final ViewGroup.LayoutParams params = v.getLayoutParams();
+            params.height = height;
+            v.setLayoutParams(params);
+        }
+    }
+
+    @Override
     public void setInsets(int l, int t, int r, int b) {
         super.setInsets(l, t, r, b);
         mRootView.setPadding(l, t, r, b);
@@ -348,18 +398,7 @@
 
         addSubtitle(titleItem != null /* hasTitle */);
 
-        if (mRowContent.hasBottomDivider()) {
-            if (mSliceStyle != null) {
-                final int padding = mSliceStyle.getHeaderDividerPadding();
-                LinearLayout.LayoutParams lp =
-                        (LinearLayout.LayoutParams) mBottomDivider.getLayoutParams();
-                lp.setMargins(padding, 0, padding, 0);
-                mBottomDivider.setLayoutParams(lp);
-            }
-            mBottomDivider.setVisibility(View.VISIBLE);
-        } else {
-            mBottomDivider.setVisibility(View.GONE);
-        }
+        mBottomDivider.setVisibility(mRowContent.hasBottomDivider() ? View.VISIBLE : View.GONE);
 
         SliceItem primaryAction = mRowContent.getPrimaryAction();
         if (primaryAction != null && primaryAction != mStartItem) {
diff --git a/slices/view/src/main/java/androidx/slice/widget/SliceStyle.java b/slices/view/src/main/java/androidx/slice/widget/SliceStyle.java
index 8995dea..83be746b 100644
--- a/slices/view/src/main/java/androidx/slice/widget/SliceStyle.java
+++ b/slices/view/src/main/java/androidx/slice/widget/SliceStyle.java
@@ -48,7 +48,6 @@
     private int mSubtitleColor;
     private int mHeaderTitleSize;
     private int mHeaderSubtitleSize;
-    private int mHeaderDividerPadding;
     private int mVerticalHeaderTextPadding;
     private int mTitleSize;
     private int mSubtitleSize;
@@ -75,6 +74,8 @@
     private int mListMinScrollHeight;
     private int mListLargeHeight;
 
+    private RowStyle mRowStyle;
+
     public SliceStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SliceView,
                 defStyleAttr, defStyleRes);
@@ -90,8 +91,6 @@
                     R.styleable.SliceView_headerSubtitleSize, 0);
             mVerticalHeaderTextPadding = (int) a.getDimension(
                     R.styleable.SliceView_headerTextVerticalPadding, 0);
-            mHeaderDividerPadding = (int) a.getDimension(
-                    R.styleable.SliceView_headerDividerPadding, 0);
 
             mTitleSize = (int) a.getDimension(R.styleable.SliceView_titleSize, 0);
             mSubtitleSize = (int) a.getDimension(
@@ -108,6 +107,11 @@
                     R.styleable.SliceView_gridTextVerticalPadding, defaultVerticalGridPadding);
             mGridTopPadding = (int) a.getDimension(R.styleable.SliceView_gridTopPadding, 0);
             mGridBottomPadding = (int) a.getDimension(R.styleable.SliceView_gridBottomPadding, 0);
+
+            int rowStyleRes = a.getResourceId(R.styleable.SliceView_rowStyle, 0);
+            if (rowStyleRes != 0) {
+                mRowStyle = new RowStyle(context, rowStyleRes);
+            }
         } finally {
             a.recycle();
         }
@@ -167,10 +171,6 @@
         return mVerticalHeaderTextPadding;
     }
 
-    public int getHeaderDividerPadding() {
-        return mHeaderDividerPadding;
-    }
-
     public int getTitleSize() {
         return mTitleSize;
     }
@@ -203,6 +203,10 @@
         return mGridBottomPadding;
     }
 
+    public RowStyle getRowStyle() {
+        return mRowStyle;
+    }
+
     public int getRowHeight(RowContent row, SliceViewPolicy policy) {
         int maxHeight = policy.getMaxSmallHeight() > 0 ? policy.getMaxSmallHeight() : mRowMaxHeight;
         if (row.getRange() != null || policy.getMode() == MODE_LARGE) {
diff --git a/slices/view/src/main/res-public/values/public_attrs.xml b/slices/view/src/main/res-public/values/public_attrs.xml
index ff4facc..dd8e6b6 100644
--- a/slices/view/src/main/res-public/values/public_attrs.xml
+++ b/slices/view/src/main/res-public/values/public_attrs.xml
@@ -23,7 +23,6 @@
     <public type="attr" name="headerTitleSize" />
     <public type="attr" name="headerSubtitleSize" />
     <public type="attr" name="headerTextVerticalPadding" />
-    <public type="attr" name="headerDividerPadding" />
     <public type="attr" name="titleSize" />
     <public type="attr" name="subtitleSize" />
     <public type="attr" name="textVerticalPadding" />
@@ -32,5 +31,14 @@
     <public type="attr" name="gridTextVerticalPadding" />
     <public type="attr" name="gridTopPadding" />
     <public type="attr" name="gridBottomPadding" />
+    <public type="attr" name="rowViewStyle" />
     <public type="attr" name="sliceViewStyle" />
+    <public type="attr" name="titleItemEndPadding" />
+    <public type="attr" name="contentStartPadding" />
+    <public type="attr" name="contentEndPadding" />
+    <public type="attr" name="endItemStartPadding" />
+    <public type="attr" name="endItemEndPadding" />
+    <public type="attr" name="bottomDividerStartPadding" />
+    <public type="attr" name="bottomDividerEndPadding" />
+    <public type="attr" name="actionDividerHeight" />
 </resources>
diff --git a/slices/view/src/main/res/layout/abc_slice_small_template.xml b/slices/view/src/main/res/layout/abc_slice_small_template.xml
index 583f3ee..1e4fce3 100644
--- a/slices/view/src/main/res/layout/abc_slice_small_template.xml
+++ b/slices/view/src/main/res/layout/abc_slice_small_template.xml
@@ -86,6 +86,7 @@
             android:layout_marginTop="8dp"
             android:layout_marginBottom="8dp"
             android:background="?android:attr/listDivider"
+            android:layout_gravity="center_vertical"
             android:visibility="gone"/>
 
         <LinearLayout
diff --git a/slices/view/src/main/res/values/attrs.xml b/slices/view/src/main/res/values/attrs.xml
index c7dfb49..f6108fa 100644
--- a/slices/view/src/main/res/values/attrs.xml
+++ b/slices/view/src/main/res/values/attrs.xml
@@ -32,8 +32,6 @@
         <attr name="headerSubtitleSize" format="dimension"/>
         <!-- Vertical padding to use between header title text and header subtitle text. -->
         <attr name="headerTextVerticalPadding" format="dimension" />
-        <!-- Padding to use between header divider and the edge of the view. -->
-        <attr name="headerDividerPadding" format="dimension" />
 
         <!-- Text size to use for title text in a non-header row of the slice. -->
         <attr name="titleSize" format="dimension" />
@@ -56,9 +54,36 @@
          last row of a slice. Use this to specify padding to apply to the bottom of the grid row in
          this situation. -->
         <attr name="gridBottomPadding" format="dimension" />
+
+        <!-- To apply a style for all slice rows shown within an activity or app you
+             may set this in your app theme pointing to your custom RowView style.-->
+        <attr name="rowStyle" format="reference" />
     </declare-styleable>
 
     <!-- To apply a style for all slices shown within an activity or app you
          may set this in your app theme pointing to your custom SliceView style.-->
     <attr name="sliceViewStyle" format="reference" />
+
+    <declare-styleable name="RowStyle">
+        <!-- Padding to the end edge of title items shown in the row. -->
+        <attr name="titleItemEndPadding" format="dimension" />
+
+        <!-- Padding to the start edge of the content in the row. -->
+        <attr name="contentStartPadding" format="dimension" />
+        <!-- Padding to the end edge of the content in the row. -->
+        <attr name="contentEndPadding" format="dimension" />
+
+        <!-- Padding to the start edge of the collection of items shown at the end of the row. -->
+        <attr name="endItemStartPadding" format="dimension" />
+        <!-- Padding to the end edge of the collection of items shown at the end of the row. -->
+        <attr name="endItemEndPadding" format="dimension" />
+
+        <!-- Padding to the start edge of the bottom divider in the row. -->
+        <attr name="bottomDividerStartPadding" format="dimension" />
+        <!-- Padding to the end edge of the bottom divider in the row. -->
+        <attr name="bottomDividerEndPadding" format="dimension" />
+
+        <!-- Height to use for action divider in the row. -->
+        <attr name="actionDividerHeight" format="dimension"/>
+    </declare-styleable>
 </resources>
\ No newline at end of file