SuggestionsView.java revision 839a9fd2828f37c9dc8345f93aefa5b8ad2f857f
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.SuggestionPosition;
20
21import android.content.Context;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.View;
25import android.widget.AdapterView;
26import android.widget.ListView;
27
28/**
29 * Holds a list of suggestions.
30 */
31public class SuggestionsView extends ListView {
32
33    private static final boolean DBG = false;
34    private static final String TAG = "QSB.SuggestionsView";
35
36    private SuggestionClickListener mSuggestionClickListener;
37
38    public SuggestionsView(Context context, AttributeSet attrs) {
39        super(context, attrs);
40    }
41
42    @Override
43    public void onFinishInflate() {
44        super.onFinishInflate();
45        setOnItemClickListener(new ItemClickListener());
46        setOnItemLongClickListener(new ItemLongClickListener());
47    }
48
49    public void setSuggestionClickListener(SuggestionClickListener listener) {
50        mSuggestionClickListener = listener;
51    }
52
53    /**
54     * Gets the position of the selected suggestion.
55     *
56     * @return A 0-based index, or {@code -1} if no suggestion is selected.
57     */
58    public int getSelectedPosition() {
59        return getSelectedItemPosition();
60    }
61
62    /**
63     * Gets the selected suggestion.
64     *
65     * @return {@code null} if no suggestion is selected.
66     */
67    public SuggestionPosition getSelectedSuggestion() {
68        return (SuggestionPosition) getSelectedItem();
69    }
70
71    private class ItemClickListener implements AdapterView.OnItemClickListener {
72        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
73            if (DBG) Log.d(TAG, "onItemClick(" + position + ")");
74            if (mSuggestionClickListener != null) {
75                mSuggestionClickListener.onSuggestionClicked(position);
76            }
77        }
78    }
79
80    private class ItemLongClickListener implements AdapterView.OnItemLongClickListener {
81        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
82            if (DBG) Log.d(TAG, "onItemLongClick(" + position + ")");
83            if (mSuggestionClickListener != null) {
84                return mSuggestionClickListener.onSuggestionLongClicked(position);
85            }
86            return false;
87        }
88    }
89
90    public void onIcon2Clicked(int position) {
91        if (mSuggestionClickListener != null) {
92            mSuggestionClickListener.onSuggestionQueryRefineClicked(position);
93        }
94    }
95
96}
97