17514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam/*
27514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Copyright (C) 2017 The Android Open Source Project
37514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
47514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
57514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * you may not use this file except in compliance with the License.
67514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * You may obtain a copy of the License at
77514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
87514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
97514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
107514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Unless required by applicable law or agreed to in writing, software
117514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
127514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * See the License for the specific language governing permissions and
147514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * limitations under the License.
157514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam */
167514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
177514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lampackage com.android.setupwizardlib.template;
187514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
197514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.content.res.TypedArray;
207514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.support.annotation.AttrRes;
217514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.support.annotation.NonNull;
227514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.support.annotation.Nullable;
237514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.util.AttributeSet;
247514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.widget.TextView;
257514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
267514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.R;
277514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.TemplateLayout;
287514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
297514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam/**
307514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * A {@link Mixin} for setting and getting the header text.
317514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam */
327514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lampublic class HeaderMixin implements Mixin {
337514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
347514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    private TemplateLayout mTemplateLayout;
357514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
367514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
377514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param layout The layout this Mixin belongs to.
387514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param attrs XML attributes given to the layout.
397514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param defStyleAttr The default style attribute as given to the constructor of the layout.
407514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
417514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public HeaderMixin(@NonNull TemplateLayout layout, @Nullable AttributeSet attrs,
427514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            @AttrRes int defStyleAttr) {
437514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        mTemplateLayout = layout;
447514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
457514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final TypedArray a = layout.getContext().obtainStyledAttributes(
467514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                attrs, R.styleable.SuwHeaderMixin, defStyleAttr, 0);
477514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
487514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        // Set the header text
497514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final CharSequence headerText = a.getText(R.styleable.SuwHeaderMixin_suwHeaderText);
507514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (headerText != null) {
517514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            setText(headerText);
527514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
537514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
547514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        a.recycle();
557514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
567514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
577514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
587514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The TextView displaying the header.
597514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
607514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public TextView getTextView() {
617514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return (TextView) mTemplateLayout.findManagedViewById(R.id.suw_layout_title);
627514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
637514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
647514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
657514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}.
667514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
677514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param title The resource ID of the text to be set as header.
687514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
697514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public void setText(int title) {
707514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final TextView titleView = getTextView();
717514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (titleView != null) {
727514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            titleView.setText(title);
737514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
747514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
757514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
767514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
777514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}.
787514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
797514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param title The text to be set as header.
807514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
817514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public void setText(CharSequence title) {
827514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final TextView titleView = getTextView();
837514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (titleView != null) {
847514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            titleView.setText(title);
857514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
867514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
877514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
887514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
897514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The current header text.
907514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
917514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public CharSequence getText() {
927514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final TextView titleView = getTextView();
937514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return titleView != null ? titleView.getText() : null;
947514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
957514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam}
96