Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 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 package com.android.quicksearchbox.ui;
     17 
     18 import android.content.Context;
     19 import android.view.View;
     20 import android.view.ViewGroup;
     21 
     22 import com.android.quicksearchbox.Suggestion;
     23 import com.android.quicksearchbox.SuggestionCursor;
     24 
     25 import java.util.Collection;
     26 import java.util.HashSet;
     27 import java.util.LinkedList;
     28 
     29 /**
     30  * Suggestion view factory for Google suggestions.
     31  */
     32 public class DefaultSuggestionViewFactory implements SuggestionViewFactory {
     33 
     34     private final LinkedList<SuggestionViewFactory> mFactories
     35             = new LinkedList<SuggestionViewFactory>();
     36     private final SuggestionViewFactory mDefaultFactory;
     37     private HashSet<String> mViewTypes;
     38 
     39     public DefaultSuggestionViewFactory(Context context) {
     40         mDefaultFactory = new DefaultSuggestionView.Factory(context);
     41         addFactory(new WebSearchSuggestionView.Factory(context));
     42     }
     43 
     44     /**
     45      * Must only be called from the constructor
     46      */
     47     protected final void addFactory(SuggestionViewFactory factory) {
     48         mFactories.addFirst(factory);
     49     }
     50 
     51     @Override
     52     public Collection<String> getSuggestionViewTypes() {
     53         if (mViewTypes == null) {
     54             mViewTypes = new HashSet<String>();
     55             mViewTypes.addAll(mDefaultFactory.getSuggestionViewTypes());
     56             for (SuggestionViewFactory factory : mFactories) {
     57                 mViewTypes.addAll(factory.getSuggestionViewTypes());
     58             }
     59         }
     60         return mViewTypes;
     61     }
     62 
     63     @Override
     64     public View getView(SuggestionCursor suggestion, String userQuery,
     65             View convertView, ViewGroup parent) {
     66         for (SuggestionViewFactory factory : mFactories) {
     67             if (factory.canCreateView(suggestion)) {
     68                 return factory.getView(suggestion, userQuery, convertView, parent);
     69             }
     70         }
     71         return mDefaultFactory.getView(suggestion, userQuery, convertView, parent);
     72     }
     73 
     74     @Override
     75     public String getViewType(Suggestion suggestion) {
     76         for (SuggestionViewFactory factory : mFactories) {
     77             if (factory.canCreateView(suggestion)) {
     78                 return factory.getViewType(suggestion);
     79             }
     80         }
     81         return mDefaultFactory.getViewType(suggestion);
     82     }
     83 
     84     @Override
     85     public boolean canCreateView(Suggestion suggestion) {
     86         return true;
     87     }
     88 
     89 }
     90