Home | History | Annotate | Download | only in ui
      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 
     17 package com.android.quicksearchbox.ui;
     18 
     19 import com.android.quicksearchbox.R;
     20 
     21 import android.content.Context;
     22 import android.graphics.drawable.Drawable;
     23 import android.util.AttributeSet;
     24 import android.view.ViewDebug;
     25 import android.widget.Checkable;
     26 import android.widget.ImageView;
     27 import android.widget.RelativeLayout;
     28 import android.widget.TextView;
     29 
     30 
     31 /**
     32  * A corpus in the corpus selection list.
     33  */
     34 public 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     @Override
     68     @ViewDebug.ExportedProperty
     69     public boolean isChecked() {
     70         return mChecked;
     71     }
     72 
     73     @Override
     74     public void setChecked(boolean checked) {
     75         if (mChecked != checked) {
     76             mChecked = checked;
     77             refreshDrawableState();
     78         }
     79     }
     80 
     81     @Override
     82     public void toggle() {
     83         setChecked(!mChecked);
     84     }
     85 
     86     @Override
     87     protected int[] onCreateDrawableState(int extraSpace) {
     88         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
     89         if (isChecked()) {
     90             mergeDrawableStates(drawableState, CHECKED_STATE_SET);
     91         }
     92         return drawableState;
     93     }
     94 
     95 }
     96