1/*
2 * Copyright (C) 2017 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
17package com.android.setupwizardlib.template;
18
19import android.content.res.ColorStateList;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.widget.TextView;
23
24import com.android.setupwizardlib.R;
25import com.android.setupwizardlib.TemplateLayout;
26
27/**
28 * A {@link Mixin} displaying a header text that can be set to different colors. This Mixin is
29 * registered to the tempalte using HeaderMixin.class, and can be retrieved using:
30 * {@code (ColoredHeaderMixin) templateLayout.getMixin(HeaderMixin.class}.
31 */
32public class ColoredHeaderMixin extends HeaderMixin {
33
34    /**
35     * {@inheritDoc}
36     */
37    public ColoredHeaderMixin(TemplateLayout layout, AttributeSet attrs, int defStyleAttr) {
38        super(layout, attrs, defStyleAttr);
39
40        final TypedArray a = layout.getContext().obtainStyledAttributes(
41                attrs, R.styleable.SuwColoredHeaderMixin, defStyleAttr, 0);
42
43        // Set the header color
44        final ColorStateList headerColor =
45                a.getColorStateList(R.styleable.SuwColoredHeaderMixin_suwHeaderColor);
46        if (headerColor != null) {
47            setColor(headerColor);
48        }
49
50        a.recycle();
51    }
52
53    /**
54     * Sets the color of the header text. This can also be set via XML using
55     * {@code app:suwHeaderColor}.
56     *
57     * @param color The text color of the header.
58     */
59    public void setColor(ColorStateList color) {
60        final TextView titleView = getTextView();
61        if (titleView != null) {
62            titleView.setTextColor(color);
63        }
64    }
65
66    /**
67     * @return The current text color of the header.
68     */
69    public ColorStateList getColor() {
70        final TextView titleView = getTextView();
71        return titleView != null ? titleView.getTextColors() : null;
72    }
73}
74