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 com.android.quicksearchbox.Suggestion;
     19 import com.android.quicksearchbox.SuggestionCursor;
     20 
     21 import android.content.Context;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     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         addFactory(new ContactSuggestionView.Factory(context));
     43     }
     44 
     45     /**
     46      * Must only be called from the constructor
     47      */
     48     protected final void addFactory(SuggestionViewFactory factory) {
     49         mFactories.addFirst(factory);
     50     }
     51 
     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     public View getView(SuggestionCursor suggestion, String userQuery,
     64             View convertView, ViewGroup parent) {
     65         for (SuggestionViewFactory factory : mFactories) {
     66             if (factory.canCreateView(suggestion)) {
     67                 return factory.getView(suggestion, userQuery, convertView, parent);
     68             }
     69         }
     70         return mDefaultFactory.getView(suggestion, userQuery, convertView, parent);
     71     }
     72 
     73     public String getViewType(Suggestion suggestion) {
     74         for (SuggestionViewFactory factory : mFactories) {
     75             if (factory.canCreateView(suggestion)) {
     76                 return factory.getViewType(suggestion);
     77             }
     78         }
     79         return mDefaultFactory.getViewType(suggestion);
     80     }
     81 
     82     @Override
     83     public boolean canCreateView(Suggestion suggestion) {
     84         return true;
     85     }
     86 
     87 }
     88