summaryrefslogtreecommitdiff
path: root/main/src/com/google/android/setupdesign/template/DescriptionMixin.java
blob: bbccf8236a8a323e7d467f16206d822286cc27b9 (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
/*
 * Copyright (C) 2020 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.template;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.google.android.setupcompat.PartnerCustomizationLayout;
import com.google.android.setupcompat.internal.TemplateLayout;
import com.google.android.setupcompat.template.Mixin;
import com.google.android.setupdesign.R;
import com.google.android.setupdesign.util.HeaderAreaStyler;
import com.google.android.setupdesign.util.PartnerStyleHelper;

/**
 * A {@link com.google.android.setupcompat.template.Mixin} for setting and getting the description
 * text.
 */
public class DescriptionMixin implements Mixin {

  private static final String TAG = "DescriptionMixin";
  private final TemplateLayout templateLayout;

  /**
   * A {@link com.google.android.setupcompat.template.Mixin} for setting and getting the
   * description.
   *
   * @param layout The layout this Mixin belongs to
   * @param attrs XML attributes given to the layout
   * @param defStyleAttr The default style attribute as given to the constructor of the layout
   */
  public DescriptionMixin(
      @NonNull TemplateLayout layout, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
    templateLayout = layout;

    final TypedArray a =
        layout
            .getContext()
            .obtainStyledAttributes(attrs, R.styleable.SudDescriptionMixin, defStyleAttr, 0);

    // Set the description text
    final CharSequence descriptionText =
        a.getText(R.styleable.SudDescriptionMixin_sudDescriptionText);
    if (descriptionText != null) {
      setText(descriptionText);
    }
    // Set the description text color
    final ColorStateList descriptionTextColor =
        a.getColorStateList(R.styleable.SudDescriptionMixin_sudDescriptionTextColor);
    if (descriptionTextColor != null) {
      setTextColor(descriptionTextColor);
    }

    a.recycle();
  }

  /**
   * Applies the partner customizations to the description text (contains text alignment) and
   * background, if apply heavy theme resource, it will apply all partner customizations, otherwise,
   * only apply alignment style.
   */
  public void tryApplyPartnerCustomizationStyle() {
    TextView description = templateLayout.findManagedViewById(R.id.sud_layout_subtitle);
    boolean partnerHeavyThemeLayout = PartnerStyleHelper.isPartnerHeavyThemeLayout(templateLayout);
    if (partnerHeavyThemeLayout) {
      if (description != null) {
        HeaderAreaStyler.applyPartnerCustomizationDescriptionHeavyStyle(description);
      }
    } else if (templateLayout instanceof PartnerCustomizationLayout
        && ((PartnerCustomizationLayout) templateLayout).shouldApplyPartnerResource()) {
      if (description != null) {
        HeaderAreaStyler.applyPartnerCustomizationDescriptionLightStyle(description);
      }
    }
  }

  /** Returns the TextView displaying the description. */
  public TextView getTextView() {
    return (TextView) templateLayout.findManagedViewById(R.id.sud_layout_subtitle);
  }

  /**
   * Sets the description text and also sets the text visibility to visible. This can also be set
   * via the XML attribute {@code app:sudDescriptionText}.
   *
   * @param title The resource ID of the text to be set as description
   */
  public void setText(@StringRes int title) {
    final TextView titleView = getTextView();
    if (titleView != null && title != 0) {
      titleView.setText(title);
      setVisibility(View.VISIBLE);
    } else {
      Log.w(TAG, "Fail to set text due to either invalid resource id or text view not found.");
    }
  }

  /**
   * Sets the description text and also sets the text visibility to visible. This can also be set
   * via the XML attribute {@code app:sudDescriptionText}.
   *
   * @param title The text to be set as description
   */
  public void setText(CharSequence title) {
    final TextView titleView = getTextView();
    if (titleView != null) {
      titleView.setText(title);
      setVisibility(View.VISIBLE);
    }
  }

  /** Returns the current description text. */
  public CharSequence getText() {
    final TextView titleView = getTextView();
    return titleView != null ? titleView.getText() : null;
  }

  /** Sets the visibility of description text */
  public void setVisibility(int visibility) {
    final TextView titleView = getTextView();
    if (titleView != null) {
      titleView.setVisibility(visibility);
    }
  }

  /**
   * Sets the color of the description text. This can also be set via XML using {@code
   * app:sudDescriptionTextColor}.
   *
   * @param color The text color of the description
   */
  public void setTextColor(ColorStateList color) {
    final TextView titleView = getTextView();
    if (titleView != null) {
      titleView.setTextColor(color);
    }
  }

  /** Returns the current text color of the description. */
  public ColorStateList getTextColor() {
    final TextView titleView = getTextView();
    return titleView != null ? titleView.getTextColors() : null;
  }

  /**
   * Call this method ONLY when a layout migrates from {@link
   * com.google.android.setupdesign.items.DescriptionItem} to {@link DescriptionMixin}.
   *
   * <p>If a screen is migrated from {@link com.google.android.setupdesign.items.DescriptionItem} it
   * will looks slightly different from the original UI. This method helps keeping the UI consistent
   * with the original UI.
   */
  public void adjustLegacyDescriptionItem() {
    final TextView titleView = getTextView();
    final Context context = titleView.getContext();

    final ViewGroup.LayoutParams lp = titleView.getLayoutParams();
    if (lp instanceof ViewGroup.MarginLayoutParams) {
      final ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lp;
      int extraBottomMargin =
          (int) context.getResources().getDimension(R.dimen.sud_description_margin_bottom_extra);
      int extraTopMargin =
          (int) context.getResources().getDimension(R.dimen.sud_description_margin_top_extra);
      mlp.setMargins(
          mlp.leftMargin,
          mlp.topMargin + extraTopMargin,
          mlp.rightMargin,
          mlp.bottomMargin + extraBottomMargin);
    }
  }
}