1/* 2 * Copyright (C) 2016 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.items; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.util.AttributeSet; 22import android.view.ContextThemeWrapper; 23import android.view.View; 24import android.view.ViewGroup; 25import android.widget.Button; 26 27import com.android.setupwizardlib.R; 28 29/** 30 * Description of a button inside {@link com.android.setupwizardlib.items.ButtonBarItem}. This item 31 * will not be bound by the adapter, and must be a child of {@code ButtonBarItem}. 32 */ 33public class ButtonItem extends AbstractItem implements View.OnClickListener { 34 35 public interface OnClickListener { 36 void onClick(ButtonItem item); 37 } 38 39 private boolean mEnabled = true; 40 private CharSequence mText; 41 private int mTheme = R.style.SuwButtonItem; 42 private OnClickListener mListener; 43 44 private Button mButton; 45 46 public ButtonItem() { 47 super(); 48 } 49 50 public ButtonItem(Context context, AttributeSet attrs) { 51 super(context, attrs); 52 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwButtonItem); 53 mEnabled = a.getBoolean(R.styleable.SuwButtonItem_android_enabled, true); 54 mText = a.getText(R.styleable.SuwButtonItem_android_text); 55 mTheme = a.getResourceId(R.styleable.SuwButtonItem_android_theme, R.style.SuwButtonItem); 56 a.recycle(); 57 } 58 59 public void setOnClickListener(OnClickListener listener) { 60 mListener = listener; 61 } 62 63 public void setText(CharSequence text) { 64 mText = text; 65 } 66 67 public CharSequence getText() { 68 return mText; 69 } 70 71 /** 72 * The theme to use for this button. This can be used to create button of a particular style 73 * (e.g. a colored or borderless button). Typically {@code android:buttonStyle} will be set in 74 * the theme to change the style applied by the button. 75 * 76 * @param theme Resource ID of the theme 77 */ 78 public void setTheme(int theme) { 79 mTheme = theme; 80 mButton = null; 81 } 82 83 /** 84 * @return Resource ID of the theme used by this button. 85 */ 86 public int getTheme() { 87 return mTheme; 88 } 89 90 public void setEnabled(boolean enabled) { 91 mEnabled = enabled; 92 } 93 94 @Override 95 public int getCount() { 96 return 0; 97 } 98 99 @Override 100 public boolean isEnabled() { 101 return mEnabled; 102 } 103 104 @Override 105 public int getLayoutResource() { 106 return 0; 107 } 108 109 /** 110 * Do not use this since ButtonItem is not directly part of a list. 111 */ 112 @Override 113 public final void onBindView(View view) { 114 throw new UnsupportedOperationException("Cannot bind to ButtonItem's view"); 115 } 116 117 protected Button createButton(ViewGroup parent) { 118 if (mButton == null) { 119 Context context = parent.getContext(); 120 if (mTheme != 0) { 121 context = new ContextThemeWrapper(context, mTheme); 122 } 123 mButton = new Button(context); 124 mButton.setOnClickListener(this); 125 } 126 mButton.setEnabled(mEnabled); 127 mButton.setText(mText); 128 return mButton; 129 } 130 131 @Override 132 public void onClick(View v) { 133 if (mListener != null) { 134 mListener.onClick(this); 135 } 136 } 137} 138