SuggestionsView.java revision 145693e12b77c193a65b7eaa038a272dd1f48f33
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.graphics.Rect;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.AdapterView;
28import android.widget.ListView;
29
30/**
31 * Holds a list of suggestions.
32 */
33public class SuggestionsView extends ListView {
34
35    private static final boolean DBG = false;
36    private static final String TAG = "QSB.SuggestionsView";
37
38    private SuggestionClickListener mSuggestionClickListener;
39
40    private SuggestionSelectionListener mSuggestionSelectionListener;
41
42    public SuggestionsView(Context context, AttributeSet attrs) {
43        super(context, attrs);
44    }
45
46    @Override
47    public void onFinishInflate() {
48        super.onFinishInflate();
49        setOnItemClickListener(new ItemClickListener());
50        setOnItemLongClickListener(new ItemLongClickListener());
51        setOnItemSelectedListener(new ItemSelectedListener());
52    }
53
54    public void setSuggestionClickListener(SuggestionClickListener listener) {
55        mSuggestionClickListener = listener;
56    }
57
58    public void setSuggestionSelectionListener(SuggestionSelectionListener listener) {
59        mSuggestionSelectionListener = listener;
60    }
61
62    /**
63     * Gets the position of the selected suggestion.
64     *
65     * @return A 0-based index, or {@code -1} if no suggestion is selected.
66     */
67    public int getSelectedPosition() {
68        return getSelectedItemPosition();
69    }
70
71    /**
72     * Gets the selected suggestion.
73     *
74     * @return {@code null} if no suggestion is selected.
75     */
76    public SuggestionPosition getSelectedSuggestion() {
77        return (SuggestionPosition) getSelectedItem();
78    }
79
80    @Override
81    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
82        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
83        if (DBG) {
84            Log.d(TAG, "Suggestions focus change, gainFocus: " + gainFocus
85                    + ", selected=" + getSelectedItemPosition());
86        }
87        // In non-touch mode, ListView does not clear the list selection when
88        // the ListView loses focus. And when it regains focus, onItemSelected() never gets
89        // called if the new selected position is the same as the old. We work around that
90        // by firing extra selection events on focus changes in non-touch mode.
91        // This implementation can result in duplicate selection events when the old selected
92        // item is not the same as the new.
93        if (!isInTouchMode()) {
94            if (gainFocus) {
95                int position = getSelectedPosition();
96                if (position != INVALID_POSITION) {
97                    fireSuggestionSelected(position);
98                }
99            } else {
100                fireNothingSelected();
101            }
102        }
103    }
104
105    private void fireSuggestionSelected(int position) {
106        if (DBG) Log.d(TAG, "fireSuggestionSelected(" + position + ")");
107        if (mSuggestionSelectionListener != null) {
108            mSuggestionSelectionListener.onSuggestionSelected(position);
109        }
110    }
111
112    private void fireNothingSelected() {
113        if (DBG) Log.d(TAG, "fireNothingSelected()");
114        if (mSuggestionSelectionListener != null) {
115            mSuggestionSelectionListener.onNothingSelected();
116        }
117    }
118
119    private class ItemClickListener implements AdapterView.OnItemClickListener {
120        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
121            if (DBG) Log.d(TAG, "onItemClick(" + position + ")");
122            SuggestionView suggestionView = (SuggestionView) view;
123            if (mSuggestionClickListener != null) {
124                mSuggestionClickListener.onSuggestionClicked(position);
125            }
126        }
127    }
128
129    private class ItemLongClickListener implements AdapterView.OnItemLongClickListener {
130        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
131            if (DBG) Log.d(TAG, "onItemLongClick(" + position + ")");
132            SuggestionView suggestionView = (SuggestionView) view;
133            if (mSuggestionClickListener != null) {
134                return mSuggestionClickListener.onSuggestionLongClicked(position);
135            }
136            return false;
137        }
138    }
139
140    private class ItemSelectedListener implements AdapterView.OnItemSelectedListener {
141        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
142            // Only fire suggestion selection events when the list has focus.
143            // This suppresses selection events caused by data set changes (as opposed
144            // to user action).
145            if (hasFocus()) {
146                fireSuggestionSelected(position);
147            } else {
148                if (DBG) Log.d(TAG, "Suppressed selection event for position " + position);
149            }
150        }
151
152        public void onNothingSelected(AdapterView<?> parent) {
153            fireNothingSelected();
154        }
155    }
156
157    public void onIcon2Clicked(int position) {
158        if (mSuggestionClickListener != null) {
159            mSuggestionClickListener.onSuggestionQueryRefineClicked(position);
160        }
161    }
162
163}
164