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