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 
17 package com.android.contacts.editor;
18 
19 import android.content.Context;
20 import android.graphics.BitmapFactory;
21 import android.net.Uri;
22 import android.provider.ContactsContract.Contacts;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.widget.ImageView;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 
29 import com.android.contacts.R;
30 import com.android.contacts.editor.AggregationSuggestionEngine.RawContact;
31 import com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
32 import com.android.contacts.common.ContactPhotoManager;
33 import com.android.contacts.common.model.AccountTypeManager;
34 import com.android.contacts.common.model.account.AccountType;
35 
36 import com.google.common.collect.Lists;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * A view that contains a name, picture and other data for a contact aggregation suggestion.
43  */
44 public class AggregationSuggestionView extends LinearLayout {
45 
46     public interface Listener {
47 
48         /**
49          * Callback that passes the contact ID to join with and, for convenience,
50          * also the list of constituent raw contact IDs to avoid a separate query
51          * for those.
52          */
onJoinAction(long contactId, List<Long> rawContacIds)53         public void onJoinAction(long contactId, List<Long> rawContacIds);
54 
55         /**
56          * Callback that passes the contact ID to edit instead of the current contact.
57          */
onEditAction(Uri contactLookupUri)58         public void onEditAction(Uri contactLookupUri);
59     }
60 
61     private Listener mListener;
62     private long mContactId;
63     private String mLookupKey;
64     private List<RawContact> mRawContacts = Lists.newArrayList();
65     private boolean mNewContact;
66 
AggregationSuggestionView(Context context)67     public AggregationSuggestionView(Context context) {
68         super(context);
69     }
70 
AggregationSuggestionView(Context context, AttributeSet attrs)71     public AggregationSuggestionView(Context context, AttributeSet attrs) {
72         super(context, attrs);
73     }
74 
AggregationSuggestionView(Context context, AttributeSet attrs, int defStyle)75     public AggregationSuggestionView(Context context, AttributeSet attrs, int defStyle) {
76         super(context, attrs, defStyle);
77     }
78 
setNewContact(boolean flag)79     public void setNewContact(boolean flag) {
80         mNewContact = flag;
81     }
82 
bindSuggestion(Suggestion suggestion)83     public void bindSuggestion(Suggestion suggestion) {
84         mContactId = suggestion.contactId;
85         mLookupKey = suggestion.lookupKey;
86         mRawContacts = suggestion.rawContacts;
87         ImageView photo = (ImageView) findViewById(R.id.aggregation_suggestion_photo);
88         if (suggestion.photo != null) {
89             photo.setImageBitmap(BitmapFactory.decodeByteArray(
90                     suggestion.photo, 0, suggestion.photo.length));
91         } else {
92             photo.setImageDrawable(ContactPhotoManager.getDefaultAvatarDrawableForContact(
93                     getResources(), false, null));
94         }
95 
96         TextView name = (TextView) findViewById(R.id.aggregation_suggestion_name);
97         name.setText(suggestion.name);
98 
99         TextView data = (TextView) findViewById(R.id.aggregation_suggestion_data);
100         String dataText = null;
101         if (suggestion.nickname != null) {
102             dataText = suggestion.nickname;
103         } else if (suggestion.emailAddress != null) {
104             dataText = suggestion.emailAddress;
105         } else if (suggestion.phoneNumber != null) {
106             dataText = suggestion.phoneNumber;
107         }
108         data.setText(dataText);
109     }
110 
111     /**
112      * Returns true if the suggested contact can be edited.
113      */
canEditSuggestedContact()114     private boolean canEditSuggestedContact() {
115         if (!mNewContact) {
116             return false;
117         }
118 
119         AccountTypeManager accountTypes = AccountTypeManager.getInstance(getContext());
120         for (RawContact rawContact : mRawContacts) {
121             String accountType = rawContact.accountType;
122             String dataSet = rawContact.dataSet;
123             if (accountType == null) {
124                 return true;
125             }
126             AccountType type = accountTypes.getAccountType(accountType, dataSet);
127             if (type.areContactsWritable()) {
128                 return true;
129             }
130         }
131 
132         return false;
133     }
134 
setListener(Listener listener)135     public void setListener(Listener listener) {
136         mListener = listener;
137     }
138 
handleItemClickEvent()139     public boolean handleItemClickEvent() {
140         if (mListener != null && isEnabled()) {
141             if (canEditSuggestedContact()) {
142                 if (TextUtils.isEmpty(mLookupKey)) {
143                     return false;
144                 }
145                 mListener.onEditAction(Contacts.getLookupUri(mContactId, mLookupKey));
146             } else {
147                 ArrayList<Long> rawContactIds = Lists.newArrayList();
148                 for (RawContact rawContact : mRawContacts) {
149                     rawContactIds.add(rawContact.rawContactId);
150                 }
151                 mListener.onJoinAction(mContactId, rawContactIds);
152             }
153             return true;
154         }
155         return false;
156     }
157 }
158