• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 static com.google.android.setupcompat.partnerconfig.PartnerConfigHelper.isFontWeightEnabled;
20 
21 import android.annotation.SuppressLint;
22 import android.content.Context;
23 import android.graphics.Typeface;
24 import android.util.TypedValue;
25 import android.view.ViewGroup;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import com.google.android.setupcompat.partnerconfig.PartnerConfig;
31 import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
32 import com.google.android.setupdesign.view.RichTextView;
33 
34 /** Helper class to apply partner configurations to a textView. */
35 final class TextViewPartnerStyler {
36 
37   /** Normal font weight. */
38   private static final int FONT_WEIGHT_NORMAL = 400;
39 
40   /** Applies given partner configurations {@code textPartnerConfigs} to the {@code textView}. */
41   @SuppressLint("NewApi") // Applying partner config should be guarded before Android S
applyPartnerCustomizationStyle( @onNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs)42   public static void applyPartnerCustomizationStyle(
43       @NonNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs) {
44 
45     if (textView == null || textPartnerConfigs == null) {
46       return;
47     }
48 
49     Context context = textView.getContext();
50     if (textPartnerConfigs.getTextColorConfig() != null
51         && PartnerConfigHelper.get(context)
52             .isPartnerConfigAvailable(textPartnerConfigs.getTextColorConfig())) {
53       int textColor =
54           PartnerConfigHelper.get(context)
55               .getColor(context, textPartnerConfigs.getTextColorConfig());
56       if (textColor != 0) {
57         textView.setTextColor(textColor);
58       }
59     }
60 
61     if (textPartnerConfigs.getTextLinkedColorConfig() != null
62         && PartnerConfigHelper.get(context)
63             .isPartnerConfigAvailable(textPartnerConfigs.getTextLinkedColorConfig())
64         && !PartnerStyleHelper.useDynamicColor(textView)) {
65       int linkTextColor =
66           PartnerConfigHelper.get(context)
67               .getColor(context, textPartnerConfigs.getTextLinkedColorConfig());
68       if (linkTextColor != 0) {
69         textView.setLinkTextColor(linkTextColor);
70       }
71     }
72 
73     if (textPartnerConfigs.getTextSizeConfig() != null
74         && PartnerConfigHelper.get(context)
75             .isPartnerConfigAvailable(textPartnerConfigs.getTextSizeConfig())) {
76       float textSize =
77           PartnerConfigHelper.get(context)
78               .getDimension(context, textPartnerConfigs.getTextSizeConfig(), 0);
79       if (textSize > 0) {
80         textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
81       }
82     }
83 
84     Typeface fontFamily = null;
85     if (textPartnerConfigs.getTextFontFamilyConfig() != null
86         && PartnerConfigHelper.get(context)
87             .isPartnerConfigAvailable(textPartnerConfigs.getTextFontFamilyConfig())) {
88       String fontFamilyName =
89           PartnerConfigHelper.get(context)
90               .getString(context, textPartnerConfigs.getTextFontFamilyConfig());
91       fontFamily = Typeface.create(fontFamilyName, Typeface.NORMAL);
92     }
93 
94     Typeface font;
95     if (isFontWeightEnabled(context)
96         && textPartnerConfigs.getTextFontWeightConfig() != null
97         && PartnerConfigHelper.get(context)
98             .isPartnerConfigAvailable(textPartnerConfigs.getTextFontWeightConfig())) {
99       int weight =
100           PartnerConfigHelper.get(context)
101               .getInteger(
102                   context, textPartnerConfigs.getTextFontWeightConfig(), FONT_WEIGHT_NORMAL);
103       if (fontFamily == null) {
104         fontFamily = textView.getTypeface();
105       }
106       font = Typeface.create(fontFamily, weight, /* italic= */ false);
107     } else {
108       font = fontFamily;
109     }
110 
111     if (font != null) {
112       textView.setTypeface(font);
113     }
114 
115     if (textView instanceof RichTextView && textPartnerConfigs.getLinkTextFontFamilyConfig() != null
116         && PartnerConfigHelper.get(context)
117         .isPartnerConfigAvailable(textPartnerConfigs.getLinkTextFontFamilyConfig())) {
118       String linkFontFamilyName =
119           PartnerConfigHelper.get(context)
120               .getString(context, textPartnerConfigs.getLinkTextFontFamilyConfig());
121       Typeface linkFont = Typeface.create(linkFontFamilyName, Typeface.NORMAL);
122       if (linkFont != null) {
123         ((RichTextView) textView).setSpanTypeface(linkFont);
124       }
125     }
126 
127     applyPartnerCustomizationVerticalMargins(textView, textPartnerConfigs);
128     textView.setGravity(textPartnerConfigs.getTextGravity());
129   }
130 
131   /**
132    * Applies given partner configurations {@code textPartnerConfigs} to the {@code textView}.
133    *
134    * @param textView A text view would apply the gravity
135    * @param textPartnerConfigs A partner conflagrations contains text gravity would be set
136    */
applyPartnerCustomizationLightStyle( @onNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs)137   public static void applyPartnerCustomizationLightStyle(
138       @NonNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs) {
139 
140     if (textView == null || textPartnerConfigs == null) {
141       return;
142     }
143 
144     applyPartnerCustomizationVerticalMargins(textView, textPartnerConfigs);
145     textView.setGravity(textPartnerConfigs.getTextGravity());
146   }
147 
applyPartnerCustomizationVerticalMargins( @onNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs)148   private static void applyPartnerCustomizationVerticalMargins(
149       @NonNull TextView textView, @NonNull TextPartnerConfigs textPartnerConfigs) {
150     if (textPartnerConfigs.getTextMarginTop() != null
151         || textPartnerConfigs.getTextMarginBottom() != null) {
152       Context context = textView.getContext();
153       int topMargin;
154       int bottomMargin;
155       final ViewGroup.LayoutParams lp = textView.getLayoutParams();
156       if (lp instanceof LinearLayout.LayoutParams) {
157         final LinearLayout.LayoutParams mlp = (LinearLayout.LayoutParams) lp;
158         if (textPartnerConfigs.getTextMarginTop() != null
159             && PartnerConfigHelper.get(context)
160                 .isPartnerConfigAvailable(textPartnerConfigs.getTextMarginTop())) {
161           topMargin =
162               (int)
163                   PartnerConfigHelper.get(context)
164                       .getDimension(context, textPartnerConfigs.getTextMarginTop());
165         } else {
166           topMargin = mlp.topMargin;
167         }
168 
169         if (textPartnerConfigs.getTextMarginBottom() != null
170             && PartnerConfigHelper.get(context)
171                 .isPartnerConfigAvailable(textPartnerConfigs.getTextMarginBottom())) {
172           bottomMargin =
173               (int)
174                   PartnerConfigHelper.get(context)
175                       .getDimension(context, textPartnerConfigs.getTextMarginBottom());
176         } else {
177           bottomMargin = mlp.bottomMargin;
178         }
179         mlp.setMargins(mlp.leftMargin, topMargin, mlp.rightMargin, bottomMargin);
180         textView.setLayoutParams(lp);
181       }
182     }
183   }
184 
185   /** Keeps the partner conflagrations for a textView. */
186   public static class TextPartnerConfigs {
187     private final PartnerConfig textColorConfig;
188     private final PartnerConfig textLinkedColorConfig;
189     private final PartnerConfig textSizeConfig;
190     private final PartnerConfig textFontFamilyConfig;
191     private final PartnerConfig textFontWeightConfig;
192     private final PartnerConfig textLinkFontFamilyConfig;
193     private final PartnerConfig textMarginTopConfig;
194     private final PartnerConfig textMarginBottomConfig;
195     private final int textGravity;
196 
TextPartnerConfigs( @ullable PartnerConfig textColorConfig, @Nullable PartnerConfig textLinkedColorConfig, @Nullable PartnerConfig textSizeConfig, @Nullable PartnerConfig textFontFamilyConfig, @Nullable PartnerConfig textFontWeightConfig, @Nullable PartnerConfig textLinkFontFamilyConfig, @Nullable PartnerConfig textMarginTopConfig, @Nullable PartnerConfig textMarginBottomConfig, int textGravity)197     public TextPartnerConfigs(
198         @Nullable PartnerConfig textColorConfig,
199         @Nullable PartnerConfig textLinkedColorConfig,
200         @Nullable PartnerConfig textSizeConfig,
201         @Nullable PartnerConfig textFontFamilyConfig,
202         @Nullable PartnerConfig textFontWeightConfig,
203         @Nullable PartnerConfig textLinkFontFamilyConfig,
204         @Nullable PartnerConfig textMarginTopConfig,
205         @Nullable PartnerConfig textMarginBottomConfig,
206         int textGravity) {
207       this.textColorConfig = textColorConfig;
208       this.textLinkedColorConfig = textLinkedColorConfig;
209       this.textSizeConfig = textSizeConfig;
210       this.textFontFamilyConfig = textFontFamilyConfig;
211       this.textFontWeightConfig = textFontWeightConfig;
212       this.textLinkFontFamilyConfig = textLinkFontFamilyConfig;
213       this.textMarginTopConfig = textMarginTopConfig;
214       this.textMarginBottomConfig = textMarginBottomConfig;
215       this.textGravity = textGravity;
216     }
217 
getTextColorConfig()218     public PartnerConfig getTextColorConfig() {
219       return textColorConfig;
220     }
221 
getTextLinkedColorConfig()222     public PartnerConfig getTextLinkedColorConfig() {
223       return textLinkedColorConfig;
224     }
225 
getTextSizeConfig()226     public PartnerConfig getTextSizeConfig() {
227       return textSizeConfig;
228     }
229 
getTextFontFamilyConfig()230     public PartnerConfig getTextFontFamilyConfig() {
231       return textFontFamilyConfig;
232     }
233 
getTextFontWeightConfig()234     public PartnerConfig getTextFontWeightConfig() {
235       return textFontWeightConfig;
236     }
237 
getLinkTextFontFamilyConfig()238     public PartnerConfig getLinkTextFontFamilyConfig() {
239       return textLinkFontFamilyConfig;
240     }
241 
getTextMarginTop()242     public PartnerConfig getTextMarginTop() {
243       return textMarginTopConfig;
244     }
245 
getTextMarginBottom()246     public PartnerConfig getTextMarginBottom() {
247       return textMarginBottomConfig;
248     }
249 
getTextGravity()250     public int getTextGravity() {
251       return textGravity;
252     }
253   }
254 
TextViewPartnerStyler()255   private TextViewPartnerStyler() {}
256 }
257