GlifLayout.java revision fca3ee628d9d5c1c1109f3df6e02cdd31c57c000
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.support.annotation.LayoutRes;
27import android.support.annotation.NonNull;
28import android.util.AttributeSet;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ProgressBar;
33import android.widget.ScrollView;
34import android.widget.TextView;
35
36import com.android.setupwizardlib.template.ButtonFooterMixin;
37import com.android.setupwizardlib.template.ColoredHeaderMixin;
38import com.android.setupwizardlib.template.HeaderMixin;
39import com.android.setupwizardlib.template.IconMixin;
40import com.android.setupwizardlib.template.ProgressBarMixin;
41import com.android.setupwizardlib.view.StatusBarBackgroundLayout;
42
43/**
44 * Layout for the GLIF theme used in Setup Wizard for N.
45 *
46 * <p>Example usage:
47 * <pre>{@code
48 * &lt;com.android.setupwizardlib.GlifLayout
49 *     xmlns:android="http://schemas.android.com/apk/res/android"
50 *     xmlns:app="http://schemas.android.com/apk/res-auto"
51 *     android:layout_width="match_parent"
52 *     android:layout_height="match_parent"
53 *     android:icon="@drawable/my_icon"
54 *     app:suwHeaderText="@string/my_title">
55 *
56 *     &lt;!-- Content here -->
57 *
58 * &lt;/com.android.setupwizardlib.GlifLayout>
59 * }</pre>
60 */
61public class GlifLayout extends TemplateLayout {
62
63    private static final String TAG = "GlifLayout";
64
65    private ColorStateList mPrimaryColor;
66
67    public GlifLayout(Context context) {
68        this(context, 0, 0);
69    }
70
71    public GlifLayout(Context context, int template) {
72        this(context, template, 0);
73    }
74
75    public GlifLayout(Context context, int template, int containerId) {
76        super(context, template, containerId);
77        init(null, R.attr.suwLayoutTheme);
78    }
79
80    public GlifLayout(Context context, AttributeSet attrs) {
81        super(context, attrs);
82        init(attrs, R.attr.suwLayoutTheme);
83    }
84
85    @TargetApi(VERSION_CODES.HONEYCOMB)
86    public GlifLayout(Context context, AttributeSet attrs, int defStyleAttr) {
87        super(context, attrs, defStyleAttr);
88        init(attrs, defStyleAttr);
89    }
90
91    // All the constructors delegate to this init method. The 3-argument constructor is not
92    // available in LinearLayout before v11, so call super with the exact same arguments.
93    private void init(AttributeSet attrs, int defStyleAttr) {
94        registerMixin(HeaderMixin.class, new ColoredHeaderMixin(this, attrs, defStyleAttr));
95        registerMixin(IconMixin.class, new IconMixin(this, attrs, defStyleAttr));
96        registerMixin(ProgressBarMixin.class, new ProgressBarMixin(this));
97        registerMixin(ButtonFooterMixin.class, new ButtonFooterMixin(this));
98
99        TypedArray a = getContext().obtainStyledAttributes(attrs,
100                R.styleable.SuwGlifLayout, defStyleAttr, 0);
101
102        ColorStateList primaryColor =
103                a.getColorStateList(R.styleable.SuwGlifLayout_suwColorPrimary);
104        if (primaryColor != null) {
105            setPrimaryColor(primaryColor);
106        }
107
108        a.recycle();
109    }
110
111    @Override
112    protected View onInflateTemplate(LayoutInflater inflater, @LayoutRes int template) {
113        if (template == 0) {
114            template = R.layout.suw_glif_template;
115        }
116        return inflateTemplate(inflater, R.style.SuwThemeGlif_Light, template);
117    }
118
119    @Override
120    protected ViewGroup findContainer(int containerId) {
121        if (containerId == 0) {
122            containerId = R.id.suw_layout_content;
123        }
124        return super.findContainer(containerId);
125    }
126
127    public ScrollView getScrollView() {
128        final View view = findManagedViewById(R.id.suw_scroll_view);
129        return view instanceof ScrollView ? (ScrollView) view : null;
130    }
131
132    public TextView getHeaderTextView() {
133        return getMixin(HeaderMixin.class).getTextView();
134    }
135
136    public void setHeaderText(int title) {
137        getMixin(HeaderMixin.class).setText(title);
138    }
139
140    public void setHeaderText(CharSequence title) {
141        getMixin(HeaderMixin.class).setText(title);
142    }
143
144    public CharSequence getHeaderText() {
145        return getMixin(HeaderMixin.class).getText();
146    }
147
148    public void setHeaderColor(ColorStateList color) {
149        final ColoredHeaderMixin mixin = (ColoredHeaderMixin) getMixin(HeaderMixin.class);
150        mixin.setColor(color);
151    }
152
153    public ColorStateList getHeaderColor() {
154        final ColoredHeaderMixin mixin = (ColoredHeaderMixin) getMixin(HeaderMixin.class);
155        return mixin.getColor();
156    }
157
158    public void setIcon(Drawable icon) {
159        getMixin(IconMixin.class).setIcon(icon);
160    }
161
162    public Drawable getIcon() {
163        return getMixin(IconMixin.class).getIcon();
164    }
165
166    /**
167     * Sets the primary color of this layout, which will be used to determine the color of the
168     * progress bar and the background pattern.
169     */
170    public void setPrimaryColor(@NonNull ColorStateList color) {
171        mPrimaryColor = color;
172        setGlifPatternColor(color);
173        getMixin(ProgressBarMixin.class).setColor(color);
174    }
175
176    public ColorStateList getPrimaryColor() {
177        return mPrimaryColor;
178    }
179
180    private void setGlifPatternColor(@NonNull ColorStateList color) {
181        final View patternBg = findManagedViewById(R.id.suw_pattern_bg);
182        if (patternBg != null) {
183            final GlifPatternDrawable background =
184                    new GlifPatternDrawable(color.getDefaultColor());
185            if (patternBg instanceof StatusBarBackgroundLayout) {
186                ((StatusBarBackgroundLayout) patternBg).setStatusBarBackground(background);
187            } else {
188                patternBg.setBackgroundDrawable(background);
189            }
190        }
191        if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
192            setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
193        }
194    }
195
196    public boolean isProgressBarShown() {
197        return getMixin(ProgressBarMixin.class).isShown();
198    }
199
200    public void setProgressBarShown(boolean shown) {
201        getMixin(ProgressBarMixin.class).setShown(shown);
202    }
203
204    public ProgressBar peekProgressBar() {
205        return getMixin(ProgressBarMixin.class).peekProgressBar();
206    }
207}
208