summaryrefslogtreecommitdiff
path: root/main/src/com/google/android/setupdesign/util/HeaderAreaStyler.java
blob: a52101dc6d848a6cda21e4b2dae4bea322bf9df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.setupdesign.util;

import static com.google.android.setupcompat.util.BuildCompatUtils.isAtLeastS;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import com.google.android.setupcompat.partnerconfig.PartnerConfig;
import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper;
import com.google.android.setupdesign.R;
import com.google.android.setupdesign.util.TextViewPartnerStyler.TextPartnerConfigs;

/**
 * Applies the partner customization for the header area widgets. The user needs to check if the
 * header area widgets should apply partner heavy theme or light theme before calling these methods.
 */
public final class HeaderAreaStyler {

  private static final String TAG = "HeaderAreaStyler";

  @VisibleForTesting
  static final String WARN_TO_USE_DRAWABLE =
      "To achieve scaling icon in SetupDesign lib, should use vector drawable icon from ";

  /**
   * Applies the partner style of header text to the given textView {@code header}.
   *
   * @param header A header text would apply partner style
   */
  public static void applyPartnerCustomizationHeaderStyle(@Nullable TextView header) {

    if (header == null) {
      return;
    }
    TextViewPartnerStyler.applyPartnerCustomizationStyle(
        header,
        new TextPartnerConfigs(
            PartnerConfig.CONFIG_HEADER_TEXT_COLOR,
            /* textLinkedColorConfig= */ null,
            PartnerConfig.CONFIG_HEADER_TEXT_SIZE,
            PartnerConfig.CONFIG_HEADER_FONT_FAMILY,
            PartnerConfig.CONFIG_HEADER_FONT_WEIGHT,
            /* textLinkFontFamilyConfig= */ null,
            PartnerConfig.CONFIG_HEADER_TEXT_MARGIN_TOP,
            PartnerConfig.CONFIG_HEADER_TEXT_MARGIN_BOTTOM,
            PartnerStyleHelper.getLayoutGravity(header.getContext())));
  }

  /**
   * Applies the partner heavy style of description text to the given textView {@code description}.
   *
   * @param description A description text would apply partner heavy style
   */
  public static void applyPartnerCustomizationDescriptionHeavyStyle(
      @Nullable TextView description) {

    if (description == null) {
      return;
    }
    TextViewPartnerStyler.applyPartnerCustomizationStyle(
        description,
        new TextPartnerConfigs(
            PartnerConfig.CONFIG_DESCRIPTION_TEXT_COLOR,
            PartnerConfig.CONFIG_DESCRIPTION_LINK_TEXT_COLOR,
            PartnerConfig.CONFIG_DESCRIPTION_TEXT_SIZE,
            PartnerConfig.CONFIG_DESCRIPTION_FONT_FAMILY,
            PartnerConfig.CONFIG_DESCRIPTION_FONT_WEIGHT,
            PartnerConfig.CONFIG_DESCRIPTION_LINK_FONT_FAMILY,
            PartnerConfig.CONFIG_DESCRIPTION_TEXT_MARGIN_TOP,
            PartnerConfig.CONFIG_DESCRIPTION_TEXT_MARGIN_BOTTOM,
            PartnerStyleHelper.getLayoutGravity(description.getContext())));
  }

  public static void applyPartnerCustomizationAccountStyle(
      ImageView avatar, TextView name, LinearLayout container) {
    if (avatar == null || name == null) {
      return;
    }

    Context context = avatar.getContext();

    ViewGroup.LayoutParams lpIcon = avatar.getLayoutParams();
    if (lpIcon instanceof ViewGroup.MarginLayoutParams) {
      ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lpIcon;

      int rightMargin =
          (int)
              PartnerConfigHelper.get(context)
                  .getDimension(context, PartnerConfig.CONFIG_ACCOUNT_AVATAR_MARGIN_END);
      mlp.setMargins(mlp.leftMargin, mlp.topMargin, rightMargin, mlp.bottomMargin);
    }

    int maxHeight =
        (int)
            PartnerConfigHelper.get(context)
                .getDimension(context, PartnerConfig.CONFIG_ACCOUNT_AVATAR_SIZE,
                context.getResources().getDimension(R.dimen.sud_account_avatar_max_height));
    avatar.setMaxHeight(maxHeight);

    int textSize =
        (int)
            PartnerConfigHelper.get(context)
                .getDimension(
                    context,
                    PartnerConfig.CONFIG_ACCOUNT_NAME_TEXT_SIZE,
                    context.getResources().getDimension(R.dimen.sud_account_name_text_size));
    name.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);

    String textFamily =
            PartnerConfigHelper.get(context)
                .getString(context, PartnerConfig.CONFIG_ACCOUNT_NAME_FONT_FAMILY);
    Typeface font = Typeface.create(textFamily, Typeface.NORMAL);
    if (font != null) {
      name.setTypeface(font);
    }

    int gravity = PartnerStyleHelper.getLayoutGravity(container.getContext());
    container.setGravity(gravity);
  }

  /**
   * Applies the partner style of header area to the given layout {@code headerArea}. The theme
   * should set partner heavy theme first, and then the partner style of header would be applied. As
   * for the margin bottom of header, it would also be appied when heavy theme parter config is
   * enabled.
   *
   * @param headerArea A ViewGroup would apply the partner style of header area
   */
  public static void applyPartnerCustomizationHeaderAreaStyle(ViewGroup headerArea) {
    if (headerArea == null) {
      return;
    }

    Context context = headerArea.getContext();
    int color =
        PartnerConfigHelper.get(context)
            .getColor(context, PartnerConfig.CONFIG_HEADER_AREA_BACKGROUND_COLOR);
    headerArea.setBackgroundColor(color);

    if (PartnerConfigHelper.get(context)
        .isPartnerConfigAvailable(PartnerConfig.CONFIG_HEADER_CONTAINER_MARGIN_BOTTOM)) {
      final ViewGroup.LayoutParams lp = headerArea.getLayoutParams();
      if (lp instanceof ViewGroup.MarginLayoutParams) {
        final ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lp;

        int bottomMargin =
            (int)
                PartnerConfigHelper.get(context)
                    .getDimension(context, PartnerConfig.CONFIG_HEADER_CONTAINER_MARGIN_BOTTOM);
        mlp.setMargins(mlp.leftMargin, mlp.topMargin, mlp.rightMargin, bottomMargin);
        headerArea.setLayoutParams(lp);
      }
    }
  }

  public static void applyPartnerCustomizationProgressBarStyle(@Nullable ProgressBar progressBar) {
    if (progressBar == null) {
      return;
    }
    Context context = progressBar.getContext();
    final ViewGroup.LayoutParams lp = progressBar.getLayoutParams();

    if (lp instanceof ViewGroup.MarginLayoutParams) {
      final ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lp;
      int marginTop = mlp.topMargin;
      if (PartnerConfigHelper.get(context)
          .isPartnerConfigAvailable(PartnerConfig.CONFIG_PROGRESS_BAR_MARGIN_TOP)) {
        marginTop =
            (int)
                PartnerConfigHelper.get(context)
                    .getDimension(
                        context,
                        PartnerConfig.CONFIG_PROGRESS_BAR_MARGIN_TOP,
                        context.getResources().getDimension(R.dimen.sud_progress_bar_margin_top));
      }
      int marginBottom = mlp.bottomMargin;
      if (PartnerConfigHelper.get(context)
          .isPartnerConfigAvailable(PartnerConfig.CONFIG_PROGRESS_BAR_MARGIN_BOTTOM)) {
        marginBottom =
            (int)
                PartnerConfigHelper.get(context)
                    .getDimension(
                        context,
                        PartnerConfig.CONFIG_PROGRESS_BAR_MARGIN_BOTTOM,
                        context
                            .getResources()
                            .getDimension(R.dimen.sud_progress_bar_margin_bottom));
      }

      if (marginTop != mlp.topMargin || marginBottom != mlp.bottomMargin) {
        mlp.setMargins(mlp.leftMargin, marginTop, mlp.rightMargin, marginBottom);
      }
    }
  }

  /**
   * Applies the partner style of header icon to the given {@code iconImage}. It needs to check if
   * it should apply partner resource first, and then the partner icon size would be applied.
   *
   * @param iconImage A ImageView would apply the partner style of header icon
   * @param iconContainer The container of the header icon
   */
  public static void applyPartnerCustomizationIconStyle(
      @Nullable ImageView iconImage, FrameLayout iconContainer) {
    if (iconImage == null || iconContainer == null) {
      return;
    }

    Context context = iconImage.getContext();
    int reducedIconHeight = 0;
    int gravity = PartnerStyleHelper.getLayoutGravity(context);
    if (gravity != 0) {
      setGravity(iconImage, gravity);
    }

    if (PartnerConfigHelper.get(context).isPartnerConfigAvailable(PartnerConfig.CONFIG_ICON_SIZE)) {
      checkImageType(iconImage);

      final ViewGroup.LayoutParams lpIcon = iconImage.getLayoutParams();

      lpIcon.height =
          (int)
              PartnerConfigHelper.get(context)
                  .getDimension(context, PartnerConfig.CONFIG_ICON_SIZE);

      lpIcon.width = LayoutParams.WRAP_CONTENT;
      iconImage.setScaleType(ScaleType.FIT_CENTER);

      Drawable drawable = iconImage.getDrawable();
      if (drawable != null && drawable.getIntrinsicWidth() > (2 * drawable.getIntrinsicHeight())) {
        int fixedIconHeight =
            (int) context.getResources().getDimension(R.dimen.sud_horizontal_icon_height);
        if (lpIcon.height > fixedIconHeight) {
          reducedIconHeight = lpIcon.height - fixedIconHeight;
          lpIcon.height = fixedIconHeight;
        }
      }
    }

    final ViewGroup.LayoutParams lp = iconContainer.getLayoutParams();
    boolean partnerConfigAvailable =
        PartnerConfigHelper.get(context)
            .isPartnerConfigAvailable(PartnerConfig.CONFIG_ICON_MARGIN_TOP);
    if (partnerConfigAvailable && lp instanceof ViewGroup.MarginLayoutParams) {
      final ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lp;
      int topMargin =
          (int)
              PartnerConfigHelper.get(context)
                  .getDimension(context, PartnerConfig.CONFIG_ICON_MARGIN_TOP);
      topMargin += reducedIconHeight;
      mlp.setMargins(mlp.leftMargin, topMargin, mlp.rightMargin, mlp.bottomMargin);
    }
  }

  private static void checkImageType(ImageView imageView) {
    ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnPreDrawListener(
        new ViewTreeObserver.OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            imageView.getViewTreeObserver().removeOnPreDrawListener(this);
            // TODO: Remove when Partners all used Drawable icon image and never use
            if (isAtLeastS()
                && !(imageView.getDrawable() == null
                    || (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP
                        && imageView.getDrawable() instanceof VectorDrawable)
                    || imageView.getDrawable() instanceof VectorDrawableCompat)
                && (Build.TYPE.equals("userdebug") || Build.TYPE.equals("eng"))) {
              Log.w(TAG, WARN_TO_USE_DRAWABLE + imageView.getContext().getPackageName());
            }
            return true;
          }
        });
  }

  private static void setGravity(ImageView icon, int gravity) {
    if (icon.getLayoutParams() instanceof FrameLayout.LayoutParams) {
      FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) icon.getLayoutParams();
      layoutParams.gravity = gravity;
      icon.setLayoutParams(layoutParams);
    }
  }

  private HeaderAreaStyler() {}
}