SuggestionView.java revision 94e8a2be78530170f50e7895a558bf8011bbf8e8
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.drawable.Drawable;
25import android.text.TextUtils;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.RelativeLayout;
31import android.widget.TextView;
32
33/**
34 * View for the items in the suggestions list. This includes promoted suggestions,
35 * sources, and suggestions under each source.
36 *
37 */
38public class SuggestionView extends RelativeLayout implements View.OnClickListener {
39
40    private static final boolean DBG = false;
41    private static final String TAG = "QSB.SuggestionView";
42
43    /**
44     * The cursor that contains the current suggestion.
45     */
46    private SuggestionCursor mCursor;
47    /**
48     * The position within the cursor of the current suggestion.
49     */
50    private int mPos;
51
52    private SuggestionClickListener mSuggestionClickListener;
53
54    private TextView mText1;
55    private TextView mText2;
56    private ImageView mIcon1;
57    private ImageView mIcon2;
58
59    public SuggestionView(Context context, AttributeSet attrs, int defStyle) {
60        super(context, attrs, defStyle);
61    }
62
63    public SuggestionView(Context context, AttributeSet attrs) {
64        super(context, attrs);
65    }
66
67    public SuggestionView(Context context) {
68        super(context);
69    }
70
71    @Override
72    protected void onFinishInflate() {
73        super.onFinishInflate();
74        mText1 = (TextView) findViewById(R.id.text1);
75        mText2 = (TextView) findViewById(R.id.text2);
76        mIcon1 = (ImageView) findViewById(R.id.icon1);
77        mIcon2 = (ImageView) findViewById(R.id.icon2);
78    }
79
80    // TODO: never gets called
81    public void setSuggestionClickListener(SuggestionClickListener listener) {
82        mSuggestionClickListener = listener;
83    }
84
85    public void onClick(View v) {
86        if (mSuggestionClickListener == null) {
87            return;
88        }
89        if (v == this) {
90            mSuggestionClickListener.onSuggestionClicked(getSuggestionPosition());
91        } else if (v == mIcon1) {
92            mSuggestionClickListener.onSuggestionIconClicked(getSuggestionPosition(),
93                    Util.getOnScreenRect(mIcon1));
94        }
95    }
96
97    /**
98     * Gets the suggestion that this view is showing.
99     */
100    public SuggestionPosition getSuggestionPosition() {
101        if (mCursor == null) {
102            throw new IllegalStateException("No cursor in SuggestionView");
103        }
104        return new SuggestionPosition(mCursor, mPos);
105    }
106
107    public void bindAsSuggestion(SuggestionCursor suggestion) {
108        mCursor = suggestion;
109        mPos = suggestion.getPosition();
110        CharSequence text1 = suggestion.getSuggestionFormattedText1();
111        CharSequence text2 = suggestion.getSuggestionFormattedText2();
112        Drawable icon1 = suggestion.getSuggestionDrawableIcon1();
113        Drawable icon2 = suggestion.getSuggestionDrawableIcon2();
114        if (DBG) {
115            Log.d(TAG, "bindAsSuggestion(), text1=" + text1 + ",text2=" + text2
116                    + ",icon1=" + icon1 + ",icon2=" + icon2);
117        }
118        setText1(text1);
119        setText2(text2);
120        setIcon1(icon1);
121        setIcon2(icon2);
122
123        if (mIcon1 != null) {
124            mIcon1.setOnClickListener(this);
125        }
126    }
127
128    /**
129     * Sets the first text line.
130     */
131    private void setText1(CharSequence text) {
132        mText1.setText(text);
133    }
134
135    /**
136     * Sets the second text line.
137     */
138    private void setText2(CharSequence text) {
139        mText2.setText(text);
140        if (TextUtils.isEmpty(text)) {
141            mText2.setVisibility(GONE);
142        } else {
143            mText2.setVisibility(VISIBLE);
144        }
145    }
146
147    /**
148     * Sets the left-hand-side icon.
149     */
150    private void setIcon1(Drawable icon) {
151        setViewDrawable(mIcon1, icon);
152    }
153
154    /**
155     * Sets the right-hand-side icon.
156     */
157    private void setIcon2(Drawable icon) {
158        setViewDrawable(mIcon2, icon);
159    }
160
161    /**
162     * Sets the drawable in an image view, makes sure the view is only visible if there
163     * is a drawable.
164     */
165    private static void setViewDrawable(ImageView v, Drawable drawable) {
166        // Set the icon even if the drawable is null, since we need to clear any
167        // previous icon.
168        v.setImageDrawable(drawable);
169
170        if (drawable == null) {
171            v.setVisibility(View.GONE);
172        } else {
173            v.setVisibility(View.VISIBLE);
174
175            // This is a hack to get any animated drawables (like a 'working' spinner)
176            // to animate. You have to setVisible true on an AnimationDrawable to get
177            // it to start animating, but it must first have been false or else the
178            // call to setVisible will be ineffective. We need to clear up the story
179            // about animated drawables in the future, see http://b/1878430.
180            drawable.setVisible(false, false);
181            drawable.setVisible(true, false);
182        }
183    }
184
185}
186