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.view.ViewGroup; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 import androidx.annotation.Nullable; 29 import com.google.android.setupcompat.partnerconfig.PartnerConfig; 30 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper; 31 import com.google.android.setupdesign.R; 32 import com.google.android.setupdesign.util.TextViewPartnerStyler.TextPartnerConfigs; 33 34 /** 35 * Applies the partner style of layout to the given View {@code view}. The user needs to check if 36 * the {@code view} should apply partner heavy theme before calling this method. 37 */ 38 public final class ItemStyler { 39 40 /** 41 * Applies the heavy theme partner configs to the given listItemView {@code listItemView}. The 42 * user needs to check before calling this method: 43 * 44 * <p>1) If the {@code listItemView} should apply heavy theme resource by calling {@link 45 * PartnerStyleHelper#shouldApplyPartnerHeavyThemeResource}. 46 * 47 * <p>2) If the layout of the {@code listItemView} contains fixed resource IDs which attempts to 48 * apply heavy theme resources (The resource ID of the title is "sud_items_title" and the resource 49 * ID of the summary is "sud_items_summary"), refer to {@link R.layout#sud_items_default}. 50 * 51 * @param listItemView A view would be applied heavy theme styles 52 */ 53 @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) applyPartnerCustomizationItemStyle(@ullable View listItemView)54 public static void applyPartnerCustomizationItemStyle(@Nullable View listItemView) { 55 if (listItemView == null) { 56 return; 57 } 58 if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(listItemView)) { 59 return; 60 } 61 62 final TextView titleTextView = listItemView.findViewById(R.id.sud_items_title); 63 // apply title text style 64 applyPartnerCustomizationItemTitleStyle(titleTextView); 65 66 // adjust list item view gravity 67 TextView summaryTextView = listItemView.findViewById(R.id.sud_items_summary); 68 if (summaryTextView.getVisibility() == View.GONE && listItemView instanceof LinearLayout) { 69 // Set list items to vertical center when there is no summary. 70 ((LinearLayout) listItemView).setGravity(Gravity.CENTER_VERTICAL); 71 } 72 73 // apply summary text style 74 applyPartnerCustomizationItemSummaryStyle(summaryTextView); 75 76 // apply list item view style 77 applyPartnerCustomizationItemViewLayoutStyle(listItemView); 78 } 79 80 /** 81 * Applies the partner heavy style to the given list item title text view. Will check the current 82 * text view enabled the partner customized heavy theme configurations before applying. 83 * 84 * @param titleTextView A textView of a list item title text. 85 */ applyPartnerCustomizationItemTitleStyle(TextView titleTextView)86 public static void applyPartnerCustomizationItemTitleStyle(TextView titleTextView) { 87 if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(titleTextView)) { 88 return; 89 } 90 TextViewPartnerStyler.applyPartnerCustomizationStyle( 91 titleTextView, 92 new TextPartnerConfigs( 93 /* textColorConfig= */ null, 94 /* textLinkedColorConfig= */ null, 95 PartnerConfig.CONFIG_ITEMS_TITLE_TEXT_SIZE, 96 PartnerConfig.CONFIG_ITEMS_TITLE_FONT_FAMILY, 97 /* textFontWeightConfig= */ null, 98 /* textLinkFontFamilyConfig= */ null, 99 /* textMarginTopConfig= */ null, 100 /* textMarginBottomConfig= */ null, 101 PartnerStyleHelper.getLayoutGravity(titleTextView.getContext()))); 102 } 103 104 /** 105 * Applies the partner heavy style to the given summary text view. Will check the current text 106 * view enabled the partner customized heavy theme configurations before applying. 107 * 108 * @param summaryTextView A textView of a list item summary text. 109 */ applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView)110 public static void applyPartnerCustomizationItemSummaryStyle(TextView summaryTextView) { 111 if (!PartnerStyleHelper.shouldApplyPartnerHeavyThemeResource(summaryTextView)) { 112 return; 113 } 114 115 TextViewPartnerStyler.applyPartnerCustomizationStyle( 116 summaryTextView, 117 new TextPartnerConfigs( 118 /* textColorConfig= */ null, 119 /* textLinkedColorConfig= */ null, 120 PartnerConfig.CONFIG_ITEMS_SUMMARY_TEXT_SIZE, 121 PartnerConfig.CONFIG_ITEMS_SUMMARY_FONT_FAMILY, 122 /* textFontWeightConfig= */ null, 123 /* textLinkFontFamilyConfig= */ null, 124 PartnerConfig.CONFIG_ITEMS_SUMMARY_MARGIN_TOP, 125 /* textMarginBottomConfig= */ null, 126 PartnerStyleHelper.getLayoutGravity(summaryTextView.getContext()))); 127 } 128 129 /** 130 * Applies the partner layout margin style to the given list item view {@code listItemView}. The 131 * theme should set partner heavy theme config first, and then the partner layout style would be 132 * applied. 133 * 134 * @param listItemView A view would be applied partner layout margin style 135 */ 136 @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) applyPartnerCustomizationLayoutMarginStyle(@ullable View listItemView)137 public static void applyPartnerCustomizationLayoutMarginStyle(@Nullable View listItemView) { 138 if (listItemView == null) { 139 return; 140 } 141 142 Context context = listItemView.getContext(); 143 boolean partnerMarginStartAvailable = 144 PartnerConfigHelper.get(context) 145 .isPartnerConfigAvailable(PartnerConfig.CONFIG_LAYOUT_MARGIN_START); 146 boolean partnerMarginEndAvailable = 147 PartnerConfigHelper.get(context) 148 .isPartnerConfigAvailable(PartnerConfig.CONFIG_LAYOUT_MARGIN_END); 149 150 // TODO: After all users added the check before calling the API, this check can be 151 // deleted. 152 if (PartnerStyleHelper.shouldApplyPartnerResource(listItemView) 153 && (partnerMarginStartAvailable || partnerMarginEndAvailable)) { 154 int marginStart; 155 int marginEnd; 156 if (listItemView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { 157 ViewGroup.MarginLayoutParams layoutParams = 158 (ViewGroup.MarginLayoutParams) listItemView.getLayoutParams(); 159 if (partnerMarginStartAvailable) { 160 marginStart = 161 (int) 162 PartnerConfigHelper.get(context) 163 .getDimension(context, PartnerConfig.CONFIG_LAYOUT_MARGIN_START); 164 } else { 165 marginStart = layoutParams.leftMargin; 166 } 167 if (partnerMarginEndAvailable) { 168 marginEnd = 169 (int) 170 PartnerConfigHelper.get(context) 171 .getDimension(context, PartnerConfig.CONFIG_LAYOUT_MARGIN_END); 172 } else { 173 marginEnd = layoutParams.rightMargin; 174 } 175 layoutParams.setMargins( 176 marginStart, layoutParams.topMargin, marginEnd, layoutParams.bottomMargin); 177 } 178 } 179 } 180 applyPartnerCustomizationItemViewLayoutStyle(@ullable View listItemView)181 public static void applyPartnerCustomizationItemViewLayoutStyle(@Nullable View listItemView) { 182 Context context = listItemView.getContext(); 183 float paddingTop; 184 if (PartnerConfigHelper.get(context) 185 .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_TOP)) { 186 paddingTop = 187 PartnerConfigHelper.get(context) 188 .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_TOP); 189 } else { 190 paddingTop = listItemView.getPaddingTop(); 191 } 192 193 float paddingBottom; 194 if (PartnerConfigHelper.get(context) 195 .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM)) { 196 paddingBottom = 197 PartnerConfigHelper.get(context) 198 .getDimension(context, PartnerConfig.CONFIG_ITEMS_PADDING_BOTTOM); 199 } else { 200 paddingBottom = listItemView.getPaddingBottom(); 201 } 202 203 if (paddingTop != listItemView.getPaddingTop() 204 || paddingBottom != listItemView.getPaddingBottom()) { 205 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 206 listItemView.setPadding( 207 listItemView.getPaddingStart(), 208 (int) paddingTop, 209 listItemView.getPaddingEnd(), 210 (int) paddingBottom); 211 } else { 212 listItemView.setPadding( 213 listItemView.getPaddingLeft(), 214 (int) paddingTop, 215 listItemView.getPaddingRight(), 216 (int) paddingBottom); 217 } 218 } 219 220 if (PartnerConfigHelper.get(context) 221 .isPartnerConfigAvailable(PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT)) { 222 float minHeight = 223 PartnerConfigHelper.get(context) 224 .getDimension(context, PartnerConfig.CONFIG_ITEMS_MIN_HEIGHT); 225 listItemView.setMinimumHeight((int) minHeight); 226 } 227 } 228 ItemStyler()229 private ItemStyler() {} 230 } 231