• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.managedprovisioning.common;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24 
25 import com.android.managedprovisioning.R;
26 
27 import com.google.android.setupdesign.util.HeaderAreaStyler;
28 import com.google.android.setupdesign.util.ItemStyler;
29 
30 /**
31  * Utils for handling styling provided by the setupdesign library.
32  */
33 public class StylerHelper {
34 
35     /**
36      * Applies styling to a list item.
37      *
38      * <p>The list item must contain two {@link TextView}s, {@code R.id.sud_items_title} and {@code
39      * R.id.sud_items_summary}.
40      *
41      * <p>Since the styles are applied using the setupdesign library, which additionally applies
42      * padding and margin, this method also maintains the original {@code listItem} and two {@link
43      * TextView} padding and margin.
44      *
45      * @see ItemStyler#applyPartnerCustomizationItemStyle(View)
46      */
applyListItemStyling(View listItem, ViewGroup.LayoutParams listItemViewParamsCopy)47     public void applyListItemStyling(View listItem, ViewGroup.LayoutParams listItemViewParamsCopy) {
48         requireNonNull(listItem);
49         View title = listItem.findViewById(R.id.sud_items_title);
50         View summary = listItem.findViewById(R.id.sud_items_summary);
51         SpacingInfo titleSpacing = new SpacingInfo(title, title.getLayoutParams());
52         SpacingInfo summarySpacing = new SpacingInfo(summary, summary.getLayoutParams());
53         SpacingInfo listItemSpacing = new SpacingInfo(listItem, listItemViewParamsCopy);
54         ItemStyler.applyPartnerCustomizationItemStyle(listItem);
55         titleSpacing.applySpacingToView(title);
56         summarySpacing.applySpacingToView(summary);
57         listItemSpacing.applySpacingToView(listItem);
58         listItem.setMinimumHeight(0);
59     }
60 
61     /**
62      *  Applies styling to a header {@link TextView}.
63      *
64      *  <p>Since the style is applied using the setupdesign library, which additionally applies
65      *  padding and margin, this method also maintains the original {@code header} padding and
66      *  margin.
67      *
68      * @see HeaderAreaStyler#applyPartnerCustomizationHeaderHeavyStyle(TextView)
69      */
applyHeaderStyling(TextView header, ViewGroup.LayoutParams headerViewParamsCopy)70     public void applyHeaderStyling(TextView header, ViewGroup.LayoutParams headerViewParamsCopy) {
71         requireNonNull(header);
72         SpacingInfo headerSpacing = new SpacingInfo(header, headerViewParamsCopy);
73         HeaderAreaStyler.applyPartnerCustomizationHeaderHeavyStyle(header);
74         headerSpacing.applySpacingToView(header);
75     }
76 
77     /**
78      * Helper which caches the paddings and {@link android.view.ViewGroup.LayoutParams} of a
79      * {@link View} and applies them back via {@link #applySpacingToView(View)}.
80      */
81     private static class SpacingInfo {
82         private final int mPaddingLeft;
83         private final int mPaddingTop;
84         private final int mPaddingRight;
85         private final int mPaddingBottom;
86         private final ViewGroup.LayoutParams mLayoutParams;
87 
88         /**
89          * Constructs a {@link SpacingInfo} which caches the {@code view}'s paddings and {@link
90          * android.view.ViewGroup.LayoutParams}.
91          */
SpacingInfo(View view, ViewGroup.LayoutParams layoutParamsCopy)92         SpacingInfo(View view, ViewGroup.LayoutParams layoutParamsCopy) {
93             requireNonNull(view);
94             mPaddingLeft = view.getPaddingLeft();
95             mPaddingTop = view.getPaddingTop();
96             mPaddingRight = view.getPaddingRight();
97             mPaddingBottom = view.getPaddingBottom();
98             mLayoutParams = layoutParamsCopy;
99         }
100 
applySpacingToView(View view)101         void applySpacingToView(View view) {
102             requireNonNull(view);
103             applyPaddingToView(view);
104             applyLayoutParamsToView(view);
105         }
106 
applyPaddingToView(View view)107         private void applyPaddingToView(View view) {
108             view.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
109         }
110 
applyLayoutParamsToView(View view)111         private void applyLayoutParamsToView(View view) {
112             view.setLayoutParams(mLayoutParams);
113         }
114     }
115 }
116