SuggestionView.java revision 782dd228e78e9294692d639597f96c26283968bb
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.R;
20import com.android.quicksearchbox.SuggestionCursor;
21import com.android.quicksearchbox.SuggestionPosition;
22
23import android.content.Context;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
26import android.text.TextUtils;
27import android.util.AttributeSet;
28import android.util.Log;
29import android.view.View;
30import android.widget.ImageView;
31import android.widget.RelativeLayout;
32import android.widget.TextView;
33
34/**
35 * View for the items in the suggestions list. This includes promoted suggestions,
36 * sources, and suggestions under each source.
37 *
38 */
39public class SuggestionView extends RelativeLayout
40        implements View.OnClickListener, View.OnFocusChangeListener {
41
42    private static final boolean DBG = true;
43    private static final String TAG = "QSB.SuggestionView";
44
45    /**
46     * The cursor that contains the current suggestion.
47     */
48    private SuggestionCursor mCursor;
49    /**
50     * The position within the cursor of the current suggestion.
51     */
52    private int mPos;
53
54    private SuggestionClickListener mSuggestionClickListener;
55
56    private TextView mText1;
57    private TextView mText2;
58    private ImageView mIcon1;
59    private ImageView mIcon2;
60
61    public SuggestionView(Context context, AttributeSet attrs, int defStyle) {
62        super(context, attrs, defStyle);
63    }
64
65    public SuggestionView(Context context, AttributeSet attrs) {
66        super(context, attrs);
67    }
68
69    public SuggestionView(Context context) {
70        super(context);
71    }
72
73    @Override
74    protected void onFinishInflate() {
75        super.onFinishInflate();
76        mText1 = (TextView) findViewById(R.id.text1);
77        mText2 = (TextView) findViewById(R.id.text2);
78        mIcon1 = (ImageView) findViewById(R.id.icon1);
79        mIcon2 = (ImageView) findViewById(R.id.icon2);
80    }
81
82    public void setSuggestionClickListener(SuggestionClickListener listener) {
83        mSuggestionClickListener = listener;
84    }
85
86    public void onClick(View v) {
87        if (mSuggestionClickListener == null) {
88            return;
89        }
90        if (v == this) {
91            mSuggestionClickListener.onItemClicked(getSuggestionPosition());
92        } else if (v == mIcon1) {
93            mSuggestionClickListener.onIconClicked(getSuggestionPosition(),
94                    getOnScreenRect(mIcon1));
95        }
96    }
97
98    public void onFocusChange(View v, boolean hasFocus) {
99        if (DBG) Log.d(TAG, "onFocusChange(" + hasFocus + ")");
100        if (mSuggestionClickListener == null) {
101            return;
102        }
103        if (hasFocus) {
104            mSuggestionClickListener.onItemSelected(getSuggestionPosition());
105        }
106    }
107
108    /**
109     * Gets the suggestion that this view is showing.
110     */
111    public SuggestionPosition getSuggestionPosition() {
112        if (mCursor == null) {
113            throw new IllegalStateException("No cursor in SuggestionView");
114        }
115        return new SuggestionPosition(mCursor, mPos);
116    }
117
118    public void bindAsSuggestion(SuggestionCursor suggestion) {
119        mCursor = suggestion;
120        mPos = suggestion.getPosition();
121        CharSequence text1 = suggestion.getSuggestionFormattedText1();
122        CharSequence text2 = suggestion.getSuggestionFormattedText2();
123        Drawable icon1 = suggestion.getSuggestionDrawableIcon1();
124        Drawable icon2 = suggestion.getSuggestionDrawableIcon2();
125        if (DBG) {
126            Log.d(TAG, "bindAsSuggestion(), text1=" + text1 + ",text2=" + text2
127                    + ",icon1=" + icon1 + ",icon2=" + icon2);
128        }
129        setText1(text1);
130        setText2(text2);
131        setIcon1(icon1);
132        setIcon2(icon2);
133
134        setOnClickListener(this);
135        if (mIcon1 != null) {
136            mIcon1.setOnClickListener(this);
137        }
138        setOnFocusChangeListener(this);
139    }
140
141    /**
142     * Sets the first text line.
143     */
144    private void setText1(CharSequence text) {
145        mText1.setText(text);
146    }
147
148    /**
149     * Sets the second text line.
150     */
151    private void setText2(CharSequence text) {
152        mText2.setText(text);
153        if (TextUtils.isEmpty(text)) {
154            mText2.setVisibility(GONE);
155        } else {
156            mText2.setVisibility(VISIBLE);
157        }
158    }
159
160    /**
161     * Sets the left-hand-side icon.
162     */
163    private void setIcon1(Drawable icon) {
164        mIcon1.setImageDrawable(icon);
165    }
166
167    /**
168     * Sets the right-hand-side icon.
169     */
170    private void setIcon2(Drawable icon) {
171        mIcon2.setImageDrawable(icon);
172        if (icon == null) {
173            mIcon2.setVisibility(GONE);
174        } else {
175            mIcon2.setVisibility(VISIBLE);
176        }
177    }
178
179    private Rect getOnScreenRect(View view) {
180        int[] location = new int[2];
181        view.getLocationOnScreen(location);
182        Rect rect = new Rect();
183        rect.left = location[0];
184        rect.top = location[1];
185        rect.right = rect.left + view.getWidth();
186        rect.bottom = rect.top + view.getHeight();
187        return rect;
188    }
189
190}
191