DefaultSuggestionView.java revision 1e938ea2f5edefab446b9562b316bc5dc72adebb
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 DefaultSuggestionView extends RelativeLayout implements SuggestionView {
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 TextView mText1;
53    private TextView mText2;
54    private ImageView mIcon1;
55    private ImageView mIcon2;
56
57    public DefaultSuggestionView(Context context, AttributeSet attrs, int defStyle) {
58        super(context, attrs, defStyle);
59    }
60
61    public DefaultSuggestionView(Context context, AttributeSet attrs) {
62        super(context, attrs);
63    }
64
65    public DefaultSuggestionView(Context context) {
66        super(context);
67    }
68
69    @Override
70    protected void onFinishInflate() {
71        super.onFinishInflate();
72        mText1 = (TextView) findViewById(R.id.text1);
73        mText2 = (TextView) findViewById(R.id.text2);
74        mIcon1 = (ImageView) findViewById(R.id.icon1);
75        mIcon2 = (ImageView) findViewById(R.id.icon2);
76    }
77
78    /**
79     * Gets the suggestion that this view is showing.
80     */
81    public SuggestionPosition getSuggestionPosition() {
82        if (mCursor == null) {
83            throw new IllegalStateException("No cursor in SuggestionView");
84        }
85        return new SuggestionPosition(mCursor, mPos);
86    }
87
88    public void bindAsSuggestion(SuggestionCursor suggestion) {
89        mCursor = suggestion;
90        mPos = suggestion.getPosition();
91        CharSequence text1 = suggestion.getSuggestionFormattedText1();
92        CharSequence text2 = suggestion.getSuggestionFormattedText2();
93        Drawable icon1 = suggestion.getSuggestionDrawableIcon1();
94        Drawable icon2 = suggestion.getSuggestionDrawableIcon2();
95        if (DBG) {
96            Log.d(TAG, "bindAsSuggestion(), text1=" + text1 + ",text2=" + text2
97                    + ",icon1=" + icon1 + ",icon2=" + icon2);
98        }
99        setText1(text1);
100        setText2(text2);
101        setIcon1(icon1);
102        setIcon2(icon2);
103    }
104
105    /**
106     * Sets the first text line.
107     */
108    private void setText1(CharSequence text) {
109        mText1.setText(text);
110    }
111
112    /**
113     * Sets the second text line.
114     */
115    private void setText2(CharSequence text) {
116        mText2.setText(text);
117        if (TextUtils.isEmpty(text)) {
118            mText2.setVisibility(GONE);
119        } else {
120            mText2.setVisibility(VISIBLE);
121        }
122    }
123
124    /**
125     * Sets the left-hand-side icon.
126     */
127    private void setIcon1(Drawable icon) {
128        setViewDrawable(mIcon1, icon);
129    }
130
131    /**
132     * Sets the right-hand-side icon.
133     */
134    private void setIcon2(Drawable icon) {
135        setViewDrawable(mIcon2, icon);
136    }
137
138    /**
139     * Sets the drawable in an image view, makes sure the view is only visible if there
140     * is a drawable.
141     */
142    private static void setViewDrawable(ImageView v, Drawable drawable) {
143        // Set the icon even if the drawable is null, since we need to clear any
144        // previous icon.
145        v.setImageDrawable(drawable);
146
147        if (drawable == null) {
148            v.setVisibility(View.GONE);
149        } else {
150            v.setVisibility(View.VISIBLE);
151
152            // This is a hack to get any animated drawables (like a 'working' spinner)
153            // to animate. You have to setVisible true on an AnimationDrawable to get
154            // it to start animating, but it must first have been false or else the
155            // call to setVisible will be ineffective. We need to clear up the story
156            // about animated drawables in the future, see http://b/1878430.
157            drawable.setVisible(false, false);
158            drawable.setVisible(true, false);
159        }
160    }
161
162}
163