Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 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 import com.android.quicksearchbox.Suggestion;
     21 
     22 import android.content.Context;
     23 import android.text.TextUtils;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.widget.ImageView;
     27 import android.widget.RelativeLayout;
     28 import android.widget.TextView;
     29 
     30 /**
     31  * Base class for suggestion views.
     32  */
     33 public abstract class BaseSuggestionView extends RelativeLayout implements SuggestionView {
     34 
     35     protected TextView mText1;
     36     protected TextView mText2;
     37     protected ImageView mIcon1;
     38     protected ImageView mIcon2;
     39     private long mSuggestionId;
     40     private SuggestionsAdapter<?> mAdapter;
     41 
     42     public BaseSuggestionView(Context context, AttributeSet attrs, int defStyle) {
     43         super(context, attrs, defStyle);
     44     }
     45 
     46     public BaseSuggestionView(Context context, AttributeSet attrs) {
     47         super(context, attrs);
     48     }
     49 
     50     public BaseSuggestionView(Context context) {
     51         super(context);
     52     }
     53 
     54     @Override
     55     protected void onFinishInflate() {
     56         super.onFinishInflate();
     57         mText1 = (TextView) findViewById(R.id.text1);
     58         mText2 = (TextView) findViewById(R.id.text2);
     59         mIcon1 = (ImageView) findViewById(R.id.icon1);
     60         mIcon2 = (ImageView) findViewById(R.id.icon2);
     61     }
     62 
     63     public void bindAsSuggestion(Suggestion suggestion, String userQuery) {
     64         setOnClickListener(new ClickListener());
     65         if (isFromHistory(suggestion)) {
     66             setLongClickable(true);
     67             setOnLongClickListener(new LongClickListener());
     68         } else {
     69             setLongClickable(false);
     70             setOnLongClickListener(null);
     71         }
     72     }
     73 
     74     public void bindAdapter(SuggestionsAdapter<?> adapter, long suggestionId) {
     75         mAdapter = adapter;
     76         mSuggestionId = suggestionId;
     77     }
     78 
     79     protected boolean isFromHistory(Suggestion suggestion) {
     80         return suggestion.isSuggestionShortcut() || suggestion.isHistorySuggestion();
     81     }
     82 
     83     /**
     84      * Sets the first text line.
     85      */
     86     protected void setText1(CharSequence text) {
     87         mText1.setText(text);
     88     }
     89 
     90     /**
     91      * Sets the second text line.
     92      */
     93     protected void setText2(CharSequence text) {
     94         mText2.setText(text);
     95         if (TextUtils.isEmpty(text)) {
     96             mText2.setVisibility(GONE);
     97         } else {
     98             mText2.setVisibility(VISIBLE);
     99         }
    100     }
    101 
    102     protected void onSuggestionClicked() {
    103         if (mAdapter != null) {
    104             mAdapter.onSuggestionClicked(mSuggestionId);
    105         }
    106     }
    107 
    108     protected void onSuggestionQuickContactClicked() {
    109         if (mAdapter != null) {
    110             mAdapter.onSuggestionQuickContactClicked(mSuggestionId);
    111         }
    112     }
    113 
    114     protected void onRemoveFromHistoryClicked() {
    115         if (mAdapter != null) {
    116             mAdapter.onSuggestionRemoveFromHistoryClicked(mSuggestionId);
    117         }
    118     }
    119 
    120     protected void onSuggestionQueryRefineClicked() {
    121         if (mAdapter != null) {
    122             mAdapter.onSuggestionQueryRefineClicked(mSuggestionId);
    123         }
    124     }
    125 
    126     private class ClickListener implements OnClickListener {
    127         public void onClick(View v) {
    128             onSuggestionClicked();
    129         }
    130     }
    131 
    132     private class LongClickListener implements View.OnLongClickListener {
    133         public boolean onLongClick(View v) {
    134             onRemoveFromHistoryClicked();
    135             return true;
    136         }
    137     }
    138 
    139 }
    140