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.SearchableSource;
21import com.android.quicksearchbox.Source;
22import com.android.quicksearchbox.Suggestion;
23
24import android.app.SearchableInfo;
25import android.content.Context;
26import android.net.Uri;
27import android.provider.ContactsContract;
28import android.text.TextUtils;
29import android.util.AttributeSet;
30import android.view.View;
31
32/**
33 * View for contacts appearing in the suggestions list.
34 */
35public class ContactSuggestionView extends DefaultSuggestionView {
36
37    private static final String VIEW_ID = "contact";
38
39    private ContactBadge mQuickContact;
40
41    public ContactSuggestionView(Context context, AttributeSet attrs, int defStyle) {
42        super(context, attrs, defStyle);
43    }
44
45    public ContactSuggestionView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47    }
48
49    public ContactSuggestionView(Context context) {
50        super(context);
51    }
52
53    @Override
54    protected void onFinishInflate() {
55        super.onFinishInflate();
56        mQuickContact = (ContactBadge) findViewById(R.id.icon1);
57    }
58
59    @Override
60    public void bindAsSuggestion(Suggestion suggestion, String userQuery) {
61        super.bindAsSuggestion(suggestion, userQuery);
62        mQuickContact.assignContactUri(Uri.parse(suggestion.getSuggestionIntentDataString()));
63        mQuickContact.setExtraOnClickListener(new ContactBadgeClickListener());
64    }
65
66    private class ContactBadgeClickListener implements View.OnClickListener {
67        public void onClick(View v) {
68            onSuggestionQuickContactClicked();
69        }
70    }
71
72    public static class Factory extends SuggestionViewInflater {
73        public Factory(Context context) {
74            super(VIEW_ID, ContactSuggestionView.class, R.layout.contact_suggestion, context);
75        }
76
77        @Override
78        public boolean canCreateView(Suggestion suggestion) {
79            Source source = suggestion.getSuggestionSource();
80            if (source instanceof SearchableSource) {
81                SearchableSource searchableSource = (SearchableSource) source;
82                return isSearchableContacts(searchableSource.getSearchableInfo());
83            }
84            return false;
85        }
86
87        protected boolean isSearchableContacts(SearchableInfo searchable) {
88            return TextUtils.equals(ContactsContract.AUTHORITY, searchable.getSuggestAuthority());
89        }
90    }
91}