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