GlifLayout.java revision fce4cf6161b2a1644ad21034f0afe4087d659ab4
1/*
2 * Copyright (C) 2015 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;
18
19import android.annotation.TargetApi;
20import android.content.Context;
21import android.content.res.ColorStateList;
22import android.content.res.TypedArray;
23import android.graphics.drawable.Drawable;
24import android.os.Build.VERSION_CODES;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.ImageView;
30import android.widget.ScrollView;
31import android.widget.TextView;
32
33/**
34 * Layout for the GLIF theme used in Setup Wizard for N.
35 *
36 * <p>Example usage:
37 * <pre>{@code
38 * &lt;com.android.setupwizardlib.GlifLayout
39 *     xmlns:android="http://schemas.android.com/apk/res/android"
40 *     xmlns:app="http://schemas.android.com/apk/res-auto"
41 *     android:layout_width="match_parent"
42 *     android:layout_height="match_parent"
43 *     android:icon="@drawable/my_icon"
44 *     app:suwHeaderText="@string/my_title">
45 *
46 *     &lt;!-- Content here -->
47 *
48 * &lt;/com.android.setupwizardlib.GlifLayout>
49 * }</pre>
50 */
51public class GlifLayout extends TemplateLayout {
52
53    private static final String TAG = "GlifLayout";
54
55    public GlifLayout(Context context) {
56        this(context, 0, 0);
57    }
58
59    public GlifLayout(Context context, int template) {
60        this(context, template, 0);
61    }
62
63    public GlifLayout(Context context, int template, int containerId) {
64        super(context, template, containerId);
65        init(null, R.attr.suwLayoutTheme);
66    }
67
68    public GlifLayout(Context context, AttributeSet attrs) {
69        super(context, attrs);
70        init(attrs, R.attr.suwLayoutTheme);
71    }
72
73    @TargetApi(VERSION_CODES.HONEYCOMB)
74    public GlifLayout(Context context, AttributeSet attrs, int defStyleAttr) {
75        super(context, attrs, defStyleAttr);
76        init(attrs, defStyleAttr);
77    }
78
79    // All the constructors delegate to this init method. The 3-argument constructor is not
80    // available in LinearLayout before v11, so call super with the exact same arguments.
81    private void init(AttributeSet attrs, int defStyleAttr) {
82        final TypedArray a = getContext().obtainStyledAttributes(attrs,
83                R.styleable.SuwGlifLayout, defStyleAttr, 0);
84
85        final Drawable icon = a.getDrawable(R.styleable.SuwGlifLayout_android_icon);
86        if (icon != null) {
87            setIcon(icon);
88        }
89
90        // Set the header color
91        final ColorStateList headerColor =
92                a.getColorStateList(R.styleable.SuwGlifLayout_suwHeaderColor);
93        if (headerColor != null) {
94            setHeaderColor(headerColor);
95        }
96
97
98        // Set the header text
99        final CharSequence headerText =
100                a.getText(R.styleable.SuwGlifLayout_suwHeaderText);
101        if (headerText != null) {
102            setHeaderText(headerText);
103        }
104
105        a.recycle();
106    }
107
108    @Override
109    protected View onInflateTemplate(LayoutInflater inflater, int template) {
110        if (template == 0) {
111            template = R.layout.suw_glif_template;
112        }
113        return super.onInflateTemplate(inflater, template);
114    }
115
116    @Override
117    protected ViewGroup findContainer(int containerId) {
118        if (containerId == 0) {
119            containerId = R.id.suw_layout_content;
120        }
121        return super.findContainer(containerId);
122    }
123
124    public ScrollView getScrollView() {
125        final View view = findViewById(R.id.suw_scroll_view);
126        return view instanceof ScrollView ? (ScrollView) view : null;
127    }
128
129    public void setHeaderText(int title) {
130        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
131        if (titleView != null) {
132            titleView.setText(title);
133        }
134    }
135
136    public void setHeaderText(CharSequence title) {
137        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
138        if (titleView != null) {
139            titleView.setText(title);
140        }
141    }
142
143    public CharSequence getHeaderText() {
144        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
145        return titleView != null ? titleView.getText() : null;
146    }
147
148    public void setHeaderColor(ColorStateList color) {
149        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
150        if (titleView != null) {
151            titleView.setTextColor(color);
152        }
153    }
154
155    public ColorStateList getHeaderColor() {
156        final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
157        return titleView != null ? titleView.getTextColors() : null;
158    }
159
160    public void setIcon(Drawable icon) {
161        final ImageView iconView = getIconView();
162        if (iconView != null) {
163            iconView.setImageDrawable(icon);
164        }
165    }
166
167    public Drawable getIcon() {
168        final ImageView iconView = getIconView();
169        return iconView != null ? iconView.getDrawable() : null;
170    }
171
172    private ImageView getIconView() {
173        return (ImageView) findViewById(R.id.suw_layout_icon);
174    }
175}
176