SuggestionsView.java revision fdb80c2962c88ac62dcd7ee7f2fab1857b61506b
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.SuggestionPosition;
21
22import android.content.Context;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.View;
26import android.widget.FrameLayout;
27import android.widget.ListAdapter;
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 boolean mLimitSuggestionsToViewHeight;
39
40    public SuggestionsView(Context context, AttributeSet attrs) {
41        super(context, attrs);
42    }
43
44    @Override
45    public void setAdapter(ListAdapter adapter) {
46        if (!(adapter == null || adapter instanceof SuggestionsAdapter)){
47            throw new ClassCastException(
48                    "SuggestionsView adapter must be a SuggestionsAdapter (got " + adapter + ")");
49        }
50        super.setAdapter(adapter);
51        if (mLimitSuggestionsToViewHeight) {
52            setMaxPromotedByHeight();
53        }
54    }
55
56    @Override
57    public SuggestionsAdapter getAdapter() {
58        return (SuggestionsAdapter) super.getAdapter();
59    }
60
61
62    @Override
63    public void onFinishInflate() {
64        super.onFinishInflate();
65        setItemsCanFocus(true);
66    }
67
68    /**
69     * Gets the position of the selected suggestion.
70     *
71     * @return A 0-based index, or {@code -1} if no suggestion is selected.
72     */
73    public int getSelectedPosition() {
74        return getSelectedItemPosition();
75    }
76
77    /**
78     * Gets the selected suggestion.
79     *
80     * @return {@code null} if no suggestion is selected.
81     */
82    public SuggestionPosition getSelectedSuggestion() {
83        return (SuggestionPosition) getSelectedItem();
84    }
85
86    public void setLimitSuggestionsToViewHeight(boolean limit) {
87        mLimitSuggestionsToViewHeight = limit;
88        if (mLimitSuggestionsToViewHeight) {
89            setMaxPromotedByHeight();
90        }
91    }
92
93    @Override
94    protected void onSizeChanged (int w, int h, int oldw, int oldh) {
95        if (mLimitSuggestionsToViewHeight) {
96            setMaxPromotedByHeight();
97        }
98    }
99
100    private void setMaxPromotedByHeight() {
101        SuggestionsAdapter adapter = getAdapter();
102        if (adapter != null) {
103            float maxHeight;
104            if (getParent() instanceof FrameLayout) {
105                // We put the SuggestionView inside a frame layout so that we know what its
106                // maximum height is. Since this views height is set to 'wrap content' (in two-pane
107                // mode at least), we can't use our own height for these calculations.
108                maxHeight = ((View) getParent()).getHeight();
109                if (DBG) Log.d(TAG, "Parent height=" + maxHeight);
110            } else {
111                maxHeight = getHeight();
112                if (DBG) Log.d(TAG, "This height=" + maxHeight);
113            }
114            float suggestionHeight =
115                getContext().getResources().getDimension(R.dimen.suggestion_view_height);
116            if (suggestionHeight != 0) {
117                int suggestions = Math.max(1, (int) Math.floor(maxHeight / suggestionHeight));
118                if (DBG) {
119                    Log.d(TAG, "view height=" + maxHeight + " suggestion height=" +
120                            suggestionHeight + " -> maxSuggestions=" + suggestions);
121                }
122                adapter.setMaxPromoted(suggestions);
123            }
124        }
125    }
126
127}
128