HeaderMixin.java revision 7514f1cee29b3feb4822ce16945c1c312057d24f
19b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh/*
29b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * Copyright (C) 2017 The Android Open Source Project
39b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh *
49b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * Licensed under the Apache License, Version 2.0 (the "License");
59b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * you may not use this file except in compliance with the License.
69b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * You may obtain a copy of the License at
79b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh *
89b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh *      http://www.apache.org/licenses/LICENSE-2.0
99b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh *
109b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * Unless required by applicable law or agreed to in writing, software
119b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * distributed under the License is distributed on an "AS IS" BASIS,
129b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * See the License for the specific language governing permissions and
149b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh * limitations under the License.
159b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh */
169b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
179b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yehpackage com.android.setupwizardlib.template;
189b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
199b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yehimport android.content.res.TypedArray;
209b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yehimport android.support.annotation.AttrRes;
219b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yehimport android.support.annotation.NonNull;
229b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yehimport android.support.annotation.Nullable;
230efca17105d112a0ff568602831b22bdafa00433Brian Carlstromimport android.util.AttributeSet;
240efca17105d112a0ff568602831b22bdafa00433Brian Carlstromimport android.widget.TextView;
250efca17105d112a0ff568602831b22bdafa00433Brian Carlstrom
269d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstromimport com.android.setupwizardlib.R;
279d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstromimport com.android.setupwizardlib.TemplateLayout;
289d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom
299d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom/**
309d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom * A {@link Mixin} for setting and getting the header text.
319d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom */
329d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrompublic class HeaderMixin implements Mixin {
33d396a448b2e36e29598c954b64bfddef73f3fae0Elliott Hughes
349b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    private TemplateLayout mTemplateLayout;
350efca17105d112a0ff568602831b22bdafa00433Brian Carlstrom
360efca17105d112a0ff568602831b22bdafa00433Brian Carlstrom    /**
370efca17105d112a0ff568602831b22bdafa00433Brian Carlstrom     * @param layout The layout this Mixin belongs to.
380efca17105d112a0ff568602831b22bdafa00433Brian Carlstrom     * @param attrs XML attributes given to the layout.
395423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root     * @param defStyleAttr The default style attribute as given to the constructor of the layout.
409d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom     */
419d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom    public HeaderMixin(@NonNull TemplateLayout layout, @Nullable AttributeSet attrs,
429b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh            @AttrRes int defStyleAttr) {
439b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh        mTemplateLayout = layout;
449b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
459b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh        final TypedArray a = layout.getContext().obtainStyledAttributes(
469b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh                attrs, R.styleable.SuwHeaderMixin, defStyleAttr, 0);
479b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
4844039172627d1c15737ea73836ad375559d76211Chia-chi Yeh        // Set the header text
4944039172627d1c15737ea73836ad375559d76211Chia-chi Yeh        final CharSequence headerText = a.getText(R.styleable.SuwHeaderMixin_suwHeaderText);
5044039172627d1c15737ea73836ad375559d76211Chia-chi Yeh        if (headerText != null) {
513e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root            setText(headerText);
523e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root        }
534a9e1a2494f2e48b157506d7c731187907b7fd4eBrian Carlstrom
544a9e1a2494f2e48b157506d7c731187907b7fd4eBrian Carlstrom        a.recycle();
559b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    }
569b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
579b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    /**
589b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     * @return The TextView displaying the header.
599b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     */
609b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    public TextView getTextView() {
619b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh        return (TextView) mTemplateLayout.findManagedViewById(R.id.suw_layout_title);
629b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    }
639b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh
64baf2838fd2c7ddf517bd5bd9917551a4706af5b6Alex Klyubin    /**
65baf2838fd2c7ddf517bd5bd9917551a4706af5b6Alex Klyubin     * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}.
66baf2838fd2c7ddf517bd5bd9917551a4706af5b6Alex Klyubin     *
679b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     * @param title The resource ID of the text to be set as header.
689b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     */
699b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    public void setText(int title) {
709b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh        final TextView titleView = getTextView();
719b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh        if (titleView != null) {
729b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh            titleView.setText(title);
7369ddab4575ff684c533c995e07ca15fe18543fc0Jeff Sharkey        }
7469ddab4575ff684c533c995e07ca15fe18543fc0Jeff Sharkey    }
7569ddab4575ff684c533c995e07ca15fe18543fc0Jeff Sharkey
769b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    /**
77a00a2b33ccc6bc079c3ee57a938f62947b48a001Brian Carlstrom     * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}.
789b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     *
799b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh     * @param title The text to be set as header.
80a00a2b33ccc6bc079c3ee57a938f62947b48a001Brian Carlstrom     */
819b7a3f1a6437605022568cad0b92d5006a2ab391Chia-chi Yeh    public void setText(CharSequence title) {
8267c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom        final TextView titleView = getTextView();
8367c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom        if (titleView != null) {
8467c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom            titleView.setText(title);
8567c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom        }
8667c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom    }
8767c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom
8867c30dfe8e4bff11a4660ac23e8679b5deb59457Brian Carlstrom    /**
899d7faa91be6661eccf73494f1ab96ae9a28d42d7Brian Carlstrom     * @return The current header text.
903e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root     */
913e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root    public CharSequence getText() {
923e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root        final TextView titleView = getTextView();
933e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root        return titleView != null ? titleView.getText() : null;
943e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root    }
953e7be43e2555bbdfe311dcbd9a36f7f05321a2d8Kenny Root}
965423e68d5dbe048ec6f042cce52a33f94184e9fbKenny Root