SuggestionViewInflater.java revision fde948e69f59589cf0d217ea414af7947de600bb
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 android.content.ContentResolver;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.net.Uri;
23import android.provider.ContactsContract;
24import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import com.android.quicksearchbox.R;
29import com.android.quicksearchbox.SuggestionCursor;
30
31/**
32 * Inflates suggestion views.
33 */
34public class SuggestionViewInflater implements SuggestionViewFactory {
35
36    private static final boolean DBG = false;
37    private static final String TAG = "QSB.SuggestionViewInflater";
38
39    // The suggestion view classes that may be returned by this factory.
40    private static final Class<?>[] SUGGESTION_VIEW_CLASSES = {
41            DefaultSuggestionView.class,
42            ContactSuggestionView.class,
43    };
44
45    // The layout ids associated with each of the above classes.
46    private static final int[] SUGGESTION_VIEW_LAYOUTS = {
47            R.layout.suggestion,
48            R.layout.contact_suggestion,
49    };
50
51    private static final String CONTACT_LOOKUP_URI
52            = ContactsContract.Contacts.CONTENT_LOOKUP_URI.toString();
53
54    private final Context mContext;
55
56    public SuggestionViewInflater(Context context) {
57        mContext = context;
58    }
59
60    protected LayoutInflater getInflater() {
61        return (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
62    }
63
64    public int getSuggestionViewTypeCount() {
65        return SUGGESTION_VIEW_CLASSES.length;
66    }
67
68    public int getSuggestionViewType(SuggestionCursor suggestion) {
69        return isContactSuggestion(suggestion) ? 1 : 0;
70    }
71
72    public SuggestionView getSuggestionView(int viewType, View convertView,
73            ViewGroup parentViewType) {
74        if (convertView == null || !convertView.getClass().equals(
75                SUGGESTION_VIEW_CLASSES[viewType])) {
76            int layoutId = SUGGESTION_VIEW_LAYOUTS[viewType];
77            convertView = getInflater().inflate(layoutId, parentViewType, false);
78        }
79        return (SuggestionView) convertView;
80    }
81
82    public CorpusView createSourceView(ViewGroup parentViewType) {
83        if (DBG) Log.d(TAG, "createSourceView()");
84        CorpusView view = (CorpusView)
85                getInflater().inflate(R.layout.corpus_grid_item, parentViewType, false);
86        return view;
87    }
88
89    public String getGlobalSearchLabel() {
90        return mContext.getString(R.string.corpus_label_global);
91    }
92
93    public Drawable getGlobalSearchIcon() {
94        return mContext.getResources().getDrawable(R.drawable.corpus_icon_global);
95    }
96
97    public Uri getGlobalSearchIconUri() {
98        return new Uri.Builder()
99                .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
100                .authority(mContext.getPackageName())
101                .appendEncodedPath(String.valueOf(R.drawable.corpus_icon_global))
102                .build();
103    }
104
105    private boolean isContactSuggestion(SuggestionCursor suggestion) {
106        String intentData = suggestion.getSuggestionIntentDataString();
107        return intentData != null && intentData.startsWith(CONTACT_LOOKUP_URI);
108    }
109}
110