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 
BaseSuggestionView(Context context, AttributeSet attrs, int defStyle)42     public BaseSuggestionView(Context context, AttributeSet attrs, int defStyle) {
43         super(context, attrs, defStyle);
44     }
45 
BaseSuggestionView(Context context, AttributeSet attrs)46     public BaseSuggestionView(Context context, AttributeSet attrs) {
47         super(context, attrs);
48     }
49 
BaseSuggestionView(Context context)50     public BaseSuggestionView(Context context) {
51         super(context);
52     }
53 
54     @Override
onFinishInflate()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     @Override
bindAsSuggestion(Suggestion suggestion, String userQuery)64     public void bindAsSuggestion(Suggestion suggestion, String userQuery) {
65         setOnClickListener(new ClickListener());
66     }
67 
68     @Override
bindAdapter(SuggestionsAdapter<?> adapter, long suggestionId)69     public void bindAdapter(SuggestionsAdapter<?> adapter, long suggestionId) {
70         mAdapter = adapter;
71         mSuggestionId = suggestionId;
72     }
73 
isFromHistory(Suggestion suggestion)74     protected boolean isFromHistory(Suggestion suggestion) {
75         return suggestion.isSuggestionShortcut() || suggestion.isHistorySuggestion();
76     }
77 
78     /**
79      * Sets the first text line.
80      */
setText1(CharSequence text)81     protected void setText1(CharSequence text) {
82         mText1.setText(text);
83     }
84 
85     /**
86      * Sets the second text line.
87      */
setText2(CharSequence text)88     protected void setText2(CharSequence text) {
89         mText2.setText(text);
90         if (TextUtils.isEmpty(text)) {
91             mText2.setVisibility(GONE);
92         } else {
93             mText2.setVisibility(VISIBLE);
94         }
95     }
96 
onSuggestionClicked()97     protected void onSuggestionClicked() {
98         if (mAdapter != null) {
99             mAdapter.onSuggestionClicked(mSuggestionId);
100         }
101     }
102 
onSuggestionQueryRefineClicked()103     protected void onSuggestionQueryRefineClicked() {
104         if (mAdapter != null) {
105             mAdapter.onSuggestionQueryRefineClicked(mSuggestionId);
106         }
107     }
108 
109     private class ClickListener implements OnClickListener {
110         @Override
onClick(View v)111         public void onClick(View v) {
112             onSuggestionClicked();
113         }
114     }
115 
116 }
117