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