BaseSuggestionView.java revision 77909685887bd6db7454b73cf274afc3aca2f58d
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
17package com.android.quicksearchbox.ui;
18
19import com.android.quicksearchbox.R;
20import com.android.quicksearchbox.Suggestion;
21
22import android.content.Context;
23import android.text.TextUtils;
24import android.util.AttributeSet;
25import android.view.ContextMenu;
26import android.view.MenuInflater;
27import android.view.MenuItem;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.RelativeLayout;
31import android.widget.TextView;
32
33/**
34 * Base class for suggestion views.
35 */
36public abstract class BaseSuggestionView extends RelativeLayout implements SuggestionView {
37
38    protected TextView mText1;
39    protected TextView mText2;
40    protected ImageView mIcon1;
41    protected ImageView mIcon2;
42    private boolean mIsFromHistory;
43    private long mSuggestionId;
44    private SuggestionsAdapter<?> mAdapter;
45
46    public BaseSuggestionView(Context context, AttributeSet attrs, int defStyle) {
47        super(context, attrs, defStyle);
48    }
49
50    public BaseSuggestionView(Context context, AttributeSet attrs) {
51        super(context, attrs);
52    }
53
54    public BaseSuggestionView(Context context) {
55        super(context);
56    }
57
58    @Override
59    protected void onFinishInflate() {
60        super.onFinishInflate();
61        mText1 = (TextView) findViewById(R.id.text1);
62        mText2 = (TextView) findViewById(R.id.text2);
63        mIcon1 = (ImageView) findViewById(R.id.icon1);
64        mIcon2 = (ImageView) findViewById(R.id.icon2);
65    }
66
67    public void bindAsSuggestion(Suggestion suggestion, String userQuery) {
68        setOnClickListener(new ClickListener());
69        updateIsFromHistory(suggestion);
70        setLongClickable(needsContextMenu());
71    }
72
73    public void bindAdapter(SuggestionsAdapter<?> adapter, long suggestionId) {
74        mAdapter = adapter;
75        mSuggestionId = suggestionId;
76    }
77
78    protected boolean needsContextMenu() {
79        return isFromHistory();
80    }
81
82    protected boolean isFromHistory() {
83        return mIsFromHistory;
84    }
85
86    protected void updateIsFromHistory(Suggestion suggestion) {
87        mIsFromHistory = suggestion.isSuggestionShortcut() || suggestion.isHistorySuggestion();
88    }
89
90    /**
91     * Sets the first text line.
92     */
93    protected void setText1(CharSequence text) {
94        mText1.setText(text);
95    }
96
97    /**
98     * Sets the second text line.
99     */
100    protected void setText2(CharSequence text) {
101        mText2.setText(text);
102        if (TextUtils.isEmpty(text)) {
103            mText2.setVisibility(GONE);
104        } else {
105            mText2.setVisibility(VISIBLE);
106        }
107    }
108
109    @Override
110    protected void onCreateContextMenu(ContextMenu menu) {
111        super.onCreateContextMenu(menu);
112        if (isFromHistory()) {
113            MenuInflater inflater = new MenuInflater(getContext());
114            inflater.inflate(R.menu.remove_from_history, menu);
115            MenuItem removeFromHistory = menu.findItem(R.id.remove_from_history);
116            removeFromHistory.setOnMenuItemClickListener(new RemoveFromHistoryListener());
117        }
118    }
119
120    protected void onSuggestionClicked() {
121        if (mAdapter != null) {
122            mAdapter.onSuggestionClicked(mSuggestionId);
123        }
124    }
125
126    protected void onSuggestionQuickContactClicked() {
127        if (mAdapter != null) {
128            mAdapter.onSuggestionQuickContactClicked(mSuggestionId);
129        }
130    }
131
132    protected void onRemoveFromHistoryClicked() {
133        if (mAdapter != null) {
134            mAdapter.onSuggestionRemoveFromHistoryClicked(mSuggestionId);
135        }
136    }
137
138    protected void onSuggestionQueryRefineClicked() {
139        if (mAdapter != null) {
140            mAdapter.onSuggestionQueryRefineClicked(mSuggestionId);
141        }
142    }
143
144    private class ClickListener implements OnClickListener {
145        public void onClick(View v) {
146            onSuggestionClicked();
147        }
148    }
149
150    private class RemoveFromHistoryListener implements MenuItem.OnMenuItemClickListener {
151        public boolean onMenuItemClick(MenuItem item) {
152            onRemoveFromHistoryClicked();
153            return false;
154        }
155    }
156
157}
158