1/* 2 * Copyright (C) 2017 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.template; 18 19import android.content.res.TypedArray; 20import android.support.annotation.AttrRes; 21import android.support.annotation.NonNull; 22import android.support.annotation.Nullable; 23import android.util.AttributeSet; 24import android.widget.TextView; 25 26import com.android.setupwizardlib.R; 27import com.android.setupwizardlib.TemplateLayout; 28 29/** 30 * A {@link Mixin} for setting and getting the header text. 31 */ 32public class HeaderMixin implements Mixin { 33 34 private TemplateLayout mTemplateLayout; 35 36 /** 37 * @param layout The layout this Mixin belongs to. 38 * @param attrs XML attributes given to the layout. 39 * @param defStyleAttr The default style attribute as given to the constructor of the layout. 40 */ 41 public HeaderMixin(@NonNull TemplateLayout layout, @Nullable AttributeSet attrs, 42 @AttrRes int defStyleAttr) { 43 mTemplateLayout = layout; 44 45 final TypedArray a = layout.getContext().obtainStyledAttributes( 46 attrs, R.styleable.SuwHeaderMixin, defStyleAttr, 0); 47 48 // Set the header text 49 final CharSequence headerText = a.getText(R.styleable.SuwHeaderMixin_suwHeaderText); 50 if (headerText != null) { 51 setText(headerText); 52 } 53 54 a.recycle(); 55 } 56 57 /** 58 * @return The TextView displaying the header. 59 */ 60 public TextView getTextView() { 61 return (TextView) mTemplateLayout.findManagedViewById(R.id.suw_layout_title); 62 } 63 64 /** 65 * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}. 66 * 67 * @param title The resource ID of the text to be set as header. 68 */ 69 public void setText(int title) { 70 final TextView titleView = getTextView(); 71 if (titleView != null) { 72 titleView.setText(title); 73 } 74 } 75 76 /** 77 * Sets the header text. This can also be set via the XML attribute {@code app:suwHeaderText}. 78 * 79 * @param title The text to be set as header. 80 */ 81 public void setText(CharSequence title) { 82 final TextView titleView = getTextView(); 83 if (titleView != null) { 84 titleView.setText(title); 85 } 86 } 87 88 /** 89 * @return The current header text. 90 */ 91 public CharSequence getText() { 92 final TextView titleView = getTextView(); 93 return titleView != null ? titleView.getText() : null; 94 } 95} 96