• 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             null,
93             null,
94             PartnerConfig.CONFIG_ITEMS_TITLE_TEXT_SIZE,
95             PartnerConfig.CONFIG_ITEMS_TITLE_FONT_FAMILY,
96             null,
97             null,
98             null,
99             PartnerStyleHelper.getLayoutGravity(titleTextView.getContext())));
100   }
101 
102   /**
103    * Applies the partner heavy style to the given summary text view. Will check the current text
104    * view enabled the partner customized heavy theme configurations before applying.
105    *
106    * @param summaryTextView A textView of a list item summary text.
107    */
applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView)108   public static void applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView) {
109     if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(summaryTextView)) {
110       return;
111     }
112 
113     TextViewPartnerStyler.applyPartnerCustomizationStyle(
114         summaryTextView,
115         new TextPartnerConfigs(
116             null,
117             null,
118             PartnerConfig.CONFIG_ITEMS_SUMMARY_TEXT_SIZE,
119             PartnerConfig.CONFIG_ITEMS_SUMMARY_FONT_FAMILY,
120             null,
121             PartnerConfig.CONFIG_ITEMS_SUMMARY_MARGIN_TOP,
122             null,
123             PartnerStyleHelper.getLayoutGravity(summaryTextView.getContext())));
124   }
125 
applyPartnerCustomizationItemViewLayoutStyle(@ullable View listItemView)126   private static void applyPartnerCustomizationItemViewLayoutStyle(@Nullable View listItemView) {
127     Context context = listItemView.getContext();
128     float paddingTop;
129     if (PartnerConfigHelper.get(context)
130         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_TOP)) {
131       paddingTop =
132           PartnerConfigHelper.get(context)
133               .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_TOP);
134     } else {
135       paddingTop = listItemView.getPaddingTop();
136     }
137 
138     float paddingBottom;
139     if (PartnerConfigHelper.get(context)
140         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM)) {
141       paddingBottom =
142           PartnerConfigHelper.get(context)
143               .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM);
144     } else {
145       paddingBottom = listItemView.getPaddingBottom();
146     }
147 
148     if (paddingTop != listItemView.getPaddingTop()
149         || paddingBottom != listItemView.getPaddingBottom()) {
150       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
151         listItemView.setPadding(
152             listItemView.getPaddingStart(),
153             (int) paddingTop,
154             listItemView.getPaddingEnd(),
155             (int) paddingBottom);
156       } else {
157         listItemView.setPadding(
158             listItemView.getPaddingLeft(),
159             (int) paddingTop,
160             listItemView.getPaddingRight(),
161             (int) paddingBottom);
162       }
163     }
164 
165     if (PartnerConfigHelper.get(context)
166         .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT)) {
167       float minHeight =
168           PartnerConfigHelper.get(context)
169               .getDimension(context, PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT);
170       listItemView.setMinimumHeight((int) minHeight);
171     }
172   }
173 
ItemStyler()174   private ItemStyler() {}
175 }
176