CorpusView.java revision 8b2936607176720172aee068abc5631bdf77e843
1/*
2 * Copyright (C) 2009 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.quicksearchbox.ui;
18
19import com.android.quicksearchbox.R;
20
21import android.content.Context;
22import android.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24import android.view.ViewDebug;
25import android.widget.Checkable;
26import android.widget.ImageView;
27import android.widget.RelativeLayout;
28import android.widget.TextView;
29
30
31/**
32 * A corpus in the corpus selection list.
33 */
34public class CorpusView extends RelativeLayout implements Checkable {
35
36    private ImageView mIcon;
37    private TextView mLabel;
38    private boolean mChecked;
39
40    private static final int[] CHECKED_STATE_SET = {
41        android.R.attr.state_checked
42    };
43
44    public CorpusView(Context context, AttributeSet attrs) {
45        super(context, attrs);
46    }
47
48    public CorpusView(Context context) {
49        super(context);
50    }
51
52    @Override
53    protected void onFinishInflate() {
54        super.onFinishInflate();
55        mIcon = (ImageView) findViewById(R.id.source_icon);
56        mLabel = (TextView) findViewById(R.id.source_label);
57    }
58
59    public void setLabel(CharSequence label) {
60        mLabel.setText(label);
61    }
62
63    public void setIcon(Drawable icon) {
64        mIcon.setImageDrawable(icon);
65    }
66
67    @ViewDebug.ExportedProperty
68    public boolean isChecked() {
69        return mChecked;
70    }
71
72    public void setChecked(boolean checked) {
73        if (mChecked != checked) {
74            mChecked = checked;
75            refreshDrawableState();
76        }
77    }
78
79    public void toggle() {
80        setChecked(!mChecked);
81    }
82
83    @Override
84    protected int[] onCreateDrawableState(int extraSpace) {
85        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
86        if (isChecked()) {
87            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
88        }
89        return drawableState;
90    }
91
92}
93