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