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
17package com.android.contacts.editor;
18
19import android.content.Context;
20import android.net.Uri;
21import android.provider.ContactsContract.Contacts;
22import android.text.TextUtils;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28
29import com.android.contacts.ContactPhotoManager;
30import com.android.contacts.R;
31import com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
32
33/**
34 * A view that contains a name, picture and other data for a contact aggregation suggestion.
35 */
36public class AggregationSuggestionView extends LinearLayout {
37
38    public interface Listener {
39        /**
40         * Callback that passes the contact URI and raw contact ID to edit instead of the
41         * current contact.
42         */
43        void onEditAction(Uri contactLookupUri, long rawContactId);
44    }
45
46    private Listener mListener;
47    private Suggestion mSuggestion;
48
49    public AggregationSuggestionView(Context context) {
50        super(context);
51    }
52
53    public AggregationSuggestionView(Context context, AttributeSet attrs) {
54        super(context, attrs);
55    }
56
57    public AggregationSuggestionView(Context context, AttributeSet attrs, int defStyle) {
58        super(context, attrs, defStyle);
59    }
60
61    public void bindSuggestion(Suggestion suggestion) {
62        mSuggestion = suggestion;
63        final ContactPhotoManager.DefaultImageRequest
64                request = new ContactPhotoManager.DefaultImageRequest(
65                suggestion.name, String.valueOf(suggestion.rawContactId), /* isCircular = */ false);
66        final ImageView photoView = (ImageView) findViewById(
67                R.id.aggregation_suggestion_photo);
68        ContactPhotoManager.getInstance(getContext()).loadThumbnail(photoView,
69                suggestion.photoId,
70                /* darkTheme = */ false,
71                /* isCircular = */ false,
72                request);
73
74        final TextView name = (TextView) findViewById(R.id.aggregation_suggestion_name);
75        name.setText(suggestion.name);
76
77        final TextView data = (TextView) findViewById(R.id.aggregation_suggestion_data);
78        String dataText = null;
79        if (suggestion.nickname != null) {
80            dataText = suggestion.nickname;
81        } else if (suggestion.emailAddress != null) {
82            dataText = suggestion.emailAddress;
83        } else if (suggestion.phoneNumber != null) {
84            dataText = suggestion.phoneNumber;
85            // Phone numbers should always be in LTR mode.
86            data.setTextDirection(View.TEXT_DIRECTION_LTR);
87        }
88        data.setText(dataText);
89    }
90
91    public void setListener(Listener listener) {
92        mListener = listener;
93    }
94
95    public boolean handleItemClickEvent() {
96        if (mListener != null && isEnabled()) {
97            if (TextUtils.isEmpty(mSuggestion.contactLookupKey)) {
98                return false;
99            }
100            mListener.onEditAction(
101                    Contacts.getLookupUri(mSuggestion.contactId, mSuggestion.contactLookupKey),
102                    mSuggestion.rawContactId);
103            return true;
104        }
105        return false;
106    }
107}
108