SuggestionsView.java revision dbdffd8316e75bc2813dbbcbef13d357970e8c84
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.MotionEvent; 25import android.view.View; 26import android.widget.AdapterView; 27import android.widget.ListView; 28 29/** 30 * Holds a list of suggestions. 31 */ 32public class SuggestionsView extends ListView { 33 34 private static final boolean DBG = true; 35 private static final String TAG = "QSB.SuggestionsView"; 36 37 private SuggestionClickListener mSuggestionClickListener; 38 39 private SuggestionSelectionListener mSuggestionSelectionListener; 40 41 private InteractionListener mInteractionListener; 42 43 public SuggestionsView(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 } 46 47 @Override 48 public void onFinishInflate() { 49 super.onFinishInflate(); 50 setOnItemClickListener(new ItemClickListener()); 51 setOnItemLongClickListener(new ItemLongClickListener()); 52 setOnItemSelectedListener(new ItemSelectedListener()); 53 } 54 55 public void setSuggestionClickListener(SuggestionClickListener listener) { 56 mSuggestionClickListener = listener; 57 } 58 59 public void setSuggestionSelectionListener(SuggestionSelectionListener listener) { 60 mSuggestionSelectionListener = listener; 61 } 62 63 public void setInteractionListener(InteractionListener listener) { 64 mInteractionListener = listener; 65 } 66 67 /** 68 * Gets the position of the selected suggestion. 69 * 70 * @return A 0-based index, or {@code -1} if no suggestion is selected. 71 */ 72 public int getSelectedPosition() { 73 return getSelectedItemPosition(); 74 } 75 76 /** 77 * Gets the selected suggestion. 78 * 79 * @return {@code null} if no suggestion is selected. 80 */ 81 public SuggestionPosition getSelectedSuggestion() { 82 return (SuggestionPosition) getSelectedItem(); 83 } 84 85 @Override 86 public boolean onInterceptTouchEvent(MotionEvent event) { 87 if (event.getAction() == MotionEvent.ACTION_DOWN && mInteractionListener != null) { 88 mInteractionListener.onInteraction(); 89 } 90 return super.onInterceptTouchEvent(event); 91 } 92 93 public interface InteractionListener { 94 /** 95 * Called when the user interacts with this view. 96 */ 97 void onInteraction(); 98 } 99 100 private class ItemClickListener implements AdapterView.OnItemClickListener { 101 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 102 if (DBG) Log.d(TAG, "onItemClick(" + position + ")"); 103 SuggestionView suggestionView = (SuggestionView) view; 104 if (mSuggestionClickListener != null) { 105 mSuggestionClickListener.onSuggestionClicked(position); 106 } 107 } 108 } 109 110 private class ItemLongClickListener implements AdapterView.OnItemLongClickListener { 111 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 112 if (DBG) Log.d(TAG, "onItemLongClick(" + position + ")"); 113 SuggestionView suggestionView = (SuggestionView) view; 114 if (mSuggestionClickListener != null) { 115 return mSuggestionClickListener.onSuggestionLongClicked(position); 116 } 117 return false; 118 } 119 } 120 121 private class ItemSelectedListener implements AdapterView.OnItemSelectedListener { 122 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 123 if (DBG) Log.d(TAG, "onItemSelected(" + position + ")"); 124 SuggestionView suggestionView = (SuggestionView) view; 125 if (mSuggestionSelectionListener != null) { 126 mSuggestionSelectionListener.onSuggestionSelected(position); 127 } 128 } 129 130 public void onNothingSelected(AdapterView<?> parent) { 131 if (DBG) Log.d(TAG, "onNothingSelected()"); 132 if (mSuggestionSelectionListener != null) { 133 mSuggestionSelectionListener.onNothingSelected(); 134 } 135 } 136 } 137} 138