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