The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import com.android.internal.R; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.view.Gravity; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * <p> |
| 33 | * A button with two states, checked and unchecked. When the button is pressed |
| 34 | * or clicked, the state changes automatically. |
| 35 | * </p> |
| 36 | * |
| 37 | * <p><strong>XML attributes</strong></p> |
| 38 | * <p> |
| 39 | * See {@link android.R.styleable#CompoundButton |
| 40 | * CompoundButton Attributes}, {@link android.R.styleable#Button Button |
| 41 | * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link |
| 42 | * android.R.styleable#View View Attributes} |
| 43 | * </p> |
| 44 | */ |
| 45 | public abstract class CompoundButton extends Button implements Checkable { |
| 46 | private boolean mChecked; |
| 47 | private int mButtonResource; |
| 48 | private boolean mBroadcasting; |
| 49 | private Drawable mButtonDrawable; |
| 50 | private OnCheckedChangeListener mOnCheckedChangeListener; |
| 51 | private OnCheckedChangeListener mOnCheckedChangeWidgetListener; |
| 52 | |
| 53 | private static final int[] CHECKED_STATE_SET = { |
| 54 | R.attr.state_checked |
| 55 | }; |
| 56 | |
| 57 | public CompoundButton(Context context) { |
| 58 | this(context, null); |
| 59 | } |
| 60 | |
| 61 | public CompoundButton(Context context, AttributeSet attrs) { |
| 62 | this(context, attrs, 0); |
| 63 | } |
| 64 | |
| 65 | public CompoundButton(Context context, AttributeSet attrs, int defStyle) { |
| 66 | super(context, attrs, defStyle); |
| 67 | |
| 68 | TypedArray a = |
| 69 | context.obtainStyledAttributes( |
| 70 | attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0); |
| 71 | |
| 72 | Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button); |
| 73 | if (d != null) { |
| 74 | setButtonDrawable(d); |
| 75 | } |
| 76 | |
| 77 | boolean checked = a |
| 78 | .getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false); |
| 79 | setChecked(checked); |
| 80 | |
| 81 | a.recycle(); |
| 82 | } |
| 83 | |
| 84 | public void toggle() { |
| 85 | setChecked(!mChecked); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean performClick() { |
| 90 | /* |
| 91 | * XXX: These are tiny, need some surrounding 'expanded touch area', |
| 92 | * which will need to be implemented in Button if we only override |
| 93 | * performClick() |
| 94 | */ |
| 95 | |
| 96 | /* When clicked, toggle the state */ |
| 97 | toggle(); |
| 98 | return super.performClick(); |
| 99 | } |
| 100 | |
| 101 | public boolean isChecked() { |
| 102 | return mChecked; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * <p>Changes the checked state of this button.</p> |
| 107 | * |
| 108 | * @param checked true to check the button, false to uncheck it |
| 109 | */ |
| 110 | public void setChecked(boolean checked) { |
| 111 | if (mChecked != checked) { |
| 112 | mChecked = checked; |
| 113 | refreshDrawableState(); |
| 114 | |
| 115 | // Avoid infinite recursions if setChecked() is called from a listener |
| 116 | if (mBroadcasting) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | mBroadcasting = true; |
| 121 | if (mOnCheckedChangeListener != null) { |
| 122 | mOnCheckedChangeListener.onCheckedChanged(this, mChecked); |
| 123 | } |
| 124 | if (mOnCheckedChangeWidgetListener != null) { |
| 125 | mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked); |
| 126 | } |
| 127 | mBroadcasting = false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Register a callback to be invoked when the checked state of this button |
| 133 | * changes. |
| 134 | * |
| 135 | * @param listener the callback to call on checked state change |
| 136 | */ |
| 137 | public void setOnCheckedChangeListener(OnCheckedChangeListener listener) { |
| 138 | mOnCheckedChangeListener = listener; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Register a callback to be invoked when the checked state of this button |
| 143 | * changes. This callback is used for internal purpose only. |
| 144 | * |
| 145 | * @param listener the callback to call on checked state change |
| 146 | * @hide |
| 147 | */ |
| 148 | void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) { |
| 149 | mOnCheckedChangeWidgetListener = listener; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Interface definition for a callback to be invoked when the checked state |
| 154 | * of a compound button changed. |
| 155 | */ |
| 156 | public static interface OnCheckedChangeListener { |
| 157 | /** |
| 158 | * Called when the checked state of a compound button has changed. |
| 159 | * |
| 160 | * @param buttonView The compound button view whose state has changed. |
| 161 | * @param isChecked The new checked state of buttonView. |
| 162 | */ |
| 163 | void onCheckedChanged(CompoundButton buttonView, boolean isChecked); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Set the background to a given Drawable, identified by its resource id. |
| 168 | * |
| 169 | * @param resid the resource id of the drawable to use as the background |
| 170 | */ |
| 171 | public void setButtonDrawable(int resid) { |
| 172 | if (resid != 0 && resid == mButtonResource) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | mButtonResource = resid; |
| 177 | |
| 178 | Drawable d = null; |
| 179 | if (mButtonResource != 0) { |
| 180 | d = getResources().getDrawable(mButtonResource); |
| 181 | } |
| 182 | setButtonDrawable(d); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Set the background to a given Drawable |
| 187 | * |
| 188 | * @param d The Drawable to use as the background |
| 189 | */ |
| 190 | public void setButtonDrawable(Drawable d) { |
| 191 | if (d != null) { |
| 192 | if (mButtonDrawable != null) { |
| 193 | mButtonDrawable.setCallback(null); |
| 194 | unscheduleDrawable(mButtonDrawable); |
| 195 | } |
| 196 | d.setCallback(this); |
| 197 | d.setState(getDrawableState()); |
| 198 | d.setVisible(getVisibility() == VISIBLE, false); |
| 199 | mButtonDrawable = d; |
| 200 | mButtonDrawable.setState(null); |
| 201 | setMinHeight(mButtonDrawable.getIntrinsicHeight()); |
| 202 | } |
| 203 | |
| 204 | refreshDrawableState(); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected void onDraw(Canvas canvas) { |
| 209 | super.onDraw(canvas); |
| 210 | |
| 211 | final Drawable buttonDrawable = mButtonDrawable; |
| 212 | if (buttonDrawable != null) { |
| 213 | final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; |
| 214 | final int height = buttonDrawable.getIntrinsicHeight(); |
| 215 | |
| 216 | int y = 0; |
| 217 | |
| 218 | switch (verticalGravity) { |
| 219 | case Gravity.BOTTOM: |
| 220 | y = getHeight() - height; |
| 221 | break; |
| 222 | case Gravity.CENTER_VERTICAL: |
| 223 | y = (getHeight() - height) / 2; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height); |
| 228 | buttonDrawable.draw(canvas); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | protected int[] onCreateDrawableState(int extraSpace) { |
| 234 | final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); |
| 235 | if (isChecked()) { |
| 236 | mergeDrawableStates(drawableState, CHECKED_STATE_SET); |
| 237 | } |
| 238 | return drawableState; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | protected void drawableStateChanged() { |
| 243 | super.drawableStateChanged(); |
| 244 | |
| 245 | if (mButtonDrawable != null) { |
| 246 | int[] myDrawableState = getDrawableState(); |
| 247 | |
| 248 | // Set the state of the Drawable |
| 249 | mButtonDrawable.setState(myDrawableState); |
| 250 | |
| 251 | invalidate(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | protected boolean verifyDrawable(Drawable who) { |
| 257 | return super.verifyDrawable(who) || who == mButtonDrawable; |
| 258 | } |
| 259 | |
| 260 | static class SavedState extends BaseSavedState { |
| 261 | boolean checked; |
| 262 | |
| 263 | /** |
| 264 | * Constructor called from {@link CompoundButton#onSaveInstanceState()} |
| 265 | */ |
| 266 | SavedState(Parcelable superState) { |
| 267 | super(superState); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Constructor called from {@link #CREATOR} |
| 272 | */ |
| 273 | private SavedState(Parcel in) { |
| 274 | super(in); |
| 275 | checked = (Boolean)in.readValue(null); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void writeToParcel(Parcel out, int flags) { |
| 280 | super.writeToParcel(out, flags); |
| 281 | out.writeValue(checked); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public String toString() { |
| 286 | return "CompoundButton.SavedState{" |
| 287 | + Integer.toHexString(System.identityHashCode(this)) |
| 288 | + " checked=" + checked + "}"; |
| 289 | } |
| 290 | |
| 291 | public static final Parcelable.Creator<SavedState> CREATOR |
| 292 | = new Parcelable.Creator<SavedState>() { |
| 293 | public SavedState createFromParcel(Parcel in) { |
| 294 | return new SavedState(in); |
| 295 | } |
| 296 | |
| 297 | public SavedState[] newArray(int size) { |
| 298 | return new SavedState[size]; |
| 299 | } |
| 300 | }; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public Parcelable onSaveInstanceState() { |
| 305 | // Force our ancestor class to save its state |
| 306 | setFreezesText(true); |
| 307 | Parcelable superState = super.onSaveInstanceState(); |
| 308 | |
| 309 | SavedState ss = new SavedState(superState); |
| 310 | |
| 311 | ss.checked = isChecked(); |
| 312 | return ss; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void onRestoreInstanceState(Parcelable state) { |
| 317 | SavedState ss = (SavedState) state; |
| 318 | |
| 319 | super.onRestoreInstanceState(ss.getSuperState()); |
| 320 | setChecked(ss.checked); |
| 321 | requestLayout(); |
| 322 | } |
| 323 | } |