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
17package android.widget;
18
19import com.android.internal.R;
20
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.view.Gravity;
27import android.view.ViewDebug;
28import android.view.accessibility.AccessibilityEvent;
29
30
31/**
32 * An extension to TextView that supports the {@link android.widget.Checkable} interface.
33 * This is useful when used in a {@link android.widget.ListView ListView} where the it's
34 * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
35 * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
36 *
37 */
38public class CheckedTextView extends TextView implements Checkable {
39    private boolean mChecked;
40    private int mCheckMarkResource;
41    private Drawable mCheckMarkDrawable;
42    private int mBasePaddingRight;
43    private int mCheckMarkWidth;
44
45    private static final int[] CHECKED_STATE_SET = {
46        R.attr.state_checked
47    };
48
49    public CheckedTextView(Context context) {
50        this(context, null);
51    }
52
53    public CheckedTextView(Context context, AttributeSet attrs) {
54        this(context, attrs, 0);
55    }
56
57    public CheckedTextView(Context context, AttributeSet attrs, int defStyle) {
58        super(context, attrs, defStyle);
59
60        TypedArray a = context.obtainStyledAttributes(attrs,
61                R.styleable.CheckedTextView, defStyle, 0);
62
63        Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
64        if (d != null) {
65            setCheckMarkDrawable(d);
66        }
67
68        boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
69        setChecked(checked);
70
71        a.recycle();
72    }
73
74    public void toggle() {
75        setChecked(!mChecked);
76    }
77
78    @ViewDebug.ExportedProperty
79    public boolean isChecked() {
80        return mChecked;
81    }
82
83    /**
84     * <p>Changes the checked state of this text view.</p>
85     *
86     * @param checked true to check the text, false to uncheck it
87     */
88    public void setChecked(boolean checked) {
89        if (mChecked != checked) {
90            mChecked = checked;
91            refreshDrawableState();
92        }
93    }
94
95
96    /**
97     * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
98     * when {@link #isChecked()} is true.
99     *
100     * @param resid The Drawable to use for the checkmark.
101     */
102    public void setCheckMarkDrawable(int resid) {
103        if (resid != 0 && resid == mCheckMarkResource) {
104            return;
105        }
106
107        mCheckMarkResource = resid;
108
109        Drawable d = null;
110        if (mCheckMarkResource != 0) {
111            d = getResources().getDrawable(mCheckMarkResource);
112        }
113        setCheckMarkDrawable(d);
114    }
115
116    /**
117     * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
118     *
119     * @param d The Drawable to use for the checkmark.
120     */
121    public void setCheckMarkDrawable(Drawable d) {
122        if (mCheckMarkDrawable != null) {
123            mCheckMarkDrawable.setCallback(null);
124            unscheduleDrawable(mCheckMarkDrawable);
125        }
126        if (d != null) {
127            d.setCallback(this);
128            d.setVisible(getVisibility() == VISIBLE, false);
129            d.setState(CHECKED_STATE_SET);
130            setMinHeight(d.getIntrinsicHeight());
131
132            mCheckMarkWidth = d.getIntrinsicWidth();
133            mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
134            d.setState(getDrawableState());
135        } else {
136            mPaddingRight = mBasePaddingRight;
137        }
138        mCheckMarkDrawable = d;
139        requestLayout();
140    }
141
142    @Override
143    public void setPadding(int left, int top, int right, int bottom) {
144        super.setPadding(left, top, right, bottom);
145        mBasePaddingRight = mPaddingRight;
146    }
147
148    @Override
149    protected void onDraw(Canvas canvas) {
150        super.onDraw(canvas);
151
152        final Drawable checkMarkDrawable = mCheckMarkDrawable;
153        if (checkMarkDrawable != null) {
154            final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
155            final int height = checkMarkDrawable.getIntrinsicHeight();
156
157            int y = 0;
158
159            switch (verticalGravity) {
160                case Gravity.BOTTOM:
161                    y = getHeight() - height;
162                    break;
163                case Gravity.CENTER_VERTICAL:
164                    y = (getHeight() - height) / 2;
165                    break;
166            }
167
168            int right = getWidth();
169            checkMarkDrawable.setBounds(
170                    right - mCheckMarkWidth - mBasePaddingRight,
171                    y,
172                    right - mBasePaddingRight,
173                    y + height);
174            checkMarkDrawable.draw(canvas);
175        }
176    }
177
178    @Override
179    protected int[] onCreateDrawableState(int extraSpace) {
180        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
181        if (isChecked()) {
182            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
183        }
184        return drawableState;
185    }
186
187    @Override
188    protected void drawableStateChanged() {
189        super.drawableStateChanged();
190
191        if (mCheckMarkDrawable != null) {
192            int[] myDrawableState = getDrawableState();
193
194            // Set the state of the Drawable
195            mCheckMarkDrawable.setState(myDrawableState);
196
197            invalidate();
198        }
199    }
200
201    @Override
202    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
203        boolean populated = super.dispatchPopulateAccessibilityEvent(event);
204        if (!populated) {
205            event.setChecked(mChecked);
206        }
207        return populated;
208    }
209}
210