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.Suggestion; 21 22import android.content.Context; 23import android.provider.ContactsContract; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27 28/** 29 * Inflates suggestion views. 30 */ 31public class SuggestionViewInflater implements SuggestionViewFactory { 32 33 // The suggestion view classes that may be returned by this factory. 34 private static final Class<?>[] SUGGESTION_VIEW_CLASSES = { 35 DefaultSuggestionView.class, 36 ContactSuggestionView.class, 37 }; 38 39 // The layout ids associated with each of the above classes. 40 private static final int[] SUGGESTION_VIEW_LAYOUTS = { 41 R.layout.suggestion, 42 R.layout.contact_suggestion, 43 }; 44 45 private static final String CONTACT_LOOKUP_URI 46 = ContactsContract.Contacts.CONTENT_LOOKUP_URI.toString(); 47 48 private final Context mContext; 49 50 public SuggestionViewInflater(Context context) { 51 mContext = context; 52 } 53 54 protected LayoutInflater getInflater() { 55 return (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 56 } 57 58 public int getSuggestionViewTypeCount() { 59 return SUGGESTION_VIEW_CLASSES.length; 60 } 61 62 public int getSuggestionViewType(Suggestion suggestion) { 63 return isContactSuggestion(suggestion) ? 1 : 0; 64 } 65 66 public SuggestionView getSuggestionView(int viewType, View convertView, 67 ViewGroup parentViewType) { 68 if (convertView == null || !convertView.getClass().equals( 69 SUGGESTION_VIEW_CLASSES[viewType])) { 70 int layoutId = SUGGESTION_VIEW_LAYOUTS[viewType]; 71 convertView = getInflater().inflate(layoutId, parentViewType, false); 72 } 73 return (SuggestionView) convertView; 74 } 75 76 private boolean isContactSuggestion(Suggestion suggestion) { 77 String intentData = suggestion.getSuggestionIntentDataString(); 78 return intentData != null && intentData.startsWith(CONTACT_LOOKUP_URI); 79 } 80} 81