• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.google.android.setupdesign.util;
18 
19 import android.annotation.TargetApi;
20 import android.content.Context;
21 import android.os.Build;
22 import android.os.Build.VERSION_CODES;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import androidx.annotation.Nullable;
28 import com.google.android.setupcompat.partnerconfig.PartnerConfig;
29 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
30 import com.google.android.setupdesign.R;
31 import com.google.android.setupdesign.util.TextViewPartnerStyler.TextPartnerConfigs;
32 
33 /**
34  * Applies the partner style of layout to the given View {@code view}. The user needs to check if
35  * the {@code view} should apply partner heavy theme before calling this method.
36  */
37 public final class ItemStyler {
38 
39   /**
40    * Applies the heavy theme partner configs to the given listItemView {@code listItemView}. The
41    * user needs to check before calling this method:
42    *
43    * <p>1) If the {@code listItemView} should apply heavy theme resource by calling {@link
44    * PartnerStyleHelper#shouldApplyPartnerHeavyThemeResource}.
45    *
46    * <p>2) If the layout of the {@code listItemView} contains fixed resource IDs which attempts to
47    * apply heavy theme resources (The resource ID of the title is "sud_items_title" and the resource
48    * ID of the summary is "sud_items_summary"), refer to {@link R.layout#sud_items_default}.
49    *
50    * @param listItemView A view would be applied heavy theme styles
51    */
52   @TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
applyPartnerCustomizationItemStyle(@ullable View listItemView)53   public static void applyPartnerCustomizationItemStyle(@Nullable View listItemView) {
54     if (listItemView == null) {
55       return;
56     }
57     if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(listItemView)) {
58       return;
59     }
60 
61     final TextView titleTextView = listItemView.findViewById(R.id.sud_items_title);
62     // apply title text style
63     applyPartnerCustomizationItemTitleStyle(titleTextView);
64 
65     // adjust list item view gravity
66     TextView summaryTextView = listItemView.findViewById(R.id.sud_items_summary);
67     if (summaryTextView.getVisibility() == View.GONE && listItemView instanceof LinearLayout) {
68       // Set list items to vertical center when there is no summary.
69       ((LinearLayout) listItemView).setGravity(Gravity.CENTER_VERTICAL);
70     }
71 
72     // apply summary text style
73     applyPartnerCustomizationItemSummaryStyle(summaryTextView);
74 
75     // apply list item view style
76     applyPartnerCustomizationItemViewLayoutStyle(listItemView);
77   }
78 
79   /**
80    * Applies the partner heavy style to the given list item title text view. Will check the current
81    * text view enabled the partner customized heavy theme configurations before applying.
82    *
83    * @param titleTextView A textView of a list item title text.
84    */
applyPartnerCustomizationItemTitleStyle(TextView titleTextView)85   public static void applyPartnerCustomizationItemTitleStyle(TextView titleTextView) {
86     if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(titleTextView)) {
87       return;
88     }
89     TextViewPartnerStyler.applyPartnerCustomizationStyle(
90         titleTextView,
91         new TextPartnerConfigs(
92             /* textColorConfig= */ null,
93             /* textLinkedColorConfig= */ null,
94             PartnerConfig.CONFIG_ITEMS_TITLE_TEXT_SIZE,
95             PartnerConfig.CONFIG_ITEMS_TITLE_FONT_FAMILY,
96             /* textFontWeightConfig= */ null,
97             /* textLinkFontFamilyConfig= */ null,
98             /* textMarginTopConfig= */ null,
99             /* textMarginBottomConfig= */ null,
100             PartnerStyleHelper.getLayoutGravity(titleTextView.getContext())));
101   }
102 
103   /**
104    * Applies the partner heavy style to the given summary text view. Will check the current text
105    * view enabled the partner customized heavy theme configurations before applying.
106    *
107    * @param summaryTextView A textView of a list item summary text.
108    */
applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView)109   public static void applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView) {
110     if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(summaryTextView)) {
111       return;
112     }
113 
114     TextViewPartnerStyler.applyPartnerCustomizationStyle(
115         summaryTextView,
116         new TextPartnerConfigs(
117             /* textColorConfig= */ null,
118             /* textLinkedColorConfig= */ null,
119             PartnerConfig.CONFIG_ITEMS_SUMMARY_TEXT_SIZE,
120             PartnerConfig.CONFIG_ITEMS_SUMMARY_FONT_FAMILY,
121             /* textFontWeightConfig= */ null,
122             /* textLinkFontFamilyConfig= */ null,
123             PartnerConfig.CONFIG_ITEMS_SUMMARY_MARGIN_TOP,
124             /* textMarginBottomConfig= */ null,
125             PartnerStyleHelper.getLayoutGravity(summaryTextView.getContext())));
126   }
127 
applyPartnerCustomizationItemViewLayoutStyle(@ullable View listItemView)128   private static void applyPartnerCustomizationItemViewLayoutStyle(@Nullable View listItemView) {
129     Context context = listItemView.getContext();
130     float paddingTop;
131     if (PartnerConfigHelper.get(context)
132         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_TOP)) {
133       paddingTop =
134           PartnerConfigHelper.get(context)
135               .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_TOP);
136     } else {
137       paddingTop = listItemView.getPaddingTop();
138     }
139 
140     float paddingBottom;
141     if (PartnerConfigHelper.get(context)
142         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM)) {
143       paddingBottom =
144           PartnerConfigHelper.get(context)
145               .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM);
146     } else {
147       paddingBottom = listItemView.getPaddingBottom();
148     }
149 
150     if (paddingTop != listItemView.getPaddingTop()
151         || paddingBottom != listItemView.getPaddingBottom()) {
152       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
153         listItemView.setPadding(
154             listItemView.getPaddingStart(),
155             (int) paddingTop,
156             listItemView.getPaddingEnd(),
157             (int) paddingBottom);
158       } else {
159         listItemView.setPadding(
160             listItemView.getPaddingLeft(),
161             (int) paddingTop,
162             listItemView.getPaddingRight(),
163             (int) paddingBottom);
164       }
165     }
166 
167     if (PartnerConfigHelper.get(context)
168         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT)) {
169       float minHeight =
170           PartnerConfigHelper.get(context)
171               .getDimension(context, PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT);
172       listItemView.setMinimumHeight((int) minHeight);
173     }
174   }
175 
ItemStyler()176   private ItemStyler() {}
177 }
178