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